i40e_main.c 400 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include <linux/etherdevice.h>
  4. #include <linux/of_net.h>
  5. #include <linux/pci.h>
  6. #include <linux/bpf.h>
  7. /* Local includes */
  8. #include "i40e.h"
  9. #include "i40e_diag.h"
  10. #include <net/udp_tunnel.h>
  11. /* All i40e tracepoints are defined by the include below, which
  12. * must be included exactly once across the whole kernel with
  13. * CREATE_TRACE_POINTS defined
  14. */
  15. #define CREATE_TRACE_POINTS
  16. #include "i40e_trace.h"
  17. const char i40e_driver_name[] = "i40e";
  18. static const char i40e_driver_string[] =
  19. "Intel(R) Ethernet Connection XL710 Network Driver";
  20. #define DRV_KERN "-k"
  21. #define DRV_VERSION_MAJOR 2
  22. #define DRV_VERSION_MINOR 3
  23. #define DRV_VERSION_BUILD 2
  24. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  25. __stringify(DRV_VERSION_MINOR) "." \
  26. __stringify(DRV_VERSION_BUILD) DRV_KERN
  27. const char i40e_driver_version_str[] = DRV_VERSION;
  28. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  29. /* a bit of forward declarations */
  30. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  31. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
  32. static int i40e_add_vsi(struct i40e_vsi *vsi);
  33. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  34. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  35. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  36. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  37. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  38. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
  39. static int i40e_reset(struct i40e_pf *pf);
  40. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  41. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  42. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  43. static int i40e_get_capabilities(struct i40e_pf *pf,
  44. enum i40e_admin_queue_opc list_type);
  45. /* i40e_pci_tbl - PCI Device ID Table
  46. *
  47. * Last entry must be all 0s
  48. *
  49. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  50. * Class, Class Mask, private data (not used) }
  51. */
  52. static const struct pci_device_id i40e_pci_tbl[] = {
  53. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  54. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  55. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  56. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  57. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  58. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  59. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  60. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  61. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  62. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  63. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  71. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  72. /* required last entry */
  73. {0, }
  74. };
  75. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  76. #define I40E_MAX_VF_COUNT 128
  77. static int debug = -1;
  78. module_param(debug, uint, 0);
  79. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  80. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  81. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  82. MODULE_LICENSE("GPL");
  83. MODULE_VERSION(DRV_VERSION);
  84. static struct workqueue_struct *i40e_wq;
  85. /**
  86. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  87. * @hw: pointer to the HW structure
  88. * @mem: ptr to mem struct to fill out
  89. * @size: size of memory requested
  90. * @alignment: what to align the allocation to
  91. **/
  92. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  93. u64 size, u32 alignment)
  94. {
  95. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  96. mem->size = ALIGN(size, alignment);
  97. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  98. &mem->pa, GFP_KERNEL);
  99. if (!mem->va)
  100. return -ENOMEM;
  101. return 0;
  102. }
  103. /**
  104. * i40e_free_dma_mem_d - OS specific memory free for shared code
  105. * @hw: pointer to the HW structure
  106. * @mem: ptr to mem struct to free
  107. **/
  108. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  109. {
  110. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  111. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  112. mem->va = NULL;
  113. mem->pa = 0;
  114. mem->size = 0;
  115. return 0;
  116. }
  117. /**
  118. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  119. * @hw: pointer to the HW structure
  120. * @mem: ptr to mem struct to fill out
  121. * @size: size of memory requested
  122. **/
  123. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  124. u32 size)
  125. {
  126. mem->size = size;
  127. mem->va = kzalloc(size, GFP_KERNEL);
  128. if (!mem->va)
  129. return -ENOMEM;
  130. return 0;
  131. }
  132. /**
  133. * i40e_free_virt_mem_d - OS specific memory free for shared code
  134. * @hw: pointer to the HW structure
  135. * @mem: ptr to mem struct to free
  136. **/
  137. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  138. {
  139. /* it's ok to kfree a NULL pointer */
  140. kfree(mem->va);
  141. mem->va = NULL;
  142. mem->size = 0;
  143. return 0;
  144. }
  145. /**
  146. * i40e_get_lump - find a lump of free generic resource
  147. * @pf: board private structure
  148. * @pile: the pile of resource to search
  149. * @needed: the number of items needed
  150. * @id: an owner id to stick on the items assigned
  151. *
  152. * Returns the base item index of the lump, or negative for error
  153. *
  154. * The search_hint trick and lack of advanced fit-finding only work
  155. * because we're highly likely to have all the same size lump requests.
  156. * Linear search time and any fragmentation should be minimal.
  157. **/
  158. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  159. u16 needed, u16 id)
  160. {
  161. int ret = -ENOMEM;
  162. int i, j;
  163. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  164. dev_info(&pf->pdev->dev,
  165. "param err: pile=%s needed=%d id=0x%04x\n",
  166. pile ? "<valid>" : "<null>", needed, id);
  167. return -EINVAL;
  168. }
  169. /* start the linear search with an imperfect hint */
  170. i = pile->search_hint;
  171. while (i < pile->num_entries) {
  172. /* skip already allocated entries */
  173. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  174. i++;
  175. continue;
  176. }
  177. /* do we have enough in this lump? */
  178. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  179. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  180. break;
  181. }
  182. if (j == needed) {
  183. /* there was enough, so assign it to the requestor */
  184. for (j = 0; j < needed; j++)
  185. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  186. ret = i;
  187. pile->search_hint = i + j;
  188. break;
  189. }
  190. /* not enough, so skip over it and continue looking */
  191. i += j;
  192. }
  193. return ret;
  194. }
  195. /**
  196. * i40e_put_lump - return a lump of generic resource
  197. * @pile: the pile of resource to search
  198. * @index: the base item index
  199. * @id: the owner id of the items assigned
  200. *
  201. * Returns the count of items in the lump
  202. **/
  203. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  204. {
  205. int valid_id = (id | I40E_PILE_VALID_BIT);
  206. int count = 0;
  207. int i;
  208. if (!pile || index >= pile->num_entries)
  209. return -EINVAL;
  210. for (i = index;
  211. i < pile->num_entries && pile->list[i] == valid_id;
  212. i++) {
  213. pile->list[i] = 0;
  214. count++;
  215. }
  216. if (count && index < pile->search_hint)
  217. pile->search_hint = index;
  218. return count;
  219. }
  220. /**
  221. * i40e_find_vsi_from_id - searches for the vsi with the given id
  222. * @pf: the pf structure to search for the vsi
  223. * @id: id of the vsi it is searching for
  224. **/
  225. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  226. {
  227. int i;
  228. for (i = 0; i < pf->num_alloc_vsi; i++)
  229. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  230. return pf->vsi[i];
  231. return NULL;
  232. }
  233. /**
  234. * i40e_service_event_schedule - Schedule the service task to wake up
  235. * @pf: board private structure
  236. *
  237. * If not already scheduled, this puts the task into the work queue
  238. **/
  239. void i40e_service_event_schedule(struct i40e_pf *pf)
  240. {
  241. if (!test_bit(__I40E_DOWN, pf->state) &&
  242. !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  243. queue_work(i40e_wq, &pf->service_task);
  244. }
  245. /**
  246. * i40e_tx_timeout - Respond to a Tx Hang
  247. * @netdev: network interface device structure
  248. *
  249. * If any port has noticed a Tx timeout, it is likely that the whole
  250. * device is munged, not just the one netdev port, so go for the full
  251. * reset.
  252. **/
  253. static void i40e_tx_timeout(struct net_device *netdev)
  254. {
  255. struct i40e_netdev_priv *np = netdev_priv(netdev);
  256. struct i40e_vsi *vsi = np->vsi;
  257. struct i40e_pf *pf = vsi->back;
  258. struct i40e_ring *tx_ring = NULL;
  259. unsigned int i, hung_queue = 0;
  260. u32 head, val;
  261. pf->tx_timeout_count++;
  262. /* find the stopped queue the same way the stack does */
  263. for (i = 0; i < netdev->num_tx_queues; i++) {
  264. struct netdev_queue *q;
  265. unsigned long trans_start;
  266. q = netdev_get_tx_queue(netdev, i);
  267. trans_start = q->trans_start;
  268. if (netif_xmit_stopped(q) &&
  269. time_after(jiffies,
  270. (trans_start + netdev->watchdog_timeo))) {
  271. hung_queue = i;
  272. break;
  273. }
  274. }
  275. if (i == netdev->num_tx_queues) {
  276. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  277. } else {
  278. /* now that we have an index, find the tx_ring struct */
  279. for (i = 0; i < vsi->num_queue_pairs; i++) {
  280. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  281. if (hung_queue ==
  282. vsi->tx_rings[i]->queue_index) {
  283. tx_ring = vsi->tx_rings[i];
  284. break;
  285. }
  286. }
  287. }
  288. }
  289. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  290. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  291. else if (time_before(jiffies,
  292. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  293. return; /* don't do any new action before the next timeout */
  294. if (tx_ring) {
  295. head = i40e_get_head(tx_ring);
  296. /* Read interrupt register */
  297. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  298. val = rd32(&pf->hw,
  299. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  300. tx_ring->vsi->base_vector - 1));
  301. else
  302. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  303. netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
  304. vsi->seid, hung_queue, tx_ring->next_to_clean,
  305. head, tx_ring->next_to_use,
  306. readl(tx_ring->tail), val);
  307. }
  308. pf->tx_timeout_last_recovery = jiffies;
  309. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  310. pf->tx_timeout_recovery_level, hung_queue);
  311. switch (pf->tx_timeout_recovery_level) {
  312. case 1:
  313. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  314. break;
  315. case 2:
  316. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  317. break;
  318. case 3:
  319. set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  320. break;
  321. default:
  322. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  323. break;
  324. }
  325. i40e_service_event_schedule(pf);
  326. pf->tx_timeout_recovery_level++;
  327. }
  328. /**
  329. * i40e_get_vsi_stats_struct - Get System Network Statistics
  330. * @vsi: the VSI we care about
  331. *
  332. * Returns the address of the device statistics structure.
  333. * The statistics are actually updated from the service task.
  334. **/
  335. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  336. {
  337. return &vsi->net_stats;
  338. }
  339. /**
  340. * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
  341. * @ring: Tx ring to get statistics from
  342. * @stats: statistics entry to be updated
  343. **/
  344. static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
  345. struct rtnl_link_stats64 *stats)
  346. {
  347. u64 bytes, packets;
  348. unsigned int start;
  349. do {
  350. start = u64_stats_fetch_begin_irq(&ring->syncp);
  351. packets = ring->stats.packets;
  352. bytes = ring->stats.bytes;
  353. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  354. stats->tx_packets += packets;
  355. stats->tx_bytes += bytes;
  356. }
  357. /**
  358. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  359. * @netdev: network interface device structure
  360. * @stats: data structure to store statistics
  361. *
  362. * Returns the address of the device statistics structure.
  363. * The statistics are actually updated from the service task.
  364. **/
  365. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  366. struct rtnl_link_stats64 *stats)
  367. {
  368. struct i40e_netdev_priv *np = netdev_priv(netdev);
  369. struct i40e_ring *tx_ring, *rx_ring;
  370. struct i40e_vsi *vsi = np->vsi;
  371. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  372. int i;
  373. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  374. return;
  375. if (!vsi->tx_rings)
  376. return;
  377. rcu_read_lock();
  378. for (i = 0; i < vsi->num_queue_pairs; i++) {
  379. u64 bytes, packets;
  380. unsigned int start;
  381. tx_ring = READ_ONCE(vsi->tx_rings[i]);
  382. if (!tx_ring)
  383. continue;
  384. i40e_get_netdev_stats_struct_tx(tx_ring, stats);
  385. rx_ring = &tx_ring[1];
  386. do {
  387. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  388. packets = rx_ring->stats.packets;
  389. bytes = rx_ring->stats.bytes;
  390. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  391. stats->rx_packets += packets;
  392. stats->rx_bytes += bytes;
  393. if (i40e_enabled_xdp_vsi(vsi))
  394. i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
  395. }
  396. rcu_read_unlock();
  397. /* following stats updated by i40e_watchdog_subtask() */
  398. stats->multicast = vsi_stats->multicast;
  399. stats->tx_errors = vsi_stats->tx_errors;
  400. stats->tx_dropped = vsi_stats->tx_dropped;
  401. stats->rx_errors = vsi_stats->rx_errors;
  402. stats->rx_dropped = vsi_stats->rx_dropped;
  403. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  404. stats->rx_length_errors = vsi_stats->rx_length_errors;
  405. }
  406. /**
  407. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  408. * @vsi: the VSI to have its stats reset
  409. **/
  410. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  411. {
  412. struct rtnl_link_stats64 *ns;
  413. int i;
  414. if (!vsi)
  415. return;
  416. ns = i40e_get_vsi_stats_struct(vsi);
  417. memset(ns, 0, sizeof(*ns));
  418. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  419. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  420. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  421. if (vsi->rx_rings && vsi->rx_rings[0]) {
  422. for (i = 0; i < vsi->num_queue_pairs; i++) {
  423. memset(&vsi->rx_rings[i]->stats, 0,
  424. sizeof(vsi->rx_rings[i]->stats));
  425. memset(&vsi->rx_rings[i]->rx_stats, 0,
  426. sizeof(vsi->rx_rings[i]->rx_stats));
  427. memset(&vsi->tx_rings[i]->stats, 0,
  428. sizeof(vsi->tx_rings[i]->stats));
  429. memset(&vsi->tx_rings[i]->tx_stats, 0,
  430. sizeof(vsi->tx_rings[i]->tx_stats));
  431. }
  432. }
  433. vsi->stat_offsets_loaded = false;
  434. }
  435. /**
  436. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  437. * @pf: the PF to be reset
  438. **/
  439. void i40e_pf_reset_stats(struct i40e_pf *pf)
  440. {
  441. int i;
  442. memset(&pf->stats, 0, sizeof(pf->stats));
  443. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  444. pf->stat_offsets_loaded = false;
  445. for (i = 0; i < I40E_MAX_VEB; i++) {
  446. if (pf->veb[i]) {
  447. memset(&pf->veb[i]->stats, 0,
  448. sizeof(pf->veb[i]->stats));
  449. memset(&pf->veb[i]->stats_offsets, 0,
  450. sizeof(pf->veb[i]->stats_offsets));
  451. pf->veb[i]->stat_offsets_loaded = false;
  452. }
  453. }
  454. pf->hw_csum_rx_error = 0;
  455. }
  456. /**
  457. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  458. * @hw: ptr to the hardware info
  459. * @hireg: the high 32 bit reg to read
  460. * @loreg: the low 32 bit reg to read
  461. * @offset_loaded: has the initial offset been loaded yet
  462. * @offset: ptr to current offset value
  463. * @stat: ptr to the stat
  464. *
  465. * Since the device stats are not reset at PFReset, they likely will not
  466. * be zeroed when the driver starts. We'll save the first values read
  467. * and use them as offsets to be subtracted from the raw values in order
  468. * to report stats that count from zero. In the process, we also manage
  469. * the potential roll-over.
  470. **/
  471. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  472. bool offset_loaded, u64 *offset, u64 *stat)
  473. {
  474. u64 new_data;
  475. if (hw->device_id == I40E_DEV_ID_QEMU) {
  476. new_data = rd32(hw, loreg);
  477. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  478. } else {
  479. new_data = rd64(hw, loreg);
  480. }
  481. if (!offset_loaded)
  482. *offset = new_data;
  483. if (likely(new_data >= *offset))
  484. *stat = new_data - *offset;
  485. else
  486. *stat = (new_data + BIT_ULL(48)) - *offset;
  487. *stat &= 0xFFFFFFFFFFFFULL;
  488. }
  489. /**
  490. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  491. * @hw: ptr to the hardware info
  492. * @reg: the hw reg to read
  493. * @offset_loaded: has the initial offset been loaded yet
  494. * @offset: ptr to current offset value
  495. * @stat: ptr to the stat
  496. **/
  497. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  498. bool offset_loaded, u64 *offset, u64 *stat)
  499. {
  500. u32 new_data;
  501. new_data = rd32(hw, reg);
  502. if (!offset_loaded)
  503. *offset = new_data;
  504. if (likely(new_data >= *offset))
  505. *stat = (u32)(new_data - *offset);
  506. else
  507. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  508. }
  509. /**
  510. * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
  511. * @hw: ptr to the hardware info
  512. * @reg: the hw reg to read and clear
  513. * @stat: ptr to the stat
  514. **/
  515. static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
  516. {
  517. u32 new_data = rd32(hw, reg);
  518. wr32(hw, reg, 1); /* must write a nonzero value to clear register */
  519. *stat += new_data;
  520. }
  521. /**
  522. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  523. * @vsi: the VSI to be updated
  524. **/
  525. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  526. {
  527. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  528. struct i40e_pf *pf = vsi->back;
  529. struct i40e_hw *hw = &pf->hw;
  530. struct i40e_eth_stats *oes;
  531. struct i40e_eth_stats *es; /* device's eth stats */
  532. es = &vsi->eth_stats;
  533. oes = &vsi->eth_stats_offsets;
  534. /* Gather up the stats that the hw collects */
  535. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  536. vsi->stat_offsets_loaded,
  537. &oes->tx_errors, &es->tx_errors);
  538. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  539. vsi->stat_offsets_loaded,
  540. &oes->rx_discards, &es->rx_discards);
  541. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  542. vsi->stat_offsets_loaded,
  543. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  544. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  545. vsi->stat_offsets_loaded,
  546. &oes->tx_errors, &es->tx_errors);
  547. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  548. I40E_GLV_GORCL(stat_idx),
  549. vsi->stat_offsets_loaded,
  550. &oes->rx_bytes, &es->rx_bytes);
  551. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  552. I40E_GLV_UPRCL(stat_idx),
  553. vsi->stat_offsets_loaded,
  554. &oes->rx_unicast, &es->rx_unicast);
  555. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  556. I40E_GLV_MPRCL(stat_idx),
  557. vsi->stat_offsets_loaded,
  558. &oes->rx_multicast, &es->rx_multicast);
  559. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  560. I40E_GLV_BPRCL(stat_idx),
  561. vsi->stat_offsets_loaded,
  562. &oes->rx_broadcast, &es->rx_broadcast);
  563. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  564. I40E_GLV_GOTCL(stat_idx),
  565. vsi->stat_offsets_loaded,
  566. &oes->tx_bytes, &es->tx_bytes);
  567. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  568. I40E_GLV_UPTCL(stat_idx),
  569. vsi->stat_offsets_loaded,
  570. &oes->tx_unicast, &es->tx_unicast);
  571. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  572. I40E_GLV_MPTCL(stat_idx),
  573. vsi->stat_offsets_loaded,
  574. &oes->tx_multicast, &es->tx_multicast);
  575. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  576. I40E_GLV_BPTCL(stat_idx),
  577. vsi->stat_offsets_loaded,
  578. &oes->tx_broadcast, &es->tx_broadcast);
  579. vsi->stat_offsets_loaded = true;
  580. }
  581. /**
  582. * i40e_update_veb_stats - Update Switch component statistics
  583. * @veb: the VEB being updated
  584. **/
  585. static void i40e_update_veb_stats(struct i40e_veb *veb)
  586. {
  587. struct i40e_pf *pf = veb->pf;
  588. struct i40e_hw *hw = &pf->hw;
  589. struct i40e_eth_stats *oes;
  590. struct i40e_eth_stats *es; /* device's eth stats */
  591. struct i40e_veb_tc_stats *veb_oes;
  592. struct i40e_veb_tc_stats *veb_es;
  593. int i, idx = 0;
  594. idx = veb->stats_idx;
  595. es = &veb->stats;
  596. oes = &veb->stats_offsets;
  597. veb_es = &veb->tc_stats;
  598. veb_oes = &veb->tc_stats_offsets;
  599. /* Gather up the stats that the hw collects */
  600. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  601. veb->stat_offsets_loaded,
  602. &oes->tx_discards, &es->tx_discards);
  603. if (hw->revision_id > 0)
  604. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  605. veb->stat_offsets_loaded,
  606. &oes->rx_unknown_protocol,
  607. &es->rx_unknown_protocol);
  608. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  609. veb->stat_offsets_loaded,
  610. &oes->rx_bytes, &es->rx_bytes);
  611. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  612. veb->stat_offsets_loaded,
  613. &oes->rx_unicast, &es->rx_unicast);
  614. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  615. veb->stat_offsets_loaded,
  616. &oes->rx_multicast, &es->rx_multicast);
  617. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  618. veb->stat_offsets_loaded,
  619. &oes->rx_broadcast, &es->rx_broadcast);
  620. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  621. veb->stat_offsets_loaded,
  622. &oes->tx_bytes, &es->tx_bytes);
  623. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  624. veb->stat_offsets_loaded,
  625. &oes->tx_unicast, &es->tx_unicast);
  626. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  627. veb->stat_offsets_loaded,
  628. &oes->tx_multicast, &es->tx_multicast);
  629. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  630. veb->stat_offsets_loaded,
  631. &oes->tx_broadcast, &es->tx_broadcast);
  632. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  633. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  634. I40E_GLVEBTC_RPCL(i, idx),
  635. veb->stat_offsets_loaded,
  636. &veb_oes->tc_rx_packets[i],
  637. &veb_es->tc_rx_packets[i]);
  638. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  639. I40E_GLVEBTC_RBCL(i, idx),
  640. veb->stat_offsets_loaded,
  641. &veb_oes->tc_rx_bytes[i],
  642. &veb_es->tc_rx_bytes[i]);
  643. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  644. I40E_GLVEBTC_TPCL(i, idx),
  645. veb->stat_offsets_loaded,
  646. &veb_oes->tc_tx_packets[i],
  647. &veb_es->tc_tx_packets[i]);
  648. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  649. I40E_GLVEBTC_TBCL(i, idx),
  650. veb->stat_offsets_loaded,
  651. &veb_oes->tc_tx_bytes[i],
  652. &veb_es->tc_tx_bytes[i]);
  653. }
  654. veb->stat_offsets_loaded = true;
  655. }
  656. /**
  657. * i40e_update_vsi_stats - Update the vsi statistics counters.
  658. * @vsi: the VSI to be updated
  659. *
  660. * There are a few instances where we store the same stat in a
  661. * couple of different structs. This is partly because we have
  662. * the netdev stats that need to be filled out, which is slightly
  663. * different from the "eth_stats" defined by the chip and used in
  664. * VF communications. We sort it out here.
  665. **/
  666. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  667. {
  668. struct i40e_pf *pf = vsi->back;
  669. struct rtnl_link_stats64 *ons;
  670. struct rtnl_link_stats64 *ns; /* netdev stats */
  671. struct i40e_eth_stats *oes;
  672. struct i40e_eth_stats *es; /* device's eth stats */
  673. u32 tx_restart, tx_busy;
  674. struct i40e_ring *p;
  675. u32 rx_page, rx_buf;
  676. u64 bytes, packets;
  677. unsigned int start;
  678. u64 tx_linearize;
  679. u64 tx_force_wb;
  680. u64 rx_p, rx_b;
  681. u64 tx_p, tx_b;
  682. u16 q;
  683. if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  684. test_bit(__I40E_CONFIG_BUSY, pf->state))
  685. return;
  686. ns = i40e_get_vsi_stats_struct(vsi);
  687. ons = &vsi->net_stats_offsets;
  688. es = &vsi->eth_stats;
  689. oes = &vsi->eth_stats_offsets;
  690. /* Gather up the netdev and vsi stats that the driver collects
  691. * on the fly during packet processing
  692. */
  693. rx_b = rx_p = 0;
  694. tx_b = tx_p = 0;
  695. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  696. rx_page = 0;
  697. rx_buf = 0;
  698. rcu_read_lock();
  699. for (q = 0; q < vsi->num_queue_pairs; q++) {
  700. /* locate Tx ring */
  701. p = READ_ONCE(vsi->tx_rings[q]);
  702. do {
  703. start = u64_stats_fetch_begin_irq(&p->syncp);
  704. packets = p->stats.packets;
  705. bytes = p->stats.bytes;
  706. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  707. tx_b += bytes;
  708. tx_p += packets;
  709. tx_restart += p->tx_stats.restart_queue;
  710. tx_busy += p->tx_stats.tx_busy;
  711. tx_linearize += p->tx_stats.tx_linearize;
  712. tx_force_wb += p->tx_stats.tx_force_wb;
  713. /* Rx queue is part of the same block as Tx queue */
  714. p = &p[1];
  715. do {
  716. start = u64_stats_fetch_begin_irq(&p->syncp);
  717. packets = p->stats.packets;
  718. bytes = p->stats.bytes;
  719. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  720. rx_b += bytes;
  721. rx_p += packets;
  722. rx_buf += p->rx_stats.alloc_buff_failed;
  723. rx_page += p->rx_stats.alloc_page_failed;
  724. }
  725. rcu_read_unlock();
  726. vsi->tx_restart = tx_restart;
  727. vsi->tx_busy = tx_busy;
  728. vsi->tx_linearize = tx_linearize;
  729. vsi->tx_force_wb = tx_force_wb;
  730. vsi->rx_page_failed = rx_page;
  731. vsi->rx_buf_failed = rx_buf;
  732. ns->rx_packets = rx_p;
  733. ns->rx_bytes = rx_b;
  734. ns->tx_packets = tx_p;
  735. ns->tx_bytes = tx_b;
  736. /* update netdev stats from eth stats */
  737. i40e_update_eth_stats(vsi);
  738. ons->tx_errors = oes->tx_errors;
  739. ns->tx_errors = es->tx_errors;
  740. ons->multicast = oes->rx_multicast;
  741. ns->multicast = es->rx_multicast;
  742. ons->rx_dropped = oes->rx_discards;
  743. ns->rx_dropped = es->rx_discards;
  744. ons->tx_dropped = oes->tx_discards;
  745. ns->tx_dropped = es->tx_discards;
  746. /* pull in a couple PF stats if this is the main vsi */
  747. if (vsi == pf->vsi[pf->lan_vsi]) {
  748. ns->rx_crc_errors = pf->stats.crc_errors;
  749. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  750. ns->rx_length_errors = pf->stats.rx_length_errors;
  751. }
  752. }
  753. /**
  754. * i40e_update_pf_stats - Update the PF statistics counters.
  755. * @pf: the PF to be updated
  756. **/
  757. static void i40e_update_pf_stats(struct i40e_pf *pf)
  758. {
  759. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  760. struct i40e_hw_port_stats *nsd = &pf->stats;
  761. struct i40e_hw *hw = &pf->hw;
  762. u32 val;
  763. int i;
  764. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  765. I40E_GLPRT_GORCL(hw->port),
  766. pf->stat_offsets_loaded,
  767. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  768. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  769. I40E_GLPRT_GOTCL(hw->port),
  770. pf->stat_offsets_loaded,
  771. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  772. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  773. pf->stat_offsets_loaded,
  774. &osd->eth.rx_discards,
  775. &nsd->eth.rx_discards);
  776. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  777. I40E_GLPRT_UPRCL(hw->port),
  778. pf->stat_offsets_loaded,
  779. &osd->eth.rx_unicast,
  780. &nsd->eth.rx_unicast);
  781. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  782. I40E_GLPRT_MPRCL(hw->port),
  783. pf->stat_offsets_loaded,
  784. &osd->eth.rx_multicast,
  785. &nsd->eth.rx_multicast);
  786. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  787. I40E_GLPRT_BPRCL(hw->port),
  788. pf->stat_offsets_loaded,
  789. &osd->eth.rx_broadcast,
  790. &nsd->eth.rx_broadcast);
  791. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  792. I40E_GLPRT_UPTCL(hw->port),
  793. pf->stat_offsets_loaded,
  794. &osd->eth.tx_unicast,
  795. &nsd->eth.tx_unicast);
  796. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  797. I40E_GLPRT_MPTCL(hw->port),
  798. pf->stat_offsets_loaded,
  799. &osd->eth.tx_multicast,
  800. &nsd->eth.tx_multicast);
  801. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  802. I40E_GLPRT_BPTCL(hw->port),
  803. pf->stat_offsets_loaded,
  804. &osd->eth.tx_broadcast,
  805. &nsd->eth.tx_broadcast);
  806. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  807. pf->stat_offsets_loaded,
  808. &osd->tx_dropped_link_down,
  809. &nsd->tx_dropped_link_down);
  810. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  811. pf->stat_offsets_loaded,
  812. &osd->crc_errors, &nsd->crc_errors);
  813. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  814. pf->stat_offsets_loaded,
  815. &osd->illegal_bytes, &nsd->illegal_bytes);
  816. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  817. pf->stat_offsets_loaded,
  818. &osd->mac_local_faults,
  819. &nsd->mac_local_faults);
  820. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  821. pf->stat_offsets_loaded,
  822. &osd->mac_remote_faults,
  823. &nsd->mac_remote_faults);
  824. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  825. pf->stat_offsets_loaded,
  826. &osd->rx_length_errors,
  827. &nsd->rx_length_errors);
  828. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  829. pf->stat_offsets_loaded,
  830. &osd->link_xon_rx, &nsd->link_xon_rx);
  831. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  832. pf->stat_offsets_loaded,
  833. &osd->link_xon_tx, &nsd->link_xon_tx);
  834. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  835. pf->stat_offsets_loaded,
  836. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  837. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  838. pf->stat_offsets_loaded,
  839. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  840. for (i = 0; i < 8; i++) {
  841. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  842. pf->stat_offsets_loaded,
  843. &osd->priority_xoff_rx[i],
  844. &nsd->priority_xoff_rx[i]);
  845. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  846. pf->stat_offsets_loaded,
  847. &osd->priority_xon_rx[i],
  848. &nsd->priority_xon_rx[i]);
  849. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  850. pf->stat_offsets_loaded,
  851. &osd->priority_xon_tx[i],
  852. &nsd->priority_xon_tx[i]);
  853. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  854. pf->stat_offsets_loaded,
  855. &osd->priority_xoff_tx[i],
  856. &nsd->priority_xoff_tx[i]);
  857. i40e_stat_update32(hw,
  858. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  859. pf->stat_offsets_loaded,
  860. &osd->priority_xon_2_xoff[i],
  861. &nsd->priority_xon_2_xoff[i]);
  862. }
  863. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  864. I40E_GLPRT_PRC64L(hw->port),
  865. pf->stat_offsets_loaded,
  866. &osd->rx_size_64, &nsd->rx_size_64);
  867. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  868. I40E_GLPRT_PRC127L(hw->port),
  869. pf->stat_offsets_loaded,
  870. &osd->rx_size_127, &nsd->rx_size_127);
  871. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  872. I40E_GLPRT_PRC255L(hw->port),
  873. pf->stat_offsets_loaded,
  874. &osd->rx_size_255, &nsd->rx_size_255);
  875. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  876. I40E_GLPRT_PRC511L(hw->port),
  877. pf->stat_offsets_loaded,
  878. &osd->rx_size_511, &nsd->rx_size_511);
  879. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  880. I40E_GLPRT_PRC1023L(hw->port),
  881. pf->stat_offsets_loaded,
  882. &osd->rx_size_1023, &nsd->rx_size_1023);
  883. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  884. I40E_GLPRT_PRC1522L(hw->port),
  885. pf->stat_offsets_loaded,
  886. &osd->rx_size_1522, &nsd->rx_size_1522);
  887. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  888. I40E_GLPRT_PRC9522L(hw->port),
  889. pf->stat_offsets_loaded,
  890. &osd->rx_size_big, &nsd->rx_size_big);
  891. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  892. I40E_GLPRT_PTC64L(hw->port),
  893. pf->stat_offsets_loaded,
  894. &osd->tx_size_64, &nsd->tx_size_64);
  895. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  896. I40E_GLPRT_PTC127L(hw->port),
  897. pf->stat_offsets_loaded,
  898. &osd->tx_size_127, &nsd->tx_size_127);
  899. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  900. I40E_GLPRT_PTC255L(hw->port),
  901. pf->stat_offsets_loaded,
  902. &osd->tx_size_255, &nsd->tx_size_255);
  903. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  904. I40E_GLPRT_PTC511L(hw->port),
  905. pf->stat_offsets_loaded,
  906. &osd->tx_size_511, &nsd->tx_size_511);
  907. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  908. I40E_GLPRT_PTC1023L(hw->port),
  909. pf->stat_offsets_loaded,
  910. &osd->tx_size_1023, &nsd->tx_size_1023);
  911. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  912. I40E_GLPRT_PTC1522L(hw->port),
  913. pf->stat_offsets_loaded,
  914. &osd->tx_size_1522, &nsd->tx_size_1522);
  915. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  916. I40E_GLPRT_PTC9522L(hw->port),
  917. pf->stat_offsets_loaded,
  918. &osd->tx_size_big, &nsd->tx_size_big);
  919. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  920. pf->stat_offsets_loaded,
  921. &osd->rx_undersize, &nsd->rx_undersize);
  922. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  923. pf->stat_offsets_loaded,
  924. &osd->rx_fragments, &nsd->rx_fragments);
  925. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  926. pf->stat_offsets_loaded,
  927. &osd->rx_oversize, &nsd->rx_oversize);
  928. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  929. pf->stat_offsets_loaded,
  930. &osd->rx_jabber, &nsd->rx_jabber);
  931. /* FDIR stats */
  932. i40e_stat_update_and_clear32(hw,
  933. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
  934. &nsd->fd_atr_match);
  935. i40e_stat_update_and_clear32(hw,
  936. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
  937. &nsd->fd_sb_match);
  938. i40e_stat_update_and_clear32(hw,
  939. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
  940. &nsd->fd_atr_tunnel_match);
  941. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  942. nsd->tx_lpi_status =
  943. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  944. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  945. nsd->rx_lpi_status =
  946. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  947. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  948. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  949. pf->stat_offsets_loaded,
  950. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  951. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  952. pf->stat_offsets_loaded,
  953. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  954. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  955. !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  956. nsd->fd_sb_status = true;
  957. else
  958. nsd->fd_sb_status = false;
  959. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  960. !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  961. nsd->fd_atr_status = true;
  962. else
  963. nsd->fd_atr_status = false;
  964. pf->stat_offsets_loaded = true;
  965. }
  966. /**
  967. * i40e_update_stats - Update the various statistics counters.
  968. * @vsi: the VSI to be updated
  969. *
  970. * Update the various stats for this VSI and its related entities.
  971. **/
  972. void i40e_update_stats(struct i40e_vsi *vsi)
  973. {
  974. struct i40e_pf *pf = vsi->back;
  975. if (vsi == pf->vsi[pf->lan_vsi])
  976. i40e_update_pf_stats(pf);
  977. i40e_update_vsi_stats(vsi);
  978. }
  979. /**
  980. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  981. * @vsi: the VSI to be searched
  982. * @macaddr: the MAC address
  983. * @vlan: the vlan
  984. *
  985. * Returns ptr to the filter object or NULL
  986. **/
  987. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  988. const u8 *macaddr, s16 vlan)
  989. {
  990. struct i40e_mac_filter *f;
  991. u64 key;
  992. if (!vsi || !macaddr)
  993. return NULL;
  994. key = i40e_addr_to_hkey(macaddr);
  995. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  996. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  997. (vlan == f->vlan))
  998. return f;
  999. }
  1000. return NULL;
  1001. }
  1002. /**
  1003. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1004. * @vsi: the VSI to be searched
  1005. * @macaddr: the MAC address we are searching for
  1006. *
  1007. * Returns the first filter with the provided MAC address or NULL if
  1008. * MAC address was not found
  1009. **/
  1010. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1011. {
  1012. struct i40e_mac_filter *f;
  1013. u64 key;
  1014. if (!vsi || !macaddr)
  1015. return NULL;
  1016. key = i40e_addr_to_hkey(macaddr);
  1017. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1018. if ((ether_addr_equal(macaddr, f->macaddr)))
  1019. return f;
  1020. }
  1021. return NULL;
  1022. }
  1023. /**
  1024. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1025. * @vsi: the VSI to be searched
  1026. *
  1027. * Returns true if VSI is in vlan mode or false otherwise
  1028. **/
  1029. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1030. {
  1031. /* If we have a PVID, always operate in VLAN mode */
  1032. if (vsi->info.pvid)
  1033. return true;
  1034. /* We need to operate in VLAN mode whenever we have any filters with
  1035. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1036. * time, incurring search cost repeatedly. However, we can notice two
  1037. * things:
  1038. *
  1039. * 1) the only place where we can gain a VLAN filter is in
  1040. * i40e_add_filter.
  1041. *
  1042. * 2) the only place where filters are actually removed is in
  1043. * i40e_sync_filters_subtask.
  1044. *
  1045. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1046. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1047. * we have to perform the full search after deleting filters in
  1048. * i40e_sync_filters_subtask, but we already have to search
  1049. * filters here and can perform the check at the same time. This
  1050. * results in avoiding embedding a loop for VLAN mode inside another
  1051. * loop over all the filters, and should maintain correctness as noted
  1052. * above.
  1053. */
  1054. return vsi->has_vlan_filter;
  1055. }
  1056. /**
  1057. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1058. * @vsi: the VSI to configure
  1059. * @tmp_add_list: list of filters ready to be added
  1060. * @tmp_del_list: list of filters ready to be deleted
  1061. * @vlan_filters: the number of active VLAN filters
  1062. *
  1063. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1064. * behave as expected. If we have any active VLAN filters remaining or about
  1065. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1066. * so that they only match against untagged traffic. If we no longer have any
  1067. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1068. * so that they match against both tagged and untagged traffic. In this way,
  1069. * we ensure that we correctly receive the desired traffic. This ensures that
  1070. * when we have an active VLAN we will receive only untagged traffic and
  1071. * traffic matching active VLANs. If we have no active VLANs then we will
  1072. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1073. *
  1074. * Finally, in a similar fashion, this function also corrects filters when
  1075. * there is an active PVID assigned to this VSI.
  1076. *
  1077. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1078. *
  1079. * This function is only expected to be called from within
  1080. * i40e_sync_vsi_filters.
  1081. *
  1082. * NOTE: This function expects to be called while under the
  1083. * mac_filter_hash_lock
  1084. */
  1085. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1086. struct hlist_head *tmp_add_list,
  1087. struct hlist_head *tmp_del_list,
  1088. int vlan_filters)
  1089. {
  1090. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1091. struct i40e_mac_filter *f, *add_head;
  1092. struct i40e_new_mac_filter *new;
  1093. struct hlist_node *h;
  1094. int bkt, new_vlan;
  1095. /* To determine if a particular filter needs to be replaced we
  1096. * have the three following conditions:
  1097. *
  1098. * a) if we have a PVID assigned, then all filters which are
  1099. * not marked as VLAN=PVID must be replaced with filters that
  1100. * are.
  1101. * b) otherwise, if we have any active VLANS, all filters
  1102. * which are marked as VLAN=-1 must be replaced with
  1103. * filters marked as VLAN=0
  1104. * c) finally, if we do not have any active VLANS, all filters
  1105. * which are marked as VLAN=0 must be replaced with filters
  1106. * marked as VLAN=-1
  1107. */
  1108. /* Update the filters about to be added in place */
  1109. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1110. if (pvid && new->f->vlan != pvid)
  1111. new->f->vlan = pvid;
  1112. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1113. new->f->vlan = 0;
  1114. else if (!vlan_filters && new->f->vlan == 0)
  1115. new->f->vlan = I40E_VLAN_ANY;
  1116. }
  1117. /* Update the remaining active filters */
  1118. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1119. /* Combine the checks for whether a filter needs to be changed
  1120. * and then determine the new VLAN inside the if block, in
  1121. * order to avoid duplicating code for adding the new filter
  1122. * then deleting the old filter.
  1123. */
  1124. if ((pvid && f->vlan != pvid) ||
  1125. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1126. (!vlan_filters && f->vlan == 0)) {
  1127. /* Determine the new vlan we will be adding */
  1128. if (pvid)
  1129. new_vlan = pvid;
  1130. else if (vlan_filters)
  1131. new_vlan = 0;
  1132. else
  1133. new_vlan = I40E_VLAN_ANY;
  1134. /* Create the new filter */
  1135. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1136. if (!add_head)
  1137. return -ENOMEM;
  1138. /* Create a temporary i40e_new_mac_filter */
  1139. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1140. if (!new)
  1141. return -ENOMEM;
  1142. new->f = add_head;
  1143. new->state = add_head->state;
  1144. /* Add the new filter to the tmp list */
  1145. hlist_add_head(&new->hlist, tmp_add_list);
  1146. /* Put the original filter into the delete list */
  1147. f->state = I40E_FILTER_REMOVE;
  1148. hash_del(&f->hlist);
  1149. hlist_add_head(&f->hlist, tmp_del_list);
  1150. }
  1151. }
  1152. vsi->has_vlan_filter = !!vlan_filters;
  1153. return 0;
  1154. }
  1155. /**
  1156. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1157. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1158. * @macaddr: the MAC address
  1159. *
  1160. * Remove whatever filter the firmware set up so the driver can manage
  1161. * its own filtering intelligently.
  1162. **/
  1163. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1164. {
  1165. struct i40e_aqc_remove_macvlan_element_data element;
  1166. struct i40e_pf *pf = vsi->back;
  1167. /* Only appropriate for the PF main VSI */
  1168. if (vsi->type != I40E_VSI_MAIN)
  1169. return;
  1170. memset(&element, 0, sizeof(element));
  1171. ether_addr_copy(element.mac_addr, macaddr);
  1172. element.vlan_tag = 0;
  1173. /* Ignore error returns, some firmware does it this way... */
  1174. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1175. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1176. memset(&element, 0, sizeof(element));
  1177. ether_addr_copy(element.mac_addr, macaddr);
  1178. element.vlan_tag = 0;
  1179. /* ...and some firmware does it this way. */
  1180. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1181. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1182. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1183. }
  1184. /**
  1185. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1186. * @vsi: the VSI to be searched
  1187. * @macaddr: the MAC address
  1188. * @vlan: the vlan
  1189. *
  1190. * Returns ptr to the filter object or NULL when no memory available.
  1191. *
  1192. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1193. * being held.
  1194. **/
  1195. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1196. const u8 *macaddr, s16 vlan)
  1197. {
  1198. struct i40e_mac_filter *f;
  1199. u64 key;
  1200. if (!vsi || !macaddr)
  1201. return NULL;
  1202. f = i40e_find_filter(vsi, macaddr, vlan);
  1203. if (!f) {
  1204. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1205. if (!f)
  1206. return NULL;
  1207. /* Update the boolean indicating if we need to function in
  1208. * VLAN mode.
  1209. */
  1210. if (vlan >= 0)
  1211. vsi->has_vlan_filter = true;
  1212. ether_addr_copy(f->macaddr, macaddr);
  1213. f->vlan = vlan;
  1214. f->state = I40E_FILTER_NEW;
  1215. INIT_HLIST_NODE(&f->hlist);
  1216. key = i40e_addr_to_hkey(macaddr);
  1217. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1218. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1219. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1220. }
  1221. /* If we're asked to add a filter that has been marked for removal, it
  1222. * is safe to simply restore it to active state. __i40e_del_filter
  1223. * will have simply deleted any filters which were previously marked
  1224. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1225. * previously been ACTIVE. Since we haven't yet run the sync filters
  1226. * task, just restore this filter to the ACTIVE state so that the
  1227. * sync task leaves it in place
  1228. */
  1229. if (f->state == I40E_FILTER_REMOVE)
  1230. f->state = I40E_FILTER_ACTIVE;
  1231. return f;
  1232. }
  1233. /**
  1234. * __i40e_del_filter - Remove a specific filter from the VSI
  1235. * @vsi: VSI to remove from
  1236. * @f: the filter to remove from the list
  1237. *
  1238. * This function should be called instead of i40e_del_filter only if you know
  1239. * the exact filter you will remove already, such as via i40e_find_filter or
  1240. * i40e_find_mac.
  1241. *
  1242. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1243. * being held.
  1244. * ANOTHER NOTE: This function MUST be called from within the context of
  1245. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1246. * instead of list_for_each_entry().
  1247. **/
  1248. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1249. {
  1250. if (!f)
  1251. return;
  1252. /* If the filter was never added to firmware then we can just delete it
  1253. * directly and we don't want to set the status to remove or else an
  1254. * admin queue command will unnecessarily fire.
  1255. */
  1256. if ((f->state == I40E_FILTER_FAILED) ||
  1257. (f->state == I40E_FILTER_NEW)) {
  1258. hash_del(&f->hlist);
  1259. kfree(f);
  1260. } else {
  1261. f->state = I40E_FILTER_REMOVE;
  1262. }
  1263. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1264. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->state);
  1265. }
  1266. /**
  1267. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1268. * @vsi: the VSI to be searched
  1269. * @macaddr: the MAC address
  1270. * @vlan: the VLAN
  1271. *
  1272. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1273. * being held.
  1274. * ANOTHER NOTE: This function MUST be called from within the context of
  1275. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1276. * instead of list_for_each_entry().
  1277. **/
  1278. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1279. {
  1280. struct i40e_mac_filter *f;
  1281. if (!vsi || !macaddr)
  1282. return;
  1283. f = i40e_find_filter(vsi, macaddr, vlan);
  1284. __i40e_del_filter(vsi, f);
  1285. }
  1286. /**
  1287. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1288. * @vsi: the VSI to be searched
  1289. * @macaddr: the mac address to be filtered
  1290. *
  1291. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1292. * go through all the macvlan filters and add a macvlan filter for each
  1293. * unique vlan that already exists. If a PVID has been assigned, instead only
  1294. * add the macaddr to that VLAN.
  1295. *
  1296. * Returns last filter added on success, else NULL
  1297. **/
  1298. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1299. const u8 *macaddr)
  1300. {
  1301. struct i40e_mac_filter *f, *add = NULL;
  1302. struct hlist_node *h;
  1303. int bkt;
  1304. if (vsi->info.pvid)
  1305. return i40e_add_filter(vsi, macaddr,
  1306. le16_to_cpu(vsi->info.pvid));
  1307. if (!i40e_is_vsi_in_vlan(vsi))
  1308. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1309. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1310. if (f->state == I40E_FILTER_REMOVE)
  1311. continue;
  1312. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1313. if (!add)
  1314. return NULL;
  1315. }
  1316. return add;
  1317. }
  1318. /**
  1319. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1320. * @vsi: the VSI to be searched
  1321. * @macaddr: the mac address to be removed
  1322. *
  1323. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1324. * associated with.
  1325. *
  1326. * Returns 0 for success, or error
  1327. **/
  1328. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1329. {
  1330. struct i40e_mac_filter *f;
  1331. struct hlist_node *h;
  1332. bool found = false;
  1333. int bkt;
  1334. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1335. "Missing mac_filter_hash_lock\n");
  1336. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1337. if (ether_addr_equal(macaddr, f->macaddr)) {
  1338. __i40e_del_filter(vsi, f);
  1339. found = true;
  1340. }
  1341. }
  1342. if (found)
  1343. return 0;
  1344. else
  1345. return -ENOENT;
  1346. }
  1347. /**
  1348. * i40e_set_mac - NDO callback to set mac address
  1349. * @netdev: network interface device structure
  1350. * @p: pointer to an address structure
  1351. *
  1352. * Returns 0 on success, negative on failure
  1353. **/
  1354. static int i40e_set_mac(struct net_device *netdev, void *p)
  1355. {
  1356. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1357. struct i40e_vsi *vsi = np->vsi;
  1358. struct i40e_pf *pf = vsi->back;
  1359. struct i40e_hw *hw = &pf->hw;
  1360. struct sockaddr *addr = p;
  1361. if (!is_valid_ether_addr(addr->sa_data))
  1362. return -EADDRNOTAVAIL;
  1363. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1364. netdev_info(netdev, "already using mac address %pM\n",
  1365. addr->sa_data);
  1366. return 0;
  1367. }
  1368. if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
  1369. test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
  1370. return -EADDRNOTAVAIL;
  1371. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1372. netdev_info(netdev, "returning to hw mac address %pM\n",
  1373. hw->mac.addr);
  1374. else
  1375. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1376. /* Copy the address first, so that we avoid a possible race with
  1377. * .set_rx_mode(). If we copy after changing the address in the filter
  1378. * list, we might open ourselves to a narrow race window where
  1379. * .set_rx_mode could delete our dev_addr filter and prevent traffic
  1380. * from passing.
  1381. */
  1382. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1383. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1384. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1385. i40e_add_mac_filter(vsi, addr->sa_data);
  1386. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1387. if (vsi->type == I40E_VSI_MAIN) {
  1388. i40e_status ret;
  1389. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1390. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1391. addr->sa_data, NULL);
  1392. if (ret)
  1393. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1394. i40e_stat_str(hw, ret),
  1395. i40e_aq_str(hw, hw->aq.asq_last_status));
  1396. }
  1397. /* schedule our worker thread which will take care of
  1398. * applying the new filter changes
  1399. */
  1400. i40e_service_event_schedule(vsi->back);
  1401. return 0;
  1402. }
  1403. /**
  1404. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  1405. * @vsi: vsi structure
  1406. * @seed: RSS hash seed
  1407. **/
  1408. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  1409. u8 *lut, u16 lut_size)
  1410. {
  1411. struct i40e_pf *pf = vsi->back;
  1412. struct i40e_hw *hw = &pf->hw;
  1413. int ret = 0;
  1414. if (seed) {
  1415. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  1416. (struct i40e_aqc_get_set_rss_key_data *)seed;
  1417. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  1418. if (ret) {
  1419. dev_info(&pf->pdev->dev,
  1420. "Cannot set RSS key, err %s aq_err %s\n",
  1421. i40e_stat_str(hw, ret),
  1422. i40e_aq_str(hw, hw->aq.asq_last_status));
  1423. return ret;
  1424. }
  1425. }
  1426. if (lut) {
  1427. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  1428. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  1429. if (ret) {
  1430. dev_info(&pf->pdev->dev,
  1431. "Cannot set RSS lut, err %s aq_err %s\n",
  1432. i40e_stat_str(hw, ret),
  1433. i40e_aq_str(hw, hw->aq.asq_last_status));
  1434. return ret;
  1435. }
  1436. }
  1437. return ret;
  1438. }
  1439. /**
  1440. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  1441. * @vsi: VSI structure
  1442. **/
  1443. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  1444. {
  1445. struct i40e_pf *pf = vsi->back;
  1446. u8 seed[I40E_HKEY_ARRAY_SIZE];
  1447. u8 *lut;
  1448. int ret;
  1449. if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
  1450. return 0;
  1451. if (!vsi->rss_size)
  1452. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  1453. vsi->num_queue_pairs);
  1454. if (!vsi->rss_size)
  1455. return -EINVAL;
  1456. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  1457. if (!lut)
  1458. return -ENOMEM;
  1459. /* Use the user configured hash keys and lookup table if there is one,
  1460. * otherwise use default
  1461. */
  1462. if (vsi->rss_lut_user)
  1463. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  1464. else
  1465. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  1466. if (vsi->rss_hkey_user)
  1467. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  1468. else
  1469. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  1470. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  1471. kfree(lut);
  1472. return ret;
  1473. }
  1474. /**
  1475. * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
  1476. * @vsi: the VSI being configured,
  1477. * @ctxt: VSI context structure
  1478. * @enabled_tc: number of traffic classes to enable
  1479. *
  1480. * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
  1481. **/
  1482. static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
  1483. struct i40e_vsi_context *ctxt,
  1484. u8 enabled_tc)
  1485. {
  1486. u16 qcount = 0, max_qcount, qmap, sections = 0;
  1487. int i, override_q, pow, num_qps, ret;
  1488. u8 netdev_tc = 0, offset = 0;
  1489. if (vsi->type != I40E_VSI_MAIN)
  1490. return -EINVAL;
  1491. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1492. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1493. vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
  1494. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1495. num_qps = vsi->mqprio_qopt.qopt.count[0];
  1496. /* find the next higher power-of-2 of num queue pairs */
  1497. pow = ilog2(num_qps);
  1498. if (!is_power_of_2(num_qps))
  1499. pow++;
  1500. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1501. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1502. /* Setup queue offset/count for all TCs for given VSI */
  1503. max_qcount = vsi->mqprio_qopt.qopt.count[0];
  1504. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1505. /* See if the given TC is enabled for the given VSI */
  1506. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1507. offset = vsi->mqprio_qopt.qopt.offset[i];
  1508. qcount = vsi->mqprio_qopt.qopt.count[i];
  1509. if (qcount > max_qcount)
  1510. max_qcount = qcount;
  1511. vsi->tc_config.tc_info[i].qoffset = offset;
  1512. vsi->tc_config.tc_info[i].qcount = qcount;
  1513. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1514. } else {
  1515. /* TC is not enabled so set the offset to
  1516. * default queue and allocate one queue
  1517. * for the given TC.
  1518. */
  1519. vsi->tc_config.tc_info[i].qoffset = 0;
  1520. vsi->tc_config.tc_info[i].qcount = 1;
  1521. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1522. }
  1523. }
  1524. /* Set actual Tx/Rx queue pairs */
  1525. vsi->num_queue_pairs = offset + qcount;
  1526. /* Setup queue TC[0].qmap for given VSI context */
  1527. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  1528. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1529. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1530. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1531. /* Reconfigure RSS for main VSI with max queue count */
  1532. vsi->rss_size = max_qcount;
  1533. ret = i40e_vsi_config_rss(vsi);
  1534. if (ret) {
  1535. dev_info(&vsi->back->pdev->dev,
  1536. "Failed to reconfig rss for num_queues (%u)\n",
  1537. max_qcount);
  1538. return ret;
  1539. }
  1540. vsi->reconfig_rss = true;
  1541. dev_dbg(&vsi->back->pdev->dev,
  1542. "Reconfigured rss with num_queues (%u)\n", max_qcount);
  1543. /* Find queue count available for channel VSIs and starting offset
  1544. * for channel VSIs
  1545. */
  1546. override_q = vsi->mqprio_qopt.qopt.count[0];
  1547. if (override_q && override_q < vsi->num_queue_pairs) {
  1548. vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
  1549. vsi->next_base_queue = override_q;
  1550. }
  1551. return 0;
  1552. }
  1553. /**
  1554. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1555. * @vsi: the VSI being setup
  1556. * @ctxt: VSI context structure
  1557. * @enabled_tc: Enabled TCs bitmap
  1558. * @is_add: True if called before Add VSI
  1559. *
  1560. * Setup VSI queue mapping for enabled traffic classes.
  1561. **/
  1562. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1563. struct i40e_vsi_context *ctxt,
  1564. u8 enabled_tc,
  1565. bool is_add)
  1566. {
  1567. struct i40e_pf *pf = vsi->back;
  1568. u16 sections = 0;
  1569. u8 netdev_tc = 0;
  1570. u16 numtc = 1;
  1571. u16 qcount;
  1572. u8 offset;
  1573. u16 qmap;
  1574. int i;
  1575. u16 num_tc_qps = 0;
  1576. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1577. offset = 0;
  1578. /* Number of queues per enabled TC */
  1579. num_tc_qps = vsi->alloc_queue_pairs;
  1580. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1581. /* Find numtc from enabled TC bitmap */
  1582. for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1583. if (enabled_tc & BIT(i)) /* TC is enabled */
  1584. numtc++;
  1585. }
  1586. if (!numtc) {
  1587. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1588. numtc = 1;
  1589. }
  1590. num_tc_qps = num_tc_qps / numtc;
  1591. num_tc_qps = min_t(int, num_tc_qps,
  1592. i40e_pf_get_max_q_per_tc(pf));
  1593. }
  1594. vsi->tc_config.numtc = numtc;
  1595. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1596. /* Do not allow use more TC queue pairs than MSI-X vectors exist */
  1597. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1598. num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
  1599. /* Setup queue offset/count for all TCs for given VSI */
  1600. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1601. /* See if the given TC is enabled for the given VSI */
  1602. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1603. /* TC is enabled */
  1604. int pow, num_qps;
  1605. switch (vsi->type) {
  1606. case I40E_VSI_MAIN:
  1607. if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
  1608. I40E_FLAG_FD_ATR_ENABLED)) ||
  1609. vsi->tc_config.enabled_tc != 1) {
  1610. qcount = min_t(int, pf->alloc_rss_size,
  1611. num_tc_qps);
  1612. break;
  1613. }
  1614. case I40E_VSI_FDIR:
  1615. case I40E_VSI_SRIOV:
  1616. case I40E_VSI_VMDQ2:
  1617. default:
  1618. qcount = num_tc_qps;
  1619. WARN_ON(i != 0);
  1620. break;
  1621. }
  1622. vsi->tc_config.tc_info[i].qoffset = offset;
  1623. vsi->tc_config.tc_info[i].qcount = qcount;
  1624. /* find the next higher power-of-2 of num queue pairs */
  1625. num_qps = qcount;
  1626. pow = 0;
  1627. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1628. pow++;
  1629. num_qps >>= 1;
  1630. }
  1631. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1632. qmap =
  1633. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1634. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1635. offset += qcount;
  1636. } else {
  1637. /* TC is not enabled so set the offset to
  1638. * default queue and allocate one queue
  1639. * for the given TC.
  1640. */
  1641. vsi->tc_config.tc_info[i].qoffset = 0;
  1642. vsi->tc_config.tc_info[i].qcount = 1;
  1643. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1644. qmap = 0;
  1645. }
  1646. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1647. }
  1648. /* Set actual Tx/Rx queue pairs */
  1649. vsi->num_queue_pairs = offset;
  1650. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1651. if (vsi->req_queue_pairs > 0)
  1652. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1653. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1654. vsi->num_queue_pairs = pf->num_lan_msix;
  1655. }
  1656. /* Scheduler section valid can only be set for ADD VSI */
  1657. if (is_add) {
  1658. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1659. ctxt->info.up_enable_bits = enabled_tc;
  1660. }
  1661. if (vsi->type == I40E_VSI_SRIOV) {
  1662. ctxt->info.mapping_flags |=
  1663. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1664. for (i = 0; i < vsi->num_queue_pairs; i++)
  1665. ctxt->info.queue_mapping[i] =
  1666. cpu_to_le16(vsi->base_queue + i);
  1667. } else {
  1668. ctxt->info.mapping_flags |=
  1669. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1670. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1671. }
  1672. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1673. }
  1674. /**
  1675. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1676. * @netdev: the netdevice
  1677. * @addr: address to add
  1678. *
  1679. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1680. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1681. */
  1682. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1683. {
  1684. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1685. struct i40e_vsi *vsi = np->vsi;
  1686. if (i40e_add_mac_filter(vsi, addr))
  1687. return 0;
  1688. else
  1689. return -ENOMEM;
  1690. }
  1691. /**
  1692. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1693. * @netdev: the netdevice
  1694. * @addr: address to add
  1695. *
  1696. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1697. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1698. */
  1699. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1700. {
  1701. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1702. struct i40e_vsi *vsi = np->vsi;
  1703. /* Under some circumstances, we might receive a request to delete
  1704. * our own device address from our uc list. Because we store the
  1705. * device address in the VSI's MAC/VLAN filter list, we need to ignore
  1706. * such requests and not delete our device address from this list.
  1707. */
  1708. if (ether_addr_equal(addr, netdev->dev_addr))
  1709. return 0;
  1710. i40e_del_mac_filter(vsi, addr);
  1711. return 0;
  1712. }
  1713. /**
  1714. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1715. * @netdev: network interface device structure
  1716. **/
  1717. static void i40e_set_rx_mode(struct net_device *netdev)
  1718. {
  1719. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1720. struct i40e_vsi *vsi = np->vsi;
  1721. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1722. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1723. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1724. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1725. /* check for other flag changes */
  1726. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1727. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1728. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1729. }
  1730. }
  1731. /**
  1732. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1733. * @vsi: Pointer to VSI struct
  1734. * @from: Pointer to list which contains MAC filter entries - changes to
  1735. * those entries needs to be undone.
  1736. *
  1737. * MAC filter entries from this list were slated for deletion.
  1738. **/
  1739. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1740. struct hlist_head *from)
  1741. {
  1742. struct i40e_mac_filter *f;
  1743. struct hlist_node *h;
  1744. hlist_for_each_entry_safe(f, h, from, hlist) {
  1745. u64 key = i40e_addr_to_hkey(f->macaddr);
  1746. /* Move the element back into MAC filter list*/
  1747. hlist_del(&f->hlist);
  1748. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1749. }
  1750. }
  1751. /**
  1752. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1753. * @vsi: Pointer to vsi struct
  1754. * @from: Pointer to list which contains MAC filter entries - changes to
  1755. * those entries needs to be undone.
  1756. *
  1757. * MAC filter entries from this list were slated for addition.
  1758. **/
  1759. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1760. struct hlist_head *from)
  1761. {
  1762. struct i40e_new_mac_filter *new;
  1763. struct hlist_node *h;
  1764. hlist_for_each_entry_safe(new, h, from, hlist) {
  1765. /* We can simply free the wrapper structure */
  1766. hlist_del(&new->hlist);
  1767. kfree(new);
  1768. }
  1769. }
  1770. /**
  1771. * i40e_next_entry - Get the next non-broadcast filter from a list
  1772. * @next: pointer to filter in list
  1773. *
  1774. * Returns the next non-broadcast filter in the list. Required so that we
  1775. * ignore broadcast filters within the list, since these are not handled via
  1776. * the normal firmware update path.
  1777. */
  1778. static
  1779. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1780. {
  1781. hlist_for_each_entry_continue(next, hlist) {
  1782. if (!is_broadcast_ether_addr(next->f->macaddr))
  1783. return next;
  1784. }
  1785. return NULL;
  1786. }
  1787. /**
  1788. * i40e_update_filter_state - Update filter state based on return data
  1789. * from firmware
  1790. * @count: Number of filters added
  1791. * @add_list: return data from fw
  1792. * @add_head: pointer to first filter in current batch
  1793. *
  1794. * MAC filter entries from list were slated to be added to device. Returns
  1795. * number of successful filters. Note that 0 does NOT mean success!
  1796. **/
  1797. static int
  1798. i40e_update_filter_state(int count,
  1799. struct i40e_aqc_add_macvlan_element_data *add_list,
  1800. struct i40e_new_mac_filter *add_head)
  1801. {
  1802. int retval = 0;
  1803. int i;
  1804. for (i = 0; i < count; i++) {
  1805. /* Always check status of each filter. We don't need to check
  1806. * the firmware return status because we pre-set the filter
  1807. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1808. * request to the adminq. Thus, if it no longer matches then
  1809. * we know the filter is active.
  1810. */
  1811. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1812. add_head->state = I40E_FILTER_FAILED;
  1813. } else {
  1814. add_head->state = I40E_FILTER_ACTIVE;
  1815. retval++;
  1816. }
  1817. add_head = i40e_next_filter(add_head);
  1818. if (!add_head)
  1819. break;
  1820. }
  1821. return retval;
  1822. }
  1823. /**
  1824. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1825. * @vsi: ptr to the VSI
  1826. * @vsi_name: name to display in messages
  1827. * @list: the list of filters to send to firmware
  1828. * @num_del: the number of filters to delete
  1829. * @retval: Set to -EIO on failure to delete
  1830. *
  1831. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1832. * *retval instead of a return value so that success does not force ret_val to
  1833. * be set to 0. This ensures that a sequence of calls to this function
  1834. * preserve the previous value of *retval on successful delete.
  1835. */
  1836. static
  1837. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1838. struct i40e_aqc_remove_macvlan_element_data *list,
  1839. int num_del, int *retval)
  1840. {
  1841. struct i40e_hw *hw = &vsi->back->hw;
  1842. i40e_status aq_ret;
  1843. int aq_err;
  1844. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1845. aq_err = hw->aq.asq_last_status;
  1846. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1847. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1848. *retval = -EIO;
  1849. dev_info(&vsi->back->pdev->dev,
  1850. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1851. vsi_name, i40e_stat_str(hw, aq_ret),
  1852. i40e_aq_str(hw, aq_err));
  1853. }
  1854. }
  1855. /**
  1856. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1857. * @vsi: ptr to the VSI
  1858. * @vsi_name: name to display in messages
  1859. * @list: the list of filters to send to firmware
  1860. * @add_head: Position in the add hlist
  1861. * @num_add: the number of filters to add
  1862. *
  1863. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1864. * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
  1865. * space for more filters.
  1866. */
  1867. static
  1868. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1869. struct i40e_aqc_add_macvlan_element_data *list,
  1870. struct i40e_new_mac_filter *add_head,
  1871. int num_add)
  1872. {
  1873. struct i40e_hw *hw = &vsi->back->hw;
  1874. int aq_err, fcnt;
  1875. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1876. aq_err = hw->aq.asq_last_status;
  1877. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1878. if (fcnt != num_add) {
  1879. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1880. dev_warn(&vsi->back->pdev->dev,
  1881. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1882. i40e_aq_str(hw, aq_err),
  1883. vsi_name);
  1884. }
  1885. }
  1886. /**
  1887. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1888. * @vsi: pointer to the VSI
  1889. * @vsi_name: the VSI name
  1890. * @f: filter data
  1891. *
  1892. * This function sets or clears the promiscuous broadcast flags for VLAN
  1893. * filters in order to properly receive broadcast frames. Assumes that only
  1894. * broadcast filters are passed.
  1895. *
  1896. * Returns status indicating success or failure;
  1897. **/
  1898. static i40e_status
  1899. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1900. struct i40e_mac_filter *f)
  1901. {
  1902. bool enable = f->state == I40E_FILTER_NEW;
  1903. struct i40e_hw *hw = &vsi->back->hw;
  1904. i40e_status aq_ret;
  1905. if (f->vlan == I40E_VLAN_ANY) {
  1906. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1907. vsi->seid,
  1908. enable,
  1909. NULL);
  1910. } else {
  1911. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1912. vsi->seid,
  1913. enable,
  1914. f->vlan,
  1915. NULL);
  1916. }
  1917. if (aq_ret) {
  1918. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1919. dev_warn(&vsi->back->pdev->dev,
  1920. "Error %s, forcing overflow promiscuous on %s\n",
  1921. i40e_aq_str(hw, hw->aq.asq_last_status),
  1922. vsi_name);
  1923. }
  1924. return aq_ret;
  1925. }
  1926. /**
  1927. * i40e_set_promiscuous - set promiscuous mode
  1928. * @pf: board private structure
  1929. * @promisc: promisc on or off
  1930. *
  1931. * There are different ways of setting promiscuous mode on a PF depending on
  1932. * what state/environment we're in. This identifies and sets it appropriately.
  1933. * Returns 0 on success.
  1934. **/
  1935. static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
  1936. {
  1937. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  1938. struct i40e_hw *hw = &pf->hw;
  1939. i40e_status aq_ret;
  1940. if (vsi->type == I40E_VSI_MAIN &&
  1941. pf->lan_veb != I40E_NO_VEB &&
  1942. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  1943. /* set defport ON for Main VSI instead of true promisc
  1944. * this way we will get all unicast/multicast and VLAN
  1945. * promisc behavior but will not get VF or VMDq traffic
  1946. * replicated on the Main VSI.
  1947. */
  1948. if (promisc)
  1949. aq_ret = i40e_aq_set_default_vsi(hw,
  1950. vsi->seid,
  1951. NULL);
  1952. else
  1953. aq_ret = i40e_aq_clear_default_vsi(hw,
  1954. vsi->seid,
  1955. NULL);
  1956. if (aq_ret) {
  1957. dev_info(&pf->pdev->dev,
  1958. "Set default VSI failed, err %s, aq_err %s\n",
  1959. i40e_stat_str(hw, aq_ret),
  1960. i40e_aq_str(hw, hw->aq.asq_last_status));
  1961. }
  1962. } else {
  1963. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  1964. hw,
  1965. vsi->seid,
  1966. promisc, NULL,
  1967. true);
  1968. if (aq_ret) {
  1969. dev_info(&pf->pdev->dev,
  1970. "set unicast promisc failed, err %s, aq_err %s\n",
  1971. i40e_stat_str(hw, aq_ret),
  1972. i40e_aq_str(hw, hw->aq.asq_last_status));
  1973. }
  1974. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  1975. hw,
  1976. vsi->seid,
  1977. promisc, NULL);
  1978. if (aq_ret) {
  1979. dev_info(&pf->pdev->dev,
  1980. "set multicast promisc failed, err %s, aq_err %s\n",
  1981. i40e_stat_str(hw, aq_ret),
  1982. i40e_aq_str(hw, hw->aq.asq_last_status));
  1983. }
  1984. }
  1985. if (!aq_ret)
  1986. pf->cur_promisc = promisc;
  1987. return aq_ret;
  1988. }
  1989. /**
  1990. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1991. * @vsi: ptr to the VSI
  1992. *
  1993. * Push any outstanding VSI filter changes through the AdminQ.
  1994. *
  1995. * Returns 0 or error value
  1996. **/
  1997. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1998. {
  1999. struct hlist_head tmp_add_list, tmp_del_list;
  2000. struct i40e_mac_filter *f;
  2001. struct i40e_new_mac_filter *new, *add_head = NULL;
  2002. struct i40e_hw *hw = &vsi->back->hw;
  2003. bool old_overflow, new_overflow;
  2004. unsigned int failed_filters = 0;
  2005. unsigned int vlan_filters = 0;
  2006. char vsi_name[16] = "PF";
  2007. int filter_list_len = 0;
  2008. i40e_status aq_ret = 0;
  2009. u32 changed_flags = 0;
  2010. struct hlist_node *h;
  2011. struct i40e_pf *pf;
  2012. int num_add = 0;
  2013. int num_del = 0;
  2014. int retval = 0;
  2015. u16 cmd_flags;
  2016. int list_size;
  2017. int bkt;
  2018. /* empty array typed pointers, kcalloc later */
  2019. struct i40e_aqc_add_macvlan_element_data *add_list;
  2020. struct i40e_aqc_remove_macvlan_element_data *del_list;
  2021. while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
  2022. usleep_range(1000, 2000);
  2023. pf = vsi->back;
  2024. old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2025. if (vsi->netdev) {
  2026. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  2027. vsi->current_netdev_flags = vsi->netdev->flags;
  2028. }
  2029. INIT_HLIST_HEAD(&tmp_add_list);
  2030. INIT_HLIST_HEAD(&tmp_del_list);
  2031. if (vsi->type == I40E_VSI_SRIOV)
  2032. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  2033. else if (vsi->type != I40E_VSI_MAIN)
  2034. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  2035. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  2036. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  2037. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2038. /* Create a list of filters to delete. */
  2039. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2040. if (f->state == I40E_FILTER_REMOVE) {
  2041. /* Move the element into temporary del_list */
  2042. hash_del(&f->hlist);
  2043. hlist_add_head(&f->hlist, &tmp_del_list);
  2044. /* Avoid counting removed filters */
  2045. continue;
  2046. }
  2047. if (f->state == I40E_FILTER_NEW) {
  2048. /* Create a temporary i40e_new_mac_filter */
  2049. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  2050. if (!new)
  2051. goto err_no_memory_locked;
  2052. /* Store pointer to the real filter */
  2053. new->f = f;
  2054. new->state = f->state;
  2055. /* Add it to the hash list */
  2056. hlist_add_head(&new->hlist, &tmp_add_list);
  2057. }
  2058. /* Count the number of active (current and new) VLAN
  2059. * filters we have now. Does not count filters which
  2060. * are marked for deletion.
  2061. */
  2062. if (f->vlan > 0)
  2063. vlan_filters++;
  2064. }
  2065. retval = i40e_correct_mac_vlan_filters(vsi,
  2066. &tmp_add_list,
  2067. &tmp_del_list,
  2068. vlan_filters);
  2069. if (retval)
  2070. goto err_no_memory_locked;
  2071. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2072. }
  2073. /* Now process 'del_list' outside the lock */
  2074. if (!hlist_empty(&tmp_del_list)) {
  2075. filter_list_len = hw->aq.asq_buf_size /
  2076. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2077. list_size = filter_list_len *
  2078. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2079. del_list = kzalloc(list_size, GFP_ATOMIC);
  2080. if (!del_list)
  2081. goto err_no_memory;
  2082. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  2083. cmd_flags = 0;
  2084. /* handle broadcast filters by updating the broadcast
  2085. * promiscuous flag and release filter list.
  2086. */
  2087. if (is_broadcast_ether_addr(f->macaddr)) {
  2088. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  2089. hlist_del(&f->hlist);
  2090. kfree(f);
  2091. continue;
  2092. }
  2093. /* add to delete list */
  2094. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  2095. if (f->vlan == I40E_VLAN_ANY) {
  2096. del_list[num_del].vlan_tag = 0;
  2097. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  2098. } else {
  2099. del_list[num_del].vlan_tag =
  2100. cpu_to_le16((u16)(f->vlan));
  2101. }
  2102. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  2103. del_list[num_del].flags = cmd_flags;
  2104. num_del++;
  2105. /* flush a full buffer */
  2106. if (num_del == filter_list_len) {
  2107. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2108. num_del, &retval);
  2109. memset(del_list, 0, list_size);
  2110. num_del = 0;
  2111. }
  2112. /* Release memory for MAC filter entries which were
  2113. * synced up with HW.
  2114. */
  2115. hlist_del(&f->hlist);
  2116. kfree(f);
  2117. }
  2118. if (num_del) {
  2119. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2120. num_del, &retval);
  2121. }
  2122. kfree(del_list);
  2123. del_list = NULL;
  2124. }
  2125. if (!hlist_empty(&tmp_add_list)) {
  2126. /* Do all the adds now. */
  2127. filter_list_len = hw->aq.asq_buf_size /
  2128. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2129. list_size = filter_list_len *
  2130. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2131. add_list = kzalloc(list_size, GFP_ATOMIC);
  2132. if (!add_list)
  2133. goto err_no_memory;
  2134. num_add = 0;
  2135. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2136. /* handle broadcast filters by updating the broadcast
  2137. * promiscuous flag instead of adding a MAC filter.
  2138. */
  2139. if (is_broadcast_ether_addr(new->f->macaddr)) {
  2140. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  2141. new->f))
  2142. new->state = I40E_FILTER_FAILED;
  2143. else
  2144. new->state = I40E_FILTER_ACTIVE;
  2145. continue;
  2146. }
  2147. /* add to add array */
  2148. if (num_add == 0)
  2149. add_head = new;
  2150. cmd_flags = 0;
  2151. ether_addr_copy(add_list[num_add].mac_addr,
  2152. new->f->macaddr);
  2153. if (new->f->vlan == I40E_VLAN_ANY) {
  2154. add_list[num_add].vlan_tag = 0;
  2155. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  2156. } else {
  2157. add_list[num_add].vlan_tag =
  2158. cpu_to_le16((u16)(new->f->vlan));
  2159. }
  2160. add_list[num_add].queue_number = 0;
  2161. /* set invalid match method for later detection */
  2162. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  2163. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  2164. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  2165. num_add++;
  2166. /* flush a full buffer */
  2167. if (num_add == filter_list_len) {
  2168. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  2169. add_head, num_add);
  2170. memset(add_list, 0, list_size);
  2171. num_add = 0;
  2172. }
  2173. }
  2174. if (num_add) {
  2175. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  2176. num_add);
  2177. }
  2178. /* Now move all of the filters from the temp add list back to
  2179. * the VSI's list.
  2180. */
  2181. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2182. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2183. /* Only update the state if we're still NEW */
  2184. if (new->f->state == I40E_FILTER_NEW)
  2185. new->f->state = new->state;
  2186. hlist_del(&new->hlist);
  2187. kfree(new);
  2188. }
  2189. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2190. kfree(add_list);
  2191. add_list = NULL;
  2192. }
  2193. /* Determine the number of active and failed filters. */
  2194. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2195. vsi->active_filters = 0;
  2196. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  2197. if (f->state == I40E_FILTER_ACTIVE)
  2198. vsi->active_filters++;
  2199. else if (f->state == I40E_FILTER_FAILED)
  2200. failed_filters++;
  2201. }
  2202. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2203. /* Check if we are able to exit overflow promiscuous mode. We can
  2204. * safely exit if we didn't just enter, we no longer have any failed
  2205. * filters, and we have reduced filters below the threshold value.
  2206. */
  2207. if (old_overflow && !failed_filters &&
  2208. vsi->active_filters < vsi->promisc_threshold) {
  2209. dev_info(&pf->pdev->dev,
  2210. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  2211. vsi_name);
  2212. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2213. vsi->promisc_threshold = 0;
  2214. }
  2215. /* if the VF is not trusted do not do promisc */
  2216. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2217. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2218. goto out;
  2219. }
  2220. new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2221. /* If we are entering overflow promiscuous, we need to calculate a new
  2222. * threshold for when we are safe to exit
  2223. */
  2224. if (!old_overflow && new_overflow)
  2225. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  2226. /* check for changes in promiscuous modes */
  2227. if (changed_flags & IFF_ALLMULTI) {
  2228. bool cur_multipromisc;
  2229. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2230. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2231. vsi->seid,
  2232. cur_multipromisc,
  2233. NULL);
  2234. if (aq_ret) {
  2235. retval = i40e_aq_rc_to_posix(aq_ret,
  2236. hw->aq.asq_last_status);
  2237. dev_info(&pf->pdev->dev,
  2238. "set multi promisc failed on %s, err %s aq_err %s\n",
  2239. vsi_name,
  2240. i40e_stat_str(hw, aq_ret),
  2241. i40e_aq_str(hw, hw->aq.asq_last_status));
  2242. }
  2243. }
  2244. if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
  2245. bool cur_promisc;
  2246. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2247. new_overflow);
  2248. aq_ret = i40e_set_promiscuous(pf, cur_promisc);
  2249. if (aq_ret) {
  2250. retval = i40e_aq_rc_to_posix(aq_ret,
  2251. hw->aq.asq_last_status);
  2252. dev_info(&pf->pdev->dev,
  2253. "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
  2254. cur_promisc ? "on" : "off",
  2255. vsi_name,
  2256. i40e_stat_str(hw, aq_ret),
  2257. i40e_aq_str(hw, hw->aq.asq_last_status));
  2258. }
  2259. }
  2260. out:
  2261. /* if something went wrong then set the changed flag so we try again */
  2262. if (retval)
  2263. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2264. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2265. return retval;
  2266. err_no_memory:
  2267. /* Restore elements on the temporary add and delete lists */
  2268. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2269. err_no_memory_locked:
  2270. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2271. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2272. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2273. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2274. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2275. return -ENOMEM;
  2276. }
  2277. /**
  2278. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2279. * @pf: board private structure
  2280. **/
  2281. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2282. {
  2283. int v;
  2284. if (!pf)
  2285. return;
  2286. if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
  2287. return;
  2288. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2289. if (pf->vsi[v] &&
  2290. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2291. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2292. if (ret) {
  2293. /* come back and try again later */
  2294. set_bit(__I40E_MACVLAN_SYNC_PENDING,
  2295. pf->state);
  2296. break;
  2297. }
  2298. }
  2299. }
  2300. }
  2301. /**
  2302. * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
  2303. * @vsi: the vsi
  2304. **/
  2305. static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
  2306. {
  2307. if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2308. return I40E_RXBUFFER_2048;
  2309. else
  2310. return I40E_RXBUFFER_3072;
  2311. }
  2312. /**
  2313. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2314. * @netdev: network interface device structure
  2315. * @new_mtu: new value for maximum frame size
  2316. *
  2317. * Returns 0 on success, negative on failure
  2318. **/
  2319. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2320. {
  2321. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2322. struct i40e_vsi *vsi = np->vsi;
  2323. struct i40e_pf *pf = vsi->back;
  2324. if (i40e_enabled_xdp_vsi(vsi)) {
  2325. int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  2326. if (frame_size > i40e_max_xdp_frame_size(vsi))
  2327. return -EINVAL;
  2328. }
  2329. netdev_info(netdev, "changing MTU from %d to %d\n",
  2330. netdev->mtu, new_mtu);
  2331. netdev->mtu = new_mtu;
  2332. if (netif_running(netdev))
  2333. i40e_vsi_reinit_locked(vsi);
  2334. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  2335. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  2336. return 0;
  2337. }
  2338. /**
  2339. * i40e_ioctl - Access the hwtstamp interface
  2340. * @netdev: network interface device structure
  2341. * @ifr: interface request data
  2342. * @cmd: ioctl command
  2343. **/
  2344. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2345. {
  2346. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2347. struct i40e_pf *pf = np->vsi->back;
  2348. switch (cmd) {
  2349. case SIOCGHWTSTAMP:
  2350. return i40e_ptp_get_ts_config(pf, ifr);
  2351. case SIOCSHWTSTAMP:
  2352. return i40e_ptp_set_ts_config(pf, ifr);
  2353. default:
  2354. return -EOPNOTSUPP;
  2355. }
  2356. }
  2357. /**
  2358. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2359. * @vsi: the vsi being adjusted
  2360. **/
  2361. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2362. {
  2363. struct i40e_vsi_context ctxt;
  2364. i40e_status ret;
  2365. if ((vsi->info.valid_sections &
  2366. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2367. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2368. return; /* already enabled */
  2369. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2370. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2371. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2372. ctxt.seid = vsi->seid;
  2373. ctxt.info = vsi->info;
  2374. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2375. if (ret) {
  2376. dev_info(&vsi->back->pdev->dev,
  2377. "update vlan stripping failed, err %s aq_err %s\n",
  2378. i40e_stat_str(&vsi->back->hw, ret),
  2379. i40e_aq_str(&vsi->back->hw,
  2380. vsi->back->hw.aq.asq_last_status));
  2381. }
  2382. }
  2383. /**
  2384. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2385. * @vsi: the vsi being adjusted
  2386. **/
  2387. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2388. {
  2389. struct i40e_vsi_context ctxt;
  2390. i40e_status ret;
  2391. if ((vsi->info.valid_sections &
  2392. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2393. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2394. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2395. return; /* already disabled */
  2396. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2397. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2398. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2399. ctxt.seid = vsi->seid;
  2400. ctxt.info = vsi->info;
  2401. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2402. if (ret) {
  2403. dev_info(&vsi->back->pdev->dev,
  2404. "update vlan stripping failed, err %s aq_err %s\n",
  2405. i40e_stat_str(&vsi->back->hw, ret),
  2406. i40e_aq_str(&vsi->back->hw,
  2407. vsi->back->hw.aq.asq_last_status));
  2408. }
  2409. }
  2410. /**
  2411. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2412. * @vsi: the vsi being configured
  2413. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2414. *
  2415. * This is a helper function for adding a new MAC/VLAN filter with the
  2416. * specified VLAN for each existing MAC address already in the hash table.
  2417. * This function does *not* perform any accounting to update filters based on
  2418. * VLAN mode.
  2419. *
  2420. * NOTE: this function expects to be called while under the
  2421. * mac_filter_hash_lock
  2422. **/
  2423. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2424. {
  2425. struct i40e_mac_filter *f, *add_f;
  2426. struct hlist_node *h;
  2427. int bkt;
  2428. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2429. if (f->state == I40E_FILTER_REMOVE)
  2430. continue;
  2431. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2432. if (!add_f) {
  2433. dev_info(&vsi->back->pdev->dev,
  2434. "Could not add vlan filter %d for %pM\n",
  2435. vid, f->macaddr);
  2436. return -ENOMEM;
  2437. }
  2438. }
  2439. return 0;
  2440. }
  2441. /**
  2442. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2443. * @vsi: the VSI being configured
  2444. * @vid: VLAN id to be added
  2445. **/
  2446. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2447. {
  2448. int err;
  2449. if (vsi->info.pvid)
  2450. return -EINVAL;
  2451. /* The network stack will attempt to add VID=0, with the intention to
  2452. * receive priority tagged packets with a VLAN of 0. Our HW receives
  2453. * these packets by default when configured to receive untagged
  2454. * packets, so we don't need to add a filter for this case.
  2455. * Additionally, HW interprets adding a VID=0 filter as meaning to
  2456. * receive *only* tagged traffic and stops receiving untagged traffic.
  2457. * Thus, we do not want to actually add a filter for VID=0
  2458. */
  2459. if (!vid)
  2460. return 0;
  2461. /* Locked once because all functions invoked below iterates list*/
  2462. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2463. err = i40e_add_vlan_all_mac(vsi, vid);
  2464. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2465. if (err)
  2466. return err;
  2467. /* schedule our worker thread which will take care of
  2468. * applying the new filter changes
  2469. */
  2470. i40e_service_event_schedule(vsi->back);
  2471. return 0;
  2472. }
  2473. /**
  2474. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2475. * @vsi: the vsi being configured
  2476. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2477. *
  2478. * This function should be used to remove all VLAN filters which match the
  2479. * given VID. It does not schedule the service event and does not take the
  2480. * mac_filter_hash_lock so it may be combined with other operations under
  2481. * a single invocation of the mac_filter_hash_lock.
  2482. *
  2483. * NOTE: this function expects to be called while under the
  2484. * mac_filter_hash_lock
  2485. */
  2486. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2487. {
  2488. struct i40e_mac_filter *f;
  2489. struct hlist_node *h;
  2490. int bkt;
  2491. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2492. if (f->vlan == vid)
  2493. __i40e_del_filter(vsi, f);
  2494. }
  2495. }
  2496. /**
  2497. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2498. * @vsi: the VSI being configured
  2499. * @vid: VLAN id to be removed
  2500. **/
  2501. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2502. {
  2503. if (!vid || vsi->info.pvid)
  2504. return;
  2505. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2506. i40e_rm_vlan_all_mac(vsi, vid);
  2507. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2508. /* schedule our worker thread which will take care of
  2509. * applying the new filter changes
  2510. */
  2511. i40e_service_event_schedule(vsi->back);
  2512. }
  2513. /**
  2514. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2515. * @netdev: network interface to be adjusted
  2516. * @proto: unused protocol value
  2517. * @vid: vlan id to be added
  2518. *
  2519. * net_device_ops implementation for adding vlan ids
  2520. **/
  2521. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2522. __always_unused __be16 proto, u16 vid)
  2523. {
  2524. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2525. struct i40e_vsi *vsi = np->vsi;
  2526. int ret = 0;
  2527. if (vid >= VLAN_N_VID)
  2528. return -EINVAL;
  2529. ret = i40e_vsi_add_vlan(vsi, vid);
  2530. if (!ret)
  2531. set_bit(vid, vsi->active_vlans);
  2532. return ret;
  2533. }
  2534. /**
  2535. * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
  2536. * @netdev: network interface to be adjusted
  2537. * @proto: unused protocol value
  2538. * @vid: vlan id to be added
  2539. **/
  2540. static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
  2541. __always_unused __be16 proto, u16 vid)
  2542. {
  2543. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2544. struct i40e_vsi *vsi = np->vsi;
  2545. if (vid >= VLAN_N_VID)
  2546. return;
  2547. set_bit(vid, vsi->active_vlans);
  2548. }
  2549. /**
  2550. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2551. * @netdev: network interface to be adjusted
  2552. * @proto: unused protocol value
  2553. * @vid: vlan id to be removed
  2554. *
  2555. * net_device_ops implementation for removing vlan ids
  2556. **/
  2557. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2558. __always_unused __be16 proto, u16 vid)
  2559. {
  2560. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2561. struct i40e_vsi *vsi = np->vsi;
  2562. /* return code is ignored as there is nothing a user
  2563. * can do about failure to remove and a log message was
  2564. * already printed from the other function
  2565. */
  2566. i40e_vsi_kill_vlan(vsi, vid);
  2567. clear_bit(vid, vsi->active_vlans);
  2568. return 0;
  2569. }
  2570. /**
  2571. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2572. * @vsi: the vsi being brought back up
  2573. **/
  2574. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2575. {
  2576. u16 vid;
  2577. if (!vsi->netdev)
  2578. return;
  2579. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  2580. i40e_vlan_stripping_enable(vsi);
  2581. else
  2582. i40e_vlan_stripping_disable(vsi);
  2583. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2584. i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
  2585. vid);
  2586. }
  2587. /**
  2588. * i40e_vsi_add_pvid - Add pvid for the VSI
  2589. * @vsi: the vsi being adjusted
  2590. * @vid: the vlan id to set as a PVID
  2591. **/
  2592. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2593. {
  2594. struct i40e_vsi_context ctxt;
  2595. i40e_status ret;
  2596. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2597. vsi->info.pvid = cpu_to_le16(vid);
  2598. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2599. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2600. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2601. ctxt.seid = vsi->seid;
  2602. ctxt.info = vsi->info;
  2603. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2604. if (ret) {
  2605. dev_info(&vsi->back->pdev->dev,
  2606. "add pvid failed, err %s aq_err %s\n",
  2607. i40e_stat_str(&vsi->back->hw, ret),
  2608. i40e_aq_str(&vsi->back->hw,
  2609. vsi->back->hw.aq.asq_last_status));
  2610. return -ENOENT;
  2611. }
  2612. return 0;
  2613. }
  2614. /**
  2615. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2616. * @vsi: the vsi being adjusted
  2617. *
  2618. * Just use the vlan_rx_register() service to put it back to normal
  2619. **/
  2620. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2621. {
  2622. i40e_vlan_stripping_disable(vsi);
  2623. vsi->info.pvid = 0;
  2624. }
  2625. /**
  2626. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2627. * @vsi: ptr to the VSI
  2628. *
  2629. * If this function returns with an error, then it's possible one or
  2630. * more of the rings is populated (while the rest are not). It is the
  2631. * callers duty to clean those orphaned rings.
  2632. *
  2633. * Return 0 on success, negative on failure
  2634. **/
  2635. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2636. {
  2637. int i, err = 0;
  2638. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2639. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2640. if (!i40e_enabled_xdp_vsi(vsi))
  2641. return err;
  2642. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2643. err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
  2644. return err;
  2645. }
  2646. /**
  2647. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2648. * @vsi: ptr to the VSI
  2649. *
  2650. * Free VSI's transmit software resources
  2651. **/
  2652. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2653. {
  2654. int i;
  2655. if (vsi->tx_rings) {
  2656. for (i = 0; i < vsi->num_queue_pairs; i++)
  2657. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2658. i40e_free_tx_resources(vsi->tx_rings[i]);
  2659. }
  2660. if (vsi->xdp_rings) {
  2661. for (i = 0; i < vsi->num_queue_pairs; i++)
  2662. if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
  2663. i40e_free_tx_resources(vsi->xdp_rings[i]);
  2664. }
  2665. }
  2666. /**
  2667. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2668. * @vsi: ptr to the VSI
  2669. *
  2670. * If this function returns with an error, then it's possible one or
  2671. * more of the rings is populated (while the rest are not). It is the
  2672. * callers duty to clean those orphaned rings.
  2673. *
  2674. * Return 0 on success, negative on failure
  2675. **/
  2676. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2677. {
  2678. int i, err = 0;
  2679. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2680. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2681. return err;
  2682. }
  2683. /**
  2684. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2685. * @vsi: ptr to the VSI
  2686. *
  2687. * Free all receive software resources
  2688. **/
  2689. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2690. {
  2691. int i;
  2692. if (!vsi->rx_rings)
  2693. return;
  2694. for (i = 0; i < vsi->num_queue_pairs; i++)
  2695. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2696. i40e_free_rx_resources(vsi->rx_rings[i]);
  2697. }
  2698. /**
  2699. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2700. * @ring: The Tx ring to configure
  2701. *
  2702. * This enables/disables XPS for a given Tx descriptor ring
  2703. * based on the TCs enabled for the VSI that ring belongs to.
  2704. **/
  2705. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2706. {
  2707. int cpu;
  2708. if (!ring->q_vector || !ring->netdev || ring->ch)
  2709. return;
  2710. /* We only initialize XPS once, so as not to overwrite user settings */
  2711. if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
  2712. return;
  2713. cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
  2714. netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
  2715. ring->queue_index);
  2716. }
  2717. /**
  2718. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2719. * @ring: The Tx ring to configure
  2720. *
  2721. * Configure the Tx descriptor ring in the HMC context.
  2722. **/
  2723. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2724. {
  2725. struct i40e_vsi *vsi = ring->vsi;
  2726. u16 pf_q = vsi->base_queue + ring->queue_index;
  2727. struct i40e_hw *hw = &vsi->back->hw;
  2728. struct i40e_hmc_obj_txq tx_ctx;
  2729. i40e_status err = 0;
  2730. u32 qtx_ctl = 0;
  2731. /* some ATR related tx ring init */
  2732. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2733. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2734. ring->atr_count = 0;
  2735. } else {
  2736. ring->atr_sample_rate = 0;
  2737. }
  2738. /* configure XPS */
  2739. i40e_config_xps_tx_ring(ring);
  2740. /* clear the context structure first */
  2741. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2742. tx_ctx.new_context = 1;
  2743. tx_ctx.base = (ring->dma / 128);
  2744. tx_ctx.qlen = ring->count;
  2745. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2746. I40E_FLAG_FD_ATR_ENABLED));
  2747. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2748. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2749. if (vsi->type != I40E_VSI_FDIR)
  2750. tx_ctx.head_wb_ena = 1;
  2751. tx_ctx.head_wb_addr = ring->dma +
  2752. (ring->count * sizeof(struct i40e_tx_desc));
  2753. /* As part of VSI creation/update, FW allocates certain
  2754. * Tx arbitration queue sets for each TC enabled for
  2755. * the VSI. The FW returns the handles to these queue
  2756. * sets as part of the response buffer to Add VSI,
  2757. * Update VSI, etc. AQ commands. It is expected that
  2758. * these queue set handles be associated with the Tx
  2759. * queues by the driver as part of the TX queue context
  2760. * initialization. This has to be done regardless of
  2761. * DCB as by default everything is mapped to TC0.
  2762. */
  2763. if (ring->ch)
  2764. tx_ctx.rdylist =
  2765. le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
  2766. else
  2767. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2768. tx_ctx.rdylist_act = 0;
  2769. /* clear the context in the HMC */
  2770. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2771. if (err) {
  2772. dev_info(&vsi->back->pdev->dev,
  2773. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2774. ring->queue_index, pf_q, err);
  2775. return -ENOMEM;
  2776. }
  2777. /* set the context in the HMC */
  2778. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2779. if (err) {
  2780. dev_info(&vsi->back->pdev->dev,
  2781. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2782. ring->queue_index, pf_q, err);
  2783. return -ENOMEM;
  2784. }
  2785. /* Now associate this queue with this PCI function */
  2786. if (ring->ch) {
  2787. if (ring->ch->type == I40E_VSI_VMDQ2)
  2788. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2789. else
  2790. return -EINVAL;
  2791. qtx_ctl |= (ring->ch->vsi_number <<
  2792. I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2793. I40E_QTX_CTL_VFVM_INDX_MASK;
  2794. } else {
  2795. if (vsi->type == I40E_VSI_VMDQ2) {
  2796. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2797. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2798. I40E_QTX_CTL_VFVM_INDX_MASK;
  2799. } else {
  2800. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2801. }
  2802. }
  2803. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2804. I40E_QTX_CTL_PF_INDX_MASK);
  2805. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2806. i40e_flush(hw);
  2807. /* cache tail off for easier writes later */
  2808. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2809. return 0;
  2810. }
  2811. /**
  2812. * i40e_configure_rx_ring - Configure a receive ring context
  2813. * @ring: The Rx ring to configure
  2814. *
  2815. * Configure the Rx descriptor ring in the HMC context.
  2816. **/
  2817. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2818. {
  2819. struct i40e_vsi *vsi = ring->vsi;
  2820. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2821. u16 pf_q = vsi->base_queue + ring->queue_index;
  2822. struct i40e_hw *hw = &vsi->back->hw;
  2823. struct i40e_hmc_obj_rxq rx_ctx;
  2824. i40e_status err = 0;
  2825. bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
  2826. /* clear the context structure first */
  2827. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2828. ring->rx_buf_len = vsi->rx_buf_len;
  2829. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2830. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2831. rx_ctx.base = (ring->dma / 128);
  2832. rx_ctx.qlen = ring->count;
  2833. /* use 32 byte descriptors */
  2834. rx_ctx.dsize = 1;
  2835. /* descriptor type is always zero
  2836. * rx_ctx.dtype = 0;
  2837. */
  2838. rx_ctx.hsplit_0 = 0;
  2839. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2840. if (hw->revision_id == 0)
  2841. rx_ctx.lrxqthresh = 0;
  2842. else
  2843. rx_ctx.lrxqthresh = 1;
  2844. rx_ctx.crcstrip = 1;
  2845. rx_ctx.l2tsel = 1;
  2846. /* this controls whether VLAN is stripped from inner headers */
  2847. rx_ctx.showiv = 0;
  2848. /* set the prefena field to 1 because the manual says to */
  2849. rx_ctx.prefena = 1;
  2850. /* clear the context in the HMC */
  2851. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2852. if (err) {
  2853. dev_info(&vsi->back->pdev->dev,
  2854. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2855. ring->queue_index, pf_q, err);
  2856. return -ENOMEM;
  2857. }
  2858. /* set the context in the HMC */
  2859. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2860. if (err) {
  2861. dev_info(&vsi->back->pdev->dev,
  2862. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2863. ring->queue_index, pf_q, err);
  2864. return -ENOMEM;
  2865. }
  2866. /* configure Rx buffer alignment */
  2867. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2868. clear_ring_build_skb_enabled(ring);
  2869. else
  2870. set_ring_build_skb_enabled(ring);
  2871. /* cache tail for quicker writes, and clear the reg before use */
  2872. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2873. writel(0, ring->tail);
  2874. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2875. return 0;
  2876. }
  2877. /**
  2878. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2879. * @vsi: VSI structure describing this set of rings and resources
  2880. *
  2881. * Configure the Tx VSI for operation.
  2882. **/
  2883. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2884. {
  2885. int err = 0;
  2886. u16 i;
  2887. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2888. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2889. if (!i40e_enabled_xdp_vsi(vsi))
  2890. return err;
  2891. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2892. err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
  2893. return err;
  2894. }
  2895. /**
  2896. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2897. * @vsi: the VSI being configured
  2898. *
  2899. * Configure the Rx VSI for operation.
  2900. **/
  2901. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2902. {
  2903. int err = 0;
  2904. u16 i;
  2905. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2906. vsi->max_frame = I40E_MAX_RXBUFFER;
  2907. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2908. #if (PAGE_SIZE < 8192)
  2909. } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  2910. (vsi->netdev->mtu <= ETH_DATA_LEN)) {
  2911. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2912. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2913. #endif
  2914. } else {
  2915. vsi->max_frame = I40E_MAX_RXBUFFER;
  2916. vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
  2917. I40E_RXBUFFER_2048;
  2918. }
  2919. /* set up individual rings */
  2920. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2921. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2922. return err;
  2923. }
  2924. /**
  2925. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2926. * @vsi: ptr to the VSI
  2927. **/
  2928. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2929. {
  2930. struct i40e_ring *tx_ring, *rx_ring;
  2931. u16 qoffset, qcount;
  2932. int i, n;
  2933. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2934. /* Reset the TC information */
  2935. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2936. rx_ring = vsi->rx_rings[i];
  2937. tx_ring = vsi->tx_rings[i];
  2938. rx_ring->dcb_tc = 0;
  2939. tx_ring->dcb_tc = 0;
  2940. }
  2941. return;
  2942. }
  2943. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2944. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2945. continue;
  2946. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2947. qcount = vsi->tc_config.tc_info[n].qcount;
  2948. for (i = qoffset; i < (qoffset + qcount); i++) {
  2949. rx_ring = vsi->rx_rings[i];
  2950. tx_ring = vsi->tx_rings[i];
  2951. rx_ring->dcb_tc = n;
  2952. tx_ring->dcb_tc = n;
  2953. }
  2954. }
  2955. }
  2956. /**
  2957. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2958. * @vsi: ptr to the VSI
  2959. **/
  2960. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2961. {
  2962. if (vsi->netdev)
  2963. i40e_set_rx_mode(vsi->netdev);
  2964. }
  2965. /**
  2966. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2967. * @vsi: Pointer to the targeted VSI
  2968. *
  2969. * This function replays the hlist on the hw where all the SB Flow Director
  2970. * filters were saved.
  2971. **/
  2972. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2973. {
  2974. struct i40e_fdir_filter *filter;
  2975. struct i40e_pf *pf = vsi->back;
  2976. struct hlist_node *node;
  2977. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2978. return;
  2979. /* Reset FDir counters as we're replaying all existing filters */
  2980. pf->fd_tcp4_filter_cnt = 0;
  2981. pf->fd_udp4_filter_cnt = 0;
  2982. pf->fd_sctp4_filter_cnt = 0;
  2983. pf->fd_ip4_filter_cnt = 0;
  2984. hlist_for_each_entry_safe(filter, node,
  2985. &pf->fdir_filter_list, fdir_node) {
  2986. i40e_add_del_fdir(vsi, filter, true);
  2987. }
  2988. }
  2989. /**
  2990. * i40e_vsi_configure - Set up the VSI for action
  2991. * @vsi: the VSI being configured
  2992. **/
  2993. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2994. {
  2995. int err;
  2996. i40e_set_vsi_rx_mode(vsi);
  2997. i40e_restore_vlan(vsi);
  2998. i40e_vsi_config_dcb_rings(vsi);
  2999. err = i40e_vsi_configure_tx(vsi);
  3000. if (!err)
  3001. err = i40e_vsi_configure_rx(vsi);
  3002. return err;
  3003. }
  3004. /**
  3005. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  3006. * @vsi: the VSI being configured
  3007. **/
  3008. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  3009. {
  3010. bool has_xdp = i40e_enabled_xdp_vsi(vsi);
  3011. struct i40e_pf *pf = vsi->back;
  3012. struct i40e_hw *hw = &pf->hw;
  3013. u16 vector;
  3014. int i, q;
  3015. u32 qp;
  3016. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  3017. * and PFINT_LNKLSTn registers, e.g.:
  3018. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  3019. */
  3020. qp = vsi->base_queue;
  3021. vector = vsi->base_vector;
  3022. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  3023. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  3024. q_vector->rx.next_update = jiffies + 1;
  3025. q_vector->rx.target_itr =
  3026. ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
  3027. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  3028. q_vector->rx.target_itr);
  3029. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3030. q_vector->tx.next_update = jiffies + 1;
  3031. q_vector->tx.target_itr =
  3032. ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
  3033. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  3034. q_vector->tx.target_itr);
  3035. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3036. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  3037. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  3038. /* Linked list for the queuepairs assigned to this vector */
  3039. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  3040. for (q = 0; q < q_vector->num_ringpairs; q++) {
  3041. u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
  3042. u32 val;
  3043. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3044. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3045. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  3046. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  3047. (I40E_QUEUE_TYPE_TX <<
  3048. I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  3049. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3050. if (has_xdp) {
  3051. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3052. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3053. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3054. (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3055. (I40E_QUEUE_TYPE_TX <<
  3056. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3057. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3058. }
  3059. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3060. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3061. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3062. ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3063. (I40E_QUEUE_TYPE_RX <<
  3064. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3065. /* Terminate the linked list */
  3066. if (q == (q_vector->num_ringpairs - 1))
  3067. val |= (I40E_QUEUE_END_OF_LIST <<
  3068. I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3069. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3070. qp++;
  3071. }
  3072. }
  3073. i40e_flush(hw);
  3074. }
  3075. /**
  3076. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  3077. * @pf: pointer to private device data structure
  3078. **/
  3079. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  3080. {
  3081. struct i40e_hw *hw = &pf->hw;
  3082. u32 val;
  3083. /* clear things first */
  3084. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  3085. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  3086. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  3087. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  3088. I40E_PFINT_ICR0_ENA_GRST_MASK |
  3089. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  3090. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  3091. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  3092. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  3093. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3094. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  3095. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3096. if (pf->flags & I40E_FLAG_PTP)
  3097. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3098. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  3099. /* SW_ITR_IDX = 0, but don't change INTENA */
  3100. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  3101. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  3102. /* OTHER_ITR_IDX = 0 */
  3103. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  3104. }
  3105. /**
  3106. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  3107. * @vsi: the VSI being configured
  3108. **/
  3109. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  3110. {
  3111. u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
  3112. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3113. struct i40e_pf *pf = vsi->back;
  3114. struct i40e_hw *hw = &pf->hw;
  3115. u32 val;
  3116. /* set the ITR configuration */
  3117. q_vector->rx.next_update = jiffies + 1;
  3118. q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
  3119. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
  3120. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3121. q_vector->tx.next_update = jiffies + 1;
  3122. q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
  3123. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
  3124. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3125. i40e_enable_misc_int_causes(pf);
  3126. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  3127. wr32(hw, I40E_PFINT_LNKLST0, 0);
  3128. /* Associate the queue pair to the vector and enable the queue int */
  3129. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3130. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3131. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  3132. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3133. wr32(hw, I40E_QINT_RQCTL(0), val);
  3134. if (i40e_enabled_xdp_vsi(vsi)) {
  3135. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3136. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
  3137. (I40E_QUEUE_TYPE_TX
  3138. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3139. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3140. }
  3141. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3142. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3143. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3144. wr32(hw, I40E_QINT_TQCTL(0), val);
  3145. i40e_flush(hw);
  3146. }
  3147. /**
  3148. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  3149. * @pf: board private structure
  3150. **/
  3151. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  3152. {
  3153. struct i40e_hw *hw = &pf->hw;
  3154. wr32(hw, I40E_PFINT_DYN_CTL0,
  3155. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  3156. i40e_flush(hw);
  3157. }
  3158. /**
  3159. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  3160. * @pf: board private structure
  3161. **/
  3162. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  3163. {
  3164. struct i40e_hw *hw = &pf->hw;
  3165. u32 val;
  3166. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3167. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  3168. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  3169. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  3170. i40e_flush(hw);
  3171. }
  3172. /**
  3173. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3174. * @irq: interrupt number
  3175. * @data: pointer to a q_vector
  3176. **/
  3177. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3178. {
  3179. struct i40e_q_vector *q_vector = data;
  3180. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3181. return IRQ_HANDLED;
  3182. napi_schedule_irqoff(&q_vector->napi);
  3183. return IRQ_HANDLED;
  3184. }
  3185. /**
  3186. * i40e_irq_affinity_notify - Callback for affinity changes
  3187. * @notify: context as to what irq was changed
  3188. * @mask: the new affinity mask
  3189. *
  3190. * This is a callback function used by the irq_set_affinity_notifier function
  3191. * so that we may register to receive changes to the irq affinity masks.
  3192. **/
  3193. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3194. const cpumask_t *mask)
  3195. {
  3196. struct i40e_q_vector *q_vector =
  3197. container_of(notify, struct i40e_q_vector, affinity_notify);
  3198. cpumask_copy(&q_vector->affinity_mask, mask);
  3199. }
  3200. /**
  3201. * i40e_irq_affinity_release - Callback for affinity notifier release
  3202. * @ref: internal core kernel usage
  3203. *
  3204. * This is a callback function used by the irq_set_affinity_notifier function
  3205. * to inform the current notification subscriber that they will no longer
  3206. * receive notifications.
  3207. **/
  3208. static void i40e_irq_affinity_release(struct kref *ref) {}
  3209. /**
  3210. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3211. * @vsi: the VSI being configured
  3212. * @basename: name for the vector
  3213. *
  3214. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3215. **/
  3216. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3217. {
  3218. int q_vectors = vsi->num_q_vectors;
  3219. struct i40e_pf *pf = vsi->back;
  3220. int base = vsi->base_vector;
  3221. int rx_int_idx = 0;
  3222. int tx_int_idx = 0;
  3223. int vector, err;
  3224. int irq_num;
  3225. int cpu;
  3226. for (vector = 0; vector < q_vectors; vector++) {
  3227. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3228. irq_num = pf->msix_entries[base + vector].vector;
  3229. if (q_vector->tx.ring && q_vector->rx.ring) {
  3230. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3231. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3232. tx_int_idx++;
  3233. } else if (q_vector->rx.ring) {
  3234. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3235. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3236. } else if (q_vector->tx.ring) {
  3237. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3238. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3239. } else {
  3240. /* skip this unused q_vector */
  3241. continue;
  3242. }
  3243. err = request_irq(irq_num,
  3244. vsi->irq_handler,
  3245. 0,
  3246. q_vector->name,
  3247. q_vector);
  3248. if (err) {
  3249. dev_info(&pf->pdev->dev,
  3250. "MSIX request_irq failed, error: %d\n", err);
  3251. goto free_queue_irqs;
  3252. }
  3253. /* register for affinity change notifications */
  3254. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3255. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3256. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3257. /* Spread affinity hints out across online CPUs.
  3258. *
  3259. * get_cpu_mask returns a static constant mask with
  3260. * a permanent lifetime so it's ok to pass to
  3261. * irq_set_affinity_hint without making a copy.
  3262. */
  3263. cpu = cpumask_local_spread(q_vector->v_idx, -1);
  3264. irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
  3265. }
  3266. vsi->irqs_ready = true;
  3267. return 0;
  3268. free_queue_irqs:
  3269. while (vector) {
  3270. vector--;
  3271. irq_num = pf->msix_entries[base + vector].vector;
  3272. irq_set_affinity_notifier(irq_num, NULL);
  3273. irq_set_affinity_hint(irq_num, NULL);
  3274. free_irq(irq_num, &vsi->q_vectors[vector]);
  3275. }
  3276. return err;
  3277. }
  3278. /**
  3279. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3280. * @vsi: the VSI being un-configured
  3281. **/
  3282. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3283. {
  3284. struct i40e_pf *pf = vsi->back;
  3285. struct i40e_hw *hw = &pf->hw;
  3286. int base = vsi->base_vector;
  3287. int i;
  3288. /* disable interrupt causation from each queue */
  3289. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3290. u32 val;
  3291. val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3292. val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  3293. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3294. val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
  3295. val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  3296. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
  3297. if (!i40e_enabled_xdp_vsi(vsi))
  3298. continue;
  3299. wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
  3300. }
  3301. /* disable each interrupt */
  3302. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3303. for (i = vsi->base_vector;
  3304. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3305. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3306. i40e_flush(hw);
  3307. for (i = 0; i < vsi->num_q_vectors; i++)
  3308. synchronize_irq(pf->msix_entries[i + base].vector);
  3309. } else {
  3310. /* Legacy and MSI mode - this stops all interrupt handling */
  3311. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3312. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3313. i40e_flush(hw);
  3314. synchronize_irq(pf->pdev->irq);
  3315. }
  3316. }
  3317. /**
  3318. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3319. * @vsi: the VSI being configured
  3320. **/
  3321. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3322. {
  3323. struct i40e_pf *pf = vsi->back;
  3324. int i;
  3325. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3326. for (i = 0; i < vsi->num_q_vectors; i++)
  3327. i40e_irq_dynamic_enable(vsi, i);
  3328. } else {
  3329. i40e_irq_dynamic_enable_icr0(pf);
  3330. }
  3331. i40e_flush(&pf->hw);
  3332. return 0;
  3333. }
  3334. /**
  3335. * i40e_free_misc_vector - Free the vector that handles non-queue events
  3336. * @pf: board private structure
  3337. **/
  3338. static void i40e_free_misc_vector(struct i40e_pf *pf)
  3339. {
  3340. /* Disable ICR 0 */
  3341. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3342. i40e_flush(&pf->hw);
  3343. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3344. synchronize_irq(pf->msix_entries[0].vector);
  3345. free_irq(pf->msix_entries[0].vector, pf);
  3346. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  3347. }
  3348. }
  3349. /**
  3350. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3351. * @irq: interrupt number
  3352. * @data: pointer to a q_vector
  3353. *
  3354. * This is the handler used for all MSI/Legacy interrupts, and deals
  3355. * with both queue and non-queue interrupts. This is also used in
  3356. * MSIX mode to handle the non-queue interrupts.
  3357. **/
  3358. static irqreturn_t i40e_intr(int irq, void *data)
  3359. {
  3360. struct i40e_pf *pf = (struct i40e_pf *)data;
  3361. struct i40e_hw *hw = &pf->hw;
  3362. irqreturn_t ret = IRQ_NONE;
  3363. u32 icr0, icr0_remaining;
  3364. u32 val, ena_mask;
  3365. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3366. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3367. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3368. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3369. goto enable_intr;
  3370. /* if interrupt but no bits showing, must be SWINT */
  3371. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3372. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3373. pf->sw_int_count++;
  3374. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3375. (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3376. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3377. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3378. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  3379. }
  3380. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3381. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3382. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3383. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3384. /* We do not have a way to disarm Queue causes while leaving
  3385. * interrupt enabled for all other causes, ideally
  3386. * interrupt should be disabled while we are in NAPI but
  3387. * this is not a performance path and napi_schedule()
  3388. * can deal with rescheduling.
  3389. */
  3390. if (!test_bit(__I40E_DOWN, pf->state))
  3391. napi_schedule_irqoff(&q_vector->napi);
  3392. }
  3393. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3394. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3395. set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  3396. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3397. }
  3398. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3399. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3400. set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  3401. }
  3402. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3403. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3404. set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3405. }
  3406. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3407. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  3408. set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  3409. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3410. val = rd32(hw, I40E_GLGEN_RSTAT);
  3411. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3412. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3413. if (val == I40E_RESET_CORER) {
  3414. pf->corer_count++;
  3415. } else if (val == I40E_RESET_GLOBR) {
  3416. pf->globr_count++;
  3417. } else if (val == I40E_RESET_EMPR) {
  3418. pf->empr_count++;
  3419. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
  3420. }
  3421. }
  3422. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3423. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3424. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3425. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3426. rd32(hw, I40E_PFHMC_ERRORINFO),
  3427. rd32(hw, I40E_PFHMC_ERRORDATA));
  3428. }
  3429. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3430. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3431. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3432. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3433. i40e_ptp_tx_hwtstamp(pf);
  3434. }
  3435. }
  3436. /* If a critical error is pending we have no choice but to reset the
  3437. * device.
  3438. * Report and mask out any remaining unexpected interrupts.
  3439. */
  3440. icr0_remaining = icr0 & ena_mask;
  3441. if (icr0_remaining) {
  3442. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3443. icr0_remaining);
  3444. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3445. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3446. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3447. dev_info(&pf->pdev->dev, "device will be reset\n");
  3448. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  3449. i40e_service_event_schedule(pf);
  3450. }
  3451. ena_mask &= ~icr0_remaining;
  3452. }
  3453. ret = IRQ_HANDLED;
  3454. enable_intr:
  3455. /* re-enable interrupt causes */
  3456. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3457. if (!test_bit(__I40E_DOWN, pf->state)) {
  3458. i40e_service_event_schedule(pf);
  3459. i40e_irq_dynamic_enable_icr0(pf);
  3460. }
  3461. return ret;
  3462. }
  3463. /**
  3464. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3465. * @tx_ring: tx ring to clean
  3466. * @budget: how many cleans we're allowed
  3467. *
  3468. * Returns true if there's any budget left (e.g. the clean is finished)
  3469. **/
  3470. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3471. {
  3472. struct i40e_vsi *vsi = tx_ring->vsi;
  3473. u16 i = tx_ring->next_to_clean;
  3474. struct i40e_tx_buffer *tx_buf;
  3475. struct i40e_tx_desc *tx_desc;
  3476. tx_buf = &tx_ring->tx_bi[i];
  3477. tx_desc = I40E_TX_DESC(tx_ring, i);
  3478. i -= tx_ring->count;
  3479. do {
  3480. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3481. /* if next_to_watch is not set then there is no work pending */
  3482. if (!eop_desc)
  3483. break;
  3484. /* prevent any other reads prior to eop_desc */
  3485. smp_rmb();
  3486. /* if the descriptor isn't done, no work yet to do */
  3487. if (!(eop_desc->cmd_type_offset_bsz &
  3488. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3489. break;
  3490. /* clear next_to_watch to prevent false hangs */
  3491. tx_buf->next_to_watch = NULL;
  3492. tx_desc->buffer_addr = 0;
  3493. tx_desc->cmd_type_offset_bsz = 0;
  3494. /* move past filter desc */
  3495. tx_buf++;
  3496. tx_desc++;
  3497. i++;
  3498. if (unlikely(!i)) {
  3499. i -= tx_ring->count;
  3500. tx_buf = tx_ring->tx_bi;
  3501. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3502. }
  3503. /* unmap skb header data */
  3504. dma_unmap_single(tx_ring->dev,
  3505. dma_unmap_addr(tx_buf, dma),
  3506. dma_unmap_len(tx_buf, len),
  3507. DMA_TO_DEVICE);
  3508. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3509. kfree(tx_buf->raw_buf);
  3510. tx_buf->raw_buf = NULL;
  3511. tx_buf->tx_flags = 0;
  3512. tx_buf->next_to_watch = NULL;
  3513. dma_unmap_len_set(tx_buf, len, 0);
  3514. tx_desc->buffer_addr = 0;
  3515. tx_desc->cmd_type_offset_bsz = 0;
  3516. /* move us past the eop_desc for start of next FD desc */
  3517. tx_buf++;
  3518. tx_desc++;
  3519. i++;
  3520. if (unlikely(!i)) {
  3521. i -= tx_ring->count;
  3522. tx_buf = tx_ring->tx_bi;
  3523. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3524. }
  3525. /* update budget accounting */
  3526. budget--;
  3527. } while (likely(budget));
  3528. i += tx_ring->count;
  3529. tx_ring->next_to_clean = i;
  3530. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3531. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3532. return budget > 0;
  3533. }
  3534. /**
  3535. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3536. * @irq: interrupt number
  3537. * @data: pointer to a q_vector
  3538. **/
  3539. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3540. {
  3541. struct i40e_q_vector *q_vector = data;
  3542. struct i40e_vsi *vsi;
  3543. if (!q_vector->tx.ring)
  3544. return IRQ_HANDLED;
  3545. vsi = q_vector->tx.ring->vsi;
  3546. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3547. return IRQ_HANDLED;
  3548. }
  3549. /**
  3550. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3551. * @vsi: the VSI being configured
  3552. * @v_idx: vector index
  3553. * @qp_idx: queue pair index
  3554. **/
  3555. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3556. {
  3557. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3558. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3559. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3560. tx_ring->q_vector = q_vector;
  3561. tx_ring->next = q_vector->tx.ring;
  3562. q_vector->tx.ring = tx_ring;
  3563. q_vector->tx.count++;
  3564. /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
  3565. if (i40e_enabled_xdp_vsi(vsi)) {
  3566. struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
  3567. xdp_ring->q_vector = q_vector;
  3568. xdp_ring->next = q_vector->tx.ring;
  3569. q_vector->tx.ring = xdp_ring;
  3570. q_vector->tx.count++;
  3571. }
  3572. rx_ring->q_vector = q_vector;
  3573. rx_ring->next = q_vector->rx.ring;
  3574. q_vector->rx.ring = rx_ring;
  3575. q_vector->rx.count++;
  3576. }
  3577. /**
  3578. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3579. * @vsi: the VSI being configured
  3580. *
  3581. * This function maps descriptor rings to the queue-specific vectors
  3582. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3583. * one vector per queue pair, but on a constrained vector budget, we
  3584. * group the queue pairs as "efficiently" as possible.
  3585. **/
  3586. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3587. {
  3588. int qp_remaining = vsi->num_queue_pairs;
  3589. int q_vectors = vsi->num_q_vectors;
  3590. int num_ringpairs;
  3591. int v_start = 0;
  3592. int qp_idx = 0;
  3593. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3594. * group them so there are multiple queues per vector.
  3595. * It is also important to go through all the vectors available to be
  3596. * sure that if we don't use all the vectors, that the remaining vectors
  3597. * are cleared. This is especially important when decreasing the
  3598. * number of queues in use.
  3599. */
  3600. for (; v_start < q_vectors; v_start++) {
  3601. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3602. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3603. q_vector->num_ringpairs = num_ringpairs;
  3604. q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
  3605. q_vector->rx.count = 0;
  3606. q_vector->tx.count = 0;
  3607. q_vector->rx.ring = NULL;
  3608. q_vector->tx.ring = NULL;
  3609. while (num_ringpairs--) {
  3610. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3611. qp_idx++;
  3612. qp_remaining--;
  3613. }
  3614. }
  3615. }
  3616. /**
  3617. * i40e_vsi_request_irq - Request IRQ from the OS
  3618. * @vsi: the VSI being configured
  3619. * @basename: name for the vector
  3620. **/
  3621. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3622. {
  3623. struct i40e_pf *pf = vsi->back;
  3624. int err;
  3625. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3626. err = i40e_vsi_request_irq_msix(vsi, basename);
  3627. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3628. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3629. pf->int_name, pf);
  3630. else
  3631. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3632. pf->int_name, pf);
  3633. if (err)
  3634. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3635. return err;
  3636. }
  3637. #ifdef CONFIG_NET_POLL_CONTROLLER
  3638. /**
  3639. * i40e_netpoll - A Polling 'interrupt' handler
  3640. * @netdev: network interface device structure
  3641. *
  3642. * This is used by netconsole to send skbs without having to re-enable
  3643. * interrupts. It's not called while the normal interrupt routine is executing.
  3644. **/
  3645. static void i40e_netpoll(struct net_device *netdev)
  3646. {
  3647. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3648. struct i40e_vsi *vsi = np->vsi;
  3649. struct i40e_pf *pf = vsi->back;
  3650. int i;
  3651. /* if interface is down do nothing */
  3652. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3653. return;
  3654. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3655. for (i = 0; i < vsi->num_q_vectors; i++)
  3656. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3657. } else {
  3658. i40e_intr(pf->pdev->irq, netdev);
  3659. }
  3660. }
  3661. #endif
  3662. #define I40E_QTX_ENA_WAIT_COUNT 50
  3663. /**
  3664. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3665. * @pf: the PF being configured
  3666. * @pf_q: the PF queue
  3667. * @enable: enable or disable state of the queue
  3668. *
  3669. * This routine will wait for the given Tx queue of the PF to reach the
  3670. * enabled or disabled state.
  3671. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3672. * multiple retries; else will return 0 in case of success.
  3673. **/
  3674. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3675. {
  3676. int i;
  3677. u32 tx_reg;
  3678. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3679. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3680. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3681. break;
  3682. usleep_range(10, 20);
  3683. }
  3684. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3685. return -ETIMEDOUT;
  3686. return 0;
  3687. }
  3688. /**
  3689. * i40e_control_tx_q - Start or stop a particular Tx queue
  3690. * @pf: the PF structure
  3691. * @pf_q: the PF queue to configure
  3692. * @enable: start or stop the queue
  3693. *
  3694. * This function enables or disables a single queue. Note that any delay
  3695. * required after the operation is expected to be handled by the caller of
  3696. * this function.
  3697. **/
  3698. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3699. {
  3700. struct i40e_hw *hw = &pf->hw;
  3701. u32 tx_reg;
  3702. int i;
  3703. /* warn the TX unit of coming changes */
  3704. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3705. if (!enable)
  3706. usleep_range(10, 20);
  3707. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3708. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3709. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3710. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3711. break;
  3712. usleep_range(1000, 2000);
  3713. }
  3714. /* Skip if the queue is already in the requested state */
  3715. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3716. return;
  3717. /* turn on/off the queue */
  3718. if (enable) {
  3719. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3720. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3721. } else {
  3722. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3723. }
  3724. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3725. }
  3726. /**
  3727. * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
  3728. * @seid: VSI SEID
  3729. * @pf: the PF structure
  3730. * @pf_q: the PF queue to configure
  3731. * @is_xdp: true if the queue is used for XDP
  3732. * @enable: start or stop the queue
  3733. **/
  3734. int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
  3735. bool is_xdp, bool enable)
  3736. {
  3737. int ret;
  3738. i40e_control_tx_q(pf, pf_q, enable);
  3739. /* wait for the change to finish */
  3740. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3741. if (ret) {
  3742. dev_info(&pf->pdev->dev,
  3743. "VSI seid %d %sTx ring %d %sable timeout\n",
  3744. seid, (is_xdp ? "XDP " : ""), pf_q,
  3745. (enable ? "en" : "dis"));
  3746. }
  3747. return ret;
  3748. }
  3749. /**
  3750. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3751. * @vsi: the VSI being configured
  3752. * @enable: start or stop the rings
  3753. **/
  3754. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3755. {
  3756. struct i40e_pf *pf = vsi->back;
  3757. int i, pf_q, ret = 0;
  3758. pf_q = vsi->base_queue;
  3759. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3760. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3761. pf_q,
  3762. false /*is xdp*/, enable);
  3763. if (ret)
  3764. break;
  3765. if (!i40e_enabled_xdp_vsi(vsi))
  3766. continue;
  3767. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3768. pf_q + vsi->alloc_queue_pairs,
  3769. true /*is xdp*/, enable);
  3770. if (ret)
  3771. break;
  3772. }
  3773. return ret;
  3774. }
  3775. /**
  3776. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3777. * @pf: the PF being configured
  3778. * @pf_q: the PF queue
  3779. * @enable: enable or disable state of the queue
  3780. *
  3781. * This routine will wait for the given Rx queue of the PF to reach the
  3782. * enabled or disabled state.
  3783. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3784. * multiple retries; else will return 0 in case of success.
  3785. **/
  3786. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3787. {
  3788. int i;
  3789. u32 rx_reg;
  3790. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3791. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3792. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3793. break;
  3794. usleep_range(10, 20);
  3795. }
  3796. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3797. return -ETIMEDOUT;
  3798. return 0;
  3799. }
  3800. /**
  3801. * i40e_control_rx_q - Start or stop a particular Rx queue
  3802. * @pf: the PF structure
  3803. * @pf_q: the PF queue to configure
  3804. * @enable: start or stop the queue
  3805. *
  3806. * This function enables or disables a single queue. Note that
  3807. * any delay required after the operation is expected to be
  3808. * handled by the caller of this function.
  3809. **/
  3810. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3811. {
  3812. struct i40e_hw *hw = &pf->hw;
  3813. u32 rx_reg;
  3814. int i;
  3815. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3816. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3817. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3818. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3819. break;
  3820. usleep_range(1000, 2000);
  3821. }
  3822. /* Skip if the queue is already in the requested state */
  3823. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3824. return;
  3825. /* turn on/off the queue */
  3826. if (enable)
  3827. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3828. else
  3829. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3830. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3831. }
  3832. /**
  3833. * i40e_control_wait_rx_q
  3834. * @pf: the PF structure
  3835. * @pf_q: queue being configured
  3836. * @enable: start or stop the rings
  3837. *
  3838. * This function enables or disables a single queue along with waiting
  3839. * for the change to finish. The caller of this function should handle
  3840. * the delays needed in the case of disabling queues.
  3841. **/
  3842. int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3843. {
  3844. int ret = 0;
  3845. i40e_control_rx_q(pf, pf_q, enable);
  3846. /* wait for the change to finish */
  3847. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3848. if (ret)
  3849. return ret;
  3850. return ret;
  3851. }
  3852. /**
  3853. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3854. * @vsi: the VSI being configured
  3855. * @enable: start or stop the rings
  3856. **/
  3857. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3858. {
  3859. struct i40e_pf *pf = vsi->back;
  3860. int i, pf_q, ret = 0;
  3861. pf_q = vsi->base_queue;
  3862. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3863. ret = i40e_control_wait_rx_q(pf, pf_q, enable);
  3864. if (ret) {
  3865. dev_info(&pf->pdev->dev,
  3866. "VSI seid %d Rx ring %d %sable timeout\n",
  3867. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3868. break;
  3869. }
  3870. }
  3871. /* Due to HW errata, on Rx disable only, the register can indicate done
  3872. * before it really is. Needs 50ms to be sure
  3873. */
  3874. if (!enable)
  3875. mdelay(50);
  3876. return ret;
  3877. }
  3878. /**
  3879. * i40e_vsi_start_rings - Start a VSI's rings
  3880. * @vsi: the VSI being configured
  3881. **/
  3882. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3883. {
  3884. int ret = 0;
  3885. /* do rx first for enable and last for disable */
  3886. ret = i40e_vsi_control_rx(vsi, true);
  3887. if (ret)
  3888. return ret;
  3889. ret = i40e_vsi_control_tx(vsi, true);
  3890. return ret;
  3891. }
  3892. /**
  3893. * i40e_vsi_stop_rings - Stop a VSI's rings
  3894. * @vsi: the VSI being configured
  3895. **/
  3896. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3897. {
  3898. /* When port TX is suspended, don't wait */
  3899. if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
  3900. return i40e_vsi_stop_rings_no_wait(vsi);
  3901. /* do rx first for enable and last for disable
  3902. * Ignore return value, we need to shutdown whatever we can
  3903. */
  3904. i40e_vsi_control_tx(vsi, false);
  3905. i40e_vsi_control_rx(vsi, false);
  3906. }
  3907. /**
  3908. * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
  3909. * @vsi: the VSI being shutdown
  3910. *
  3911. * This function stops all the rings for a VSI but does not delay to verify
  3912. * that rings have been disabled. It is expected that the caller is shutting
  3913. * down multiple VSIs at once and will delay together for all the VSIs after
  3914. * initiating the shutdown. This is particularly useful for shutting down lots
  3915. * of VFs together. Otherwise, a large delay can be incurred while configuring
  3916. * each VSI in serial.
  3917. **/
  3918. void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
  3919. {
  3920. struct i40e_pf *pf = vsi->back;
  3921. int i, pf_q;
  3922. pf_q = vsi->base_queue;
  3923. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3924. i40e_control_tx_q(pf, pf_q, false);
  3925. i40e_control_rx_q(pf, pf_q, false);
  3926. }
  3927. }
  3928. /**
  3929. * i40e_vsi_free_irq - Free the irq association with the OS
  3930. * @vsi: the VSI being configured
  3931. **/
  3932. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3933. {
  3934. struct i40e_pf *pf = vsi->back;
  3935. struct i40e_hw *hw = &pf->hw;
  3936. int base = vsi->base_vector;
  3937. u32 val, qp;
  3938. int i;
  3939. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3940. if (!vsi->q_vectors)
  3941. return;
  3942. if (!vsi->irqs_ready)
  3943. return;
  3944. vsi->irqs_ready = false;
  3945. for (i = 0; i < vsi->num_q_vectors; i++) {
  3946. int irq_num;
  3947. u16 vector;
  3948. vector = i + base;
  3949. irq_num = pf->msix_entries[vector].vector;
  3950. /* free only the irqs that were actually requested */
  3951. if (!vsi->q_vectors[i] ||
  3952. !vsi->q_vectors[i]->num_ringpairs)
  3953. continue;
  3954. /* clear the affinity notifier in the IRQ descriptor */
  3955. irq_set_affinity_notifier(irq_num, NULL);
  3956. /* remove our suggested affinity mask for this IRQ */
  3957. irq_set_affinity_hint(irq_num, NULL);
  3958. synchronize_irq(irq_num);
  3959. free_irq(irq_num, vsi->q_vectors[i]);
  3960. /* Tear down the interrupt queue link list
  3961. *
  3962. * We know that they come in pairs and always
  3963. * the Rx first, then the Tx. To clear the
  3964. * link list, stick the EOL value into the
  3965. * next_q field of the registers.
  3966. */
  3967. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3968. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3969. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3970. val |= I40E_QUEUE_END_OF_LIST
  3971. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3972. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3973. while (qp != I40E_QUEUE_END_OF_LIST) {
  3974. u32 next;
  3975. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3976. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3977. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3978. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3979. I40E_QINT_RQCTL_INTEVENT_MASK);
  3980. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3981. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3982. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3983. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3984. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3985. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3986. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3987. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3988. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3989. I40E_QINT_TQCTL_INTEVENT_MASK);
  3990. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3991. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3992. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3993. qp = next;
  3994. }
  3995. }
  3996. } else {
  3997. free_irq(pf->pdev->irq, pf);
  3998. val = rd32(hw, I40E_PFINT_LNKLST0);
  3999. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  4000. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  4001. val |= I40E_QUEUE_END_OF_LIST
  4002. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  4003. wr32(hw, I40E_PFINT_LNKLST0, val);
  4004. val = rd32(hw, I40E_QINT_RQCTL(qp));
  4005. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  4006. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  4007. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  4008. I40E_QINT_RQCTL_INTEVENT_MASK);
  4009. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  4010. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  4011. wr32(hw, I40E_QINT_RQCTL(qp), val);
  4012. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4013. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4014. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4015. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4016. I40E_QINT_TQCTL_INTEVENT_MASK);
  4017. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4018. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4019. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4020. }
  4021. }
  4022. /**
  4023. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  4024. * @vsi: the VSI being configured
  4025. * @v_idx: Index of vector to be freed
  4026. *
  4027. * This function frees the memory allocated to the q_vector. In addition if
  4028. * NAPI is enabled it will delete any references to the NAPI struct prior
  4029. * to freeing the q_vector.
  4030. **/
  4031. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  4032. {
  4033. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  4034. struct i40e_ring *ring;
  4035. if (!q_vector)
  4036. return;
  4037. /* disassociate q_vector from rings */
  4038. i40e_for_each_ring(ring, q_vector->tx)
  4039. ring->q_vector = NULL;
  4040. i40e_for_each_ring(ring, q_vector->rx)
  4041. ring->q_vector = NULL;
  4042. /* only VSI w/ an associated netdev is set up w/ NAPI */
  4043. if (vsi->netdev)
  4044. netif_napi_del(&q_vector->napi);
  4045. vsi->q_vectors[v_idx] = NULL;
  4046. kfree_rcu(q_vector, rcu);
  4047. }
  4048. /**
  4049. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  4050. * @vsi: the VSI being un-configured
  4051. *
  4052. * This frees the memory allocated to the q_vectors and
  4053. * deletes references to the NAPI struct.
  4054. **/
  4055. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  4056. {
  4057. int v_idx;
  4058. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  4059. i40e_free_q_vector(vsi, v_idx);
  4060. }
  4061. /**
  4062. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  4063. * @pf: board private structure
  4064. **/
  4065. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  4066. {
  4067. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  4068. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  4069. pci_disable_msix(pf->pdev);
  4070. kfree(pf->msix_entries);
  4071. pf->msix_entries = NULL;
  4072. kfree(pf->irq_pile);
  4073. pf->irq_pile = NULL;
  4074. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  4075. pci_disable_msi(pf->pdev);
  4076. }
  4077. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  4078. }
  4079. /**
  4080. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  4081. * @pf: board private structure
  4082. *
  4083. * We go through and clear interrupt specific resources and reset the structure
  4084. * to pre-load conditions
  4085. **/
  4086. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  4087. {
  4088. int i;
  4089. i40e_free_misc_vector(pf);
  4090. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  4091. I40E_IWARP_IRQ_PILE_ID);
  4092. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  4093. for (i = 0; i < pf->num_alloc_vsi; i++)
  4094. if (pf->vsi[i])
  4095. i40e_vsi_free_q_vectors(pf->vsi[i]);
  4096. i40e_reset_interrupt_capability(pf);
  4097. }
  4098. /**
  4099. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  4100. * @vsi: the VSI being configured
  4101. **/
  4102. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  4103. {
  4104. int q_idx;
  4105. if (!vsi->netdev)
  4106. return;
  4107. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4108. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4109. if (q_vector->rx.ring || q_vector->tx.ring)
  4110. napi_enable(&q_vector->napi);
  4111. }
  4112. }
  4113. /**
  4114. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  4115. * @vsi: the VSI being configured
  4116. **/
  4117. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  4118. {
  4119. int q_idx;
  4120. if (!vsi->netdev)
  4121. return;
  4122. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4123. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4124. if (q_vector->rx.ring || q_vector->tx.ring)
  4125. napi_disable(&q_vector->napi);
  4126. }
  4127. }
  4128. /**
  4129. * i40e_vsi_close - Shut down a VSI
  4130. * @vsi: the vsi to be quelled
  4131. **/
  4132. static void i40e_vsi_close(struct i40e_vsi *vsi)
  4133. {
  4134. struct i40e_pf *pf = vsi->back;
  4135. if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
  4136. i40e_down(vsi);
  4137. i40e_vsi_free_irq(vsi);
  4138. i40e_vsi_free_tx_resources(vsi);
  4139. i40e_vsi_free_rx_resources(vsi);
  4140. vsi->current_netdev_flags = 0;
  4141. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  4142. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  4143. set_bit(__I40E_CLIENT_RESET, pf->state);
  4144. }
  4145. /**
  4146. * i40e_quiesce_vsi - Pause a given VSI
  4147. * @vsi: the VSI being paused
  4148. **/
  4149. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  4150. {
  4151. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  4152. return;
  4153. set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
  4154. if (vsi->netdev && netif_running(vsi->netdev))
  4155. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4156. else
  4157. i40e_vsi_close(vsi);
  4158. }
  4159. /**
  4160. * i40e_unquiesce_vsi - Resume a given VSI
  4161. * @vsi: the VSI being resumed
  4162. **/
  4163. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  4164. {
  4165. if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
  4166. return;
  4167. if (vsi->netdev && netif_running(vsi->netdev))
  4168. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4169. else
  4170. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  4171. }
  4172. /**
  4173. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  4174. * @pf: the PF
  4175. **/
  4176. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  4177. {
  4178. int v;
  4179. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4180. if (pf->vsi[v])
  4181. i40e_quiesce_vsi(pf->vsi[v]);
  4182. }
  4183. }
  4184. /**
  4185. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  4186. * @pf: the PF
  4187. **/
  4188. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  4189. {
  4190. int v;
  4191. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4192. if (pf->vsi[v])
  4193. i40e_unquiesce_vsi(pf->vsi[v]);
  4194. }
  4195. }
  4196. /**
  4197. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  4198. * @vsi: the VSI being configured
  4199. *
  4200. * Wait until all queues on a given VSI have been disabled.
  4201. **/
  4202. int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  4203. {
  4204. struct i40e_pf *pf = vsi->back;
  4205. int i, pf_q, ret;
  4206. pf_q = vsi->base_queue;
  4207. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4208. /* Check and wait for the Tx queue */
  4209. ret = i40e_pf_txq_wait(pf, pf_q, false);
  4210. if (ret) {
  4211. dev_info(&pf->pdev->dev,
  4212. "VSI seid %d Tx ring %d disable timeout\n",
  4213. vsi->seid, pf_q);
  4214. return ret;
  4215. }
  4216. if (!i40e_enabled_xdp_vsi(vsi))
  4217. goto wait_rx;
  4218. /* Check and wait for the XDP Tx queue */
  4219. ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
  4220. false);
  4221. if (ret) {
  4222. dev_info(&pf->pdev->dev,
  4223. "VSI seid %d XDP Tx ring %d disable timeout\n",
  4224. vsi->seid, pf_q);
  4225. return ret;
  4226. }
  4227. wait_rx:
  4228. /* Check and wait for the Rx queue */
  4229. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  4230. if (ret) {
  4231. dev_info(&pf->pdev->dev,
  4232. "VSI seid %d Rx ring %d disable timeout\n",
  4233. vsi->seid, pf_q);
  4234. return ret;
  4235. }
  4236. }
  4237. return 0;
  4238. }
  4239. #ifdef CONFIG_I40E_DCB
  4240. /**
  4241. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  4242. * @pf: the PF
  4243. *
  4244. * This function waits for the queues to be in disabled state for all the
  4245. * VSIs that are managed by this PF.
  4246. **/
  4247. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  4248. {
  4249. int v, ret = 0;
  4250. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4251. if (pf->vsi[v]) {
  4252. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  4253. if (ret)
  4254. break;
  4255. }
  4256. }
  4257. return ret;
  4258. }
  4259. #endif
  4260. /**
  4261. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4262. * @pf: pointer to PF
  4263. *
  4264. * Get TC map for ISCSI PF type that will include iSCSI TC
  4265. * and LAN TC.
  4266. **/
  4267. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4268. {
  4269. struct i40e_dcb_app_priority_table app;
  4270. struct i40e_hw *hw = &pf->hw;
  4271. u8 enabled_tc = 1; /* TC0 is always enabled */
  4272. u8 tc, i;
  4273. /* Get the iSCSI APP TLV */
  4274. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4275. for (i = 0; i < dcbcfg->numapps; i++) {
  4276. app = dcbcfg->app[i];
  4277. if (app.selector == I40E_APP_SEL_TCPIP &&
  4278. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4279. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4280. enabled_tc |= BIT(tc);
  4281. break;
  4282. }
  4283. }
  4284. return enabled_tc;
  4285. }
  4286. /**
  4287. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4288. * @dcbcfg: the corresponding DCBx configuration structure
  4289. *
  4290. * Return the number of TCs from given DCBx configuration
  4291. **/
  4292. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4293. {
  4294. int i, tc_unused = 0;
  4295. u8 num_tc = 0;
  4296. u8 ret = 0;
  4297. /* Scan the ETS Config Priority Table to find
  4298. * traffic class enabled for a given priority
  4299. * and create a bitmask of enabled TCs
  4300. */
  4301. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4302. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4303. /* Now scan the bitmask to check for
  4304. * contiguous TCs starting with TC0
  4305. */
  4306. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4307. if (num_tc & BIT(i)) {
  4308. if (!tc_unused) {
  4309. ret++;
  4310. } else {
  4311. pr_err("Non-contiguous TC - Disabling DCB\n");
  4312. return 1;
  4313. }
  4314. } else {
  4315. tc_unused = 1;
  4316. }
  4317. }
  4318. /* There is always at least TC0 */
  4319. if (!ret)
  4320. ret = 1;
  4321. return ret;
  4322. }
  4323. /**
  4324. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4325. * @dcbcfg: the corresponding DCBx configuration structure
  4326. *
  4327. * Query the current DCB configuration and return the number of
  4328. * traffic classes enabled from the given DCBX config
  4329. **/
  4330. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4331. {
  4332. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4333. u8 enabled_tc = 1;
  4334. u8 i;
  4335. for (i = 0; i < num_tc; i++)
  4336. enabled_tc |= BIT(i);
  4337. return enabled_tc;
  4338. }
  4339. /**
  4340. * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
  4341. * @pf: PF being queried
  4342. *
  4343. * Query the current MQPRIO configuration and return the number of
  4344. * traffic classes enabled.
  4345. **/
  4346. static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
  4347. {
  4348. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  4349. u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
  4350. u8 enabled_tc = 1, i;
  4351. for (i = 1; i < num_tc; i++)
  4352. enabled_tc |= BIT(i);
  4353. return enabled_tc;
  4354. }
  4355. /**
  4356. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4357. * @pf: PF being queried
  4358. *
  4359. * Return number of traffic classes enabled for the given PF
  4360. **/
  4361. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4362. {
  4363. struct i40e_hw *hw = &pf->hw;
  4364. u8 i, enabled_tc = 1;
  4365. u8 num_tc = 0;
  4366. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4367. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4368. return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
  4369. /* If neither MQPRIO nor DCB is enabled, then always use single TC */
  4370. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4371. return 1;
  4372. /* SFP mode will be enabled for all TCs on port */
  4373. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4374. return i40e_dcb_get_num_tc(dcbcfg);
  4375. /* MFP mode return count of enabled TCs for this PF */
  4376. if (pf->hw.func_caps.iscsi)
  4377. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4378. else
  4379. return 1; /* Only TC0 */
  4380. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4381. if (enabled_tc & BIT(i))
  4382. num_tc++;
  4383. }
  4384. return num_tc;
  4385. }
  4386. /**
  4387. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4388. * @pf: PF being queried
  4389. *
  4390. * Return a bitmap for enabled traffic classes for this PF.
  4391. **/
  4392. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4393. {
  4394. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4395. return i40e_mqprio_get_enabled_tc(pf);
  4396. /* If neither MQPRIO nor DCB is enabled for this PF then just return
  4397. * default TC
  4398. */
  4399. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4400. return I40E_DEFAULT_TRAFFIC_CLASS;
  4401. /* SFP mode we want PF to be enabled for all TCs */
  4402. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4403. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4404. /* MFP enabled and iSCSI PF type */
  4405. if (pf->hw.func_caps.iscsi)
  4406. return i40e_get_iscsi_tc_map(pf);
  4407. else
  4408. return I40E_DEFAULT_TRAFFIC_CLASS;
  4409. }
  4410. /**
  4411. * i40e_vsi_get_bw_info - Query VSI BW Information
  4412. * @vsi: the VSI being queried
  4413. *
  4414. * Returns 0 on success, negative value on failure
  4415. **/
  4416. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4417. {
  4418. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4419. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4420. struct i40e_pf *pf = vsi->back;
  4421. struct i40e_hw *hw = &pf->hw;
  4422. i40e_status ret;
  4423. u32 tc_bw_max;
  4424. int i;
  4425. /* Get the VSI level BW configuration */
  4426. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4427. if (ret) {
  4428. dev_info(&pf->pdev->dev,
  4429. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4430. i40e_stat_str(&pf->hw, ret),
  4431. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4432. return -EINVAL;
  4433. }
  4434. /* Get the VSI level BW configuration per TC */
  4435. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4436. NULL);
  4437. if (ret) {
  4438. dev_info(&pf->pdev->dev,
  4439. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4440. i40e_stat_str(&pf->hw, ret),
  4441. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4442. return -EINVAL;
  4443. }
  4444. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4445. dev_info(&pf->pdev->dev,
  4446. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4447. bw_config.tc_valid_bits,
  4448. bw_ets_config.tc_valid_bits);
  4449. /* Still continuing */
  4450. }
  4451. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4452. vsi->bw_max_quanta = bw_config.max_bw;
  4453. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4454. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4455. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4456. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4457. vsi->bw_ets_limit_credits[i] =
  4458. le16_to_cpu(bw_ets_config.credits[i]);
  4459. /* 3 bits out of 4 for each TC */
  4460. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4461. }
  4462. return 0;
  4463. }
  4464. /**
  4465. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4466. * @vsi: the VSI being configured
  4467. * @enabled_tc: TC bitmap
  4468. * @bw_share: BW shared credits per TC
  4469. *
  4470. * Returns 0 on success, negative value on failure
  4471. **/
  4472. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4473. u8 *bw_share)
  4474. {
  4475. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4476. i40e_status ret;
  4477. int i;
  4478. if (vsi->back->flags & I40E_FLAG_TC_MQPRIO)
  4479. return 0;
  4480. if (!vsi->mqprio_qopt.qopt.hw) {
  4481. ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
  4482. if (ret)
  4483. dev_info(&vsi->back->pdev->dev,
  4484. "Failed to reset tx rate for vsi->seid %u\n",
  4485. vsi->seid);
  4486. return ret;
  4487. }
  4488. bw_data.tc_valid_bits = enabled_tc;
  4489. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4490. bw_data.tc_bw_credits[i] = bw_share[i];
  4491. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4492. NULL);
  4493. if (ret) {
  4494. dev_info(&vsi->back->pdev->dev,
  4495. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4496. vsi->back->hw.aq.asq_last_status);
  4497. return -EINVAL;
  4498. }
  4499. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4500. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4501. return 0;
  4502. }
  4503. /**
  4504. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4505. * @vsi: the VSI being configured
  4506. * @enabled_tc: TC map to be enabled
  4507. *
  4508. **/
  4509. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4510. {
  4511. struct net_device *netdev = vsi->netdev;
  4512. struct i40e_pf *pf = vsi->back;
  4513. struct i40e_hw *hw = &pf->hw;
  4514. u8 netdev_tc = 0;
  4515. int i;
  4516. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4517. if (!netdev)
  4518. return;
  4519. if (!enabled_tc) {
  4520. netdev_reset_tc(netdev);
  4521. return;
  4522. }
  4523. /* Set up actual enabled TCs on the VSI */
  4524. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4525. return;
  4526. /* set per TC queues for the VSI */
  4527. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4528. /* Only set TC queues for enabled tcs
  4529. *
  4530. * e.g. For a VSI that has TC0 and TC3 enabled the
  4531. * enabled_tc bitmap would be 0x00001001; the driver
  4532. * will set the numtc for netdev as 2 that will be
  4533. * referenced by the netdev layer as TC 0 and 1.
  4534. */
  4535. if (vsi->tc_config.enabled_tc & BIT(i))
  4536. netdev_set_tc_queue(netdev,
  4537. vsi->tc_config.tc_info[i].netdev_tc,
  4538. vsi->tc_config.tc_info[i].qcount,
  4539. vsi->tc_config.tc_info[i].qoffset);
  4540. }
  4541. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4542. return;
  4543. /* Assign UP2TC map for the VSI */
  4544. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4545. /* Get the actual TC# for the UP */
  4546. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4547. /* Get the mapped netdev TC# for the UP */
  4548. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4549. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4550. }
  4551. }
  4552. /**
  4553. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4554. * @vsi: the VSI being configured
  4555. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4556. **/
  4557. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4558. struct i40e_vsi_context *ctxt)
  4559. {
  4560. /* copy just the sections touched not the entire info
  4561. * since not all sections are valid as returned by
  4562. * update vsi params
  4563. */
  4564. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4565. memcpy(&vsi->info.queue_mapping,
  4566. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4567. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4568. sizeof(vsi->info.tc_mapping));
  4569. }
  4570. /**
  4571. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4572. * @vsi: VSI to be configured
  4573. * @enabled_tc: TC bitmap
  4574. *
  4575. * This configures a particular VSI for TCs that are mapped to the
  4576. * given TC bitmap. It uses default bandwidth share for TCs across
  4577. * VSIs to configure TC for a particular VSI.
  4578. *
  4579. * NOTE:
  4580. * It is expected that the VSI queues have been quisced before calling
  4581. * this function.
  4582. **/
  4583. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4584. {
  4585. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4586. struct i40e_pf *pf = vsi->back;
  4587. struct i40e_hw *hw = &pf->hw;
  4588. struct i40e_vsi_context ctxt;
  4589. int ret = 0;
  4590. int i;
  4591. /* Check if enabled_tc is same as existing or new TCs */
  4592. if (vsi->tc_config.enabled_tc == enabled_tc &&
  4593. vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
  4594. return ret;
  4595. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4596. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4597. if (enabled_tc & BIT(i))
  4598. bw_share[i] = 1;
  4599. }
  4600. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4601. if (ret) {
  4602. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4603. dev_info(&pf->pdev->dev,
  4604. "Failed configuring TC map %d for VSI %d\n",
  4605. enabled_tc, vsi->seid);
  4606. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
  4607. &bw_config, NULL);
  4608. if (ret) {
  4609. dev_info(&pf->pdev->dev,
  4610. "Failed querying vsi bw info, err %s aq_err %s\n",
  4611. i40e_stat_str(hw, ret),
  4612. i40e_aq_str(hw, hw->aq.asq_last_status));
  4613. goto out;
  4614. }
  4615. if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
  4616. u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
  4617. if (!valid_tc)
  4618. valid_tc = bw_config.tc_valid_bits;
  4619. /* Always enable TC0, no matter what */
  4620. valid_tc |= 1;
  4621. dev_info(&pf->pdev->dev,
  4622. "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
  4623. enabled_tc, bw_config.tc_valid_bits, valid_tc);
  4624. enabled_tc = valid_tc;
  4625. }
  4626. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4627. if (ret) {
  4628. dev_err(&pf->pdev->dev,
  4629. "Unable to configure TC map %d for VSI %d\n",
  4630. enabled_tc, vsi->seid);
  4631. goto out;
  4632. }
  4633. }
  4634. /* Update Queue Pairs Mapping for currently enabled UPs */
  4635. ctxt.seid = vsi->seid;
  4636. ctxt.pf_num = vsi->back->hw.pf_id;
  4637. ctxt.vf_num = 0;
  4638. ctxt.uplink_seid = vsi->uplink_seid;
  4639. ctxt.info = vsi->info;
  4640. if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
  4641. ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
  4642. if (ret)
  4643. goto out;
  4644. } else {
  4645. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4646. }
  4647. /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
  4648. * queues changed.
  4649. */
  4650. if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
  4651. vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
  4652. vsi->num_queue_pairs);
  4653. ret = i40e_vsi_config_rss(vsi);
  4654. if (ret) {
  4655. dev_info(&vsi->back->pdev->dev,
  4656. "Failed to reconfig rss for num_queues\n");
  4657. return ret;
  4658. }
  4659. vsi->reconfig_rss = false;
  4660. }
  4661. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4662. ctxt.info.valid_sections |=
  4663. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4664. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4665. }
  4666. /* Update the VSI after updating the VSI queue-mapping
  4667. * information
  4668. */
  4669. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  4670. if (ret) {
  4671. dev_info(&pf->pdev->dev,
  4672. "Update vsi tc config failed, err %s aq_err %s\n",
  4673. i40e_stat_str(hw, ret),
  4674. i40e_aq_str(hw, hw->aq.asq_last_status));
  4675. goto out;
  4676. }
  4677. /* update the local VSI info with updated queue map */
  4678. i40e_vsi_update_queue_map(vsi, &ctxt);
  4679. vsi->info.valid_sections = 0;
  4680. /* Update current VSI BW information */
  4681. ret = i40e_vsi_get_bw_info(vsi);
  4682. if (ret) {
  4683. dev_info(&pf->pdev->dev,
  4684. "Failed updating vsi bw info, err %s aq_err %s\n",
  4685. i40e_stat_str(hw, ret),
  4686. i40e_aq_str(hw, hw->aq.asq_last_status));
  4687. goto out;
  4688. }
  4689. /* Update the netdev TC setup */
  4690. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4691. out:
  4692. return ret;
  4693. }
  4694. /**
  4695. * i40e_get_link_speed - Returns link speed for the interface
  4696. * @vsi: VSI to be configured
  4697. *
  4698. **/
  4699. static int i40e_get_link_speed(struct i40e_vsi *vsi)
  4700. {
  4701. struct i40e_pf *pf = vsi->back;
  4702. switch (pf->hw.phy.link_info.link_speed) {
  4703. case I40E_LINK_SPEED_40GB:
  4704. return 40000;
  4705. case I40E_LINK_SPEED_25GB:
  4706. return 25000;
  4707. case I40E_LINK_SPEED_20GB:
  4708. return 20000;
  4709. case I40E_LINK_SPEED_10GB:
  4710. return 10000;
  4711. case I40E_LINK_SPEED_1GB:
  4712. return 1000;
  4713. default:
  4714. return -EINVAL;
  4715. }
  4716. }
  4717. /**
  4718. * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
  4719. * @vsi: VSI to be configured
  4720. * @seid: seid of the channel/VSI
  4721. * @max_tx_rate: max TX rate to be configured as BW limit
  4722. *
  4723. * Helper function to set BW limit for a given VSI
  4724. **/
  4725. int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
  4726. {
  4727. struct i40e_pf *pf = vsi->back;
  4728. u64 credits = 0;
  4729. int speed = 0;
  4730. int ret = 0;
  4731. speed = i40e_get_link_speed(vsi);
  4732. if (max_tx_rate > speed) {
  4733. dev_err(&pf->pdev->dev,
  4734. "Invalid max tx rate %llu specified for VSI seid %d.",
  4735. max_tx_rate, seid);
  4736. return -EINVAL;
  4737. }
  4738. if (max_tx_rate && max_tx_rate < 50) {
  4739. dev_warn(&pf->pdev->dev,
  4740. "Setting max tx rate to minimum usable value of 50Mbps.\n");
  4741. max_tx_rate = 50;
  4742. }
  4743. /* Tx rate credits are in values of 50Mbps, 0 is disabled */
  4744. credits = max_tx_rate;
  4745. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  4746. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
  4747. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  4748. if (ret)
  4749. dev_err(&pf->pdev->dev,
  4750. "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
  4751. max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
  4752. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4753. return ret;
  4754. }
  4755. /**
  4756. * i40e_remove_queue_channels - Remove queue channels for the TCs
  4757. * @vsi: VSI to be configured
  4758. *
  4759. * Remove queue channels for the TCs
  4760. **/
  4761. static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
  4762. {
  4763. enum i40e_admin_queue_err last_aq_status;
  4764. struct i40e_cloud_filter *cfilter;
  4765. struct i40e_channel *ch, *ch_tmp;
  4766. struct i40e_pf *pf = vsi->back;
  4767. struct hlist_node *node;
  4768. int ret, i;
  4769. /* Reset rss size that was stored when reconfiguring rss for
  4770. * channel VSIs with non-power-of-2 queue count.
  4771. */
  4772. vsi->current_rss_size = 0;
  4773. /* perform cleanup for channels if they exist */
  4774. if (list_empty(&vsi->ch_list))
  4775. return;
  4776. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4777. struct i40e_vsi *p_vsi;
  4778. list_del(&ch->list);
  4779. p_vsi = ch->parent_vsi;
  4780. if (!p_vsi || !ch->initialized) {
  4781. kfree(ch);
  4782. continue;
  4783. }
  4784. /* Reset queue contexts */
  4785. for (i = 0; i < ch->num_queue_pairs; i++) {
  4786. struct i40e_ring *tx_ring, *rx_ring;
  4787. u16 pf_q;
  4788. pf_q = ch->base_queue + i;
  4789. tx_ring = vsi->tx_rings[pf_q];
  4790. tx_ring->ch = NULL;
  4791. rx_ring = vsi->rx_rings[pf_q];
  4792. rx_ring->ch = NULL;
  4793. }
  4794. /* Reset BW configured for this VSI via mqprio */
  4795. ret = i40e_set_bw_limit(vsi, ch->seid, 0);
  4796. if (ret)
  4797. dev_info(&vsi->back->pdev->dev,
  4798. "Failed to reset tx rate for ch->seid %u\n",
  4799. ch->seid);
  4800. /* delete cloud filters associated with this channel */
  4801. hlist_for_each_entry_safe(cfilter, node,
  4802. &pf->cloud_filter_list, cloud_node) {
  4803. if (cfilter->seid != ch->seid)
  4804. continue;
  4805. hash_del(&cfilter->cloud_node);
  4806. if (cfilter->dst_port)
  4807. ret = i40e_add_del_cloud_filter_big_buf(vsi,
  4808. cfilter,
  4809. false);
  4810. else
  4811. ret = i40e_add_del_cloud_filter(vsi, cfilter,
  4812. false);
  4813. last_aq_status = pf->hw.aq.asq_last_status;
  4814. if (ret)
  4815. dev_info(&pf->pdev->dev,
  4816. "Failed to delete cloud filter, err %s aq_err %s\n",
  4817. i40e_stat_str(&pf->hw, ret),
  4818. i40e_aq_str(&pf->hw, last_aq_status));
  4819. kfree(cfilter);
  4820. }
  4821. /* delete VSI from FW */
  4822. ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
  4823. NULL);
  4824. if (ret)
  4825. dev_err(&vsi->back->pdev->dev,
  4826. "unable to remove channel (%d) for parent VSI(%d)\n",
  4827. ch->seid, p_vsi->seid);
  4828. kfree(ch);
  4829. }
  4830. INIT_LIST_HEAD(&vsi->ch_list);
  4831. }
  4832. /**
  4833. * i40e_is_any_channel - channel exist or not
  4834. * @vsi: ptr to VSI to which channels are associated with
  4835. *
  4836. * Returns true or false if channel(s) exist for associated VSI or not
  4837. **/
  4838. static bool i40e_is_any_channel(struct i40e_vsi *vsi)
  4839. {
  4840. struct i40e_channel *ch, *ch_tmp;
  4841. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4842. if (ch->initialized)
  4843. return true;
  4844. }
  4845. return false;
  4846. }
  4847. /**
  4848. * i40e_get_max_queues_for_channel
  4849. * @vsi: ptr to VSI to which channels are associated with
  4850. *
  4851. * Helper function which returns max value among the queue counts set on the
  4852. * channels/TCs created.
  4853. **/
  4854. static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
  4855. {
  4856. struct i40e_channel *ch, *ch_tmp;
  4857. int max = 0;
  4858. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4859. if (!ch->initialized)
  4860. continue;
  4861. if (ch->num_queue_pairs > max)
  4862. max = ch->num_queue_pairs;
  4863. }
  4864. return max;
  4865. }
  4866. /**
  4867. * i40e_validate_num_queues - validate num_queues w.r.t channel
  4868. * @pf: ptr to PF device
  4869. * @num_queues: number of queues
  4870. * @vsi: the parent VSI
  4871. * @reconfig_rss: indicates should the RSS be reconfigured or not
  4872. *
  4873. * This function validates number of queues in the context of new channel
  4874. * which is being established and determines if RSS should be reconfigured
  4875. * or not for parent VSI.
  4876. **/
  4877. static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
  4878. struct i40e_vsi *vsi, bool *reconfig_rss)
  4879. {
  4880. int max_ch_queues;
  4881. if (!reconfig_rss)
  4882. return -EINVAL;
  4883. *reconfig_rss = false;
  4884. if (vsi->current_rss_size) {
  4885. if (num_queues > vsi->current_rss_size) {
  4886. dev_dbg(&pf->pdev->dev,
  4887. "Error: num_queues (%d) > vsi's current_size(%d)\n",
  4888. num_queues, vsi->current_rss_size);
  4889. return -EINVAL;
  4890. } else if ((num_queues < vsi->current_rss_size) &&
  4891. (!is_power_of_2(num_queues))) {
  4892. dev_dbg(&pf->pdev->dev,
  4893. "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
  4894. num_queues, vsi->current_rss_size);
  4895. return -EINVAL;
  4896. }
  4897. }
  4898. if (!is_power_of_2(num_queues)) {
  4899. /* Find the max num_queues configured for channel if channel
  4900. * exist.
  4901. * if channel exist, then enforce 'num_queues' to be more than
  4902. * max ever queues configured for channel.
  4903. */
  4904. max_ch_queues = i40e_get_max_queues_for_channel(vsi);
  4905. if (num_queues < max_ch_queues) {
  4906. dev_dbg(&pf->pdev->dev,
  4907. "Error: num_queues (%d) < max queues configured for channel(%d)\n",
  4908. num_queues, max_ch_queues);
  4909. return -EINVAL;
  4910. }
  4911. *reconfig_rss = true;
  4912. }
  4913. return 0;
  4914. }
  4915. /**
  4916. * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
  4917. * @vsi: the VSI being setup
  4918. * @rss_size: size of RSS, accordingly LUT gets reprogrammed
  4919. *
  4920. * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
  4921. **/
  4922. static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
  4923. {
  4924. struct i40e_pf *pf = vsi->back;
  4925. u8 seed[I40E_HKEY_ARRAY_SIZE];
  4926. struct i40e_hw *hw = &pf->hw;
  4927. int local_rss_size;
  4928. u8 *lut;
  4929. int ret;
  4930. if (!vsi->rss_size)
  4931. return -EINVAL;
  4932. if (rss_size > vsi->rss_size)
  4933. return -EINVAL;
  4934. local_rss_size = min_t(int, vsi->rss_size, rss_size);
  4935. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  4936. if (!lut)
  4937. return -ENOMEM;
  4938. /* Ignoring user configured lut if there is one */
  4939. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
  4940. /* Use user configured hash key if there is one, otherwise
  4941. * use default.
  4942. */
  4943. if (vsi->rss_hkey_user)
  4944. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  4945. else
  4946. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  4947. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  4948. if (ret) {
  4949. dev_info(&pf->pdev->dev,
  4950. "Cannot set RSS lut, err %s aq_err %s\n",
  4951. i40e_stat_str(hw, ret),
  4952. i40e_aq_str(hw, hw->aq.asq_last_status));
  4953. kfree(lut);
  4954. return ret;
  4955. }
  4956. kfree(lut);
  4957. /* Do the update w.r.t. storing rss_size */
  4958. if (!vsi->orig_rss_size)
  4959. vsi->orig_rss_size = vsi->rss_size;
  4960. vsi->current_rss_size = local_rss_size;
  4961. return ret;
  4962. }
  4963. /**
  4964. * i40e_channel_setup_queue_map - Setup a channel queue map
  4965. * @pf: ptr to PF device
  4966. * @vsi: the VSI being setup
  4967. * @ctxt: VSI context structure
  4968. * @ch: ptr to channel structure
  4969. *
  4970. * Setup queue map for a specific channel
  4971. **/
  4972. static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
  4973. struct i40e_vsi_context *ctxt,
  4974. struct i40e_channel *ch)
  4975. {
  4976. u16 qcount, qmap, sections = 0;
  4977. u8 offset = 0;
  4978. int pow;
  4979. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  4980. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  4981. qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
  4982. ch->num_queue_pairs = qcount;
  4983. /* find the next higher power-of-2 of num queue pairs */
  4984. pow = ilog2(qcount);
  4985. if (!is_power_of_2(qcount))
  4986. pow++;
  4987. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  4988. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  4989. /* Setup queue TC[0].qmap for given VSI context */
  4990. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  4991. ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
  4992. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  4993. ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
  4994. ctxt->info.valid_sections |= cpu_to_le16(sections);
  4995. }
  4996. /**
  4997. * i40e_add_channel - add a channel by adding VSI
  4998. * @pf: ptr to PF device
  4999. * @uplink_seid: underlying HW switching element (VEB) ID
  5000. * @ch: ptr to channel structure
  5001. *
  5002. * Add a channel (VSI) using add_vsi and queue_map
  5003. **/
  5004. static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
  5005. struct i40e_channel *ch)
  5006. {
  5007. struct i40e_hw *hw = &pf->hw;
  5008. struct i40e_vsi_context ctxt;
  5009. u8 enabled_tc = 0x1; /* TC0 enabled */
  5010. int ret;
  5011. if (ch->type != I40E_VSI_VMDQ2) {
  5012. dev_info(&pf->pdev->dev,
  5013. "add new vsi failed, ch->type %d\n", ch->type);
  5014. return -EINVAL;
  5015. }
  5016. memset(&ctxt, 0, sizeof(ctxt));
  5017. ctxt.pf_num = hw->pf_id;
  5018. ctxt.vf_num = 0;
  5019. ctxt.uplink_seid = uplink_seid;
  5020. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  5021. if (ch->type == I40E_VSI_VMDQ2)
  5022. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  5023. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
  5024. ctxt.info.valid_sections |=
  5025. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5026. ctxt.info.switch_id =
  5027. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5028. }
  5029. /* Set queue map for a given VSI context */
  5030. i40e_channel_setup_queue_map(pf, &ctxt, ch);
  5031. /* Now time to create VSI */
  5032. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  5033. if (ret) {
  5034. dev_info(&pf->pdev->dev,
  5035. "add new vsi failed, err %s aq_err %s\n",
  5036. i40e_stat_str(&pf->hw, ret),
  5037. i40e_aq_str(&pf->hw,
  5038. pf->hw.aq.asq_last_status));
  5039. return -ENOENT;
  5040. }
  5041. /* Success, update channel */
  5042. ch->enabled_tc = enabled_tc;
  5043. ch->seid = ctxt.seid;
  5044. ch->vsi_number = ctxt.vsi_number;
  5045. ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
  5046. /* copy just the sections touched not the entire info
  5047. * since not all sections are valid as returned by
  5048. * update vsi params
  5049. */
  5050. ch->info.mapping_flags = ctxt.info.mapping_flags;
  5051. memcpy(&ch->info.queue_mapping,
  5052. &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
  5053. memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
  5054. sizeof(ctxt.info.tc_mapping));
  5055. return 0;
  5056. }
  5057. static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
  5058. u8 *bw_share)
  5059. {
  5060. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  5061. i40e_status ret;
  5062. int i;
  5063. bw_data.tc_valid_bits = ch->enabled_tc;
  5064. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5065. bw_data.tc_bw_credits[i] = bw_share[i];
  5066. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
  5067. &bw_data, NULL);
  5068. if (ret) {
  5069. dev_info(&vsi->back->pdev->dev,
  5070. "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
  5071. vsi->back->hw.aq.asq_last_status, ch->seid);
  5072. return -EINVAL;
  5073. }
  5074. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5075. ch->info.qs_handle[i] = bw_data.qs_handles[i];
  5076. return 0;
  5077. }
  5078. /**
  5079. * i40e_channel_config_tx_ring - config TX ring associated with new channel
  5080. * @pf: ptr to PF device
  5081. * @vsi: the VSI being setup
  5082. * @ch: ptr to channel structure
  5083. *
  5084. * Configure TX rings associated with channel (VSI) since queues are being
  5085. * from parent VSI.
  5086. **/
  5087. static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
  5088. struct i40e_vsi *vsi,
  5089. struct i40e_channel *ch)
  5090. {
  5091. i40e_status ret;
  5092. int i;
  5093. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  5094. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  5095. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5096. if (ch->enabled_tc & BIT(i))
  5097. bw_share[i] = 1;
  5098. }
  5099. /* configure BW for new VSI */
  5100. ret = i40e_channel_config_bw(vsi, ch, bw_share);
  5101. if (ret) {
  5102. dev_info(&vsi->back->pdev->dev,
  5103. "Failed configuring TC map %d for channel (seid %u)\n",
  5104. ch->enabled_tc, ch->seid);
  5105. return ret;
  5106. }
  5107. for (i = 0; i < ch->num_queue_pairs; i++) {
  5108. struct i40e_ring *tx_ring, *rx_ring;
  5109. u16 pf_q;
  5110. pf_q = ch->base_queue + i;
  5111. /* Get to TX ring ptr of main VSI, for re-setup TX queue
  5112. * context
  5113. */
  5114. tx_ring = vsi->tx_rings[pf_q];
  5115. tx_ring->ch = ch;
  5116. /* Get the RX ring ptr */
  5117. rx_ring = vsi->rx_rings[pf_q];
  5118. rx_ring->ch = ch;
  5119. }
  5120. return 0;
  5121. }
  5122. /**
  5123. * i40e_setup_hw_channel - setup new channel
  5124. * @pf: ptr to PF device
  5125. * @vsi: the VSI being setup
  5126. * @ch: ptr to channel structure
  5127. * @uplink_seid: underlying HW switching element (VEB) ID
  5128. * @type: type of channel to be created (VMDq2/VF)
  5129. *
  5130. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5131. * and configures TX rings accordingly
  5132. **/
  5133. static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
  5134. struct i40e_vsi *vsi,
  5135. struct i40e_channel *ch,
  5136. u16 uplink_seid, u8 type)
  5137. {
  5138. int ret;
  5139. ch->initialized = false;
  5140. ch->base_queue = vsi->next_base_queue;
  5141. ch->type = type;
  5142. /* Proceed with creation of channel (VMDq2) VSI */
  5143. ret = i40e_add_channel(pf, uplink_seid, ch);
  5144. if (ret) {
  5145. dev_info(&pf->pdev->dev,
  5146. "failed to add_channel using uplink_seid %u\n",
  5147. uplink_seid);
  5148. return ret;
  5149. }
  5150. /* Mark the successful creation of channel */
  5151. ch->initialized = true;
  5152. /* Reconfigure TX queues using QTX_CTL register */
  5153. ret = i40e_channel_config_tx_ring(pf, vsi, ch);
  5154. if (ret) {
  5155. dev_info(&pf->pdev->dev,
  5156. "failed to configure TX rings for channel %u\n",
  5157. ch->seid);
  5158. return ret;
  5159. }
  5160. /* update 'next_base_queue' */
  5161. vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
  5162. dev_dbg(&pf->pdev->dev,
  5163. "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
  5164. ch->seid, ch->vsi_number, ch->stat_counter_idx,
  5165. ch->num_queue_pairs,
  5166. vsi->next_base_queue);
  5167. return ret;
  5168. }
  5169. /**
  5170. * i40e_setup_channel - setup new channel using uplink element
  5171. * @pf: ptr to PF device
  5172. * @type: type of channel to be created (VMDq2/VF)
  5173. * @uplink_seid: underlying HW switching element (VEB) ID
  5174. * @ch: ptr to channel structure
  5175. *
  5176. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5177. * and uplink switching element (uplink_seid)
  5178. **/
  5179. static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
  5180. struct i40e_channel *ch)
  5181. {
  5182. u8 vsi_type;
  5183. u16 seid;
  5184. int ret;
  5185. if (vsi->type == I40E_VSI_MAIN) {
  5186. vsi_type = I40E_VSI_VMDQ2;
  5187. } else {
  5188. dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
  5189. vsi->type);
  5190. return false;
  5191. }
  5192. /* underlying switching element */
  5193. seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5194. /* create channel (VSI), configure TX rings */
  5195. ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
  5196. if (ret) {
  5197. dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
  5198. return false;
  5199. }
  5200. return ch->initialized ? true : false;
  5201. }
  5202. /**
  5203. * i40e_validate_and_set_switch_mode - sets up switch mode correctly
  5204. * @vsi: ptr to VSI which has PF backing
  5205. *
  5206. * Sets up switch mode correctly if it needs to be changed and perform
  5207. * what are allowed modes.
  5208. **/
  5209. static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
  5210. {
  5211. u8 mode;
  5212. struct i40e_pf *pf = vsi->back;
  5213. struct i40e_hw *hw = &pf->hw;
  5214. int ret;
  5215. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
  5216. if (ret)
  5217. return -EINVAL;
  5218. if (hw->dev_caps.switch_mode) {
  5219. /* if switch mode is set, support mode2 (non-tunneled for
  5220. * cloud filter) for now
  5221. */
  5222. u32 switch_mode = hw->dev_caps.switch_mode &
  5223. I40E_SWITCH_MODE_MASK;
  5224. if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
  5225. if (switch_mode == I40E_CLOUD_FILTER_MODE2)
  5226. return 0;
  5227. dev_err(&pf->pdev->dev,
  5228. "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
  5229. hw->dev_caps.switch_mode);
  5230. return -EINVAL;
  5231. }
  5232. }
  5233. /* Set Bit 7 to be valid */
  5234. mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
  5235. /* Set L4type for TCP support */
  5236. mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
  5237. /* Set cloud filter mode */
  5238. mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
  5239. /* Prep mode field for set_switch_config */
  5240. ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
  5241. pf->last_sw_conf_valid_flags,
  5242. mode, NULL);
  5243. if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
  5244. dev_err(&pf->pdev->dev,
  5245. "couldn't set switch config bits, err %s aq_err %s\n",
  5246. i40e_stat_str(hw, ret),
  5247. i40e_aq_str(hw,
  5248. hw->aq.asq_last_status));
  5249. return ret;
  5250. }
  5251. /**
  5252. * i40e_create_queue_channel - function to create channel
  5253. * @vsi: VSI to be configured
  5254. * @ch: ptr to channel (it contains channel specific params)
  5255. *
  5256. * This function creates channel (VSI) using num_queues specified by user,
  5257. * reconfigs RSS if needed.
  5258. **/
  5259. int i40e_create_queue_channel(struct i40e_vsi *vsi,
  5260. struct i40e_channel *ch)
  5261. {
  5262. struct i40e_pf *pf = vsi->back;
  5263. bool reconfig_rss;
  5264. int err;
  5265. if (!ch)
  5266. return -EINVAL;
  5267. if (!ch->num_queue_pairs) {
  5268. dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
  5269. ch->num_queue_pairs);
  5270. return -EINVAL;
  5271. }
  5272. /* validate user requested num_queues for channel */
  5273. err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
  5274. &reconfig_rss);
  5275. if (err) {
  5276. dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
  5277. ch->num_queue_pairs);
  5278. return -EINVAL;
  5279. }
  5280. /* By default we are in VEPA mode, if this is the first VF/VMDq
  5281. * VSI to be added switch to VEB mode.
  5282. */
  5283. if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
  5284. (!i40e_is_any_channel(vsi))) {
  5285. if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
  5286. dev_dbg(&pf->pdev->dev,
  5287. "Failed to create channel. Override queues (%u) not power of 2\n",
  5288. vsi->tc_config.tc_info[0].qcount);
  5289. return -EINVAL;
  5290. }
  5291. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  5292. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  5293. if (vsi->type == I40E_VSI_MAIN) {
  5294. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  5295. i40e_do_reset(pf, I40E_PF_RESET_FLAG,
  5296. true);
  5297. else
  5298. i40e_do_reset_safe(pf,
  5299. I40E_PF_RESET_FLAG);
  5300. }
  5301. }
  5302. /* now onwards for main VSI, number of queues will be value
  5303. * of TC0's queue count
  5304. */
  5305. }
  5306. /* By this time, vsi->cnt_q_avail shall be set to non-zero and
  5307. * it should be more than num_queues
  5308. */
  5309. if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
  5310. dev_dbg(&pf->pdev->dev,
  5311. "Error: cnt_q_avail (%u) less than num_queues %d\n",
  5312. vsi->cnt_q_avail, ch->num_queue_pairs);
  5313. return -EINVAL;
  5314. }
  5315. /* reconfig_rss only if vsi type is MAIN_VSI */
  5316. if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
  5317. err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
  5318. if (err) {
  5319. dev_info(&pf->pdev->dev,
  5320. "Error: unable to reconfig rss for num_queues (%u)\n",
  5321. ch->num_queue_pairs);
  5322. return -EINVAL;
  5323. }
  5324. }
  5325. if (!i40e_setup_channel(pf, vsi, ch)) {
  5326. dev_info(&pf->pdev->dev, "Failed to setup channel\n");
  5327. return -EINVAL;
  5328. }
  5329. dev_info(&pf->pdev->dev,
  5330. "Setup channel (id:%u) utilizing num_queues %d\n",
  5331. ch->seid, ch->num_queue_pairs);
  5332. /* configure VSI for BW limit */
  5333. if (ch->max_tx_rate) {
  5334. u64 credits = ch->max_tx_rate;
  5335. if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
  5336. return -EINVAL;
  5337. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5338. dev_dbg(&pf->pdev->dev,
  5339. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5340. ch->max_tx_rate,
  5341. credits,
  5342. ch->seid);
  5343. }
  5344. /* in case of VF, this will be main SRIOV VSI */
  5345. ch->parent_vsi = vsi;
  5346. /* and update main_vsi's count for queue_available to use */
  5347. vsi->cnt_q_avail -= ch->num_queue_pairs;
  5348. return 0;
  5349. }
  5350. /**
  5351. * i40e_configure_queue_channels - Add queue channel for the given TCs
  5352. * @vsi: VSI to be configured
  5353. *
  5354. * Configures queue channel mapping to the given TCs
  5355. **/
  5356. static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
  5357. {
  5358. struct i40e_channel *ch;
  5359. u64 max_rate = 0;
  5360. int ret = 0, i;
  5361. /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
  5362. vsi->tc_seid_map[0] = vsi->seid;
  5363. for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5364. if (vsi->tc_config.enabled_tc & BIT(i)) {
  5365. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  5366. if (!ch) {
  5367. ret = -ENOMEM;
  5368. goto err_free;
  5369. }
  5370. INIT_LIST_HEAD(&ch->list);
  5371. ch->num_queue_pairs =
  5372. vsi->tc_config.tc_info[i].qcount;
  5373. ch->base_queue =
  5374. vsi->tc_config.tc_info[i].qoffset;
  5375. /* Bandwidth limit through tc interface is in bytes/s,
  5376. * change to Mbit/s
  5377. */
  5378. max_rate = vsi->mqprio_qopt.max_rate[i];
  5379. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5380. ch->max_tx_rate = max_rate;
  5381. list_add_tail(&ch->list, &vsi->ch_list);
  5382. ret = i40e_create_queue_channel(vsi, ch);
  5383. if (ret) {
  5384. dev_err(&vsi->back->pdev->dev,
  5385. "Failed creating queue channel with TC%d: queues %d\n",
  5386. i, ch->num_queue_pairs);
  5387. goto err_free;
  5388. }
  5389. vsi->tc_seid_map[i] = ch->seid;
  5390. }
  5391. }
  5392. return ret;
  5393. err_free:
  5394. i40e_remove_queue_channels(vsi);
  5395. return ret;
  5396. }
  5397. /**
  5398. * i40e_veb_config_tc - Configure TCs for given VEB
  5399. * @veb: given VEB
  5400. * @enabled_tc: TC bitmap
  5401. *
  5402. * Configures given TC bitmap for VEB (switching) element
  5403. **/
  5404. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  5405. {
  5406. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  5407. struct i40e_pf *pf = veb->pf;
  5408. int ret = 0;
  5409. int i;
  5410. /* No TCs or already enabled TCs just return */
  5411. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  5412. return ret;
  5413. bw_data.tc_valid_bits = enabled_tc;
  5414. /* bw_data.absolute_credits is not set (relative) */
  5415. /* Enable ETS TCs with equal BW Share for now */
  5416. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5417. if (enabled_tc & BIT(i))
  5418. bw_data.tc_bw_share_credits[i] = 1;
  5419. }
  5420. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  5421. &bw_data, NULL);
  5422. if (ret) {
  5423. dev_info(&pf->pdev->dev,
  5424. "VEB bw config failed, err %s aq_err %s\n",
  5425. i40e_stat_str(&pf->hw, ret),
  5426. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5427. goto out;
  5428. }
  5429. /* Update the BW information */
  5430. ret = i40e_veb_get_bw_info(veb);
  5431. if (ret) {
  5432. dev_info(&pf->pdev->dev,
  5433. "Failed getting veb bw config, err %s aq_err %s\n",
  5434. i40e_stat_str(&pf->hw, ret),
  5435. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5436. }
  5437. out:
  5438. return ret;
  5439. }
  5440. #ifdef CONFIG_I40E_DCB
  5441. /**
  5442. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  5443. * @pf: PF struct
  5444. *
  5445. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  5446. * the caller would've quiesce all the VSIs before calling
  5447. * this function
  5448. **/
  5449. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  5450. {
  5451. u8 tc_map = 0;
  5452. int ret;
  5453. u8 v;
  5454. /* Enable the TCs available on PF to all VEBs */
  5455. tc_map = i40e_pf_get_tc_map(pf);
  5456. for (v = 0; v < I40E_MAX_VEB; v++) {
  5457. if (!pf->veb[v])
  5458. continue;
  5459. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  5460. if (ret) {
  5461. dev_info(&pf->pdev->dev,
  5462. "Failed configuring TC for VEB seid=%d\n",
  5463. pf->veb[v]->seid);
  5464. /* Will try to configure as many components */
  5465. }
  5466. }
  5467. /* Update each VSI */
  5468. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5469. if (!pf->vsi[v])
  5470. continue;
  5471. /* - Enable all TCs for the LAN VSI
  5472. * - For all others keep them at TC0 for now
  5473. */
  5474. if (v == pf->lan_vsi)
  5475. tc_map = i40e_pf_get_tc_map(pf);
  5476. else
  5477. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  5478. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  5479. if (ret) {
  5480. dev_info(&pf->pdev->dev,
  5481. "Failed configuring TC for VSI seid=%d\n",
  5482. pf->vsi[v]->seid);
  5483. /* Will try to configure as many components */
  5484. } else {
  5485. /* Re-configure VSI vectors based on updated TC map */
  5486. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  5487. if (pf->vsi[v]->netdev)
  5488. i40e_dcbnl_set_all(pf->vsi[v]);
  5489. }
  5490. }
  5491. }
  5492. /**
  5493. * i40e_resume_port_tx - Resume port Tx
  5494. * @pf: PF struct
  5495. *
  5496. * Resume a port's Tx and issue a PF reset in case of failure to
  5497. * resume.
  5498. **/
  5499. static int i40e_resume_port_tx(struct i40e_pf *pf)
  5500. {
  5501. struct i40e_hw *hw = &pf->hw;
  5502. int ret;
  5503. ret = i40e_aq_resume_port_tx(hw, NULL);
  5504. if (ret) {
  5505. dev_info(&pf->pdev->dev,
  5506. "Resume Port Tx failed, err %s aq_err %s\n",
  5507. i40e_stat_str(&pf->hw, ret),
  5508. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5509. /* Schedule PF reset to recover */
  5510. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  5511. i40e_service_event_schedule(pf);
  5512. }
  5513. return ret;
  5514. }
  5515. /**
  5516. * i40e_init_pf_dcb - Initialize DCB configuration
  5517. * @pf: PF being configured
  5518. *
  5519. * Query the current DCB configuration and cache it
  5520. * in the hardware structure
  5521. **/
  5522. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  5523. {
  5524. struct i40e_hw *hw = &pf->hw;
  5525. int err = 0;
  5526. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable
  5527. * Also do not enable DCBx if FW LLDP agent is disabled
  5528. */
  5529. if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
  5530. (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
  5531. goto out;
  5532. /* Get the initial DCB configuration */
  5533. err = i40e_init_dcb(hw);
  5534. if (!err) {
  5535. /* Device/Function is not DCBX capable */
  5536. if ((!hw->func_caps.dcb) ||
  5537. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  5538. dev_info(&pf->pdev->dev,
  5539. "DCBX offload is not supported or is disabled for this PF.\n");
  5540. } else {
  5541. /* When status is not DISABLED then DCBX in FW */
  5542. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  5543. DCB_CAP_DCBX_VER_IEEE;
  5544. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  5545. /* Enable DCB tagging only when more than one TC
  5546. * or explicitly disable if only one TC
  5547. */
  5548. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5549. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5550. else
  5551. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5552. dev_dbg(&pf->pdev->dev,
  5553. "DCBX offload is supported for this PF.\n");
  5554. }
  5555. } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
  5556. dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
  5557. pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
  5558. } else {
  5559. dev_info(&pf->pdev->dev,
  5560. "Query for DCB configuration failed, err %s aq_err %s\n",
  5561. i40e_stat_str(&pf->hw, err),
  5562. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5563. }
  5564. out:
  5565. return err;
  5566. }
  5567. #endif /* CONFIG_I40E_DCB */
  5568. #define SPEED_SIZE 14
  5569. #define FC_SIZE 8
  5570. /**
  5571. * i40e_print_link_message - print link up or down
  5572. * @vsi: the VSI for which link needs a message
  5573. * @isup: true of link is up, false otherwise
  5574. */
  5575. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  5576. {
  5577. enum i40e_aq_link_speed new_speed;
  5578. struct i40e_pf *pf = vsi->back;
  5579. char *speed = "Unknown";
  5580. char *fc = "Unknown";
  5581. char *fec = "";
  5582. char *req_fec = "";
  5583. char *an = "";
  5584. new_speed = pf->hw.phy.link_info.link_speed;
  5585. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  5586. return;
  5587. vsi->current_isup = isup;
  5588. vsi->current_speed = new_speed;
  5589. if (!isup) {
  5590. netdev_info(vsi->netdev, "NIC Link is Down\n");
  5591. return;
  5592. }
  5593. /* Warn user if link speed on NPAR enabled partition is not at
  5594. * least 10GB
  5595. */
  5596. if (pf->hw.func_caps.npar_enable &&
  5597. (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  5598. pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  5599. netdev_warn(vsi->netdev,
  5600. "The partition detected link speed that is less than 10Gbps\n");
  5601. switch (pf->hw.phy.link_info.link_speed) {
  5602. case I40E_LINK_SPEED_40GB:
  5603. speed = "40 G";
  5604. break;
  5605. case I40E_LINK_SPEED_20GB:
  5606. speed = "20 G";
  5607. break;
  5608. case I40E_LINK_SPEED_25GB:
  5609. speed = "25 G";
  5610. break;
  5611. case I40E_LINK_SPEED_10GB:
  5612. speed = "10 G";
  5613. break;
  5614. case I40E_LINK_SPEED_1GB:
  5615. speed = "1000 M";
  5616. break;
  5617. case I40E_LINK_SPEED_100MB:
  5618. speed = "100 M";
  5619. break;
  5620. default:
  5621. break;
  5622. }
  5623. switch (pf->hw.fc.current_mode) {
  5624. case I40E_FC_FULL:
  5625. fc = "RX/TX";
  5626. break;
  5627. case I40E_FC_TX_PAUSE:
  5628. fc = "TX";
  5629. break;
  5630. case I40E_FC_RX_PAUSE:
  5631. fc = "RX";
  5632. break;
  5633. default:
  5634. fc = "None";
  5635. break;
  5636. }
  5637. if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  5638. req_fec = ", Requested FEC: None";
  5639. fec = ", FEC: None";
  5640. an = ", Autoneg: False";
  5641. if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  5642. an = ", Autoneg: True";
  5643. if (pf->hw.phy.link_info.fec_info &
  5644. I40E_AQ_CONFIG_FEC_KR_ENA)
  5645. fec = ", FEC: CL74 FC-FEC/BASE-R";
  5646. else if (pf->hw.phy.link_info.fec_info &
  5647. I40E_AQ_CONFIG_FEC_RS_ENA)
  5648. fec = ", FEC: CL108 RS-FEC";
  5649. /* 'CL108 RS-FEC' should be displayed when RS is requested, or
  5650. * both RS and FC are requested
  5651. */
  5652. if (vsi->back->hw.phy.link_info.req_fec_info &
  5653. (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
  5654. if (vsi->back->hw.phy.link_info.req_fec_info &
  5655. I40E_AQ_REQUEST_FEC_RS)
  5656. req_fec = ", Requested FEC: CL108 RS-FEC";
  5657. else
  5658. req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
  5659. }
  5660. }
  5661. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
  5662. speed, req_fec, fec, an, fc);
  5663. }
  5664. /**
  5665. * i40e_up_complete - Finish the last steps of bringing up a connection
  5666. * @vsi: the VSI being configured
  5667. **/
  5668. static int i40e_up_complete(struct i40e_vsi *vsi)
  5669. {
  5670. struct i40e_pf *pf = vsi->back;
  5671. int err;
  5672. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5673. i40e_vsi_configure_msix(vsi);
  5674. else
  5675. i40e_configure_msi_and_legacy(vsi);
  5676. /* start rings */
  5677. err = i40e_vsi_start_rings(vsi);
  5678. if (err)
  5679. return err;
  5680. clear_bit(__I40E_VSI_DOWN, vsi->state);
  5681. i40e_napi_enable_all(vsi);
  5682. i40e_vsi_enable_irq(vsi);
  5683. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  5684. (vsi->netdev)) {
  5685. i40e_print_link_message(vsi, true);
  5686. netif_tx_start_all_queues(vsi->netdev);
  5687. netif_carrier_on(vsi->netdev);
  5688. }
  5689. /* replay FDIR SB filters */
  5690. if (vsi->type == I40E_VSI_FDIR) {
  5691. /* reset fd counters */
  5692. pf->fd_add_err = 0;
  5693. pf->fd_atr_cnt = 0;
  5694. i40e_fdir_filter_restore(vsi);
  5695. }
  5696. /* On the next run of the service_task, notify any clients of the new
  5697. * opened netdev
  5698. */
  5699. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  5700. i40e_service_event_schedule(pf);
  5701. return 0;
  5702. }
  5703. /**
  5704. * i40e_vsi_reinit_locked - Reset the VSI
  5705. * @vsi: the VSI being configured
  5706. *
  5707. * Rebuild the ring structs after some configuration
  5708. * has changed, e.g. MTU size.
  5709. **/
  5710. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  5711. {
  5712. struct i40e_pf *pf = vsi->back;
  5713. WARN_ON(in_interrupt());
  5714. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
  5715. usleep_range(1000, 2000);
  5716. i40e_down(vsi);
  5717. i40e_up(vsi);
  5718. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  5719. }
  5720. /**
  5721. * i40e_up - Bring the connection back up after being down
  5722. * @vsi: the VSI being configured
  5723. **/
  5724. int i40e_up(struct i40e_vsi *vsi)
  5725. {
  5726. int err;
  5727. err = i40e_vsi_configure(vsi);
  5728. if (!err)
  5729. err = i40e_up_complete(vsi);
  5730. return err;
  5731. }
  5732. /**
  5733. * i40e_force_link_state - Force the link status
  5734. * @pf: board private structure
  5735. * @is_up: whether the link state should be forced up or down
  5736. **/
  5737. static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
  5738. {
  5739. struct i40e_aq_get_phy_abilities_resp abilities;
  5740. struct i40e_aq_set_phy_config config = {0};
  5741. struct i40e_hw *hw = &pf->hw;
  5742. i40e_status err;
  5743. u64 mask;
  5744. /* Get the current phy config */
  5745. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
  5746. NULL);
  5747. if (err) {
  5748. dev_err(&pf->pdev->dev,
  5749. "failed to get phy cap., ret = %s last_status = %s\n",
  5750. i40e_stat_str(hw, err),
  5751. i40e_aq_str(hw, hw->aq.asq_last_status));
  5752. return err;
  5753. }
  5754. /* If link needs to go up, but was not forced to go down,
  5755. * no need for a flap
  5756. */
  5757. if (is_up && abilities.phy_type != 0)
  5758. return I40E_SUCCESS;
  5759. /* To force link we need to set bits for all supported PHY types,
  5760. * but there are now more than 32, so we need to split the bitmap
  5761. * across two fields.
  5762. */
  5763. mask = I40E_PHY_TYPES_BITMASK;
  5764. config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
  5765. config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
  5766. /* Copy the old settings, except of phy_type */
  5767. config.abilities = abilities.abilities;
  5768. config.link_speed = abilities.link_speed;
  5769. config.eee_capability = abilities.eee_capability;
  5770. config.eeer = abilities.eeer_val;
  5771. config.low_power_ctrl = abilities.d3_lpan;
  5772. config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
  5773. I40E_AQ_PHY_FEC_CONFIG_MASK;
  5774. err = i40e_aq_set_phy_config(hw, &config, NULL);
  5775. if (err) {
  5776. dev_err(&pf->pdev->dev,
  5777. "set phy config ret = %s last_status = %s\n",
  5778. i40e_stat_str(&pf->hw, err),
  5779. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5780. return err;
  5781. }
  5782. /* Update the link info */
  5783. err = i40e_update_link_info(hw);
  5784. if (err) {
  5785. /* Wait a little bit (on 40G cards it sometimes takes a really
  5786. * long time for link to come back from the atomic reset)
  5787. * and try once more
  5788. */
  5789. msleep(1000);
  5790. i40e_update_link_info(hw);
  5791. }
  5792. i40e_aq_set_link_restart_an(hw, true, NULL);
  5793. return I40E_SUCCESS;
  5794. }
  5795. /**
  5796. * i40e_down - Shutdown the connection processing
  5797. * @vsi: the VSI being stopped
  5798. **/
  5799. void i40e_down(struct i40e_vsi *vsi)
  5800. {
  5801. int i;
  5802. /* It is assumed that the caller of this function
  5803. * sets the vsi->state __I40E_VSI_DOWN bit.
  5804. */
  5805. if (vsi->netdev) {
  5806. netif_carrier_off(vsi->netdev);
  5807. netif_tx_disable(vsi->netdev);
  5808. }
  5809. i40e_vsi_disable_irq(vsi);
  5810. i40e_vsi_stop_rings(vsi);
  5811. if (vsi->type == I40E_VSI_MAIN &&
  5812. vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
  5813. i40e_force_link_state(vsi->back, false);
  5814. i40e_napi_disable_all(vsi);
  5815. for (i = 0; i < vsi->num_queue_pairs; i++) {
  5816. i40e_clean_tx_ring(vsi->tx_rings[i]);
  5817. if (i40e_enabled_xdp_vsi(vsi))
  5818. i40e_clean_tx_ring(vsi->xdp_rings[i]);
  5819. i40e_clean_rx_ring(vsi->rx_rings[i]);
  5820. }
  5821. }
  5822. /**
  5823. * i40e_validate_mqprio_qopt- validate queue mapping info
  5824. * @vsi: the VSI being configured
  5825. * @mqprio_qopt: queue parametrs
  5826. **/
  5827. static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
  5828. struct tc_mqprio_qopt_offload *mqprio_qopt)
  5829. {
  5830. u64 sum_max_rate = 0;
  5831. u64 max_rate = 0;
  5832. int i;
  5833. if (mqprio_qopt->qopt.offset[0] != 0 ||
  5834. mqprio_qopt->qopt.num_tc < 1 ||
  5835. mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
  5836. return -EINVAL;
  5837. for (i = 0; ; i++) {
  5838. if (!mqprio_qopt->qopt.count[i])
  5839. return -EINVAL;
  5840. if (mqprio_qopt->min_rate[i]) {
  5841. dev_err(&vsi->back->pdev->dev,
  5842. "Invalid min tx rate (greater than 0) specified\n");
  5843. return -EINVAL;
  5844. }
  5845. max_rate = mqprio_qopt->max_rate[i];
  5846. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5847. sum_max_rate += max_rate;
  5848. if (i >= mqprio_qopt->qopt.num_tc - 1)
  5849. break;
  5850. if (mqprio_qopt->qopt.offset[i + 1] !=
  5851. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
  5852. return -EINVAL;
  5853. }
  5854. if (vsi->num_queue_pairs <
  5855. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
  5856. return -EINVAL;
  5857. }
  5858. if (sum_max_rate > i40e_get_link_speed(vsi)) {
  5859. dev_err(&vsi->back->pdev->dev,
  5860. "Invalid max tx rate specified\n");
  5861. return -EINVAL;
  5862. }
  5863. return 0;
  5864. }
  5865. /**
  5866. * i40e_vsi_set_default_tc_config - set default values for tc configuration
  5867. * @vsi: the VSI being configured
  5868. **/
  5869. static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
  5870. {
  5871. u16 qcount;
  5872. int i;
  5873. /* Only TC0 is enabled */
  5874. vsi->tc_config.numtc = 1;
  5875. vsi->tc_config.enabled_tc = 1;
  5876. qcount = min_t(int, vsi->alloc_queue_pairs,
  5877. i40e_pf_get_max_q_per_tc(vsi->back));
  5878. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5879. /* For the TC that is not enabled set the offset to to default
  5880. * queue and allocate one queue for the given TC.
  5881. */
  5882. vsi->tc_config.tc_info[i].qoffset = 0;
  5883. if (i == 0)
  5884. vsi->tc_config.tc_info[i].qcount = qcount;
  5885. else
  5886. vsi->tc_config.tc_info[i].qcount = 1;
  5887. vsi->tc_config.tc_info[i].netdev_tc = 0;
  5888. }
  5889. }
  5890. /**
  5891. * i40e_setup_tc - configure multiple traffic classes
  5892. * @netdev: net device to configure
  5893. * @type_data: tc offload data
  5894. **/
  5895. static int i40e_setup_tc(struct net_device *netdev, void *type_data)
  5896. {
  5897. struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
  5898. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5899. struct i40e_vsi *vsi = np->vsi;
  5900. struct i40e_pf *pf = vsi->back;
  5901. u8 enabled_tc = 0, num_tc, hw;
  5902. bool need_reset = false;
  5903. int ret = -EINVAL;
  5904. u16 mode;
  5905. int i;
  5906. num_tc = mqprio_qopt->qopt.num_tc;
  5907. hw = mqprio_qopt->qopt.hw;
  5908. mode = mqprio_qopt->mode;
  5909. if (!hw) {
  5910. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5911. memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
  5912. goto config_tc;
  5913. }
  5914. /* Check if MFP enabled */
  5915. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  5916. netdev_info(netdev,
  5917. "Configuring TC not supported in MFP mode\n");
  5918. return ret;
  5919. }
  5920. switch (mode) {
  5921. case TC_MQPRIO_MODE_DCB:
  5922. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5923. /* Check if DCB enabled to continue */
  5924. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  5925. netdev_info(netdev,
  5926. "DCB is not enabled for adapter\n");
  5927. return ret;
  5928. }
  5929. /* Check whether tc count is within enabled limit */
  5930. if (num_tc > i40e_pf_get_num_tc(pf)) {
  5931. netdev_info(netdev,
  5932. "TC count greater than enabled on link for adapter\n");
  5933. return ret;
  5934. }
  5935. break;
  5936. case TC_MQPRIO_MODE_CHANNEL:
  5937. if (pf->flags & I40E_FLAG_DCB_ENABLED) {
  5938. netdev_info(netdev,
  5939. "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
  5940. return ret;
  5941. }
  5942. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  5943. return ret;
  5944. ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
  5945. if (ret)
  5946. return ret;
  5947. memcpy(&vsi->mqprio_qopt, mqprio_qopt,
  5948. sizeof(*mqprio_qopt));
  5949. pf->flags |= I40E_FLAG_TC_MQPRIO;
  5950. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5951. break;
  5952. default:
  5953. return -EINVAL;
  5954. }
  5955. config_tc:
  5956. /* Generate TC map for number of tc requested */
  5957. for (i = 0; i < num_tc; i++)
  5958. enabled_tc |= BIT(i);
  5959. /* Requesting same TC configuration as already enabled */
  5960. if (enabled_tc == vsi->tc_config.enabled_tc &&
  5961. mode != TC_MQPRIO_MODE_CHANNEL)
  5962. return 0;
  5963. /* Quiesce VSI queues */
  5964. i40e_quiesce_vsi(vsi);
  5965. if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
  5966. i40e_remove_queue_channels(vsi);
  5967. /* Configure VSI for enabled TCs */
  5968. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  5969. if (ret) {
  5970. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  5971. vsi->seid);
  5972. need_reset = true;
  5973. goto exit;
  5974. }
  5975. if (pf->flags & I40E_FLAG_TC_MQPRIO) {
  5976. if (vsi->mqprio_qopt.max_rate[0]) {
  5977. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  5978. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  5979. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  5980. if (!ret) {
  5981. u64 credits = max_tx_rate;
  5982. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5983. dev_dbg(&vsi->back->pdev->dev,
  5984. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5985. max_tx_rate,
  5986. credits,
  5987. vsi->seid);
  5988. } else {
  5989. need_reset = true;
  5990. goto exit;
  5991. }
  5992. }
  5993. ret = i40e_configure_queue_channels(vsi);
  5994. if (ret) {
  5995. netdev_info(netdev,
  5996. "Failed configuring queue channels\n");
  5997. need_reset = true;
  5998. goto exit;
  5999. }
  6000. }
  6001. exit:
  6002. /* Reset the configuration data to defaults, only TC0 is enabled */
  6003. if (need_reset) {
  6004. i40e_vsi_set_default_tc_config(vsi);
  6005. need_reset = false;
  6006. }
  6007. /* Unquiesce VSI */
  6008. i40e_unquiesce_vsi(vsi);
  6009. return ret;
  6010. }
  6011. /**
  6012. * i40e_set_cld_element - sets cloud filter element data
  6013. * @filter: cloud filter rule
  6014. * @cld: ptr to cloud filter element data
  6015. *
  6016. * This is helper function to copy data into cloud filter element
  6017. **/
  6018. static inline void
  6019. i40e_set_cld_element(struct i40e_cloud_filter *filter,
  6020. struct i40e_aqc_cloud_filters_element_data *cld)
  6021. {
  6022. int i, j;
  6023. u32 ipa;
  6024. memset(cld, 0, sizeof(*cld));
  6025. ether_addr_copy(cld->outer_mac, filter->dst_mac);
  6026. ether_addr_copy(cld->inner_mac, filter->src_mac);
  6027. if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
  6028. return;
  6029. if (filter->n_proto == ETH_P_IPV6) {
  6030. #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
  6031. for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
  6032. i++, j += 2) {
  6033. ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
  6034. ipa = cpu_to_le32(ipa);
  6035. memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
  6036. }
  6037. } else {
  6038. ipa = be32_to_cpu(filter->dst_ipv4);
  6039. memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
  6040. }
  6041. cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
  6042. /* tenant_id is not supported by FW now, once the support is enabled
  6043. * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
  6044. */
  6045. if (filter->tenant_id)
  6046. return;
  6047. }
  6048. /**
  6049. * i40e_add_del_cloud_filter - Add/del cloud filter
  6050. * @vsi: pointer to VSI
  6051. * @filter: cloud filter rule
  6052. * @add: if true, add, if false, delete
  6053. *
  6054. * Add or delete a cloud filter for a specific flow spec.
  6055. * Returns 0 if the filter were successfully added.
  6056. **/
  6057. int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
  6058. struct i40e_cloud_filter *filter, bool add)
  6059. {
  6060. struct i40e_aqc_cloud_filters_element_data cld_filter;
  6061. struct i40e_pf *pf = vsi->back;
  6062. int ret;
  6063. static const u16 flag_table[128] = {
  6064. [I40E_CLOUD_FILTER_FLAGS_OMAC] =
  6065. I40E_AQC_ADD_CLOUD_FILTER_OMAC,
  6066. [I40E_CLOUD_FILTER_FLAGS_IMAC] =
  6067. I40E_AQC_ADD_CLOUD_FILTER_IMAC,
  6068. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
  6069. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
  6070. [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
  6071. I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
  6072. [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
  6073. I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
  6074. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
  6075. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
  6076. [I40E_CLOUD_FILTER_FLAGS_IIP] =
  6077. I40E_AQC_ADD_CLOUD_FILTER_IIP,
  6078. };
  6079. if (filter->flags >= ARRAY_SIZE(flag_table))
  6080. return I40E_ERR_CONFIG;
  6081. /* copy element needed to add cloud filter from filter */
  6082. i40e_set_cld_element(filter, &cld_filter);
  6083. if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
  6084. cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
  6085. I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
  6086. if (filter->n_proto == ETH_P_IPV6)
  6087. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6088. I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6089. else
  6090. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6091. I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6092. if (add)
  6093. ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
  6094. &cld_filter, 1);
  6095. else
  6096. ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
  6097. &cld_filter, 1);
  6098. if (ret)
  6099. dev_dbg(&pf->pdev->dev,
  6100. "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
  6101. add ? "add" : "delete", filter->dst_port, ret,
  6102. pf->hw.aq.asq_last_status);
  6103. else
  6104. dev_info(&pf->pdev->dev,
  6105. "%s cloud filter for VSI: %d\n",
  6106. add ? "Added" : "Deleted", filter->seid);
  6107. return ret;
  6108. }
  6109. /**
  6110. * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
  6111. * @vsi: pointer to VSI
  6112. * @filter: cloud filter rule
  6113. * @add: if true, add, if false, delete
  6114. *
  6115. * Add or delete a cloud filter for a specific flow spec using big buffer.
  6116. * Returns 0 if the filter were successfully added.
  6117. **/
  6118. int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
  6119. struct i40e_cloud_filter *filter,
  6120. bool add)
  6121. {
  6122. struct i40e_aqc_cloud_filters_element_bb cld_filter;
  6123. struct i40e_pf *pf = vsi->back;
  6124. int ret;
  6125. /* Both (src/dst) valid mac_addr are not supported */
  6126. if ((is_valid_ether_addr(filter->dst_mac) &&
  6127. is_valid_ether_addr(filter->src_mac)) ||
  6128. (is_multicast_ether_addr(filter->dst_mac) &&
  6129. is_multicast_ether_addr(filter->src_mac)))
  6130. return -EOPNOTSUPP;
  6131. /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
  6132. * ports are not supported via big buffer now.
  6133. */
  6134. if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
  6135. return -EOPNOTSUPP;
  6136. /* adding filter using src_port/src_ip is not supported at this stage */
  6137. if (filter->src_port || filter->src_ipv4 ||
  6138. !ipv6_addr_any(&filter->ip.v6.src_ip6))
  6139. return -EOPNOTSUPP;
  6140. /* copy element needed to add cloud filter from filter */
  6141. i40e_set_cld_element(filter, &cld_filter.element);
  6142. if (is_valid_ether_addr(filter->dst_mac) ||
  6143. is_valid_ether_addr(filter->src_mac) ||
  6144. is_multicast_ether_addr(filter->dst_mac) ||
  6145. is_multicast_ether_addr(filter->src_mac)) {
  6146. /* MAC + IP : unsupported mode */
  6147. if (filter->dst_ipv4)
  6148. return -EOPNOTSUPP;
  6149. /* since we validated that L4 port must be valid before
  6150. * we get here, start with respective "flags" value
  6151. * and update if vlan is present or not
  6152. */
  6153. cld_filter.element.flags =
  6154. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
  6155. if (filter->vlan_id) {
  6156. cld_filter.element.flags =
  6157. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
  6158. }
  6159. } else if (filter->dst_ipv4 ||
  6160. !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
  6161. cld_filter.element.flags =
  6162. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
  6163. if (filter->n_proto == ETH_P_IPV6)
  6164. cld_filter.element.flags |=
  6165. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6166. else
  6167. cld_filter.element.flags |=
  6168. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6169. } else {
  6170. dev_err(&pf->pdev->dev,
  6171. "either mac or ip has to be valid for cloud filter\n");
  6172. return -EINVAL;
  6173. }
  6174. /* Now copy L4 port in Byte 6..7 in general fields */
  6175. cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
  6176. be16_to_cpu(filter->dst_port);
  6177. if (add) {
  6178. /* Validate current device switch mode, change if necessary */
  6179. ret = i40e_validate_and_set_switch_mode(vsi);
  6180. if (ret) {
  6181. dev_err(&pf->pdev->dev,
  6182. "failed to set switch mode, ret %d\n",
  6183. ret);
  6184. return ret;
  6185. }
  6186. ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
  6187. &cld_filter, 1);
  6188. } else {
  6189. ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
  6190. &cld_filter, 1);
  6191. }
  6192. if (ret)
  6193. dev_dbg(&pf->pdev->dev,
  6194. "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
  6195. add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
  6196. else
  6197. dev_info(&pf->pdev->dev,
  6198. "%s cloud filter for VSI: %d, L4 port: %d\n",
  6199. add ? "add" : "delete", filter->seid,
  6200. ntohs(filter->dst_port));
  6201. return ret;
  6202. }
  6203. /**
  6204. * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
  6205. * @vsi: Pointer to VSI
  6206. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6207. * @filter: Pointer to cloud filter structure
  6208. *
  6209. **/
  6210. static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
  6211. struct tc_cls_flower_offload *f,
  6212. struct i40e_cloud_filter *filter)
  6213. {
  6214. u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
  6215. struct i40e_pf *pf = vsi->back;
  6216. u8 field_flags = 0;
  6217. if (f->dissector->used_keys &
  6218. ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  6219. BIT(FLOW_DISSECTOR_KEY_BASIC) |
  6220. BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
  6221. BIT(FLOW_DISSECTOR_KEY_VLAN) |
  6222. BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  6223. BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  6224. BIT(FLOW_DISSECTOR_KEY_PORTS) |
  6225. BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
  6226. dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
  6227. f->dissector->used_keys);
  6228. return -EOPNOTSUPP;
  6229. }
  6230. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  6231. struct flow_dissector_key_keyid *key =
  6232. skb_flow_dissector_target(f->dissector,
  6233. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6234. f->key);
  6235. struct flow_dissector_key_keyid *mask =
  6236. skb_flow_dissector_target(f->dissector,
  6237. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6238. f->mask);
  6239. if (mask->keyid != 0)
  6240. field_flags |= I40E_CLOUD_FIELD_TEN_ID;
  6241. filter->tenant_id = be32_to_cpu(key->keyid);
  6242. }
  6243. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  6244. struct flow_dissector_key_basic *key =
  6245. skb_flow_dissector_target(f->dissector,
  6246. FLOW_DISSECTOR_KEY_BASIC,
  6247. f->key);
  6248. struct flow_dissector_key_basic *mask =
  6249. skb_flow_dissector_target(f->dissector,
  6250. FLOW_DISSECTOR_KEY_BASIC,
  6251. f->mask);
  6252. n_proto_key = ntohs(key->n_proto);
  6253. n_proto_mask = ntohs(mask->n_proto);
  6254. if (n_proto_key == ETH_P_ALL) {
  6255. n_proto_key = 0;
  6256. n_proto_mask = 0;
  6257. }
  6258. filter->n_proto = n_proto_key & n_proto_mask;
  6259. filter->ip_proto = key->ip_proto;
  6260. }
  6261. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  6262. struct flow_dissector_key_eth_addrs *key =
  6263. skb_flow_dissector_target(f->dissector,
  6264. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6265. f->key);
  6266. struct flow_dissector_key_eth_addrs *mask =
  6267. skb_flow_dissector_target(f->dissector,
  6268. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6269. f->mask);
  6270. /* use is_broadcast and is_zero to check for all 0xf or 0 */
  6271. if (!is_zero_ether_addr(mask->dst)) {
  6272. if (is_broadcast_ether_addr(mask->dst)) {
  6273. field_flags |= I40E_CLOUD_FIELD_OMAC;
  6274. } else {
  6275. dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
  6276. mask->dst);
  6277. return I40E_ERR_CONFIG;
  6278. }
  6279. }
  6280. if (!is_zero_ether_addr(mask->src)) {
  6281. if (is_broadcast_ether_addr(mask->src)) {
  6282. field_flags |= I40E_CLOUD_FIELD_IMAC;
  6283. } else {
  6284. dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
  6285. mask->src);
  6286. return I40E_ERR_CONFIG;
  6287. }
  6288. }
  6289. ether_addr_copy(filter->dst_mac, key->dst);
  6290. ether_addr_copy(filter->src_mac, key->src);
  6291. }
  6292. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
  6293. struct flow_dissector_key_vlan *key =
  6294. skb_flow_dissector_target(f->dissector,
  6295. FLOW_DISSECTOR_KEY_VLAN,
  6296. f->key);
  6297. struct flow_dissector_key_vlan *mask =
  6298. skb_flow_dissector_target(f->dissector,
  6299. FLOW_DISSECTOR_KEY_VLAN,
  6300. f->mask);
  6301. if (mask->vlan_id) {
  6302. if (mask->vlan_id == VLAN_VID_MASK) {
  6303. field_flags |= I40E_CLOUD_FIELD_IVLAN;
  6304. } else {
  6305. dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
  6306. mask->vlan_id);
  6307. return I40E_ERR_CONFIG;
  6308. }
  6309. }
  6310. filter->vlan_id = cpu_to_be16(key->vlan_id);
  6311. }
  6312. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
  6313. struct flow_dissector_key_control *key =
  6314. skb_flow_dissector_target(f->dissector,
  6315. FLOW_DISSECTOR_KEY_CONTROL,
  6316. f->key);
  6317. addr_type = key->addr_type;
  6318. }
  6319. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  6320. struct flow_dissector_key_ipv4_addrs *key =
  6321. skb_flow_dissector_target(f->dissector,
  6322. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6323. f->key);
  6324. struct flow_dissector_key_ipv4_addrs *mask =
  6325. skb_flow_dissector_target(f->dissector,
  6326. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6327. f->mask);
  6328. if (mask->dst) {
  6329. if (mask->dst == cpu_to_be32(0xffffffff)) {
  6330. field_flags |= I40E_CLOUD_FIELD_IIP;
  6331. } else {
  6332. dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
  6333. &mask->dst);
  6334. return I40E_ERR_CONFIG;
  6335. }
  6336. }
  6337. if (mask->src) {
  6338. if (mask->src == cpu_to_be32(0xffffffff)) {
  6339. field_flags |= I40E_CLOUD_FIELD_IIP;
  6340. } else {
  6341. dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
  6342. &mask->src);
  6343. return I40E_ERR_CONFIG;
  6344. }
  6345. }
  6346. if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
  6347. dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
  6348. return I40E_ERR_CONFIG;
  6349. }
  6350. filter->dst_ipv4 = key->dst;
  6351. filter->src_ipv4 = key->src;
  6352. }
  6353. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  6354. struct flow_dissector_key_ipv6_addrs *key =
  6355. skb_flow_dissector_target(f->dissector,
  6356. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6357. f->key);
  6358. struct flow_dissector_key_ipv6_addrs *mask =
  6359. skb_flow_dissector_target(f->dissector,
  6360. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6361. f->mask);
  6362. /* src and dest IPV6 address should not be LOOPBACK
  6363. * (0:0:0:0:0:0:0:1), which can be represented as ::1
  6364. */
  6365. if (ipv6_addr_loopback(&key->dst) ||
  6366. ipv6_addr_loopback(&key->src)) {
  6367. dev_err(&pf->pdev->dev,
  6368. "Bad ipv6, addr is LOOPBACK\n");
  6369. return I40E_ERR_CONFIG;
  6370. }
  6371. if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
  6372. field_flags |= I40E_CLOUD_FIELD_IIP;
  6373. memcpy(&filter->src_ipv6, &key->src.s6_addr32,
  6374. sizeof(filter->src_ipv6));
  6375. memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
  6376. sizeof(filter->dst_ipv6));
  6377. }
  6378. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
  6379. struct flow_dissector_key_ports *key =
  6380. skb_flow_dissector_target(f->dissector,
  6381. FLOW_DISSECTOR_KEY_PORTS,
  6382. f->key);
  6383. struct flow_dissector_key_ports *mask =
  6384. skb_flow_dissector_target(f->dissector,
  6385. FLOW_DISSECTOR_KEY_PORTS,
  6386. f->mask);
  6387. if (mask->src) {
  6388. if (mask->src == cpu_to_be16(0xffff)) {
  6389. field_flags |= I40E_CLOUD_FIELD_IIP;
  6390. } else {
  6391. dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
  6392. be16_to_cpu(mask->src));
  6393. return I40E_ERR_CONFIG;
  6394. }
  6395. }
  6396. if (mask->dst) {
  6397. if (mask->dst == cpu_to_be16(0xffff)) {
  6398. field_flags |= I40E_CLOUD_FIELD_IIP;
  6399. } else {
  6400. dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
  6401. be16_to_cpu(mask->dst));
  6402. return I40E_ERR_CONFIG;
  6403. }
  6404. }
  6405. filter->dst_port = key->dst;
  6406. filter->src_port = key->src;
  6407. switch (filter->ip_proto) {
  6408. case IPPROTO_TCP:
  6409. case IPPROTO_UDP:
  6410. break;
  6411. default:
  6412. dev_err(&pf->pdev->dev,
  6413. "Only UDP and TCP transport are supported\n");
  6414. return -EINVAL;
  6415. }
  6416. }
  6417. filter->flags = field_flags;
  6418. return 0;
  6419. }
  6420. /**
  6421. * i40e_handle_tclass: Forward to a traffic class on the device
  6422. * @vsi: Pointer to VSI
  6423. * @tc: traffic class index on the device
  6424. * @filter: Pointer to cloud filter structure
  6425. *
  6426. **/
  6427. static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
  6428. struct i40e_cloud_filter *filter)
  6429. {
  6430. struct i40e_channel *ch, *ch_tmp;
  6431. /* direct to a traffic class on the same device */
  6432. if (tc == 0) {
  6433. filter->seid = vsi->seid;
  6434. return 0;
  6435. } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
  6436. if (!filter->dst_port) {
  6437. dev_err(&vsi->back->pdev->dev,
  6438. "Specify destination port to direct to traffic class that is not default\n");
  6439. return -EINVAL;
  6440. }
  6441. if (list_empty(&vsi->ch_list))
  6442. return -EINVAL;
  6443. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
  6444. list) {
  6445. if (ch->seid == vsi->tc_seid_map[tc])
  6446. filter->seid = ch->seid;
  6447. }
  6448. return 0;
  6449. }
  6450. dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
  6451. return -EINVAL;
  6452. }
  6453. /**
  6454. * i40e_configure_clsflower - Configure tc flower filters
  6455. * @vsi: Pointer to VSI
  6456. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6457. *
  6458. **/
  6459. static int i40e_configure_clsflower(struct i40e_vsi *vsi,
  6460. struct tc_cls_flower_offload *cls_flower)
  6461. {
  6462. int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
  6463. struct i40e_cloud_filter *filter = NULL;
  6464. struct i40e_pf *pf = vsi->back;
  6465. int err = 0;
  6466. if (tc < 0) {
  6467. dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
  6468. return -EOPNOTSUPP;
  6469. }
  6470. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  6471. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  6472. return -EBUSY;
  6473. if (pf->fdir_pf_active_filters ||
  6474. (!hlist_empty(&pf->fdir_filter_list))) {
  6475. dev_err(&vsi->back->pdev->dev,
  6476. "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
  6477. return -EINVAL;
  6478. }
  6479. if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
  6480. dev_err(&vsi->back->pdev->dev,
  6481. "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
  6482. vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6483. vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6484. }
  6485. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  6486. if (!filter)
  6487. return -ENOMEM;
  6488. filter->cookie = cls_flower->cookie;
  6489. err = i40e_parse_cls_flower(vsi, cls_flower, filter);
  6490. if (err < 0)
  6491. goto err;
  6492. err = i40e_handle_tclass(vsi, tc, filter);
  6493. if (err < 0)
  6494. goto err;
  6495. /* Add cloud filter */
  6496. if (filter->dst_port)
  6497. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
  6498. else
  6499. err = i40e_add_del_cloud_filter(vsi, filter, true);
  6500. if (err) {
  6501. dev_err(&pf->pdev->dev,
  6502. "Failed to add cloud filter, err %s\n",
  6503. i40e_stat_str(&pf->hw, err));
  6504. goto err;
  6505. }
  6506. /* add filter to the ordered list */
  6507. INIT_HLIST_NODE(&filter->cloud_node);
  6508. hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
  6509. pf->num_cloud_filters++;
  6510. return err;
  6511. err:
  6512. kfree(filter);
  6513. return err;
  6514. }
  6515. /**
  6516. * i40e_find_cloud_filter - Find the could filter in the list
  6517. * @vsi: Pointer to VSI
  6518. * @cookie: filter specific cookie
  6519. *
  6520. **/
  6521. static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
  6522. unsigned long *cookie)
  6523. {
  6524. struct i40e_cloud_filter *filter = NULL;
  6525. struct hlist_node *node2;
  6526. hlist_for_each_entry_safe(filter, node2,
  6527. &vsi->back->cloud_filter_list, cloud_node)
  6528. if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
  6529. return filter;
  6530. return NULL;
  6531. }
  6532. /**
  6533. * i40e_delete_clsflower - Remove tc flower filters
  6534. * @vsi: Pointer to VSI
  6535. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6536. *
  6537. **/
  6538. static int i40e_delete_clsflower(struct i40e_vsi *vsi,
  6539. struct tc_cls_flower_offload *cls_flower)
  6540. {
  6541. struct i40e_cloud_filter *filter = NULL;
  6542. struct i40e_pf *pf = vsi->back;
  6543. int err = 0;
  6544. filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
  6545. if (!filter)
  6546. return -EINVAL;
  6547. hash_del(&filter->cloud_node);
  6548. if (filter->dst_port)
  6549. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
  6550. else
  6551. err = i40e_add_del_cloud_filter(vsi, filter, false);
  6552. kfree(filter);
  6553. if (err) {
  6554. dev_err(&pf->pdev->dev,
  6555. "Failed to delete cloud filter, err %s\n",
  6556. i40e_stat_str(&pf->hw, err));
  6557. return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
  6558. }
  6559. pf->num_cloud_filters--;
  6560. if (!pf->num_cloud_filters)
  6561. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6562. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6563. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6564. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6565. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6566. }
  6567. return 0;
  6568. }
  6569. /**
  6570. * i40e_setup_tc_cls_flower - flower classifier offloads
  6571. * @netdev: net device to configure
  6572. * @type_data: offload data
  6573. **/
  6574. static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
  6575. struct tc_cls_flower_offload *cls_flower)
  6576. {
  6577. struct i40e_vsi *vsi = np->vsi;
  6578. switch (cls_flower->command) {
  6579. case TC_CLSFLOWER_REPLACE:
  6580. return i40e_configure_clsflower(vsi, cls_flower);
  6581. case TC_CLSFLOWER_DESTROY:
  6582. return i40e_delete_clsflower(vsi, cls_flower);
  6583. case TC_CLSFLOWER_STATS:
  6584. return -EOPNOTSUPP;
  6585. default:
  6586. return -EOPNOTSUPP;
  6587. }
  6588. }
  6589. static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  6590. void *cb_priv)
  6591. {
  6592. struct i40e_netdev_priv *np = cb_priv;
  6593. if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
  6594. return -EOPNOTSUPP;
  6595. switch (type) {
  6596. case TC_SETUP_CLSFLOWER:
  6597. return i40e_setup_tc_cls_flower(np, type_data);
  6598. default:
  6599. return -EOPNOTSUPP;
  6600. }
  6601. }
  6602. static int i40e_setup_tc_block(struct net_device *dev,
  6603. struct tc_block_offload *f)
  6604. {
  6605. struct i40e_netdev_priv *np = netdev_priv(dev);
  6606. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  6607. return -EOPNOTSUPP;
  6608. switch (f->command) {
  6609. case TC_BLOCK_BIND:
  6610. return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
  6611. np, np, f->extack);
  6612. case TC_BLOCK_UNBIND:
  6613. tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
  6614. return 0;
  6615. default:
  6616. return -EOPNOTSUPP;
  6617. }
  6618. }
  6619. static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  6620. void *type_data)
  6621. {
  6622. switch (type) {
  6623. case TC_SETUP_QDISC_MQPRIO:
  6624. return i40e_setup_tc(netdev, type_data);
  6625. case TC_SETUP_BLOCK:
  6626. return i40e_setup_tc_block(netdev, type_data);
  6627. default:
  6628. return -EOPNOTSUPP;
  6629. }
  6630. }
  6631. /**
  6632. * i40e_open - Called when a network interface is made active
  6633. * @netdev: network interface device structure
  6634. *
  6635. * The open entry point is called when a network interface is made
  6636. * active by the system (IFF_UP). At this point all resources needed
  6637. * for transmit and receive operations are allocated, the interrupt
  6638. * handler is registered with the OS, the netdev watchdog subtask is
  6639. * enabled, and the stack is notified that the interface is ready.
  6640. *
  6641. * Returns 0 on success, negative value on failure
  6642. **/
  6643. int i40e_open(struct net_device *netdev)
  6644. {
  6645. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6646. struct i40e_vsi *vsi = np->vsi;
  6647. struct i40e_pf *pf = vsi->back;
  6648. int err;
  6649. /* disallow open during test or if eeprom is broken */
  6650. if (test_bit(__I40E_TESTING, pf->state) ||
  6651. test_bit(__I40E_BAD_EEPROM, pf->state))
  6652. return -EBUSY;
  6653. netif_carrier_off(netdev);
  6654. if (i40e_force_link_state(pf, true))
  6655. return -EAGAIN;
  6656. err = i40e_vsi_open(vsi);
  6657. if (err)
  6658. return err;
  6659. /* configure global TSO hardware offload settings */
  6660. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  6661. TCP_FLAG_FIN) >> 16);
  6662. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  6663. TCP_FLAG_FIN |
  6664. TCP_FLAG_CWR) >> 16);
  6665. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  6666. udp_tunnel_get_rx_info(netdev);
  6667. return 0;
  6668. }
  6669. /**
  6670. * i40e_vsi_open -
  6671. * @vsi: the VSI to open
  6672. *
  6673. * Finish initialization of the VSI.
  6674. *
  6675. * Returns 0 on success, negative value on failure
  6676. *
  6677. * Note: expects to be called while under rtnl_lock()
  6678. **/
  6679. int i40e_vsi_open(struct i40e_vsi *vsi)
  6680. {
  6681. struct i40e_pf *pf = vsi->back;
  6682. char int_name[I40E_INT_NAME_STR_LEN];
  6683. int err;
  6684. /* allocate descriptors */
  6685. err = i40e_vsi_setup_tx_resources(vsi);
  6686. if (err)
  6687. goto err_setup_tx;
  6688. err = i40e_vsi_setup_rx_resources(vsi);
  6689. if (err)
  6690. goto err_setup_rx;
  6691. err = i40e_vsi_configure(vsi);
  6692. if (err)
  6693. goto err_setup_rx;
  6694. if (vsi->netdev) {
  6695. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  6696. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  6697. err = i40e_vsi_request_irq(vsi, int_name);
  6698. if (err)
  6699. goto err_setup_rx;
  6700. /* Notify the stack of the actual queue counts. */
  6701. err = netif_set_real_num_tx_queues(vsi->netdev,
  6702. vsi->num_queue_pairs);
  6703. if (err)
  6704. goto err_set_queues;
  6705. err = netif_set_real_num_rx_queues(vsi->netdev,
  6706. vsi->num_queue_pairs);
  6707. if (err)
  6708. goto err_set_queues;
  6709. } else if (vsi->type == I40E_VSI_FDIR) {
  6710. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  6711. dev_driver_string(&pf->pdev->dev),
  6712. dev_name(&pf->pdev->dev));
  6713. err = i40e_vsi_request_irq(vsi, int_name);
  6714. } else {
  6715. err = -EINVAL;
  6716. goto err_setup_rx;
  6717. }
  6718. err = i40e_up_complete(vsi);
  6719. if (err)
  6720. goto err_up_complete;
  6721. return 0;
  6722. err_up_complete:
  6723. i40e_down(vsi);
  6724. err_set_queues:
  6725. i40e_vsi_free_irq(vsi);
  6726. err_setup_rx:
  6727. i40e_vsi_free_rx_resources(vsi);
  6728. err_setup_tx:
  6729. i40e_vsi_free_tx_resources(vsi);
  6730. if (vsi == pf->vsi[pf->lan_vsi])
  6731. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  6732. return err;
  6733. }
  6734. /**
  6735. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  6736. * @pf: Pointer to PF
  6737. *
  6738. * This function destroys the hlist where all the Flow Director
  6739. * filters were saved.
  6740. **/
  6741. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  6742. {
  6743. struct i40e_fdir_filter *filter;
  6744. struct i40e_flex_pit *pit_entry, *tmp;
  6745. struct hlist_node *node2;
  6746. hlist_for_each_entry_safe(filter, node2,
  6747. &pf->fdir_filter_list, fdir_node) {
  6748. hlist_del(&filter->fdir_node);
  6749. kfree(filter);
  6750. }
  6751. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  6752. list_del(&pit_entry->list);
  6753. kfree(pit_entry);
  6754. }
  6755. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  6756. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  6757. list_del(&pit_entry->list);
  6758. kfree(pit_entry);
  6759. }
  6760. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  6761. pf->fdir_pf_active_filters = 0;
  6762. pf->fd_tcp4_filter_cnt = 0;
  6763. pf->fd_udp4_filter_cnt = 0;
  6764. pf->fd_sctp4_filter_cnt = 0;
  6765. pf->fd_ip4_filter_cnt = 0;
  6766. /* Reprogram the default input set for TCP/IPv4 */
  6767. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  6768. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6769. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6770. /* Reprogram the default input set for UDP/IPv4 */
  6771. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  6772. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6773. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6774. /* Reprogram the default input set for SCTP/IPv4 */
  6775. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  6776. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6777. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6778. /* Reprogram the default input set for Other/IPv4 */
  6779. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  6780. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6781. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
  6782. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6783. }
  6784. /**
  6785. * i40e_cloud_filter_exit - Cleans up the cloud filters
  6786. * @pf: Pointer to PF
  6787. *
  6788. * This function destroys the hlist where all the cloud filters
  6789. * were saved.
  6790. **/
  6791. static void i40e_cloud_filter_exit(struct i40e_pf *pf)
  6792. {
  6793. struct i40e_cloud_filter *cfilter;
  6794. struct hlist_node *node;
  6795. hlist_for_each_entry_safe(cfilter, node,
  6796. &pf->cloud_filter_list, cloud_node) {
  6797. hlist_del(&cfilter->cloud_node);
  6798. kfree(cfilter);
  6799. }
  6800. pf->num_cloud_filters = 0;
  6801. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6802. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6803. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6804. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6805. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6806. }
  6807. }
  6808. /**
  6809. * i40e_close - Disables a network interface
  6810. * @netdev: network interface device structure
  6811. *
  6812. * The close entry point is called when an interface is de-activated
  6813. * by the OS. The hardware is still under the driver's control, but
  6814. * this netdev interface is disabled.
  6815. *
  6816. * Returns 0, this is not allowed to fail
  6817. **/
  6818. int i40e_close(struct net_device *netdev)
  6819. {
  6820. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6821. struct i40e_vsi *vsi = np->vsi;
  6822. i40e_vsi_close(vsi);
  6823. return 0;
  6824. }
  6825. /**
  6826. * i40e_do_reset - Start a PF or Core Reset sequence
  6827. * @pf: board private structure
  6828. * @reset_flags: which reset is requested
  6829. * @lock_acquired: indicates whether or not the lock has been acquired
  6830. * before this function was called.
  6831. *
  6832. * The essential difference in resets is that the PF Reset
  6833. * doesn't clear the packet buffers, doesn't reset the PE
  6834. * firmware, and doesn't bother the other PFs on the chip.
  6835. **/
  6836. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  6837. {
  6838. u32 val;
  6839. WARN_ON(in_interrupt());
  6840. /* do the biggest reset indicated */
  6841. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  6842. /* Request a Global Reset
  6843. *
  6844. * This will start the chip's countdown to the actual full
  6845. * chip reset event, and a warning interrupt to be sent
  6846. * to all PFs, including the requestor. Our handler
  6847. * for the warning interrupt will deal with the shutdown
  6848. * and recovery of the switch setup.
  6849. */
  6850. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  6851. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6852. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  6853. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6854. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  6855. /* Request a Core Reset
  6856. *
  6857. * Same as Global Reset, except does *not* include the MAC/PHY
  6858. */
  6859. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  6860. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6861. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  6862. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6863. i40e_flush(&pf->hw);
  6864. } else if (reset_flags & I40E_PF_RESET_FLAG) {
  6865. /* Request a PF Reset
  6866. *
  6867. * Resets only the PF-specific registers
  6868. *
  6869. * This goes directly to the tear-down and rebuild of
  6870. * the switch, since we need to do all the recovery as
  6871. * for the Core Reset.
  6872. */
  6873. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  6874. i40e_handle_reset_warning(pf, lock_acquired);
  6875. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  6876. int v;
  6877. /* Find the VSI(s) that requested a re-init */
  6878. dev_info(&pf->pdev->dev,
  6879. "VSI reinit requested\n");
  6880. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6881. struct i40e_vsi *vsi = pf->vsi[v];
  6882. if (vsi != NULL &&
  6883. test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
  6884. vsi->state))
  6885. i40e_vsi_reinit_locked(pf->vsi[v]);
  6886. }
  6887. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  6888. int v;
  6889. /* Find the VSI(s) that needs to be brought down */
  6890. dev_info(&pf->pdev->dev, "VSI down requested\n");
  6891. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6892. struct i40e_vsi *vsi = pf->vsi[v];
  6893. if (vsi != NULL &&
  6894. test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
  6895. vsi->state)) {
  6896. set_bit(__I40E_VSI_DOWN, vsi->state);
  6897. i40e_down(vsi);
  6898. }
  6899. }
  6900. } else {
  6901. dev_info(&pf->pdev->dev,
  6902. "bad reset request 0x%08x\n", reset_flags);
  6903. }
  6904. }
  6905. #ifdef CONFIG_I40E_DCB
  6906. /**
  6907. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  6908. * @pf: board private structure
  6909. * @old_cfg: current DCB config
  6910. * @new_cfg: new DCB config
  6911. **/
  6912. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  6913. struct i40e_dcbx_config *old_cfg,
  6914. struct i40e_dcbx_config *new_cfg)
  6915. {
  6916. bool need_reconfig = false;
  6917. /* Check if ETS configuration has changed */
  6918. if (memcmp(&new_cfg->etscfg,
  6919. &old_cfg->etscfg,
  6920. sizeof(new_cfg->etscfg))) {
  6921. /* If Priority Table has changed reconfig is needed */
  6922. if (memcmp(&new_cfg->etscfg.prioritytable,
  6923. &old_cfg->etscfg.prioritytable,
  6924. sizeof(new_cfg->etscfg.prioritytable))) {
  6925. need_reconfig = true;
  6926. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  6927. }
  6928. if (memcmp(&new_cfg->etscfg.tcbwtable,
  6929. &old_cfg->etscfg.tcbwtable,
  6930. sizeof(new_cfg->etscfg.tcbwtable)))
  6931. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  6932. if (memcmp(&new_cfg->etscfg.tsatable,
  6933. &old_cfg->etscfg.tsatable,
  6934. sizeof(new_cfg->etscfg.tsatable)))
  6935. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  6936. }
  6937. /* Check if PFC configuration has changed */
  6938. if (memcmp(&new_cfg->pfc,
  6939. &old_cfg->pfc,
  6940. sizeof(new_cfg->pfc))) {
  6941. need_reconfig = true;
  6942. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  6943. }
  6944. /* Check if APP Table has changed */
  6945. if (memcmp(&new_cfg->app,
  6946. &old_cfg->app,
  6947. sizeof(new_cfg->app))) {
  6948. need_reconfig = true;
  6949. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  6950. }
  6951. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  6952. return need_reconfig;
  6953. }
  6954. /**
  6955. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  6956. * @pf: board private structure
  6957. * @e: event info posted on ARQ
  6958. **/
  6959. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  6960. struct i40e_arq_event_info *e)
  6961. {
  6962. struct i40e_aqc_lldp_get_mib *mib =
  6963. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  6964. struct i40e_hw *hw = &pf->hw;
  6965. struct i40e_dcbx_config tmp_dcbx_cfg;
  6966. bool need_reconfig = false;
  6967. int ret = 0;
  6968. u8 type;
  6969. /* Not DCB capable or capability disabled */
  6970. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  6971. return ret;
  6972. /* Ignore if event is not for Nearest Bridge */
  6973. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  6974. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  6975. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  6976. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  6977. return ret;
  6978. /* Check MIB Type and return if event for Remote MIB update */
  6979. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  6980. dev_dbg(&pf->pdev->dev,
  6981. "LLDP event mib type %s\n", type ? "remote" : "local");
  6982. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  6983. /* Update the remote cached instance and return */
  6984. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  6985. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  6986. &hw->remote_dcbx_config);
  6987. goto exit;
  6988. }
  6989. /* Store the old configuration */
  6990. tmp_dcbx_cfg = hw->local_dcbx_config;
  6991. /* Reset the old DCBx configuration data */
  6992. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  6993. /* Get updated DCBX data from firmware */
  6994. ret = i40e_get_dcb_config(&pf->hw);
  6995. if (ret) {
  6996. dev_info(&pf->pdev->dev,
  6997. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  6998. i40e_stat_str(&pf->hw, ret),
  6999. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7000. goto exit;
  7001. }
  7002. /* No change detected in DCBX configs */
  7003. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  7004. sizeof(tmp_dcbx_cfg))) {
  7005. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  7006. goto exit;
  7007. }
  7008. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  7009. &hw->local_dcbx_config);
  7010. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  7011. if (!need_reconfig)
  7012. goto exit;
  7013. /* Enable DCB tagging only when more than one TC */
  7014. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  7015. pf->flags |= I40E_FLAG_DCB_ENABLED;
  7016. else
  7017. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  7018. set_bit(__I40E_PORT_SUSPENDED, pf->state);
  7019. /* Reconfiguration needed quiesce all VSIs */
  7020. i40e_pf_quiesce_all_vsi(pf);
  7021. /* Changes in configuration update VEB/VSI */
  7022. i40e_dcb_reconfigure(pf);
  7023. ret = i40e_resume_port_tx(pf);
  7024. clear_bit(__I40E_PORT_SUSPENDED, pf->state);
  7025. /* In case of error no point in resuming VSIs */
  7026. if (ret)
  7027. goto exit;
  7028. /* Wait for the PF's queues to be disabled */
  7029. ret = i40e_pf_wait_queues_disabled(pf);
  7030. if (ret) {
  7031. /* Schedule PF reset to recover */
  7032. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7033. i40e_service_event_schedule(pf);
  7034. } else {
  7035. i40e_pf_unquiesce_all_vsi(pf);
  7036. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  7037. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  7038. }
  7039. exit:
  7040. return ret;
  7041. }
  7042. #endif /* CONFIG_I40E_DCB */
  7043. /**
  7044. * i40e_do_reset_safe - Protected reset path for userland calls.
  7045. * @pf: board private structure
  7046. * @reset_flags: which reset is requested
  7047. *
  7048. **/
  7049. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  7050. {
  7051. rtnl_lock();
  7052. i40e_do_reset(pf, reset_flags, true);
  7053. rtnl_unlock();
  7054. }
  7055. /**
  7056. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  7057. * @pf: board private structure
  7058. * @e: event info posted on ARQ
  7059. *
  7060. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  7061. * and VF queues
  7062. **/
  7063. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  7064. struct i40e_arq_event_info *e)
  7065. {
  7066. struct i40e_aqc_lan_overflow *data =
  7067. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  7068. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  7069. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  7070. struct i40e_hw *hw = &pf->hw;
  7071. struct i40e_vf *vf;
  7072. u16 vf_id;
  7073. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  7074. queue, qtx_ctl);
  7075. /* Queue belongs to VF, find the VF and issue VF reset */
  7076. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  7077. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  7078. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  7079. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  7080. vf_id -= hw->func_caps.vf_base_id;
  7081. vf = &pf->vf[vf_id];
  7082. i40e_vc_notify_vf_reset(vf);
  7083. /* Allow VF to process pending reset notification */
  7084. msleep(20);
  7085. i40e_reset_vf(vf, false);
  7086. }
  7087. }
  7088. /**
  7089. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  7090. * @pf: board private structure
  7091. **/
  7092. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  7093. {
  7094. u32 val, fcnt_prog;
  7095. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7096. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  7097. return fcnt_prog;
  7098. }
  7099. /**
  7100. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  7101. * @pf: board private structure
  7102. **/
  7103. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  7104. {
  7105. u32 val, fcnt_prog;
  7106. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7107. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  7108. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  7109. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  7110. return fcnt_prog;
  7111. }
  7112. /**
  7113. * i40e_get_global_fd_count - Get total FD filters programmed on device
  7114. * @pf: board private structure
  7115. **/
  7116. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  7117. {
  7118. u32 val, fcnt_prog;
  7119. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  7120. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  7121. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  7122. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  7123. return fcnt_prog;
  7124. }
  7125. /**
  7126. * i40e_reenable_fdir_sb - Restore FDir SB capability
  7127. * @pf: board private structure
  7128. **/
  7129. static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
  7130. {
  7131. if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  7132. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  7133. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7134. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  7135. }
  7136. /**
  7137. * i40e_reenable_fdir_atr - Restore FDir ATR capability
  7138. * @pf: board private structure
  7139. **/
  7140. static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
  7141. {
  7142. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
  7143. /* ATR uses the same filtering logic as SB rules. It only
  7144. * functions properly if the input set mask is at the default
  7145. * settings. It is safe to restore the default input set
  7146. * because there are no active TCPv4 filter rules.
  7147. */
  7148. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  7149. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  7150. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  7151. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7152. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7153. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  7154. }
  7155. }
  7156. /**
  7157. * i40e_delete_invalid_filter - Delete an invalid FDIR filter
  7158. * @pf: board private structure
  7159. * @filter: FDir filter to remove
  7160. */
  7161. static void i40e_delete_invalid_filter(struct i40e_pf *pf,
  7162. struct i40e_fdir_filter *filter)
  7163. {
  7164. /* Update counters */
  7165. pf->fdir_pf_active_filters--;
  7166. pf->fd_inv = 0;
  7167. switch (filter->flow_type) {
  7168. case TCP_V4_FLOW:
  7169. pf->fd_tcp4_filter_cnt--;
  7170. break;
  7171. case UDP_V4_FLOW:
  7172. pf->fd_udp4_filter_cnt--;
  7173. break;
  7174. case SCTP_V4_FLOW:
  7175. pf->fd_sctp4_filter_cnt--;
  7176. break;
  7177. case IP_USER_FLOW:
  7178. switch (filter->ip4_proto) {
  7179. case IPPROTO_TCP:
  7180. pf->fd_tcp4_filter_cnt--;
  7181. break;
  7182. case IPPROTO_UDP:
  7183. pf->fd_udp4_filter_cnt--;
  7184. break;
  7185. case IPPROTO_SCTP:
  7186. pf->fd_sctp4_filter_cnt--;
  7187. break;
  7188. case IPPROTO_IP:
  7189. pf->fd_ip4_filter_cnt--;
  7190. break;
  7191. }
  7192. break;
  7193. }
  7194. /* Remove the filter from the list and free memory */
  7195. hlist_del(&filter->fdir_node);
  7196. kfree(filter);
  7197. }
  7198. /**
  7199. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  7200. * @pf: board private structure
  7201. **/
  7202. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  7203. {
  7204. struct i40e_fdir_filter *filter;
  7205. u32 fcnt_prog, fcnt_avail;
  7206. struct hlist_node *node;
  7207. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7208. return;
  7209. /* Check if we have enough room to re-enable FDir SB capability. */
  7210. fcnt_prog = i40e_get_global_fd_count(pf);
  7211. fcnt_avail = pf->fdir_pf_filter_count;
  7212. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  7213. (pf->fd_add_err == 0) ||
  7214. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
  7215. i40e_reenable_fdir_sb(pf);
  7216. /* We should wait for even more space before re-enabling ATR.
  7217. * Additionally, we cannot enable ATR as long as we still have TCP SB
  7218. * rules active.
  7219. */
  7220. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
  7221. (pf->fd_tcp4_filter_cnt == 0))
  7222. i40e_reenable_fdir_atr(pf);
  7223. /* if hw had a problem adding a filter, delete it */
  7224. if (pf->fd_inv > 0) {
  7225. hlist_for_each_entry_safe(filter, node,
  7226. &pf->fdir_filter_list, fdir_node)
  7227. if (filter->fd_id == pf->fd_inv)
  7228. i40e_delete_invalid_filter(pf, filter);
  7229. }
  7230. }
  7231. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  7232. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  7233. /**
  7234. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  7235. * @pf: board private structure
  7236. **/
  7237. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  7238. {
  7239. unsigned long min_flush_time;
  7240. int flush_wait_retry = 50;
  7241. bool disable_atr = false;
  7242. int fd_room;
  7243. int reg;
  7244. if (!time_after(jiffies, pf->fd_flush_timestamp +
  7245. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  7246. return;
  7247. /* If the flush is happening too quick and we have mostly SB rules we
  7248. * should not re-enable ATR for some time.
  7249. */
  7250. min_flush_time = pf->fd_flush_timestamp +
  7251. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  7252. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  7253. if (!(time_after(jiffies, min_flush_time)) &&
  7254. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  7255. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7256. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  7257. disable_atr = true;
  7258. }
  7259. pf->fd_flush_timestamp = jiffies;
  7260. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7261. /* flush all filters */
  7262. wr32(&pf->hw, I40E_PFQF_CTL_1,
  7263. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  7264. i40e_flush(&pf->hw);
  7265. pf->fd_flush_cnt++;
  7266. pf->fd_add_err = 0;
  7267. do {
  7268. /* Check FD flush status every 5-6msec */
  7269. usleep_range(5000, 6000);
  7270. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  7271. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  7272. break;
  7273. } while (flush_wait_retry--);
  7274. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  7275. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  7276. } else {
  7277. /* replay sideband filters */
  7278. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  7279. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  7280. clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7281. clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  7282. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7283. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  7284. }
  7285. }
  7286. /**
  7287. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  7288. * @pf: board private structure
  7289. **/
  7290. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  7291. {
  7292. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  7293. }
  7294. /* We can see up to 256 filter programming desc in transit if the filters are
  7295. * being applied really fast; before we see the first
  7296. * filter miss error on Rx queue 0. Accumulating enough error messages before
  7297. * reacting will make sure we don't cause flush too often.
  7298. */
  7299. #define I40E_MAX_FD_PROGRAM_ERROR 256
  7300. /**
  7301. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  7302. * @pf: board private structure
  7303. **/
  7304. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  7305. {
  7306. /* if interface is down do nothing */
  7307. if (test_bit(__I40E_DOWN, pf->state))
  7308. return;
  7309. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7310. i40e_fdir_flush_and_replay(pf);
  7311. i40e_fdir_check_and_reenable(pf);
  7312. }
  7313. /**
  7314. * i40e_vsi_link_event - notify VSI of a link event
  7315. * @vsi: vsi to be notified
  7316. * @link_up: link up or down
  7317. **/
  7318. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  7319. {
  7320. if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
  7321. return;
  7322. switch (vsi->type) {
  7323. case I40E_VSI_MAIN:
  7324. if (!vsi->netdev || !vsi->netdev_registered)
  7325. break;
  7326. if (link_up) {
  7327. netif_carrier_on(vsi->netdev);
  7328. netif_tx_wake_all_queues(vsi->netdev);
  7329. } else {
  7330. netif_carrier_off(vsi->netdev);
  7331. netif_tx_stop_all_queues(vsi->netdev);
  7332. }
  7333. break;
  7334. case I40E_VSI_SRIOV:
  7335. case I40E_VSI_VMDQ2:
  7336. case I40E_VSI_CTRL:
  7337. case I40E_VSI_IWARP:
  7338. case I40E_VSI_MIRROR:
  7339. default:
  7340. /* there is no notification for other VSIs */
  7341. break;
  7342. }
  7343. }
  7344. /**
  7345. * i40e_veb_link_event - notify elements on the veb of a link event
  7346. * @veb: veb to be notified
  7347. * @link_up: link up or down
  7348. **/
  7349. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  7350. {
  7351. struct i40e_pf *pf;
  7352. int i;
  7353. if (!veb || !veb->pf)
  7354. return;
  7355. pf = veb->pf;
  7356. /* depth first... */
  7357. for (i = 0; i < I40E_MAX_VEB; i++)
  7358. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  7359. i40e_veb_link_event(pf->veb[i], link_up);
  7360. /* ... now the local VSIs */
  7361. for (i = 0; i < pf->num_alloc_vsi; i++)
  7362. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  7363. i40e_vsi_link_event(pf->vsi[i], link_up);
  7364. }
  7365. /**
  7366. * i40e_link_event - Update netif_carrier status
  7367. * @pf: board private structure
  7368. **/
  7369. static void i40e_link_event(struct i40e_pf *pf)
  7370. {
  7371. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7372. u8 new_link_speed, old_link_speed;
  7373. i40e_status status;
  7374. bool new_link, old_link;
  7375. /* save off old link status information */
  7376. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  7377. /* set this to force the get_link_status call to refresh state */
  7378. pf->hw.phy.get_link_info = true;
  7379. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  7380. status = i40e_get_link_status(&pf->hw, &new_link);
  7381. /* On success, disable temp link polling */
  7382. if (status == I40E_SUCCESS) {
  7383. clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7384. } else {
  7385. /* Enable link polling temporarily until i40e_get_link_status
  7386. * returns I40E_SUCCESS
  7387. */
  7388. set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7389. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  7390. status);
  7391. return;
  7392. }
  7393. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  7394. new_link_speed = pf->hw.phy.link_info.link_speed;
  7395. if (new_link == old_link &&
  7396. new_link_speed == old_link_speed &&
  7397. (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  7398. new_link == netif_carrier_ok(vsi->netdev)))
  7399. return;
  7400. i40e_print_link_message(vsi, new_link);
  7401. /* Notify the base of the switch tree connected to
  7402. * the link. Floating VEBs are not notified.
  7403. */
  7404. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  7405. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  7406. else
  7407. i40e_vsi_link_event(vsi, new_link);
  7408. if (pf->vf)
  7409. i40e_vc_notify_link_state(pf);
  7410. if (pf->flags & I40E_FLAG_PTP)
  7411. i40e_ptp_set_increment(pf);
  7412. }
  7413. /**
  7414. * i40e_watchdog_subtask - periodic checks not using event driven response
  7415. * @pf: board private structure
  7416. **/
  7417. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  7418. {
  7419. int i;
  7420. /* if interface is down do nothing */
  7421. if (test_bit(__I40E_DOWN, pf->state) ||
  7422. test_bit(__I40E_CONFIG_BUSY, pf->state))
  7423. return;
  7424. /* make sure we don't do these things too often */
  7425. if (time_before(jiffies, (pf->service_timer_previous +
  7426. pf->service_timer_period)))
  7427. return;
  7428. pf->service_timer_previous = jiffies;
  7429. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  7430. test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
  7431. i40e_link_event(pf);
  7432. /* Update the stats for active netdevs so the network stack
  7433. * can look at updated numbers whenever it cares to
  7434. */
  7435. for (i = 0; i < pf->num_alloc_vsi; i++)
  7436. if (pf->vsi[i] && pf->vsi[i]->netdev)
  7437. i40e_update_stats(pf->vsi[i]);
  7438. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  7439. /* Update the stats for the active switching components */
  7440. for (i = 0; i < I40E_MAX_VEB; i++)
  7441. if (pf->veb[i])
  7442. i40e_update_veb_stats(pf->veb[i]);
  7443. }
  7444. i40e_ptp_rx_hang(pf);
  7445. i40e_ptp_tx_hang(pf);
  7446. }
  7447. /**
  7448. * i40e_reset_subtask - Set up for resetting the device and driver
  7449. * @pf: board private structure
  7450. **/
  7451. static void i40e_reset_subtask(struct i40e_pf *pf)
  7452. {
  7453. u32 reset_flags = 0;
  7454. if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
  7455. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  7456. clear_bit(__I40E_REINIT_REQUESTED, pf->state);
  7457. }
  7458. if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
  7459. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  7460. clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7461. }
  7462. if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
  7463. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  7464. clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  7465. }
  7466. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
  7467. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  7468. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  7469. }
  7470. if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
  7471. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  7472. clear_bit(__I40E_DOWN_REQUESTED, pf->state);
  7473. }
  7474. /* If there's a recovery already waiting, it takes
  7475. * precedence before starting a new reset sequence.
  7476. */
  7477. if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
  7478. i40e_prep_for_reset(pf, false);
  7479. i40e_reset(pf);
  7480. i40e_rebuild(pf, false, false);
  7481. }
  7482. /* If we're already down or resetting, just bail */
  7483. if (reset_flags &&
  7484. !test_bit(__I40E_DOWN, pf->state) &&
  7485. !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
  7486. i40e_do_reset(pf, reset_flags, false);
  7487. }
  7488. }
  7489. /**
  7490. * i40e_handle_link_event - Handle link event
  7491. * @pf: board private structure
  7492. * @e: event info posted on ARQ
  7493. **/
  7494. static void i40e_handle_link_event(struct i40e_pf *pf,
  7495. struct i40e_arq_event_info *e)
  7496. {
  7497. struct i40e_aqc_get_link_status *status =
  7498. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  7499. /* Do a new status request to re-enable LSE reporting
  7500. * and load new status information into the hw struct
  7501. * This completely ignores any state information
  7502. * in the ARQ event info, instead choosing to always
  7503. * issue the AQ update link status command.
  7504. */
  7505. i40e_link_event(pf);
  7506. /* Check if module meets thermal requirements */
  7507. if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
  7508. dev_err(&pf->pdev->dev,
  7509. "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
  7510. dev_err(&pf->pdev->dev,
  7511. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7512. } else {
  7513. /* check for unqualified module, if link is down, suppress
  7514. * the message if link was forced to be down.
  7515. */
  7516. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  7517. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  7518. (!(status->link_info & I40E_AQ_LINK_UP)) &&
  7519. (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
  7520. dev_err(&pf->pdev->dev,
  7521. "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
  7522. dev_err(&pf->pdev->dev,
  7523. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7524. }
  7525. }
  7526. }
  7527. /**
  7528. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  7529. * @pf: board private structure
  7530. **/
  7531. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  7532. {
  7533. struct i40e_arq_event_info event;
  7534. struct i40e_hw *hw = &pf->hw;
  7535. u16 pending, i = 0;
  7536. i40e_status ret;
  7537. u16 opcode;
  7538. u32 oldval;
  7539. u32 val;
  7540. /* Do not run clean AQ when PF reset fails */
  7541. if (test_bit(__I40E_RESET_FAILED, pf->state))
  7542. return;
  7543. /* check for error indications */
  7544. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  7545. oldval = val;
  7546. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  7547. if (hw->debug_mask & I40E_DEBUG_AQ)
  7548. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  7549. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  7550. }
  7551. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  7552. if (hw->debug_mask & I40E_DEBUG_AQ)
  7553. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  7554. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  7555. pf->arq_overflows++;
  7556. }
  7557. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  7558. if (hw->debug_mask & I40E_DEBUG_AQ)
  7559. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  7560. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  7561. }
  7562. if (oldval != val)
  7563. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  7564. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  7565. oldval = val;
  7566. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  7567. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7568. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  7569. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  7570. }
  7571. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  7572. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7573. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  7574. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  7575. }
  7576. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  7577. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7578. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  7579. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  7580. }
  7581. if (oldval != val)
  7582. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  7583. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  7584. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  7585. if (!event.msg_buf)
  7586. return;
  7587. do {
  7588. ret = i40e_clean_arq_element(hw, &event, &pending);
  7589. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  7590. break;
  7591. else if (ret) {
  7592. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  7593. break;
  7594. }
  7595. opcode = le16_to_cpu(event.desc.opcode);
  7596. switch (opcode) {
  7597. case i40e_aqc_opc_get_link_status:
  7598. i40e_handle_link_event(pf, &event);
  7599. break;
  7600. case i40e_aqc_opc_send_msg_to_pf:
  7601. ret = i40e_vc_process_vf_msg(pf,
  7602. le16_to_cpu(event.desc.retval),
  7603. le32_to_cpu(event.desc.cookie_high),
  7604. le32_to_cpu(event.desc.cookie_low),
  7605. event.msg_buf,
  7606. event.msg_len);
  7607. break;
  7608. case i40e_aqc_opc_lldp_update_mib:
  7609. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  7610. #ifdef CONFIG_I40E_DCB
  7611. rtnl_lock();
  7612. ret = i40e_handle_lldp_event(pf, &event);
  7613. rtnl_unlock();
  7614. #endif /* CONFIG_I40E_DCB */
  7615. break;
  7616. case i40e_aqc_opc_event_lan_overflow:
  7617. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  7618. i40e_handle_lan_overflow_event(pf, &event);
  7619. break;
  7620. case i40e_aqc_opc_send_msg_to_peer:
  7621. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  7622. break;
  7623. case i40e_aqc_opc_nvm_erase:
  7624. case i40e_aqc_opc_nvm_update:
  7625. case i40e_aqc_opc_oem_post_update:
  7626. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  7627. "ARQ NVM operation 0x%04x completed\n",
  7628. opcode);
  7629. break;
  7630. default:
  7631. dev_info(&pf->pdev->dev,
  7632. "ARQ: Unknown event 0x%04x ignored\n",
  7633. opcode);
  7634. break;
  7635. }
  7636. } while (i++ < pf->adminq_work_limit);
  7637. if (i < pf->adminq_work_limit)
  7638. clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  7639. /* re-enable Admin queue interrupt cause */
  7640. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  7641. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  7642. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  7643. i40e_flush(hw);
  7644. kfree(event.msg_buf);
  7645. }
  7646. /**
  7647. * i40e_verify_eeprom - make sure eeprom is good to use
  7648. * @pf: board private structure
  7649. **/
  7650. static void i40e_verify_eeprom(struct i40e_pf *pf)
  7651. {
  7652. int err;
  7653. err = i40e_diag_eeprom_test(&pf->hw);
  7654. if (err) {
  7655. /* retry in case of garbage read */
  7656. err = i40e_diag_eeprom_test(&pf->hw);
  7657. if (err) {
  7658. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  7659. err);
  7660. set_bit(__I40E_BAD_EEPROM, pf->state);
  7661. }
  7662. }
  7663. if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
  7664. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  7665. clear_bit(__I40E_BAD_EEPROM, pf->state);
  7666. }
  7667. }
  7668. /**
  7669. * i40e_enable_pf_switch_lb
  7670. * @pf: pointer to the PF structure
  7671. *
  7672. * enable switch loop back or die - no point in a return value
  7673. **/
  7674. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  7675. {
  7676. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7677. struct i40e_vsi_context ctxt;
  7678. int ret;
  7679. ctxt.seid = pf->main_vsi_seid;
  7680. ctxt.pf_num = pf->hw.pf_id;
  7681. ctxt.vf_num = 0;
  7682. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7683. if (ret) {
  7684. dev_info(&pf->pdev->dev,
  7685. "couldn't get PF vsi config, err %s aq_err %s\n",
  7686. i40e_stat_str(&pf->hw, ret),
  7687. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7688. return;
  7689. }
  7690. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7691. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7692. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7693. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7694. if (ret) {
  7695. dev_info(&pf->pdev->dev,
  7696. "update vsi switch failed, err %s aq_err %s\n",
  7697. i40e_stat_str(&pf->hw, ret),
  7698. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7699. }
  7700. }
  7701. /**
  7702. * i40e_disable_pf_switch_lb
  7703. * @pf: pointer to the PF structure
  7704. *
  7705. * disable switch loop back or die - no point in a return value
  7706. **/
  7707. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  7708. {
  7709. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7710. struct i40e_vsi_context ctxt;
  7711. int ret;
  7712. ctxt.seid = pf->main_vsi_seid;
  7713. ctxt.pf_num = pf->hw.pf_id;
  7714. ctxt.vf_num = 0;
  7715. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7716. if (ret) {
  7717. dev_info(&pf->pdev->dev,
  7718. "couldn't get PF vsi config, err %s aq_err %s\n",
  7719. i40e_stat_str(&pf->hw, ret),
  7720. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7721. return;
  7722. }
  7723. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7724. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7725. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7726. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7727. if (ret) {
  7728. dev_info(&pf->pdev->dev,
  7729. "update vsi switch failed, err %s aq_err %s\n",
  7730. i40e_stat_str(&pf->hw, ret),
  7731. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7732. }
  7733. }
  7734. /**
  7735. * i40e_config_bridge_mode - Configure the HW bridge mode
  7736. * @veb: pointer to the bridge instance
  7737. *
  7738. * Configure the loop back mode for the LAN VSI that is downlink to the
  7739. * specified HW bridge instance. It is expected this function is called
  7740. * when a new HW bridge is instantiated.
  7741. **/
  7742. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  7743. {
  7744. struct i40e_pf *pf = veb->pf;
  7745. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  7746. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  7747. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  7748. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  7749. i40e_disable_pf_switch_lb(pf);
  7750. else
  7751. i40e_enable_pf_switch_lb(pf);
  7752. }
  7753. /**
  7754. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  7755. * @veb: pointer to the VEB instance
  7756. *
  7757. * This is a recursive function that first builds the attached VSIs then
  7758. * recurses in to build the next layer of VEB. We track the connections
  7759. * through our own index numbers because the seid's from the HW could
  7760. * change across the reset.
  7761. **/
  7762. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  7763. {
  7764. struct i40e_vsi *ctl_vsi = NULL;
  7765. struct i40e_pf *pf = veb->pf;
  7766. int v, veb_idx;
  7767. int ret;
  7768. /* build VSI that owns this VEB, temporarily attached to base VEB */
  7769. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  7770. if (pf->vsi[v] &&
  7771. pf->vsi[v]->veb_idx == veb->idx &&
  7772. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  7773. ctl_vsi = pf->vsi[v];
  7774. break;
  7775. }
  7776. }
  7777. if (!ctl_vsi) {
  7778. dev_info(&pf->pdev->dev,
  7779. "missing owner VSI for veb_idx %d\n", veb->idx);
  7780. ret = -ENOENT;
  7781. goto end_reconstitute;
  7782. }
  7783. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  7784. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  7785. ret = i40e_add_vsi(ctl_vsi);
  7786. if (ret) {
  7787. dev_info(&pf->pdev->dev,
  7788. "rebuild of veb_idx %d owner VSI failed: %d\n",
  7789. veb->idx, ret);
  7790. goto end_reconstitute;
  7791. }
  7792. i40e_vsi_reset_stats(ctl_vsi);
  7793. /* create the VEB in the switch and move the VSI onto the VEB */
  7794. ret = i40e_add_veb(veb, ctl_vsi);
  7795. if (ret)
  7796. goto end_reconstitute;
  7797. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  7798. veb->bridge_mode = BRIDGE_MODE_VEB;
  7799. else
  7800. veb->bridge_mode = BRIDGE_MODE_VEPA;
  7801. i40e_config_bridge_mode(veb);
  7802. /* create the remaining VSIs attached to this VEB */
  7803. for (v = 0; v < pf->num_alloc_vsi; v++) {
  7804. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  7805. continue;
  7806. if (pf->vsi[v]->veb_idx == veb->idx) {
  7807. struct i40e_vsi *vsi = pf->vsi[v];
  7808. vsi->uplink_seid = veb->seid;
  7809. ret = i40e_add_vsi(vsi);
  7810. if (ret) {
  7811. dev_info(&pf->pdev->dev,
  7812. "rebuild of vsi_idx %d failed: %d\n",
  7813. v, ret);
  7814. goto end_reconstitute;
  7815. }
  7816. i40e_vsi_reset_stats(vsi);
  7817. }
  7818. }
  7819. /* create any VEBs attached to this VEB - RECURSION */
  7820. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  7821. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  7822. pf->veb[veb_idx]->uplink_seid = veb->seid;
  7823. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  7824. if (ret)
  7825. break;
  7826. }
  7827. }
  7828. end_reconstitute:
  7829. return ret;
  7830. }
  7831. /**
  7832. * i40e_get_capabilities - get info about the HW
  7833. * @pf: the PF struct
  7834. **/
  7835. static int i40e_get_capabilities(struct i40e_pf *pf,
  7836. enum i40e_admin_queue_opc list_type)
  7837. {
  7838. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  7839. u16 data_size;
  7840. int buf_len;
  7841. int err;
  7842. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  7843. do {
  7844. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  7845. if (!cap_buf)
  7846. return -ENOMEM;
  7847. /* this loads the data into the hw struct for us */
  7848. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  7849. &data_size, list_type,
  7850. NULL);
  7851. /* data loaded, buffer no longer needed */
  7852. kfree(cap_buf);
  7853. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  7854. /* retry with a larger buffer */
  7855. buf_len = data_size;
  7856. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  7857. dev_info(&pf->pdev->dev,
  7858. "capability discovery failed, err %s aq_err %s\n",
  7859. i40e_stat_str(&pf->hw, err),
  7860. i40e_aq_str(&pf->hw,
  7861. pf->hw.aq.asq_last_status));
  7862. return -ENODEV;
  7863. }
  7864. } while (err);
  7865. if (pf->hw.debug_mask & I40E_DEBUG_USER) {
  7866. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7867. dev_info(&pf->pdev->dev,
  7868. "pf=%d, num_vfs=%d, msix_pf=%d, msix_vf=%d, fd_g=%d, fd_b=%d, pf_max_q=%d num_vsi=%d\n",
  7869. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  7870. pf->hw.func_caps.num_msix_vectors,
  7871. pf->hw.func_caps.num_msix_vectors_vf,
  7872. pf->hw.func_caps.fd_filters_guaranteed,
  7873. pf->hw.func_caps.fd_filters_best_effort,
  7874. pf->hw.func_caps.num_tx_qp,
  7875. pf->hw.func_caps.num_vsis);
  7876. } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
  7877. dev_info(&pf->pdev->dev,
  7878. "switch_mode=0x%04x, function_valid=0x%08x\n",
  7879. pf->hw.dev_caps.switch_mode,
  7880. pf->hw.dev_caps.valid_functions);
  7881. dev_info(&pf->pdev->dev,
  7882. "SR-IOV=%d, num_vfs for all function=%u\n",
  7883. pf->hw.dev_caps.sr_iov_1_1,
  7884. pf->hw.dev_caps.num_vfs);
  7885. dev_info(&pf->pdev->dev,
  7886. "num_vsis=%u, num_rx:%u, num_tx=%u\n",
  7887. pf->hw.dev_caps.num_vsis,
  7888. pf->hw.dev_caps.num_rx_qp,
  7889. pf->hw.dev_caps.num_tx_qp);
  7890. }
  7891. }
  7892. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7893. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  7894. + pf->hw.func_caps.num_vfs)
  7895. if (pf->hw.revision_id == 0 &&
  7896. pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
  7897. dev_info(&pf->pdev->dev,
  7898. "got num_vsis %d, setting num_vsis to %d\n",
  7899. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  7900. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  7901. }
  7902. }
  7903. return 0;
  7904. }
  7905. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  7906. /**
  7907. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  7908. * @pf: board private structure
  7909. **/
  7910. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  7911. {
  7912. struct i40e_vsi *vsi;
  7913. /* quick workaround for an NVM issue that leaves a critical register
  7914. * uninitialized
  7915. */
  7916. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  7917. static const u32 hkey[] = {
  7918. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  7919. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  7920. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  7921. 0x95b3a76d};
  7922. int i;
  7923. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  7924. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  7925. }
  7926. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7927. return;
  7928. /* find existing VSI and see if it needs configuring */
  7929. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7930. /* create a new VSI if none exists */
  7931. if (!vsi) {
  7932. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  7933. pf->vsi[pf->lan_vsi]->seid, 0);
  7934. if (!vsi) {
  7935. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  7936. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7937. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  7938. return;
  7939. }
  7940. }
  7941. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  7942. }
  7943. /**
  7944. * i40e_fdir_teardown - release the Flow Director resources
  7945. * @pf: board private structure
  7946. **/
  7947. static void i40e_fdir_teardown(struct i40e_pf *pf)
  7948. {
  7949. struct i40e_vsi *vsi;
  7950. i40e_fdir_filter_exit(pf);
  7951. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7952. if (vsi)
  7953. i40e_vsi_release(vsi);
  7954. }
  7955. /**
  7956. * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
  7957. * @vsi: PF main vsi
  7958. * @seid: seid of main or channel VSIs
  7959. *
  7960. * Rebuilds cloud filters associated with main VSI and channel VSIs if they
  7961. * existed before reset
  7962. **/
  7963. static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
  7964. {
  7965. struct i40e_cloud_filter *cfilter;
  7966. struct i40e_pf *pf = vsi->back;
  7967. struct hlist_node *node;
  7968. i40e_status ret;
  7969. /* Add cloud filters back if they exist */
  7970. hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
  7971. cloud_node) {
  7972. if (cfilter->seid != seid)
  7973. continue;
  7974. if (cfilter->dst_port)
  7975. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
  7976. true);
  7977. else
  7978. ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
  7979. if (ret) {
  7980. dev_dbg(&pf->pdev->dev,
  7981. "Failed to rebuild cloud filter, err %s aq_err %s\n",
  7982. i40e_stat_str(&pf->hw, ret),
  7983. i40e_aq_str(&pf->hw,
  7984. pf->hw.aq.asq_last_status));
  7985. return ret;
  7986. }
  7987. }
  7988. return 0;
  7989. }
  7990. /**
  7991. * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
  7992. * @vsi: PF main vsi
  7993. *
  7994. * Rebuilds channel VSIs if they existed before reset
  7995. **/
  7996. static int i40e_rebuild_channels(struct i40e_vsi *vsi)
  7997. {
  7998. struct i40e_channel *ch, *ch_tmp;
  7999. i40e_status ret;
  8000. if (list_empty(&vsi->ch_list))
  8001. return 0;
  8002. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  8003. if (!ch->initialized)
  8004. break;
  8005. /* Proceed with creation of channel (VMDq2) VSI */
  8006. ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
  8007. if (ret) {
  8008. dev_info(&vsi->back->pdev->dev,
  8009. "failed to rebuild channels using uplink_seid %u\n",
  8010. vsi->uplink_seid);
  8011. return ret;
  8012. }
  8013. /* Reconfigure TX queues using QTX_CTL register */
  8014. ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
  8015. if (ret) {
  8016. dev_info(&vsi->back->pdev->dev,
  8017. "failed to configure TX rings for channel %u\n",
  8018. ch->seid);
  8019. return ret;
  8020. }
  8021. /* update 'next_base_queue' */
  8022. vsi->next_base_queue = vsi->next_base_queue +
  8023. ch->num_queue_pairs;
  8024. if (ch->max_tx_rate) {
  8025. u64 credits = ch->max_tx_rate;
  8026. if (i40e_set_bw_limit(vsi, ch->seid,
  8027. ch->max_tx_rate))
  8028. return -EINVAL;
  8029. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8030. dev_dbg(&vsi->back->pdev->dev,
  8031. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8032. ch->max_tx_rate,
  8033. credits,
  8034. ch->seid);
  8035. }
  8036. ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
  8037. if (ret) {
  8038. dev_dbg(&vsi->back->pdev->dev,
  8039. "Failed to rebuild cloud filters for channel VSI %u\n",
  8040. ch->seid);
  8041. return ret;
  8042. }
  8043. }
  8044. return 0;
  8045. }
  8046. /**
  8047. * i40e_prep_for_reset - prep for the core to reset
  8048. * @pf: board private structure
  8049. * @lock_acquired: indicates whether or not the lock has been acquired
  8050. * before this function was called.
  8051. *
  8052. * Close up the VFs and other things in prep for PF Reset.
  8053. **/
  8054. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  8055. {
  8056. struct i40e_hw *hw = &pf->hw;
  8057. i40e_status ret = 0;
  8058. u32 v;
  8059. clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  8060. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8061. return;
  8062. if (i40e_check_asq_alive(&pf->hw))
  8063. i40e_vc_notify_reset(pf);
  8064. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  8065. /* quiesce the VSIs and their queues that are not already DOWN */
  8066. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  8067. if (!lock_acquired)
  8068. rtnl_lock();
  8069. i40e_pf_quiesce_all_vsi(pf);
  8070. if (!lock_acquired)
  8071. rtnl_unlock();
  8072. for (v = 0; v < pf->num_alloc_vsi; v++) {
  8073. if (pf->vsi[v])
  8074. pf->vsi[v]->seid = 0;
  8075. }
  8076. i40e_shutdown_adminq(&pf->hw);
  8077. /* call shutdown HMC */
  8078. if (hw->hmc.hmc_obj) {
  8079. ret = i40e_shutdown_lan_hmc(hw);
  8080. if (ret)
  8081. dev_warn(&pf->pdev->dev,
  8082. "shutdown_lan_hmc failed: %d\n", ret);
  8083. }
  8084. }
  8085. /**
  8086. * i40e_send_version - update firmware with driver version
  8087. * @pf: PF struct
  8088. */
  8089. static void i40e_send_version(struct i40e_pf *pf)
  8090. {
  8091. struct i40e_driver_version dv;
  8092. dv.major_version = DRV_VERSION_MAJOR;
  8093. dv.minor_version = DRV_VERSION_MINOR;
  8094. dv.build_version = DRV_VERSION_BUILD;
  8095. dv.subbuild_version = 0;
  8096. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  8097. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  8098. }
  8099. /**
  8100. * i40e_get_oem_version - get OEM specific version information
  8101. * @hw: pointer to the hardware structure
  8102. **/
  8103. static void i40e_get_oem_version(struct i40e_hw *hw)
  8104. {
  8105. u16 block_offset = 0xffff;
  8106. u16 block_length = 0;
  8107. u16 capabilities = 0;
  8108. u16 gen_snap = 0;
  8109. u16 release = 0;
  8110. #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
  8111. #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
  8112. #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
  8113. #define I40E_NVM_OEM_GEN_OFFSET 0x02
  8114. #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
  8115. #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
  8116. #define I40E_NVM_OEM_LENGTH 3
  8117. /* Check if pointer to OEM version block is valid. */
  8118. i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
  8119. if (block_offset == 0xffff)
  8120. return;
  8121. /* Check if OEM version block has correct length. */
  8122. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
  8123. &block_length);
  8124. if (block_length < I40E_NVM_OEM_LENGTH)
  8125. return;
  8126. /* Check if OEM version format is as expected. */
  8127. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
  8128. &capabilities);
  8129. if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
  8130. return;
  8131. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
  8132. &gen_snap);
  8133. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
  8134. &release);
  8135. hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
  8136. hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
  8137. }
  8138. /**
  8139. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  8140. * @pf: board private structure
  8141. **/
  8142. static int i40e_reset(struct i40e_pf *pf)
  8143. {
  8144. struct i40e_hw *hw = &pf->hw;
  8145. i40e_status ret;
  8146. ret = i40e_pf_reset(hw);
  8147. if (ret) {
  8148. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  8149. set_bit(__I40E_RESET_FAILED, pf->state);
  8150. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8151. } else {
  8152. pf->pfr_count++;
  8153. }
  8154. return ret;
  8155. }
  8156. /**
  8157. * i40e_rebuild - rebuild using a saved config
  8158. * @pf: board private structure
  8159. * @reinit: if the Main VSI needs to re-initialized.
  8160. * @lock_acquired: indicates whether or not the lock has been acquired
  8161. * before this function was called.
  8162. **/
  8163. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  8164. {
  8165. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  8166. struct i40e_hw *hw = &pf->hw;
  8167. u8 set_fc_aq_fail = 0;
  8168. i40e_status ret;
  8169. u32 val;
  8170. int v;
  8171. if (test_bit(__I40E_DOWN, pf->state))
  8172. goto clear_recovery;
  8173. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  8174. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  8175. ret = i40e_init_adminq(&pf->hw);
  8176. if (ret) {
  8177. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  8178. i40e_stat_str(&pf->hw, ret),
  8179. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8180. goto clear_recovery;
  8181. }
  8182. i40e_get_oem_version(&pf->hw);
  8183. if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
  8184. ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
  8185. hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
  8186. /* The following delay is necessary for 4.33 firmware and older
  8187. * to recover after EMP reset. 200 ms should suffice but we
  8188. * put here 300 ms to be sure that FW is ready to operate
  8189. * after reset.
  8190. */
  8191. mdelay(300);
  8192. }
  8193. /* re-verify the eeprom if we just had an EMP reset */
  8194. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
  8195. i40e_verify_eeprom(pf);
  8196. i40e_clear_pxe_mode(hw);
  8197. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  8198. if (ret)
  8199. goto end_core_reset;
  8200. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  8201. hw->func_caps.num_rx_qp, 0, 0);
  8202. if (ret) {
  8203. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  8204. goto end_core_reset;
  8205. }
  8206. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  8207. if (ret) {
  8208. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  8209. goto end_core_reset;
  8210. }
  8211. /* Enable FW to write a default DCB config on link-up */
  8212. i40e_aq_set_dcb_parameters(hw, true, NULL);
  8213. #ifdef CONFIG_I40E_DCB
  8214. ret = i40e_init_pf_dcb(pf);
  8215. if (ret) {
  8216. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  8217. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  8218. /* Continue without DCB enabled */
  8219. }
  8220. #endif /* CONFIG_I40E_DCB */
  8221. /* do basic switch setup */
  8222. if (!lock_acquired)
  8223. rtnl_lock();
  8224. ret = i40e_setup_pf_switch(pf, reinit);
  8225. if (ret)
  8226. goto end_unlock;
  8227. /* The driver only wants link up/down and module qualification
  8228. * reports from firmware. Note the negative logic.
  8229. */
  8230. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  8231. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  8232. I40E_AQ_EVENT_MEDIA_NA |
  8233. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  8234. if (ret)
  8235. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  8236. i40e_stat_str(&pf->hw, ret),
  8237. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8238. /* make sure our flow control settings are restored */
  8239. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  8240. if (ret)
  8241. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  8242. i40e_stat_str(&pf->hw, ret),
  8243. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8244. /* Rebuild the VSIs and VEBs that existed before reset.
  8245. * They are still in our local switch element arrays, so only
  8246. * need to rebuild the switch model in the HW.
  8247. *
  8248. * If there were VEBs but the reconstitution failed, we'll try
  8249. * try to recover minimal use by getting the basic PF VSI working.
  8250. */
  8251. if (vsi->uplink_seid != pf->mac_seid) {
  8252. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  8253. /* find the one VEB connected to the MAC, and find orphans */
  8254. for (v = 0; v < I40E_MAX_VEB; v++) {
  8255. if (!pf->veb[v])
  8256. continue;
  8257. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  8258. pf->veb[v]->uplink_seid == 0) {
  8259. ret = i40e_reconstitute_veb(pf->veb[v]);
  8260. if (!ret)
  8261. continue;
  8262. /* If Main VEB failed, we're in deep doodoo,
  8263. * so give up rebuilding the switch and set up
  8264. * for minimal rebuild of PF VSI.
  8265. * If orphan failed, we'll report the error
  8266. * but try to keep going.
  8267. */
  8268. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  8269. dev_info(&pf->pdev->dev,
  8270. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  8271. ret);
  8272. vsi->uplink_seid = pf->mac_seid;
  8273. break;
  8274. } else if (pf->veb[v]->uplink_seid == 0) {
  8275. dev_info(&pf->pdev->dev,
  8276. "rebuild of orphan VEB failed: %d\n",
  8277. ret);
  8278. }
  8279. }
  8280. }
  8281. }
  8282. if (vsi->uplink_seid == pf->mac_seid) {
  8283. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  8284. /* no VEB, so rebuild only the Main VSI */
  8285. ret = i40e_add_vsi(vsi);
  8286. if (ret) {
  8287. dev_info(&pf->pdev->dev,
  8288. "rebuild of Main VSI failed: %d\n", ret);
  8289. goto end_unlock;
  8290. }
  8291. }
  8292. if (vsi->mqprio_qopt.max_rate[0]) {
  8293. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  8294. u64 credits = 0;
  8295. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  8296. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  8297. if (ret)
  8298. goto end_unlock;
  8299. credits = max_tx_rate;
  8300. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8301. dev_dbg(&vsi->back->pdev->dev,
  8302. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8303. max_tx_rate,
  8304. credits,
  8305. vsi->seid);
  8306. }
  8307. ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
  8308. if (ret)
  8309. goto end_unlock;
  8310. /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
  8311. * for this main VSI if they exist
  8312. */
  8313. ret = i40e_rebuild_channels(vsi);
  8314. if (ret)
  8315. goto end_unlock;
  8316. /* Reconfigure hardware for allowing smaller MSS in the case
  8317. * of TSO, so that we avoid the MDD being fired and causing
  8318. * a reset in the case of small MSS+TSO.
  8319. */
  8320. #define I40E_REG_MSS 0x000E64DC
  8321. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  8322. #define I40E_64BYTE_MSS 0x400000
  8323. val = rd32(hw, I40E_REG_MSS);
  8324. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  8325. val &= ~I40E_REG_MSS_MIN_MASK;
  8326. val |= I40E_64BYTE_MSS;
  8327. wr32(hw, I40E_REG_MSS, val);
  8328. }
  8329. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  8330. msleep(75);
  8331. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  8332. if (ret)
  8333. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  8334. i40e_stat_str(&pf->hw, ret),
  8335. i40e_aq_str(&pf->hw,
  8336. pf->hw.aq.asq_last_status));
  8337. }
  8338. /* reinit the misc interrupt */
  8339. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8340. ret = i40e_setup_misc_vector(pf);
  8341. /* Add a filter to drop all Flow control frames from any VSI from being
  8342. * transmitted. By doing so we stop a malicious VF from sending out
  8343. * PAUSE or PFC frames and potentially controlling traffic for other
  8344. * PF/VF VSIs.
  8345. * The FW can still send Flow control frames if enabled.
  8346. */
  8347. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  8348. pf->main_vsi_seid);
  8349. /* restart the VSIs that were rebuilt and running before the reset */
  8350. i40e_pf_unquiesce_all_vsi(pf);
  8351. /* Release the RTNL lock before we start resetting VFs */
  8352. if (!lock_acquired)
  8353. rtnl_unlock();
  8354. /* Restore promiscuous settings */
  8355. ret = i40e_set_promiscuous(pf, pf->cur_promisc);
  8356. if (ret)
  8357. dev_warn(&pf->pdev->dev,
  8358. "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
  8359. pf->cur_promisc ? "on" : "off",
  8360. i40e_stat_str(&pf->hw, ret),
  8361. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8362. i40e_reset_all_vfs(pf, true);
  8363. /* tell the firmware that we're starting */
  8364. i40e_send_version(pf);
  8365. /* We've already released the lock, so don't do it again */
  8366. goto end_core_reset;
  8367. end_unlock:
  8368. if (!lock_acquired)
  8369. rtnl_unlock();
  8370. end_core_reset:
  8371. clear_bit(__I40E_RESET_FAILED, pf->state);
  8372. clear_recovery:
  8373. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8374. }
  8375. /**
  8376. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  8377. * @pf: board private structure
  8378. * @reinit: if the Main VSI needs to re-initialized.
  8379. * @lock_acquired: indicates whether or not the lock has been acquired
  8380. * before this function was called.
  8381. **/
  8382. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  8383. bool lock_acquired)
  8384. {
  8385. int ret;
  8386. /* Now we wait for GRST to settle out.
  8387. * We don't have to delete the VEBs or VSIs from the hw switch
  8388. * because the reset will make them disappear.
  8389. */
  8390. ret = i40e_reset(pf);
  8391. if (!ret)
  8392. i40e_rebuild(pf, reinit, lock_acquired);
  8393. }
  8394. /**
  8395. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  8396. * @pf: board private structure
  8397. *
  8398. * Close up the VFs and other things in prep for a Core Reset,
  8399. * then get ready to rebuild the world.
  8400. * @lock_acquired: indicates whether or not the lock has been acquired
  8401. * before this function was called.
  8402. **/
  8403. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  8404. {
  8405. i40e_prep_for_reset(pf, lock_acquired);
  8406. i40e_reset_and_rebuild(pf, false, lock_acquired);
  8407. }
  8408. /**
  8409. * i40e_handle_mdd_event
  8410. * @pf: pointer to the PF structure
  8411. *
  8412. * Called from the MDD irq handler to identify possibly malicious vfs
  8413. **/
  8414. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  8415. {
  8416. struct i40e_hw *hw = &pf->hw;
  8417. bool mdd_detected = false;
  8418. bool pf_mdd_detected = false;
  8419. struct i40e_vf *vf;
  8420. u32 reg;
  8421. int i;
  8422. if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
  8423. return;
  8424. /* find what triggered the MDD event */
  8425. reg = rd32(hw, I40E_GL_MDET_TX);
  8426. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  8427. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  8428. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  8429. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  8430. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  8431. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  8432. I40E_GL_MDET_TX_EVENT_SHIFT;
  8433. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  8434. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  8435. pf->hw.func_caps.base_queue;
  8436. if (netif_msg_tx_err(pf))
  8437. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  8438. event, queue, pf_num, vf_num);
  8439. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  8440. mdd_detected = true;
  8441. }
  8442. reg = rd32(hw, I40E_GL_MDET_RX);
  8443. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  8444. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  8445. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  8446. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  8447. I40E_GL_MDET_RX_EVENT_SHIFT;
  8448. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  8449. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  8450. pf->hw.func_caps.base_queue;
  8451. if (netif_msg_rx_err(pf))
  8452. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  8453. event, queue, func);
  8454. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  8455. mdd_detected = true;
  8456. }
  8457. if (mdd_detected) {
  8458. reg = rd32(hw, I40E_PF_MDET_TX);
  8459. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  8460. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  8461. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  8462. pf_mdd_detected = true;
  8463. }
  8464. reg = rd32(hw, I40E_PF_MDET_RX);
  8465. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  8466. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  8467. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  8468. pf_mdd_detected = true;
  8469. }
  8470. /* Queue belongs to the PF, initiate a reset */
  8471. if (pf_mdd_detected) {
  8472. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  8473. i40e_service_event_schedule(pf);
  8474. }
  8475. }
  8476. /* see if one of the VFs needs its hand slapped */
  8477. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  8478. vf = &(pf->vf[i]);
  8479. reg = rd32(hw, I40E_VP_MDET_TX(i));
  8480. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  8481. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  8482. vf->num_mdd_events++;
  8483. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  8484. i);
  8485. }
  8486. reg = rd32(hw, I40E_VP_MDET_RX(i));
  8487. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  8488. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  8489. vf->num_mdd_events++;
  8490. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  8491. i);
  8492. }
  8493. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  8494. dev_info(&pf->pdev->dev,
  8495. "Too many MDD events on VF %d, disabled\n", i);
  8496. dev_info(&pf->pdev->dev,
  8497. "Use PF Control I/F to re-enable the VF\n");
  8498. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  8499. }
  8500. }
  8501. /* re-enable mdd interrupt cause */
  8502. clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  8503. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  8504. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  8505. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  8506. i40e_flush(hw);
  8507. }
  8508. static const char *i40e_tunnel_name(u8 type)
  8509. {
  8510. switch (type) {
  8511. case UDP_TUNNEL_TYPE_VXLAN:
  8512. return "vxlan";
  8513. case UDP_TUNNEL_TYPE_GENEVE:
  8514. return "geneve";
  8515. default:
  8516. return "unknown";
  8517. }
  8518. }
  8519. /**
  8520. * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
  8521. * @pf: board private structure
  8522. **/
  8523. static void i40e_sync_udp_filters(struct i40e_pf *pf)
  8524. {
  8525. int i;
  8526. /* loop through and set pending bit for all active UDP filters */
  8527. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8528. if (pf->udp_ports[i].port)
  8529. pf->pending_udp_bitmap |= BIT_ULL(i);
  8530. }
  8531. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  8532. }
  8533. /**
  8534. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  8535. * @pf: board private structure
  8536. **/
  8537. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  8538. {
  8539. struct i40e_hw *hw = &pf->hw;
  8540. u8 filter_index, type;
  8541. u16 port;
  8542. int i;
  8543. if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
  8544. return;
  8545. /* acquire RTNL to maintain state of flags and port requests */
  8546. rtnl_lock();
  8547. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8548. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  8549. struct i40e_udp_port_config *udp_port;
  8550. i40e_status ret = 0;
  8551. udp_port = &pf->udp_ports[i];
  8552. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8553. port = READ_ONCE(udp_port->port);
  8554. type = READ_ONCE(udp_port->type);
  8555. filter_index = READ_ONCE(udp_port->filter_index);
  8556. /* release RTNL while we wait on AQ command */
  8557. rtnl_unlock();
  8558. if (port)
  8559. ret = i40e_aq_add_udp_tunnel(hw, port,
  8560. type,
  8561. &filter_index,
  8562. NULL);
  8563. else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
  8564. ret = i40e_aq_del_udp_tunnel(hw, filter_index,
  8565. NULL);
  8566. /* reacquire RTNL so we can update filter_index */
  8567. rtnl_lock();
  8568. if (ret) {
  8569. dev_info(&pf->pdev->dev,
  8570. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  8571. i40e_tunnel_name(type),
  8572. port ? "add" : "delete",
  8573. port,
  8574. filter_index,
  8575. i40e_stat_str(&pf->hw, ret),
  8576. i40e_aq_str(&pf->hw,
  8577. pf->hw.aq.asq_last_status));
  8578. if (port) {
  8579. /* failed to add, just reset port,
  8580. * drop pending bit for any deletion
  8581. */
  8582. udp_port->port = 0;
  8583. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8584. }
  8585. } else if (port) {
  8586. /* record filter index on success */
  8587. udp_port->filter_index = filter_index;
  8588. }
  8589. }
  8590. }
  8591. rtnl_unlock();
  8592. }
  8593. /**
  8594. * i40e_service_task - Run the driver's async subtasks
  8595. * @work: pointer to work_struct containing our data
  8596. **/
  8597. static void i40e_service_task(struct work_struct *work)
  8598. {
  8599. struct i40e_pf *pf = container_of(work,
  8600. struct i40e_pf,
  8601. service_task);
  8602. unsigned long start_time = jiffies;
  8603. /* don't bother with service tasks if a reset is in progress */
  8604. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8605. return;
  8606. if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
  8607. return;
  8608. i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
  8609. i40e_sync_filters_subtask(pf);
  8610. i40e_reset_subtask(pf);
  8611. i40e_handle_mdd_event(pf);
  8612. i40e_vc_process_vflr_event(pf);
  8613. i40e_watchdog_subtask(pf);
  8614. i40e_fdir_reinit_subtask(pf);
  8615. if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
  8616. /* Client subtask will reopen next time through. */
  8617. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  8618. } else {
  8619. i40e_client_subtask(pf);
  8620. if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
  8621. pf->state))
  8622. i40e_notify_client_of_l2_param_changes(
  8623. pf->vsi[pf->lan_vsi]);
  8624. }
  8625. i40e_sync_filters_subtask(pf);
  8626. i40e_sync_udp_filters_subtask(pf);
  8627. i40e_clean_adminq_subtask(pf);
  8628. /* flush memory to make sure state is correct before next watchdog */
  8629. smp_mb__before_atomic();
  8630. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  8631. /* If the tasks have taken longer than one timer cycle or there
  8632. * is more work to be done, reschedule the service task now
  8633. * rather than wait for the timer to tick again.
  8634. */
  8635. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  8636. test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
  8637. test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
  8638. test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  8639. i40e_service_event_schedule(pf);
  8640. }
  8641. /**
  8642. * i40e_service_timer - timer callback
  8643. * @data: pointer to PF struct
  8644. **/
  8645. static void i40e_service_timer(struct timer_list *t)
  8646. {
  8647. struct i40e_pf *pf = from_timer(pf, t, service_timer);
  8648. mod_timer(&pf->service_timer,
  8649. round_jiffies(jiffies + pf->service_timer_period));
  8650. i40e_service_event_schedule(pf);
  8651. }
  8652. /**
  8653. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  8654. * @vsi: the VSI being configured
  8655. **/
  8656. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  8657. {
  8658. struct i40e_pf *pf = vsi->back;
  8659. switch (vsi->type) {
  8660. case I40E_VSI_MAIN:
  8661. vsi->alloc_queue_pairs = pf->num_lan_qps;
  8662. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8663. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8664. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8665. vsi->num_q_vectors = pf->num_lan_msix;
  8666. else
  8667. vsi->num_q_vectors = 1;
  8668. break;
  8669. case I40E_VSI_FDIR:
  8670. vsi->alloc_queue_pairs = 1;
  8671. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  8672. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8673. vsi->num_q_vectors = pf->num_fdsb_msix;
  8674. break;
  8675. case I40E_VSI_VMDQ2:
  8676. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  8677. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8678. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8679. vsi->num_q_vectors = pf->num_vmdq_msix;
  8680. break;
  8681. case I40E_VSI_SRIOV:
  8682. vsi->alloc_queue_pairs = pf->num_vf_qps;
  8683. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8684. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8685. break;
  8686. default:
  8687. WARN_ON(1);
  8688. return -ENODATA;
  8689. }
  8690. return 0;
  8691. }
  8692. /**
  8693. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  8694. * @vsi: VSI pointer
  8695. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  8696. *
  8697. * On error: returns error code (negative)
  8698. * On success: returns 0
  8699. **/
  8700. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  8701. {
  8702. struct i40e_ring **next_rings;
  8703. int size;
  8704. int ret = 0;
  8705. /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
  8706. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
  8707. (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
  8708. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  8709. if (!vsi->tx_rings)
  8710. return -ENOMEM;
  8711. next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
  8712. if (i40e_enabled_xdp_vsi(vsi)) {
  8713. vsi->xdp_rings = next_rings;
  8714. next_rings += vsi->alloc_queue_pairs;
  8715. }
  8716. vsi->rx_rings = next_rings;
  8717. if (alloc_qvectors) {
  8718. /* allocate memory for q_vector pointers */
  8719. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  8720. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  8721. if (!vsi->q_vectors) {
  8722. ret = -ENOMEM;
  8723. goto err_vectors;
  8724. }
  8725. }
  8726. return ret;
  8727. err_vectors:
  8728. kfree(vsi->tx_rings);
  8729. return ret;
  8730. }
  8731. /**
  8732. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  8733. * @pf: board private structure
  8734. * @type: type of VSI
  8735. *
  8736. * On error: returns error code (negative)
  8737. * On success: returns vsi index in PF (positive)
  8738. **/
  8739. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  8740. {
  8741. int ret = -ENODEV;
  8742. struct i40e_vsi *vsi;
  8743. int vsi_idx;
  8744. int i;
  8745. /* Need to protect the allocation of the VSIs at the PF level */
  8746. mutex_lock(&pf->switch_mutex);
  8747. /* VSI list may be fragmented if VSI creation/destruction has
  8748. * been happening. We can afford to do a quick scan to look
  8749. * for any free VSIs in the list.
  8750. *
  8751. * find next empty vsi slot, looping back around if necessary
  8752. */
  8753. i = pf->next_vsi;
  8754. while (i < pf->num_alloc_vsi && pf->vsi[i])
  8755. i++;
  8756. if (i >= pf->num_alloc_vsi) {
  8757. i = 0;
  8758. while (i < pf->next_vsi && pf->vsi[i])
  8759. i++;
  8760. }
  8761. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  8762. vsi_idx = i; /* Found one! */
  8763. } else {
  8764. ret = -ENODEV;
  8765. goto unlock_pf; /* out of VSI slots! */
  8766. }
  8767. pf->next_vsi = ++i;
  8768. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  8769. if (!vsi) {
  8770. ret = -ENOMEM;
  8771. goto unlock_pf;
  8772. }
  8773. vsi->type = type;
  8774. vsi->back = pf;
  8775. set_bit(__I40E_VSI_DOWN, vsi->state);
  8776. vsi->flags = 0;
  8777. vsi->idx = vsi_idx;
  8778. vsi->int_rate_limit = 0;
  8779. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  8780. pf->rss_table_size : 64;
  8781. vsi->netdev_registered = false;
  8782. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  8783. hash_init(vsi->mac_filter_hash);
  8784. vsi->irqs_ready = false;
  8785. ret = i40e_set_num_rings_in_vsi(vsi);
  8786. if (ret)
  8787. goto err_rings;
  8788. ret = i40e_vsi_alloc_arrays(vsi, true);
  8789. if (ret)
  8790. goto err_rings;
  8791. /* Setup default MSIX irq handler for VSI */
  8792. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  8793. /* Initialize VSI lock */
  8794. spin_lock_init(&vsi->mac_filter_hash_lock);
  8795. pf->vsi[vsi_idx] = vsi;
  8796. ret = vsi_idx;
  8797. goto unlock_pf;
  8798. err_rings:
  8799. pf->next_vsi = i - 1;
  8800. kfree(vsi);
  8801. unlock_pf:
  8802. mutex_unlock(&pf->switch_mutex);
  8803. return ret;
  8804. }
  8805. /**
  8806. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  8807. * @vsi: VSI pointer
  8808. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  8809. *
  8810. * On error: returns error code (negative)
  8811. * On success: returns 0
  8812. **/
  8813. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  8814. {
  8815. /* free the ring and vector containers */
  8816. if (free_qvectors) {
  8817. kfree(vsi->q_vectors);
  8818. vsi->q_vectors = NULL;
  8819. }
  8820. kfree(vsi->tx_rings);
  8821. vsi->tx_rings = NULL;
  8822. vsi->rx_rings = NULL;
  8823. vsi->xdp_rings = NULL;
  8824. }
  8825. /**
  8826. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  8827. * and lookup table
  8828. * @vsi: Pointer to VSI structure
  8829. */
  8830. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  8831. {
  8832. if (!vsi)
  8833. return;
  8834. kfree(vsi->rss_hkey_user);
  8835. vsi->rss_hkey_user = NULL;
  8836. kfree(vsi->rss_lut_user);
  8837. vsi->rss_lut_user = NULL;
  8838. }
  8839. /**
  8840. * i40e_vsi_clear - Deallocate the VSI provided
  8841. * @vsi: the VSI being un-configured
  8842. **/
  8843. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  8844. {
  8845. struct i40e_pf *pf;
  8846. if (!vsi)
  8847. return 0;
  8848. if (!vsi->back)
  8849. goto free_vsi;
  8850. pf = vsi->back;
  8851. mutex_lock(&pf->switch_mutex);
  8852. if (!pf->vsi[vsi->idx]) {
  8853. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
  8854. vsi->idx, vsi->idx, vsi->type);
  8855. goto unlock_vsi;
  8856. }
  8857. if (pf->vsi[vsi->idx] != vsi) {
  8858. dev_err(&pf->pdev->dev,
  8859. "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
  8860. pf->vsi[vsi->idx]->idx,
  8861. pf->vsi[vsi->idx]->type,
  8862. vsi->idx, vsi->type);
  8863. goto unlock_vsi;
  8864. }
  8865. /* updates the PF for this cleared vsi */
  8866. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8867. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  8868. i40e_vsi_free_arrays(vsi, true);
  8869. i40e_clear_rss_config_user(vsi);
  8870. pf->vsi[vsi->idx] = NULL;
  8871. if (vsi->idx < pf->next_vsi)
  8872. pf->next_vsi = vsi->idx;
  8873. unlock_vsi:
  8874. mutex_unlock(&pf->switch_mutex);
  8875. free_vsi:
  8876. kfree(vsi);
  8877. return 0;
  8878. }
  8879. /**
  8880. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  8881. * @vsi: the VSI being cleaned
  8882. **/
  8883. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  8884. {
  8885. int i;
  8886. if (vsi->tx_rings && vsi->tx_rings[0]) {
  8887. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8888. kfree_rcu(vsi->tx_rings[i], rcu);
  8889. vsi->tx_rings[i] = NULL;
  8890. vsi->rx_rings[i] = NULL;
  8891. if (vsi->xdp_rings)
  8892. vsi->xdp_rings[i] = NULL;
  8893. }
  8894. }
  8895. }
  8896. /**
  8897. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  8898. * @vsi: the VSI being configured
  8899. **/
  8900. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  8901. {
  8902. int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
  8903. struct i40e_pf *pf = vsi->back;
  8904. struct i40e_ring *ring;
  8905. /* Set basic values in the rings to be used later during open() */
  8906. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8907. /* allocate space for both Tx and Rx in one shot */
  8908. ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
  8909. if (!ring)
  8910. goto err_out;
  8911. ring->queue_index = i;
  8912. ring->reg_idx = vsi->base_queue + i;
  8913. ring->ring_active = false;
  8914. ring->vsi = vsi;
  8915. ring->netdev = vsi->netdev;
  8916. ring->dev = &pf->pdev->dev;
  8917. ring->count = vsi->num_desc;
  8918. ring->size = 0;
  8919. ring->dcb_tc = 0;
  8920. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8921. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8922. ring->itr_setting = pf->tx_itr_default;
  8923. vsi->tx_rings[i] = ring++;
  8924. if (!i40e_enabled_xdp_vsi(vsi))
  8925. goto setup_rx;
  8926. ring->queue_index = vsi->alloc_queue_pairs + i;
  8927. ring->reg_idx = vsi->base_queue + ring->queue_index;
  8928. ring->ring_active = false;
  8929. ring->vsi = vsi;
  8930. ring->netdev = NULL;
  8931. ring->dev = &pf->pdev->dev;
  8932. ring->count = vsi->num_desc;
  8933. ring->size = 0;
  8934. ring->dcb_tc = 0;
  8935. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8936. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8937. set_ring_xdp(ring);
  8938. ring->itr_setting = pf->tx_itr_default;
  8939. vsi->xdp_rings[i] = ring++;
  8940. setup_rx:
  8941. ring->queue_index = i;
  8942. ring->reg_idx = vsi->base_queue + i;
  8943. ring->ring_active = false;
  8944. ring->vsi = vsi;
  8945. ring->netdev = vsi->netdev;
  8946. ring->dev = &pf->pdev->dev;
  8947. ring->count = vsi->num_desc;
  8948. ring->size = 0;
  8949. ring->dcb_tc = 0;
  8950. ring->itr_setting = pf->rx_itr_default;
  8951. vsi->rx_rings[i] = ring;
  8952. }
  8953. return 0;
  8954. err_out:
  8955. i40e_vsi_clear_rings(vsi);
  8956. return -ENOMEM;
  8957. }
  8958. /**
  8959. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  8960. * @pf: board private structure
  8961. * @vectors: the number of MSI-X vectors to request
  8962. *
  8963. * Returns the number of vectors reserved, or error
  8964. **/
  8965. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  8966. {
  8967. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  8968. I40E_MIN_MSIX, vectors);
  8969. if (vectors < 0) {
  8970. dev_info(&pf->pdev->dev,
  8971. "MSI-X vector reservation failed: %d\n", vectors);
  8972. vectors = 0;
  8973. }
  8974. return vectors;
  8975. }
  8976. /**
  8977. * i40e_init_msix - Setup the MSIX capability
  8978. * @pf: board private structure
  8979. *
  8980. * Work with the OS to set up the MSIX vectors needed.
  8981. *
  8982. * Returns the number of vectors reserved or negative on failure
  8983. **/
  8984. static int i40e_init_msix(struct i40e_pf *pf)
  8985. {
  8986. struct i40e_hw *hw = &pf->hw;
  8987. int cpus, extra_vectors;
  8988. int vectors_left;
  8989. int v_budget, i;
  8990. int v_actual;
  8991. int iwarp_requested = 0;
  8992. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8993. return -ENODEV;
  8994. /* The number of vectors we'll request will be comprised of:
  8995. * - Add 1 for "other" cause for Admin Queue events, etc.
  8996. * - The number of LAN queue pairs
  8997. * - Queues being used for RSS.
  8998. * We don't need as many as max_rss_size vectors.
  8999. * use rss_size instead in the calculation since that
  9000. * is governed by number of cpus in the system.
  9001. * - assumes symmetric Tx/Rx pairing
  9002. * - The number of VMDq pairs
  9003. * - The CPU count within the NUMA node if iWARP is enabled
  9004. * Once we count this up, try the request.
  9005. *
  9006. * If we can't get what we want, we'll simplify to nearly nothing
  9007. * and try again. If that still fails, we punt.
  9008. */
  9009. vectors_left = hw->func_caps.num_msix_vectors;
  9010. v_budget = 0;
  9011. /* reserve one vector for miscellaneous handler */
  9012. if (vectors_left) {
  9013. v_budget++;
  9014. vectors_left--;
  9015. }
  9016. /* reserve some vectors for the main PF traffic queues. Initially we
  9017. * only reserve at most 50% of the available vectors, in the case that
  9018. * the number of online CPUs is large. This ensures that we can enable
  9019. * extra features as well. Once we've enabled the other features, we
  9020. * will use any remaining vectors to reach as close as we can to the
  9021. * number of online CPUs.
  9022. */
  9023. cpus = num_online_cpus();
  9024. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  9025. vectors_left -= pf->num_lan_msix;
  9026. /* reserve one vector for sideband flow director */
  9027. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9028. if (vectors_left) {
  9029. pf->num_fdsb_msix = 1;
  9030. v_budget++;
  9031. vectors_left--;
  9032. } else {
  9033. pf->num_fdsb_msix = 0;
  9034. }
  9035. }
  9036. /* can we reserve enough for iWARP? */
  9037. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9038. iwarp_requested = pf->num_iwarp_msix;
  9039. if (!vectors_left)
  9040. pf->num_iwarp_msix = 0;
  9041. else if (vectors_left < pf->num_iwarp_msix)
  9042. pf->num_iwarp_msix = 1;
  9043. v_budget += pf->num_iwarp_msix;
  9044. vectors_left -= pf->num_iwarp_msix;
  9045. }
  9046. /* any vectors left over go for VMDq support */
  9047. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  9048. if (!vectors_left) {
  9049. pf->num_vmdq_msix = 0;
  9050. pf->num_vmdq_qps = 0;
  9051. } else {
  9052. int vmdq_vecs_wanted =
  9053. pf->num_vmdq_vsis * pf->num_vmdq_qps;
  9054. int vmdq_vecs =
  9055. min_t(int, vectors_left, vmdq_vecs_wanted);
  9056. /* if we're short on vectors for what's desired, we limit
  9057. * the queues per vmdq. If this is still more than are
  9058. * available, the user will need to change the number of
  9059. * queues/vectors used by the PF later with the ethtool
  9060. * channels command
  9061. */
  9062. if (vectors_left < vmdq_vecs_wanted) {
  9063. pf->num_vmdq_qps = 1;
  9064. vmdq_vecs_wanted = pf->num_vmdq_vsis;
  9065. vmdq_vecs = min_t(int,
  9066. vectors_left,
  9067. vmdq_vecs_wanted);
  9068. }
  9069. pf->num_vmdq_msix = pf->num_vmdq_qps;
  9070. v_budget += vmdq_vecs;
  9071. vectors_left -= vmdq_vecs;
  9072. }
  9073. }
  9074. /* On systems with a large number of SMP cores, we previously limited
  9075. * the number of vectors for num_lan_msix to be at most 50% of the
  9076. * available vectors, to allow for other features. Now, we add back
  9077. * the remaining vectors. However, we ensure that the total
  9078. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  9079. * calculate the number of vectors we can add without going over the
  9080. * cap of CPUs. For systems with a small number of CPUs this will be
  9081. * zero.
  9082. */
  9083. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  9084. pf->num_lan_msix += extra_vectors;
  9085. vectors_left -= extra_vectors;
  9086. WARN(vectors_left < 0,
  9087. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  9088. v_budget += pf->num_lan_msix;
  9089. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  9090. GFP_KERNEL);
  9091. if (!pf->msix_entries)
  9092. return -ENOMEM;
  9093. for (i = 0; i < v_budget; i++)
  9094. pf->msix_entries[i].entry = i;
  9095. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  9096. if (v_actual < I40E_MIN_MSIX) {
  9097. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  9098. kfree(pf->msix_entries);
  9099. pf->msix_entries = NULL;
  9100. pci_disable_msix(pf->pdev);
  9101. return -ENODEV;
  9102. } else if (v_actual == I40E_MIN_MSIX) {
  9103. /* Adjust for minimal MSIX use */
  9104. pf->num_vmdq_vsis = 0;
  9105. pf->num_vmdq_qps = 0;
  9106. pf->num_lan_qps = 1;
  9107. pf->num_lan_msix = 1;
  9108. } else if (v_actual != v_budget) {
  9109. /* If we have limited resources, we will start with no vectors
  9110. * for the special features and then allocate vectors to some
  9111. * of these features based on the policy and at the end disable
  9112. * the features that did not get any vectors.
  9113. */
  9114. int vec;
  9115. dev_info(&pf->pdev->dev,
  9116. "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
  9117. v_actual, v_budget);
  9118. /* reserve the misc vector */
  9119. vec = v_actual - 1;
  9120. /* Scale vector usage down */
  9121. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  9122. pf->num_vmdq_vsis = 1;
  9123. pf->num_vmdq_qps = 1;
  9124. /* partition out the remaining vectors */
  9125. switch (vec) {
  9126. case 2:
  9127. pf->num_lan_msix = 1;
  9128. break;
  9129. case 3:
  9130. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9131. pf->num_lan_msix = 1;
  9132. pf->num_iwarp_msix = 1;
  9133. } else {
  9134. pf->num_lan_msix = 2;
  9135. }
  9136. break;
  9137. default:
  9138. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9139. pf->num_iwarp_msix = min_t(int, (vec / 3),
  9140. iwarp_requested);
  9141. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  9142. I40E_DEFAULT_NUM_VMDQ_VSI);
  9143. } else {
  9144. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  9145. I40E_DEFAULT_NUM_VMDQ_VSI);
  9146. }
  9147. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9148. pf->num_fdsb_msix = 1;
  9149. vec--;
  9150. }
  9151. pf->num_lan_msix = min_t(int,
  9152. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  9153. pf->num_lan_msix);
  9154. pf->num_lan_qps = pf->num_lan_msix;
  9155. break;
  9156. }
  9157. }
  9158. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  9159. (pf->num_fdsb_msix == 0)) {
  9160. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  9161. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9162. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9163. }
  9164. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9165. (pf->num_vmdq_msix == 0)) {
  9166. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  9167. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  9168. }
  9169. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  9170. (pf->num_iwarp_msix == 0)) {
  9171. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  9172. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9173. }
  9174. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  9175. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  9176. pf->num_lan_msix,
  9177. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  9178. pf->num_fdsb_msix,
  9179. pf->num_iwarp_msix);
  9180. return v_actual;
  9181. }
  9182. /**
  9183. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  9184. * @vsi: the VSI being configured
  9185. * @v_idx: index of the vector in the vsi struct
  9186. * @cpu: cpu to be used on affinity_mask
  9187. *
  9188. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  9189. **/
  9190. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  9191. {
  9192. struct i40e_q_vector *q_vector;
  9193. /* allocate q_vector */
  9194. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  9195. if (!q_vector)
  9196. return -ENOMEM;
  9197. q_vector->vsi = vsi;
  9198. q_vector->v_idx = v_idx;
  9199. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  9200. if (vsi->netdev)
  9201. netif_napi_add(vsi->netdev, &q_vector->napi,
  9202. i40e_napi_poll, NAPI_POLL_WEIGHT);
  9203. /* tie q_vector and vsi together */
  9204. vsi->q_vectors[v_idx] = q_vector;
  9205. return 0;
  9206. }
  9207. /**
  9208. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  9209. * @vsi: the VSI being configured
  9210. *
  9211. * We allocate one q_vector per queue interrupt. If allocation fails we
  9212. * return -ENOMEM.
  9213. **/
  9214. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  9215. {
  9216. struct i40e_pf *pf = vsi->back;
  9217. int err, v_idx, num_q_vectors, current_cpu;
  9218. /* if not MSIX, give the one vector only to the LAN VSI */
  9219. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  9220. num_q_vectors = vsi->num_q_vectors;
  9221. else if (vsi == pf->vsi[pf->lan_vsi])
  9222. num_q_vectors = 1;
  9223. else
  9224. return -EINVAL;
  9225. current_cpu = cpumask_first(cpu_online_mask);
  9226. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  9227. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  9228. if (err)
  9229. goto err_out;
  9230. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  9231. if (unlikely(current_cpu >= nr_cpu_ids))
  9232. current_cpu = cpumask_first(cpu_online_mask);
  9233. }
  9234. return 0;
  9235. err_out:
  9236. while (v_idx--)
  9237. i40e_free_q_vector(vsi, v_idx);
  9238. return err;
  9239. }
  9240. /**
  9241. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  9242. * @pf: board private structure to initialize
  9243. **/
  9244. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  9245. {
  9246. int vectors = 0;
  9247. ssize_t size;
  9248. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9249. vectors = i40e_init_msix(pf);
  9250. if (vectors < 0) {
  9251. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  9252. I40E_FLAG_IWARP_ENABLED |
  9253. I40E_FLAG_RSS_ENABLED |
  9254. I40E_FLAG_DCB_CAPABLE |
  9255. I40E_FLAG_DCB_ENABLED |
  9256. I40E_FLAG_SRIOV_ENABLED |
  9257. I40E_FLAG_FD_SB_ENABLED |
  9258. I40E_FLAG_FD_ATR_ENABLED |
  9259. I40E_FLAG_VMDQ_ENABLED);
  9260. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9261. /* rework the queue expectations without MSIX */
  9262. i40e_determine_queue_usage(pf);
  9263. }
  9264. }
  9265. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9266. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  9267. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  9268. vectors = pci_enable_msi(pf->pdev);
  9269. if (vectors < 0) {
  9270. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  9271. vectors);
  9272. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  9273. }
  9274. vectors = 1; /* one MSI or Legacy vector */
  9275. }
  9276. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  9277. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  9278. /* set up vector assignment tracking */
  9279. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  9280. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  9281. if (!pf->irq_pile)
  9282. return -ENOMEM;
  9283. pf->irq_pile->num_entries = vectors;
  9284. pf->irq_pile->search_hint = 0;
  9285. /* track first vector for misc interrupts, ignore return */
  9286. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  9287. return 0;
  9288. }
  9289. /**
  9290. * i40e_restore_interrupt_scheme - Restore the interrupt scheme
  9291. * @pf: private board data structure
  9292. *
  9293. * Restore the interrupt scheme that was cleared when we suspended the
  9294. * device. This should be called during resume to re-allocate the q_vectors
  9295. * and reacquire IRQs.
  9296. */
  9297. static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
  9298. {
  9299. int err, i;
  9300. /* We cleared the MSI and MSI-X flags when disabling the old interrupt
  9301. * scheme. We need to re-enabled them here in order to attempt to
  9302. * re-acquire the MSI or MSI-X vectors
  9303. */
  9304. pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  9305. err = i40e_init_interrupt_scheme(pf);
  9306. if (err)
  9307. return err;
  9308. /* Now that we've re-acquired IRQs, we need to remap the vectors and
  9309. * rings together again.
  9310. */
  9311. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9312. if (pf->vsi[i]) {
  9313. err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
  9314. if (err)
  9315. goto err_unwind;
  9316. i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
  9317. }
  9318. }
  9319. err = i40e_setup_misc_vector(pf);
  9320. if (err)
  9321. goto err_unwind;
  9322. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  9323. i40e_client_update_msix_info(pf);
  9324. return 0;
  9325. err_unwind:
  9326. while (i--) {
  9327. if (pf->vsi[i])
  9328. i40e_vsi_free_q_vectors(pf->vsi[i]);
  9329. }
  9330. return err;
  9331. }
  9332. /**
  9333. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  9334. * @pf: board private structure
  9335. *
  9336. * This sets up the handler for MSIX 0, which is used to manage the
  9337. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  9338. * when in MSI or Legacy interrupt mode.
  9339. **/
  9340. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  9341. {
  9342. struct i40e_hw *hw = &pf->hw;
  9343. int err = 0;
  9344. /* Only request the IRQ once, the first time through. */
  9345. if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
  9346. err = request_irq(pf->msix_entries[0].vector,
  9347. i40e_intr, 0, pf->int_name, pf);
  9348. if (err) {
  9349. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  9350. dev_info(&pf->pdev->dev,
  9351. "request_irq for %s failed: %d\n",
  9352. pf->int_name, err);
  9353. return -EFAULT;
  9354. }
  9355. }
  9356. i40e_enable_misc_int_causes(pf);
  9357. /* associate no queues to the misc vector */
  9358. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  9359. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  9360. i40e_flush(hw);
  9361. i40e_irq_dynamic_enable_icr0(pf);
  9362. return err;
  9363. }
  9364. /**
  9365. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  9366. * @vsi: Pointer to vsi structure
  9367. * @seed: Buffter to store the hash keys
  9368. * @lut: Buffer to store the lookup table entries
  9369. * @lut_size: Size of buffer to store the lookup table entries
  9370. *
  9371. * Return 0 on success, negative on failure
  9372. */
  9373. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  9374. u8 *lut, u16 lut_size)
  9375. {
  9376. struct i40e_pf *pf = vsi->back;
  9377. struct i40e_hw *hw = &pf->hw;
  9378. int ret = 0;
  9379. if (seed) {
  9380. ret = i40e_aq_get_rss_key(hw, vsi->id,
  9381. (struct i40e_aqc_get_set_rss_key_data *)seed);
  9382. if (ret) {
  9383. dev_info(&pf->pdev->dev,
  9384. "Cannot get RSS key, err %s aq_err %s\n",
  9385. i40e_stat_str(&pf->hw, ret),
  9386. i40e_aq_str(&pf->hw,
  9387. pf->hw.aq.asq_last_status));
  9388. return ret;
  9389. }
  9390. }
  9391. if (lut) {
  9392. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  9393. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  9394. if (ret) {
  9395. dev_info(&pf->pdev->dev,
  9396. "Cannot get RSS lut, err %s aq_err %s\n",
  9397. i40e_stat_str(&pf->hw, ret),
  9398. i40e_aq_str(&pf->hw,
  9399. pf->hw.aq.asq_last_status));
  9400. return ret;
  9401. }
  9402. }
  9403. return ret;
  9404. }
  9405. /**
  9406. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  9407. * @vsi: Pointer to vsi structure
  9408. * @seed: RSS hash seed
  9409. * @lut: Lookup table
  9410. * @lut_size: Lookup table size
  9411. *
  9412. * Returns 0 on success, negative on failure
  9413. **/
  9414. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  9415. const u8 *lut, u16 lut_size)
  9416. {
  9417. struct i40e_pf *pf = vsi->back;
  9418. struct i40e_hw *hw = &pf->hw;
  9419. u16 vf_id = vsi->vf_id;
  9420. u8 i;
  9421. /* Fill out hash function seed */
  9422. if (seed) {
  9423. u32 *seed_dw = (u32 *)seed;
  9424. if (vsi->type == I40E_VSI_MAIN) {
  9425. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9426. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  9427. } else if (vsi->type == I40E_VSI_SRIOV) {
  9428. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  9429. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  9430. } else {
  9431. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  9432. }
  9433. }
  9434. if (lut) {
  9435. u32 *lut_dw = (u32 *)lut;
  9436. if (vsi->type == I40E_VSI_MAIN) {
  9437. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9438. return -EINVAL;
  9439. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9440. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  9441. } else if (vsi->type == I40E_VSI_SRIOV) {
  9442. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  9443. return -EINVAL;
  9444. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9445. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  9446. } else {
  9447. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  9448. }
  9449. }
  9450. i40e_flush(hw);
  9451. return 0;
  9452. }
  9453. /**
  9454. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  9455. * @vsi: Pointer to VSI structure
  9456. * @seed: Buffer to store the keys
  9457. * @lut: Buffer to store the lookup table entries
  9458. * @lut_size: Size of buffer to store the lookup table entries
  9459. *
  9460. * Returns 0 on success, negative on failure
  9461. */
  9462. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  9463. u8 *lut, u16 lut_size)
  9464. {
  9465. struct i40e_pf *pf = vsi->back;
  9466. struct i40e_hw *hw = &pf->hw;
  9467. u16 i;
  9468. if (seed) {
  9469. u32 *seed_dw = (u32 *)seed;
  9470. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9471. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  9472. }
  9473. if (lut) {
  9474. u32 *lut_dw = (u32 *)lut;
  9475. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9476. return -EINVAL;
  9477. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9478. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  9479. }
  9480. return 0;
  9481. }
  9482. /**
  9483. * i40e_config_rss - Configure RSS keys and lut
  9484. * @vsi: Pointer to VSI structure
  9485. * @seed: RSS hash seed
  9486. * @lut: Lookup table
  9487. * @lut_size: Lookup table size
  9488. *
  9489. * Returns 0 on success, negative on failure
  9490. */
  9491. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9492. {
  9493. struct i40e_pf *pf = vsi->back;
  9494. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9495. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  9496. else
  9497. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  9498. }
  9499. /**
  9500. * i40e_get_rss - Get RSS keys and lut
  9501. * @vsi: Pointer to VSI structure
  9502. * @seed: Buffer to store the keys
  9503. * @lut: Buffer to store the lookup table entries
  9504. * @lut_size: Size of buffer to store the lookup table entries
  9505. *
  9506. * Returns 0 on success, negative on failure
  9507. */
  9508. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9509. {
  9510. struct i40e_pf *pf = vsi->back;
  9511. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9512. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  9513. else
  9514. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  9515. }
  9516. /**
  9517. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  9518. * @pf: Pointer to board private structure
  9519. * @lut: Lookup table
  9520. * @rss_table_size: Lookup table size
  9521. * @rss_size: Range of queue number for hashing
  9522. */
  9523. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  9524. u16 rss_table_size, u16 rss_size)
  9525. {
  9526. u16 i;
  9527. for (i = 0; i < rss_table_size; i++)
  9528. lut[i] = i % rss_size;
  9529. }
  9530. /**
  9531. * i40e_pf_config_rss - Prepare for RSS if used
  9532. * @pf: board private structure
  9533. **/
  9534. static int i40e_pf_config_rss(struct i40e_pf *pf)
  9535. {
  9536. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9537. u8 seed[I40E_HKEY_ARRAY_SIZE];
  9538. u8 *lut;
  9539. struct i40e_hw *hw = &pf->hw;
  9540. u32 reg_val;
  9541. u64 hena;
  9542. int ret;
  9543. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  9544. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  9545. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  9546. hena |= i40e_pf_get_default_rss_hena(pf);
  9547. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  9548. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  9549. /* Determine the RSS table size based on the hardware capabilities */
  9550. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  9551. reg_val = (pf->rss_table_size == 512) ?
  9552. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  9553. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  9554. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  9555. /* Determine the RSS size of the VSI */
  9556. if (!vsi->rss_size) {
  9557. u16 qcount;
  9558. /* If the firmware does something weird during VSI init, we
  9559. * could end up with zero TCs. Check for that to avoid
  9560. * divide-by-zero. It probably won't pass traffic, but it also
  9561. * won't panic.
  9562. */
  9563. qcount = vsi->num_queue_pairs /
  9564. (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
  9565. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9566. }
  9567. if (!vsi->rss_size)
  9568. return -EINVAL;
  9569. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  9570. if (!lut)
  9571. return -ENOMEM;
  9572. /* Use user configured lut if there is one, otherwise use default */
  9573. if (vsi->rss_lut_user)
  9574. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  9575. else
  9576. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  9577. /* Use user configured hash key if there is one, otherwise
  9578. * use default.
  9579. */
  9580. if (vsi->rss_hkey_user)
  9581. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  9582. else
  9583. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  9584. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  9585. kfree(lut);
  9586. return ret;
  9587. }
  9588. /**
  9589. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  9590. * @pf: board private structure
  9591. * @queue_count: the requested queue count for rss.
  9592. *
  9593. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  9594. * count which may be different from the requested queue count.
  9595. * Note: expects to be called while under rtnl_lock()
  9596. **/
  9597. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  9598. {
  9599. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9600. int new_rss_size;
  9601. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  9602. return 0;
  9603. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  9604. if (queue_count != vsi->num_queue_pairs) {
  9605. u16 qcount;
  9606. vsi->req_queue_pairs = queue_count;
  9607. i40e_prep_for_reset(pf, true);
  9608. pf->alloc_rss_size = new_rss_size;
  9609. i40e_reset_and_rebuild(pf, true, true);
  9610. /* Discard the user configured hash keys and lut, if less
  9611. * queues are enabled.
  9612. */
  9613. if (queue_count < vsi->rss_size) {
  9614. i40e_clear_rss_config_user(vsi);
  9615. dev_dbg(&pf->pdev->dev,
  9616. "discard user configured hash keys and lut\n");
  9617. }
  9618. /* Reset vsi->rss_size, as number of enabled queues changed */
  9619. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  9620. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9621. i40e_pf_config_rss(pf);
  9622. }
  9623. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  9624. vsi->req_queue_pairs, pf->rss_size_max);
  9625. return pf->alloc_rss_size;
  9626. }
  9627. /**
  9628. * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
  9629. * @pf: board private structure
  9630. **/
  9631. i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
  9632. {
  9633. i40e_status status;
  9634. bool min_valid, max_valid;
  9635. u32 max_bw, min_bw;
  9636. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  9637. &min_valid, &max_valid);
  9638. if (!status) {
  9639. if (min_valid)
  9640. pf->min_bw = min_bw;
  9641. if (max_valid)
  9642. pf->max_bw = max_bw;
  9643. }
  9644. return status;
  9645. }
  9646. /**
  9647. * i40e_set_partition_bw_setting - Set BW settings for this PF partition
  9648. * @pf: board private structure
  9649. **/
  9650. i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
  9651. {
  9652. struct i40e_aqc_configure_partition_bw_data bw_data;
  9653. i40e_status status;
  9654. /* Set the valid bit for this PF */
  9655. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  9656. bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
  9657. bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
  9658. /* Set the new bandwidths */
  9659. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  9660. return status;
  9661. }
  9662. /**
  9663. * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
  9664. * @pf: board private structure
  9665. **/
  9666. i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
  9667. {
  9668. /* Commit temporary BW setting to permanent NVM image */
  9669. enum i40e_admin_queue_err last_aq_status;
  9670. i40e_status ret;
  9671. u16 nvm_word;
  9672. if (pf->hw.partition_id != 1) {
  9673. dev_info(&pf->pdev->dev,
  9674. "Commit BW only works on partition 1! This is partition %d",
  9675. pf->hw.partition_id);
  9676. ret = I40E_NOT_SUPPORTED;
  9677. goto bw_commit_out;
  9678. }
  9679. /* Acquire NVM for read access */
  9680. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  9681. last_aq_status = pf->hw.aq.asq_last_status;
  9682. if (ret) {
  9683. dev_info(&pf->pdev->dev,
  9684. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  9685. i40e_stat_str(&pf->hw, ret),
  9686. i40e_aq_str(&pf->hw, last_aq_status));
  9687. goto bw_commit_out;
  9688. }
  9689. /* Read word 0x10 of NVM - SW compatibility word 1 */
  9690. ret = i40e_aq_read_nvm(&pf->hw,
  9691. I40E_SR_NVM_CONTROL_WORD,
  9692. 0x10, sizeof(nvm_word), &nvm_word,
  9693. false, NULL);
  9694. /* Save off last admin queue command status before releasing
  9695. * the NVM
  9696. */
  9697. last_aq_status = pf->hw.aq.asq_last_status;
  9698. i40e_release_nvm(&pf->hw);
  9699. if (ret) {
  9700. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  9701. i40e_stat_str(&pf->hw, ret),
  9702. i40e_aq_str(&pf->hw, last_aq_status));
  9703. goto bw_commit_out;
  9704. }
  9705. /* Wait a bit for NVM release to complete */
  9706. msleep(50);
  9707. /* Acquire NVM for write access */
  9708. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  9709. last_aq_status = pf->hw.aq.asq_last_status;
  9710. if (ret) {
  9711. dev_info(&pf->pdev->dev,
  9712. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  9713. i40e_stat_str(&pf->hw, ret),
  9714. i40e_aq_str(&pf->hw, last_aq_status));
  9715. goto bw_commit_out;
  9716. }
  9717. /* Write it back out unchanged to initiate update NVM,
  9718. * which will force a write of the shadow (alt) RAM to
  9719. * the NVM - thus storing the bandwidth values permanently.
  9720. */
  9721. ret = i40e_aq_update_nvm(&pf->hw,
  9722. I40E_SR_NVM_CONTROL_WORD,
  9723. 0x10, sizeof(nvm_word),
  9724. &nvm_word, true, 0, NULL);
  9725. /* Save off last admin queue command status before releasing
  9726. * the NVM
  9727. */
  9728. last_aq_status = pf->hw.aq.asq_last_status;
  9729. i40e_release_nvm(&pf->hw);
  9730. if (ret)
  9731. dev_info(&pf->pdev->dev,
  9732. "BW settings NOT SAVED, err %s aq_err %s\n",
  9733. i40e_stat_str(&pf->hw, ret),
  9734. i40e_aq_str(&pf->hw, last_aq_status));
  9735. bw_commit_out:
  9736. return ret;
  9737. }
  9738. /**
  9739. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  9740. * @pf: board private structure to initialize
  9741. *
  9742. * i40e_sw_init initializes the Adapter private data structure.
  9743. * Fields are initialized based on PCI device information and
  9744. * OS network device settings (MTU size).
  9745. **/
  9746. static int i40e_sw_init(struct i40e_pf *pf)
  9747. {
  9748. int err = 0;
  9749. int size;
  9750. /* Set default capability flags */
  9751. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  9752. I40E_FLAG_MSI_ENABLED |
  9753. I40E_FLAG_MSIX_ENABLED;
  9754. /* Set default ITR */
  9755. pf->rx_itr_default = I40E_ITR_RX_DEF;
  9756. pf->tx_itr_default = I40E_ITR_TX_DEF;
  9757. /* Depending on PF configurations, it is possible that the RSS
  9758. * maximum might end up larger than the available queues
  9759. */
  9760. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  9761. pf->alloc_rss_size = 1;
  9762. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  9763. pf->rss_size_max = min_t(int, pf->rss_size_max,
  9764. pf->hw.func_caps.num_tx_qp);
  9765. if (pf->hw.func_caps.rss) {
  9766. pf->flags |= I40E_FLAG_RSS_ENABLED;
  9767. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  9768. num_online_cpus());
  9769. }
  9770. /* MFP mode enabled */
  9771. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  9772. pf->flags |= I40E_FLAG_MFP_ENABLED;
  9773. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  9774. if (i40e_get_partition_bw_setting(pf)) {
  9775. dev_warn(&pf->pdev->dev,
  9776. "Could not get partition bw settings\n");
  9777. } else {
  9778. dev_info(&pf->pdev->dev,
  9779. "Partition BW Min = %8.8x, Max = %8.8x\n",
  9780. pf->min_bw, pf->max_bw);
  9781. /* nudge the Tx scheduler */
  9782. i40e_set_partition_bw_setting(pf);
  9783. }
  9784. }
  9785. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  9786. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  9787. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  9788. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  9789. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  9790. pf->hw.num_partitions > 1)
  9791. dev_info(&pf->pdev->dev,
  9792. "Flow Director Sideband mode Disabled in MFP mode\n");
  9793. else
  9794. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9795. pf->fdir_pf_filter_count =
  9796. pf->hw.func_caps.fd_filters_guaranteed;
  9797. pf->hw.fdir_shared_filter_count =
  9798. pf->hw.func_caps.fd_filters_best_effort;
  9799. }
  9800. if (pf->hw.mac.type == I40E_MAC_X722) {
  9801. pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
  9802. I40E_HW_128_QP_RSS_CAPABLE |
  9803. I40E_HW_ATR_EVICT_CAPABLE |
  9804. I40E_HW_WB_ON_ITR_CAPABLE |
  9805. I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
  9806. I40E_HW_NO_PCI_LINK_CHECK |
  9807. I40E_HW_USE_SET_LLDP_MIB |
  9808. I40E_HW_GENEVE_OFFLOAD_CAPABLE |
  9809. I40E_HW_PTP_L4_CAPABLE |
  9810. I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
  9811. I40E_HW_OUTER_UDP_CSUM_CAPABLE);
  9812. #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
  9813. if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
  9814. I40E_FDEVICT_PCTYPE_DEFAULT) {
  9815. dev_warn(&pf->pdev->dev,
  9816. "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
  9817. pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
  9818. }
  9819. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  9820. ((pf->hw.aq.api_maj_ver == 1) &&
  9821. (pf->hw.aq.api_min_ver > 4))) {
  9822. /* Supported in FW API version higher than 1.4 */
  9823. pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
  9824. }
  9825. /* Enable HW ATR eviction if possible */
  9826. if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
  9827. pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
  9828. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9829. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  9830. (pf->hw.aq.fw_maj_ver < 4))) {
  9831. pf->hw_features |= I40E_HW_RESTART_AUTONEG;
  9832. /* No DCB support for FW < v4.33 */
  9833. pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
  9834. }
  9835. /* Disable FW LLDP if FW < v4.3 */
  9836. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9837. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  9838. (pf->hw.aq.fw_maj_ver < 4)))
  9839. pf->hw_features |= I40E_HW_STOP_FW_LLDP;
  9840. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  9841. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9842. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  9843. (pf->hw.aq.fw_maj_ver >= 5)))
  9844. pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
  9845. /* Enable PTP L4 if FW > v6.0 */
  9846. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9847. pf->hw.aq.fw_maj_ver >= 6)
  9848. pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
  9849. if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
  9850. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  9851. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  9852. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  9853. }
  9854. if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
  9855. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  9856. /* IWARP needs one extra vector for CQP just like MISC.*/
  9857. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  9858. }
  9859. /* Stopping the FW LLDP engine is only supported on the
  9860. * XL710 with a FW ver >= 1.7. Also, stopping FW LLDP
  9861. * engine is not supported if NPAR is functioning on this
  9862. * part
  9863. */
  9864. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9865. !pf->hw.func_caps.npar_enable &&
  9866. (pf->hw.aq.api_maj_ver > 1 ||
  9867. (pf->hw.aq.api_maj_ver == 1 && pf->hw.aq.api_min_ver > 6)))
  9868. pf->hw_features |= I40E_HW_STOPPABLE_FW_LLDP;
  9869. #ifdef CONFIG_PCI_IOV
  9870. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  9871. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  9872. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  9873. pf->num_req_vfs = min_t(int,
  9874. pf->hw.func_caps.num_vfs,
  9875. I40E_MAX_VF_COUNT);
  9876. }
  9877. #endif /* CONFIG_PCI_IOV */
  9878. pf->eeprom_version = 0xDEAD;
  9879. pf->lan_veb = I40E_NO_VEB;
  9880. pf->lan_vsi = I40E_NO_VSI;
  9881. /* By default FW has this off for performance reasons */
  9882. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  9883. /* set up queue assignment tracking */
  9884. size = sizeof(struct i40e_lump_tracking)
  9885. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  9886. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  9887. if (!pf->qp_pile) {
  9888. err = -ENOMEM;
  9889. goto sw_init_done;
  9890. }
  9891. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  9892. pf->qp_pile->search_hint = 0;
  9893. pf->tx_timeout_recovery_level = 1;
  9894. mutex_init(&pf->switch_mutex);
  9895. sw_init_done:
  9896. return err;
  9897. }
  9898. /**
  9899. * i40e_set_ntuple - set the ntuple feature flag and take action
  9900. * @pf: board private structure to initialize
  9901. * @features: the feature set that the stack is suggesting
  9902. *
  9903. * returns a bool to indicate if reset needs to happen
  9904. **/
  9905. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  9906. {
  9907. bool need_reset = false;
  9908. /* Check if Flow Director n-tuple support was enabled or disabled. If
  9909. * the state changed, we need to reset.
  9910. */
  9911. if (features & NETIF_F_NTUPLE) {
  9912. /* Enable filters and mark for reset */
  9913. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  9914. need_reset = true;
  9915. /* enable FD_SB only if there is MSI-X vector and no cloud
  9916. * filters exist
  9917. */
  9918. if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
  9919. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9920. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  9921. }
  9922. } else {
  9923. /* turn off filters, mark for reset and clear SW filter list */
  9924. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9925. need_reset = true;
  9926. i40e_fdir_filter_exit(pf);
  9927. }
  9928. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9929. clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
  9930. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9931. /* reset fd counters */
  9932. pf->fd_add_err = 0;
  9933. pf->fd_atr_cnt = 0;
  9934. /* if ATR was auto disabled it can be re-enabled. */
  9935. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  9936. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  9937. (I40E_DEBUG_FD & pf->hw.debug_mask))
  9938. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  9939. }
  9940. return need_reset;
  9941. }
  9942. /**
  9943. * i40e_clear_rss_lut - clear the rx hash lookup table
  9944. * @vsi: the VSI being configured
  9945. **/
  9946. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  9947. {
  9948. struct i40e_pf *pf = vsi->back;
  9949. struct i40e_hw *hw = &pf->hw;
  9950. u16 vf_id = vsi->vf_id;
  9951. u8 i;
  9952. if (vsi->type == I40E_VSI_MAIN) {
  9953. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9954. wr32(hw, I40E_PFQF_HLUT(i), 0);
  9955. } else if (vsi->type == I40E_VSI_SRIOV) {
  9956. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9957. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  9958. } else {
  9959. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  9960. }
  9961. }
  9962. /**
  9963. * i40e_set_features - set the netdev feature flags
  9964. * @netdev: ptr to the netdev being adjusted
  9965. * @features: the feature set that the stack is suggesting
  9966. * Note: expects to be called while under rtnl_lock()
  9967. **/
  9968. static int i40e_set_features(struct net_device *netdev,
  9969. netdev_features_t features)
  9970. {
  9971. struct i40e_netdev_priv *np = netdev_priv(netdev);
  9972. struct i40e_vsi *vsi = np->vsi;
  9973. struct i40e_pf *pf = vsi->back;
  9974. bool need_reset;
  9975. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  9976. i40e_pf_config_rss(pf);
  9977. else if (!(features & NETIF_F_RXHASH) &&
  9978. netdev->features & NETIF_F_RXHASH)
  9979. i40e_clear_rss_lut(vsi);
  9980. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  9981. i40e_vlan_stripping_enable(vsi);
  9982. else
  9983. i40e_vlan_stripping_disable(vsi);
  9984. if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
  9985. dev_err(&pf->pdev->dev,
  9986. "Offloaded tc filters active, can't turn hw_tc_offload off");
  9987. return -EINVAL;
  9988. }
  9989. need_reset = i40e_set_ntuple(pf, features);
  9990. if (need_reset)
  9991. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  9992. return 0;
  9993. }
  9994. /**
  9995. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  9996. * @pf: board private structure
  9997. * @port: The UDP port to look up
  9998. *
  9999. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  10000. **/
  10001. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  10002. {
  10003. u8 i;
  10004. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  10005. /* Do not report ports with pending deletions as
  10006. * being available.
  10007. */
  10008. if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
  10009. continue;
  10010. if (pf->udp_ports[i].port == port)
  10011. return i;
  10012. }
  10013. return i;
  10014. }
  10015. /**
  10016. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  10017. * @netdev: This physical port's netdev
  10018. * @ti: Tunnel endpoint information
  10019. **/
  10020. static void i40e_udp_tunnel_add(struct net_device *netdev,
  10021. struct udp_tunnel_info *ti)
  10022. {
  10023. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10024. struct i40e_vsi *vsi = np->vsi;
  10025. struct i40e_pf *pf = vsi->back;
  10026. u16 port = ntohs(ti->port);
  10027. u8 next_idx;
  10028. u8 idx;
  10029. idx = i40e_get_udp_port_idx(pf, port);
  10030. /* Check if port already exists */
  10031. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10032. netdev_info(netdev, "port %d already offloaded\n", port);
  10033. return;
  10034. }
  10035. /* Now check if there is space to add the new port */
  10036. next_idx = i40e_get_udp_port_idx(pf, 0);
  10037. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10038. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  10039. port);
  10040. return;
  10041. }
  10042. switch (ti->type) {
  10043. case UDP_TUNNEL_TYPE_VXLAN:
  10044. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  10045. break;
  10046. case UDP_TUNNEL_TYPE_GENEVE:
  10047. if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
  10048. return;
  10049. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  10050. break;
  10051. default:
  10052. return;
  10053. }
  10054. /* New port: add it and mark its index in the bitmap */
  10055. pf->udp_ports[next_idx].port = port;
  10056. pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
  10057. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  10058. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10059. }
  10060. /**
  10061. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  10062. * @netdev: This physical port's netdev
  10063. * @ti: Tunnel endpoint information
  10064. **/
  10065. static void i40e_udp_tunnel_del(struct net_device *netdev,
  10066. struct udp_tunnel_info *ti)
  10067. {
  10068. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10069. struct i40e_vsi *vsi = np->vsi;
  10070. struct i40e_pf *pf = vsi->back;
  10071. u16 port = ntohs(ti->port);
  10072. u8 idx;
  10073. idx = i40e_get_udp_port_idx(pf, port);
  10074. /* Check if port already exists */
  10075. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  10076. goto not_found;
  10077. switch (ti->type) {
  10078. case UDP_TUNNEL_TYPE_VXLAN:
  10079. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  10080. goto not_found;
  10081. break;
  10082. case UDP_TUNNEL_TYPE_GENEVE:
  10083. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  10084. goto not_found;
  10085. break;
  10086. default:
  10087. goto not_found;
  10088. }
  10089. /* if port exists, set it to 0 (mark for deletion)
  10090. * and make it pending
  10091. */
  10092. pf->udp_ports[idx].port = 0;
  10093. /* Toggle pending bit instead of setting it. This way if we are
  10094. * deleting a port that has yet to be added we just clear the pending
  10095. * bit and don't have to worry about it.
  10096. */
  10097. pf->pending_udp_bitmap ^= BIT_ULL(idx);
  10098. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10099. return;
  10100. not_found:
  10101. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  10102. port);
  10103. }
  10104. static int i40e_get_phys_port_id(struct net_device *netdev,
  10105. struct netdev_phys_item_id *ppid)
  10106. {
  10107. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10108. struct i40e_pf *pf = np->vsi->back;
  10109. struct i40e_hw *hw = &pf->hw;
  10110. if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
  10111. return -EOPNOTSUPP;
  10112. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  10113. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  10114. return 0;
  10115. }
  10116. /**
  10117. * i40e_ndo_fdb_add - add an entry to the hardware database
  10118. * @ndm: the input from the stack
  10119. * @tb: pointer to array of nladdr (unused)
  10120. * @dev: the net device pointer
  10121. * @addr: the MAC address entry being added
  10122. * @vid: VLAN ID
  10123. * @flags: instructions from stack about fdb operation
  10124. */
  10125. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  10126. struct net_device *dev,
  10127. const unsigned char *addr, u16 vid,
  10128. u16 flags)
  10129. {
  10130. struct i40e_netdev_priv *np = netdev_priv(dev);
  10131. struct i40e_pf *pf = np->vsi->back;
  10132. int err = 0;
  10133. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  10134. return -EOPNOTSUPP;
  10135. if (vid) {
  10136. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  10137. return -EINVAL;
  10138. }
  10139. /* Hardware does not support aging addresses so if a
  10140. * ndm_state is given only allow permanent addresses
  10141. */
  10142. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  10143. netdev_info(dev, "FDB only supports static addresses\n");
  10144. return -EINVAL;
  10145. }
  10146. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  10147. err = dev_uc_add_excl(dev, addr);
  10148. else if (is_multicast_ether_addr(addr))
  10149. err = dev_mc_add_excl(dev, addr);
  10150. else
  10151. err = -EINVAL;
  10152. /* Only return duplicate errors if NLM_F_EXCL is set */
  10153. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  10154. err = 0;
  10155. return err;
  10156. }
  10157. /**
  10158. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  10159. * @dev: the netdev being configured
  10160. * @nlh: RTNL message
  10161. * @flags: bridge flags
  10162. *
  10163. * Inserts a new hardware bridge if not already created and
  10164. * enables the bridging mode requested (VEB or VEPA). If the
  10165. * hardware bridge has already been inserted and the request
  10166. * is to change the mode then that requires a PF reset to
  10167. * allow rebuild of the components with required hardware
  10168. * bridge mode enabled.
  10169. *
  10170. * Note: expects to be called while under rtnl_lock()
  10171. **/
  10172. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  10173. struct nlmsghdr *nlh,
  10174. u16 flags)
  10175. {
  10176. struct i40e_netdev_priv *np = netdev_priv(dev);
  10177. struct i40e_vsi *vsi = np->vsi;
  10178. struct i40e_pf *pf = vsi->back;
  10179. struct i40e_veb *veb = NULL;
  10180. struct nlattr *attr, *br_spec;
  10181. int i, rem;
  10182. /* Only for PF VSI for now */
  10183. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10184. return -EOPNOTSUPP;
  10185. /* Find the HW bridge for PF VSI */
  10186. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10187. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10188. veb = pf->veb[i];
  10189. }
  10190. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  10191. nla_for_each_nested(attr, br_spec, rem) {
  10192. __u16 mode;
  10193. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  10194. continue;
  10195. mode = nla_get_u16(attr);
  10196. if ((mode != BRIDGE_MODE_VEPA) &&
  10197. (mode != BRIDGE_MODE_VEB))
  10198. return -EINVAL;
  10199. /* Insert a new HW bridge */
  10200. if (!veb) {
  10201. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  10202. vsi->tc_config.enabled_tc);
  10203. if (veb) {
  10204. veb->bridge_mode = mode;
  10205. i40e_config_bridge_mode(veb);
  10206. } else {
  10207. /* No Bridge HW offload available */
  10208. return -ENOENT;
  10209. }
  10210. break;
  10211. } else if (mode != veb->bridge_mode) {
  10212. /* Existing HW bridge but different mode needs reset */
  10213. veb->bridge_mode = mode;
  10214. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  10215. if (mode == BRIDGE_MODE_VEB)
  10216. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  10217. else
  10218. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  10219. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  10220. break;
  10221. }
  10222. }
  10223. return 0;
  10224. }
  10225. /**
  10226. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  10227. * @skb: skb buff
  10228. * @pid: process id
  10229. * @seq: RTNL message seq #
  10230. * @dev: the netdev being configured
  10231. * @filter_mask: unused
  10232. * @nlflags: netlink flags passed in
  10233. *
  10234. * Return the mode in which the hardware bridge is operating in
  10235. * i.e VEB or VEPA.
  10236. **/
  10237. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  10238. struct net_device *dev,
  10239. u32 __always_unused filter_mask,
  10240. int nlflags)
  10241. {
  10242. struct i40e_netdev_priv *np = netdev_priv(dev);
  10243. struct i40e_vsi *vsi = np->vsi;
  10244. struct i40e_pf *pf = vsi->back;
  10245. struct i40e_veb *veb = NULL;
  10246. int i;
  10247. /* Only for PF VSI for now */
  10248. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10249. return -EOPNOTSUPP;
  10250. /* Find the HW bridge for the PF VSI */
  10251. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10252. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10253. veb = pf->veb[i];
  10254. }
  10255. if (!veb)
  10256. return 0;
  10257. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  10258. 0, 0, nlflags, filter_mask, NULL);
  10259. }
  10260. /**
  10261. * i40e_features_check - Validate encapsulated packet conforms to limits
  10262. * @skb: skb buff
  10263. * @dev: This physical port's netdev
  10264. * @features: Offload features that the stack believes apply
  10265. **/
  10266. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  10267. struct net_device *dev,
  10268. netdev_features_t features)
  10269. {
  10270. size_t len;
  10271. /* No point in doing any of this if neither checksum nor GSO are
  10272. * being requested for this frame. We can rule out both by just
  10273. * checking for CHECKSUM_PARTIAL
  10274. */
  10275. if (skb->ip_summed != CHECKSUM_PARTIAL)
  10276. return features;
  10277. /* We cannot support GSO if the MSS is going to be less than
  10278. * 64 bytes. If it is then we need to drop support for GSO.
  10279. */
  10280. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  10281. features &= ~NETIF_F_GSO_MASK;
  10282. /* MACLEN can support at most 63 words */
  10283. len = skb_network_header(skb) - skb->data;
  10284. if (len & ~(63 * 2))
  10285. goto out_err;
  10286. /* IPLEN and EIPLEN can support at most 127 dwords */
  10287. len = skb_transport_header(skb) - skb_network_header(skb);
  10288. if (len & ~(127 * 4))
  10289. goto out_err;
  10290. if (skb->encapsulation) {
  10291. /* L4TUNLEN can support 127 words */
  10292. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  10293. if (len & ~(127 * 2))
  10294. goto out_err;
  10295. /* IPLEN can support at most 127 dwords */
  10296. len = skb_inner_transport_header(skb) -
  10297. skb_inner_network_header(skb);
  10298. if (len & ~(127 * 4))
  10299. goto out_err;
  10300. }
  10301. /* No need to validate L4LEN as TCP is the only protocol with a
  10302. * a flexible value and we support all possible values supported
  10303. * by TCP, which is at most 15 dwords
  10304. */
  10305. return features;
  10306. out_err:
  10307. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  10308. }
  10309. /**
  10310. * i40e_xdp_setup - add/remove an XDP program
  10311. * @vsi: VSI to changed
  10312. * @prog: XDP program
  10313. **/
  10314. static int i40e_xdp_setup(struct i40e_vsi *vsi,
  10315. struct bpf_prog *prog)
  10316. {
  10317. int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  10318. struct i40e_pf *pf = vsi->back;
  10319. struct bpf_prog *old_prog;
  10320. bool need_reset;
  10321. int i;
  10322. /* Don't allow frames that span over multiple buffers */
  10323. if (frame_size > vsi->rx_buf_len)
  10324. return -EINVAL;
  10325. if (!i40e_enabled_xdp_vsi(vsi) && !prog)
  10326. return 0;
  10327. /* When turning XDP on->off/off->on we reset and rebuild the rings. */
  10328. need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
  10329. if (need_reset)
  10330. i40e_prep_for_reset(pf, true);
  10331. old_prog = xchg(&vsi->xdp_prog, prog);
  10332. if (need_reset)
  10333. i40e_reset_and_rebuild(pf, true, true);
  10334. for (i = 0; i < vsi->num_queue_pairs; i++)
  10335. WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
  10336. if (old_prog)
  10337. bpf_prog_put(old_prog);
  10338. return 0;
  10339. }
  10340. /**
  10341. * i40e_xdp - implements ndo_bpf for i40e
  10342. * @dev: netdevice
  10343. * @xdp: XDP command
  10344. **/
  10345. static int i40e_xdp(struct net_device *dev,
  10346. struct netdev_bpf *xdp)
  10347. {
  10348. struct i40e_netdev_priv *np = netdev_priv(dev);
  10349. struct i40e_vsi *vsi = np->vsi;
  10350. if (vsi->type != I40E_VSI_MAIN)
  10351. return -EINVAL;
  10352. switch (xdp->command) {
  10353. case XDP_SETUP_PROG:
  10354. return i40e_xdp_setup(vsi, xdp->prog);
  10355. case XDP_QUERY_PROG:
  10356. xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
  10357. return 0;
  10358. default:
  10359. return -EINVAL;
  10360. }
  10361. }
  10362. static const struct net_device_ops i40e_netdev_ops = {
  10363. .ndo_open = i40e_open,
  10364. .ndo_stop = i40e_close,
  10365. .ndo_start_xmit = i40e_lan_xmit_frame,
  10366. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  10367. .ndo_set_rx_mode = i40e_set_rx_mode,
  10368. .ndo_validate_addr = eth_validate_addr,
  10369. .ndo_set_mac_address = i40e_set_mac,
  10370. .ndo_change_mtu = i40e_change_mtu,
  10371. .ndo_do_ioctl = i40e_ioctl,
  10372. .ndo_tx_timeout = i40e_tx_timeout,
  10373. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  10374. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  10375. #ifdef CONFIG_NET_POLL_CONTROLLER
  10376. .ndo_poll_controller = i40e_netpoll,
  10377. #endif
  10378. .ndo_setup_tc = __i40e_setup_tc,
  10379. .ndo_set_features = i40e_set_features,
  10380. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  10381. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  10382. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  10383. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  10384. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  10385. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  10386. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  10387. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  10388. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  10389. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  10390. .ndo_fdb_add = i40e_ndo_fdb_add,
  10391. .ndo_features_check = i40e_features_check,
  10392. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  10393. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  10394. .ndo_bpf = i40e_xdp,
  10395. .ndo_xdp_xmit = i40e_xdp_xmit,
  10396. };
  10397. /**
  10398. * i40e_config_netdev - Setup the netdev flags
  10399. * @vsi: the VSI being configured
  10400. *
  10401. * Returns 0 on success, negative value on failure
  10402. **/
  10403. static int i40e_config_netdev(struct i40e_vsi *vsi)
  10404. {
  10405. struct i40e_pf *pf = vsi->back;
  10406. struct i40e_hw *hw = &pf->hw;
  10407. struct i40e_netdev_priv *np;
  10408. struct net_device *netdev;
  10409. u8 broadcast[ETH_ALEN];
  10410. u8 mac_addr[ETH_ALEN];
  10411. int etherdev_size;
  10412. netdev_features_t hw_enc_features;
  10413. netdev_features_t hw_features;
  10414. etherdev_size = sizeof(struct i40e_netdev_priv);
  10415. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  10416. if (!netdev)
  10417. return -ENOMEM;
  10418. vsi->netdev = netdev;
  10419. np = netdev_priv(netdev);
  10420. np->vsi = vsi;
  10421. hw_enc_features = NETIF_F_SG |
  10422. NETIF_F_IP_CSUM |
  10423. NETIF_F_IPV6_CSUM |
  10424. NETIF_F_HIGHDMA |
  10425. NETIF_F_SOFT_FEATURES |
  10426. NETIF_F_TSO |
  10427. NETIF_F_TSO_ECN |
  10428. NETIF_F_TSO6 |
  10429. NETIF_F_GSO_GRE |
  10430. NETIF_F_GSO_GRE_CSUM |
  10431. NETIF_F_GSO_PARTIAL |
  10432. NETIF_F_GSO_UDP_TUNNEL |
  10433. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  10434. NETIF_F_SCTP_CRC |
  10435. NETIF_F_RXHASH |
  10436. NETIF_F_RXCSUM |
  10437. 0;
  10438. if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
  10439. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  10440. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  10441. netdev->hw_enc_features |= hw_enc_features;
  10442. /* record features VLANs can make use of */
  10443. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  10444. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  10445. netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
  10446. hw_features = hw_enc_features |
  10447. NETIF_F_HW_VLAN_CTAG_TX |
  10448. NETIF_F_HW_VLAN_CTAG_RX;
  10449. netdev->hw_features |= hw_features;
  10450. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  10451. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  10452. if (vsi->type == I40E_VSI_MAIN) {
  10453. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  10454. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  10455. /* The following steps are necessary for two reasons. First,
  10456. * some older NVM configurations load a default MAC-VLAN
  10457. * filter that will accept any tagged packet, and we want to
  10458. * replace this with a normal filter. Additionally, it is
  10459. * possible our MAC address was provided by the platform using
  10460. * Open Firmware or similar.
  10461. *
  10462. * Thus, we need to remove the default filter and install one
  10463. * specific to the MAC address.
  10464. */
  10465. i40e_rm_default_mac_filter(vsi, mac_addr);
  10466. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10467. i40e_add_mac_filter(vsi, mac_addr);
  10468. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10469. } else {
  10470. /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
  10471. * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
  10472. * the end, which is 4 bytes long, so force truncation of the
  10473. * original name by IFNAMSIZ - 4
  10474. */
  10475. snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
  10476. IFNAMSIZ - 4,
  10477. pf->vsi[pf->lan_vsi]->netdev->name);
  10478. eth_random_addr(mac_addr);
  10479. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10480. i40e_add_mac_filter(vsi, mac_addr);
  10481. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10482. }
  10483. /* Add the broadcast filter so that we initially will receive
  10484. * broadcast packets. Note that when a new VLAN is first added the
  10485. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  10486. * specific filters as part of transitioning into "vlan" operation.
  10487. * When more VLANs are added, the driver will copy each existing MAC
  10488. * filter and add it for the new VLAN.
  10489. *
  10490. * Broadcast filters are handled specially by
  10491. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  10492. * promiscuous bit instead of adding this directly as a MAC/VLAN
  10493. * filter. The subtask will update the correct broadcast promiscuous
  10494. * bits as VLANs become active or inactive.
  10495. */
  10496. eth_broadcast_addr(broadcast);
  10497. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10498. i40e_add_mac_filter(vsi, broadcast);
  10499. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10500. ether_addr_copy(netdev->dev_addr, mac_addr);
  10501. ether_addr_copy(netdev->perm_addr, mac_addr);
  10502. netdev->priv_flags |= IFF_UNICAST_FLT;
  10503. netdev->priv_flags |= IFF_SUPP_NOFCS;
  10504. /* Setup netdev TC information */
  10505. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  10506. netdev->netdev_ops = &i40e_netdev_ops;
  10507. netdev->watchdog_timeo = 5 * HZ;
  10508. i40e_set_ethtool_ops(netdev);
  10509. /* MTU range: 68 - 9706 */
  10510. netdev->min_mtu = ETH_MIN_MTU;
  10511. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  10512. return 0;
  10513. }
  10514. /**
  10515. * i40e_vsi_delete - Delete a VSI from the switch
  10516. * @vsi: the VSI being removed
  10517. *
  10518. * Returns 0 on success, negative value on failure
  10519. **/
  10520. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  10521. {
  10522. /* remove default VSI is not allowed */
  10523. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  10524. return;
  10525. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  10526. }
  10527. /**
  10528. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  10529. * @vsi: the VSI being queried
  10530. *
  10531. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  10532. **/
  10533. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  10534. {
  10535. struct i40e_veb *veb;
  10536. struct i40e_pf *pf = vsi->back;
  10537. /* Uplink is not a bridge so default to VEB */
  10538. if (vsi->veb_idx == I40E_NO_VEB)
  10539. return 1;
  10540. veb = pf->veb[vsi->veb_idx];
  10541. if (!veb) {
  10542. dev_info(&pf->pdev->dev,
  10543. "There is no veb associated with the bridge\n");
  10544. return -ENOENT;
  10545. }
  10546. /* Uplink is a bridge in VEPA mode */
  10547. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  10548. return 0;
  10549. } else {
  10550. /* Uplink is a bridge in VEB mode */
  10551. return 1;
  10552. }
  10553. /* VEPA is now default bridge, so return 0 */
  10554. return 0;
  10555. }
  10556. /**
  10557. * i40e_add_vsi - Add a VSI to the switch
  10558. * @vsi: the VSI being configured
  10559. *
  10560. * This initializes a VSI context depending on the VSI type to be added and
  10561. * passes it down to the add_vsi aq command.
  10562. **/
  10563. static int i40e_add_vsi(struct i40e_vsi *vsi)
  10564. {
  10565. int ret = -ENODEV;
  10566. struct i40e_pf *pf = vsi->back;
  10567. struct i40e_hw *hw = &pf->hw;
  10568. struct i40e_vsi_context ctxt;
  10569. struct i40e_mac_filter *f;
  10570. struct hlist_node *h;
  10571. int bkt;
  10572. u8 enabled_tc = 0x1; /* TC0 enabled */
  10573. int f_count = 0;
  10574. memset(&ctxt, 0, sizeof(ctxt));
  10575. switch (vsi->type) {
  10576. case I40E_VSI_MAIN:
  10577. /* The PF's main VSI is already setup as part of the
  10578. * device initialization, so we'll not bother with
  10579. * the add_vsi call, but we will retrieve the current
  10580. * VSI context.
  10581. */
  10582. ctxt.seid = pf->main_vsi_seid;
  10583. ctxt.pf_num = pf->hw.pf_id;
  10584. ctxt.vf_num = 0;
  10585. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  10586. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10587. if (ret) {
  10588. dev_info(&pf->pdev->dev,
  10589. "couldn't get PF vsi config, err %s aq_err %s\n",
  10590. i40e_stat_str(&pf->hw, ret),
  10591. i40e_aq_str(&pf->hw,
  10592. pf->hw.aq.asq_last_status));
  10593. return -ENOENT;
  10594. }
  10595. vsi->info = ctxt.info;
  10596. vsi->info.valid_sections = 0;
  10597. vsi->seid = ctxt.seid;
  10598. vsi->id = ctxt.vsi_number;
  10599. enabled_tc = i40e_pf_get_tc_map(pf);
  10600. /* Source pruning is enabled by default, so the flag is
  10601. * negative logic - if it's set, we need to fiddle with
  10602. * the VSI to disable source pruning.
  10603. */
  10604. if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
  10605. memset(&ctxt, 0, sizeof(ctxt));
  10606. ctxt.seid = pf->main_vsi_seid;
  10607. ctxt.pf_num = pf->hw.pf_id;
  10608. ctxt.vf_num = 0;
  10609. ctxt.info.valid_sections |=
  10610. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10611. ctxt.info.switch_id =
  10612. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
  10613. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10614. if (ret) {
  10615. dev_info(&pf->pdev->dev,
  10616. "update vsi failed, err %s aq_err %s\n",
  10617. i40e_stat_str(&pf->hw, ret),
  10618. i40e_aq_str(&pf->hw,
  10619. pf->hw.aq.asq_last_status));
  10620. ret = -ENOENT;
  10621. goto err;
  10622. }
  10623. }
  10624. /* MFP mode setup queue map and update VSI */
  10625. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  10626. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  10627. memset(&ctxt, 0, sizeof(ctxt));
  10628. ctxt.seid = pf->main_vsi_seid;
  10629. ctxt.pf_num = pf->hw.pf_id;
  10630. ctxt.vf_num = 0;
  10631. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  10632. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10633. if (ret) {
  10634. dev_info(&pf->pdev->dev,
  10635. "update vsi failed, err %s aq_err %s\n",
  10636. i40e_stat_str(&pf->hw, ret),
  10637. i40e_aq_str(&pf->hw,
  10638. pf->hw.aq.asq_last_status));
  10639. ret = -ENOENT;
  10640. goto err;
  10641. }
  10642. /* update the local VSI info queue map */
  10643. i40e_vsi_update_queue_map(vsi, &ctxt);
  10644. vsi->info.valid_sections = 0;
  10645. } else {
  10646. /* Default/Main VSI is only enabled for TC0
  10647. * reconfigure it to enable all TCs that are
  10648. * available on the port in SFP mode.
  10649. * For MFP case the iSCSI PF would use this
  10650. * flow to enable LAN+iSCSI TC.
  10651. */
  10652. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  10653. if (ret) {
  10654. /* Single TC condition is not fatal,
  10655. * message and continue
  10656. */
  10657. dev_info(&pf->pdev->dev,
  10658. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  10659. enabled_tc,
  10660. i40e_stat_str(&pf->hw, ret),
  10661. i40e_aq_str(&pf->hw,
  10662. pf->hw.aq.asq_last_status));
  10663. }
  10664. }
  10665. break;
  10666. case I40E_VSI_FDIR:
  10667. ctxt.pf_num = hw->pf_id;
  10668. ctxt.vf_num = 0;
  10669. ctxt.uplink_seid = vsi->uplink_seid;
  10670. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10671. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10672. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  10673. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  10674. ctxt.info.valid_sections |=
  10675. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10676. ctxt.info.switch_id =
  10677. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10678. }
  10679. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10680. break;
  10681. case I40E_VSI_VMDQ2:
  10682. ctxt.pf_num = hw->pf_id;
  10683. ctxt.vf_num = 0;
  10684. ctxt.uplink_seid = vsi->uplink_seid;
  10685. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10686. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  10687. /* This VSI is connected to VEB so the switch_id
  10688. * should be set to zero by default.
  10689. */
  10690. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10691. ctxt.info.valid_sections |=
  10692. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10693. ctxt.info.switch_id =
  10694. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10695. }
  10696. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10697. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10698. break;
  10699. case I40E_VSI_SRIOV:
  10700. ctxt.pf_num = hw->pf_id;
  10701. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  10702. ctxt.uplink_seid = vsi->uplink_seid;
  10703. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10704. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  10705. /* This VSI is connected to VEB so the switch_id
  10706. * should be set to zero by default.
  10707. */
  10708. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10709. ctxt.info.valid_sections |=
  10710. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10711. ctxt.info.switch_id =
  10712. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10713. }
  10714. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  10715. ctxt.info.valid_sections |=
  10716. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  10717. ctxt.info.queueing_opt_flags |=
  10718. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  10719. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  10720. }
  10721. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  10722. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  10723. if (pf->vf[vsi->vf_id].spoofchk) {
  10724. ctxt.info.valid_sections |=
  10725. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  10726. ctxt.info.sec_flags |=
  10727. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  10728. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  10729. }
  10730. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10731. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10732. break;
  10733. case I40E_VSI_IWARP:
  10734. /* send down message to iWARP */
  10735. break;
  10736. default:
  10737. return -ENODEV;
  10738. }
  10739. if (vsi->type != I40E_VSI_MAIN) {
  10740. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  10741. if (ret) {
  10742. dev_info(&vsi->back->pdev->dev,
  10743. "add vsi failed, err %s aq_err %s\n",
  10744. i40e_stat_str(&pf->hw, ret),
  10745. i40e_aq_str(&pf->hw,
  10746. pf->hw.aq.asq_last_status));
  10747. ret = -ENOENT;
  10748. goto err;
  10749. }
  10750. vsi->info = ctxt.info;
  10751. vsi->info.valid_sections = 0;
  10752. vsi->seid = ctxt.seid;
  10753. vsi->id = ctxt.vsi_number;
  10754. }
  10755. vsi->active_filters = 0;
  10756. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  10757. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10758. /* If macvlan filters already exist, force them to get loaded */
  10759. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  10760. f->state = I40E_FILTER_NEW;
  10761. f_count++;
  10762. }
  10763. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10764. if (f_count) {
  10765. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  10766. set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
  10767. }
  10768. /* Update VSI BW information */
  10769. ret = i40e_vsi_get_bw_info(vsi);
  10770. if (ret) {
  10771. dev_info(&pf->pdev->dev,
  10772. "couldn't get vsi bw info, err %s aq_err %s\n",
  10773. i40e_stat_str(&pf->hw, ret),
  10774. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10775. /* VSI is already added so not tearing that up */
  10776. ret = 0;
  10777. }
  10778. err:
  10779. return ret;
  10780. }
  10781. /**
  10782. * i40e_vsi_release - Delete a VSI and free its resources
  10783. * @vsi: the VSI being removed
  10784. *
  10785. * Returns 0 on success or < 0 on error
  10786. **/
  10787. int i40e_vsi_release(struct i40e_vsi *vsi)
  10788. {
  10789. struct i40e_mac_filter *f;
  10790. struct hlist_node *h;
  10791. struct i40e_veb *veb = NULL;
  10792. struct i40e_pf *pf;
  10793. u16 uplink_seid;
  10794. int i, n, bkt;
  10795. pf = vsi->back;
  10796. /* release of a VEB-owner or last VSI is not allowed */
  10797. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  10798. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  10799. vsi->seid, vsi->uplink_seid);
  10800. return -ENODEV;
  10801. }
  10802. if (vsi == pf->vsi[pf->lan_vsi] &&
  10803. !test_bit(__I40E_DOWN, pf->state)) {
  10804. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  10805. return -ENODEV;
  10806. }
  10807. uplink_seid = vsi->uplink_seid;
  10808. if (vsi->type != I40E_VSI_SRIOV) {
  10809. if (vsi->netdev_registered) {
  10810. vsi->netdev_registered = false;
  10811. if (vsi->netdev) {
  10812. /* results in a call to i40e_close() */
  10813. unregister_netdev(vsi->netdev);
  10814. }
  10815. } else {
  10816. i40e_vsi_close(vsi);
  10817. }
  10818. i40e_vsi_disable_irq(vsi);
  10819. }
  10820. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10821. /* clear the sync flag on all filters */
  10822. if (vsi->netdev) {
  10823. __dev_uc_unsync(vsi->netdev, NULL);
  10824. __dev_mc_unsync(vsi->netdev, NULL);
  10825. }
  10826. /* make sure any remaining filters are marked for deletion */
  10827. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  10828. __i40e_del_filter(vsi, f);
  10829. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10830. i40e_sync_vsi_filters(vsi);
  10831. i40e_vsi_delete(vsi);
  10832. i40e_vsi_free_q_vectors(vsi);
  10833. if (vsi->netdev) {
  10834. free_netdev(vsi->netdev);
  10835. vsi->netdev = NULL;
  10836. }
  10837. i40e_vsi_clear_rings(vsi);
  10838. i40e_vsi_clear(vsi);
  10839. /* If this was the last thing on the VEB, except for the
  10840. * controlling VSI, remove the VEB, which puts the controlling
  10841. * VSI onto the next level down in the switch.
  10842. *
  10843. * Well, okay, there's one more exception here: don't remove
  10844. * the orphan VEBs yet. We'll wait for an explicit remove request
  10845. * from up the network stack.
  10846. */
  10847. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  10848. if (pf->vsi[i] &&
  10849. pf->vsi[i]->uplink_seid == uplink_seid &&
  10850. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  10851. n++; /* count the VSIs */
  10852. }
  10853. }
  10854. for (i = 0; i < I40E_MAX_VEB; i++) {
  10855. if (!pf->veb[i])
  10856. continue;
  10857. if (pf->veb[i]->uplink_seid == uplink_seid)
  10858. n++; /* count the VEBs */
  10859. if (pf->veb[i]->seid == uplink_seid)
  10860. veb = pf->veb[i];
  10861. }
  10862. if (n == 0 && veb && veb->uplink_seid != 0)
  10863. i40e_veb_release(veb);
  10864. return 0;
  10865. }
  10866. /**
  10867. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  10868. * @vsi: ptr to the VSI
  10869. *
  10870. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  10871. * corresponding SW VSI structure and initializes num_queue_pairs for the
  10872. * newly allocated VSI.
  10873. *
  10874. * Returns 0 on success or negative on failure
  10875. **/
  10876. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  10877. {
  10878. int ret = -ENOENT;
  10879. struct i40e_pf *pf = vsi->back;
  10880. if (vsi->q_vectors[0]) {
  10881. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  10882. vsi->seid);
  10883. return -EEXIST;
  10884. }
  10885. if (vsi->base_vector) {
  10886. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  10887. vsi->seid, vsi->base_vector);
  10888. return -EEXIST;
  10889. }
  10890. ret = i40e_vsi_alloc_q_vectors(vsi);
  10891. if (ret) {
  10892. dev_info(&pf->pdev->dev,
  10893. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  10894. vsi->num_q_vectors, vsi->seid, ret);
  10895. vsi->num_q_vectors = 0;
  10896. goto vector_setup_out;
  10897. }
  10898. /* In Legacy mode, we do not have to get any other vector since we
  10899. * piggyback on the misc/ICR0 for queue interrupts.
  10900. */
  10901. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  10902. return ret;
  10903. if (vsi->num_q_vectors)
  10904. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  10905. vsi->num_q_vectors, vsi->idx);
  10906. if (vsi->base_vector < 0) {
  10907. dev_info(&pf->pdev->dev,
  10908. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  10909. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  10910. i40e_vsi_free_q_vectors(vsi);
  10911. ret = -ENOENT;
  10912. goto vector_setup_out;
  10913. }
  10914. vector_setup_out:
  10915. return ret;
  10916. }
  10917. /**
  10918. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  10919. * @vsi: pointer to the vsi.
  10920. *
  10921. * This re-allocates a vsi's queue resources.
  10922. *
  10923. * Returns pointer to the successfully allocated and configured VSI sw struct
  10924. * on success, otherwise returns NULL on failure.
  10925. **/
  10926. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  10927. {
  10928. u16 alloc_queue_pairs;
  10929. struct i40e_pf *pf;
  10930. u8 enabled_tc;
  10931. int ret;
  10932. if (!vsi)
  10933. return NULL;
  10934. pf = vsi->back;
  10935. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  10936. i40e_vsi_clear_rings(vsi);
  10937. i40e_vsi_free_arrays(vsi, false);
  10938. i40e_set_num_rings_in_vsi(vsi);
  10939. ret = i40e_vsi_alloc_arrays(vsi, false);
  10940. if (ret)
  10941. goto err_vsi;
  10942. alloc_queue_pairs = vsi->alloc_queue_pairs *
  10943. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  10944. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  10945. if (ret < 0) {
  10946. dev_info(&pf->pdev->dev,
  10947. "failed to get tracking for %d queues for VSI %d err %d\n",
  10948. alloc_queue_pairs, vsi->seid, ret);
  10949. goto err_vsi;
  10950. }
  10951. vsi->base_queue = ret;
  10952. /* Update the FW view of the VSI. Force a reset of TC and queue
  10953. * layout configurations.
  10954. */
  10955. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  10956. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  10957. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  10958. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  10959. if (vsi->type == I40E_VSI_MAIN)
  10960. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  10961. /* assign it some queues */
  10962. ret = i40e_alloc_rings(vsi);
  10963. if (ret)
  10964. goto err_rings;
  10965. /* map all of the rings to the q_vectors */
  10966. i40e_vsi_map_rings_to_vectors(vsi);
  10967. return vsi;
  10968. err_rings:
  10969. i40e_vsi_free_q_vectors(vsi);
  10970. if (vsi->netdev_registered) {
  10971. vsi->netdev_registered = false;
  10972. unregister_netdev(vsi->netdev);
  10973. free_netdev(vsi->netdev);
  10974. vsi->netdev = NULL;
  10975. }
  10976. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  10977. err_vsi:
  10978. i40e_vsi_clear(vsi);
  10979. return NULL;
  10980. }
  10981. /**
  10982. * i40e_vsi_setup - Set up a VSI by a given type
  10983. * @pf: board private structure
  10984. * @type: VSI type
  10985. * @uplink_seid: the switch element to link to
  10986. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  10987. *
  10988. * This allocates the sw VSI structure and its queue resources, then add a VSI
  10989. * to the identified VEB.
  10990. *
  10991. * Returns pointer to the successfully allocated and configure VSI sw struct on
  10992. * success, otherwise returns NULL on failure.
  10993. **/
  10994. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  10995. u16 uplink_seid, u32 param1)
  10996. {
  10997. struct i40e_vsi *vsi = NULL;
  10998. struct i40e_veb *veb = NULL;
  10999. u16 alloc_queue_pairs;
  11000. int ret, i;
  11001. int v_idx;
  11002. /* The requested uplink_seid must be either
  11003. * - the PF's port seid
  11004. * no VEB is needed because this is the PF
  11005. * or this is a Flow Director special case VSI
  11006. * - seid of an existing VEB
  11007. * - seid of a VSI that owns an existing VEB
  11008. * - seid of a VSI that doesn't own a VEB
  11009. * a new VEB is created and the VSI becomes the owner
  11010. * - seid of the PF VSI, which is what creates the first VEB
  11011. * this is a special case of the previous
  11012. *
  11013. * Find which uplink_seid we were given and create a new VEB if needed
  11014. */
  11015. for (i = 0; i < I40E_MAX_VEB; i++) {
  11016. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  11017. veb = pf->veb[i];
  11018. break;
  11019. }
  11020. }
  11021. if (!veb && uplink_seid != pf->mac_seid) {
  11022. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11023. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  11024. vsi = pf->vsi[i];
  11025. break;
  11026. }
  11027. }
  11028. if (!vsi) {
  11029. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  11030. uplink_seid);
  11031. return NULL;
  11032. }
  11033. if (vsi->uplink_seid == pf->mac_seid)
  11034. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  11035. vsi->tc_config.enabled_tc);
  11036. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  11037. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  11038. vsi->tc_config.enabled_tc);
  11039. if (veb) {
  11040. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  11041. dev_info(&vsi->back->pdev->dev,
  11042. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  11043. return NULL;
  11044. }
  11045. /* We come up by default in VEPA mode if SRIOV is not
  11046. * already enabled, in which case we can't force VEPA
  11047. * mode.
  11048. */
  11049. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  11050. veb->bridge_mode = BRIDGE_MODE_VEPA;
  11051. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  11052. }
  11053. i40e_config_bridge_mode(veb);
  11054. }
  11055. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  11056. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  11057. veb = pf->veb[i];
  11058. }
  11059. if (!veb) {
  11060. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  11061. return NULL;
  11062. }
  11063. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11064. uplink_seid = veb->seid;
  11065. }
  11066. /* get vsi sw struct */
  11067. v_idx = i40e_vsi_mem_alloc(pf, type);
  11068. if (v_idx < 0)
  11069. goto err_alloc;
  11070. vsi = pf->vsi[v_idx];
  11071. if (!vsi)
  11072. goto err_alloc;
  11073. vsi->type = type;
  11074. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  11075. if (type == I40E_VSI_MAIN)
  11076. pf->lan_vsi = v_idx;
  11077. else if (type == I40E_VSI_SRIOV)
  11078. vsi->vf_id = param1;
  11079. /* assign it some queues */
  11080. alloc_queue_pairs = vsi->alloc_queue_pairs *
  11081. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  11082. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  11083. if (ret < 0) {
  11084. dev_info(&pf->pdev->dev,
  11085. "failed to get tracking for %d queues for VSI %d err=%d\n",
  11086. alloc_queue_pairs, vsi->seid, ret);
  11087. goto err_vsi;
  11088. }
  11089. vsi->base_queue = ret;
  11090. /* get a VSI from the hardware */
  11091. vsi->uplink_seid = uplink_seid;
  11092. ret = i40e_add_vsi(vsi);
  11093. if (ret)
  11094. goto err_vsi;
  11095. switch (vsi->type) {
  11096. /* setup the netdev if needed */
  11097. case I40E_VSI_MAIN:
  11098. case I40E_VSI_VMDQ2:
  11099. ret = i40e_config_netdev(vsi);
  11100. if (ret)
  11101. goto err_netdev;
  11102. ret = register_netdev(vsi->netdev);
  11103. if (ret)
  11104. goto err_netdev;
  11105. vsi->netdev_registered = true;
  11106. netif_carrier_off(vsi->netdev);
  11107. #ifdef CONFIG_I40E_DCB
  11108. /* Setup DCB netlink interface */
  11109. i40e_dcbnl_setup(vsi);
  11110. #endif /* CONFIG_I40E_DCB */
  11111. /* fall through */
  11112. case I40E_VSI_FDIR:
  11113. /* set up vectors and rings if needed */
  11114. ret = i40e_vsi_setup_vectors(vsi);
  11115. if (ret)
  11116. goto err_msix;
  11117. ret = i40e_alloc_rings(vsi);
  11118. if (ret)
  11119. goto err_rings;
  11120. /* map all of the rings to the q_vectors */
  11121. i40e_vsi_map_rings_to_vectors(vsi);
  11122. i40e_vsi_reset_stats(vsi);
  11123. break;
  11124. default:
  11125. /* no netdev or rings for the other VSI types */
  11126. break;
  11127. }
  11128. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  11129. (vsi->type == I40E_VSI_VMDQ2)) {
  11130. ret = i40e_vsi_config_rss(vsi);
  11131. }
  11132. return vsi;
  11133. err_rings:
  11134. i40e_vsi_free_q_vectors(vsi);
  11135. err_msix:
  11136. if (vsi->netdev_registered) {
  11137. vsi->netdev_registered = false;
  11138. unregister_netdev(vsi->netdev);
  11139. free_netdev(vsi->netdev);
  11140. vsi->netdev = NULL;
  11141. }
  11142. err_netdev:
  11143. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  11144. err_vsi:
  11145. i40e_vsi_clear(vsi);
  11146. err_alloc:
  11147. return NULL;
  11148. }
  11149. /**
  11150. * i40e_veb_get_bw_info - Query VEB BW information
  11151. * @veb: the veb to query
  11152. *
  11153. * Query the Tx scheduler BW configuration data for given VEB
  11154. **/
  11155. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  11156. {
  11157. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  11158. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  11159. struct i40e_pf *pf = veb->pf;
  11160. struct i40e_hw *hw = &pf->hw;
  11161. u32 tc_bw_max;
  11162. int ret = 0;
  11163. int i;
  11164. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  11165. &bw_data, NULL);
  11166. if (ret) {
  11167. dev_info(&pf->pdev->dev,
  11168. "query veb bw config failed, err %s aq_err %s\n",
  11169. i40e_stat_str(&pf->hw, ret),
  11170. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11171. goto out;
  11172. }
  11173. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  11174. &ets_data, NULL);
  11175. if (ret) {
  11176. dev_info(&pf->pdev->dev,
  11177. "query veb bw ets config failed, err %s aq_err %s\n",
  11178. i40e_stat_str(&pf->hw, ret),
  11179. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11180. goto out;
  11181. }
  11182. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  11183. veb->bw_max_quanta = ets_data.tc_bw_max;
  11184. veb->is_abs_credits = bw_data.absolute_credits_enable;
  11185. veb->enabled_tc = ets_data.tc_valid_bits;
  11186. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  11187. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  11188. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  11189. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  11190. veb->bw_tc_limit_credits[i] =
  11191. le16_to_cpu(bw_data.tc_bw_limits[i]);
  11192. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  11193. }
  11194. out:
  11195. return ret;
  11196. }
  11197. /**
  11198. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  11199. * @pf: board private structure
  11200. *
  11201. * On error: returns error code (negative)
  11202. * On success: returns vsi index in PF (positive)
  11203. **/
  11204. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  11205. {
  11206. int ret = -ENOENT;
  11207. struct i40e_veb *veb;
  11208. int i;
  11209. /* Need to protect the allocation of switch elements at the PF level */
  11210. mutex_lock(&pf->switch_mutex);
  11211. /* VEB list may be fragmented if VEB creation/destruction has
  11212. * been happening. We can afford to do a quick scan to look
  11213. * for any free slots in the list.
  11214. *
  11215. * find next empty veb slot, looping back around if necessary
  11216. */
  11217. i = 0;
  11218. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  11219. i++;
  11220. if (i >= I40E_MAX_VEB) {
  11221. ret = -ENOMEM;
  11222. goto err_alloc_veb; /* out of VEB slots! */
  11223. }
  11224. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  11225. if (!veb) {
  11226. ret = -ENOMEM;
  11227. goto err_alloc_veb;
  11228. }
  11229. veb->pf = pf;
  11230. veb->idx = i;
  11231. veb->enabled_tc = 1;
  11232. pf->veb[i] = veb;
  11233. ret = i;
  11234. err_alloc_veb:
  11235. mutex_unlock(&pf->switch_mutex);
  11236. return ret;
  11237. }
  11238. /**
  11239. * i40e_switch_branch_release - Delete a branch of the switch tree
  11240. * @branch: where to start deleting
  11241. *
  11242. * This uses recursion to find the tips of the branch to be
  11243. * removed, deleting until we get back to and can delete this VEB.
  11244. **/
  11245. static void i40e_switch_branch_release(struct i40e_veb *branch)
  11246. {
  11247. struct i40e_pf *pf = branch->pf;
  11248. u16 branch_seid = branch->seid;
  11249. u16 veb_idx = branch->idx;
  11250. int i;
  11251. /* release any VEBs on this VEB - RECURSION */
  11252. for (i = 0; i < I40E_MAX_VEB; i++) {
  11253. if (!pf->veb[i])
  11254. continue;
  11255. if (pf->veb[i]->uplink_seid == branch->seid)
  11256. i40e_switch_branch_release(pf->veb[i]);
  11257. }
  11258. /* Release the VSIs on this VEB, but not the owner VSI.
  11259. *
  11260. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  11261. * the VEB itself, so don't use (*branch) after this loop.
  11262. */
  11263. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11264. if (!pf->vsi[i])
  11265. continue;
  11266. if (pf->vsi[i]->uplink_seid == branch_seid &&
  11267. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  11268. i40e_vsi_release(pf->vsi[i]);
  11269. }
  11270. }
  11271. /* There's one corner case where the VEB might not have been
  11272. * removed, so double check it here and remove it if needed.
  11273. * This case happens if the veb was created from the debugfs
  11274. * commands and no VSIs were added to it.
  11275. */
  11276. if (pf->veb[veb_idx])
  11277. i40e_veb_release(pf->veb[veb_idx]);
  11278. }
  11279. /**
  11280. * i40e_veb_clear - remove veb struct
  11281. * @veb: the veb to remove
  11282. **/
  11283. static void i40e_veb_clear(struct i40e_veb *veb)
  11284. {
  11285. if (!veb)
  11286. return;
  11287. if (veb->pf) {
  11288. struct i40e_pf *pf = veb->pf;
  11289. mutex_lock(&pf->switch_mutex);
  11290. if (pf->veb[veb->idx] == veb)
  11291. pf->veb[veb->idx] = NULL;
  11292. mutex_unlock(&pf->switch_mutex);
  11293. }
  11294. kfree(veb);
  11295. }
  11296. /**
  11297. * i40e_veb_release - Delete a VEB and free its resources
  11298. * @veb: the VEB being removed
  11299. **/
  11300. void i40e_veb_release(struct i40e_veb *veb)
  11301. {
  11302. struct i40e_vsi *vsi = NULL;
  11303. struct i40e_pf *pf;
  11304. int i, n = 0;
  11305. pf = veb->pf;
  11306. /* find the remaining VSI and check for extras */
  11307. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11308. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  11309. n++;
  11310. vsi = pf->vsi[i];
  11311. }
  11312. }
  11313. if (n != 1) {
  11314. dev_info(&pf->pdev->dev,
  11315. "can't remove VEB %d with %d VSIs left\n",
  11316. veb->seid, n);
  11317. return;
  11318. }
  11319. /* move the remaining VSI to uplink veb */
  11320. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  11321. if (veb->uplink_seid) {
  11322. vsi->uplink_seid = veb->uplink_seid;
  11323. if (veb->uplink_seid == pf->mac_seid)
  11324. vsi->veb_idx = I40E_NO_VEB;
  11325. else
  11326. vsi->veb_idx = veb->veb_idx;
  11327. } else {
  11328. /* floating VEB */
  11329. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  11330. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  11331. }
  11332. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11333. i40e_veb_clear(veb);
  11334. }
  11335. /**
  11336. * i40e_add_veb - create the VEB in the switch
  11337. * @veb: the VEB to be instantiated
  11338. * @vsi: the controlling VSI
  11339. **/
  11340. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  11341. {
  11342. struct i40e_pf *pf = veb->pf;
  11343. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  11344. int ret;
  11345. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  11346. veb->enabled_tc, false,
  11347. &veb->seid, enable_stats, NULL);
  11348. /* get a VEB from the hardware */
  11349. if (ret) {
  11350. dev_info(&pf->pdev->dev,
  11351. "couldn't add VEB, err %s aq_err %s\n",
  11352. i40e_stat_str(&pf->hw, ret),
  11353. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11354. return -EPERM;
  11355. }
  11356. /* get statistics counter */
  11357. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  11358. &veb->stats_idx, NULL, NULL, NULL);
  11359. if (ret) {
  11360. dev_info(&pf->pdev->dev,
  11361. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  11362. i40e_stat_str(&pf->hw, ret),
  11363. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11364. return -EPERM;
  11365. }
  11366. ret = i40e_veb_get_bw_info(veb);
  11367. if (ret) {
  11368. dev_info(&pf->pdev->dev,
  11369. "couldn't get VEB bw info, err %s aq_err %s\n",
  11370. i40e_stat_str(&pf->hw, ret),
  11371. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11372. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11373. return -ENOENT;
  11374. }
  11375. vsi->uplink_seid = veb->seid;
  11376. vsi->veb_idx = veb->idx;
  11377. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11378. return 0;
  11379. }
  11380. /**
  11381. * i40e_veb_setup - Set up a VEB
  11382. * @pf: board private structure
  11383. * @flags: VEB setup flags
  11384. * @uplink_seid: the switch element to link to
  11385. * @vsi_seid: the initial VSI seid
  11386. * @enabled_tc: Enabled TC bit-map
  11387. *
  11388. * This allocates the sw VEB structure and links it into the switch
  11389. * It is possible and legal for this to be a duplicate of an already
  11390. * existing VEB. It is also possible for both uplink and vsi seids
  11391. * to be zero, in order to create a floating VEB.
  11392. *
  11393. * Returns pointer to the successfully allocated VEB sw struct on
  11394. * success, otherwise returns NULL on failure.
  11395. **/
  11396. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  11397. u16 uplink_seid, u16 vsi_seid,
  11398. u8 enabled_tc)
  11399. {
  11400. struct i40e_veb *veb, *uplink_veb = NULL;
  11401. int vsi_idx, veb_idx;
  11402. int ret;
  11403. /* if one seid is 0, the other must be 0 to create a floating relay */
  11404. if ((uplink_seid == 0 || vsi_seid == 0) &&
  11405. (uplink_seid + vsi_seid != 0)) {
  11406. dev_info(&pf->pdev->dev,
  11407. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  11408. uplink_seid, vsi_seid);
  11409. return NULL;
  11410. }
  11411. /* make sure there is such a vsi and uplink */
  11412. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  11413. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  11414. break;
  11415. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  11416. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  11417. vsi_seid);
  11418. return NULL;
  11419. }
  11420. if (uplink_seid && uplink_seid != pf->mac_seid) {
  11421. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  11422. if (pf->veb[veb_idx] &&
  11423. pf->veb[veb_idx]->seid == uplink_seid) {
  11424. uplink_veb = pf->veb[veb_idx];
  11425. break;
  11426. }
  11427. }
  11428. if (!uplink_veb) {
  11429. dev_info(&pf->pdev->dev,
  11430. "uplink seid %d not found\n", uplink_seid);
  11431. return NULL;
  11432. }
  11433. }
  11434. /* get veb sw struct */
  11435. veb_idx = i40e_veb_mem_alloc(pf);
  11436. if (veb_idx < 0)
  11437. goto err_alloc;
  11438. veb = pf->veb[veb_idx];
  11439. veb->flags = flags;
  11440. veb->uplink_seid = uplink_seid;
  11441. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  11442. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  11443. /* create the VEB in the switch */
  11444. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  11445. if (ret)
  11446. goto err_veb;
  11447. if (vsi_idx == pf->lan_vsi)
  11448. pf->lan_veb = veb->idx;
  11449. return veb;
  11450. err_veb:
  11451. i40e_veb_clear(veb);
  11452. err_alloc:
  11453. return NULL;
  11454. }
  11455. /**
  11456. * i40e_setup_pf_switch_element - set PF vars based on switch type
  11457. * @pf: board private structure
  11458. * @ele: element we are building info from
  11459. * @num_reported: total number of elements
  11460. * @printconfig: should we print the contents
  11461. *
  11462. * helper function to assist in extracting a few useful SEID values.
  11463. **/
  11464. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  11465. struct i40e_aqc_switch_config_element_resp *ele,
  11466. u16 num_reported, bool printconfig)
  11467. {
  11468. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  11469. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  11470. u8 element_type = ele->element_type;
  11471. u16 seid = le16_to_cpu(ele->seid);
  11472. if (printconfig)
  11473. dev_info(&pf->pdev->dev,
  11474. "type=%d seid=%d uplink=%d downlink=%d\n",
  11475. element_type, seid, uplink_seid, downlink_seid);
  11476. switch (element_type) {
  11477. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  11478. pf->mac_seid = seid;
  11479. break;
  11480. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  11481. /* Main VEB? */
  11482. if (uplink_seid != pf->mac_seid)
  11483. break;
  11484. if (pf->lan_veb == I40E_NO_VEB) {
  11485. int v;
  11486. /* find existing or else empty VEB */
  11487. for (v = 0; v < I40E_MAX_VEB; v++) {
  11488. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  11489. pf->lan_veb = v;
  11490. break;
  11491. }
  11492. }
  11493. if (pf->lan_veb == I40E_NO_VEB) {
  11494. v = i40e_veb_mem_alloc(pf);
  11495. if (v < 0)
  11496. break;
  11497. pf->lan_veb = v;
  11498. }
  11499. }
  11500. pf->veb[pf->lan_veb]->seid = seid;
  11501. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  11502. pf->veb[pf->lan_veb]->pf = pf;
  11503. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  11504. break;
  11505. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  11506. if (num_reported != 1)
  11507. break;
  11508. /* This is immediately after a reset so we can assume this is
  11509. * the PF's VSI
  11510. */
  11511. pf->mac_seid = uplink_seid;
  11512. pf->pf_seid = downlink_seid;
  11513. pf->main_vsi_seid = seid;
  11514. if (printconfig)
  11515. dev_info(&pf->pdev->dev,
  11516. "pf_seid=%d main_vsi_seid=%d\n",
  11517. pf->pf_seid, pf->main_vsi_seid);
  11518. break;
  11519. case I40E_SWITCH_ELEMENT_TYPE_PF:
  11520. case I40E_SWITCH_ELEMENT_TYPE_VF:
  11521. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  11522. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  11523. case I40E_SWITCH_ELEMENT_TYPE_PE:
  11524. case I40E_SWITCH_ELEMENT_TYPE_PA:
  11525. /* ignore these for now */
  11526. break;
  11527. default:
  11528. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  11529. element_type, seid);
  11530. break;
  11531. }
  11532. }
  11533. /**
  11534. * i40e_fetch_switch_configuration - Get switch config from firmware
  11535. * @pf: board private structure
  11536. * @printconfig: should we print the contents
  11537. *
  11538. * Get the current switch configuration from the device and
  11539. * extract a few useful SEID values.
  11540. **/
  11541. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  11542. {
  11543. struct i40e_aqc_get_switch_config_resp *sw_config;
  11544. u16 next_seid = 0;
  11545. int ret = 0;
  11546. u8 *aq_buf;
  11547. int i;
  11548. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  11549. if (!aq_buf)
  11550. return -ENOMEM;
  11551. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  11552. do {
  11553. u16 num_reported, num_total;
  11554. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  11555. I40E_AQ_LARGE_BUF,
  11556. &next_seid, NULL);
  11557. if (ret) {
  11558. dev_info(&pf->pdev->dev,
  11559. "get switch config failed err %s aq_err %s\n",
  11560. i40e_stat_str(&pf->hw, ret),
  11561. i40e_aq_str(&pf->hw,
  11562. pf->hw.aq.asq_last_status));
  11563. kfree(aq_buf);
  11564. return -ENOENT;
  11565. }
  11566. num_reported = le16_to_cpu(sw_config->header.num_reported);
  11567. num_total = le16_to_cpu(sw_config->header.num_total);
  11568. if (printconfig)
  11569. dev_info(&pf->pdev->dev,
  11570. "header: %d reported %d total\n",
  11571. num_reported, num_total);
  11572. for (i = 0; i < num_reported; i++) {
  11573. struct i40e_aqc_switch_config_element_resp *ele =
  11574. &sw_config->element[i];
  11575. i40e_setup_pf_switch_element(pf, ele, num_reported,
  11576. printconfig);
  11577. }
  11578. } while (next_seid != 0);
  11579. kfree(aq_buf);
  11580. return ret;
  11581. }
  11582. /**
  11583. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  11584. * @pf: board private structure
  11585. * @reinit: if the Main VSI needs to re-initialized.
  11586. *
  11587. * Returns 0 on success, negative value on failure
  11588. **/
  11589. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  11590. {
  11591. u16 flags = 0;
  11592. int ret;
  11593. /* find out what's out there already */
  11594. ret = i40e_fetch_switch_configuration(pf, false);
  11595. if (ret) {
  11596. dev_info(&pf->pdev->dev,
  11597. "couldn't fetch switch config, err %s aq_err %s\n",
  11598. i40e_stat_str(&pf->hw, ret),
  11599. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11600. return ret;
  11601. }
  11602. i40e_pf_reset_stats(pf);
  11603. /* set the switch config bit for the whole device to
  11604. * support limited promisc or true promisc
  11605. * when user requests promisc. The default is limited
  11606. * promisc.
  11607. */
  11608. if ((pf->hw.pf_id == 0) &&
  11609. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
  11610. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11611. pf->last_sw_conf_flags = flags;
  11612. }
  11613. if (pf->hw.pf_id == 0) {
  11614. u16 valid_flags;
  11615. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11616. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
  11617. NULL);
  11618. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  11619. dev_info(&pf->pdev->dev,
  11620. "couldn't set switch config bits, err %s aq_err %s\n",
  11621. i40e_stat_str(&pf->hw, ret),
  11622. i40e_aq_str(&pf->hw,
  11623. pf->hw.aq.asq_last_status));
  11624. /* not a fatal problem, just keep going */
  11625. }
  11626. pf->last_sw_conf_valid_flags = valid_flags;
  11627. }
  11628. /* first time setup */
  11629. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  11630. struct i40e_vsi *vsi = NULL;
  11631. u16 uplink_seid;
  11632. /* Set up the PF VSI associated with the PF's main VSI
  11633. * that is already in the HW switch
  11634. */
  11635. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  11636. uplink_seid = pf->veb[pf->lan_veb]->seid;
  11637. else
  11638. uplink_seid = pf->mac_seid;
  11639. if (pf->lan_vsi == I40E_NO_VSI)
  11640. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  11641. else if (reinit)
  11642. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  11643. if (!vsi) {
  11644. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  11645. i40e_cloud_filter_exit(pf);
  11646. i40e_fdir_teardown(pf);
  11647. return -EAGAIN;
  11648. }
  11649. } else {
  11650. /* force a reset of TC and queue layout configurations */
  11651. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  11652. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  11653. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  11654. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  11655. }
  11656. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  11657. i40e_fdir_sb_setup(pf);
  11658. /* Setup static PF queue filter control settings */
  11659. ret = i40e_setup_pf_filter_control(pf);
  11660. if (ret) {
  11661. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  11662. ret);
  11663. /* Failure here should not stop continuing other steps */
  11664. }
  11665. /* enable RSS in the HW, even for only one queue, as the stack can use
  11666. * the hash
  11667. */
  11668. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  11669. i40e_pf_config_rss(pf);
  11670. /* fill in link information and enable LSE reporting */
  11671. i40e_link_event(pf);
  11672. /* Initialize user-specific link properties */
  11673. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  11674. I40E_AQ_AN_COMPLETED) ? true : false);
  11675. i40e_ptp_init(pf);
  11676. /* repopulate tunnel port filters */
  11677. i40e_sync_udp_filters(pf);
  11678. return ret;
  11679. }
  11680. /**
  11681. * i40e_determine_queue_usage - Work out queue distribution
  11682. * @pf: board private structure
  11683. **/
  11684. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  11685. {
  11686. int queues_left;
  11687. int q_max;
  11688. pf->num_lan_qps = 0;
  11689. /* Find the max queues to be put into basic use. We'll always be
  11690. * using TC0, whether or not DCB is running, and TC0 will get the
  11691. * big RSS set.
  11692. */
  11693. queues_left = pf->hw.func_caps.num_tx_qp;
  11694. if ((queues_left == 1) ||
  11695. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  11696. /* one qp for PF, no queues for anything else */
  11697. queues_left = 0;
  11698. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11699. /* make sure all the fancies are disabled */
  11700. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11701. I40E_FLAG_IWARP_ENABLED |
  11702. I40E_FLAG_FD_SB_ENABLED |
  11703. I40E_FLAG_FD_ATR_ENABLED |
  11704. I40E_FLAG_DCB_CAPABLE |
  11705. I40E_FLAG_DCB_ENABLED |
  11706. I40E_FLAG_SRIOV_ENABLED |
  11707. I40E_FLAG_VMDQ_ENABLED);
  11708. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11709. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  11710. I40E_FLAG_FD_SB_ENABLED |
  11711. I40E_FLAG_FD_ATR_ENABLED |
  11712. I40E_FLAG_DCB_CAPABLE))) {
  11713. /* one qp for PF */
  11714. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11715. queues_left -= pf->num_lan_qps;
  11716. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11717. I40E_FLAG_IWARP_ENABLED |
  11718. I40E_FLAG_FD_SB_ENABLED |
  11719. I40E_FLAG_FD_ATR_ENABLED |
  11720. I40E_FLAG_DCB_ENABLED |
  11721. I40E_FLAG_VMDQ_ENABLED);
  11722. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11723. } else {
  11724. /* Not enough queues for all TCs */
  11725. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  11726. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  11727. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  11728. I40E_FLAG_DCB_ENABLED);
  11729. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  11730. }
  11731. /* limit lan qps to the smaller of qps, cpus or msix */
  11732. q_max = max_t(int, pf->rss_size_max, num_online_cpus());
  11733. q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
  11734. q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
  11735. pf->num_lan_qps = q_max;
  11736. queues_left -= pf->num_lan_qps;
  11737. }
  11738. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  11739. if (queues_left > 1) {
  11740. queues_left -= 1; /* save 1 queue for FD */
  11741. } else {
  11742. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  11743. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11744. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  11745. }
  11746. }
  11747. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  11748. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  11749. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  11750. (queues_left / pf->num_vf_qps));
  11751. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  11752. }
  11753. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  11754. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  11755. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  11756. (queues_left / pf->num_vmdq_qps));
  11757. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  11758. }
  11759. pf->queues_left = queues_left;
  11760. dev_dbg(&pf->pdev->dev,
  11761. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  11762. pf->hw.func_caps.num_tx_qp,
  11763. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  11764. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  11765. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  11766. queues_left);
  11767. }
  11768. /**
  11769. * i40e_setup_pf_filter_control - Setup PF static filter control
  11770. * @pf: PF to be setup
  11771. *
  11772. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  11773. * settings. If PE/FCoE are enabled then it will also set the per PF
  11774. * based filter sizes required for them. It also enables Flow director,
  11775. * ethertype and macvlan type filter settings for the pf.
  11776. *
  11777. * Returns 0 on success, negative on failure
  11778. **/
  11779. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  11780. {
  11781. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  11782. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  11783. /* Flow Director is enabled */
  11784. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  11785. settings->enable_fdir = true;
  11786. /* Ethtype and MACVLAN filters enabled for PF */
  11787. settings->enable_ethtype = true;
  11788. settings->enable_macvlan = true;
  11789. if (i40e_set_filter_control(&pf->hw, settings))
  11790. return -ENOENT;
  11791. return 0;
  11792. }
  11793. #define INFO_STRING_LEN 255
  11794. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  11795. static void i40e_print_features(struct i40e_pf *pf)
  11796. {
  11797. struct i40e_hw *hw = &pf->hw;
  11798. char *buf;
  11799. int i;
  11800. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  11801. if (!buf)
  11802. return;
  11803. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  11804. #ifdef CONFIG_PCI_IOV
  11805. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  11806. #endif
  11807. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  11808. pf->hw.func_caps.num_vsis,
  11809. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  11810. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  11811. i += snprintf(&buf[i], REMAIN(i), " RSS");
  11812. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  11813. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  11814. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  11815. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  11816. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  11817. }
  11818. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  11819. i += snprintf(&buf[i], REMAIN(i), " DCB");
  11820. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  11821. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  11822. if (pf->flags & I40E_FLAG_PTP)
  11823. i += snprintf(&buf[i], REMAIN(i), " PTP");
  11824. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  11825. i += snprintf(&buf[i], REMAIN(i), " VEB");
  11826. else
  11827. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  11828. dev_info(&pf->pdev->dev, "%s\n", buf);
  11829. kfree(buf);
  11830. WARN_ON(i > INFO_STRING_LEN);
  11831. }
  11832. /**
  11833. * i40e_get_platform_mac_addr - get platform-specific MAC address
  11834. * @pdev: PCI device information struct
  11835. * @pf: board private structure
  11836. *
  11837. * Look up the MAC address for the device. First we'll try
  11838. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  11839. * specific fallback. Otherwise, we'll default to the stored value in
  11840. * firmware.
  11841. **/
  11842. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  11843. {
  11844. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  11845. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  11846. }
  11847. /**
  11848. * i40e_probe - Device initialization routine
  11849. * @pdev: PCI device information struct
  11850. * @ent: entry in i40e_pci_tbl
  11851. *
  11852. * i40e_probe initializes a PF identified by a pci_dev structure.
  11853. * The OS initialization, configuring of the PF private structure,
  11854. * and a hardware reset occur.
  11855. *
  11856. * Returns 0 on success, negative on failure
  11857. **/
  11858. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  11859. {
  11860. struct i40e_aq_get_phy_abilities_resp abilities;
  11861. struct i40e_pf *pf;
  11862. struct i40e_hw *hw;
  11863. static u16 pfs_found;
  11864. u16 wol_nvm_bits;
  11865. u16 link_status;
  11866. int err;
  11867. u32 val;
  11868. u32 i;
  11869. u8 set_fc_aq_fail;
  11870. err = pci_enable_device_mem(pdev);
  11871. if (err)
  11872. return err;
  11873. /* set up for high or low dma */
  11874. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  11875. if (err) {
  11876. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  11877. if (err) {
  11878. dev_err(&pdev->dev,
  11879. "DMA configuration failed: 0x%x\n", err);
  11880. goto err_dma;
  11881. }
  11882. }
  11883. /* set up pci connections */
  11884. err = pci_request_mem_regions(pdev, i40e_driver_name);
  11885. if (err) {
  11886. dev_info(&pdev->dev,
  11887. "pci_request_selected_regions failed %d\n", err);
  11888. goto err_pci_reg;
  11889. }
  11890. pci_enable_pcie_error_reporting(pdev);
  11891. pci_set_master(pdev);
  11892. /* Now that we have a PCI connection, we need to do the
  11893. * low level device setup. This is primarily setting up
  11894. * the Admin Queue structures and then querying for the
  11895. * device's current profile information.
  11896. */
  11897. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  11898. if (!pf) {
  11899. err = -ENOMEM;
  11900. goto err_pf_alloc;
  11901. }
  11902. pf->next_vsi = 0;
  11903. pf->pdev = pdev;
  11904. set_bit(__I40E_DOWN, pf->state);
  11905. hw = &pf->hw;
  11906. hw->back = pf;
  11907. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  11908. I40E_MAX_CSR_SPACE);
  11909. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  11910. if (!hw->hw_addr) {
  11911. err = -EIO;
  11912. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  11913. (unsigned int)pci_resource_start(pdev, 0),
  11914. pf->ioremap_len, err);
  11915. goto err_ioremap;
  11916. }
  11917. hw->vendor_id = pdev->vendor;
  11918. hw->device_id = pdev->device;
  11919. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  11920. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  11921. hw->subsystem_device_id = pdev->subsystem_device;
  11922. hw->bus.device = PCI_SLOT(pdev->devfn);
  11923. hw->bus.func = PCI_FUNC(pdev->devfn);
  11924. hw->bus.bus_id = pdev->bus->number;
  11925. pf->instance = pfs_found;
  11926. /* Select something other than the 802.1ad ethertype for the
  11927. * switch to use internally and drop on ingress.
  11928. */
  11929. hw->switch_tag = 0xffff;
  11930. hw->first_tag = ETH_P_8021AD;
  11931. hw->second_tag = ETH_P_8021Q;
  11932. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  11933. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  11934. /* set up the locks for the AQ, do this only once in probe
  11935. * and destroy them only once in remove
  11936. */
  11937. mutex_init(&hw->aq.asq_mutex);
  11938. mutex_init(&hw->aq.arq_mutex);
  11939. pf->msg_enable = netif_msg_init(debug,
  11940. NETIF_MSG_DRV |
  11941. NETIF_MSG_PROBE |
  11942. NETIF_MSG_LINK);
  11943. if (debug < -1)
  11944. pf->hw.debug_mask = debug;
  11945. /* do a special CORER for clearing PXE mode once at init */
  11946. if (hw->revision_id == 0 &&
  11947. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  11948. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  11949. i40e_flush(hw);
  11950. msleep(200);
  11951. pf->corer_count++;
  11952. i40e_clear_pxe_mode(hw);
  11953. }
  11954. /* Reset here to make sure all is clean and to define PF 'n' */
  11955. i40e_clear_hw(hw);
  11956. err = i40e_pf_reset(hw);
  11957. if (err) {
  11958. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  11959. goto err_pf_reset;
  11960. }
  11961. pf->pfr_count++;
  11962. hw->aq.num_arq_entries = I40E_AQ_LEN;
  11963. hw->aq.num_asq_entries = I40E_AQ_LEN;
  11964. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  11965. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  11966. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  11967. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  11968. "%s-%s:misc",
  11969. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  11970. err = i40e_init_shared_code(hw);
  11971. if (err) {
  11972. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  11973. err);
  11974. goto err_pf_reset;
  11975. }
  11976. /* set up a default setting for link flow control */
  11977. pf->hw.fc.requested_mode = I40E_FC_NONE;
  11978. err = i40e_init_adminq(hw);
  11979. if (err) {
  11980. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  11981. dev_info(&pdev->dev,
  11982. "The driver for the device stopped because the NVM image is newer than expected. You must install the most recent version of the network driver.\n");
  11983. else
  11984. dev_info(&pdev->dev,
  11985. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  11986. goto err_pf_reset;
  11987. }
  11988. i40e_get_oem_version(hw);
  11989. /* provide nvm, fw, api versions */
  11990. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  11991. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  11992. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  11993. i40e_nvm_version_str(hw));
  11994. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  11995. hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
  11996. dev_info(&pdev->dev,
  11997. "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
  11998. else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
  11999. dev_info(&pdev->dev,
  12000. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  12001. i40e_verify_eeprom(pf);
  12002. /* Rev 0 hardware was never productized */
  12003. if (hw->revision_id < 1)
  12004. dev_warn(&pdev->dev, "This device is a pre-production adapter/LOM. Please be aware there may be issues with your hardware. If you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n");
  12005. i40e_clear_pxe_mode(hw);
  12006. err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  12007. if (err)
  12008. goto err_adminq_setup;
  12009. err = i40e_sw_init(pf);
  12010. if (err) {
  12011. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  12012. goto err_sw_init;
  12013. }
  12014. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  12015. hw->func_caps.num_rx_qp, 0, 0);
  12016. if (err) {
  12017. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  12018. goto err_init_lan_hmc;
  12019. }
  12020. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  12021. if (err) {
  12022. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  12023. err = -ENOENT;
  12024. goto err_configure_lan_hmc;
  12025. }
  12026. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  12027. * Ignore error return codes because if it was already disabled via
  12028. * hardware settings this will fail
  12029. */
  12030. if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
  12031. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  12032. i40e_aq_stop_lldp(hw, true, NULL);
  12033. }
  12034. /* allow a platform config to override the HW addr */
  12035. i40e_get_platform_mac_addr(pdev, pf);
  12036. if (!is_valid_ether_addr(hw->mac.addr)) {
  12037. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  12038. err = -EIO;
  12039. goto err_mac_addr;
  12040. }
  12041. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  12042. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  12043. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  12044. if (is_valid_ether_addr(hw->mac.port_addr))
  12045. pf->hw_features |= I40E_HW_PORT_ID_VALID;
  12046. pci_set_drvdata(pdev, pf);
  12047. pci_save_state(pdev);
  12048. /* Enable FW to write default DCB config on link-up */
  12049. i40e_aq_set_dcb_parameters(hw, true, NULL);
  12050. #ifdef CONFIG_I40E_DCB
  12051. err = i40e_init_pf_dcb(pf);
  12052. if (err) {
  12053. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  12054. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  12055. /* Continue without DCB enabled */
  12056. }
  12057. #endif /* CONFIG_I40E_DCB */
  12058. /* set up periodic task facility */
  12059. timer_setup(&pf->service_timer, i40e_service_timer, 0);
  12060. pf->service_timer_period = HZ;
  12061. INIT_WORK(&pf->service_task, i40e_service_task);
  12062. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  12063. /* NVM bit on means WoL disabled for the port */
  12064. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  12065. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  12066. pf->wol_en = false;
  12067. else
  12068. pf->wol_en = true;
  12069. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  12070. /* set up the main switch operations */
  12071. i40e_determine_queue_usage(pf);
  12072. err = i40e_init_interrupt_scheme(pf);
  12073. if (err)
  12074. goto err_switch_setup;
  12075. /* The number of VSIs reported by the FW is the minimum guaranteed
  12076. * to us; HW supports far more and we share the remaining pool with
  12077. * the other PFs. We allocate space for more than the guarantee with
  12078. * the understanding that we might not get them all later.
  12079. */
  12080. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  12081. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  12082. else
  12083. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  12084. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  12085. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  12086. GFP_KERNEL);
  12087. if (!pf->vsi) {
  12088. err = -ENOMEM;
  12089. goto err_switch_setup;
  12090. }
  12091. #ifdef CONFIG_PCI_IOV
  12092. /* prep for VF support */
  12093. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12094. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12095. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12096. if (pci_num_vf(pdev))
  12097. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  12098. }
  12099. #endif
  12100. err = i40e_setup_pf_switch(pf, false);
  12101. if (err) {
  12102. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  12103. goto err_vsis;
  12104. }
  12105. INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
  12106. /* Make sure flow control is set according to current settings */
  12107. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  12108. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  12109. dev_dbg(&pf->pdev->dev,
  12110. "Set fc with err %s aq_err %s on get_phy_cap\n",
  12111. i40e_stat_str(hw, err),
  12112. i40e_aq_str(hw, hw->aq.asq_last_status));
  12113. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  12114. dev_dbg(&pf->pdev->dev,
  12115. "Set fc with err %s aq_err %s on set_phy_config\n",
  12116. i40e_stat_str(hw, err),
  12117. i40e_aq_str(hw, hw->aq.asq_last_status));
  12118. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  12119. dev_dbg(&pf->pdev->dev,
  12120. "Set fc with err %s aq_err %s on get_link_info\n",
  12121. i40e_stat_str(hw, err),
  12122. i40e_aq_str(hw, hw->aq.asq_last_status));
  12123. /* if FDIR VSI was set up, start it now */
  12124. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12125. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  12126. i40e_vsi_open(pf->vsi[i]);
  12127. break;
  12128. }
  12129. }
  12130. /* The driver only wants link up/down and module qualification
  12131. * reports from firmware. Note the negative logic.
  12132. */
  12133. err = i40e_aq_set_phy_int_mask(&pf->hw,
  12134. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  12135. I40E_AQ_EVENT_MEDIA_NA |
  12136. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  12137. if (err)
  12138. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  12139. i40e_stat_str(&pf->hw, err),
  12140. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12141. /* Reconfigure hardware for allowing smaller MSS in the case
  12142. * of TSO, so that we avoid the MDD being fired and causing
  12143. * a reset in the case of small MSS+TSO.
  12144. */
  12145. val = rd32(hw, I40E_REG_MSS);
  12146. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  12147. val &= ~I40E_REG_MSS_MIN_MASK;
  12148. val |= I40E_64BYTE_MSS;
  12149. wr32(hw, I40E_REG_MSS, val);
  12150. }
  12151. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  12152. msleep(75);
  12153. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  12154. if (err)
  12155. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  12156. i40e_stat_str(&pf->hw, err),
  12157. i40e_aq_str(&pf->hw,
  12158. pf->hw.aq.asq_last_status));
  12159. }
  12160. /* The main driver is (mostly) up and happy. We need to set this state
  12161. * before setting up the misc vector or we get a race and the vector
  12162. * ends up disabled forever.
  12163. */
  12164. clear_bit(__I40E_DOWN, pf->state);
  12165. /* In case of MSIX we are going to setup the misc vector right here
  12166. * to handle admin queue events etc. In case of legacy and MSI
  12167. * the misc functionality and queue processing is combined in
  12168. * the same vector and that gets setup at open.
  12169. */
  12170. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  12171. err = i40e_setup_misc_vector(pf);
  12172. if (err) {
  12173. dev_info(&pdev->dev,
  12174. "setup of misc vector failed: %d\n", err);
  12175. goto err_vsis;
  12176. }
  12177. }
  12178. #ifdef CONFIG_PCI_IOV
  12179. /* prep for VF support */
  12180. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12181. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12182. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12183. /* disable link interrupts for VFs */
  12184. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  12185. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  12186. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  12187. i40e_flush(hw);
  12188. if (pci_num_vf(pdev)) {
  12189. dev_info(&pdev->dev,
  12190. "Active VFs found, allocating resources.\n");
  12191. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  12192. if (err)
  12193. dev_info(&pdev->dev,
  12194. "Error %d allocating resources for existing VFs\n",
  12195. err);
  12196. }
  12197. }
  12198. #endif /* CONFIG_PCI_IOV */
  12199. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12200. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  12201. pf->num_iwarp_msix,
  12202. I40E_IWARP_IRQ_PILE_ID);
  12203. if (pf->iwarp_base_vector < 0) {
  12204. dev_info(&pdev->dev,
  12205. "failed to get tracking for %d vectors for IWARP err=%d\n",
  12206. pf->num_iwarp_msix, pf->iwarp_base_vector);
  12207. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  12208. }
  12209. }
  12210. i40e_dbg_pf_init(pf);
  12211. /* tell the firmware that we're starting */
  12212. i40e_send_version(pf);
  12213. /* since everything's happy, start the service_task timer */
  12214. mod_timer(&pf->service_timer,
  12215. round_jiffies(jiffies + pf->service_timer_period));
  12216. /* add this PF to client device list and launch a client service task */
  12217. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12218. err = i40e_lan_add_device(pf);
  12219. if (err)
  12220. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  12221. err);
  12222. }
  12223. #define PCI_SPEED_SIZE 8
  12224. #define PCI_WIDTH_SIZE 8
  12225. /* Devices on the IOSF bus do not have this information
  12226. * and will report PCI Gen 1 x 1 by default so don't bother
  12227. * checking them.
  12228. */
  12229. if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
  12230. char speed[PCI_SPEED_SIZE] = "Unknown";
  12231. char width[PCI_WIDTH_SIZE] = "Unknown";
  12232. /* Get the negotiated link width and speed from PCI config
  12233. * space
  12234. */
  12235. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  12236. &link_status);
  12237. i40e_set_pci_config_data(hw, link_status);
  12238. switch (hw->bus.speed) {
  12239. case i40e_bus_speed_8000:
  12240. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  12241. case i40e_bus_speed_5000:
  12242. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  12243. case i40e_bus_speed_2500:
  12244. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  12245. default:
  12246. break;
  12247. }
  12248. switch (hw->bus.width) {
  12249. case i40e_bus_width_pcie_x8:
  12250. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  12251. case i40e_bus_width_pcie_x4:
  12252. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  12253. case i40e_bus_width_pcie_x2:
  12254. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  12255. case i40e_bus_width_pcie_x1:
  12256. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  12257. default:
  12258. break;
  12259. }
  12260. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  12261. speed, width);
  12262. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  12263. hw->bus.speed < i40e_bus_speed_8000) {
  12264. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  12265. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  12266. }
  12267. }
  12268. /* get the requested speeds from the fw */
  12269. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  12270. if (err)
  12271. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  12272. i40e_stat_str(&pf->hw, err),
  12273. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12274. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  12275. /* get the supported phy types from the fw */
  12276. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  12277. if (err)
  12278. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  12279. i40e_stat_str(&pf->hw, err),
  12280. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12281. /* Add a filter to drop all Flow control frames from any VSI from being
  12282. * transmitted. By doing so we stop a malicious VF from sending out
  12283. * PAUSE or PFC frames and potentially controlling traffic for other
  12284. * PF/VF VSIs.
  12285. * The FW can still send Flow control frames if enabled.
  12286. */
  12287. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  12288. pf->main_vsi_seid);
  12289. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  12290. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  12291. pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
  12292. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  12293. pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
  12294. /* print a string summarizing features */
  12295. i40e_print_features(pf);
  12296. return 0;
  12297. /* Unwind what we've done if something failed in the setup */
  12298. err_vsis:
  12299. set_bit(__I40E_DOWN, pf->state);
  12300. i40e_clear_interrupt_scheme(pf);
  12301. kfree(pf->vsi);
  12302. err_switch_setup:
  12303. i40e_reset_interrupt_capability(pf);
  12304. del_timer_sync(&pf->service_timer);
  12305. err_mac_addr:
  12306. err_configure_lan_hmc:
  12307. (void)i40e_shutdown_lan_hmc(hw);
  12308. err_init_lan_hmc:
  12309. kfree(pf->qp_pile);
  12310. err_sw_init:
  12311. err_adminq_setup:
  12312. err_pf_reset:
  12313. iounmap(hw->hw_addr);
  12314. err_ioremap:
  12315. kfree(pf);
  12316. err_pf_alloc:
  12317. pci_disable_pcie_error_reporting(pdev);
  12318. pci_release_mem_regions(pdev);
  12319. err_pci_reg:
  12320. err_dma:
  12321. pci_disable_device(pdev);
  12322. return err;
  12323. }
  12324. /**
  12325. * i40e_remove - Device removal routine
  12326. * @pdev: PCI device information struct
  12327. *
  12328. * i40e_remove is called by the PCI subsystem to alert the driver
  12329. * that is should release a PCI device. This could be caused by a
  12330. * Hot-Plug event, or because the driver is going to be removed from
  12331. * memory.
  12332. **/
  12333. static void i40e_remove(struct pci_dev *pdev)
  12334. {
  12335. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12336. struct i40e_hw *hw = &pf->hw;
  12337. i40e_status ret_code;
  12338. int i;
  12339. i40e_dbg_pf_exit(pf);
  12340. i40e_ptp_stop(pf);
  12341. /* Disable RSS in hw */
  12342. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  12343. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  12344. /* no more scheduling of any task */
  12345. set_bit(__I40E_SUSPENDED, pf->state);
  12346. set_bit(__I40E_DOWN, pf->state);
  12347. if (pf->service_timer.function)
  12348. del_timer_sync(&pf->service_timer);
  12349. if (pf->service_task.func)
  12350. cancel_work_sync(&pf->service_task);
  12351. /* Client close must be called explicitly here because the timer
  12352. * has been stopped.
  12353. */
  12354. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12355. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  12356. i40e_free_vfs(pf);
  12357. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  12358. }
  12359. i40e_fdir_teardown(pf);
  12360. /* If there is a switch structure or any orphans, remove them.
  12361. * This will leave only the PF's VSI remaining.
  12362. */
  12363. for (i = 0; i < I40E_MAX_VEB; i++) {
  12364. if (!pf->veb[i])
  12365. continue;
  12366. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  12367. pf->veb[i]->uplink_seid == 0)
  12368. i40e_switch_branch_release(pf->veb[i]);
  12369. }
  12370. /* Now we can shutdown the PF's VSI, just before we kill
  12371. * adminq and hmc.
  12372. */
  12373. if (pf->vsi[pf->lan_vsi])
  12374. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  12375. i40e_cloud_filter_exit(pf);
  12376. /* remove attached clients */
  12377. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12378. ret_code = i40e_lan_del_device(pf);
  12379. if (ret_code)
  12380. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  12381. ret_code);
  12382. }
  12383. /* shutdown and destroy the HMC */
  12384. if (hw->hmc.hmc_obj) {
  12385. ret_code = i40e_shutdown_lan_hmc(hw);
  12386. if (ret_code)
  12387. dev_warn(&pdev->dev,
  12388. "Failed to destroy the HMC resources: %d\n",
  12389. ret_code);
  12390. }
  12391. /* shutdown the adminq */
  12392. i40e_shutdown_adminq(hw);
  12393. /* destroy the locks only once, here */
  12394. mutex_destroy(&hw->aq.arq_mutex);
  12395. mutex_destroy(&hw->aq.asq_mutex);
  12396. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  12397. i40e_clear_interrupt_scheme(pf);
  12398. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12399. if (pf->vsi[i]) {
  12400. i40e_vsi_clear_rings(pf->vsi[i]);
  12401. i40e_vsi_clear(pf->vsi[i]);
  12402. pf->vsi[i] = NULL;
  12403. }
  12404. }
  12405. for (i = 0; i < I40E_MAX_VEB; i++) {
  12406. kfree(pf->veb[i]);
  12407. pf->veb[i] = NULL;
  12408. }
  12409. kfree(pf->qp_pile);
  12410. kfree(pf->vsi);
  12411. iounmap(hw->hw_addr);
  12412. kfree(pf);
  12413. pci_release_mem_regions(pdev);
  12414. pci_disable_pcie_error_reporting(pdev);
  12415. pci_disable_device(pdev);
  12416. }
  12417. /**
  12418. * i40e_pci_error_detected - warning that something funky happened in PCI land
  12419. * @pdev: PCI device information struct
  12420. * @error: the type of PCI error
  12421. *
  12422. * Called to warn that something happened and the error handling steps
  12423. * are in progress. Allows the driver to quiesce things, be ready for
  12424. * remediation.
  12425. **/
  12426. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  12427. enum pci_channel_state error)
  12428. {
  12429. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12430. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  12431. if (!pf) {
  12432. dev_info(&pdev->dev,
  12433. "Cannot recover - error happened during device probe\n");
  12434. return PCI_ERS_RESULT_DISCONNECT;
  12435. }
  12436. /* shutdown all operations */
  12437. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12438. i40e_prep_for_reset(pf, false);
  12439. /* Request a slot reset */
  12440. return PCI_ERS_RESULT_NEED_RESET;
  12441. }
  12442. /**
  12443. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  12444. * @pdev: PCI device information struct
  12445. *
  12446. * Called to find if the driver can work with the device now that
  12447. * the pci slot has been reset. If a basic connection seems good
  12448. * (registers are readable and have sane content) then return a
  12449. * happy little PCI_ERS_RESULT_xxx.
  12450. **/
  12451. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  12452. {
  12453. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12454. pci_ers_result_t result;
  12455. int err;
  12456. u32 reg;
  12457. dev_dbg(&pdev->dev, "%s\n", __func__);
  12458. if (pci_enable_device_mem(pdev)) {
  12459. dev_info(&pdev->dev,
  12460. "Cannot re-enable PCI device after reset.\n");
  12461. result = PCI_ERS_RESULT_DISCONNECT;
  12462. } else {
  12463. pci_set_master(pdev);
  12464. pci_restore_state(pdev);
  12465. pci_save_state(pdev);
  12466. pci_wake_from_d3(pdev, false);
  12467. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  12468. if (reg == 0)
  12469. result = PCI_ERS_RESULT_RECOVERED;
  12470. else
  12471. result = PCI_ERS_RESULT_DISCONNECT;
  12472. }
  12473. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  12474. if (err) {
  12475. dev_info(&pdev->dev,
  12476. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  12477. err);
  12478. /* non-fatal, continue */
  12479. }
  12480. return result;
  12481. }
  12482. /**
  12483. * i40e_pci_error_reset_prepare - prepare device driver for pci reset
  12484. * @pdev: PCI device information struct
  12485. */
  12486. static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
  12487. {
  12488. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12489. i40e_prep_for_reset(pf, false);
  12490. }
  12491. /**
  12492. * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
  12493. * @pdev: PCI device information struct
  12494. */
  12495. static void i40e_pci_error_reset_done(struct pci_dev *pdev)
  12496. {
  12497. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12498. i40e_reset_and_rebuild(pf, false, false);
  12499. }
  12500. /**
  12501. * i40e_pci_error_resume - restart operations after PCI error recovery
  12502. * @pdev: PCI device information struct
  12503. *
  12504. * Called to allow the driver to bring things back up after PCI error
  12505. * and/or reset recovery has finished.
  12506. **/
  12507. static void i40e_pci_error_resume(struct pci_dev *pdev)
  12508. {
  12509. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12510. dev_dbg(&pdev->dev, "%s\n", __func__);
  12511. if (test_bit(__I40E_SUSPENDED, pf->state))
  12512. return;
  12513. i40e_handle_reset_warning(pf, false);
  12514. }
  12515. /**
  12516. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  12517. * using the mac_address_write admin q function
  12518. * @pf: pointer to i40e_pf struct
  12519. **/
  12520. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  12521. {
  12522. struct i40e_hw *hw = &pf->hw;
  12523. i40e_status ret;
  12524. u8 mac_addr[6];
  12525. u16 flags = 0;
  12526. /* Get current MAC address in case it's an LAA */
  12527. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  12528. ether_addr_copy(mac_addr,
  12529. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  12530. } else {
  12531. dev_err(&pf->pdev->dev,
  12532. "Failed to retrieve MAC address; using default\n");
  12533. ether_addr_copy(mac_addr, hw->mac.addr);
  12534. }
  12535. /* The FW expects the mac address write cmd to first be called with
  12536. * one of these flags before calling it again with the multicast
  12537. * enable flags.
  12538. */
  12539. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  12540. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  12541. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  12542. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12543. if (ret) {
  12544. dev_err(&pf->pdev->dev,
  12545. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  12546. return;
  12547. }
  12548. flags = I40E_AQC_MC_MAG_EN
  12549. | I40E_AQC_WOL_PRESERVE_ON_PFR
  12550. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  12551. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12552. if (ret)
  12553. dev_err(&pf->pdev->dev,
  12554. "Failed to enable Multicast Magic Packet wake up\n");
  12555. }
  12556. /**
  12557. * i40e_shutdown - PCI callback for shutting down
  12558. * @pdev: PCI device information struct
  12559. **/
  12560. static void i40e_shutdown(struct pci_dev *pdev)
  12561. {
  12562. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12563. struct i40e_hw *hw = &pf->hw;
  12564. set_bit(__I40E_SUSPENDED, pf->state);
  12565. set_bit(__I40E_DOWN, pf->state);
  12566. del_timer_sync(&pf->service_timer);
  12567. cancel_work_sync(&pf->service_task);
  12568. i40e_cloud_filter_exit(pf);
  12569. i40e_fdir_teardown(pf);
  12570. /* Client close must be called explicitly here because the timer
  12571. * has been stopped.
  12572. */
  12573. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12574. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12575. i40e_enable_mc_magic_wake(pf);
  12576. i40e_prep_for_reset(pf, false);
  12577. wr32(hw, I40E_PFPM_APM,
  12578. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12579. wr32(hw, I40E_PFPM_WUFC,
  12580. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12581. i40e_clear_interrupt_scheme(pf);
  12582. if (system_state == SYSTEM_POWER_OFF) {
  12583. pci_wake_from_d3(pdev, pf->wol_en);
  12584. pci_set_power_state(pdev, PCI_D3hot);
  12585. }
  12586. }
  12587. /**
  12588. * i40e_suspend - PM callback for moving to D3
  12589. * @dev: generic device information structure
  12590. **/
  12591. static int __maybe_unused i40e_suspend(struct device *dev)
  12592. {
  12593. struct pci_dev *pdev = to_pci_dev(dev);
  12594. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12595. struct i40e_hw *hw = &pf->hw;
  12596. /* If we're already suspended, then there is nothing to do */
  12597. if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
  12598. return 0;
  12599. set_bit(__I40E_DOWN, pf->state);
  12600. /* Ensure service task will not be running */
  12601. del_timer_sync(&pf->service_timer);
  12602. cancel_work_sync(&pf->service_task);
  12603. /* Client close must be called explicitly here because the timer
  12604. * has been stopped.
  12605. */
  12606. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12607. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12608. i40e_enable_mc_magic_wake(pf);
  12609. /* Since we're going to destroy queues during the
  12610. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  12611. * whole section
  12612. */
  12613. rtnl_lock();
  12614. i40e_prep_for_reset(pf, true);
  12615. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12616. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12617. /* Clear the interrupt scheme and release our IRQs so that the system
  12618. * can safely hibernate even when there are a large number of CPUs.
  12619. * Otherwise hibernation might fail when mapping all the vectors back
  12620. * to CPU0.
  12621. */
  12622. i40e_clear_interrupt_scheme(pf);
  12623. rtnl_unlock();
  12624. return 0;
  12625. }
  12626. /**
  12627. * i40e_resume - PM callback for waking up from D3
  12628. * @dev: generic device information structure
  12629. **/
  12630. static int __maybe_unused i40e_resume(struct device *dev)
  12631. {
  12632. struct pci_dev *pdev = to_pci_dev(dev);
  12633. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12634. int err;
  12635. /* If we're not suspended, then there is nothing to do */
  12636. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12637. return 0;
  12638. /* We need to hold the RTNL lock prior to restoring interrupt schemes,
  12639. * since we're going to be restoring queues
  12640. */
  12641. rtnl_lock();
  12642. /* We cleared the interrupt scheme when we suspended, so we need to
  12643. * restore it now to resume device functionality.
  12644. */
  12645. err = i40e_restore_interrupt_scheme(pf);
  12646. if (err) {
  12647. dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
  12648. err);
  12649. }
  12650. clear_bit(__I40E_DOWN, pf->state);
  12651. i40e_reset_and_rebuild(pf, false, true);
  12652. rtnl_unlock();
  12653. /* Clear suspended state last after everything is recovered */
  12654. clear_bit(__I40E_SUSPENDED, pf->state);
  12655. /* Restart the service task */
  12656. mod_timer(&pf->service_timer,
  12657. round_jiffies(jiffies + pf->service_timer_period));
  12658. return 0;
  12659. }
  12660. static const struct pci_error_handlers i40e_err_handler = {
  12661. .error_detected = i40e_pci_error_detected,
  12662. .slot_reset = i40e_pci_error_slot_reset,
  12663. .reset_prepare = i40e_pci_error_reset_prepare,
  12664. .reset_done = i40e_pci_error_reset_done,
  12665. .resume = i40e_pci_error_resume,
  12666. };
  12667. static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
  12668. static struct pci_driver i40e_driver = {
  12669. .name = i40e_driver_name,
  12670. .id_table = i40e_pci_tbl,
  12671. .probe = i40e_probe,
  12672. .remove = i40e_remove,
  12673. .driver = {
  12674. .pm = &i40e_pm_ops,
  12675. },
  12676. .shutdown = i40e_shutdown,
  12677. .err_handler = &i40e_err_handler,
  12678. .sriov_configure = i40e_pci_sriov_configure,
  12679. };
  12680. /**
  12681. * i40e_init_module - Driver registration routine
  12682. *
  12683. * i40e_init_module is the first routine called when the driver is
  12684. * loaded. All it does is register with the PCI subsystem.
  12685. **/
  12686. static int __init i40e_init_module(void)
  12687. {
  12688. pr_info("%s: %s - version %s\n", i40e_driver_name,
  12689. i40e_driver_string, i40e_driver_version_str);
  12690. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  12691. /* There is no need to throttle the number of active tasks because
  12692. * each device limits its own task using a state bit for scheduling
  12693. * the service task, and the device tasks do not interfere with each
  12694. * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
  12695. * since we need to be able to guarantee forward progress even under
  12696. * memory pressure.
  12697. */
  12698. i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
  12699. if (!i40e_wq) {
  12700. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  12701. return -ENOMEM;
  12702. }
  12703. i40e_dbg_init();
  12704. return pci_register_driver(&i40e_driver);
  12705. }
  12706. module_init(i40e_init_module);
  12707. /**
  12708. * i40e_exit_module - Driver exit cleanup routine
  12709. *
  12710. * i40e_exit_module is called just before the driver is removed
  12711. * from memory.
  12712. **/
  12713. static void __exit i40e_exit_module(void)
  12714. {
  12715. pci_unregister_driver(&i40e_driver);
  12716. destroy_workqueue(i40e_wq);
  12717. i40e_dbg_exit();
  12718. }
  12719. module_exit(i40e_exit_module);