i40e_main.c 402 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606
  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. /* don't kick off another recovery if one is already pending */
  295. if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
  296. return;
  297. if (tx_ring) {
  298. head = i40e_get_head(tx_ring);
  299. /* Read interrupt register */
  300. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  301. val = rd32(&pf->hw,
  302. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  303. tx_ring->vsi->base_vector - 1));
  304. else
  305. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  306. 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",
  307. vsi->seid, hung_queue, tx_ring->next_to_clean,
  308. head, tx_ring->next_to_use,
  309. readl(tx_ring->tail), val);
  310. }
  311. pf->tx_timeout_last_recovery = jiffies;
  312. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  313. pf->tx_timeout_recovery_level, hung_queue);
  314. switch (pf->tx_timeout_recovery_level) {
  315. case 1:
  316. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  317. break;
  318. case 2:
  319. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  320. break;
  321. case 3:
  322. set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  323. break;
  324. default:
  325. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  326. break;
  327. }
  328. i40e_service_event_schedule(pf);
  329. pf->tx_timeout_recovery_level++;
  330. }
  331. /**
  332. * i40e_get_vsi_stats_struct - Get System Network Statistics
  333. * @vsi: the VSI we care about
  334. *
  335. * Returns the address of the device statistics structure.
  336. * The statistics are actually updated from the service task.
  337. **/
  338. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  339. {
  340. return &vsi->net_stats;
  341. }
  342. /**
  343. * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
  344. * @ring: Tx ring to get statistics from
  345. * @stats: statistics entry to be updated
  346. **/
  347. static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
  348. struct rtnl_link_stats64 *stats)
  349. {
  350. u64 bytes, packets;
  351. unsigned int start;
  352. do {
  353. start = u64_stats_fetch_begin_irq(&ring->syncp);
  354. packets = ring->stats.packets;
  355. bytes = ring->stats.bytes;
  356. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  357. stats->tx_packets += packets;
  358. stats->tx_bytes += bytes;
  359. }
  360. /**
  361. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  362. * @netdev: network interface device structure
  363. * @stats: data structure to store statistics
  364. *
  365. * Returns the address of the device statistics structure.
  366. * The statistics are actually updated from the service task.
  367. **/
  368. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  369. struct rtnl_link_stats64 *stats)
  370. {
  371. struct i40e_netdev_priv *np = netdev_priv(netdev);
  372. struct i40e_vsi *vsi = np->vsi;
  373. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  374. struct i40e_ring *ring;
  375. int i;
  376. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  377. return;
  378. if (!vsi->tx_rings)
  379. return;
  380. rcu_read_lock();
  381. for (i = 0; i < vsi->num_queue_pairs; i++) {
  382. u64 bytes, packets;
  383. unsigned int start;
  384. ring = READ_ONCE(vsi->tx_rings[i]);
  385. if (!ring)
  386. continue;
  387. i40e_get_netdev_stats_struct_tx(ring, stats);
  388. if (i40e_enabled_xdp_vsi(vsi)) {
  389. ring++;
  390. i40e_get_netdev_stats_struct_tx(ring, stats);
  391. }
  392. ring++;
  393. do {
  394. start = u64_stats_fetch_begin_irq(&ring->syncp);
  395. packets = ring->stats.packets;
  396. bytes = ring->stats.bytes;
  397. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  398. stats->rx_packets += packets;
  399. stats->rx_bytes += bytes;
  400. }
  401. rcu_read_unlock();
  402. /* following stats updated by i40e_watchdog_subtask() */
  403. stats->multicast = vsi_stats->multicast;
  404. stats->tx_errors = vsi_stats->tx_errors;
  405. stats->tx_dropped = vsi_stats->tx_dropped;
  406. stats->rx_errors = vsi_stats->rx_errors;
  407. stats->rx_dropped = vsi_stats->rx_dropped;
  408. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  409. stats->rx_length_errors = vsi_stats->rx_length_errors;
  410. }
  411. /**
  412. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  413. * @vsi: the VSI to have its stats reset
  414. **/
  415. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  416. {
  417. struct rtnl_link_stats64 *ns;
  418. int i;
  419. if (!vsi)
  420. return;
  421. ns = i40e_get_vsi_stats_struct(vsi);
  422. memset(ns, 0, sizeof(*ns));
  423. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  424. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  425. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  426. if (vsi->rx_rings && vsi->rx_rings[0]) {
  427. for (i = 0; i < vsi->num_queue_pairs; i++) {
  428. memset(&vsi->rx_rings[i]->stats, 0,
  429. sizeof(vsi->rx_rings[i]->stats));
  430. memset(&vsi->rx_rings[i]->rx_stats, 0,
  431. sizeof(vsi->rx_rings[i]->rx_stats));
  432. memset(&vsi->tx_rings[i]->stats, 0,
  433. sizeof(vsi->tx_rings[i]->stats));
  434. memset(&vsi->tx_rings[i]->tx_stats, 0,
  435. sizeof(vsi->tx_rings[i]->tx_stats));
  436. }
  437. }
  438. vsi->stat_offsets_loaded = false;
  439. }
  440. /**
  441. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  442. * @pf: the PF to be reset
  443. **/
  444. void i40e_pf_reset_stats(struct i40e_pf *pf)
  445. {
  446. int i;
  447. memset(&pf->stats, 0, sizeof(pf->stats));
  448. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  449. pf->stat_offsets_loaded = false;
  450. for (i = 0; i < I40E_MAX_VEB; i++) {
  451. if (pf->veb[i]) {
  452. memset(&pf->veb[i]->stats, 0,
  453. sizeof(pf->veb[i]->stats));
  454. memset(&pf->veb[i]->stats_offsets, 0,
  455. sizeof(pf->veb[i]->stats_offsets));
  456. pf->veb[i]->stat_offsets_loaded = false;
  457. }
  458. }
  459. pf->hw_csum_rx_error = 0;
  460. }
  461. /**
  462. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  463. * @hw: ptr to the hardware info
  464. * @hireg: the high 32 bit reg to read
  465. * @loreg: the low 32 bit reg to read
  466. * @offset_loaded: has the initial offset been loaded yet
  467. * @offset: ptr to current offset value
  468. * @stat: ptr to the stat
  469. *
  470. * Since the device stats are not reset at PFReset, they likely will not
  471. * be zeroed when the driver starts. We'll save the first values read
  472. * and use them as offsets to be subtracted from the raw values in order
  473. * to report stats that count from zero. In the process, we also manage
  474. * the potential roll-over.
  475. **/
  476. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  477. bool offset_loaded, u64 *offset, u64 *stat)
  478. {
  479. u64 new_data;
  480. if (hw->device_id == I40E_DEV_ID_QEMU) {
  481. new_data = rd32(hw, loreg);
  482. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  483. } else {
  484. new_data = rd64(hw, loreg);
  485. }
  486. if (!offset_loaded)
  487. *offset = new_data;
  488. if (likely(new_data >= *offset))
  489. *stat = new_data - *offset;
  490. else
  491. *stat = (new_data + BIT_ULL(48)) - *offset;
  492. *stat &= 0xFFFFFFFFFFFFULL;
  493. }
  494. /**
  495. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  496. * @hw: ptr to the hardware info
  497. * @reg: the hw reg to read
  498. * @offset_loaded: has the initial offset been loaded yet
  499. * @offset: ptr to current offset value
  500. * @stat: ptr to the stat
  501. **/
  502. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  503. bool offset_loaded, u64 *offset, u64 *stat)
  504. {
  505. u32 new_data;
  506. new_data = rd32(hw, reg);
  507. if (!offset_loaded)
  508. *offset = new_data;
  509. if (likely(new_data >= *offset))
  510. *stat = (u32)(new_data - *offset);
  511. else
  512. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  513. }
  514. /**
  515. * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
  516. * @hw: ptr to the hardware info
  517. * @reg: the hw reg to read and clear
  518. * @stat: ptr to the stat
  519. **/
  520. static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
  521. {
  522. u32 new_data = rd32(hw, reg);
  523. wr32(hw, reg, 1); /* must write a nonzero value to clear register */
  524. *stat += new_data;
  525. }
  526. /**
  527. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  528. * @vsi: the VSI to be updated
  529. **/
  530. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  531. {
  532. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  533. struct i40e_pf *pf = vsi->back;
  534. struct i40e_hw *hw = &pf->hw;
  535. struct i40e_eth_stats *oes;
  536. struct i40e_eth_stats *es; /* device's eth stats */
  537. es = &vsi->eth_stats;
  538. oes = &vsi->eth_stats_offsets;
  539. /* Gather up the stats that the hw collects */
  540. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  541. vsi->stat_offsets_loaded,
  542. &oes->tx_errors, &es->tx_errors);
  543. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  544. vsi->stat_offsets_loaded,
  545. &oes->rx_discards, &es->rx_discards);
  546. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  547. vsi->stat_offsets_loaded,
  548. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  549. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  550. vsi->stat_offsets_loaded,
  551. &oes->tx_errors, &es->tx_errors);
  552. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  553. I40E_GLV_GORCL(stat_idx),
  554. vsi->stat_offsets_loaded,
  555. &oes->rx_bytes, &es->rx_bytes);
  556. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  557. I40E_GLV_UPRCL(stat_idx),
  558. vsi->stat_offsets_loaded,
  559. &oes->rx_unicast, &es->rx_unicast);
  560. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  561. I40E_GLV_MPRCL(stat_idx),
  562. vsi->stat_offsets_loaded,
  563. &oes->rx_multicast, &es->rx_multicast);
  564. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  565. I40E_GLV_BPRCL(stat_idx),
  566. vsi->stat_offsets_loaded,
  567. &oes->rx_broadcast, &es->rx_broadcast);
  568. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  569. I40E_GLV_GOTCL(stat_idx),
  570. vsi->stat_offsets_loaded,
  571. &oes->tx_bytes, &es->tx_bytes);
  572. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  573. I40E_GLV_UPTCL(stat_idx),
  574. vsi->stat_offsets_loaded,
  575. &oes->tx_unicast, &es->tx_unicast);
  576. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  577. I40E_GLV_MPTCL(stat_idx),
  578. vsi->stat_offsets_loaded,
  579. &oes->tx_multicast, &es->tx_multicast);
  580. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  581. I40E_GLV_BPTCL(stat_idx),
  582. vsi->stat_offsets_loaded,
  583. &oes->tx_broadcast, &es->tx_broadcast);
  584. vsi->stat_offsets_loaded = true;
  585. }
  586. /**
  587. * i40e_update_veb_stats - Update Switch component statistics
  588. * @veb: the VEB being updated
  589. **/
  590. static void i40e_update_veb_stats(struct i40e_veb *veb)
  591. {
  592. struct i40e_pf *pf = veb->pf;
  593. struct i40e_hw *hw = &pf->hw;
  594. struct i40e_eth_stats *oes;
  595. struct i40e_eth_stats *es; /* device's eth stats */
  596. struct i40e_veb_tc_stats *veb_oes;
  597. struct i40e_veb_tc_stats *veb_es;
  598. int i, idx = 0;
  599. idx = veb->stats_idx;
  600. es = &veb->stats;
  601. oes = &veb->stats_offsets;
  602. veb_es = &veb->tc_stats;
  603. veb_oes = &veb->tc_stats_offsets;
  604. /* Gather up the stats that the hw collects */
  605. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  606. veb->stat_offsets_loaded,
  607. &oes->tx_discards, &es->tx_discards);
  608. if (hw->revision_id > 0)
  609. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  610. veb->stat_offsets_loaded,
  611. &oes->rx_unknown_protocol,
  612. &es->rx_unknown_protocol);
  613. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  614. veb->stat_offsets_loaded,
  615. &oes->rx_bytes, &es->rx_bytes);
  616. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  617. veb->stat_offsets_loaded,
  618. &oes->rx_unicast, &es->rx_unicast);
  619. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  620. veb->stat_offsets_loaded,
  621. &oes->rx_multicast, &es->rx_multicast);
  622. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  623. veb->stat_offsets_loaded,
  624. &oes->rx_broadcast, &es->rx_broadcast);
  625. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  626. veb->stat_offsets_loaded,
  627. &oes->tx_bytes, &es->tx_bytes);
  628. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  629. veb->stat_offsets_loaded,
  630. &oes->tx_unicast, &es->tx_unicast);
  631. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  632. veb->stat_offsets_loaded,
  633. &oes->tx_multicast, &es->tx_multicast);
  634. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  635. veb->stat_offsets_loaded,
  636. &oes->tx_broadcast, &es->tx_broadcast);
  637. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  638. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  639. I40E_GLVEBTC_RPCL(i, idx),
  640. veb->stat_offsets_loaded,
  641. &veb_oes->tc_rx_packets[i],
  642. &veb_es->tc_rx_packets[i]);
  643. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  644. I40E_GLVEBTC_RBCL(i, idx),
  645. veb->stat_offsets_loaded,
  646. &veb_oes->tc_rx_bytes[i],
  647. &veb_es->tc_rx_bytes[i]);
  648. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  649. I40E_GLVEBTC_TPCL(i, idx),
  650. veb->stat_offsets_loaded,
  651. &veb_oes->tc_tx_packets[i],
  652. &veb_es->tc_tx_packets[i]);
  653. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  654. I40E_GLVEBTC_TBCL(i, idx),
  655. veb->stat_offsets_loaded,
  656. &veb_oes->tc_tx_bytes[i],
  657. &veb_es->tc_tx_bytes[i]);
  658. }
  659. veb->stat_offsets_loaded = true;
  660. }
  661. /**
  662. * i40e_update_vsi_stats - Update the vsi statistics counters.
  663. * @vsi: the VSI to be updated
  664. *
  665. * There are a few instances where we store the same stat in a
  666. * couple of different structs. This is partly because we have
  667. * the netdev stats that need to be filled out, which is slightly
  668. * different from the "eth_stats" defined by the chip and used in
  669. * VF communications. We sort it out here.
  670. **/
  671. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  672. {
  673. struct i40e_pf *pf = vsi->back;
  674. struct rtnl_link_stats64 *ons;
  675. struct rtnl_link_stats64 *ns; /* netdev stats */
  676. struct i40e_eth_stats *oes;
  677. struct i40e_eth_stats *es; /* device's eth stats */
  678. u32 tx_restart, tx_busy;
  679. struct i40e_ring *p;
  680. u32 rx_page, rx_buf;
  681. u64 bytes, packets;
  682. unsigned int start;
  683. u64 tx_linearize;
  684. u64 tx_force_wb;
  685. u64 rx_p, rx_b;
  686. u64 tx_p, tx_b;
  687. u16 q;
  688. if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  689. test_bit(__I40E_CONFIG_BUSY, pf->state))
  690. return;
  691. ns = i40e_get_vsi_stats_struct(vsi);
  692. ons = &vsi->net_stats_offsets;
  693. es = &vsi->eth_stats;
  694. oes = &vsi->eth_stats_offsets;
  695. /* Gather up the netdev and vsi stats that the driver collects
  696. * on the fly during packet processing
  697. */
  698. rx_b = rx_p = 0;
  699. tx_b = tx_p = 0;
  700. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  701. rx_page = 0;
  702. rx_buf = 0;
  703. rcu_read_lock();
  704. for (q = 0; q < vsi->num_queue_pairs; q++) {
  705. /* locate Tx ring */
  706. p = READ_ONCE(vsi->tx_rings[q]);
  707. do {
  708. start = u64_stats_fetch_begin_irq(&p->syncp);
  709. packets = p->stats.packets;
  710. bytes = p->stats.bytes;
  711. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  712. tx_b += bytes;
  713. tx_p += packets;
  714. tx_restart += p->tx_stats.restart_queue;
  715. tx_busy += p->tx_stats.tx_busy;
  716. tx_linearize += p->tx_stats.tx_linearize;
  717. tx_force_wb += p->tx_stats.tx_force_wb;
  718. /* Rx queue is part of the same block as Tx queue */
  719. p = &p[1];
  720. do {
  721. start = u64_stats_fetch_begin_irq(&p->syncp);
  722. packets = p->stats.packets;
  723. bytes = p->stats.bytes;
  724. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  725. rx_b += bytes;
  726. rx_p += packets;
  727. rx_buf += p->rx_stats.alloc_buff_failed;
  728. rx_page += p->rx_stats.alloc_page_failed;
  729. }
  730. rcu_read_unlock();
  731. vsi->tx_restart = tx_restart;
  732. vsi->tx_busy = tx_busy;
  733. vsi->tx_linearize = tx_linearize;
  734. vsi->tx_force_wb = tx_force_wb;
  735. vsi->rx_page_failed = rx_page;
  736. vsi->rx_buf_failed = rx_buf;
  737. ns->rx_packets = rx_p;
  738. ns->rx_bytes = rx_b;
  739. ns->tx_packets = tx_p;
  740. ns->tx_bytes = tx_b;
  741. /* update netdev stats from eth stats */
  742. i40e_update_eth_stats(vsi);
  743. ons->tx_errors = oes->tx_errors;
  744. ns->tx_errors = es->tx_errors;
  745. ons->multicast = oes->rx_multicast;
  746. ns->multicast = es->rx_multicast;
  747. ons->rx_dropped = oes->rx_discards;
  748. ns->rx_dropped = es->rx_discards;
  749. ons->tx_dropped = oes->tx_discards;
  750. ns->tx_dropped = es->tx_discards;
  751. /* pull in a couple PF stats if this is the main vsi */
  752. if (vsi == pf->vsi[pf->lan_vsi]) {
  753. ns->rx_crc_errors = pf->stats.crc_errors;
  754. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  755. ns->rx_length_errors = pf->stats.rx_length_errors;
  756. }
  757. }
  758. /**
  759. * i40e_update_pf_stats - Update the PF statistics counters.
  760. * @pf: the PF to be updated
  761. **/
  762. static void i40e_update_pf_stats(struct i40e_pf *pf)
  763. {
  764. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  765. struct i40e_hw_port_stats *nsd = &pf->stats;
  766. struct i40e_hw *hw = &pf->hw;
  767. u32 val;
  768. int i;
  769. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  770. I40E_GLPRT_GORCL(hw->port),
  771. pf->stat_offsets_loaded,
  772. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  773. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  774. I40E_GLPRT_GOTCL(hw->port),
  775. pf->stat_offsets_loaded,
  776. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  777. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  778. pf->stat_offsets_loaded,
  779. &osd->eth.rx_discards,
  780. &nsd->eth.rx_discards);
  781. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  782. I40E_GLPRT_UPRCL(hw->port),
  783. pf->stat_offsets_loaded,
  784. &osd->eth.rx_unicast,
  785. &nsd->eth.rx_unicast);
  786. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  787. I40E_GLPRT_MPRCL(hw->port),
  788. pf->stat_offsets_loaded,
  789. &osd->eth.rx_multicast,
  790. &nsd->eth.rx_multicast);
  791. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  792. I40E_GLPRT_BPRCL(hw->port),
  793. pf->stat_offsets_loaded,
  794. &osd->eth.rx_broadcast,
  795. &nsd->eth.rx_broadcast);
  796. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  797. I40E_GLPRT_UPTCL(hw->port),
  798. pf->stat_offsets_loaded,
  799. &osd->eth.tx_unicast,
  800. &nsd->eth.tx_unicast);
  801. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  802. I40E_GLPRT_MPTCL(hw->port),
  803. pf->stat_offsets_loaded,
  804. &osd->eth.tx_multicast,
  805. &nsd->eth.tx_multicast);
  806. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  807. I40E_GLPRT_BPTCL(hw->port),
  808. pf->stat_offsets_loaded,
  809. &osd->eth.tx_broadcast,
  810. &nsd->eth.tx_broadcast);
  811. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  812. pf->stat_offsets_loaded,
  813. &osd->tx_dropped_link_down,
  814. &nsd->tx_dropped_link_down);
  815. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  816. pf->stat_offsets_loaded,
  817. &osd->crc_errors, &nsd->crc_errors);
  818. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  819. pf->stat_offsets_loaded,
  820. &osd->illegal_bytes, &nsd->illegal_bytes);
  821. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  822. pf->stat_offsets_loaded,
  823. &osd->mac_local_faults,
  824. &nsd->mac_local_faults);
  825. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  826. pf->stat_offsets_loaded,
  827. &osd->mac_remote_faults,
  828. &nsd->mac_remote_faults);
  829. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  830. pf->stat_offsets_loaded,
  831. &osd->rx_length_errors,
  832. &nsd->rx_length_errors);
  833. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  834. pf->stat_offsets_loaded,
  835. &osd->link_xon_rx, &nsd->link_xon_rx);
  836. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  837. pf->stat_offsets_loaded,
  838. &osd->link_xon_tx, &nsd->link_xon_tx);
  839. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  840. pf->stat_offsets_loaded,
  841. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  842. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  843. pf->stat_offsets_loaded,
  844. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  845. for (i = 0; i < 8; i++) {
  846. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  847. pf->stat_offsets_loaded,
  848. &osd->priority_xoff_rx[i],
  849. &nsd->priority_xoff_rx[i]);
  850. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  851. pf->stat_offsets_loaded,
  852. &osd->priority_xon_rx[i],
  853. &nsd->priority_xon_rx[i]);
  854. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  855. pf->stat_offsets_loaded,
  856. &osd->priority_xon_tx[i],
  857. &nsd->priority_xon_tx[i]);
  858. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  859. pf->stat_offsets_loaded,
  860. &osd->priority_xoff_tx[i],
  861. &nsd->priority_xoff_tx[i]);
  862. i40e_stat_update32(hw,
  863. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  864. pf->stat_offsets_loaded,
  865. &osd->priority_xon_2_xoff[i],
  866. &nsd->priority_xon_2_xoff[i]);
  867. }
  868. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  869. I40E_GLPRT_PRC64L(hw->port),
  870. pf->stat_offsets_loaded,
  871. &osd->rx_size_64, &nsd->rx_size_64);
  872. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  873. I40E_GLPRT_PRC127L(hw->port),
  874. pf->stat_offsets_loaded,
  875. &osd->rx_size_127, &nsd->rx_size_127);
  876. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  877. I40E_GLPRT_PRC255L(hw->port),
  878. pf->stat_offsets_loaded,
  879. &osd->rx_size_255, &nsd->rx_size_255);
  880. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  881. I40E_GLPRT_PRC511L(hw->port),
  882. pf->stat_offsets_loaded,
  883. &osd->rx_size_511, &nsd->rx_size_511);
  884. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  885. I40E_GLPRT_PRC1023L(hw->port),
  886. pf->stat_offsets_loaded,
  887. &osd->rx_size_1023, &nsd->rx_size_1023);
  888. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  889. I40E_GLPRT_PRC1522L(hw->port),
  890. pf->stat_offsets_loaded,
  891. &osd->rx_size_1522, &nsd->rx_size_1522);
  892. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  893. I40E_GLPRT_PRC9522L(hw->port),
  894. pf->stat_offsets_loaded,
  895. &osd->rx_size_big, &nsd->rx_size_big);
  896. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  897. I40E_GLPRT_PTC64L(hw->port),
  898. pf->stat_offsets_loaded,
  899. &osd->tx_size_64, &nsd->tx_size_64);
  900. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  901. I40E_GLPRT_PTC127L(hw->port),
  902. pf->stat_offsets_loaded,
  903. &osd->tx_size_127, &nsd->tx_size_127);
  904. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  905. I40E_GLPRT_PTC255L(hw->port),
  906. pf->stat_offsets_loaded,
  907. &osd->tx_size_255, &nsd->tx_size_255);
  908. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  909. I40E_GLPRT_PTC511L(hw->port),
  910. pf->stat_offsets_loaded,
  911. &osd->tx_size_511, &nsd->tx_size_511);
  912. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  913. I40E_GLPRT_PTC1023L(hw->port),
  914. pf->stat_offsets_loaded,
  915. &osd->tx_size_1023, &nsd->tx_size_1023);
  916. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  917. I40E_GLPRT_PTC1522L(hw->port),
  918. pf->stat_offsets_loaded,
  919. &osd->tx_size_1522, &nsd->tx_size_1522);
  920. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  921. I40E_GLPRT_PTC9522L(hw->port),
  922. pf->stat_offsets_loaded,
  923. &osd->tx_size_big, &nsd->tx_size_big);
  924. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  925. pf->stat_offsets_loaded,
  926. &osd->rx_undersize, &nsd->rx_undersize);
  927. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  928. pf->stat_offsets_loaded,
  929. &osd->rx_fragments, &nsd->rx_fragments);
  930. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  931. pf->stat_offsets_loaded,
  932. &osd->rx_oversize, &nsd->rx_oversize);
  933. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  934. pf->stat_offsets_loaded,
  935. &osd->rx_jabber, &nsd->rx_jabber);
  936. /* FDIR stats */
  937. i40e_stat_update_and_clear32(hw,
  938. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
  939. &nsd->fd_atr_match);
  940. i40e_stat_update_and_clear32(hw,
  941. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
  942. &nsd->fd_sb_match);
  943. i40e_stat_update_and_clear32(hw,
  944. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
  945. &nsd->fd_atr_tunnel_match);
  946. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  947. nsd->tx_lpi_status =
  948. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  949. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  950. nsd->rx_lpi_status =
  951. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  952. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  953. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  954. pf->stat_offsets_loaded,
  955. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  956. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  957. pf->stat_offsets_loaded,
  958. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  959. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  960. !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  961. nsd->fd_sb_status = true;
  962. else
  963. nsd->fd_sb_status = false;
  964. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  965. !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  966. nsd->fd_atr_status = true;
  967. else
  968. nsd->fd_atr_status = false;
  969. pf->stat_offsets_loaded = true;
  970. }
  971. /**
  972. * i40e_update_stats - Update the various statistics counters.
  973. * @vsi: the VSI to be updated
  974. *
  975. * Update the various stats for this VSI and its related entities.
  976. **/
  977. void i40e_update_stats(struct i40e_vsi *vsi)
  978. {
  979. struct i40e_pf *pf = vsi->back;
  980. if (vsi == pf->vsi[pf->lan_vsi])
  981. i40e_update_pf_stats(pf);
  982. i40e_update_vsi_stats(vsi);
  983. }
  984. /**
  985. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  986. * @vsi: the VSI to be searched
  987. * @macaddr: the MAC address
  988. * @vlan: the vlan
  989. *
  990. * Returns ptr to the filter object or NULL
  991. **/
  992. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  993. const u8 *macaddr, s16 vlan)
  994. {
  995. struct i40e_mac_filter *f;
  996. u64 key;
  997. if (!vsi || !macaddr)
  998. return NULL;
  999. key = i40e_addr_to_hkey(macaddr);
  1000. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1001. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  1002. (vlan == f->vlan))
  1003. return f;
  1004. }
  1005. return NULL;
  1006. }
  1007. /**
  1008. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1009. * @vsi: the VSI to be searched
  1010. * @macaddr: the MAC address we are searching for
  1011. *
  1012. * Returns the first filter with the provided MAC address or NULL if
  1013. * MAC address was not found
  1014. **/
  1015. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1016. {
  1017. struct i40e_mac_filter *f;
  1018. u64 key;
  1019. if (!vsi || !macaddr)
  1020. return NULL;
  1021. key = i40e_addr_to_hkey(macaddr);
  1022. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1023. if ((ether_addr_equal(macaddr, f->macaddr)))
  1024. return f;
  1025. }
  1026. return NULL;
  1027. }
  1028. /**
  1029. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1030. * @vsi: the VSI to be searched
  1031. *
  1032. * Returns true if VSI is in vlan mode or false otherwise
  1033. **/
  1034. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1035. {
  1036. /* If we have a PVID, always operate in VLAN mode */
  1037. if (vsi->info.pvid)
  1038. return true;
  1039. /* We need to operate in VLAN mode whenever we have any filters with
  1040. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1041. * time, incurring search cost repeatedly. However, we can notice two
  1042. * things:
  1043. *
  1044. * 1) the only place where we can gain a VLAN filter is in
  1045. * i40e_add_filter.
  1046. *
  1047. * 2) the only place where filters are actually removed is in
  1048. * i40e_sync_filters_subtask.
  1049. *
  1050. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1051. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1052. * we have to perform the full search after deleting filters in
  1053. * i40e_sync_filters_subtask, but we already have to search
  1054. * filters here and can perform the check at the same time. This
  1055. * results in avoiding embedding a loop for VLAN mode inside another
  1056. * loop over all the filters, and should maintain correctness as noted
  1057. * above.
  1058. */
  1059. return vsi->has_vlan_filter;
  1060. }
  1061. /**
  1062. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1063. * @vsi: the VSI to configure
  1064. * @tmp_add_list: list of filters ready to be added
  1065. * @tmp_del_list: list of filters ready to be deleted
  1066. * @vlan_filters: the number of active VLAN filters
  1067. *
  1068. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1069. * behave as expected. If we have any active VLAN filters remaining or about
  1070. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1071. * so that they only match against untagged traffic. If we no longer have any
  1072. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1073. * so that they match against both tagged and untagged traffic. In this way,
  1074. * we ensure that we correctly receive the desired traffic. This ensures that
  1075. * when we have an active VLAN we will receive only untagged traffic and
  1076. * traffic matching active VLANs. If we have no active VLANs then we will
  1077. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1078. *
  1079. * Finally, in a similar fashion, this function also corrects filters when
  1080. * there is an active PVID assigned to this VSI.
  1081. *
  1082. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1083. *
  1084. * This function is only expected to be called from within
  1085. * i40e_sync_vsi_filters.
  1086. *
  1087. * NOTE: This function expects to be called while under the
  1088. * mac_filter_hash_lock
  1089. */
  1090. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1091. struct hlist_head *tmp_add_list,
  1092. struct hlist_head *tmp_del_list,
  1093. int vlan_filters)
  1094. {
  1095. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1096. struct i40e_mac_filter *f, *add_head;
  1097. struct i40e_new_mac_filter *new;
  1098. struct hlist_node *h;
  1099. int bkt, new_vlan;
  1100. /* To determine if a particular filter needs to be replaced we
  1101. * have the three following conditions:
  1102. *
  1103. * a) if we have a PVID assigned, then all filters which are
  1104. * not marked as VLAN=PVID must be replaced with filters that
  1105. * are.
  1106. * b) otherwise, if we have any active VLANS, all filters
  1107. * which are marked as VLAN=-1 must be replaced with
  1108. * filters marked as VLAN=0
  1109. * c) finally, if we do not have any active VLANS, all filters
  1110. * which are marked as VLAN=0 must be replaced with filters
  1111. * marked as VLAN=-1
  1112. */
  1113. /* Update the filters about to be added in place */
  1114. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1115. if (pvid && new->f->vlan != pvid)
  1116. new->f->vlan = pvid;
  1117. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1118. new->f->vlan = 0;
  1119. else if (!vlan_filters && new->f->vlan == 0)
  1120. new->f->vlan = I40E_VLAN_ANY;
  1121. }
  1122. /* Update the remaining active filters */
  1123. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1124. /* Combine the checks for whether a filter needs to be changed
  1125. * and then determine the new VLAN inside the if block, in
  1126. * order to avoid duplicating code for adding the new filter
  1127. * then deleting the old filter.
  1128. */
  1129. if ((pvid && f->vlan != pvid) ||
  1130. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1131. (!vlan_filters && f->vlan == 0)) {
  1132. /* Determine the new vlan we will be adding */
  1133. if (pvid)
  1134. new_vlan = pvid;
  1135. else if (vlan_filters)
  1136. new_vlan = 0;
  1137. else
  1138. new_vlan = I40E_VLAN_ANY;
  1139. /* Create the new filter */
  1140. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1141. if (!add_head)
  1142. return -ENOMEM;
  1143. /* Create a temporary i40e_new_mac_filter */
  1144. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1145. if (!new)
  1146. return -ENOMEM;
  1147. new->f = add_head;
  1148. new->state = add_head->state;
  1149. /* Add the new filter to the tmp list */
  1150. hlist_add_head(&new->hlist, tmp_add_list);
  1151. /* Put the original filter into the delete list */
  1152. f->state = I40E_FILTER_REMOVE;
  1153. hash_del(&f->hlist);
  1154. hlist_add_head(&f->hlist, tmp_del_list);
  1155. }
  1156. }
  1157. vsi->has_vlan_filter = !!vlan_filters;
  1158. return 0;
  1159. }
  1160. /**
  1161. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1162. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1163. * @macaddr: the MAC address
  1164. *
  1165. * Remove whatever filter the firmware set up so the driver can manage
  1166. * its own filtering intelligently.
  1167. **/
  1168. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1169. {
  1170. struct i40e_aqc_remove_macvlan_element_data element;
  1171. struct i40e_pf *pf = vsi->back;
  1172. /* Only appropriate for the PF main VSI */
  1173. if (vsi->type != I40E_VSI_MAIN)
  1174. return;
  1175. memset(&element, 0, sizeof(element));
  1176. ether_addr_copy(element.mac_addr, macaddr);
  1177. element.vlan_tag = 0;
  1178. /* Ignore error returns, some firmware does it this way... */
  1179. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1180. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1181. memset(&element, 0, sizeof(element));
  1182. ether_addr_copy(element.mac_addr, macaddr);
  1183. element.vlan_tag = 0;
  1184. /* ...and some firmware does it this way. */
  1185. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1186. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1187. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1188. }
  1189. /**
  1190. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1191. * @vsi: the VSI to be searched
  1192. * @macaddr: the MAC address
  1193. * @vlan: the vlan
  1194. *
  1195. * Returns ptr to the filter object or NULL when no memory available.
  1196. *
  1197. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1198. * being held.
  1199. **/
  1200. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1201. const u8 *macaddr, s16 vlan)
  1202. {
  1203. struct i40e_mac_filter *f;
  1204. u64 key;
  1205. if (!vsi || !macaddr)
  1206. return NULL;
  1207. f = i40e_find_filter(vsi, macaddr, vlan);
  1208. if (!f) {
  1209. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1210. if (!f)
  1211. return NULL;
  1212. /* Update the boolean indicating if we need to function in
  1213. * VLAN mode.
  1214. */
  1215. if (vlan >= 0)
  1216. vsi->has_vlan_filter = true;
  1217. ether_addr_copy(f->macaddr, macaddr);
  1218. f->vlan = vlan;
  1219. f->state = I40E_FILTER_NEW;
  1220. INIT_HLIST_NODE(&f->hlist);
  1221. key = i40e_addr_to_hkey(macaddr);
  1222. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1223. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1224. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1225. }
  1226. /* If we're asked to add a filter that has been marked for removal, it
  1227. * is safe to simply restore it to active state. __i40e_del_filter
  1228. * will have simply deleted any filters which were previously marked
  1229. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1230. * previously been ACTIVE. Since we haven't yet run the sync filters
  1231. * task, just restore this filter to the ACTIVE state so that the
  1232. * sync task leaves it in place
  1233. */
  1234. if (f->state == I40E_FILTER_REMOVE)
  1235. f->state = I40E_FILTER_ACTIVE;
  1236. return f;
  1237. }
  1238. /**
  1239. * __i40e_del_filter - Remove a specific filter from the VSI
  1240. * @vsi: VSI to remove from
  1241. * @f: the filter to remove from the list
  1242. *
  1243. * This function should be called instead of i40e_del_filter only if you know
  1244. * the exact filter you will remove already, such as via i40e_find_filter or
  1245. * i40e_find_mac.
  1246. *
  1247. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1248. * being held.
  1249. * ANOTHER NOTE: This function MUST be called from within the context of
  1250. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1251. * instead of list_for_each_entry().
  1252. **/
  1253. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1254. {
  1255. if (!f)
  1256. return;
  1257. /* If the filter was never added to firmware then we can just delete it
  1258. * directly and we don't want to set the status to remove or else an
  1259. * admin queue command will unnecessarily fire.
  1260. */
  1261. if ((f->state == I40E_FILTER_FAILED) ||
  1262. (f->state == I40E_FILTER_NEW)) {
  1263. hash_del(&f->hlist);
  1264. kfree(f);
  1265. } else {
  1266. f->state = I40E_FILTER_REMOVE;
  1267. }
  1268. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1269. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1270. }
  1271. /**
  1272. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1273. * @vsi: the VSI to be searched
  1274. * @macaddr: the MAC address
  1275. * @vlan: the VLAN
  1276. *
  1277. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1278. * being held.
  1279. * ANOTHER NOTE: This function MUST be called from within the context of
  1280. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1281. * instead of list_for_each_entry().
  1282. **/
  1283. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1284. {
  1285. struct i40e_mac_filter *f;
  1286. if (!vsi || !macaddr)
  1287. return;
  1288. f = i40e_find_filter(vsi, macaddr, vlan);
  1289. __i40e_del_filter(vsi, f);
  1290. }
  1291. /**
  1292. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1293. * @vsi: the VSI to be searched
  1294. * @macaddr: the mac address to be filtered
  1295. *
  1296. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1297. * go through all the macvlan filters and add a macvlan filter for each
  1298. * unique vlan that already exists. If a PVID has been assigned, instead only
  1299. * add the macaddr to that VLAN.
  1300. *
  1301. * Returns last filter added on success, else NULL
  1302. **/
  1303. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1304. const u8 *macaddr)
  1305. {
  1306. struct i40e_mac_filter *f, *add = NULL;
  1307. struct hlist_node *h;
  1308. int bkt;
  1309. if (vsi->info.pvid)
  1310. return i40e_add_filter(vsi, macaddr,
  1311. le16_to_cpu(vsi->info.pvid));
  1312. if (!i40e_is_vsi_in_vlan(vsi))
  1313. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1314. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1315. if (f->state == I40E_FILTER_REMOVE)
  1316. continue;
  1317. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1318. if (!add)
  1319. return NULL;
  1320. }
  1321. return add;
  1322. }
  1323. /**
  1324. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1325. * @vsi: the VSI to be searched
  1326. * @macaddr: the mac address to be removed
  1327. *
  1328. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1329. * associated with.
  1330. *
  1331. * Returns 0 for success, or error
  1332. **/
  1333. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1334. {
  1335. struct i40e_mac_filter *f;
  1336. struct hlist_node *h;
  1337. bool found = false;
  1338. int bkt;
  1339. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1340. "Missing mac_filter_hash_lock\n");
  1341. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1342. if (ether_addr_equal(macaddr, f->macaddr)) {
  1343. __i40e_del_filter(vsi, f);
  1344. found = true;
  1345. }
  1346. }
  1347. if (found)
  1348. return 0;
  1349. else
  1350. return -ENOENT;
  1351. }
  1352. /**
  1353. * i40e_set_mac - NDO callback to set mac address
  1354. * @netdev: network interface device structure
  1355. * @p: pointer to an address structure
  1356. *
  1357. * Returns 0 on success, negative on failure
  1358. **/
  1359. static int i40e_set_mac(struct net_device *netdev, void *p)
  1360. {
  1361. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1362. struct i40e_vsi *vsi = np->vsi;
  1363. struct i40e_pf *pf = vsi->back;
  1364. struct i40e_hw *hw = &pf->hw;
  1365. struct sockaddr *addr = p;
  1366. if (!is_valid_ether_addr(addr->sa_data))
  1367. return -EADDRNOTAVAIL;
  1368. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1369. netdev_info(netdev, "already using mac address %pM\n",
  1370. addr->sa_data);
  1371. return 0;
  1372. }
  1373. if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
  1374. test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
  1375. return -EADDRNOTAVAIL;
  1376. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1377. netdev_info(netdev, "returning to hw mac address %pM\n",
  1378. hw->mac.addr);
  1379. else
  1380. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1381. /* Copy the address first, so that we avoid a possible race with
  1382. * .set_rx_mode().
  1383. * - Remove old address from MAC filter
  1384. * - Copy new address
  1385. * - Add new address to MAC filter
  1386. */
  1387. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1388. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1389. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1390. i40e_add_mac_filter(vsi, netdev->dev_addr);
  1391. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1392. if (vsi->type == I40E_VSI_MAIN) {
  1393. i40e_status ret;
  1394. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1395. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1396. addr->sa_data, NULL);
  1397. if (ret)
  1398. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1399. i40e_stat_str(hw, ret),
  1400. i40e_aq_str(hw, hw->aq.asq_last_status));
  1401. }
  1402. /* schedule our worker thread which will take care of
  1403. * applying the new filter changes
  1404. */
  1405. i40e_service_event_schedule(vsi->back);
  1406. return 0;
  1407. }
  1408. /**
  1409. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  1410. * @vsi: vsi structure
  1411. * @seed: RSS hash seed
  1412. **/
  1413. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  1414. u8 *lut, u16 lut_size)
  1415. {
  1416. struct i40e_pf *pf = vsi->back;
  1417. struct i40e_hw *hw = &pf->hw;
  1418. int ret = 0;
  1419. if (seed) {
  1420. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  1421. (struct i40e_aqc_get_set_rss_key_data *)seed;
  1422. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  1423. if (ret) {
  1424. dev_info(&pf->pdev->dev,
  1425. "Cannot set RSS key, err %s aq_err %s\n",
  1426. i40e_stat_str(hw, ret),
  1427. i40e_aq_str(hw, hw->aq.asq_last_status));
  1428. return ret;
  1429. }
  1430. }
  1431. if (lut) {
  1432. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  1433. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  1434. if (ret) {
  1435. dev_info(&pf->pdev->dev,
  1436. "Cannot set RSS lut, err %s aq_err %s\n",
  1437. i40e_stat_str(hw, ret),
  1438. i40e_aq_str(hw, hw->aq.asq_last_status));
  1439. return ret;
  1440. }
  1441. }
  1442. return ret;
  1443. }
  1444. /**
  1445. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  1446. * @vsi: VSI structure
  1447. **/
  1448. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  1449. {
  1450. struct i40e_pf *pf = vsi->back;
  1451. u8 seed[I40E_HKEY_ARRAY_SIZE];
  1452. u8 *lut;
  1453. int ret;
  1454. if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
  1455. return 0;
  1456. if (!vsi->rss_size)
  1457. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  1458. vsi->num_queue_pairs);
  1459. if (!vsi->rss_size)
  1460. return -EINVAL;
  1461. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  1462. if (!lut)
  1463. return -ENOMEM;
  1464. /* Use the user configured hash keys and lookup table if there is one,
  1465. * otherwise use default
  1466. */
  1467. if (vsi->rss_lut_user)
  1468. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  1469. else
  1470. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  1471. if (vsi->rss_hkey_user)
  1472. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  1473. else
  1474. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  1475. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  1476. kfree(lut);
  1477. return ret;
  1478. }
  1479. /**
  1480. * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
  1481. * @vsi: the VSI being configured,
  1482. * @ctxt: VSI context structure
  1483. * @enabled_tc: number of traffic classes to enable
  1484. *
  1485. * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
  1486. **/
  1487. static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
  1488. struct i40e_vsi_context *ctxt,
  1489. u8 enabled_tc)
  1490. {
  1491. u16 qcount = 0, max_qcount, qmap, sections = 0;
  1492. int i, override_q, pow, num_qps, ret;
  1493. u8 netdev_tc = 0, offset = 0;
  1494. if (vsi->type != I40E_VSI_MAIN)
  1495. return -EINVAL;
  1496. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1497. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1498. vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
  1499. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1500. num_qps = vsi->mqprio_qopt.qopt.count[0];
  1501. /* find the next higher power-of-2 of num queue pairs */
  1502. pow = ilog2(num_qps);
  1503. if (!is_power_of_2(num_qps))
  1504. pow++;
  1505. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1506. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1507. /* Setup queue offset/count for all TCs for given VSI */
  1508. max_qcount = vsi->mqprio_qopt.qopt.count[0];
  1509. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1510. /* See if the given TC is enabled for the given VSI */
  1511. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1512. offset = vsi->mqprio_qopt.qopt.offset[i];
  1513. qcount = vsi->mqprio_qopt.qopt.count[i];
  1514. if (qcount > max_qcount)
  1515. max_qcount = qcount;
  1516. vsi->tc_config.tc_info[i].qoffset = offset;
  1517. vsi->tc_config.tc_info[i].qcount = qcount;
  1518. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1519. } else {
  1520. /* TC is not enabled so set the offset to
  1521. * default queue and allocate one queue
  1522. * for the given TC.
  1523. */
  1524. vsi->tc_config.tc_info[i].qoffset = 0;
  1525. vsi->tc_config.tc_info[i].qcount = 1;
  1526. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1527. }
  1528. }
  1529. /* Set actual Tx/Rx queue pairs */
  1530. vsi->num_queue_pairs = offset + qcount;
  1531. /* Setup queue TC[0].qmap for given VSI context */
  1532. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  1533. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1534. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1535. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1536. /* Reconfigure RSS for main VSI with max queue count */
  1537. vsi->rss_size = max_qcount;
  1538. ret = i40e_vsi_config_rss(vsi);
  1539. if (ret) {
  1540. dev_info(&vsi->back->pdev->dev,
  1541. "Failed to reconfig rss for num_queues (%u)\n",
  1542. max_qcount);
  1543. return ret;
  1544. }
  1545. vsi->reconfig_rss = true;
  1546. dev_dbg(&vsi->back->pdev->dev,
  1547. "Reconfigured rss with num_queues (%u)\n", max_qcount);
  1548. /* Find queue count available for channel VSIs and starting offset
  1549. * for channel VSIs
  1550. */
  1551. override_q = vsi->mqprio_qopt.qopt.count[0];
  1552. if (override_q && override_q < vsi->num_queue_pairs) {
  1553. vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
  1554. vsi->next_base_queue = override_q;
  1555. }
  1556. return 0;
  1557. }
  1558. /**
  1559. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1560. * @vsi: the VSI being setup
  1561. * @ctxt: VSI context structure
  1562. * @enabled_tc: Enabled TCs bitmap
  1563. * @is_add: True if called before Add VSI
  1564. *
  1565. * Setup VSI queue mapping for enabled traffic classes.
  1566. **/
  1567. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1568. struct i40e_vsi_context *ctxt,
  1569. u8 enabled_tc,
  1570. bool is_add)
  1571. {
  1572. struct i40e_pf *pf = vsi->back;
  1573. u16 sections = 0;
  1574. u8 netdev_tc = 0;
  1575. u16 numtc = 1;
  1576. u16 qcount;
  1577. u8 offset;
  1578. u16 qmap;
  1579. int i;
  1580. u16 num_tc_qps = 0;
  1581. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1582. offset = 0;
  1583. /* Number of queues per enabled TC */
  1584. num_tc_qps = vsi->alloc_queue_pairs;
  1585. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1586. /* Find numtc from enabled TC bitmap */
  1587. for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1588. if (enabled_tc & BIT(i)) /* TC is enabled */
  1589. numtc++;
  1590. }
  1591. if (!numtc) {
  1592. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1593. numtc = 1;
  1594. }
  1595. num_tc_qps = num_tc_qps / numtc;
  1596. num_tc_qps = min_t(int, num_tc_qps,
  1597. i40e_pf_get_max_q_per_tc(pf));
  1598. }
  1599. vsi->tc_config.numtc = numtc;
  1600. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1601. /* Do not allow use more TC queue pairs than MSI-X vectors exist */
  1602. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1603. num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
  1604. /* Setup queue offset/count for all TCs for given VSI */
  1605. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1606. /* See if the given TC is enabled for the given VSI */
  1607. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1608. /* TC is enabled */
  1609. int pow, num_qps;
  1610. switch (vsi->type) {
  1611. case I40E_VSI_MAIN:
  1612. if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
  1613. I40E_FLAG_FD_ATR_ENABLED)) ||
  1614. vsi->tc_config.enabled_tc != 1) {
  1615. qcount = min_t(int, pf->alloc_rss_size,
  1616. num_tc_qps);
  1617. break;
  1618. }
  1619. /* fall through */
  1620. case I40E_VSI_FDIR:
  1621. case I40E_VSI_SRIOV:
  1622. case I40E_VSI_VMDQ2:
  1623. default:
  1624. qcount = num_tc_qps;
  1625. WARN_ON(i != 0);
  1626. break;
  1627. }
  1628. vsi->tc_config.tc_info[i].qoffset = offset;
  1629. vsi->tc_config.tc_info[i].qcount = qcount;
  1630. /* find the next higher power-of-2 of num queue pairs */
  1631. num_qps = qcount;
  1632. pow = 0;
  1633. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1634. pow++;
  1635. num_qps >>= 1;
  1636. }
  1637. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1638. qmap =
  1639. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1640. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1641. offset += qcount;
  1642. } else {
  1643. /* TC is not enabled so set the offset to
  1644. * default queue and allocate one queue
  1645. * for the given TC.
  1646. */
  1647. vsi->tc_config.tc_info[i].qoffset = 0;
  1648. vsi->tc_config.tc_info[i].qcount = 1;
  1649. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1650. qmap = 0;
  1651. }
  1652. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1653. }
  1654. /* Set actual Tx/Rx queue pairs */
  1655. vsi->num_queue_pairs = offset;
  1656. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1657. if (vsi->req_queue_pairs > 0)
  1658. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1659. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1660. vsi->num_queue_pairs = pf->num_lan_msix;
  1661. }
  1662. /* Scheduler section valid can only be set for ADD VSI */
  1663. if (is_add) {
  1664. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1665. ctxt->info.up_enable_bits = enabled_tc;
  1666. }
  1667. if (vsi->type == I40E_VSI_SRIOV) {
  1668. ctxt->info.mapping_flags |=
  1669. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1670. for (i = 0; i < vsi->num_queue_pairs; i++)
  1671. ctxt->info.queue_mapping[i] =
  1672. cpu_to_le16(vsi->base_queue + i);
  1673. } else {
  1674. ctxt->info.mapping_flags |=
  1675. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1676. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1677. }
  1678. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1679. }
  1680. /**
  1681. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1682. * @netdev: the netdevice
  1683. * @addr: address to add
  1684. *
  1685. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1686. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1687. */
  1688. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1689. {
  1690. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1691. struct i40e_vsi *vsi = np->vsi;
  1692. if (i40e_add_mac_filter(vsi, addr))
  1693. return 0;
  1694. else
  1695. return -ENOMEM;
  1696. }
  1697. /**
  1698. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1699. * @netdev: the netdevice
  1700. * @addr: address to add
  1701. *
  1702. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1703. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1704. */
  1705. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1706. {
  1707. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1708. struct i40e_vsi *vsi = np->vsi;
  1709. /* Under some circumstances, we might receive a request to delete
  1710. * our own device address from our uc list. Because we store the
  1711. * device address in the VSI's MAC/VLAN filter list, we need to ignore
  1712. * such requests and not delete our device address from this list.
  1713. */
  1714. if (ether_addr_equal(addr, netdev->dev_addr))
  1715. return 0;
  1716. i40e_del_mac_filter(vsi, addr);
  1717. return 0;
  1718. }
  1719. /**
  1720. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1721. * @netdev: network interface device structure
  1722. **/
  1723. static void i40e_set_rx_mode(struct net_device *netdev)
  1724. {
  1725. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1726. struct i40e_vsi *vsi = np->vsi;
  1727. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1728. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1729. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1730. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1731. /* check for other flag changes */
  1732. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1733. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1734. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1735. }
  1736. }
  1737. /**
  1738. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1739. * @vsi: Pointer to VSI struct
  1740. * @from: Pointer to list which contains MAC filter entries - changes to
  1741. * those entries needs to be undone.
  1742. *
  1743. * MAC filter entries from this list were slated for deletion.
  1744. **/
  1745. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1746. struct hlist_head *from)
  1747. {
  1748. struct i40e_mac_filter *f;
  1749. struct hlist_node *h;
  1750. hlist_for_each_entry_safe(f, h, from, hlist) {
  1751. u64 key = i40e_addr_to_hkey(f->macaddr);
  1752. /* Move the element back into MAC filter list*/
  1753. hlist_del(&f->hlist);
  1754. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1755. }
  1756. }
  1757. /**
  1758. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1759. * @vsi: Pointer to vsi struct
  1760. * @from: Pointer to list which contains MAC filter entries - changes to
  1761. * those entries needs to be undone.
  1762. *
  1763. * MAC filter entries from this list were slated for addition.
  1764. **/
  1765. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1766. struct hlist_head *from)
  1767. {
  1768. struct i40e_new_mac_filter *new;
  1769. struct hlist_node *h;
  1770. hlist_for_each_entry_safe(new, h, from, hlist) {
  1771. /* We can simply free the wrapper structure */
  1772. hlist_del(&new->hlist);
  1773. kfree(new);
  1774. }
  1775. }
  1776. /**
  1777. * i40e_next_entry - Get the next non-broadcast filter from a list
  1778. * @next: pointer to filter in list
  1779. *
  1780. * Returns the next non-broadcast filter in the list. Required so that we
  1781. * ignore broadcast filters within the list, since these are not handled via
  1782. * the normal firmware update path.
  1783. */
  1784. static
  1785. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1786. {
  1787. hlist_for_each_entry_continue(next, hlist) {
  1788. if (!is_broadcast_ether_addr(next->f->macaddr))
  1789. return next;
  1790. }
  1791. return NULL;
  1792. }
  1793. /**
  1794. * i40e_update_filter_state - Update filter state based on return data
  1795. * from firmware
  1796. * @count: Number of filters added
  1797. * @add_list: return data from fw
  1798. * @add_head: pointer to first filter in current batch
  1799. *
  1800. * MAC filter entries from list were slated to be added to device. Returns
  1801. * number of successful filters. Note that 0 does NOT mean success!
  1802. **/
  1803. static int
  1804. i40e_update_filter_state(int count,
  1805. struct i40e_aqc_add_macvlan_element_data *add_list,
  1806. struct i40e_new_mac_filter *add_head)
  1807. {
  1808. int retval = 0;
  1809. int i;
  1810. for (i = 0; i < count; i++) {
  1811. /* Always check status of each filter. We don't need to check
  1812. * the firmware return status because we pre-set the filter
  1813. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1814. * request to the adminq. Thus, if it no longer matches then
  1815. * we know the filter is active.
  1816. */
  1817. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1818. add_head->state = I40E_FILTER_FAILED;
  1819. } else {
  1820. add_head->state = I40E_FILTER_ACTIVE;
  1821. retval++;
  1822. }
  1823. add_head = i40e_next_filter(add_head);
  1824. if (!add_head)
  1825. break;
  1826. }
  1827. return retval;
  1828. }
  1829. /**
  1830. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1831. * @vsi: ptr to the VSI
  1832. * @vsi_name: name to display in messages
  1833. * @list: the list of filters to send to firmware
  1834. * @num_del: the number of filters to delete
  1835. * @retval: Set to -EIO on failure to delete
  1836. *
  1837. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1838. * *retval instead of a return value so that success does not force ret_val to
  1839. * be set to 0. This ensures that a sequence of calls to this function
  1840. * preserve the previous value of *retval on successful delete.
  1841. */
  1842. static
  1843. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1844. struct i40e_aqc_remove_macvlan_element_data *list,
  1845. int num_del, int *retval)
  1846. {
  1847. struct i40e_hw *hw = &vsi->back->hw;
  1848. i40e_status aq_ret;
  1849. int aq_err;
  1850. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1851. aq_err = hw->aq.asq_last_status;
  1852. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1853. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1854. *retval = -EIO;
  1855. dev_info(&vsi->back->pdev->dev,
  1856. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1857. vsi_name, i40e_stat_str(hw, aq_ret),
  1858. i40e_aq_str(hw, aq_err));
  1859. }
  1860. }
  1861. /**
  1862. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1863. * @vsi: ptr to the VSI
  1864. * @vsi_name: name to display in messages
  1865. * @list: the list of filters to send to firmware
  1866. * @add_head: Position in the add hlist
  1867. * @num_add: the number of filters to add
  1868. *
  1869. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1870. * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
  1871. * space for more filters.
  1872. */
  1873. static
  1874. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1875. struct i40e_aqc_add_macvlan_element_data *list,
  1876. struct i40e_new_mac_filter *add_head,
  1877. int num_add)
  1878. {
  1879. struct i40e_hw *hw = &vsi->back->hw;
  1880. int aq_err, fcnt;
  1881. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1882. aq_err = hw->aq.asq_last_status;
  1883. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1884. if (fcnt != num_add) {
  1885. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1886. dev_warn(&vsi->back->pdev->dev,
  1887. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1888. i40e_aq_str(hw, aq_err),
  1889. vsi_name);
  1890. }
  1891. }
  1892. /**
  1893. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1894. * @vsi: pointer to the VSI
  1895. * @vsi_name: the VSI name
  1896. * @f: filter data
  1897. *
  1898. * This function sets or clears the promiscuous broadcast flags for VLAN
  1899. * filters in order to properly receive broadcast frames. Assumes that only
  1900. * broadcast filters are passed.
  1901. *
  1902. * Returns status indicating success or failure;
  1903. **/
  1904. static i40e_status
  1905. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1906. struct i40e_mac_filter *f)
  1907. {
  1908. bool enable = f->state == I40E_FILTER_NEW;
  1909. struct i40e_hw *hw = &vsi->back->hw;
  1910. i40e_status aq_ret;
  1911. if (f->vlan == I40E_VLAN_ANY) {
  1912. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1913. vsi->seid,
  1914. enable,
  1915. NULL);
  1916. } else {
  1917. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1918. vsi->seid,
  1919. enable,
  1920. f->vlan,
  1921. NULL);
  1922. }
  1923. if (aq_ret) {
  1924. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1925. dev_warn(&vsi->back->pdev->dev,
  1926. "Error %s, forcing overflow promiscuous on %s\n",
  1927. i40e_aq_str(hw, hw->aq.asq_last_status),
  1928. vsi_name);
  1929. }
  1930. return aq_ret;
  1931. }
  1932. /**
  1933. * i40e_set_promiscuous - set promiscuous mode
  1934. * @pf: board private structure
  1935. * @promisc: promisc on or off
  1936. *
  1937. * There are different ways of setting promiscuous mode on a PF depending on
  1938. * what state/environment we're in. This identifies and sets it appropriately.
  1939. * Returns 0 on success.
  1940. **/
  1941. static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
  1942. {
  1943. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  1944. struct i40e_hw *hw = &pf->hw;
  1945. i40e_status aq_ret;
  1946. if (vsi->type == I40E_VSI_MAIN &&
  1947. pf->lan_veb != I40E_NO_VEB &&
  1948. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  1949. /* set defport ON for Main VSI instead of true promisc
  1950. * this way we will get all unicast/multicast and VLAN
  1951. * promisc behavior but will not get VF or VMDq traffic
  1952. * replicated on the Main VSI.
  1953. */
  1954. if (promisc)
  1955. aq_ret = i40e_aq_set_default_vsi(hw,
  1956. vsi->seid,
  1957. NULL);
  1958. else
  1959. aq_ret = i40e_aq_clear_default_vsi(hw,
  1960. vsi->seid,
  1961. NULL);
  1962. if (aq_ret) {
  1963. dev_info(&pf->pdev->dev,
  1964. "Set default VSI failed, err %s, aq_err %s\n",
  1965. i40e_stat_str(hw, aq_ret),
  1966. i40e_aq_str(hw, hw->aq.asq_last_status));
  1967. }
  1968. } else {
  1969. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  1970. hw,
  1971. vsi->seid,
  1972. promisc, NULL,
  1973. true);
  1974. if (aq_ret) {
  1975. dev_info(&pf->pdev->dev,
  1976. "set unicast promisc failed, err %s, aq_err %s\n",
  1977. i40e_stat_str(hw, aq_ret),
  1978. i40e_aq_str(hw, hw->aq.asq_last_status));
  1979. }
  1980. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  1981. hw,
  1982. vsi->seid,
  1983. promisc, NULL);
  1984. if (aq_ret) {
  1985. dev_info(&pf->pdev->dev,
  1986. "set multicast promisc failed, err %s, aq_err %s\n",
  1987. i40e_stat_str(hw, aq_ret),
  1988. i40e_aq_str(hw, hw->aq.asq_last_status));
  1989. }
  1990. }
  1991. if (!aq_ret)
  1992. pf->cur_promisc = promisc;
  1993. return aq_ret;
  1994. }
  1995. /**
  1996. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1997. * @vsi: ptr to the VSI
  1998. *
  1999. * Push any outstanding VSI filter changes through the AdminQ.
  2000. *
  2001. * Returns 0 or error value
  2002. **/
  2003. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  2004. {
  2005. struct hlist_head tmp_add_list, tmp_del_list;
  2006. struct i40e_mac_filter *f;
  2007. struct i40e_new_mac_filter *new, *add_head = NULL;
  2008. struct i40e_hw *hw = &vsi->back->hw;
  2009. bool old_overflow, new_overflow;
  2010. unsigned int failed_filters = 0;
  2011. unsigned int vlan_filters = 0;
  2012. char vsi_name[16] = "PF";
  2013. int filter_list_len = 0;
  2014. i40e_status aq_ret = 0;
  2015. u32 changed_flags = 0;
  2016. struct hlist_node *h;
  2017. struct i40e_pf *pf;
  2018. int num_add = 0;
  2019. int num_del = 0;
  2020. int retval = 0;
  2021. u16 cmd_flags;
  2022. int list_size;
  2023. int bkt;
  2024. /* empty array typed pointers, kcalloc later */
  2025. struct i40e_aqc_add_macvlan_element_data *add_list;
  2026. struct i40e_aqc_remove_macvlan_element_data *del_list;
  2027. while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
  2028. usleep_range(1000, 2000);
  2029. pf = vsi->back;
  2030. old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2031. if (vsi->netdev) {
  2032. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  2033. vsi->current_netdev_flags = vsi->netdev->flags;
  2034. }
  2035. INIT_HLIST_HEAD(&tmp_add_list);
  2036. INIT_HLIST_HEAD(&tmp_del_list);
  2037. if (vsi->type == I40E_VSI_SRIOV)
  2038. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  2039. else if (vsi->type != I40E_VSI_MAIN)
  2040. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  2041. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  2042. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  2043. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2044. /* Create a list of filters to delete. */
  2045. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2046. if (f->state == I40E_FILTER_REMOVE) {
  2047. /* Move the element into temporary del_list */
  2048. hash_del(&f->hlist);
  2049. hlist_add_head(&f->hlist, &tmp_del_list);
  2050. /* Avoid counting removed filters */
  2051. continue;
  2052. }
  2053. if (f->state == I40E_FILTER_NEW) {
  2054. /* Create a temporary i40e_new_mac_filter */
  2055. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  2056. if (!new)
  2057. goto err_no_memory_locked;
  2058. /* Store pointer to the real filter */
  2059. new->f = f;
  2060. new->state = f->state;
  2061. /* Add it to the hash list */
  2062. hlist_add_head(&new->hlist, &tmp_add_list);
  2063. }
  2064. /* Count the number of active (current and new) VLAN
  2065. * filters we have now. Does not count filters which
  2066. * are marked for deletion.
  2067. */
  2068. if (f->vlan > 0)
  2069. vlan_filters++;
  2070. }
  2071. retval = i40e_correct_mac_vlan_filters(vsi,
  2072. &tmp_add_list,
  2073. &tmp_del_list,
  2074. vlan_filters);
  2075. if (retval)
  2076. goto err_no_memory_locked;
  2077. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2078. }
  2079. /* Now process 'del_list' outside the lock */
  2080. if (!hlist_empty(&tmp_del_list)) {
  2081. filter_list_len = hw->aq.asq_buf_size /
  2082. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2083. list_size = filter_list_len *
  2084. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2085. del_list = kzalloc(list_size, GFP_ATOMIC);
  2086. if (!del_list)
  2087. goto err_no_memory;
  2088. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  2089. cmd_flags = 0;
  2090. /* handle broadcast filters by updating the broadcast
  2091. * promiscuous flag and release filter list.
  2092. */
  2093. if (is_broadcast_ether_addr(f->macaddr)) {
  2094. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  2095. hlist_del(&f->hlist);
  2096. kfree(f);
  2097. continue;
  2098. }
  2099. /* add to delete list */
  2100. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  2101. if (f->vlan == I40E_VLAN_ANY) {
  2102. del_list[num_del].vlan_tag = 0;
  2103. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  2104. } else {
  2105. del_list[num_del].vlan_tag =
  2106. cpu_to_le16((u16)(f->vlan));
  2107. }
  2108. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  2109. del_list[num_del].flags = cmd_flags;
  2110. num_del++;
  2111. /* flush a full buffer */
  2112. if (num_del == filter_list_len) {
  2113. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2114. num_del, &retval);
  2115. memset(del_list, 0, list_size);
  2116. num_del = 0;
  2117. }
  2118. /* Release memory for MAC filter entries which were
  2119. * synced up with HW.
  2120. */
  2121. hlist_del(&f->hlist);
  2122. kfree(f);
  2123. }
  2124. if (num_del) {
  2125. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2126. num_del, &retval);
  2127. }
  2128. kfree(del_list);
  2129. del_list = NULL;
  2130. }
  2131. if (!hlist_empty(&tmp_add_list)) {
  2132. /* Do all the adds now. */
  2133. filter_list_len = hw->aq.asq_buf_size /
  2134. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2135. list_size = filter_list_len *
  2136. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2137. add_list = kzalloc(list_size, GFP_ATOMIC);
  2138. if (!add_list)
  2139. goto err_no_memory;
  2140. num_add = 0;
  2141. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2142. /* handle broadcast filters by updating the broadcast
  2143. * promiscuous flag instead of adding a MAC filter.
  2144. */
  2145. if (is_broadcast_ether_addr(new->f->macaddr)) {
  2146. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  2147. new->f))
  2148. new->state = I40E_FILTER_FAILED;
  2149. else
  2150. new->state = I40E_FILTER_ACTIVE;
  2151. continue;
  2152. }
  2153. /* add to add array */
  2154. if (num_add == 0)
  2155. add_head = new;
  2156. cmd_flags = 0;
  2157. ether_addr_copy(add_list[num_add].mac_addr,
  2158. new->f->macaddr);
  2159. if (new->f->vlan == I40E_VLAN_ANY) {
  2160. add_list[num_add].vlan_tag = 0;
  2161. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  2162. } else {
  2163. add_list[num_add].vlan_tag =
  2164. cpu_to_le16((u16)(new->f->vlan));
  2165. }
  2166. add_list[num_add].queue_number = 0;
  2167. /* set invalid match method for later detection */
  2168. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  2169. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  2170. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  2171. num_add++;
  2172. /* flush a full buffer */
  2173. if (num_add == filter_list_len) {
  2174. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  2175. add_head, num_add);
  2176. memset(add_list, 0, list_size);
  2177. num_add = 0;
  2178. }
  2179. }
  2180. if (num_add) {
  2181. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  2182. num_add);
  2183. }
  2184. /* Now move all of the filters from the temp add list back to
  2185. * the VSI's list.
  2186. */
  2187. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2188. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2189. /* Only update the state if we're still NEW */
  2190. if (new->f->state == I40E_FILTER_NEW)
  2191. new->f->state = new->state;
  2192. hlist_del(&new->hlist);
  2193. kfree(new);
  2194. }
  2195. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2196. kfree(add_list);
  2197. add_list = NULL;
  2198. }
  2199. /* Determine the number of active and failed filters. */
  2200. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2201. vsi->active_filters = 0;
  2202. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  2203. if (f->state == I40E_FILTER_ACTIVE)
  2204. vsi->active_filters++;
  2205. else if (f->state == I40E_FILTER_FAILED)
  2206. failed_filters++;
  2207. }
  2208. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2209. /* Check if we are able to exit overflow promiscuous mode. We can
  2210. * safely exit if we didn't just enter, we no longer have any failed
  2211. * filters, and we have reduced filters below the threshold value.
  2212. */
  2213. if (old_overflow && !failed_filters &&
  2214. vsi->active_filters < vsi->promisc_threshold) {
  2215. dev_info(&pf->pdev->dev,
  2216. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  2217. vsi_name);
  2218. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2219. vsi->promisc_threshold = 0;
  2220. }
  2221. /* if the VF is not trusted do not do promisc */
  2222. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2223. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2224. goto out;
  2225. }
  2226. new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2227. /* If we are entering overflow promiscuous, we need to calculate a new
  2228. * threshold for when we are safe to exit
  2229. */
  2230. if (!old_overflow && new_overflow)
  2231. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  2232. /* check for changes in promiscuous modes */
  2233. if (changed_flags & IFF_ALLMULTI) {
  2234. bool cur_multipromisc;
  2235. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2236. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2237. vsi->seid,
  2238. cur_multipromisc,
  2239. NULL);
  2240. if (aq_ret) {
  2241. retval = i40e_aq_rc_to_posix(aq_ret,
  2242. hw->aq.asq_last_status);
  2243. dev_info(&pf->pdev->dev,
  2244. "set multi promisc failed on %s, err %s aq_err %s\n",
  2245. vsi_name,
  2246. i40e_stat_str(hw, aq_ret),
  2247. i40e_aq_str(hw, hw->aq.asq_last_status));
  2248. }
  2249. }
  2250. if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
  2251. bool cur_promisc;
  2252. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2253. new_overflow);
  2254. aq_ret = i40e_set_promiscuous(pf, cur_promisc);
  2255. if (aq_ret) {
  2256. retval = i40e_aq_rc_to_posix(aq_ret,
  2257. hw->aq.asq_last_status);
  2258. dev_info(&pf->pdev->dev,
  2259. "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
  2260. cur_promisc ? "on" : "off",
  2261. vsi_name,
  2262. i40e_stat_str(hw, aq_ret),
  2263. i40e_aq_str(hw, hw->aq.asq_last_status));
  2264. }
  2265. }
  2266. out:
  2267. /* if something went wrong then set the changed flag so we try again */
  2268. if (retval)
  2269. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2270. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2271. return retval;
  2272. err_no_memory:
  2273. /* Restore elements on the temporary add and delete lists */
  2274. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2275. err_no_memory_locked:
  2276. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2277. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2278. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2279. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2280. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2281. return -ENOMEM;
  2282. }
  2283. /**
  2284. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2285. * @pf: board private structure
  2286. **/
  2287. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2288. {
  2289. int v;
  2290. if (!pf)
  2291. return;
  2292. if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
  2293. return;
  2294. if (test_and_set_bit(__I40E_VF_DISABLE, pf->state)) {
  2295. set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
  2296. return;
  2297. }
  2298. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2299. if (pf->vsi[v] &&
  2300. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2301. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2302. if (ret) {
  2303. /* come back and try again later */
  2304. set_bit(__I40E_MACVLAN_SYNC_PENDING,
  2305. pf->state);
  2306. break;
  2307. }
  2308. }
  2309. }
  2310. clear_bit(__I40E_VF_DISABLE, pf->state);
  2311. }
  2312. /**
  2313. * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
  2314. * @vsi: the vsi
  2315. **/
  2316. static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
  2317. {
  2318. if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2319. return I40E_RXBUFFER_2048;
  2320. else
  2321. return I40E_RXBUFFER_3072;
  2322. }
  2323. /**
  2324. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2325. * @netdev: network interface device structure
  2326. * @new_mtu: new value for maximum frame size
  2327. *
  2328. * Returns 0 on success, negative on failure
  2329. **/
  2330. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2331. {
  2332. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2333. struct i40e_vsi *vsi = np->vsi;
  2334. struct i40e_pf *pf = vsi->back;
  2335. if (i40e_enabled_xdp_vsi(vsi)) {
  2336. int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  2337. if (frame_size > i40e_max_xdp_frame_size(vsi))
  2338. return -EINVAL;
  2339. }
  2340. netdev_info(netdev, "changing MTU from %d to %d\n",
  2341. netdev->mtu, new_mtu);
  2342. netdev->mtu = new_mtu;
  2343. if (netif_running(netdev))
  2344. i40e_vsi_reinit_locked(vsi);
  2345. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  2346. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  2347. return 0;
  2348. }
  2349. /**
  2350. * i40e_ioctl - Access the hwtstamp interface
  2351. * @netdev: network interface device structure
  2352. * @ifr: interface request data
  2353. * @cmd: ioctl command
  2354. **/
  2355. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2356. {
  2357. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2358. struct i40e_pf *pf = np->vsi->back;
  2359. switch (cmd) {
  2360. case SIOCGHWTSTAMP:
  2361. return i40e_ptp_get_ts_config(pf, ifr);
  2362. case SIOCSHWTSTAMP:
  2363. return i40e_ptp_set_ts_config(pf, ifr);
  2364. default:
  2365. return -EOPNOTSUPP;
  2366. }
  2367. }
  2368. /**
  2369. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2370. * @vsi: the vsi being adjusted
  2371. **/
  2372. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2373. {
  2374. struct i40e_vsi_context ctxt;
  2375. i40e_status ret;
  2376. /* Don't modify stripping options if a port VLAN is active */
  2377. if (vsi->info.pvid)
  2378. return;
  2379. if ((vsi->info.valid_sections &
  2380. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2381. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2382. return; /* already enabled */
  2383. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2384. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2385. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2386. ctxt.seid = vsi->seid;
  2387. ctxt.info = vsi->info;
  2388. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2389. if (ret) {
  2390. dev_info(&vsi->back->pdev->dev,
  2391. "update vlan stripping failed, err %s aq_err %s\n",
  2392. i40e_stat_str(&vsi->back->hw, ret),
  2393. i40e_aq_str(&vsi->back->hw,
  2394. vsi->back->hw.aq.asq_last_status));
  2395. }
  2396. }
  2397. /**
  2398. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2399. * @vsi: the vsi being adjusted
  2400. **/
  2401. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2402. {
  2403. struct i40e_vsi_context ctxt;
  2404. i40e_status ret;
  2405. /* Don't modify stripping options if a port VLAN is active */
  2406. if (vsi->info.pvid)
  2407. return;
  2408. if ((vsi->info.valid_sections &
  2409. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2410. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2411. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2412. return; /* already disabled */
  2413. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2414. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2415. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2416. ctxt.seid = vsi->seid;
  2417. ctxt.info = vsi->info;
  2418. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2419. if (ret) {
  2420. dev_info(&vsi->back->pdev->dev,
  2421. "update vlan stripping failed, err %s aq_err %s\n",
  2422. i40e_stat_str(&vsi->back->hw, ret),
  2423. i40e_aq_str(&vsi->back->hw,
  2424. vsi->back->hw.aq.asq_last_status));
  2425. }
  2426. }
  2427. /**
  2428. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2429. * @vsi: the vsi being configured
  2430. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2431. *
  2432. * This is a helper function for adding a new MAC/VLAN filter with the
  2433. * specified VLAN for each existing MAC address already in the hash table.
  2434. * This function does *not* perform any accounting to update filters based on
  2435. * VLAN mode.
  2436. *
  2437. * NOTE: this function expects to be called while under the
  2438. * mac_filter_hash_lock
  2439. **/
  2440. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2441. {
  2442. struct i40e_mac_filter *f, *add_f;
  2443. struct hlist_node *h;
  2444. int bkt;
  2445. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2446. if (f->state == I40E_FILTER_REMOVE)
  2447. continue;
  2448. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2449. if (!add_f) {
  2450. dev_info(&vsi->back->pdev->dev,
  2451. "Could not add vlan filter %d for %pM\n",
  2452. vid, f->macaddr);
  2453. return -ENOMEM;
  2454. }
  2455. }
  2456. return 0;
  2457. }
  2458. /**
  2459. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2460. * @vsi: the VSI being configured
  2461. * @vid: VLAN id to be added
  2462. **/
  2463. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2464. {
  2465. int err;
  2466. if (vsi->info.pvid)
  2467. return -EINVAL;
  2468. /* The network stack will attempt to add VID=0, with the intention to
  2469. * receive priority tagged packets with a VLAN of 0. Our HW receives
  2470. * these packets by default when configured to receive untagged
  2471. * packets, so we don't need to add a filter for this case.
  2472. * Additionally, HW interprets adding a VID=0 filter as meaning to
  2473. * receive *only* tagged traffic and stops receiving untagged traffic.
  2474. * Thus, we do not want to actually add a filter for VID=0
  2475. */
  2476. if (!vid)
  2477. return 0;
  2478. /* Locked once because all functions invoked below iterates list*/
  2479. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2480. err = i40e_add_vlan_all_mac(vsi, vid);
  2481. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2482. if (err)
  2483. return err;
  2484. /* schedule our worker thread which will take care of
  2485. * applying the new filter changes
  2486. */
  2487. i40e_service_event_schedule(vsi->back);
  2488. return 0;
  2489. }
  2490. /**
  2491. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2492. * @vsi: the vsi being configured
  2493. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2494. *
  2495. * This function should be used to remove all VLAN filters which match the
  2496. * given VID. It does not schedule the service event and does not take the
  2497. * mac_filter_hash_lock so it may be combined with other operations under
  2498. * a single invocation of the mac_filter_hash_lock.
  2499. *
  2500. * NOTE: this function expects to be called while under the
  2501. * mac_filter_hash_lock
  2502. */
  2503. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2504. {
  2505. struct i40e_mac_filter *f;
  2506. struct hlist_node *h;
  2507. int bkt;
  2508. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2509. if (f->vlan == vid)
  2510. __i40e_del_filter(vsi, f);
  2511. }
  2512. }
  2513. /**
  2514. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2515. * @vsi: the VSI being configured
  2516. * @vid: VLAN id to be removed
  2517. **/
  2518. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2519. {
  2520. if (!vid || vsi->info.pvid)
  2521. return;
  2522. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2523. i40e_rm_vlan_all_mac(vsi, vid);
  2524. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2525. /* schedule our worker thread which will take care of
  2526. * applying the new filter changes
  2527. */
  2528. i40e_service_event_schedule(vsi->back);
  2529. }
  2530. /**
  2531. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2532. * @netdev: network interface to be adjusted
  2533. * @proto: unused protocol value
  2534. * @vid: vlan id to be added
  2535. *
  2536. * net_device_ops implementation for adding vlan ids
  2537. **/
  2538. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2539. __always_unused __be16 proto, u16 vid)
  2540. {
  2541. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2542. struct i40e_vsi *vsi = np->vsi;
  2543. int ret = 0;
  2544. if (vid >= VLAN_N_VID)
  2545. return -EINVAL;
  2546. ret = i40e_vsi_add_vlan(vsi, vid);
  2547. if (!ret)
  2548. set_bit(vid, vsi->active_vlans);
  2549. return ret;
  2550. }
  2551. /**
  2552. * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
  2553. * @netdev: network interface to be adjusted
  2554. * @proto: unused protocol value
  2555. * @vid: vlan id to be added
  2556. **/
  2557. static void i40e_vlan_rx_add_vid_up(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. if (vid >= VLAN_N_VID)
  2563. return;
  2564. set_bit(vid, vsi->active_vlans);
  2565. }
  2566. /**
  2567. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2568. * @netdev: network interface to be adjusted
  2569. * @proto: unused protocol value
  2570. * @vid: vlan id to be removed
  2571. *
  2572. * net_device_ops implementation for removing vlan ids
  2573. **/
  2574. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2575. __always_unused __be16 proto, u16 vid)
  2576. {
  2577. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2578. struct i40e_vsi *vsi = np->vsi;
  2579. /* return code is ignored as there is nothing a user
  2580. * can do about failure to remove and a log message was
  2581. * already printed from the other function
  2582. */
  2583. i40e_vsi_kill_vlan(vsi, vid);
  2584. clear_bit(vid, vsi->active_vlans);
  2585. return 0;
  2586. }
  2587. /**
  2588. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2589. * @vsi: the vsi being brought back up
  2590. **/
  2591. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2592. {
  2593. u16 vid;
  2594. if (!vsi->netdev)
  2595. return;
  2596. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  2597. i40e_vlan_stripping_enable(vsi);
  2598. else
  2599. i40e_vlan_stripping_disable(vsi);
  2600. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2601. i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
  2602. vid);
  2603. }
  2604. /**
  2605. * i40e_vsi_add_pvid - Add pvid for the VSI
  2606. * @vsi: the vsi being adjusted
  2607. * @vid: the vlan id to set as a PVID
  2608. **/
  2609. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2610. {
  2611. struct i40e_vsi_context ctxt;
  2612. i40e_status ret;
  2613. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2614. vsi->info.pvid = cpu_to_le16(vid);
  2615. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2616. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2617. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2618. ctxt.seid = vsi->seid;
  2619. ctxt.info = vsi->info;
  2620. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2621. if (ret) {
  2622. dev_info(&vsi->back->pdev->dev,
  2623. "add pvid failed, err %s aq_err %s\n",
  2624. i40e_stat_str(&vsi->back->hw, ret),
  2625. i40e_aq_str(&vsi->back->hw,
  2626. vsi->back->hw.aq.asq_last_status));
  2627. return -ENOENT;
  2628. }
  2629. return 0;
  2630. }
  2631. /**
  2632. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2633. * @vsi: the vsi being adjusted
  2634. *
  2635. * Just use the vlan_rx_register() service to put it back to normal
  2636. **/
  2637. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2638. {
  2639. i40e_vlan_stripping_disable(vsi);
  2640. vsi->info.pvid = 0;
  2641. }
  2642. /**
  2643. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2644. * @vsi: ptr to the VSI
  2645. *
  2646. * If this function returns with an error, then it's possible one or
  2647. * more of the rings is populated (while the rest are not). It is the
  2648. * callers duty to clean those orphaned rings.
  2649. *
  2650. * Return 0 on success, negative on failure
  2651. **/
  2652. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2653. {
  2654. int i, err = 0;
  2655. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2656. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2657. if (!i40e_enabled_xdp_vsi(vsi))
  2658. return err;
  2659. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2660. err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
  2661. return err;
  2662. }
  2663. /**
  2664. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2665. * @vsi: ptr to the VSI
  2666. *
  2667. * Free VSI's transmit software resources
  2668. **/
  2669. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2670. {
  2671. int i;
  2672. if (vsi->tx_rings) {
  2673. for (i = 0; i < vsi->num_queue_pairs; i++)
  2674. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2675. i40e_free_tx_resources(vsi->tx_rings[i]);
  2676. }
  2677. if (vsi->xdp_rings) {
  2678. for (i = 0; i < vsi->num_queue_pairs; i++)
  2679. if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
  2680. i40e_free_tx_resources(vsi->xdp_rings[i]);
  2681. }
  2682. }
  2683. /**
  2684. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2685. * @vsi: ptr to the VSI
  2686. *
  2687. * If this function returns with an error, then it's possible one or
  2688. * more of the rings is populated (while the rest are not). It is the
  2689. * callers duty to clean those orphaned rings.
  2690. *
  2691. * Return 0 on success, negative on failure
  2692. **/
  2693. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2694. {
  2695. int i, err = 0;
  2696. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2697. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2698. return err;
  2699. }
  2700. /**
  2701. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2702. * @vsi: ptr to the VSI
  2703. *
  2704. * Free all receive software resources
  2705. **/
  2706. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2707. {
  2708. int i;
  2709. if (!vsi->rx_rings)
  2710. return;
  2711. for (i = 0; i < vsi->num_queue_pairs; i++)
  2712. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2713. i40e_free_rx_resources(vsi->rx_rings[i]);
  2714. }
  2715. /**
  2716. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2717. * @ring: The Tx ring to configure
  2718. *
  2719. * This enables/disables XPS for a given Tx descriptor ring
  2720. * based on the TCs enabled for the VSI that ring belongs to.
  2721. **/
  2722. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2723. {
  2724. int cpu;
  2725. if (!ring->q_vector || !ring->netdev || ring->ch)
  2726. return;
  2727. /* We only initialize XPS once, so as not to overwrite user settings */
  2728. if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
  2729. return;
  2730. cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
  2731. netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
  2732. ring->queue_index);
  2733. }
  2734. /**
  2735. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2736. * @ring: The Tx ring to configure
  2737. *
  2738. * Configure the Tx descriptor ring in the HMC context.
  2739. **/
  2740. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2741. {
  2742. struct i40e_vsi *vsi = ring->vsi;
  2743. u16 pf_q = vsi->base_queue + ring->queue_index;
  2744. struct i40e_hw *hw = &vsi->back->hw;
  2745. struct i40e_hmc_obj_txq tx_ctx;
  2746. i40e_status err = 0;
  2747. u32 qtx_ctl = 0;
  2748. /* some ATR related tx ring init */
  2749. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2750. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2751. ring->atr_count = 0;
  2752. } else {
  2753. ring->atr_sample_rate = 0;
  2754. }
  2755. /* configure XPS */
  2756. i40e_config_xps_tx_ring(ring);
  2757. /* clear the context structure first */
  2758. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2759. tx_ctx.new_context = 1;
  2760. tx_ctx.base = (ring->dma / 128);
  2761. tx_ctx.qlen = ring->count;
  2762. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2763. I40E_FLAG_FD_ATR_ENABLED));
  2764. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2765. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2766. if (vsi->type != I40E_VSI_FDIR)
  2767. tx_ctx.head_wb_ena = 1;
  2768. tx_ctx.head_wb_addr = ring->dma +
  2769. (ring->count * sizeof(struct i40e_tx_desc));
  2770. /* As part of VSI creation/update, FW allocates certain
  2771. * Tx arbitration queue sets for each TC enabled for
  2772. * the VSI. The FW returns the handles to these queue
  2773. * sets as part of the response buffer to Add VSI,
  2774. * Update VSI, etc. AQ commands. It is expected that
  2775. * these queue set handles be associated with the Tx
  2776. * queues by the driver as part of the TX queue context
  2777. * initialization. This has to be done regardless of
  2778. * DCB as by default everything is mapped to TC0.
  2779. */
  2780. if (ring->ch)
  2781. tx_ctx.rdylist =
  2782. le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
  2783. else
  2784. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2785. tx_ctx.rdylist_act = 0;
  2786. /* clear the context in the HMC */
  2787. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2788. if (err) {
  2789. dev_info(&vsi->back->pdev->dev,
  2790. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2791. ring->queue_index, pf_q, err);
  2792. return -ENOMEM;
  2793. }
  2794. /* set the context in the HMC */
  2795. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2796. if (err) {
  2797. dev_info(&vsi->back->pdev->dev,
  2798. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2799. ring->queue_index, pf_q, err);
  2800. return -ENOMEM;
  2801. }
  2802. /* Now associate this queue with this PCI function */
  2803. if (ring->ch) {
  2804. if (ring->ch->type == I40E_VSI_VMDQ2)
  2805. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2806. else
  2807. return -EINVAL;
  2808. qtx_ctl |= (ring->ch->vsi_number <<
  2809. I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2810. I40E_QTX_CTL_VFVM_INDX_MASK;
  2811. } else {
  2812. if (vsi->type == I40E_VSI_VMDQ2) {
  2813. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2814. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2815. I40E_QTX_CTL_VFVM_INDX_MASK;
  2816. } else {
  2817. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2818. }
  2819. }
  2820. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2821. I40E_QTX_CTL_PF_INDX_MASK);
  2822. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2823. i40e_flush(hw);
  2824. /* cache tail off for easier writes later */
  2825. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2826. return 0;
  2827. }
  2828. /**
  2829. * i40e_configure_rx_ring - Configure a receive ring context
  2830. * @ring: The Rx ring to configure
  2831. *
  2832. * Configure the Rx descriptor ring in the HMC context.
  2833. **/
  2834. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2835. {
  2836. struct i40e_vsi *vsi = ring->vsi;
  2837. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2838. u16 pf_q = vsi->base_queue + ring->queue_index;
  2839. struct i40e_hw *hw = &vsi->back->hw;
  2840. struct i40e_hmc_obj_rxq rx_ctx;
  2841. i40e_status err = 0;
  2842. bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
  2843. /* clear the context structure first */
  2844. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2845. ring->rx_buf_len = vsi->rx_buf_len;
  2846. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2847. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2848. rx_ctx.base = (ring->dma / 128);
  2849. rx_ctx.qlen = ring->count;
  2850. /* use 32 byte descriptors */
  2851. rx_ctx.dsize = 1;
  2852. /* descriptor type is always zero
  2853. * rx_ctx.dtype = 0;
  2854. */
  2855. rx_ctx.hsplit_0 = 0;
  2856. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2857. if (hw->revision_id == 0)
  2858. rx_ctx.lrxqthresh = 0;
  2859. else
  2860. rx_ctx.lrxqthresh = 1;
  2861. rx_ctx.crcstrip = 1;
  2862. rx_ctx.l2tsel = 1;
  2863. /* this controls whether VLAN is stripped from inner headers */
  2864. rx_ctx.showiv = 0;
  2865. /* set the prefena field to 1 because the manual says to */
  2866. rx_ctx.prefena = 1;
  2867. /* clear the context in the HMC */
  2868. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2869. if (err) {
  2870. dev_info(&vsi->back->pdev->dev,
  2871. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2872. ring->queue_index, pf_q, err);
  2873. return -ENOMEM;
  2874. }
  2875. /* set the context in the HMC */
  2876. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2877. if (err) {
  2878. dev_info(&vsi->back->pdev->dev,
  2879. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2880. ring->queue_index, pf_q, err);
  2881. return -ENOMEM;
  2882. }
  2883. /* configure Rx buffer alignment */
  2884. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2885. clear_ring_build_skb_enabled(ring);
  2886. else
  2887. set_ring_build_skb_enabled(ring);
  2888. /* cache tail for quicker writes, and clear the reg before use */
  2889. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2890. writel(0, ring->tail);
  2891. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2892. return 0;
  2893. }
  2894. /**
  2895. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2896. * @vsi: VSI structure describing this set of rings and resources
  2897. *
  2898. * Configure the Tx VSI for operation.
  2899. **/
  2900. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2901. {
  2902. int err = 0;
  2903. u16 i;
  2904. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2905. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2906. if (!i40e_enabled_xdp_vsi(vsi))
  2907. return err;
  2908. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2909. err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
  2910. return err;
  2911. }
  2912. /**
  2913. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2914. * @vsi: the VSI being configured
  2915. *
  2916. * Configure the Rx VSI for operation.
  2917. **/
  2918. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2919. {
  2920. int err = 0;
  2921. u16 i;
  2922. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2923. vsi->max_frame = I40E_MAX_RXBUFFER;
  2924. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2925. #if (PAGE_SIZE < 8192)
  2926. } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  2927. (vsi->netdev->mtu <= ETH_DATA_LEN)) {
  2928. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2929. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2930. #endif
  2931. } else {
  2932. vsi->max_frame = I40E_MAX_RXBUFFER;
  2933. vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
  2934. I40E_RXBUFFER_2048;
  2935. }
  2936. /* set up individual rings */
  2937. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2938. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2939. return err;
  2940. }
  2941. /**
  2942. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2943. * @vsi: ptr to the VSI
  2944. **/
  2945. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2946. {
  2947. struct i40e_ring *tx_ring, *rx_ring;
  2948. u16 qoffset, qcount;
  2949. int i, n;
  2950. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2951. /* Reset the TC information */
  2952. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2953. rx_ring = vsi->rx_rings[i];
  2954. tx_ring = vsi->tx_rings[i];
  2955. rx_ring->dcb_tc = 0;
  2956. tx_ring->dcb_tc = 0;
  2957. }
  2958. return;
  2959. }
  2960. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2961. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2962. continue;
  2963. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2964. qcount = vsi->tc_config.tc_info[n].qcount;
  2965. for (i = qoffset; i < (qoffset + qcount); i++) {
  2966. rx_ring = vsi->rx_rings[i];
  2967. tx_ring = vsi->tx_rings[i];
  2968. rx_ring->dcb_tc = n;
  2969. tx_ring->dcb_tc = n;
  2970. }
  2971. }
  2972. }
  2973. /**
  2974. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2975. * @vsi: ptr to the VSI
  2976. **/
  2977. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2978. {
  2979. if (vsi->netdev)
  2980. i40e_set_rx_mode(vsi->netdev);
  2981. }
  2982. /**
  2983. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2984. * @vsi: Pointer to the targeted VSI
  2985. *
  2986. * This function replays the hlist on the hw where all the SB Flow Director
  2987. * filters were saved.
  2988. **/
  2989. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2990. {
  2991. struct i40e_fdir_filter *filter;
  2992. struct i40e_pf *pf = vsi->back;
  2993. struct hlist_node *node;
  2994. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2995. return;
  2996. /* Reset FDir counters as we're replaying all existing filters */
  2997. pf->fd_tcp4_filter_cnt = 0;
  2998. pf->fd_udp4_filter_cnt = 0;
  2999. pf->fd_sctp4_filter_cnt = 0;
  3000. pf->fd_ip4_filter_cnt = 0;
  3001. hlist_for_each_entry_safe(filter, node,
  3002. &pf->fdir_filter_list, fdir_node) {
  3003. i40e_add_del_fdir(vsi, filter, true);
  3004. }
  3005. }
  3006. /**
  3007. * i40e_vsi_configure - Set up the VSI for action
  3008. * @vsi: the VSI being configured
  3009. **/
  3010. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  3011. {
  3012. int err;
  3013. i40e_set_vsi_rx_mode(vsi);
  3014. i40e_restore_vlan(vsi);
  3015. i40e_vsi_config_dcb_rings(vsi);
  3016. err = i40e_vsi_configure_tx(vsi);
  3017. if (!err)
  3018. err = i40e_vsi_configure_rx(vsi);
  3019. return err;
  3020. }
  3021. /**
  3022. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  3023. * @vsi: the VSI being configured
  3024. **/
  3025. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  3026. {
  3027. bool has_xdp = i40e_enabled_xdp_vsi(vsi);
  3028. struct i40e_pf *pf = vsi->back;
  3029. struct i40e_hw *hw = &pf->hw;
  3030. u16 vector;
  3031. int i, q;
  3032. u32 qp;
  3033. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  3034. * and PFINT_LNKLSTn registers, e.g.:
  3035. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  3036. */
  3037. qp = vsi->base_queue;
  3038. vector = vsi->base_vector;
  3039. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  3040. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  3041. q_vector->rx.next_update = jiffies + 1;
  3042. q_vector->rx.target_itr =
  3043. ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
  3044. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  3045. q_vector->rx.target_itr >> 1);
  3046. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3047. q_vector->tx.next_update = jiffies + 1;
  3048. q_vector->tx.target_itr =
  3049. ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
  3050. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  3051. q_vector->tx.target_itr >> 1);
  3052. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3053. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  3054. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  3055. /* Linked list for the queuepairs assigned to this vector */
  3056. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  3057. for (q = 0; q < q_vector->num_ringpairs; q++) {
  3058. u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
  3059. u32 val;
  3060. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3061. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3062. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  3063. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  3064. (I40E_QUEUE_TYPE_TX <<
  3065. I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  3066. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3067. if (has_xdp) {
  3068. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3069. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3070. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3071. (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3072. (I40E_QUEUE_TYPE_TX <<
  3073. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3074. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3075. }
  3076. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3077. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3078. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3079. ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3080. (I40E_QUEUE_TYPE_RX <<
  3081. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3082. /* Terminate the linked list */
  3083. if (q == (q_vector->num_ringpairs - 1))
  3084. val |= (I40E_QUEUE_END_OF_LIST <<
  3085. I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3086. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3087. qp++;
  3088. }
  3089. }
  3090. i40e_flush(hw);
  3091. }
  3092. /**
  3093. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  3094. * @pf: pointer to private device data structure
  3095. **/
  3096. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  3097. {
  3098. struct i40e_hw *hw = &pf->hw;
  3099. u32 val;
  3100. /* clear things first */
  3101. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  3102. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  3103. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  3104. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  3105. I40E_PFINT_ICR0_ENA_GRST_MASK |
  3106. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  3107. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  3108. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  3109. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  3110. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3111. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  3112. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3113. if (pf->flags & I40E_FLAG_PTP)
  3114. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3115. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  3116. /* SW_ITR_IDX = 0, but don't change INTENA */
  3117. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  3118. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  3119. /* OTHER_ITR_IDX = 0 */
  3120. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  3121. }
  3122. /**
  3123. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  3124. * @vsi: the VSI being configured
  3125. **/
  3126. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  3127. {
  3128. u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
  3129. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3130. struct i40e_pf *pf = vsi->back;
  3131. struct i40e_hw *hw = &pf->hw;
  3132. u32 val;
  3133. /* set the ITR configuration */
  3134. q_vector->rx.next_update = jiffies + 1;
  3135. q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
  3136. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
  3137. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3138. q_vector->tx.next_update = jiffies + 1;
  3139. q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
  3140. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
  3141. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3142. i40e_enable_misc_int_causes(pf);
  3143. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  3144. wr32(hw, I40E_PFINT_LNKLST0, 0);
  3145. /* Associate the queue pair to the vector and enable the queue int */
  3146. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3147. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3148. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  3149. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3150. wr32(hw, I40E_QINT_RQCTL(0), val);
  3151. if (i40e_enabled_xdp_vsi(vsi)) {
  3152. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3153. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
  3154. (I40E_QUEUE_TYPE_TX
  3155. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3156. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3157. }
  3158. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3159. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3160. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3161. wr32(hw, I40E_QINT_TQCTL(0), val);
  3162. i40e_flush(hw);
  3163. }
  3164. /**
  3165. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  3166. * @pf: board private structure
  3167. **/
  3168. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  3169. {
  3170. struct i40e_hw *hw = &pf->hw;
  3171. wr32(hw, I40E_PFINT_DYN_CTL0,
  3172. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  3173. i40e_flush(hw);
  3174. }
  3175. /**
  3176. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  3177. * @pf: board private structure
  3178. **/
  3179. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  3180. {
  3181. struct i40e_hw *hw = &pf->hw;
  3182. u32 val;
  3183. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3184. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  3185. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  3186. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  3187. i40e_flush(hw);
  3188. }
  3189. /**
  3190. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3191. * @irq: interrupt number
  3192. * @data: pointer to a q_vector
  3193. **/
  3194. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3195. {
  3196. struct i40e_q_vector *q_vector = data;
  3197. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3198. return IRQ_HANDLED;
  3199. napi_schedule_irqoff(&q_vector->napi);
  3200. return IRQ_HANDLED;
  3201. }
  3202. /**
  3203. * i40e_irq_affinity_notify - Callback for affinity changes
  3204. * @notify: context as to what irq was changed
  3205. * @mask: the new affinity mask
  3206. *
  3207. * This is a callback function used by the irq_set_affinity_notifier function
  3208. * so that we may register to receive changes to the irq affinity masks.
  3209. **/
  3210. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3211. const cpumask_t *mask)
  3212. {
  3213. struct i40e_q_vector *q_vector =
  3214. container_of(notify, struct i40e_q_vector, affinity_notify);
  3215. cpumask_copy(&q_vector->affinity_mask, mask);
  3216. }
  3217. /**
  3218. * i40e_irq_affinity_release - Callback for affinity notifier release
  3219. * @ref: internal core kernel usage
  3220. *
  3221. * This is a callback function used by the irq_set_affinity_notifier function
  3222. * to inform the current notification subscriber that they will no longer
  3223. * receive notifications.
  3224. **/
  3225. static void i40e_irq_affinity_release(struct kref *ref) {}
  3226. /**
  3227. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3228. * @vsi: the VSI being configured
  3229. * @basename: name for the vector
  3230. *
  3231. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3232. **/
  3233. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3234. {
  3235. int q_vectors = vsi->num_q_vectors;
  3236. struct i40e_pf *pf = vsi->back;
  3237. int base = vsi->base_vector;
  3238. int rx_int_idx = 0;
  3239. int tx_int_idx = 0;
  3240. int vector, err;
  3241. int irq_num;
  3242. int cpu;
  3243. for (vector = 0; vector < q_vectors; vector++) {
  3244. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3245. irq_num = pf->msix_entries[base + vector].vector;
  3246. if (q_vector->tx.ring && q_vector->rx.ring) {
  3247. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3248. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3249. tx_int_idx++;
  3250. } else if (q_vector->rx.ring) {
  3251. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3252. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3253. } else if (q_vector->tx.ring) {
  3254. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3255. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3256. } else {
  3257. /* skip this unused q_vector */
  3258. continue;
  3259. }
  3260. err = request_irq(irq_num,
  3261. vsi->irq_handler,
  3262. 0,
  3263. q_vector->name,
  3264. q_vector);
  3265. if (err) {
  3266. dev_info(&pf->pdev->dev,
  3267. "MSIX request_irq failed, error: %d\n", err);
  3268. goto free_queue_irqs;
  3269. }
  3270. /* register for affinity change notifications */
  3271. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3272. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3273. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3274. /* Spread affinity hints out across online CPUs.
  3275. *
  3276. * get_cpu_mask returns a static constant mask with
  3277. * a permanent lifetime so it's ok to pass to
  3278. * irq_set_affinity_hint without making a copy.
  3279. */
  3280. cpu = cpumask_local_spread(q_vector->v_idx, -1);
  3281. irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
  3282. }
  3283. vsi->irqs_ready = true;
  3284. return 0;
  3285. free_queue_irqs:
  3286. while (vector) {
  3287. vector--;
  3288. irq_num = pf->msix_entries[base + vector].vector;
  3289. irq_set_affinity_notifier(irq_num, NULL);
  3290. irq_set_affinity_hint(irq_num, NULL);
  3291. free_irq(irq_num, &vsi->q_vectors[vector]);
  3292. }
  3293. return err;
  3294. }
  3295. /**
  3296. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3297. * @vsi: the VSI being un-configured
  3298. **/
  3299. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3300. {
  3301. struct i40e_pf *pf = vsi->back;
  3302. struct i40e_hw *hw = &pf->hw;
  3303. int base = vsi->base_vector;
  3304. int i;
  3305. /* disable interrupt causation from each queue */
  3306. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3307. u32 val;
  3308. val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3309. val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  3310. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3311. val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
  3312. val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  3313. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
  3314. if (!i40e_enabled_xdp_vsi(vsi))
  3315. continue;
  3316. wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
  3317. }
  3318. /* disable each interrupt */
  3319. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3320. for (i = vsi->base_vector;
  3321. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3322. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3323. i40e_flush(hw);
  3324. for (i = 0; i < vsi->num_q_vectors; i++)
  3325. synchronize_irq(pf->msix_entries[i + base].vector);
  3326. } else {
  3327. /* Legacy and MSI mode - this stops all interrupt handling */
  3328. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3329. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3330. i40e_flush(hw);
  3331. synchronize_irq(pf->pdev->irq);
  3332. }
  3333. }
  3334. /**
  3335. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3336. * @vsi: the VSI being configured
  3337. **/
  3338. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3339. {
  3340. struct i40e_pf *pf = vsi->back;
  3341. int i;
  3342. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3343. for (i = 0; i < vsi->num_q_vectors; i++)
  3344. i40e_irq_dynamic_enable(vsi, i);
  3345. } else {
  3346. i40e_irq_dynamic_enable_icr0(pf);
  3347. }
  3348. i40e_flush(&pf->hw);
  3349. return 0;
  3350. }
  3351. /**
  3352. * i40e_free_misc_vector - Free the vector that handles non-queue events
  3353. * @pf: board private structure
  3354. **/
  3355. static void i40e_free_misc_vector(struct i40e_pf *pf)
  3356. {
  3357. /* Disable ICR 0 */
  3358. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3359. i40e_flush(&pf->hw);
  3360. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3361. synchronize_irq(pf->msix_entries[0].vector);
  3362. free_irq(pf->msix_entries[0].vector, pf);
  3363. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  3364. }
  3365. }
  3366. /**
  3367. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3368. * @irq: interrupt number
  3369. * @data: pointer to a q_vector
  3370. *
  3371. * This is the handler used for all MSI/Legacy interrupts, and deals
  3372. * with both queue and non-queue interrupts. This is also used in
  3373. * MSIX mode to handle the non-queue interrupts.
  3374. **/
  3375. static irqreturn_t i40e_intr(int irq, void *data)
  3376. {
  3377. struct i40e_pf *pf = (struct i40e_pf *)data;
  3378. struct i40e_hw *hw = &pf->hw;
  3379. irqreturn_t ret = IRQ_NONE;
  3380. u32 icr0, icr0_remaining;
  3381. u32 val, ena_mask;
  3382. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3383. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3384. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3385. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3386. goto enable_intr;
  3387. /* if interrupt but no bits showing, must be SWINT */
  3388. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3389. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3390. pf->sw_int_count++;
  3391. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3392. (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3393. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3394. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3395. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  3396. }
  3397. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3398. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3399. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3400. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3401. /* We do not have a way to disarm Queue causes while leaving
  3402. * interrupt enabled for all other causes, ideally
  3403. * interrupt should be disabled while we are in NAPI but
  3404. * this is not a performance path and napi_schedule()
  3405. * can deal with rescheduling.
  3406. */
  3407. if (!test_bit(__I40E_DOWN, pf->state))
  3408. napi_schedule_irqoff(&q_vector->napi);
  3409. }
  3410. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3411. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3412. set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  3413. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3414. }
  3415. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3416. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3417. set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  3418. }
  3419. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3420. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3421. set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3422. }
  3423. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3424. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  3425. set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  3426. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3427. val = rd32(hw, I40E_GLGEN_RSTAT);
  3428. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3429. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3430. if (val == I40E_RESET_CORER) {
  3431. pf->corer_count++;
  3432. } else if (val == I40E_RESET_GLOBR) {
  3433. pf->globr_count++;
  3434. } else if (val == I40E_RESET_EMPR) {
  3435. pf->empr_count++;
  3436. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
  3437. }
  3438. }
  3439. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3440. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3441. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3442. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3443. rd32(hw, I40E_PFHMC_ERRORINFO),
  3444. rd32(hw, I40E_PFHMC_ERRORDATA));
  3445. }
  3446. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3447. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3448. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3449. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3450. i40e_ptp_tx_hwtstamp(pf);
  3451. }
  3452. }
  3453. /* If a critical error is pending we have no choice but to reset the
  3454. * device.
  3455. * Report and mask out any remaining unexpected interrupts.
  3456. */
  3457. icr0_remaining = icr0 & ena_mask;
  3458. if (icr0_remaining) {
  3459. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3460. icr0_remaining);
  3461. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3462. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3463. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3464. dev_info(&pf->pdev->dev, "device will be reset\n");
  3465. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  3466. i40e_service_event_schedule(pf);
  3467. }
  3468. ena_mask &= ~icr0_remaining;
  3469. }
  3470. ret = IRQ_HANDLED;
  3471. enable_intr:
  3472. /* re-enable interrupt causes */
  3473. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3474. if (!test_bit(__I40E_DOWN, pf->state)) {
  3475. i40e_service_event_schedule(pf);
  3476. i40e_irq_dynamic_enable_icr0(pf);
  3477. }
  3478. return ret;
  3479. }
  3480. /**
  3481. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3482. * @tx_ring: tx ring to clean
  3483. * @budget: how many cleans we're allowed
  3484. *
  3485. * Returns true if there's any budget left (e.g. the clean is finished)
  3486. **/
  3487. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3488. {
  3489. struct i40e_vsi *vsi = tx_ring->vsi;
  3490. u16 i = tx_ring->next_to_clean;
  3491. struct i40e_tx_buffer *tx_buf;
  3492. struct i40e_tx_desc *tx_desc;
  3493. tx_buf = &tx_ring->tx_bi[i];
  3494. tx_desc = I40E_TX_DESC(tx_ring, i);
  3495. i -= tx_ring->count;
  3496. do {
  3497. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3498. /* if next_to_watch is not set then there is no work pending */
  3499. if (!eop_desc)
  3500. break;
  3501. /* prevent any other reads prior to eop_desc */
  3502. smp_rmb();
  3503. /* if the descriptor isn't done, no work yet to do */
  3504. if (!(eop_desc->cmd_type_offset_bsz &
  3505. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3506. break;
  3507. /* clear next_to_watch to prevent false hangs */
  3508. tx_buf->next_to_watch = NULL;
  3509. tx_desc->buffer_addr = 0;
  3510. tx_desc->cmd_type_offset_bsz = 0;
  3511. /* move past filter desc */
  3512. tx_buf++;
  3513. tx_desc++;
  3514. i++;
  3515. if (unlikely(!i)) {
  3516. i -= tx_ring->count;
  3517. tx_buf = tx_ring->tx_bi;
  3518. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3519. }
  3520. /* unmap skb header data */
  3521. dma_unmap_single(tx_ring->dev,
  3522. dma_unmap_addr(tx_buf, dma),
  3523. dma_unmap_len(tx_buf, len),
  3524. DMA_TO_DEVICE);
  3525. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3526. kfree(tx_buf->raw_buf);
  3527. tx_buf->raw_buf = NULL;
  3528. tx_buf->tx_flags = 0;
  3529. tx_buf->next_to_watch = NULL;
  3530. dma_unmap_len_set(tx_buf, len, 0);
  3531. tx_desc->buffer_addr = 0;
  3532. tx_desc->cmd_type_offset_bsz = 0;
  3533. /* move us past the eop_desc for start of next FD desc */
  3534. tx_buf++;
  3535. tx_desc++;
  3536. i++;
  3537. if (unlikely(!i)) {
  3538. i -= tx_ring->count;
  3539. tx_buf = tx_ring->tx_bi;
  3540. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3541. }
  3542. /* update budget accounting */
  3543. budget--;
  3544. } while (likely(budget));
  3545. i += tx_ring->count;
  3546. tx_ring->next_to_clean = i;
  3547. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3548. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3549. return budget > 0;
  3550. }
  3551. /**
  3552. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3553. * @irq: interrupt number
  3554. * @data: pointer to a q_vector
  3555. **/
  3556. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3557. {
  3558. struct i40e_q_vector *q_vector = data;
  3559. struct i40e_vsi *vsi;
  3560. if (!q_vector->tx.ring)
  3561. return IRQ_HANDLED;
  3562. vsi = q_vector->tx.ring->vsi;
  3563. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3564. return IRQ_HANDLED;
  3565. }
  3566. /**
  3567. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3568. * @vsi: the VSI being configured
  3569. * @v_idx: vector index
  3570. * @qp_idx: queue pair index
  3571. **/
  3572. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3573. {
  3574. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3575. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3576. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3577. tx_ring->q_vector = q_vector;
  3578. tx_ring->next = q_vector->tx.ring;
  3579. q_vector->tx.ring = tx_ring;
  3580. q_vector->tx.count++;
  3581. /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
  3582. if (i40e_enabled_xdp_vsi(vsi)) {
  3583. struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
  3584. xdp_ring->q_vector = q_vector;
  3585. xdp_ring->next = q_vector->tx.ring;
  3586. q_vector->tx.ring = xdp_ring;
  3587. q_vector->tx.count++;
  3588. }
  3589. rx_ring->q_vector = q_vector;
  3590. rx_ring->next = q_vector->rx.ring;
  3591. q_vector->rx.ring = rx_ring;
  3592. q_vector->rx.count++;
  3593. }
  3594. /**
  3595. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3596. * @vsi: the VSI being configured
  3597. *
  3598. * This function maps descriptor rings to the queue-specific vectors
  3599. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3600. * one vector per queue pair, but on a constrained vector budget, we
  3601. * group the queue pairs as "efficiently" as possible.
  3602. **/
  3603. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3604. {
  3605. int qp_remaining = vsi->num_queue_pairs;
  3606. int q_vectors = vsi->num_q_vectors;
  3607. int num_ringpairs;
  3608. int v_start = 0;
  3609. int qp_idx = 0;
  3610. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3611. * group them so there are multiple queues per vector.
  3612. * It is also important to go through all the vectors available to be
  3613. * sure that if we don't use all the vectors, that the remaining vectors
  3614. * are cleared. This is especially important when decreasing the
  3615. * number of queues in use.
  3616. */
  3617. for (; v_start < q_vectors; v_start++) {
  3618. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3619. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3620. q_vector->num_ringpairs = num_ringpairs;
  3621. q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
  3622. q_vector->rx.count = 0;
  3623. q_vector->tx.count = 0;
  3624. q_vector->rx.ring = NULL;
  3625. q_vector->tx.ring = NULL;
  3626. while (num_ringpairs--) {
  3627. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3628. qp_idx++;
  3629. qp_remaining--;
  3630. }
  3631. }
  3632. }
  3633. /**
  3634. * i40e_vsi_request_irq - Request IRQ from the OS
  3635. * @vsi: the VSI being configured
  3636. * @basename: name for the vector
  3637. **/
  3638. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3639. {
  3640. struct i40e_pf *pf = vsi->back;
  3641. int err;
  3642. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3643. err = i40e_vsi_request_irq_msix(vsi, basename);
  3644. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3645. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3646. pf->int_name, pf);
  3647. else
  3648. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3649. pf->int_name, pf);
  3650. if (err)
  3651. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3652. return err;
  3653. }
  3654. #ifdef CONFIG_NET_POLL_CONTROLLER
  3655. /**
  3656. * i40e_netpoll - A Polling 'interrupt' handler
  3657. * @netdev: network interface device structure
  3658. *
  3659. * This is used by netconsole to send skbs without having to re-enable
  3660. * interrupts. It's not called while the normal interrupt routine is executing.
  3661. **/
  3662. static void i40e_netpoll(struct net_device *netdev)
  3663. {
  3664. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3665. struct i40e_vsi *vsi = np->vsi;
  3666. struct i40e_pf *pf = vsi->back;
  3667. int i;
  3668. /* if interface is down do nothing */
  3669. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3670. return;
  3671. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3672. for (i = 0; i < vsi->num_q_vectors; i++)
  3673. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3674. } else {
  3675. i40e_intr(pf->pdev->irq, netdev);
  3676. }
  3677. }
  3678. #endif
  3679. #define I40E_QTX_ENA_WAIT_COUNT 50
  3680. /**
  3681. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3682. * @pf: the PF being configured
  3683. * @pf_q: the PF queue
  3684. * @enable: enable or disable state of the queue
  3685. *
  3686. * This routine will wait for the given Tx queue of the PF to reach the
  3687. * enabled or disabled state.
  3688. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3689. * multiple retries; else will return 0 in case of success.
  3690. **/
  3691. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3692. {
  3693. int i;
  3694. u32 tx_reg;
  3695. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3696. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3697. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3698. break;
  3699. usleep_range(10, 20);
  3700. }
  3701. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3702. return -ETIMEDOUT;
  3703. return 0;
  3704. }
  3705. /**
  3706. * i40e_control_tx_q - Start or stop a particular Tx queue
  3707. * @pf: the PF structure
  3708. * @pf_q: the PF queue to configure
  3709. * @enable: start or stop the queue
  3710. *
  3711. * This function enables or disables a single queue. Note that any delay
  3712. * required after the operation is expected to be handled by the caller of
  3713. * this function.
  3714. **/
  3715. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3716. {
  3717. struct i40e_hw *hw = &pf->hw;
  3718. u32 tx_reg;
  3719. int i;
  3720. /* warn the TX unit of coming changes */
  3721. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3722. if (!enable)
  3723. usleep_range(10, 20);
  3724. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3725. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3726. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3727. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3728. break;
  3729. usleep_range(1000, 2000);
  3730. }
  3731. /* Skip if the queue is already in the requested state */
  3732. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3733. return;
  3734. /* turn on/off the queue */
  3735. if (enable) {
  3736. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3737. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3738. } else {
  3739. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3740. }
  3741. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3742. }
  3743. /**
  3744. * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
  3745. * @seid: VSI SEID
  3746. * @pf: the PF structure
  3747. * @pf_q: the PF queue to configure
  3748. * @is_xdp: true if the queue is used for XDP
  3749. * @enable: start or stop the queue
  3750. **/
  3751. int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
  3752. bool is_xdp, bool enable)
  3753. {
  3754. int ret;
  3755. i40e_control_tx_q(pf, pf_q, enable);
  3756. /* wait for the change to finish */
  3757. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3758. if (ret) {
  3759. dev_info(&pf->pdev->dev,
  3760. "VSI seid %d %sTx ring %d %sable timeout\n",
  3761. seid, (is_xdp ? "XDP " : ""), pf_q,
  3762. (enable ? "en" : "dis"));
  3763. }
  3764. return ret;
  3765. }
  3766. /**
  3767. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3768. * @vsi: the VSI being configured
  3769. * @enable: start or stop the rings
  3770. **/
  3771. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3772. {
  3773. struct i40e_pf *pf = vsi->back;
  3774. int i, pf_q, ret = 0;
  3775. pf_q = vsi->base_queue;
  3776. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3777. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3778. pf_q,
  3779. false /*is xdp*/, enable);
  3780. if (ret)
  3781. break;
  3782. if (!i40e_enabled_xdp_vsi(vsi))
  3783. continue;
  3784. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3785. pf_q + vsi->alloc_queue_pairs,
  3786. true /*is xdp*/, enable);
  3787. if (ret)
  3788. break;
  3789. }
  3790. return ret;
  3791. }
  3792. /**
  3793. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3794. * @pf: the PF being configured
  3795. * @pf_q: the PF queue
  3796. * @enable: enable or disable state of the queue
  3797. *
  3798. * This routine will wait for the given Rx queue of the PF to reach the
  3799. * enabled or disabled state.
  3800. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3801. * multiple retries; else will return 0 in case of success.
  3802. **/
  3803. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3804. {
  3805. int i;
  3806. u32 rx_reg;
  3807. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3808. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3809. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3810. break;
  3811. usleep_range(10, 20);
  3812. }
  3813. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3814. return -ETIMEDOUT;
  3815. return 0;
  3816. }
  3817. /**
  3818. * i40e_control_rx_q - Start or stop a particular Rx queue
  3819. * @pf: the PF structure
  3820. * @pf_q: the PF queue to configure
  3821. * @enable: start or stop the queue
  3822. *
  3823. * This function enables or disables a single queue. Note that
  3824. * any delay required after the operation is expected to be
  3825. * handled by the caller of this function.
  3826. **/
  3827. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3828. {
  3829. struct i40e_hw *hw = &pf->hw;
  3830. u32 rx_reg;
  3831. int i;
  3832. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3833. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3834. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3835. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3836. break;
  3837. usleep_range(1000, 2000);
  3838. }
  3839. /* Skip if the queue is already in the requested state */
  3840. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3841. return;
  3842. /* turn on/off the queue */
  3843. if (enable)
  3844. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3845. else
  3846. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3847. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3848. }
  3849. /**
  3850. * i40e_control_wait_rx_q
  3851. * @pf: the PF structure
  3852. * @pf_q: queue being configured
  3853. * @enable: start or stop the rings
  3854. *
  3855. * This function enables or disables a single queue along with waiting
  3856. * for the change to finish. The caller of this function should handle
  3857. * the delays needed in the case of disabling queues.
  3858. **/
  3859. int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3860. {
  3861. int ret = 0;
  3862. i40e_control_rx_q(pf, pf_q, enable);
  3863. /* wait for the change to finish */
  3864. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3865. if (ret)
  3866. return ret;
  3867. return ret;
  3868. }
  3869. /**
  3870. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3871. * @vsi: the VSI being configured
  3872. * @enable: start or stop the rings
  3873. **/
  3874. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3875. {
  3876. struct i40e_pf *pf = vsi->back;
  3877. int i, pf_q, ret = 0;
  3878. pf_q = vsi->base_queue;
  3879. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3880. ret = i40e_control_wait_rx_q(pf, pf_q, enable);
  3881. if (ret) {
  3882. dev_info(&pf->pdev->dev,
  3883. "VSI seid %d Rx ring %d %sable timeout\n",
  3884. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3885. break;
  3886. }
  3887. }
  3888. /* Due to HW errata, on Rx disable only, the register can indicate done
  3889. * before it really is. Needs 50ms to be sure
  3890. */
  3891. if (!enable)
  3892. mdelay(50);
  3893. return ret;
  3894. }
  3895. /**
  3896. * i40e_vsi_start_rings - Start a VSI's rings
  3897. * @vsi: the VSI being configured
  3898. **/
  3899. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3900. {
  3901. int ret = 0;
  3902. /* do rx first for enable and last for disable */
  3903. ret = i40e_vsi_control_rx(vsi, true);
  3904. if (ret)
  3905. return ret;
  3906. ret = i40e_vsi_control_tx(vsi, true);
  3907. return ret;
  3908. }
  3909. /**
  3910. * i40e_vsi_stop_rings - Stop a VSI's rings
  3911. * @vsi: the VSI being configured
  3912. **/
  3913. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3914. {
  3915. /* When port TX is suspended, don't wait */
  3916. if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
  3917. return i40e_vsi_stop_rings_no_wait(vsi);
  3918. /* do rx first for enable and last for disable
  3919. * Ignore return value, we need to shutdown whatever we can
  3920. */
  3921. i40e_vsi_control_tx(vsi, false);
  3922. i40e_vsi_control_rx(vsi, false);
  3923. }
  3924. /**
  3925. * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
  3926. * @vsi: the VSI being shutdown
  3927. *
  3928. * This function stops all the rings for a VSI but does not delay to verify
  3929. * that rings have been disabled. It is expected that the caller is shutting
  3930. * down multiple VSIs at once and will delay together for all the VSIs after
  3931. * initiating the shutdown. This is particularly useful for shutting down lots
  3932. * of VFs together. Otherwise, a large delay can be incurred while configuring
  3933. * each VSI in serial.
  3934. **/
  3935. void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
  3936. {
  3937. struct i40e_pf *pf = vsi->back;
  3938. int i, pf_q;
  3939. pf_q = vsi->base_queue;
  3940. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3941. i40e_control_tx_q(pf, pf_q, false);
  3942. i40e_control_rx_q(pf, pf_q, false);
  3943. }
  3944. }
  3945. /**
  3946. * i40e_vsi_free_irq - Free the irq association with the OS
  3947. * @vsi: the VSI being configured
  3948. **/
  3949. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3950. {
  3951. struct i40e_pf *pf = vsi->back;
  3952. struct i40e_hw *hw = &pf->hw;
  3953. int base = vsi->base_vector;
  3954. u32 val, qp;
  3955. int i;
  3956. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3957. if (!vsi->q_vectors)
  3958. return;
  3959. if (!vsi->irqs_ready)
  3960. return;
  3961. vsi->irqs_ready = false;
  3962. for (i = 0; i < vsi->num_q_vectors; i++) {
  3963. int irq_num;
  3964. u16 vector;
  3965. vector = i + base;
  3966. irq_num = pf->msix_entries[vector].vector;
  3967. /* free only the irqs that were actually requested */
  3968. if (!vsi->q_vectors[i] ||
  3969. !vsi->q_vectors[i]->num_ringpairs)
  3970. continue;
  3971. /* clear the affinity notifier in the IRQ descriptor */
  3972. irq_set_affinity_notifier(irq_num, NULL);
  3973. /* remove our suggested affinity mask for this IRQ */
  3974. irq_set_affinity_hint(irq_num, NULL);
  3975. synchronize_irq(irq_num);
  3976. free_irq(irq_num, vsi->q_vectors[i]);
  3977. /* Tear down the interrupt queue link list
  3978. *
  3979. * We know that they come in pairs and always
  3980. * the Rx first, then the Tx. To clear the
  3981. * link list, stick the EOL value into the
  3982. * next_q field of the registers.
  3983. */
  3984. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3985. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3986. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3987. val |= I40E_QUEUE_END_OF_LIST
  3988. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3989. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3990. while (qp != I40E_QUEUE_END_OF_LIST) {
  3991. u32 next;
  3992. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3993. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3994. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3995. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3996. I40E_QINT_RQCTL_INTEVENT_MASK);
  3997. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3998. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3999. wr32(hw, I40E_QINT_RQCTL(qp), val);
  4000. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4001. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  4002. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  4003. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4004. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4005. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4006. I40E_QINT_TQCTL_INTEVENT_MASK);
  4007. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4008. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4009. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4010. qp = next;
  4011. }
  4012. }
  4013. } else {
  4014. free_irq(pf->pdev->irq, pf);
  4015. val = rd32(hw, I40E_PFINT_LNKLST0);
  4016. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  4017. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  4018. val |= I40E_QUEUE_END_OF_LIST
  4019. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  4020. wr32(hw, I40E_PFINT_LNKLST0, val);
  4021. val = rd32(hw, I40E_QINT_RQCTL(qp));
  4022. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  4023. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  4024. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  4025. I40E_QINT_RQCTL_INTEVENT_MASK);
  4026. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  4027. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  4028. wr32(hw, I40E_QINT_RQCTL(qp), val);
  4029. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4030. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4031. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4032. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4033. I40E_QINT_TQCTL_INTEVENT_MASK);
  4034. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4035. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4036. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4037. }
  4038. }
  4039. /**
  4040. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  4041. * @vsi: the VSI being configured
  4042. * @v_idx: Index of vector to be freed
  4043. *
  4044. * This function frees the memory allocated to the q_vector. In addition if
  4045. * NAPI is enabled it will delete any references to the NAPI struct prior
  4046. * to freeing the q_vector.
  4047. **/
  4048. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  4049. {
  4050. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  4051. struct i40e_ring *ring;
  4052. if (!q_vector)
  4053. return;
  4054. /* disassociate q_vector from rings */
  4055. i40e_for_each_ring(ring, q_vector->tx)
  4056. ring->q_vector = NULL;
  4057. i40e_for_each_ring(ring, q_vector->rx)
  4058. ring->q_vector = NULL;
  4059. /* only VSI w/ an associated netdev is set up w/ NAPI */
  4060. if (vsi->netdev)
  4061. netif_napi_del(&q_vector->napi);
  4062. vsi->q_vectors[v_idx] = NULL;
  4063. kfree_rcu(q_vector, rcu);
  4064. }
  4065. /**
  4066. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  4067. * @vsi: the VSI being un-configured
  4068. *
  4069. * This frees the memory allocated to the q_vectors and
  4070. * deletes references to the NAPI struct.
  4071. **/
  4072. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  4073. {
  4074. int v_idx;
  4075. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  4076. i40e_free_q_vector(vsi, v_idx);
  4077. }
  4078. /**
  4079. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  4080. * @pf: board private structure
  4081. **/
  4082. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  4083. {
  4084. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  4085. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  4086. pci_disable_msix(pf->pdev);
  4087. kfree(pf->msix_entries);
  4088. pf->msix_entries = NULL;
  4089. kfree(pf->irq_pile);
  4090. pf->irq_pile = NULL;
  4091. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  4092. pci_disable_msi(pf->pdev);
  4093. }
  4094. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  4095. }
  4096. /**
  4097. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  4098. * @pf: board private structure
  4099. *
  4100. * We go through and clear interrupt specific resources and reset the structure
  4101. * to pre-load conditions
  4102. **/
  4103. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  4104. {
  4105. int i;
  4106. i40e_free_misc_vector(pf);
  4107. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  4108. I40E_IWARP_IRQ_PILE_ID);
  4109. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  4110. for (i = 0; i < pf->num_alloc_vsi; i++)
  4111. if (pf->vsi[i])
  4112. i40e_vsi_free_q_vectors(pf->vsi[i]);
  4113. i40e_reset_interrupt_capability(pf);
  4114. }
  4115. /**
  4116. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  4117. * @vsi: the VSI being configured
  4118. **/
  4119. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  4120. {
  4121. int q_idx;
  4122. if (!vsi->netdev)
  4123. return;
  4124. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4125. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4126. if (q_vector->rx.ring || q_vector->tx.ring)
  4127. napi_enable(&q_vector->napi);
  4128. }
  4129. }
  4130. /**
  4131. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  4132. * @vsi: the VSI being configured
  4133. **/
  4134. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  4135. {
  4136. int q_idx;
  4137. if (!vsi->netdev)
  4138. return;
  4139. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4140. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4141. if (q_vector->rx.ring || q_vector->tx.ring)
  4142. napi_disable(&q_vector->napi);
  4143. }
  4144. }
  4145. /**
  4146. * i40e_vsi_close - Shut down a VSI
  4147. * @vsi: the vsi to be quelled
  4148. **/
  4149. static void i40e_vsi_close(struct i40e_vsi *vsi)
  4150. {
  4151. struct i40e_pf *pf = vsi->back;
  4152. if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
  4153. i40e_down(vsi);
  4154. i40e_vsi_free_irq(vsi);
  4155. i40e_vsi_free_tx_resources(vsi);
  4156. i40e_vsi_free_rx_resources(vsi);
  4157. vsi->current_netdev_flags = 0;
  4158. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  4159. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  4160. set_bit(__I40E_CLIENT_RESET, pf->state);
  4161. }
  4162. /**
  4163. * i40e_quiesce_vsi - Pause a given VSI
  4164. * @vsi: the VSI being paused
  4165. **/
  4166. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  4167. {
  4168. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  4169. return;
  4170. set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
  4171. if (vsi->netdev && netif_running(vsi->netdev))
  4172. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4173. else
  4174. i40e_vsi_close(vsi);
  4175. }
  4176. /**
  4177. * i40e_unquiesce_vsi - Resume a given VSI
  4178. * @vsi: the VSI being resumed
  4179. **/
  4180. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  4181. {
  4182. if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
  4183. return;
  4184. if (vsi->netdev && netif_running(vsi->netdev))
  4185. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4186. else
  4187. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  4188. }
  4189. /**
  4190. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  4191. * @pf: the PF
  4192. **/
  4193. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  4194. {
  4195. int v;
  4196. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4197. if (pf->vsi[v])
  4198. i40e_quiesce_vsi(pf->vsi[v]);
  4199. }
  4200. }
  4201. /**
  4202. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  4203. * @pf: the PF
  4204. **/
  4205. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  4206. {
  4207. int v;
  4208. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4209. if (pf->vsi[v])
  4210. i40e_unquiesce_vsi(pf->vsi[v]);
  4211. }
  4212. }
  4213. /**
  4214. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  4215. * @vsi: the VSI being configured
  4216. *
  4217. * Wait until all queues on a given VSI have been disabled.
  4218. **/
  4219. int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  4220. {
  4221. struct i40e_pf *pf = vsi->back;
  4222. int i, pf_q, ret;
  4223. pf_q = vsi->base_queue;
  4224. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4225. /* Check and wait for the Tx queue */
  4226. ret = i40e_pf_txq_wait(pf, pf_q, false);
  4227. if (ret) {
  4228. dev_info(&pf->pdev->dev,
  4229. "VSI seid %d Tx ring %d disable timeout\n",
  4230. vsi->seid, pf_q);
  4231. return ret;
  4232. }
  4233. if (!i40e_enabled_xdp_vsi(vsi))
  4234. goto wait_rx;
  4235. /* Check and wait for the XDP Tx queue */
  4236. ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
  4237. false);
  4238. if (ret) {
  4239. dev_info(&pf->pdev->dev,
  4240. "VSI seid %d XDP Tx ring %d disable timeout\n",
  4241. vsi->seid, pf_q);
  4242. return ret;
  4243. }
  4244. wait_rx:
  4245. /* Check and wait for the Rx queue */
  4246. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  4247. if (ret) {
  4248. dev_info(&pf->pdev->dev,
  4249. "VSI seid %d Rx ring %d disable timeout\n",
  4250. vsi->seid, pf_q);
  4251. return ret;
  4252. }
  4253. }
  4254. return 0;
  4255. }
  4256. #ifdef CONFIG_I40E_DCB
  4257. /**
  4258. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  4259. * @pf: the PF
  4260. *
  4261. * This function waits for the queues to be in disabled state for all the
  4262. * VSIs that are managed by this PF.
  4263. **/
  4264. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  4265. {
  4266. int v, ret = 0;
  4267. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4268. if (pf->vsi[v]) {
  4269. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  4270. if (ret)
  4271. break;
  4272. }
  4273. }
  4274. return ret;
  4275. }
  4276. #endif
  4277. /**
  4278. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4279. * @pf: pointer to PF
  4280. *
  4281. * Get TC map for ISCSI PF type that will include iSCSI TC
  4282. * and LAN TC.
  4283. **/
  4284. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4285. {
  4286. struct i40e_dcb_app_priority_table app;
  4287. struct i40e_hw *hw = &pf->hw;
  4288. u8 enabled_tc = 1; /* TC0 is always enabled */
  4289. u8 tc, i;
  4290. /* Get the iSCSI APP TLV */
  4291. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4292. for (i = 0; i < dcbcfg->numapps; i++) {
  4293. app = dcbcfg->app[i];
  4294. if (app.selector == I40E_APP_SEL_TCPIP &&
  4295. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4296. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4297. enabled_tc |= BIT(tc);
  4298. break;
  4299. }
  4300. }
  4301. return enabled_tc;
  4302. }
  4303. /**
  4304. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4305. * @dcbcfg: the corresponding DCBx configuration structure
  4306. *
  4307. * Return the number of TCs from given DCBx configuration
  4308. **/
  4309. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4310. {
  4311. int i, tc_unused = 0;
  4312. u8 num_tc = 0;
  4313. u8 ret = 0;
  4314. /* Scan the ETS Config Priority Table to find
  4315. * traffic class enabled for a given priority
  4316. * and create a bitmask of enabled TCs
  4317. */
  4318. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4319. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4320. /* Now scan the bitmask to check for
  4321. * contiguous TCs starting with TC0
  4322. */
  4323. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4324. if (num_tc & BIT(i)) {
  4325. if (!tc_unused) {
  4326. ret++;
  4327. } else {
  4328. pr_err("Non-contiguous TC - Disabling DCB\n");
  4329. return 1;
  4330. }
  4331. } else {
  4332. tc_unused = 1;
  4333. }
  4334. }
  4335. /* There is always at least TC0 */
  4336. if (!ret)
  4337. ret = 1;
  4338. return ret;
  4339. }
  4340. /**
  4341. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4342. * @dcbcfg: the corresponding DCBx configuration structure
  4343. *
  4344. * Query the current DCB configuration and return the number of
  4345. * traffic classes enabled from the given DCBX config
  4346. **/
  4347. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4348. {
  4349. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4350. u8 enabled_tc = 1;
  4351. u8 i;
  4352. for (i = 0; i < num_tc; i++)
  4353. enabled_tc |= BIT(i);
  4354. return enabled_tc;
  4355. }
  4356. /**
  4357. * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
  4358. * @pf: PF being queried
  4359. *
  4360. * Query the current MQPRIO configuration and return the number of
  4361. * traffic classes enabled.
  4362. **/
  4363. static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
  4364. {
  4365. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  4366. u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
  4367. u8 enabled_tc = 1, i;
  4368. for (i = 1; i < num_tc; i++)
  4369. enabled_tc |= BIT(i);
  4370. return enabled_tc;
  4371. }
  4372. /**
  4373. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4374. * @pf: PF being queried
  4375. *
  4376. * Return number of traffic classes enabled for the given PF
  4377. **/
  4378. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4379. {
  4380. struct i40e_hw *hw = &pf->hw;
  4381. u8 i, enabled_tc = 1;
  4382. u8 num_tc = 0;
  4383. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4384. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4385. return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
  4386. /* If neither MQPRIO nor DCB is enabled, then always use single TC */
  4387. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4388. return 1;
  4389. /* SFP mode will be enabled for all TCs on port */
  4390. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4391. return i40e_dcb_get_num_tc(dcbcfg);
  4392. /* MFP mode return count of enabled TCs for this PF */
  4393. if (pf->hw.func_caps.iscsi)
  4394. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4395. else
  4396. return 1; /* Only TC0 */
  4397. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4398. if (enabled_tc & BIT(i))
  4399. num_tc++;
  4400. }
  4401. return num_tc;
  4402. }
  4403. /**
  4404. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4405. * @pf: PF being queried
  4406. *
  4407. * Return a bitmap for enabled traffic classes for this PF.
  4408. **/
  4409. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4410. {
  4411. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4412. return i40e_mqprio_get_enabled_tc(pf);
  4413. /* If neither MQPRIO nor DCB is enabled for this PF then just return
  4414. * default TC
  4415. */
  4416. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4417. return I40E_DEFAULT_TRAFFIC_CLASS;
  4418. /* SFP mode we want PF to be enabled for all TCs */
  4419. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4420. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4421. /* MFP enabled and iSCSI PF type */
  4422. if (pf->hw.func_caps.iscsi)
  4423. return i40e_get_iscsi_tc_map(pf);
  4424. else
  4425. return I40E_DEFAULT_TRAFFIC_CLASS;
  4426. }
  4427. /**
  4428. * i40e_vsi_get_bw_info - Query VSI BW Information
  4429. * @vsi: the VSI being queried
  4430. *
  4431. * Returns 0 on success, negative value on failure
  4432. **/
  4433. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4434. {
  4435. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4436. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4437. struct i40e_pf *pf = vsi->back;
  4438. struct i40e_hw *hw = &pf->hw;
  4439. i40e_status ret;
  4440. u32 tc_bw_max;
  4441. int i;
  4442. /* Get the VSI level BW configuration */
  4443. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4444. if (ret) {
  4445. dev_info(&pf->pdev->dev,
  4446. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4447. i40e_stat_str(&pf->hw, ret),
  4448. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4449. return -EINVAL;
  4450. }
  4451. /* Get the VSI level BW configuration per TC */
  4452. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4453. NULL);
  4454. if (ret) {
  4455. dev_info(&pf->pdev->dev,
  4456. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4457. i40e_stat_str(&pf->hw, ret),
  4458. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4459. return -EINVAL;
  4460. }
  4461. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4462. dev_info(&pf->pdev->dev,
  4463. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4464. bw_config.tc_valid_bits,
  4465. bw_ets_config.tc_valid_bits);
  4466. /* Still continuing */
  4467. }
  4468. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4469. vsi->bw_max_quanta = bw_config.max_bw;
  4470. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4471. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4472. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4473. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4474. vsi->bw_ets_limit_credits[i] =
  4475. le16_to_cpu(bw_ets_config.credits[i]);
  4476. /* 3 bits out of 4 for each TC */
  4477. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4478. }
  4479. return 0;
  4480. }
  4481. /**
  4482. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4483. * @vsi: the VSI being configured
  4484. * @enabled_tc: TC bitmap
  4485. * @bw_share: BW shared credits per TC
  4486. *
  4487. * Returns 0 on success, negative value on failure
  4488. **/
  4489. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4490. u8 *bw_share)
  4491. {
  4492. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4493. struct i40e_pf *pf = vsi->back;
  4494. i40e_status ret;
  4495. int i;
  4496. /* There is no need to reset BW when mqprio mode is on. */
  4497. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4498. return 0;
  4499. if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4500. ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
  4501. if (ret)
  4502. dev_info(&pf->pdev->dev,
  4503. "Failed to reset tx rate for vsi->seid %u\n",
  4504. vsi->seid);
  4505. return ret;
  4506. }
  4507. bw_data.tc_valid_bits = enabled_tc;
  4508. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4509. bw_data.tc_bw_credits[i] = bw_share[i];
  4510. ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
  4511. if (ret) {
  4512. dev_info(&pf->pdev->dev,
  4513. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4514. pf->hw.aq.asq_last_status);
  4515. return -EINVAL;
  4516. }
  4517. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4518. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4519. return 0;
  4520. }
  4521. /**
  4522. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4523. * @vsi: the VSI being configured
  4524. * @enabled_tc: TC map to be enabled
  4525. *
  4526. **/
  4527. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4528. {
  4529. struct net_device *netdev = vsi->netdev;
  4530. struct i40e_pf *pf = vsi->back;
  4531. struct i40e_hw *hw = &pf->hw;
  4532. u8 netdev_tc = 0;
  4533. int i;
  4534. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4535. if (!netdev)
  4536. return;
  4537. if (!enabled_tc) {
  4538. netdev_reset_tc(netdev);
  4539. return;
  4540. }
  4541. /* Set up actual enabled TCs on the VSI */
  4542. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4543. return;
  4544. /* set per TC queues for the VSI */
  4545. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4546. /* Only set TC queues for enabled tcs
  4547. *
  4548. * e.g. For a VSI that has TC0 and TC3 enabled the
  4549. * enabled_tc bitmap would be 0x00001001; the driver
  4550. * will set the numtc for netdev as 2 that will be
  4551. * referenced by the netdev layer as TC 0 and 1.
  4552. */
  4553. if (vsi->tc_config.enabled_tc & BIT(i))
  4554. netdev_set_tc_queue(netdev,
  4555. vsi->tc_config.tc_info[i].netdev_tc,
  4556. vsi->tc_config.tc_info[i].qcount,
  4557. vsi->tc_config.tc_info[i].qoffset);
  4558. }
  4559. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4560. return;
  4561. /* Assign UP2TC map for the VSI */
  4562. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4563. /* Get the actual TC# for the UP */
  4564. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4565. /* Get the mapped netdev TC# for the UP */
  4566. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4567. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4568. }
  4569. }
  4570. /**
  4571. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4572. * @vsi: the VSI being configured
  4573. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4574. **/
  4575. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4576. struct i40e_vsi_context *ctxt)
  4577. {
  4578. /* copy just the sections touched not the entire info
  4579. * since not all sections are valid as returned by
  4580. * update vsi params
  4581. */
  4582. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4583. memcpy(&vsi->info.queue_mapping,
  4584. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4585. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4586. sizeof(vsi->info.tc_mapping));
  4587. }
  4588. /**
  4589. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4590. * @vsi: VSI to be configured
  4591. * @enabled_tc: TC bitmap
  4592. *
  4593. * This configures a particular VSI for TCs that are mapped to the
  4594. * given TC bitmap. It uses default bandwidth share for TCs across
  4595. * VSIs to configure TC for a particular VSI.
  4596. *
  4597. * NOTE:
  4598. * It is expected that the VSI queues have been quisced before calling
  4599. * this function.
  4600. **/
  4601. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4602. {
  4603. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4604. struct i40e_pf *pf = vsi->back;
  4605. struct i40e_hw *hw = &pf->hw;
  4606. struct i40e_vsi_context ctxt;
  4607. int ret = 0;
  4608. int i;
  4609. /* Check if enabled_tc is same as existing or new TCs */
  4610. if (vsi->tc_config.enabled_tc == enabled_tc &&
  4611. vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
  4612. return ret;
  4613. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4614. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4615. if (enabled_tc & BIT(i))
  4616. bw_share[i] = 1;
  4617. }
  4618. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4619. if (ret) {
  4620. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4621. dev_info(&pf->pdev->dev,
  4622. "Failed configuring TC map %d for VSI %d\n",
  4623. enabled_tc, vsi->seid);
  4624. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
  4625. &bw_config, NULL);
  4626. if (ret) {
  4627. dev_info(&pf->pdev->dev,
  4628. "Failed querying vsi bw info, err %s aq_err %s\n",
  4629. i40e_stat_str(hw, ret),
  4630. i40e_aq_str(hw, hw->aq.asq_last_status));
  4631. goto out;
  4632. }
  4633. if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
  4634. u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
  4635. if (!valid_tc)
  4636. valid_tc = bw_config.tc_valid_bits;
  4637. /* Always enable TC0, no matter what */
  4638. valid_tc |= 1;
  4639. dev_info(&pf->pdev->dev,
  4640. "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
  4641. enabled_tc, bw_config.tc_valid_bits, valid_tc);
  4642. enabled_tc = valid_tc;
  4643. }
  4644. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4645. if (ret) {
  4646. dev_err(&pf->pdev->dev,
  4647. "Unable to configure TC map %d for VSI %d\n",
  4648. enabled_tc, vsi->seid);
  4649. goto out;
  4650. }
  4651. }
  4652. /* Update Queue Pairs Mapping for currently enabled UPs */
  4653. ctxt.seid = vsi->seid;
  4654. ctxt.pf_num = vsi->back->hw.pf_id;
  4655. ctxt.vf_num = 0;
  4656. ctxt.uplink_seid = vsi->uplink_seid;
  4657. ctxt.info = vsi->info;
  4658. if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
  4659. ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
  4660. if (ret)
  4661. goto out;
  4662. } else {
  4663. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4664. }
  4665. /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
  4666. * queues changed.
  4667. */
  4668. if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
  4669. vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
  4670. vsi->num_queue_pairs);
  4671. ret = i40e_vsi_config_rss(vsi);
  4672. if (ret) {
  4673. dev_info(&vsi->back->pdev->dev,
  4674. "Failed to reconfig rss for num_queues\n");
  4675. return ret;
  4676. }
  4677. vsi->reconfig_rss = false;
  4678. }
  4679. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4680. ctxt.info.valid_sections |=
  4681. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4682. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4683. }
  4684. /* Update the VSI after updating the VSI queue-mapping
  4685. * information
  4686. */
  4687. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  4688. if (ret) {
  4689. dev_info(&pf->pdev->dev,
  4690. "Update vsi tc config failed, err %s aq_err %s\n",
  4691. i40e_stat_str(hw, ret),
  4692. i40e_aq_str(hw, hw->aq.asq_last_status));
  4693. goto out;
  4694. }
  4695. /* update the local VSI info with updated queue map */
  4696. i40e_vsi_update_queue_map(vsi, &ctxt);
  4697. vsi->info.valid_sections = 0;
  4698. /* Update current VSI BW information */
  4699. ret = i40e_vsi_get_bw_info(vsi);
  4700. if (ret) {
  4701. dev_info(&pf->pdev->dev,
  4702. "Failed updating vsi bw info, err %s aq_err %s\n",
  4703. i40e_stat_str(hw, ret),
  4704. i40e_aq_str(hw, hw->aq.asq_last_status));
  4705. goto out;
  4706. }
  4707. /* Update the netdev TC setup */
  4708. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4709. out:
  4710. return ret;
  4711. }
  4712. /**
  4713. * i40e_get_link_speed - Returns link speed for the interface
  4714. * @vsi: VSI to be configured
  4715. *
  4716. **/
  4717. static int i40e_get_link_speed(struct i40e_vsi *vsi)
  4718. {
  4719. struct i40e_pf *pf = vsi->back;
  4720. switch (pf->hw.phy.link_info.link_speed) {
  4721. case I40E_LINK_SPEED_40GB:
  4722. return 40000;
  4723. case I40E_LINK_SPEED_25GB:
  4724. return 25000;
  4725. case I40E_LINK_SPEED_20GB:
  4726. return 20000;
  4727. case I40E_LINK_SPEED_10GB:
  4728. return 10000;
  4729. case I40E_LINK_SPEED_1GB:
  4730. return 1000;
  4731. default:
  4732. return -EINVAL;
  4733. }
  4734. }
  4735. /**
  4736. * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
  4737. * @vsi: VSI to be configured
  4738. * @seid: seid of the channel/VSI
  4739. * @max_tx_rate: max TX rate to be configured as BW limit
  4740. *
  4741. * Helper function to set BW limit for a given VSI
  4742. **/
  4743. int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
  4744. {
  4745. struct i40e_pf *pf = vsi->back;
  4746. u64 credits = 0;
  4747. int speed = 0;
  4748. int ret = 0;
  4749. speed = i40e_get_link_speed(vsi);
  4750. if (max_tx_rate > speed) {
  4751. dev_err(&pf->pdev->dev,
  4752. "Invalid max tx rate %llu specified for VSI seid %d.",
  4753. max_tx_rate, seid);
  4754. return -EINVAL;
  4755. }
  4756. if (max_tx_rate && max_tx_rate < 50) {
  4757. dev_warn(&pf->pdev->dev,
  4758. "Setting max tx rate to minimum usable value of 50Mbps.\n");
  4759. max_tx_rate = 50;
  4760. }
  4761. /* Tx rate credits are in values of 50Mbps, 0 is disabled */
  4762. credits = max_tx_rate;
  4763. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  4764. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
  4765. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  4766. if (ret)
  4767. dev_err(&pf->pdev->dev,
  4768. "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
  4769. max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
  4770. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4771. return ret;
  4772. }
  4773. /**
  4774. * i40e_remove_queue_channels - Remove queue channels for the TCs
  4775. * @vsi: VSI to be configured
  4776. *
  4777. * Remove queue channels for the TCs
  4778. **/
  4779. static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
  4780. {
  4781. enum i40e_admin_queue_err last_aq_status;
  4782. struct i40e_cloud_filter *cfilter;
  4783. struct i40e_channel *ch, *ch_tmp;
  4784. struct i40e_pf *pf = vsi->back;
  4785. struct hlist_node *node;
  4786. int ret, i;
  4787. /* Reset rss size that was stored when reconfiguring rss for
  4788. * channel VSIs with non-power-of-2 queue count.
  4789. */
  4790. vsi->current_rss_size = 0;
  4791. /* perform cleanup for channels if they exist */
  4792. if (list_empty(&vsi->ch_list))
  4793. return;
  4794. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4795. struct i40e_vsi *p_vsi;
  4796. list_del(&ch->list);
  4797. p_vsi = ch->parent_vsi;
  4798. if (!p_vsi || !ch->initialized) {
  4799. kfree(ch);
  4800. continue;
  4801. }
  4802. /* Reset queue contexts */
  4803. for (i = 0; i < ch->num_queue_pairs; i++) {
  4804. struct i40e_ring *tx_ring, *rx_ring;
  4805. u16 pf_q;
  4806. pf_q = ch->base_queue + i;
  4807. tx_ring = vsi->tx_rings[pf_q];
  4808. tx_ring->ch = NULL;
  4809. rx_ring = vsi->rx_rings[pf_q];
  4810. rx_ring->ch = NULL;
  4811. }
  4812. /* Reset BW configured for this VSI via mqprio */
  4813. ret = i40e_set_bw_limit(vsi, ch->seid, 0);
  4814. if (ret)
  4815. dev_info(&vsi->back->pdev->dev,
  4816. "Failed to reset tx rate for ch->seid %u\n",
  4817. ch->seid);
  4818. /* delete cloud filters associated with this channel */
  4819. hlist_for_each_entry_safe(cfilter, node,
  4820. &pf->cloud_filter_list, cloud_node) {
  4821. if (cfilter->seid != ch->seid)
  4822. continue;
  4823. hash_del(&cfilter->cloud_node);
  4824. if (cfilter->dst_port)
  4825. ret = i40e_add_del_cloud_filter_big_buf(vsi,
  4826. cfilter,
  4827. false);
  4828. else
  4829. ret = i40e_add_del_cloud_filter(vsi, cfilter,
  4830. false);
  4831. last_aq_status = pf->hw.aq.asq_last_status;
  4832. if (ret)
  4833. dev_info(&pf->pdev->dev,
  4834. "Failed to delete cloud filter, err %s aq_err %s\n",
  4835. i40e_stat_str(&pf->hw, ret),
  4836. i40e_aq_str(&pf->hw, last_aq_status));
  4837. kfree(cfilter);
  4838. }
  4839. /* delete VSI from FW */
  4840. ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
  4841. NULL);
  4842. if (ret)
  4843. dev_err(&vsi->back->pdev->dev,
  4844. "unable to remove channel (%d) for parent VSI(%d)\n",
  4845. ch->seid, p_vsi->seid);
  4846. kfree(ch);
  4847. }
  4848. INIT_LIST_HEAD(&vsi->ch_list);
  4849. }
  4850. /**
  4851. * i40e_is_any_channel - channel exist or not
  4852. * @vsi: ptr to VSI to which channels are associated with
  4853. *
  4854. * Returns true or false if channel(s) exist for associated VSI or not
  4855. **/
  4856. static bool i40e_is_any_channel(struct i40e_vsi *vsi)
  4857. {
  4858. struct i40e_channel *ch, *ch_tmp;
  4859. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4860. if (ch->initialized)
  4861. return true;
  4862. }
  4863. return false;
  4864. }
  4865. /**
  4866. * i40e_get_max_queues_for_channel
  4867. * @vsi: ptr to VSI to which channels are associated with
  4868. *
  4869. * Helper function which returns max value among the queue counts set on the
  4870. * channels/TCs created.
  4871. **/
  4872. static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
  4873. {
  4874. struct i40e_channel *ch, *ch_tmp;
  4875. int max = 0;
  4876. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4877. if (!ch->initialized)
  4878. continue;
  4879. if (ch->num_queue_pairs > max)
  4880. max = ch->num_queue_pairs;
  4881. }
  4882. return max;
  4883. }
  4884. /**
  4885. * i40e_validate_num_queues - validate num_queues w.r.t channel
  4886. * @pf: ptr to PF device
  4887. * @num_queues: number of queues
  4888. * @vsi: the parent VSI
  4889. * @reconfig_rss: indicates should the RSS be reconfigured or not
  4890. *
  4891. * This function validates number of queues in the context of new channel
  4892. * which is being established and determines if RSS should be reconfigured
  4893. * or not for parent VSI.
  4894. **/
  4895. static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
  4896. struct i40e_vsi *vsi, bool *reconfig_rss)
  4897. {
  4898. int max_ch_queues;
  4899. if (!reconfig_rss)
  4900. return -EINVAL;
  4901. *reconfig_rss = false;
  4902. if (vsi->current_rss_size) {
  4903. if (num_queues > vsi->current_rss_size) {
  4904. dev_dbg(&pf->pdev->dev,
  4905. "Error: num_queues (%d) > vsi's current_size(%d)\n",
  4906. num_queues, vsi->current_rss_size);
  4907. return -EINVAL;
  4908. } else if ((num_queues < vsi->current_rss_size) &&
  4909. (!is_power_of_2(num_queues))) {
  4910. dev_dbg(&pf->pdev->dev,
  4911. "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
  4912. num_queues, vsi->current_rss_size);
  4913. return -EINVAL;
  4914. }
  4915. }
  4916. if (!is_power_of_2(num_queues)) {
  4917. /* Find the max num_queues configured for channel if channel
  4918. * exist.
  4919. * if channel exist, then enforce 'num_queues' to be more than
  4920. * max ever queues configured for channel.
  4921. */
  4922. max_ch_queues = i40e_get_max_queues_for_channel(vsi);
  4923. if (num_queues < max_ch_queues) {
  4924. dev_dbg(&pf->pdev->dev,
  4925. "Error: num_queues (%d) < max queues configured for channel(%d)\n",
  4926. num_queues, max_ch_queues);
  4927. return -EINVAL;
  4928. }
  4929. *reconfig_rss = true;
  4930. }
  4931. return 0;
  4932. }
  4933. /**
  4934. * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
  4935. * @vsi: the VSI being setup
  4936. * @rss_size: size of RSS, accordingly LUT gets reprogrammed
  4937. *
  4938. * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
  4939. **/
  4940. static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
  4941. {
  4942. struct i40e_pf *pf = vsi->back;
  4943. u8 seed[I40E_HKEY_ARRAY_SIZE];
  4944. struct i40e_hw *hw = &pf->hw;
  4945. int local_rss_size;
  4946. u8 *lut;
  4947. int ret;
  4948. if (!vsi->rss_size)
  4949. return -EINVAL;
  4950. if (rss_size > vsi->rss_size)
  4951. return -EINVAL;
  4952. local_rss_size = min_t(int, vsi->rss_size, rss_size);
  4953. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  4954. if (!lut)
  4955. return -ENOMEM;
  4956. /* Ignoring user configured lut if there is one */
  4957. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
  4958. /* Use user configured hash key if there is one, otherwise
  4959. * use default.
  4960. */
  4961. if (vsi->rss_hkey_user)
  4962. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  4963. else
  4964. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  4965. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  4966. if (ret) {
  4967. dev_info(&pf->pdev->dev,
  4968. "Cannot set RSS lut, err %s aq_err %s\n",
  4969. i40e_stat_str(hw, ret),
  4970. i40e_aq_str(hw, hw->aq.asq_last_status));
  4971. kfree(lut);
  4972. return ret;
  4973. }
  4974. kfree(lut);
  4975. /* Do the update w.r.t. storing rss_size */
  4976. if (!vsi->orig_rss_size)
  4977. vsi->orig_rss_size = vsi->rss_size;
  4978. vsi->current_rss_size = local_rss_size;
  4979. return ret;
  4980. }
  4981. /**
  4982. * i40e_channel_setup_queue_map - Setup a channel queue map
  4983. * @pf: ptr to PF device
  4984. * @vsi: the VSI being setup
  4985. * @ctxt: VSI context structure
  4986. * @ch: ptr to channel structure
  4987. *
  4988. * Setup queue map for a specific channel
  4989. **/
  4990. static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
  4991. struct i40e_vsi_context *ctxt,
  4992. struct i40e_channel *ch)
  4993. {
  4994. u16 qcount, qmap, sections = 0;
  4995. u8 offset = 0;
  4996. int pow;
  4997. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  4998. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  4999. qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
  5000. ch->num_queue_pairs = qcount;
  5001. /* find the next higher power-of-2 of num queue pairs */
  5002. pow = ilog2(qcount);
  5003. if (!is_power_of_2(qcount))
  5004. pow++;
  5005. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  5006. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  5007. /* Setup queue TC[0].qmap for given VSI context */
  5008. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  5009. ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
  5010. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  5011. ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
  5012. ctxt->info.valid_sections |= cpu_to_le16(sections);
  5013. }
  5014. /**
  5015. * i40e_add_channel - add a channel by adding VSI
  5016. * @pf: ptr to PF device
  5017. * @uplink_seid: underlying HW switching element (VEB) ID
  5018. * @ch: ptr to channel structure
  5019. *
  5020. * Add a channel (VSI) using add_vsi and queue_map
  5021. **/
  5022. static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
  5023. struct i40e_channel *ch)
  5024. {
  5025. struct i40e_hw *hw = &pf->hw;
  5026. struct i40e_vsi_context ctxt;
  5027. u8 enabled_tc = 0x1; /* TC0 enabled */
  5028. int ret;
  5029. if (ch->type != I40E_VSI_VMDQ2) {
  5030. dev_info(&pf->pdev->dev,
  5031. "add new vsi failed, ch->type %d\n", ch->type);
  5032. return -EINVAL;
  5033. }
  5034. memset(&ctxt, 0, sizeof(ctxt));
  5035. ctxt.pf_num = hw->pf_id;
  5036. ctxt.vf_num = 0;
  5037. ctxt.uplink_seid = uplink_seid;
  5038. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  5039. if (ch->type == I40E_VSI_VMDQ2)
  5040. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  5041. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
  5042. ctxt.info.valid_sections |=
  5043. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5044. ctxt.info.switch_id =
  5045. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5046. }
  5047. /* Set queue map for a given VSI context */
  5048. i40e_channel_setup_queue_map(pf, &ctxt, ch);
  5049. /* Now time to create VSI */
  5050. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  5051. if (ret) {
  5052. dev_info(&pf->pdev->dev,
  5053. "add new vsi failed, err %s aq_err %s\n",
  5054. i40e_stat_str(&pf->hw, ret),
  5055. i40e_aq_str(&pf->hw,
  5056. pf->hw.aq.asq_last_status));
  5057. return -ENOENT;
  5058. }
  5059. /* Success, update channel */
  5060. ch->enabled_tc = enabled_tc;
  5061. ch->seid = ctxt.seid;
  5062. ch->vsi_number = ctxt.vsi_number;
  5063. ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
  5064. /* copy just the sections touched not the entire info
  5065. * since not all sections are valid as returned by
  5066. * update vsi params
  5067. */
  5068. ch->info.mapping_flags = ctxt.info.mapping_flags;
  5069. memcpy(&ch->info.queue_mapping,
  5070. &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
  5071. memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
  5072. sizeof(ctxt.info.tc_mapping));
  5073. return 0;
  5074. }
  5075. static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
  5076. u8 *bw_share)
  5077. {
  5078. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  5079. i40e_status ret;
  5080. int i;
  5081. bw_data.tc_valid_bits = ch->enabled_tc;
  5082. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5083. bw_data.tc_bw_credits[i] = bw_share[i];
  5084. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
  5085. &bw_data, NULL);
  5086. if (ret) {
  5087. dev_info(&vsi->back->pdev->dev,
  5088. "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
  5089. vsi->back->hw.aq.asq_last_status, ch->seid);
  5090. return -EINVAL;
  5091. }
  5092. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5093. ch->info.qs_handle[i] = bw_data.qs_handles[i];
  5094. return 0;
  5095. }
  5096. /**
  5097. * i40e_channel_config_tx_ring - config TX ring associated with new channel
  5098. * @pf: ptr to PF device
  5099. * @vsi: the VSI being setup
  5100. * @ch: ptr to channel structure
  5101. *
  5102. * Configure TX rings associated with channel (VSI) since queues are being
  5103. * from parent VSI.
  5104. **/
  5105. static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
  5106. struct i40e_vsi *vsi,
  5107. struct i40e_channel *ch)
  5108. {
  5109. i40e_status ret;
  5110. int i;
  5111. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  5112. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  5113. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5114. if (ch->enabled_tc & BIT(i))
  5115. bw_share[i] = 1;
  5116. }
  5117. /* configure BW for new VSI */
  5118. ret = i40e_channel_config_bw(vsi, ch, bw_share);
  5119. if (ret) {
  5120. dev_info(&vsi->back->pdev->dev,
  5121. "Failed configuring TC map %d for channel (seid %u)\n",
  5122. ch->enabled_tc, ch->seid);
  5123. return ret;
  5124. }
  5125. for (i = 0; i < ch->num_queue_pairs; i++) {
  5126. struct i40e_ring *tx_ring, *rx_ring;
  5127. u16 pf_q;
  5128. pf_q = ch->base_queue + i;
  5129. /* Get to TX ring ptr of main VSI, for re-setup TX queue
  5130. * context
  5131. */
  5132. tx_ring = vsi->tx_rings[pf_q];
  5133. tx_ring->ch = ch;
  5134. /* Get the RX ring ptr */
  5135. rx_ring = vsi->rx_rings[pf_q];
  5136. rx_ring->ch = ch;
  5137. }
  5138. return 0;
  5139. }
  5140. /**
  5141. * i40e_setup_hw_channel - setup new channel
  5142. * @pf: ptr to PF device
  5143. * @vsi: the VSI being setup
  5144. * @ch: ptr to channel structure
  5145. * @uplink_seid: underlying HW switching element (VEB) ID
  5146. * @type: type of channel to be created (VMDq2/VF)
  5147. *
  5148. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5149. * and configures TX rings accordingly
  5150. **/
  5151. static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
  5152. struct i40e_vsi *vsi,
  5153. struct i40e_channel *ch,
  5154. u16 uplink_seid, u8 type)
  5155. {
  5156. int ret;
  5157. ch->initialized = false;
  5158. ch->base_queue = vsi->next_base_queue;
  5159. ch->type = type;
  5160. /* Proceed with creation of channel (VMDq2) VSI */
  5161. ret = i40e_add_channel(pf, uplink_seid, ch);
  5162. if (ret) {
  5163. dev_info(&pf->pdev->dev,
  5164. "failed to add_channel using uplink_seid %u\n",
  5165. uplink_seid);
  5166. return ret;
  5167. }
  5168. /* Mark the successful creation of channel */
  5169. ch->initialized = true;
  5170. /* Reconfigure TX queues using QTX_CTL register */
  5171. ret = i40e_channel_config_tx_ring(pf, vsi, ch);
  5172. if (ret) {
  5173. dev_info(&pf->pdev->dev,
  5174. "failed to configure TX rings for channel %u\n",
  5175. ch->seid);
  5176. return ret;
  5177. }
  5178. /* update 'next_base_queue' */
  5179. vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
  5180. dev_dbg(&pf->pdev->dev,
  5181. "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
  5182. ch->seid, ch->vsi_number, ch->stat_counter_idx,
  5183. ch->num_queue_pairs,
  5184. vsi->next_base_queue);
  5185. return ret;
  5186. }
  5187. /**
  5188. * i40e_setup_channel - setup new channel using uplink element
  5189. * @pf: ptr to PF device
  5190. * @type: type of channel to be created (VMDq2/VF)
  5191. * @uplink_seid: underlying HW switching element (VEB) ID
  5192. * @ch: ptr to channel structure
  5193. *
  5194. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5195. * and uplink switching element (uplink_seid)
  5196. **/
  5197. static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
  5198. struct i40e_channel *ch)
  5199. {
  5200. u8 vsi_type;
  5201. u16 seid;
  5202. int ret;
  5203. if (vsi->type == I40E_VSI_MAIN) {
  5204. vsi_type = I40E_VSI_VMDQ2;
  5205. } else {
  5206. dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
  5207. vsi->type);
  5208. return false;
  5209. }
  5210. /* underlying switching element */
  5211. seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5212. /* create channel (VSI), configure TX rings */
  5213. ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
  5214. if (ret) {
  5215. dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
  5216. return false;
  5217. }
  5218. return ch->initialized ? true : false;
  5219. }
  5220. /**
  5221. * i40e_validate_and_set_switch_mode - sets up switch mode correctly
  5222. * @vsi: ptr to VSI which has PF backing
  5223. *
  5224. * Sets up switch mode correctly if it needs to be changed and perform
  5225. * what are allowed modes.
  5226. **/
  5227. static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
  5228. {
  5229. u8 mode;
  5230. struct i40e_pf *pf = vsi->back;
  5231. struct i40e_hw *hw = &pf->hw;
  5232. int ret;
  5233. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
  5234. if (ret)
  5235. return -EINVAL;
  5236. if (hw->dev_caps.switch_mode) {
  5237. /* if switch mode is set, support mode2 (non-tunneled for
  5238. * cloud filter) for now
  5239. */
  5240. u32 switch_mode = hw->dev_caps.switch_mode &
  5241. I40E_SWITCH_MODE_MASK;
  5242. if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
  5243. if (switch_mode == I40E_CLOUD_FILTER_MODE2)
  5244. return 0;
  5245. dev_err(&pf->pdev->dev,
  5246. "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
  5247. hw->dev_caps.switch_mode);
  5248. return -EINVAL;
  5249. }
  5250. }
  5251. /* Set Bit 7 to be valid */
  5252. mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
  5253. /* Set L4type for TCP support */
  5254. mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
  5255. /* Set cloud filter mode */
  5256. mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
  5257. /* Prep mode field for set_switch_config */
  5258. ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
  5259. pf->last_sw_conf_valid_flags,
  5260. mode, NULL);
  5261. if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
  5262. dev_err(&pf->pdev->dev,
  5263. "couldn't set switch config bits, err %s aq_err %s\n",
  5264. i40e_stat_str(hw, ret),
  5265. i40e_aq_str(hw,
  5266. hw->aq.asq_last_status));
  5267. return ret;
  5268. }
  5269. /**
  5270. * i40e_create_queue_channel - function to create channel
  5271. * @vsi: VSI to be configured
  5272. * @ch: ptr to channel (it contains channel specific params)
  5273. *
  5274. * This function creates channel (VSI) using num_queues specified by user,
  5275. * reconfigs RSS if needed.
  5276. **/
  5277. int i40e_create_queue_channel(struct i40e_vsi *vsi,
  5278. struct i40e_channel *ch)
  5279. {
  5280. struct i40e_pf *pf = vsi->back;
  5281. bool reconfig_rss;
  5282. int err;
  5283. if (!ch)
  5284. return -EINVAL;
  5285. if (!ch->num_queue_pairs) {
  5286. dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
  5287. ch->num_queue_pairs);
  5288. return -EINVAL;
  5289. }
  5290. /* validate user requested num_queues for channel */
  5291. err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
  5292. &reconfig_rss);
  5293. if (err) {
  5294. dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
  5295. ch->num_queue_pairs);
  5296. return -EINVAL;
  5297. }
  5298. /* By default we are in VEPA mode, if this is the first VF/VMDq
  5299. * VSI to be added switch to VEB mode.
  5300. */
  5301. if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
  5302. (!i40e_is_any_channel(vsi))) {
  5303. if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
  5304. dev_dbg(&pf->pdev->dev,
  5305. "Failed to create channel. Override queues (%u) not power of 2\n",
  5306. vsi->tc_config.tc_info[0].qcount);
  5307. return -EINVAL;
  5308. }
  5309. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  5310. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  5311. if (vsi->type == I40E_VSI_MAIN) {
  5312. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  5313. i40e_do_reset(pf, I40E_PF_RESET_FLAG,
  5314. true);
  5315. else
  5316. i40e_do_reset_safe(pf,
  5317. I40E_PF_RESET_FLAG);
  5318. }
  5319. }
  5320. /* now onwards for main VSI, number of queues will be value
  5321. * of TC0's queue count
  5322. */
  5323. }
  5324. /* By this time, vsi->cnt_q_avail shall be set to non-zero and
  5325. * it should be more than num_queues
  5326. */
  5327. if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
  5328. dev_dbg(&pf->pdev->dev,
  5329. "Error: cnt_q_avail (%u) less than num_queues %d\n",
  5330. vsi->cnt_q_avail, ch->num_queue_pairs);
  5331. return -EINVAL;
  5332. }
  5333. /* reconfig_rss only if vsi type is MAIN_VSI */
  5334. if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
  5335. err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
  5336. if (err) {
  5337. dev_info(&pf->pdev->dev,
  5338. "Error: unable to reconfig rss for num_queues (%u)\n",
  5339. ch->num_queue_pairs);
  5340. return -EINVAL;
  5341. }
  5342. }
  5343. if (!i40e_setup_channel(pf, vsi, ch)) {
  5344. dev_info(&pf->pdev->dev, "Failed to setup channel\n");
  5345. return -EINVAL;
  5346. }
  5347. dev_info(&pf->pdev->dev,
  5348. "Setup channel (id:%u) utilizing num_queues %d\n",
  5349. ch->seid, ch->num_queue_pairs);
  5350. /* configure VSI for BW limit */
  5351. if (ch->max_tx_rate) {
  5352. u64 credits = ch->max_tx_rate;
  5353. if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
  5354. return -EINVAL;
  5355. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5356. dev_dbg(&pf->pdev->dev,
  5357. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5358. ch->max_tx_rate,
  5359. credits,
  5360. ch->seid);
  5361. }
  5362. /* in case of VF, this will be main SRIOV VSI */
  5363. ch->parent_vsi = vsi;
  5364. /* and update main_vsi's count for queue_available to use */
  5365. vsi->cnt_q_avail -= ch->num_queue_pairs;
  5366. return 0;
  5367. }
  5368. /**
  5369. * i40e_configure_queue_channels - Add queue channel for the given TCs
  5370. * @vsi: VSI to be configured
  5371. *
  5372. * Configures queue channel mapping to the given TCs
  5373. **/
  5374. static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
  5375. {
  5376. struct i40e_channel *ch;
  5377. u64 max_rate = 0;
  5378. int ret = 0, i;
  5379. /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
  5380. vsi->tc_seid_map[0] = vsi->seid;
  5381. for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5382. if (vsi->tc_config.enabled_tc & BIT(i)) {
  5383. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  5384. if (!ch) {
  5385. ret = -ENOMEM;
  5386. goto err_free;
  5387. }
  5388. INIT_LIST_HEAD(&ch->list);
  5389. ch->num_queue_pairs =
  5390. vsi->tc_config.tc_info[i].qcount;
  5391. ch->base_queue =
  5392. vsi->tc_config.tc_info[i].qoffset;
  5393. /* Bandwidth limit through tc interface is in bytes/s,
  5394. * change to Mbit/s
  5395. */
  5396. max_rate = vsi->mqprio_qopt.max_rate[i];
  5397. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5398. ch->max_tx_rate = max_rate;
  5399. list_add_tail(&ch->list, &vsi->ch_list);
  5400. ret = i40e_create_queue_channel(vsi, ch);
  5401. if (ret) {
  5402. dev_err(&vsi->back->pdev->dev,
  5403. "Failed creating queue channel with TC%d: queues %d\n",
  5404. i, ch->num_queue_pairs);
  5405. goto err_free;
  5406. }
  5407. vsi->tc_seid_map[i] = ch->seid;
  5408. }
  5409. }
  5410. return ret;
  5411. err_free:
  5412. i40e_remove_queue_channels(vsi);
  5413. return ret;
  5414. }
  5415. /**
  5416. * i40e_veb_config_tc - Configure TCs for given VEB
  5417. * @veb: given VEB
  5418. * @enabled_tc: TC bitmap
  5419. *
  5420. * Configures given TC bitmap for VEB (switching) element
  5421. **/
  5422. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  5423. {
  5424. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  5425. struct i40e_pf *pf = veb->pf;
  5426. int ret = 0;
  5427. int i;
  5428. /* No TCs or already enabled TCs just return */
  5429. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  5430. return ret;
  5431. bw_data.tc_valid_bits = enabled_tc;
  5432. /* bw_data.absolute_credits is not set (relative) */
  5433. /* Enable ETS TCs with equal BW Share for now */
  5434. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5435. if (enabled_tc & BIT(i))
  5436. bw_data.tc_bw_share_credits[i] = 1;
  5437. }
  5438. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  5439. &bw_data, NULL);
  5440. if (ret) {
  5441. dev_info(&pf->pdev->dev,
  5442. "VEB bw config failed, err %s aq_err %s\n",
  5443. i40e_stat_str(&pf->hw, ret),
  5444. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5445. goto out;
  5446. }
  5447. /* Update the BW information */
  5448. ret = i40e_veb_get_bw_info(veb);
  5449. if (ret) {
  5450. dev_info(&pf->pdev->dev,
  5451. "Failed getting veb bw config, err %s aq_err %s\n",
  5452. i40e_stat_str(&pf->hw, ret),
  5453. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5454. }
  5455. out:
  5456. return ret;
  5457. }
  5458. #ifdef CONFIG_I40E_DCB
  5459. /**
  5460. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  5461. * @pf: PF struct
  5462. *
  5463. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  5464. * the caller would've quiesce all the VSIs before calling
  5465. * this function
  5466. **/
  5467. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  5468. {
  5469. u8 tc_map = 0;
  5470. int ret;
  5471. u8 v;
  5472. /* Enable the TCs available on PF to all VEBs */
  5473. tc_map = i40e_pf_get_tc_map(pf);
  5474. for (v = 0; v < I40E_MAX_VEB; v++) {
  5475. if (!pf->veb[v])
  5476. continue;
  5477. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  5478. if (ret) {
  5479. dev_info(&pf->pdev->dev,
  5480. "Failed configuring TC for VEB seid=%d\n",
  5481. pf->veb[v]->seid);
  5482. /* Will try to configure as many components */
  5483. }
  5484. }
  5485. /* Update each VSI */
  5486. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5487. if (!pf->vsi[v])
  5488. continue;
  5489. /* - Enable all TCs for the LAN VSI
  5490. * - For all others keep them at TC0 for now
  5491. */
  5492. if (v == pf->lan_vsi)
  5493. tc_map = i40e_pf_get_tc_map(pf);
  5494. else
  5495. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  5496. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  5497. if (ret) {
  5498. dev_info(&pf->pdev->dev,
  5499. "Failed configuring TC for VSI seid=%d\n",
  5500. pf->vsi[v]->seid);
  5501. /* Will try to configure as many components */
  5502. } else {
  5503. /* Re-configure VSI vectors based on updated TC map */
  5504. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  5505. if (pf->vsi[v]->netdev)
  5506. i40e_dcbnl_set_all(pf->vsi[v]);
  5507. }
  5508. }
  5509. }
  5510. /**
  5511. * i40e_resume_port_tx - Resume port Tx
  5512. * @pf: PF struct
  5513. *
  5514. * Resume a port's Tx and issue a PF reset in case of failure to
  5515. * resume.
  5516. **/
  5517. static int i40e_resume_port_tx(struct i40e_pf *pf)
  5518. {
  5519. struct i40e_hw *hw = &pf->hw;
  5520. int ret;
  5521. ret = i40e_aq_resume_port_tx(hw, NULL);
  5522. if (ret) {
  5523. dev_info(&pf->pdev->dev,
  5524. "Resume Port Tx failed, err %s aq_err %s\n",
  5525. i40e_stat_str(&pf->hw, ret),
  5526. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5527. /* Schedule PF reset to recover */
  5528. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  5529. i40e_service_event_schedule(pf);
  5530. }
  5531. return ret;
  5532. }
  5533. /**
  5534. * i40e_init_pf_dcb - Initialize DCB configuration
  5535. * @pf: PF being configured
  5536. *
  5537. * Query the current DCB configuration and cache it
  5538. * in the hardware structure
  5539. **/
  5540. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  5541. {
  5542. struct i40e_hw *hw = &pf->hw;
  5543. int err = 0;
  5544. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable
  5545. * Also do not enable DCBx if FW LLDP agent is disabled
  5546. */
  5547. if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
  5548. (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
  5549. goto out;
  5550. /* Get the initial DCB configuration */
  5551. err = i40e_init_dcb(hw);
  5552. if (!err) {
  5553. /* Device/Function is not DCBX capable */
  5554. if ((!hw->func_caps.dcb) ||
  5555. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  5556. dev_info(&pf->pdev->dev,
  5557. "DCBX offload is not supported or is disabled for this PF.\n");
  5558. } else {
  5559. /* When status is not DISABLED then DCBX in FW */
  5560. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  5561. DCB_CAP_DCBX_VER_IEEE;
  5562. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  5563. /* Enable DCB tagging only when more than one TC
  5564. * or explicitly disable if only one TC
  5565. */
  5566. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5567. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5568. else
  5569. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5570. dev_dbg(&pf->pdev->dev,
  5571. "DCBX offload is supported for this PF.\n");
  5572. }
  5573. } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
  5574. dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
  5575. pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
  5576. } else {
  5577. dev_info(&pf->pdev->dev,
  5578. "Query for DCB configuration failed, err %s aq_err %s\n",
  5579. i40e_stat_str(&pf->hw, err),
  5580. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5581. }
  5582. out:
  5583. return err;
  5584. }
  5585. #endif /* CONFIG_I40E_DCB */
  5586. #define SPEED_SIZE 14
  5587. #define FC_SIZE 8
  5588. /**
  5589. * i40e_print_link_message - print link up or down
  5590. * @vsi: the VSI for which link needs a message
  5591. * @isup: true of link is up, false otherwise
  5592. */
  5593. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  5594. {
  5595. enum i40e_aq_link_speed new_speed;
  5596. struct i40e_pf *pf = vsi->back;
  5597. char *speed = "Unknown";
  5598. char *fc = "Unknown";
  5599. char *fec = "";
  5600. char *req_fec = "";
  5601. char *an = "";
  5602. new_speed = pf->hw.phy.link_info.link_speed;
  5603. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  5604. return;
  5605. vsi->current_isup = isup;
  5606. vsi->current_speed = new_speed;
  5607. if (!isup) {
  5608. netdev_info(vsi->netdev, "NIC Link is Down\n");
  5609. return;
  5610. }
  5611. /* Warn user if link speed on NPAR enabled partition is not at
  5612. * least 10GB
  5613. */
  5614. if (pf->hw.func_caps.npar_enable &&
  5615. (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  5616. pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  5617. netdev_warn(vsi->netdev,
  5618. "The partition detected link speed that is less than 10Gbps\n");
  5619. switch (pf->hw.phy.link_info.link_speed) {
  5620. case I40E_LINK_SPEED_40GB:
  5621. speed = "40 G";
  5622. break;
  5623. case I40E_LINK_SPEED_20GB:
  5624. speed = "20 G";
  5625. break;
  5626. case I40E_LINK_SPEED_25GB:
  5627. speed = "25 G";
  5628. break;
  5629. case I40E_LINK_SPEED_10GB:
  5630. speed = "10 G";
  5631. break;
  5632. case I40E_LINK_SPEED_1GB:
  5633. speed = "1000 M";
  5634. break;
  5635. case I40E_LINK_SPEED_100MB:
  5636. speed = "100 M";
  5637. break;
  5638. default:
  5639. break;
  5640. }
  5641. switch (pf->hw.fc.current_mode) {
  5642. case I40E_FC_FULL:
  5643. fc = "RX/TX";
  5644. break;
  5645. case I40E_FC_TX_PAUSE:
  5646. fc = "TX";
  5647. break;
  5648. case I40E_FC_RX_PAUSE:
  5649. fc = "RX";
  5650. break;
  5651. default:
  5652. fc = "None";
  5653. break;
  5654. }
  5655. if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  5656. req_fec = ", Requested FEC: None";
  5657. fec = ", FEC: None";
  5658. an = ", Autoneg: False";
  5659. if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  5660. an = ", Autoneg: True";
  5661. if (pf->hw.phy.link_info.fec_info &
  5662. I40E_AQ_CONFIG_FEC_KR_ENA)
  5663. fec = ", FEC: CL74 FC-FEC/BASE-R";
  5664. else if (pf->hw.phy.link_info.fec_info &
  5665. I40E_AQ_CONFIG_FEC_RS_ENA)
  5666. fec = ", FEC: CL108 RS-FEC";
  5667. /* 'CL108 RS-FEC' should be displayed when RS is requested, or
  5668. * both RS and FC are requested
  5669. */
  5670. if (vsi->back->hw.phy.link_info.req_fec_info &
  5671. (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
  5672. if (vsi->back->hw.phy.link_info.req_fec_info &
  5673. I40E_AQ_REQUEST_FEC_RS)
  5674. req_fec = ", Requested FEC: CL108 RS-FEC";
  5675. else
  5676. req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
  5677. }
  5678. }
  5679. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
  5680. speed, req_fec, fec, an, fc);
  5681. }
  5682. /**
  5683. * i40e_up_complete - Finish the last steps of bringing up a connection
  5684. * @vsi: the VSI being configured
  5685. **/
  5686. static int i40e_up_complete(struct i40e_vsi *vsi)
  5687. {
  5688. struct i40e_pf *pf = vsi->back;
  5689. int err;
  5690. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5691. i40e_vsi_configure_msix(vsi);
  5692. else
  5693. i40e_configure_msi_and_legacy(vsi);
  5694. /* start rings */
  5695. err = i40e_vsi_start_rings(vsi);
  5696. if (err)
  5697. return err;
  5698. clear_bit(__I40E_VSI_DOWN, vsi->state);
  5699. i40e_napi_enable_all(vsi);
  5700. i40e_vsi_enable_irq(vsi);
  5701. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  5702. (vsi->netdev)) {
  5703. i40e_print_link_message(vsi, true);
  5704. netif_tx_start_all_queues(vsi->netdev);
  5705. netif_carrier_on(vsi->netdev);
  5706. }
  5707. /* replay FDIR SB filters */
  5708. if (vsi->type == I40E_VSI_FDIR) {
  5709. /* reset fd counters */
  5710. pf->fd_add_err = 0;
  5711. pf->fd_atr_cnt = 0;
  5712. i40e_fdir_filter_restore(vsi);
  5713. }
  5714. /* On the next run of the service_task, notify any clients of the new
  5715. * opened netdev
  5716. */
  5717. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  5718. i40e_service_event_schedule(pf);
  5719. return 0;
  5720. }
  5721. /**
  5722. * i40e_vsi_reinit_locked - Reset the VSI
  5723. * @vsi: the VSI being configured
  5724. *
  5725. * Rebuild the ring structs after some configuration
  5726. * has changed, e.g. MTU size.
  5727. **/
  5728. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  5729. {
  5730. struct i40e_pf *pf = vsi->back;
  5731. WARN_ON(in_interrupt());
  5732. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
  5733. usleep_range(1000, 2000);
  5734. i40e_down(vsi);
  5735. i40e_up(vsi);
  5736. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  5737. }
  5738. /**
  5739. * i40e_up - Bring the connection back up after being down
  5740. * @vsi: the VSI being configured
  5741. **/
  5742. int i40e_up(struct i40e_vsi *vsi)
  5743. {
  5744. int err;
  5745. err = i40e_vsi_configure(vsi);
  5746. if (!err)
  5747. err = i40e_up_complete(vsi);
  5748. return err;
  5749. }
  5750. /**
  5751. * i40e_force_link_state - Force the link status
  5752. * @pf: board private structure
  5753. * @is_up: whether the link state should be forced up or down
  5754. **/
  5755. static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
  5756. {
  5757. struct i40e_aq_get_phy_abilities_resp abilities;
  5758. struct i40e_aq_set_phy_config config = {0};
  5759. struct i40e_hw *hw = &pf->hw;
  5760. i40e_status err;
  5761. u64 mask;
  5762. u8 speed;
  5763. /* Card might've been put in an unstable state by other drivers
  5764. * and applications, which causes incorrect speed values being
  5765. * set on startup. In order to clear speed registers, we call
  5766. * get_phy_capabilities twice, once to get initial state of
  5767. * available speeds, and once to get current PHY config.
  5768. */
  5769. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
  5770. NULL);
  5771. if (err) {
  5772. dev_err(&pf->pdev->dev,
  5773. "failed to get phy cap., ret = %s last_status = %s\n",
  5774. i40e_stat_str(hw, err),
  5775. i40e_aq_str(hw, hw->aq.asq_last_status));
  5776. return err;
  5777. }
  5778. speed = abilities.link_speed;
  5779. /* Get the current phy config */
  5780. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
  5781. NULL);
  5782. if (err) {
  5783. dev_err(&pf->pdev->dev,
  5784. "failed to get phy cap., ret = %s last_status = %s\n",
  5785. i40e_stat_str(hw, err),
  5786. i40e_aq_str(hw, hw->aq.asq_last_status));
  5787. return err;
  5788. }
  5789. /* If link needs to go up, but was not forced to go down,
  5790. * and its speed values are OK, no need for a flap
  5791. */
  5792. if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
  5793. return I40E_SUCCESS;
  5794. /* To force link we need to set bits for all supported PHY types,
  5795. * but there are now more than 32, so we need to split the bitmap
  5796. * across two fields.
  5797. */
  5798. mask = I40E_PHY_TYPES_BITMASK;
  5799. config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
  5800. config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
  5801. /* Copy the old settings, except of phy_type */
  5802. config.abilities = abilities.abilities;
  5803. if (abilities.link_speed != 0)
  5804. config.link_speed = abilities.link_speed;
  5805. else
  5806. config.link_speed = speed;
  5807. config.eee_capability = abilities.eee_capability;
  5808. config.eeer = abilities.eeer_val;
  5809. config.low_power_ctrl = abilities.d3_lpan;
  5810. config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
  5811. I40E_AQ_PHY_FEC_CONFIG_MASK;
  5812. err = i40e_aq_set_phy_config(hw, &config, NULL);
  5813. if (err) {
  5814. dev_err(&pf->pdev->dev,
  5815. "set phy config ret = %s last_status = %s\n",
  5816. i40e_stat_str(&pf->hw, err),
  5817. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5818. return err;
  5819. }
  5820. /* Update the link info */
  5821. err = i40e_update_link_info(hw);
  5822. if (err) {
  5823. /* Wait a little bit (on 40G cards it sometimes takes a really
  5824. * long time for link to come back from the atomic reset)
  5825. * and try once more
  5826. */
  5827. msleep(1000);
  5828. i40e_update_link_info(hw);
  5829. }
  5830. i40e_aq_set_link_restart_an(hw, true, NULL);
  5831. return I40E_SUCCESS;
  5832. }
  5833. /**
  5834. * i40e_down - Shutdown the connection processing
  5835. * @vsi: the VSI being stopped
  5836. **/
  5837. void i40e_down(struct i40e_vsi *vsi)
  5838. {
  5839. int i;
  5840. /* It is assumed that the caller of this function
  5841. * sets the vsi->state __I40E_VSI_DOWN bit.
  5842. */
  5843. if (vsi->netdev) {
  5844. netif_carrier_off(vsi->netdev);
  5845. netif_tx_disable(vsi->netdev);
  5846. }
  5847. i40e_vsi_disable_irq(vsi);
  5848. i40e_vsi_stop_rings(vsi);
  5849. if (vsi->type == I40E_VSI_MAIN &&
  5850. vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
  5851. i40e_force_link_state(vsi->back, false);
  5852. i40e_napi_disable_all(vsi);
  5853. for (i = 0; i < vsi->num_queue_pairs; i++) {
  5854. i40e_clean_tx_ring(vsi->tx_rings[i]);
  5855. if (i40e_enabled_xdp_vsi(vsi))
  5856. i40e_clean_tx_ring(vsi->xdp_rings[i]);
  5857. i40e_clean_rx_ring(vsi->rx_rings[i]);
  5858. }
  5859. }
  5860. /**
  5861. * i40e_validate_mqprio_qopt- validate queue mapping info
  5862. * @vsi: the VSI being configured
  5863. * @mqprio_qopt: queue parametrs
  5864. **/
  5865. static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
  5866. struct tc_mqprio_qopt_offload *mqprio_qopt)
  5867. {
  5868. u64 sum_max_rate = 0;
  5869. u64 max_rate = 0;
  5870. int i;
  5871. if (mqprio_qopt->qopt.offset[0] != 0 ||
  5872. mqprio_qopt->qopt.num_tc < 1 ||
  5873. mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
  5874. return -EINVAL;
  5875. for (i = 0; ; i++) {
  5876. if (!mqprio_qopt->qopt.count[i])
  5877. return -EINVAL;
  5878. if (mqprio_qopt->min_rate[i]) {
  5879. dev_err(&vsi->back->pdev->dev,
  5880. "Invalid min tx rate (greater than 0) specified\n");
  5881. return -EINVAL;
  5882. }
  5883. max_rate = mqprio_qopt->max_rate[i];
  5884. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5885. sum_max_rate += max_rate;
  5886. if (i >= mqprio_qopt->qopt.num_tc - 1)
  5887. break;
  5888. if (mqprio_qopt->qopt.offset[i + 1] !=
  5889. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
  5890. return -EINVAL;
  5891. }
  5892. if (vsi->num_queue_pairs <
  5893. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
  5894. return -EINVAL;
  5895. }
  5896. if (sum_max_rate > i40e_get_link_speed(vsi)) {
  5897. dev_err(&vsi->back->pdev->dev,
  5898. "Invalid max tx rate specified\n");
  5899. return -EINVAL;
  5900. }
  5901. return 0;
  5902. }
  5903. /**
  5904. * i40e_vsi_set_default_tc_config - set default values for tc configuration
  5905. * @vsi: the VSI being configured
  5906. **/
  5907. static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
  5908. {
  5909. u16 qcount;
  5910. int i;
  5911. /* Only TC0 is enabled */
  5912. vsi->tc_config.numtc = 1;
  5913. vsi->tc_config.enabled_tc = 1;
  5914. qcount = min_t(int, vsi->alloc_queue_pairs,
  5915. i40e_pf_get_max_q_per_tc(vsi->back));
  5916. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5917. /* For the TC that is not enabled set the offset to to default
  5918. * queue and allocate one queue for the given TC.
  5919. */
  5920. vsi->tc_config.tc_info[i].qoffset = 0;
  5921. if (i == 0)
  5922. vsi->tc_config.tc_info[i].qcount = qcount;
  5923. else
  5924. vsi->tc_config.tc_info[i].qcount = 1;
  5925. vsi->tc_config.tc_info[i].netdev_tc = 0;
  5926. }
  5927. }
  5928. /**
  5929. * i40e_setup_tc - configure multiple traffic classes
  5930. * @netdev: net device to configure
  5931. * @type_data: tc offload data
  5932. **/
  5933. static int i40e_setup_tc(struct net_device *netdev, void *type_data)
  5934. {
  5935. struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
  5936. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5937. struct i40e_vsi *vsi = np->vsi;
  5938. struct i40e_pf *pf = vsi->back;
  5939. u8 enabled_tc = 0, num_tc, hw;
  5940. bool need_reset = false;
  5941. int old_queue_pairs;
  5942. int ret = -EINVAL;
  5943. u16 mode;
  5944. int i;
  5945. old_queue_pairs = vsi->num_queue_pairs;
  5946. num_tc = mqprio_qopt->qopt.num_tc;
  5947. hw = mqprio_qopt->qopt.hw;
  5948. mode = mqprio_qopt->mode;
  5949. if (!hw) {
  5950. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5951. memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
  5952. goto config_tc;
  5953. }
  5954. /* Check if MFP enabled */
  5955. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  5956. netdev_info(netdev,
  5957. "Configuring TC not supported in MFP mode\n");
  5958. return ret;
  5959. }
  5960. switch (mode) {
  5961. case TC_MQPRIO_MODE_DCB:
  5962. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5963. /* Check if DCB enabled to continue */
  5964. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  5965. netdev_info(netdev,
  5966. "DCB is not enabled for adapter\n");
  5967. return ret;
  5968. }
  5969. /* Check whether tc count is within enabled limit */
  5970. if (num_tc > i40e_pf_get_num_tc(pf)) {
  5971. netdev_info(netdev,
  5972. "TC count greater than enabled on link for adapter\n");
  5973. return ret;
  5974. }
  5975. break;
  5976. case TC_MQPRIO_MODE_CHANNEL:
  5977. if (pf->flags & I40E_FLAG_DCB_ENABLED) {
  5978. netdev_info(netdev,
  5979. "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
  5980. return ret;
  5981. }
  5982. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  5983. return ret;
  5984. ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
  5985. if (ret)
  5986. return ret;
  5987. memcpy(&vsi->mqprio_qopt, mqprio_qopt,
  5988. sizeof(*mqprio_qopt));
  5989. pf->flags |= I40E_FLAG_TC_MQPRIO;
  5990. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5991. break;
  5992. default:
  5993. return -EINVAL;
  5994. }
  5995. config_tc:
  5996. /* Generate TC map for number of tc requested */
  5997. for (i = 0; i < num_tc; i++)
  5998. enabled_tc |= BIT(i);
  5999. /* Requesting same TC configuration as already enabled */
  6000. if (enabled_tc == vsi->tc_config.enabled_tc &&
  6001. mode != TC_MQPRIO_MODE_CHANNEL)
  6002. return 0;
  6003. /* Quiesce VSI queues */
  6004. i40e_quiesce_vsi(vsi);
  6005. if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
  6006. i40e_remove_queue_channels(vsi);
  6007. /* Configure VSI for enabled TCs */
  6008. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  6009. if (ret) {
  6010. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  6011. vsi->seid);
  6012. need_reset = true;
  6013. goto exit;
  6014. }
  6015. if (pf->flags & I40E_FLAG_TC_MQPRIO) {
  6016. if (vsi->mqprio_qopt.max_rate[0]) {
  6017. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  6018. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  6019. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  6020. if (!ret) {
  6021. u64 credits = max_tx_rate;
  6022. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  6023. dev_dbg(&vsi->back->pdev->dev,
  6024. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  6025. max_tx_rate,
  6026. credits,
  6027. vsi->seid);
  6028. } else {
  6029. need_reset = true;
  6030. goto exit;
  6031. }
  6032. }
  6033. ret = i40e_configure_queue_channels(vsi);
  6034. if (ret) {
  6035. vsi->num_queue_pairs = old_queue_pairs;
  6036. netdev_info(netdev,
  6037. "Failed configuring queue channels\n");
  6038. need_reset = true;
  6039. goto exit;
  6040. }
  6041. }
  6042. exit:
  6043. /* Reset the configuration data to defaults, only TC0 is enabled */
  6044. if (need_reset) {
  6045. i40e_vsi_set_default_tc_config(vsi);
  6046. need_reset = false;
  6047. }
  6048. /* Unquiesce VSI */
  6049. i40e_unquiesce_vsi(vsi);
  6050. return ret;
  6051. }
  6052. /**
  6053. * i40e_set_cld_element - sets cloud filter element data
  6054. * @filter: cloud filter rule
  6055. * @cld: ptr to cloud filter element data
  6056. *
  6057. * This is helper function to copy data into cloud filter element
  6058. **/
  6059. static inline void
  6060. i40e_set_cld_element(struct i40e_cloud_filter *filter,
  6061. struct i40e_aqc_cloud_filters_element_data *cld)
  6062. {
  6063. int i, j;
  6064. u32 ipa;
  6065. memset(cld, 0, sizeof(*cld));
  6066. ether_addr_copy(cld->outer_mac, filter->dst_mac);
  6067. ether_addr_copy(cld->inner_mac, filter->src_mac);
  6068. if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
  6069. return;
  6070. if (filter->n_proto == ETH_P_IPV6) {
  6071. #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
  6072. for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
  6073. i++, j += 2) {
  6074. ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
  6075. ipa = cpu_to_le32(ipa);
  6076. memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
  6077. }
  6078. } else {
  6079. ipa = be32_to_cpu(filter->dst_ipv4);
  6080. memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
  6081. }
  6082. cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
  6083. /* tenant_id is not supported by FW now, once the support is enabled
  6084. * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
  6085. */
  6086. if (filter->tenant_id)
  6087. return;
  6088. }
  6089. /**
  6090. * i40e_add_del_cloud_filter - Add/del cloud filter
  6091. * @vsi: pointer to VSI
  6092. * @filter: cloud filter rule
  6093. * @add: if true, add, if false, delete
  6094. *
  6095. * Add or delete a cloud filter for a specific flow spec.
  6096. * Returns 0 if the filter were successfully added.
  6097. **/
  6098. int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
  6099. struct i40e_cloud_filter *filter, bool add)
  6100. {
  6101. struct i40e_aqc_cloud_filters_element_data cld_filter;
  6102. struct i40e_pf *pf = vsi->back;
  6103. int ret;
  6104. static const u16 flag_table[128] = {
  6105. [I40E_CLOUD_FILTER_FLAGS_OMAC] =
  6106. I40E_AQC_ADD_CLOUD_FILTER_OMAC,
  6107. [I40E_CLOUD_FILTER_FLAGS_IMAC] =
  6108. I40E_AQC_ADD_CLOUD_FILTER_IMAC,
  6109. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
  6110. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
  6111. [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
  6112. I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
  6113. [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
  6114. I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
  6115. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
  6116. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
  6117. [I40E_CLOUD_FILTER_FLAGS_IIP] =
  6118. I40E_AQC_ADD_CLOUD_FILTER_IIP,
  6119. };
  6120. if (filter->flags >= ARRAY_SIZE(flag_table))
  6121. return I40E_ERR_CONFIG;
  6122. /* copy element needed to add cloud filter from filter */
  6123. i40e_set_cld_element(filter, &cld_filter);
  6124. if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
  6125. cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
  6126. I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
  6127. if (filter->n_proto == ETH_P_IPV6)
  6128. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6129. I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6130. else
  6131. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6132. I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6133. if (add)
  6134. ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
  6135. &cld_filter, 1);
  6136. else
  6137. ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
  6138. &cld_filter, 1);
  6139. if (ret)
  6140. dev_dbg(&pf->pdev->dev,
  6141. "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
  6142. add ? "add" : "delete", filter->dst_port, ret,
  6143. pf->hw.aq.asq_last_status);
  6144. else
  6145. dev_info(&pf->pdev->dev,
  6146. "%s cloud filter for VSI: %d\n",
  6147. add ? "Added" : "Deleted", filter->seid);
  6148. return ret;
  6149. }
  6150. /**
  6151. * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
  6152. * @vsi: pointer to VSI
  6153. * @filter: cloud filter rule
  6154. * @add: if true, add, if false, delete
  6155. *
  6156. * Add or delete a cloud filter for a specific flow spec using big buffer.
  6157. * Returns 0 if the filter were successfully added.
  6158. **/
  6159. int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
  6160. struct i40e_cloud_filter *filter,
  6161. bool add)
  6162. {
  6163. struct i40e_aqc_cloud_filters_element_bb cld_filter;
  6164. struct i40e_pf *pf = vsi->back;
  6165. int ret;
  6166. /* Both (src/dst) valid mac_addr are not supported */
  6167. if ((is_valid_ether_addr(filter->dst_mac) &&
  6168. is_valid_ether_addr(filter->src_mac)) ||
  6169. (is_multicast_ether_addr(filter->dst_mac) &&
  6170. is_multicast_ether_addr(filter->src_mac)))
  6171. return -EOPNOTSUPP;
  6172. /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
  6173. * ports are not supported via big buffer now.
  6174. */
  6175. if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
  6176. return -EOPNOTSUPP;
  6177. /* adding filter using src_port/src_ip is not supported at this stage */
  6178. if (filter->src_port || filter->src_ipv4 ||
  6179. !ipv6_addr_any(&filter->ip.v6.src_ip6))
  6180. return -EOPNOTSUPP;
  6181. /* copy element needed to add cloud filter from filter */
  6182. i40e_set_cld_element(filter, &cld_filter.element);
  6183. if (is_valid_ether_addr(filter->dst_mac) ||
  6184. is_valid_ether_addr(filter->src_mac) ||
  6185. is_multicast_ether_addr(filter->dst_mac) ||
  6186. is_multicast_ether_addr(filter->src_mac)) {
  6187. /* MAC + IP : unsupported mode */
  6188. if (filter->dst_ipv4)
  6189. return -EOPNOTSUPP;
  6190. /* since we validated that L4 port must be valid before
  6191. * we get here, start with respective "flags" value
  6192. * and update if vlan is present or not
  6193. */
  6194. cld_filter.element.flags =
  6195. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
  6196. if (filter->vlan_id) {
  6197. cld_filter.element.flags =
  6198. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
  6199. }
  6200. } else if (filter->dst_ipv4 ||
  6201. !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
  6202. cld_filter.element.flags =
  6203. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
  6204. if (filter->n_proto == ETH_P_IPV6)
  6205. cld_filter.element.flags |=
  6206. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6207. else
  6208. cld_filter.element.flags |=
  6209. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6210. } else {
  6211. dev_err(&pf->pdev->dev,
  6212. "either mac or ip has to be valid for cloud filter\n");
  6213. return -EINVAL;
  6214. }
  6215. /* Now copy L4 port in Byte 6..7 in general fields */
  6216. cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
  6217. be16_to_cpu(filter->dst_port);
  6218. if (add) {
  6219. /* Validate current device switch mode, change if necessary */
  6220. ret = i40e_validate_and_set_switch_mode(vsi);
  6221. if (ret) {
  6222. dev_err(&pf->pdev->dev,
  6223. "failed to set switch mode, ret %d\n",
  6224. ret);
  6225. return ret;
  6226. }
  6227. ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
  6228. &cld_filter, 1);
  6229. } else {
  6230. ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
  6231. &cld_filter, 1);
  6232. }
  6233. if (ret)
  6234. dev_dbg(&pf->pdev->dev,
  6235. "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
  6236. add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
  6237. else
  6238. dev_info(&pf->pdev->dev,
  6239. "%s cloud filter for VSI: %d, L4 port: %d\n",
  6240. add ? "add" : "delete", filter->seid,
  6241. ntohs(filter->dst_port));
  6242. return ret;
  6243. }
  6244. /**
  6245. * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
  6246. * @vsi: Pointer to VSI
  6247. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6248. * @filter: Pointer to cloud filter structure
  6249. *
  6250. **/
  6251. static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
  6252. struct tc_cls_flower_offload *f,
  6253. struct i40e_cloud_filter *filter)
  6254. {
  6255. u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
  6256. struct i40e_pf *pf = vsi->back;
  6257. u8 field_flags = 0;
  6258. if (f->dissector->used_keys &
  6259. ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  6260. BIT(FLOW_DISSECTOR_KEY_BASIC) |
  6261. BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
  6262. BIT(FLOW_DISSECTOR_KEY_VLAN) |
  6263. BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  6264. BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  6265. BIT(FLOW_DISSECTOR_KEY_PORTS) |
  6266. BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
  6267. dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
  6268. f->dissector->used_keys);
  6269. return -EOPNOTSUPP;
  6270. }
  6271. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  6272. struct flow_dissector_key_keyid *key =
  6273. skb_flow_dissector_target(f->dissector,
  6274. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6275. f->key);
  6276. struct flow_dissector_key_keyid *mask =
  6277. skb_flow_dissector_target(f->dissector,
  6278. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6279. f->mask);
  6280. if (mask->keyid != 0)
  6281. field_flags |= I40E_CLOUD_FIELD_TEN_ID;
  6282. filter->tenant_id = be32_to_cpu(key->keyid);
  6283. }
  6284. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  6285. struct flow_dissector_key_basic *key =
  6286. skb_flow_dissector_target(f->dissector,
  6287. FLOW_DISSECTOR_KEY_BASIC,
  6288. f->key);
  6289. struct flow_dissector_key_basic *mask =
  6290. skb_flow_dissector_target(f->dissector,
  6291. FLOW_DISSECTOR_KEY_BASIC,
  6292. f->mask);
  6293. n_proto_key = ntohs(key->n_proto);
  6294. n_proto_mask = ntohs(mask->n_proto);
  6295. if (n_proto_key == ETH_P_ALL) {
  6296. n_proto_key = 0;
  6297. n_proto_mask = 0;
  6298. }
  6299. filter->n_proto = n_proto_key & n_proto_mask;
  6300. filter->ip_proto = key->ip_proto;
  6301. }
  6302. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  6303. struct flow_dissector_key_eth_addrs *key =
  6304. skb_flow_dissector_target(f->dissector,
  6305. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6306. f->key);
  6307. struct flow_dissector_key_eth_addrs *mask =
  6308. skb_flow_dissector_target(f->dissector,
  6309. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6310. f->mask);
  6311. /* use is_broadcast and is_zero to check for all 0xf or 0 */
  6312. if (!is_zero_ether_addr(mask->dst)) {
  6313. if (is_broadcast_ether_addr(mask->dst)) {
  6314. field_flags |= I40E_CLOUD_FIELD_OMAC;
  6315. } else {
  6316. dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
  6317. mask->dst);
  6318. return I40E_ERR_CONFIG;
  6319. }
  6320. }
  6321. if (!is_zero_ether_addr(mask->src)) {
  6322. if (is_broadcast_ether_addr(mask->src)) {
  6323. field_flags |= I40E_CLOUD_FIELD_IMAC;
  6324. } else {
  6325. dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
  6326. mask->src);
  6327. return I40E_ERR_CONFIG;
  6328. }
  6329. }
  6330. ether_addr_copy(filter->dst_mac, key->dst);
  6331. ether_addr_copy(filter->src_mac, key->src);
  6332. }
  6333. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
  6334. struct flow_dissector_key_vlan *key =
  6335. skb_flow_dissector_target(f->dissector,
  6336. FLOW_DISSECTOR_KEY_VLAN,
  6337. f->key);
  6338. struct flow_dissector_key_vlan *mask =
  6339. skb_flow_dissector_target(f->dissector,
  6340. FLOW_DISSECTOR_KEY_VLAN,
  6341. f->mask);
  6342. if (mask->vlan_id) {
  6343. if (mask->vlan_id == VLAN_VID_MASK) {
  6344. field_flags |= I40E_CLOUD_FIELD_IVLAN;
  6345. } else {
  6346. dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
  6347. mask->vlan_id);
  6348. return I40E_ERR_CONFIG;
  6349. }
  6350. }
  6351. filter->vlan_id = cpu_to_be16(key->vlan_id);
  6352. }
  6353. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
  6354. struct flow_dissector_key_control *key =
  6355. skb_flow_dissector_target(f->dissector,
  6356. FLOW_DISSECTOR_KEY_CONTROL,
  6357. f->key);
  6358. addr_type = key->addr_type;
  6359. }
  6360. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  6361. struct flow_dissector_key_ipv4_addrs *key =
  6362. skb_flow_dissector_target(f->dissector,
  6363. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6364. f->key);
  6365. struct flow_dissector_key_ipv4_addrs *mask =
  6366. skb_flow_dissector_target(f->dissector,
  6367. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6368. f->mask);
  6369. if (mask->dst) {
  6370. if (mask->dst == cpu_to_be32(0xffffffff)) {
  6371. field_flags |= I40E_CLOUD_FIELD_IIP;
  6372. } else {
  6373. dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
  6374. &mask->dst);
  6375. return I40E_ERR_CONFIG;
  6376. }
  6377. }
  6378. if (mask->src) {
  6379. if (mask->src == cpu_to_be32(0xffffffff)) {
  6380. field_flags |= I40E_CLOUD_FIELD_IIP;
  6381. } else {
  6382. dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
  6383. &mask->src);
  6384. return I40E_ERR_CONFIG;
  6385. }
  6386. }
  6387. if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
  6388. dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
  6389. return I40E_ERR_CONFIG;
  6390. }
  6391. filter->dst_ipv4 = key->dst;
  6392. filter->src_ipv4 = key->src;
  6393. }
  6394. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  6395. struct flow_dissector_key_ipv6_addrs *key =
  6396. skb_flow_dissector_target(f->dissector,
  6397. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6398. f->key);
  6399. struct flow_dissector_key_ipv6_addrs *mask =
  6400. skb_flow_dissector_target(f->dissector,
  6401. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6402. f->mask);
  6403. /* src and dest IPV6 address should not be LOOPBACK
  6404. * (0:0:0:0:0:0:0:1), which can be represented as ::1
  6405. */
  6406. if (ipv6_addr_loopback(&key->dst) ||
  6407. ipv6_addr_loopback(&key->src)) {
  6408. dev_err(&pf->pdev->dev,
  6409. "Bad ipv6, addr is LOOPBACK\n");
  6410. return I40E_ERR_CONFIG;
  6411. }
  6412. if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
  6413. field_flags |= I40E_CLOUD_FIELD_IIP;
  6414. memcpy(&filter->src_ipv6, &key->src.s6_addr32,
  6415. sizeof(filter->src_ipv6));
  6416. memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
  6417. sizeof(filter->dst_ipv6));
  6418. }
  6419. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
  6420. struct flow_dissector_key_ports *key =
  6421. skb_flow_dissector_target(f->dissector,
  6422. FLOW_DISSECTOR_KEY_PORTS,
  6423. f->key);
  6424. struct flow_dissector_key_ports *mask =
  6425. skb_flow_dissector_target(f->dissector,
  6426. FLOW_DISSECTOR_KEY_PORTS,
  6427. f->mask);
  6428. if (mask->src) {
  6429. if (mask->src == cpu_to_be16(0xffff)) {
  6430. field_flags |= I40E_CLOUD_FIELD_IIP;
  6431. } else {
  6432. dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
  6433. be16_to_cpu(mask->src));
  6434. return I40E_ERR_CONFIG;
  6435. }
  6436. }
  6437. if (mask->dst) {
  6438. if (mask->dst == cpu_to_be16(0xffff)) {
  6439. field_flags |= I40E_CLOUD_FIELD_IIP;
  6440. } else {
  6441. dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
  6442. be16_to_cpu(mask->dst));
  6443. return I40E_ERR_CONFIG;
  6444. }
  6445. }
  6446. filter->dst_port = key->dst;
  6447. filter->src_port = key->src;
  6448. switch (filter->ip_proto) {
  6449. case IPPROTO_TCP:
  6450. case IPPROTO_UDP:
  6451. break;
  6452. default:
  6453. dev_err(&pf->pdev->dev,
  6454. "Only UDP and TCP transport are supported\n");
  6455. return -EINVAL;
  6456. }
  6457. }
  6458. filter->flags = field_flags;
  6459. return 0;
  6460. }
  6461. /**
  6462. * i40e_handle_tclass: Forward to a traffic class on the device
  6463. * @vsi: Pointer to VSI
  6464. * @tc: traffic class index on the device
  6465. * @filter: Pointer to cloud filter structure
  6466. *
  6467. **/
  6468. static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
  6469. struct i40e_cloud_filter *filter)
  6470. {
  6471. struct i40e_channel *ch, *ch_tmp;
  6472. /* direct to a traffic class on the same device */
  6473. if (tc == 0) {
  6474. filter->seid = vsi->seid;
  6475. return 0;
  6476. } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
  6477. if (!filter->dst_port) {
  6478. dev_err(&vsi->back->pdev->dev,
  6479. "Specify destination port to direct to traffic class that is not default\n");
  6480. return -EINVAL;
  6481. }
  6482. if (list_empty(&vsi->ch_list))
  6483. return -EINVAL;
  6484. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
  6485. list) {
  6486. if (ch->seid == vsi->tc_seid_map[tc])
  6487. filter->seid = ch->seid;
  6488. }
  6489. return 0;
  6490. }
  6491. dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
  6492. return -EINVAL;
  6493. }
  6494. /**
  6495. * i40e_configure_clsflower - Configure tc flower filters
  6496. * @vsi: Pointer to VSI
  6497. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6498. *
  6499. **/
  6500. static int i40e_configure_clsflower(struct i40e_vsi *vsi,
  6501. struct tc_cls_flower_offload *cls_flower)
  6502. {
  6503. int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
  6504. struct i40e_cloud_filter *filter = NULL;
  6505. struct i40e_pf *pf = vsi->back;
  6506. int err = 0;
  6507. if (tc < 0) {
  6508. dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
  6509. return -EOPNOTSUPP;
  6510. }
  6511. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  6512. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  6513. return -EBUSY;
  6514. if (pf->fdir_pf_active_filters ||
  6515. (!hlist_empty(&pf->fdir_filter_list))) {
  6516. dev_err(&vsi->back->pdev->dev,
  6517. "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
  6518. return -EINVAL;
  6519. }
  6520. if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
  6521. dev_err(&vsi->back->pdev->dev,
  6522. "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
  6523. vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6524. vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6525. }
  6526. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  6527. if (!filter)
  6528. return -ENOMEM;
  6529. filter->cookie = cls_flower->cookie;
  6530. err = i40e_parse_cls_flower(vsi, cls_flower, filter);
  6531. if (err < 0)
  6532. goto err;
  6533. err = i40e_handle_tclass(vsi, tc, filter);
  6534. if (err < 0)
  6535. goto err;
  6536. /* Add cloud filter */
  6537. if (filter->dst_port)
  6538. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
  6539. else
  6540. err = i40e_add_del_cloud_filter(vsi, filter, true);
  6541. if (err) {
  6542. dev_err(&pf->pdev->dev,
  6543. "Failed to add cloud filter, err %s\n",
  6544. i40e_stat_str(&pf->hw, err));
  6545. goto err;
  6546. }
  6547. /* add filter to the ordered list */
  6548. INIT_HLIST_NODE(&filter->cloud_node);
  6549. hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
  6550. pf->num_cloud_filters++;
  6551. return err;
  6552. err:
  6553. kfree(filter);
  6554. return err;
  6555. }
  6556. /**
  6557. * i40e_find_cloud_filter - Find the could filter in the list
  6558. * @vsi: Pointer to VSI
  6559. * @cookie: filter specific cookie
  6560. *
  6561. **/
  6562. static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
  6563. unsigned long *cookie)
  6564. {
  6565. struct i40e_cloud_filter *filter = NULL;
  6566. struct hlist_node *node2;
  6567. hlist_for_each_entry_safe(filter, node2,
  6568. &vsi->back->cloud_filter_list, cloud_node)
  6569. if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
  6570. return filter;
  6571. return NULL;
  6572. }
  6573. /**
  6574. * i40e_delete_clsflower - Remove tc flower filters
  6575. * @vsi: Pointer to VSI
  6576. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6577. *
  6578. **/
  6579. static int i40e_delete_clsflower(struct i40e_vsi *vsi,
  6580. struct tc_cls_flower_offload *cls_flower)
  6581. {
  6582. struct i40e_cloud_filter *filter = NULL;
  6583. struct i40e_pf *pf = vsi->back;
  6584. int err = 0;
  6585. filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
  6586. if (!filter)
  6587. return -EINVAL;
  6588. hash_del(&filter->cloud_node);
  6589. if (filter->dst_port)
  6590. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
  6591. else
  6592. err = i40e_add_del_cloud_filter(vsi, filter, false);
  6593. kfree(filter);
  6594. if (err) {
  6595. dev_err(&pf->pdev->dev,
  6596. "Failed to delete cloud filter, err %s\n",
  6597. i40e_stat_str(&pf->hw, err));
  6598. return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
  6599. }
  6600. pf->num_cloud_filters--;
  6601. if (!pf->num_cloud_filters)
  6602. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6603. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6604. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6605. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6606. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6607. }
  6608. return 0;
  6609. }
  6610. /**
  6611. * i40e_setup_tc_cls_flower - flower classifier offloads
  6612. * @netdev: net device to configure
  6613. * @type_data: offload data
  6614. **/
  6615. static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
  6616. struct tc_cls_flower_offload *cls_flower)
  6617. {
  6618. struct i40e_vsi *vsi = np->vsi;
  6619. switch (cls_flower->command) {
  6620. case TC_CLSFLOWER_REPLACE:
  6621. return i40e_configure_clsflower(vsi, cls_flower);
  6622. case TC_CLSFLOWER_DESTROY:
  6623. return i40e_delete_clsflower(vsi, cls_flower);
  6624. case TC_CLSFLOWER_STATS:
  6625. return -EOPNOTSUPP;
  6626. default:
  6627. return -EOPNOTSUPP;
  6628. }
  6629. }
  6630. static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  6631. void *cb_priv)
  6632. {
  6633. struct i40e_netdev_priv *np = cb_priv;
  6634. if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
  6635. return -EOPNOTSUPP;
  6636. switch (type) {
  6637. case TC_SETUP_CLSFLOWER:
  6638. return i40e_setup_tc_cls_flower(np, type_data);
  6639. default:
  6640. return -EOPNOTSUPP;
  6641. }
  6642. }
  6643. static int i40e_setup_tc_block(struct net_device *dev,
  6644. struct tc_block_offload *f)
  6645. {
  6646. struct i40e_netdev_priv *np = netdev_priv(dev);
  6647. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  6648. return -EOPNOTSUPP;
  6649. switch (f->command) {
  6650. case TC_BLOCK_BIND:
  6651. return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
  6652. np, np, f->extack);
  6653. case TC_BLOCK_UNBIND:
  6654. tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
  6655. return 0;
  6656. default:
  6657. return -EOPNOTSUPP;
  6658. }
  6659. }
  6660. static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  6661. void *type_data)
  6662. {
  6663. switch (type) {
  6664. case TC_SETUP_QDISC_MQPRIO:
  6665. return i40e_setup_tc(netdev, type_data);
  6666. case TC_SETUP_BLOCK:
  6667. return i40e_setup_tc_block(netdev, type_data);
  6668. default:
  6669. return -EOPNOTSUPP;
  6670. }
  6671. }
  6672. /**
  6673. * i40e_open - Called when a network interface is made active
  6674. * @netdev: network interface device structure
  6675. *
  6676. * The open entry point is called when a network interface is made
  6677. * active by the system (IFF_UP). At this point all resources needed
  6678. * for transmit and receive operations are allocated, the interrupt
  6679. * handler is registered with the OS, the netdev watchdog subtask is
  6680. * enabled, and the stack is notified that the interface is ready.
  6681. *
  6682. * Returns 0 on success, negative value on failure
  6683. **/
  6684. int i40e_open(struct net_device *netdev)
  6685. {
  6686. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6687. struct i40e_vsi *vsi = np->vsi;
  6688. struct i40e_pf *pf = vsi->back;
  6689. int err;
  6690. /* disallow open during test or if eeprom is broken */
  6691. if (test_bit(__I40E_TESTING, pf->state) ||
  6692. test_bit(__I40E_BAD_EEPROM, pf->state))
  6693. return -EBUSY;
  6694. netif_carrier_off(netdev);
  6695. if (i40e_force_link_state(pf, true))
  6696. return -EAGAIN;
  6697. err = i40e_vsi_open(vsi);
  6698. if (err)
  6699. return err;
  6700. /* configure global TSO hardware offload settings */
  6701. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  6702. TCP_FLAG_FIN) >> 16);
  6703. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  6704. TCP_FLAG_FIN |
  6705. TCP_FLAG_CWR) >> 16);
  6706. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  6707. udp_tunnel_get_rx_info(netdev);
  6708. return 0;
  6709. }
  6710. /**
  6711. * i40e_vsi_open -
  6712. * @vsi: the VSI to open
  6713. *
  6714. * Finish initialization of the VSI.
  6715. *
  6716. * Returns 0 on success, negative value on failure
  6717. *
  6718. * Note: expects to be called while under rtnl_lock()
  6719. **/
  6720. int i40e_vsi_open(struct i40e_vsi *vsi)
  6721. {
  6722. struct i40e_pf *pf = vsi->back;
  6723. char int_name[I40E_INT_NAME_STR_LEN];
  6724. int err;
  6725. /* allocate descriptors */
  6726. err = i40e_vsi_setup_tx_resources(vsi);
  6727. if (err)
  6728. goto err_setup_tx;
  6729. err = i40e_vsi_setup_rx_resources(vsi);
  6730. if (err)
  6731. goto err_setup_rx;
  6732. err = i40e_vsi_configure(vsi);
  6733. if (err)
  6734. goto err_setup_rx;
  6735. if (vsi->netdev) {
  6736. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  6737. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  6738. err = i40e_vsi_request_irq(vsi, int_name);
  6739. if (err)
  6740. goto err_setup_rx;
  6741. /* Notify the stack of the actual queue counts. */
  6742. err = netif_set_real_num_tx_queues(vsi->netdev,
  6743. vsi->num_queue_pairs);
  6744. if (err)
  6745. goto err_set_queues;
  6746. err = netif_set_real_num_rx_queues(vsi->netdev,
  6747. vsi->num_queue_pairs);
  6748. if (err)
  6749. goto err_set_queues;
  6750. } else if (vsi->type == I40E_VSI_FDIR) {
  6751. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  6752. dev_driver_string(&pf->pdev->dev),
  6753. dev_name(&pf->pdev->dev));
  6754. err = i40e_vsi_request_irq(vsi, int_name);
  6755. } else {
  6756. err = -EINVAL;
  6757. goto err_setup_rx;
  6758. }
  6759. err = i40e_up_complete(vsi);
  6760. if (err)
  6761. goto err_up_complete;
  6762. return 0;
  6763. err_up_complete:
  6764. i40e_down(vsi);
  6765. err_set_queues:
  6766. i40e_vsi_free_irq(vsi);
  6767. err_setup_rx:
  6768. i40e_vsi_free_rx_resources(vsi);
  6769. err_setup_tx:
  6770. i40e_vsi_free_tx_resources(vsi);
  6771. if (vsi == pf->vsi[pf->lan_vsi])
  6772. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  6773. return err;
  6774. }
  6775. /**
  6776. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  6777. * @pf: Pointer to PF
  6778. *
  6779. * This function destroys the hlist where all the Flow Director
  6780. * filters were saved.
  6781. **/
  6782. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  6783. {
  6784. struct i40e_fdir_filter *filter;
  6785. struct i40e_flex_pit *pit_entry, *tmp;
  6786. struct hlist_node *node2;
  6787. hlist_for_each_entry_safe(filter, node2,
  6788. &pf->fdir_filter_list, fdir_node) {
  6789. hlist_del(&filter->fdir_node);
  6790. kfree(filter);
  6791. }
  6792. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  6793. list_del(&pit_entry->list);
  6794. kfree(pit_entry);
  6795. }
  6796. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  6797. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  6798. list_del(&pit_entry->list);
  6799. kfree(pit_entry);
  6800. }
  6801. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  6802. pf->fdir_pf_active_filters = 0;
  6803. pf->fd_tcp4_filter_cnt = 0;
  6804. pf->fd_udp4_filter_cnt = 0;
  6805. pf->fd_sctp4_filter_cnt = 0;
  6806. pf->fd_ip4_filter_cnt = 0;
  6807. /* Reprogram the default input set for TCP/IPv4 */
  6808. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  6809. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6810. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6811. /* Reprogram the default input set for UDP/IPv4 */
  6812. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  6813. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6814. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6815. /* Reprogram the default input set for SCTP/IPv4 */
  6816. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  6817. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6818. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6819. /* Reprogram the default input set for Other/IPv4 */
  6820. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  6821. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6822. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
  6823. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6824. }
  6825. /**
  6826. * i40e_cloud_filter_exit - Cleans up the cloud filters
  6827. * @pf: Pointer to PF
  6828. *
  6829. * This function destroys the hlist where all the cloud filters
  6830. * were saved.
  6831. **/
  6832. static void i40e_cloud_filter_exit(struct i40e_pf *pf)
  6833. {
  6834. struct i40e_cloud_filter *cfilter;
  6835. struct hlist_node *node;
  6836. hlist_for_each_entry_safe(cfilter, node,
  6837. &pf->cloud_filter_list, cloud_node) {
  6838. hlist_del(&cfilter->cloud_node);
  6839. kfree(cfilter);
  6840. }
  6841. pf->num_cloud_filters = 0;
  6842. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6843. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6844. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6845. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6846. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6847. }
  6848. }
  6849. /**
  6850. * i40e_close - Disables a network interface
  6851. * @netdev: network interface device structure
  6852. *
  6853. * The close entry point is called when an interface is de-activated
  6854. * by the OS. The hardware is still under the driver's control, but
  6855. * this netdev interface is disabled.
  6856. *
  6857. * Returns 0, this is not allowed to fail
  6858. **/
  6859. int i40e_close(struct net_device *netdev)
  6860. {
  6861. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6862. struct i40e_vsi *vsi = np->vsi;
  6863. i40e_vsi_close(vsi);
  6864. return 0;
  6865. }
  6866. /**
  6867. * i40e_do_reset - Start a PF or Core Reset sequence
  6868. * @pf: board private structure
  6869. * @reset_flags: which reset is requested
  6870. * @lock_acquired: indicates whether or not the lock has been acquired
  6871. * before this function was called.
  6872. *
  6873. * The essential difference in resets is that the PF Reset
  6874. * doesn't clear the packet buffers, doesn't reset the PE
  6875. * firmware, and doesn't bother the other PFs on the chip.
  6876. **/
  6877. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  6878. {
  6879. u32 val;
  6880. WARN_ON(in_interrupt());
  6881. /* do the biggest reset indicated */
  6882. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  6883. /* Request a Global Reset
  6884. *
  6885. * This will start the chip's countdown to the actual full
  6886. * chip reset event, and a warning interrupt to be sent
  6887. * to all PFs, including the requestor. Our handler
  6888. * for the warning interrupt will deal with the shutdown
  6889. * and recovery of the switch setup.
  6890. */
  6891. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  6892. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6893. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  6894. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6895. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  6896. /* Request a Core Reset
  6897. *
  6898. * Same as Global Reset, except does *not* include the MAC/PHY
  6899. */
  6900. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  6901. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6902. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  6903. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6904. i40e_flush(&pf->hw);
  6905. } else if (reset_flags & I40E_PF_RESET_FLAG) {
  6906. /* Request a PF Reset
  6907. *
  6908. * Resets only the PF-specific registers
  6909. *
  6910. * This goes directly to the tear-down and rebuild of
  6911. * the switch, since we need to do all the recovery as
  6912. * for the Core Reset.
  6913. */
  6914. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  6915. i40e_handle_reset_warning(pf, lock_acquired);
  6916. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  6917. int v;
  6918. /* Find the VSI(s) that requested a re-init */
  6919. dev_info(&pf->pdev->dev,
  6920. "VSI reinit requested\n");
  6921. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6922. struct i40e_vsi *vsi = pf->vsi[v];
  6923. if (vsi != NULL &&
  6924. test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
  6925. vsi->state))
  6926. i40e_vsi_reinit_locked(pf->vsi[v]);
  6927. }
  6928. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  6929. int v;
  6930. /* Find the VSI(s) that needs to be brought down */
  6931. dev_info(&pf->pdev->dev, "VSI down requested\n");
  6932. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6933. struct i40e_vsi *vsi = pf->vsi[v];
  6934. if (vsi != NULL &&
  6935. test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
  6936. vsi->state)) {
  6937. set_bit(__I40E_VSI_DOWN, vsi->state);
  6938. i40e_down(vsi);
  6939. }
  6940. }
  6941. } else {
  6942. dev_info(&pf->pdev->dev,
  6943. "bad reset request 0x%08x\n", reset_flags);
  6944. }
  6945. }
  6946. #ifdef CONFIG_I40E_DCB
  6947. /**
  6948. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  6949. * @pf: board private structure
  6950. * @old_cfg: current DCB config
  6951. * @new_cfg: new DCB config
  6952. **/
  6953. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  6954. struct i40e_dcbx_config *old_cfg,
  6955. struct i40e_dcbx_config *new_cfg)
  6956. {
  6957. bool need_reconfig = false;
  6958. /* Check if ETS configuration has changed */
  6959. if (memcmp(&new_cfg->etscfg,
  6960. &old_cfg->etscfg,
  6961. sizeof(new_cfg->etscfg))) {
  6962. /* If Priority Table has changed reconfig is needed */
  6963. if (memcmp(&new_cfg->etscfg.prioritytable,
  6964. &old_cfg->etscfg.prioritytable,
  6965. sizeof(new_cfg->etscfg.prioritytable))) {
  6966. need_reconfig = true;
  6967. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  6968. }
  6969. if (memcmp(&new_cfg->etscfg.tcbwtable,
  6970. &old_cfg->etscfg.tcbwtable,
  6971. sizeof(new_cfg->etscfg.tcbwtable)))
  6972. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  6973. if (memcmp(&new_cfg->etscfg.tsatable,
  6974. &old_cfg->etscfg.tsatable,
  6975. sizeof(new_cfg->etscfg.tsatable)))
  6976. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  6977. }
  6978. /* Check if PFC configuration has changed */
  6979. if (memcmp(&new_cfg->pfc,
  6980. &old_cfg->pfc,
  6981. sizeof(new_cfg->pfc))) {
  6982. need_reconfig = true;
  6983. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  6984. }
  6985. /* Check if APP Table has changed */
  6986. if (memcmp(&new_cfg->app,
  6987. &old_cfg->app,
  6988. sizeof(new_cfg->app))) {
  6989. need_reconfig = true;
  6990. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  6991. }
  6992. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  6993. return need_reconfig;
  6994. }
  6995. /**
  6996. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  6997. * @pf: board private structure
  6998. * @e: event info posted on ARQ
  6999. **/
  7000. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  7001. struct i40e_arq_event_info *e)
  7002. {
  7003. struct i40e_aqc_lldp_get_mib *mib =
  7004. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  7005. struct i40e_hw *hw = &pf->hw;
  7006. struct i40e_dcbx_config tmp_dcbx_cfg;
  7007. bool need_reconfig = false;
  7008. int ret = 0;
  7009. u8 type;
  7010. /* Not DCB capable or capability disabled */
  7011. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  7012. return ret;
  7013. /* Ignore if event is not for Nearest Bridge */
  7014. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  7015. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  7016. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  7017. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  7018. return ret;
  7019. /* Check MIB Type and return if event for Remote MIB update */
  7020. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  7021. dev_dbg(&pf->pdev->dev,
  7022. "LLDP event mib type %s\n", type ? "remote" : "local");
  7023. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  7024. /* Update the remote cached instance and return */
  7025. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  7026. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  7027. &hw->remote_dcbx_config);
  7028. goto exit;
  7029. }
  7030. /* Store the old configuration */
  7031. tmp_dcbx_cfg = hw->local_dcbx_config;
  7032. /* Reset the old DCBx configuration data */
  7033. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  7034. /* Get updated DCBX data from firmware */
  7035. ret = i40e_get_dcb_config(&pf->hw);
  7036. if (ret) {
  7037. dev_info(&pf->pdev->dev,
  7038. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  7039. i40e_stat_str(&pf->hw, ret),
  7040. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7041. goto exit;
  7042. }
  7043. /* No change detected in DCBX configs */
  7044. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  7045. sizeof(tmp_dcbx_cfg))) {
  7046. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  7047. goto exit;
  7048. }
  7049. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  7050. &hw->local_dcbx_config);
  7051. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  7052. if (!need_reconfig)
  7053. goto exit;
  7054. /* Enable DCB tagging only when more than one TC */
  7055. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  7056. pf->flags |= I40E_FLAG_DCB_ENABLED;
  7057. else
  7058. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  7059. set_bit(__I40E_PORT_SUSPENDED, pf->state);
  7060. /* Reconfiguration needed quiesce all VSIs */
  7061. i40e_pf_quiesce_all_vsi(pf);
  7062. /* Changes in configuration update VEB/VSI */
  7063. i40e_dcb_reconfigure(pf);
  7064. ret = i40e_resume_port_tx(pf);
  7065. clear_bit(__I40E_PORT_SUSPENDED, pf->state);
  7066. /* In case of error no point in resuming VSIs */
  7067. if (ret)
  7068. goto exit;
  7069. /* Wait for the PF's queues to be disabled */
  7070. ret = i40e_pf_wait_queues_disabled(pf);
  7071. if (ret) {
  7072. /* Schedule PF reset to recover */
  7073. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7074. i40e_service_event_schedule(pf);
  7075. } else {
  7076. i40e_pf_unquiesce_all_vsi(pf);
  7077. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  7078. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  7079. }
  7080. exit:
  7081. return ret;
  7082. }
  7083. #endif /* CONFIG_I40E_DCB */
  7084. /**
  7085. * i40e_do_reset_safe - Protected reset path for userland calls.
  7086. * @pf: board private structure
  7087. * @reset_flags: which reset is requested
  7088. *
  7089. **/
  7090. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  7091. {
  7092. rtnl_lock();
  7093. i40e_do_reset(pf, reset_flags, true);
  7094. rtnl_unlock();
  7095. }
  7096. /**
  7097. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  7098. * @pf: board private structure
  7099. * @e: event info posted on ARQ
  7100. *
  7101. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  7102. * and VF queues
  7103. **/
  7104. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  7105. struct i40e_arq_event_info *e)
  7106. {
  7107. struct i40e_aqc_lan_overflow *data =
  7108. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  7109. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  7110. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  7111. struct i40e_hw *hw = &pf->hw;
  7112. struct i40e_vf *vf;
  7113. u16 vf_id;
  7114. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  7115. queue, qtx_ctl);
  7116. /* Queue belongs to VF, find the VF and issue VF reset */
  7117. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  7118. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  7119. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  7120. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  7121. vf_id -= hw->func_caps.vf_base_id;
  7122. vf = &pf->vf[vf_id];
  7123. i40e_vc_notify_vf_reset(vf);
  7124. /* Allow VF to process pending reset notification */
  7125. msleep(20);
  7126. i40e_reset_vf(vf, false);
  7127. }
  7128. }
  7129. /**
  7130. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  7131. * @pf: board private structure
  7132. **/
  7133. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  7134. {
  7135. u32 val, fcnt_prog;
  7136. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7137. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  7138. return fcnt_prog;
  7139. }
  7140. /**
  7141. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  7142. * @pf: board private structure
  7143. **/
  7144. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  7145. {
  7146. u32 val, fcnt_prog;
  7147. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7148. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  7149. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  7150. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  7151. return fcnt_prog;
  7152. }
  7153. /**
  7154. * i40e_get_global_fd_count - Get total FD filters programmed on device
  7155. * @pf: board private structure
  7156. **/
  7157. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  7158. {
  7159. u32 val, fcnt_prog;
  7160. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  7161. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  7162. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  7163. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  7164. return fcnt_prog;
  7165. }
  7166. /**
  7167. * i40e_reenable_fdir_sb - Restore FDir SB capability
  7168. * @pf: board private structure
  7169. **/
  7170. static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
  7171. {
  7172. if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  7173. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  7174. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7175. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  7176. }
  7177. /**
  7178. * i40e_reenable_fdir_atr - Restore FDir ATR capability
  7179. * @pf: board private structure
  7180. **/
  7181. static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
  7182. {
  7183. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
  7184. /* ATR uses the same filtering logic as SB rules. It only
  7185. * functions properly if the input set mask is at the default
  7186. * settings. It is safe to restore the default input set
  7187. * because there are no active TCPv4 filter rules.
  7188. */
  7189. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  7190. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  7191. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  7192. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7193. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7194. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  7195. }
  7196. }
  7197. /**
  7198. * i40e_delete_invalid_filter - Delete an invalid FDIR filter
  7199. * @pf: board private structure
  7200. * @filter: FDir filter to remove
  7201. */
  7202. static void i40e_delete_invalid_filter(struct i40e_pf *pf,
  7203. struct i40e_fdir_filter *filter)
  7204. {
  7205. /* Update counters */
  7206. pf->fdir_pf_active_filters--;
  7207. pf->fd_inv = 0;
  7208. switch (filter->flow_type) {
  7209. case TCP_V4_FLOW:
  7210. pf->fd_tcp4_filter_cnt--;
  7211. break;
  7212. case UDP_V4_FLOW:
  7213. pf->fd_udp4_filter_cnt--;
  7214. break;
  7215. case SCTP_V4_FLOW:
  7216. pf->fd_sctp4_filter_cnt--;
  7217. break;
  7218. case IP_USER_FLOW:
  7219. switch (filter->ip4_proto) {
  7220. case IPPROTO_TCP:
  7221. pf->fd_tcp4_filter_cnt--;
  7222. break;
  7223. case IPPROTO_UDP:
  7224. pf->fd_udp4_filter_cnt--;
  7225. break;
  7226. case IPPROTO_SCTP:
  7227. pf->fd_sctp4_filter_cnt--;
  7228. break;
  7229. case IPPROTO_IP:
  7230. pf->fd_ip4_filter_cnt--;
  7231. break;
  7232. }
  7233. break;
  7234. }
  7235. /* Remove the filter from the list and free memory */
  7236. hlist_del(&filter->fdir_node);
  7237. kfree(filter);
  7238. }
  7239. /**
  7240. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  7241. * @pf: board private structure
  7242. **/
  7243. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  7244. {
  7245. struct i40e_fdir_filter *filter;
  7246. u32 fcnt_prog, fcnt_avail;
  7247. struct hlist_node *node;
  7248. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7249. return;
  7250. /* Check if we have enough room to re-enable FDir SB capability. */
  7251. fcnt_prog = i40e_get_global_fd_count(pf);
  7252. fcnt_avail = pf->fdir_pf_filter_count;
  7253. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  7254. (pf->fd_add_err == 0) ||
  7255. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
  7256. i40e_reenable_fdir_sb(pf);
  7257. /* We should wait for even more space before re-enabling ATR.
  7258. * Additionally, we cannot enable ATR as long as we still have TCP SB
  7259. * rules active.
  7260. */
  7261. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
  7262. (pf->fd_tcp4_filter_cnt == 0))
  7263. i40e_reenable_fdir_atr(pf);
  7264. /* if hw had a problem adding a filter, delete it */
  7265. if (pf->fd_inv > 0) {
  7266. hlist_for_each_entry_safe(filter, node,
  7267. &pf->fdir_filter_list, fdir_node)
  7268. if (filter->fd_id == pf->fd_inv)
  7269. i40e_delete_invalid_filter(pf, filter);
  7270. }
  7271. }
  7272. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  7273. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  7274. /**
  7275. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  7276. * @pf: board private structure
  7277. **/
  7278. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  7279. {
  7280. unsigned long min_flush_time;
  7281. int flush_wait_retry = 50;
  7282. bool disable_atr = false;
  7283. int fd_room;
  7284. int reg;
  7285. if (!time_after(jiffies, pf->fd_flush_timestamp +
  7286. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  7287. return;
  7288. /* If the flush is happening too quick and we have mostly SB rules we
  7289. * should not re-enable ATR for some time.
  7290. */
  7291. min_flush_time = pf->fd_flush_timestamp +
  7292. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  7293. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  7294. if (!(time_after(jiffies, min_flush_time)) &&
  7295. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  7296. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7297. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  7298. disable_atr = true;
  7299. }
  7300. pf->fd_flush_timestamp = jiffies;
  7301. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7302. /* flush all filters */
  7303. wr32(&pf->hw, I40E_PFQF_CTL_1,
  7304. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  7305. i40e_flush(&pf->hw);
  7306. pf->fd_flush_cnt++;
  7307. pf->fd_add_err = 0;
  7308. do {
  7309. /* Check FD flush status every 5-6msec */
  7310. usleep_range(5000, 6000);
  7311. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  7312. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  7313. break;
  7314. } while (flush_wait_retry--);
  7315. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  7316. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  7317. } else {
  7318. /* replay sideband filters */
  7319. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  7320. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  7321. clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7322. clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  7323. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7324. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  7325. }
  7326. }
  7327. /**
  7328. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  7329. * @pf: board private structure
  7330. **/
  7331. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  7332. {
  7333. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  7334. }
  7335. /* We can see up to 256 filter programming desc in transit if the filters are
  7336. * being applied really fast; before we see the first
  7337. * filter miss error on Rx queue 0. Accumulating enough error messages before
  7338. * reacting will make sure we don't cause flush too often.
  7339. */
  7340. #define I40E_MAX_FD_PROGRAM_ERROR 256
  7341. /**
  7342. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  7343. * @pf: board private structure
  7344. **/
  7345. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  7346. {
  7347. /* if interface is down do nothing */
  7348. if (test_bit(__I40E_DOWN, pf->state))
  7349. return;
  7350. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7351. i40e_fdir_flush_and_replay(pf);
  7352. i40e_fdir_check_and_reenable(pf);
  7353. }
  7354. /**
  7355. * i40e_vsi_link_event - notify VSI of a link event
  7356. * @vsi: vsi to be notified
  7357. * @link_up: link up or down
  7358. **/
  7359. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  7360. {
  7361. if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
  7362. return;
  7363. switch (vsi->type) {
  7364. case I40E_VSI_MAIN:
  7365. if (!vsi->netdev || !vsi->netdev_registered)
  7366. break;
  7367. if (link_up) {
  7368. netif_carrier_on(vsi->netdev);
  7369. netif_tx_wake_all_queues(vsi->netdev);
  7370. } else {
  7371. netif_carrier_off(vsi->netdev);
  7372. netif_tx_stop_all_queues(vsi->netdev);
  7373. }
  7374. break;
  7375. case I40E_VSI_SRIOV:
  7376. case I40E_VSI_VMDQ2:
  7377. case I40E_VSI_CTRL:
  7378. case I40E_VSI_IWARP:
  7379. case I40E_VSI_MIRROR:
  7380. default:
  7381. /* there is no notification for other VSIs */
  7382. break;
  7383. }
  7384. }
  7385. /**
  7386. * i40e_veb_link_event - notify elements on the veb of a link event
  7387. * @veb: veb to be notified
  7388. * @link_up: link up or down
  7389. **/
  7390. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  7391. {
  7392. struct i40e_pf *pf;
  7393. int i;
  7394. if (!veb || !veb->pf)
  7395. return;
  7396. pf = veb->pf;
  7397. /* depth first... */
  7398. for (i = 0; i < I40E_MAX_VEB; i++)
  7399. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  7400. i40e_veb_link_event(pf->veb[i], link_up);
  7401. /* ... now the local VSIs */
  7402. for (i = 0; i < pf->num_alloc_vsi; i++)
  7403. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  7404. i40e_vsi_link_event(pf->vsi[i], link_up);
  7405. }
  7406. /**
  7407. * i40e_link_event - Update netif_carrier status
  7408. * @pf: board private structure
  7409. **/
  7410. static void i40e_link_event(struct i40e_pf *pf)
  7411. {
  7412. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7413. u8 new_link_speed, old_link_speed;
  7414. i40e_status status;
  7415. bool new_link, old_link;
  7416. /* save off old link status information */
  7417. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  7418. /* set this to force the get_link_status call to refresh state */
  7419. pf->hw.phy.get_link_info = true;
  7420. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  7421. status = i40e_get_link_status(&pf->hw, &new_link);
  7422. /* On success, disable temp link polling */
  7423. if (status == I40E_SUCCESS) {
  7424. clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7425. } else {
  7426. /* Enable link polling temporarily until i40e_get_link_status
  7427. * returns I40E_SUCCESS
  7428. */
  7429. set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7430. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  7431. status);
  7432. return;
  7433. }
  7434. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  7435. new_link_speed = pf->hw.phy.link_info.link_speed;
  7436. if (new_link == old_link &&
  7437. new_link_speed == old_link_speed &&
  7438. (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  7439. new_link == netif_carrier_ok(vsi->netdev)))
  7440. return;
  7441. i40e_print_link_message(vsi, new_link);
  7442. /* Notify the base of the switch tree connected to
  7443. * the link. Floating VEBs are not notified.
  7444. */
  7445. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  7446. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  7447. else
  7448. i40e_vsi_link_event(vsi, new_link);
  7449. if (pf->vf)
  7450. i40e_vc_notify_link_state(pf);
  7451. if (pf->flags & I40E_FLAG_PTP)
  7452. i40e_ptp_set_increment(pf);
  7453. }
  7454. /**
  7455. * i40e_watchdog_subtask - periodic checks not using event driven response
  7456. * @pf: board private structure
  7457. **/
  7458. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  7459. {
  7460. int i;
  7461. /* if interface is down do nothing */
  7462. if (test_bit(__I40E_DOWN, pf->state) ||
  7463. test_bit(__I40E_CONFIG_BUSY, pf->state))
  7464. return;
  7465. /* make sure we don't do these things too often */
  7466. if (time_before(jiffies, (pf->service_timer_previous +
  7467. pf->service_timer_period)))
  7468. return;
  7469. pf->service_timer_previous = jiffies;
  7470. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  7471. test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
  7472. i40e_link_event(pf);
  7473. /* Update the stats for active netdevs so the network stack
  7474. * can look at updated numbers whenever it cares to
  7475. */
  7476. for (i = 0; i < pf->num_alloc_vsi; i++)
  7477. if (pf->vsi[i] && pf->vsi[i]->netdev)
  7478. i40e_update_stats(pf->vsi[i]);
  7479. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  7480. /* Update the stats for the active switching components */
  7481. for (i = 0; i < I40E_MAX_VEB; i++)
  7482. if (pf->veb[i])
  7483. i40e_update_veb_stats(pf->veb[i]);
  7484. }
  7485. i40e_ptp_rx_hang(pf);
  7486. i40e_ptp_tx_hang(pf);
  7487. }
  7488. /**
  7489. * i40e_reset_subtask - Set up for resetting the device and driver
  7490. * @pf: board private structure
  7491. **/
  7492. static void i40e_reset_subtask(struct i40e_pf *pf)
  7493. {
  7494. u32 reset_flags = 0;
  7495. if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
  7496. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  7497. clear_bit(__I40E_REINIT_REQUESTED, pf->state);
  7498. }
  7499. if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
  7500. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  7501. clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7502. }
  7503. if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
  7504. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  7505. clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  7506. }
  7507. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
  7508. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  7509. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  7510. }
  7511. if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
  7512. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  7513. clear_bit(__I40E_DOWN_REQUESTED, pf->state);
  7514. }
  7515. /* If there's a recovery already waiting, it takes
  7516. * precedence before starting a new reset sequence.
  7517. */
  7518. if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
  7519. i40e_prep_for_reset(pf, false);
  7520. i40e_reset(pf);
  7521. i40e_rebuild(pf, false, false);
  7522. }
  7523. /* If we're already down or resetting, just bail */
  7524. if (reset_flags &&
  7525. !test_bit(__I40E_DOWN, pf->state) &&
  7526. !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
  7527. i40e_do_reset(pf, reset_flags, false);
  7528. }
  7529. }
  7530. /**
  7531. * i40e_handle_link_event - Handle link event
  7532. * @pf: board private structure
  7533. * @e: event info posted on ARQ
  7534. **/
  7535. static void i40e_handle_link_event(struct i40e_pf *pf,
  7536. struct i40e_arq_event_info *e)
  7537. {
  7538. struct i40e_aqc_get_link_status *status =
  7539. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  7540. /* Do a new status request to re-enable LSE reporting
  7541. * and load new status information into the hw struct
  7542. * This completely ignores any state information
  7543. * in the ARQ event info, instead choosing to always
  7544. * issue the AQ update link status command.
  7545. */
  7546. i40e_link_event(pf);
  7547. /* Check if module meets thermal requirements */
  7548. if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
  7549. dev_err(&pf->pdev->dev,
  7550. "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
  7551. dev_err(&pf->pdev->dev,
  7552. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7553. } else {
  7554. /* check for unqualified module, if link is down, suppress
  7555. * the message if link was forced to be down.
  7556. */
  7557. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  7558. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  7559. (!(status->link_info & I40E_AQ_LINK_UP)) &&
  7560. (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
  7561. dev_err(&pf->pdev->dev,
  7562. "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
  7563. dev_err(&pf->pdev->dev,
  7564. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7565. }
  7566. }
  7567. }
  7568. /**
  7569. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  7570. * @pf: board private structure
  7571. **/
  7572. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  7573. {
  7574. struct i40e_arq_event_info event;
  7575. struct i40e_hw *hw = &pf->hw;
  7576. u16 pending, i = 0;
  7577. i40e_status ret;
  7578. u16 opcode;
  7579. u32 oldval;
  7580. u32 val;
  7581. /* Do not run clean AQ when PF reset fails */
  7582. if (test_bit(__I40E_RESET_FAILED, pf->state))
  7583. return;
  7584. /* check for error indications */
  7585. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  7586. oldval = val;
  7587. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  7588. if (hw->debug_mask & I40E_DEBUG_AQ)
  7589. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  7590. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  7591. }
  7592. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  7593. if (hw->debug_mask & I40E_DEBUG_AQ)
  7594. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  7595. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  7596. pf->arq_overflows++;
  7597. }
  7598. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  7599. if (hw->debug_mask & I40E_DEBUG_AQ)
  7600. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  7601. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  7602. }
  7603. if (oldval != val)
  7604. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  7605. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  7606. oldval = val;
  7607. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  7608. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7609. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  7610. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  7611. }
  7612. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  7613. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7614. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  7615. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  7616. }
  7617. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  7618. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7619. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  7620. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  7621. }
  7622. if (oldval != val)
  7623. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  7624. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  7625. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  7626. if (!event.msg_buf)
  7627. return;
  7628. do {
  7629. ret = i40e_clean_arq_element(hw, &event, &pending);
  7630. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  7631. break;
  7632. else if (ret) {
  7633. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  7634. break;
  7635. }
  7636. opcode = le16_to_cpu(event.desc.opcode);
  7637. switch (opcode) {
  7638. case i40e_aqc_opc_get_link_status:
  7639. i40e_handle_link_event(pf, &event);
  7640. break;
  7641. case i40e_aqc_opc_send_msg_to_pf:
  7642. ret = i40e_vc_process_vf_msg(pf,
  7643. le16_to_cpu(event.desc.retval),
  7644. le32_to_cpu(event.desc.cookie_high),
  7645. le32_to_cpu(event.desc.cookie_low),
  7646. event.msg_buf,
  7647. event.msg_len);
  7648. break;
  7649. case i40e_aqc_opc_lldp_update_mib:
  7650. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  7651. #ifdef CONFIG_I40E_DCB
  7652. rtnl_lock();
  7653. ret = i40e_handle_lldp_event(pf, &event);
  7654. rtnl_unlock();
  7655. #endif /* CONFIG_I40E_DCB */
  7656. break;
  7657. case i40e_aqc_opc_event_lan_overflow:
  7658. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  7659. i40e_handle_lan_overflow_event(pf, &event);
  7660. break;
  7661. case i40e_aqc_opc_send_msg_to_peer:
  7662. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  7663. break;
  7664. case i40e_aqc_opc_nvm_erase:
  7665. case i40e_aqc_opc_nvm_update:
  7666. case i40e_aqc_opc_oem_post_update:
  7667. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  7668. "ARQ NVM operation 0x%04x completed\n",
  7669. opcode);
  7670. break;
  7671. default:
  7672. dev_info(&pf->pdev->dev,
  7673. "ARQ: Unknown event 0x%04x ignored\n",
  7674. opcode);
  7675. break;
  7676. }
  7677. } while (i++ < pf->adminq_work_limit);
  7678. if (i < pf->adminq_work_limit)
  7679. clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  7680. /* re-enable Admin queue interrupt cause */
  7681. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  7682. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  7683. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  7684. i40e_flush(hw);
  7685. kfree(event.msg_buf);
  7686. }
  7687. /**
  7688. * i40e_verify_eeprom - make sure eeprom is good to use
  7689. * @pf: board private structure
  7690. **/
  7691. static void i40e_verify_eeprom(struct i40e_pf *pf)
  7692. {
  7693. int err;
  7694. err = i40e_diag_eeprom_test(&pf->hw);
  7695. if (err) {
  7696. /* retry in case of garbage read */
  7697. err = i40e_diag_eeprom_test(&pf->hw);
  7698. if (err) {
  7699. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  7700. err);
  7701. set_bit(__I40E_BAD_EEPROM, pf->state);
  7702. }
  7703. }
  7704. if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
  7705. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  7706. clear_bit(__I40E_BAD_EEPROM, pf->state);
  7707. }
  7708. }
  7709. /**
  7710. * i40e_enable_pf_switch_lb
  7711. * @pf: pointer to the PF structure
  7712. *
  7713. * enable switch loop back or die - no point in a return value
  7714. **/
  7715. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  7716. {
  7717. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7718. struct i40e_vsi_context ctxt;
  7719. int ret;
  7720. ctxt.seid = pf->main_vsi_seid;
  7721. ctxt.pf_num = pf->hw.pf_id;
  7722. ctxt.vf_num = 0;
  7723. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7724. if (ret) {
  7725. dev_info(&pf->pdev->dev,
  7726. "couldn't get PF vsi config, err %s aq_err %s\n",
  7727. i40e_stat_str(&pf->hw, ret),
  7728. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7729. return;
  7730. }
  7731. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7732. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7733. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7734. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7735. if (ret) {
  7736. dev_info(&pf->pdev->dev,
  7737. "update vsi switch failed, err %s aq_err %s\n",
  7738. i40e_stat_str(&pf->hw, ret),
  7739. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7740. }
  7741. }
  7742. /**
  7743. * i40e_disable_pf_switch_lb
  7744. * @pf: pointer to the PF structure
  7745. *
  7746. * disable switch loop back or die - no point in a return value
  7747. **/
  7748. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  7749. {
  7750. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7751. struct i40e_vsi_context ctxt;
  7752. int ret;
  7753. ctxt.seid = pf->main_vsi_seid;
  7754. ctxt.pf_num = pf->hw.pf_id;
  7755. ctxt.vf_num = 0;
  7756. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7757. if (ret) {
  7758. dev_info(&pf->pdev->dev,
  7759. "couldn't get PF vsi config, err %s aq_err %s\n",
  7760. i40e_stat_str(&pf->hw, ret),
  7761. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7762. return;
  7763. }
  7764. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7765. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7766. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7767. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7768. if (ret) {
  7769. dev_info(&pf->pdev->dev,
  7770. "update vsi switch failed, err %s aq_err %s\n",
  7771. i40e_stat_str(&pf->hw, ret),
  7772. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7773. }
  7774. }
  7775. /**
  7776. * i40e_config_bridge_mode - Configure the HW bridge mode
  7777. * @veb: pointer to the bridge instance
  7778. *
  7779. * Configure the loop back mode for the LAN VSI that is downlink to the
  7780. * specified HW bridge instance. It is expected this function is called
  7781. * when a new HW bridge is instantiated.
  7782. **/
  7783. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  7784. {
  7785. struct i40e_pf *pf = veb->pf;
  7786. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  7787. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  7788. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  7789. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  7790. i40e_disable_pf_switch_lb(pf);
  7791. else
  7792. i40e_enable_pf_switch_lb(pf);
  7793. }
  7794. /**
  7795. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  7796. * @veb: pointer to the VEB instance
  7797. *
  7798. * This is a recursive function that first builds the attached VSIs then
  7799. * recurses in to build the next layer of VEB. We track the connections
  7800. * through our own index numbers because the seid's from the HW could
  7801. * change across the reset.
  7802. **/
  7803. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  7804. {
  7805. struct i40e_vsi *ctl_vsi = NULL;
  7806. struct i40e_pf *pf = veb->pf;
  7807. int v, veb_idx;
  7808. int ret;
  7809. /* build VSI that owns this VEB, temporarily attached to base VEB */
  7810. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  7811. if (pf->vsi[v] &&
  7812. pf->vsi[v]->veb_idx == veb->idx &&
  7813. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  7814. ctl_vsi = pf->vsi[v];
  7815. break;
  7816. }
  7817. }
  7818. if (!ctl_vsi) {
  7819. dev_info(&pf->pdev->dev,
  7820. "missing owner VSI for veb_idx %d\n", veb->idx);
  7821. ret = -ENOENT;
  7822. goto end_reconstitute;
  7823. }
  7824. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  7825. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  7826. ret = i40e_add_vsi(ctl_vsi);
  7827. if (ret) {
  7828. dev_info(&pf->pdev->dev,
  7829. "rebuild of veb_idx %d owner VSI failed: %d\n",
  7830. veb->idx, ret);
  7831. goto end_reconstitute;
  7832. }
  7833. i40e_vsi_reset_stats(ctl_vsi);
  7834. /* create the VEB in the switch and move the VSI onto the VEB */
  7835. ret = i40e_add_veb(veb, ctl_vsi);
  7836. if (ret)
  7837. goto end_reconstitute;
  7838. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  7839. veb->bridge_mode = BRIDGE_MODE_VEB;
  7840. else
  7841. veb->bridge_mode = BRIDGE_MODE_VEPA;
  7842. i40e_config_bridge_mode(veb);
  7843. /* create the remaining VSIs attached to this VEB */
  7844. for (v = 0; v < pf->num_alloc_vsi; v++) {
  7845. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  7846. continue;
  7847. if (pf->vsi[v]->veb_idx == veb->idx) {
  7848. struct i40e_vsi *vsi = pf->vsi[v];
  7849. vsi->uplink_seid = veb->seid;
  7850. ret = i40e_add_vsi(vsi);
  7851. if (ret) {
  7852. dev_info(&pf->pdev->dev,
  7853. "rebuild of vsi_idx %d failed: %d\n",
  7854. v, ret);
  7855. goto end_reconstitute;
  7856. }
  7857. i40e_vsi_reset_stats(vsi);
  7858. }
  7859. }
  7860. /* create any VEBs attached to this VEB - RECURSION */
  7861. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  7862. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  7863. pf->veb[veb_idx]->uplink_seid = veb->seid;
  7864. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  7865. if (ret)
  7866. break;
  7867. }
  7868. }
  7869. end_reconstitute:
  7870. return ret;
  7871. }
  7872. /**
  7873. * i40e_get_capabilities - get info about the HW
  7874. * @pf: the PF struct
  7875. **/
  7876. static int i40e_get_capabilities(struct i40e_pf *pf,
  7877. enum i40e_admin_queue_opc list_type)
  7878. {
  7879. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  7880. u16 data_size;
  7881. int buf_len;
  7882. int err;
  7883. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  7884. do {
  7885. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  7886. if (!cap_buf)
  7887. return -ENOMEM;
  7888. /* this loads the data into the hw struct for us */
  7889. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  7890. &data_size, list_type,
  7891. NULL);
  7892. /* data loaded, buffer no longer needed */
  7893. kfree(cap_buf);
  7894. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  7895. /* retry with a larger buffer */
  7896. buf_len = data_size;
  7897. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  7898. dev_info(&pf->pdev->dev,
  7899. "capability discovery failed, err %s aq_err %s\n",
  7900. i40e_stat_str(&pf->hw, err),
  7901. i40e_aq_str(&pf->hw,
  7902. pf->hw.aq.asq_last_status));
  7903. return -ENODEV;
  7904. }
  7905. } while (err);
  7906. if (pf->hw.debug_mask & I40E_DEBUG_USER) {
  7907. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7908. dev_info(&pf->pdev->dev,
  7909. "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",
  7910. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  7911. pf->hw.func_caps.num_msix_vectors,
  7912. pf->hw.func_caps.num_msix_vectors_vf,
  7913. pf->hw.func_caps.fd_filters_guaranteed,
  7914. pf->hw.func_caps.fd_filters_best_effort,
  7915. pf->hw.func_caps.num_tx_qp,
  7916. pf->hw.func_caps.num_vsis);
  7917. } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
  7918. dev_info(&pf->pdev->dev,
  7919. "switch_mode=0x%04x, function_valid=0x%08x\n",
  7920. pf->hw.dev_caps.switch_mode,
  7921. pf->hw.dev_caps.valid_functions);
  7922. dev_info(&pf->pdev->dev,
  7923. "SR-IOV=%d, num_vfs for all function=%u\n",
  7924. pf->hw.dev_caps.sr_iov_1_1,
  7925. pf->hw.dev_caps.num_vfs);
  7926. dev_info(&pf->pdev->dev,
  7927. "num_vsis=%u, num_rx:%u, num_tx=%u\n",
  7928. pf->hw.dev_caps.num_vsis,
  7929. pf->hw.dev_caps.num_rx_qp,
  7930. pf->hw.dev_caps.num_tx_qp);
  7931. }
  7932. }
  7933. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7934. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  7935. + pf->hw.func_caps.num_vfs)
  7936. if (pf->hw.revision_id == 0 &&
  7937. pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
  7938. dev_info(&pf->pdev->dev,
  7939. "got num_vsis %d, setting num_vsis to %d\n",
  7940. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  7941. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  7942. }
  7943. }
  7944. return 0;
  7945. }
  7946. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  7947. /**
  7948. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  7949. * @pf: board private structure
  7950. **/
  7951. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  7952. {
  7953. struct i40e_vsi *vsi;
  7954. /* quick workaround for an NVM issue that leaves a critical register
  7955. * uninitialized
  7956. */
  7957. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  7958. static const u32 hkey[] = {
  7959. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  7960. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  7961. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  7962. 0x95b3a76d};
  7963. int i;
  7964. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  7965. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  7966. }
  7967. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7968. return;
  7969. /* find existing VSI and see if it needs configuring */
  7970. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7971. /* create a new VSI if none exists */
  7972. if (!vsi) {
  7973. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  7974. pf->vsi[pf->lan_vsi]->seid, 0);
  7975. if (!vsi) {
  7976. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  7977. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7978. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  7979. return;
  7980. }
  7981. }
  7982. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  7983. }
  7984. /**
  7985. * i40e_fdir_teardown - release the Flow Director resources
  7986. * @pf: board private structure
  7987. **/
  7988. static void i40e_fdir_teardown(struct i40e_pf *pf)
  7989. {
  7990. struct i40e_vsi *vsi;
  7991. i40e_fdir_filter_exit(pf);
  7992. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7993. if (vsi)
  7994. i40e_vsi_release(vsi);
  7995. }
  7996. /**
  7997. * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
  7998. * @vsi: PF main vsi
  7999. * @seid: seid of main or channel VSIs
  8000. *
  8001. * Rebuilds cloud filters associated with main VSI and channel VSIs if they
  8002. * existed before reset
  8003. **/
  8004. static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
  8005. {
  8006. struct i40e_cloud_filter *cfilter;
  8007. struct i40e_pf *pf = vsi->back;
  8008. struct hlist_node *node;
  8009. i40e_status ret;
  8010. /* Add cloud filters back if they exist */
  8011. hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
  8012. cloud_node) {
  8013. if (cfilter->seid != seid)
  8014. continue;
  8015. if (cfilter->dst_port)
  8016. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
  8017. true);
  8018. else
  8019. ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
  8020. if (ret) {
  8021. dev_dbg(&pf->pdev->dev,
  8022. "Failed to rebuild cloud filter, err %s aq_err %s\n",
  8023. i40e_stat_str(&pf->hw, ret),
  8024. i40e_aq_str(&pf->hw,
  8025. pf->hw.aq.asq_last_status));
  8026. return ret;
  8027. }
  8028. }
  8029. return 0;
  8030. }
  8031. /**
  8032. * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
  8033. * @vsi: PF main vsi
  8034. *
  8035. * Rebuilds channel VSIs if they existed before reset
  8036. **/
  8037. static int i40e_rebuild_channels(struct i40e_vsi *vsi)
  8038. {
  8039. struct i40e_channel *ch, *ch_tmp;
  8040. i40e_status ret;
  8041. if (list_empty(&vsi->ch_list))
  8042. return 0;
  8043. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  8044. if (!ch->initialized)
  8045. break;
  8046. /* Proceed with creation of channel (VMDq2) VSI */
  8047. ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
  8048. if (ret) {
  8049. dev_info(&vsi->back->pdev->dev,
  8050. "failed to rebuild channels using uplink_seid %u\n",
  8051. vsi->uplink_seid);
  8052. return ret;
  8053. }
  8054. /* Reconfigure TX queues using QTX_CTL register */
  8055. ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
  8056. if (ret) {
  8057. dev_info(&vsi->back->pdev->dev,
  8058. "failed to configure TX rings for channel %u\n",
  8059. ch->seid);
  8060. return ret;
  8061. }
  8062. /* update 'next_base_queue' */
  8063. vsi->next_base_queue = vsi->next_base_queue +
  8064. ch->num_queue_pairs;
  8065. if (ch->max_tx_rate) {
  8066. u64 credits = ch->max_tx_rate;
  8067. if (i40e_set_bw_limit(vsi, ch->seid,
  8068. ch->max_tx_rate))
  8069. return -EINVAL;
  8070. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8071. dev_dbg(&vsi->back->pdev->dev,
  8072. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8073. ch->max_tx_rate,
  8074. credits,
  8075. ch->seid);
  8076. }
  8077. ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
  8078. if (ret) {
  8079. dev_dbg(&vsi->back->pdev->dev,
  8080. "Failed to rebuild cloud filters for channel VSI %u\n",
  8081. ch->seid);
  8082. return ret;
  8083. }
  8084. }
  8085. return 0;
  8086. }
  8087. /**
  8088. * i40e_prep_for_reset - prep for the core to reset
  8089. * @pf: board private structure
  8090. * @lock_acquired: indicates whether or not the lock has been acquired
  8091. * before this function was called.
  8092. *
  8093. * Close up the VFs and other things in prep for PF Reset.
  8094. **/
  8095. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  8096. {
  8097. struct i40e_hw *hw = &pf->hw;
  8098. i40e_status ret = 0;
  8099. u32 v;
  8100. clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  8101. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8102. return;
  8103. if (i40e_check_asq_alive(&pf->hw))
  8104. i40e_vc_notify_reset(pf);
  8105. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  8106. /* quiesce the VSIs and their queues that are not already DOWN */
  8107. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  8108. if (!lock_acquired)
  8109. rtnl_lock();
  8110. i40e_pf_quiesce_all_vsi(pf);
  8111. if (!lock_acquired)
  8112. rtnl_unlock();
  8113. for (v = 0; v < pf->num_alloc_vsi; v++) {
  8114. if (pf->vsi[v])
  8115. pf->vsi[v]->seid = 0;
  8116. }
  8117. i40e_shutdown_adminq(&pf->hw);
  8118. /* call shutdown HMC */
  8119. if (hw->hmc.hmc_obj) {
  8120. ret = i40e_shutdown_lan_hmc(hw);
  8121. if (ret)
  8122. dev_warn(&pf->pdev->dev,
  8123. "shutdown_lan_hmc failed: %d\n", ret);
  8124. }
  8125. }
  8126. /**
  8127. * i40e_send_version - update firmware with driver version
  8128. * @pf: PF struct
  8129. */
  8130. static void i40e_send_version(struct i40e_pf *pf)
  8131. {
  8132. struct i40e_driver_version dv;
  8133. dv.major_version = DRV_VERSION_MAJOR;
  8134. dv.minor_version = DRV_VERSION_MINOR;
  8135. dv.build_version = DRV_VERSION_BUILD;
  8136. dv.subbuild_version = 0;
  8137. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  8138. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  8139. }
  8140. /**
  8141. * i40e_get_oem_version - get OEM specific version information
  8142. * @hw: pointer to the hardware structure
  8143. **/
  8144. static void i40e_get_oem_version(struct i40e_hw *hw)
  8145. {
  8146. u16 block_offset = 0xffff;
  8147. u16 block_length = 0;
  8148. u16 capabilities = 0;
  8149. u16 gen_snap = 0;
  8150. u16 release = 0;
  8151. #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
  8152. #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
  8153. #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
  8154. #define I40E_NVM_OEM_GEN_OFFSET 0x02
  8155. #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
  8156. #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
  8157. #define I40E_NVM_OEM_LENGTH 3
  8158. /* Check if pointer to OEM version block is valid. */
  8159. i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
  8160. if (block_offset == 0xffff)
  8161. return;
  8162. /* Check if OEM version block has correct length. */
  8163. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
  8164. &block_length);
  8165. if (block_length < I40E_NVM_OEM_LENGTH)
  8166. return;
  8167. /* Check if OEM version format is as expected. */
  8168. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
  8169. &capabilities);
  8170. if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
  8171. return;
  8172. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
  8173. &gen_snap);
  8174. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
  8175. &release);
  8176. hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
  8177. hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
  8178. }
  8179. /**
  8180. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  8181. * @pf: board private structure
  8182. **/
  8183. static int i40e_reset(struct i40e_pf *pf)
  8184. {
  8185. struct i40e_hw *hw = &pf->hw;
  8186. i40e_status ret;
  8187. ret = i40e_pf_reset(hw);
  8188. if (ret) {
  8189. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  8190. set_bit(__I40E_RESET_FAILED, pf->state);
  8191. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8192. } else {
  8193. pf->pfr_count++;
  8194. }
  8195. return ret;
  8196. }
  8197. /**
  8198. * i40e_rebuild - rebuild using a saved config
  8199. * @pf: board private structure
  8200. * @reinit: if the Main VSI needs to re-initialized.
  8201. * @lock_acquired: indicates whether or not the lock has been acquired
  8202. * before this function was called.
  8203. **/
  8204. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  8205. {
  8206. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  8207. struct i40e_hw *hw = &pf->hw;
  8208. u8 set_fc_aq_fail = 0;
  8209. i40e_status ret;
  8210. u32 val;
  8211. int v;
  8212. if (test_bit(__I40E_DOWN, pf->state))
  8213. goto clear_recovery;
  8214. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  8215. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  8216. ret = i40e_init_adminq(&pf->hw);
  8217. if (ret) {
  8218. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  8219. i40e_stat_str(&pf->hw, ret),
  8220. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8221. goto clear_recovery;
  8222. }
  8223. i40e_get_oem_version(&pf->hw);
  8224. if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
  8225. ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
  8226. hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
  8227. /* The following delay is necessary for 4.33 firmware and older
  8228. * to recover after EMP reset. 200 ms should suffice but we
  8229. * put here 300 ms to be sure that FW is ready to operate
  8230. * after reset.
  8231. */
  8232. mdelay(300);
  8233. }
  8234. /* re-verify the eeprom if we just had an EMP reset */
  8235. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
  8236. i40e_verify_eeprom(pf);
  8237. i40e_clear_pxe_mode(hw);
  8238. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  8239. if (ret)
  8240. goto end_core_reset;
  8241. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  8242. hw->func_caps.num_rx_qp, 0, 0);
  8243. if (ret) {
  8244. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  8245. goto end_core_reset;
  8246. }
  8247. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  8248. if (ret) {
  8249. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  8250. goto end_core_reset;
  8251. }
  8252. /* Enable FW to write a default DCB config on link-up */
  8253. i40e_aq_set_dcb_parameters(hw, true, NULL);
  8254. #ifdef CONFIG_I40E_DCB
  8255. ret = i40e_init_pf_dcb(pf);
  8256. if (ret) {
  8257. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  8258. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  8259. /* Continue without DCB enabled */
  8260. }
  8261. #endif /* CONFIG_I40E_DCB */
  8262. /* do basic switch setup */
  8263. if (!lock_acquired)
  8264. rtnl_lock();
  8265. ret = i40e_setup_pf_switch(pf, reinit);
  8266. if (ret)
  8267. goto end_unlock;
  8268. /* The driver only wants link up/down and module qualification
  8269. * reports from firmware. Note the negative logic.
  8270. */
  8271. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  8272. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  8273. I40E_AQ_EVENT_MEDIA_NA |
  8274. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  8275. if (ret)
  8276. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  8277. i40e_stat_str(&pf->hw, ret),
  8278. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8279. /* make sure our flow control settings are restored */
  8280. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  8281. if (ret)
  8282. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  8283. i40e_stat_str(&pf->hw, ret),
  8284. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8285. /* Rebuild the VSIs and VEBs that existed before reset.
  8286. * They are still in our local switch element arrays, so only
  8287. * need to rebuild the switch model in the HW.
  8288. *
  8289. * If there were VEBs but the reconstitution failed, we'll try
  8290. * try to recover minimal use by getting the basic PF VSI working.
  8291. */
  8292. if (vsi->uplink_seid != pf->mac_seid) {
  8293. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  8294. /* find the one VEB connected to the MAC, and find orphans */
  8295. for (v = 0; v < I40E_MAX_VEB; v++) {
  8296. if (!pf->veb[v])
  8297. continue;
  8298. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  8299. pf->veb[v]->uplink_seid == 0) {
  8300. ret = i40e_reconstitute_veb(pf->veb[v]);
  8301. if (!ret)
  8302. continue;
  8303. /* If Main VEB failed, we're in deep doodoo,
  8304. * so give up rebuilding the switch and set up
  8305. * for minimal rebuild of PF VSI.
  8306. * If orphan failed, we'll report the error
  8307. * but try to keep going.
  8308. */
  8309. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  8310. dev_info(&pf->pdev->dev,
  8311. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  8312. ret);
  8313. vsi->uplink_seid = pf->mac_seid;
  8314. break;
  8315. } else if (pf->veb[v]->uplink_seid == 0) {
  8316. dev_info(&pf->pdev->dev,
  8317. "rebuild of orphan VEB failed: %d\n",
  8318. ret);
  8319. }
  8320. }
  8321. }
  8322. }
  8323. if (vsi->uplink_seid == pf->mac_seid) {
  8324. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  8325. /* no VEB, so rebuild only the Main VSI */
  8326. ret = i40e_add_vsi(vsi);
  8327. if (ret) {
  8328. dev_info(&pf->pdev->dev,
  8329. "rebuild of Main VSI failed: %d\n", ret);
  8330. goto end_unlock;
  8331. }
  8332. }
  8333. if (vsi->mqprio_qopt.max_rate[0]) {
  8334. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  8335. u64 credits = 0;
  8336. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  8337. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  8338. if (ret)
  8339. goto end_unlock;
  8340. credits = max_tx_rate;
  8341. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8342. dev_dbg(&vsi->back->pdev->dev,
  8343. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8344. max_tx_rate,
  8345. credits,
  8346. vsi->seid);
  8347. }
  8348. ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
  8349. if (ret)
  8350. goto end_unlock;
  8351. /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
  8352. * for this main VSI if they exist
  8353. */
  8354. ret = i40e_rebuild_channels(vsi);
  8355. if (ret)
  8356. goto end_unlock;
  8357. /* Reconfigure hardware for allowing smaller MSS in the case
  8358. * of TSO, so that we avoid the MDD being fired and causing
  8359. * a reset in the case of small MSS+TSO.
  8360. */
  8361. #define I40E_REG_MSS 0x000E64DC
  8362. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  8363. #define I40E_64BYTE_MSS 0x400000
  8364. val = rd32(hw, I40E_REG_MSS);
  8365. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  8366. val &= ~I40E_REG_MSS_MIN_MASK;
  8367. val |= I40E_64BYTE_MSS;
  8368. wr32(hw, I40E_REG_MSS, val);
  8369. }
  8370. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  8371. msleep(75);
  8372. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  8373. if (ret)
  8374. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  8375. i40e_stat_str(&pf->hw, ret),
  8376. i40e_aq_str(&pf->hw,
  8377. pf->hw.aq.asq_last_status));
  8378. }
  8379. /* reinit the misc interrupt */
  8380. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8381. ret = i40e_setup_misc_vector(pf);
  8382. /* Add a filter to drop all Flow control frames from any VSI from being
  8383. * transmitted. By doing so we stop a malicious VF from sending out
  8384. * PAUSE or PFC frames and potentially controlling traffic for other
  8385. * PF/VF VSIs.
  8386. * The FW can still send Flow control frames if enabled.
  8387. */
  8388. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  8389. pf->main_vsi_seid);
  8390. /* restart the VSIs that were rebuilt and running before the reset */
  8391. i40e_pf_unquiesce_all_vsi(pf);
  8392. /* Release the RTNL lock before we start resetting VFs */
  8393. if (!lock_acquired)
  8394. rtnl_unlock();
  8395. /* Restore promiscuous settings */
  8396. ret = i40e_set_promiscuous(pf, pf->cur_promisc);
  8397. if (ret)
  8398. dev_warn(&pf->pdev->dev,
  8399. "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
  8400. pf->cur_promisc ? "on" : "off",
  8401. i40e_stat_str(&pf->hw, ret),
  8402. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8403. i40e_reset_all_vfs(pf, true);
  8404. /* tell the firmware that we're starting */
  8405. i40e_send_version(pf);
  8406. /* We've already released the lock, so don't do it again */
  8407. goto end_core_reset;
  8408. end_unlock:
  8409. if (!lock_acquired)
  8410. rtnl_unlock();
  8411. end_core_reset:
  8412. clear_bit(__I40E_RESET_FAILED, pf->state);
  8413. clear_recovery:
  8414. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8415. clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
  8416. }
  8417. /**
  8418. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  8419. * @pf: board private structure
  8420. * @reinit: if the Main VSI needs to re-initialized.
  8421. * @lock_acquired: indicates whether or not the lock has been acquired
  8422. * before this function was called.
  8423. **/
  8424. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  8425. bool lock_acquired)
  8426. {
  8427. int ret;
  8428. /* Now we wait for GRST to settle out.
  8429. * We don't have to delete the VEBs or VSIs from the hw switch
  8430. * because the reset will make them disappear.
  8431. */
  8432. ret = i40e_reset(pf);
  8433. if (!ret)
  8434. i40e_rebuild(pf, reinit, lock_acquired);
  8435. }
  8436. /**
  8437. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  8438. * @pf: board private structure
  8439. *
  8440. * Close up the VFs and other things in prep for a Core Reset,
  8441. * then get ready to rebuild the world.
  8442. * @lock_acquired: indicates whether or not the lock has been acquired
  8443. * before this function was called.
  8444. **/
  8445. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  8446. {
  8447. i40e_prep_for_reset(pf, lock_acquired);
  8448. i40e_reset_and_rebuild(pf, false, lock_acquired);
  8449. }
  8450. /**
  8451. * i40e_handle_mdd_event
  8452. * @pf: pointer to the PF structure
  8453. *
  8454. * Called from the MDD irq handler to identify possibly malicious vfs
  8455. **/
  8456. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  8457. {
  8458. struct i40e_hw *hw = &pf->hw;
  8459. bool mdd_detected = false;
  8460. bool pf_mdd_detected = false;
  8461. struct i40e_vf *vf;
  8462. u32 reg;
  8463. int i;
  8464. if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
  8465. return;
  8466. /* find what triggered the MDD event */
  8467. reg = rd32(hw, I40E_GL_MDET_TX);
  8468. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  8469. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  8470. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  8471. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  8472. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  8473. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  8474. I40E_GL_MDET_TX_EVENT_SHIFT;
  8475. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  8476. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  8477. pf->hw.func_caps.base_queue;
  8478. if (netif_msg_tx_err(pf))
  8479. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  8480. event, queue, pf_num, vf_num);
  8481. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  8482. mdd_detected = true;
  8483. }
  8484. reg = rd32(hw, I40E_GL_MDET_RX);
  8485. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  8486. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  8487. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  8488. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  8489. I40E_GL_MDET_RX_EVENT_SHIFT;
  8490. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  8491. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  8492. pf->hw.func_caps.base_queue;
  8493. if (netif_msg_rx_err(pf))
  8494. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  8495. event, queue, func);
  8496. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  8497. mdd_detected = true;
  8498. }
  8499. if (mdd_detected) {
  8500. reg = rd32(hw, I40E_PF_MDET_TX);
  8501. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  8502. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  8503. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  8504. pf_mdd_detected = true;
  8505. }
  8506. reg = rd32(hw, I40E_PF_MDET_RX);
  8507. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  8508. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  8509. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  8510. pf_mdd_detected = true;
  8511. }
  8512. /* Queue belongs to the PF, initiate a reset */
  8513. if (pf_mdd_detected) {
  8514. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  8515. i40e_service_event_schedule(pf);
  8516. }
  8517. }
  8518. /* see if one of the VFs needs its hand slapped */
  8519. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  8520. vf = &(pf->vf[i]);
  8521. reg = rd32(hw, I40E_VP_MDET_TX(i));
  8522. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  8523. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  8524. vf->num_mdd_events++;
  8525. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  8526. i);
  8527. }
  8528. reg = rd32(hw, I40E_VP_MDET_RX(i));
  8529. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  8530. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  8531. vf->num_mdd_events++;
  8532. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  8533. i);
  8534. }
  8535. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  8536. dev_info(&pf->pdev->dev,
  8537. "Too many MDD events on VF %d, disabled\n", i);
  8538. dev_info(&pf->pdev->dev,
  8539. "Use PF Control I/F to re-enable the VF\n");
  8540. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  8541. }
  8542. }
  8543. /* re-enable mdd interrupt cause */
  8544. clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  8545. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  8546. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  8547. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  8548. i40e_flush(hw);
  8549. }
  8550. static const char *i40e_tunnel_name(u8 type)
  8551. {
  8552. switch (type) {
  8553. case UDP_TUNNEL_TYPE_VXLAN:
  8554. return "vxlan";
  8555. case UDP_TUNNEL_TYPE_GENEVE:
  8556. return "geneve";
  8557. default:
  8558. return "unknown";
  8559. }
  8560. }
  8561. /**
  8562. * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
  8563. * @pf: board private structure
  8564. **/
  8565. static void i40e_sync_udp_filters(struct i40e_pf *pf)
  8566. {
  8567. int i;
  8568. /* loop through and set pending bit for all active UDP filters */
  8569. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8570. if (pf->udp_ports[i].port)
  8571. pf->pending_udp_bitmap |= BIT_ULL(i);
  8572. }
  8573. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  8574. }
  8575. /**
  8576. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  8577. * @pf: board private structure
  8578. **/
  8579. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  8580. {
  8581. struct i40e_hw *hw = &pf->hw;
  8582. u8 filter_index, type;
  8583. u16 port;
  8584. int i;
  8585. if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
  8586. return;
  8587. /* acquire RTNL to maintain state of flags and port requests */
  8588. rtnl_lock();
  8589. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8590. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  8591. struct i40e_udp_port_config *udp_port;
  8592. i40e_status ret = 0;
  8593. udp_port = &pf->udp_ports[i];
  8594. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8595. port = READ_ONCE(udp_port->port);
  8596. type = READ_ONCE(udp_port->type);
  8597. filter_index = READ_ONCE(udp_port->filter_index);
  8598. /* release RTNL while we wait on AQ command */
  8599. rtnl_unlock();
  8600. if (port)
  8601. ret = i40e_aq_add_udp_tunnel(hw, port,
  8602. type,
  8603. &filter_index,
  8604. NULL);
  8605. else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
  8606. ret = i40e_aq_del_udp_tunnel(hw, filter_index,
  8607. NULL);
  8608. /* reacquire RTNL so we can update filter_index */
  8609. rtnl_lock();
  8610. if (ret) {
  8611. dev_info(&pf->pdev->dev,
  8612. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  8613. i40e_tunnel_name(type),
  8614. port ? "add" : "delete",
  8615. port,
  8616. filter_index,
  8617. i40e_stat_str(&pf->hw, ret),
  8618. i40e_aq_str(&pf->hw,
  8619. pf->hw.aq.asq_last_status));
  8620. if (port) {
  8621. /* failed to add, just reset port,
  8622. * drop pending bit for any deletion
  8623. */
  8624. udp_port->port = 0;
  8625. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8626. }
  8627. } else if (port) {
  8628. /* record filter index on success */
  8629. udp_port->filter_index = filter_index;
  8630. }
  8631. }
  8632. }
  8633. rtnl_unlock();
  8634. }
  8635. /**
  8636. * i40e_service_task - Run the driver's async subtasks
  8637. * @work: pointer to work_struct containing our data
  8638. **/
  8639. static void i40e_service_task(struct work_struct *work)
  8640. {
  8641. struct i40e_pf *pf = container_of(work,
  8642. struct i40e_pf,
  8643. service_task);
  8644. unsigned long start_time = jiffies;
  8645. /* don't bother with service tasks if a reset is in progress */
  8646. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8647. return;
  8648. if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
  8649. return;
  8650. i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
  8651. i40e_sync_filters_subtask(pf);
  8652. i40e_reset_subtask(pf);
  8653. i40e_handle_mdd_event(pf);
  8654. i40e_vc_process_vflr_event(pf);
  8655. i40e_watchdog_subtask(pf);
  8656. i40e_fdir_reinit_subtask(pf);
  8657. if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
  8658. /* Client subtask will reopen next time through. */
  8659. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  8660. } else {
  8661. i40e_client_subtask(pf);
  8662. if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
  8663. pf->state))
  8664. i40e_notify_client_of_l2_param_changes(
  8665. pf->vsi[pf->lan_vsi]);
  8666. }
  8667. i40e_sync_filters_subtask(pf);
  8668. i40e_sync_udp_filters_subtask(pf);
  8669. i40e_clean_adminq_subtask(pf);
  8670. /* flush memory to make sure state is correct before next watchdog */
  8671. smp_mb__before_atomic();
  8672. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  8673. /* If the tasks have taken longer than one timer cycle or there
  8674. * is more work to be done, reschedule the service task now
  8675. * rather than wait for the timer to tick again.
  8676. */
  8677. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  8678. test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
  8679. test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
  8680. test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  8681. i40e_service_event_schedule(pf);
  8682. }
  8683. /**
  8684. * i40e_service_timer - timer callback
  8685. * @data: pointer to PF struct
  8686. **/
  8687. static void i40e_service_timer(struct timer_list *t)
  8688. {
  8689. struct i40e_pf *pf = from_timer(pf, t, service_timer);
  8690. mod_timer(&pf->service_timer,
  8691. round_jiffies(jiffies + pf->service_timer_period));
  8692. i40e_service_event_schedule(pf);
  8693. }
  8694. /**
  8695. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  8696. * @vsi: the VSI being configured
  8697. **/
  8698. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  8699. {
  8700. struct i40e_pf *pf = vsi->back;
  8701. switch (vsi->type) {
  8702. case I40E_VSI_MAIN:
  8703. vsi->alloc_queue_pairs = pf->num_lan_qps;
  8704. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8705. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8706. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8707. vsi->num_q_vectors = pf->num_lan_msix;
  8708. else
  8709. vsi->num_q_vectors = 1;
  8710. break;
  8711. case I40E_VSI_FDIR:
  8712. vsi->alloc_queue_pairs = 1;
  8713. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  8714. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8715. vsi->num_q_vectors = pf->num_fdsb_msix;
  8716. break;
  8717. case I40E_VSI_VMDQ2:
  8718. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  8719. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8720. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8721. vsi->num_q_vectors = pf->num_vmdq_msix;
  8722. break;
  8723. case I40E_VSI_SRIOV:
  8724. vsi->alloc_queue_pairs = pf->num_vf_qps;
  8725. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8726. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8727. break;
  8728. default:
  8729. WARN_ON(1);
  8730. return -ENODATA;
  8731. }
  8732. return 0;
  8733. }
  8734. /**
  8735. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  8736. * @vsi: VSI pointer
  8737. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  8738. *
  8739. * On error: returns error code (negative)
  8740. * On success: returns 0
  8741. **/
  8742. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  8743. {
  8744. struct i40e_ring **next_rings;
  8745. int size;
  8746. int ret = 0;
  8747. /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
  8748. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
  8749. (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
  8750. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  8751. if (!vsi->tx_rings)
  8752. return -ENOMEM;
  8753. next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
  8754. if (i40e_enabled_xdp_vsi(vsi)) {
  8755. vsi->xdp_rings = next_rings;
  8756. next_rings += vsi->alloc_queue_pairs;
  8757. }
  8758. vsi->rx_rings = next_rings;
  8759. if (alloc_qvectors) {
  8760. /* allocate memory for q_vector pointers */
  8761. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  8762. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  8763. if (!vsi->q_vectors) {
  8764. ret = -ENOMEM;
  8765. goto err_vectors;
  8766. }
  8767. }
  8768. return ret;
  8769. err_vectors:
  8770. kfree(vsi->tx_rings);
  8771. return ret;
  8772. }
  8773. /**
  8774. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  8775. * @pf: board private structure
  8776. * @type: type of VSI
  8777. *
  8778. * On error: returns error code (negative)
  8779. * On success: returns vsi index in PF (positive)
  8780. **/
  8781. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  8782. {
  8783. int ret = -ENODEV;
  8784. struct i40e_vsi *vsi;
  8785. int vsi_idx;
  8786. int i;
  8787. /* Need to protect the allocation of the VSIs at the PF level */
  8788. mutex_lock(&pf->switch_mutex);
  8789. /* VSI list may be fragmented if VSI creation/destruction has
  8790. * been happening. We can afford to do a quick scan to look
  8791. * for any free VSIs in the list.
  8792. *
  8793. * find next empty vsi slot, looping back around if necessary
  8794. */
  8795. i = pf->next_vsi;
  8796. while (i < pf->num_alloc_vsi && pf->vsi[i])
  8797. i++;
  8798. if (i >= pf->num_alloc_vsi) {
  8799. i = 0;
  8800. while (i < pf->next_vsi && pf->vsi[i])
  8801. i++;
  8802. }
  8803. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  8804. vsi_idx = i; /* Found one! */
  8805. } else {
  8806. ret = -ENODEV;
  8807. goto unlock_pf; /* out of VSI slots! */
  8808. }
  8809. pf->next_vsi = ++i;
  8810. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  8811. if (!vsi) {
  8812. ret = -ENOMEM;
  8813. goto unlock_pf;
  8814. }
  8815. vsi->type = type;
  8816. vsi->back = pf;
  8817. set_bit(__I40E_VSI_DOWN, vsi->state);
  8818. vsi->flags = 0;
  8819. vsi->idx = vsi_idx;
  8820. vsi->int_rate_limit = 0;
  8821. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  8822. pf->rss_table_size : 64;
  8823. vsi->netdev_registered = false;
  8824. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  8825. hash_init(vsi->mac_filter_hash);
  8826. vsi->irqs_ready = false;
  8827. ret = i40e_set_num_rings_in_vsi(vsi);
  8828. if (ret)
  8829. goto err_rings;
  8830. ret = i40e_vsi_alloc_arrays(vsi, true);
  8831. if (ret)
  8832. goto err_rings;
  8833. /* Setup default MSIX irq handler for VSI */
  8834. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  8835. /* Initialize VSI lock */
  8836. spin_lock_init(&vsi->mac_filter_hash_lock);
  8837. pf->vsi[vsi_idx] = vsi;
  8838. ret = vsi_idx;
  8839. goto unlock_pf;
  8840. err_rings:
  8841. pf->next_vsi = i - 1;
  8842. kfree(vsi);
  8843. unlock_pf:
  8844. mutex_unlock(&pf->switch_mutex);
  8845. return ret;
  8846. }
  8847. /**
  8848. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  8849. * @vsi: VSI pointer
  8850. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  8851. *
  8852. * On error: returns error code (negative)
  8853. * On success: returns 0
  8854. **/
  8855. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  8856. {
  8857. /* free the ring and vector containers */
  8858. if (free_qvectors) {
  8859. kfree(vsi->q_vectors);
  8860. vsi->q_vectors = NULL;
  8861. }
  8862. kfree(vsi->tx_rings);
  8863. vsi->tx_rings = NULL;
  8864. vsi->rx_rings = NULL;
  8865. vsi->xdp_rings = NULL;
  8866. }
  8867. /**
  8868. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  8869. * and lookup table
  8870. * @vsi: Pointer to VSI structure
  8871. */
  8872. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  8873. {
  8874. if (!vsi)
  8875. return;
  8876. kfree(vsi->rss_hkey_user);
  8877. vsi->rss_hkey_user = NULL;
  8878. kfree(vsi->rss_lut_user);
  8879. vsi->rss_lut_user = NULL;
  8880. }
  8881. /**
  8882. * i40e_vsi_clear - Deallocate the VSI provided
  8883. * @vsi: the VSI being un-configured
  8884. **/
  8885. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  8886. {
  8887. struct i40e_pf *pf;
  8888. if (!vsi)
  8889. return 0;
  8890. if (!vsi->back)
  8891. goto free_vsi;
  8892. pf = vsi->back;
  8893. mutex_lock(&pf->switch_mutex);
  8894. if (!pf->vsi[vsi->idx]) {
  8895. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
  8896. vsi->idx, vsi->idx, vsi->type);
  8897. goto unlock_vsi;
  8898. }
  8899. if (pf->vsi[vsi->idx] != vsi) {
  8900. dev_err(&pf->pdev->dev,
  8901. "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
  8902. pf->vsi[vsi->idx]->idx,
  8903. pf->vsi[vsi->idx]->type,
  8904. vsi->idx, vsi->type);
  8905. goto unlock_vsi;
  8906. }
  8907. /* updates the PF for this cleared vsi */
  8908. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8909. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  8910. i40e_vsi_free_arrays(vsi, true);
  8911. i40e_clear_rss_config_user(vsi);
  8912. pf->vsi[vsi->idx] = NULL;
  8913. if (vsi->idx < pf->next_vsi)
  8914. pf->next_vsi = vsi->idx;
  8915. unlock_vsi:
  8916. mutex_unlock(&pf->switch_mutex);
  8917. free_vsi:
  8918. kfree(vsi);
  8919. return 0;
  8920. }
  8921. /**
  8922. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  8923. * @vsi: the VSI being cleaned
  8924. **/
  8925. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  8926. {
  8927. int i;
  8928. if (vsi->tx_rings && vsi->tx_rings[0]) {
  8929. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8930. kfree_rcu(vsi->tx_rings[i], rcu);
  8931. vsi->tx_rings[i] = NULL;
  8932. vsi->rx_rings[i] = NULL;
  8933. if (vsi->xdp_rings)
  8934. vsi->xdp_rings[i] = NULL;
  8935. }
  8936. }
  8937. }
  8938. /**
  8939. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  8940. * @vsi: the VSI being configured
  8941. **/
  8942. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  8943. {
  8944. int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
  8945. struct i40e_pf *pf = vsi->back;
  8946. struct i40e_ring *ring;
  8947. /* Set basic values in the rings to be used later during open() */
  8948. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8949. /* allocate space for both Tx and Rx in one shot */
  8950. ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
  8951. if (!ring)
  8952. goto err_out;
  8953. ring->queue_index = i;
  8954. ring->reg_idx = vsi->base_queue + i;
  8955. ring->ring_active = false;
  8956. ring->vsi = vsi;
  8957. ring->netdev = vsi->netdev;
  8958. ring->dev = &pf->pdev->dev;
  8959. ring->count = vsi->num_desc;
  8960. ring->size = 0;
  8961. ring->dcb_tc = 0;
  8962. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8963. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8964. ring->itr_setting = pf->tx_itr_default;
  8965. vsi->tx_rings[i] = ring++;
  8966. if (!i40e_enabled_xdp_vsi(vsi))
  8967. goto setup_rx;
  8968. ring->queue_index = vsi->alloc_queue_pairs + i;
  8969. ring->reg_idx = vsi->base_queue + ring->queue_index;
  8970. ring->ring_active = false;
  8971. ring->vsi = vsi;
  8972. ring->netdev = NULL;
  8973. ring->dev = &pf->pdev->dev;
  8974. ring->count = vsi->num_desc;
  8975. ring->size = 0;
  8976. ring->dcb_tc = 0;
  8977. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8978. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8979. set_ring_xdp(ring);
  8980. ring->itr_setting = pf->tx_itr_default;
  8981. vsi->xdp_rings[i] = ring++;
  8982. setup_rx:
  8983. ring->queue_index = i;
  8984. ring->reg_idx = vsi->base_queue + i;
  8985. ring->ring_active = false;
  8986. ring->vsi = vsi;
  8987. ring->netdev = vsi->netdev;
  8988. ring->dev = &pf->pdev->dev;
  8989. ring->count = vsi->num_desc;
  8990. ring->size = 0;
  8991. ring->dcb_tc = 0;
  8992. ring->itr_setting = pf->rx_itr_default;
  8993. vsi->rx_rings[i] = ring;
  8994. }
  8995. return 0;
  8996. err_out:
  8997. i40e_vsi_clear_rings(vsi);
  8998. return -ENOMEM;
  8999. }
  9000. /**
  9001. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  9002. * @pf: board private structure
  9003. * @vectors: the number of MSI-X vectors to request
  9004. *
  9005. * Returns the number of vectors reserved, or error
  9006. **/
  9007. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  9008. {
  9009. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  9010. I40E_MIN_MSIX, vectors);
  9011. if (vectors < 0) {
  9012. dev_info(&pf->pdev->dev,
  9013. "MSI-X vector reservation failed: %d\n", vectors);
  9014. vectors = 0;
  9015. }
  9016. return vectors;
  9017. }
  9018. /**
  9019. * i40e_init_msix - Setup the MSIX capability
  9020. * @pf: board private structure
  9021. *
  9022. * Work with the OS to set up the MSIX vectors needed.
  9023. *
  9024. * Returns the number of vectors reserved or negative on failure
  9025. **/
  9026. static int i40e_init_msix(struct i40e_pf *pf)
  9027. {
  9028. struct i40e_hw *hw = &pf->hw;
  9029. int cpus, extra_vectors;
  9030. int vectors_left;
  9031. int v_budget, i;
  9032. int v_actual;
  9033. int iwarp_requested = 0;
  9034. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  9035. return -ENODEV;
  9036. /* The number of vectors we'll request will be comprised of:
  9037. * - Add 1 for "other" cause for Admin Queue events, etc.
  9038. * - The number of LAN queue pairs
  9039. * - Queues being used for RSS.
  9040. * We don't need as many as max_rss_size vectors.
  9041. * use rss_size instead in the calculation since that
  9042. * is governed by number of cpus in the system.
  9043. * - assumes symmetric Tx/Rx pairing
  9044. * - The number of VMDq pairs
  9045. * - The CPU count within the NUMA node if iWARP is enabled
  9046. * Once we count this up, try the request.
  9047. *
  9048. * If we can't get what we want, we'll simplify to nearly nothing
  9049. * and try again. If that still fails, we punt.
  9050. */
  9051. vectors_left = hw->func_caps.num_msix_vectors;
  9052. v_budget = 0;
  9053. /* reserve one vector for miscellaneous handler */
  9054. if (vectors_left) {
  9055. v_budget++;
  9056. vectors_left--;
  9057. }
  9058. /* reserve some vectors for the main PF traffic queues. Initially we
  9059. * only reserve at most 50% of the available vectors, in the case that
  9060. * the number of online CPUs is large. This ensures that we can enable
  9061. * extra features as well. Once we've enabled the other features, we
  9062. * will use any remaining vectors to reach as close as we can to the
  9063. * number of online CPUs.
  9064. */
  9065. cpus = num_online_cpus();
  9066. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  9067. vectors_left -= pf->num_lan_msix;
  9068. /* reserve one vector for sideband flow director */
  9069. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9070. if (vectors_left) {
  9071. pf->num_fdsb_msix = 1;
  9072. v_budget++;
  9073. vectors_left--;
  9074. } else {
  9075. pf->num_fdsb_msix = 0;
  9076. }
  9077. }
  9078. /* can we reserve enough for iWARP? */
  9079. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9080. iwarp_requested = pf->num_iwarp_msix;
  9081. if (!vectors_left)
  9082. pf->num_iwarp_msix = 0;
  9083. else if (vectors_left < pf->num_iwarp_msix)
  9084. pf->num_iwarp_msix = 1;
  9085. v_budget += pf->num_iwarp_msix;
  9086. vectors_left -= pf->num_iwarp_msix;
  9087. }
  9088. /* any vectors left over go for VMDq support */
  9089. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  9090. if (!vectors_left) {
  9091. pf->num_vmdq_msix = 0;
  9092. pf->num_vmdq_qps = 0;
  9093. } else {
  9094. int vmdq_vecs_wanted =
  9095. pf->num_vmdq_vsis * pf->num_vmdq_qps;
  9096. int vmdq_vecs =
  9097. min_t(int, vectors_left, vmdq_vecs_wanted);
  9098. /* if we're short on vectors for what's desired, we limit
  9099. * the queues per vmdq. If this is still more than are
  9100. * available, the user will need to change the number of
  9101. * queues/vectors used by the PF later with the ethtool
  9102. * channels command
  9103. */
  9104. if (vectors_left < vmdq_vecs_wanted) {
  9105. pf->num_vmdq_qps = 1;
  9106. vmdq_vecs_wanted = pf->num_vmdq_vsis;
  9107. vmdq_vecs = min_t(int,
  9108. vectors_left,
  9109. vmdq_vecs_wanted);
  9110. }
  9111. pf->num_vmdq_msix = pf->num_vmdq_qps;
  9112. v_budget += vmdq_vecs;
  9113. vectors_left -= vmdq_vecs;
  9114. }
  9115. }
  9116. /* On systems with a large number of SMP cores, we previously limited
  9117. * the number of vectors for num_lan_msix to be at most 50% of the
  9118. * available vectors, to allow for other features. Now, we add back
  9119. * the remaining vectors. However, we ensure that the total
  9120. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  9121. * calculate the number of vectors we can add without going over the
  9122. * cap of CPUs. For systems with a small number of CPUs this will be
  9123. * zero.
  9124. */
  9125. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  9126. pf->num_lan_msix += extra_vectors;
  9127. vectors_left -= extra_vectors;
  9128. WARN(vectors_left < 0,
  9129. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  9130. v_budget += pf->num_lan_msix;
  9131. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  9132. GFP_KERNEL);
  9133. if (!pf->msix_entries)
  9134. return -ENOMEM;
  9135. for (i = 0; i < v_budget; i++)
  9136. pf->msix_entries[i].entry = i;
  9137. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  9138. if (v_actual < I40E_MIN_MSIX) {
  9139. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  9140. kfree(pf->msix_entries);
  9141. pf->msix_entries = NULL;
  9142. pci_disable_msix(pf->pdev);
  9143. return -ENODEV;
  9144. } else if (v_actual == I40E_MIN_MSIX) {
  9145. /* Adjust for minimal MSIX use */
  9146. pf->num_vmdq_vsis = 0;
  9147. pf->num_vmdq_qps = 0;
  9148. pf->num_lan_qps = 1;
  9149. pf->num_lan_msix = 1;
  9150. } else if (v_actual != v_budget) {
  9151. /* If we have limited resources, we will start with no vectors
  9152. * for the special features and then allocate vectors to some
  9153. * of these features based on the policy and at the end disable
  9154. * the features that did not get any vectors.
  9155. */
  9156. int vec;
  9157. dev_info(&pf->pdev->dev,
  9158. "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
  9159. v_actual, v_budget);
  9160. /* reserve the misc vector */
  9161. vec = v_actual - 1;
  9162. /* Scale vector usage down */
  9163. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  9164. pf->num_vmdq_vsis = 1;
  9165. pf->num_vmdq_qps = 1;
  9166. /* partition out the remaining vectors */
  9167. switch (vec) {
  9168. case 2:
  9169. pf->num_lan_msix = 1;
  9170. break;
  9171. case 3:
  9172. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9173. pf->num_lan_msix = 1;
  9174. pf->num_iwarp_msix = 1;
  9175. } else {
  9176. pf->num_lan_msix = 2;
  9177. }
  9178. break;
  9179. default:
  9180. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9181. pf->num_iwarp_msix = min_t(int, (vec / 3),
  9182. iwarp_requested);
  9183. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  9184. I40E_DEFAULT_NUM_VMDQ_VSI);
  9185. } else {
  9186. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  9187. I40E_DEFAULT_NUM_VMDQ_VSI);
  9188. }
  9189. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9190. pf->num_fdsb_msix = 1;
  9191. vec--;
  9192. }
  9193. pf->num_lan_msix = min_t(int,
  9194. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  9195. pf->num_lan_msix);
  9196. pf->num_lan_qps = pf->num_lan_msix;
  9197. break;
  9198. }
  9199. }
  9200. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  9201. (pf->num_fdsb_msix == 0)) {
  9202. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  9203. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9204. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9205. }
  9206. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9207. (pf->num_vmdq_msix == 0)) {
  9208. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  9209. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  9210. }
  9211. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  9212. (pf->num_iwarp_msix == 0)) {
  9213. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  9214. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9215. }
  9216. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  9217. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  9218. pf->num_lan_msix,
  9219. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  9220. pf->num_fdsb_msix,
  9221. pf->num_iwarp_msix);
  9222. return v_actual;
  9223. }
  9224. /**
  9225. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  9226. * @vsi: the VSI being configured
  9227. * @v_idx: index of the vector in the vsi struct
  9228. * @cpu: cpu to be used on affinity_mask
  9229. *
  9230. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  9231. **/
  9232. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  9233. {
  9234. struct i40e_q_vector *q_vector;
  9235. /* allocate q_vector */
  9236. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  9237. if (!q_vector)
  9238. return -ENOMEM;
  9239. q_vector->vsi = vsi;
  9240. q_vector->v_idx = v_idx;
  9241. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  9242. if (vsi->netdev)
  9243. netif_napi_add(vsi->netdev, &q_vector->napi,
  9244. i40e_napi_poll, NAPI_POLL_WEIGHT);
  9245. /* tie q_vector and vsi together */
  9246. vsi->q_vectors[v_idx] = q_vector;
  9247. return 0;
  9248. }
  9249. /**
  9250. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  9251. * @vsi: the VSI being configured
  9252. *
  9253. * We allocate one q_vector per queue interrupt. If allocation fails we
  9254. * return -ENOMEM.
  9255. **/
  9256. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  9257. {
  9258. struct i40e_pf *pf = vsi->back;
  9259. int err, v_idx, num_q_vectors, current_cpu;
  9260. /* if not MSIX, give the one vector only to the LAN VSI */
  9261. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  9262. num_q_vectors = vsi->num_q_vectors;
  9263. else if (vsi == pf->vsi[pf->lan_vsi])
  9264. num_q_vectors = 1;
  9265. else
  9266. return -EINVAL;
  9267. current_cpu = cpumask_first(cpu_online_mask);
  9268. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  9269. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  9270. if (err)
  9271. goto err_out;
  9272. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  9273. if (unlikely(current_cpu >= nr_cpu_ids))
  9274. current_cpu = cpumask_first(cpu_online_mask);
  9275. }
  9276. return 0;
  9277. err_out:
  9278. while (v_idx--)
  9279. i40e_free_q_vector(vsi, v_idx);
  9280. return err;
  9281. }
  9282. /**
  9283. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  9284. * @pf: board private structure to initialize
  9285. **/
  9286. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  9287. {
  9288. int vectors = 0;
  9289. ssize_t size;
  9290. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9291. vectors = i40e_init_msix(pf);
  9292. if (vectors < 0) {
  9293. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  9294. I40E_FLAG_IWARP_ENABLED |
  9295. I40E_FLAG_RSS_ENABLED |
  9296. I40E_FLAG_DCB_CAPABLE |
  9297. I40E_FLAG_DCB_ENABLED |
  9298. I40E_FLAG_SRIOV_ENABLED |
  9299. I40E_FLAG_FD_SB_ENABLED |
  9300. I40E_FLAG_FD_ATR_ENABLED |
  9301. I40E_FLAG_VMDQ_ENABLED);
  9302. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9303. /* rework the queue expectations without MSIX */
  9304. i40e_determine_queue_usage(pf);
  9305. }
  9306. }
  9307. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9308. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  9309. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  9310. vectors = pci_enable_msi(pf->pdev);
  9311. if (vectors < 0) {
  9312. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  9313. vectors);
  9314. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  9315. }
  9316. vectors = 1; /* one MSI or Legacy vector */
  9317. }
  9318. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  9319. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  9320. /* set up vector assignment tracking */
  9321. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  9322. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  9323. if (!pf->irq_pile)
  9324. return -ENOMEM;
  9325. pf->irq_pile->num_entries = vectors;
  9326. pf->irq_pile->search_hint = 0;
  9327. /* track first vector for misc interrupts, ignore return */
  9328. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  9329. return 0;
  9330. }
  9331. /**
  9332. * i40e_restore_interrupt_scheme - Restore the interrupt scheme
  9333. * @pf: private board data structure
  9334. *
  9335. * Restore the interrupt scheme that was cleared when we suspended the
  9336. * device. This should be called during resume to re-allocate the q_vectors
  9337. * and reacquire IRQs.
  9338. */
  9339. static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
  9340. {
  9341. int err, i;
  9342. /* We cleared the MSI and MSI-X flags when disabling the old interrupt
  9343. * scheme. We need to re-enabled them here in order to attempt to
  9344. * re-acquire the MSI or MSI-X vectors
  9345. */
  9346. pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  9347. err = i40e_init_interrupt_scheme(pf);
  9348. if (err)
  9349. return err;
  9350. /* Now that we've re-acquired IRQs, we need to remap the vectors and
  9351. * rings together again.
  9352. */
  9353. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9354. if (pf->vsi[i]) {
  9355. err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
  9356. if (err)
  9357. goto err_unwind;
  9358. i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
  9359. }
  9360. }
  9361. err = i40e_setup_misc_vector(pf);
  9362. if (err)
  9363. goto err_unwind;
  9364. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  9365. i40e_client_update_msix_info(pf);
  9366. return 0;
  9367. err_unwind:
  9368. while (i--) {
  9369. if (pf->vsi[i])
  9370. i40e_vsi_free_q_vectors(pf->vsi[i]);
  9371. }
  9372. return err;
  9373. }
  9374. /**
  9375. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  9376. * @pf: board private structure
  9377. *
  9378. * This sets up the handler for MSIX 0, which is used to manage the
  9379. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  9380. * when in MSI or Legacy interrupt mode.
  9381. **/
  9382. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  9383. {
  9384. struct i40e_hw *hw = &pf->hw;
  9385. int err = 0;
  9386. /* Only request the IRQ once, the first time through. */
  9387. if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
  9388. err = request_irq(pf->msix_entries[0].vector,
  9389. i40e_intr, 0, pf->int_name, pf);
  9390. if (err) {
  9391. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  9392. dev_info(&pf->pdev->dev,
  9393. "request_irq for %s failed: %d\n",
  9394. pf->int_name, err);
  9395. return -EFAULT;
  9396. }
  9397. }
  9398. i40e_enable_misc_int_causes(pf);
  9399. /* associate no queues to the misc vector */
  9400. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  9401. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
  9402. i40e_flush(hw);
  9403. i40e_irq_dynamic_enable_icr0(pf);
  9404. return err;
  9405. }
  9406. /**
  9407. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  9408. * @vsi: Pointer to vsi structure
  9409. * @seed: Buffter to store the hash keys
  9410. * @lut: Buffer to store the lookup table entries
  9411. * @lut_size: Size of buffer to store the lookup table entries
  9412. *
  9413. * Return 0 on success, negative on failure
  9414. */
  9415. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  9416. u8 *lut, u16 lut_size)
  9417. {
  9418. struct i40e_pf *pf = vsi->back;
  9419. struct i40e_hw *hw = &pf->hw;
  9420. int ret = 0;
  9421. if (seed) {
  9422. ret = i40e_aq_get_rss_key(hw, vsi->id,
  9423. (struct i40e_aqc_get_set_rss_key_data *)seed);
  9424. if (ret) {
  9425. dev_info(&pf->pdev->dev,
  9426. "Cannot get RSS key, err %s aq_err %s\n",
  9427. i40e_stat_str(&pf->hw, ret),
  9428. i40e_aq_str(&pf->hw,
  9429. pf->hw.aq.asq_last_status));
  9430. return ret;
  9431. }
  9432. }
  9433. if (lut) {
  9434. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  9435. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  9436. if (ret) {
  9437. dev_info(&pf->pdev->dev,
  9438. "Cannot get RSS lut, err %s aq_err %s\n",
  9439. i40e_stat_str(&pf->hw, ret),
  9440. i40e_aq_str(&pf->hw,
  9441. pf->hw.aq.asq_last_status));
  9442. return ret;
  9443. }
  9444. }
  9445. return ret;
  9446. }
  9447. /**
  9448. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  9449. * @vsi: Pointer to vsi structure
  9450. * @seed: RSS hash seed
  9451. * @lut: Lookup table
  9452. * @lut_size: Lookup table size
  9453. *
  9454. * Returns 0 on success, negative on failure
  9455. **/
  9456. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  9457. const u8 *lut, u16 lut_size)
  9458. {
  9459. struct i40e_pf *pf = vsi->back;
  9460. struct i40e_hw *hw = &pf->hw;
  9461. u16 vf_id = vsi->vf_id;
  9462. u8 i;
  9463. /* Fill out hash function seed */
  9464. if (seed) {
  9465. u32 *seed_dw = (u32 *)seed;
  9466. if (vsi->type == I40E_VSI_MAIN) {
  9467. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9468. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  9469. } else if (vsi->type == I40E_VSI_SRIOV) {
  9470. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  9471. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  9472. } else {
  9473. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  9474. }
  9475. }
  9476. if (lut) {
  9477. u32 *lut_dw = (u32 *)lut;
  9478. if (vsi->type == I40E_VSI_MAIN) {
  9479. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9480. return -EINVAL;
  9481. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9482. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  9483. } else if (vsi->type == I40E_VSI_SRIOV) {
  9484. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  9485. return -EINVAL;
  9486. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9487. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  9488. } else {
  9489. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  9490. }
  9491. }
  9492. i40e_flush(hw);
  9493. return 0;
  9494. }
  9495. /**
  9496. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  9497. * @vsi: Pointer to VSI structure
  9498. * @seed: Buffer to store the keys
  9499. * @lut: Buffer to store the lookup table entries
  9500. * @lut_size: Size of buffer to store the lookup table entries
  9501. *
  9502. * Returns 0 on success, negative on failure
  9503. */
  9504. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  9505. u8 *lut, u16 lut_size)
  9506. {
  9507. struct i40e_pf *pf = vsi->back;
  9508. struct i40e_hw *hw = &pf->hw;
  9509. u16 i;
  9510. if (seed) {
  9511. u32 *seed_dw = (u32 *)seed;
  9512. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9513. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  9514. }
  9515. if (lut) {
  9516. u32 *lut_dw = (u32 *)lut;
  9517. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9518. return -EINVAL;
  9519. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9520. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  9521. }
  9522. return 0;
  9523. }
  9524. /**
  9525. * i40e_config_rss - Configure RSS keys and lut
  9526. * @vsi: Pointer to VSI structure
  9527. * @seed: RSS hash seed
  9528. * @lut: Lookup table
  9529. * @lut_size: Lookup table size
  9530. *
  9531. * Returns 0 on success, negative on failure
  9532. */
  9533. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9534. {
  9535. struct i40e_pf *pf = vsi->back;
  9536. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9537. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  9538. else
  9539. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  9540. }
  9541. /**
  9542. * i40e_get_rss - Get RSS keys and lut
  9543. * @vsi: Pointer to VSI structure
  9544. * @seed: Buffer to store the keys
  9545. * @lut: Buffer to store the lookup table entries
  9546. * @lut_size: Size of buffer to store the lookup table entries
  9547. *
  9548. * Returns 0 on success, negative on failure
  9549. */
  9550. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9551. {
  9552. struct i40e_pf *pf = vsi->back;
  9553. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9554. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  9555. else
  9556. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  9557. }
  9558. /**
  9559. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  9560. * @pf: Pointer to board private structure
  9561. * @lut: Lookup table
  9562. * @rss_table_size: Lookup table size
  9563. * @rss_size: Range of queue number for hashing
  9564. */
  9565. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  9566. u16 rss_table_size, u16 rss_size)
  9567. {
  9568. u16 i;
  9569. for (i = 0; i < rss_table_size; i++)
  9570. lut[i] = i % rss_size;
  9571. }
  9572. /**
  9573. * i40e_pf_config_rss - Prepare for RSS if used
  9574. * @pf: board private structure
  9575. **/
  9576. static int i40e_pf_config_rss(struct i40e_pf *pf)
  9577. {
  9578. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9579. u8 seed[I40E_HKEY_ARRAY_SIZE];
  9580. u8 *lut;
  9581. struct i40e_hw *hw = &pf->hw;
  9582. u32 reg_val;
  9583. u64 hena;
  9584. int ret;
  9585. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  9586. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  9587. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  9588. hena |= i40e_pf_get_default_rss_hena(pf);
  9589. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  9590. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  9591. /* Determine the RSS table size based on the hardware capabilities */
  9592. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  9593. reg_val = (pf->rss_table_size == 512) ?
  9594. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  9595. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  9596. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  9597. /* Determine the RSS size of the VSI */
  9598. if (!vsi->rss_size) {
  9599. u16 qcount;
  9600. /* If the firmware does something weird during VSI init, we
  9601. * could end up with zero TCs. Check for that to avoid
  9602. * divide-by-zero. It probably won't pass traffic, but it also
  9603. * won't panic.
  9604. */
  9605. qcount = vsi->num_queue_pairs /
  9606. (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
  9607. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9608. }
  9609. if (!vsi->rss_size)
  9610. return -EINVAL;
  9611. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  9612. if (!lut)
  9613. return -ENOMEM;
  9614. /* Use user configured lut if there is one, otherwise use default */
  9615. if (vsi->rss_lut_user)
  9616. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  9617. else
  9618. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  9619. /* Use user configured hash key if there is one, otherwise
  9620. * use default.
  9621. */
  9622. if (vsi->rss_hkey_user)
  9623. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  9624. else
  9625. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  9626. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  9627. kfree(lut);
  9628. return ret;
  9629. }
  9630. /**
  9631. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  9632. * @pf: board private structure
  9633. * @queue_count: the requested queue count for rss.
  9634. *
  9635. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  9636. * count which may be different from the requested queue count.
  9637. * Note: expects to be called while under rtnl_lock()
  9638. **/
  9639. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  9640. {
  9641. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9642. int new_rss_size;
  9643. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  9644. return 0;
  9645. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  9646. if (queue_count != vsi->num_queue_pairs) {
  9647. u16 qcount;
  9648. vsi->req_queue_pairs = queue_count;
  9649. i40e_prep_for_reset(pf, true);
  9650. pf->alloc_rss_size = new_rss_size;
  9651. i40e_reset_and_rebuild(pf, true, true);
  9652. /* Discard the user configured hash keys and lut, if less
  9653. * queues are enabled.
  9654. */
  9655. if (queue_count < vsi->rss_size) {
  9656. i40e_clear_rss_config_user(vsi);
  9657. dev_dbg(&pf->pdev->dev,
  9658. "discard user configured hash keys and lut\n");
  9659. }
  9660. /* Reset vsi->rss_size, as number of enabled queues changed */
  9661. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  9662. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9663. i40e_pf_config_rss(pf);
  9664. }
  9665. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  9666. vsi->req_queue_pairs, pf->rss_size_max);
  9667. return pf->alloc_rss_size;
  9668. }
  9669. /**
  9670. * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
  9671. * @pf: board private structure
  9672. **/
  9673. i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
  9674. {
  9675. i40e_status status;
  9676. bool min_valid, max_valid;
  9677. u32 max_bw, min_bw;
  9678. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  9679. &min_valid, &max_valid);
  9680. if (!status) {
  9681. if (min_valid)
  9682. pf->min_bw = min_bw;
  9683. if (max_valid)
  9684. pf->max_bw = max_bw;
  9685. }
  9686. return status;
  9687. }
  9688. /**
  9689. * i40e_set_partition_bw_setting - Set BW settings for this PF partition
  9690. * @pf: board private structure
  9691. **/
  9692. i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
  9693. {
  9694. struct i40e_aqc_configure_partition_bw_data bw_data;
  9695. i40e_status status;
  9696. /* Set the valid bit for this PF */
  9697. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  9698. bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
  9699. bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
  9700. /* Set the new bandwidths */
  9701. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  9702. return status;
  9703. }
  9704. /**
  9705. * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
  9706. * @pf: board private structure
  9707. **/
  9708. i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
  9709. {
  9710. /* Commit temporary BW setting to permanent NVM image */
  9711. enum i40e_admin_queue_err last_aq_status;
  9712. i40e_status ret;
  9713. u16 nvm_word;
  9714. if (pf->hw.partition_id != 1) {
  9715. dev_info(&pf->pdev->dev,
  9716. "Commit BW only works on partition 1! This is partition %d",
  9717. pf->hw.partition_id);
  9718. ret = I40E_NOT_SUPPORTED;
  9719. goto bw_commit_out;
  9720. }
  9721. /* Acquire NVM for read access */
  9722. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  9723. last_aq_status = pf->hw.aq.asq_last_status;
  9724. if (ret) {
  9725. dev_info(&pf->pdev->dev,
  9726. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  9727. i40e_stat_str(&pf->hw, ret),
  9728. i40e_aq_str(&pf->hw, last_aq_status));
  9729. goto bw_commit_out;
  9730. }
  9731. /* Read word 0x10 of NVM - SW compatibility word 1 */
  9732. ret = i40e_aq_read_nvm(&pf->hw,
  9733. I40E_SR_NVM_CONTROL_WORD,
  9734. 0x10, sizeof(nvm_word), &nvm_word,
  9735. false, NULL);
  9736. /* Save off last admin queue command status before releasing
  9737. * the NVM
  9738. */
  9739. last_aq_status = pf->hw.aq.asq_last_status;
  9740. i40e_release_nvm(&pf->hw);
  9741. if (ret) {
  9742. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  9743. i40e_stat_str(&pf->hw, ret),
  9744. i40e_aq_str(&pf->hw, last_aq_status));
  9745. goto bw_commit_out;
  9746. }
  9747. /* Wait a bit for NVM release to complete */
  9748. msleep(50);
  9749. /* Acquire NVM for write access */
  9750. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  9751. last_aq_status = pf->hw.aq.asq_last_status;
  9752. if (ret) {
  9753. dev_info(&pf->pdev->dev,
  9754. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  9755. i40e_stat_str(&pf->hw, ret),
  9756. i40e_aq_str(&pf->hw, last_aq_status));
  9757. goto bw_commit_out;
  9758. }
  9759. /* Write it back out unchanged to initiate update NVM,
  9760. * which will force a write of the shadow (alt) RAM to
  9761. * the NVM - thus storing the bandwidth values permanently.
  9762. */
  9763. ret = i40e_aq_update_nvm(&pf->hw,
  9764. I40E_SR_NVM_CONTROL_WORD,
  9765. 0x10, sizeof(nvm_word),
  9766. &nvm_word, true, 0, NULL);
  9767. /* Save off last admin queue command status before releasing
  9768. * the NVM
  9769. */
  9770. last_aq_status = pf->hw.aq.asq_last_status;
  9771. i40e_release_nvm(&pf->hw);
  9772. if (ret)
  9773. dev_info(&pf->pdev->dev,
  9774. "BW settings NOT SAVED, err %s aq_err %s\n",
  9775. i40e_stat_str(&pf->hw, ret),
  9776. i40e_aq_str(&pf->hw, last_aq_status));
  9777. bw_commit_out:
  9778. return ret;
  9779. }
  9780. /**
  9781. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  9782. * @pf: board private structure to initialize
  9783. *
  9784. * i40e_sw_init initializes the Adapter private data structure.
  9785. * Fields are initialized based on PCI device information and
  9786. * OS network device settings (MTU size).
  9787. **/
  9788. static int i40e_sw_init(struct i40e_pf *pf)
  9789. {
  9790. int err = 0;
  9791. int size;
  9792. /* Set default capability flags */
  9793. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  9794. I40E_FLAG_MSI_ENABLED |
  9795. I40E_FLAG_MSIX_ENABLED;
  9796. /* Set default ITR */
  9797. pf->rx_itr_default = I40E_ITR_RX_DEF;
  9798. pf->tx_itr_default = I40E_ITR_TX_DEF;
  9799. /* Depending on PF configurations, it is possible that the RSS
  9800. * maximum might end up larger than the available queues
  9801. */
  9802. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  9803. pf->alloc_rss_size = 1;
  9804. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  9805. pf->rss_size_max = min_t(int, pf->rss_size_max,
  9806. pf->hw.func_caps.num_tx_qp);
  9807. if (pf->hw.func_caps.rss) {
  9808. pf->flags |= I40E_FLAG_RSS_ENABLED;
  9809. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  9810. num_online_cpus());
  9811. }
  9812. /* MFP mode enabled */
  9813. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  9814. pf->flags |= I40E_FLAG_MFP_ENABLED;
  9815. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  9816. if (i40e_get_partition_bw_setting(pf)) {
  9817. dev_warn(&pf->pdev->dev,
  9818. "Could not get partition bw settings\n");
  9819. } else {
  9820. dev_info(&pf->pdev->dev,
  9821. "Partition BW Min = %8.8x, Max = %8.8x\n",
  9822. pf->min_bw, pf->max_bw);
  9823. /* nudge the Tx scheduler */
  9824. i40e_set_partition_bw_setting(pf);
  9825. }
  9826. }
  9827. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  9828. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  9829. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  9830. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  9831. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  9832. pf->hw.num_partitions > 1)
  9833. dev_info(&pf->pdev->dev,
  9834. "Flow Director Sideband mode Disabled in MFP mode\n");
  9835. else
  9836. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9837. pf->fdir_pf_filter_count =
  9838. pf->hw.func_caps.fd_filters_guaranteed;
  9839. pf->hw.fdir_shared_filter_count =
  9840. pf->hw.func_caps.fd_filters_best_effort;
  9841. }
  9842. if (pf->hw.mac.type == I40E_MAC_X722) {
  9843. pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
  9844. I40E_HW_128_QP_RSS_CAPABLE |
  9845. I40E_HW_ATR_EVICT_CAPABLE |
  9846. I40E_HW_WB_ON_ITR_CAPABLE |
  9847. I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
  9848. I40E_HW_NO_PCI_LINK_CHECK |
  9849. I40E_HW_USE_SET_LLDP_MIB |
  9850. I40E_HW_GENEVE_OFFLOAD_CAPABLE |
  9851. I40E_HW_PTP_L4_CAPABLE |
  9852. I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
  9853. I40E_HW_OUTER_UDP_CSUM_CAPABLE);
  9854. #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
  9855. if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
  9856. I40E_FDEVICT_PCTYPE_DEFAULT) {
  9857. dev_warn(&pf->pdev->dev,
  9858. "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
  9859. pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
  9860. }
  9861. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  9862. ((pf->hw.aq.api_maj_ver == 1) &&
  9863. (pf->hw.aq.api_min_ver > 4))) {
  9864. /* Supported in FW API version higher than 1.4 */
  9865. pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
  9866. }
  9867. /* Enable HW ATR eviction if possible */
  9868. if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
  9869. pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
  9870. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9871. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  9872. (pf->hw.aq.fw_maj_ver < 4))) {
  9873. pf->hw_features |= I40E_HW_RESTART_AUTONEG;
  9874. /* No DCB support for FW < v4.33 */
  9875. pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
  9876. }
  9877. /* Disable FW LLDP if FW < v4.3 */
  9878. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9879. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  9880. (pf->hw.aq.fw_maj_ver < 4)))
  9881. pf->hw_features |= I40E_HW_STOP_FW_LLDP;
  9882. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  9883. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9884. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  9885. (pf->hw.aq.fw_maj_ver >= 5)))
  9886. pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
  9887. /* Enable PTP L4 if FW > v6.0 */
  9888. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9889. pf->hw.aq.fw_maj_ver >= 6)
  9890. pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
  9891. if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
  9892. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  9893. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  9894. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  9895. }
  9896. if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
  9897. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  9898. /* IWARP needs one extra vector for CQP just like MISC.*/
  9899. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  9900. }
  9901. /* Stopping the FW LLDP engine is only supported on the
  9902. * XL710 with a FW ver >= 1.7. Also, stopping FW LLDP
  9903. * engine is not supported if NPAR is functioning on this
  9904. * part
  9905. */
  9906. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9907. !pf->hw.func_caps.npar_enable &&
  9908. (pf->hw.aq.api_maj_ver > 1 ||
  9909. (pf->hw.aq.api_maj_ver == 1 && pf->hw.aq.api_min_ver > 6)))
  9910. pf->hw_features |= I40E_HW_STOPPABLE_FW_LLDP;
  9911. #ifdef CONFIG_PCI_IOV
  9912. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  9913. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  9914. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  9915. pf->num_req_vfs = min_t(int,
  9916. pf->hw.func_caps.num_vfs,
  9917. I40E_MAX_VF_COUNT);
  9918. }
  9919. #endif /* CONFIG_PCI_IOV */
  9920. pf->eeprom_version = 0xDEAD;
  9921. pf->lan_veb = I40E_NO_VEB;
  9922. pf->lan_vsi = I40E_NO_VSI;
  9923. /* By default FW has this off for performance reasons */
  9924. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  9925. /* set up queue assignment tracking */
  9926. size = sizeof(struct i40e_lump_tracking)
  9927. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  9928. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  9929. if (!pf->qp_pile) {
  9930. err = -ENOMEM;
  9931. goto sw_init_done;
  9932. }
  9933. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  9934. pf->qp_pile->search_hint = 0;
  9935. pf->tx_timeout_recovery_level = 1;
  9936. mutex_init(&pf->switch_mutex);
  9937. sw_init_done:
  9938. return err;
  9939. }
  9940. /**
  9941. * i40e_set_ntuple - set the ntuple feature flag and take action
  9942. * @pf: board private structure to initialize
  9943. * @features: the feature set that the stack is suggesting
  9944. *
  9945. * returns a bool to indicate if reset needs to happen
  9946. **/
  9947. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  9948. {
  9949. bool need_reset = false;
  9950. /* Check if Flow Director n-tuple support was enabled or disabled. If
  9951. * the state changed, we need to reset.
  9952. */
  9953. if (features & NETIF_F_NTUPLE) {
  9954. /* Enable filters and mark for reset */
  9955. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  9956. need_reset = true;
  9957. /* enable FD_SB only if there is MSI-X vector and no cloud
  9958. * filters exist
  9959. */
  9960. if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
  9961. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9962. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  9963. }
  9964. } else {
  9965. /* turn off filters, mark for reset and clear SW filter list */
  9966. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9967. need_reset = true;
  9968. i40e_fdir_filter_exit(pf);
  9969. }
  9970. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9971. clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
  9972. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9973. /* reset fd counters */
  9974. pf->fd_add_err = 0;
  9975. pf->fd_atr_cnt = 0;
  9976. /* if ATR was auto disabled it can be re-enabled. */
  9977. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  9978. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  9979. (I40E_DEBUG_FD & pf->hw.debug_mask))
  9980. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  9981. }
  9982. return need_reset;
  9983. }
  9984. /**
  9985. * i40e_clear_rss_lut - clear the rx hash lookup table
  9986. * @vsi: the VSI being configured
  9987. **/
  9988. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  9989. {
  9990. struct i40e_pf *pf = vsi->back;
  9991. struct i40e_hw *hw = &pf->hw;
  9992. u16 vf_id = vsi->vf_id;
  9993. u8 i;
  9994. if (vsi->type == I40E_VSI_MAIN) {
  9995. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9996. wr32(hw, I40E_PFQF_HLUT(i), 0);
  9997. } else if (vsi->type == I40E_VSI_SRIOV) {
  9998. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9999. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  10000. } else {
  10001. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  10002. }
  10003. }
  10004. /**
  10005. * i40e_set_features - set the netdev feature flags
  10006. * @netdev: ptr to the netdev being adjusted
  10007. * @features: the feature set that the stack is suggesting
  10008. * Note: expects to be called while under rtnl_lock()
  10009. **/
  10010. static int i40e_set_features(struct net_device *netdev,
  10011. netdev_features_t features)
  10012. {
  10013. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10014. struct i40e_vsi *vsi = np->vsi;
  10015. struct i40e_pf *pf = vsi->back;
  10016. bool need_reset;
  10017. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  10018. i40e_pf_config_rss(pf);
  10019. else if (!(features & NETIF_F_RXHASH) &&
  10020. netdev->features & NETIF_F_RXHASH)
  10021. i40e_clear_rss_lut(vsi);
  10022. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  10023. i40e_vlan_stripping_enable(vsi);
  10024. else
  10025. i40e_vlan_stripping_disable(vsi);
  10026. if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
  10027. dev_err(&pf->pdev->dev,
  10028. "Offloaded tc filters active, can't turn hw_tc_offload off");
  10029. return -EINVAL;
  10030. }
  10031. need_reset = i40e_set_ntuple(pf, features);
  10032. if (need_reset)
  10033. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  10034. return 0;
  10035. }
  10036. /**
  10037. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  10038. * @pf: board private structure
  10039. * @port: The UDP port to look up
  10040. *
  10041. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  10042. **/
  10043. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  10044. {
  10045. u8 i;
  10046. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  10047. /* Do not report ports with pending deletions as
  10048. * being available.
  10049. */
  10050. if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
  10051. continue;
  10052. if (pf->udp_ports[i].port == port)
  10053. return i;
  10054. }
  10055. return i;
  10056. }
  10057. /**
  10058. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  10059. * @netdev: This physical port's netdev
  10060. * @ti: Tunnel endpoint information
  10061. **/
  10062. static void i40e_udp_tunnel_add(struct net_device *netdev,
  10063. struct udp_tunnel_info *ti)
  10064. {
  10065. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10066. struct i40e_vsi *vsi = np->vsi;
  10067. struct i40e_pf *pf = vsi->back;
  10068. u16 port = ntohs(ti->port);
  10069. u8 next_idx;
  10070. u8 idx;
  10071. idx = i40e_get_udp_port_idx(pf, port);
  10072. /* Check if port already exists */
  10073. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10074. netdev_info(netdev, "port %d already offloaded\n", port);
  10075. return;
  10076. }
  10077. /* Now check if there is space to add the new port */
  10078. next_idx = i40e_get_udp_port_idx(pf, 0);
  10079. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10080. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  10081. port);
  10082. return;
  10083. }
  10084. switch (ti->type) {
  10085. case UDP_TUNNEL_TYPE_VXLAN:
  10086. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  10087. break;
  10088. case UDP_TUNNEL_TYPE_GENEVE:
  10089. if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
  10090. return;
  10091. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  10092. break;
  10093. default:
  10094. return;
  10095. }
  10096. /* New port: add it and mark its index in the bitmap */
  10097. pf->udp_ports[next_idx].port = port;
  10098. pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
  10099. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  10100. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10101. }
  10102. /**
  10103. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  10104. * @netdev: This physical port's netdev
  10105. * @ti: Tunnel endpoint information
  10106. **/
  10107. static void i40e_udp_tunnel_del(struct net_device *netdev,
  10108. struct udp_tunnel_info *ti)
  10109. {
  10110. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10111. struct i40e_vsi *vsi = np->vsi;
  10112. struct i40e_pf *pf = vsi->back;
  10113. u16 port = ntohs(ti->port);
  10114. u8 idx;
  10115. idx = i40e_get_udp_port_idx(pf, port);
  10116. /* Check if port already exists */
  10117. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  10118. goto not_found;
  10119. switch (ti->type) {
  10120. case UDP_TUNNEL_TYPE_VXLAN:
  10121. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  10122. goto not_found;
  10123. break;
  10124. case UDP_TUNNEL_TYPE_GENEVE:
  10125. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  10126. goto not_found;
  10127. break;
  10128. default:
  10129. goto not_found;
  10130. }
  10131. /* if port exists, set it to 0 (mark for deletion)
  10132. * and make it pending
  10133. */
  10134. pf->udp_ports[idx].port = 0;
  10135. /* Toggle pending bit instead of setting it. This way if we are
  10136. * deleting a port that has yet to be added we just clear the pending
  10137. * bit and don't have to worry about it.
  10138. */
  10139. pf->pending_udp_bitmap ^= BIT_ULL(idx);
  10140. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10141. return;
  10142. not_found:
  10143. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  10144. port);
  10145. }
  10146. static int i40e_get_phys_port_id(struct net_device *netdev,
  10147. struct netdev_phys_item_id *ppid)
  10148. {
  10149. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10150. struct i40e_pf *pf = np->vsi->back;
  10151. struct i40e_hw *hw = &pf->hw;
  10152. if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
  10153. return -EOPNOTSUPP;
  10154. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  10155. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  10156. return 0;
  10157. }
  10158. /**
  10159. * i40e_ndo_fdb_add - add an entry to the hardware database
  10160. * @ndm: the input from the stack
  10161. * @tb: pointer to array of nladdr (unused)
  10162. * @dev: the net device pointer
  10163. * @addr: the MAC address entry being added
  10164. * @vid: VLAN ID
  10165. * @flags: instructions from stack about fdb operation
  10166. */
  10167. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  10168. struct net_device *dev,
  10169. const unsigned char *addr, u16 vid,
  10170. u16 flags)
  10171. {
  10172. struct i40e_netdev_priv *np = netdev_priv(dev);
  10173. struct i40e_pf *pf = np->vsi->back;
  10174. int err = 0;
  10175. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  10176. return -EOPNOTSUPP;
  10177. if (vid) {
  10178. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  10179. return -EINVAL;
  10180. }
  10181. /* Hardware does not support aging addresses so if a
  10182. * ndm_state is given only allow permanent addresses
  10183. */
  10184. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  10185. netdev_info(dev, "FDB only supports static addresses\n");
  10186. return -EINVAL;
  10187. }
  10188. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  10189. err = dev_uc_add_excl(dev, addr);
  10190. else if (is_multicast_ether_addr(addr))
  10191. err = dev_mc_add_excl(dev, addr);
  10192. else
  10193. err = -EINVAL;
  10194. /* Only return duplicate errors if NLM_F_EXCL is set */
  10195. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  10196. err = 0;
  10197. return err;
  10198. }
  10199. /**
  10200. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  10201. * @dev: the netdev being configured
  10202. * @nlh: RTNL message
  10203. * @flags: bridge flags
  10204. *
  10205. * Inserts a new hardware bridge if not already created and
  10206. * enables the bridging mode requested (VEB or VEPA). If the
  10207. * hardware bridge has already been inserted and the request
  10208. * is to change the mode then that requires a PF reset to
  10209. * allow rebuild of the components with required hardware
  10210. * bridge mode enabled.
  10211. *
  10212. * Note: expects to be called while under rtnl_lock()
  10213. **/
  10214. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  10215. struct nlmsghdr *nlh,
  10216. u16 flags)
  10217. {
  10218. struct i40e_netdev_priv *np = netdev_priv(dev);
  10219. struct i40e_vsi *vsi = np->vsi;
  10220. struct i40e_pf *pf = vsi->back;
  10221. struct i40e_veb *veb = NULL;
  10222. struct nlattr *attr, *br_spec;
  10223. int i, rem;
  10224. /* Only for PF VSI for now */
  10225. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10226. return -EOPNOTSUPP;
  10227. /* Find the HW bridge for PF VSI */
  10228. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10229. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10230. veb = pf->veb[i];
  10231. }
  10232. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  10233. nla_for_each_nested(attr, br_spec, rem) {
  10234. __u16 mode;
  10235. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  10236. continue;
  10237. mode = nla_get_u16(attr);
  10238. if ((mode != BRIDGE_MODE_VEPA) &&
  10239. (mode != BRIDGE_MODE_VEB))
  10240. return -EINVAL;
  10241. /* Insert a new HW bridge */
  10242. if (!veb) {
  10243. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  10244. vsi->tc_config.enabled_tc);
  10245. if (veb) {
  10246. veb->bridge_mode = mode;
  10247. i40e_config_bridge_mode(veb);
  10248. } else {
  10249. /* No Bridge HW offload available */
  10250. return -ENOENT;
  10251. }
  10252. break;
  10253. } else if (mode != veb->bridge_mode) {
  10254. /* Existing HW bridge but different mode needs reset */
  10255. veb->bridge_mode = mode;
  10256. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  10257. if (mode == BRIDGE_MODE_VEB)
  10258. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  10259. else
  10260. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  10261. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  10262. break;
  10263. }
  10264. }
  10265. return 0;
  10266. }
  10267. /**
  10268. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  10269. * @skb: skb buff
  10270. * @pid: process id
  10271. * @seq: RTNL message seq #
  10272. * @dev: the netdev being configured
  10273. * @filter_mask: unused
  10274. * @nlflags: netlink flags passed in
  10275. *
  10276. * Return the mode in which the hardware bridge is operating in
  10277. * i.e VEB or VEPA.
  10278. **/
  10279. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  10280. struct net_device *dev,
  10281. u32 __always_unused filter_mask,
  10282. int nlflags)
  10283. {
  10284. struct i40e_netdev_priv *np = netdev_priv(dev);
  10285. struct i40e_vsi *vsi = np->vsi;
  10286. struct i40e_pf *pf = vsi->back;
  10287. struct i40e_veb *veb = NULL;
  10288. int i;
  10289. /* Only for PF VSI for now */
  10290. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10291. return -EOPNOTSUPP;
  10292. /* Find the HW bridge for the PF VSI */
  10293. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10294. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10295. veb = pf->veb[i];
  10296. }
  10297. if (!veb)
  10298. return 0;
  10299. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  10300. 0, 0, nlflags, filter_mask, NULL);
  10301. }
  10302. /**
  10303. * i40e_features_check - Validate encapsulated packet conforms to limits
  10304. * @skb: skb buff
  10305. * @dev: This physical port's netdev
  10306. * @features: Offload features that the stack believes apply
  10307. **/
  10308. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  10309. struct net_device *dev,
  10310. netdev_features_t features)
  10311. {
  10312. size_t len;
  10313. /* No point in doing any of this if neither checksum nor GSO are
  10314. * being requested for this frame. We can rule out both by just
  10315. * checking for CHECKSUM_PARTIAL
  10316. */
  10317. if (skb->ip_summed != CHECKSUM_PARTIAL)
  10318. return features;
  10319. /* We cannot support GSO if the MSS is going to be less than
  10320. * 64 bytes. If it is then we need to drop support for GSO.
  10321. */
  10322. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  10323. features &= ~NETIF_F_GSO_MASK;
  10324. /* MACLEN can support at most 63 words */
  10325. len = skb_network_header(skb) - skb->data;
  10326. if (len & ~(63 * 2))
  10327. goto out_err;
  10328. /* IPLEN and EIPLEN can support at most 127 dwords */
  10329. len = skb_transport_header(skb) - skb_network_header(skb);
  10330. if (len & ~(127 * 4))
  10331. goto out_err;
  10332. if (skb->encapsulation) {
  10333. /* L4TUNLEN can support 127 words */
  10334. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  10335. if (len & ~(127 * 2))
  10336. goto out_err;
  10337. /* IPLEN can support at most 127 dwords */
  10338. len = skb_inner_transport_header(skb) -
  10339. skb_inner_network_header(skb);
  10340. if (len & ~(127 * 4))
  10341. goto out_err;
  10342. }
  10343. /* No need to validate L4LEN as TCP is the only protocol with a
  10344. * a flexible value and we support all possible values supported
  10345. * by TCP, which is at most 15 dwords
  10346. */
  10347. return features;
  10348. out_err:
  10349. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  10350. }
  10351. /**
  10352. * i40e_xdp_setup - add/remove an XDP program
  10353. * @vsi: VSI to changed
  10354. * @prog: XDP program
  10355. **/
  10356. static int i40e_xdp_setup(struct i40e_vsi *vsi,
  10357. struct bpf_prog *prog)
  10358. {
  10359. int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  10360. struct i40e_pf *pf = vsi->back;
  10361. struct bpf_prog *old_prog;
  10362. bool need_reset;
  10363. int i;
  10364. /* Don't allow frames that span over multiple buffers */
  10365. if (frame_size > vsi->rx_buf_len)
  10366. return -EINVAL;
  10367. if (!i40e_enabled_xdp_vsi(vsi) && !prog)
  10368. return 0;
  10369. /* When turning XDP on->off/off->on we reset and rebuild the rings. */
  10370. need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
  10371. if (need_reset)
  10372. i40e_prep_for_reset(pf, true);
  10373. old_prog = xchg(&vsi->xdp_prog, prog);
  10374. if (need_reset)
  10375. i40e_reset_and_rebuild(pf, true, true);
  10376. for (i = 0; i < vsi->num_queue_pairs; i++)
  10377. WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
  10378. if (old_prog)
  10379. bpf_prog_put(old_prog);
  10380. return 0;
  10381. }
  10382. /**
  10383. * i40e_xdp - implements ndo_bpf for i40e
  10384. * @dev: netdevice
  10385. * @xdp: XDP command
  10386. **/
  10387. static int i40e_xdp(struct net_device *dev,
  10388. struct netdev_bpf *xdp)
  10389. {
  10390. struct i40e_netdev_priv *np = netdev_priv(dev);
  10391. struct i40e_vsi *vsi = np->vsi;
  10392. if (vsi->type != I40E_VSI_MAIN)
  10393. return -EINVAL;
  10394. switch (xdp->command) {
  10395. case XDP_SETUP_PROG:
  10396. return i40e_xdp_setup(vsi, xdp->prog);
  10397. case XDP_QUERY_PROG:
  10398. xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
  10399. return 0;
  10400. default:
  10401. return -EINVAL;
  10402. }
  10403. }
  10404. static const struct net_device_ops i40e_netdev_ops = {
  10405. .ndo_open = i40e_open,
  10406. .ndo_stop = i40e_close,
  10407. .ndo_start_xmit = i40e_lan_xmit_frame,
  10408. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  10409. .ndo_set_rx_mode = i40e_set_rx_mode,
  10410. .ndo_validate_addr = eth_validate_addr,
  10411. .ndo_set_mac_address = i40e_set_mac,
  10412. .ndo_change_mtu = i40e_change_mtu,
  10413. .ndo_do_ioctl = i40e_ioctl,
  10414. .ndo_tx_timeout = i40e_tx_timeout,
  10415. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  10416. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  10417. #ifdef CONFIG_NET_POLL_CONTROLLER
  10418. .ndo_poll_controller = i40e_netpoll,
  10419. #endif
  10420. .ndo_setup_tc = __i40e_setup_tc,
  10421. .ndo_set_features = i40e_set_features,
  10422. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  10423. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  10424. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  10425. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  10426. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  10427. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  10428. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  10429. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  10430. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  10431. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  10432. .ndo_fdb_add = i40e_ndo_fdb_add,
  10433. .ndo_features_check = i40e_features_check,
  10434. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  10435. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  10436. .ndo_bpf = i40e_xdp,
  10437. .ndo_xdp_xmit = i40e_xdp_xmit,
  10438. };
  10439. /**
  10440. * i40e_config_netdev - Setup the netdev flags
  10441. * @vsi: the VSI being configured
  10442. *
  10443. * Returns 0 on success, negative value on failure
  10444. **/
  10445. static int i40e_config_netdev(struct i40e_vsi *vsi)
  10446. {
  10447. struct i40e_pf *pf = vsi->back;
  10448. struct i40e_hw *hw = &pf->hw;
  10449. struct i40e_netdev_priv *np;
  10450. struct net_device *netdev;
  10451. u8 broadcast[ETH_ALEN];
  10452. u8 mac_addr[ETH_ALEN];
  10453. int etherdev_size;
  10454. netdev_features_t hw_enc_features;
  10455. netdev_features_t hw_features;
  10456. etherdev_size = sizeof(struct i40e_netdev_priv);
  10457. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  10458. if (!netdev)
  10459. return -ENOMEM;
  10460. vsi->netdev = netdev;
  10461. np = netdev_priv(netdev);
  10462. np->vsi = vsi;
  10463. hw_enc_features = NETIF_F_SG |
  10464. NETIF_F_IP_CSUM |
  10465. NETIF_F_IPV6_CSUM |
  10466. NETIF_F_HIGHDMA |
  10467. NETIF_F_SOFT_FEATURES |
  10468. NETIF_F_TSO |
  10469. NETIF_F_TSO_ECN |
  10470. NETIF_F_TSO6 |
  10471. NETIF_F_GSO_GRE |
  10472. NETIF_F_GSO_GRE_CSUM |
  10473. NETIF_F_GSO_PARTIAL |
  10474. NETIF_F_GSO_IPXIP4 |
  10475. NETIF_F_GSO_IPXIP6 |
  10476. NETIF_F_GSO_UDP_TUNNEL |
  10477. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  10478. NETIF_F_SCTP_CRC |
  10479. NETIF_F_RXHASH |
  10480. NETIF_F_RXCSUM |
  10481. 0;
  10482. if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
  10483. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  10484. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  10485. netdev->hw_enc_features |= hw_enc_features;
  10486. /* record features VLANs can make use of */
  10487. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  10488. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  10489. netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
  10490. hw_features = hw_enc_features |
  10491. NETIF_F_HW_VLAN_CTAG_TX |
  10492. NETIF_F_HW_VLAN_CTAG_RX;
  10493. netdev->hw_features |= hw_features;
  10494. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  10495. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  10496. if (vsi->type == I40E_VSI_MAIN) {
  10497. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  10498. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  10499. /* The following steps are necessary for two reasons. First,
  10500. * some older NVM configurations load a default MAC-VLAN
  10501. * filter that will accept any tagged packet, and we want to
  10502. * replace this with a normal filter. Additionally, it is
  10503. * possible our MAC address was provided by the platform using
  10504. * Open Firmware or similar.
  10505. *
  10506. * Thus, we need to remove the default filter and install one
  10507. * specific to the MAC address.
  10508. */
  10509. i40e_rm_default_mac_filter(vsi, mac_addr);
  10510. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10511. i40e_add_mac_filter(vsi, mac_addr);
  10512. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10513. } else {
  10514. /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
  10515. * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
  10516. * the end, which is 4 bytes long, so force truncation of the
  10517. * original name by IFNAMSIZ - 4
  10518. */
  10519. snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
  10520. IFNAMSIZ - 4,
  10521. pf->vsi[pf->lan_vsi]->netdev->name);
  10522. eth_random_addr(mac_addr);
  10523. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10524. i40e_add_mac_filter(vsi, mac_addr);
  10525. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10526. }
  10527. /* Add the broadcast filter so that we initially will receive
  10528. * broadcast packets. Note that when a new VLAN is first added the
  10529. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  10530. * specific filters as part of transitioning into "vlan" operation.
  10531. * When more VLANs are added, the driver will copy each existing MAC
  10532. * filter and add it for the new VLAN.
  10533. *
  10534. * Broadcast filters are handled specially by
  10535. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  10536. * promiscuous bit instead of adding this directly as a MAC/VLAN
  10537. * filter. The subtask will update the correct broadcast promiscuous
  10538. * bits as VLANs become active or inactive.
  10539. */
  10540. eth_broadcast_addr(broadcast);
  10541. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10542. i40e_add_mac_filter(vsi, broadcast);
  10543. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10544. ether_addr_copy(netdev->dev_addr, mac_addr);
  10545. ether_addr_copy(netdev->perm_addr, mac_addr);
  10546. /* i40iw_net_event() reads 16 bytes from neigh->primary_key */
  10547. netdev->neigh_priv_len = sizeof(u32) * 4;
  10548. netdev->priv_flags |= IFF_UNICAST_FLT;
  10549. netdev->priv_flags |= IFF_SUPP_NOFCS;
  10550. /* Setup netdev TC information */
  10551. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  10552. netdev->netdev_ops = &i40e_netdev_ops;
  10553. netdev->watchdog_timeo = 5 * HZ;
  10554. i40e_set_ethtool_ops(netdev);
  10555. /* MTU range: 68 - 9706 */
  10556. netdev->min_mtu = ETH_MIN_MTU;
  10557. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  10558. return 0;
  10559. }
  10560. /**
  10561. * i40e_vsi_delete - Delete a VSI from the switch
  10562. * @vsi: the VSI being removed
  10563. *
  10564. * Returns 0 on success, negative value on failure
  10565. **/
  10566. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  10567. {
  10568. /* remove default VSI is not allowed */
  10569. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  10570. return;
  10571. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  10572. }
  10573. /**
  10574. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  10575. * @vsi: the VSI being queried
  10576. *
  10577. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  10578. **/
  10579. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  10580. {
  10581. struct i40e_veb *veb;
  10582. struct i40e_pf *pf = vsi->back;
  10583. /* Uplink is not a bridge so default to VEB */
  10584. if (vsi->veb_idx == I40E_NO_VEB)
  10585. return 1;
  10586. veb = pf->veb[vsi->veb_idx];
  10587. if (!veb) {
  10588. dev_info(&pf->pdev->dev,
  10589. "There is no veb associated with the bridge\n");
  10590. return -ENOENT;
  10591. }
  10592. /* Uplink is a bridge in VEPA mode */
  10593. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  10594. return 0;
  10595. } else {
  10596. /* Uplink is a bridge in VEB mode */
  10597. return 1;
  10598. }
  10599. /* VEPA is now default bridge, so return 0 */
  10600. return 0;
  10601. }
  10602. /**
  10603. * i40e_add_vsi - Add a VSI to the switch
  10604. * @vsi: the VSI being configured
  10605. *
  10606. * This initializes a VSI context depending on the VSI type to be added and
  10607. * passes it down to the add_vsi aq command.
  10608. **/
  10609. static int i40e_add_vsi(struct i40e_vsi *vsi)
  10610. {
  10611. int ret = -ENODEV;
  10612. struct i40e_pf *pf = vsi->back;
  10613. struct i40e_hw *hw = &pf->hw;
  10614. struct i40e_vsi_context ctxt;
  10615. struct i40e_mac_filter *f;
  10616. struct hlist_node *h;
  10617. int bkt;
  10618. u8 enabled_tc = 0x1; /* TC0 enabled */
  10619. int f_count = 0;
  10620. memset(&ctxt, 0, sizeof(ctxt));
  10621. switch (vsi->type) {
  10622. case I40E_VSI_MAIN:
  10623. /* The PF's main VSI is already setup as part of the
  10624. * device initialization, so we'll not bother with
  10625. * the add_vsi call, but we will retrieve the current
  10626. * VSI context.
  10627. */
  10628. ctxt.seid = pf->main_vsi_seid;
  10629. ctxt.pf_num = pf->hw.pf_id;
  10630. ctxt.vf_num = 0;
  10631. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  10632. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10633. if (ret) {
  10634. dev_info(&pf->pdev->dev,
  10635. "couldn't get PF vsi config, 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. return -ENOENT;
  10640. }
  10641. vsi->info = ctxt.info;
  10642. vsi->info.valid_sections = 0;
  10643. vsi->seid = ctxt.seid;
  10644. vsi->id = ctxt.vsi_number;
  10645. enabled_tc = i40e_pf_get_tc_map(pf);
  10646. /* Source pruning is enabled by default, so the flag is
  10647. * negative logic - if it's set, we need to fiddle with
  10648. * the VSI to disable source pruning.
  10649. */
  10650. if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
  10651. memset(&ctxt, 0, sizeof(ctxt));
  10652. ctxt.seid = pf->main_vsi_seid;
  10653. ctxt.pf_num = pf->hw.pf_id;
  10654. ctxt.vf_num = 0;
  10655. ctxt.info.valid_sections |=
  10656. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10657. ctxt.info.switch_id =
  10658. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
  10659. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10660. if (ret) {
  10661. dev_info(&pf->pdev->dev,
  10662. "update vsi failed, err %s aq_err %s\n",
  10663. i40e_stat_str(&pf->hw, ret),
  10664. i40e_aq_str(&pf->hw,
  10665. pf->hw.aq.asq_last_status));
  10666. ret = -ENOENT;
  10667. goto err;
  10668. }
  10669. }
  10670. /* MFP mode setup queue map and update VSI */
  10671. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  10672. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  10673. memset(&ctxt, 0, sizeof(ctxt));
  10674. ctxt.seid = pf->main_vsi_seid;
  10675. ctxt.pf_num = pf->hw.pf_id;
  10676. ctxt.vf_num = 0;
  10677. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  10678. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10679. if (ret) {
  10680. dev_info(&pf->pdev->dev,
  10681. "update vsi failed, err %s aq_err %s\n",
  10682. i40e_stat_str(&pf->hw, ret),
  10683. i40e_aq_str(&pf->hw,
  10684. pf->hw.aq.asq_last_status));
  10685. ret = -ENOENT;
  10686. goto err;
  10687. }
  10688. /* update the local VSI info queue map */
  10689. i40e_vsi_update_queue_map(vsi, &ctxt);
  10690. vsi->info.valid_sections = 0;
  10691. } else {
  10692. /* Default/Main VSI is only enabled for TC0
  10693. * reconfigure it to enable all TCs that are
  10694. * available on the port in SFP mode.
  10695. * For MFP case the iSCSI PF would use this
  10696. * flow to enable LAN+iSCSI TC.
  10697. */
  10698. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  10699. if (ret) {
  10700. /* Single TC condition is not fatal,
  10701. * message and continue
  10702. */
  10703. dev_info(&pf->pdev->dev,
  10704. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  10705. enabled_tc,
  10706. i40e_stat_str(&pf->hw, ret),
  10707. i40e_aq_str(&pf->hw,
  10708. pf->hw.aq.asq_last_status));
  10709. }
  10710. }
  10711. break;
  10712. case I40E_VSI_FDIR:
  10713. ctxt.pf_num = hw->pf_id;
  10714. ctxt.vf_num = 0;
  10715. ctxt.uplink_seid = vsi->uplink_seid;
  10716. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10717. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10718. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  10719. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  10720. ctxt.info.valid_sections |=
  10721. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10722. ctxt.info.switch_id =
  10723. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10724. }
  10725. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10726. break;
  10727. case I40E_VSI_VMDQ2:
  10728. ctxt.pf_num = hw->pf_id;
  10729. ctxt.vf_num = 0;
  10730. ctxt.uplink_seid = vsi->uplink_seid;
  10731. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10732. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  10733. /* This VSI is connected to VEB so the switch_id
  10734. * should be set to zero by default.
  10735. */
  10736. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10737. ctxt.info.valid_sections |=
  10738. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10739. ctxt.info.switch_id =
  10740. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10741. }
  10742. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10743. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10744. break;
  10745. case I40E_VSI_SRIOV:
  10746. ctxt.pf_num = hw->pf_id;
  10747. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  10748. ctxt.uplink_seid = vsi->uplink_seid;
  10749. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10750. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  10751. /* This VSI is connected to VEB so the switch_id
  10752. * should be set to zero by default.
  10753. */
  10754. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10755. ctxt.info.valid_sections |=
  10756. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10757. ctxt.info.switch_id =
  10758. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10759. }
  10760. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  10761. ctxt.info.valid_sections |=
  10762. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  10763. ctxt.info.queueing_opt_flags |=
  10764. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  10765. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  10766. }
  10767. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  10768. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  10769. if (pf->vf[vsi->vf_id].spoofchk) {
  10770. ctxt.info.valid_sections |=
  10771. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  10772. ctxt.info.sec_flags |=
  10773. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  10774. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  10775. }
  10776. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10777. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10778. break;
  10779. case I40E_VSI_IWARP:
  10780. /* send down message to iWARP */
  10781. break;
  10782. default:
  10783. return -ENODEV;
  10784. }
  10785. if (vsi->type != I40E_VSI_MAIN) {
  10786. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  10787. if (ret) {
  10788. dev_info(&vsi->back->pdev->dev,
  10789. "add vsi failed, err %s aq_err %s\n",
  10790. i40e_stat_str(&pf->hw, ret),
  10791. i40e_aq_str(&pf->hw,
  10792. pf->hw.aq.asq_last_status));
  10793. ret = -ENOENT;
  10794. goto err;
  10795. }
  10796. vsi->info = ctxt.info;
  10797. vsi->info.valid_sections = 0;
  10798. vsi->seid = ctxt.seid;
  10799. vsi->id = ctxt.vsi_number;
  10800. }
  10801. vsi->active_filters = 0;
  10802. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  10803. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10804. /* If macvlan filters already exist, force them to get loaded */
  10805. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  10806. f->state = I40E_FILTER_NEW;
  10807. f_count++;
  10808. }
  10809. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10810. if (f_count) {
  10811. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  10812. set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
  10813. }
  10814. /* Update VSI BW information */
  10815. ret = i40e_vsi_get_bw_info(vsi);
  10816. if (ret) {
  10817. dev_info(&pf->pdev->dev,
  10818. "couldn't get vsi bw info, err %s aq_err %s\n",
  10819. i40e_stat_str(&pf->hw, ret),
  10820. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10821. /* VSI is already added so not tearing that up */
  10822. ret = 0;
  10823. }
  10824. err:
  10825. return ret;
  10826. }
  10827. /**
  10828. * i40e_vsi_release - Delete a VSI and free its resources
  10829. * @vsi: the VSI being removed
  10830. *
  10831. * Returns 0 on success or < 0 on error
  10832. **/
  10833. int i40e_vsi_release(struct i40e_vsi *vsi)
  10834. {
  10835. struct i40e_mac_filter *f;
  10836. struct hlist_node *h;
  10837. struct i40e_veb *veb = NULL;
  10838. struct i40e_pf *pf;
  10839. u16 uplink_seid;
  10840. int i, n, bkt;
  10841. pf = vsi->back;
  10842. /* release of a VEB-owner or last VSI is not allowed */
  10843. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  10844. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  10845. vsi->seid, vsi->uplink_seid);
  10846. return -ENODEV;
  10847. }
  10848. if (vsi == pf->vsi[pf->lan_vsi] &&
  10849. !test_bit(__I40E_DOWN, pf->state)) {
  10850. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  10851. return -ENODEV;
  10852. }
  10853. uplink_seid = vsi->uplink_seid;
  10854. if (vsi->type != I40E_VSI_SRIOV) {
  10855. if (vsi->netdev_registered) {
  10856. vsi->netdev_registered = false;
  10857. if (vsi->netdev) {
  10858. /* results in a call to i40e_close() */
  10859. unregister_netdev(vsi->netdev);
  10860. }
  10861. } else {
  10862. i40e_vsi_close(vsi);
  10863. }
  10864. i40e_vsi_disable_irq(vsi);
  10865. }
  10866. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10867. /* clear the sync flag on all filters */
  10868. if (vsi->netdev) {
  10869. __dev_uc_unsync(vsi->netdev, NULL);
  10870. __dev_mc_unsync(vsi->netdev, NULL);
  10871. }
  10872. /* make sure any remaining filters are marked for deletion */
  10873. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  10874. __i40e_del_filter(vsi, f);
  10875. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10876. i40e_sync_vsi_filters(vsi);
  10877. i40e_vsi_delete(vsi);
  10878. i40e_vsi_free_q_vectors(vsi);
  10879. if (vsi->netdev) {
  10880. free_netdev(vsi->netdev);
  10881. vsi->netdev = NULL;
  10882. }
  10883. i40e_vsi_clear_rings(vsi);
  10884. i40e_vsi_clear(vsi);
  10885. /* If this was the last thing on the VEB, except for the
  10886. * controlling VSI, remove the VEB, which puts the controlling
  10887. * VSI onto the next level down in the switch.
  10888. *
  10889. * Well, okay, there's one more exception here: don't remove
  10890. * the orphan VEBs yet. We'll wait for an explicit remove request
  10891. * from up the network stack.
  10892. */
  10893. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  10894. if (pf->vsi[i] &&
  10895. pf->vsi[i]->uplink_seid == uplink_seid &&
  10896. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  10897. n++; /* count the VSIs */
  10898. }
  10899. }
  10900. for (i = 0; i < I40E_MAX_VEB; i++) {
  10901. if (!pf->veb[i])
  10902. continue;
  10903. if (pf->veb[i]->uplink_seid == uplink_seid)
  10904. n++; /* count the VEBs */
  10905. if (pf->veb[i]->seid == uplink_seid)
  10906. veb = pf->veb[i];
  10907. }
  10908. if (n == 0 && veb && veb->uplink_seid != 0)
  10909. i40e_veb_release(veb);
  10910. return 0;
  10911. }
  10912. /**
  10913. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  10914. * @vsi: ptr to the VSI
  10915. *
  10916. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  10917. * corresponding SW VSI structure and initializes num_queue_pairs for the
  10918. * newly allocated VSI.
  10919. *
  10920. * Returns 0 on success or negative on failure
  10921. **/
  10922. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  10923. {
  10924. int ret = -ENOENT;
  10925. struct i40e_pf *pf = vsi->back;
  10926. if (vsi->q_vectors[0]) {
  10927. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  10928. vsi->seid);
  10929. return -EEXIST;
  10930. }
  10931. if (vsi->base_vector) {
  10932. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  10933. vsi->seid, vsi->base_vector);
  10934. return -EEXIST;
  10935. }
  10936. ret = i40e_vsi_alloc_q_vectors(vsi);
  10937. if (ret) {
  10938. dev_info(&pf->pdev->dev,
  10939. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  10940. vsi->num_q_vectors, vsi->seid, ret);
  10941. vsi->num_q_vectors = 0;
  10942. goto vector_setup_out;
  10943. }
  10944. /* In Legacy mode, we do not have to get any other vector since we
  10945. * piggyback on the misc/ICR0 for queue interrupts.
  10946. */
  10947. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  10948. return ret;
  10949. if (vsi->num_q_vectors)
  10950. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  10951. vsi->num_q_vectors, vsi->idx);
  10952. if (vsi->base_vector < 0) {
  10953. dev_info(&pf->pdev->dev,
  10954. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  10955. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  10956. i40e_vsi_free_q_vectors(vsi);
  10957. ret = -ENOENT;
  10958. goto vector_setup_out;
  10959. }
  10960. vector_setup_out:
  10961. return ret;
  10962. }
  10963. /**
  10964. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  10965. * @vsi: pointer to the vsi.
  10966. *
  10967. * This re-allocates a vsi's queue resources.
  10968. *
  10969. * Returns pointer to the successfully allocated and configured VSI sw struct
  10970. * on success, otherwise returns NULL on failure.
  10971. **/
  10972. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  10973. {
  10974. u16 alloc_queue_pairs;
  10975. struct i40e_pf *pf;
  10976. u8 enabled_tc;
  10977. int ret;
  10978. if (!vsi)
  10979. return NULL;
  10980. pf = vsi->back;
  10981. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  10982. i40e_vsi_clear_rings(vsi);
  10983. i40e_vsi_free_arrays(vsi, false);
  10984. i40e_set_num_rings_in_vsi(vsi);
  10985. ret = i40e_vsi_alloc_arrays(vsi, false);
  10986. if (ret)
  10987. goto err_vsi;
  10988. alloc_queue_pairs = vsi->alloc_queue_pairs *
  10989. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  10990. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  10991. if (ret < 0) {
  10992. dev_info(&pf->pdev->dev,
  10993. "failed to get tracking for %d queues for VSI %d err %d\n",
  10994. alloc_queue_pairs, vsi->seid, ret);
  10995. goto err_vsi;
  10996. }
  10997. vsi->base_queue = ret;
  10998. /* Update the FW view of the VSI. Force a reset of TC and queue
  10999. * layout configurations.
  11000. */
  11001. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  11002. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  11003. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  11004. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  11005. if (vsi->type == I40E_VSI_MAIN)
  11006. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  11007. /* assign it some queues */
  11008. ret = i40e_alloc_rings(vsi);
  11009. if (ret)
  11010. goto err_rings;
  11011. /* map all of the rings to the q_vectors */
  11012. i40e_vsi_map_rings_to_vectors(vsi);
  11013. return vsi;
  11014. err_rings:
  11015. i40e_vsi_free_q_vectors(vsi);
  11016. if (vsi->netdev_registered) {
  11017. vsi->netdev_registered = false;
  11018. unregister_netdev(vsi->netdev);
  11019. free_netdev(vsi->netdev);
  11020. vsi->netdev = NULL;
  11021. }
  11022. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  11023. err_vsi:
  11024. i40e_vsi_clear(vsi);
  11025. return NULL;
  11026. }
  11027. /**
  11028. * i40e_vsi_setup - Set up a VSI by a given type
  11029. * @pf: board private structure
  11030. * @type: VSI type
  11031. * @uplink_seid: the switch element to link to
  11032. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  11033. *
  11034. * This allocates the sw VSI structure and its queue resources, then add a VSI
  11035. * to the identified VEB.
  11036. *
  11037. * Returns pointer to the successfully allocated and configure VSI sw struct on
  11038. * success, otherwise returns NULL on failure.
  11039. **/
  11040. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  11041. u16 uplink_seid, u32 param1)
  11042. {
  11043. struct i40e_vsi *vsi = NULL;
  11044. struct i40e_veb *veb = NULL;
  11045. u16 alloc_queue_pairs;
  11046. int ret, i;
  11047. int v_idx;
  11048. /* The requested uplink_seid must be either
  11049. * - the PF's port seid
  11050. * no VEB is needed because this is the PF
  11051. * or this is a Flow Director special case VSI
  11052. * - seid of an existing VEB
  11053. * - seid of a VSI that owns an existing VEB
  11054. * - seid of a VSI that doesn't own a VEB
  11055. * a new VEB is created and the VSI becomes the owner
  11056. * - seid of the PF VSI, which is what creates the first VEB
  11057. * this is a special case of the previous
  11058. *
  11059. * Find which uplink_seid we were given and create a new VEB if needed
  11060. */
  11061. for (i = 0; i < I40E_MAX_VEB; i++) {
  11062. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  11063. veb = pf->veb[i];
  11064. break;
  11065. }
  11066. }
  11067. if (!veb && uplink_seid != pf->mac_seid) {
  11068. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11069. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  11070. vsi = pf->vsi[i];
  11071. break;
  11072. }
  11073. }
  11074. if (!vsi) {
  11075. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  11076. uplink_seid);
  11077. return NULL;
  11078. }
  11079. if (vsi->uplink_seid == pf->mac_seid)
  11080. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  11081. vsi->tc_config.enabled_tc);
  11082. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  11083. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  11084. vsi->tc_config.enabled_tc);
  11085. if (veb) {
  11086. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  11087. dev_info(&vsi->back->pdev->dev,
  11088. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  11089. return NULL;
  11090. }
  11091. /* We come up by default in VEPA mode if SRIOV is not
  11092. * already enabled, in which case we can't force VEPA
  11093. * mode.
  11094. */
  11095. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  11096. veb->bridge_mode = BRIDGE_MODE_VEPA;
  11097. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  11098. }
  11099. i40e_config_bridge_mode(veb);
  11100. }
  11101. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  11102. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  11103. veb = pf->veb[i];
  11104. }
  11105. if (!veb) {
  11106. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  11107. return NULL;
  11108. }
  11109. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11110. uplink_seid = veb->seid;
  11111. }
  11112. /* get vsi sw struct */
  11113. v_idx = i40e_vsi_mem_alloc(pf, type);
  11114. if (v_idx < 0)
  11115. goto err_alloc;
  11116. vsi = pf->vsi[v_idx];
  11117. if (!vsi)
  11118. goto err_alloc;
  11119. vsi->type = type;
  11120. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  11121. if (type == I40E_VSI_MAIN)
  11122. pf->lan_vsi = v_idx;
  11123. else if (type == I40E_VSI_SRIOV)
  11124. vsi->vf_id = param1;
  11125. /* assign it some queues */
  11126. alloc_queue_pairs = vsi->alloc_queue_pairs *
  11127. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  11128. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  11129. if (ret < 0) {
  11130. dev_info(&pf->pdev->dev,
  11131. "failed to get tracking for %d queues for VSI %d err=%d\n",
  11132. alloc_queue_pairs, vsi->seid, ret);
  11133. goto err_vsi;
  11134. }
  11135. vsi->base_queue = ret;
  11136. /* get a VSI from the hardware */
  11137. vsi->uplink_seid = uplink_seid;
  11138. ret = i40e_add_vsi(vsi);
  11139. if (ret)
  11140. goto err_vsi;
  11141. switch (vsi->type) {
  11142. /* setup the netdev if needed */
  11143. case I40E_VSI_MAIN:
  11144. case I40E_VSI_VMDQ2:
  11145. ret = i40e_config_netdev(vsi);
  11146. if (ret)
  11147. goto err_netdev;
  11148. ret = register_netdev(vsi->netdev);
  11149. if (ret)
  11150. goto err_netdev;
  11151. vsi->netdev_registered = true;
  11152. netif_carrier_off(vsi->netdev);
  11153. #ifdef CONFIG_I40E_DCB
  11154. /* Setup DCB netlink interface */
  11155. i40e_dcbnl_setup(vsi);
  11156. #endif /* CONFIG_I40E_DCB */
  11157. /* fall through */
  11158. case I40E_VSI_FDIR:
  11159. /* set up vectors and rings if needed */
  11160. ret = i40e_vsi_setup_vectors(vsi);
  11161. if (ret)
  11162. goto err_msix;
  11163. ret = i40e_alloc_rings(vsi);
  11164. if (ret)
  11165. goto err_rings;
  11166. /* map all of the rings to the q_vectors */
  11167. i40e_vsi_map_rings_to_vectors(vsi);
  11168. i40e_vsi_reset_stats(vsi);
  11169. break;
  11170. default:
  11171. /* no netdev or rings for the other VSI types */
  11172. break;
  11173. }
  11174. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  11175. (vsi->type == I40E_VSI_VMDQ2)) {
  11176. ret = i40e_vsi_config_rss(vsi);
  11177. }
  11178. return vsi;
  11179. err_rings:
  11180. i40e_vsi_free_q_vectors(vsi);
  11181. err_msix:
  11182. if (vsi->netdev_registered) {
  11183. vsi->netdev_registered = false;
  11184. unregister_netdev(vsi->netdev);
  11185. free_netdev(vsi->netdev);
  11186. vsi->netdev = NULL;
  11187. }
  11188. err_netdev:
  11189. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  11190. err_vsi:
  11191. i40e_vsi_clear(vsi);
  11192. err_alloc:
  11193. return NULL;
  11194. }
  11195. /**
  11196. * i40e_veb_get_bw_info - Query VEB BW information
  11197. * @veb: the veb to query
  11198. *
  11199. * Query the Tx scheduler BW configuration data for given VEB
  11200. **/
  11201. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  11202. {
  11203. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  11204. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  11205. struct i40e_pf *pf = veb->pf;
  11206. struct i40e_hw *hw = &pf->hw;
  11207. u32 tc_bw_max;
  11208. int ret = 0;
  11209. int i;
  11210. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  11211. &bw_data, NULL);
  11212. if (ret) {
  11213. dev_info(&pf->pdev->dev,
  11214. "query veb bw config failed, err %s aq_err %s\n",
  11215. i40e_stat_str(&pf->hw, ret),
  11216. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11217. goto out;
  11218. }
  11219. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  11220. &ets_data, NULL);
  11221. if (ret) {
  11222. dev_info(&pf->pdev->dev,
  11223. "query veb bw ets config failed, err %s aq_err %s\n",
  11224. i40e_stat_str(&pf->hw, ret),
  11225. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11226. goto out;
  11227. }
  11228. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  11229. veb->bw_max_quanta = ets_data.tc_bw_max;
  11230. veb->is_abs_credits = bw_data.absolute_credits_enable;
  11231. veb->enabled_tc = ets_data.tc_valid_bits;
  11232. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  11233. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  11234. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  11235. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  11236. veb->bw_tc_limit_credits[i] =
  11237. le16_to_cpu(bw_data.tc_bw_limits[i]);
  11238. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  11239. }
  11240. out:
  11241. return ret;
  11242. }
  11243. /**
  11244. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  11245. * @pf: board private structure
  11246. *
  11247. * On error: returns error code (negative)
  11248. * On success: returns vsi index in PF (positive)
  11249. **/
  11250. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  11251. {
  11252. int ret = -ENOENT;
  11253. struct i40e_veb *veb;
  11254. int i;
  11255. /* Need to protect the allocation of switch elements at the PF level */
  11256. mutex_lock(&pf->switch_mutex);
  11257. /* VEB list may be fragmented if VEB creation/destruction has
  11258. * been happening. We can afford to do a quick scan to look
  11259. * for any free slots in the list.
  11260. *
  11261. * find next empty veb slot, looping back around if necessary
  11262. */
  11263. i = 0;
  11264. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  11265. i++;
  11266. if (i >= I40E_MAX_VEB) {
  11267. ret = -ENOMEM;
  11268. goto err_alloc_veb; /* out of VEB slots! */
  11269. }
  11270. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  11271. if (!veb) {
  11272. ret = -ENOMEM;
  11273. goto err_alloc_veb;
  11274. }
  11275. veb->pf = pf;
  11276. veb->idx = i;
  11277. veb->enabled_tc = 1;
  11278. pf->veb[i] = veb;
  11279. ret = i;
  11280. err_alloc_veb:
  11281. mutex_unlock(&pf->switch_mutex);
  11282. return ret;
  11283. }
  11284. /**
  11285. * i40e_switch_branch_release - Delete a branch of the switch tree
  11286. * @branch: where to start deleting
  11287. *
  11288. * This uses recursion to find the tips of the branch to be
  11289. * removed, deleting until we get back to and can delete this VEB.
  11290. **/
  11291. static void i40e_switch_branch_release(struct i40e_veb *branch)
  11292. {
  11293. struct i40e_pf *pf = branch->pf;
  11294. u16 branch_seid = branch->seid;
  11295. u16 veb_idx = branch->idx;
  11296. int i;
  11297. /* release any VEBs on this VEB - RECURSION */
  11298. for (i = 0; i < I40E_MAX_VEB; i++) {
  11299. if (!pf->veb[i])
  11300. continue;
  11301. if (pf->veb[i]->uplink_seid == branch->seid)
  11302. i40e_switch_branch_release(pf->veb[i]);
  11303. }
  11304. /* Release the VSIs on this VEB, but not the owner VSI.
  11305. *
  11306. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  11307. * the VEB itself, so don't use (*branch) after this loop.
  11308. */
  11309. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11310. if (!pf->vsi[i])
  11311. continue;
  11312. if (pf->vsi[i]->uplink_seid == branch_seid &&
  11313. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  11314. i40e_vsi_release(pf->vsi[i]);
  11315. }
  11316. }
  11317. /* There's one corner case where the VEB might not have been
  11318. * removed, so double check it here and remove it if needed.
  11319. * This case happens if the veb was created from the debugfs
  11320. * commands and no VSIs were added to it.
  11321. */
  11322. if (pf->veb[veb_idx])
  11323. i40e_veb_release(pf->veb[veb_idx]);
  11324. }
  11325. /**
  11326. * i40e_veb_clear - remove veb struct
  11327. * @veb: the veb to remove
  11328. **/
  11329. static void i40e_veb_clear(struct i40e_veb *veb)
  11330. {
  11331. if (!veb)
  11332. return;
  11333. if (veb->pf) {
  11334. struct i40e_pf *pf = veb->pf;
  11335. mutex_lock(&pf->switch_mutex);
  11336. if (pf->veb[veb->idx] == veb)
  11337. pf->veb[veb->idx] = NULL;
  11338. mutex_unlock(&pf->switch_mutex);
  11339. }
  11340. kfree(veb);
  11341. }
  11342. /**
  11343. * i40e_veb_release - Delete a VEB and free its resources
  11344. * @veb: the VEB being removed
  11345. **/
  11346. void i40e_veb_release(struct i40e_veb *veb)
  11347. {
  11348. struct i40e_vsi *vsi = NULL;
  11349. struct i40e_pf *pf;
  11350. int i, n = 0;
  11351. pf = veb->pf;
  11352. /* find the remaining VSI and check for extras */
  11353. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11354. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  11355. n++;
  11356. vsi = pf->vsi[i];
  11357. }
  11358. }
  11359. if (n != 1) {
  11360. dev_info(&pf->pdev->dev,
  11361. "can't remove VEB %d with %d VSIs left\n",
  11362. veb->seid, n);
  11363. return;
  11364. }
  11365. /* move the remaining VSI to uplink veb */
  11366. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  11367. if (veb->uplink_seid) {
  11368. vsi->uplink_seid = veb->uplink_seid;
  11369. if (veb->uplink_seid == pf->mac_seid)
  11370. vsi->veb_idx = I40E_NO_VEB;
  11371. else
  11372. vsi->veb_idx = veb->veb_idx;
  11373. } else {
  11374. /* floating VEB */
  11375. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  11376. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  11377. }
  11378. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11379. i40e_veb_clear(veb);
  11380. }
  11381. /**
  11382. * i40e_add_veb - create the VEB in the switch
  11383. * @veb: the VEB to be instantiated
  11384. * @vsi: the controlling VSI
  11385. **/
  11386. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  11387. {
  11388. struct i40e_pf *pf = veb->pf;
  11389. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  11390. int ret;
  11391. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  11392. veb->enabled_tc, false,
  11393. &veb->seid, enable_stats, NULL);
  11394. /* get a VEB from the hardware */
  11395. if (ret) {
  11396. dev_info(&pf->pdev->dev,
  11397. "couldn't add VEB, err %s aq_err %s\n",
  11398. i40e_stat_str(&pf->hw, ret),
  11399. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11400. return -EPERM;
  11401. }
  11402. /* get statistics counter */
  11403. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  11404. &veb->stats_idx, NULL, NULL, NULL);
  11405. if (ret) {
  11406. dev_info(&pf->pdev->dev,
  11407. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  11408. i40e_stat_str(&pf->hw, ret),
  11409. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11410. return -EPERM;
  11411. }
  11412. ret = i40e_veb_get_bw_info(veb);
  11413. if (ret) {
  11414. dev_info(&pf->pdev->dev,
  11415. "couldn't get VEB bw info, err %s aq_err %s\n",
  11416. i40e_stat_str(&pf->hw, ret),
  11417. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11418. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11419. return -ENOENT;
  11420. }
  11421. vsi->uplink_seid = veb->seid;
  11422. vsi->veb_idx = veb->idx;
  11423. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11424. return 0;
  11425. }
  11426. /**
  11427. * i40e_veb_setup - Set up a VEB
  11428. * @pf: board private structure
  11429. * @flags: VEB setup flags
  11430. * @uplink_seid: the switch element to link to
  11431. * @vsi_seid: the initial VSI seid
  11432. * @enabled_tc: Enabled TC bit-map
  11433. *
  11434. * This allocates the sw VEB structure and links it into the switch
  11435. * It is possible and legal for this to be a duplicate of an already
  11436. * existing VEB. It is also possible for both uplink and vsi seids
  11437. * to be zero, in order to create a floating VEB.
  11438. *
  11439. * Returns pointer to the successfully allocated VEB sw struct on
  11440. * success, otherwise returns NULL on failure.
  11441. **/
  11442. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  11443. u16 uplink_seid, u16 vsi_seid,
  11444. u8 enabled_tc)
  11445. {
  11446. struct i40e_veb *veb, *uplink_veb = NULL;
  11447. int vsi_idx, veb_idx;
  11448. int ret;
  11449. /* if one seid is 0, the other must be 0 to create a floating relay */
  11450. if ((uplink_seid == 0 || vsi_seid == 0) &&
  11451. (uplink_seid + vsi_seid != 0)) {
  11452. dev_info(&pf->pdev->dev,
  11453. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  11454. uplink_seid, vsi_seid);
  11455. return NULL;
  11456. }
  11457. /* make sure there is such a vsi and uplink */
  11458. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  11459. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  11460. break;
  11461. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  11462. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  11463. vsi_seid);
  11464. return NULL;
  11465. }
  11466. if (uplink_seid && uplink_seid != pf->mac_seid) {
  11467. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  11468. if (pf->veb[veb_idx] &&
  11469. pf->veb[veb_idx]->seid == uplink_seid) {
  11470. uplink_veb = pf->veb[veb_idx];
  11471. break;
  11472. }
  11473. }
  11474. if (!uplink_veb) {
  11475. dev_info(&pf->pdev->dev,
  11476. "uplink seid %d not found\n", uplink_seid);
  11477. return NULL;
  11478. }
  11479. }
  11480. /* get veb sw struct */
  11481. veb_idx = i40e_veb_mem_alloc(pf);
  11482. if (veb_idx < 0)
  11483. goto err_alloc;
  11484. veb = pf->veb[veb_idx];
  11485. veb->flags = flags;
  11486. veb->uplink_seid = uplink_seid;
  11487. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  11488. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  11489. /* create the VEB in the switch */
  11490. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  11491. if (ret)
  11492. goto err_veb;
  11493. if (vsi_idx == pf->lan_vsi)
  11494. pf->lan_veb = veb->idx;
  11495. return veb;
  11496. err_veb:
  11497. i40e_veb_clear(veb);
  11498. err_alloc:
  11499. return NULL;
  11500. }
  11501. /**
  11502. * i40e_setup_pf_switch_element - set PF vars based on switch type
  11503. * @pf: board private structure
  11504. * @ele: element we are building info from
  11505. * @num_reported: total number of elements
  11506. * @printconfig: should we print the contents
  11507. *
  11508. * helper function to assist in extracting a few useful SEID values.
  11509. **/
  11510. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  11511. struct i40e_aqc_switch_config_element_resp *ele,
  11512. u16 num_reported, bool printconfig)
  11513. {
  11514. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  11515. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  11516. u8 element_type = ele->element_type;
  11517. u16 seid = le16_to_cpu(ele->seid);
  11518. if (printconfig)
  11519. dev_info(&pf->pdev->dev,
  11520. "type=%d seid=%d uplink=%d downlink=%d\n",
  11521. element_type, seid, uplink_seid, downlink_seid);
  11522. switch (element_type) {
  11523. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  11524. pf->mac_seid = seid;
  11525. break;
  11526. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  11527. /* Main VEB? */
  11528. if (uplink_seid != pf->mac_seid)
  11529. break;
  11530. if (pf->lan_veb == I40E_NO_VEB) {
  11531. int v;
  11532. /* find existing or else empty VEB */
  11533. for (v = 0; v < I40E_MAX_VEB; v++) {
  11534. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  11535. pf->lan_veb = v;
  11536. break;
  11537. }
  11538. }
  11539. if (pf->lan_veb == I40E_NO_VEB) {
  11540. v = i40e_veb_mem_alloc(pf);
  11541. if (v < 0)
  11542. break;
  11543. pf->lan_veb = v;
  11544. }
  11545. }
  11546. pf->veb[pf->lan_veb]->seid = seid;
  11547. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  11548. pf->veb[pf->lan_veb]->pf = pf;
  11549. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  11550. break;
  11551. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  11552. if (num_reported != 1)
  11553. break;
  11554. /* This is immediately after a reset so we can assume this is
  11555. * the PF's VSI
  11556. */
  11557. pf->mac_seid = uplink_seid;
  11558. pf->pf_seid = downlink_seid;
  11559. pf->main_vsi_seid = seid;
  11560. if (printconfig)
  11561. dev_info(&pf->pdev->dev,
  11562. "pf_seid=%d main_vsi_seid=%d\n",
  11563. pf->pf_seid, pf->main_vsi_seid);
  11564. break;
  11565. case I40E_SWITCH_ELEMENT_TYPE_PF:
  11566. case I40E_SWITCH_ELEMENT_TYPE_VF:
  11567. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  11568. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  11569. case I40E_SWITCH_ELEMENT_TYPE_PE:
  11570. case I40E_SWITCH_ELEMENT_TYPE_PA:
  11571. /* ignore these for now */
  11572. break;
  11573. default:
  11574. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  11575. element_type, seid);
  11576. break;
  11577. }
  11578. }
  11579. /**
  11580. * i40e_fetch_switch_configuration - Get switch config from firmware
  11581. * @pf: board private structure
  11582. * @printconfig: should we print the contents
  11583. *
  11584. * Get the current switch configuration from the device and
  11585. * extract a few useful SEID values.
  11586. **/
  11587. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  11588. {
  11589. struct i40e_aqc_get_switch_config_resp *sw_config;
  11590. u16 next_seid = 0;
  11591. int ret = 0;
  11592. u8 *aq_buf;
  11593. int i;
  11594. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  11595. if (!aq_buf)
  11596. return -ENOMEM;
  11597. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  11598. do {
  11599. u16 num_reported, num_total;
  11600. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  11601. I40E_AQ_LARGE_BUF,
  11602. &next_seid, NULL);
  11603. if (ret) {
  11604. dev_info(&pf->pdev->dev,
  11605. "get switch config failed err %s aq_err %s\n",
  11606. i40e_stat_str(&pf->hw, ret),
  11607. i40e_aq_str(&pf->hw,
  11608. pf->hw.aq.asq_last_status));
  11609. kfree(aq_buf);
  11610. return -ENOENT;
  11611. }
  11612. num_reported = le16_to_cpu(sw_config->header.num_reported);
  11613. num_total = le16_to_cpu(sw_config->header.num_total);
  11614. if (printconfig)
  11615. dev_info(&pf->pdev->dev,
  11616. "header: %d reported %d total\n",
  11617. num_reported, num_total);
  11618. for (i = 0; i < num_reported; i++) {
  11619. struct i40e_aqc_switch_config_element_resp *ele =
  11620. &sw_config->element[i];
  11621. i40e_setup_pf_switch_element(pf, ele, num_reported,
  11622. printconfig);
  11623. }
  11624. } while (next_seid != 0);
  11625. kfree(aq_buf);
  11626. return ret;
  11627. }
  11628. /**
  11629. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  11630. * @pf: board private structure
  11631. * @reinit: if the Main VSI needs to re-initialized.
  11632. *
  11633. * Returns 0 on success, negative value on failure
  11634. **/
  11635. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  11636. {
  11637. u16 flags = 0;
  11638. int ret;
  11639. /* find out what's out there already */
  11640. ret = i40e_fetch_switch_configuration(pf, false);
  11641. if (ret) {
  11642. dev_info(&pf->pdev->dev,
  11643. "couldn't fetch switch config, err %s aq_err %s\n",
  11644. i40e_stat_str(&pf->hw, ret),
  11645. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11646. return ret;
  11647. }
  11648. i40e_pf_reset_stats(pf);
  11649. /* set the switch config bit for the whole device to
  11650. * support limited promisc or true promisc
  11651. * when user requests promisc. The default is limited
  11652. * promisc.
  11653. */
  11654. if ((pf->hw.pf_id == 0) &&
  11655. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
  11656. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11657. pf->last_sw_conf_flags = flags;
  11658. }
  11659. if (pf->hw.pf_id == 0) {
  11660. u16 valid_flags;
  11661. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11662. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
  11663. NULL);
  11664. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  11665. dev_info(&pf->pdev->dev,
  11666. "couldn't set switch config bits, err %s aq_err %s\n",
  11667. i40e_stat_str(&pf->hw, ret),
  11668. i40e_aq_str(&pf->hw,
  11669. pf->hw.aq.asq_last_status));
  11670. /* not a fatal problem, just keep going */
  11671. }
  11672. pf->last_sw_conf_valid_flags = valid_flags;
  11673. }
  11674. /* first time setup */
  11675. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  11676. struct i40e_vsi *vsi = NULL;
  11677. u16 uplink_seid;
  11678. /* Set up the PF VSI associated with the PF's main VSI
  11679. * that is already in the HW switch
  11680. */
  11681. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  11682. uplink_seid = pf->veb[pf->lan_veb]->seid;
  11683. else
  11684. uplink_seid = pf->mac_seid;
  11685. if (pf->lan_vsi == I40E_NO_VSI)
  11686. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  11687. else if (reinit)
  11688. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  11689. if (!vsi) {
  11690. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  11691. i40e_cloud_filter_exit(pf);
  11692. i40e_fdir_teardown(pf);
  11693. return -EAGAIN;
  11694. }
  11695. } else {
  11696. /* force a reset of TC and queue layout configurations */
  11697. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  11698. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  11699. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  11700. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  11701. }
  11702. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  11703. i40e_fdir_sb_setup(pf);
  11704. /* Setup static PF queue filter control settings */
  11705. ret = i40e_setup_pf_filter_control(pf);
  11706. if (ret) {
  11707. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  11708. ret);
  11709. /* Failure here should not stop continuing other steps */
  11710. }
  11711. /* enable RSS in the HW, even for only one queue, as the stack can use
  11712. * the hash
  11713. */
  11714. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  11715. i40e_pf_config_rss(pf);
  11716. /* fill in link information and enable LSE reporting */
  11717. i40e_link_event(pf);
  11718. /* Initialize user-specific link properties */
  11719. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  11720. I40E_AQ_AN_COMPLETED) ? true : false);
  11721. i40e_ptp_init(pf);
  11722. /* repopulate tunnel port filters */
  11723. i40e_sync_udp_filters(pf);
  11724. return ret;
  11725. }
  11726. /**
  11727. * i40e_determine_queue_usage - Work out queue distribution
  11728. * @pf: board private structure
  11729. **/
  11730. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  11731. {
  11732. int queues_left;
  11733. int q_max;
  11734. pf->num_lan_qps = 0;
  11735. /* Find the max queues to be put into basic use. We'll always be
  11736. * using TC0, whether or not DCB is running, and TC0 will get the
  11737. * big RSS set.
  11738. */
  11739. queues_left = pf->hw.func_caps.num_tx_qp;
  11740. if ((queues_left == 1) ||
  11741. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  11742. /* one qp for PF, no queues for anything else */
  11743. queues_left = 0;
  11744. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11745. /* make sure all the fancies are disabled */
  11746. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11747. I40E_FLAG_IWARP_ENABLED |
  11748. I40E_FLAG_FD_SB_ENABLED |
  11749. I40E_FLAG_FD_ATR_ENABLED |
  11750. I40E_FLAG_DCB_CAPABLE |
  11751. I40E_FLAG_DCB_ENABLED |
  11752. I40E_FLAG_SRIOV_ENABLED |
  11753. I40E_FLAG_VMDQ_ENABLED);
  11754. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11755. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  11756. I40E_FLAG_FD_SB_ENABLED |
  11757. I40E_FLAG_FD_ATR_ENABLED |
  11758. I40E_FLAG_DCB_CAPABLE))) {
  11759. /* one qp for PF */
  11760. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11761. queues_left -= pf->num_lan_qps;
  11762. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11763. I40E_FLAG_IWARP_ENABLED |
  11764. I40E_FLAG_FD_SB_ENABLED |
  11765. I40E_FLAG_FD_ATR_ENABLED |
  11766. I40E_FLAG_DCB_ENABLED |
  11767. I40E_FLAG_VMDQ_ENABLED);
  11768. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11769. } else {
  11770. /* Not enough queues for all TCs */
  11771. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  11772. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  11773. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  11774. I40E_FLAG_DCB_ENABLED);
  11775. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  11776. }
  11777. /* limit lan qps to the smaller of qps, cpus or msix */
  11778. q_max = max_t(int, pf->rss_size_max, num_online_cpus());
  11779. q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
  11780. q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
  11781. pf->num_lan_qps = q_max;
  11782. queues_left -= pf->num_lan_qps;
  11783. }
  11784. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  11785. if (queues_left > 1) {
  11786. queues_left -= 1; /* save 1 queue for FD */
  11787. } else {
  11788. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  11789. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11790. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  11791. }
  11792. }
  11793. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  11794. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  11795. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  11796. (queues_left / pf->num_vf_qps));
  11797. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  11798. }
  11799. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  11800. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  11801. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  11802. (queues_left / pf->num_vmdq_qps));
  11803. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  11804. }
  11805. pf->queues_left = queues_left;
  11806. dev_dbg(&pf->pdev->dev,
  11807. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  11808. pf->hw.func_caps.num_tx_qp,
  11809. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  11810. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  11811. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  11812. queues_left);
  11813. }
  11814. /**
  11815. * i40e_setup_pf_filter_control - Setup PF static filter control
  11816. * @pf: PF to be setup
  11817. *
  11818. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  11819. * settings. If PE/FCoE are enabled then it will also set the per PF
  11820. * based filter sizes required for them. It also enables Flow director,
  11821. * ethertype and macvlan type filter settings for the pf.
  11822. *
  11823. * Returns 0 on success, negative on failure
  11824. **/
  11825. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  11826. {
  11827. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  11828. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  11829. /* Flow Director is enabled */
  11830. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  11831. settings->enable_fdir = true;
  11832. /* Ethtype and MACVLAN filters enabled for PF */
  11833. settings->enable_ethtype = true;
  11834. settings->enable_macvlan = true;
  11835. if (i40e_set_filter_control(&pf->hw, settings))
  11836. return -ENOENT;
  11837. return 0;
  11838. }
  11839. #define INFO_STRING_LEN 255
  11840. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  11841. static void i40e_print_features(struct i40e_pf *pf)
  11842. {
  11843. struct i40e_hw *hw = &pf->hw;
  11844. char *buf;
  11845. int i;
  11846. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  11847. if (!buf)
  11848. return;
  11849. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  11850. #ifdef CONFIG_PCI_IOV
  11851. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  11852. #endif
  11853. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  11854. pf->hw.func_caps.num_vsis,
  11855. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  11856. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  11857. i += snprintf(&buf[i], REMAIN(i), " RSS");
  11858. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  11859. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  11860. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  11861. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  11862. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  11863. }
  11864. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  11865. i += snprintf(&buf[i], REMAIN(i), " DCB");
  11866. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  11867. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  11868. if (pf->flags & I40E_FLAG_PTP)
  11869. i += snprintf(&buf[i], REMAIN(i), " PTP");
  11870. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  11871. i += snprintf(&buf[i], REMAIN(i), " VEB");
  11872. else
  11873. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  11874. dev_info(&pf->pdev->dev, "%s\n", buf);
  11875. kfree(buf);
  11876. WARN_ON(i > INFO_STRING_LEN);
  11877. }
  11878. /**
  11879. * i40e_get_platform_mac_addr - get platform-specific MAC address
  11880. * @pdev: PCI device information struct
  11881. * @pf: board private structure
  11882. *
  11883. * Look up the MAC address for the device. First we'll try
  11884. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  11885. * specific fallback. Otherwise, we'll default to the stored value in
  11886. * firmware.
  11887. **/
  11888. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  11889. {
  11890. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  11891. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  11892. }
  11893. /**
  11894. * i40e_probe - Device initialization routine
  11895. * @pdev: PCI device information struct
  11896. * @ent: entry in i40e_pci_tbl
  11897. *
  11898. * i40e_probe initializes a PF identified by a pci_dev structure.
  11899. * The OS initialization, configuring of the PF private structure,
  11900. * and a hardware reset occur.
  11901. *
  11902. * Returns 0 on success, negative on failure
  11903. **/
  11904. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  11905. {
  11906. struct i40e_aq_get_phy_abilities_resp abilities;
  11907. struct i40e_pf *pf;
  11908. struct i40e_hw *hw;
  11909. static u16 pfs_found;
  11910. u16 wol_nvm_bits;
  11911. u16 link_status;
  11912. int err;
  11913. u32 val;
  11914. u32 i;
  11915. u8 set_fc_aq_fail;
  11916. err = pci_enable_device_mem(pdev);
  11917. if (err)
  11918. return err;
  11919. /* set up for high or low dma */
  11920. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  11921. if (err) {
  11922. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  11923. if (err) {
  11924. dev_err(&pdev->dev,
  11925. "DMA configuration failed: 0x%x\n", err);
  11926. goto err_dma;
  11927. }
  11928. }
  11929. /* set up pci connections */
  11930. err = pci_request_mem_regions(pdev, i40e_driver_name);
  11931. if (err) {
  11932. dev_info(&pdev->dev,
  11933. "pci_request_selected_regions failed %d\n", err);
  11934. goto err_pci_reg;
  11935. }
  11936. pci_enable_pcie_error_reporting(pdev);
  11937. pci_set_master(pdev);
  11938. /* Now that we have a PCI connection, we need to do the
  11939. * low level device setup. This is primarily setting up
  11940. * the Admin Queue structures and then querying for the
  11941. * device's current profile information.
  11942. */
  11943. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  11944. if (!pf) {
  11945. err = -ENOMEM;
  11946. goto err_pf_alloc;
  11947. }
  11948. pf->next_vsi = 0;
  11949. pf->pdev = pdev;
  11950. set_bit(__I40E_DOWN, pf->state);
  11951. hw = &pf->hw;
  11952. hw->back = pf;
  11953. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  11954. I40E_MAX_CSR_SPACE);
  11955. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  11956. if (!hw->hw_addr) {
  11957. err = -EIO;
  11958. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  11959. (unsigned int)pci_resource_start(pdev, 0),
  11960. pf->ioremap_len, err);
  11961. goto err_ioremap;
  11962. }
  11963. hw->vendor_id = pdev->vendor;
  11964. hw->device_id = pdev->device;
  11965. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  11966. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  11967. hw->subsystem_device_id = pdev->subsystem_device;
  11968. hw->bus.device = PCI_SLOT(pdev->devfn);
  11969. hw->bus.func = PCI_FUNC(pdev->devfn);
  11970. hw->bus.bus_id = pdev->bus->number;
  11971. pf->instance = pfs_found;
  11972. /* Select something other than the 802.1ad ethertype for the
  11973. * switch to use internally and drop on ingress.
  11974. */
  11975. hw->switch_tag = 0xffff;
  11976. hw->first_tag = ETH_P_8021AD;
  11977. hw->second_tag = ETH_P_8021Q;
  11978. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  11979. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  11980. /* set up the locks for the AQ, do this only once in probe
  11981. * and destroy them only once in remove
  11982. */
  11983. mutex_init(&hw->aq.asq_mutex);
  11984. mutex_init(&hw->aq.arq_mutex);
  11985. pf->msg_enable = netif_msg_init(debug,
  11986. NETIF_MSG_DRV |
  11987. NETIF_MSG_PROBE |
  11988. NETIF_MSG_LINK);
  11989. if (debug < -1)
  11990. pf->hw.debug_mask = debug;
  11991. /* do a special CORER for clearing PXE mode once at init */
  11992. if (hw->revision_id == 0 &&
  11993. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  11994. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  11995. i40e_flush(hw);
  11996. msleep(200);
  11997. pf->corer_count++;
  11998. i40e_clear_pxe_mode(hw);
  11999. }
  12000. /* Reset here to make sure all is clean and to define PF 'n' */
  12001. i40e_clear_hw(hw);
  12002. err = i40e_pf_reset(hw);
  12003. if (err) {
  12004. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  12005. goto err_pf_reset;
  12006. }
  12007. pf->pfr_count++;
  12008. hw->aq.num_arq_entries = I40E_AQ_LEN;
  12009. hw->aq.num_asq_entries = I40E_AQ_LEN;
  12010. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  12011. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  12012. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  12013. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  12014. "%s-%s:misc",
  12015. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  12016. err = i40e_init_shared_code(hw);
  12017. if (err) {
  12018. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  12019. err);
  12020. goto err_pf_reset;
  12021. }
  12022. /* set up a default setting for link flow control */
  12023. pf->hw.fc.requested_mode = I40E_FC_NONE;
  12024. err = i40e_init_adminq(hw);
  12025. if (err) {
  12026. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  12027. dev_info(&pdev->dev,
  12028. "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");
  12029. else
  12030. dev_info(&pdev->dev,
  12031. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  12032. goto err_pf_reset;
  12033. }
  12034. i40e_get_oem_version(hw);
  12035. /* provide nvm, fw, api versions */
  12036. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  12037. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  12038. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  12039. i40e_nvm_version_str(hw));
  12040. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  12041. hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
  12042. dev_info(&pdev->dev,
  12043. "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");
  12044. else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
  12045. dev_info(&pdev->dev,
  12046. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  12047. i40e_verify_eeprom(pf);
  12048. /* Rev 0 hardware was never productized */
  12049. if (hw->revision_id < 1)
  12050. 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");
  12051. i40e_clear_pxe_mode(hw);
  12052. err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  12053. if (err)
  12054. goto err_adminq_setup;
  12055. err = i40e_sw_init(pf);
  12056. if (err) {
  12057. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  12058. goto err_sw_init;
  12059. }
  12060. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  12061. hw->func_caps.num_rx_qp, 0, 0);
  12062. if (err) {
  12063. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  12064. goto err_init_lan_hmc;
  12065. }
  12066. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  12067. if (err) {
  12068. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  12069. err = -ENOENT;
  12070. goto err_configure_lan_hmc;
  12071. }
  12072. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  12073. * Ignore error return codes because if it was already disabled via
  12074. * hardware settings this will fail
  12075. */
  12076. if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
  12077. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  12078. i40e_aq_stop_lldp(hw, true, NULL);
  12079. }
  12080. /* allow a platform config to override the HW addr */
  12081. i40e_get_platform_mac_addr(pdev, pf);
  12082. if (!is_valid_ether_addr(hw->mac.addr)) {
  12083. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  12084. err = -EIO;
  12085. goto err_mac_addr;
  12086. }
  12087. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  12088. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  12089. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  12090. if (is_valid_ether_addr(hw->mac.port_addr))
  12091. pf->hw_features |= I40E_HW_PORT_ID_VALID;
  12092. pci_set_drvdata(pdev, pf);
  12093. pci_save_state(pdev);
  12094. /* Enable FW to write default DCB config on link-up */
  12095. i40e_aq_set_dcb_parameters(hw, true, NULL);
  12096. #ifdef CONFIG_I40E_DCB
  12097. err = i40e_init_pf_dcb(pf);
  12098. if (err) {
  12099. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  12100. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  12101. /* Continue without DCB enabled */
  12102. }
  12103. #endif /* CONFIG_I40E_DCB */
  12104. /* set up periodic task facility */
  12105. timer_setup(&pf->service_timer, i40e_service_timer, 0);
  12106. pf->service_timer_period = HZ;
  12107. INIT_WORK(&pf->service_task, i40e_service_task);
  12108. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  12109. /* NVM bit on means WoL disabled for the port */
  12110. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  12111. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  12112. pf->wol_en = false;
  12113. else
  12114. pf->wol_en = true;
  12115. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  12116. /* set up the main switch operations */
  12117. i40e_determine_queue_usage(pf);
  12118. err = i40e_init_interrupt_scheme(pf);
  12119. if (err)
  12120. goto err_switch_setup;
  12121. /* The number of VSIs reported by the FW is the minimum guaranteed
  12122. * to us; HW supports far more and we share the remaining pool with
  12123. * the other PFs. We allocate space for more than the guarantee with
  12124. * the understanding that we might not get them all later.
  12125. */
  12126. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  12127. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  12128. else
  12129. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  12130. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  12131. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  12132. GFP_KERNEL);
  12133. if (!pf->vsi) {
  12134. err = -ENOMEM;
  12135. goto err_switch_setup;
  12136. }
  12137. #ifdef CONFIG_PCI_IOV
  12138. /* prep for VF support */
  12139. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12140. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12141. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12142. if (pci_num_vf(pdev))
  12143. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  12144. }
  12145. #endif
  12146. err = i40e_setup_pf_switch(pf, false);
  12147. if (err) {
  12148. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  12149. goto err_vsis;
  12150. }
  12151. INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
  12152. /* Make sure flow control is set according to current settings */
  12153. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  12154. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  12155. dev_dbg(&pf->pdev->dev,
  12156. "Set fc with err %s aq_err %s on get_phy_cap\n",
  12157. i40e_stat_str(hw, err),
  12158. i40e_aq_str(hw, hw->aq.asq_last_status));
  12159. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  12160. dev_dbg(&pf->pdev->dev,
  12161. "Set fc with err %s aq_err %s on set_phy_config\n",
  12162. i40e_stat_str(hw, err),
  12163. i40e_aq_str(hw, hw->aq.asq_last_status));
  12164. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  12165. dev_dbg(&pf->pdev->dev,
  12166. "Set fc with err %s aq_err %s on get_link_info\n",
  12167. i40e_stat_str(hw, err),
  12168. i40e_aq_str(hw, hw->aq.asq_last_status));
  12169. /* if FDIR VSI was set up, start it now */
  12170. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12171. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  12172. i40e_vsi_open(pf->vsi[i]);
  12173. break;
  12174. }
  12175. }
  12176. /* The driver only wants link up/down and module qualification
  12177. * reports from firmware. Note the negative logic.
  12178. */
  12179. err = i40e_aq_set_phy_int_mask(&pf->hw,
  12180. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  12181. I40E_AQ_EVENT_MEDIA_NA |
  12182. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  12183. if (err)
  12184. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  12185. i40e_stat_str(&pf->hw, err),
  12186. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12187. /* Reconfigure hardware for allowing smaller MSS in the case
  12188. * of TSO, so that we avoid the MDD being fired and causing
  12189. * a reset in the case of small MSS+TSO.
  12190. */
  12191. val = rd32(hw, I40E_REG_MSS);
  12192. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  12193. val &= ~I40E_REG_MSS_MIN_MASK;
  12194. val |= I40E_64BYTE_MSS;
  12195. wr32(hw, I40E_REG_MSS, val);
  12196. }
  12197. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  12198. msleep(75);
  12199. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  12200. if (err)
  12201. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  12202. i40e_stat_str(&pf->hw, err),
  12203. i40e_aq_str(&pf->hw,
  12204. pf->hw.aq.asq_last_status));
  12205. }
  12206. /* The main driver is (mostly) up and happy. We need to set this state
  12207. * before setting up the misc vector or we get a race and the vector
  12208. * ends up disabled forever.
  12209. */
  12210. clear_bit(__I40E_DOWN, pf->state);
  12211. /* In case of MSIX we are going to setup the misc vector right here
  12212. * to handle admin queue events etc. In case of legacy and MSI
  12213. * the misc functionality and queue processing is combined in
  12214. * the same vector and that gets setup at open.
  12215. */
  12216. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  12217. err = i40e_setup_misc_vector(pf);
  12218. if (err) {
  12219. dev_info(&pdev->dev,
  12220. "setup of misc vector failed: %d\n", err);
  12221. goto err_vsis;
  12222. }
  12223. }
  12224. #ifdef CONFIG_PCI_IOV
  12225. /* prep for VF support */
  12226. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12227. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12228. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12229. /* disable link interrupts for VFs */
  12230. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  12231. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  12232. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  12233. i40e_flush(hw);
  12234. if (pci_num_vf(pdev)) {
  12235. dev_info(&pdev->dev,
  12236. "Active VFs found, allocating resources.\n");
  12237. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  12238. if (err)
  12239. dev_info(&pdev->dev,
  12240. "Error %d allocating resources for existing VFs\n",
  12241. err);
  12242. }
  12243. }
  12244. #endif /* CONFIG_PCI_IOV */
  12245. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12246. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  12247. pf->num_iwarp_msix,
  12248. I40E_IWARP_IRQ_PILE_ID);
  12249. if (pf->iwarp_base_vector < 0) {
  12250. dev_info(&pdev->dev,
  12251. "failed to get tracking for %d vectors for IWARP err=%d\n",
  12252. pf->num_iwarp_msix, pf->iwarp_base_vector);
  12253. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  12254. }
  12255. }
  12256. i40e_dbg_pf_init(pf);
  12257. /* tell the firmware that we're starting */
  12258. i40e_send_version(pf);
  12259. /* since everything's happy, start the service_task timer */
  12260. mod_timer(&pf->service_timer,
  12261. round_jiffies(jiffies + pf->service_timer_period));
  12262. /* add this PF to client device list and launch a client service task */
  12263. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12264. err = i40e_lan_add_device(pf);
  12265. if (err)
  12266. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  12267. err);
  12268. }
  12269. #define PCI_SPEED_SIZE 8
  12270. #define PCI_WIDTH_SIZE 8
  12271. /* Devices on the IOSF bus do not have this information
  12272. * and will report PCI Gen 1 x 1 by default so don't bother
  12273. * checking them.
  12274. */
  12275. if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
  12276. char speed[PCI_SPEED_SIZE] = "Unknown";
  12277. char width[PCI_WIDTH_SIZE] = "Unknown";
  12278. /* Get the negotiated link width and speed from PCI config
  12279. * space
  12280. */
  12281. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  12282. &link_status);
  12283. i40e_set_pci_config_data(hw, link_status);
  12284. switch (hw->bus.speed) {
  12285. case i40e_bus_speed_8000:
  12286. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  12287. case i40e_bus_speed_5000:
  12288. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  12289. case i40e_bus_speed_2500:
  12290. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  12291. default:
  12292. break;
  12293. }
  12294. switch (hw->bus.width) {
  12295. case i40e_bus_width_pcie_x8:
  12296. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  12297. case i40e_bus_width_pcie_x4:
  12298. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  12299. case i40e_bus_width_pcie_x2:
  12300. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  12301. case i40e_bus_width_pcie_x1:
  12302. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  12303. default:
  12304. break;
  12305. }
  12306. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  12307. speed, width);
  12308. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  12309. hw->bus.speed < i40e_bus_speed_8000) {
  12310. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  12311. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  12312. }
  12313. }
  12314. /* get the requested speeds from the fw */
  12315. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  12316. if (err)
  12317. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  12318. i40e_stat_str(&pf->hw, err),
  12319. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12320. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  12321. /* get the supported phy types from the fw */
  12322. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  12323. if (err)
  12324. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  12325. i40e_stat_str(&pf->hw, err),
  12326. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12327. /* Add a filter to drop all Flow control frames from any VSI from being
  12328. * transmitted. By doing so we stop a malicious VF from sending out
  12329. * PAUSE or PFC frames and potentially controlling traffic for other
  12330. * PF/VF VSIs.
  12331. * The FW can still send Flow control frames if enabled.
  12332. */
  12333. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  12334. pf->main_vsi_seid);
  12335. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  12336. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  12337. pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
  12338. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  12339. pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
  12340. /* print a string summarizing features */
  12341. i40e_print_features(pf);
  12342. return 0;
  12343. /* Unwind what we've done if something failed in the setup */
  12344. err_vsis:
  12345. set_bit(__I40E_DOWN, pf->state);
  12346. i40e_clear_interrupt_scheme(pf);
  12347. kfree(pf->vsi);
  12348. err_switch_setup:
  12349. i40e_reset_interrupt_capability(pf);
  12350. del_timer_sync(&pf->service_timer);
  12351. err_mac_addr:
  12352. err_configure_lan_hmc:
  12353. (void)i40e_shutdown_lan_hmc(hw);
  12354. err_init_lan_hmc:
  12355. kfree(pf->qp_pile);
  12356. err_sw_init:
  12357. err_adminq_setup:
  12358. err_pf_reset:
  12359. iounmap(hw->hw_addr);
  12360. err_ioremap:
  12361. kfree(pf);
  12362. err_pf_alloc:
  12363. pci_disable_pcie_error_reporting(pdev);
  12364. pci_release_mem_regions(pdev);
  12365. err_pci_reg:
  12366. err_dma:
  12367. pci_disable_device(pdev);
  12368. return err;
  12369. }
  12370. /**
  12371. * i40e_remove - Device removal routine
  12372. * @pdev: PCI device information struct
  12373. *
  12374. * i40e_remove is called by the PCI subsystem to alert the driver
  12375. * that is should release a PCI device. This could be caused by a
  12376. * Hot-Plug event, or because the driver is going to be removed from
  12377. * memory.
  12378. **/
  12379. static void i40e_remove(struct pci_dev *pdev)
  12380. {
  12381. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12382. struct i40e_hw *hw = &pf->hw;
  12383. i40e_status ret_code;
  12384. int i;
  12385. i40e_dbg_pf_exit(pf);
  12386. i40e_ptp_stop(pf);
  12387. /* Disable RSS in hw */
  12388. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  12389. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  12390. /* no more scheduling of any task */
  12391. set_bit(__I40E_SUSPENDED, pf->state);
  12392. set_bit(__I40E_DOWN, pf->state);
  12393. if (pf->service_timer.function)
  12394. del_timer_sync(&pf->service_timer);
  12395. if (pf->service_task.func)
  12396. cancel_work_sync(&pf->service_task);
  12397. /* Client close must be called explicitly here because the timer
  12398. * has been stopped.
  12399. */
  12400. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12401. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  12402. i40e_free_vfs(pf);
  12403. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  12404. }
  12405. i40e_fdir_teardown(pf);
  12406. /* If there is a switch structure or any orphans, remove them.
  12407. * This will leave only the PF's VSI remaining.
  12408. */
  12409. for (i = 0; i < I40E_MAX_VEB; i++) {
  12410. if (!pf->veb[i])
  12411. continue;
  12412. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  12413. pf->veb[i]->uplink_seid == 0)
  12414. i40e_switch_branch_release(pf->veb[i]);
  12415. }
  12416. /* Now we can shutdown the PF's VSI, just before we kill
  12417. * adminq and hmc.
  12418. */
  12419. if (pf->vsi[pf->lan_vsi])
  12420. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  12421. i40e_cloud_filter_exit(pf);
  12422. /* remove attached clients */
  12423. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12424. ret_code = i40e_lan_del_device(pf);
  12425. if (ret_code)
  12426. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  12427. ret_code);
  12428. }
  12429. /* shutdown and destroy the HMC */
  12430. if (hw->hmc.hmc_obj) {
  12431. ret_code = i40e_shutdown_lan_hmc(hw);
  12432. if (ret_code)
  12433. dev_warn(&pdev->dev,
  12434. "Failed to destroy the HMC resources: %d\n",
  12435. ret_code);
  12436. }
  12437. /* shutdown the adminq */
  12438. i40e_shutdown_adminq(hw);
  12439. /* destroy the locks only once, here */
  12440. mutex_destroy(&hw->aq.arq_mutex);
  12441. mutex_destroy(&hw->aq.asq_mutex);
  12442. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  12443. rtnl_lock();
  12444. i40e_clear_interrupt_scheme(pf);
  12445. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12446. if (pf->vsi[i]) {
  12447. i40e_vsi_clear_rings(pf->vsi[i]);
  12448. i40e_vsi_clear(pf->vsi[i]);
  12449. pf->vsi[i] = NULL;
  12450. }
  12451. }
  12452. rtnl_unlock();
  12453. for (i = 0; i < I40E_MAX_VEB; i++) {
  12454. kfree(pf->veb[i]);
  12455. pf->veb[i] = NULL;
  12456. }
  12457. kfree(pf->qp_pile);
  12458. kfree(pf->vsi);
  12459. iounmap(hw->hw_addr);
  12460. kfree(pf);
  12461. pci_release_mem_regions(pdev);
  12462. pci_disable_pcie_error_reporting(pdev);
  12463. pci_disable_device(pdev);
  12464. }
  12465. /**
  12466. * i40e_pci_error_detected - warning that something funky happened in PCI land
  12467. * @pdev: PCI device information struct
  12468. * @error: the type of PCI error
  12469. *
  12470. * Called to warn that something happened and the error handling steps
  12471. * are in progress. Allows the driver to quiesce things, be ready for
  12472. * remediation.
  12473. **/
  12474. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  12475. enum pci_channel_state error)
  12476. {
  12477. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12478. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  12479. if (!pf) {
  12480. dev_info(&pdev->dev,
  12481. "Cannot recover - error happened during device probe\n");
  12482. return PCI_ERS_RESULT_DISCONNECT;
  12483. }
  12484. /* shutdown all operations */
  12485. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12486. i40e_prep_for_reset(pf, false);
  12487. /* Request a slot reset */
  12488. return PCI_ERS_RESULT_NEED_RESET;
  12489. }
  12490. /**
  12491. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  12492. * @pdev: PCI device information struct
  12493. *
  12494. * Called to find if the driver can work with the device now that
  12495. * the pci slot has been reset. If a basic connection seems good
  12496. * (registers are readable and have sane content) then return a
  12497. * happy little PCI_ERS_RESULT_xxx.
  12498. **/
  12499. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  12500. {
  12501. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12502. pci_ers_result_t result;
  12503. int err;
  12504. u32 reg;
  12505. dev_dbg(&pdev->dev, "%s\n", __func__);
  12506. if (pci_enable_device_mem(pdev)) {
  12507. dev_info(&pdev->dev,
  12508. "Cannot re-enable PCI device after reset.\n");
  12509. result = PCI_ERS_RESULT_DISCONNECT;
  12510. } else {
  12511. pci_set_master(pdev);
  12512. pci_restore_state(pdev);
  12513. pci_save_state(pdev);
  12514. pci_wake_from_d3(pdev, false);
  12515. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  12516. if (reg == 0)
  12517. result = PCI_ERS_RESULT_RECOVERED;
  12518. else
  12519. result = PCI_ERS_RESULT_DISCONNECT;
  12520. }
  12521. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  12522. if (err) {
  12523. dev_info(&pdev->dev,
  12524. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  12525. err);
  12526. /* non-fatal, continue */
  12527. }
  12528. return result;
  12529. }
  12530. /**
  12531. * i40e_pci_error_reset_prepare - prepare device driver for pci reset
  12532. * @pdev: PCI device information struct
  12533. */
  12534. static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
  12535. {
  12536. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12537. i40e_prep_for_reset(pf, false);
  12538. }
  12539. /**
  12540. * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
  12541. * @pdev: PCI device information struct
  12542. */
  12543. static void i40e_pci_error_reset_done(struct pci_dev *pdev)
  12544. {
  12545. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12546. i40e_reset_and_rebuild(pf, false, false);
  12547. }
  12548. /**
  12549. * i40e_pci_error_resume - restart operations after PCI error recovery
  12550. * @pdev: PCI device information struct
  12551. *
  12552. * Called to allow the driver to bring things back up after PCI error
  12553. * and/or reset recovery has finished.
  12554. **/
  12555. static void i40e_pci_error_resume(struct pci_dev *pdev)
  12556. {
  12557. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12558. dev_dbg(&pdev->dev, "%s\n", __func__);
  12559. if (test_bit(__I40E_SUSPENDED, pf->state))
  12560. return;
  12561. i40e_handle_reset_warning(pf, false);
  12562. }
  12563. /**
  12564. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  12565. * using the mac_address_write admin q function
  12566. * @pf: pointer to i40e_pf struct
  12567. **/
  12568. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  12569. {
  12570. struct i40e_hw *hw = &pf->hw;
  12571. i40e_status ret;
  12572. u8 mac_addr[6];
  12573. u16 flags = 0;
  12574. /* Get current MAC address in case it's an LAA */
  12575. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  12576. ether_addr_copy(mac_addr,
  12577. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  12578. } else {
  12579. dev_err(&pf->pdev->dev,
  12580. "Failed to retrieve MAC address; using default\n");
  12581. ether_addr_copy(mac_addr, hw->mac.addr);
  12582. }
  12583. /* The FW expects the mac address write cmd to first be called with
  12584. * one of these flags before calling it again with the multicast
  12585. * enable flags.
  12586. */
  12587. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  12588. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  12589. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  12590. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12591. if (ret) {
  12592. dev_err(&pf->pdev->dev,
  12593. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  12594. return;
  12595. }
  12596. flags = I40E_AQC_MC_MAG_EN
  12597. | I40E_AQC_WOL_PRESERVE_ON_PFR
  12598. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  12599. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12600. if (ret)
  12601. dev_err(&pf->pdev->dev,
  12602. "Failed to enable Multicast Magic Packet wake up\n");
  12603. }
  12604. /**
  12605. * i40e_shutdown - PCI callback for shutting down
  12606. * @pdev: PCI device information struct
  12607. **/
  12608. static void i40e_shutdown(struct pci_dev *pdev)
  12609. {
  12610. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12611. struct i40e_hw *hw = &pf->hw;
  12612. set_bit(__I40E_SUSPENDED, pf->state);
  12613. set_bit(__I40E_DOWN, pf->state);
  12614. del_timer_sync(&pf->service_timer);
  12615. cancel_work_sync(&pf->service_task);
  12616. i40e_cloud_filter_exit(pf);
  12617. i40e_fdir_teardown(pf);
  12618. /* Client close must be called explicitly here because the timer
  12619. * has been stopped.
  12620. */
  12621. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12622. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12623. i40e_enable_mc_magic_wake(pf);
  12624. i40e_prep_for_reset(pf, false);
  12625. wr32(hw, I40E_PFPM_APM,
  12626. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12627. wr32(hw, I40E_PFPM_WUFC,
  12628. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12629. /* Since we're going to destroy queues during the
  12630. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  12631. * whole section
  12632. */
  12633. rtnl_lock();
  12634. i40e_clear_interrupt_scheme(pf);
  12635. rtnl_unlock();
  12636. if (system_state == SYSTEM_POWER_OFF) {
  12637. pci_wake_from_d3(pdev, pf->wol_en);
  12638. pci_set_power_state(pdev, PCI_D3hot);
  12639. }
  12640. }
  12641. /**
  12642. * i40e_suspend - PM callback for moving to D3
  12643. * @dev: generic device information structure
  12644. **/
  12645. static int __maybe_unused i40e_suspend(struct device *dev)
  12646. {
  12647. struct pci_dev *pdev = to_pci_dev(dev);
  12648. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12649. struct i40e_hw *hw = &pf->hw;
  12650. /* If we're already suspended, then there is nothing to do */
  12651. if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
  12652. return 0;
  12653. set_bit(__I40E_DOWN, pf->state);
  12654. /* Ensure service task will not be running */
  12655. del_timer_sync(&pf->service_timer);
  12656. cancel_work_sync(&pf->service_task);
  12657. /* Client close must be called explicitly here because the timer
  12658. * has been stopped.
  12659. */
  12660. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12661. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12662. i40e_enable_mc_magic_wake(pf);
  12663. /* Since we're going to destroy queues during the
  12664. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  12665. * whole section
  12666. */
  12667. rtnl_lock();
  12668. i40e_prep_for_reset(pf, true);
  12669. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12670. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12671. /* Clear the interrupt scheme and release our IRQs so that the system
  12672. * can safely hibernate even when there are a large number of CPUs.
  12673. * Otherwise hibernation might fail when mapping all the vectors back
  12674. * to CPU0.
  12675. */
  12676. i40e_clear_interrupt_scheme(pf);
  12677. rtnl_unlock();
  12678. return 0;
  12679. }
  12680. /**
  12681. * i40e_resume - PM callback for waking up from D3
  12682. * @dev: generic device information structure
  12683. **/
  12684. static int __maybe_unused i40e_resume(struct device *dev)
  12685. {
  12686. struct pci_dev *pdev = to_pci_dev(dev);
  12687. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12688. int err;
  12689. /* If we're not suspended, then there is nothing to do */
  12690. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12691. return 0;
  12692. /* We need to hold the RTNL lock prior to restoring interrupt schemes,
  12693. * since we're going to be restoring queues
  12694. */
  12695. rtnl_lock();
  12696. /* We cleared the interrupt scheme when we suspended, so we need to
  12697. * restore it now to resume device functionality.
  12698. */
  12699. err = i40e_restore_interrupt_scheme(pf);
  12700. if (err) {
  12701. dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
  12702. err);
  12703. }
  12704. clear_bit(__I40E_DOWN, pf->state);
  12705. i40e_reset_and_rebuild(pf, false, true);
  12706. rtnl_unlock();
  12707. /* Clear suspended state last after everything is recovered */
  12708. clear_bit(__I40E_SUSPENDED, pf->state);
  12709. /* Restart the service task */
  12710. mod_timer(&pf->service_timer,
  12711. round_jiffies(jiffies + pf->service_timer_period));
  12712. return 0;
  12713. }
  12714. static const struct pci_error_handlers i40e_err_handler = {
  12715. .error_detected = i40e_pci_error_detected,
  12716. .slot_reset = i40e_pci_error_slot_reset,
  12717. .reset_prepare = i40e_pci_error_reset_prepare,
  12718. .reset_done = i40e_pci_error_reset_done,
  12719. .resume = i40e_pci_error_resume,
  12720. };
  12721. static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
  12722. static struct pci_driver i40e_driver = {
  12723. .name = i40e_driver_name,
  12724. .id_table = i40e_pci_tbl,
  12725. .probe = i40e_probe,
  12726. .remove = i40e_remove,
  12727. .driver = {
  12728. .pm = &i40e_pm_ops,
  12729. },
  12730. .shutdown = i40e_shutdown,
  12731. .err_handler = &i40e_err_handler,
  12732. .sriov_configure = i40e_pci_sriov_configure,
  12733. };
  12734. /**
  12735. * i40e_init_module - Driver registration routine
  12736. *
  12737. * i40e_init_module is the first routine called when the driver is
  12738. * loaded. All it does is register with the PCI subsystem.
  12739. **/
  12740. static int __init i40e_init_module(void)
  12741. {
  12742. pr_info("%s: %s - version %s\n", i40e_driver_name,
  12743. i40e_driver_string, i40e_driver_version_str);
  12744. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  12745. /* There is no need to throttle the number of active tasks because
  12746. * each device limits its own task using a state bit for scheduling
  12747. * the service task, and the device tasks do not interfere with each
  12748. * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
  12749. * since we need to be able to guarantee forward progress even under
  12750. * memory pressure.
  12751. */
  12752. i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
  12753. if (!i40e_wq) {
  12754. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  12755. return -ENOMEM;
  12756. }
  12757. i40e_dbg_init();
  12758. return pci_register_driver(&i40e_driver);
  12759. }
  12760. module_init(i40e_init_module);
  12761. /**
  12762. * i40e_exit_module - Driver exit cleanup routine
  12763. *
  12764. * i40e_exit_module is called just before the driver is removed
  12765. * from memory.
  12766. **/
  12767. static void __exit i40e_exit_module(void)
  12768. {
  12769. pci_unregister_driver(&i40e_driver);
  12770. destroy_workqueue(i40e_wq);
  12771. i40e_dbg_exit();
  12772. }
  12773. module_exit(i40e_exit_module);