i40e_main.c 409 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879
  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 "i40e_xsk.h"
  11. #include <net/udp_tunnel.h>
  12. #include <net/xdp_sock.h>
  13. /* All i40e tracepoints are defined by the include below, which
  14. * must be included exactly once across the whole kernel with
  15. * CREATE_TRACE_POINTS defined
  16. */
  17. #define CREATE_TRACE_POINTS
  18. #include "i40e_trace.h"
  19. const char i40e_driver_name[] = "i40e";
  20. static const char i40e_driver_string[] =
  21. "Intel(R) Ethernet Connection XL710 Network Driver";
  22. #define DRV_KERN "-k"
  23. #define DRV_VERSION_MAJOR 2
  24. #define DRV_VERSION_MINOR 3
  25. #define DRV_VERSION_BUILD 2
  26. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  27. __stringify(DRV_VERSION_MINOR) "." \
  28. __stringify(DRV_VERSION_BUILD) DRV_KERN
  29. const char i40e_driver_version_str[] = DRV_VERSION;
  30. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  31. /* a bit of forward declarations */
  32. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  33. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
  34. static int i40e_add_vsi(struct i40e_vsi *vsi);
  35. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  36. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  37. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  38. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  39. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  40. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
  41. static int i40e_reset(struct i40e_pf *pf);
  42. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  43. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  44. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  45. static int i40e_get_capabilities(struct i40e_pf *pf,
  46. enum i40e_admin_queue_opc list_type);
  47. /* i40e_pci_tbl - PCI Device ID Table
  48. *
  49. * Last entry must be all 0s
  50. *
  51. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  52. * Class, Class Mask, private data (not used) }
  53. */
  54. static const struct pci_device_id i40e_pci_tbl[] = {
  55. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  56. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  57. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  58. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  59. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  60. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  61. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  62. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  63. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  71. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  72. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  73. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  74. /* required last entry */
  75. {0, }
  76. };
  77. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  78. #define I40E_MAX_VF_COUNT 128
  79. static int debug = -1;
  80. module_param(debug, uint, 0);
  81. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  82. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  83. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  84. MODULE_LICENSE("GPL v2");
  85. MODULE_VERSION(DRV_VERSION);
  86. static struct workqueue_struct *i40e_wq;
  87. /**
  88. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  89. * @hw: pointer to the HW structure
  90. * @mem: ptr to mem struct to fill out
  91. * @size: size of memory requested
  92. * @alignment: what to align the allocation to
  93. **/
  94. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  95. u64 size, u32 alignment)
  96. {
  97. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  98. mem->size = ALIGN(size, alignment);
  99. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  100. &mem->pa, GFP_KERNEL);
  101. if (!mem->va)
  102. return -ENOMEM;
  103. return 0;
  104. }
  105. /**
  106. * i40e_free_dma_mem_d - OS specific memory free for shared code
  107. * @hw: pointer to the HW structure
  108. * @mem: ptr to mem struct to free
  109. **/
  110. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  111. {
  112. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  113. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  114. mem->va = NULL;
  115. mem->pa = 0;
  116. mem->size = 0;
  117. return 0;
  118. }
  119. /**
  120. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  121. * @hw: pointer to the HW structure
  122. * @mem: ptr to mem struct to fill out
  123. * @size: size of memory requested
  124. **/
  125. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  126. u32 size)
  127. {
  128. mem->size = size;
  129. mem->va = kzalloc(size, GFP_KERNEL);
  130. if (!mem->va)
  131. return -ENOMEM;
  132. return 0;
  133. }
  134. /**
  135. * i40e_free_virt_mem_d - OS specific memory free for shared code
  136. * @hw: pointer to the HW structure
  137. * @mem: ptr to mem struct to free
  138. **/
  139. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  140. {
  141. /* it's ok to kfree a NULL pointer */
  142. kfree(mem->va);
  143. mem->va = NULL;
  144. mem->size = 0;
  145. return 0;
  146. }
  147. /**
  148. * i40e_get_lump - find a lump of free generic resource
  149. * @pf: board private structure
  150. * @pile: the pile of resource to search
  151. * @needed: the number of items needed
  152. * @id: an owner id to stick on the items assigned
  153. *
  154. * Returns the base item index of the lump, or negative for error
  155. *
  156. * The search_hint trick and lack of advanced fit-finding only work
  157. * because we're highly likely to have all the same size lump requests.
  158. * Linear search time and any fragmentation should be minimal.
  159. **/
  160. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  161. u16 needed, u16 id)
  162. {
  163. int ret = -ENOMEM;
  164. int i, j;
  165. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  166. dev_info(&pf->pdev->dev,
  167. "param err: pile=%s needed=%d id=0x%04x\n",
  168. pile ? "<valid>" : "<null>", needed, id);
  169. return -EINVAL;
  170. }
  171. /* start the linear search with an imperfect hint */
  172. i = pile->search_hint;
  173. while (i < pile->num_entries) {
  174. /* skip already allocated entries */
  175. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  176. i++;
  177. continue;
  178. }
  179. /* do we have enough in this lump? */
  180. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  181. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  182. break;
  183. }
  184. if (j == needed) {
  185. /* there was enough, so assign it to the requestor */
  186. for (j = 0; j < needed; j++)
  187. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  188. ret = i;
  189. pile->search_hint = i + j;
  190. break;
  191. }
  192. /* not enough, so skip over it and continue looking */
  193. i += j;
  194. }
  195. return ret;
  196. }
  197. /**
  198. * i40e_put_lump - return a lump of generic resource
  199. * @pile: the pile of resource to search
  200. * @index: the base item index
  201. * @id: the owner id of the items assigned
  202. *
  203. * Returns the count of items in the lump
  204. **/
  205. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  206. {
  207. int valid_id = (id | I40E_PILE_VALID_BIT);
  208. int count = 0;
  209. int i;
  210. if (!pile || index >= pile->num_entries)
  211. return -EINVAL;
  212. for (i = index;
  213. i < pile->num_entries && pile->list[i] == valid_id;
  214. i++) {
  215. pile->list[i] = 0;
  216. count++;
  217. }
  218. if (count && index < pile->search_hint)
  219. pile->search_hint = index;
  220. return count;
  221. }
  222. /**
  223. * i40e_find_vsi_from_id - searches for the vsi with the given id
  224. * @pf: the pf structure to search for the vsi
  225. * @id: id of the vsi it is searching for
  226. **/
  227. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  228. {
  229. int i;
  230. for (i = 0; i < pf->num_alloc_vsi; i++)
  231. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  232. return pf->vsi[i];
  233. return NULL;
  234. }
  235. /**
  236. * i40e_service_event_schedule - Schedule the service task to wake up
  237. * @pf: board private structure
  238. *
  239. * If not already scheduled, this puts the task into the work queue
  240. **/
  241. void i40e_service_event_schedule(struct i40e_pf *pf)
  242. {
  243. if (!test_bit(__I40E_DOWN, pf->state) &&
  244. !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  245. queue_work(i40e_wq, &pf->service_task);
  246. }
  247. /**
  248. * i40e_tx_timeout - Respond to a Tx Hang
  249. * @netdev: network interface device structure
  250. *
  251. * If any port has noticed a Tx timeout, it is likely that the whole
  252. * device is munged, not just the one netdev port, so go for the full
  253. * reset.
  254. **/
  255. static void i40e_tx_timeout(struct net_device *netdev)
  256. {
  257. struct i40e_netdev_priv *np = netdev_priv(netdev);
  258. struct i40e_vsi *vsi = np->vsi;
  259. struct i40e_pf *pf = vsi->back;
  260. struct i40e_ring *tx_ring = NULL;
  261. unsigned int i, hung_queue = 0;
  262. u32 head, val;
  263. pf->tx_timeout_count++;
  264. /* find the stopped queue the same way the stack does */
  265. for (i = 0; i < netdev->num_tx_queues; i++) {
  266. struct netdev_queue *q;
  267. unsigned long trans_start;
  268. q = netdev_get_tx_queue(netdev, i);
  269. trans_start = q->trans_start;
  270. if (netif_xmit_stopped(q) &&
  271. time_after(jiffies,
  272. (trans_start + netdev->watchdog_timeo))) {
  273. hung_queue = i;
  274. break;
  275. }
  276. }
  277. if (i == netdev->num_tx_queues) {
  278. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  279. } else {
  280. /* now that we have an index, find the tx_ring struct */
  281. for (i = 0; i < vsi->num_queue_pairs; i++) {
  282. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  283. if (hung_queue ==
  284. vsi->tx_rings[i]->queue_index) {
  285. tx_ring = vsi->tx_rings[i];
  286. break;
  287. }
  288. }
  289. }
  290. }
  291. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  292. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  293. else if (time_before(jiffies,
  294. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  295. return; /* don't do any new action before the next timeout */
  296. if (tx_ring) {
  297. head = i40e_get_head(tx_ring);
  298. /* Read interrupt register */
  299. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  300. val = rd32(&pf->hw,
  301. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  302. tx_ring->vsi->base_vector - 1));
  303. else
  304. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  305. 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",
  306. vsi->seid, hung_queue, tx_ring->next_to_clean,
  307. head, tx_ring->next_to_use,
  308. readl(tx_ring->tail), val);
  309. }
  310. pf->tx_timeout_last_recovery = jiffies;
  311. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  312. pf->tx_timeout_recovery_level, hung_queue);
  313. switch (pf->tx_timeout_recovery_level) {
  314. case 1:
  315. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  316. break;
  317. case 2:
  318. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  319. break;
  320. case 3:
  321. set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  322. break;
  323. default:
  324. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  325. break;
  326. }
  327. i40e_service_event_schedule(pf);
  328. pf->tx_timeout_recovery_level++;
  329. }
  330. /**
  331. * i40e_get_vsi_stats_struct - Get System Network Statistics
  332. * @vsi: the VSI we care about
  333. *
  334. * Returns the address of the device statistics structure.
  335. * The statistics are actually updated from the service task.
  336. **/
  337. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  338. {
  339. return &vsi->net_stats;
  340. }
  341. /**
  342. * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
  343. * @ring: Tx ring to get statistics from
  344. * @stats: statistics entry to be updated
  345. **/
  346. static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
  347. struct rtnl_link_stats64 *stats)
  348. {
  349. u64 bytes, packets;
  350. unsigned int start;
  351. do {
  352. start = u64_stats_fetch_begin_irq(&ring->syncp);
  353. packets = ring->stats.packets;
  354. bytes = ring->stats.bytes;
  355. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  356. stats->tx_packets += packets;
  357. stats->tx_bytes += bytes;
  358. }
  359. /**
  360. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  361. * @netdev: network interface device structure
  362. * @stats: data structure to store statistics
  363. *
  364. * Returns the address of the device statistics structure.
  365. * The statistics are actually updated from the service task.
  366. **/
  367. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  368. struct rtnl_link_stats64 *stats)
  369. {
  370. struct i40e_netdev_priv *np = netdev_priv(netdev);
  371. struct i40e_vsi *vsi = np->vsi;
  372. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  373. struct i40e_ring *ring;
  374. int i;
  375. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  376. return;
  377. if (!vsi->tx_rings)
  378. return;
  379. rcu_read_lock();
  380. for (i = 0; i < vsi->num_queue_pairs; i++) {
  381. u64 bytes, packets;
  382. unsigned int start;
  383. ring = READ_ONCE(vsi->tx_rings[i]);
  384. if (!ring)
  385. continue;
  386. i40e_get_netdev_stats_struct_tx(ring, stats);
  387. if (i40e_enabled_xdp_vsi(vsi)) {
  388. ring++;
  389. i40e_get_netdev_stats_struct_tx(ring, stats);
  390. }
  391. ring++;
  392. do {
  393. start = u64_stats_fetch_begin_irq(&ring->syncp);
  394. packets = ring->stats.packets;
  395. bytes = ring->stats.bytes;
  396. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  397. stats->rx_packets += packets;
  398. stats->rx_bytes += bytes;
  399. }
  400. rcu_read_unlock();
  401. /* following stats updated by i40e_watchdog_subtask() */
  402. stats->multicast = vsi_stats->multicast;
  403. stats->tx_errors = vsi_stats->tx_errors;
  404. stats->tx_dropped = vsi_stats->tx_dropped;
  405. stats->rx_errors = vsi_stats->rx_errors;
  406. stats->rx_dropped = vsi_stats->rx_dropped;
  407. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  408. stats->rx_length_errors = vsi_stats->rx_length_errors;
  409. }
  410. /**
  411. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  412. * @vsi: the VSI to have its stats reset
  413. **/
  414. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  415. {
  416. struct rtnl_link_stats64 *ns;
  417. int i;
  418. if (!vsi)
  419. return;
  420. ns = i40e_get_vsi_stats_struct(vsi);
  421. memset(ns, 0, sizeof(*ns));
  422. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  423. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  424. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  425. if (vsi->rx_rings && vsi->rx_rings[0]) {
  426. for (i = 0; i < vsi->num_queue_pairs; i++) {
  427. memset(&vsi->rx_rings[i]->stats, 0,
  428. sizeof(vsi->rx_rings[i]->stats));
  429. memset(&vsi->rx_rings[i]->rx_stats, 0,
  430. sizeof(vsi->rx_rings[i]->rx_stats));
  431. memset(&vsi->tx_rings[i]->stats, 0,
  432. sizeof(vsi->tx_rings[i]->stats));
  433. memset(&vsi->tx_rings[i]->tx_stats, 0,
  434. sizeof(vsi->tx_rings[i]->tx_stats));
  435. }
  436. }
  437. vsi->stat_offsets_loaded = false;
  438. }
  439. /**
  440. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  441. * @pf: the PF to be reset
  442. **/
  443. void i40e_pf_reset_stats(struct i40e_pf *pf)
  444. {
  445. int i;
  446. memset(&pf->stats, 0, sizeof(pf->stats));
  447. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  448. pf->stat_offsets_loaded = false;
  449. for (i = 0; i < I40E_MAX_VEB; i++) {
  450. if (pf->veb[i]) {
  451. memset(&pf->veb[i]->stats, 0,
  452. sizeof(pf->veb[i]->stats));
  453. memset(&pf->veb[i]->stats_offsets, 0,
  454. sizeof(pf->veb[i]->stats_offsets));
  455. pf->veb[i]->stat_offsets_loaded = false;
  456. }
  457. }
  458. pf->hw_csum_rx_error = 0;
  459. }
  460. /**
  461. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  462. * @hw: ptr to the hardware info
  463. * @hireg: the high 32 bit reg to read
  464. * @loreg: the low 32 bit reg to read
  465. * @offset_loaded: has the initial offset been loaded yet
  466. * @offset: ptr to current offset value
  467. * @stat: ptr to the stat
  468. *
  469. * Since the device stats are not reset at PFReset, they likely will not
  470. * be zeroed when the driver starts. We'll save the first values read
  471. * and use them as offsets to be subtracted from the raw values in order
  472. * to report stats that count from zero. In the process, we also manage
  473. * the potential roll-over.
  474. **/
  475. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  476. bool offset_loaded, u64 *offset, u64 *stat)
  477. {
  478. u64 new_data;
  479. if (hw->device_id == I40E_DEV_ID_QEMU) {
  480. new_data = rd32(hw, loreg);
  481. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  482. } else {
  483. new_data = rd64(hw, loreg);
  484. }
  485. if (!offset_loaded)
  486. *offset = new_data;
  487. if (likely(new_data >= *offset))
  488. *stat = new_data - *offset;
  489. else
  490. *stat = (new_data + BIT_ULL(48)) - *offset;
  491. *stat &= 0xFFFFFFFFFFFFULL;
  492. }
  493. /**
  494. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  495. * @hw: ptr to the hardware info
  496. * @reg: the hw reg to read
  497. * @offset_loaded: has the initial offset been loaded yet
  498. * @offset: ptr to current offset value
  499. * @stat: ptr to the stat
  500. **/
  501. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  502. bool offset_loaded, u64 *offset, u64 *stat)
  503. {
  504. u32 new_data;
  505. new_data = rd32(hw, reg);
  506. if (!offset_loaded)
  507. *offset = new_data;
  508. if (likely(new_data >= *offset))
  509. *stat = (u32)(new_data - *offset);
  510. else
  511. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  512. }
  513. /**
  514. * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
  515. * @hw: ptr to the hardware info
  516. * @reg: the hw reg to read and clear
  517. * @stat: ptr to the stat
  518. **/
  519. static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
  520. {
  521. u32 new_data = rd32(hw, reg);
  522. wr32(hw, reg, 1); /* must write a nonzero value to clear register */
  523. *stat += new_data;
  524. }
  525. /**
  526. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  527. * @vsi: the VSI to be updated
  528. **/
  529. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  530. {
  531. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  532. struct i40e_pf *pf = vsi->back;
  533. struct i40e_hw *hw = &pf->hw;
  534. struct i40e_eth_stats *oes;
  535. struct i40e_eth_stats *es; /* device's eth stats */
  536. es = &vsi->eth_stats;
  537. oes = &vsi->eth_stats_offsets;
  538. /* Gather up the stats that the hw collects */
  539. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  540. vsi->stat_offsets_loaded,
  541. &oes->tx_errors, &es->tx_errors);
  542. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  543. vsi->stat_offsets_loaded,
  544. &oes->rx_discards, &es->rx_discards);
  545. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  546. vsi->stat_offsets_loaded,
  547. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  548. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  549. vsi->stat_offsets_loaded,
  550. &oes->tx_errors, &es->tx_errors);
  551. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  552. I40E_GLV_GORCL(stat_idx),
  553. vsi->stat_offsets_loaded,
  554. &oes->rx_bytes, &es->rx_bytes);
  555. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  556. I40E_GLV_UPRCL(stat_idx),
  557. vsi->stat_offsets_loaded,
  558. &oes->rx_unicast, &es->rx_unicast);
  559. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  560. I40E_GLV_MPRCL(stat_idx),
  561. vsi->stat_offsets_loaded,
  562. &oes->rx_multicast, &es->rx_multicast);
  563. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  564. I40E_GLV_BPRCL(stat_idx),
  565. vsi->stat_offsets_loaded,
  566. &oes->rx_broadcast, &es->rx_broadcast);
  567. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  568. I40E_GLV_GOTCL(stat_idx),
  569. vsi->stat_offsets_loaded,
  570. &oes->tx_bytes, &es->tx_bytes);
  571. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  572. I40E_GLV_UPTCL(stat_idx),
  573. vsi->stat_offsets_loaded,
  574. &oes->tx_unicast, &es->tx_unicast);
  575. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  576. I40E_GLV_MPTCL(stat_idx),
  577. vsi->stat_offsets_loaded,
  578. &oes->tx_multicast, &es->tx_multicast);
  579. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  580. I40E_GLV_BPTCL(stat_idx),
  581. vsi->stat_offsets_loaded,
  582. &oes->tx_broadcast, &es->tx_broadcast);
  583. vsi->stat_offsets_loaded = true;
  584. }
  585. /**
  586. * i40e_update_veb_stats - Update Switch component statistics
  587. * @veb: the VEB being updated
  588. **/
  589. static void i40e_update_veb_stats(struct i40e_veb *veb)
  590. {
  591. struct i40e_pf *pf = veb->pf;
  592. struct i40e_hw *hw = &pf->hw;
  593. struct i40e_eth_stats *oes;
  594. struct i40e_eth_stats *es; /* device's eth stats */
  595. struct i40e_veb_tc_stats *veb_oes;
  596. struct i40e_veb_tc_stats *veb_es;
  597. int i, idx = 0;
  598. idx = veb->stats_idx;
  599. es = &veb->stats;
  600. oes = &veb->stats_offsets;
  601. veb_es = &veb->tc_stats;
  602. veb_oes = &veb->tc_stats_offsets;
  603. /* Gather up the stats that the hw collects */
  604. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  605. veb->stat_offsets_loaded,
  606. &oes->tx_discards, &es->tx_discards);
  607. if (hw->revision_id > 0)
  608. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  609. veb->stat_offsets_loaded,
  610. &oes->rx_unknown_protocol,
  611. &es->rx_unknown_protocol);
  612. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  613. veb->stat_offsets_loaded,
  614. &oes->rx_bytes, &es->rx_bytes);
  615. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  616. veb->stat_offsets_loaded,
  617. &oes->rx_unicast, &es->rx_unicast);
  618. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  619. veb->stat_offsets_loaded,
  620. &oes->rx_multicast, &es->rx_multicast);
  621. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  622. veb->stat_offsets_loaded,
  623. &oes->rx_broadcast, &es->rx_broadcast);
  624. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  625. veb->stat_offsets_loaded,
  626. &oes->tx_bytes, &es->tx_bytes);
  627. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  628. veb->stat_offsets_loaded,
  629. &oes->tx_unicast, &es->tx_unicast);
  630. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  631. veb->stat_offsets_loaded,
  632. &oes->tx_multicast, &es->tx_multicast);
  633. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  634. veb->stat_offsets_loaded,
  635. &oes->tx_broadcast, &es->tx_broadcast);
  636. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  637. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  638. I40E_GLVEBTC_RPCL(i, idx),
  639. veb->stat_offsets_loaded,
  640. &veb_oes->tc_rx_packets[i],
  641. &veb_es->tc_rx_packets[i]);
  642. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  643. I40E_GLVEBTC_RBCL(i, idx),
  644. veb->stat_offsets_loaded,
  645. &veb_oes->tc_rx_bytes[i],
  646. &veb_es->tc_rx_bytes[i]);
  647. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  648. I40E_GLVEBTC_TPCL(i, idx),
  649. veb->stat_offsets_loaded,
  650. &veb_oes->tc_tx_packets[i],
  651. &veb_es->tc_tx_packets[i]);
  652. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  653. I40E_GLVEBTC_TBCL(i, idx),
  654. veb->stat_offsets_loaded,
  655. &veb_oes->tc_tx_bytes[i],
  656. &veb_es->tc_tx_bytes[i]);
  657. }
  658. veb->stat_offsets_loaded = true;
  659. }
  660. /**
  661. * i40e_update_vsi_stats - Update the vsi statistics counters.
  662. * @vsi: the VSI to be updated
  663. *
  664. * There are a few instances where we store the same stat in a
  665. * couple of different structs. This is partly because we have
  666. * the netdev stats that need to be filled out, which is slightly
  667. * different from the "eth_stats" defined by the chip and used in
  668. * VF communications. We sort it out here.
  669. **/
  670. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  671. {
  672. struct i40e_pf *pf = vsi->back;
  673. struct rtnl_link_stats64 *ons;
  674. struct rtnl_link_stats64 *ns; /* netdev stats */
  675. struct i40e_eth_stats *oes;
  676. struct i40e_eth_stats *es; /* device's eth stats */
  677. u32 tx_restart, tx_busy;
  678. struct i40e_ring *p;
  679. u32 rx_page, rx_buf;
  680. u64 bytes, packets;
  681. unsigned int start;
  682. u64 tx_linearize;
  683. u64 tx_force_wb;
  684. u64 rx_p, rx_b;
  685. u64 tx_p, tx_b;
  686. u16 q;
  687. if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  688. test_bit(__I40E_CONFIG_BUSY, pf->state))
  689. return;
  690. ns = i40e_get_vsi_stats_struct(vsi);
  691. ons = &vsi->net_stats_offsets;
  692. es = &vsi->eth_stats;
  693. oes = &vsi->eth_stats_offsets;
  694. /* Gather up the netdev and vsi stats that the driver collects
  695. * on the fly during packet processing
  696. */
  697. rx_b = rx_p = 0;
  698. tx_b = tx_p = 0;
  699. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  700. rx_page = 0;
  701. rx_buf = 0;
  702. rcu_read_lock();
  703. for (q = 0; q < vsi->num_queue_pairs; q++) {
  704. /* locate Tx ring */
  705. p = READ_ONCE(vsi->tx_rings[q]);
  706. do {
  707. start = u64_stats_fetch_begin_irq(&p->syncp);
  708. packets = p->stats.packets;
  709. bytes = p->stats.bytes;
  710. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  711. tx_b += bytes;
  712. tx_p += packets;
  713. tx_restart += p->tx_stats.restart_queue;
  714. tx_busy += p->tx_stats.tx_busy;
  715. tx_linearize += p->tx_stats.tx_linearize;
  716. tx_force_wb += p->tx_stats.tx_force_wb;
  717. /* Rx queue is part of the same block as Tx queue */
  718. p = &p[1];
  719. do {
  720. start = u64_stats_fetch_begin_irq(&p->syncp);
  721. packets = p->stats.packets;
  722. bytes = p->stats.bytes;
  723. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  724. rx_b += bytes;
  725. rx_p += packets;
  726. rx_buf += p->rx_stats.alloc_buff_failed;
  727. rx_page += p->rx_stats.alloc_page_failed;
  728. }
  729. rcu_read_unlock();
  730. vsi->tx_restart = tx_restart;
  731. vsi->tx_busy = tx_busy;
  732. vsi->tx_linearize = tx_linearize;
  733. vsi->tx_force_wb = tx_force_wb;
  734. vsi->rx_page_failed = rx_page;
  735. vsi->rx_buf_failed = rx_buf;
  736. ns->rx_packets = rx_p;
  737. ns->rx_bytes = rx_b;
  738. ns->tx_packets = tx_p;
  739. ns->tx_bytes = tx_b;
  740. /* update netdev stats from eth stats */
  741. i40e_update_eth_stats(vsi);
  742. ons->tx_errors = oes->tx_errors;
  743. ns->tx_errors = es->tx_errors;
  744. ons->multicast = oes->rx_multicast;
  745. ns->multicast = es->rx_multicast;
  746. ons->rx_dropped = oes->rx_discards;
  747. ns->rx_dropped = es->rx_discards;
  748. ons->tx_dropped = oes->tx_discards;
  749. ns->tx_dropped = es->tx_discards;
  750. /* pull in a couple PF stats if this is the main vsi */
  751. if (vsi == pf->vsi[pf->lan_vsi]) {
  752. ns->rx_crc_errors = pf->stats.crc_errors;
  753. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  754. ns->rx_length_errors = pf->stats.rx_length_errors;
  755. }
  756. }
  757. /**
  758. * i40e_update_pf_stats - Update the PF statistics counters.
  759. * @pf: the PF to be updated
  760. **/
  761. static void i40e_update_pf_stats(struct i40e_pf *pf)
  762. {
  763. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  764. struct i40e_hw_port_stats *nsd = &pf->stats;
  765. struct i40e_hw *hw = &pf->hw;
  766. u32 val;
  767. int i;
  768. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  769. I40E_GLPRT_GORCL(hw->port),
  770. pf->stat_offsets_loaded,
  771. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  772. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  773. I40E_GLPRT_GOTCL(hw->port),
  774. pf->stat_offsets_loaded,
  775. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  776. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  777. pf->stat_offsets_loaded,
  778. &osd->eth.rx_discards,
  779. &nsd->eth.rx_discards);
  780. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  781. I40E_GLPRT_UPRCL(hw->port),
  782. pf->stat_offsets_loaded,
  783. &osd->eth.rx_unicast,
  784. &nsd->eth.rx_unicast);
  785. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  786. I40E_GLPRT_MPRCL(hw->port),
  787. pf->stat_offsets_loaded,
  788. &osd->eth.rx_multicast,
  789. &nsd->eth.rx_multicast);
  790. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  791. I40E_GLPRT_BPRCL(hw->port),
  792. pf->stat_offsets_loaded,
  793. &osd->eth.rx_broadcast,
  794. &nsd->eth.rx_broadcast);
  795. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  796. I40E_GLPRT_UPTCL(hw->port),
  797. pf->stat_offsets_loaded,
  798. &osd->eth.tx_unicast,
  799. &nsd->eth.tx_unicast);
  800. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  801. I40E_GLPRT_MPTCL(hw->port),
  802. pf->stat_offsets_loaded,
  803. &osd->eth.tx_multicast,
  804. &nsd->eth.tx_multicast);
  805. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  806. I40E_GLPRT_BPTCL(hw->port),
  807. pf->stat_offsets_loaded,
  808. &osd->eth.tx_broadcast,
  809. &nsd->eth.tx_broadcast);
  810. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  811. pf->stat_offsets_loaded,
  812. &osd->tx_dropped_link_down,
  813. &nsd->tx_dropped_link_down);
  814. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  815. pf->stat_offsets_loaded,
  816. &osd->crc_errors, &nsd->crc_errors);
  817. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  818. pf->stat_offsets_loaded,
  819. &osd->illegal_bytes, &nsd->illegal_bytes);
  820. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  821. pf->stat_offsets_loaded,
  822. &osd->mac_local_faults,
  823. &nsd->mac_local_faults);
  824. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  825. pf->stat_offsets_loaded,
  826. &osd->mac_remote_faults,
  827. &nsd->mac_remote_faults);
  828. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  829. pf->stat_offsets_loaded,
  830. &osd->rx_length_errors,
  831. &nsd->rx_length_errors);
  832. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  833. pf->stat_offsets_loaded,
  834. &osd->link_xon_rx, &nsd->link_xon_rx);
  835. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  836. pf->stat_offsets_loaded,
  837. &osd->link_xon_tx, &nsd->link_xon_tx);
  838. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  839. pf->stat_offsets_loaded,
  840. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  841. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  842. pf->stat_offsets_loaded,
  843. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  844. for (i = 0; i < 8; i++) {
  845. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  846. pf->stat_offsets_loaded,
  847. &osd->priority_xoff_rx[i],
  848. &nsd->priority_xoff_rx[i]);
  849. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  850. pf->stat_offsets_loaded,
  851. &osd->priority_xon_rx[i],
  852. &nsd->priority_xon_rx[i]);
  853. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  854. pf->stat_offsets_loaded,
  855. &osd->priority_xon_tx[i],
  856. &nsd->priority_xon_tx[i]);
  857. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  858. pf->stat_offsets_loaded,
  859. &osd->priority_xoff_tx[i],
  860. &nsd->priority_xoff_tx[i]);
  861. i40e_stat_update32(hw,
  862. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  863. pf->stat_offsets_loaded,
  864. &osd->priority_xon_2_xoff[i],
  865. &nsd->priority_xon_2_xoff[i]);
  866. }
  867. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  868. I40E_GLPRT_PRC64L(hw->port),
  869. pf->stat_offsets_loaded,
  870. &osd->rx_size_64, &nsd->rx_size_64);
  871. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  872. I40E_GLPRT_PRC127L(hw->port),
  873. pf->stat_offsets_loaded,
  874. &osd->rx_size_127, &nsd->rx_size_127);
  875. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  876. I40E_GLPRT_PRC255L(hw->port),
  877. pf->stat_offsets_loaded,
  878. &osd->rx_size_255, &nsd->rx_size_255);
  879. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  880. I40E_GLPRT_PRC511L(hw->port),
  881. pf->stat_offsets_loaded,
  882. &osd->rx_size_511, &nsd->rx_size_511);
  883. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  884. I40E_GLPRT_PRC1023L(hw->port),
  885. pf->stat_offsets_loaded,
  886. &osd->rx_size_1023, &nsd->rx_size_1023);
  887. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  888. I40E_GLPRT_PRC1522L(hw->port),
  889. pf->stat_offsets_loaded,
  890. &osd->rx_size_1522, &nsd->rx_size_1522);
  891. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  892. I40E_GLPRT_PRC9522L(hw->port),
  893. pf->stat_offsets_loaded,
  894. &osd->rx_size_big, &nsd->rx_size_big);
  895. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  896. I40E_GLPRT_PTC64L(hw->port),
  897. pf->stat_offsets_loaded,
  898. &osd->tx_size_64, &nsd->tx_size_64);
  899. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  900. I40E_GLPRT_PTC127L(hw->port),
  901. pf->stat_offsets_loaded,
  902. &osd->tx_size_127, &nsd->tx_size_127);
  903. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  904. I40E_GLPRT_PTC255L(hw->port),
  905. pf->stat_offsets_loaded,
  906. &osd->tx_size_255, &nsd->tx_size_255);
  907. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  908. I40E_GLPRT_PTC511L(hw->port),
  909. pf->stat_offsets_loaded,
  910. &osd->tx_size_511, &nsd->tx_size_511);
  911. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  912. I40E_GLPRT_PTC1023L(hw->port),
  913. pf->stat_offsets_loaded,
  914. &osd->tx_size_1023, &nsd->tx_size_1023);
  915. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  916. I40E_GLPRT_PTC1522L(hw->port),
  917. pf->stat_offsets_loaded,
  918. &osd->tx_size_1522, &nsd->tx_size_1522);
  919. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  920. I40E_GLPRT_PTC9522L(hw->port),
  921. pf->stat_offsets_loaded,
  922. &osd->tx_size_big, &nsd->tx_size_big);
  923. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  924. pf->stat_offsets_loaded,
  925. &osd->rx_undersize, &nsd->rx_undersize);
  926. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  927. pf->stat_offsets_loaded,
  928. &osd->rx_fragments, &nsd->rx_fragments);
  929. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  930. pf->stat_offsets_loaded,
  931. &osd->rx_oversize, &nsd->rx_oversize);
  932. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  933. pf->stat_offsets_loaded,
  934. &osd->rx_jabber, &nsd->rx_jabber);
  935. /* FDIR stats */
  936. i40e_stat_update_and_clear32(hw,
  937. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
  938. &nsd->fd_atr_match);
  939. i40e_stat_update_and_clear32(hw,
  940. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
  941. &nsd->fd_sb_match);
  942. i40e_stat_update_and_clear32(hw,
  943. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
  944. &nsd->fd_atr_tunnel_match);
  945. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  946. nsd->tx_lpi_status =
  947. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  948. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  949. nsd->rx_lpi_status =
  950. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  951. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  952. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  953. pf->stat_offsets_loaded,
  954. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  955. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  956. pf->stat_offsets_loaded,
  957. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  958. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  959. !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  960. nsd->fd_sb_status = true;
  961. else
  962. nsd->fd_sb_status = false;
  963. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  964. !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  965. nsd->fd_atr_status = true;
  966. else
  967. nsd->fd_atr_status = false;
  968. pf->stat_offsets_loaded = true;
  969. }
  970. /**
  971. * i40e_update_stats - Update the various statistics counters.
  972. * @vsi: the VSI to be updated
  973. *
  974. * Update the various stats for this VSI and its related entities.
  975. **/
  976. void i40e_update_stats(struct i40e_vsi *vsi)
  977. {
  978. struct i40e_pf *pf = vsi->back;
  979. if (vsi == pf->vsi[pf->lan_vsi])
  980. i40e_update_pf_stats(pf);
  981. i40e_update_vsi_stats(vsi);
  982. }
  983. /**
  984. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  985. * @vsi: the VSI to be searched
  986. * @macaddr: the MAC address
  987. * @vlan: the vlan
  988. *
  989. * Returns ptr to the filter object or NULL
  990. **/
  991. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  992. const u8 *macaddr, s16 vlan)
  993. {
  994. struct i40e_mac_filter *f;
  995. u64 key;
  996. if (!vsi || !macaddr)
  997. return NULL;
  998. key = i40e_addr_to_hkey(macaddr);
  999. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1000. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  1001. (vlan == f->vlan))
  1002. return f;
  1003. }
  1004. return NULL;
  1005. }
  1006. /**
  1007. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1008. * @vsi: the VSI to be searched
  1009. * @macaddr: the MAC address we are searching for
  1010. *
  1011. * Returns the first filter with the provided MAC address or NULL if
  1012. * MAC address was not found
  1013. **/
  1014. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1015. {
  1016. struct i40e_mac_filter *f;
  1017. u64 key;
  1018. if (!vsi || !macaddr)
  1019. return NULL;
  1020. key = i40e_addr_to_hkey(macaddr);
  1021. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1022. if ((ether_addr_equal(macaddr, f->macaddr)))
  1023. return f;
  1024. }
  1025. return NULL;
  1026. }
  1027. /**
  1028. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1029. * @vsi: the VSI to be searched
  1030. *
  1031. * Returns true if VSI is in vlan mode or false otherwise
  1032. **/
  1033. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1034. {
  1035. /* If we have a PVID, always operate in VLAN mode */
  1036. if (vsi->info.pvid)
  1037. return true;
  1038. /* We need to operate in VLAN mode whenever we have any filters with
  1039. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1040. * time, incurring search cost repeatedly. However, we can notice two
  1041. * things:
  1042. *
  1043. * 1) the only place where we can gain a VLAN filter is in
  1044. * i40e_add_filter.
  1045. *
  1046. * 2) the only place where filters are actually removed is in
  1047. * i40e_sync_filters_subtask.
  1048. *
  1049. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1050. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1051. * we have to perform the full search after deleting filters in
  1052. * i40e_sync_filters_subtask, but we already have to search
  1053. * filters here and can perform the check at the same time. This
  1054. * results in avoiding embedding a loop for VLAN mode inside another
  1055. * loop over all the filters, and should maintain correctness as noted
  1056. * above.
  1057. */
  1058. return vsi->has_vlan_filter;
  1059. }
  1060. /**
  1061. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1062. * @vsi: the VSI to configure
  1063. * @tmp_add_list: list of filters ready to be added
  1064. * @tmp_del_list: list of filters ready to be deleted
  1065. * @vlan_filters: the number of active VLAN filters
  1066. *
  1067. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1068. * behave as expected. If we have any active VLAN filters remaining or about
  1069. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1070. * so that they only match against untagged traffic. If we no longer have any
  1071. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1072. * so that they match against both tagged and untagged traffic. In this way,
  1073. * we ensure that we correctly receive the desired traffic. This ensures that
  1074. * when we have an active VLAN we will receive only untagged traffic and
  1075. * traffic matching active VLANs. If we have no active VLANs then we will
  1076. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1077. *
  1078. * Finally, in a similar fashion, this function also corrects filters when
  1079. * there is an active PVID assigned to this VSI.
  1080. *
  1081. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1082. *
  1083. * This function is only expected to be called from within
  1084. * i40e_sync_vsi_filters.
  1085. *
  1086. * NOTE: This function expects to be called while under the
  1087. * mac_filter_hash_lock
  1088. */
  1089. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1090. struct hlist_head *tmp_add_list,
  1091. struct hlist_head *tmp_del_list,
  1092. int vlan_filters)
  1093. {
  1094. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1095. struct i40e_mac_filter *f, *add_head;
  1096. struct i40e_new_mac_filter *new;
  1097. struct hlist_node *h;
  1098. int bkt, new_vlan;
  1099. /* To determine if a particular filter needs to be replaced we
  1100. * have the three following conditions:
  1101. *
  1102. * a) if we have a PVID assigned, then all filters which are
  1103. * not marked as VLAN=PVID must be replaced with filters that
  1104. * are.
  1105. * b) otherwise, if we have any active VLANS, all filters
  1106. * which are marked as VLAN=-1 must be replaced with
  1107. * filters marked as VLAN=0
  1108. * c) finally, if we do not have any active VLANS, all filters
  1109. * which are marked as VLAN=0 must be replaced with filters
  1110. * marked as VLAN=-1
  1111. */
  1112. /* Update the filters about to be added in place */
  1113. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1114. if (pvid && new->f->vlan != pvid)
  1115. new->f->vlan = pvid;
  1116. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1117. new->f->vlan = 0;
  1118. else if (!vlan_filters && new->f->vlan == 0)
  1119. new->f->vlan = I40E_VLAN_ANY;
  1120. }
  1121. /* Update the remaining active filters */
  1122. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1123. /* Combine the checks for whether a filter needs to be changed
  1124. * and then determine the new VLAN inside the if block, in
  1125. * order to avoid duplicating code for adding the new filter
  1126. * then deleting the old filter.
  1127. */
  1128. if ((pvid && f->vlan != pvid) ||
  1129. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1130. (!vlan_filters && f->vlan == 0)) {
  1131. /* Determine the new vlan we will be adding */
  1132. if (pvid)
  1133. new_vlan = pvid;
  1134. else if (vlan_filters)
  1135. new_vlan = 0;
  1136. else
  1137. new_vlan = I40E_VLAN_ANY;
  1138. /* Create the new filter */
  1139. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1140. if (!add_head)
  1141. return -ENOMEM;
  1142. /* Create a temporary i40e_new_mac_filter */
  1143. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1144. if (!new)
  1145. return -ENOMEM;
  1146. new->f = add_head;
  1147. new->state = add_head->state;
  1148. /* Add the new filter to the tmp list */
  1149. hlist_add_head(&new->hlist, tmp_add_list);
  1150. /* Put the original filter into the delete list */
  1151. f->state = I40E_FILTER_REMOVE;
  1152. hash_del(&f->hlist);
  1153. hlist_add_head(&f->hlist, tmp_del_list);
  1154. }
  1155. }
  1156. vsi->has_vlan_filter = !!vlan_filters;
  1157. return 0;
  1158. }
  1159. /**
  1160. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1161. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1162. * @macaddr: the MAC address
  1163. *
  1164. * Remove whatever filter the firmware set up so the driver can manage
  1165. * its own filtering intelligently.
  1166. **/
  1167. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1168. {
  1169. struct i40e_aqc_remove_macvlan_element_data element;
  1170. struct i40e_pf *pf = vsi->back;
  1171. /* Only appropriate for the PF main VSI */
  1172. if (vsi->type != I40E_VSI_MAIN)
  1173. return;
  1174. memset(&element, 0, sizeof(element));
  1175. ether_addr_copy(element.mac_addr, macaddr);
  1176. element.vlan_tag = 0;
  1177. /* Ignore error returns, some firmware does it this way... */
  1178. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1179. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1180. memset(&element, 0, sizeof(element));
  1181. ether_addr_copy(element.mac_addr, macaddr);
  1182. element.vlan_tag = 0;
  1183. /* ...and some firmware does it this way. */
  1184. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1185. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1186. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1187. }
  1188. /**
  1189. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1190. * @vsi: the VSI to be searched
  1191. * @macaddr: the MAC address
  1192. * @vlan: the vlan
  1193. *
  1194. * Returns ptr to the filter object or NULL when no memory available.
  1195. *
  1196. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1197. * being held.
  1198. **/
  1199. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1200. const u8 *macaddr, s16 vlan)
  1201. {
  1202. struct i40e_mac_filter *f;
  1203. u64 key;
  1204. if (!vsi || !macaddr)
  1205. return NULL;
  1206. f = i40e_find_filter(vsi, macaddr, vlan);
  1207. if (!f) {
  1208. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1209. if (!f)
  1210. return NULL;
  1211. /* Update the boolean indicating if we need to function in
  1212. * VLAN mode.
  1213. */
  1214. if (vlan >= 0)
  1215. vsi->has_vlan_filter = true;
  1216. ether_addr_copy(f->macaddr, macaddr);
  1217. f->vlan = vlan;
  1218. f->state = I40E_FILTER_NEW;
  1219. INIT_HLIST_NODE(&f->hlist);
  1220. key = i40e_addr_to_hkey(macaddr);
  1221. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1222. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1223. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1224. }
  1225. /* If we're asked to add a filter that has been marked for removal, it
  1226. * is safe to simply restore it to active state. __i40e_del_filter
  1227. * will have simply deleted any filters which were previously marked
  1228. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1229. * previously been ACTIVE. Since we haven't yet run the sync filters
  1230. * task, just restore this filter to the ACTIVE state so that the
  1231. * sync task leaves it in place
  1232. */
  1233. if (f->state == I40E_FILTER_REMOVE)
  1234. f->state = I40E_FILTER_ACTIVE;
  1235. return f;
  1236. }
  1237. /**
  1238. * __i40e_del_filter - Remove a specific filter from the VSI
  1239. * @vsi: VSI to remove from
  1240. * @f: the filter to remove from the list
  1241. *
  1242. * This function should be called instead of i40e_del_filter only if you know
  1243. * the exact filter you will remove already, such as via i40e_find_filter or
  1244. * i40e_find_mac.
  1245. *
  1246. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1247. * being held.
  1248. * ANOTHER NOTE: This function MUST be called from within the context of
  1249. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1250. * instead of list_for_each_entry().
  1251. **/
  1252. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1253. {
  1254. if (!f)
  1255. return;
  1256. /* If the filter was never added to firmware then we can just delete it
  1257. * directly and we don't want to set the status to remove or else an
  1258. * admin queue command will unnecessarily fire.
  1259. */
  1260. if ((f->state == I40E_FILTER_FAILED) ||
  1261. (f->state == I40E_FILTER_NEW)) {
  1262. hash_del(&f->hlist);
  1263. kfree(f);
  1264. } else {
  1265. f->state = I40E_FILTER_REMOVE;
  1266. }
  1267. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1268. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->state);
  1269. }
  1270. /**
  1271. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1272. * @vsi: the VSI to be searched
  1273. * @macaddr: the MAC address
  1274. * @vlan: the VLAN
  1275. *
  1276. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1277. * being held.
  1278. * ANOTHER NOTE: This function MUST be called from within the context of
  1279. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1280. * instead of list_for_each_entry().
  1281. **/
  1282. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1283. {
  1284. struct i40e_mac_filter *f;
  1285. if (!vsi || !macaddr)
  1286. return;
  1287. f = i40e_find_filter(vsi, macaddr, vlan);
  1288. __i40e_del_filter(vsi, f);
  1289. }
  1290. /**
  1291. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1292. * @vsi: the VSI to be searched
  1293. * @macaddr: the mac address to be filtered
  1294. *
  1295. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1296. * go through all the macvlan filters and add a macvlan filter for each
  1297. * unique vlan that already exists. If a PVID has been assigned, instead only
  1298. * add the macaddr to that VLAN.
  1299. *
  1300. * Returns last filter added on success, else NULL
  1301. **/
  1302. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1303. const u8 *macaddr)
  1304. {
  1305. struct i40e_mac_filter *f, *add = NULL;
  1306. struct hlist_node *h;
  1307. int bkt;
  1308. if (vsi->info.pvid)
  1309. return i40e_add_filter(vsi, macaddr,
  1310. le16_to_cpu(vsi->info.pvid));
  1311. if (!i40e_is_vsi_in_vlan(vsi))
  1312. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1313. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1314. if (f->state == I40E_FILTER_REMOVE)
  1315. continue;
  1316. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1317. if (!add)
  1318. return NULL;
  1319. }
  1320. return add;
  1321. }
  1322. /**
  1323. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1324. * @vsi: the VSI to be searched
  1325. * @macaddr: the mac address to be removed
  1326. *
  1327. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1328. * associated with.
  1329. *
  1330. * Returns 0 for success, or error
  1331. **/
  1332. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1333. {
  1334. struct i40e_mac_filter *f;
  1335. struct hlist_node *h;
  1336. bool found = false;
  1337. int bkt;
  1338. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1339. "Missing mac_filter_hash_lock\n");
  1340. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1341. if (ether_addr_equal(macaddr, f->macaddr)) {
  1342. __i40e_del_filter(vsi, f);
  1343. found = true;
  1344. }
  1345. }
  1346. if (found)
  1347. return 0;
  1348. else
  1349. return -ENOENT;
  1350. }
  1351. /**
  1352. * i40e_set_mac - NDO callback to set mac address
  1353. * @netdev: network interface device structure
  1354. * @p: pointer to an address structure
  1355. *
  1356. * Returns 0 on success, negative on failure
  1357. **/
  1358. static int i40e_set_mac(struct net_device *netdev, void *p)
  1359. {
  1360. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1361. struct i40e_vsi *vsi = np->vsi;
  1362. struct i40e_pf *pf = vsi->back;
  1363. struct i40e_hw *hw = &pf->hw;
  1364. struct sockaddr *addr = p;
  1365. if (!is_valid_ether_addr(addr->sa_data))
  1366. return -EADDRNOTAVAIL;
  1367. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1368. netdev_info(netdev, "already using mac address %pM\n",
  1369. addr->sa_data);
  1370. return 0;
  1371. }
  1372. if (test_bit(__I40E_DOWN, pf->state) ||
  1373. test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  1374. return -EADDRNOTAVAIL;
  1375. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1376. netdev_info(netdev, "returning to hw mac address %pM\n",
  1377. hw->mac.addr);
  1378. else
  1379. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1380. /* Copy the address first, so that we avoid a possible race with
  1381. * .set_rx_mode(). If we copy after changing the address in the filter
  1382. * list, we might open ourselves to a narrow race window where
  1383. * .set_rx_mode could delete our dev_addr filter and prevent traffic
  1384. * from passing.
  1385. */
  1386. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1387. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1388. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1389. i40e_add_mac_filter(vsi, addr->sa_data);
  1390. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1391. if (vsi->type == I40E_VSI_MAIN) {
  1392. i40e_status ret;
  1393. ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
  1394. addr->sa_data, NULL);
  1395. if (ret)
  1396. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1397. i40e_stat_str(hw, ret),
  1398. i40e_aq_str(hw, hw->aq.asq_last_status));
  1399. }
  1400. /* schedule our worker thread which will take care of
  1401. * applying the new filter changes
  1402. */
  1403. i40e_service_event_schedule(pf);
  1404. return 0;
  1405. }
  1406. /**
  1407. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  1408. * @vsi: vsi structure
  1409. * @seed: RSS hash seed
  1410. **/
  1411. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  1412. u8 *lut, u16 lut_size)
  1413. {
  1414. struct i40e_pf *pf = vsi->back;
  1415. struct i40e_hw *hw = &pf->hw;
  1416. int ret = 0;
  1417. if (seed) {
  1418. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  1419. (struct i40e_aqc_get_set_rss_key_data *)seed;
  1420. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  1421. if (ret) {
  1422. dev_info(&pf->pdev->dev,
  1423. "Cannot set RSS key, err %s aq_err %s\n",
  1424. i40e_stat_str(hw, ret),
  1425. i40e_aq_str(hw, hw->aq.asq_last_status));
  1426. return ret;
  1427. }
  1428. }
  1429. if (lut) {
  1430. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  1431. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  1432. if (ret) {
  1433. dev_info(&pf->pdev->dev,
  1434. "Cannot set RSS lut, err %s aq_err %s\n",
  1435. i40e_stat_str(hw, ret),
  1436. i40e_aq_str(hw, hw->aq.asq_last_status));
  1437. return ret;
  1438. }
  1439. }
  1440. return ret;
  1441. }
  1442. /**
  1443. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  1444. * @vsi: VSI structure
  1445. **/
  1446. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  1447. {
  1448. struct i40e_pf *pf = vsi->back;
  1449. u8 seed[I40E_HKEY_ARRAY_SIZE];
  1450. u8 *lut;
  1451. int ret;
  1452. if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
  1453. return 0;
  1454. if (!vsi->rss_size)
  1455. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  1456. vsi->num_queue_pairs);
  1457. if (!vsi->rss_size)
  1458. return -EINVAL;
  1459. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  1460. if (!lut)
  1461. return -ENOMEM;
  1462. /* Use the user configured hash keys and lookup table if there is one,
  1463. * otherwise use default
  1464. */
  1465. if (vsi->rss_lut_user)
  1466. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  1467. else
  1468. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  1469. if (vsi->rss_hkey_user)
  1470. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  1471. else
  1472. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  1473. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  1474. kfree(lut);
  1475. return ret;
  1476. }
  1477. /**
  1478. * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
  1479. * @vsi: the VSI being configured,
  1480. * @ctxt: VSI context structure
  1481. * @enabled_tc: number of traffic classes to enable
  1482. *
  1483. * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
  1484. **/
  1485. static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
  1486. struct i40e_vsi_context *ctxt,
  1487. u8 enabled_tc)
  1488. {
  1489. u16 qcount = 0, max_qcount, qmap, sections = 0;
  1490. int i, override_q, pow, num_qps, ret;
  1491. u8 netdev_tc = 0, offset = 0;
  1492. if (vsi->type != I40E_VSI_MAIN)
  1493. return -EINVAL;
  1494. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1495. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1496. vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
  1497. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1498. num_qps = vsi->mqprio_qopt.qopt.count[0];
  1499. /* find the next higher power-of-2 of num queue pairs */
  1500. pow = ilog2(num_qps);
  1501. if (!is_power_of_2(num_qps))
  1502. pow++;
  1503. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1504. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1505. /* Setup queue offset/count for all TCs for given VSI */
  1506. max_qcount = vsi->mqprio_qopt.qopt.count[0];
  1507. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1508. /* See if the given TC is enabled for the given VSI */
  1509. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1510. offset = vsi->mqprio_qopt.qopt.offset[i];
  1511. qcount = vsi->mqprio_qopt.qopt.count[i];
  1512. if (qcount > max_qcount)
  1513. max_qcount = qcount;
  1514. vsi->tc_config.tc_info[i].qoffset = offset;
  1515. vsi->tc_config.tc_info[i].qcount = qcount;
  1516. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1517. } else {
  1518. /* TC is not enabled so set the offset to
  1519. * default queue and allocate one queue
  1520. * for the given TC.
  1521. */
  1522. vsi->tc_config.tc_info[i].qoffset = 0;
  1523. vsi->tc_config.tc_info[i].qcount = 1;
  1524. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1525. }
  1526. }
  1527. /* Set actual Tx/Rx queue pairs */
  1528. vsi->num_queue_pairs = offset + qcount;
  1529. /* Setup queue TC[0].qmap for given VSI context */
  1530. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  1531. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1532. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1533. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1534. /* Reconfigure RSS for main VSI with max queue count */
  1535. vsi->rss_size = max_qcount;
  1536. ret = i40e_vsi_config_rss(vsi);
  1537. if (ret) {
  1538. dev_info(&vsi->back->pdev->dev,
  1539. "Failed to reconfig rss for num_queues (%u)\n",
  1540. max_qcount);
  1541. return ret;
  1542. }
  1543. vsi->reconfig_rss = true;
  1544. dev_dbg(&vsi->back->pdev->dev,
  1545. "Reconfigured rss with num_queues (%u)\n", max_qcount);
  1546. /* Find queue count available for channel VSIs and starting offset
  1547. * for channel VSIs
  1548. */
  1549. override_q = vsi->mqprio_qopt.qopt.count[0];
  1550. if (override_q && override_q < vsi->num_queue_pairs) {
  1551. vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
  1552. vsi->next_base_queue = override_q;
  1553. }
  1554. return 0;
  1555. }
  1556. /**
  1557. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1558. * @vsi: the VSI being setup
  1559. * @ctxt: VSI context structure
  1560. * @enabled_tc: Enabled TCs bitmap
  1561. * @is_add: True if called before Add VSI
  1562. *
  1563. * Setup VSI queue mapping for enabled traffic classes.
  1564. **/
  1565. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1566. struct i40e_vsi_context *ctxt,
  1567. u8 enabled_tc,
  1568. bool is_add)
  1569. {
  1570. struct i40e_pf *pf = vsi->back;
  1571. u16 sections = 0;
  1572. u8 netdev_tc = 0;
  1573. u16 numtc = 1;
  1574. u16 qcount;
  1575. u8 offset;
  1576. u16 qmap;
  1577. int i;
  1578. u16 num_tc_qps = 0;
  1579. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1580. offset = 0;
  1581. /* Number of queues per enabled TC */
  1582. num_tc_qps = vsi->alloc_queue_pairs;
  1583. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1584. /* Find numtc from enabled TC bitmap */
  1585. for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1586. if (enabled_tc & BIT(i)) /* TC is enabled */
  1587. numtc++;
  1588. }
  1589. if (!numtc) {
  1590. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1591. numtc = 1;
  1592. }
  1593. num_tc_qps = num_tc_qps / numtc;
  1594. num_tc_qps = min_t(int, num_tc_qps,
  1595. i40e_pf_get_max_q_per_tc(pf));
  1596. }
  1597. vsi->tc_config.numtc = numtc;
  1598. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1599. /* Do not allow use more TC queue pairs than MSI-X vectors exist */
  1600. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1601. num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
  1602. /* Setup queue offset/count for all TCs for given VSI */
  1603. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1604. /* See if the given TC is enabled for the given VSI */
  1605. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1606. /* TC is enabled */
  1607. int pow, num_qps;
  1608. switch (vsi->type) {
  1609. case I40E_VSI_MAIN:
  1610. if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
  1611. I40E_FLAG_FD_ATR_ENABLED)) ||
  1612. vsi->tc_config.enabled_tc != 1) {
  1613. qcount = min_t(int, pf->alloc_rss_size,
  1614. num_tc_qps);
  1615. break;
  1616. }
  1617. /* fall through */
  1618. case I40E_VSI_FDIR:
  1619. case I40E_VSI_SRIOV:
  1620. case I40E_VSI_VMDQ2:
  1621. default:
  1622. qcount = num_tc_qps;
  1623. WARN_ON(i != 0);
  1624. break;
  1625. }
  1626. vsi->tc_config.tc_info[i].qoffset = offset;
  1627. vsi->tc_config.tc_info[i].qcount = qcount;
  1628. /* find the next higher power-of-2 of num queue pairs */
  1629. num_qps = qcount;
  1630. pow = 0;
  1631. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1632. pow++;
  1633. num_qps >>= 1;
  1634. }
  1635. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1636. qmap =
  1637. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1638. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1639. offset += qcount;
  1640. } else {
  1641. /* TC is not enabled so set the offset to
  1642. * default queue and allocate one queue
  1643. * for the given TC.
  1644. */
  1645. vsi->tc_config.tc_info[i].qoffset = 0;
  1646. vsi->tc_config.tc_info[i].qcount = 1;
  1647. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1648. qmap = 0;
  1649. }
  1650. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1651. }
  1652. /* Set actual Tx/Rx queue pairs */
  1653. vsi->num_queue_pairs = offset;
  1654. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1655. if (vsi->req_queue_pairs > 0)
  1656. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1657. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1658. vsi->num_queue_pairs = pf->num_lan_msix;
  1659. }
  1660. /* Scheduler section valid can only be set for ADD VSI */
  1661. if (is_add) {
  1662. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1663. ctxt->info.up_enable_bits = enabled_tc;
  1664. }
  1665. if (vsi->type == I40E_VSI_SRIOV) {
  1666. ctxt->info.mapping_flags |=
  1667. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1668. for (i = 0; i < vsi->num_queue_pairs; i++)
  1669. ctxt->info.queue_mapping[i] =
  1670. cpu_to_le16(vsi->base_queue + i);
  1671. } else {
  1672. ctxt->info.mapping_flags |=
  1673. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1674. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1675. }
  1676. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1677. }
  1678. /**
  1679. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1680. * @netdev: the netdevice
  1681. * @addr: address to add
  1682. *
  1683. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1684. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1685. */
  1686. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1687. {
  1688. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1689. struct i40e_vsi *vsi = np->vsi;
  1690. if (i40e_add_mac_filter(vsi, addr))
  1691. return 0;
  1692. else
  1693. return -ENOMEM;
  1694. }
  1695. /**
  1696. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1697. * @netdev: the netdevice
  1698. * @addr: address to add
  1699. *
  1700. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1701. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1702. */
  1703. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1704. {
  1705. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1706. struct i40e_vsi *vsi = np->vsi;
  1707. /* Under some circumstances, we might receive a request to delete
  1708. * our own device address from our uc list. Because we store the
  1709. * device address in the VSI's MAC/VLAN filter list, we need to ignore
  1710. * such requests and not delete our device address from this list.
  1711. */
  1712. if (ether_addr_equal(addr, netdev->dev_addr))
  1713. return 0;
  1714. i40e_del_mac_filter(vsi, addr);
  1715. return 0;
  1716. }
  1717. /**
  1718. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1719. * @netdev: network interface device structure
  1720. **/
  1721. static void i40e_set_rx_mode(struct net_device *netdev)
  1722. {
  1723. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1724. struct i40e_vsi *vsi = np->vsi;
  1725. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1726. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1727. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1728. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1729. /* check for other flag changes */
  1730. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1731. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1732. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1733. }
  1734. }
  1735. /**
  1736. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1737. * @vsi: Pointer to VSI struct
  1738. * @from: Pointer to list which contains MAC filter entries - changes to
  1739. * those entries needs to be undone.
  1740. *
  1741. * MAC filter entries from this list were slated for deletion.
  1742. **/
  1743. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1744. struct hlist_head *from)
  1745. {
  1746. struct i40e_mac_filter *f;
  1747. struct hlist_node *h;
  1748. hlist_for_each_entry_safe(f, h, from, hlist) {
  1749. u64 key = i40e_addr_to_hkey(f->macaddr);
  1750. /* Move the element back into MAC filter list*/
  1751. hlist_del(&f->hlist);
  1752. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1753. }
  1754. }
  1755. /**
  1756. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1757. * @vsi: Pointer to vsi struct
  1758. * @from: Pointer to list which contains MAC filter entries - changes to
  1759. * those entries needs to be undone.
  1760. *
  1761. * MAC filter entries from this list were slated for addition.
  1762. **/
  1763. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1764. struct hlist_head *from)
  1765. {
  1766. struct i40e_new_mac_filter *new;
  1767. struct hlist_node *h;
  1768. hlist_for_each_entry_safe(new, h, from, hlist) {
  1769. /* We can simply free the wrapper structure */
  1770. hlist_del(&new->hlist);
  1771. kfree(new);
  1772. }
  1773. }
  1774. /**
  1775. * i40e_next_entry - Get the next non-broadcast filter from a list
  1776. * @next: pointer to filter in list
  1777. *
  1778. * Returns the next non-broadcast filter in the list. Required so that we
  1779. * ignore broadcast filters within the list, since these are not handled via
  1780. * the normal firmware update path.
  1781. */
  1782. static
  1783. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1784. {
  1785. hlist_for_each_entry_continue(next, hlist) {
  1786. if (!is_broadcast_ether_addr(next->f->macaddr))
  1787. return next;
  1788. }
  1789. return NULL;
  1790. }
  1791. /**
  1792. * i40e_update_filter_state - Update filter state based on return data
  1793. * from firmware
  1794. * @count: Number of filters added
  1795. * @add_list: return data from fw
  1796. * @add_head: pointer to first filter in current batch
  1797. *
  1798. * MAC filter entries from list were slated to be added to device. Returns
  1799. * number of successful filters. Note that 0 does NOT mean success!
  1800. **/
  1801. static int
  1802. i40e_update_filter_state(int count,
  1803. struct i40e_aqc_add_macvlan_element_data *add_list,
  1804. struct i40e_new_mac_filter *add_head)
  1805. {
  1806. int retval = 0;
  1807. int i;
  1808. for (i = 0; i < count; i++) {
  1809. /* Always check status of each filter. We don't need to check
  1810. * the firmware return status because we pre-set the filter
  1811. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1812. * request to the adminq. Thus, if it no longer matches then
  1813. * we know the filter is active.
  1814. */
  1815. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1816. add_head->state = I40E_FILTER_FAILED;
  1817. } else {
  1818. add_head->state = I40E_FILTER_ACTIVE;
  1819. retval++;
  1820. }
  1821. add_head = i40e_next_filter(add_head);
  1822. if (!add_head)
  1823. break;
  1824. }
  1825. return retval;
  1826. }
  1827. /**
  1828. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1829. * @vsi: ptr to the VSI
  1830. * @vsi_name: name to display in messages
  1831. * @list: the list of filters to send to firmware
  1832. * @num_del: the number of filters to delete
  1833. * @retval: Set to -EIO on failure to delete
  1834. *
  1835. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1836. * *retval instead of a return value so that success does not force ret_val to
  1837. * be set to 0. This ensures that a sequence of calls to this function
  1838. * preserve the previous value of *retval on successful delete.
  1839. */
  1840. static
  1841. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1842. struct i40e_aqc_remove_macvlan_element_data *list,
  1843. int num_del, int *retval)
  1844. {
  1845. struct i40e_hw *hw = &vsi->back->hw;
  1846. i40e_status aq_ret;
  1847. int aq_err;
  1848. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1849. aq_err = hw->aq.asq_last_status;
  1850. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1851. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1852. *retval = -EIO;
  1853. dev_info(&vsi->back->pdev->dev,
  1854. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1855. vsi_name, i40e_stat_str(hw, aq_ret),
  1856. i40e_aq_str(hw, aq_err));
  1857. }
  1858. }
  1859. /**
  1860. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1861. * @vsi: ptr to the VSI
  1862. * @vsi_name: name to display in messages
  1863. * @list: the list of filters to send to firmware
  1864. * @add_head: Position in the add hlist
  1865. * @num_add: the number of filters to add
  1866. *
  1867. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1868. * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
  1869. * space for more filters.
  1870. */
  1871. static
  1872. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1873. struct i40e_aqc_add_macvlan_element_data *list,
  1874. struct i40e_new_mac_filter *add_head,
  1875. int num_add)
  1876. {
  1877. struct i40e_hw *hw = &vsi->back->hw;
  1878. int aq_err, fcnt;
  1879. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1880. aq_err = hw->aq.asq_last_status;
  1881. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1882. if (fcnt != num_add) {
  1883. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1884. dev_warn(&vsi->back->pdev->dev,
  1885. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1886. i40e_aq_str(hw, aq_err),
  1887. vsi_name);
  1888. }
  1889. }
  1890. /**
  1891. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1892. * @vsi: pointer to the VSI
  1893. * @vsi_name: the VSI name
  1894. * @f: filter data
  1895. *
  1896. * This function sets or clears the promiscuous broadcast flags for VLAN
  1897. * filters in order to properly receive broadcast frames. Assumes that only
  1898. * broadcast filters are passed.
  1899. *
  1900. * Returns status indicating success or failure;
  1901. **/
  1902. static i40e_status
  1903. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1904. struct i40e_mac_filter *f)
  1905. {
  1906. bool enable = f->state == I40E_FILTER_NEW;
  1907. struct i40e_hw *hw = &vsi->back->hw;
  1908. i40e_status aq_ret;
  1909. if (f->vlan == I40E_VLAN_ANY) {
  1910. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1911. vsi->seid,
  1912. enable,
  1913. NULL);
  1914. } else {
  1915. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1916. vsi->seid,
  1917. enable,
  1918. f->vlan,
  1919. NULL);
  1920. }
  1921. if (aq_ret) {
  1922. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1923. dev_warn(&vsi->back->pdev->dev,
  1924. "Error %s, forcing overflow promiscuous on %s\n",
  1925. i40e_aq_str(hw, hw->aq.asq_last_status),
  1926. vsi_name);
  1927. }
  1928. return aq_ret;
  1929. }
  1930. /**
  1931. * i40e_set_promiscuous - set promiscuous mode
  1932. * @pf: board private structure
  1933. * @promisc: promisc on or off
  1934. *
  1935. * There are different ways of setting promiscuous mode on a PF depending on
  1936. * what state/environment we're in. This identifies and sets it appropriately.
  1937. * Returns 0 on success.
  1938. **/
  1939. static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
  1940. {
  1941. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  1942. struct i40e_hw *hw = &pf->hw;
  1943. i40e_status aq_ret;
  1944. if (vsi->type == I40E_VSI_MAIN &&
  1945. pf->lan_veb != I40E_NO_VEB &&
  1946. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  1947. /* set defport ON for Main VSI instead of true promisc
  1948. * this way we will get all unicast/multicast and VLAN
  1949. * promisc behavior but will not get VF or VMDq traffic
  1950. * replicated on the Main VSI.
  1951. */
  1952. if (promisc)
  1953. aq_ret = i40e_aq_set_default_vsi(hw,
  1954. vsi->seid,
  1955. NULL);
  1956. else
  1957. aq_ret = i40e_aq_clear_default_vsi(hw,
  1958. vsi->seid,
  1959. NULL);
  1960. if (aq_ret) {
  1961. dev_info(&pf->pdev->dev,
  1962. "Set default VSI failed, err %s, aq_err %s\n",
  1963. i40e_stat_str(hw, aq_ret),
  1964. i40e_aq_str(hw, hw->aq.asq_last_status));
  1965. }
  1966. } else {
  1967. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  1968. hw,
  1969. vsi->seid,
  1970. promisc, NULL,
  1971. true);
  1972. if (aq_ret) {
  1973. dev_info(&pf->pdev->dev,
  1974. "set unicast promisc failed, err %s, aq_err %s\n",
  1975. i40e_stat_str(hw, aq_ret),
  1976. i40e_aq_str(hw, hw->aq.asq_last_status));
  1977. }
  1978. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  1979. hw,
  1980. vsi->seid,
  1981. promisc, NULL);
  1982. if (aq_ret) {
  1983. dev_info(&pf->pdev->dev,
  1984. "set multicast promisc failed, err %s, aq_err %s\n",
  1985. i40e_stat_str(hw, aq_ret),
  1986. i40e_aq_str(hw, hw->aq.asq_last_status));
  1987. }
  1988. }
  1989. if (!aq_ret)
  1990. pf->cur_promisc = promisc;
  1991. return aq_ret;
  1992. }
  1993. /**
  1994. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1995. * @vsi: ptr to the VSI
  1996. *
  1997. * Push any outstanding VSI filter changes through the AdminQ.
  1998. *
  1999. * Returns 0 or error value
  2000. **/
  2001. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  2002. {
  2003. struct hlist_head tmp_add_list, tmp_del_list;
  2004. struct i40e_mac_filter *f;
  2005. struct i40e_new_mac_filter *new, *add_head = NULL;
  2006. struct i40e_hw *hw = &vsi->back->hw;
  2007. bool old_overflow, new_overflow;
  2008. unsigned int failed_filters = 0;
  2009. unsigned int vlan_filters = 0;
  2010. char vsi_name[16] = "PF";
  2011. int filter_list_len = 0;
  2012. i40e_status aq_ret = 0;
  2013. u32 changed_flags = 0;
  2014. struct hlist_node *h;
  2015. struct i40e_pf *pf;
  2016. int num_add = 0;
  2017. int num_del = 0;
  2018. int retval = 0;
  2019. u16 cmd_flags;
  2020. int list_size;
  2021. int bkt;
  2022. /* empty array typed pointers, kcalloc later */
  2023. struct i40e_aqc_add_macvlan_element_data *add_list;
  2024. struct i40e_aqc_remove_macvlan_element_data *del_list;
  2025. while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
  2026. usleep_range(1000, 2000);
  2027. pf = vsi->back;
  2028. old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2029. if (vsi->netdev) {
  2030. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  2031. vsi->current_netdev_flags = vsi->netdev->flags;
  2032. }
  2033. INIT_HLIST_HEAD(&tmp_add_list);
  2034. INIT_HLIST_HEAD(&tmp_del_list);
  2035. if (vsi->type == I40E_VSI_SRIOV)
  2036. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  2037. else if (vsi->type != I40E_VSI_MAIN)
  2038. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  2039. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  2040. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  2041. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2042. /* Create a list of filters to delete. */
  2043. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2044. if (f->state == I40E_FILTER_REMOVE) {
  2045. /* Move the element into temporary del_list */
  2046. hash_del(&f->hlist);
  2047. hlist_add_head(&f->hlist, &tmp_del_list);
  2048. /* Avoid counting removed filters */
  2049. continue;
  2050. }
  2051. if (f->state == I40E_FILTER_NEW) {
  2052. /* Create a temporary i40e_new_mac_filter */
  2053. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  2054. if (!new)
  2055. goto err_no_memory_locked;
  2056. /* Store pointer to the real filter */
  2057. new->f = f;
  2058. new->state = f->state;
  2059. /* Add it to the hash list */
  2060. hlist_add_head(&new->hlist, &tmp_add_list);
  2061. }
  2062. /* Count the number of active (current and new) VLAN
  2063. * filters we have now. Does not count filters which
  2064. * are marked for deletion.
  2065. */
  2066. if (f->vlan > 0)
  2067. vlan_filters++;
  2068. }
  2069. retval = i40e_correct_mac_vlan_filters(vsi,
  2070. &tmp_add_list,
  2071. &tmp_del_list,
  2072. vlan_filters);
  2073. if (retval)
  2074. goto err_no_memory_locked;
  2075. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2076. }
  2077. /* Now process 'del_list' outside the lock */
  2078. if (!hlist_empty(&tmp_del_list)) {
  2079. filter_list_len = hw->aq.asq_buf_size /
  2080. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2081. list_size = filter_list_len *
  2082. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2083. del_list = kzalloc(list_size, GFP_ATOMIC);
  2084. if (!del_list)
  2085. goto err_no_memory;
  2086. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  2087. cmd_flags = 0;
  2088. /* handle broadcast filters by updating the broadcast
  2089. * promiscuous flag and release filter list.
  2090. */
  2091. if (is_broadcast_ether_addr(f->macaddr)) {
  2092. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  2093. hlist_del(&f->hlist);
  2094. kfree(f);
  2095. continue;
  2096. }
  2097. /* add to delete list */
  2098. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  2099. if (f->vlan == I40E_VLAN_ANY) {
  2100. del_list[num_del].vlan_tag = 0;
  2101. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  2102. } else {
  2103. del_list[num_del].vlan_tag =
  2104. cpu_to_le16((u16)(f->vlan));
  2105. }
  2106. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  2107. del_list[num_del].flags = cmd_flags;
  2108. num_del++;
  2109. /* flush a full buffer */
  2110. if (num_del == filter_list_len) {
  2111. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2112. num_del, &retval);
  2113. memset(del_list, 0, list_size);
  2114. num_del = 0;
  2115. }
  2116. /* Release memory for MAC filter entries which were
  2117. * synced up with HW.
  2118. */
  2119. hlist_del(&f->hlist);
  2120. kfree(f);
  2121. }
  2122. if (num_del) {
  2123. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2124. num_del, &retval);
  2125. }
  2126. kfree(del_list);
  2127. del_list = NULL;
  2128. }
  2129. if (!hlist_empty(&tmp_add_list)) {
  2130. /* Do all the adds now. */
  2131. filter_list_len = hw->aq.asq_buf_size /
  2132. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2133. list_size = filter_list_len *
  2134. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2135. add_list = kzalloc(list_size, GFP_ATOMIC);
  2136. if (!add_list)
  2137. goto err_no_memory;
  2138. num_add = 0;
  2139. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2140. /* handle broadcast filters by updating the broadcast
  2141. * promiscuous flag instead of adding a MAC filter.
  2142. */
  2143. if (is_broadcast_ether_addr(new->f->macaddr)) {
  2144. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  2145. new->f))
  2146. new->state = I40E_FILTER_FAILED;
  2147. else
  2148. new->state = I40E_FILTER_ACTIVE;
  2149. continue;
  2150. }
  2151. /* add to add array */
  2152. if (num_add == 0)
  2153. add_head = new;
  2154. cmd_flags = 0;
  2155. ether_addr_copy(add_list[num_add].mac_addr,
  2156. new->f->macaddr);
  2157. if (new->f->vlan == I40E_VLAN_ANY) {
  2158. add_list[num_add].vlan_tag = 0;
  2159. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  2160. } else {
  2161. add_list[num_add].vlan_tag =
  2162. cpu_to_le16((u16)(new->f->vlan));
  2163. }
  2164. add_list[num_add].queue_number = 0;
  2165. /* set invalid match method for later detection */
  2166. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  2167. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  2168. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  2169. num_add++;
  2170. /* flush a full buffer */
  2171. if (num_add == filter_list_len) {
  2172. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  2173. add_head, num_add);
  2174. memset(add_list, 0, list_size);
  2175. num_add = 0;
  2176. }
  2177. }
  2178. if (num_add) {
  2179. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  2180. num_add);
  2181. }
  2182. /* Now move all of the filters from the temp add list back to
  2183. * the VSI's list.
  2184. */
  2185. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2186. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2187. /* Only update the state if we're still NEW */
  2188. if (new->f->state == I40E_FILTER_NEW)
  2189. new->f->state = new->state;
  2190. hlist_del(&new->hlist);
  2191. kfree(new);
  2192. }
  2193. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2194. kfree(add_list);
  2195. add_list = NULL;
  2196. }
  2197. /* Determine the number of active and failed filters. */
  2198. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2199. vsi->active_filters = 0;
  2200. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  2201. if (f->state == I40E_FILTER_ACTIVE)
  2202. vsi->active_filters++;
  2203. else if (f->state == I40E_FILTER_FAILED)
  2204. failed_filters++;
  2205. }
  2206. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2207. /* Check if we are able to exit overflow promiscuous mode. We can
  2208. * safely exit if we didn't just enter, we no longer have any failed
  2209. * filters, and we have reduced filters below the threshold value.
  2210. */
  2211. if (old_overflow && !failed_filters &&
  2212. vsi->active_filters < vsi->promisc_threshold) {
  2213. dev_info(&pf->pdev->dev,
  2214. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  2215. vsi_name);
  2216. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2217. vsi->promisc_threshold = 0;
  2218. }
  2219. /* if the VF is not trusted do not do promisc */
  2220. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2221. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2222. goto out;
  2223. }
  2224. new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2225. /* If we are entering overflow promiscuous, we need to calculate a new
  2226. * threshold for when we are safe to exit
  2227. */
  2228. if (!old_overflow && new_overflow)
  2229. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  2230. /* check for changes in promiscuous modes */
  2231. if (changed_flags & IFF_ALLMULTI) {
  2232. bool cur_multipromisc;
  2233. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2234. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2235. vsi->seid,
  2236. cur_multipromisc,
  2237. NULL);
  2238. if (aq_ret) {
  2239. retval = i40e_aq_rc_to_posix(aq_ret,
  2240. hw->aq.asq_last_status);
  2241. dev_info(&pf->pdev->dev,
  2242. "set multi promisc failed on %s, err %s aq_err %s\n",
  2243. vsi_name,
  2244. i40e_stat_str(hw, aq_ret),
  2245. i40e_aq_str(hw, hw->aq.asq_last_status));
  2246. }
  2247. }
  2248. if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
  2249. bool cur_promisc;
  2250. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2251. new_overflow);
  2252. aq_ret = i40e_set_promiscuous(pf, cur_promisc);
  2253. if (aq_ret) {
  2254. retval = i40e_aq_rc_to_posix(aq_ret,
  2255. hw->aq.asq_last_status);
  2256. dev_info(&pf->pdev->dev,
  2257. "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
  2258. cur_promisc ? "on" : "off",
  2259. vsi_name,
  2260. i40e_stat_str(hw, aq_ret),
  2261. i40e_aq_str(hw, hw->aq.asq_last_status));
  2262. }
  2263. }
  2264. out:
  2265. /* if something went wrong then set the changed flag so we try again */
  2266. if (retval)
  2267. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2268. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2269. return retval;
  2270. err_no_memory:
  2271. /* Restore elements on the temporary add and delete lists */
  2272. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2273. err_no_memory_locked:
  2274. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2275. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2276. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2277. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2278. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2279. return -ENOMEM;
  2280. }
  2281. /**
  2282. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2283. * @pf: board private structure
  2284. **/
  2285. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2286. {
  2287. int v;
  2288. if (!pf)
  2289. return;
  2290. if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
  2291. return;
  2292. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2293. if (pf->vsi[v] &&
  2294. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2295. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2296. if (ret) {
  2297. /* come back and try again later */
  2298. set_bit(__I40E_MACVLAN_SYNC_PENDING,
  2299. pf->state);
  2300. break;
  2301. }
  2302. }
  2303. }
  2304. }
  2305. /**
  2306. * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
  2307. * @vsi: the vsi
  2308. **/
  2309. static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
  2310. {
  2311. if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2312. return I40E_RXBUFFER_2048;
  2313. else
  2314. return I40E_RXBUFFER_3072;
  2315. }
  2316. /**
  2317. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2318. * @netdev: network interface device structure
  2319. * @new_mtu: new value for maximum frame size
  2320. *
  2321. * Returns 0 on success, negative on failure
  2322. **/
  2323. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2324. {
  2325. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2326. struct i40e_vsi *vsi = np->vsi;
  2327. struct i40e_pf *pf = vsi->back;
  2328. if (i40e_enabled_xdp_vsi(vsi)) {
  2329. int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  2330. if (frame_size > i40e_max_xdp_frame_size(vsi))
  2331. return -EINVAL;
  2332. }
  2333. netdev_info(netdev, "changing MTU from %d to %d\n",
  2334. netdev->mtu, new_mtu);
  2335. netdev->mtu = new_mtu;
  2336. if (netif_running(netdev))
  2337. i40e_vsi_reinit_locked(vsi);
  2338. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  2339. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  2340. return 0;
  2341. }
  2342. /**
  2343. * i40e_ioctl - Access the hwtstamp interface
  2344. * @netdev: network interface device structure
  2345. * @ifr: interface request data
  2346. * @cmd: ioctl command
  2347. **/
  2348. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2349. {
  2350. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2351. struct i40e_pf *pf = np->vsi->back;
  2352. switch (cmd) {
  2353. case SIOCGHWTSTAMP:
  2354. return i40e_ptp_get_ts_config(pf, ifr);
  2355. case SIOCSHWTSTAMP:
  2356. return i40e_ptp_set_ts_config(pf, ifr);
  2357. default:
  2358. return -EOPNOTSUPP;
  2359. }
  2360. }
  2361. /**
  2362. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2363. * @vsi: the vsi being adjusted
  2364. **/
  2365. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2366. {
  2367. struct i40e_vsi_context ctxt;
  2368. i40e_status ret;
  2369. if ((vsi->info.valid_sections &
  2370. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2371. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2372. return; /* already enabled */
  2373. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2374. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2375. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2376. ctxt.seid = vsi->seid;
  2377. ctxt.info = vsi->info;
  2378. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2379. if (ret) {
  2380. dev_info(&vsi->back->pdev->dev,
  2381. "update vlan stripping failed, err %s aq_err %s\n",
  2382. i40e_stat_str(&vsi->back->hw, ret),
  2383. i40e_aq_str(&vsi->back->hw,
  2384. vsi->back->hw.aq.asq_last_status));
  2385. }
  2386. }
  2387. /**
  2388. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2389. * @vsi: the vsi being adjusted
  2390. **/
  2391. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2392. {
  2393. struct i40e_vsi_context ctxt;
  2394. i40e_status ret;
  2395. if ((vsi->info.valid_sections &
  2396. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2397. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2398. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2399. return; /* already disabled */
  2400. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2401. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2402. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2403. ctxt.seid = vsi->seid;
  2404. ctxt.info = vsi->info;
  2405. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2406. if (ret) {
  2407. dev_info(&vsi->back->pdev->dev,
  2408. "update vlan stripping failed, err %s aq_err %s\n",
  2409. i40e_stat_str(&vsi->back->hw, ret),
  2410. i40e_aq_str(&vsi->back->hw,
  2411. vsi->back->hw.aq.asq_last_status));
  2412. }
  2413. }
  2414. /**
  2415. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2416. * @vsi: the vsi being configured
  2417. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2418. *
  2419. * This is a helper function for adding a new MAC/VLAN filter with the
  2420. * specified VLAN for each existing MAC address already in the hash table.
  2421. * This function does *not* perform any accounting to update filters based on
  2422. * VLAN mode.
  2423. *
  2424. * NOTE: this function expects to be called while under the
  2425. * mac_filter_hash_lock
  2426. **/
  2427. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2428. {
  2429. struct i40e_mac_filter *f, *add_f;
  2430. struct hlist_node *h;
  2431. int bkt;
  2432. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2433. if (f->state == I40E_FILTER_REMOVE)
  2434. continue;
  2435. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2436. if (!add_f) {
  2437. dev_info(&vsi->back->pdev->dev,
  2438. "Could not add vlan filter %d for %pM\n",
  2439. vid, f->macaddr);
  2440. return -ENOMEM;
  2441. }
  2442. }
  2443. return 0;
  2444. }
  2445. /**
  2446. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2447. * @vsi: the VSI being configured
  2448. * @vid: VLAN id to be added
  2449. **/
  2450. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2451. {
  2452. int err;
  2453. if (vsi->info.pvid)
  2454. return -EINVAL;
  2455. /* The network stack will attempt to add VID=0, with the intention to
  2456. * receive priority tagged packets with a VLAN of 0. Our HW receives
  2457. * these packets by default when configured to receive untagged
  2458. * packets, so we don't need to add a filter for this case.
  2459. * Additionally, HW interprets adding a VID=0 filter as meaning to
  2460. * receive *only* tagged traffic and stops receiving untagged traffic.
  2461. * Thus, we do not want to actually add a filter for VID=0
  2462. */
  2463. if (!vid)
  2464. return 0;
  2465. /* Locked once because all functions invoked below iterates list*/
  2466. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2467. err = i40e_add_vlan_all_mac(vsi, vid);
  2468. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2469. if (err)
  2470. return err;
  2471. /* schedule our worker thread which will take care of
  2472. * applying the new filter changes
  2473. */
  2474. i40e_service_event_schedule(vsi->back);
  2475. return 0;
  2476. }
  2477. /**
  2478. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2479. * @vsi: the vsi being configured
  2480. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2481. *
  2482. * This function should be used to remove all VLAN filters which match the
  2483. * given VID. It does not schedule the service event and does not take the
  2484. * mac_filter_hash_lock so it may be combined with other operations under
  2485. * a single invocation of the mac_filter_hash_lock.
  2486. *
  2487. * NOTE: this function expects to be called while under the
  2488. * mac_filter_hash_lock
  2489. */
  2490. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2491. {
  2492. struct i40e_mac_filter *f;
  2493. struct hlist_node *h;
  2494. int bkt;
  2495. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2496. if (f->vlan == vid)
  2497. __i40e_del_filter(vsi, f);
  2498. }
  2499. }
  2500. /**
  2501. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2502. * @vsi: the VSI being configured
  2503. * @vid: VLAN id to be removed
  2504. **/
  2505. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2506. {
  2507. if (!vid || vsi->info.pvid)
  2508. return;
  2509. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2510. i40e_rm_vlan_all_mac(vsi, vid);
  2511. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2512. /* schedule our worker thread which will take care of
  2513. * applying the new filter changes
  2514. */
  2515. i40e_service_event_schedule(vsi->back);
  2516. }
  2517. /**
  2518. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2519. * @netdev: network interface to be adjusted
  2520. * @proto: unused protocol value
  2521. * @vid: vlan id to be added
  2522. *
  2523. * net_device_ops implementation for adding vlan ids
  2524. **/
  2525. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2526. __always_unused __be16 proto, u16 vid)
  2527. {
  2528. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2529. struct i40e_vsi *vsi = np->vsi;
  2530. int ret = 0;
  2531. if (vid >= VLAN_N_VID)
  2532. return -EINVAL;
  2533. ret = i40e_vsi_add_vlan(vsi, vid);
  2534. if (!ret)
  2535. set_bit(vid, vsi->active_vlans);
  2536. return ret;
  2537. }
  2538. /**
  2539. * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
  2540. * @netdev: network interface to be adjusted
  2541. * @proto: unused protocol value
  2542. * @vid: vlan id to be added
  2543. **/
  2544. static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
  2545. __always_unused __be16 proto, u16 vid)
  2546. {
  2547. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2548. struct i40e_vsi *vsi = np->vsi;
  2549. if (vid >= VLAN_N_VID)
  2550. return;
  2551. set_bit(vid, vsi->active_vlans);
  2552. }
  2553. /**
  2554. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2555. * @netdev: network interface to be adjusted
  2556. * @proto: unused protocol value
  2557. * @vid: vlan id to be removed
  2558. *
  2559. * net_device_ops implementation for removing vlan ids
  2560. **/
  2561. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2562. __always_unused __be16 proto, u16 vid)
  2563. {
  2564. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2565. struct i40e_vsi *vsi = np->vsi;
  2566. /* return code is ignored as there is nothing a user
  2567. * can do about failure to remove and a log message was
  2568. * already printed from the other function
  2569. */
  2570. i40e_vsi_kill_vlan(vsi, vid);
  2571. clear_bit(vid, vsi->active_vlans);
  2572. return 0;
  2573. }
  2574. /**
  2575. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2576. * @vsi: the vsi being brought back up
  2577. **/
  2578. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2579. {
  2580. u16 vid;
  2581. if (!vsi->netdev)
  2582. return;
  2583. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  2584. i40e_vlan_stripping_enable(vsi);
  2585. else
  2586. i40e_vlan_stripping_disable(vsi);
  2587. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2588. i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
  2589. vid);
  2590. }
  2591. /**
  2592. * i40e_vsi_add_pvid - Add pvid for the VSI
  2593. * @vsi: the vsi being adjusted
  2594. * @vid: the vlan id to set as a PVID
  2595. **/
  2596. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2597. {
  2598. struct i40e_vsi_context ctxt;
  2599. i40e_status ret;
  2600. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2601. vsi->info.pvid = cpu_to_le16(vid);
  2602. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2603. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2604. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2605. ctxt.seid = vsi->seid;
  2606. ctxt.info = vsi->info;
  2607. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2608. if (ret) {
  2609. dev_info(&vsi->back->pdev->dev,
  2610. "add pvid failed, err %s aq_err %s\n",
  2611. i40e_stat_str(&vsi->back->hw, ret),
  2612. i40e_aq_str(&vsi->back->hw,
  2613. vsi->back->hw.aq.asq_last_status));
  2614. return -ENOENT;
  2615. }
  2616. return 0;
  2617. }
  2618. /**
  2619. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2620. * @vsi: the vsi being adjusted
  2621. *
  2622. * Just use the vlan_rx_register() service to put it back to normal
  2623. **/
  2624. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2625. {
  2626. i40e_vlan_stripping_disable(vsi);
  2627. vsi->info.pvid = 0;
  2628. }
  2629. /**
  2630. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2631. * @vsi: ptr to the VSI
  2632. *
  2633. * If this function returns with an error, then it's possible one or
  2634. * more of the rings is populated (while the rest are not). It is the
  2635. * callers duty to clean those orphaned rings.
  2636. *
  2637. * Return 0 on success, negative on failure
  2638. **/
  2639. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2640. {
  2641. int i, err = 0;
  2642. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2643. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2644. if (!i40e_enabled_xdp_vsi(vsi))
  2645. return err;
  2646. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2647. err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
  2648. return err;
  2649. }
  2650. /**
  2651. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2652. * @vsi: ptr to the VSI
  2653. *
  2654. * Free VSI's transmit software resources
  2655. **/
  2656. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2657. {
  2658. int i;
  2659. if (vsi->tx_rings) {
  2660. for (i = 0; i < vsi->num_queue_pairs; i++)
  2661. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2662. i40e_free_tx_resources(vsi->tx_rings[i]);
  2663. }
  2664. if (vsi->xdp_rings) {
  2665. for (i = 0; i < vsi->num_queue_pairs; i++)
  2666. if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
  2667. i40e_free_tx_resources(vsi->xdp_rings[i]);
  2668. }
  2669. }
  2670. /**
  2671. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2672. * @vsi: ptr to the VSI
  2673. *
  2674. * If this function returns with an error, then it's possible one or
  2675. * more of the rings is populated (while the rest are not). It is the
  2676. * callers duty to clean those orphaned rings.
  2677. *
  2678. * Return 0 on success, negative on failure
  2679. **/
  2680. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2681. {
  2682. int i, err = 0;
  2683. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2684. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2685. return err;
  2686. }
  2687. /**
  2688. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2689. * @vsi: ptr to the VSI
  2690. *
  2691. * Free all receive software resources
  2692. **/
  2693. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2694. {
  2695. int i;
  2696. if (!vsi->rx_rings)
  2697. return;
  2698. for (i = 0; i < vsi->num_queue_pairs; i++)
  2699. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2700. i40e_free_rx_resources(vsi->rx_rings[i]);
  2701. }
  2702. /**
  2703. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2704. * @ring: The Tx ring to configure
  2705. *
  2706. * This enables/disables XPS for a given Tx descriptor ring
  2707. * based on the TCs enabled for the VSI that ring belongs to.
  2708. **/
  2709. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2710. {
  2711. int cpu;
  2712. if (!ring->q_vector || !ring->netdev || ring->ch)
  2713. return;
  2714. /* We only initialize XPS once, so as not to overwrite user settings */
  2715. if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
  2716. return;
  2717. cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
  2718. netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
  2719. ring->queue_index);
  2720. }
  2721. /**
  2722. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2723. * @ring: The Tx ring to configure
  2724. *
  2725. * Configure the Tx descriptor ring in the HMC context.
  2726. **/
  2727. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2728. {
  2729. struct i40e_vsi *vsi = ring->vsi;
  2730. u16 pf_q = vsi->base_queue + ring->queue_index;
  2731. struct i40e_hw *hw = &vsi->back->hw;
  2732. struct i40e_hmc_obj_txq tx_ctx;
  2733. i40e_status err = 0;
  2734. u32 qtx_ctl = 0;
  2735. if (ring_is_xdp(ring))
  2736. ring->xsk_umem = i40e_xsk_umem(ring);
  2737. /* some ATR related tx ring init */
  2738. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2739. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2740. ring->atr_count = 0;
  2741. } else {
  2742. ring->atr_sample_rate = 0;
  2743. }
  2744. /* configure XPS */
  2745. i40e_config_xps_tx_ring(ring);
  2746. /* clear the context structure first */
  2747. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2748. tx_ctx.new_context = 1;
  2749. tx_ctx.base = (ring->dma / 128);
  2750. tx_ctx.qlen = ring->count;
  2751. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2752. I40E_FLAG_FD_ATR_ENABLED));
  2753. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2754. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2755. if (vsi->type != I40E_VSI_FDIR)
  2756. tx_ctx.head_wb_ena = 1;
  2757. tx_ctx.head_wb_addr = ring->dma +
  2758. (ring->count * sizeof(struct i40e_tx_desc));
  2759. /* As part of VSI creation/update, FW allocates certain
  2760. * Tx arbitration queue sets for each TC enabled for
  2761. * the VSI. The FW returns the handles to these queue
  2762. * sets as part of the response buffer to Add VSI,
  2763. * Update VSI, etc. AQ commands. It is expected that
  2764. * these queue set handles be associated with the Tx
  2765. * queues by the driver as part of the TX queue context
  2766. * initialization. This has to be done regardless of
  2767. * DCB as by default everything is mapped to TC0.
  2768. */
  2769. if (ring->ch)
  2770. tx_ctx.rdylist =
  2771. le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
  2772. else
  2773. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2774. tx_ctx.rdylist_act = 0;
  2775. /* clear the context in the HMC */
  2776. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2777. if (err) {
  2778. dev_info(&vsi->back->pdev->dev,
  2779. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2780. ring->queue_index, pf_q, err);
  2781. return -ENOMEM;
  2782. }
  2783. /* set the context in the HMC */
  2784. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2785. if (err) {
  2786. dev_info(&vsi->back->pdev->dev,
  2787. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2788. ring->queue_index, pf_q, err);
  2789. return -ENOMEM;
  2790. }
  2791. /* Now associate this queue with this PCI function */
  2792. if (ring->ch) {
  2793. if (ring->ch->type == I40E_VSI_VMDQ2)
  2794. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2795. else
  2796. return -EINVAL;
  2797. qtx_ctl |= (ring->ch->vsi_number <<
  2798. I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2799. I40E_QTX_CTL_VFVM_INDX_MASK;
  2800. } else {
  2801. if (vsi->type == I40E_VSI_VMDQ2) {
  2802. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2803. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2804. I40E_QTX_CTL_VFVM_INDX_MASK;
  2805. } else {
  2806. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2807. }
  2808. }
  2809. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2810. I40E_QTX_CTL_PF_INDX_MASK);
  2811. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2812. i40e_flush(hw);
  2813. /* cache tail off for easier writes later */
  2814. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2815. return 0;
  2816. }
  2817. /**
  2818. * i40e_configure_rx_ring - Configure a receive ring context
  2819. * @ring: The Rx ring to configure
  2820. *
  2821. * Configure the Rx descriptor ring in the HMC context.
  2822. **/
  2823. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2824. {
  2825. struct i40e_vsi *vsi = ring->vsi;
  2826. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2827. u16 pf_q = vsi->base_queue + ring->queue_index;
  2828. struct i40e_hw *hw = &vsi->back->hw;
  2829. struct i40e_hmc_obj_rxq rx_ctx;
  2830. i40e_status err = 0;
  2831. bool ok;
  2832. int ret;
  2833. bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
  2834. /* clear the context structure first */
  2835. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2836. if (ring->vsi->type == I40E_VSI_MAIN)
  2837. xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
  2838. ring->xsk_umem = i40e_xsk_umem(ring);
  2839. if (ring->xsk_umem) {
  2840. ring->rx_buf_len = ring->xsk_umem->chunk_size_nohr -
  2841. XDP_PACKET_HEADROOM;
  2842. /* For AF_XDP ZC, we disallow packets to span on
  2843. * multiple buffers, thus letting us skip that
  2844. * handling in the fast-path.
  2845. */
  2846. chain_len = 1;
  2847. ring->zca.free = i40e_zca_free;
  2848. ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
  2849. MEM_TYPE_ZERO_COPY,
  2850. &ring->zca);
  2851. if (ret)
  2852. return ret;
  2853. dev_info(&vsi->back->pdev->dev,
  2854. "Registered XDP mem model MEM_TYPE_ZERO_COPY on Rx ring %d\n",
  2855. ring->queue_index);
  2856. } else {
  2857. ring->rx_buf_len = vsi->rx_buf_len;
  2858. if (ring->vsi->type == I40E_VSI_MAIN) {
  2859. ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
  2860. MEM_TYPE_PAGE_SHARED,
  2861. NULL);
  2862. if (ret)
  2863. return ret;
  2864. }
  2865. }
  2866. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2867. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2868. rx_ctx.base = (ring->dma / 128);
  2869. rx_ctx.qlen = ring->count;
  2870. /* use 32 byte descriptors */
  2871. rx_ctx.dsize = 1;
  2872. /* descriptor type is always zero
  2873. * rx_ctx.dtype = 0;
  2874. */
  2875. rx_ctx.hsplit_0 = 0;
  2876. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2877. if (hw->revision_id == 0)
  2878. rx_ctx.lrxqthresh = 0;
  2879. else
  2880. rx_ctx.lrxqthresh = 1;
  2881. rx_ctx.crcstrip = 1;
  2882. rx_ctx.l2tsel = 1;
  2883. /* this controls whether VLAN is stripped from inner headers */
  2884. rx_ctx.showiv = 0;
  2885. /* set the prefena field to 1 because the manual says to */
  2886. rx_ctx.prefena = 1;
  2887. /* clear the context in the HMC */
  2888. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2889. if (err) {
  2890. dev_info(&vsi->back->pdev->dev,
  2891. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2892. ring->queue_index, pf_q, err);
  2893. return -ENOMEM;
  2894. }
  2895. /* set the context in the HMC */
  2896. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2897. if (err) {
  2898. dev_info(&vsi->back->pdev->dev,
  2899. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2900. ring->queue_index, pf_q, err);
  2901. return -ENOMEM;
  2902. }
  2903. /* configure Rx buffer alignment */
  2904. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2905. clear_ring_build_skb_enabled(ring);
  2906. else
  2907. set_ring_build_skb_enabled(ring);
  2908. /* cache tail for quicker writes, and clear the reg before use */
  2909. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2910. writel(0, ring->tail);
  2911. ok = ring->xsk_umem ?
  2912. i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring)) :
  2913. !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2914. if (!ok) {
  2915. dev_info(&vsi->back->pdev->dev,
  2916. "Failed allocate some buffers on %sRx ring %d (pf_q %d)\n",
  2917. ring->xsk_umem ? "UMEM enabled " : "",
  2918. ring->queue_index, pf_q);
  2919. }
  2920. return 0;
  2921. }
  2922. /**
  2923. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2924. * @vsi: VSI structure describing this set of rings and resources
  2925. *
  2926. * Configure the Tx VSI for operation.
  2927. **/
  2928. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2929. {
  2930. int err = 0;
  2931. u16 i;
  2932. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2933. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2934. if (!i40e_enabled_xdp_vsi(vsi))
  2935. return err;
  2936. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2937. err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
  2938. return err;
  2939. }
  2940. /**
  2941. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2942. * @vsi: the VSI being configured
  2943. *
  2944. * Configure the Rx VSI for operation.
  2945. **/
  2946. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2947. {
  2948. int err = 0;
  2949. u16 i;
  2950. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2951. vsi->max_frame = I40E_MAX_RXBUFFER;
  2952. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2953. #if (PAGE_SIZE < 8192)
  2954. } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  2955. (vsi->netdev->mtu <= ETH_DATA_LEN)) {
  2956. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2957. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2958. #endif
  2959. } else {
  2960. vsi->max_frame = I40E_MAX_RXBUFFER;
  2961. vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
  2962. I40E_RXBUFFER_2048;
  2963. }
  2964. /* set up individual rings */
  2965. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2966. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2967. return err;
  2968. }
  2969. /**
  2970. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2971. * @vsi: ptr to the VSI
  2972. **/
  2973. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2974. {
  2975. struct i40e_ring *tx_ring, *rx_ring;
  2976. u16 qoffset, qcount;
  2977. int i, n;
  2978. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2979. /* Reset the TC information */
  2980. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2981. rx_ring = vsi->rx_rings[i];
  2982. tx_ring = vsi->tx_rings[i];
  2983. rx_ring->dcb_tc = 0;
  2984. tx_ring->dcb_tc = 0;
  2985. }
  2986. return;
  2987. }
  2988. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2989. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2990. continue;
  2991. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2992. qcount = vsi->tc_config.tc_info[n].qcount;
  2993. for (i = qoffset; i < (qoffset + qcount); i++) {
  2994. rx_ring = vsi->rx_rings[i];
  2995. tx_ring = vsi->tx_rings[i];
  2996. rx_ring->dcb_tc = n;
  2997. tx_ring->dcb_tc = n;
  2998. }
  2999. }
  3000. }
  3001. /**
  3002. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  3003. * @vsi: ptr to the VSI
  3004. **/
  3005. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  3006. {
  3007. if (vsi->netdev)
  3008. i40e_set_rx_mode(vsi->netdev);
  3009. }
  3010. /**
  3011. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  3012. * @vsi: Pointer to the targeted VSI
  3013. *
  3014. * This function replays the hlist on the hw where all the SB Flow Director
  3015. * filters were saved.
  3016. **/
  3017. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  3018. {
  3019. struct i40e_fdir_filter *filter;
  3020. struct i40e_pf *pf = vsi->back;
  3021. struct hlist_node *node;
  3022. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  3023. return;
  3024. /* Reset FDir counters as we're replaying all existing filters */
  3025. pf->fd_tcp4_filter_cnt = 0;
  3026. pf->fd_udp4_filter_cnt = 0;
  3027. pf->fd_sctp4_filter_cnt = 0;
  3028. pf->fd_ip4_filter_cnt = 0;
  3029. hlist_for_each_entry_safe(filter, node,
  3030. &pf->fdir_filter_list, fdir_node) {
  3031. i40e_add_del_fdir(vsi, filter, true);
  3032. }
  3033. }
  3034. /**
  3035. * i40e_vsi_configure - Set up the VSI for action
  3036. * @vsi: the VSI being configured
  3037. **/
  3038. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  3039. {
  3040. int err;
  3041. i40e_set_vsi_rx_mode(vsi);
  3042. i40e_restore_vlan(vsi);
  3043. i40e_vsi_config_dcb_rings(vsi);
  3044. err = i40e_vsi_configure_tx(vsi);
  3045. if (!err)
  3046. err = i40e_vsi_configure_rx(vsi);
  3047. return err;
  3048. }
  3049. /**
  3050. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  3051. * @vsi: the VSI being configured
  3052. **/
  3053. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  3054. {
  3055. bool has_xdp = i40e_enabled_xdp_vsi(vsi);
  3056. struct i40e_pf *pf = vsi->back;
  3057. struct i40e_hw *hw = &pf->hw;
  3058. u16 vector;
  3059. int i, q;
  3060. u32 qp;
  3061. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  3062. * and PFINT_LNKLSTn registers, e.g.:
  3063. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  3064. */
  3065. qp = vsi->base_queue;
  3066. vector = vsi->base_vector;
  3067. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  3068. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  3069. q_vector->rx.next_update = jiffies + 1;
  3070. q_vector->rx.target_itr =
  3071. ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
  3072. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  3073. q_vector->rx.target_itr);
  3074. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3075. q_vector->tx.next_update = jiffies + 1;
  3076. q_vector->tx.target_itr =
  3077. ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
  3078. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  3079. q_vector->tx.target_itr);
  3080. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3081. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  3082. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  3083. /* Linked list for the queuepairs assigned to this vector */
  3084. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  3085. for (q = 0; q < q_vector->num_ringpairs; q++) {
  3086. u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
  3087. u32 val;
  3088. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3089. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3090. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  3091. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  3092. (I40E_QUEUE_TYPE_TX <<
  3093. I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  3094. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3095. if (has_xdp) {
  3096. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3097. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3098. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3099. (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3100. (I40E_QUEUE_TYPE_TX <<
  3101. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3102. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3103. }
  3104. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3105. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3106. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3107. ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3108. (I40E_QUEUE_TYPE_RX <<
  3109. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3110. /* Terminate the linked list */
  3111. if (q == (q_vector->num_ringpairs - 1))
  3112. val |= (I40E_QUEUE_END_OF_LIST <<
  3113. I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3114. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3115. qp++;
  3116. }
  3117. }
  3118. i40e_flush(hw);
  3119. }
  3120. /**
  3121. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  3122. * @pf: pointer to private device data structure
  3123. **/
  3124. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  3125. {
  3126. struct i40e_hw *hw = &pf->hw;
  3127. u32 val;
  3128. /* clear things first */
  3129. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  3130. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  3131. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  3132. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  3133. I40E_PFINT_ICR0_ENA_GRST_MASK |
  3134. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  3135. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  3136. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  3137. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  3138. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3139. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  3140. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3141. if (pf->flags & I40E_FLAG_PTP)
  3142. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3143. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  3144. /* SW_ITR_IDX = 0, but don't change INTENA */
  3145. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  3146. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  3147. /* OTHER_ITR_IDX = 0 */
  3148. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  3149. }
  3150. /**
  3151. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  3152. * @vsi: the VSI being configured
  3153. **/
  3154. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  3155. {
  3156. u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
  3157. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3158. struct i40e_pf *pf = vsi->back;
  3159. struct i40e_hw *hw = &pf->hw;
  3160. u32 val;
  3161. /* set the ITR configuration */
  3162. q_vector->rx.next_update = jiffies + 1;
  3163. q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
  3164. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
  3165. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3166. q_vector->tx.next_update = jiffies + 1;
  3167. q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
  3168. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
  3169. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3170. i40e_enable_misc_int_causes(pf);
  3171. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  3172. wr32(hw, I40E_PFINT_LNKLST0, 0);
  3173. /* Associate the queue pair to the vector and enable the queue int */
  3174. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3175. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3176. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  3177. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3178. wr32(hw, I40E_QINT_RQCTL(0), val);
  3179. if (i40e_enabled_xdp_vsi(vsi)) {
  3180. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3181. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
  3182. (I40E_QUEUE_TYPE_TX
  3183. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3184. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3185. }
  3186. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3187. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3188. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3189. wr32(hw, I40E_QINT_TQCTL(0), val);
  3190. i40e_flush(hw);
  3191. }
  3192. /**
  3193. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  3194. * @pf: board private structure
  3195. **/
  3196. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  3197. {
  3198. struct i40e_hw *hw = &pf->hw;
  3199. wr32(hw, I40E_PFINT_DYN_CTL0,
  3200. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  3201. i40e_flush(hw);
  3202. }
  3203. /**
  3204. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  3205. * @pf: board private structure
  3206. **/
  3207. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  3208. {
  3209. struct i40e_hw *hw = &pf->hw;
  3210. u32 val;
  3211. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3212. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  3213. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  3214. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  3215. i40e_flush(hw);
  3216. }
  3217. /**
  3218. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3219. * @irq: interrupt number
  3220. * @data: pointer to a q_vector
  3221. **/
  3222. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3223. {
  3224. struct i40e_q_vector *q_vector = data;
  3225. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3226. return IRQ_HANDLED;
  3227. napi_schedule_irqoff(&q_vector->napi);
  3228. return IRQ_HANDLED;
  3229. }
  3230. /**
  3231. * i40e_irq_affinity_notify - Callback for affinity changes
  3232. * @notify: context as to what irq was changed
  3233. * @mask: the new affinity mask
  3234. *
  3235. * This is a callback function used by the irq_set_affinity_notifier function
  3236. * so that we may register to receive changes to the irq affinity masks.
  3237. **/
  3238. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3239. const cpumask_t *mask)
  3240. {
  3241. struct i40e_q_vector *q_vector =
  3242. container_of(notify, struct i40e_q_vector, affinity_notify);
  3243. cpumask_copy(&q_vector->affinity_mask, mask);
  3244. }
  3245. /**
  3246. * i40e_irq_affinity_release - Callback for affinity notifier release
  3247. * @ref: internal core kernel usage
  3248. *
  3249. * This is a callback function used by the irq_set_affinity_notifier function
  3250. * to inform the current notification subscriber that they will no longer
  3251. * receive notifications.
  3252. **/
  3253. static void i40e_irq_affinity_release(struct kref *ref) {}
  3254. /**
  3255. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3256. * @vsi: the VSI being configured
  3257. * @basename: name for the vector
  3258. *
  3259. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3260. **/
  3261. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3262. {
  3263. int q_vectors = vsi->num_q_vectors;
  3264. struct i40e_pf *pf = vsi->back;
  3265. int base = vsi->base_vector;
  3266. int rx_int_idx = 0;
  3267. int tx_int_idx = 0;
  3268. int vector, err;
  3269. int irq_num;
  3270. int cpu;
  3271. for (vector = 0; vector < q_vectors; vector++) {
  3272. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3273. irq_num = pf->msix_entries[base + vector].vector;
  3274. if (q_vector->tx.ring && q_vector->rx.ring) {
  3275. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3276. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3277. tx_int_idx++;
  3278. } else if (q_vector->rx.ring) {
  3279. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3280. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3281. } else if (q_vector->tx.ring) {
  3282. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3283. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3284. } else {
  3285. /* skip this unused q_vector */
  3286. continue;
  3287. }
  3288. err = request_irq(irq_num,
  3289. vsi->irq_handler,
  3290. 0,
  3291. q_vector->name,
  3292. q_vector);
  3293. if (err) {
  3294. dev_info(&pf->pdev->dev,
  3295. "MSIX request_irq failed, error: %d\n", err);
  3296. goto free_queue_irqs;
  3297. }
  3298. /* register for affinity change notifications */
  3299. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3300. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3301. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3302. /* Spread affinity hints out across online CPUs.
  3303. *
  3304. * get_cpu_mask returns a static constant mask with
  3305. * a permanent lifetime so it's ok to pass to
  3306. * irq_set_affinity_hint without making a copy.
  3307. */
  3308. cpu = cpumask_local_spread(q_vector->v_idx, -1);
  3309. irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
  3310. }
  3311. vsi->irqs_ready = true;
  3312. return 0;
  3313. free_queue_irqs:
  3314. while (vector) {
  3315. vector--;
  3316. irq_num = pf->msix_entries[base + vector].vector;
  3317. irq_set_affinity_notifier(irq_num, NULL);
  3318. irq_set_affinity_hint(irq_num, NULL);
  3319. free_irq(irq_num, &vsi->q_vectors[vector]);
  3320. }
  3321. return err;
  3322. }
  3323. /**
  3324. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3325. * @vsi: the VSI being un-configured
  3326. **/
  3327. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3328. {
  3329. struct i40e_pf *pf = vsi->back;
  3330. struct i40e_hw *hw = &pf->hw;
  3331. int base = vsi->base_vector;
  3332. int i;
  3333. /* disable interrupt causation from each queue */
  3334. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3335. u32 val;
  3336. val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3337. val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  3338. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3339. val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
  3340. val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  3341. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
  3342. if (!i40e_enabled_xdp_vsi(vsi))
  3343. continue;
  3344. wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
  3345. }
  3346. /* disable each interrupt */
  3347. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3348. for (i = vsi->base_vector;
  3349. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3350. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3351. i40e_flush(hw);
  3352. for (i = 0; i < vsi->num_q_vectors; i++)
  3353. synchronize_irq(pf->msix_entries[i + base].vector);
  3354. } else {
  3355. /* Legacy and MSI mode - this stops all interrupt handling */
  3356. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3357. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3358. i40e_flush(hw);
  3359. synchronize_irq(pf->pdev->irq);
  3360. }
  3361. }
  3362. /**
  3363. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3364. * @vsi: the VSI being configured
  3365. **/
  3366. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3367. {
  3368. struct i40e_pf *pf = vsi->back;
  3369. int i;
  3370. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3371. for (i = 0; i < vsi->num_q_vectors; i++)
  3372. i40e_irq_dynamic_enable(vsi, i);
  3373. } else {
  3374. i40e_irq_dynamic_enable_icr0(pf);
  3375. }
  3376. i40e_flush(&pf->hw);
  3377. return 0;
  3378. }
  3379. /**
  3380. * i40e_free_misc_vector - Free the vector that handles non-queue events
  3381. * @pf: board private structure
  3382. **/
  3383. static void i40e_free_misc_vector(struct i40e_pf *pf)
  3384. {
  3385. /* Disable ICR 0 */
  3386. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3387. i40e_flush(&pf->hw);
  3388. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3389. synchronize_irq(pf->msix_entries[0].vector);
  3390. free_irq(pf->msix_entries[0].vector, pf);
  3391. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  3392. }
  3393. }
  3394. /**
  3395. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3396. * @irq: interrupt number
  3397. * @data: pointer to a q_vector
  3398. *
  3399. * This is the handler used for all MSI/Legacy interrupts, and deals
  3400. * with both queue and non-queue interrupts. This is also used in
  3401. * MSIX mode to handle the non-queue interrupts.
  3402. **/
  3403. static irqreturn_t i40e_intr(int irq, void *data)
  3404. {
  3405. struct i40e_pf *pf = (struct i40e_pf *)data;
  3406. struct i40e_hw *hw = &pf->hw;
  3407. irqreturn_t ret = IRQ_NONE;
  3408. u32 icr0, icr0_remaining;
  3409. u32 val, ena_mask;
  3410. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3411. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3412. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3413. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3414. goto enable_intr;
  3415. /* if interrupt but no bits showing, must be SWINT */
  3416. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3417. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3418. pf->sw_int_count++;
  3419. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3420. (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3421. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3422. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3423. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  3424. }
  3425. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3426. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3427. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3428. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3429. /* We do not have a way to disarm Queue causes while leaving
  3430. * interrupt enabled for all other causes, ideally
  3431. * interrupt should be disabled while we are in NAPI but
  3432. * this is not a performance path and napi_schedule()
  3433. * can deal with rescheduling.
  3434. */
  3435. if (!test_bit(__I40E_DOWN, pf->state))
  3436. napi_schedule_irqoff(&q_vector->napi);
  3437. }
  3438. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3439. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3440. set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  3441. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3442. }
  3443. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3444. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3445. set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  3446. }
  3447. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3448. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3449. set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3450. }
  3451. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3452. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  3453. set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  3454. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3455. val = rd32(hw, I40E_GLGEN_RSTAT);
  3456. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3457. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3458. if (val == I40E_RESET_CORER) {
  3459. pf->corer_count++;
  3460. } else if (val == I40E_RESET_GLOBR) {
  3461. pf->globr_count++;
  3462. } else if (val == I40E_RESET_EMPR) {
  3463. pf->empr_count++;
  3464. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
  3465. }
  3466. }
  3467. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3468. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3469. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3470. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3471. rd32(hw, I40E_PFHMC_ERRORINFO),
  3472. rd32(hw, I40E_PFHMC_ERRORDATA));
  3473. }
  3474. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3475. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3476. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3477. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3478. i40e_ptp_tx_hwtstamp(pf);
  3479. }
  3480. }
  3481. /* If a critical error is pending we have no choice but to reset the
  3482. * device.
  3483. * Report and mask out any remaining unexpected interrupts.
  3484. */
  3485. icr0_remaining = icr0 & ena_mask;
  3486. if (icr0_remaining) {
  3487. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3488. icr0_remaining);
  3489. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3490. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3491. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3492. dev_info(&pf->pdev->dev, "device will be reset\n");
  3493. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  3494. i40e_service_event_schedule(pf);
  3495. }
  3496. ena_mask &= ~icr0_remaining;
  3497. }
  3498. ret = IRQ_HANDLED;
  3499. enable_intr:
  3500. /* re-enable interrupt causes */
  3501. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3502. if (!test_bit(__I40E_DOWN, pf->state)) {
  3503. i40e_service_event_schedule(pf);
  3504. i40e_irq_dynamic_enable_icr0(pf);
  3505. }
  3506. return ret;
  3507. }
  3508. /**
  3509. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3510. * @tx_ring: tx ring to clean
  3511. * @budget: how many cleans we're allowed
  3512. *
  3513. * Returns true if there's any budget left (e.g. the clean is finished)
  3514. **/
  3515. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3516. {
  3517. struct i40e_vsi *vsi = tx_ring->vsi;
  3518. u16 i = tx_ring->next_to_clean;
  3519. struct i40e_tx_buffer *tx_buf;
  3520. struct i40e_tx_desc *tx_desc;
  3521. tx_buf = &tx_ring->tx_bi[i];
  3522. tx_desc = I40E_TX_DESC(tx_ring, i);
  3523. i -= tx_ring->count;
  3524. do {
  3525. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3526. /* if next_to_watch is not set then there is no work pending */
  3527. if (!eop_desc)
  3528. break;
  3529. /* prevent any other reads prior to eop_desc */
  3530. smp_rmb();
  3531. /* if the descriptor isn't done, no work yet to do */
  3532. if (!(eop_desc->cmd_type_offset_bsz &
  3533. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3534. break;
  3535. /* clear next_to_watch to prevent false hangs */
  3536. tx_buf->next_to_watch = NULL;
  3537. tx_desc->buffer_addr = 0;
  3538. tx_desc->cmd_type_offset_bsz = 0;
  3539. /* move past filter desc */
  3540. tx_buf++;
  3541. tx_desc++;
  3542. i++;
  3543. if (unlikely(!i)) {
  3544. i -= tx_ring->count;
  3545. tx_buf = tx_ring->tx_bi;
  3546. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3547. }
  3548. /* unmap skb header data */
  3549. dma_unmap_single(tx_ring->dev,
  3550. dma_unmap_addr(tx_buf, dma),
  3551. dma_unmap_len(tx_buf, len),
  3552. DMA_TO_DEVICE);
  3553. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3554. kfree(tx_buf->raw_buf);
  3555. tx_buf->raw_buf = NULL;
  3556. tx_buf->tx_flags = 0;
  3557. tx_buf->next_to_watch = NULL;
  3558. dma_unmap_len_set(tx_buf, len, 0);
  3559. tx_desc->buffer_addr = 0;
  3560. tx_desc->cmd_type_offset_bsz = 0;
  3561. /* move us past the eop_desc for start of next FD desc */
  3562. tx_buf++;
  3563. tx_desc++;
  3564. i++;
  3565. if (unlikely(!i)) {
  3566. i -= tx_ring->count;
  3567. tx_buf = tx_ring->tx_bi;
  3568. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3569. }
  3570. /* update budget accounting */
  3571. budget--;
  3572. } while (likely(budget));
  3573. i += tx_ring->count;
  3574. tx_ring->next_to_clean = i;
  3575. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3576. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3577. return budget > 0;
  3578. }
  3579. /**
  3580. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3581. * @irq: interrupt number
  3582. * @data: pointer to a q_vector
  3583. **/
  3584. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3585. {
  3586. struct i40e_q_vector *q_vector = data;
  3587. struct i40e_vsi *vsi;
  3588. if (!q_vector->tx.ring)
  3589. return IRQ_HANDLED;
  3590. vsi = q_vector->tx.ring->vsi;
  3591. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3592. return IRQ_HANDLED;
  3593. }
  3594. /**
  3595. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3596. * @vsi: the VSI being configured
  3597. * @v_idx: vector index
  3598. * @qp_idx: queue pair index
  3599. **/
  3600. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3601. {
  3602. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3603. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3604. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3605. tx_ring->q_vector = q_vector;
  3606. tx_ring->next = q_vector->tx.ring;
  3607. q_vector->tx.ring = tx_ring;
  3608. q_vector->tx.count++;
  3609. /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
  3610. if (i40e_enabled_xdp_vsi(vsi)) {
  3611. struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
  3612. xdp_ring->q_vector = q_vector;
  3613. xdp_ring->next = q_vector->tx.ring;
  3614. q_vector->tx.ring = xdp_ring;
  3615. q_vector->tx.count++;
  3616. }
  3617. rx_ring->q_vector = q_vector;
  3618. rx_ring->next = q_vector->rx.ring;
  3619. q_vector->rx.ring = rx_ring;
  3620. q_vector->rx.count++;
  3621. }
  3622. /**
  3623. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3624. * @vsi: the VSI being configured
  3625. *
  3626. * This function maps descriptor rings to the queue-specific vectors
  3627. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3628. * one vector per queue pair, but on a constrained vector budget, we
  3629. * group the queue pairs as "efficiently" as possible.
  3630. **/
  3631. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3632. {
  3633. int qp_remaining = vsi->num_queue_pairs;
  3634. int q_vectors = vsi->num_q_vectors;
  3635. int num_ringpairs;
  3636. int v_start = 0;
  3637. int qp_idx = 0;
  3638. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3639. * group them so there are multiple queues per vector.
  3640. * It is also important to go through all the vectors available to be
  3641. * sure that if we don't use all the vectors, that the remaining vectors
  3642. * are cleared. This is especially important when decreasing the
  3643. * number of queues in use.
  3644. */
  3645. for (; v_start < q_vectors; v_start++) {
  3646. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3647. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3648. q_vector->num_ringpairs = num_ringpairs;
  3649. q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
  3650. q_vector->rx.count = 0;
  3651. q_vector->tx.count = 0;
  3652. q_vector->rx.ring = NULL;
  3653. q_vector->tx.ring = NULL;
  3654. while (num_ringpairs--) {
  3655. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3656. qp_idx++;
  3657. qp_remaining--;
  3658. }
  3659. }
  3660. }
  3661. /**
  3662. * i40e_vsi_request_irq - Request IRQ from the OS
  3663. * @vsi: the VSI being configured
  3664. * @basename: name for the vector
  3665. **/
  3666. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3667. {
  3668. struct i40e_pf *pf = vsi->back;
  3669. int err;
  3670. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3671. err = i40e_vsi_request_irq_msix(vsi, basename);
  3672. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3673. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3674. pf->int_name, pf);
  3675. else
  3676. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3677. pf->int_name, pf);
  3678. if (err)
  3679. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3680. return err;
  3681. }
  3682. #ifdef CONFIG_NET_POLL_CONTROLLER
  3683. /**
  3684. * i40e_netpoll - A Polling 'interrupt' handler
  3685. * @netdev: network interface device structure
  3686. *
  3687. * This is used by netconsole to send skbs without having to re-enable
  3688. * interrupts. It's not called while the normal interrupt routine is executing.
  3689. **/
  3690. static void i40e_netpoll(struct net_device *netdev)
  3691. {
  3692. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3693. struct i40e_vsi *vsi = np->vsi;
  3694. struct i40e_pf *pf = vsi->back;
  3695. int i;
  3696. /* if interface is down do nothing */
  3697. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3698. return;
  3699. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3700. for (i = 0; i < vsi->num_q_vectors; i++)
  3701. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3702. } else {
  3703. i40e_intr(pf->pdev->irq, netdev);
  3704. }
  3705. }
  3706. #endif
  3707. #define I40E_QTX_ENA_WAIT_COUNT 50
  3708. /**
  3709. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3710. * @pf: the PF being configured
  3711. * @pf_q: the PF queue
  3712. * @enable: enable or disable state of the queue
  3713. *
  3714. * This routine will wait for the given Tx queue of the PF to reach the
  3715. * enabled or disabled state.
  3716. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3717. * multiple retries; else will return 0 in case of success.
  3718. **/
  3719. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3720. {
  3721. int i;
  3722. u32 tx_reg;
  3723. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3724. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3725. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3726. break;
  3727. usleep_range(10, 20);
  3728. }
  3729. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3730. return -ETIMEDOUT;
  3731. return 0;
  3732. }
  3733. /**
  3734. * i40e_control_tx_q - Start or stop a particular Tx queue
  3735. * @pf: the PF structure
  3736. * @pf_q: the PF queue to configure
  3737. * @enable: start or stop the queue
  3738. *
  3739. * This function enables or disables a single queue. Note that any delay
  3740. * required after the operation is expected to be handled by the caller of
  3741. * this function.
  3742. **/
  3743. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3744. {
  3745. struct i40e_hw *hw = &pf->hw;
  3746. u32 tx_reg;
  3747. int i;
  3748. /* warn the TX unit of coming changes */
  3749. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3750. if (!enable)
  3751. usleep_range(10, 20);
  3752. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3753. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3754. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3755. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3756. break;
  3757. usleep_range(1000, 2000);
  3758. }
  3759. /* Skip if the queue is already in the requested state */
  3760. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3761. return;
  3762. /* turn on/off the queue */
  3763. if (enable) {
  3764. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3765. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3766. } else {
  3767. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3768. }
  3769. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3770. }
  3771. /**
  3772. * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
  3773. * @seid: VSI SEID
  3774. * @pf: the PF structure
  3775. * @pf_q: the PF queue to configure
  3776. * @is_xdp: true if the queue is used for XDP
  3777. * @enable: start or stop the queue
  3778. **/
  3779. int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
  3780. bool is_xdp, bool enable)
  3781. {
  3782. int ret;
  3783. i40e_control_tx_q(pf, pf_q, enable);
  3784. /* wait for the change to finish */
  3785. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3786. if (ret) {
  3787. dev_info(&pf->pdev->dev,
  3788. "VSI seid %d %sTx ring %d %sable timeout\n",
  3789. seid, (is_xdp ? "XDP " : ""), pf_q,
  3790. (enable ? "en" : "dis"));
  3791. }
  3792. return ret;
  3793. }
  3794. /**
  3795. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3796. * @vsi: the VSI being configured
  3797. * @enable: start or stop the rings
  3798. **/
  3799. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3800. {
  3801. struct i40e_pf *pf = vsi->back;
  3802. int i, pf_q, ret = 0;
  3803. pf_q = vsi->base_queue;
  3804. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3805. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3806. pf_q,
  3807. false /*is xdp*/, enable);
  3808. if (ret)
  3809. break;
  3810. if (!i40e_enabled_xdp_vsi(vsi))
  3811. continue;
  3812. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3813. pf_q + vsi->alloc_queue_pairs,
  3814. true /*is xdp*/, enable);
  3815. if (ret)
  3816. break;
  3817. }
  3818. return ret;
  3819. }
  3820. /**
  3821. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3822. * @pf: the PF being configured
  3823. * @pf_q: the PF queue
  3824. * @enable: enable or disable state of the queue
  3825. *
  3826. * This routine will wait for the given Rx queue of the PF to reach the
  3827. * enabled or disabled state.
  3828. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3829. * multiple retries; else will return 0 in case of success.
  3830. **/
  3831. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3832. {
  3833. int i;
  3834. u32 rx_reg;
  3835. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3836. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3837. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3838. break;
  3839. usleep_range(10, 20);
  3840. }
  3841. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3842. return -ETIMEDOUT;
  3843. return 0;
  3844. }
  3845. /**
  3846. * i40e_control_rx_q - Start or stop a particular Rx queue
  3847. * @pf: the PF structure
  3848. * @pf_q: the PF queue to configure
  3849. * @enable: start or stop the queue
  3850. *
  3851. * This function enables or disables a single queue. Note that
  3852. * any delay required after the operation is expected to be
  3853. * handled by the caller of this function.
  3854. **/
  3855. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3856. {
  3857. struct i40e_hw *hw = &pf->hw;
  3858. u32 rx_reg;
  3859. int i;
  3860. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3861. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3862. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3863. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3864. break;
  3865. usleep_range(1000, 2000);
  3866. }
  3867. /* Skip if the queue is already in the requested state */
  3868. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3869. return;
  3870. /* turn on/off the queue */
  3871. if (enable)
  3872. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3873. else
  3874. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3875. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3876. }
  3877. /**
  3878. * i40e_control_wait_rx_q
  3879. * @pf: the PF structure
  3880. * @pf_q: queue being configured
  3881. * @enable: start or stop the rings
  3882. *
  3883. * This function enables or disables a single queue along with waiting
  3884. * for the change to finish. The caller of this function should handle
  3885. * the delays needed in the case of disabling queues.
  3886. **/
  3887. int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3888. {
  3889. int ret = 0;
  3890. i40e_control_rx_q(pf, pf_q, enable);
  3891. /* wait for the change to finish */
  3892. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3893. if (ret)
  3894. return ret;
  3895. return ret;
  3896. }
  3897. /**
  3898. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3899. * @vsi: the VSI being configured
  3900. * @enable: start or stop the rings
  3901. **/
  3902. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3903. {
  3904. struct i40e_pf *pf = vsi->back;
  3905. int i, pf_q, ret = 0;
  3906. pf_q = vsi->base_queue;
  3907. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3908. ret = i40e_control_wait_rx_q(pf, pf_q, enable);
  3909. if (ret) {
  3910. dev_info(&pf->pdev->dev,
  3911. "VSI seid %d Rx ring %d %sable timeout\n",
  3912. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3913. break;
  3914. }
  3915. }
  3916. /* Due to HW errata, on Rx disable only, the register can indicate done
  3917. * before it really is. Needs 50ms to be sure
  3918. */
  3919. if (!enable)
  3920. mdelay(50);
  3921. return ret;
  3922. }
  3923. /**
  3924. * i40e_vsi_start_rings - Start a VSI's rings
  3925. * @vsi: the VSI being configured
  3926. **/
  3927. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3928. {
  3929. int ret = 0;
  3930. /* do rx first for enable and last for disable */
  3931. ret = i40e_vsi_control_rx(vsi, true);
  3932. if (ret)
  3933. return ret;
  3934. ret = i40e_vsi_control_tx(vsi, true);
  3935. return ret;
  3936. }
  3937. /**
  3938. * i40e_vsi_stop_rings - Stop a VSI's rings
  3939. * @vsi: the VSI being configured
  3940. **/
  3941. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3942. {
  3943. /* When port TX is suspended, don't wait */
  3944. if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
  3945. return i40e_vsi_stop_rings_no_wait(vsi);
  3946. /* do rx first for enable and last for disable
  3947. * Ignore return value, we need to shutdown whatever we can
  3948. */
  3949. i40e_vsi_control_tx(vsi, false);
  3950. i40e_vsi_control_rx(vsi, false);
  3951. }
  3952. /**
  3953. * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
  3954. * @vsi: the VSI being shutdown
  3955. *
  3956. * This function stops all the rings for a VSI but does not delay to verify
  3957. * that rings have been disabled. It is expected that the caller is shutting
  3958. * down multiple VSIs at once and will delay together for all the VSIs after
  3959. * initiating the shutdown. This is particularly useful for shutting down lots
  3960. * of VFs together. Otherwise, a large delay can be incurred while configuring
  3961. * each VSI in serial.
  3962. **/
  3963. void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
  3964. {
  3965. struct i40e_pf *pf = vsi->back;
  3966. int i, pf_q;
  3967. pf_q = vsi->base_queue;
  3968. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3969. i40e_control_tx_q(pf, pf_q, false);
  3970. i40e_control_rx_q(pf, pf_q, false);
  3971. }
  3972. }
  3973. /**
  3974. * i40e_vsi_free_irq - Free the irq association with the OS
  3975. * @vsi: the VSI being configured
  3976. **/
  3977. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3978. {
  3979. struct i40e_pf *pf = vsi->back;
  3980. struct i40e_hw *hw = &pf->hw;
  3981. int base = vsi->base_vector;
  3982. u32 val, qp;
  3983. int i;
  3984. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3985. if (!vsi->q_vectors)
  3986. return;
  3987. if (!vsi->irqs_ready)
  3988. return;
  3989. vsi->irqs_ready = false;
  3990. for (i = 0; i < vsi->num_q_vectors; i++) {
  3991. int irq_num;
  3992. u16 vector;
  3993. vector = i + base;
  3994. irq_num = pf->msix_entries[vector].vector;
  3995. /* free only the irqs that were actually requested */
  3996. if (!vsi->q_vectors[i] ||
  3997. !vsi->q_vectors[i]->num_ringpairs)
  3998. continue;
  3999. /* clear the affinity notifier in the IRQ descriptor */
  4000. irq_set_affinity_notifier(irq_num, NULL);
  4001. /* remove our suggested affinity mask for this IRQ */
  4002. irq_set_affinity_hint(irq_num, NULL);
  4003. synchronize_irq(irq_num);
  4004. free_irq(irq_num, vsi->q_vectors[i]);
  4005. /* Tear down the interrupt queue link list
  4006. *
  4007. * We know that they come in pairs and always
  4008. * the Rx first, then the Tx. To clear the
  4009. * link list, stick the EOL value into the
  4010. * next_q field of the registers.
  4011. */
  4012. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  4013. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  4014. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  4015. val |= I40E_QUEUE_END_OF_LIST
  4016. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  4017. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  4018. while (qp != I40E_QUEUE_END_OF_LIST) {
  4019. u32 next;
  4020. val = rd32(hw, I40E_QINT_RQCTL(qp));
  4021. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  4022. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  4023. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  4024. I40E_QINT_RQCTL_INTEVENT_MASK);
  4025. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  4026. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  4027. wr32(hw, I40E_QINT_RQCTL(qp), val);
  4028. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4029. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  4030. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  4031. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4032. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4033. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4034. I40E_QINT_TQCTL_INTEVENT_MASK);
  4035. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4036. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4037. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4038. qp = next;
  4039. }
  4040. }
  4041. } else {
  4042. free_irq(pf->pdev->irq, pf);
  4043. val = rd32(hw, I40E_PFINT_LNKLST0);
  4044. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  4045. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  4046. val |= I40E_QUEUE_END_OF_LIST
  4047. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  4048. wr32(hw, I40E_PFINT_LNKLST0, val);
  4049. val = rd32(hw, I40E_QINT_RQCTL(qp));
  4050. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  4051. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  4052. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  4053. I40E_QINT_RQCTL_INTEVENT_MASK);
  4054. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  4055. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  4056. wr32(hw, I40E_QINT_RQCTL(qp), val);
  4057. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4058. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4059. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4060. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4061. I40E_QINT_TQCTL_INTEVENT_MASK);
  4062. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4063. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4064. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4065. }
  4066. }
  4067. /**
  4068. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  4069. * @vsi: the VSI being configured
  4070. * @v_idx: Index of vector to be freed
  4071. *
  4072. * This function frees the memory allocated to the q_vector. In addition if
  4073. * NAPI is enabled it will delete any references to the NAPI struct prior
  4074. * to freeing the q_vector.
  4075. **/
  4076. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  4077. {
  4078. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  4079. struct i40e_ring *ring;
  4080. if (!q_vector)
  4081. return;
  4082. /* disassociate q_vector from rings */
  4083. i40e_for_each_ring(ring, q_vector->tx)
  4084. ring->q_vector = NULL;
  4085. i40e_for_each_ring(ring, q_vector->rx)
  4086. ring->q_vector = NULL;
  4087. /* only VSI w/ an associated netdev is set up w/ NAPI */
  4088. if (vsi->netdev)
  4089. netif_napi_del(&q_vector->napi);
  4090. vsi->q_vectors[v_idx] = NULL;
  4091. kfree_rcu(q_vector, rcu);
  4092. }
  4093. /**
  4094. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  4095. * @vsi: the VSI being un-configured
  4096. *
  4097. * This frees the memory allocated to the q_vectors and
  4098. * deletes references to the NAPI struct.
  4099. **/
  4100. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  4101. {
  4102. int v_idx;
  4103. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  4104. i40e_free_q_vector(vsi, v_idx);
  4105. }
  4106. /**
  4107. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  4108. * @pf: board private structure
  4109. **/
  4110. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  4111. {
  4112. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  4113. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  4114. pci_disable_msix(pf->pdev);
  4115. kfree(pf->msix_entries);
  4116. pf->msix_entries = NULL;
  4117. kfree(pf->irq_pile);
  4118. pf->irq_pile = NULL;
  4119. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  4120. pci_disable_msi(pf->pdev);
  4121. }
  4122. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  4123. }
  4124. /**
  4125. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  4126. * @pf: board private structure
  4127. *
  4128. * We go through and clear interrupt specific resources and reset the structure
  4129. * to pre-load conditions
  4130. **/
  4131. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  4132. {
  4133. int i;
  4134. i40e_free_misc_vector(pf);
  4135. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  4136. I40E_IWARP_IRQ_PILE_ID);
  4137. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  4138. for (i = 0; i < pf->num_alloc_vsi; i++)
  4139. if (pf->vsi[i])
  4140. i40e_vsi_free_q_vectors(pf->vsi[i]);
  4141. i40e_reset_interrupt_capability(pf);
  4142. }
  4143. /**
  4144. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  4145. * @vsi: the VSI being configured
  4146. **/
  4147. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  4148. {
  4149. int q_idx;
  4150. if (!vsi->netdev)
  4151. return;
  4152. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4153. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4154. if (q_vector->rx.ring || q_vector->tx.ring)
  4155. napi_enable(&q_vector->napi);
  4156. }
  4157. }
  4158. /**
  4159. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  4160. * @vsi: the VSI being configured
  4161. **/
  4162. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  4163. {
  4164. int q_idx;
  4165. if (!vsi->netdev)
  4166. return;
  4167. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4168. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4169. if (q_vector->rx.ring || q_vector->tx.ring)
  4170. napi_disable(&q_vector->napi);
  4171. }
  4172. }
  4173. /**
  4174. * i40e_vsi_close - Shut down a VSI
  4175. * @vsi: the vsi to be quelled
  4176. **/
  4177. static void i40e_vsi_close(struct i40e_vsi *vsi)
  4178. {
  4179. struct i40e_pf *pf = vsi->back;
  4180. if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
  4181. i40e_down(vsi);
  4182. i40e_vsi_free_irq(vsi);
  4183. i40e_vsi_free_tx_resources(vsi);
  4184. i40e_vsi_free_rx_resources(vsi);
  4185. vsi->current_netdev_flags = 0;
  4186. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  4187. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  4188. set_bit(__I40E_CLIENT_RESET, pf->state);
  4189. }
  4190. /**
  4191. * i40e_quiesce_vsi - Pause a given VSI
  4192. * @vsi: the VSI being paused
  4193. **/
  4194. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  4195. {
  4196. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  4197. return;
  4198. set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
  4199. if (vsi->netdev && netif_running(vsi->netdev))
  4200. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4201. else
  4202. i40e_vsi_close(vsi);
  4203. }
  4204. /**
  4205. * i40e_unquiesce_vsi - Resume a given VSI
  4206. * @vsi: the VSI being resumed
  4207. **/
  4208. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  4209. {
  4210. if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
  4211. return;
  4212. if (vsi->netdev && netif_running(vsi->netdev))
  4213. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4214. else
  4215. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  4216. }
  4217. /**
  4218. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  4219. * @pf: the PF
  4220. **/
  4221. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  4222. {
  4223. int v;
  4224. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4225. if (pf->vsi[v])
  4226. i40e_quiesce_vsi(pf->vsi[v]);
  4227. }
  4228. }
  4229. /**
  4230. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  4231. * @pf: the PF
  4232. **/
  4233. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  4234. {
  4235. int v;
  4236. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4237. if (pf->vsi[v])
  4238. i40e_unquiesce_vsi(pf->vsi[v]);
  4239. }
  4240. }
  4241. /**
  4242. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  4243. * @vsi: the VSI being configured
  4244. *
  4245. * Wait until all queues on a given VSI have been disabled.
  4246. **/
  4247. int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  4248. {
  4249. struct i40e_pf *pf = vsi->back;
  4250. int i, pf_q, ret;
  4251. pf_q = vsi->base_queue;
  4252. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4253. /* Check and wait for the Tx queue */
  4254. ret = i40e_pf_txq_wait(pf, pf_q, false);
  4255. if (ret) {
  4256. dev_info(&pf->pdev->dev,
  4257. "VSI seid %d Tx ring %d disable timeout\n",
  4258. vsi->seid, pf_q);
  4259. return ret;
  4260. }
  4261. if (!i40e_enabled_xdp_vsi(vsi))
  4262. goto wait_rx;
  4263. /* Check and wait for the XDP Tx queue */
  4264. ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
  4265. false);
  4266. if (ret) {
  4267. dev_info(&pf->pdev->dev,
  4268. "VSI seid %d XDP Tx ring %d disable timeout\n",
  4269. vsi->seid, pf_q);
  4270. return ret;
  4271. }
  4272. wait_rx:
  4273. /* Check and wait for the Rx queue */
  4274. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  4275. if (ret) {
  4276. dev_info(&pf->pdev->dev,
  4277. "VSI seid %d Rx ring %d disable timeout\n",
  4278. vsi->seid, pf_q);
  4279. return ret;
  4280. }
  4281. }
  4282. return 0;
  4283. }
  4284. #ifdef CONFIG_I40E_DCB
  4285. /**
  4286. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  4287. * @pf: the PF
  4288. *
  4289. * This function waits for the queues to be in disabled state for all the
  4290. * VSIs that are managed by this PF.
  4291. **/
  4292. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  4293. {
  4294. int v, ret = 0;
  4295. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4296. if (pf->vsi[v]) {
  4297. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  4298. if (ret)
  4299. break;
  4300. }
  4301. }
  4302. return ret;
  4303. }
  4304. #endif
  4305. /**
  4306. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4307. * @pf: pointer to PF
  4308. *
  4309. * Get TC map for ISCSI PF type that will include iSCSI TC
  4310. * and LAN TC.
  4311. **/
  4312. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4313. {
  4314. struct i40e_dcb_app_priority_table app;
  4315. struct i40e_hw *hw = &pf->hw;
  4316. u8 enabled_tc = 1; /* TC0 is always enabled */
  4317. u8 tc, i;
  4318. /* Get the iSCSI APP TLV */
  4319. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4320. for (i = 0; i < dcbcfg->numapps; i++) {
  4321. app = dcbcfg->app[i];
  4322. if (app.selector == I40E_APP_SEL_TCPIP &&
  4323. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4324. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4325. enabled_tc |= BIT(tc);
  4326. break;
  4327. }
  4328. }
  4329. return enabled_tc;
  4330. }
  4331. /**
  4332. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4333. * @dcbcfg: the corresponding DCBx configuration structure
  4334. *
  4335. * Return the number of TCs from given DCBx configuration
  4336. **/
  4337. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4338. {
  4339. int i, tc_unused = 0;
  4340. u8 num_tc = 0;
  4341. u8 ret = 0;
  4342. /* Scan the ETS Config Priority Table to find
  4343. * traffic class enabled for a given priority
  4344. * and create a bitmask of enabled TCs
  4345. */
  4346. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4347. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4348. /* Now scan the bitmask to check for
  4349. * contiguous TCs starting with TC0
  4350. */
  4351. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4352. if (num_tc & BIT(i)) {
  4353. if (!tc_unused) {
  4354. ret++;
  4355. } else {
  4356. pr_err("Non-contiguous TC - Disabling DCB\n");
  4357. return 1;
  4358. }
  4359. } else {
  4360. tc_unused = 1;
  4361. }
  4362. }
  4363. /* There is always at least TC0 */
  4364. if (!ret)
  4365. ret = 1;
  4366. return ret;
  4367. }
  4368. /**
  4369. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4370. * @dcbcfg: the corresponding DCBx configuration structure
  4371. *
  4372. * Query the current DCB configuration and return the number of
  4373. * traffic classes enabled from the given DCBX config
  4374. **/
  4375. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4376. {
  4377. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4378. u8 enabled_tc = 1;
  4379. u8 i;
  4380. for (i = 0; i < num_tc; i++)
  4381. enabled_tc |= BIT(i);
  4382. return enabled_tc;
  4383. }
  4384. /**
  4385. * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
  4386. * @pf: PF being queried
  4387. *
  4388. * Query the current MQPRIO configuration and return the number of
  4389. * traffic classes enabled.
  4390. **/
  4391. static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
  4392. {
  4393. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  4394. u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
  4395. u8 enabled_tc = 1, i;
  4396. for (i = 1; i < num_tc; i++)
  4397. enabled_tc |= BIT(i);
  4398. return enabled_tc;
  4399. }
  4400. /**
  4401. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4402. * @pf: PF being queried
  4403. *
  4404. * Return number of traffic classes enabled for the given PF
  4405. **/
  4406. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4407. {
  4408. struct i40e_hw *hw = &pf->hw;
  4409. u8 i, enabled_tc = 1;
  4410. u8 num_tc = 0;
  4411. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4412. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4413. return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
  4414. /* If neither MQPRIO nor DCB is enabled, then always use single TC */
  4415. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4416. return 1;
  4417. /* SFP mode will be enabled for all TCs on port */
  4418. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4419. return i40e_dcb_get_num_tc(dcbcfg);
  4420. /* MFP mode return count of enabled TCs for this PF */
  4421. if (pf->hw.func_caps.iscsi)
  4422. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4423. else
  4424. return 1; /* Only TC0 */
  4425. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4426. if (enabled_tc & BIT(i))
  4427. num_tc++;
  4428. }
  4429. return num_tc;
  4430. }
  4431. /**
  4432. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4433. * @pf: PF being queried
  4434. *
  4435. * Return a bitmap for enabled traffic classes for this PF.
  4436. **/
  4437. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4438. {
  4439. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4440. return i40e_mqprio_get_enabled_tc(pf);
  4441. /* If neither MQPRIO nor DCB is enabled for this PF then just return
  4442. * default TC
  4443. */
  4444. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4445. return I40E_DEFAULT_TRAFFIC_CLASS;
  4446. /* SFP mode we want PF to be enabled for all TCs */
  4447. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4448. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4449. /* MFP enabled and iSCSI PF type */
  4450. if (pf->hw.func_caps.iscsi)
  4451. return i40e_get_iscsi_tc_map(pf);
  4452. else
  4453. return I40E_DEFAULT_TRAFFIC_CLASS;
  4454. }
  4455. /**
  4456. * i40e_vsi_get_bw_info - Query VSI BW Information
  4457. * @vsi: the VSI being queried
  4458. *
  4459. * Returns 0 on success, negative value on failure
  4460. **/
  4461. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4462. {
  4463. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4464. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4465. struct i40e_pf *pf = vsi->back;
  4466. struct i40e_hw *hw = &pf->hw;
  4467. i40e_status ret;
  4468. u32 tc_bw_max;
  4469. int i;
  4470. /* Get the VSI level BW configuration */
  4471. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4472. if (ret) {
  4473. dev_info(&pf->pdev->dev,
  4474. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4475. i40e_stat_str(&pf->hw, ret),
  4476. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4477. return -EINVAL;
  4478. }
  4479. /* Get the VSI level BW configuration per TC */
  4480. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4481. NULL);
  4482. if (ret) {
  4483. dev_info(&pf->pdev->dev,
  4484. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4485. i40e_stat_str(&pf->hw, ret),
  4486. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4487. return -EINVAL;
  4488. }
  4489. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4490. dev_info(&pf->pdev->dev,
  4491. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4492. bw_config.tc_valid_bits,
  4493. bw_ets_config.tc_valid_bits);
  4494. /* Still continuing */
  4495. }
  4496. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4497. vsi->bw_max_quanta = bw_config.max_bw;
  4498. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4499. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4500. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4501. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4502. vsi->bw_ets_limit_credits[i] =
  4503. le16_to_cpu(bw_ets_config.credits[i]);
  4504. /* 3 bits out of 4 for each TC */
  4505. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4506. }
  4507. return 0;
  4508. }
  4509. /**
  4510. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4511. * @vsi: the VSI being configured
  4512. * @enabled_tc: TC bitmap
  4513. * @bw_share: BW shared credits per TC
  4514. *
  4515. * Returns 0 on success, negative value on failure
  4516. **/
  4517. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4518. u8 *bw_share)
  4519. {
  4520. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4521. struct i40e_pf *pf = vsi->back;
  4522. i40e_status ret;
  4523. int i;
  4524. /* There is no need to reset BW when mqprio mode is on. */
  4525. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4526. return 0;
  4527. if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4528. ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
  4529. if (ret)
  4530. dev_info(&pf->pdev->dev,
  4531. "Failed to reset tx rate for vsi->seid %u\n",
  4532. vsi->seid);
  4533. return ret;
  4534. }
  4535. bw_data.tc_valid_bits = enabled_tc;
  4536. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4537. bw_data.tc_bw_credits[i] = bw_share[i];
  4538. ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
  4539. if (ret) {
  4540. dev_info(&pf->pdev->dev,
  4541. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4542. pf->hw.aq.asq_last_status);
  4543. return -EINVAL;
  4544. }
  4545. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4546. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4547. return 0;
  4548. }
  4549. /**
  4550. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4551. * @vsi: the VSI being configured
  4552. * @enabled_tc: TC map to be enabled
  4553. *
  4554. **/
  4555. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4556. {
  4557. struct net_device *netdev = vsi->netdev;
  4558. struct i40e_pf *pf = vsi->back;
  4559. struct i40e_hw *hw = &pf->hw;
  4560. u8 netdev_tc = 0;
  4561. int i;
  4562. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4563. if (!netdev)
  4564. return;
  4565. if (!enabled_tc) {
  4566. netdev_reset_tc(netdev);
  4567. return;
  4568. }
  4569. /* Set up actual enabled TCs on the VSI */
  4570. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4571. return;
  4572. /* set per TC queues for the VSI */
  4573. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4574. /* Only set TC queues for enabled tcs
  4575. *
  4576. * e.g. For a VSI that has TC0 and TC3 enabled the
  4577. * enabled_tc bitmap would be 0x00001001; the driver
  4578. * will set the numtc for netdev as 2 that will be
  4579. * referenced by the netdev layer as TC 0 and 1.
  4580. */
  4581. if (vsi->tc_config.enabled_tc & BIT(i))
  4582. netdev_set_tc_queue(netdev,
  4583. vsi->tc_config.tc_info[i].netdev_tc,
  4584. vsi->tc_config.tc_info[i].qcount,
  4585. vsi->tc_config.tc_info[i].qoffset);
  4586. }
  4587. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4588. return;
  4589. /* Assign UP2TC map for the VSI */
  4590. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4591. /* Get the actual TC# for the UP */
  4592. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4593. /* Get the mapped netdev TC# for the UP */
  4594. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4595. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4596. }
  4597. }
  4598. /**
  4599. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4600. * @vsi: the VSI being configured
  4601. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4602. **/
  4603. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4604. struct i40e_vsi_context *ctxt)
  4605. {
  4606. /* copy just the sections touched not the entire info
  4607. * since not all sections are valid as returned by
  4608. * update vsi params
  4609. */
  4610. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4611. memcpy(&vsi->info.queue_mapping,
  4612. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4613. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4614. sizeof(vsi->info.tc_mapping));
  4615. }
  4616. /**
  4617. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4618. * @vsi: VSI to be configured
  4619. * @enabled_tc: TC bitmap
  4620. *
  4621. * This configures a particular VSI for TCs that are mapped to the
  4622. * given TC bitmap. It uses default bandwidth share for TCs across
  4623. * VSIs to configure TC for a particular VSI.
  4624. *
  4625. * NOTE:
  4626. * It is expected that the VSI queues have been quisced before calling
  4627. * this function.
  4628. **/
  4629. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4630. {
  4631. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4632. struct i40e_pf *pf = vsi->back;
  4633. struct i40e_hw *hw = &pf->hw;
  4634. struct i40e_vsi_context ctxt;
  4635. int ret = 0;
  4636. int i;
  4637. /* Check if enabled_tc is same as existing or new TCs */
  4638. if (vsi->tc_config.enabled_tc == enabled_tc &&
  4639. vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
  4640. return ret;
  4641. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4642. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4643. if (enabled_tc & BIT(i))
  4644. bw_share[i] = 1;
  4645. }
  4646. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4647. if (ret) {
  4648. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4649. dev_info(&pf->pdev->dev,
  4650. "Failed configuring TC map %d for VSI %d\n",
  4651. enabled_tc, vsi->seid);
  4652. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
  4653. &bw_config, NULL);
  4654. if (ret) {
  4655. dev_info(&pf->pdev->dev,
  4656. "Failed querying vsi bw info, err %s aq_err %s\n",
  4657. i40e_stat_str(hw, ret),
  4658. i40e_aq_str(hw, hw->aq.asq_last_status));
  4659. goto out;
  4660. }
  4661. if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
  4662. u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
  4663. if (!valid_tc)
  4664. valid_tc = bw_config.tc_valid_bits;
  4665. /* Always enable TC0, no matter what */
  4666. valid_tc |= 1;
  4667. dev_info(&pf->pdev->dev,
  4668. "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
  4669. enabled_tc, bw_config.tc_valid_bits, valid_tc);
  4670. enabled_tc = valid_tc;
  4671. }
  4672. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4673. if (ret) {
  4674. dev_err(&pf->pdev->dev,
  4675. "Unable to configure TC map %d for VSI %d\n",
  4676. enabled_tc, vsi->seid);
  4677. goto out;
  4678. }
  4679. }
  4680. /* Update Queue Pairs Mapping for currently enabled UPs */
  4681. ctxt.seid = vsi->seid;
  4682. ctxt.pf_num = vsi->back->hw.pf_id;
  4683. ctxt.vf_num = 0;
  4684. ctxt.uplink_seid = vsi->uplink_seid;
  4685. ctxt.info = vsi->info;
  4686. if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
  4687. ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
  4688. if (ret)
  4689. goto out;
  4690. } else {
  4691. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4692. }
  4693. /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
  4694. * queues changed.
  4695. */
  4696. if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
  4697. vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
  4698. vsi->num_queue_pairs);
  4699. ret = i40e_vsi_config_rss(vsi);
  4700. if (ret) {
  4701. dev_info(&vsi->back->pdev->dev,
  4702. "Failed to reconfig rss for num_queues\n");
  4703. return ret;
  4704. }
  4705. vsi->reconfig_rss = false;
  4706. }
  4707. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4708. ctxt.info.valid_sections |=
  4709. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4710. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4711. }
  4712. /* Update the VSI after updating the VSI queue-mapping
  4713. * information
  4714. */
  4715. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  4716. if (ret) {
  4717. dev_info(&pf->pdev->dev,
  4718. "Update vsi tc config failed, err %s aq_err %s\n",
  4719. i40e_stat_str(hw, ret),
  4720. i40e_aq_str(hw, hw->aq.asq_last_status));
  4721. goto out;
  4722. }
  4723. /* update the local VSI info with updated queue map */
  4724. i40e_vsi_update_queue_map(vsi, &ctxt);
  4725. vsi->info.valid_sections = 0;
  4726. /* Update current VSI BW information */
  4727. ret = i40e_vsi_get_bw_info(vsi);
  4728. if (ret) {
  4729. dev_info(&pf->pdev->dev,
  4730. "Failed updating vsi bw info, err %s aq_err %s\n",
  4731. i40e_stat_str(hw, ret),
  4732. i40e_aq_str(hw, hw->aq.asq_last_status));
  4733. goto out;
  4734. }
  4735. /* Update the netdev TC setup */
  4736. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4737. out:
  4738. return ret;
  4739. }
  4740. /**
  4741. * i40e_get_link_speed - Returns link speed for the interface
  4742. * @vsi: VSI to be configured
  4743. *
  4744. **/
  4745. static int i40e_get_link_speed(struct i40e_vsi *vsi)
  4746. {
  4747. struct i40e_pf *pf = vsi->back;
  4748. switch (pf->hw.phy.link_info.link_speed) {
  4749. case I40E_LINK_SPEED_40GB:
  4750. return 40000;
  4751. case I40E_LINK_SPEED_25GB:
  4752. return 25000;
  4753. case I40E_LINK_SPEED_20GB:
  4754. return 20000;
  4755. case I40E_LINK_SPEED_10GB:
  4756. return 10000;
  4757. case I40E_LINK_SPEED_1GB:
  4758. return 1000;
  4759. default:
  4760. return -EINVAL;
  4761. }
  4762. }
  4763. /**
  4764. * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
  4765. * @vsi: VSI to be configured
  4766. * @seid: seid of the channel/VSI
  4767. * @max_tx_rate: max TX rate to be configured as BW limit
  4768. *
  4769. * Helper function to set BW limit for a given VSI
  4770. **/
  4771. int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
  4772. {
  4773. struct i40e_pf *pf = vsi->back;
  4774. u64 credits = 0;
  4775. int speed = 0;
  4776. int ret = 0;
  4777. speed = i40e_get_link_speed(vsi);
  4778. if (max_tx_rate > speed) {
  4779. dev_err(&pf->pdev->dev,
  4780. "Invalid max tx rate %llu specified for VSI seid %d.",
  4781. max_tx_rate, seid);
  4782. return -EINVAL;
  4783. }
  4784. if (max_tx_rate && max_tx_rate < 50) {
  4785. dev_warn(&pf->pdev->dev,
  4786. "Setting max tx rate to minimum usable value of 50Mbps.\n");
  4787. max_tx_rate = 50;
  4788. }
  4789. /* Tx rate credits are in values of 50Mbps, 0 is disabled */
  4790. credits = max_tx_rate;
  4791. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  4792. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
  4793. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  4794. if (ret)
  4795. dev_err(&pf->pdev->dev,
  4796. "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
  4797. max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
  4798. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4799. return ret;
  4800. }
  4801. /**
  4802. * i40e_remove_queue_channels - Remove queue channels for the TCs
  4803. * @vsi: VSI to be configured
  4804. *
  4805. * Remove queue channels for the TCs
  4806. **/
  4807. static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
  4808. {
  4809. enum i40e_admin_queue_err last_aq_status;
  4810. struct i40e_cloud_filter *cfilter;
  4811. struct i40e_channel *ch, *ch_tmp;
  4812. struct i40e_pf *pf = vsi->back;
  4813. struct hlist_node *node;
  4814. int ret, i;
  4815. /* Reset rss size that was stored when reconfiguring rss for
  4816. * channel VSIs with non-power-of-2 queue count.
  4817. */
  4818. vsi->current_rss_size = 0;
  4819. /* perform cleanup for channels if they exist */
  4820. if (list_empty(&vsi->ch_list))
  4821. return;
  4822. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4823. struct i40e_vsi *p_vsi;
  4824. list_del(&ch->list);
  4825. p_vsi = ch->parent_vsi;
  4826. if (!p_vsi || !ch->initialized) {
  4827. kfree(ch);
  4828. continue;
  4829. }
  4830. /* Reset queue contexts */
  4831. for (i = 0; i < ch->num_queue_pairs; i++) {
  4832. struct i40e_ring *tx_ring, *rx_ring;
  4833. u16 pf_q;
  4834. pf_q = ch->base_queue + i;
  4835. tx_ring = vsi->tx_rings[pf_q];
  4836. tx_ring->ch = NULL;
  4837. rx_ring = vsi->rx_rings[pf_q];
  4838. rx_ring->ch = NULL;
  4839. }
  4840. /* Reset BW configured for this VSI via mqprio */
  4841. ret = i40e_set_bw_limit(vsi, ch->seid, 0);
  4842. if (ret)
  4843. dev_info(&vsi->back->pdev->dev,
  4844. "Failed to reset tx rate for ch->seid %u\n",
  4845. ch->seid);
  4846. /* delete cloud filters associated with this channel */
  4847. hlist_for_each_entry_safe(cfilter, node,
  4848. &pf->cloud_filter_list, cloud_node) {
  4849. if (cfilter->seid != ch->seid)
  4850. continue;
  4851. hash_del(&cfilter->cloud_node);
  4852. if (cfilter->dst_port)
  4853. ret = i40e_add_del_cloud_filter_big_buf(vsi,
  4854. cfilter,
  4855. false);
  4856. else
  4857. ret = i40e_add_del_cloud_filter(vsi, cfilter,
  4858. false);
  4859. last_aq_status = pf->hw.aq.asq_last_status;
  4860. if (ret)
  4861. dev_info(&pf->pdev->dev,
  4862. "Failed to delete cloud filter, err %s aq_err %s\n",
  4863. i40e_stat_str(&pf->hw, ret),
  4864. i40e_aq_str(&pf->hw, last_aq_status));
  4865. kfree(cfilter);
  4866. }
  4867. /* delete VSI from FW */
  4868. ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
  4869. NULL);
  4870. if (ret)
  4871. dev_err(&vsi->back->pdev->dev,
  4872. "unable to remove channel (%d) for parent VSI(%d)\n",
  4873. ch->seid, p_vsi->seid);
  4874. kfree(ch);
  4875. }
  4876. INIT_LIST_HEAD(&vsi->ch_list);
  4877. }
  4878. /**
  4879. * i40e_is_any_channel - channel exist or not
  4880. * @vsi: ptr to VSI to which channels are associated with
  4881. *
  4882. * Returns true or false if channel(s) exist for associated VSI or not
  4883. **/
  4884. static bool i40e_is_any_channel(struct i40e_vsi *vsi)
  4885. {
  4886. struct i40e_channel *ch, *ch_tmp;
  4887. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4888. if (ch->initialized)
  4889. return true;
  4890. }
  4891. return false;
  4892. }
  4893. /**
  4894. * i40e_get_max_queues_for_channel
  4895. * @vsi: ptr to VSI to which channels are associated with
  4896. *
  4897. * Helper function which returns max value among the queue counts set on the
  4898. * channels/TCs created.
  4899. **/
  4900. static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
  4901. {
  4902. struct i40e_channel *ch, *ch_tmp;
  4903. int max = 0;
  4904. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4905. if (!ch->initialized)
  4906. continue;
  4907. if (ch->num_queue_pairs > max)
  4908. max = ch->num_queue_pairs;
  4909. }
  4910. return max;
  4911. }
  4912. /**
  4913. * i40e_validate_num_queues - validate num_queues w.r.t channel
  4914. * @pf: ptr to PF device
  4915. * @num_queues: number of queues
  4916. * @vsi: the parent VSI
  4917. * @reconfig_rss: indicates should the RSS be reconfigured or not
  4918. *
  4919. * This function validates number of queues in the context of new channel
  4920. * which is being established and determines if RSS should be reconfigured
  4921. * or not for parent VSI.
  4922. **/
  4923. static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
  4924. struct i40e_vsi *vsi, bool *reconfig_rss)
  4925. {
  4926. int max_ch_queues;
  4927. if (!reconfig_rss)
  4928. return -EINVAL;
  4929. *reconfig_rss = false;
  4930. if (vsi->current_rss_size) {
  4931. if (num_queues > vsi->current_rss_size) {
  4932. dev_dbg(&pf->pdev->dev,
  4933. "Error: num_queues (%d) > vsi's current_size(%d)\n",
  4934. num_queues, vsi->current_rss_size);
  4935. return -EINVAL;
  4936. } else if ((num_queues < vsi->current_rss_size) &&
  4937. (!is_power_of_2(num_queues))) {
  4938. dev_dbg(&pf->pdev->dev,
  4939. "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
  4940. num_queues, vsi->current_rss_size);
  4941. return -EINVAL;
  4942. }
  4943. }
  4944. if (!is_power_of_2(num_queues)) {
  4945. /* Find the max num_queues configured for channel if channel
  4946. * exist.
  4947. * if channel exist, then enforce 'num_queues' to be more than
  4948. * max ever queues configured for channel.
  4949. */
  4950. max_ch_queues = i40e_get_max_queues_for_channel(vsi);
  4951. if (num_queues < max_ch_queues) {
  4952. dev_dbg(&pf->pdev->dev,
  4953. "Error: num_queues (%d) < max queues configured for channel(%d)\n",
  4954. num_queues, max_ch_queues);
  4955. return -EINVAL;
  4956. }
  4957. *reconfig_rss = true;
  4958. }
  4959. return 0;
  4960. }
  4961. /**
  4962. * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
  4963. * @vsi: the VSI being setup
  4964. * @rss_size: size of RSS, accordingly LUT gets reprogrammed
  4965. *
  4966. * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
  4967. **/
  4968. static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
  4969. {
  4970. struct i40e_pf *pf = vsi->back;
  4971. u8 seed[I40E_HKEY_ARRAY_SIZE];
  4972. struct i40e_hw *hw = &pf->hw;
  4973. int local_rss_size;
  4974. u8 *lut;
  4975. int ret;
  4976. if (!vsi->rss_size)
  4977. return -EINVAL;
  4978. if (rss_size > vsi->rss_size)
  4979. return -EINVAL;
  4980. local_rss_size = min_t(int, vsi->rss_size, rss_size);
  4981. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  4982. if (!lut)
  4983. return -ENOMEM;
  4984. /* Ignoring user configured lut if there is one */
  4985. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
  4986. /* Use user configured hash key if there is one, otherwise
  4987. * use default.
  4988. */
  4989. if (vsi->rss_hkey_user)
  4990. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  4991. else
  4992. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  4993. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  4994. if (ret) {
  4995. dev_info(&pf->pdev->dev,
  4996. "Cannot set RSS lut, err %s aq_err %s\n",
  4997. i40e_stat_str(hw, ret),
  4998. i40e_aq_str(hw, hw->aq.asq_last_status));
  4999. kfree(lut);
  5000. return ret;
  5001. }
  5002. kfree(lut);
  5003. /* Do the update w.r.t. storing rss_size */
  5004. if (!vsi->orig_rss_size)
  5005. vsi->orig_rss_size = vsi->rss_size;
  5006. vsi->current_rss_size = local_rss_size;
  5007. return ret;
  5008. }
  5009. /**
  5010. * i40e_channel_setup_queue_map - Setup a channel queue map
  5011. * @pf: ptr to PF device
  5012. * @vsi: the VSI being setup
  5013. * @ctxt: VSI context structure
  5014. * @ch: ptr to channel structure
  5015. *
  5016. * Setup queue map for a specific channel
  5017. **/
  5018. static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
  5019. struct i40e_vsi_context *ctxt,
  5020. struct i40e_channel *ch)
  5021. {
  5022. u16 qcount, qmap, sections = 0;
  5023. u8 offset = 0;
  5024. int pow;
  5025. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  5026. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  5027. qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
  5028. ch->num_queue_pairs = qcount;
  5029. /* find the next higher power-of-2 of num queue pairs */
  5030. pow = ilog2(qcount);
  5031. if (!is_power_of_2(qcount))
  5032. pow++;
  5033. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  5034. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  5035. /* Setup queue TC[0].qmap for given VSI context */
  5036. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  5037. ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
  5038. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  5039. ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
  5040. ctxt->info.valid_sections |= cpu_to_le16(sections);
  5041. }
  5042. /**
  5043. * i40e_add_channel - add a channel by adding VSI
  5044. * @pf: ptr to PF device
  5045. * @uplink_seid: underlying HW switching element (VEB) ID
  5046. * @ch: ptr to channel structure
  5047. *
  5048. * Add a channel (VSI) using add_vsi and queue_map
  5049. **/
  5050. static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
  5051. struct i40e_channel *ch)
  5052. {
  5053. struct i40e_hw *hw = &pf->hw;
  5054. struct i40e_vsi_context ctxt;
  5055. u8 enabled_tc = 0x1; /* TC0 enabled */
  5056. int ret;
  5057. if (ch->type != I40E_VSI_VMDQ2) {
  5058. dev_info(&pf->pdev->dev,
  5059. "add new vsi failed, ch->type %d\n", ch->type);
  5060. return -EINVAL;
  5061. }
  5062. memset(&ctxt, 0, sizeof(ctxt));
  5063. ctxt.pf_num = hw->pf_id;
  5064. ctxt.vf_num = 0;
  5065. ctxt.uplink_seid = uplink_seid;
  5066. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  5067. if (ch->type == I40E_VSI_VMDQ2)
  5068. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  5069. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
  5070. ctxt.info.valid_sections |=
  5071. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5072. ctxt.info.switch_id =
  5073. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5074. }
  5075. /* Set queue map for a given VSI context */
  5076. i40e_channel_setup_queue_map(pf, &ctxt, ch);
  5077. /* Now time to create VSI */
  5078. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  5079. if (ret) {
  5080. dev_info(&pf->pdev->dev,
  5081. "add new vsi failed, err %s aq_err %s\n",
  5082. i40e_stat_str(&pf->hw, ret),
  5083. i40e_aq_str(&pf->hw,
  5084. pf->hw.aq.asq_last_status));
  5085. return -ENOENT;
  5086. }
  5087. /* Success, update channel */
  5088. ch->enabled_tc = enabled_tc;
  5089. ch->seid = ctxt.seid;
  5090. ch->vsi_number = ctxt.vsi_number;
  5091. ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
  5092. /* copy just the sections touched not the entire info
  5093. * since not all sections are valid as returned by
  5094. * update vsi params
  5095. */
  5096. ch->info.mapping_flags = ctxt.info.mapping_flags;
  5097. memcpy(&ch->info.queue_mapping,
  5098. &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
  5099. memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
  5100. sizeof(ctxt.info.tc_mapping));
  5101. return 0;
  5102. }
  5103. static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
  5104. u8 *bw_share)
  5105. {
  5106. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  5107. i40e_status ret;
  5108. int i;
  5109. bw_data.tc_valid_bits = ch->enabled_tc;
  5110. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5111. bw_data.tc_bw_credits[i] = bw_share[i];
  5112. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
  5113. &bw_data, NULL);
  5114. if (ret) {
  5115. dev_info(&vsi->back->pdev->dev,
  5116. "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
  5117. vsi->back->hw.aq.asq_last_status, ch->seid);
  5118. return -EINVAL;
  5119. }
  5120. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5121. ch->info.qs_handle[i] = bw_data.qs_handles[i];
  5122. return 0;
  5123. }
  5124. /**
  5125. * i40e_channel_config_tx_ring - config TX ring associated with new channel
  5126. * @pf: ptr to PF device
  5127. * @vsi: the VSI being setup
  5128. * @ch: ptr to channel structure
  5129. *
  5130. * Configure TX rings associated with channel (VSI) since queues are being
  5131. * from parent VSI.
  5132. **/
  5133. static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
  5134. struct i40e_vsi *vsi,
  5135. struct i40e_channel *ch)
  5136. {
  5137. i40e_status ret;
  5138. int i;
  5139. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  5140. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  5141. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5142. if (ch->enabled_tc & BIT(i))
  5143. bw_share[i] = 1;
  5144. }
  5145. /* configure BW for new VSI */
  5146. ret = i40e_channel_config_bw(vsi, ch, bw_share);
  5147. if (ret) {
  5148. dev_info(&vsi->back->pdev->dev,
  5149. "Failed configuring TC map %d for channel (seid %u)\n",
  5150. ch->enabled_tc, ch->seid);
  5151. return ret;
  5152. }
  5153. for (i = 0; i < ch->num_queue_pairs; i++) {
  5154. struct i40e_ring *tx_ring, *rx_ring;
  5155. u16 pf_q;
  5156. pf_q = ch->base_queue + i;
  5157. /* Get to TX ring ptr of main VSI, for re-setup TX queue
  5158. * context
  5159. */
  5160. tx_ring = vsi->tx_rings[pf_q];
  5161. tx_ring->ch = ch;
  5162. /* Get the RX ring ptr */
  5163. rx_ring = vsi->rx_rings[pf_q];
  5164. rx_ring->ch = ch;
  5165. }
  5166. return 0;
  5167. }
  5168. /**
  5169. * i40e_setup_hw_channel - setup new channel
  5170. * @pf: ptr to PF device
  5171. * @vsi: the VSI being setup
  5172. * @ch: ptr to channel structure
  5173. * @uplink_seid: underlying HW switching element (VEB) ID
  5174. * @type: type of channel to be created (VMDq2/VF)
  5175. *
  5176. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5177. * and configures TX rings accordingly
  5178. **/
  5179. static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
  5180. struct i40e_vsi *vsi,
  5181. struct i40e_channel *ch,
  5182. u16 uplink_seid, u8 type)
  5183. {
  5184. int ret;
  5185. ch->initialized = false;
  5186. ch->base_queue = vsi->next_base_queue;
  5187. ch->type = type;
  5188. /* Proceed with creation of channel (VMDq2) VSI */
  5189. ret = i40e_add_channel(pf, uplink_seid, ch);
  5190. if (ret) {
  5191. dev_info(&pf->pdev->dev,
  5192. "failed to add_channel using uplink_seid %u\n",
  5193. uplink_seid);
  5194. return ret;
  5195. }
  5196. /* Mark the successful creation of channel */
  5197. ch->initialized = true;
  5198. /* Reconfigure TX queues using QTX_CTL register */
  5199. ret = i40e_channel_config_tx_ring(pf, vsi, ch);
  5200. if (ret) {
  5201. dev_info(&pf->pdev->dev,
  5202. "failed to configure TX rings for channel %u\n",
  5203. ch->seid);
  5204. return ret;
  5205. }
  5206. /* update 'next_base_queue' */
  5207. vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
  5208. dev_dbg(&pf->pdev->dev,
  5209. "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
  5210. ch->seid, ch->vsi_number, ch->stat_counter_idx,
  5211. ch->num_queue_pairs,
  5212. vsi->next_base_queue);
  5213. return ret;
  5214. }
  5215. /**
  5216. * i40e_setup_channel - setup new channel using uplink element
  5217. * @pf: ptr to PF device
  5218. * @type: type of channel to be created (VMDq2/VF)
  5219. * @uplink_seid: underlying HW switching element (VEB) ID
  5220. * @ch: ptr to channel structure
  5221. *
  5222. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5223. * and uplink switching element (uplink_seid)
  5224. **/
  5225. static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
  5226. struct i40e_channel *ch)
  5227. {
  5228. u8 vsi_type;
  5229. u16 seid;
  5230. int ret;
  5231. if (vsi->type == I40E_VSI_MAIN) {
  5232. vsi_type = I40E_VSI_VMDQ2;
  5233. } else {
  5234. dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
  5235. vsi->type);
  5236. return false;
  5237. }
  5238. /* underlying switching element */
  5239. seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5240. /* create channel (VSI), configure TX rings */
  5241. ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
  5242. if (ret) {
  5243. dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
  5244. return false;
  5245. }
  5246. return ch->initialized ? true : false;
  5247. }
  5248. /**
  5249. * i40e_validate_and_set_switch_mode - sets up switch mode correctly
  5250. * @vsi: ptr to VSI which has PF backing
  5251. *
  5252. * Sets up switch mode correctly if it needs to be changed and perform
  5253. * what are allowed modes.
  5254. **/
  5255. static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
  5256. {
  5257. u8 mode;
  5258. struct i40e_pf *pf = vsi->back;
  5259. struct i40e_hw *hw = &pf->hw;
  5260. int ret;
  5261. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
  5262. if (ret)
  5263. return -EINVAL;
  5264. if (hw->dev_caps.switch_mode) {
  5265. /* if switch mode is set, support mode2 (non-tunneled for
  5266. * cloud filter) for now
  5267. */
  5268. u32 switch_mode = hw->dev_caps.switch_mode &
  5269. I40E_SWITCH_MODE_MASK;
  5270. if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
  5271. if (switch_mode == I40E_CLOUD_FILTER_MODE2)
  5272. return 0;
  5273. dev_err(&pf->pdev->dev,
  5274. "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
  5275. hw->dev_caps.switch_mode);
  5276. return -EINVAL;
  5277. }
  5278. }
  5279. /* Set Bit 7 to be valid */
  5280. mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
  5281. /* Set L4type for TCP support */
  5282. mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
  5283. /* Set cloud filter mode */
  5284. mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
  5285. /* Prep mode field for set_switch_config */
  5286. ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
  5287. pf->last_sw_conf_valid_flags,
  5288. mode, NULL);
  5289. if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
  5290. dev_err(&pf->pdev->dev,
  5291. "couldn't set switch config bits, err %s aq_err %s\n",
  5292. i40e_stat_str(hw, ret),
  5293. i40e_aq_str(hw,
  5294. hw->aq.asq_last_status));
  5295. return ret;
  5296. }
  5297. /**
  5298. * i40e_create_queue_channel - function to create channel
  5299. * @vsi: VSI to be configured
  5300. * @ch: ptr to channel (it contains channel specific params)
  5301. *
  5302. * This function creates channel (VSI) using num_queues specified by user,
  5303. * reconfigs RSS if needed.
  5304. **/
  5305. int i40e_create_queue_channel(struct i40e_vsi *vsi,
  5306. struct i40e_channel *ch)
  5307. {
  5308. struct i40e_pf *pf = vsi->back;
  5309. bool reconfig_rss;
  5310. int err;
  5311. if (!ch)
  5312. return -EINVAL;
  5313. if (!ch->num_queue_pairs) {
  5314. dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
  5315. ch->num_queue_pairs);
  5316. return -EINVAL;
  5317. }
  5318. /* validate user requested num_queues for channel */
  5319. err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
  5320. &reconfig_rss);
  5321. if (err) {
  5322. dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
  5323. ch->num_queue_pairs);
  5324. return -EINVAL;
  5325. }
  5326. /* By default we are in VEPA mode, if this is the first VF/VMDq
  5327. * VSI to be added switch to VEB mode.
  5328. */
  5329. if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
  5330. (!i40e_is_any_channel(vsi))) {
  5331. if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
  5332. dev_dbg(&pf->pdev->dev,
  5333. "Failed to create channel. Override queues (%u) not power of 2\n",
  5334. vsi->tc_config.tc_info[0].qcount);
  5335. return -EINVAL;
  5336. }
  5337. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  5338. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  5339. if (vsi->type == I40E_VSI_MAIN) {
  5340. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  5341. i40e_do_reset(pf, I40E_PF_RESET_FLAG,
  5342. true);
  5343. else
  5344. i40e_do_reset_safe(pf,
  5345. I40E_PF_RESET_FLAG);
  5346. }
  5347. }
  5348. /* now onwards for main VSI, number of queues will be value
  5349. * of TC0's queue count
  5350. */
  5351. }
  5352. /* By this time, vsi->cnt_q_avail shall be set to non-zero and
  5353. * it should be more than num_queues
  5354. */
  5355. if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
  5356. dev_dbg(&pf->pdev->dev,
  5357. "Error: cnt_q_avail (%u) less than num_queues %d\n",
  5358. vsi->cnt_q_avail, ch->num_queue_pairs);
  5359. return -EINVAL;
  5360. }
  5361. /* reconfig_rss only if vsi type is MAIN_VSI */
  5362. if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
  5363. err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
  5364. if (err) {
  5365. dev_info(&pf->pdev->dev,
  5366. "Error: unable to reconfig rss for num_queues (%u)\n",
  5367. ch->num_queue_pairs);
  5368. return -EINVAL;
  5369. }
  5370. }
  5371. if (!i40e_setup_channel(pf, vsi, ch)) {
  5372. dev_info(&pf->pdev->dev, "Failed to setup channel\n");
  5373. return -EINVAL;
  5374. }
  5375. dev_info(&pf->pdev->dev,
  5376. "Setup channel (id:%u) utilizing num_queues %d\n",
  5377. ch->seid, ch->num_queue_pairs);
  5378. /* configure VSI for BW limit */
  5379. if (ch->max_tx_rate) {
  5380. u64 credits = ch->max_tx_rate;
  5381. if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
  5382. return -EINVAL;
  5383. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5384. dev_dbg(&pf->pdev->dev,
  5385. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5386. ch->max_tx_rate,
  5387. credits,
  5388. ch->seid);
  5389. }
  5390. /* in case of VF, this will be main SRIOV VSI */
  5391. ch->parent_vsi = vsi;
  5392. /* and update main_vsi's count for queue_available to use */
  5393. vsi->cnt_q_avail -= ch->num_queue_pairs;
  5394. return 0;
  5395. }
  5396. /**
  5397. * i40e_configure_queue_channels - Add queue channel for the given TCs
  5398. * @vsi: VSI to be configured
  5399. *
  5400. * Configures queue channel mapping to the given TCs
  5401. **/
  5402. static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
  5403. {
  5404. struct i40e_channel *ch;
  5405. u64 max_rate = 0;
  5406. int ret = 0, i;
  5407. /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
  5408. vsi->tc_seid_map[0] = vsi->seid;
  5409. for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5410. if (vsi->tc_config.enabled_tc & BIT(i)) {
  5411. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  5412. if (!ch) {
  5413. ret = -ENOMEM;
  5414. goto err_free;
  5415. }
  5416. INIT_LIST_HEAD(&ch->list);
  5417. ch->num_queue_pairs =
  5418. vsi->tc_config.tc_info[i].qcount;
  5419. ch->base_queue =
  5420. vsi->tc_config.tc_info[i].qoffset;
  5421. /* Bandwidth limit through tc interface is in bytes/s,
  5422. * change to Mbit/s
  5423. */
  5424. max_rate = vsi->mqprio_qopt.max_rate[i];
  5425. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5426. ch->max_tx_rate = max_rate;
  5427. list_add_tail(&ch->list, &vsi->ch_list);
  5428. ret = i40e_create_queue_channel(vsi, ch);
  5429. if (ret) {
  5430. dev_err(&vsi->back->pdev->dev,
  5431. "Failed creating queue channel with TC%d: queues %d\n",
  5432. i, ch->num_queue_pairs);
  5433. goto err_free;
  5434. }
  5435. vsi->tc_seid_map[i] = ch->seid;
  5436. }
  5437. }
  5438. return ret;
  5439. err_free:
  5440. i40e_remove_queue_channels(vsi);
  5441. return ret;
  5442. }
  5443. /**
  5444. * i40e_veb_config_tc - Configure TCs for given VEB
  5445. * @veb: given VEB
  5446. * @enabled_tc: TC bitmap
  5447. *
  5448. * Configures given TC bitmap for VEB (switching) element
  5449. **/
  5450. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  5451. {
  5452. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  5453. struct i40e_pf *pf = veb->pf;
  5454. int ret = 0;
  5455. int i;
  5456. /* No TCs or already enabled TCs just return */
  5457. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  5458. return ret;
  5459. bw_data.tc_valid_bits = enabled_tc;
  5460. /* bw_data.absolute_credits is not set (relative) */
  5461. /* Enable ETS TCs with equal BW Share for now */
  5462. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5463. if (enabled_tc & BIT(i))
  5464. bw_data.tc_bw_share_credits[i] = 1;
  5465. }
  5466. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  5467. &bw_data, NULL);
  5468. if (ret) {
  5469. dev_info(&pf->pdev->dev,
  5470. "VEB bw config failed, err %s aq_err %s\n",
  5471. i40e_stat_str(&pf->hw, ret),
  5472. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5473. goto out;
  5474. }
  5475. /* Update the BW information */
  5476. ret = i40e_veb_get_bw_info(veb);
  5477. if (ret) {
  5478. dev_info(&pf->pdev->dev,
  5479. "Failed getting veb bw config, err %s aq_err %s\n",
  5480. i40e_stat_str(&pf->hw, ret),
  5481. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5482. }
  5483. out:
  5484. return ret;
  5485. }
  5486. #ifdef CONFIG_I40E_DCB
  5487. /**
  5488. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  5489. * @pf: PF struct
  5490. *
  5491. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  5492. * the caller would've quiesce all the VSIs before calling
  5493. * this function
  5494. **/
  5495. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  5496. {
  5497. u8 tc_map = 0;
  5498. int ret;
  5499. u8 v;
  5500. /* Enable the TCs available on PF to all VEBs */
  5501. tc_map = i40e_pf_get_tc_map(pf);
  5502. for (v = 0; v < I40E_MAX_VEB; v++) {
  5503. if (!pf->veb[v])
  5504. continue;
  5505. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  5506. if (ret) {
  5507. dev_info(&pf->pdev->dev,
  5508. "Failed configuring TC for VEB seid=%d\n",
  5509. pf->veb[v]->seid);
  5510. /* Will try to configure as many components */
  5511. }
  5512. }
  5513. /* Update each VSI */
  5514. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5515. if (!pf->vsi[v])
  5516. continue;
  5517. /* - Enable all TCs for the LAN VSI
  5518. * - For all others keep them at TC0 for now
  5519. */
  5520. if (v == pf->lan_vsi)
  5521. tc_map = i40e_pf_get_tc_map(pf);
  5522. else
  5523. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  5524. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  5525. if (ret) {
  5526. dev_info(&pf->pdev->dev,
  5527. "Failed configuring TC for VSI seid=%d\n",
  5528. pf->vsi[v]->seid);
  5529. /* Will try to configure as many components */
  5530. } else {
  5531. /* Re-configure VSI vectors based on updated TC map */
  5532. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  5533. if (pf->vsi[v]->netdev)
  5534. i40e_dcbnl_set_all(pf->vsi[v]);
  5535. }
  5536. }
  5537. }
  5538. /**
  5539. * i40e_resume_port_tx - Resume port Tx
  5540. * @pf: PF struct
  5541. *
  5542. * Resume a port's Tx and issue a PF reset in case of failure to
  5543. * resume.
  5544. **/
  5545. static int i40e_resume_port_tx(struct i40e_pf *pf)
  5546. {
  5547. struct i40e_hw *hw = &pf->hw;
  5548. int ret;
  5549. ret = i40e_aq_resume_port_tx(hw, NULL);
  5550. if (ret) {
  5551. dev_info(&pf->pdev->dev,
  5552. "Resume Port Tx failed, err %s aq_err %s\n",
  5553. i40e_stat_str(&pf->hw, ret),
  5554. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5555. /* Schedule PF reset to recover */
  5556. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  5557. i40e_service_event_schedule(pf);
  5558. }
  5559. return ret;
  5560. }
  5561. /**
  5562. * i40e_init_pf_dcb - Initialize DCB configuration
  5563. * @pf: PF being configured
  5564. *
  5565. * Query the current DCB configuration and cache it
  5566. * in the hardware structure
  5567. **/
  5568. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  5569. {
  5570. struct i40e_hw *hw = &pf->hw;
  5571. int err = 0;
  5572. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable
  5573. * Also do not enable DCBx if FW LLDP agent is disabled
  5574. */
  5575. if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
  5576. (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
  5577. goto out;
  5578. /* Get the initial DCB configuration */
  5579. err = i40e_init_dcb(hw);
  5580. if (!err) {
  5581. /* Device/Function is not DCBX capable */
  5582. if ((!hw->func_caps.dcb) ||
  5583. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  5584. dev_info(&pf->pdev->dev,
  5585. "DCBX offload is not supported or is disabled for this PF.\n");
  5586. } else {
  5587. /* When status is not DISABLED then DCBX in FW */
  5588. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  5589. DCB_CAP_DCBX_VER_IEEE;
  5590. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  5591. /* Enable DCB tagging only when more than one TC
  5592. * or explicitly disable if only one TC
  5593. */
  5594. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5595. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5596. else
  5597. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5598. dev_dbg(&pf->pdev->dev,
  5599. "DCBX offload is supported for this PF.\n");
  5600. }
  5601. } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
  5602. dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
  5603. pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
  5604. } else {
  5605. dev_info(&pf->pdev->dev,
  5606. "Query for DCB configuration failed, err %s aq_err %s\n",
  5607. i40e_stat_str(&pf->hw, err),
  5608. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5609. }
  5610. out:
  5611. return err;
  5612. }
  5613. #endif /* CONFIG_I40E_DCB */
  5614. #define SPEED_SIZE 14
  5615. #define FC_SIZE 8
  5616. /**
  5617. * i40e_print_link_message - print link up or down
  5618. * @vsi: the VSI for which link needs a message
  5619. * @isup: true of link is up, false otherwise
  5620. */
  5621. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  5622. {
  5623. enum i40e_aq_link_speed new_speed;
  5624. struct i40e_pf *pf = vsi->back;
  5625. char *speed = "Unknown";
  5626. char *fc = "Unknown";
  5627. char *fec = "";
  5628. char *req_fec = "";
  5629. char *an = "";
  5630. if (isup)
  5631. new_speed = pf->hw.phy.link_info.link_speed;
  5632. else
  5633. new_speed = I40E_LINK_SPEED_UNKNOWN;
  5634. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  5635. return;
  5636. vsi->current_isup = isup;
  5637. vsi->current_speed = new_speed;
  5638. if (!isup) {
  5639. netdev_info(vsi->netdev, "NIC Link is Down\n");
  5640. return;
  5641. }
  5642. /* Warn user if link speed on NPAR enabled partition is not at
  5643. * least 10GB
  5644. */
  5645. if (pf->hw.func_caps.npar_enable &&
  5646. (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  5647. pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  5648. netdev_warn(vsi->netdev,
  5649. "The partition detected link speed that is less than 10Gbps\n");
  5650. switch (pf->hw.phy.link_info.link_speed) {
  5651. case I40E_LINK_SPEED_40GB:
  5652. speed = "40 G";
  5653. break;
  5654. case I40E_LINK_SPEED_20GB:
  5655. speed = "20 G";
  5656. break;
  5657. case I40E_LINK_SPEED_25GB:
  5658. speed = "25 G";
  5659. break;
  5660. case I40E_LINK_SPEED_10GB:
  5661. speed = "10 G";
  5662. break;
  5663. case I40E_LINK_SPEED_1GB:
  5664. speed = "1000 M";
  5665. break;
  5666. case I40E_LINK_SPEED_100MB:
  5667. speed = "100 M";
  5668. break;
  5669. default:
  5670. break;
  5671. }
  5672. switch (pf->hw.fc.current_mode) {
  5673. case I40E_FC_FULL:
  5674. fc = "RX/TX";
  5675. break;
  5676. case I40E_FC_TX_PAUSE:
  5677. fc = "TX";
  5678. break;
  5679. case I40E_FC_RX_PAUSE:
  5680. fc = "RX";
  5681. break;
  5682. default:
  5683. fc = "None";
  5684. break;
  5685. }
  5686. if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  5687. req_fec = ", Requested FEC: None";
  5688. fec = ", FEC: None";
  5689. an = ", Autoneg: False";
  5690. if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  5691. an = ", Autoneg: True";
  5692. if (pf->hw.phy.link_info.fec_info &
  5693. I40E_AQ_CONFIG_FEC_KR_ENA)
  5694. fec = ", FEC: CL74 FC-FEC/BASE-R";
  5695. else if (pf->hw.phy.link_info.fec_info &
  5696. I40E_AQ_CONFIG_FEC_RS_ENA)
  5697. fec = ", FEC: CL108 RS-FEC";
  5698. /* 'CL108 RS-FEC' should be displayed when RS is requested, or
  5699. * both RS and FC are requested
  5700. */
  5701. if (vsi->back->hw.phy.link_info.req_fec_info &
  5702. (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
  5703. if (vsi->back->hw.phy.link_info.req_fec_info &
  5704. I40E_AQ_REQUEST_FEC_RS)
  5705. req_fec = ", Requested FEC: CL108 RS-FEC";
  5706. else
  5707. req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
  5708. }
  5709. }
  5710. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
  5711. speed, req_fec, fec, an, fc);
  5712. }
  5713. /**
  5714. * i40e_up_complete - Finish the last steps of bringing up a connection
  5715. * @vsi: the VSI being configured
  5716. **/
  5717. static int i40e_up_complete(struct i40e_vsi *vsi)
  5718. {
  5719. struct i40e_pf *pf = vsi->back;
  5720. int err;
  5721. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5722. i40e_vsi_configure_msix(vsi);
  5723. else
  5724. i40e_configure_msi_and_legacy(vsi);
  5725. /* start rings */
  5726. err = i40e_vsi_start_rings(vsi);
  5727. if (err)
  5728. return err;
  5729. clear_bit(__I40E_VSI_DOWN, vsi->state);
  5730. i40e_napi_enable_all(vsi);
  5731. i40e_vsi_enable_irq(vsi);
  5732. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  5733. (vsi->netdev)) {
  5734. i40e_print_link_message(vsi, true);
  5735. netif_tx_start_all_queues(vsi->netdev);
  5736. netif_carrier_on(vsi->netdev);
  5737. }
  5738. /* replay FDIR SB filters */
  5739. if (vsi->type == I40E_VSI_FDIR) {
  5740. /* reset fd counters */
  5741. pf->fd_add_err = 0;
  5742. pf->fd_atr_cnt = 0;
  5743. i40e_fdir_filter_restore(vsi);
  5744. }
  5745. /* On the next run of the service_task, notify any clients of the new
  5746. * opened netdev
  5747. */
  5748. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  5749. i40e_service_event_schedule(pf);
  5750. return 0;
  5751. }
  5752. /**
  5753. * i40e_vsi_reinit_locked - Reset the VSI
  5754. * @vsi: the VSI being configured
  5755. *
  5756. * Rebuild the ring structs after some configuration
  5757. * has changed, e.g. MTU size.
  5758. **/
  5759. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  5760. {
  5761. struct i40e_pf *pf = vsi->back;
  5762. WARN_ON(in_interrupt());
  5763. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
  5764. usleep_range(1000, 2000);
  5765. i40e_down(vsi);
  5766. i40e_up(vsi);
  5767. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  5768. }
  5769. /**
  5770. * i40e_up - Bring the connection back up after being down
  5771. * @vsi: the VSI being configured
  5772. **/
  5773. int i40e_up(struct i40e_vsi *vsi)
  5774. {
  5775. int err;
  5776. err = i40e_vsi_configure(vsi);
  5777. if (!err)
  5778. err = i40e_up_complete(vsi);
  5779. return err;
  5780. }
  5781. /**
  5782. * i40e_force_link_state - Force the link status
  5783. * @pf: board private structure
  5784. * @is_up: whether the link state should be forced up or down
  5785. **/
  5786. static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
  5787. {
  5788. struct i40e_aq_get_phy_abilities_resp abilities;
  5789. struct i40e_aq_set_phy_config config = {0};
  5790. struct i40e_hw *hw = &pf->hw;
  5791. i40e_status err;
  5792. u64 mask;
  5793. u8 speed;
  5794. /* Card might've been put in an unstable state by other drivers
  5795. * and applications, which causes incorrect speed values being
  5796. * set on startup. In order to clear speed registers, we call
  5797. * get_phy_capabilities twice, once to get initial state of
  5798. * available speeds, and once to get current PHY config.
  5799. */
  5800. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
  5801. NULL);
  5802. if (err) {
  5803. dev_err(&pf->pdev->dev,
  5804. "failed to get phy cap., ret = %s last_status = %s\n",
  5805. i40e_stat_str(hw, err),
  5806. i40e_aq_str(hw, hw->aq.asq_last_status));
  5807. return err;
  5808. }
  5809. speed = abilities.link_speed;
  5810. /* Get the current phy config */
  5811. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
  5812. NULL);
  5813. if (err) {
  5814. dev_err(&pf->pdev->dev,
  5815. "failed to get phy cap., ret = %s last_status = %s\n",
  5816. i40e_stat_str(hw, err),
  5817. i40e_aq_str(hw, hw->aq.asq_last_status));
  5818. return err;
  5819. }
  5820. /* If link needs to go up, but was not forced to go down,
  5821. * and its speed values are OK, no need for a flap
  5822. */
  5823. if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
  5824. return I40E_SUCCESS;
  5825. /* To force link we need to set bits for all supported PHY types,
  5826. * but there are now more than 32, so we need to split the bitmap
  5827. * across two fields.
  5828. */
  5829. mask = I40E_PHY_TYPES_BITMASK;
  5830. config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
  5831. config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
  5832. /* Copy the old settings, except of phy_type */
  5833. config.abilities = abilities.abilities;
  5834. if (abilities.link_speed != 0)
  5835. config.link_speed = abilities.link_speed;
  5836. else
  5837. config.link_speed = speed;
  5838. config.eee_capability = abilities.eee_capability;
  5839. config.eeer = abilities.eeer_val;
  5840. config.low_power_ctrl = abilities.d3_lpan;
  5841. config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
  5842. I40E_AQ_PHY_FEC_CONFIG_MASK;
  5843. err = i40e_aq_set_phy_config(hw, &config, NULL);
  5844. if (err) {
  5845. dev_err(&pf->pdev->dev,
  5846. "set phy config ret = %s last_status = %s\n",
  5847. i40e_stat_str(&pf->hw, err),
  5848. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5849. return err;
  5850. }
  5851. /* Update the link info */
  5852. err = i40e_update_link_info(hw);
  5853. if (err) {
  5854. /* Wait a little bit (on 40G cards it sometimes takes a really
  5855. * long time for link to come back from the atomic reset)
  5856. * and try once more
  5857. */
  5858. msleep(1000);
  5859. i40e_update_link_info(hw);
  5860. }
  5861. i40e_aq_set_link_restart_an(hw, true, NULL);
  5862. return I40E_SUCCESS;
  5863. }
  5864. /**
  5865. * i40e_down - Shutdown the connection processing
  5866. * @vsi: the VSI being stopped
  5867. **/
  5868. void i40e_down(struct i40e_vsi *vsi)
  5869. {
  5870. int i;
  5871. /* It is assumed that the caller of this function
  5872. * sets the vsi->state __I40E_VSI_DOWN bit.
  5873. */
  5874. if (vsi->netdev) {
  5875. netif_carrier_off(vsi->netdev);
  5876. netif_tx_disable(vsi->netdev);
  5877. }
  5878. i40e_vsi_disable_irq(vsi);
  5879. i40e_vsi_stop_rings(vsi);
  5880. if (vsi->type == I40E_VSI_MAIN &&
  5881. vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
  5882. i40e_force_link_state(vsi->back, false);
  5883. i40e_napi_disable_all(vsi);
  5884. for (i = 0; i < vsi->num_queue_pairs; i++) {
  5885. i40e_clean_tx_ring(vsi->tx_rings[i]);
  5886. if (i40e_enabled_xdp_vsi(vsi))
  5887. i40e_clean_tx_ring(vsi->xdp_rings[i]);
  5888. i40e_clean_rx_ring(vsi->rx_rings[i]);
  5889. }
  5890. }
  5891. /**
  5892. * i40e_validate_mqprio_qopt- validate queue mapping info
  5893. * @vsi: the VSI being configured
  5894. * @mqprio_qopt: queue parametrs
  5895. **/
  5896. static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
  5897. struct tc_mqprio_qopt_offload *mqprio_qopt)
  5898. {
  5899. u64 sum_max_rate = 0;
  5900. u64 max_rate = 0;
  5901. int i;
  5902. if (mqprio_qopt->qopt.offset[0] != 0 ||
  5903. mqprio_qopt->qopt.num_tc < 1 ||
  5904. mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
  5905. return -EINVAL;
  5906. for (i = 0; ; i++) {
  5907. if (!mqprio_qopt->qopt.count[i])
  5908. return -EINVAL;
  5909. if (mqprio_qopt->min_rate[i]) {
  5910. dev_err(&vsi->back->pdev->dev,
  5911. "Invalid min tx rate (greater than 0) specified\n");
  5912. return -EINVAL;
  5913. }
  5914. max_rate = mqprio_qopt->max_rate[i];
  5915. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5916. sum_max_rate += max_rate;
  5917. if (i >= mqprio_qopt->qopt.num_tc - 1)
  5918. break;
  5919. if (mqprio_qopt->qopt.offset[i + 1] !=
  5920. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
  5921. return -EINVAL;
  5922. }
  5923. if (vsi->num_queue_pairs <
  5924. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
  5925. return -EINVAL;
  5926. }
  5927. if (sum_max_rate > i40e_get_link_speed(vsi)) {
  5928. dev_err(&vsi->back->pdev->dev,
  5929. "Invalid max tx rate specified\n");
  5930. return -EINVAL;
  5931. }
  5932. return 0;
  5933. }
  5934. /**
  5935. * i40e_vsi_set_default_tc_config - set default values for tc configuration
  5936. * @vsi: the VSI being configured
  5937. **/
  5938. static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
  5939. {
  5940. u16 qcount;
  5941. int i;
  5942. /* Only TC0 is enabled */
  5943. vsi->tc_config.numtc = 1;
  5944. vsi->tc_config.enabled_tc = 1;
  5945. qcount = min_t(int, vsi->alloc_queue_pairs,
  5946. i40e_pf_get_max_q_per_tc(vsi->back));
  5947. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5948. /* For the TC that is not enabled set the offset to to default
  5949. * queue and allocate one queue for the given TC.
  5950. */
  5951. vsi->tc_config.tc_info[i].qoffset = 0;
  5952. if (i == 0)
  5953. vsi->tc_config.tc_info[i].qcount = qcount;
  5954. else
  5955. vsi->tc_config.tc_info[i].qcount = 1;
  5956. vsi->tc_config.tc_info[i].netdev_tc = 0;
  5957. }
  5958. }
  5959. /**
  5960. * i40e_setup_tc - configure multiple traffic classes
  5961. * @netdev: net device to configure
  5962. * @type_data: tc offload data
  5963. **/
  5964. static int i40e_setup_tc(struct net_device *netdev, void *type_data)
  5965. {
  5966. struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
  5967. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5968. struct i40e_vsi *vsi = np->vsi;
  5969. struct i40e_pf *pf = vsi->back;
  5970. u8 enabled_tc = 0, num_tc, hw;
  5971. bool need_reset = false;
  5972. int ret = -EINVAL;
  5973. u16 mode;
  5974. int i;
  5975. num_tc = mqprio_qopt->qopt.num_tc;
  5976. hw = mqprio_qopt->qopt.hw;
  5977. mode = mqprio_qopt->mode;
  5978. if (!hw) {
  5979. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5980. memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
  5981. goto config_tc;
  5982. }
  5983. /* Check if MFP enabled */
  5984. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  5985. netdev_info(netdev,
  5986. "Configuring TC not supported in MFP mode\n");
  5987. return ret;
  5988. }
  5989. switch (mode) {
  5990. case TC_MQPRIO_MODE_DCB:
  5991. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5992. /* Check if DCB enabled to continue */
  5993. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  5994. netdev_info(netdev,
  5995. "DCB is not enabled for adapter\n");
  5996. return ret;
  5997. }
  5998. /* Check whether tc count is within enabled limit */
  5999. if (num_tc > i40e_pf_get_num_tc(pf)) {
  6000. netdev_info(netdev,
  6001. "TC count greater than enabled on link for adapter\n");
  6002. return ret;
  6003. }
  6004. break;
  6005. case TC_MQPRIO_MODE_CHANNEL:
  6006. if (pf->flags & I40E_FLAG_DCB_ENABLED) {
  6007. netdev_info(netdev,
  6008. "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
  6009. return ret;
  6010. }
  6011. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  6012. return ret;
  6013. ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
  6014. if (ret)
  6015. return ret;
  6016. memcpy(&vsi->mqprio_qopt, mqprio_qopt,
  6017. sizeof(*mqprio_qopt));
  6018. pf->flags |= I40E_FLAG_TC_MQPRIO;
  6019. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  6020. break;
  6021. default:
  6022. return -EINVAL;
  6023. }
  6024. config_tc:
  6025. /* Generate TC map for number of tc requested */
  6026. for (i = 0; i < num_tc; i++)
  6027. enabled_tc |= BIT(i);
  6028. /* Requesting same TC configuration as already enabled */
  6029. if (enabled_tc == vsi->tc_config.enabled_tc &&
  6030. mode != TC_MQPRIO_MODE_CHANNEL)
  6031. return 0;
  6032. /* Quiesce VSI queues */
  6033. i40e_quiesce_vsi(vsi);
  6034. if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
  6035. i40e_remove_queue_channels(vsi);
  6036. /* Configure VSI for enabled TCs */
  6037. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  6038. if (ret) {
  6039. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  6040. vsi->seid);
  6041. need_reset = true;
  6042. goto exit;
  6043. }
  6044. if (pf->flags & I40E_FLAG_TC_MQPRIO) {
  6045. if (vsi->mqprio_qopt.max_rate[0]) {
  6046. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  6047. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  6048. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  6049. if (!ret) {
  6050. u64 credits = max_tx_rate;
  6051. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  6052. dev_dbg(&vsi->back->pdev->dev,
  6053. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  6054. max_tx_rate,
  6055. credits,
  6056. vsi->seid);
  6057. } else {
  6058. need_reset = true;
  6059. goto exit;
  6060. }
  6061. }
  6062. ret = i40e_configure_queue_channels(vsi);
  6063. if (ret) {
  6064. netdev_info(netdev,
  6065. "Failed configuring queue channels\n");
  6066. need_reset = true;
  6067. goto exit;
  6068. }
  6069. }
  6070. exit:
  6071. /* Reset the configuration data to defaults, only TC0 is enabled */
  6072. if (need_reset) {
  6073. i40e_vsi_set_default_tc_config(vsi);
  6074. need_reset = false;
  6075. }
  6076. /* Unquiesce VSI */
  6077. i40e_unquiesce_vsi(vsi);
  6078. return ret;
  6079. }
  6080. /**
  6081. * i40e_set_cld_element - sets cloud filter element data
  6082. * @filter: cloud filter rule
  6083. * @cld: ptr to cloud filter element data
  6084. *
  6085. * This is helper function to copy data into cloud filter element
  6086. **/
  6087. static inline void
  6088. i40e_set_cld_element(struct i40e_cloud_filter *filter,
  6089. struct i40e_aqc_cloud_filters_element_data *cld)
  6090. {
  6091. int i, j;
  6092. u32 ipa;
  6093. memset(cld, 0, sizeof(*cld));
  6094. ether_addr_copy(cld->outer_mac, filter->dst_mac);
  6095. ether_addr_copy(cld->inner_mac, filter->src_mac);
  6096. if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
  6097. return;
  6098. if (filter->n_proto == ETH_P_IPV6) {
  6099. #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
  6100. for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
  6101. i++, j += 2) {
  6102. ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
  6103. ipa = cpu_to_le32(ipa);
  6104. memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
  6105. }
  6106. } else {
  6107. ipa = be32_to_cpu(filter->dst_ipv4);
  6108. memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
  6109. }
  6110. cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
  6111. /* tenant_id is not supported by FW now, once the support is enabled
  6112. * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
  6113. */
  6114. if (filter->tenant_id)
  6115. return;
  6116. }
  6117. /**
  6118. * i40e_add_del_cloud_filter - Add/del cloud filter
  6119. * @vsi: pointer to VSI
  6120. * @filter: cloud filter rule
  6121. * @add: if true, add, if false, delete
  6122. *
  6123. * Add or delete a cloud filter for a specific flow spec.
  6124. * Returns 0 if the filter were successfully added.
  6125. **/
  6126. int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
  6127. struct i40e_cloud_filter *filter, bool add)
  6128. {
  6129. struct i40e_aqc_cloud_filters_element_data cld_filter;
  6130. struct i40e_pf *pf = vsi->back;
  6131. int ret;
  6132. static const u16 flag_table[128] = {
  6133. [I40E_CLOUD_FILTER_FLAGS_OMAC] =
  6134. I40E_AQC_ADD_CLOUD_FILTER_OMAC,
  6135. [I40E_CLOUD_FILTER_FLAGS_IMAC] =
  6136. I40E_AQC_ADD_CLOUD_FILTER_IMAC,
  6137. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
  6138. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
  6139. [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
  6140. I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
  6141. [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
  6142. I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
  6143. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
  6144. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
  6145. [I40E_CLOUD_FILTER_FLAGS_IIP] =
  6146. I40E_AQC_ADD_CLOUD_FILTER_IIP,
  6147. };
  6148. if (filter->flags >= ARRAY_SIZE(flag_table))
  6149. return I40E_ERR_CONFIG;
  6150. /* copy element needed to add cloud filter from filter */
  6151. i40e_set_cld_element(filter, &cld_filter);
  6152. if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
  6153. cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
  6154. I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
  6155. if (filter->n_proto == ETH_P_IPV6)
  6156. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6157. I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6158. else
  6159. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6160. I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6161. if (add)
  6162. ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
  6163. &cld_filter, 1);
  6164. else
  6165. ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
  6166. &cld_filter, 1);
  6167. if (ret)
  6168. dev_dbg(&pf->pdev->dev,
  6169. "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
  6170. add ? "add" : "delete", filter->dst_port, ret,
  6171. pf->hw.aq.asq_last_status);
  6172. else
  6173. dev_info(&pf->pdev->dev,
  6174. "%s cloud filter for VSI: %d\n",
  6175. add ? "Added" : "Deleted", filter->seid);
  6176. return ret;
  6177. }
  6178. /**
  6179. * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
  6180. * @vsi: pointer to VSI
  6181. * @filter: cloud filter rule
  6182. * @add: if true, add, if false, delete
  6183. *
  6184. * Add or delete a cloud filter for a specific flow spec using big buffer.
  6185. * Returns 0 if the filter were successfully added.
  6186. **/
  6187. int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
  6188. struct i40e_cloud_filter *filter,
  6189. bool add)
  6190. {
  6191. struct i40e_aqc_cloud_filters_element_bb cld_filter;
  6192. struct i40e_pf *pf = vsi->back;
  6193. int ret;
  6194. /* Both (src/dst) valid mac_addr are not supported */
  6195. if ((is_valid_ether_addr(filter->dst_mac) &&
  6196. is_valid_ether_addr(filter->src_mac)) ||
  6197. (is_multicast_ether_addr(filter->dst_mac) &&
  6198. is_multicast_ether_addr(filter->src_mac)))
  6199. return -EOPNOTSUPP;
  6200. /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
  6201. * ports are not supported via big buffer now.
  6202. */
  6203. if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
  6204. return -EOPNOTSUPP;
  6205. /* adding filter using src_port/src_ip is not supported at this stage */
  6206. if (filter->src_port || filter->src_ipv4 ||
  6207. !ipv6_addr_any(&filter->ip.v6.src_ip6))
  6208. return -EOPNOTSUPP;
  6209. /* copy element needed to add cloud filter from filter */
  6210. i40e_set_cld_element(filter, &cld_filter.element);
  6211. if (is_valid_ether_addr(filter->dst_mac) ||
  6212. is_valid_ether_addr(filter->src_mac) ||
  6213. is_multicast_ether_addr(filter->dst_mac) ||
  6214. is_multicast_ether_addr(filter->src_mac)) {
  6215. /* MAC + IP : unsupported mode */
  6216. if (filter->dst_ipv4)
  6217. return -EOPNOTSUPP;
  6218. /* since we validated that L4 port must be valid before
  6219. * we get here, start with respective "flags" value
  6220. * and update if vlan is present or not
  6221. */
  6222. cld_filter.element.flags =
  6223. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
  6224. if (filter->vlan_id) {
  6225. cld_filter.element.flags =
  6226. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
  6227. }
  6228. } else if (filter->dst_ipv4 ||
  6229. !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
  6230. cld_filter.element.flags =
  6231. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
  6232. if (filter->n_proto == ETH_P_IPV6)
  6233. cld_filter.element.flags |=
  6234. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6235. else
  6236. cld_filter.element.flags |=
  6237. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6238. } else {
  6239. dev_err(&pf->pdev->dev,
  6240. "either mac or ip has to be valid for cloud filter\n");
  6241. return -EINVAL;
  6242. }
  6243. /* Now copy L4 port in Byte 6..7 in general fields */
  6244. cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
  6245. be16_to_cpu(filter->dst_port);
  6246. if (add) {
  6247. /* Validate current device switch mode, change if necessary */
  6248. ret = i40e_validate_and_set_switch_mode(vsi);
  6249. if (ret) {
  6250. dev_err(&pf->pdev->dev,
  6251. "failed to set switch mode, ret %d\n",
  6252. ret);
  6253. return ret;
  6254. }
  6255. ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
  6256. &cld_filter, 1);
  6257. } else {
  6258. ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
  6259. &cld_filter, 1);
  6260. }
  6261. if (ret)
  6262. dev_dbg(&pf->pdev->dev,
  6263. "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
  6264. add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
  6265. else
  6266. dev_info(&pf->pdev->dev,
  6267. "%s cloud filter for VSI: %d, L4 port: %d\n",
  6268. add ? "add" : "delete", filter->seid,
  6269. ntohs(filter->dst_port));
  6270. return ret;
  6271. }
  6272. /**
  6273. * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
  6274. * @vsi: Pointer to VSI
  6275. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6276. * @filter: Pointer to cloud filter structure
  6277. *
  6278. **/
  6279. static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
  6280. struct tc_cls_flower_offload *f,
  6281. struct i40e_cloud_filter *filter)
  6282. {
  6283. u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
  6284. struct i40e_pf *pf = vsi->back;
  6285. u8 field_flags = 0;
  6286. if (f->dissector->used_keys &
  6287. ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  6288. BIT(FLOW_DISSECTOR_KEY_BASIC) |
  6289. BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
  6290. BIT(FLOW_DISSECTOR_KEY_VLAN) |
  6291. BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  6292. BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  6293. BIT(FLOW_DISSECTOR_KEY_PORTS) |
  6294. BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
  6295. dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
  6296. f->dissector->used_keys);
  6297. return -EOPNOTSUPP;
  6298. }
  6299. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  6300. struct flow_dissector_key_keyid *key =
  6301. skb_flow_dissector_target(f->dissector,
  6302. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6303. f->key);
  6304. struct flow_dissector_key_keyid *mask =
  6305. skb_flow_dissector_target(f->dissector,
  6306. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6307. f->mask);
  6308. if (mask->keyid != 0)
  6309. field_flags |= I40E_CLOUD_FIELD_TEN_ID;
  6310. filter->tenant_id = be32_to_cpu(key->keyid);
  6311. }
  6312. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  6313. struct flow_dissector_key_basic *key =
  6314. skb_flow_dissector_target(f->dissector,
  6315. FLOW_DISSECTOR_KEY_BASIC,
  6316. f->key);
  6317. struct flow_dissector_key_basic *mask =
  6318. skb_flow_dissector_target(f->dissector,
  6319. FLOW_DISSECTOR_KEY_BASIC,
  6320. f->mask);
  6321. n_proto_key = ntohs(key->n_proto);
  6322. n_proto_mask = ntohs(mask->n_proto);
  6323. if (n_proto_key == ETH_P_ALL) {
  6324. n_proto_key = 0;
  6325. n_proto_mask = 0;
  6326. }
  6327. filter->n_proto = n_proto_key & n_proto_mask;
  6328. filter->ip_proto = key->ip_proto;
  6329. }
  6330. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  6331. struct flow_dissector_key_eth_addrs *key =
  6332. skb_flow_dissector_target(f->dissector,
  6333. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6334. f->key);
  6335. struct flow_dissector_key_eth_addrs *mask =
  6336. skb_flow_dissector_target(f->dissector,
  6337. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6338. f->mask);
  6339. /* use is_broadcast and is_zero to check for all 0xf or 0 */
  6340. if (!is_zero_ether_addr(mask->dst)) {
  6341. if (is_broadcast_ether_addr(mask->dst)) {
  6342. field_flags |= I40E_CLOUD_FIELD_OMAC;
  6343. } else {
  6344. dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
  6345. mask->dst);
  6346. return I40E_ERR_CONFIG;
  6347. }
  6348. }
  6349. if (!is_zero_ether_addr(mask->src)) {
  6350. if (is_broadcast_ether_addr(mask->src)) {
  6351. field_flags |= I40E_CLOUD_FIELD_IMAC;
  6352. } else {
  6353. dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
  6354. mask->src);
  6355. return I40E_ERR_CONFIG;
  6356. }
  6357. }
  6358. ether_addr_copy(filter->dst_mac, key->dst);
  6359. ether_addr_copy(filter->src_mac, key->src);
  6360. }
  6361. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
  6362. struct flow_dissector_key_vlan *key =
  6363. skb_flow_dissector_target(f->dissector,
  6364. FLOW_DISSECTOR_KEY_VLAN,
  6365. f->key);
  6366. struct flow_dissector_key_vlan *mask =
  6367. skb_flow_dissector_target(f->dissector,
  6368. FLOW_DISSECTOR_KEY_VLAN,
  6369. f->mask);
  6370. if (mask->vlan_id) {
  6371. if (mask->vlan_id == VLAN_VID_MASK) {
  6372. field_flags |= I40E_CLOUD_FIELD_IVLAN;
  6373. } else {
  6374. dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
  6375. mask->vlan_id);
  6376. return I40E_ERR_CONFIG;
  6377. }
  6378. }
  6379. filter->vlan_id = cpu_to_be16(key->vlan_id);
  6380. }
  6381. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
  6382. struct flow_dissector_key_control *key =
  6383. skb_flow_dissector_target(f->dissector,
  6384. FLOW_DISSECTOR_KEY_CONTROL,
  6385. f->key);
  6386. addr_type = key->addr_type;
  6387. }
  6388. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  6389. struct flow_dissector_key_ipv4_addrs *key =
  6390. skb_flow_dissector_target(f->dissector,
  6391. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6392. f->key);
  6393. struct flow_dissector_key_ipv4_addrs *mask =
  6394. skb_flow_dissector_target(f->dissector,
  6395. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6396. f->mask);
  6397. if (mask->dst) {
  6398. if (mask->dst == cpu_to_be32(0xffffffff)) {
  6399. field_flags |= I40E_CLOUD_FIELD_IIP;
  6400. } else {
  6401. dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
  6402. &mask->dst);
  6403. return I40E_ERR_CONFIG;
  6404. }
  6405. }
  6406. if (mask->src) {
  6407. if (mask->src == cpu_to_be32(0xffffffff)) {
  6408. field_flags |= I40E_CLOUD_FIELD_IIP;
  6409. } else {
  6410. dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
  6411. &mask->src);
  6412. return I40E_ERR_CONFIG;
  6413. }
  6414. }
  6415. if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
  6416. dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
  6417. return I40E_ERR_CONFIG;
  6418. }
  6419. filter->dst_ipv4 = key->dst;
  6420. filter->src_ipv4 = key->src;
  6421. }
  6422. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  6423. struct flow_dissector_key_ipv6_addrs *key =
  6424. skb_flow_dissector_target(f->dissector,
  6425. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6426. f->key);
  6427. struct flow_dissector_key_ipv6_addrs *mask =
  6428. skb_flow_dissector_target(f->dissector,
  6429. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6430. f->mask);
  6431. /* src and dest IPV6 address should not be LOOPBACK
  6432. * (0:0:0:0:0:0:0:1), which can be represented as ::1
  6433. */
  6434. if (ipv6_addr_loopback(&key->dst) ||
  6435. ipv6_addr_loopback(&key->src)) {
  6436. dev_err(&pf->pdev->dev,
  6437. "Bad ipv6, addr is LOOPBACK\n");
  6438. return I40E_ERR_CONFIG;
  6439. }
  6440. if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
  6441. field_flags |= I40E_CLOUD_FIELD_IIP;
  6442. memcpy(&filter->src_ipv6, &key->src.s6_addr32,
  6443. sizeof(filter->src_ipv6));
  6444. memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
  6445. sizeof(filter->dst_ipv6));
  6446. }
  6447. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
  6448. struct flow_dissector_key_ports *key =
  6449. skb_flow_dissector_target(f->dissector,
  6450. FLOW_DISSECTOR_KEY_PORTS,
  6451. f->key);
  6452. struct flow_dissector_key_ports *mask =
  6453. skb_flow_dissector_target(f->dissector,
  6454. FLOW_DISSECTOR_KEY_PORTS,
  6455. f->mask);
  6456. if (mask->src) {
  6457. if (mask->src == cpu_to_be16(0xffff)) {
  6458. field_flags |= I40E_CLOUD_FIELD_IIP;
  6459. } else {
  6460. dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
  6461. be16_to_cpu(mask->src));
  6462. return I40E_ERR_CONFIG;
  6463. }
  6464. }
  6465. if (mask->dst) {
  6466. if (mask->dst == cpu_to_be16(0xffff)) {
  6467. field_flags |= I40E_CLOUD_FIELD_IIP;
  6468. } else {
  6469. dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
  6470. be16_to_cpu(mask->dst));
  6471. return I40E_ERR_CONFIG;
  6472. }
  6473. }
  6474. filter->dst_port = key->dst;
  6475. filter->src_port = key->src;
  6476. switch (filter->ip_proto) {
  6477. case IPPROTO_TCP:
  6478. case IPPROTO_UDP:
  6479. break;
  6480. default:
  6481. dev_err(&pf->pdev->dev,
  6482. "Only UDP and TCP transport are supported\n");
  6483. return -EINVAL;
  6484. }
  6485. }
  6486. filter->flags = field_flags;
  6487. return 0;
  6488. }
  6489. /**
  6490. * i40e_handle_tclass: Forward to a traffic class on the device
  6491. * @vsi: Pointer to VSI
  6492. * @tc: traffic class index on the device
  6493. * @filter: Pointer to cloud filter structure
  6494. *
  6495. **/
  6496. static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
  6497. struct i40e_cloud_filter *filter)
  6498. {
  6499. struct i40e_channel *ch, *ch_tmp;
  6500. /* direct to a traffic class on the same device */
  6501. if (tc == 0) {
  6502. filter->seid = vsi->seid;
  6503. return 0;
  6504. } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
  6505. if (!filter->dst_port) {
  6506. dev_err(&vsi->back->pdev->dev,
  6507. "Specify destination port to direct to traffic class that is not default\n");
  6508. return -EINVAL;
  6509. }
  6510. if (list_empty(&vsi->ch_list))
  6511. return -EINVAL;
  6512. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
  6513. list) {
  6514. if (ch->seid == vsi->tc_seid_map[tc])
  6515. filter->seid = ch->seid;
  6516. }
  6517. return 0;
  6518. }
  6519. dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
  6520. return -EINVAL;
  6521. }
  6522. /**
  6523. * i40e_configure_clsflower - Configure tc flower filters
  6524. * @vsi: Pointer to VSI
  6525. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6526. *
  6527. **/
  6528. static int i40e_configure_clsflower(struct i40e_vsi *vsi,
  6529. struct tc_cls_flower_offload *cls_flower)
  6530. {
  6531. int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
  6532. struct i40e_cloud_filter *filter = NULL;
  6533. struct i40e_pf *pf = vsi->back;
  6534. int err = 0;
  6535. if (tc < 0) {
  6536. dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
  6537. return -EOPNOTSUPP;
  6538. }
  6539. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  6540. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  6541. return -EBUSY;
  6542. if (pf->fdir_pf_active_filters ||
  6543. (!hlist_empty(&pf->fdir_filter_list))) {
  6544. dev_err(&vsi->back->pdev->dev,
  6545. "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
  6546. return -EINVAL;
  6547. }
  6548. if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
  6549. dev_err(&vsi->back->pdev->dev,
  6550. "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
  6551. vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6552. vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6553. }
  6554. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  6555. if (!filter)
  6556. return -ENOMEM;
  6557. filter->cookie = cls_flower->cookie;
  6558. err = i40e_parse_cls_flower(vsi, cls_flower, filter);
  6559. if (err < 0)
  6560. goto err;
  6561. err = i40e_handle_tclass(vsi, tc, filter);
  6562. if (err < 0)
  6563. goto err;
  6564. /* Add cloud filter */
  6565. if (filter->dst_port)
  6566. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
  6567. else
  6568. err = i40e_add_del_cloud_filter(vsi, filter, true);
  6569. if (err) {
  6570. dev_err(&pf->pdev->dev,
  6571. "Failed to add cloud filter, err %s\n",
  6572. i40e_stat_str(&pf->hw, err));
  6573. goto err;
  6574. }
  6575. /* add filter to the ordered list */
  6576. INIT_HLIST_NODE(&filter->cloud_node);
  6577. hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
  6578. pf->num_cloud_filters++;
  6579. return err;
  6580. err:
  6581. kfree(filter);
  6582. return err;
  6583. }
  6584. /**
  6585. * i40e_find_cloud_filter - Find the could filter in the list
  6586. * @vsi: Pointer to VSI
  6587. * @cookie: filter specific cookie
  6588. *
  6589. **/
  6590. static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
  6591. unsigned long *cookie)
  6592. {
  6593. struct i40e_cloud_filter *filter = NULL;
  6594. struct hlist_node *node2;
  6595. hlist_for_each_entry_safe(filter, node2,
  6596. &vsi->back->cloud_filter_list, cloud_node)
  6597. if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
  6598. return filter;
  6599. return NULL;
  6600. }
  6601. /**
  6602. * i40e_delete_clsflower - Remove tc flower filters
  6603. * @vsi: Pointer to VSI
  6604. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6605. *
  6606. **/
  6607. static int i40e_delete_clsflower(struct i40e_vsi *vsi,
  6608. struct tc_cls_flower_offload *cls_flower)
  6609. {
  6610. struct i40e_cloud_filter *filter = NULL;
  6611. struct i40e_pf *pf = vsi->back;
  6612. int err = 0;
  6613. filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
  6614. if (!filter)
  6615. return -EINVAL;
  6616. hash_del(&filter->cloud_node);
  6617. if (filter->dst_port)
  6618. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
  6619. else
  6620. err = i40e_add_del_cloud_filter(vsi, filter, false);
  6621. kfree(filter);
  6622. if (err) {
  6623. dev_err(&pf->pdev->dev,
  6624. "Failed to delete cloud filter, err %s\n",
  6625. i40e_stat_str(&pf->hw, err));
  6626. return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
  6627. }
  6628. pf->num_cloud_filters--;
  6629. if (!pf->num_cloud_filters)
  6630. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6631. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6632. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6633. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6634. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6635. }
  6636. return 0;
  6637. }
  6638. /**
  6639. * i40e_setup_tc_cls_flower - flower classifier offloads
  6640. * @netdev: net device to configure
  6641. * @type_data: offload data
  6642. **/
  6643. static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
  6644. struct tc_cls_flower_offload *cls_flower)
  6645. {
  6646. struct i40e_vsi *vsi = np->vsi;
  6647. switch (cls_flower->command) {
  6648. case TC_CLSFLOWER_REPLACE:
  6649. return i40e_configure_clsflower(vsi, cls_flower);
  6650. case TC_CLSFLOWER_DESTROY:
  6651. return i40e_delete_clsflower(vsi, cls_flower);
  6652. case TC_CLSFLOWER_STATS:
  6653. return -EOPNOTSUPP;
  6654. default:
  6655. return -EOPNOTSUPP;
  6656. }
  6657. }
  6658. static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  6659. void *cb_priv)
  6660. {
  6661. struct i40e_netdev_priv *np = cb_priv;
  6662. if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
  6663. return -EOPNOTSUPP;
  6664. switch (type) {
  6665. case TC_SETUP_CLSFLOWER:
  6666. return i40e_setup_tc_cls_flower(np, type_data);
  6667. default:
  6668. return -EOPNOTSUPP;
  6669. }
  6670. }
  6671. static int i40e_setup_tc_block(struct net_device *dev,
  6672. struct tc_block_offload *f)
  6673. {
  6674. struct i40e_netdev_priv *np = netdev_priv(dev);
  6675. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  6676. return -EOPNOTSUPP;
  6677. switch (f->command) {
  6678. case TC_BLOCK_BIND:
  6679. return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
  6680. np, np, f->extack);
  6681. case TC_BLOCK_UNBIND:
  6682. tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
  6683. return 0;
  6684. default:
  6685. return -EOPNOTSUPP;
  6686. }
  6687. }
  6688. static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  6689. void *type_data)
  6690. {
  6691. switch (type) {
  6692. case TC_SETUP_QDISC_MQPRIO:
  6693. return i40e_setup_tc(netdev, type_data);
  6694. case TC_SETUP_BLOCK:
  6695. return i40e_setup_tc_block(netdev, type_data);
  6696. default:
  6697. return -EOPNOTSUPP;
  6698. }
  6699. }
  6700. /**
  6701. * i40e_open - Called when a network interface is made active
  6702. * @netdev: network interface device structure
  6703. *
  6704. * The open entry point is called when a network interface is made
  6705. * active by the system (IFF_UP). At this point all resources needed
  6706. * for transmit and receive operations are allocated, the interrupt
  6707. * handler is registered with the OS, the netdev watchdog subtask is
  6708. * enabled, and the stack is notified that the interface is ready.
  6709. *
  6710. * Returns 0 on success, negative value on failure
  6711. **/
  6712. int i40e_open(struct net_device *netdev)
  6713. {
  6714. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6715. struct i40e_vsi *vsi = np->vsi;
  6716. struct i40e_pf *pf = vsi->back;
  6717. int err;
  6718. /* disallow open during test or if eeprom is broken */
  6719. if (test_bit(__I40E_TESTING, pf->state) ||
  6720. test_bit(__I40E_BAD_EEPROM, pf->state))
  6721. return -EBUSY;
  6722. netif_carrier_off(netdev);
  6723. if (i40e_force_link_state(pf, true))
  6724. return -EAGAIN;
  6725. err = i40e_vsi_open(vsi);
  6726. if (err)
  6727. return err;
  6728. /* configure global TSO hardware offload settings */
  6729. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  6730. TCP_FLAG_FIN) >> 16);
  6731. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  6732. TCP_FLAG_FIN |
  6733. TCP_FLAG_CWR) >> 16);
  6734. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  6735. udp_tunnel_get_rx_info(netdev);
  6736. return 0;
  6737. }
  6738. /**
  6739. * i40e_vsi_open -
  6740. * @vsi: the VSI to open
  6741. *
  6742. * Finish initialization of the VSI.
  6743. *
  6744. * Returns 0 on success, negative value on failure
  6745. *
  6746. * Note: expects to be called while under rtnl_lock()
  6747. **/
  6748. int i40e_vsi_open(struct i40e_vsi *vsi)
  6749. {
  6750. struct i40e_pf *pf = vsi->back;
  6751. char int_name[I40E_INT_NAME_STR_LEN];
  6752. int err;
  6753. /* allocate descriptors */
  6754. err = i40e_vsi_setup_tx_resources(vsi);
  6755. if (err)
  6756. goto err_setup_tx;
  6757. err = i40e_vsi_setup_rx_resources(vsi);
  6758. if (err)
  6759. goto err_setup_rx;
  6760. err = i40e_vsi_configure(vsi);
  6761. if (err)
  6762. goto err_setup_rx;
  6763. if (vsi->netdev) {
  6764. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  6765. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  6766. err = i40e_vsi_request_irq(vsi, int_name);
  6767. if (err)
  6768. goto err_setup_rx;
  6769. /* Notify the stack of the actual queue counts. */
  6770. err = netif_set_real_num_tx_queues(vsi->netdev,
  6771. vsi->num_queue_pairs);
  6772. if (err)
  6773. goto err_set_queues;
  6774. err = netif_set_real_num_rx_queues(vsi->netdev,
  6775. vsi->num_queue_pairs);
  6776. if (err)
  6777. goto err_set_queues;
  6778. } else if (vsi->type == I40E_VSI_FDIR) {
  6779. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  6780. dev_driver_string(&pf->pdev->dev),
  6781. dev_name(&pf->pdev->dev));
  6782. err = i40e_vsi_request_irq(vsi, int_name);
  6783. } else {
  6784. err = -EINVAL;
  6785. goto err_setup_rx;
  6786. }
  6787. err = i40e_up_complete(vsi);
  6788. if (err)
  6789. goto err_up_complete;
  6790. return 0;
  6791. err_up_complete:
  6792. i40e_down(vsi);
  6793. err_set_queues:
  6794. i40e_vsi_free_irq(vsi);
  6795. err_setup_rx:
  6796. i40e_vsi_free_rx_resources(vsi);
  6797. err_setup_tx:
  6798. i40e_vsi_free_tx_resources(vsi);
  6799. if (vsi == pf->vsi[pf->lan_vsi])
  6800. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  6801. return err;
  6802. }
  6803. /**
  6804. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  6805. * @pf: Pointer to PF
  6806. *
  6807. * This function destroys the hlist where all the Flow Director
  6808. * filters were saved.
  6809. **/
  6810. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  6811. {
  6812. struct i40e_fdir_filter *filter;
  6813. struct i40e_flex_pit *pit_entry, *tmp;
  6814. struct hlist_node *node2;
  6815. hlist_for_each_entry_safe(filter, node2,
  6816. &pf->fdir_filter_list, fdir_node) {
  6817. hlist_del(&filter->fdir_node);
  6818. kfree(filter);
  6819. }
  6820. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  6821. list_del(&pit_entry->list);
  6822. kfree(pit_entry);
  6823. }
  6824. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  6825. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  6826. list_del(&pit_entry->list);
  6827. kfree(pit_entry);
  6828. }
  6829. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  6830. pf->fdir_pf_active_filters = 0;
  6831. pf->fd_tcp4_filter_cnt = 0;
  6832. pf->fd_udp4_filter_cnt = 0;
  6833. pf->fd_sctp4_filter_cnt = 0;
  6834. pf->fd_ip4_filter_cnt = 0;
  6835. /* Reprogram the default input set for TCP/IPv4 */
  6836. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  6837. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6838. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6839. /* Reprogram the default input set for UDP/IPv4 */
  6840. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  6841. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6842. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6843. /* Reprogram the default input set for SCTP/IPv4 */
  6844. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  6845. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6846. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6847. /* Reprogram the default input set for Other/IPv4 */
  6848. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  6849. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6850. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
  6851. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6852. }
  6853. /**
  6854. * i40e_cloud_filter_exit - Cleans up the cloud filters
  6855. * @pf: Pointer to PF
  6856. *
  6857. * This function destroys the hlist where all the cloud filters
  6858. * were saved.
  6859. **/
  6860. static void i40e_cloud_filter_exit(struct i40e_pf *pf)
  6861. {
  6862. struct i40e_cloud_filter *cfilter;
  6863. struct hlist_node *node;
  6864. hlist_for_each_entry_safe(cfilter, node,
  6865. &pf->cloud_filter_list, cloud_node) {
  6866. hlist_del(&cfilter->cloud_node);
  6867. kfree(cfilter);
  6868. }
  6869. pf->num_cloud_filters = 0;
  6870. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6871. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6872. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6873. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6874. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6875. }
  6876. }
  6877. /**
  6878. * i40e_close - Disables a network interface
  6879. * @netdev: network interface device structure
  6880. *
  6881. * The close entry point is called when an interface is de-activated
  6882. * by the OS. The hardware is still under the driver's control, but
  6883. * this netdev interface is disabled.
  6884. *
  6885. * Returns 0, this is not allowed to fail
  6886. **/
  6887. int i40e_close(struct net_device *netdev)
  6888. {
  6889. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6890. struct i40e_vsi *vsi = np->vsi;
  6891. i40e_vsi_close(vsi);
  6892. return 0;
  6893. }
  6894. /**
  6895. * i40e_do_reset - Start a PF or Core Reset sequence
  6896. * @pf: board private structure
  6897. * @reset_flags: which reset is requested
  6898. * @lock_acquired: indicates whether or not the lock has been acquired
  6899. * before this function was called.
  6900. *
  6901. * The essential difference in resets is that the PF Reset
  6902. * doesn't clear the packet buffers, doesn't reset the PE
  6903. * firmware, and doesn't bother the other PFs on the chip.
  6904. **/
  6905. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  6906. {
  6907. u32 val;
  6908. WARN_ON(in_interrupt());
  6909. /* do the biggest reset indicated */
  6910. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  6911. /* Request a Global Reset
  6912. *
  6913. * This will start the chip's countdown to the actual full
  6914. * chip reset event, and a warning interrupt to be sent
  6915. * to all PFs, including the requestor. Our handler
  6916. * for the warning interrupt will deal with the shutdown
  6917. * and recovery of the switch setup.
  6918. */
  6919. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  6920. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6921. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  6922. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6923. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  6924. /* Request a Core Reset
  6925. *
  6926. * Same as Global Reset, except does *not* include the MAC/PHY
  6927. */
  6928. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  6929. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6930. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  6931. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6932. i40e_flush(&pf->hw);
  6933. } else if (reset_flags & I40E_PF_RESET_FLAG) {
  6934. /* Request a PF Reset
  6935. *
  6936. * Resets only the PF-specific registers
  6937. *
  6938. * This goes directly to the tear-down and rebuild of
  6939. * the switch, since we need to do all the recovery as
  6940. * for the Core Reset.
  6941. */
  6942. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  6943. i40e_handle_reset_warning(pf, lock_acquired);
  6944. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  6945. int v;
  6946. /* Find the VSI(s) that requested a re-init */
  6947. dev_info(&pf->pdev->dev,
  6948. "VSI reinit requested\n");
  6949. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6950. struct i40e_vsi *vsi = pf->vsi[v];
  6951. if (vsi != NULL &&
  6952. test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
  6953. vsi->state))
  6954. i40e_vsi_reinit_locked(pf->vsi[v]);
  6955. }
  6956. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  6957. int v;
  6958. /* Find the VSI(s) that needs to be brought down */
  6959. dev_info(&pf->pdev->dev, "VSI down requested\n");
  6960. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6961. struct i40e_vsi *vsi = pf->vsi[v];
  6962. if (vsi != NULL &&
  6963. test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
  6964. vsi->state)) {
  6965. set_bit(__I40E_VSI_DOWN, vsi->state);
  6966. i40e_down(vsi);
  6967. }
  6968. }
  6969. } else {
  6970. dev_info(&pf->pdev->dev,
  6971. "bad reset request 0x%08x\n", reset_flags);
  6972. }
  6973. }
  6974. #ifdef CONFIG_I40E_DCB
  6975. /**
  6976. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  6977. * @pf: board private structure
  6978. * @old_cfg: current DCB config
  6979. * @new_cfg: new DCB config
  6980. **/
  6981. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  6982. struct i40e_dcbx_config *old_cfg,
  6983. struct i40e_dcbx_config *new_cfg)
  6984. {
  6985. bool need_reconfig = false;
  6986. /* Check if ETS configuration has changed */
  6987. if (memcmp(&new_cfg->etscfg,
  6988. &old_cfg->etscfg,
  6989. sizeof(new_cfg->etscfg))) {
  6990. /* If Priority Table has changed reconfig is needed */
  6991. if (memcmp(&new_cfg->etscfg.prioritytable,
  6992. &old_cfg->etscfg.prioritytable,
  6993. sizeof(new_cfg->etscfg.prioritytable))) {
  6994. need_reconfig = true;
  6995. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  6996. }
  6997. if (memcmp(&new_cfg->etscfg.tcbwtable,
  6998. &old_cfg->etscfg.tcbwtable,
  6999. sizeof(new_cfg->etscfg.tcbwtable)))
  7000. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  7001. if (memcmp(&new_cfg->etscfg.tsatable,
  7002. &old_cfg->etscfg.tsatable,
  7003. sizeof(new_cfg->etscfg.tsatable)))
  7004. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  7005. }
  7006. /* Check if PFC configuration has changed */
  7007. if (memcmp(&new_cfg->pfc,
  7008. &old_cfg->pfc,
  7009. sizeof(new_cfg->pfc))) {
  7010. need_reconfig = true;
  7011. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  7012. }
  7013. /* Check if APP Table has changed */
  7014. if (memcmp(&new_cfg->app,
  7015. &old_cfg->app,
  7016. sizeof(new_cfg->app))) {
  7017. need_reconfig = true;
  7018. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  7019. }
  7020. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  7021. return need_reconfig;
  7022. }
  7023. /**
  7024. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  7025. * @pf: board private structure
  7026. * @e: event info posted on ARQ
  7027. **/
  7028. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  7029. struct i40e_arq_event_info *e)
  7030. {
  7031. struct i40e_aqc_lldp_get_mib *mib =
  7032. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  7033. struct i40e_hw *hw = &pf->hw;
  7034. struct i40e_dcbx_config tmp_dcbx_cfg;
  7035. bool need_reconfig = false;
  7036. int ret = 0;
  7037. u8 type;
  7038. /* Not DCB capable or capability disabled */
  7039. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  7040. return ret;
  7041. /* Ignore if event is not for Nearest Bridge */
  7042. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  7043. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  7044. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  7045. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  7046. return ret;
  7047. /* Check MIB Type and return if event for Remote MIB update */
  7048. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  7049. dev_dbg(&pf->pdev->dev,
  7050. "LLDP event mib type %s\n", type ? "remote" : "local");
  7051. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  7052. /* Update the remote cached instance and return */
  7053. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  7054. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  7055. &hw->remote_dcbx_config);
  7056. goto exit;
  7057. }
  7058. /* Store the old configuration */
  7059. tmp_dcbx_cfg = hw->local_dcbx_config;
  7060. /* Reset the old DCBx configuration data */
  7061. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  7062. /* Get updated DCBX data from firmware */
  7063. ret = i40e_get_dcb_config(&pf->hw);
  7064. if (ret) {
  7065. dev_info(&pf->pdev->dev,
  7066. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  7067. i40e_stat_str(&pf->hw, ret),
  7068. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7069. goto exit;
  7070. }
  7071. /* No change detected in DCBX configs */
  7072. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  7073. sizeof(tmp_dcbx_cfg))) {
  7074. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  7075. goto exit;
  7076. }
  7077. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  7078. &hw->local_dcbx_config);
  7079. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  7080. if (!need_reconfig)
  7081. goto exit;
  7082. /* Enable DCB tagging only when more than one TC */
  7083. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  7084. pf->flags |= I40E_FLAG_DCB_ENABLED;
  7085. else
  7086. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  7087. set_bit(__I40E_PORT_SUSPENDED, pf->state);
  7088. /* Reconfiguration needed quiesce all VSIs */
  7089. i40e_pf_quiesce_all_vsi(pf);
  7090. /* Changes in configuration update VEB/VSI */
  7091. i40e_dcb_reconfigure(pf);
  7092. ret = i40e_resume_port_tx(pf);
  7093. clear_bit(__I40E_PORT_SUSPENDED, pf->state);
  7094. /* In case of error no point in resuming VSIs */
  7095. if (ret)
  7096. goto exit;
  7097. /* Wait for the PF's queues to be disabled */
  7098. ret = i40e_pf_wait_queues_disabled(pf);
  7099. if (ret) {
  7100. /* Schedule PF reset to recover */
  7101. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7102. i40e_service_event_schedule(pf);
  7103. } else {
  7104. i40e_pf_unquiesce_all_vsi(pf);
  7105. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  7106. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  7107. }
  7108. exit:
  7109. return ret;
  7110. }
  7111. #endif /* CONFIG_I40E_DCB */
  7112. /**
  7113. * i40e_do_reset_safe - Protected reset path for userland calls.
  7114. * @pf: board private structure
  7115. * @reset_flags: which reset is requested
  7116. *
  7117. **/
  7118. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  7119. {
  7120. rtnl_lock();
  7121. i40e_do_reset(pf, reset_flags, true);
  7122. rtnl_unlock();
  7123. }
  7124. /**
  7125. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  7126. * @pf: board private structure
  7127. * @e: event info posted on ARQ
  7128. *
  7129. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  7130. * and VF queues
  7131. **/
  7132. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  7133. struct i40e_arq_event_info *e)
  7134. {
  7135. struct i40e_aqc_lan_overflow *data =
  7136. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  7137. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  7138. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  7139. struct i40e_hw *hw = &pf->hw;
  7140. struct i40e_vf *vf;
  7141. u16 vf_id;
  7142. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  7143. queue, qtx_ctl);
  7144. /* Queue belongs to VF, find the VF and issue VF reset */
  7145. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  7146. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  7147. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  7148. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  7149. vf_id -= hw->func_caps.vf_base_id;
  7150. vf = &pf->vf[vf_id];
  7151. i40e_vc_notify_vf_reset(vf);
  7152. /* Allow VF to process pending reset notification */
  7153. msleep(20);
  7154. i40e_reset_vf(vf, false);
  7155. }
  7156. }
  7157. /**
  7158. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  7159. * @pf: board private structure
  7160. **/
  7161. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  7162. {
  7163. u32 val, fcnt_prog;
  7164. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7165. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  7166. return fcnt_prog;
  7167. }
  7168. /**
  7169. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  7170. * @pf: board private structure
  7171. **/
  7172. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  7173. {
  7174. u32 val, fcnt_prog;
  7175. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7176. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  7177. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  7178. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  7179. return fcnt_prog;
  7180. }
  7181. /**
  7182. * i40e_get_global_fd_count - Get total FD filters programmed on device
  7183. * @pf: board private structure
  7184. **/
  7185. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  7186. {
  7187. u32 val, fcnt_prog;
  7188. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  7189. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  7190. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  7191. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  7192. return fcnt_prog;
  7193. }
  7194. /**
  7195. * i40e_reenable_fdir_sb - Restore FDir SB capability
  7196. * @pf: board private structure
  7197. **/
  7198. static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
  7199. {
  7200. if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  7201. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  7202. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7203. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  7204. }
  7205. /**
  7206. * i40e_reenable_fdir_atr - Restore FDir ATR capability
  7207. * @pf: board private structure
  7208. **/
  7209. static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
  7210. {
  7211. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
  7212. /* ATR uses the same filtering logic as SB rules. It only
  7213. * functions properly if the input set mask is at the default
  7214. * settings. It is safe to restore the default input set
  7215. * because there are no active TCPv4 filter rules.
  7216. */
  7217. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  7218. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  7219. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  7220. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7221. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7222. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  7223. }
  7224. }
  7225. /**
  7226. * i40e_delete_invalid_filter - Delete an invalid FDIR filter
  7227. * @pf: board private structure
  7228. * @filter: FDir filter to remove
  7229. */
  7230. static void i40e_delete_invalid_filter(struct i40e_pf *pf,
  7231. struct i40e_fdir_filter *filter)
  7232. {
  7233. /* Update counters */
  7234. pf->fdir_pf_active_filters--;
  7235. pf->fd_inv = 0;
  7236. switch (filter->flow_type) {
  7237. case TCP_V4_FLOW:
  7238. pf->fd_tcp4_filter_cnt--;
  7239. break;
  7240. case UDP_V4_FLOW:
  7241. pf->fd_udp4_filter_cnt--;
  7242. break;
  7243. case SCTP_V4_FLOW:
  7244. pf->fd_sctp4_filter_cnt--;
  7245. break;
  7246. case IP_USER_FLOW:
  7247. switch (filter->ip4_proto) {
  7248. case IPPROTO_TCP:
  7249. pf->fd_tcp4_filter_cnt--;
  7250. break;
  7251. case IPPROTO_UDP:
  7252. pf->fd_udp4_filter_cnt--;
  7253. break;
  7254. case IPPROTO_SCTP:
  7255. pf->fd_sctp4_filter_cnt--;
  7256. break;
  7257. case IPPROTO_IP:
  7258. pf->fd_ip4_filter_cnt--;
  7259. break;
  7260. }
  7261. break;
  7262. }
  7263. /* Remove the filter from the list and free memory */
  7264. hlist_del(&filter->fdir_node);
  7265. kfree(filter);
  7266. }
  7267. /**
  7268. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  7269. * @pf: board private structure
  7270. **/
  7271. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  7272. {
  7273. struct i40e_fdir_filter *filter;
  7274. u32 fcnt_prog, fcnt_avail;
  7275. struct hlist_node *node;
  7276. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7277. return;
  7278. /* Check if we have enough room to re-enable FDir SB capability. */
  7279. fcnt_prog = i40e_get_global_fd_count(pf);
  7280. fcnt_avail = pf->fdir_pf_filter_count;
  7281. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  7282. (pf->fd_add_err == 0) ||
  7283. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
  7284. i40e_reenable_fdir_sb(pf);
  7285. /* We should wait for even more space before re-enabling ATR.
  7286. * Additionally, we cannot enable ATR as long as we still have TCP SB
  7287. * rules active.
  7288. */
  7289. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
  7290. (pf->fd_tcp4_filter_cnt == 0))
  7291. i40e_reenable_fdir_atr(pf);
  7292. /* if hw had a problem adding a filter, delete it */
  7293. if (pf->fd_inv > 0) {
  7294. hlist_for_each_entry_safe(filter, node,
  7295. &pf->fdir_filter_list, fdir_node)
  7296. if (filter->fd_id == pf->fd_inv)
  7297. i40e_delete_invalid_filter(pf, filter);
  7298. }
  7299. }
  7300. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  7301. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  7302. /**
  7303. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  7304. * @pf: board private structure
  7305. **/
  7306. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  7307. {
  7308. unsigned long min_flush_time;
  7309. int flush_wait_retry = 50;
  7310. bool disable_atr = false;
  7311. int fd_room;
  7312. int reg;
  7313. if (!time_after(jiffies, pf->fd_flush_timestamp +
  7314. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  7315. return;
  7316. /* If the flush is happening too quick and we have mostly SB rules we
  7317. * should not re-enable ATR for some time.
  7318. */
  7319. min_flush_time = pf->fd_flush_timestamp +
  7320. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  7321. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  7322. if (!(time_after(jiffies, min_flush_time)) &&
  7323. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  7324. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7325. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  7326. disable_atr = true;
  7327. }
  7328. pf->fd_flush_timestamp = jiffies;
  7329. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7330. /* flush all filters */
  7331. wr32(&pf->hw, I40E_PFQF_CTL_1,
  7332. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  7333. i40e_flush(&pf->hw);
  7334. pf->fd_flush_cnt++;
  7335. pf->fd_add_err = 0;
  7336. do {
  7337. /* Check FD flush status every 5-6msec */
  7338. usleep_range(5000, 6000);
  7339. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  7340. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  7341. break;
  7342. } while (flush_wait_retry--);
  7343. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  7344. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  7345. } else {
  7346. /* replay sideband filters */
  7347. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  7348. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  7349. clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7350. clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  7351. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7352. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  7353. }
  7354. }
  7355. /**
  7356. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  7357. * @pf: board private structure
  7358. **/
  7359. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  7360. {
  7361. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  7362. }
  7363. /* We can see up to 256 filter programming desc in transit if the filters are
  7364. * being applied really fast; before we see the first
  7365. * filter miss error on Rx queue 0. Accumulating enough error messages before
  7366. * reacting will make sure we don't cause flush too often.
  7367. */
  7368. #define I40E_MAX_FD_PROGRAM_ERROR 256
  7369. /**
  7370. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  7371. * @pf: board private structure
  7372. **/
  7373. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  7374. {
  7375. /* if interface is down do nothing */
  7376. if (test_bit(__I40E_DOWN, pf->state))
  7377. return;
  7378. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7379. i40e_fdir_flush_and_replay(pf);
  7380. i40e_fdir_check_and_reenable(pf);
  7381. }
  7382. /**
  7383. * i40e_vsi_link_event - notify VSI of a link event
  7384. * @vsi: vsi to be notified
  7385. * @link_up: link up or down
  7386. **/
  7387. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  7388. {
  7389. if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
  7390. return;
  7391. switch (vsi->type) {
  7392. case I40E_VSI_MAIN:
  7393. if (!vsi->netdev || !vsi->netdev_registered)
  7394. break;
  7395. if (link_up) {
  7396. netif_carrier_on(vsi->netdev);
  7397. netif_tx_wake_all_queues(vsi->netdev);
  7398. } else {
  7399. netif_carrier_off(vsi->netdev);
  7400. netif_tx_stop_all_queues(vsi->netdev);
  7401. }
  7402. break;
  7403. case I40E_VSI_SRIOV:
  7404. case I40E_VSI_VMDQ2:
  7405. case I40E_VSI_CTRL:
  7406. case I40E_VSI_IWARP:
  7407. case I40E_VSI_MIRROR:
  7408. default:
  7409. /* there is no notification for other VSIs */
  7410. break;
  7411. }
  7412. }
  7413. /**
  7414. * i40e_veb_link_event - notify elements on the veb of a link event
  7415. * @veb: veb to be notified
  7416. * @link_up: link up or down
  7417. **/
  7418. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  7419. {
  7420. struct i40e_pf *pf;
  7421. int i;
  7422. if (!veb || !veb->pf)
  7423. return;
  7424. pf = veb->pf;
  7425. /* depth first... */
  7426. for (i = 0; i < I40E_MAX_VEB; i++)
  7427. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  7428. i40e_veb_link_event(pf->veb[i], link_up);
  7429. /* ... now the local VSIs */
  7430. for (i = 0; i < pf->num_alloc_vsi; i++)
  7431. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  7432. i40e_vsi_link_event(pf->vsi[i], link_up);
  7433. }
  7434. /**
  7435. * i40e_link_event - Update netif_carrier status
  7436. * @pf: board private structure
  7437. **/
  7438. static void i40e_link_event(struct i40e_pf *pf)
  7439. {
  7440. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7441. u8 new_link_speed, old_link_speed;
  7442. i40e_status status;
  7443. bool new_link, old_link;
  7444. /* set this to force the get_link_status call to refresh state */
  7445. pf->hw.phy.get_link_info = true;
  7446. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  7447. status = i40e_get_link_status(&pf->hw, &new_link);
  7448. /* On success, disable temp link polling */
  7449. if (status == I40E_SUCCESS) {
  7450. clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7451. } else {
  7452. /* Enable link polling temporarily until i40e_get_link_status
  7453. * returns I40E_SUCCESS
  7454. */
  7455. set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7456. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  7457. status);
  7458. return;
  7459. }
  7460. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  7461. new_link_speed = pf->hw.phy.link_info.link_speed;
  7462. if (new_link == old_link &&
  7463. new_link_speed == old_link_speed &&
  7464. (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  7465. new_link == netif_carrier_ok(vsi->netdev)))
  7466. return;
  7467. i40e_print_link_message(vsi, new_link);
  7468. /* Notify the base of the switch tree connected to
  7469. * the link. Floating VEBs are not notified.
  7470. */
  7471. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  7472. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  7473. else
  7474. i40e_vsi_link_event(vsi, new_link);
  7475. if (pf->vf)
  7476. i40e_vc_notify_link_state(pf);
  7477. if (pf->flags & I40E_FLAG_PTP)
  7478. i40e_ptp_set_increment(pf);
  7479. }
  7480. /**
  7481. * i40e_watchdog_subtask - periodic checks not using event driven response
  7482. * @pf: board private structure
  7483. **/
  7484. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  7485. {
  7486. int i;
  7487. /* if interface is down do nothing */
  7488. if (test_bit(__I40E_DOWN, pf->state) ||
  7489. test_bit(__I40E_CONFIG_BUSY, pf->state))
  7490. return;
  7491. /* make sure we don't do these things too often */
  7492. if (time_before(jiffies, (pf->service_timer_previous +
  7493. pf->service_timer_period)))
  7494. return;
  7495. pf->service_timer_previous = jiffies;
  7496. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  7497. test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
  7498. i40e_link_event(pf);
  7499. /* Update the stats for active netdevs so the network stack
  7500. * can look at updated numbers whenever it cares to
  7501. */
  7502. for (i = 0; i < pf->num_alloc_vsi; i++)
  7503. if (pf->vsi[i] && pf->vsi[i]->netdev)
  7504. i40e_update_stats(pf->vsi[i]);
  7505. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  7506. /* Update the stats for the active switching components */
  7507. for (i = 0; i < I40E_MAX_VEB; i++)
  7508. if (pf->veb[i])
  7509. i40e_update_veb_stats(pf->veb[i]);
  7510. }
  7511. i40e_ptp_rx_hang(pf);
  7512. i40e_ptp_tx_hang(pf);
  7513. }
  7514. /**
  7515. * i40e_reset_subtask - Set up for resetting the device and driver
  7516. * @pf: board private structure
  7517. **/
  7518. static void i40e_reset_subtask(struct i40e_pf *pf)
  7519. {
  7520. u32 reset_flags = 0;
  7521. if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
  7522. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  7523. clear_bit(__I40E_REINIT_REQUESTED, pf->state);
  7524. }
  7525. if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
  7526. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  7527. clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7528. }
  7529. if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
  7530. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  7531. clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  7532. }
  7533. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
  7534. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  7535. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  7536. }
  7537. if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
  7538. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  7539. clear_bit(__I40E_DOWN_REQUESTED, pf->state);
  7540. }
  7541. /* If there's a recovery already waiting, it takes
  7542. * precedence before starting a new reset sequence.
  7543. */
  7544. if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
  7545. i40e_prep_for_reset(pf, false);
  7546. i40e_reset(pf);
  7547. i40e_rebuild(pf, false, false);
  7548. }
  7549. /* If we're already down or resetting, just bail */
  7550. if (reset_flags &&
  7551. !test_bit(__I40E_DOWN, pf->state) &&
  7552. !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
  7553. i40e_do_reset(pf, reset_flags, false);
  7554. }
  7555. }
  7556. /**
  7557. * i40e_handle_link_event - Handle link event
  7558. * @pf: board private structure
  7559. * @e: event info posted on ARQ
  7560. **/
  7561. static void i40e_handle_link_event(struct i40e_pf *pf,
  7562. struct i40e_arq_event_info *e)
  7563. {
  7564. struct i40e_aqc_get_link_status *status =
  7565. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  7566. /* Do a new status request to re-enable LSE reporting
  7567. * and load new status information into the hw struct
  7568. * This completely ignores any state information
  7569. * in the ARQ event info, instead choosing to always
  7570. * issue the AQ update link status command.
  7571. */
  7572. i40e_link_event(pf);
  7573. /* Check if module meets thermal requirements */
  7574. if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
  7575. dev_err(&pf->pdev->dev,
  7576. "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
  7577. dev_err(&pf->pdev->dev,
  7578. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7579. } else {
  7580. /* check for unqualified module, if link is down, suppress
  7581. * the message if link was forced to be down.
  7582. */
  7583. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  7584. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  7585. (!(status->link_info & I40E_AQ_LINK_UP)) &&
  7586. (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
  7587. dev_err(&pf->pdev->dev,
  7588. "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
  7589. dev_err(&pf->pdev->dev,
  7590. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7591. }
  7592. }
  7593. }
  7594. /**
  7595. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  7596. * @pf: board private structure
  7597. **/
  7598. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  7599. {
  7600. struct i40e_arq_event_info event;
  7601. struct i40e_hw *hw = &pf->hw;
  7602. u16 pending, i = 0;
  7603. i40e_status ret;
  7604. u16 opcode;
  7605. u32 oldval;
  7606. u32 val;
  7607. /* Do not run clean AQ when PF reset fails */
  7608. if (test_bit(__I40E_RESET_FAILED, pf->state))
  7609. return;
  7610. /* check for error indications */
  7611. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  7612. oldval = val;
  7613. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  7614. if (hw->debug_mask & I40E_DEBUG_AQ)
  7615. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  7616. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  7617. }
  7618. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  7619. if (hw->debug_mask & I40E_DEBUG_AQ)
  7620. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  7621. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  7622. pf->arq_overflows++;
  7623. }
  7624. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  7625. if (hw->debug_mask & I40E_DEBUG_AQ)
  7626. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  7627. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  7628. }
  7629. if (oldval != val)
  7630. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  7631. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  7632. oldval = val;
  7633. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  7634. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7635. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  7636. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  7637. }
  7638. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  7639. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7640. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  7641. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  7642. }
  7643. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  7644. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7645. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  7646. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  7647. }
  7648. if (oldval != val)
  7649. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  7650. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  7651. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  7652. if (!event.msg_buf)
  7653. return;
  7654. do {
  7655. ret = i40e_clean_arq_element(hw, &event, &pending);
  7656. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  7657. break;
  7658. else if (ret) {
  7659. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  7660. break;
  7661. }
  7662. opcode = le16_to_cpu(event.desc.opcode);
  7663. switch (opcode) {
  7664. case i40e_aqc_opc_get_link_status:
  7665. i40e_handle_link_event(pf, &event);
  7666. break;
  7667. case i40e_aqc_opc_send_msg_to_pf:
  7668. ret = i40e_vc_process_vf_msg(pf,
  7669. le16_to_cpu(event.desc.retval),
  7670. le32_to_cpu(event.desc.cookie_high),
  7671. le32_to_cpu(event.desc.cookie_low),
  7672. event.msg_buf,
  7673. event.msg_len);
  7674. break;
  7675. case i40e_aqc_opc_lldp_update_mib:
  7676. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  7677. #ifdef CONFIG_I40E_DCB
  7678. rtnl_lock();
  7679. ret = i40e_handle_lldp_event(pf, &event);
  7680. rtnl_unlock();
  7681. #endif /* CONFIG_I40E_DCB */
  7682. break;
  7683. case i40e_aqc_opc_event_lan_overflow:
  7684. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  7685. i40e_handle_lan_overflow_event(pf, &event);
  7686. break;
  7687. case i40e_aqc_opc_send_msg_to_peer:
  7688. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  7689. break;
  7690. case i40e_aqc_opc_nvm_erase:
  7691. case i40e_aqc_opc_nvm_update:
  7692. case i40e_aqc_opc_oem_post_update:
  7693. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  7694. "ARQ NVM operation 0x%04x completed\n",
  7695. opcode);
  7696. break;
  7697. default:
  7698. dev_info(&pf->pdev->dev,
  7699. "ARQ: Unknown event 0x%04x ignored\n",
  7700. opcode);
  7701. break;
  7702. }
  7703. } while (i++ < pf->adminq_work_limit);
  7704. if (i < pf->adminq_work_limit)
  7705. clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  7706. /* re-enable Admin queue interrupt cause */
  7707. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  7708. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  7709. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  7710. i40e_flush(hw);
  7711. kfree(event.msg_buf);
  7712. }
  7713. /**
  7714. * i40e_verify_eeprom - make sure eeprom is good to use
  7715. * @pf: board private structure
  7716. **/
  7717. static void i40e_verify_eeprom(struct i40e_pf *pf)
  7718. {
  7719. int err;
  7720. err = i40e_diag_eeprom_test(&pf->hw);
  7721. if (err) {
  7722. /* retry in case of garbage read */
  7723. err = i40e_diag_eeprom_test(&pf->hw);
  7724. if (err) {
  7725. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  7726. err);
  7727. set_bit(__I40E_BAD_EEPROM, pf->state);
  7728. }
  7729. }
  7730. if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
  7731. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  7732. clear_bit(__I40E_BAD_EEPROM, pf->state);
  7733. }
  7734. }
  7735. /**
  7736. * i40e_enable_pf_switch_lb
  7737. * @pf: pointer to the PF structure
  7738. *
  7739. * enable switch loop back or die - no point in a return value
  7740. **/
  7741. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  7742. {
  7743. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7744. struct i40e_vsi_context ctxt;
  7745. int ret;
  7746. ctxt.seid = pf->main_vsi_seid;
  7747. ctxt.pf_num = pf->hw.pf_id;
  7748. ctxt.vf_num = 0;
  7749. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7750. if (ret) {
  7751. dev_info(&pf->pdev->dev,
  7752. "couldn't get PF vsi config, err %s aq_err %s\n",
  7753. i40e_stat_str(&pf->hw, ret),
  7754. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7755. return;
  7756. }
  7757. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7758. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7759. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7760. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7761. if (ret) {
  7762. dev_info(&pf->pdev->dev,
  7763. "update vsi switch failed, err %s aq_err %s\n",
  7764. i40e_stat_str(&pf->hw, ret),
  7765. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7766. }
  7767. }
  7768. /**
  7769. * i40e_disable_pf_switch_lb
  7770. * @pf: pointer to the PF structure
  7771. *
  7772. * disable switch loop back or die - no point in a return value
  7773. **/
  7774. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  7775. {
  7776. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7777. struct i40e_vsi_context ctxt;
  7778. int ret;
  7779. ctxt.seid = pf->main_vsi_seid;
  7780. ctxt.pf_num = pf->hw.pf_id;
  7781. ctxt.vf_num = 0;
  7782. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7783. if (ret) {
  7784. dev_info(&pf->pdev->dev,
  7785. "couldn't get PF vsi config, err %s aq_err %s\n",
  7786. i40e_stat_str(&pf->hw, ret),
  7787. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7788. return;
  7789. }
  7790. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7791. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7792. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7793. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7794. if (ret) {
  7795. dev_info(&pf->pdev->dev,
  7796. "update vsi switch failed, err %s aq_err %s\n",
  7797. i40e_stat_str(&pf->hw, ret),
  7798. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7799. }
  7800. }
  7801. /**
  7802. * i40e_config_bridge_mode - Configure the HW bridge mode
  7803. * @veb: pointer to the bridge instance
  7804. *
  7805. * Configure the loop back mode for the LAN VSI that is downlink to the
  7806. * specified HW bridge instance. It is expected this function is called
  7807. * when a new HW bridge is instantiated.
  7808. **/
  7809. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  7810. {
  7811. struct i40e_pf *pf = veb->pf;
  7812. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  7813. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  7814. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  7815. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  7816. i40e_disable_pf_switch_lb(pf);
  7817. else
  7818. i40e_enable_pf_switch_lb(pf);
  7819. }
  7820. /**
  7821. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  7822. * @veb: pointer to the VEB instance
  7823. *
  7824. * This is a recursive function that first builds the attached VSIs then
  7825. * recurses in to build the next layer of VEB. We track the connections
  7826. * through our own index numbers because the seid's from the HW could
  7827. * change across the reset.
  7828. **/
  7829. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  7830. {
  7831. struct i40e_vsi *ctl_vsi = NULL;
  7832. struct i40e_pf *pf = veb->pf;
  7833. int v, veb_idx;
  7834. int ret;
  7835. /* build VSI that owns this VEB, temporarily attached to base VEB */
  7836. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  7837. if (pf->vsi[v] &&
  7838. pf->vsi[v]->veb_idx == veb->idx &&
  7839. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  7840. ctl_vsi = pf->vsi[v];
  7841. break;
  7842. }
  7843. }
  7844. if (!ctl_vsi) {
  7845. dev_info(&pf->pdev->dev,
  7846. "missing owner VSI for veb_idx %d\n", veb->idx);
  7847. ret = -ENOENT;
  7848. goto end_reconstitute;
  7849. }
  7850. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  7851. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  7852. ret = i40e_add_vsi(ctl_vsi);
  7853. if (ret) {
  7854. dev_info(&pf->pdev->dev,
  7855. "rebuild of veb_idx %d owner VSI failed: %d\n",
  7856. veb->idx, ret);
  7857. goto end_reconstitute;
  7858. }
  7859. i40e_vsi_reset_stats(ctl_vsi);
  7860. /* create the VEB in the switch and move the VSI onto the VEB */
  7861. ret = i40e_add_veb(veb, ctl_vsi);
  7862. if (ret)
  7863. goto end_reconstitute;
  7864. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  7865. veb->bridge_mode = BRIDGE_MODE_VEB;
  7866. else
  7867. veb->bridge_mode = BRIDGE_MODE_VEPA;
  7868. i40e_config_bridge_mode(veb);
  7869. /* create the remaining VSIs attached to this VEB */
  7870. for (v = 0; v < pf->num_alloc_vsi; v++) {
  7871. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  7872. continue;
  7873. if (pf->vsi[v]->veb_idx == veb->idx) {
  7874. struct i40e_vsi *vsi = pf->vsi[v];
  7875. vsi->uplink_seid = veb->seid;
  7876. ret = i40e_add_vsi(vsi);
  7877. if (ret) {
  7878. dev_info(&pf->pdev->dev,
  7879. "rebuild of vsi_idx %d failed: %d\n",
  7880. v, ret);
  7881. goto end_reconstitute;
  7882. }
  7883. i40e_vsi_reset_stats(vsi);
  7884. }
  7885. }
  7886. /* create any VEBs attached to this VEB - RECURSION */
  7887. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  7888. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  7889. pf->veb[veb_idx]->uplink_seid = veb->seid;
  7890. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  7891. if (ret)
  7892. break;
  7893. }
  7894. }
  7895. end_reconstitute:
  7896. return ret;
  7897. }
  7898. /**
  7899. * i40e_get_capabilities - get info about the HW
  7900. * @pf: the PF struct
  7901. **/
  7902. static int i40e_get_capabilities(struct i40e_pf *pf,
  7903. enum i40e_admin_queue_opc list_type)
  7904. {
  7905. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  7906. u16 data_size;
  7907. int buf_len;
  7908. int err;
  7909. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  7910. do {
  7911. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  7912. if (!cap_buf)
  7913. return -ENOMEM;
  7914. /* this loads the data into the hw struct for us */
  7915. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  7916. &data_size, list_type,
  7917. NULL);
  7918. /* data loaded, buffer no longer needed */
  7919. kfree(cap_buf);
  7920. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  7921. /* retry with a larger buffer */
  7922. buf_len = data_size;
  7923. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  7924. dev_info(&pf->pdev->dev,
  7925. "capability discovery failed, err %s aq_err %s\n",
  7926. i40e_stat_str(&pf->hw, err),
  7927. i40e_aq_str(&pf->hw,
  7928. pf->hw.aq.asq_last_status));
  7929. return -ENODEV;
  7930. }
  7931. } while (err);
  7932. if (pf->hw.debug_mask & I40E_DEBUG_USER) {
  7933. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7934. dev_info(&pf->pdev->dev,
  7935. "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",
  7936. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  7937. pf->hw.func_caps.num_msix_vectors,
  7938. pf->hw.func_caps.num_msix_vectors_vf,
  7939. pf->hw.func_caps.fd_filters_guaranteed,
  7940. pf->hw.func_caps.fd_filters_best_effort,
  7941. pf->hw.func_caps.num_tx_qp,
  7942. pf->hw.func_caps.num_vsis);
  7943. } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
  7944. dev_info(&pf->pdev->dev,
  7945. "switch_mode=0x%04x, function_valid=0x%08x\n",
  7946. pf->hw.dev_caps.switch_mode,
  7947. pf->hw.dev_caps.valid_functions);
  7948. dev_info(&pf->pdev->dev,
  7949. "SR-IOV=%d, num_vfs for all function=%u\n",
  7950. pf->hw.dev_caps.sr_iov_1_1,
  7951. pf->hw.dev_caps.num_vfs);
  7952. dev_info(&pf->pdev->dev,
  7953. "num_vsis=%u, num_rx:%u, num_tx=%u\n",
  7954. pf->hw.dev_caps.num_vsis,
  7955. pf->hw.dev_caps.num_rx_qp,
  7956. pf->hw.dev_caps.num_tx_qp);
  7957. }
  7958. }
  7959. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7960. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  7961. + pf->hw.func_caps.num_vfs)
  7962. if (pf->hw.revision_id == 0 &&
  7963. pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
  7964. dev_info(&pf->pdev->dev,
  7965. "got num_vsis %d, setting num_vsis to %d\n",
  7966. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  7967. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  7968. }
  7969. }
  7970. return 0;
  7971. }
  7972. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  7973. /**
  7974. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  7975. * @pf: board private structure
  7976. **/
  7977. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  7978. {
  7979. struct i40e_vsi *vsi;
  7980. /* quick workaround for an NVM issue that leaves a critical register
  7981. * uninitialized
  7982. */
  7983. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  7984. static const u32 hkey[] = {
  7985. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  7986. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  7987. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  7988. 0x95b3a76d};
  7989. int i;
  7990. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  7991. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  7992. }
  7993. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7994. return;
  7995. /* find existing VSI and see if it needs configuring */
  7996. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7997. /* create a new VSI if none exists */
  7998. if (!vsi) {
  7999. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  8000. pf->vsi[pf->lan_vsi]->seid, 0);
  8001. if (!vsi) {
  8002. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  8003. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  8004. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  8005. return;
  8006. }
  8007. }
  8008. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  8009. }
  8010. /**
  8011. * i40e_fdir_teardown - release the Flow Director resources
  8012. * @pf: board private structure
  8013. **/
  8014. static void i40e_fdir_teardown(struct i40e_pf *pf)
  8015. {
  8016. struct i40e_vsi *vsi;
  8017. i40e_fdir_filter_exit(pf);
  8018. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  8019. if (vsi)
  8020. i40e_vsi_release(vsi);
  8021. }
  8022. /**
  8023. * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
  8024. * @vsi: PF main vsi
  8025. * @seid: seid of main or channel VSIs
  8026. *
  8027. * Rebuilds cloud filters associated with main VSI and channel VSIs if they
  8028. * existed before reset
  8029. **/
  8030. static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
  8031. {
  8032. struct i40e_cloud_filter *cfilter;
  8033. struct i40e_pf *pf = vsi->back;
  8034. struct hlist_node *node;
  8035. i40e_status ret;
  8036. /* Add cloud filters back if they exist */
  8037. hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
  8038. cloud_node) {
  8039. if (cfilter->seid != seid)
  8040. continue;
  8041. if (cfilter->dst_port)
  8042. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
  8043. true);
  8044. else
  8045. ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
  8046. if (ret) {
  8047. dev_dbg(&pf->pdev->dev,
  8048. "Failed to rebuild cloud filter, err %s aq_err %s\n",
  8049. i40e_stat_str(&pf->hw, ret),
  8050. i40e_aq_str(&pf->hw,
  8051. pf->hw.aq.asq_last_status));
  8052. return ret;
  8053. }
  8054. }
  8055. return 0;
  8056. }
  8057. /**
  8058. * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
  8059. * @vsi: PF main vsi
  8060. *
  8061. * Rebuilds channel VSIs if they existed before reset
  8062. **/
  8063. static int i40e_rebuild_channels(struct i40e_vsi *vsi)
  8064. {
  8065. struct i40e_channel *ch, *ch_tmp;
  8066. i40e_status ret;
  8067. if (list_empty(&vsi->ch_list))
  8068. return 0;
  8069. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  8070. if (!ch->initialized)
  8071. break;
  8072. /* Proceed with creation of channel (VMDq2) VSI */
  8073. ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
  8074. if (ret) {
  8075. dev_info(&vsi->back->pdev->dev,
  8076. "failed to rebuild channels using uplink_seid %u\n",
  8077. vsi->uplink_seid);
  8078. return ret;
  8079. }
  8080. /* Reconfigure TX queues using QTX_CTL register */
  8081. ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
  8082. if (ret) {
  8083. dev_info(&vsi->back->pdev->dev,
  8084. "failed to configure TX rings for channel %u\n",
  8085. ch->seid);
  8086. return ret;
  8087. }
  8088. /* update 'next_base_queue' */
  8089. vsi->next_base_queue = vsi->next_base_queue +
  8090. ch->num_queue_pairs;
  8091. if (ch->max_tx_rate) {
  8092. u64 credits = ch->max_tx_rate;
  8093. if (i40e_set_bw_limit(vsi, ch->seid,
  8094. ch->max_tx_rate))
  8095. return -EINVAL;
  8096. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8097. dev_dbg(&vsi->back->pdev->dev,
  8098. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8099. ch->max_tx_rate,
  8100. credits,
  8101. ch->seid);
  8102. }
  8103. ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
  8104. if (ret) {
  8105. dev_dbg(&vsi->back->pdev->dev,
  8106. "Failed to rebuild cloud filters for channel VSI %u\n",
  8107. ch->seid);
  8108. return ret;
  8109. }
  8110. }
  8111. return 0;
  8112. }
  8113. /**
  8114. * i40e_prep_for_reset - prep for the core to reset
  8115. * @pf: board private structure
  8116. * @lock_acquired: indicates whether or not the lock has been acquired
  8117. * before this function was called.
  8118. *
  8119. * Close up the VFs and other things in prep for PF Reset.
  8120. **/
  8121. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  8122. {
  8123. struct i40e_hw *hw = &pf->hw;
  8124. i40e_status ret = 0;
  8125. u32 v;
  8126. clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  8127. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8128. return;
  8129. if (i40e_check_asq_alive(&pf->hw))
  8130. i40e_vc_notify_reset(pf);
  8131. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  8132. /* quiesce the VSIs and their queues that are not already DOWN */
  8133. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  8134. if (!lock_acquired)
  8135. rtnl_lock();
  8136. i40e_pf_quiesce_all_vsi(pf);
  8137. if (!lock_acquired)
  8138. rtnl_unlock();
  8139. for (v = 0; v < pf->num_alloc_vsi; v++) {
  8140. if (pf->vsi[v])
  8141. pf->vsi[v]->seid = 0;
  8142. }
  8143. i40e_shutdown_adminq(&pf->hw);
  8144. /* call shutdown HMC */
  8145. if (hw->hmc.hmc_obj) {
  8146. ret = i40e_shutdown_lan_hmc(hw);
  8147. if (ret)
  8148. dev_warn(&pf->pdev->dev,
  8149. "shutdown_lan_hmc failed: %d\n", ret);
  8150. }
  8151. }
  8152. /**
  8153. * i40e_send_version - update firmware with driver version
  8154. * @pf: PF struct
  8155. */
  8156. static void i40e_send_version(struct i40e_pf *pf)
  8157. {
  8158. struct i40e_driver_version dv;
  8159. dv.major_version = DRV_VERSION_MAJOR;
  8160. dv.minor_version = DRV_VERSION_MINOR;
  8161. dv.build_version = DRV_VERSION_BUILD;
  8162. dv.subbuild_version = 0;
  8163. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  8164. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  8165. }
  8166. /**
  8167. * i40e_get_oem_version - get OEM specific version information
  8168. * @hw: pointer to the hardware structure
  8169. **/
  8170. static void i40e_get_oem_version(struct i40e_hw *hw)
  8171. {
  8172. u16 block_offset = 0xffff;
  8173. u16 block_length = 0;
  8174. u16 capabilities = 0;
  8175. u16 gen_snap = 0;
  8176. u16 release = 0;
  8177. #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
  8178. #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
  8179. #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
  8180. #define I40E_NVM_OEM_GEN_OFFSET 0x02
  8181. #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
  8182. #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
  8183. #define I40E_NVM_OEM_LENGTH 3
  8184. /* Check if pointer to OEM version block is valid. */
  8185. i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
  8186. if (block_offset == 0xffff)
  8187. return;
  8188. /* Check if OEM version block has correct length. */
  8189. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
  8190. &block_length);
  8191. if (block_length < I40E_NVM_OEM_LENGTH)
  8192. return;
  8193. /* Check if OEM version format is as expected. */
  8194. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
  8195. &capabilities);
  8196. if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
  8197. return;
  8198. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
  8199. &gen_snap);
  8200. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
  8201. &release);
  8202. hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
  8203. hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
  8204. }
  8205. /**
  8206. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  8207. * @pf: board private structure
  8208. **/
  8209. static int i40e_reset(struct i40e_pf *pf)
  8210. {
  8211. struct i40e_hw *hw = &pf->hw;
  8212. i40e_status ret;
  8213. ret = i40e_pf_reset(hw);
  8214. if (ret) {
  8215. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  8216. set_bit(__I40E_RESET_FAILED, pf->state);
  8217. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8218. } else {
  8219. pf->pfr_count++;
  8220. }
  8221. return ret;
  8222. }
  8223. /**
  8224. * i40e_rebuild - rebuild using a saved config
  8225. * @pf: board private structure
  8226. * @reinit: if the Main VSI needs to re-initialized.
  8227. * @lock_acquired: indicates whether or not the lock has been acquired
  8228. * before this function was called.
  8229. **/
  8230. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  8231. {
  8232. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  8233. struct i40e_hw *hw = &pf->hw;
  8234. u8 set_fc_aq_fail = 0;
  8235. i40e_status ret;
  8236. u32 val;
  8237. int v;
  8238. if (test_bit(__I40E_DOWN, pf->state))
  8239. goto clear_recovery;
  8240. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  8241. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  8242. ret = i40e_init_adminq(&pf->hw);
  8243. if (ret) {
  8244. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  8245. i40e_stat_str(&pf->hw, ret),
  8246. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8247. goto clear_recovery;
  8248. }
  8249. i40e_get_oem_version(&pf->hw);
  8250. if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
  8251. ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
  8252. hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
  8253. /* The following delay is necessary for 4.33 firmware and older
  8254. * to recover after EMP reset. 200 ms should suffice but we
  8255. * put here 300 ms to be sure that FW is ready to operate
  8256. * after reset.
  8257. */
  8258. mdelay(300);
  8259. }
  8260. /* re-verify the eeprom if we just had an EMP reset */
  8261. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
  8262. i40e_verify_eeprom(pf);
  8263. i40e_clear_pxe_mode(hw);
  8264. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  8265. if (ret)
  8266. goto end_core_reset;
  8267. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  8268. hw->func_caps.num_rx_qp, 0, 0);
  8269. if (ret) {
  8270. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  8271. goto end_core_reset;
  8272. }
  8273. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  8274. if (ret) {
  8275. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  8276. goto end_core_reset;
  8277. }
  8278. /* Enable FW to write a default DCB config on link-up */
  8279. i40e_aq_set_dcb_parameters(hw, true, NULL);
  8280. #ifdef CONFIG_I40E_DCB
  8281. ret = i40e_init_pf_dcb(pf);
  8282. if (ret) {
  8283. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  8284. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  8285. /* Continue without DCB enabled */
  8286. }
  8287. #endif /* CONFIG_I40E_DCB */
  8288. /* do basic switch setup */
  8289. if (!lock_acquired)
  8290. rtnl_lock();
  8291. ret = i40e_setup_pf_switch(pf, reinit);
  8292. if (ret)
  8293. goto end_unlock;
  8294. /* The driver only wants link up/down and module qualification
  8295. * reports from firmware. Note the negative logic.
  8296. */
  8297. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  8298. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  8299. I40E_AQ_EVENT_MEDIA_NA |
  8300. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  8301. if (ret)
  8302. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  8303. i40e_stat_str(&pf->hw, ret),
  8304. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8305. /* make sure our flow control settings are restored */
  8306. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  8307. if (ret)
  8308. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  8309. i40e_stat_str(&pf->hw, ret),
  8310. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8311. /* Rebuild the VSIs and VEBs that existed before reset.
  8312. * They are still in our local switch element arrays, so only
  8313. * need to rebuild the switch model in the HW.
  8314. *
  8315. * If there were VEBs but the reconstitution failed, we'll try
  8316. * try to recover minimal use by getting the basic PF VSI working.
  8317. */
  8318. if (vsi->uplink_seid != pf->mac_seid) {
  8319. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  8320. /* find the one VEB connected to the MAC, and find orphans */
  8321. for (v = 0; v < I40E_MAX_VEB; v++) {
  8322. if (!pf->veb[v])
  8323. continue;
  8324. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  8325. pf->veb[v]->uplink_seid == 0) {
  8326. ret = i40e_reconstitute_veb(pf->veb[v]);
  8327. if (!ret)
  8328. continue;
  8329. /* If Main VEB failed, we're in deep doodoo,
  8330. * so give up rebuilding the switch and set up
  8331. * for minimal rebuild of PF VSI.
  8332. * If orphan failed, we'll report the error
  8333. * but try to keep going.
  8334. */
  8335. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  8336. dev_info(&pf->pdev->dev,
  8337. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  8338. ret);
  8339. vsi->uplink_seid = pf->mac_seid;
  8340. break;
  8341. } else if (pf->veb[v]->uplink_seid == 0) {
  8342. dev_info(&pf->pdev->dev,
  8343. "rebuild of orphan VEB failed: %d\n",
  8344. ret);
  8345. }
  8346. }
  8347. }
  8348. }
  8349. if (vsi->uplink_seid == pf->mac_seid) {
  8350. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  8351. /* no VEB, so rebuild only the Main VSI */
  8352. ret = i40e_add_vsi(vsi);
  8353. if (ret) {
  8354. dev_info(&pf->pdev->dev,
  8355. "rebuild of Main VSI failed: %d\n", ret);
  8356. goto end_unlock;
  8357. }
  8358. }
  8359. if (vsi->mqprio_qopt.max_rate[0]) {
  8360. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  8361. u64 credits = 0;
  8362. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  8363. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  8364. if (ret)
  8365. goto end_unlock;
  8366. credits = max_tx_rate;
  8367. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8368. dev_dbg(&vsi->back->pdev->dev,
  8369. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8370. max_tx_rate,
  8371. credits,
  8372. vsi->seid);
  8373. }
  8374. ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
  8375. if (ret)
  8376. goto end_unlock;
  8377. /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
  8378. * for this main VSI if they exist
  8379. */
  8380. ret = i40e_rebuild_channels(vsi);
  8381. if (ret)
  8382. goto end_unlock;
  8383. /* Reconfigure hardware for allowing smaller MSS in the case
  8384. * of TSO, so that we avoid the MDD being fired and causing
  8385. * a reset in the case of small MSS+TSO.
  8386. */
  8387. #define I40E_REG_MSS 0x000E64DC
  8388. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  8389. #define I40E_64BYTE_MSS 0x400000
  8390. val = rd32(hw, I40E_REG_MSS);
  8391. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  8392. val &= ~I40E_REG_MSS_MIN_MASK;
  8393. val |= I40E_64BYTE_MSS;
  8394. wr32(hw, I40E_REG_MSS, val);
  8395. }
  8396. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  8397. msleep(75);
  8398. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  8399. if (ret)
  8400. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  8401. i40e_stat_str(&pf->hw, ret),
  8402. i40e_aq_str(&pf->hw,
  8403. pf->hw.aq.asq_last_status));
  8404. }
  8405. /* reinit the misc interrupt */
  8406. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8407. ret = i40e_setup_misc_vector(pf);
  8408. /* Add a filter to drop all Flow control frames from any VSI from being
  8409. * transmitted. By doing so we stop a malicious VF from sending out
  8410. * PAUSE or PFC frames and potentially controlling traffic for other
  8411. * PF/VF VSIs.
  8412. * The FW can still send Flow control frames if enabled.
  8413. */
  8414. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  8415. pf->main_vsi_seid);
  8416. /* restart the VSIs that were rebuilt and running before the reset */
  8417. i40e_pf_unquiesce_all_vsi(pf);
  8418. /* Release the RTNL lock before we start resetting VFs */
  8419. if (!lock_acquired)
  8420. rtnl_unlock();
  8421. /* Restore promiscuous settings */
  8422. ret = i40e_set_promiscuous(pf, pf->cur_promisc);
  8423. if (ret)
  8424. dev_warn(&pf->pdev->dev,
  8425. "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
  8426. pf->cur_promisc ? "on" : "off",
  8427. i40e_stat_str(&pf->hw, ret),
  8428. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8429. i40e_reset_all_vfs(pf, true);
  8430. /* tell the firmware that we're starting */
  8431. i40e_send_version(pf);
  8432. /* We've already released the lock, so don't do it again */
  8433. goto end_core_reset;
  8434. end_unlock:
  8435. if (!lock_acquired)
  8436. rtnl_unlock();
  8437. end_core_reset:
  8438. clear_bit(__I40E_RESET_FAILED, pf->state);
  8439. clear_recovery:
  8440. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8441. }
  8442. /**
  8443. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  8444. * @pf: board private structure
  8445. * @reinit: if the Main VSI needs to re-initialized.
  8446. * @lock_acquired: indicates whether or not the lock has been acquired
  8447. * before this function was called.
  8448. **/
  8449. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  8450. bool lock_acquired)
  8451. {
  8452. int ret;
  8453. /* Now we wait for GRST to settle out.
  8454. * We don't have to delete the VEBs or VSIs from the hw switch
  8455. * because the reset will make them disappear.
  8456. */
  8457. ret = i40e_reset(pf);
  8458. if (!ret)
  8459. i40e_rebuild(pf, reinit, lock_acquired);
  8460. }
  8461. /**
  8462. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  8463. * @pf: board private structure
  8464. *
  8465. * Close up the VFs and other things in prep for a Core Reset,
  8466. * then get ready to rebuild the world.
  8467. * @lock_acquired: indicates whether or not the lock has been acquired
  8468. * before this function was called.
  8469. **/
  8470. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  8471. {
  8472. i40e_prep_for_reset(pf, lock_acquired);
  8473. i40e_reset_and_rebuild(pf, false, lock_acquired);
  8474. }
  8475. /**
  8476. * i40e_handle_mdd_event
  8477. * @pf: pointer to the PF structure
  8478. *
  8479. * Called from the MDD irq handler to identify possibly malicious vfs
  8480. **/
  8481. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  8482. {
  8483. struct i40e_hw *hw = &pf->hw;
  8484. bool mdd_detected = false;
  8485. bool pf_mdd_detected = false;
  8486. struct i40e_vf *vf;
  8487. u32 reg;
  8488. int i;
  8489. if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
  8490. return;
  8491. /* find what triggered the MDD event */
  8492. reg = rd32(hw, I40E_GL_MDET_TX);
  8493. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  8494. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  8495. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  8496. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  8497. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  8498. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  8499. I40E_GL_MDET_TX_EVENT_SHIFT;
  8500. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  8501. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  8502. pf->hw.func_caps.base_queue;
  8503. if (netif_msg_tx_err(pf))
  8504. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  8505. event, queue, pf_num, vf_num);
  8506. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  8507. mdd_detected = true;
  8508. }
  8509. reg = rd32(hw, I40E_GL_MDET_RX);
  8510. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  8511. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  8512. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  8513. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  8514. I40E_GL_MDET_RX_EVENT_SHIFT;
  8515. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  8516. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  8517. pf->hw.func_caps.base_queue;
  8518. if (netif_msg_rx_err(pf))
  8519. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  8520. event, queue, func);
  8521. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  8522. mdd_detected = true;
  8523. }
  8524. if (mdd_detected) {
  8525. reg = rd32(hw, I40E_PF_MDET_TX);
  8526. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  8527. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  8528. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  8529. pf_mdd_detected = true;
  8530. }
  8531. reg = rd32(hw, I40E_PF_MDET_RX);
  8532. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  8533. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  8534. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  8535. pf_mdd_detected = true;
  8536. }
  8537. /* Queue belongs to the PF, initiate a reset */
  8538. if (pf_mdd_detected) {
  8539. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  8540. i40e_service_event_schedule(pf);
  8541. }
  8542. }
  8543. /* see if one of the VFs needs its hand slapped */
  8544. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  8545. vf = &(pf->vf[i]);
  8546. reg = rd32(hw, I40E_VP_MDET_TX(i));
  8547. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  8548. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  8549. vf->num_mdd_events++;
  8550. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  8551. i);
  8552. }
  8553. reg = rd32(hw, I40E_VP_MDET_RX(i));
  8554. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  8555. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  8556. vf->num_mdd_events++;
  8557. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  8558. i);
  8559. }
  8560. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  8561. dev_info(&pf->pdev->dev,
  8562. "Too many MDD events on VF %d, disabled\n", i);
  8563. dev_info(&pf->pdev->dev,
  8564. "Use PF Control I/F to re-enable the VF\n");
  8565. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  8566. }
  8567. }
  8568. /* re-enable mdd interrupt cause */
  8569. clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  8570. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  8571. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  8572. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  8573. i40e_flush(hw);
  8574. }
  8575. static const char *i40e_tunnel_name(u8 type)
  8576. {
  8577. switch (type) {
  8578. case UDP_TUNNEL_TYPE_VXLAN:
  8579. return "vxlan";
  8580. case UDP_TUNNEL_TYPE_GENEVE:
  8581. return "geneve";
  8582. default:
  8583. return "unknown";
  8584. }
  8585. }
  8586. /**
  8587. * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
  8588. * @pf: board private structure
  8589. **/
  8590. static void i40e_sync_udp_filters(struct i40e_pf *pf)
  8591. {
  8592. int i;
  8593. /* loop through and set pending bit for all active UDP filters */
  8594. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8595. if (pf->udp_ports[i].port)
  8596. pf->pending_udp_bitmap |= BIT_ULL(i);
  8597. }
  8598. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  8599. }
  8600. /**
  8601. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  8602. * @pf: board private structure
  8603. **/
  8604. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  8605. {
  8606. struct i40e_hw *hw = &pf->hw;
  8607. u8 filter_index, type;
  8608. u16 port;
  8609. int i;
  8610. if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
  8611. return;
  8612. /* acquire RTNL to maintain state of flags and port requests */
  8613. rtnl_lock();
  8614. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8615. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  8616. struct i40e_udp_port_config *udp_port;
  8617. i40e_status ret = 0;
  8618. udp_port = &pf->udp_ports[i];
  8619. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8620. port = READ_ONCE(udp_port->port);
  8621. type = READ_ONCE(udp_port->type);
  8622. filter_index = READ_ONCE(udp_port->filter_index);
  8623. /* release RTNL while we wait on AQ command */
  8624. rtnl_unlock();
  8625. if (port)
  8626. ret = i40e_aq_add_udp_tunnel(hw, port,
  8627. type,
  8628. &filter_index,
  8629. NULL);
  8630. else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
  8631. ret = i40e_aq_del_udp_tunnel(hw, filter_index,
  8632. NULL);
  8633. /* reacquire RTNL so we can update filter_index */
  8634. rtnl_lock();
  8635. if (ret) {
  8636. dev_info(&pf->pdev->dev,
  8637. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  8638. i40e_tunnel_name(type),
  8639. port ? "add" : "delete",
  8640. port,
  8641. filter_index,
  8642. i40e_stat_str(&pf->hw, ret),
  8643. i40e_aq_str(&pf->hw,
  8644. pf->hw.aq.asq_last_status));
  8645. if (port) {
  8646. /* failed to add, just reset port,
  8647. * drop pending bit for any deletion
  8648. */
  8649. udp_port->port = 0;
  8650. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8651. }
  8652. } else if (port) {
  8653. /* record filter index on success */
  8654. udp_port->filter_index = filter_index;
  8655. }
  8656. }
  8657. }
  8658. rtnl_unlock();
  8659. }
  8660. /**
  8661. * i40e_service_task - Run the driver's async subtasks
  8662. * @work: pointer to work_struct containing our data
  8663. **/
  8664. static void i40e_service_task(struct work_struct *work)
  8665. {
  8666. struct i40e_pf *pf = container_of(work,
  8667. struct i40e_pf,
  8668. service_task);
  8669. unsigned long start_time = jiffies;
  8670. /* don't bother with service tasks if a reset is in progress */
  8671. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8672. return;
  8673. if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
  8674. return;
  8675. i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
  8676. i40e_sync_filters_subtask(pf);
  8677. i40e_reset_subtask(pf);
  8678. i40e_handle_mdd_event(pf);
  8679. i40e_vc_process_vflr_event(pf);
  8680. i40e_watchdog_subtask(pf);
  8681. i40e_fdir_reinit_subtask(pf);
  8682. if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
  8683. /* Client subtask will reopen next time through. */
  8684. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  8685. } else {
  8686. i40e_client_subtask(pf);
  8687. if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
  8688. pf->state))
  8689. i40e_notify_client_of_l2_param_changes(
  8690. pf->vsi[pf->lan_vsi]);
  8691. }
  8692. i40e_sync_filters_subtask(pf);
  8693. i40e_sync_udp_filters_subtask(pf);
  8694. i40e_clean_adminq_subtask(pf);
  8695. /* flush memory to make sure state is correct before next watchdog */
  8696. smp_mb__before_atomic();
  8697. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  8698. /* If the tasks have taken longer than one timer cycle or there
  8699. * is more work to be done, reschedule the service task now
  8700. * rather than wait for the timer to tick again.
  8701. */
  8702. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  8703. test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
  8704. test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
  8705. test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  8706. i40e_service_event_schedule(pf);
  8707. }
  8708. /**
  8709. * i40e_service_timer - timer callback
  8710. * @data: pointer to PF struct
  8711. **/
  8712. static void i40e_service_timer(struct timer_list *t)
  8713. {
  8714. struct i40e_pf *pf = from_timer(pf, t, service_timer);
  8715. mod_timer(&pf->service_timer,
  8716. round_jiffies(jiffies + pf->service_timer_period));
  8717. i40e_service_event_schedule(pf);
  8718. }
  8719. /**
  8720. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  8721. * @vsi: the VSI being configured
  8722. **/
  8723. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  8724. {
  8725. struct i40e_pf *pf = vsi->back;
  8726. switch (vsi->type) {
  8727. case I40E_VSI_MAIN:
  8728. vsi->alloc_queue_pairs = pf->num_lan_qps;
  8729. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8730. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8731. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8732. vsi->num_q_vectors = pf->num_lan_msix;
  8733. else
  8734. vsi->num_q_vectors = 1;
  8735. break;
  8736. case I40E_VSI_FDIR:
  8737. vsi->alloc_queue_pairs = 1;
  8738. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  8739. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8740. vsi->num_q_vectors = pf->num_fdsb_msix;
  8741. break;
  8742. case I40E_VSI_VMDQ2:
  8743. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  8744. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8745. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8746. vsi->num_q_vectors = pf->num_vmdq_msix;
  8747. break;
  8748. case I40E_VSI_SRIOV:
  8749. vsi->alloc_queue_pairs = pf->num_vf_qps;
  8750. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8751. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8752. break;
  8753. default:
  8754. WARN_ON(1);
  8755. return -ENODATA;
  8756. }
  8757. return 0;
  8758. }
  8759. /**
  8760. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  8761. * @vsi: VSI pointer
  8762. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  8763. *
  8764. * On error: returns error code (negative)
  8765. * On success: returns 0
  8766. **/
  8767. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  8768. {
  8769. struct i40e_ring **next_rings;
  8770. int size;
  8771. int ret = 0;
  8772. /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
  8773. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
  8774. (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
  8775. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  8776. if (!vsi->tx_rings)
  8777. return -ENOMEM;
  8778. next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
  8779. if (i40e_enabled_xdp_vsi(vsi)) {
  8780. vsi->xdp_rings = next_rings;
  8781. next_rings += vsi->alloc_queue_pairs;
  8782. }
  8783. vsi->rx_rings = next_rings;
  8784. if (alloc_qvectors) {
  8785. /* allocate memory for q_vector pointers */
  8786. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  8787. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  8788. if (!vsi->q_vectors) {
  8789. ret = -ENOMEM;
  8790. goto err_vectors;
  8791. }
  8792. }
  8793. return ret;
  8794. err_vectors:
  8795. kfree(vsi->tx_rings);
  8796. return ret;
  8797. }
  8798. /**
  8799. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  8800. * @pf: board private structure
  8801. * @type: type of VSI
  8802. *
  8803. * On error: returns error code (negative)
  8804. * On success: returns vsi index in PF (positive)
  8805. **/
  8806. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  8807. {
  8808. int ret = -ENODEV;
  8809. struct i40e_vsi *vsi;
  8810. int vsi_idx;
  8811. int i;
  8812. /* Need to protect the allocation of the VSIs at the PF level */
  8813. mutex_lock(&pf->switch_mutex);
  8814. /* VSI list may be fragmented if VSI creation/destruction has
  8815. * been happening. We can afford to do a quick scan to look
  8816. * for any free VSIs in the list.
  8817. *
  8818. * find next empty vsi slot, looping back around if necessary
  8819. */
  8820. i = pf->next_vsi;
  8821. while (i < pf->num_alloc_vsi && pf->vsi[i])
  8822. i++;
  8823. if (i >= pf->num_alloc_vsi) {
  8824. i = 0;
  8825. while (i < pf->next_vsi && pf->vsi[i])
  8826. i++;
  8827. }
  8828. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  8829. vsi_idx = i; /* Found one! */
  8830. } else {
  8831. ret = -ENODEV;
  8832. goto unlock_pf; /* out of VSI slots! */
  8833. }
  8834. pf->next_vsi = ++i;
  8835. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  8836. if (!vsi) {
  8837. ret = -ENOMEM;
  8838. goto unlock_pf;
  8839. }
  8840. vsi->type = type;
  8841. vsi->back = pf;
  8842. set_bit(__I40E_VSI_DOWN, vsi->state);
  8843. vsi->flags = 0;
  8844. vsi->idx = vsi_idx;
  8845. vsi->int_rate_limit = 0;
  8846. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  8847. pf->rss_table_size : 64;
  8848. vsi->netdev_registered = false;
  8849. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  8850. hash_init(vsi->mac_filter_hash);
  8851. vsi->irqs_ready = false;
  8852. ret = i40e_set_num_rings_in_vsi(vsi);
  8853. if (ret)
  8854. goto err_rings;
  8855. ret = i40e_vsi_alloc_arrays(vsi, true);
  8856. if (ret)
  8857. goto err_rings;
  8858. /* Setup default MSIX irq handler for VSI */
  8859. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  8860. /* Initialize VSI lock */
  8861. spin_lock_init(&vsi->mac_filter_hash_lock);
  8862. pf->vsi[vsi_idx] = vsi;
  8863. ret = vsi_idx;
  8864. goto unlock_pf;
  8865. err_rings:
  8866. pf->next_vsi = i - 1;
  8867. kfree(vsi);
  8868. unlock_pf:
  8869. mutex_unlock(&pf->switch_mutex);
  8870. return ret;
  8871. }
  8872. /**
  8873. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  8874. * @vsi: VSI pointer
  8875. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  8876. *
  8877. * On error: returns error code (negative)
  8878. * On success: returns 0
  8879. **/
  8880. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  8881. {
  8882. /* free the ring and vector containers */
  8883. if (free_qvectors) {
  8884. kfree(vsi->q_vectors);
  8885. vsi->q_vectors = NULL;
  8886. }
  8887. kfree(vsi->tx_rings);
  8888. vsi->tx_rings = NULL;
  8889. vsi->rx_rings = NULL;
  8890. vsi->xdp_rings = NULL;
  8891. }
  8892. /**
  8893. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  8894. * and lookup table
  8895. * @vsi: Pointer to VSI structure
  8896. */
  8897. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  8898. {
  8899. if (!vsi)
  8900. return;
  8901. kfree(vsi->rss_hkey_user);
  8902. vsi->rss_hkey_user = NULL;
  8903. kfree(vsi->rss_lut_user);
  8904. vsi->rss_lut_user = NULL;
  8905. }
  8906. /**
  8907. * i40e_vsi_clear - Deallocate the VSI provided
  8908. * @vsi: the VSI being un-configured
  8909. **/
  8910. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  8911. {
  8912. struct i40e_pf *pf;
  8913. if (!vsi)
  8914. return 0;
  8915. if (!vsi->back)
  8916. goto free_vsi;
  8917. pf = vsi->back;
  8918. mutex_lock(&pf->switch_mutex);
  8919. if (!pf->vsi[vsi->idx]) {
  8920. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
  8921. vsi->idx, vsi->idx, vsi->type);
  8922. goto unlock_vsi;
  8923. }
  8924. if (pf->vsi[vsi->idx] != vsi) {
  8925. dev_err(&pf->pdev->dev,
  8926. "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
  8927. pf->vsi[vsi->idx]->idx,
  8928. pf->vsi[vsi->idx]->type,
  8929. vsi->idx, vsi->type);
  8930. goto unlock_vsi;
  8931. }
  8932. /* updates the PF for this cleared vsi */
  8933. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8934. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  8935. i40e_vsi_free_arrays(vsi, true);
  8936. i40e_clear_rss_config_user(vsi);
  8937. pf->vsi[vsi->idx] = NULL;
  8938. if (vsi->idx < pf->next_vsi)
  8939. pf->next_vsi = vsi->idx;
  8940. unlock_vsi:
  8941. mutex_unlock(&pf->switch_mutex);
  8942. free_vsi:
  8943. kfree(vsi);
  8944. return 0;
  8945. }
  8946. /**
  8947. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  8948. * @vsi: the VSI being cleaned
  8949. **/
  8950. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  8951. {
  8952. int i;
  8953. if (vsi->tx_rings && vsi->tx_rings[0]) {
  8954. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8955. kfree_rcu(vsi->tx_rings[i], rcu);
  8956. vsi->tx_rings[i] = NULL;
  8957. vsi->rx_rings[i] = NULL;
  8958. if (vsi->xdp_rings)
  8959. vsi->xdp_rings[i] = NULL;
  8960. }
  8961. }
  8962. }
  8963. /**
  8964. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  8965. * @vsi: the VSI being configured
  8966. **/
  8967. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  8968. {
  8969. int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
  8970. struct i40e_pf *pf = vsi->back;
  8971. struct i40e_ring *ring;
  8972. /* Set basic values in the rings to be used later during open() */
  8973. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8974. /* allocate space for both Tx and Rx in one shot */
  8975. ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
  8976. if (!ring)
  8977. goto err_out;
  8978. ring->queue_index = i;
  8979. ring->reg_idx = vsi->base_queue + i;
  8980. ring->ring_active = false;
  8981. ring->vsi = vsi;
  8982. ring->netdev = vsi->netdev;
  8983. ring->dev = &pf->pdev->dev;
  8984. ring->count = vsi->num_desc;
  8985. ring->size = 0;
  8986. ring->dcb_tc = 0;
  8987. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8988. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8989. ring->itr_setting = pf->tx_itr_default;
  8990. vsi->tx_rings[i] = ring++;
  8991. if (!i40e_enabled_xdp_vsi(vsi))
  8992. goto setup_rx;
  8993. ring->queue_index = vsi->alloc_queue_pairs + i;
  8994. ring->reg_idx = vsi->base_queue + ring->queue_index;
  8995. ring->ring_active = false;
  8996. ring->vsi = vsi;
  8997. ring->netdev = NULL;
  8998. ring->dev = &pf->pdev->dev;
  8999. ring->count = vsi->num_desc;
  9000. ring->size = 0;
  9001. ring->dcb_tc = 0;
  9002. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  9003. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  9004. set_ring_xdp(ring);
  9005. ring->itr_setting = pf->tx_itr_default;
  9006. vsi->xdp_rings[i] = ring++;
  9007. setup_rx:
  9008. ring->queue_index = i;
  9009. ring->reg_idx = vsi->base_queue + i;
  9010. ring->ring_active = false;
  9011. ring->vsi = vsi;
  9012. ring->netdev = vsi->netdev;
  9013. ring->dev = &pf->pdev->dev;
  9014. ring->count = vsi->num_desc;
  9015. ring->size = 0;
  9016. ring->dcb_tc = 0;
  9017. ring->itr_setting = pf->rx_itr_default;
  9018. vsi->rx_rings[i] = ring;
  9019. }
  9020. return 0;
  9021. err_out:
  9022. i40e_vsi_clear_rings(vsi);
  9023. return -ENOMEM;
  9024. }
  9025. /**
  9026. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  9027. * @pf: board private structure
  9028. * @vectors: the number of MSI-X vectors to request
  9029. *
  9030. * Returns the number of vectors reserved, or error
  9031. **/
  9032. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  9033. {
  9034. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  9035. I40E_MIN_MSIX, vectors);
  9036. if (vectors < 0) {
  9037. dev_info(&pf->pdev->dev,
  9038. "MSI-X vector reservation failed: %d\n", vectors);
  9039. vectors = 0;
  9040. }
  9041. return vectors;
  9042. }
  9043. /**
  9044. * i40e_init_msix - Setup the MSIX capability
  9045. * @pf: board private structure
  9046. *
  9047. * Work with the OS to set up the MSIX vectors needed.
  9048. *
  9049. * Returns the number of vectors reserved or negative on failure
  9050. **/
  9051. static int i40e_init_msix(struct i40e_pf *pf)
  9052. {
  9053. struct i40e_hw *hw = &pf->hw;
  9054. int cpus, extra_vectors;
  9055. int vectors_left;
  9056. int v_budget, i;
  9057. int v_actual;
  9058. int iwarp_requested = 0;
  9059. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  9060. return -ENODEV;
  9061. /* The number of vectors we'll request will be comprised of:
  9062. * - Add 1 for "other" cause for Admin Queue events, etc.
  9063. * - The number of LAN queue pairs
  9064. * - Queues being used for RSS.
  9065. * We don't need as many as max_rss_size vectors.
  9066. * use rss_size instead in the calculation since that
  9067. * is governed by number of cpus in the system.
  9068. * - assumes symmetric Tx/Rx pairing
  9069. * - The number of VMDq pairs
  9070. * - The CPU count within the NUMA node if iWARP is enabled
  9071. * Once we count this up, try the request.
  9072. *
  9073. * If we can't get what we want, we'll simplify to nearly nothing
  9074. * and try again. If that still fails, we punt.
  9075. */
  9076. vectors_left = hw->func_caps.num_msix_vectors;
  9077. v_budget = 0;
  9078. /* reserve one vector for miscellaneous handler */
  9079. if (vectors_left) {
  9080. v_budget++;
  9081. vectors_left--;
  9082. }
  9083. /* reserve some vectors for the main PF traffic queues. Initially we
  9084. * only reserve at most 50% of the available vectors, in the case that
  9085. * the number of online CPUs is large. This ensures that we can enable
  9086. * extra features as well. Once we've enabled the other features, we
  9087. * will use any remaining vectors to reach as close as we can to the
  9088. * number of online CPUs.
  9089. */
  9090. cpus = num_online_cpus();
  9091. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  9092. vectors_left -= pf->num_lan_msix;
  9093. /* reserve one vector for sideband flow director */
  9094. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9095. if (vectors_left) {
  9096. pf->num_fdsb_msix = 1;
  9097. v_budget++;
  9098. vectors_left--;
  9099. } else {
  9100. pf->num_fdsb_msix = 0;
  9101. }
  9102. }
  9103. /* can we reserve enough for iWARP? */
  9104. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9105. iwarp_requested = pf->num_iwarp_msix;
  9106. if (!vectors_left)
  9107. pf->num_iwarp_msix = 0;
  9108. else if (vectors_left < pf->num_iwarp_msix)
  9109. pf->num_iwarp_msix = 1;
  9110. v_budget += pf->num_iwarp_msix;
  9111. vectors_left -= pf->num_iwarp_msix;
  9112. }
  9113. /* any vectors left over go for VMDq support */
  9114. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  9115. if (!vectors_left) {
  9116. pf->num_vmdq_msix = 0;
  9117. pf->num_vmdq_qps = 0;
  9118. } else {
  9119. int vmdq_vecs_wanted =
  9120. pf->num_vmdq_vsis * pf->num_vmdq_qps;
  9121. int vmdq_vecs =
  9122. min_t(int, vectors_left, vmdq_vecs_wanted);
  9123. /* if we're short on vectors for what's desired, we limit
  9124. * the queues per vmdq. If this is still more than are
  9125. * available, the user will need to change the number of
  9126. * queues/vectors used by the PF later with the ethtool
  9127. * channels command
  9128. */
  9129. if (vectors_left < vmdq_vecs_wanted) {
  9130. pf->num_vmdq_qps = 1;
  9131. vmdq_vecs_wanted = pf->num_vmdq_vsis;
  9132. vmdq_vecs = min_t(int,
  9133. vectors_left,
  9134. vmdq_vecs_wanted);
  9135. }
  9136. pf->num_vmdq_msix = pf->num_vmdq_qps;
  9137. v_budget += vmdq_vecs;
  9138. vectors_left -= vmdq_vecs;
  9139. }
  9140. }
  9141. /* On systems with a large number of SMP cores, we previously limited
  9142. * the number of vectors for num_lan_msix to be at most 50% of the
  9143. * available vectors, to allow for other features. Now, we add back
  9144. * the remaining vectors. However, we ensure that the total
  9145. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  9146. * calculate the number of vectors we can add without going over the
  9147. * cap of CPUs. For systems with a small number of CPUs this will be
  9148. * zero.
  9149. */
  9150. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  9151. pf->num_lan_msix += extra_vectors;
  9152. vectors_left -= extra_vectors;
  9153. WARN(vectors_left < 0,
  9154. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  9155. v_budget += pf->num_lan_msix;
  9156. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  9157. GFP_KERNEL);
  9158. if (!pf->msix_entries)
  9159. return -ENOMEM;
  9160. for (i = 0; i < v_budget; i++)
  9161. pf->msix_entries[i].entry = i;
  9162. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  9163. if (v_actual < I40E_MIN_MSIX) {
  9164. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  9165. kfree(pf->msix_entries);
  9166. pf->msix_entries = NULL;
  9167. pci_disable_msix(pf->pdev);
  9168. return -ENODEV;
  9169. } else if (v_actual == I40E_MIN_MSIX) {
  9170. /* Adjust for minimal MSIX use */
  9171. pf->num_vmdq_vsis = 0;
  9172. pf->num_vmdq_qps = 0;
  9173. pf->num_lan_qps = 1;
  9174. pf->num_lan_msix = 1;
  9175. } else if (v_actual != v_budget) {
  9176. /* If we have limited resources, we will start with no vectors
  9177. * for the special features and then allocate vectors to some
  9178. * of these features based on the policy and at the end disable
  9179. * the features that did not get any vectors.
  9180. */
  9181. int vec;
  9182. dev_info(&pf->pdev->dev,
  9183. "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
  9184. v_actual, v_budget);
  9185. /* reserve the misc vector */
  9186. vec = v_actual - 1;
  9187. /* Scale vector usage down */
  9188. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  9189. pf->num_vmdq_vsis = 1;
  9190. pf->num_vmdq_qps = 1;
  9191. /* partition out the remaining vectors */
  9192. switch (vec) {
  9193. case 2:
  9194. pf->num_lan_msix = 1;
  9195. break;
  9196. case 3:
  9197. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9198. pf->num_lan_msix = 1;
  9199. pf->num_iwarp_msix = 1;
  9200. } else {
  9201. pf->num_lan_msix = 2;
  9202. }
  9203. break;
  9204. default:
  9205. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9206. pf->num_iwarp_msix = min_t(int, (vec / 3),
  9207. iwarp_requested);
  9208. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  9209. I40E_DEFAULT_NUM_VMDQ_VSI);
  9210. } else {
  9211. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  9212. I40E_DEFAULT_NUM_VMDQ_VSI);
  9213. }
  9214. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9215. pf->num_fdsb_msix = 1;
  9216. vec--;
  9217. }
  9218. pf->num_lan_msix = min_t(int,
  9219. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  9220. pf->num_lan_msix);
  9221. pf->num_lan_qps = pf->num_lan_msix;
  9222. break;
  9223. }
  9224. }
  9225. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  9226. (pf->num_fdsb_msix == 0)) {
  9227. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  9228. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9229. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9230. }
  9231. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9232. (pf->num_vmdq_msix == 0)) {
  9233. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  9234. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  9235. }
  9236. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  9237. (pf->num_iwarp_msix == 0)) {
  9238. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  9239. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9240. }
  9241. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  9242. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  9243. pf->num_lan_msix,
  9244. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  9245. pf->num_fdsb_msix,
  9246. pf->num_iwarp_msix);
  9247. return v_actual;
  9248. }
  9249. /**
  9250. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  9251. * @vsi: the VSI being configured
  9252. * @v_idx: index of the vector in the vsi struct
  9253. * @cpu: cpu to be used on affinity_mask
  9254. *
  9255. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  9256. **/
  9257. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  9258. {
  9259. struct i40e_q_vector *q_vector;
  9260. /* allocate q_vector */
  9261. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  9262. if (!q_vector)
  9263. return -ENOMEM;
  9264. q_vector->vsi = vsi;
  9265. q_vector->v_idx = v_idx;
  9266. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  9267. if (vsi->netdev)
  9268. netif_napi_add(vsi->netdev, &q_vector->napi,
  9269. i40e_napi_poll, NAPI_POLL_WEIGHT);
  9270. /* tie q_vector and vsi together */
  9271. vsi->q_vectors[v_idx] = q_vector;
  9272. return 0;
  9273. }
  9274. /**
  9275. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  9276. * @vsi: the VSI being configured
  9277. *
  9278. * We allocate one q_vector per queue interrupt. If allocation fails we
  9279. * return -ENOMEM.
  9280. **/
  9281. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  9282. {
  9283. struct i40e_pf *pf = vsi->back;
  9284. int err, v_idx, num_q_vectors, current_cpu;
  9285. /* if not MSIX, give the one vector only to the LAN VSI */
  9286. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  9287. num_q_vectors = vsi->num_q_vectors;
  9288. else if (vsi == pf->vsi[pf->lan_vsi])
  9289. num_q_vectors = 1;
  9290. else
  9291. return -EINVAL;
  9292. current_cpu = cpumask_first(cpu_online_mask);
  9293. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  9294. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  9295. if (err)
  9296. goto err_out;
  9297. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  9298. if (unlikely(current_cpu >= nr_cpu_ids))
  9299. current_cpu = cpumask_first(cpu_online_mask);
  9300. }
  9301. return 0;
  9302. err_out:
  9303. while (v_idx--)
  9304. i40e_free_q_vector(vsi, v_idx);
  9305. return err;
  9306. }
  9307. /**
  9308. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  9309. * @pf: board private structure to initialize
  9310. **/
  9311. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  9312. {
  9313. int vectors = 0;
  9314. ssize_t size;
  9315. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9316. vectors = i40e_init_msix(pf);
  9317. if (vectors < 0) {
  9318. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  9319. I40E_FLAG_IWARP_ENABLED |
  9320. I40E_FLAG_RSS_ENABLED |
  9321. I40E_FLAG_DCB_CAPABLE |
  9322. I40E_FLAG_DCB_ENABLED |
  9323. I40E_FLAG_SRIOV_ENABLED |
  9324. I40E_FLAG_FD_SB_ENABLED |
  9325. I40E_FLAG_FD_ATR_ENABLED |
  9326. I40E_FLAG_VMDQ_ENABLED);
  9327. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9328. /* rework the queue expectations without MSIX */
  9329. i40e_determine_queue_usage(pf);
  9330. }
  9331. }
  9332. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9333. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  9334. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  9335. vectors = pci_enable_msi(pf->pdev);
  9336. if (vectors < 0) {
  9337. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  9338. vectors);
  9339. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  9340. }
  9341. vectors = 1; /* one MSI or Legacy vector */
  9342. }
  9343. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  9344. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  9345. /* set up vector assignment tracking */
  9346. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  9347. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  9348. if (!pf->irq_pile)
  9349. return -ENOMEM;
  9350. pf->irq_pile->num_entries = vectors;
  9351. pf->irq_pile->search_hint = 0;
  9352. /* track first vector for misc interrupts, ignore return */
  9353. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  9354. return 0;
  9355. }
  9356. /**
  9357. * i40e_restore_interrupt_scheme - Restore the interrupt scheme
  9358. * @pf: private board data structure
  9359. *
  9360. * Restore the interrupt scheme that was cleared when we suspended the
  9361. * device. This should be called during resume to re-allocate the q_vectors
  9362. * and reacquire IRQs.
  9363. */
  9364. static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
  9365. {
  9366. int err, i;
  9367. /* We cleared the MSI and MSI-X flags when disabling the old interrupt
  9368. * scheme. We need to re-enabled them here in order to attempt to
  9369. * re-acquire the MSI or MSI-X vectors
  9370. */
  9371. pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  9372. err = i40e_init_interrupt_scheme(pf);
  9373. if (err)
  9374. return err;
  9375. /* Now that we've re-acquired IRQs, we need to remap the vectors and
  9376. * rings together again.
  9377. */
  9378. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9379. if (pf->vsi[i]) {
  9380. err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
  9381. if (err)
  9382. goto err_unwind;
  9383. i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
  9384. }
  9385. }
  9386. err = i40e_setup_misc_vector(pf);
  9387. if (err)
  9388. goto err_unwind;
  9389. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  9390. i40e_client_update_msix_info(pf);
  9391. return 0;
  9392. err_unwind:
  9393. while (i--) {
  9394. if (pf->vsi[i])
  9395. i40e_vsi_free_q_vectors(pf->vsi[i]);
  9396. }
  9397. return err;
  9398. }
  9399. /**
  9400. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  9401. * @pf: board private structure
  9402. *
  9403. * This sets up the handler for MSIX 0, which is used to manage the
  9404. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  9405. * when in MSI or Legacy interrupt mode.
  9406. **/
  9407. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  9408. {
  9409. struct i40e_hw *hw = &pf->hw;
  9410. int err = 0;
  9411. /* Only request the IRQ once, the first time through. */
  9412. if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
  9413. err = request_irq(pf->msix_entries[0].vector,
  9414. i40e_intr, 0, pf->int_name, pf);
  9415. if (err) {
  9416. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  9417. dev_info(&pf->pdev->dev,
  9418. "request_irq for %s failed: %d\n",
  9419. pf->int_name, err);
  9420. return -EFAULT;
  9421. }
  9422. }
  9423. i40e_enable_misc_int_causes(pf);
  9424. /* associate no queues to the misc vector */
  9425. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  9426. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  9427. i40e_flush(hw);
  9428. i40e_irq_dynamic_enable_icr0(pf);
  9429. return err;
  9430. }
  9431. /**
  9432. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  9433. * @vsi: Pointer to vsi structure
  9434. * @seed: Buffter to store the hash keys
  9435. * @lut: Buffer to store the lookup table entries
  9436. * @lut_size: Size of buffer to store the lookup table entries
  9437. *
  9438. * Return 0 on success, negative on failure
  9439. */
  9440. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  9441. u8 *lut, u16 lut_size)
  9442. {
  9443. struct i40e_pf *pf = vsi->back;
  9444. struct i40e_hw *hw = &pf->hw;
  9445. int ret = 0;
  9446. if (seed) {
  9447. ret = i40e_aq_get_rss_key(hw, vsi->id,
  9448. (struct i40e_aqc_get_set_rss_key_data *)seed);
  9449. if (ret) {
  9450. dev_info(&pf->pdev->dev,
  9451. "Cannot get RSS key, err %s aq_err %s\n",
  9452. i40e_stat_str(&pf->hw, ret),
  9453. i40e_aq_str(&pf->hw,
  9454. pf->hw.aq.asq_last_status));
  9455. return ret;
  9456. }
  9457. }
  9458. if (lut) {
  9459. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  9460. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  9461. if (ret) {
  9462. dev_info(&pf->pdev->dev,
  9463. "Cannot get RSS lut, err %s aq_err %s\n",
  9464. i40e_stat_str(&pf->hw, ret),
  9465. i40e_aq_str(&pf->hw,
  9466. pf->hw.aq.asq_last_status));
  9467. return ret;
  9468. }
  9469. }
  9470. return ret;
  9471. }
  9472. /**
  9473. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  9474. * @vsi: Pointer to vsi structure
  9475. * @seed: RSS hash seed
  9476. * @lut: Lookup table
  9477. * @lut_size: Lookup table size
  9478. *
  9479. * Returns 0 on success, negative on failure
  9480. **/
  9481. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  9482. const u8 *lut, u16 lut_size)
  9483. {
  9484. struct i40e_pf *pf = vsi->back;
  9485. struct i40e_hw *hw = &pf->hw;
  9486. u16 vf_id = vsi->vf_id;
  9487. u8 i;
  9488. /* Fill out hash function seed */
  9489. if (seed) {
  9490. u32 *seed_dw = (u32 *)seed;
  9491. if (vsi->type == I40E_VSI_MAIN) {
  9492. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9493. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  9494. } else if (vsi->type == I40E_VSI_SRIOV) {
  9495. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  9496. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  9497. } else {
  9498. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  9499. }
  9500. }
  9501. if (lut) {
  9502. u32 *lut_dw = (u32 *)lut;
  9503. if (vsi->type == I40E_VSI_MAIN) {
  9504. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9505. return -EINVAL;
  9506. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9507. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  9508. } else if (vsi->type == I40E_VSI_SRIOV) {
  9509. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  9510. return -EINVAL;
  9511. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9512. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  9513. } else {
  9514. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  9515. }
  9516. }
  9517. i40e_flush(hw);
  9518. return 0;
  9519. }
  9520. /**
  9521. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  9522. * @vsi: Pointer to VSI structure
  9523. * @seed: Buffer to store the keys
  9524. * @lut: Buffer to store the lookup table entries
  9525. * @lut_size: Size of buffer to store the lookup table entries
  9526. *
  9527. * Returns 0 on success, negative on failure
  9528. */
  9529. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  9530. u8 *lut, u16 lut_size)
  9531. {
  9532. struct i40e_pf *pf = vsi->back;
  9533. struct i40e_hw *hw = &pf->hw;
  9534. u16 i;
  9535. if (seed) {
  9536. u32 *seed_dw = (u32 *)seed;
  9537. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9538. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  9539. }
  9540. if (lut) {
  9541. u32 *lut_dw = (u32 *)lut;
  9542. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9543. return -EINVAL;
  9544. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9545. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  9546. }
  9547. return 0;
  9548. }
  9549. /**
  9550. * i40e_config_rss - Configure RSS keys and lut
  9551. * @vsi: Pointer to VSI structure
  9552. * @seed: RSS hash seed
  9553. * @lut: Lookup table
  9554. * @lut_size: Lookup table size
  9555. *
  9556. * Returns 0 on success, negative on failure
  9557. */
  9558. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9559. {
  9560. struct i40e_pf *pf = vsi->back;
  9561. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9562. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  9563. else
  9564. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  9565. }
  9566. /**
  9567. * i40e_get_rss - Get RSS keys and lut
  9568. * @vsi: Pointer to VSI structure
  9569. * @seed: Buffer to store the keys
  9570. * @lut: Buffer to store the lookup table entries
  9571. * @lut_size: Size of buffer to store the lookup table entries
  9572. *
  9573. * Returns 0 on success, negative on failure
  9574. */
  9575. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9576. {
  9577. struct i40e_pf *pf = vsi->back;
  9578. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9579. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  9580. else
  9581. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  9582. }
  9583. /**
  9584. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  9585. * @pf: Pointer to board private structure
  9586. * @lut: Lookup table
  9587. * @rss_table_size: Lookup table size
  9588. * @rss_size: Range of queue number for hashing
  9589. */
  9590. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  9591. u16 rss_table_size, u16 rss_size)
  9592. {
  9593. u16 i;
  9594. for (i = 0; i < rss_table_size; i++)
  9595. lut[i] = i % rss_size;
  9596. }
  9597. /**
  9598. * i40e_pf_config_rss - Prepare for RSS if used
  9599. * @pf: board private structure
  9600. **/
  9601. static int i40e_pf_config_rss(struct i40e_pf *pf)
  9602. {
  9603. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9604. u8 seed[I40E_HKEY_ARRAY_SIZE];
  9605. u8 *lut;
  9606. struct i40e_hw *hw = &pf->hw;
  9607. u32 reg_val;
  9608. u64 hena;
  9609. int ret;
  9610. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  9611. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  9612. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  9613. hena |= i40e_pf_get_default_rss_hena(pf);
  9614. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  9615. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  9616. /* Determine the RSS table size based on the hardware capabilities */
  9617. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  9618. reg_val = (pf->rss_table_size == 512) ?
  9619. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  9620. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  9621. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  9622. /* Determine the RSS size of the VSI */
  9623. if (!vsi->rss_size) {
  9624. u16 qcount;
  9625. /* If the firmware does something weird during VSI init, we
  9626. * could end up with zero TCs. Check for that to avoid
  9627. * divide-by-zero. It probably won't pass traffic, but it also
  9628. * won't panic.
  9629. */
  9630. qcount = vsi->num_queue_pairs /
  9631. (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
  9632. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9633. }
  9634. if (!vsi->rss_size)
  9635. return -EINVAL;
  9636. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  9637. if (!lut)
  9638. return -ENOMEM;
  9639. /* Use user configured lut if there is one, otherwise use default */
  9640. if (vsi->rss_lut_user)
  9641. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  9642. else
  9643. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  9644. /* Use user configured hash key if there is one, otherwise
  9645. * use default.
  9646. */
  9647. if (vsi->rss_hkey_user)
  9648. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  9649. else
  9650. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  9651. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  9652. kfree(lut);
  9653. return ret;
  9654. }
  9655. /**
  9656. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  9657. * @pf: board private structure
  9658. * @queue_count: the requested queue count for rss.
  9659. *
  9660. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  9661. * count which may be different from the requested queue count.
  9662. * Note: expects to be called while under rtnl_lock()
  9663. **/
  9664. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  9665. {
  9666. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9667. int new_rss_size;
  9668. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  9669. return 0;
  9670. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  9671. if (queue_count != vsi->num_queue_pairs) {
  9672. u16 qcount;
  9673. vsi->req_queue_pairs = queue_count;
  9674. i40e_prep_for_reset(pf, true);
  9675. pf->alloc_rss_size = new_rss_size;
  9676. i40e_reset_and_rebuild(pf, true, true);
  9677. /* Discard the user configured hash keys and lut, if less
  9678. * queues are enabled.
  9679. */
  9680. if (queue_count < vsi->rss_size) {
  9681. i40e_clear_rss_config_user(vsi);
  9682. dev_dbg(&pf->pdev->dev,
  9683. "discard user configured hash keys and lut\n");
  9684. }
  9685. /* Reset vsi->rss_size, as number of enabled queues changed */
  9686. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  9687. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9688. i40e_pf_config_rss(pf);
  9689. }
  9690. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  9691. vsi->req_queue_pairs, pf->rss_size_max);
  9692. return pf->alloc_rss_size;
  9693. }
  9694. /**
  9695. * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
  9696. * @pf: board private structure
  9697. **/
  9698. i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
  9699. {
  9700. i40e_status status;
  9701. bool min_valid, max_valid;
  9702. u32 max_bw, min_bw;
  9703. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  9704. &min_valid, &max_valid);
  9705. if (!status) {
  9706. if (min_valid)
  9707. pf->min_bw = min_bw;
  9708. if (max_valid)
  9709. pf->max_bw = max_bw;
  9710. }
  9711. return status;
  9712. }
  9713. /**
  9714. * i40e_set_partition_bw_setting - Set BW settings for this PF partition
  9715. * @pf: board private structure
  9716. **/
  9717. i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
  9718. {
  9719. struct i40e_aqc_configure_partition_bw_data bw_data;
  9720. i40e_status status;
  9721. /* Set the valid bit for this PF */
  9722. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  9723. bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
  9724. bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
  9725. /* Set the new bandwidths */
  9726. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  9727. return status;
  9728. }
  9729. /**
  9730. * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
  9731. * @pf: board private structure
  9732. **/
  9733. i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
  9734. {
  9735. /* Commit temporary BW setting to permanent NVM image */
  9736. enum i40e_admin_queue_err last_aq_status;
  9737. i40e_status ret;
  9738. u16 nvm_word;
  9739. if (pf->hw.partition_id != 1) {
  9740. dev_info(&pf->pdev->dev,
  9741. "Commit BW only works on partition 1! This is partition %d",
  9742. pf->hw.partition_id);
  9743. ret = I40E_NOT_SUPPORTED;
  9744. goto bw_commit_out;
  9745. }
  9746. /* Acquire NVM for read access */
  9747. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  9748. last_aq_status = pf->hw.aq.asq_last_status;
  9749. if (ret) {
  9750. dev_info(&pf->pdev->dev,
  9751. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  9752. i40e_stat_str(&pf->hw, ret),
  9753. i40e_aq_str(&pf->hw, last_aq_status));
  9754. goto bw_commit_out;
  9755. }
  9756. /* Read word 0x10 of NVM - SW compatibility word 1 */
  9757. ret = i40e_aq_read_nvm(&pf->hw,
  9758. I40E_SR_NVM_CONTROL_WORD,
  9759. 0x10, sizeof(nvm_word), &nvm_word,
  9760. false, NULL);
  9761. /* Save off last admin queue command status before releasing
  9762. * the NVM
  9763. */
  9764. last_aq_status = pf->hw.aq.asq_last_status;
  9765. i40e_release_nvm(&pf->hw);
  9766. if (ret) {
  9767. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  9768. i40e_stat_str(&pf->hw, ret),
  9769. i40e_aq_str(&pf->hw, last_aq_status));
  9770. goto bw_commit_out;
  9771. }
  9772. /* Wait a bit for NVM release to complete */
  9773. msleep(50);
  9774. /* Acquire NVM for write access */
  9775. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  9776. last_aq_status = pf->hw.aq.asq_last_status;
  9777. if (ret) {
  9778. dev_info(&pf->pdev->dev,
  9779. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  9780. i40e_stat_str(&pf->hw, ret),
  9781. i40e_aq_str(&pf->hw, last_aq_status));
  9782. goto bw_commit_out;
  9783. }
  9784. /* Write it back out unchanged to initiate update NVM,
  9785. * which will force a write of the shadow (alt) RAM to
  9786. * the NVM - thus storing the bandwidth values permanently.
  9787. */
  9788. ret = i40e_aq_update_nvm(&pf->hw,
  9789. I40E_SR_NVM_CONTROL_WORD,
  9790. 0x10, sizeof(nvm_word),
  9791. &nvm_word, true, 0, NULL);
  9792. /* Save off last admin queue command status before releasing
  9793. * the NVM
  9794. */
  9795. last_aq_status = pf->hw.aq.asq_last_status;
  9796. i40e_release_nvm(&pf->hw);
  9797. if (ret)
  9798. dev_info(&pf->pdev->dev,
  9799. "BW settings NOT SAVED, err %s aq_err %s\n",
  9800. i40e_stat_str(&pf->hw, ret),
  9801. i40e_aq_str(&pf->hw, last_aq_status));
  9802. bw_commit_out:
  9803. return ret;
  9804. }
  9805. /**
  9806. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  9807. * @pf: board private structure to initialize
  9808. *
  9809. * i40e_sw_init initializes the Adapter private data structure.
  9810. * Fields are initialized based on PCI device information and
  9811. * OS network device settings (MTU size).
  9812. **/
  9813. static int i40e_sw_init(struct i40e_pf *pf)
  9814. {
  9815. int err = 0;
  9816. int size;
  9817. /* Set default capability flags */
  9818. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  9819. I40E_FLAG_MSI_ENABLED |
  9820. I40E_FLAG_MSIX_ENABLED;
  9821. /* Set default ITR */
  9822. pf->rx_itr_default = I40E_ITR_RX_DEF;
  9823. pf->tx_itr_default = I40E_ITR_TX_DEF;
  9824. /* Depending on PF configurations, it is possible that the RSS
  9825. * maximum might end up larger than the available queues
  9826. */
  9827. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  9828. pf->alloc_rss_size = 1;
  9829. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  9830. pf->rss_size_max = min_t(int, pf->rss_size_max,
  9831. pf->hw.func_caps.num_tx_qp);
  9832. if (pf->hw.func_caps.rss) {
  9833. pf->flags |= I40E_FLAG_RSS_ENABLED;
  9834. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  9835. num_online_cpus());
  9836. }
  9837. /* MFP mode enabled */
  9838. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  9839. pf->flags |= I40E_FLAG_MFP_ENABLED;
  9840. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  9841. if (i40e_get_partition_bw_setting(pf)) {
  9842. dev_warn(&pf->pdev->dev,
  9843. "Could not get partition bw settings\n");
  9844. } else {
  9845. dev_info(&pf->pdev->dev,
  9846. "Partition BW Min = %8.8x, Max = %8.8x\n",
  9847. pf->min_bw, pf->max_bw);
  9848. /* nudge the Tx scheduler */
  9849. i40e_set_partition_bw_setting(pf);
  9850. }
  9851. }
  9852. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  9853. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  9854. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  9855. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  9856. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  9857. pf->hw.num_partitions > 1)
  9858. dev_info(&pf->pdev->dev,
  9859. "Flow Director Sideband mode Disabled in MFP mode\n");
  9860. else
  9861. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9862. pf->fdir_pf_filter_count =
  9863. pf->hw.func_caps.fd_filters_guaranteed;
  9864. pf->hw.fdir_shared_filter_count =
  9865. pf->hw.func_caps.fd_filters_best_effort;
  9866. }
  9867. if (pf->hw.mac.type == I40E_MAC_X722) {
  9868. pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
  9869. I40E_HW_128_QP_RSS_CAPABLE |
  9870. I40E_HW_ATR_EVICT_CAPABLE |
  9871. I40E_HW_WB_ON_ITR_CAPABLE |
  9872. I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
  9873. I40E_HW_NO_PCI_LINK_CHECK |
  9874. I40E_HW_USE_SET_LLDP_MIB |
  9875. I40E_HW_GENEVE_OFFLOAD_CAPABLE |
  9876. I40E_HW_PTP_L4_CAPABLE |
  9877. I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
  9878. I40E_HW_OUTER_UDP_CSUM_CAPABLE);
  9879. #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
  9880. if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
  9881. I40E_FDEVICT_PCTYPE_DEFAULT) {
  9882. dev_warn(&pf->pdev->dev,
  9883. "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
  9884. pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
  9885. }
  9886. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  9887. ((pf->hw.aq.api_maj_ver == 1) &&
  9888. (pf->hw.aq.api_min_ver > 4))) {
  9889. /* Supported in FW API version higher than 1.4 */
  9890. pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
  9891. }
  9892. /* Enable HW ATR eviction if possible */
  9893. if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
  9894. pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
  9895. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9896. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  9897. (pf->hw.aq.fw_maj_ver < 4))) {
  9898. pf->hw_features |= I40E_HW_RESTART_AUTONEG;
  9899. /* No DCB support for FW < v4.33 */
  9900. pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
  9901. }
  9902. /* Disable FW LLDP if FW < v4.3 */
  9903. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9904. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  9905. (pf->hw.aq.fw_maj_ver < 4)))
  9906. pf->hw_features |= I40E_HW_STOP_FW_LLDP;
  9907. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  9908. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9909. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  9910. (pf->hw.aq.fw_maj_ver >= 5)))
  9911. pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
  9912. /* Enable PTP L4 if FW > v6.0 */
  9913. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9914. pf->hw.aq.fw_maj_ver >= 6)
  9915. pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
  9916. if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
  9917. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  9918. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  9919. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  9920. }
  9921. if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
  9922. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  9923. /* IWARP needs one extra vector for CQP just like MISC.*/
  9924. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  9925. }
  9926. /* Stopping the FW LLDP engine is only supported on the
  9927. * XL710 with a FW ver >= 1.7. Also, stopping FW LLDP
  9928. * engine is not supported if NPAR is functioning on this
  9929. * part
  9930. */
  9931. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9932. !pf->hw.func_caps.npar_enable &&
  9933. (pf->hw.aq.api_maj_ver > 1 ||
  9934. (pf->hw.aq.api_maj_ver == 1 && pf->hw.aq.api_min_ver > 6)))
  9935. pf->hw_features |= I40E_HW_STOPPABLE_FW_LLDP;
  9936. #ifdef CONFIG_PCI_IOV
  9937. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  9938. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  9939. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  9940. pf->num_req_vfs = min_t(int,
  9941. pf->hw.func_caps.num_vfs,
  9942. I40E_MAX_VF_COUNT);
  9943. }
  9944. #endif /* CONFIG_PCI_IOV */
  9945. pf->eeprom_version = 0xDEAD;
  9946. pf->lan_veb = I40E_NO_VEB;
  9947. pf->lan_vsi = I40E_NO_VSI;
  9948. /* By default FW has this off for performance reasons */
  9949. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  9950. /* set up queue assignment tracking */
  9951. size = sizeof(struct i40e_lump_tracking)
  9952. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  9953. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  9954. if (!pf->qp_pile) {
  9955. err = -ENOMEM;
  9956. goto sw_init_done;
  9957. }
  9958. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  9959. pf->qp_pile->search_hint = 0;
  9960. pf->tx_timeout_recovery_level = 1;
  9961. mutex_init(&pf->switch_mutex);
  9962. sw_init_done:
  9963. return err;
  9964. }
  9965. /**
  9966. * i40e_set_ntuple - set the ntuple feature flag and take action
  9967. * @pf: board private structure to initialize
  9968. * @features: the feature set that the stack is suggesting
  9969. *
  9970. * returns a bool to indicate if reset needs to happen
  9971. **/
  9972. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  9973. {
  9974. bool need_reset = false;
  9975. /* Check if Flow Director n-tuple support was enabled or disabled. If
  9976. * the state changed, we need to reset.
  9977. */
  9978. if (features & NETIF_F_NTUPLE) {
  9979. /* Enable filters and mark for reset */
  9980. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  9981. need_reset = true;
  9982. /* enable FD_SB only if there is MSI-X vector and no cloud
  9983. * filters exist
  9984. */
  9985. if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
  9986. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9987. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  9988. }
  9989. } else {
  9990. /* turn off filters, mark for reset and clear SW filter list */
  9991. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9992. need_reset = true;
  9993. i40e_fdir_filter_exit(pf);
  9994. }
  9995. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9996. clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
  9997. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9998. /* reset fd counters */
  9999. pf->fd_add_err = 0;
  10000. pf->fd_atr_cnt = 0;
  10001. /* if ATR was auto disabled it can be re-enabled. */
  10002. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  10003. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  10004. (I40E_DEBUG_FD & pf->hw.debug_mask))
  10005. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  10006. }
  10007. return need_reset;
  10008. }
  10009. /**
  10010. * i40e_clear_rss_lut - clear the rx hash lookup table
  10011. * @vsi: the VSI being configured
  10012. **/
  10013. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  10014. {
  10015. struct i40e_pf *pf = vsi->back;
  10016. struct i40e_hw *hw = &pf->hw;
  10017. u16 vf_id = vsi->vf_id;
  10018. u8 i;
  10019. if (vsi->type == I40E_VSI_MAIN) {
  10020. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  10021. wr32(hw, I40E_PFQF_HLUT(i), 0);
  10022. } else if (vsi->type == I40E_VSI_SRIOV) {
  10023. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  10024. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  10025. } else {
  10026. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  10027. }
  10028. }
  10029. /**
  10030. * i40e_set_features - set the netdev feature flags
  10031. * @netdev: ptr to the netdev being adjusted
  10032. * @features: the feature set that the stack is suggesting
  10033. * Note: expects to be called while under rtnl_lock()
  10034. **/
  10035. static int i40e_set_features(struct net_device *netdev,
  10036. netdev_features_t features)
  10037. {
  10038. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10039. struct i40e_vsi *vsi = np->vsi;
  10040. struct i40e_pf *pf = vsi->back;
  10041. bool need_reset;
  10042. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  10043. i40e_pf_config_rss(pf);
  10044. else if (!(features & NETIF_F_RXHASH) &&
  10045. netdev->features & NETIF_F_RXHASH)
  10046. i40e_clear_rss_lut(vsi);
  10047. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  10048. i40e_vlan_stripping_enable(vsi);
  10049. else
  10050. i40e_vlan_stripping_disable(vsi);
  10051. if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
  10052. dev_err(&pf->pdev->dev,
  10053. "Offloaded tc filters active, can't turn hw_tc_offload off");
  10054. return -EINVAL;
  10055. }
  10056. need_reset = i40e_set_ntuple(pf, features);
  10057. if (need_reset)
  10058. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  10059. return 0;
  10060. }
  10061. /**
  10062. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  10063. * @pf: board private structure
  10064. * @port: The UDP port to look up
  10065. *
  10066. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  10067. **/
  10068. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  10069. {
  10070. u8 i;
  10071. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  10072. /* Do not report ports with pending deletions as
  10073. * being available.
  10074. */
  10075. if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
  10076. continue;
  10077. if (pf->udp_ports[i].port == port)
  10078. return i;
  10079. }
  10080. return i;
  10081. }
  10082. /**
  10083. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  10084. * @netdev: This physical port's netdev
  10085. * @ti: Tunnel endpoint information
  10086. **/
  10087. static void i40e_udp_tunnel_add(struct net_device *netdev,
  10088. struct udp_tunnel_info *ti)
  10089. {
  10090. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10091. struct i40e_vsi *vsi = np->vsi;
  10092. struct i40e_pf *pf = vsi->back;
  10093. u16 port = ntohs(ti->port);
  10094. u8 next_idx;
  10095. u8 idx;
  10096. idx = i40e_get_udp_port_idx(pf, port);
  10097. /* Check if port already exists */
  10098. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10099. netdev_info(netdev, "port %d already offloaded\n", port);
  10100. return;
  10101. }
  10102. /* Now check if there is space to add the new port */
  10103. next_idx = i40e_get_udp_port_idx(pf, 0);
  10104. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10105. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  10106. port);
  10107. return;
  10108. }
  10109. switch (ti->type) {
  10110. case UDP_TUNNEL_TYPE_VXLAN:
  10111. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  10112. break;
  10113. case UDP_TUNNEL_TYPE_GENEVE:
  10114. if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
  10115. return;
  10116. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  10117. break;
  10118. default:
  10119. return;
  10120. }
  10121. /* New port: add it and mark its index in the bitmap */
  10122. pf->udp_ports[next_idx].port = port;
  10123. pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
  10124. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  10125. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10126. }
  10127. /**
  10128. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  10129. * @netdev: This physical port's netdev
  10130. * @ti: Tunnel endpoint information
  10131. **/
  10132. static void i40e_udp_tunnel_del(struct net_device *netdev,
  10133. struct udp_tunnel_info *ti)
  10134. {
  10135. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10136. struct i40e_vsi *vsi = np->vsi;
  10137. struct i40e_pf *pf = vsi->back;
  10138. u16 port = ntohs(ti->port);
  10139. u8 idx;
  10140. idx = i40e_get_udp_port_idx(pf, port);
  10141. /* Check if port already exists */
  10142. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  10143. goto not_found;
  10144. switch (ti->type) {
  10145. case UDP_TUNNEL_TYPE_VXLAN:
  10146. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  10147. goto not_found;
  10148. break;
  10149. case UDP_TUNNEL_TYPE_GENEVE:
  10150. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  10151. goto not_found;
  10152. break;
  10153. default:
  10154. goto not_found;
  10155. }
  10156. /* if port exists, set it to 0 (mark for deletion)
  10157. * and make it pending
  10158. */
  10159. pf->udp_ports[idx].port = 0;
  10160. /* Toggle pending bit instead of setting it. This way if we are
  10161. * deleting a port that has yet to be added we just clear the pending
  10162. * bit and don't have to worry about it.
  10163. */
  10164. pf->pending_udp_bitmap ^= BIT_ULL(idx);
  10165. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10166. return;
  10167. not_found:
  10168. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  10169. port);
  10170. }
  10171. static int i40e_get_phys_port_id(struct net_device *netdev,
  10172. struct netdev_phys_item_id *ppid)
  10173. {
  10174. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10175. struct i40e_pf *pf = np->vsi->back;
  10176. struct i40e_hw *hw = &pf->hw;
  10177. if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
  10178. return -EOPNOTSUPP;
  10179. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  10180. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  10181. return 0;
  10182. }
  10183. /**
  10184. * i40e_ndo_fdb_add - add an entry to the hardware database
  10185. * @ndm: the input from the stack
  10186. * @tb: pointer to array of nladdr (unused)
  10187. * @dev: the net device pointer
  10188. * @addr: the MAC address entry being added
  10189. * @vid: VLAN ID
  10190. * @flags: instructions from stack about fdb operation
  10191. */
  10192. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  10193. struct net_device *dev,
  10194. const unsigned char *addr, u16 vid,
  10195. u16 flags)
  10196. {
  10197. struct i40e_netdev_priv *np = netdev_priv(dev);
  10198. struct i40e_pf *pf = np->vsi->back;
  10199. int err = 0;
  10200. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  10201. return -EOPNOTSUPP;
  10202. if (vid) {
  10203. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  10204. return -EINVAL;
  10205. }
  10206. /* Hardware does not support aging addresses so if a
  10207. * ndm_state is given only allow permanent addresses
  10208. */
  10209. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  10210. netdev_info(dev, "FDB only supports static addresses\n");
  10211. return -EINVAL;
  10212. }
  10213. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  10214. err = dev_uc_add_excl(dev, addr);
  10215. else if (is_multicast_ether_addr(addr))
  10216. err = dev_mc_add_excl(dev, addr);
  10217. else
  10218. err = -EINVAL;
  10219. /* Only return duplicate errors if NLM_F_EXCL is set */
  10220. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  10221. err = 0;
  10222. return err;
  10223. }
  10224. /**
  10225. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  10226. * @dev: the netdev being configured
  10227. * @nlh: RTNL message
  10228. * @flags: bridge flags
  10229. *
  10230. * Inserts a new hardware bridge if not already created and
  10231. * enables the bridging mode requested (VEB or VEPA). If the
  10232. * hardware bridge has already been inserted and the request
  10233. * is to change the mode then that requires a PF reset to
  10234. * allow rebuild of the components with required hardware
  10235. * bridge mode enabled.
  10236. *
  10237. * Note: expects to be called while under rtnl_lock()
  10238. **/
  10239. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  10240. struct nlmsghdr *nlh,
  10241. u16 flags)
  10242. {
  10243. struct i40e_netdev_priv *np = netdev_priv(dev);
  10244. struct i40e_vsi *vsi = np->vsi;
  10245. struct i40e_pf *pf = vsi->back;
  10246. struct i40e_veb *veb = NULL;
  10247. struct nlattr *attr, *br_spec;
  10248. int i, rem;
  10249. /* Only for PF VSI for now */
  10250. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10251. return -EOPNOTSUPP;
  10252. /* Find the HW bridge for PF VSI */
  10253. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10254. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10255. veb = pf->veb[i];
  10256. }
  10257. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  10258. nla_for_each_nested(attr, br_spec, rem) {
  10259. __u16 mode;
  10260. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  10261. continue;
  10262. mode = nla_get_u16(attr);
  10263. if ((mode != BRIDGE_MODE_VEPA) &&
  10264. (mode != BRIDGE_MODE_VEB))
  10265. return -EINVAL;
  10266. /* Insert a new HW bridge */
  10267. if (!veb) {
  10268. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  10269. vsi->tc_config.enabled_tc);
  10270. if (veb) {
  10271. veb->bridge_mode = mode;
  10272. i40e_config_bridge_mode(veb);
  10273. } else {
  10274. /* No Bridge HW offload available */
  10275. return -ENOENT;
  10276. }
  10277. break;
  10278. } else if (mode != veb->bridge_mode) {
  10279. /* Existing HW bridge but different mode needs reset */
  10280. veb->bridge_mode = mode;
  10281. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  10282. if (mode == BRIDGE_MODE_VEB)
  10283. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  10284. else
  10285. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  10286. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  10287. break;
  10288. }
  10289. }
  10290. return 0;
  10291. }
  10292. /**
  10293. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  10294. * @skb: skb buff
  10295. * @pid: process id
  10296. * @seq: RTNL message seq #
  10297. * @dev: the netdev being configured
  10298. * @filter_mask: unused
  10299. * @nlflags: netlink flags passed in
  10300. *
  10301. * Return the mode in which the hardware bridge is operating in
  10302. * i.e VEB or VEPA.
  10303. **/
  10304. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  10305. struct net_device *dev,
  10306. u32 __always_unused filter_mask,
  10307. int nlflags)
  10308. {
  10309. struct i40e_netdev_priv *np = netdev_priv(dev);
  10310. struct i40e_vsi *vsi = np->vsi;
  10311. struct i40e_pf *pf = vsi->back;
  10312. struct i40e_veb *veb = NULL;
  10313. int i;
  10314. /* Only for PF VSI for now */
  10315. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10316. return -EOPNOTSUPP;
  10317. /* Find the HW bridge for the PF VSI */
  10318. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10319. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10320. veb = pf->veb[i];
  10321. }
  10322. if (!veb)
  10323. return 0;
  10324. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  10325. 0, 0, nlflags, filter_mask, NULL);
  10326. }
  10327. /**
  10328. * i40e_features_check - Validate encapsulated packet conforms to limits
  10329. * @skb: skb buff
  10330. * @dev: This physical port's netdev
  10331. * @features: Offload features that the stack believes apply
  10332. **/
  10333. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  10334. struct net_device *dev,
  10335. netdev_features_t features)
  10336. {
  10337. size_t len;
  10338. /* No point in doing any of this if neither checksum nor GSO are
  10339. * being requested for this frame. We can rule out both by just
  10340. * checking for CHECKSUM_PARTIAL
  10341. */
  10342. if (skb->ip_summed != CHECKSUM_PARTIAL)
  10343. return features;
  10344. /* We cannot support GSO if the MSS is going to be less than
  10345. * 64 bytes. If it is then we need to drop support for GSO.
  10346. */
  10347. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  10348. features &= ~NETIF_F_GSO_MASK;
  10349. /* MACLEN can support at most 63 words */
  10350. len = skb_network_header(skb) - skb->data;
  10351. if (len & ~(63 * 2))
  10352. goto out_err;
  10353. /* IPLEN and EIPLEN can support at most 127 dwords */
  10354. len = skb_transport_header(skb) - skb_network_header(skb);
  10355. if (len & ~(127 * 4))
  10356. goto out_err;
  10357. if (skb->encapsulation) {
  10358. /* L4TUNLEN can support 127 words */
  10359. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  10360. if (len & ~(127 * 2))
  10361. goto out_err;
  10362. /* IPLEN can support at most 127 dwords */
  10363. len = skb_inner_transport_header(skb) -
  10364. skb_inner_network_header(skb);
  10365. if (len & ~(127 * 4))
  10366. goto out_err;
  10367. }
  10368. /* No need to validate L4LEN as TCP is the only protocol with a
  10369. * a flexible value and we support all possible values supported
  10370. * by TCP, which is at most 15 dwords
  10371. */
  10372. return features;
  10373. out_err:
  10374. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  10375. }
  10376. /**
  10377. * i40e_xdp_setup - add/remove an XDP program
  10378. * @vsi: VSI to changed
  10379. * @prog: XDP program
  10380. **/
  10381. static int i40e_xdp_setup(struct i40e_vsi *vsi,
  10382. struct bpf_prog *prog)
  10383. {
  10384. int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  10385. struct i40e_pf *pf = vsi->back;
  10386. struct bpf_prog *old_prog;
  10387. bool need_reset;
  10388. int i;
  10389. /* Don't allow frames that span over multiple buffers */
  10390. if (frame_size > vsi->rx_buf_len)
  10391. return -EINVAL;
  10392. if (!i40e_enabled_xdp_vsi(vsi) && !prog)
  10393. return 0;
  10394. /* When turning XDP on->off/off->on we reset and rebuild the rings. */
  10395. need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
  10396. if (need_reset)
  10397. i40e_prep_for_reset(pf, true);
  10398. old_prog = xchg(&vsi->xdp_prog, prog);
  10399. if (need_reset)
  10400. i40e_reset_and_rebuild(pf, true, true);
  10401. for (i = 0; i < vsi->num_queue_pairs; i++)
  10402. WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
  10403. if (old_prog)
  10404. bpf_prog_put(old_prog);
  10405. return 0;
  10406. }
  10407. /**
  10408. * i40e_enter_busy_conf - Enters busy config state
  10409. * @vsi: vsi
  10410. *
  10411. * Returns 0 on success, <0 for failure.
  10412. **/
  10413. static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
  10414. {
  10415. struct i40e_pf *pf = vsi->back;
  10416. int timeout = 50;
  10417. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
  10418. timeout--;
  10419. if (!timeout)
  10420. return -EBUSY;
  10421. usleep_range(1000, 2000);
  10422. }
  10423. return 0;
  10424. }
  10425. /**
  10426. * i40e_exit_busy_conf - Exits busy config state
  10427. * @vsi: vsi
  10428. **/
  10429. static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
  10430. {
  10431. struct i40e_pf *pf = vsi->back;
  10432. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  10433. }
  10434. /**
  10435. * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
  10436. * @vsi: vsi
  10437. * @queue_pair: queue pair
  10438. **/
  10439. static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
  10440. {
  10441. memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
  10442. sizeof(vsi->rx_rings[queue_pair]->rx_stats));
  10443. memset(&vsi->tx_rings[queue_pair]->stats, 0,
  10444. sizeof(vsi->tx_rings[queue_pair]->stats));
  10445. if (i40e_enabled_xdp_vsi(vsi)) {
  10446. memset(&vsi->xdp_rings[queue_pair]->stats, 0,
  10447. sizeof(vsi->xdp_rings[queue_pair]->stats));
  10448. }
  10449. }
  10450. /**
  10451. * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
  10452. * @vsi: vsi
  10453. * @queue_pair: queue pair
  10454. **/
  10455. static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
  10456. {
  10457. i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
  10458. if (i40e_enabled_xdp_vsi(vsi))
  10459. i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
  10460. i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
  10461. }
  10462. /**
  10463. * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
  10464. * @vsi: vsi
  10465. * @queue_pair: queue pair
  10466. * @enable: true for enable, false for disable
  10467. **/
  10468. static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
  10469. bool enable)
  10470. {
  10471. struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
  10472. struct i40e_q_vector *q_vector = rxr->q_vector;
  10473. if (!vsi->netdev)
  10474. return;
  10475. /* All rings in a qp belong to the same qvector. */
  10476. if (q_vector->rx.ring || q_vector->tx.ring) {
  10477. if (enable)
  10478. napi_enable(&q_vector->napi);
  10479. else
  10480. napi_disable(&q_vector->napi);
  10481. }
  10482. }
  10483. /**
  10484. * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
  10485. * @vsi: vsi
  10486. * @queue_pair: queue pair
  10487. * @enable: true for enable, false for disable
  10488. *
  10489. * Returns 0 on success, <0 on failure.
  10490. **/
  10491. static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
  10492. bool enable)
  10493. {
  10494. struct i40e_pf *pf = vsi->back;
  10495. int pf_q, ret = 0;
  10496. pf_q = vsi->base_queue + queue_pair;
  10497. ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
  10498. false /*is xdp*/, enable);
  10499. if (ret) {
  10500. dev_info(&pf->pdev->dev,
  10501. "VSI seid %d Tx ring %d %sable timeout\n",
  10502. vsi->seid, pf_q, (enable ? "en" : "dis"));
  10503. return ret;
  10504. }
  10505. i40e_control_rx_q(pf, pf_q, enable);
  10506. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  10507. if (ret) {
  10508. dev_info(&pf->pdev->dev,
  10509. "VSI seid %d Rx ring %d %sable timeout\n",
  10510. vsi->seid, pf_q, (enable ? "en" : "dis"));
  10511. return ret;
  10512. }
  10513. /* Due to HW errata, on Rx disable only, the register can
  10514. * indicate done before it really is. Needs 50ms to be sure
  10515. */
  10516. if (!enable)
  10517. mdelay(50);
  10518. if (!i40e_enabled_xdp_vsi(vsi))
  10519. return ret;
  10520. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  10521. pf_q + vsi->alloc_queue_pairs,
  10522. true /*is xdp*/, enable);
  10523. if (ret) {
  10524. dev_info(&pf->pdev->dev,
  10525. "VSI seid %d XDP Tx ring %d %sable timeout\n",
  10526. vsi->seid, pf_q, (enable ? "en" : "dis"));
  10527. }
  10528. return ret;
  10529. }
  10530. /**
  10531. * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
  10532. * @vsi: vsi
  10533. * @queue_pair: queue_pair
  10534. **/
  10535. static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
  10536. {
  10537. struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
  10538. struct i40e_pf *pf = vsi->back;
  10539. struct i40e_hw *hw = &pf->hw;
  10540. /* All rings in a qp belong to the same qvector. */
  10541. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  10542. i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
  10543. else
  10544. i40e_irq_dynamic_enable_icr0(pf);
  10545. i40e_flush(hw);
  10546. }
  10547. /**
  10548. * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
  10549. * @vsi: vsi
  10550. * @queue_pair: queue_pair
  10551. **/
  10552. static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
  10553. {
  10554. struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
  10555. struct i40e_pf *pf = vsi->back;
  10556. struct i40e_hw *hw = &pf->hw;
  10557. /* For simplicity, instead of removing the qp interrupt causes
  10558. * from the interrupt linked list, we simply disable the interrupt, and
  10559. * leave the list intact.
  10560. *
  10561. * All rings in a qp belong to the same qvector.
  10562. */
  10563. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  10564. u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
  10565. wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
  10566. i40e_flush(hw);
  10567. synchronize_irq(pf->msix_entries[intpf].vector);
  10568. } else {
  10569. /* Legacy and MSI mode - this stops all interrupt handling */
  10570. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  10571. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  10572. i40e_flush(hw);
  10573. synchronize_irq(pf->pdev->irq);
  10574. }
  10575. }
  10576. /**
  10577. * i40e_queue_pair_disable - Disables a queue pair
  10578. * @vsi: vsi
  10579. * @queue_pair: queue pair
  10580. *
  10581. * Returns 0 on success, <0 on failure.
  10582. **/
  10583. int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
  10584. {
  10585. int err;
  10586. err = i40e_enter_busy_conf(vsi);
  10587. if (err)
  10588. return err;
  10589. i40e_queue_pair_disable_irq(vsi, queue_pair);
  10590. err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
  10591. i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
  10592. i40e_queue_pair_clean_rings(vsi, queue_pair);
  10593. i40e_queue_pair_reset_stats(vsi, queue_pair);
  10594. return err;
  10595. }
  10596. /**
  10597. * i40e_queue_pair_enable - Enables a queue pair
  10598. * @vsi: vsi
  10599. * @queue_pair: queue pair
  10600. *
  10601. * Returns 0 on success, <0 on failure.
  10602. **/
  10603. int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
  10604. {
  10605. int err;
  10606. err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
  10607. if (err)
  10608. return err;
  10609. if (i40e_enabled_xdp_vsi(vsi)) {
  10610. err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
  10611. if (err)
  10612. return err;
  10613. }
  10614. err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
  10615. if (err)
  10616. return err;
  10617. err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
  10618. i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
  10619. i40e_queue_pair_enable_irq(vsi, queue_pair);
  10620. i40e_exit_busy_conf(vsi);
  10621. return err;
  10622. }
  10623. /**
  10624. * i40e_xdp - implements ndo_bpf for i40e
  10625. * @dev: netdevice
  10626. * @xdp: XDP command
  10627. **/
  10628. static int i40e_xdp(struct net_device *dev,
  10629. struct netdev_bpf *xdp)
  10630. {
  10631. struct i40e_netdev_priv *np = netdev_priv(dev);
  10632. struct i40e_vsi *vsi = np->vsi;
  10633. if (vsi->type != I40E_VSI_MAIN)
  10634. return -EINVAL;
  10635. switch (xdp->command) {
  10636. case XDP_SETUP_PROG:
  10637. return i40e_xdp_setup(vsi, xdp->prog);
  10638. case XDP_QUERY_PROG:
  10639. xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
  10640. return 0;
  10641. case XDP_QUERY_XSK_UMEM:
  10642. return i40e_xsk_umem_query(vsi, &xdp->xsk.umem,
  10643. xdp->xsk.queue_id);
  10644. case XDP_SETUP_XSK_UMEM:
  10645. return i40e_xsk_umem_setup(vsi, xdp->xsk.umem,
  10646. xdp->xsk.queue_id);
  10647. default:
  10648. return -EINVAL;
  10649. }
  10650. }
  10651. static const struct net_device_ops i40e_netdev_ops = {
  10652. .ndo_open = i40e_open,
  10653. .ndo_stop = i40e_close,
  10654. .ndo_start_xmit = i40e_lan_xmit_frame,
  10655. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  10656. .ndo_set_rx_mode = i40e_set_rx_mode,
  10657. .ndo_validate_addr = eth_validate_addr,
  10658. .ndo_set_mac_address = i40e_set_mac,
  10659. .ndo_change_mtu = i40e_change_mtu,
  10660. .ndo_do_ioctl = i40e_ioctl,
  10661. .ndo_tx_timeout = i40e_tx_timeout,
  10662. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  10663. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  10664. #ifdef CONFIG_NET_POLL_CONTROLLER
  10665. .ndo_poll_controller = i40e_netpoll,
  10666. #endif
  10667. .ndo_setup_tc = __i40e_setup_tc,
  10668. .ndo_set_features = i40e_set_features,
  10669. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  10670. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  10671. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  10672. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  10673. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  10674. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  10675. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  10676. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  10677. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  10678. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  10679. .ndo_fdb_add = i40e_ndo_fdb_add,
  10680. .ndo_features_check = i40e_features_check,
  10681. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  10682. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  10683. .ndo_bpf = i40e_xdp,
  10684. .ndo_xdp_xmit = i40e_xdp_xmit,
  10685. .ndo_xsk_async_xmit = i40e_xsk_async_xmit,
  10686. };
  10687. /**
  10688. * i40e_config_netdev - Setup the netdev flags
  10689. * @vsi: the VSI being configured
  10690. *
  10691. * Returns 0 on success, negative value on failure
  10692. **/
  10693. static int i40e_config_netdev(struct i40e_vsi *vsi)
  10694. {
  10695. struct i40e_pf *pf = vsi->back;
  10696. struct i40e_hw *hw = &pf->hw;
  10697. struct i40e_netdev_priv *np;
  10698. struct net_device *netdev;
  10699. u8 broadcast[ETH_ALEN];
  10700. u8 mac_addr[ETH_ALEN];
  10701. int etherdev_size;
  10702. netdev_features_t hw_enc_features;
  10703. netdev_features_t hw_features;
  10704. etherdev_size = sizeof(struct i40e_netdev_priv);
  10705. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  10706. if (!netdev)
  10707. return -ENOMEM;
  10708. vsi->netdev = netdev;
  10709. np = netdev_priv(netdev);
  10710. np->vsi = vsi;
  10711. hw_enc_features = NETIF_F_SG |
  10712. NETIF_F_IP_CSUM |
  10713. NETIF_F_IPV6_CSUM |
  10714. NETIF_F_HIGHDMA |
  10715. NETIF_F_SOFT_FEATURES |
  10716. NETIF_F_TSO |
  10717. NETIF_F_TSO_ECN |
  10718. NETIF_F_TSO6 |
  10719. NETIF_F_GSO_GRE |
  10720. NETIF_F_GSO_GRE_CSUM |
  10721. NETIF_F_GSO_PARTIAL |
  10722. NETIF_F_GSO_UDP_TUNNEL |
  10723. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  10724. NETIF_F_SCTP_CRC |
  10725. NETIF_F_RXHASH |
  10726. NETIF_F_RXCSUM |
  10727. 0;
  10728. if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
  10729. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  10730. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  10731. netdev->hw_enc_features |= hw_enc_features;
  10732. /* record features VLANs can make use of */
  10733. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  10734. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  10735. netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
  10736. hw_features = hw_enc_features |
  10737. NETIF_F_HW_VLAN_CTAG_TX |
  10738. NETIF_F_HW_VLAN_CTAG_RX;
  10739. netdev->hw_features |= hw_features;
  10740. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  10741. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  10742. if (vsi->type == I40E_VSI_MAIN) {
  10743. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  10744. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  10745. /* The following steps are necessary for two reasons. First,
  10746. * some older NVM configurations load a default MAC-VLAN
  10747. * filter that will accept any tagged packet, and we want to
  10748. * replace this with a normal filter. Additionally, it is
  10749. * possible our MAC address was provided by the platform using
  10750. * Open Firmware or similar.
  10751. *
  10752. * Thus, we need to remove the default filter and install one
  10753. * specific to the MAC address.
  10754. */
  10755. i40e_rm_default_mac_filter(vsi, mac_addr);
  10756. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10757. i40e_add_mac_filter(vsi, mac_addr);
  10758. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10759. } else {
  10760. /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
  10761. * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
  10762. * the end, which is 4 bytes long, so force truncation of the
  10763. * original name by IFNAMSIZ - 4
  10764. */
  10765. snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
  10766. IFNAMSIZ - 4,
  10767. pf->vsi[pf->lan_vsi]->netdev->name);
  10768. eth_random_addr(mac_addr);
  10769. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10770. i40e_add_mac_filter(vsi, mac_addr);
  10771. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10772. }
  10773. /* Add the broadcast filter so that we initially will receive
  10774. * broadcast packets. Note that when a new VLAN is first added the
  10775. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  10776. * specific filters as part of transitioning into "vlan" operation.
  10777. * When more VLANs are added, the driver will copy each existing MAC
  10778. * filter and add it for the new VLAN.
  10779. *
  10780. * Broadcast filters are handled specially by
  10781. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  10782. * promiscuous bit instead of adding this directly as a MAC/VLAN
  10783. * filter. The subtask will update the correct broadcast promiscuous
  10784. * bits as VLANs become active or inactive.
  10785. */
  10786. eth_broadcast_addr(broadcast);
  10787. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10788. i40e_add_mac_filter(vsi, broadcast);
  10789. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10790. ether_addr_copy(netdev->dev_addr, mac_addr);
  10791. ether_addr_copy(netdev->perm_addr, mac_addr);
  10792. netdev->priv_flags |= IFF_UNICAST_FLT;
  10793. netdev->priv_flags |= IFF_SUPP_NOFCS;
  10794. /* Setup netdev TC information */
  10795. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  10796. netdev->netdev_ops = &i40e_netdev_ops;
  10797. netdev->watchdog_timeo = 5 * HZ;
  10798. i40e_set_ethtool_ops(netdev);
  10799. /* MTU range: 68 - 9706 */
  10800. netdev->min_mtu = ETH_MIN_MTU;
  10801. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  10802. return 0;
  10803. }
  10804. /**
  10805. * i40e_vsi_delete - Delete a VSI from the switch
  10806. * @vsi: the VSI being removed
  10807. *
  10808. * Returns 0 on success, negative value on failure
  10809. **/
  10810. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  10811. {
  10812. /* remove default VSI is not allowed */
  10813. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  10814. return;
  10815. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  10816. }
  10817. /**
  10818. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  10819. * @vsi: the VSI being queried
  10820. *
  10821. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  10822. **/
  10823. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  10824. {
  10825. struct i40e_veb *veb;
  10826. struct i40e_pf *pf = vsi->back;
  10827. /* Uplink is not a bridge so default to VEB */
  10828. if (vsi->veb_idx == I40E_NO_VEB)
  10829. return 1;
  10830. veb = pf->veb[vsi->veb_idx];
  10831. if (!veb) {
  10832. dev_info(&pf->pdev->dev,
  10833. "There is no veb associated with the bridge\n");
  10834. return -ENOENT;
  10835. }
  10836. /* Uplink is a bridge in VEPA mode */
  10837. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  10838. return 0;
  10839. } else {
  10840. /* Uplink is a bridge in VEB mode */
  10841. return 1;
  10842. }
  10843. /* VEPA is now default bridge, so return 0 */
  10844. return 0;
  10845. }
  10846. /**
  10847. * i40e_add_vsi - Add a VSI to the switch
  10848. * @vsi: the VSI being configured
  10849. *
  10850. * This initializes a VSI context depending on the VSI type to be added and
  10851. * passes it down to the add_vsi aq command.
  10852. **/
  10853. static int i40e_add_vsi(struct i40e_vsi *vsi)
  10854. {
  10855. int ret = -ENODEV;
  10856. struct i40e_pf *pf = vsi->back;
  10857. struct i40e_hw *hw = &pf->hw;
  10858. struct i40e_vsi_context ctxt;
  10859. struct i40e_mac_filter *f;
  10860. struct hlist_node *h;
  10861. int bkt;
  10862. u8 enabled_tc = 0x1; /* TC0 enabled */
  10863. int f_count = 0;
  10864. memset(&ctxt, 0, sizeof(ctxt));
  10865. switch (vsi->type) {
  10866. case I40E_VSI_MAIN:
  10867. /* The PF's main VSI is already setup as part of the
  10868. * device initialization, so we'll not bother with
  10869. * the add_vsi call, but we will retrieve the current
  10870. * VSI context.
  10871. */
  10872. ctxt.seid = pf->main_vsi_seid;
  10873. ctxt.pf_num = pf->hw.pf_id;
  10874. ctxt.vf_num = 0;
  10875. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  10876. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10877. if (ret) {
  10878. dev_info(&pf->pdev->dev,
  10879. "couldn't get PF vsi config, err %s aq_err %s\n",
  10880. i40e_stat_str(&pf->hw, ret),
  10881. i40e_aq_str(&pf->hw,
  10882. pf->hw.aq.asq_last_status));
  10883. return -ENOENT;
  10884. }
  10885. vsi->info = ctxt.info;
  10886. vsi->info.valid_sections = 0;
  10887. vsi->seid = ctxt.seid;
  10888. vsi->id = ctxt.vsi_number;
  10889. enabled_tc = i40e_pf_get_tc_map(pf);
  10890. /* Source pruning is enabled by default, so the flag is
  10891. * negative logic - if it's set, we need to fiddle with
  10892. * the VSI to disable source pruning.
  10893. */
  10894. if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
  10895. memset(&ctxt, 0, sizeof(ctxt));
  10896. ctxt.seid = pf->main_vsi_seid;
  10897. ctxt.pf_num = pf->hw.pf_id;
  10898. ctxt.vf_num = 0;
  10899. ctxt.info.valid_sections |=
  10900. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10901. ctxt.info.switch_id =
  10902. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
  10903. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10904. if (ret) {
  10905. dev_info(&pf->pdev->dev,
  10906. "update vsi failed, err %s aq_err %s\n",
  10907. i40e_stat_str(&pf->hw, ret),
  10908. i40e_aq_str(&pf->hw,
  10909. pf->hw.aq.asq_last_status));
  10910. ret = -ENOENT;
  10911. goto err;
  10912. }
  10913. }
  10914. /* MFP mode setup queue map and update VSI */
  10915. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  10916. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  10917. memset(&ctxt, 0, sizeof(ctxt));
  10918. ctxt.seid = pf->main_vsi_seid;
  10919. ctxt.pf_num = pf->hw.pf_id;
  10920. ctxt.vf_num = 0;
  10921. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  10922. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10923. if (ret) {
  10924. dev_info(&pf->pdev->dev,
  10925. "update vsi failed, err %s aq_err %s\n",
  10926. i40e_stat_str(&pf->hw, ret),
  10927. i40e_aq_str(&pf->hw,
  10928. pf->hw.aq.asq_last_status));
  10929. ret = -ENOENT;
  10930. goto err;
  10931. }
  10932. /* update the local VSI info queue map */
  10933. i40e_vsi_update_queue_map(vsi, &ctxt);
  10934. vsi->info.valid_sections = 0;
  10935. } else {
  10936. /* Default/Main VSI is only enabled for TC0
  10937. * reconfigure it to enable all TCs that are
  10938. * available on the port in SFP mode.
  10939. * For MFP case the iSCSI PF would use this
  10940. * flow to enable LAN+iSCSI TC.
  10941. */
  10942. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  10943. if (ret) {
  10944. /* Single TC condition is not fatal,
  10945. * message and continue
  10946. */
  10947. dev_info(&pf->pdev->dev,
  10948. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  10949. enabled_tc,
  10950. i40e_stat_str(&pf->hw, ret),
  10951. i40e_aq_str(&pf->hw,
  10952. pf->hw.aq.asq_last_status));
  10953. }
  10954. }
  10955. break;
  10956. case I40E_VSI_FDIR:
  10957. ctxt.pf_num = hw->pf_id;
  10958. ctxt.vf_num = 0;
  10959. ctxt.uplink_seid = vsi->uplink_seid;
  10960. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10961. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10962. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  10963. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  10964. ctxt.info.valid_sections |=
  10965. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10966. ctxt.info.switch_id =
  10967. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10968. }
  10969. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10970. break;
  10971. case I40E_VSI_VMDQ2:
  10972. ctxt.pf_num = hw->pf_id;
  10973. ctxt.vf_num = 0;
  10974. ctxt.uplink_seid = vsi->uplink_seid;
  10975. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10976. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  10977. /* This VSI is connected to VEB so the switch_id
  10978. * should be set to zero by default.
  10979. */
  10980. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10981. ctxt.info.valid_sections |=
  10982. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10983. ctxt.info.switch_id =
  10984. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10985. }
  10986. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10987. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10988. break;
  10989. case I40E_VSI_SRIOV:
  10990. ctxt.pf_num = hw->pf_id;
  10991. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  10992. ctxt.uplink_seid = vsi->uplink_seid;
  10993. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10994. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  10995. /* This VSI is connected to VEB so the switch_id
  10996. * should be set to zero by default.
  10997. */
  10998. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10999. ctxt.info.valid_sections |=
  11000. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  11001. ctxt.info.switch_id =
  11002. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  11003. }
  11004. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  11005. ctxt.info.valid_sections |=
  11006. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  11007. ctxt.info.queueing_opt_flags |=
  11008. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  11009. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  11010. }
  11011. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  11012. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  11013. if (pf->vf[vsi->vf_id].spoofchk) {
  11014. ctxt.info.valid_sections |=
  11015. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  11016. ctxt.info.sec_flags |=
  11017. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  11018. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  11019. }
  11020. /* Setup the VSI tx/rx queue map for TC0 only for now */
  11021. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  11022. break;
  11023. case I40E_VSI_IWARP:
  11024. /* send down message to iWARP */
  11025. break;
  11026. default:
  11027. return -ENODEV;
  11028. }
  11029. if (vsi->type != I40E_VSI_MAIN) {
  11030. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  11031. if (ret) {
  11032. dev_info(&vsi->back->pdev->dev,
  11033. "add vsi failed, err %s aq_err %s\n",
  11034. i40e_stat_str(&pf->hw, ret),
  11035. i40e_aq_str(&pf->hw,
  11036. pf->hw.aq.asq_last_status));
  11037. ret = -ENOENT;
  11038. goto err;
  11039. }
  11040. vsi->info = ctxt.info;
  11041. vsi->info.valid_sections = 0;
  11042. vsi->seid = ctxt.seid;
  11043. vsi->id = ctxt.vsi_number;
  11044. }
  11045. vsi->active_filters = 0;
  11046. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  11047. spin_lock_bh(&vsi->mac_filter_hash_lock);
  11048. /* If macvlan filters already exist, force them to get loaded */
  11049. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  11050. f->state = I40E_FILTER_NEW;
  11051. f_count++;
  11052. }
  11053. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  11054. if (f_count) {
  11055. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  11056. set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
  11057. }
  11058. /* Update VSI BW information */
  11059. ret = i40e_vsi_get_bw_info(vsi);
  11060. if (ret) {
  11061. dev_info(&pf->pdev->dev,
  11062. "couldn't get vsi bw info, err %s aq_err %s\n",
  11063. i40e_stat_str(&pf->hw, ret),
  11064. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11065. /* VSI is already added so not tearing that up */
  11066. ret = 0;
  11067. }
  11068. err:
  11069. return ret;
  11070. }
  11071. /**
  11072. * i40e_vsi_release - Delete a VSI and free its resources
  11073. * @vsi: the VSI being removed
  11074. *
  11075. * Returns 0 on success or < 0 on error
  11076. **/
  11077. int i40e_vsi_release(struct i40e_vsi *vsi)
  11078. {
  11079. struct i40e_mac_filter *f;
  11080. struct hlist_node *h;
  11081. struct i40e_veb *veb = NULL;
  11082. struct i40e_pf *pf;
  11083. u16 uplink_seid;
  11084. int i, n, bkt;
  11085. pf = vsi->back;
  11086. /* release of a VEB-owner or last VSI is not allowed */
  11087. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  11088. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  11089. vsi->seid, vsi->uplink_seid);
  11090. return -ENODEV;
  11091. }
  11092. if (vsi == pf->vsi[pf->lan_vsi] &&
  11093. !test_bit(__I40E_DOWN, pf->state)) {
  11094. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  11095. return -ENODEV;
  11096. }
  11097. uplink_seid = vsi->uplink_seid;
  11098. if (vsi->type != I40E_VSI_SRIOV) {
  11099. if (vsi->netdev_registered) {
  11100. vsi->netdev_registered = false;
  11101. if (vsi->netdev) {
  11102. /* results in a call to i40e_close() */
  11103. unregister_netdev(vsi->netdev);
  11104. }
  11105. } else {
  11106. i40e_vsi_close(vsi);
  11107. }
  11108. i40e_vsi_disable_irq(vsi);
  11109. }
  11110. spin_lock_bh(&vsi->mac_filter_hash_lock);
  11111. /* clear the sync flag on all filters */
  11112. if (vsi->netdev) {
  11113. __dev_uc_unsync(vsi->netdev, NULL);
  11114. __dev_mc_unsync(vsi->netdev, NULL);
  11115. }
  11116. /* make sure any remaining filters are marked for deletion */
  11117. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  11118. __i40e_del_filter(vsi, f);
  11119. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  11120. i40e_sync_vsi_filters(vsi);
  11121. i40e_vsi_delete(vsi);
  11122. i40e_vsi_free_q_vectors(vsi);
  11123. if (vsi->netdev) {
  11124. free_netdev(vsi->netdev);
  11125. vsi->netdev = NULL;
  11126. }
  11127. i40e_vsi_clear_rings(vsi);
  11128. i40e_vsi_clear(vsi);
  11129. /* If this was the last thing on the VEB, except for the
  11130. * controlling VSI, remove the VEB, which puts the controlling
  11131. * VSI onto the next level down in the switch.
  11132. *
  11133. * Well, okay, there's one more exception here: don't remove
  11134. * the orphan VEBs yet. We'll wait for an explicit remove request
  11135. * from up the network stack.
  11136. */
  11137. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  11138. if (pf->vsi[i] &&
  11139. pf->vsi[i]->uplink_seid == uplink_seid &&
  11140. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  11141. n++; /* count the VSIs */
  11142. }
  11143. }
  11144. for (i = 0; i < I40E_MAX_VEB; i++) {
  11145. if (!pf->veb[i])
  11146. continue;
  11147. if (pf->veb[i]->uplink_seid == uplink_seid)
  11148. n++; /* count the VEBs */
  11149. if (pf->veb[i]->seid == uplink_seid)
  11150. veb = pf->veb[i];
  11151. }
  11152. if (n == 0 && veb && veb->uplink_seid != 0)
  11153. i40e_veb_release(veb);
  11154. return 0;
  11155. }
  11156. /**
  11157. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  11158. * @vsi: ptr to the VSI
  11159. *
  11160. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  11161. * corresponding SW VSI structure and initializes num_queue_pairs for the
  11162. * newly allocated VSI.
  11163. *
  11164. * Returns 0 on success or negative on failure
  11165. **/
  11166. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  11167. {
  11168. int ret = -ENOENT;
  11169. struct i40e_pf *pf = vsi->back;
  11170. if (vsi->q_vectors[0]) {
  11171. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  11172. vsi->seid);
  11173. return -EEXIST;
  11174. }
  11175. if (vsi->base_vector) {
  11176. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  11177. vsi->seid, vsi->base_vector);
  11178. return -EEXIST;
  11179. }
  11180. ret = i40e_vsi_alloc_q_vectors(vsi);
  11181. if (ret) {
  11182. dev_info(&pf->pdev->dev,
  11183. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  11184. vsi->num_q_vectors, vsi->seid, ret);
  11185. vsi->num_q_vectors = 0;
  11186. goto vector_setup_out;
  11187. }
  11188. /* In Legacy mode, we do not have to get any other vector since we
  11189. * piggyback on the misc/ICR0 for queue interrupts.
  11190. */
  11191. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  11192. return ret;
  11193. if (vsi->num_q_vectors)
  11194. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  11195. vsi->num_q_vectors, vsi->idx);
  11196. if (vsi->base_vector < 0) {
  11197. dev_info(&pf->pdev->dev,
  11198. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  11199. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  11200. i40e_vsi_free_q_vectors(vsi);
  11201. ret = -ENOENT;
  11202. goto vector_setup_out;
  11203. }
  11204. vector_setup_out:
  11205. return ret;
  11206. }
  11207. /**
  11208. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  11209. * @vsi: pointer to the vsi.
  11210. *
  11211. * This re-allocates a vsi's queue resources.
  11212. *
  11213. * Returns pointer to the successfully allocated and configured VSI sw struct
  11214. * on success, otherwise returns NULL on failure.
  11215. **/
  11216. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  11217. {
  11218. u16 alloc_queue_pairs;
  11219. struct i40e_pf *pf;
  11220. u8 enabled_tc;
  11221. int ret;
  11222. if (!vsi)
  11223. return NULL;
  11224. pf = vsi->back;
  11225. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  11226. i40e_vsi_clear_rings(vsi);
  11227. i40e_vsi_free_arrays(vsi, false);
  11228. i40e_set_num_rings_in_vsi(vsi);
  11229. ret = i40e_vsi_alloc_arrays(vsi, false);
  11230. if (ret)
  11231. goto err_vsi;
  11232. alloc_queue_pairs = vsi->alloc_queue_pairs *
  11233. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  11234. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  11235. if (ret < 0) {
  11236. dev_info(&pf->pdev->dev,
  11237. "failed to get tracking for %d queues for VSI %d err %d\n",
  11238. alloc_queue_pairs, vsi->seid, ret);
  11239. goto err_vsi;
  11240. }
  11241. vsi->base_queue = ret;
  11242. /* Update the FW view of the VSI. Force a reset of TC and queue
  11243. * layout configurations.
  11244. */
  11245. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  11246. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  11247. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  11248. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  11249. if (vsi->type == I40E_VSI_MAIN)
  11250. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  11251. /* assign it some queues */
  11252. ret = i40e_alloc_rings(vsi);
  11253. if (ret)
  11254. goto err_rings;
  11255. /* map all of the rings to the q_vectors */
  11256. i40e_vsi_map_rings_to_vectors(vsi);
  11257. return vsi;
  11258. err_rings:
  11259. i40e_vsi_free_q_vectors(vsi);
  11260. if (vsi->netdev_registered) {
  11261. vsi->netdev_registered = false;
  11262. unregister_netdev(vsi->netdev);
  11263. free_netdev(vsi->netdev);
  11264. vsi->netdev = NULL;
  11265. }
  11266. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  11267. err_vsi:
  11268. i40e_vsi_clear(vsi);
  11269. return NULL;
  11270. }
  11271. /**
  11272. * i40e_vsi_setup - Set up a VSI by a given type
  11273. * @pf: board private structure
  11274. * @type: VSI type
  11275. * @uplink_seid: the switch element to link to
  11276. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  11277. *
  11278. * This allocates the sw VSI structure and its queue resources, then add a VSI
  11279. * to the identified VEB.
  11280. *
  11281. * Returns pointer to the successfully allocated and configure VSI sw struct on
  11282. * success, otherwise returns NULL on failure.
  11283. **/
  11284. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  11285. u16 uplink_seid, u32 param1)
  11286. {
  11287. struct i40e_vsi *vsi = NULL;
  11288. struct i40e_veb *veb = NULL;
  11289. u16 alloc_queue_pairs;
  11290. int ret, i;
  11291. int v_idx;
  11292. /* The requested uplink_seid must be either
  11293. * - the PF's port seid
  11294. * no VEB is needed because this is the PF
  11295. * or this is a Flow Director special case VSI
  11296. * - seid of an existing VEB
  11297. * - seid of a VSI that owns an existing VEB
  11298. * - seid of a VSI that doesn't own a VEB
  11299. * a new VEB is created and the VSI becomes the owner
  11300. * - seid of the PF VSI, which is what creates the first VEB
  11301. * this is a special case of the previous
  11302. *
  11303. * Find which uplink_seid we were given and create a new VEB if needed
  11304. */
  11305. for (i = 0; i < I40E_MAX_VEB; i++) {
  11306. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  11307. veb = pf->veb[i];
  11308. break;
  11309. }
  11310. }
  11311. if (!veb && uplink_seid != pf->mac_seid) {
  11312. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11313. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  11314. vsi = pf->vsi[i];
  11315. break;
  11316. }
  11317. }
  11318. if (!vsi) {
  11319. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  11320. uplink_seid);
  11321. return NULL;
  11322. }
  11323. if (vsi->uplink_seid == pf->mac_seid)
  11324. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  11325. vsi->tc_config.enabled_tc);
  11326. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  11327. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  11328. vsi->tc_config.enabled_tc);
  11329. if (veb) {
  11330. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  11331. dev_info(&vsi->back->pdev->dev,
  11332. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  11333. return NULL;
  11334. }
  11335. /* We come up by default in VEPA mode if SRIOV is not
  11336. * already enabled, in which case we can't force VEPA
  11337. * mode.
  11338. */
  11339. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  11340. veb->bridge_mode = BRIDGE_MODE_VEPA;
  11341. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  11342. }
  11343. i40e_config_bridge_mode(veb);
  11344. }
  11345. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  11346. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  11347. veb = pf->veb[i];
  11348. }
  11349. if (!veb) {
  11350. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  11351. return NULL;
  11352. }
  11353. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11354. uplink_seid = veb->seid;
  11355. }
  11356. /* get vsi sw struct */
  11357. v_idx = i40e_vsi_mem_alloc(pf, type);
  11358. if (v_idx < 0)
  11359. goto err_alloc;
  11360. vsi = pf->vsi[v_idx];
  11361. if (!vsi)
  11362. goto err_alloc;
  11363. vsi->type = type;
  11364. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  11365. if (type == I40E_VSI_MAIN)
  11366. pf->lan_vsi = v_idx;
  11367. else if (type == I40E_VSI_SRIOV)
  11368. vsi->vf_id = param1;
  11369. /* assign it some queues */
  11370. alloc_queue_pairs = vsi->alloc_queue_pairs *
  11371. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  11372. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  11373. if (ret < 0) {
  11374. dev_info(&pf->pdev->dev,
  11375. "failed to get tracking for %d queues for VSI %d err=%d\n",
  11376. alloc_queue_pairs, vsi->seid, ret);
  11377. goto err_vsi;
  11378. }
  11379. vsi->base_queue = ret;
  11380. /* get a VSI from the hardware */
  11381. vsi->uplink_seid = uplink_seid;
  11382. ret = i40e_add_vsi(vsi);
  11383. if (ret)
  11384. goto err_vsi;
  11385. switch (vsi->type) {
  11386. /* setup the netdev if needed */
  11387. case I40E_VSI_MAIN:
  11388. case I40E_VSI_VMDQ2:
  11389. ret = i40e_config_netdev(vsi);
  11390. if (ret)
  11391. goto err_netdev;
  11392. ret = register_netdev(vsi->netdev);
  11393. if (ret)
  11394. goto err_netdev;
  11395. vsi->netdev_registered = true;
  11396. netif_carrier_off(vsi->netdev);
  11397. #ifdef CONFIG_I40E_DCB
  11398. /* Setup DCB netlink interface */
  11399. i40e_dcbnl_setup(vsi);
  11400. #endif /* CONFIG_I40E_DCB */
  11401. /* fall through */
  11402. case I40E_VSI_FDIR:
  11403. /* set up vectors and rings if needed */
  11404. ret = i40e_vsi_setup_vectors(vsi);
  11405. if (ret)
  11406. goto err_msix;
  11407. ret = i40e_alloc_rings(vsi);
  11408. if (ret)
  11409. goto err_rings;
  11410. /* map all of the rings to the q_vectors */
  11411. i40e_vsi_map_rings_to_vectors(vsi);
  11412. i40e_vsi_reset_stats(vsi);
  11413. break;
  11414. default:
  11415. /* no netdev or rings for the other VSI types */
  11416. break;
  11417. }
  11418. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  11419. (vsi->type == I40E_VSI_VMDQ2)) {
  11420. ret = i40e_vsi_config_rss(vsi);
  11421. }
  11422. return vsi;
  11423. err_rings:
  11424. i40e_vsi_free_q_vectors(vsi);
  11425. err_msix:
  11426. if (vsi->netdev_registered) {
  11427. vsi->netdev_registered = false;
  11428. unregister_netdev(vsi->netdev);
  11429. free_netdev(vsi->netdev);
  11430. vsi->netdev = NULL;
  11431. }
  11432. err_netdev:
  11433. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  11434. err_vsi:
  11435. i40e_vsi_clear(vsi);
  11436. err_alloc:
  11437. return NULL;
  11438. }
  11439. /**
  11440. * i40e_veb_get_bw_info - Query VEB BW information
  11441. * @veb: the veb to query
  11442. *
  11443. * Query the Tx scheduler BW configuration data for given VEB
  11444. **/
  11445. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  11446. {
  11447. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  11448. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  11449. struct i40e_pf *pf = veb->pf;
  11450. struct i40e_hw *hw = &pf->hw;
  11451. u32 tc_bw_max;
  11452. int ret = 0;
  11453. int i;
  11454. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  11455. &bw_data, NULL);
  11456. if (ret) {
  11457. dev_info(&pf->pdev->dev,
  11458. "query veb bw config failed, err %s aq_err %s\n",
  11459. i40e_stat_str(&pf->hw, ret),
  11460. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11461. goto out;
  11462. }
  11463. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  11464. &ets_data, NULL);
  11465. if (ret) {
  11466. dev_info(&pf->pdev->dev,
  11467. "query veb bw ets config failed, err %s aq_err %s\n",
  11468. i40e_stat_str(&pf->hw, ret),
  11469. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11470. goto out;
  11471. }
  11472. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  11473. veb->bw_max_quanta = ets_data.tc_bw_max;
  11474. veb->is_abs_credits = bw_data.absolute_credits_enable;
  11475. veb->enabled_tc = ets_data.tc_valid_bits;
  11476. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  11477. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  11478. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  11479. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  11480. veb->bw_tc_limit_credits[i] =
  11481. le16_to_cpu(bw_data.tc_bw_limits[i]);
  11482. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  11483. }
  11484. out:
  11485. return ret;
  11486. }
  11487. /**
  11488. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  11489. * @pf: board private structure
  11490. *
  11491. * On error: returns error code (negative)
  11492. * On success: returns vsi index in PF (positive)
  11493. **/
  11494. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  11495. {
  11496. int ret = -ENOENT;
  11497. struct i40e_veb *veb;
  11498. int i;
  11499. /* Need to protect the allocation of switch elements at the PF level */
  11500. mutex_lock(&pf->switch_mutex);
  11501. /* VEB list may be fragmented if VEB creation/destruction has
  11502. * been happening. We can afford to do a quick scan to look
  11503. * for any free slots in the list.
  11504. *
  11505. * find next empty veb slot, looping back around if necessary
  11506. */
  11507. i = 0;
  11508. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  11509. i++;
  11510. if (i >= I40E_MAX_VEB) {
  11511. ret = -ENOMEM;
  11512. goto err_alloc_veb; /* out of VEB slots! */
  11513. }
  11514. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  11515. if (!veb) {
  11516. ret = -ENOMEM;
  11517. goto err_alloc_veb;
  11518. }
  11519. veb->pf = pf;
  11520. veb->idx = i;
  11521. veb->enabled_tc = 1;
  11522. pf->veb[i] = veb;
  11523. ret = i;
  11524. err_alloc_veb:
  11525. mutex_unlock(&pf->switch_mutex);
  11526. return ret;
  11527. }
  11528. /**
  11529. * i40e_switch_branch_release - Delete a branch of the switch tree
  11530. * @branch: where to start deleting
  11531. *
  11532. * This uses recursion to find the tips of the branch to be
  11533. * removed, deleting until we get back to and can delete this VEB.
  11534. **/
  11535. static void i40e_switch_branch_release(struct i40e_veb *branch)
  11536. {
  11537. struct i40e_pf *pf = branch->pf;
  11538. u16 branch_seid = branch->seid;
  11539. u16 veb_idx = branch->idx;
  11540. int i;
  11541. /* release any VEBs on this VEB - RECURSION */
  11542. for (i = 0; i < I40E_MAX_VEB; i++) {
  11543. if (!pf->veb[i])
  11544. continue;
  11545. if (pf->veb[i]->uplink_seid == branch->seid)
  11546. i40e_switch_branch_release(pf->veb[i]);
  11547. }
  11548. /* Release the VSIs on this VEB, but not the owner VSI.
  11549. *
  11550. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  11551. * the VEB itself, so don't use (*branch) after this loop.
  11552. */
  11553. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11554. if (!pf->vsi[i])
  11555. continue;
  11556. if (pf->vsi[i]->uplink_seid == branch_seid &&
  11557. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  11558. i40e_vsi_release(pf->vsi[i]);
  11559. }
  11560. }
  11561. /* There's one corner case where the VEB might not have been
  11562. * removed, so double check it here and remove it if needed.
  11563. * This case happens if the veb was created from the debugfs
  11564. * commands and no VSIs were added to it.
  11565. */
  11566. if (pf->veb[veb_idx])
  11567. i40e_veb_release(pf->veb[veb_idx]);
  11568. }
  11569. /**
  11570. * i40e_veb_clear - remove veb struct
  11571. * @veb: the veb to remove
  11572. **/
  11573. static void i40e_veb_clear(struct i40e_veb *veb)
  11574. {
  11575. if (!veb)
  11576. return;
  11577. if (veb->pf) {
  11578. struct i40e_pf *pf = veb->pf;
  11579. mutex_lock(&pf->switch_mutex);
  11580. if (pf->veb[veb->idx] == veb)
  11581. pf->veb[veb->idx] = NULL;
  11582. mutex_unlock(&pf->switch_mutex);
  11583. }
  11584. kfree(veb);
  11585. }
  11586. /**
  11587. * i40e_veb_release - Delete a VEB and free its resources
  11588. * @veb: the VEB being removed
  11589. **/
  11590. void i40e_veb_release(struct i40e_veb *veb)
  11591. {
  11592. struct i40e_vsi *vsi = NULL;
  11593. struct i40e_pf *pf;
  11594. int i, n = 0;
  11595. pf = veb->pf;
  11596. /* find the remaining VSI and check for extras */
  11597. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11598. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  11599. n++;
  11600. vsi = pf->vsi[i];
  11601. }
  11602. }
  11603. if (n != 1) {
  11604. dev_info(&pf->pdev->dev,
  11605. "can't remove VEB %d with %d VSIs left\n",
  11606. veb->seid, n);
  11607. return;
  11608. }
  11609. /* move the remaining VSI to uplink veb */
  11610. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  11611. if (veb->uplink_seid) {
  11612. vsi->uplink_seid = veb->uplink_seid;
  11613. if (veb->uplink_seid == pf->mac_seid)
  11614. vsi->veb_idx = I40E_NO_VEB;
  11615. else
  11616. vsi->veb_idx = veb->veb_idx;
  11617. } else {
  11618. /* floating VEB */
  11619. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  11620. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  11621. }
  11622. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11623. i40e_veb_clear(veb);
  11624. }
  11625. /**
  11626. * i40e_add_veb - create the VEB in the switch
  11627. * @veb: the VEB to be instantiated
  11628. * @vsi: the controlling VSI
  11629. **/
  11630. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  11631. {
  11632. struct i40e_pf *pf = veb->pf;
  11633. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  11634. int ret;
  11635. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  11636. veb->enabled_tc, false,
  11637. &veb->seid, enable_stats, NULL);
  11638. /* get a VEB from the hardware */
  11639. if (ret) {
  11640. dev_info(&pf->pdev->dev,
  11641. "couldn't add VEB, err %s aq_err %s\n",
  11642. i40e_stat_str(&pf->hw, ret),
  11643. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11644. return -EPERM;
  11645. }
  11646. /* get statistics counter */
  11647. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  11648. &veb->stats_idx, NULL, NULL, NULL);
  11649. if (ret) {
  11650. dev_info(&pf->pdev->dev,
  11651. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  11652. i40e_stat_str(&pf->hw, ret),
  11653. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11654. return -EPERM;
  11655. }
  11656. ret = i40e_veb_get_bw_info(veb);
  11657. if (ret) {
  11658. dev_info(&pf->pdev->dev,
  11659. "couldn't get VEB bw info, err %s aq_err %s\n",
  11660. i40e_stat_str(&pf->hw, ret),
  11661. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11662. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11663. return -ENOENT;
  11664. }
  11665. vsi->uplink_seid = veb->seid;
  11666. vsi->veb_idx = veb->idx;
  11667. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11668. return 0;
  11669. }
  11670. /**
  11671. * i40e_veb_setup - Set up a VEB
  11672. * @pf: board private structure
  11673. * @flags: VEB setup flags
  11674. * @uplink_seid: the switch element to link to
  11675. * @vsi_seid: the initial VSI seid
  11676. * @enabled_tc: Enabled TC bit-map
  11677. *
  11678. * This allocates the sw VEB structure and links it into the switch
  11679. * It is possible and legal for this to be a duplicate of an already
  11680. * existing VEB. It is also possible for both uplink and vsi seids
  11681. * to be zero, in order to create a floating VEB.
  11682. *
  11683. * Returns pointer to the successfully allocated VEB sw struct on
  11684. * success, otherwise returns NULL on failure.
  11685. **/
  11686. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  11687. u16 uplink_seid, u16 vsi_seid,
  11688. u8 enabled_tc)
  11689. {
  11690. struct i40e_veb *veb, *uplink_veb = NULL;
  11691. int vsi_idx, veb_idx;
  11692. int ret;
  11693. /* if one seid is 0, the other must be 0 to create a floating relay */
  11694. if ((uplink_seid == 0 || vsi_seid == 0) &&
  11695. (uplink_seid + vsi_seid != 0)) {
  11696. dev_info(&pf->pdev->dev,
  11697. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  11698. uplink_seid, vsi_seid);
  11699. return NULL;
  11700. }
  11701. /* make sure there is such a vsi and uplink */
  11702. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  11703. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  11704. break;
  11705. if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
  11706. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  11707. vsi_seid);
  11708. return NULL;
  11709. }
  11710. if (uplink_seid && uplink_seid != pf->mac_seid) {
  11711. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  11712. if (pf->veb[veb_idx] &&
  11713. pf->veb[veb_idx]->seid == uplink_seid) {
  11714. uplink_veb = pf->veb[veb_idx];
  11715. break;
  11716. }
  11717. }
  11718. if (!uplink_veb) {
  11719. dev_info(&pf->pdev->dev,
  11720. "uplink seid %d not found\n", uplink_seid);
  11721. return NULL;
  11722. }
  11723. }
  11724. /* get veb sw struct */
  11725. veb_idx = i40e_veb_mem_alloc(pf);
  11726. if (veb_idx < 0)
  11727. goto err_alloc;
  11728. veb = pf->veb[veb_idx];
  11729. veb->flags = flags;
  11730. veb->uplink_seid = uplink_seid;
  11731. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  11732. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  11733. /* create the VEB in the switch */
  11734. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  11735. if (ret)
  11736. goto err_veb;
  11737. if (vsi_idx == pf->lan_vsi)
  11738. pf->lan_veb = veb->idx;
  11739. return veb;
  11740. err_veb:
  11741. i40e_veb_clear(veb);
  11742. err_alloc:
  11743. return NULL;
  11744. }
  11745. /**
  11746. * i40e_setup_pf_switch_element - set PF vars based on switch type
  11747. * @pf: board private structure
  11748. * @ele: element we are building info from
  11749. * @num_reported: total number of elements
  11750. * @printconfig: should we print the contents
  11751. *
  11752. * helper function to assist in extracting a few useful SEID values.
  11753. **/
  11754. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  11755. struct i40e_aqc_switch_config_element_resp *ele,
  11756. u16 num_reported, bool printconfig)
  11757. {
  11758. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  11759. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  11760. u8 element_type = ele->element_type;
  11761. u16 seid = le16_to_cpu(ele->seid);
  11762. if (printconfig)
  11763. dev_info(&pf->pdev->dev,
  11764. "type=%d seid=%d uplink=%d downlink=%d\n",
  11765. element_type, seid, uplink_seid, downlink_seid);
  11766. switch (element_type) {
  11767. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  11768. pf->mac_seid = seid;
  11769. break;
  11770. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  11771. /* Main VEB? */
  11772. if (uplink_seid != pf->mac_seid)
  11773. break;
  11774. if (pf->lan_veb == I40E_NO_VEB) {
  11775. int v;
  11776. /* find existing or else empty VEB */
  11777. for (v = 0; v < I40E_MAX_VEB; v++) {
  11778. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  11779. pf->lan_veb = v;
  11780. break;
  11781. }
  11782. }
  11783. if (pf->lan_veb == I40E_NO_VEB) {
  11784. v = i40e_veb_mem_alloc(pf);
  11785. if (v < 0)
  11786. break;
  11787. pf->lan_veb = v;
  11788. }
  11789. }
  11790. pf->veb[pf->lan_veb]->seid = seid;
  11791. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  11792. pf->veb[pf->lan_veb]->pf = pf;
  11793. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  11794. break;
  11795. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  11796. if (num_reported != 1)
  11797. break;
  11798. /* This is immediately after a reset so we can assume this is
  11799. * the PF's VSI
  11800. */
  11801. pf->mac_seid = uplink_seid;
  11802. pf->pf_seid = downlink_seid;
  11803. pf->main_vsi_seid = seid;
  11804. if (printconfig)
  11805. dev_info(&pf->pdev->dev,
  11806. "pf_seid=%d main_vsi_seid=%d\n",
  11807. pf->pf_seid, pf->main_vsi_seid);
  11808. break;
  11809. case I40E_SWITCH_ELEMENT_TYPE_PF:
  11810. case I40E_SWITCH_ELEMENT_TYPE_VF:
  11811. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  11812. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  11813. case I40E_SWITCH_ELEMENT_TYPE_PE:
  11814. case I40E_SWITCH_ELEMENT_TYPE_PA:
  11815. /* ignore these for now */
  11816. break;
  11817. default:
  11818. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  11819. element_type, seid);
  11820. break;
  11821. }
  11822. }
  11823. /**
  11824. * i40e_fetch_switch_configuration - Get switch config from firmware
  11825. * @pf: board private structure
  11826. * @printconfig: should we print the contents
  11827. *
  11828. * Get the current switch configuration from the device and
  11829. * extract a few useful SEID values.
  11830. **/
  11831. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  11832. {
  11833. struct i40e_aqc_get_switch_config_resp *sw_config;
  11834. u16 next_seid = 0;
  11835. int ret = 0;
  11836. u8 *aq_buf;
  11837. int i;
  11838. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  11839. if (!aq_buf)
  11840. return -ENOMEM;
  11841. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  11842. do {
  11843. u16 num_reported, num_total;
  11844. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  11845. I40E_AQ_LARGE_BUF,
  11846. &next_seid, NULL);
  11847. if (ret) {
  11848. dev_info(&pf->pdev->dev,
  11849. "get switch config failed err %s aq_err %s\n",
  11850. i40e_stat_str(&pf->hw, ret),
  11851. i40e_aq_str(&pf->hw,
  11852. pf->hw.aq.asq_last_status));
  11853. kfree(aq_buf);
  11854. return -ENOENT;
  11855. }
  11856. num_reported = le16_to_cpu(sw_config->header.num_reported);
  11857. num_total = le16_to_cpu(sw_config->header.num_total);
  11858. if (printconfig)
  11859. dev_info(&pf->pdev->dev,
  11860. "header: %d reported %d total\n",
  11861. num_reported, num_total);
  11862. for (i = 0; i < num_reported; i++) {
  11863. struct i40e_aqc_switch_config_element_resp *ele =
  11864. &sw_config->element[i];
  11865. i40e_setup_pf_switch_element(pf, ele, num_reported,
  11866. printconfig);
  11867. }
  11868. } while (next_seid != 0);
  11869. kfree(aq_buf);
  11870. return ret;
  11871. }
  11872. /**
  11873. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  11874. * @pf: board private structure
  11875. * @reinit: if the Main VSI needs to re-initialized.
  11876. *
  11877. * Returns 0 on success, negative value on failure
  11878. **/
  11879. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  11880. {
  11881. u16 flags = 0;
  11882. int ret;
  11883. /* find out what's out there already */
  11884. ret = i40e_fetch_switch_configuration(pf, false);
  11885. if (ret) {
  11886. dev_info(&pf->pdev->dev,
  11887. "couldn't fetch switch config, err %s aq_err %s\n",
  11888. i40e_stat_str(&pf->hw, ret),
  11889. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11890. return ret;
  11891. }
  11892. i40e_pf_reset_stats(pf);
  11893. /* set the switch config bit for the whole device to
  11894. * support limited promisc or true promisc
  11895. * when user requests promisc. The default is limited
  11896. * promisc.
  11897. */
  11898. if ((pf->hw.pf_id == 0) &&
  11899. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
  11900. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11901. pf->last_sw_conf_flags = flags;
  11902. }
  11903. if (pf->hw.pf_id == 0) {
  11904. u16 valid_flags;
  11905. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11906. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
  11907. NULL);
  11908. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  11909. dev_info(&pf->pdev->dev,
  11910. "couldn't set switch config bits, err %s aq_err %s\n",
  11911. i40e_stat_str(&pf->hw, ret),
  11912. i40e_aq_str(&pf->hw,
  11913. pf->hw.aq.asq_last_status));
  11914. /* not a fatal problem, just keep going */
  11915. }
  11916. pf->last_sw_conf_valid_flags = valid_flags;
  11917. }
  11918. /* first time setup */
  11919. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  11920. struct i40e_vsi *vsi = NULL;
  11921. u16 uplink_seid;
  11922. /* Set up the PF VSI associated with the PF's main VSI
  11923. * that is already in the HW switch
  11924. */
  11925. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  11926. uplink_seid = pf->veb[pf->lan_veb]->seid;
  11927. else
  11928. uplink_seid = pf->mac_seid;
  11929. if (pf->lan_vsi == I40E_NO_VSI)
  11930. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  11931. else if (reinit)
  11932. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  11933. if (!vsi) {
  11934. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  11935. i40e_cloud_filter_exit(pf);
  11936. i40e_fdir_teardown(pf);
  11937. return -EAGAIN;
  11938. }
  11939. } else {
  11940. /* force a reset of TC and queue layout configurations */
  11941. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  11942. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  11943. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  11944. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  11945. }
  11946. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  11947. i40e_fdir_sb_setup(pf);
  11948. /* Setup static PF queue filter control settings */
  11949. ret = i40e_setup_pf_filter_control(pf);
  11950. if (ret) {
  11951. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  11952. ret);
  11953. /* Failure here should not stop continuing other steps */
  11954. }
  11955. /* enable RSS in the HW, even for only one queue, as the stack can use
  11956. * the hash
  11957. */
  11958. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  11959. i40e_pf_config_rss(pf);
  11960. /* fill in link information and enable LSE reporting */
  11961. i40e_link_event(pf);
  11962. /* Initialize user-specific link properties */
  11963. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  11964. I40E_AQ_AN_COMPLETED) ? true : false);
  11965. i40e_ptp_init(pf);
  11966. /* repopulate tunnel port filters */
  11967. i40e_sync_udp_filters(pf);
  11968. return ret;
  11969. }
  11970. /**
  11971. * i40e_determine_queue_usage - Work out queue distribution
  11972. * @pf: board private structure
  11973. **/
  11974. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  11975. {
  11976. int queues_left;
  11977. int q_max;
  11978. pf->num_lan_qps = 0;
  11979. /* Find the max queues to be put into basic use. We'll always be
  11980. * using TC0, whether or not DCB is running, and TC0 will get the
  11981. * big RSS set.
  11982. */
  11983. queues_left = pf->hw.func_caps.num_tx_qp;
  11984. if ((queues_left == 1) ||
  11985. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  11986. /* one qp for PF, no queues for anything else */
  11987. queues_left = 0;
  11988. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11989. /* make sure all the fancies are disabled */
  11990. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11991. I40E_FLAG_IWARP_ENABLED |
  11992. I40E_FLAG_FD_SB_ENABLED |
  11993. I40E_FLAG_FD_ATR_ENABLED |
  11994. I40E_FLAG_DCB_CAPABLE |
  11995. I40E_FLAG_DCB_ENABLED |
  11996. I40E_FLAG_SRIOV_ENABLED |
  11997. I40E_FLAG_VMDQ_ENABLED);
  11998. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11999. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  12000. I40E_FLAG_FD_SB_ENABLED |
  12001. I40E_FLAG_FD_ATR_ENABLED |
  12002. I40E_FLAG_DCB_CAPABLE))) {
  12003. /* one qp for PF */
  12004. pf->alloc_rss_size = pf->num_lan_qps = 1;
  12005. queues_left -= pf->num_lan_qps;
  12006. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  12007. I40E_FLAG_IWARP_ENABLED |
  12008. I40E_FLAG_FD_SB_ENABLED |
  12009. I40E_FLAG_FD_ATR_ENABLED |
  12010. I40E_FLAG_DCB_ENABLED |
  12011. I40E_FLAG_VMDQ_ENABLED);
  12012. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  12013. } else {
  12014. /* Not enough queues for all TCs */
  12015. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  12016. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  12017. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  12018. I40E_FLAG_DCB_ENABLED);
  12019. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  12020. }
  12021. /* limit lan qps to the smaller of qps, cpus or msix */
  12022. q_max = max_t(int, pf->rss_size_max, num_online_cpus());
  12023. q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
  12024. q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
  12025. pf->num_lan_qps = q_max;
  12026. queues_left -= pf->num_lan_qps;
  12027. }
  12028. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  12029. if (queues_left > 1) {
  12030. queues_left -= 1; /* save 1 queue for FD */
  12031. } else {
  12032. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  12033. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  12034. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  12035. }
  12036. }
  12037. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12038. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  12039. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  12040. (queues_left / pf->num_vf_qps));
  12041. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  12042. }
  12043. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  12044. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  12045. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  12046. (queues_left / pf->num_vmdq_qps));
  12047. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  12048. }
  12049. pf->queues_left = queues_left;
  12050. dev_dbg(&pf->pdev->dev,
  12051. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  12052. pf->hw.func_caps.num_tx_qp,
  12053. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  12054. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  12055. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  12056. queues_left);
  12057. }
  12058. /**
  12059. * i40e_setup_pf_filter_control - Setup PF static filter control
  12060. * @pf: PF to be setup
  12061. *
  12062. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  12063. * settings. If PE/FCoE are enabled then it will also set the per PF
  12064. * based filter sizes required for them. It also enables Flow director,
  12065. * ethertype and macvlan type filter settings for the pf.
  12066. *
  12067. * Returns 0 on success, negative on failure
  12068. **/
  12069. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  12070. {
  12071. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  12072. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  12073. /* Flow Director is enabled */
  12074. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  12075. settings->enable_fdir = true;
  12076. /* Ethtype and MACVLAN filters enabled for PF */
  12077. settings->enable_ethtype = true;
  12078. settings->enable_macvlan = true;
  12079. if (i40e_set_filter_control(&pf->hw, settings))
  12080. return -ENOENT;
  12081. return 0;
  12082. }
  12083. #define INFO_STRING_LEN 255
  12084. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  12085. static void i40e_print_features(struct i40e_pf *pf)
  12086. {
  12087. struct i40e_hw *hw = &pf->hw;
  12088. char *buf;
  12089. int i;
  12090. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  12091. if (!buf)
  12092. return;
  12093. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  12094. #ifdef CONFIG_PCI_IOV
  12095. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  12096. #endif
  12097. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  12098. pf->hw.func_caps.num_vsis,
  12099. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  12100. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  12101. i += snprintf(&buf[i], REMAIN(i), " RSS");
  12102. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  12103. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  12104. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  12105. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  12106. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  12107. }
  12108. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  12109. i += snprintf(&buf[i], REMAIN(i), " DCB");
  12110. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  12111. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  12112. if (pf->flags & I40E_FLAG_PTP)
  12113. i += snprintf(&buf[i], REMAIN(i), " PTP");
  12114. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  12115. i += snprintf(&buf[i], REMAIN(i), " VEB");
  12116. else
  12117. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  12118. dev_info(&pf->pdev->dev, "%s\n", buf);
  12119. kfree(buf);
  12120. WARN_ON(i > INFO_STRING_LEN);
  12121. }
  12122. /**
  12123. * i40e_get_platform_mac_addr - get platform-specific MAC address
  12124. * @pdev: PCI device information struct
  12125. * @pf: board private structure
  12126. *
  12127. * Look up the MAC address for the device. First we'll try
  12128. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  12129. * specific fallback. Otherwise, we'll default to the stored value in
  12130. * firmware.
  12131. **/
  12132. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  12133. {
  12134. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  12135. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  12136. }
  12137. /**
  12138. * i40e_probe - Device initialization routine
  12139. * @pdev: PCI device information struct
  12140. * @ent: entry in i40e_pci_tbl
  12141. *
  12142. * i40e_probe initializes a PF identified by a pci_dev structure.
  12143. * The OS initialization, configuring of the PF private structure,
  12144. * and a hardware reset occur.
  12145. *
  12146. * Returns 0 on success, negative on failure
  12147. **/
  12148. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  12149. {
  12150. struct i40e_aq_get_phy_abilities_resp abilities;
  12151. struct i40e_pf *pf;
  12152. struct i40e_hw *hw;
  12153. static u16 pfs_found;
  12154. u16 wol_nvm_bits;
  12155. u16 link_status;
  12156. int err;
  12157. u32 val;
  12158. u32 i;
  12159. u8 set_fc_aq_fail;
  12160. err = pci_enable_device_mem(pdev);
  12161. if (err)
  12162. return err;
  12163. /* set up for high or low dma */
  12164. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  12165. if (err) {
  12166. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  12167. if (err) {
  12168. dev_err(&pdev->dev,
  12169. "DMA configuration failed: 0x%x\n", err);
  12170. goto err_dma;
  12171. }
  12172. }
  12173. /* set up pci connections */
  12174. err = pci_request_mem_regions(pdev, i40e_driver_name);
  12175. if (err) {
  12176. dev_info(&pdev->dev,
  12177. "pci_request_selected_regions failed %d\n", err);
  12178. goto err_pci_reg;
  12179. }
  12180. pci_enable_pcie_error_reporting(pdev);
  12181. pci_set_master(pdev);
  12182. /* Now that we have a PCI connection, we need to do the
  12183. * low level device setup. This is primarily setting up
  12184. * the Admin Queue structures and then querying for the
  12185. * device's current profile information.
  12186. */
  12187. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  12188. if (!pf) {
  12189. err = -ENOMEM;
  12190. goto err_pf_alloc;
  12191. }
  12192. pf->next_vsi = 0;
  12193. pf->pdev = pdev;
  12194. set_bit(__I40E_DOWN, pf->state);
  12195. hw = &pf->hw;
  12196. hw->back = pf;
  12197. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  12198. I40E_MAX_CSR_SPACE);
  12199. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  12200. if (!hw->hw_addr) {
  12201. err = -EIO;
  12202. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  12203. (unsigned int)pci_resource_start(pdev, 0),
  12204. pf->ioremap_len, err);
  12205. goto err_ioremap;
  12206. }
  12207. hw->vendor_id = pdev->vendor;
  12208. hw->device_id = pdev->device;
  12209. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  12210. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  12211. hw->subsystem_device_id = pdev->subsystem_device;
  12212. hw->bus.device = PCI_SLOT(pdev->devfn);
  12213. hw->bus.func = PCI_FUNC(pdev->devfn);
  12214. hw->bus.bus_id = pdev->bus->number;
  12215. pf->instance = pfs_found;
  12216. /* Select something other than the 802.1ad ethertype for the
  12217. * switch to use internally and drop on ingress.
  12218. */
  12219. hw->switch_tag = 0xffff;
  12220. hw->first_tag = ETH_P_8021AD;
  12221. hw->second_tag = ETH_P_8021Q;
  12222. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  12223. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  12224. /* set up the locks for the AQ, do this only once in probe
  12225. * and destroy them only once in remove
  12226. */
  12227. mutex_init(&hw->aq.asq_mutex);
  12228. mutex_init(&hw->aq.arq_mutex);
  12229. pf->msg_enable = netif_msg_init(debug,
  12230. NETIF_MSG_DRV |
  12231. NETIF_MSG_PROBE |
  12232. NETIF_MSG_LINK);
  12233. if (debug < -1)
  12234. pf->hw.debug_mask = debug;
  12235. /* do a special CORER for clearing PXE mode once at init */
  12236. if (hw->revision_id == 0 &&
  12237. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  12238. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  12239. i40e_flush(hw);
  12240. msleep(200);
  12241. pf->corer_count++;
  12242. i40e_clear_pxe_mode(hw);
  12243. }
  12244. /* Reset here to make sure all is clean and to define PF 'n' */
  12245. i40e_clear_hw(hw);
  12246. err = i40e_pf_reset(hw);
  12247. if (err) {
  12248. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  12249. goto err_pf_reset;
  12250. }
  12251. pf->pfr_count++;
  12252. hw->aq.num_arq_entries = I40E_AQ_LEN;
  12253. hw->aq.num_asq_entries = I40E_AQ_LEN;
  12254. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  12255. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  12256. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  12257. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  12258. "%s-%s:misc",
  12259. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  12260. err = i40e_init_shared_code(hw);
  12261. if (err) {
  12262. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  12263. err);
  12264. goto err_pf_reset;
  12265. }
  12266. /* set up a default setting for link flow control */
  12267. pf->hw.fc.requested_mode = I40E_FC_NONE;
  12268. err = i40e_init_adminq(hw);
  12269. if (err) {
  12270. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  12271. dev_info(&pdev->dev,
  12272. "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");
  12273. else
  12274. dev_info(&pdev->dev,
  12275. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  12276. goto err_pf_reset;
  12277. }
  12278. i40e_get_oem_version(hw);
  12279. /* provide nvm, fw, api versions */
  12280. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  12281. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  12282. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  12283. i40e_nvm_version_str(hw));
  12284. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  12285. hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
  12286. dev_info(&pdev->dev,
  12287. "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");
  12288. else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
  12289. dev_info(&pdev->dev,
  12290. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  12291. i40e_verify_eeprom(pf);
  12292. /* Rev 0 hardware was never productized */
  12293. if (hw->revision_id < 1)
  12294. 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");
  12295. i40e_clear_pxe_mode(hw);
  12296. err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  12297. if (err)
  12298. goto err_adminq_setup;
  12299. err = i40e_sw_init(pf);
  12300. if (err) {
  12301. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  12302. goto err_sw_init;
  12303. }
  12304. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  12305. hw->func_caps.num_rx_qp, 0, 0);
  12306. if (err) {
  12307. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  12308. goto err_init_lan_hmc;
  12309. }
  12310. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  12311. if (err) {
  12312. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  12313. err = -ENOENT;
  12314. goto err_configure_lan_hmc;
  12315. }
  12316. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  12317. * Ignore error return codes because if it was already disabled via
  12318. * hardware settings this will fail
  12319. */
  12320. if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
  12321. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  12322. i40e_aq_stop_lldp(hw, true, NULL);
  12323. }
  12324. /* allow a platform config to override the HW addr */
  12325. i40e_get_platform_mac_addr(pdev, pf);
  12326. if (!is_valid_ether_addr(hw->mac.addr)) {
  12327. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  12328. err = -EIO;
  12329. goto err_mac_addr;
  12330. }
  12331. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  12332. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  12333. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  12334. if (is_valid_ether_addr(hw->mac.port_addr))
  12335. pf->hw_features |= I40E_HW_PORT_ID_VALID;
  12336. pci_set_drvdata(pdev, pf);
  12337. pci_save_state(pdev);
  12338. /* Enable FW to write default DCB config on link-up */
  12339. i40e_aq_set_dcb_parameters(hw, true, NULL);
  12340. #ifdef CONFIG_I40E_DCB
  12341. err = i40e_init_pf_dcb(pf);
  12342. if (err) {
  12343. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  12344. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  12345. /* Continue without DCB enabled */
  12346. }
  12347. #endif /* CONFIG_I40E_DCB */
  12348. /* set up periodic task facility */
  12349. timer_setup(&pf->service_timer, i40e_service_timer, 0);
  12350. pf->service_timer_period = HZ;
  12351. INIT_WORK(&pf->service_task, i40e_service_task);
  12352. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  12353. /* NVM bit on means WoL disabled for the port */
  12354. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  12355. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  12356. pf->wol_en = false;
  12357. else
  12358. pf->wol_en = true;
  12359. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  12360. /* set up the main switch operations */
  12361. i40e_determine_queue_usage(pf);
  12362. err = i40e_init_interrupt_scheme(pf);
  12363. if (err)
  12364. goto err_switch_setup;
  12365. /* The number of VSIs reported by the FW is the minimum guaranteed
  12366. * to us; HW supports far more and we share the remaining pool with
  12367. * the other PFs. We allocate space for more than the guarantee with
  12368. * the understanding that we might not get them all later.
  12369. */
  12370. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  12371. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  12372. else
  12373. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  12374. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  12375. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  12376. GFP_KERNEL);
  12377. if (!pf->vsi) {
  12378. err = -ENOMEM;
  12379. goto err_switch_setup;
  12380. }
  12381. #ifdef CONFIG_PCI_IOV
  12382. /* prep for VF support */
  12383. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12384. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12385. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12386. if (pci_num_vf(pdev))
  12387. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  12388. }
  12389. #endif
  12390. err = i40e_setup_pf_switch(pf, false);
  12391. if (err) {
  12392. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  12393. goto err_vsis;
  12394. }
  12395. INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
  12396. /* Make sure flow control is set according to current settings */
  12397. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  12398. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  12399. dev_dbg(&pf->pdev->dev,
  12400. "Set fc with err %s aq_err %s on get_phy_cap\n",
  12401. i40e_stat_str(hw, err),
  12402. i40e_aq_str(hw, hw->aq.asq_last_status));
  12403. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  12404. dev_dbg(&pf->pdev->dev,
  12405. "Set fc with err %s aq_err %s on set_phy_config\n",
  12406. i40e_stat_str(hw, err),
  12407. i40e_aq_str(hw, hw->aq.asq_last_status));
  12408. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  12409. dev_dbg(&pf->pdev->dev,
  12410. "Set fc with err %s aq_err %s on get_link_info\n",
  12411. i40e_stat_str(hw, err),
  12412. i40e_aq_str(hw, hw->aq.asq_last_status));
  12413. /* if FDIR VSI was set up, start it now */
  12414. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12415. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  12416. i40e_vsi_open(pf->vsi[i]);
  12417. break;
  12418. }
  12419. }
  12420. /* The driver only wants link up/down and module qualification
  12421. * reports from firmware. Note the negative logic.
  12422. */
  12423. err = i40e_aq_set_phy_int_mask(&pf->hw,
  12424. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  12425. I40E_AQ_EVENT_MEDIA_NA |
  12426. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  12427. if (err)
  12428. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  12429. i40e_stat_str(&pf->hw, err),
  12430. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12431. /* Reconfigure hardware for allowing smaller MSS in the case
  12432. * of TSO, so that we avoid the MDD being fired and causing
  12433. * a reset in the case of small MSS+TSO.
  12434. */
  12435. val = rd32(hw, I40E_REG_MSS);
  12436. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  12437. val &= ~I40E_REG_MSS_MIN_MASK;
  12438. val |= I40E_64BYTE_MSS;
  12439. wr32(hw, I40E_REG_MSS, val);
  12440. }
  12441. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  12442. msleep(75);
  12443. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  12444. if (err)
  12445. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  12446. i40e_stat_str(&pf->hw, err),
  12447. i40e_aq_str(&pf->hw,
  12448. pf->hw.aq.asq_last_status));
  12449. }
  12450. /* The main driver is (mostly) up and happy. We need to set this state
  12451. * before setting up the misc vector or we get a race and the vector
  12452. * ends up disabled forever.
  12453. */
  12454. clear_bit(__I40E_DOWN, pf->state);
  12455. /* In case of MSIX we are going to setup the misc vector right here
  12456. * to handle admin queue events etc. In case of legacy and MSI
  12457. * the misc functionality and queue processing is combined in
  12458. * the same vector and that gets setup at open.
  12459. */
  12460. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  12461. err = i40e_setup_misc_vector(pf);
  12462. if (err) {
  12463. dev_info(&pdev->dev,
  12464. "setup of misc vector failed: %d\n", err);
  12465. goto err_vsis;
  12466. }
  12467. }
  12468. #ifdef CONFIG_PCI_IOV
  12469. /* prep for VF support */
  12470. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12471. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12472. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12473. /* disable link interrupts for VFs */
  12474. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  12475. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  12476. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  12477. i40e_flush(hw);
  12478. if (pci_num_vf(pdev)) {
  12479. dev_info(&pdev->dev,
  12480. "Active VFs found, allocating resources.\n");
  12481. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  12482. if (err)
  12483. dev_info(&pdev->dev,
  12484. "Error %d allocating resources for existing VFs\n",
  12485. err);
  12486. }
  12487. }
  12488. #endif /* CONFIG_PCI_IOV */
  12489. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12490. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  12491. pf->num_iwarp_msix,
  12492. I40E_IWARP_IRQ_PILE_ID);
  12493. if (pf->iwarp_base_vector < 0) {
  12494. dev_info(&pdev->dev,
  12495. "failed to get tracking for %d vectors for IWARP err=%d\n",
  12496. pf->num_iwarp_msix, pf->iwarp_base_vector);
  12497. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  12498. }
  12499. }
  12500. i40e_dbg_pf_init(pf);
  12501. /* tell the firmware that we're starting */
  12502. i40e_send_version(pf);
  12503. /* since everything's happy, start the service_task timer */
  12504. mod_timer(&pf->service_timer,
  12505. round_jiffies(jiffies + pf->service_timer_period));
  12506. /* add this PF to client device list and launch a client service task */
  12507. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12508. err = i40e_lan_add_device(pf);
  12509. if (err)
  12510. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  12511. err);
  12512. }
  12513. #define PCI_SPEED_SIZE 8
  12514. #define PCI_WIDTH_SIZE 8
  12515. /* Devices on the IOSF bus do not have this information
  12516. * and will report PCI Gen 1 x 1 by default so don't bother
  12517. * checking them.
  12518. */
  12519. if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
  12520. char speed[PCI_SPEED_SIZE] = "Unknown";
  12521. char width[PCI_WIDTH_SIZE] = "Unknown";
  12522. /* Get the negotiated link width and speed from PCI config
  12523. * space
  12524. */
  12525. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  12526. &link_status);
  12527. i40e_set_pci_config_data(hw, link_status);
  12528. switch (hw->bus.speed) {
  12529. case i40e_bus_speed_8000:
  12530. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  12531. case i40e_bus_speed_5000:
  12532. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  12533. case i40e_bus_speed_2500:
  12534. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  12535. default:
  12536. break;
  12537. }
  12538. switch (hw->bus.width) {
  12539. case i40e_bus_width_pcie_x8:
  12540. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  12541. case i40e_bus_width_pcie_x4:
  12542. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  12543. case i40e_bus_width_pcie_x2:
  12544. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  12545. case i40e_bus_width_pcie_x1:
  12546. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  12547. default:
  12548. break;
  12549. }
  12550. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  12551. speed, width);
  12552. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  12553. hw->bus.speed < i40e_bus_speed_8000) {
  12554. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  12555. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  12556. }
  12557. }
  12558. /* get the requested speeds from the fw */
  12559. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  12560. if (err)
  12561. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  12562. i40e_stat_str(&pf->hw, err),
  12563. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12564. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  12565. /* get the supported phy types from the fw */
  12566. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  12567. if (err)
  12568. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  12569. i40e_stat_str(&pf->hw, err),
  12570. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12571. /* Add a filter to drop all Flow control frames from any VSI from being
  12572. * transmitted. By doing so we stop a malicious VF from sending out
  12573. * PAUSE or PFC frames and potentially controlling traffic for other
  12574. * PF/VF VSIs.
  12575. * The FW can still send Flow control frames if enabled.
  12576. */
  12577. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  12578. pf->main_vsi_seid);
  12579. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  12580. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  12581. pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
  12582. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  12583. pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
  12584. /* print a string summarizing features */
  12585. i40e_print_features(pf);
  12586. return 0;
  12587. /* Unwind what we've done if something failed in the setup */
  12588. err_vsis:
  12589. set_bit(__I40E_DOWN, pf->state);
  12590. i40e_clear_interrupt_scheme(pf);
  12591. kfree(pf->vsi);
  12592. err_switch_setup:
  12593. i40e_reset_interrupt_capability(pf);
  12594. del_timer_sync(&pf->service_timer);
  12595. err_mac_addr:
  12596. err_configure_lan_hmc:
  12597. (void)i40e_shutdown_lan_hmc(hw);
  12598. err_init_lan_hmc:
  12599. kfree(pf->qp_pile);
  12600. err_sw_init:
  12601. err_adminq_setup:
  12602. err_pf_reset:
  12603. iounmap(hw->hw_addr);
  12604. err_ioremap:
  12605. kfree(pf);
  12606. err_pf_alloc:
  12607. pci_disable_pcie_error_reporting(pdev);
  12608. pci_release_mem_regions(pdev);
  12609. err_pci_reg:
  12610. err_dma:
  12611. pci_disable_device(pdev);
  12612. return err;
  12613. }
  12614. /**
  12615. * i40e_remove - Device removal routine
  12616. * @pdev: PCI device information struct
  12617. *
  12618. * i40e_remove is called by the PCI subsystem to alert the driver
  12619. * that is should release a PCI device. This could be caused by a
  12620. * Hot-Plug event, or because the driver is going to be removed from
  12621. * memory.
  12622. **/
  12623. static void i40e_remove(struct pci_dev *pdev)
  12624. {
  12625. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12626. struct i40e_hw *hw = &pf->hw;
  12627. i40e_status ret_code;
  12628. int i;
  12629. i40e_dbg_pf_exit(pf);
  12630. i40e_ptp_stop(pf);
  12631. /* Disable RSS in hw */
  12632. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  12633. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  12634. /* no more scheduling of any task */
  12635. set_bit(__I40E_SUSPENDED, pf->state);
  12636. set_bit(__I40E_DOWN, pf->state);
  12637. if (pf->service_timer.function)
  12638. del_timer_sync(&pf->service_timer);
  12639. if (pf->service_task.func)
  12640. cancel_work_sync(&pf->service_task);
  12641. /* Client close must be called explicitly here because the timer
  12642. * has been stopped.
  12643. */
  12644. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12645. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  12646. i40e_free_vfs(pf);
  12647. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  12648. }
  12649. i40e_fdir_teardown(pf);
  12650. /* If there is a switch structure or any orphans, remove them.
  12651. * This will leave only the PF's VSI remaining.
  12652. */
  12653. for (i = 0; i < I40E_MAX_VEB; i++) {
  12654. if (!pf->veb[i])
  12655. continue;
  12656. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  12657. pf->veb[i]->uplink_seid == 0)
  12658. i40e_switch_branch_release(pf->veb[i]);
  12659. }
  12660. /* Now we can shutdown the PF's VSI, just before we kill
  12661. * adminq and hmc.
  12662. */
  12663. if (pf->vsi[pf->lan_vsi])
  12664. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  12665. i40e_cloud_filter_exit(pf);
  12666. /* remove attached clients */
  12667. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12668. ret_code = i40e_lan_del_device(pf);
  12669. if (ret_code)
  12670. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  12671. ret_code);
  12672. }
  12673. /* shutdown and destroy the HMC */
  12674. if (hw->hmc.hmc_obj) {
  12675. ret_code = i40e_shutdown_lan_hmc(hw);
  12676. if (ret_code)
  12677. dev_warn(&pdev->dev,
  12678. "Failed to destroy the HMC resources: %d\n",
  12679. ret_code);
  12680. }
  12681. /* shutdown the adminq */
  12682. i40e_shutdown_adminq(hw);
  12683. /* destroy the locks only once, here */
  12684. mutex_destroy(&hw->aq.arq_mutex);
  12685. mutex_destroy(&hw->aq.asq_mutex);
  12686. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  12687. rtnl_lock();
  12688. i40e_clear_interrupt_scheme(pf);
  12689. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12690. if (pf->vsi[i]) {
  12691. i40e_vsi_clear_rings(pf->vsi[i]);
  12692. i40e_vsi_clear(pf->vsi[i]);
  12693. pf->vsi[i] = NULL;
  12694. }
  12695. }
  12696. rtnl_unlock();
  12697. for (i = 0; i < I40E_MAX_VEB; i++) {
  12698. kfree(pf->veb[i]);
  12699. pf->veb[i] = NULL;
  12700. }
  12701. kfree(pf->qp_pile);
  12702. kfree(pf->vsi);
  12703. iounmap(hw->hw_addr);
  12704. kfree(pf);
  12705. pci_release_mem_regions(pdev);
  12706. pci_disable_pcie_error_reporting(pdev);
  12707. pci_disable_device(pdev);
  12708. }
  12709. /**
  12710. * i40e_pci_error_detected - warning that something funky happened in PCI land
  12711. * @pdev: PCI device information struct
  12712. * @error: the type of PCI error
  12713. *
  12714. * Called to warn that something happened and the error handling steps
  12715. * are in progress. Allows the driver to quiesce things, be ready for
  12716. * remediation.
  12717. **/
  12718. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  12719. enum pci_channel_state error)
  12720. {
  12721. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12722. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  12723. if (!pf) {
  12724. dev_info(&pdev->dev,
  12725. "Cannot recover - error happened during device probe\n");
  12726. return PCI_ERS_RESULT_DISCONNECT;
  12727. }
  12728. /* shutdown all operations */
  12729. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12730. i40e_prep_for_reset(pf, false);
  12731. /* Request a slot reset */
  12732. return PCI_ERS_RESULT_NEED_RESET;
  12733. }
  12734. /**
  12735. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  12736. * @pdev: PCI device information struct
  12737. *
  12738. * Called to find if the driver can work with the device now that
  12739. * the pci slot has been reset. If a basic connection seems good
  12740. * (registers are readable and have sane content) then return a
  12741. * happy little PCI_ERS_RESULT_xxx.
  12742. **/
  12743. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  12744. {
  12745. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12746. pci_ers_result_t result;
  12747. int err;
  12748. u32 reg;
  12749. dev_dbg(&pdev->dev, "%s\n", __func__);
  12750. if (pci_enable_device_mem(pdev)) {
  12751. dev_info(&pdev->dev,
  12752. "Cannot re-enable PCI device after reset.\n");
  12753. result = PCI_ERS_RESULT_DISCONNECT;
  12754. } else {
  12755. pci_set_master(pdev);
  12756. pci_restore_state(pdev);
  12757. pci_save_state(pdev);
  12758. pci_wake_from_d3(pdev, false);
  12759. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  12760. if (reg == 0)
  12761. result = PCI_ERS_RESULT_RECOVERED;
  12762. else
  12763. result = PCI_ERS_RESULT_DISCONNECT;
  12764. }
  12765. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  12766. if (err) {
  12767. dev_info(&pdev->dev,
  12768. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  12769. err);
  12770. /* non-fatal, continue */
  12771. }
  12772. return result;
  12773. }
  12774. /**
  12775. * i40e_pci_error_reset_prepare - prepare device driver for pci reset
  12776. * @pdev: PCI device information struct
  12777. */
  12778. static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
  12779. {
  12780. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12781. i40e_prep_for_reset(pf, false);
  12782. }
  12783. /**
  12784. * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
  12785. * @pdev: PCI device information struct
  12786. */
  12787. static void i40e_pci_error_reset_done(struct pci_dev *pdev)
  12788. {
  12789. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12790. i40e_reset_and_rebuild(pf, false, false);
  12791. }
  12792. /**
  12793. * i40e_pci_error_resume - restart operations after PCI error recovery
  12794. * @pdev: PCI device information struct
  12795. *
  12796. * Called to allow the driver to bring things back up after PCI error
  12797. * and/or reset recovery has finished.
  12798. **/
  12799. static void i40e_pci_error_resume(struct pci_dev *pdev)
  12800. {
  12801. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12802. dev_dbg(&pdev->dev, "%s\n", __func__);
  12803. if (test_bit(__I40E_SUSPENDED, pf->state))
  12804. return;
  12805. i40e_handle_reset_warning(pf, false);
  12806. }
  12807. /**
  12808. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  12809. * using the mac_address_write admin q function
  12810. * @pf: pointer to i40e_pf struct
  12811. **/
  12812. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  12813. {
  12814. struct i40e_hw *hw = &pf->hw;
  12815. i40e_status ret;
  12816. u8 mac_addr[6];
  12817. u16 flags = 0;
  12818. /* Get current MAC address in case it's an LAA */
  12819. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  12820. ether_addr_copy(mac_addr,
  12821. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  12822. } else {
  12823. dev_err(&pf->pdev->dev,
  12824. "Failed to retrieve MAC address; using default\n");
  12825. ether_addr_copy(mac_addr, hw->mac.addr);
  12826. }
  12827. /* The FW expects the mac address write cmd to first be called with
  12828. * one of these flags before calling it again with the multicast
  12829. * enable flags.
  12830. */
  12831. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  12832. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  12833. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  12834. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12835. if (ret) {
  12836. dev_err(&pf->pdev->dev,
  12837. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  12838. return;
  12839. }
  12840. flags = I40E_AQC_MC_MAG_EN
  12841. | I40E_AQC_WOL_PRESERVE_ON_PFR
  12842. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  12843. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12844. if (ret)
  12845. dev_err(&pf->pdev->dev,
  12846. "Failed to enable Multicast Magic Packet wake up\n");
  12847. }
  12848. /**
  12849. * i40e_shutdown - PCI callback for shutting down
  12850. * @pdev: PCI device information struct
  12851. **/
  12852. static void i40e_shutdown(struct pci_dev *pdev)
  12853. {
  12854. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12855. struct i40e_hw *hw = &pf->hw;
  12856. set_bit(__I40E_SUSPENDED, pf->state);
  12857. set_bit(__I40E_DOWN, pf->state);
  12858. del_timer_sync(&pf->service_timer);
  12859. cancel_work_sync(&pf->service_task);
  12860. i40e_cloud_filter_exit(pf);
  12861. i40e_fdir_teardown(pf);
  12862. /* Client close must be called explicitly here because the timer
  12863. * has been stopped.
  12864. */
  12865. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12866. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12867. i40e_enable_mc_magic_wake(pf);
  12868. i40e_prep_for_reset(pf, false);
  12869. wr32(hw, I40E_PFPM_APM,
  12870. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12871. wr32(hw, I40E_PFPM_WUFC,
  12872. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12873. /* Since we're going to destroy queues during the
  12874. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  12875. * whole section
  12876. */
  12877. rtnl_lock();
  12878. i40e_clear_interrupt_scheme(pf);
  12879. rtnl_unlock();
  12880. if (system_state == SYSTEM_POWER_OFF) {
  12881. pci_wake_from_d3(pdev, pf->wol_en);
  12882. pci_set_power_state(pdev, PCI_D3hot);
  12883. }
  12884. }
  12885. /**
  12886. * i40e_suspend - PM callback for moving to D3
  12887. * @dev: generic device information structure
  12888. **/
  12889. static int __maybe_unused i40e_suspend(struct device *dev)
  12890. {
  12891. struct pci_dev *pdev = to_pci_dev(dev);
  12892. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12893. struct i40e_hw *hw = &pf->hw;
  12894. /* If we're already suspended, then there is nothing to do */
  12895. if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
  12896. return 0;
  12897. set_bit(__I40E_DOWN, pf->state);
  12898. /* Ensure service task will not be running */
  12899. del_timer_sync(&pf->service_timer);
  12900. cancel_work_sync(&pf->service_task);
  12901. /* Client close must be called explicitly here because the timer
  12902. * has been stopped.
  12903. */
  12904. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12905. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12906. i40e_enable_mc_magic_wake(pf);
  12907. /* Since we're going to destroy queues during the
  12908. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  12909. * whole section
  12910. */
  12911. rtnl_lock();
  12912. i40e_prep_for_reset(pf, true);
  12913. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12914. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12915. /* Clear the interrupt scheme and release our IRQs so that the system
  12916. * can safely hibernate even when there are a large number of CPUs.
  12917. * Otherwise hibernation might fail when mapping all the vectors back
  12918. * to CPU0.
  12919. */
  12920. i40e_clear_interrupt_scheme(pf);
  12921. rtnl_unlock();
  12922. return 0;
  12923. }
  12924. /**
  12925. * i40e_resume - PM callback for waking up from D3
  12926. * @dev: generic device information structure
  12927. **/
  12928. static int __maybe_unused i40e_resume(struct device *dev)
  12929. {
  12930. struct pci_dev *pdev = to_pci_dev(dev);
  12931. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12932. int err;
  12933. /* If we're not suspended, then there is nothing to do */
  12934. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12935. return 0;
  12936. /* We need to hold the RTNL lock prior to restoring interrupt schemes,
  12937. * since we're going to be restoring queues
  12938. */
  12939. rtnl_lock();
  12940. /* We cleared the interrupt scheme when we suspended, so we need to
  12941. * restore it now to resume device functionality.
  12942. */
  12943. err = i40e_restore_interrupt_scheme(pf);
  12944. if (err) {
  12945. dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
  12946. err);
  12947. }
  12948. clear_bit(__I40E_DOWN, pf->state);
  12949. i40e_reset_and_rebuild(pf, false, true);
  12950. rtnl_unlock();
  12951. /* Clear suspended state last after everything is recovered */
  12952. clear_bit(__I40E_SUSPENDED, pf->state);
  12953. /* Restart the service task */
  12954. mod_timer(&pf->service_timer,
  12955. round_jiffies(jiffies + pf->service_timer_period));
  12956. return 0;
  12957. }
  12958. static const struct pci_error_handlers i40e_err_handler = {
  12959. .error_detected = i40e_pci_error_detected,
  12960. .slot_reset = i40e_pci_error_slot_reset,
  12961. .reset_prepare = i40e_pci_error_reset_prepare,
  12962. .reset_done = i40e_pci_error_reset_done,
  12963. .resume = i40e_pci_error_resume,
  12964. };
  12965. static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
  12966. static struct pci_driver i40e_driver = {
  12967. .name = i40e_driver_name,
  12968. .id_table = i40e_pci_tbl,
  12969. .probe = i40e_probe,
  12970. .remove = i40e_remove,
  12971. .driver = {
  12972. .pm = &i40e_pm_ops,
  12973. },
  12974. .shutdown = i40e_shutdown,
  12975. .err_handler = &i40e_err_handler,
  12976. .sriov_configure = i40e_pci_sriov_configure,
  12977. };
  12978. /**
  12979. * i40e_init_module - Driver registration routine
  12980. *
  12981. * i40e_init_module is the first routine called when the driver is
  12982. * loaded. All it does is register with the PCI subsystem.
  12983. **/
  12984. static int __init i40e_init_module(void)
  12985. {
  12986. pr_info("%s: %s - version %s\n", i40e_driver_name,
  12987. i40e_driver_string, i40e_driver_version_str);
  12988. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  12989. /* There is no need to throttle the number of active tasks because
  12990. * each device limits its own task using a state bit for scheduling
  12991. * the service task, and the device tasks do not interfere with each
  12992. * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
  12993. * since we need to be able to guarantee forward progress even under
  12994. * memory pressure.
  12995. */
  12996. i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
  12997. if (!i40e_wq) {
  12998. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  12999. return -ENOMEM;
  13000. }
  13001. i40e_dbg_init();
  13002. return pci_register_driver(&i40e_driver);
  13003. }
  13004. module_init(i40e_init_module);
  13005. /**
  13006. * i40e_exit_module - Driver exit cleanup routine
  13007. *
  13008. * i40e_exit_module is called just before the driver is removed
  13009. * from memory.
  13010. **/
  13011. static void __exit i40e_exit_module(void)
  13012. {
  13013. pci_unregister_driver(&i40e_driver);
  13014. destroy_workqueue(i40e_wq);
  13015. i40e_dbg_exit();
  13016. }
  13017. module_exit(i40e_exit_module);