i40e_main.c 330 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include <linux/etherdevice.h>
  27. #include <linux/of_net.h>
  28. #include <linux/pci.h>
  29. /* Local includes */
  30. #include "i40e.h"
  31. #include "i40e_diag.h"
  32. #include <net/udp_tunnel.h>
  33. const char i40e_driver_name[] = "i40e";
  34. static const char i40e_driver_string[] =
  35. "Intel(R) Ethernet Connection XL710 Network Driver";
  36. #define DRV_KERN "-k"
  37. #define DRV_VERSION_MAJOR 1
  38. #define DRV_VERSION_MINOR 6
  39. #define DRV_VERSION_BUILD 27
  40. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  41. __stringify(DRV_VERSION_MINOR) "." \
  42. __stringify(DRV_VERSION_BUILD) DRV_KERN
  43. const char i40e_driver_version_str[] = DRV_VERSION;
  44. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  45. /* a bit of forward declarations */
  46. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  47. static void i40e_handle_reset_warning(struct i40e_pf *pf);
  48. static int i40e_add_vsi(struct i40e_vsi *vsi);
  49. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  50. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  51. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  52. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  53. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  54. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  55. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  56. /* i40e_pci_tbl - PCI Device ID Table
  57. *
  58. * Last entry must be all 0s
  59. *
  60. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  61. * Class, Class Mask, private data (not used) }
  62. */
  63. static const struct pci_device_id i40e_pci_tbl[] = {
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  71. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  72. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  73. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  74. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  75. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  76. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  81. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  82. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  83. /* required last entry */
  84. {0, }
  85. };
  86. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  87. #define I40E_MAX_VF_COUNT 128
  88. static int debug = -1;
  89. module_param(debug, uint, 0);
  90. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  91. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  92. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  93. MODULE_LICENSE("GPL");
  94. MODULE_VERSION(DRV_VERSION);
  95. static struct workqueue_struct *i40e_wq;
  96. /**
  97. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  98. * @hw: pointer to the HW structure
  99. * @mem: ptr to mem struct to fill out
  100. * @size: size of memory requested
  101. * @alignment: what to align the allocation to
  102. **/
  103. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  104. u64 size, u32 alignment)
  105. {
  106. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  107. mem->size = ALIGN(size, alignment);
  108. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  109. &mem->pa, GFP_KERNEL);
  110. if (!mem->va)
  111. return -ENOMEM;
  112. return 0;
  113. }
  114. /**
  115. * i40e_free_dma_mem_d - OS specific memory free for shared code
  116. * @hw: pointer to the HW structure
  117. * @mem: ptr to mem struct to free
  118. **/
  119. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  120. {
  121. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  122. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  123. mem->va = NULL;
  124. mem->pa = 0;
  125. mem->size = 0;
  126. return 0;
  127. }
  128. /**
  129. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  130. * @hw: pointer to the HW structure
  131. * @mem: ptr to mem struct to fill out
  132. * @size: size of memory requested
  133. **/
  134. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  135. u32 size)
  136. {
  137. mem->size = size;
  138. mem->va = kzalloc(size, GFP_KERNEL);
  139. if (!mem->va)
  140. return -ENOMEM;
  141. return 0;
  142. }
  143. /**
  144. * i40e_free_virt_mem_d - OS specific memory free for shared code
  145. * @hw: pointer to the HW structure
  146. * @mem: ptr to mem struct to free
  147. **/
  148. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  149. {
  150. /* it's ok to kfree a NULL pointer */
  151. kfree(mem->va);
  152. mem->va = NULL;
  153. mem->size = 0;
  154. return 0;
  155. }
  156. /**
  157. * i40e_get_lump - find a lump of free generic resource
  158. * @pf: board private structure
  159. * @pile: the pile of resource to search
  160. * @needed: the number of items needed
  161. * @id: an owner id to stick on the items assigned
  162. *
  163. * Returns the base item index of the lump, or negative for error
  164. *
  165. * The search_hint trick and lack of advanced fit-finding only work
  166. * because we're highly likely to have all the same size lump requests.
  167. * Linear search time and any fragmentation should be minimal.
  168. **/
  169. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  170. u16 needed, u16 id)
  171. {
  172. int ret = -ENOMEM;
  173. int i, j;
  174. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  175. dev_info(&pf->pdev->dev,
  176. "param err: pile=%p needed=%d id=0x%04x\n",
  177. pile, needed, id);
  178. return -EINVAL;
  179. }
  180. /* start the linear search with an imperfect hint */
  181. i = pile->search_hint;
  182. while (i < pile->num_entries) {
  183. /* skip already allocated entries */
  184. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  185. i++;
  186. continue;
  187. }
  188. /* do we have enough in this lump? */
  189. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  190. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  191. break;
  192. }
  193. if (j == needed) {
  194. /* there was enough, so assign it to the requestor */
  195. for (j = 0; j < needed; j++)
  196. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  197. ret = i;
  198. pile->search_hint = i + j;
  199. break;
  200. }
  201. /* not enough, so skip over it and continue looking */
  202. i += j;
  203. }
  204. return ret;
  205. }
  206. /**
  207. * i40e_put_lump - return a lump of generic resource
  208. * @pile: the pile of resource to search
  209. * @index: the base item index
  210. * @id: the owner id of the items assigned
  211. *
  212. * Returns the count of items in the lump
  213. **/
  214. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  215. {
  216. int valid_id = (id | I40E_PILE_VALID_BIT);
  217. int count = 0;
  218. int i;
  219. if (!pile || index >= pile->num_entries)
  220. return -EINVAL;
  221. for (i = index;
  222. i < pile->num_entries && pile->list[i] == valid_id;
  223. i++) {
  224. pile->list[i] = 0;
  225. count++;
  226. }
  227. if (count && index < pile->search_hint)
  228. pile->search_hint = index;
  229. return count;
  230. }
  231. /**
  232. * i40e_find_vsi_from_id - searches for the vsi with the given id
  233. * @pf - the pf structure to search for the vsi
  234. * @id - id of the vsi it is searching for
  235. **/
  236. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  237. {
  238. int i;
  239. for (i = 0; i < pf->num_alloc_vsi; i++)
  240. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  241. return pf->vsi[i];
  242. return NULL;
  243. }
  244. /**
  245. * i40e_service_event_schedule - Schedule the service task to wake up
  246. * @pf: board private structure
  247. *
  248. * If not already scheduled, this puts the task into the work queue
  249. **/
  250. void i40e_service_event_schedule(struct i40e_pf *pf)
  251. {
  252. if (!test_bit(__I40E_DOWN, &pf->state) &&
  253. !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  254. queue_work(i40e_wq, &pf->service_task);
  255. }
  256. /**
  257. * i40e_tx_timeout - Respond to a Tx Hang
  258. * @netdev: network interface device structure
  259. *
  260. * If any port has noticed a Tx timeout, it is likely that the whole
  261. * device is munged, not just the one netdev port, so go for the full
  262. * reset.
  263. **/
  264. #ifdef I40E_FCOE
  265. void i40e_tx_timeout(struct net_device *netdev)
  266. #else
  267. static void i40e_tx_timeout(struct net_device *netdev)
  268. #endif
  269. {
  270. struct i40e_netdev_priv *np = netdev_priv(netdev);
  271. struct i40e_vsi *vsi = np->vsi;
  272. struct i40e_pf *pf = vsi->back;
  273. struct i40e_ring *tx_ring = NULL;
  274. unsigned int i, hung_queue = 0;
  275. u32 head, val;
  276. pf->tx_timeout_count++;
  277. /* find the stopped queue the same way the stack does */
  278. for (i = 0; i < netdev->num_tx_queues; i++) {
  279. struct netdev_queue *q;
  280. unsigned long trans_start;
  281. q = netdev_get_tx_queue(netdev, i);
  282. trans_start = q->trans_start;
  283. if (netif_xmit_stopped(q) &&
  284. time_after(jiffies,
  285. (trans_start + netdev->watchdog_timeo))) {
  286. hung_queue = i;
  287. break;
  288. }
  289. }
  290. if (i == netdev->num_tx_queues) {
  291. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  292. } else {
  293. /* now that we have an index, find the tx_ring struct */
  294. for (i = 0; i < vsi->num_queue_pairs; i++) {
  295. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  296. if (hung_queue ==
  297. vsi->tx_rings[i]->queue_index) {
  298. tx_ring = vsi->tx_rings[i];
  299. break;
  300. }
  301. }
  302. }
  303. }
  304. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  305. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  306. else if (time_before(jiffies,
  307. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  308. return; /* don't do any new action before the next timeout */
  309. if (tx_ring) {
  310. head = i40e_get_head(tx_ring);
  311. /* Read interrupt register */
  312. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  313. val = rd32(&pf->hw,
  314. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  315. tx_ring->vsi->base_vector - 1));
  316. else
  317. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  318. 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",
  319. vsi->seid, hung_queue, tx_ring->next_to_clean,
  320. head, tx_ring->next_to_use,
  321. readl(tx_ring->tail), val);
  322. }
  323. pf->tx_timeout_last_recovery = jiffies;
  324. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  325. pf->tx_timeout_recovery_level, hung_queue);
  326. switch (pf->tx_timeout_recovery_level) {
  327. case 1:
  328. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  329. break;
  330. case 2:
  331. set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  332. break;
  333. case 3:
  334. set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  335. break;
  336. default:
  337. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  338. break;
  339. }
  340. i40e_service_event_schedule(pf);
  341. pf->tx_timeout_recovery_level++;
  342. }
  343. /**
  344. * i40e_get_vsi_stats_struct - Get System Network Statistics
  345. * @vsi: the VSI we care about
  346. *
  347. * Returns the address of the device statistics structure.
  348. * The statistics are actually updated from the service task.
  349. **/
  350. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  351. {
  352. return &vsi->net_stats;
  353. }
  354. /**
  355. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  356. * @netdev: network interface device structure
  357. *
  358. * Returns the address of the device statistics structure.
  359. * The statistics are actually updated from the service task.
  360. **/
  361. #ifndef I40E_FCOE
  362. static
  363. #endif
  364. void i40e_get_netdev_stats_struct(struct net_device *netdev,
  365. struct rtnl_link_stats64 *stats)
  366. {
  367. struct i40e_netdev_priv *np = netdev_priv(netdev);
  368. struct i40e_ring *tx_ring, *rx_ring;
  369. struct i40e_vsi *vsi = np->vsi;
  370. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  371. int i;
  372. if (test_bit(__I40E_DOWN, &vsi->state))
  373. return;
  374. if (!vsi->tx_rings)
  375. return;
  376. rcu_read_lock();
  377. for (i = 0; i < vsi->num_queue_pairs; i++) {
  378. u64 bytes, packets;
  379. unsigned int start;
  380. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  381. if (!tx_ring)
  382. continue;
  383. do {
  384. start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
  385. packets = tx_ring->stats.packets;
  386. bytes = tx_ring->stats.bytes;
  387. } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
  388. stats->tx_packets += packets;
  389. stats->tx_bytes += bytes;
  390. rx_ring = &tx_ring[1];
  391. do {
  392. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  393. packets = rx_ring->stats.packets;
  394. bytes = rx_ring->stats.bytes;
  395. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  396. stats->rx_packets += packets;
  397. stats->rx_bytes += bytes;
  398. }
  399. rcu_read_unlock();
  400. /* following stats updated by i40e_watchdog_subtask() */
  401. stats->multicast = vsi_stats->multicast;
  402. stats->tx_errors = vsi_stats->tx_errors;
  403. stats->tx_dropped = vsi_stats->tx_dropped;
  404. stats->rx_errors = vsi_stats->rx_errors;
  405. stats->rx_dropped = vsi_stats->rx_dropped;
  406. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  407. stats->rx_length_errors = vsi_stats->rx_length_errors;
  408. }
  409. /**
  410. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  411. * @vsi: the VSI to have its stats reset
  412. **/
  413. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  414. {
  415. struct rtnl_link_stats64 *ns;
  416. int i;
  417. if (!vsi)
  418. return;
  419. ns = i40e_get_vsi_stats_struct(vsi);
  420. memset(ns, 0, sizeof(*ns));
  421. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  422. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  423. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  424. if (vsi->rx_rings && vsi->rx_rings[0]) {
  425. for (i = 0; i < vsi->num_queue_pairs; i++) {
  426. memset(&vsi->rx_rings[i]->stats, 0,
  427. sizeof(vsi->rx_rings[i]->stats));
  428. memset(&vsi->rx_rings[i]->rx_stats, 0,
  429. sizeof(vsi->rx_rings[i]->rx_stats));
  430. memset(&vsi->tx_rings[i]->stats, 0,
  431. sizeof(vsi->tx_rings[i]->stats));
  432. memset(&vsi->tx_rings[i]->tx_stats, 0,
  433. sizeof(vsi->tx_rings[i]->tx_stats));
  434. }
  435. }
  436. vsi->stat_offsets_loaded = false;
  437. }
  438. /**
  439. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  440. * @pf: the PF to be reset
  441. **/
  442. void i40e_pf_reset_stats(struct i40e_pf *pf)
  443. {
  444. int i;
  445. memset(&pf->stats, 0, sizeof(pf->stats));
  446. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  447. pf->stat_offsets_loaded = false;
  448. for (i = 0; i < I40E_MAX_VEB; i++) {
  449. if (pf->veb[i]) {
  450. memset(&pf->veb[i]->stats, 0,
  451. sizeof(pf->veb[i]->stats));
  452. memset(&pf->veb[i]->stats_offsets, 0,
  453. sizeof(pf->veb[i]->stats_offsets));
  454. pf->veb[i]->stat_offsets_loaded = false;
  455. }
  456. }
  457. pf->hw_csum_rx_error = 0;
  458. }
  459. /**
  460. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  461. * @hw: ptr to the hardware info
  462. * @hireg: the high 32 bit reg to read
  463. * @loreg: the low 32 bit reg to read
  464. * @offset_loaded: has the initial offset been loaded yet
  465. * @offset: ptr to current offset value
  466. * @stat: ptr to the stat
  467. *
  468. * Since the device stats are not reset at PFReset, they likely will not
  469. * be zeroed when the driver starts. We'll save the first values read
  470. * and use them as offsets to be subtracted from the raw values in order
  471. * to report stats that count from zero. In the process, we also manage
  472. * the potential roll-over.
  473. **/
  474. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  475. bool offset_loaded, u64 *offset, u64 *stat)
  476. {
  477. u64 new_data;
  478. if (hw->device_id == I40E_DEV_ID_QEMU) {
  479. new_data = rd32(hw, loreg);
  480. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  481. } else {
  482. new_data = rd64(hw, loreg);
  483. }
  484. if (!offset_loaded)
  485. *offset = new_data;
  486. if (likely(new_data >= *offset))
  487. *stat = new_data - *offset;
  488. else
  489. *stat = (new_data + BIT_ULL(48)) - *offset;
  490. *stat &= 0xFFFFFFFFFFFFULL;
  491. }
  492. /**
  493. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  494. * @hw: ptr to the hardware info
  495. * @reg: the hw reg to read
  496. * @offset_loaded: has the initial offset been loaded yet
  497. * @offset: ptr to current offset value
  498. * @stat: ptr to the stat
  499. **/
  500. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  501. bool offset_loaded, u64 *offset, u64 *stat)
  502. {
  503. u32 new_data;
  504. new_data = rd32(hw, reg);
  505. if (!offset_loaded)
  506. *offset = new_data;
  507. if (likely(new_data >= *offset))
  508. *stat = (u32)(new_data - *offset);
  509. else
  510. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  511. }
  512. /**
  513. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  514. * @vsi: the VSI to be updated
  515. **/
  516. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  517. {
  518. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  519. struct i40e_pf *pf = vsi->back;
  520. struct i40e_hw *hw = &pf->hw;
  521. struct i40e_eth_stats *oes;
  522. struct i40e_eth_stats *es; /* device's eth stats */
  523. es = &vsi->eth_stats;
  524. oes = &vsi->eth_stats_offsets;
  525. /* Gather up the stats that the hw collects */
  526. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  527. vsi->stat_offsets_loaded,
  528. &oes->tx_errors, &es->tx_errors);
  529. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  530. vsi->stat_offsets_loaded,
  531. &oes->rx_discards, &es->rx_discards);
  532. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  533. vsi->stat_offsets_loaded,
  534. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  535. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  536. vsi->stat_offsets_loaded,
  537. &oes->tx_errors, &es->tx_errors);
  538. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  539. I40E_GLV_GORCL(stat_idx),
  540. vsi->stat_offsets_loaded,
  541. &oes->rx_bytes, &es->rx_bytes);
  542. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  543. I40E_GLV_UPRCL(stat_idx),
  544. vsi->stat_offsets_loaded,
  545. &oes->rx_unicast, &es->rx_unicast);
  546. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  547. I40E_GLV_MPRCL(stat_idx),
  548. vsi->stat_offsets_loaded,
  549. &oes->rx_multicast, &es->rx_multicast);
  550. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  551. I40E_GLV_BPRCL(stat_idx),
  552. vsi->stat_offsets_loaded,
  553. &oes->rx_broadcast, &es->rx_broadcast);
  554. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  555. I40E_GLV_GOTCL(stat_idx),
  556. vsi->stat_offsets_loaded,
  557. &oes->tx_bytes, &es->tx_bytes);
  558. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  559. I40E_GLV_UPTCL(stat_idx),
  560. vsi->stat_offsets_loaded,
  561. &oes->tx_unicast, &es->tx_unicast);
  562. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  563. I40E_GLV_MPTCL(stat_idx),
  564. vsi->stat_offsets_loaded,
  565. &oes->tx_multicast, &es->tx_multicast);
  566. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  567. I40E_GLV_BPTCL(stat_idx),
  568. vsi->stat_offsets_loaded,
  569. &oes->tx_broadcast, &es->tx_broadcast);
  570. vsi->stat_offsets_loaded = true;
  571. }
  572. /**
  573. * i40e_update_veb_stats - Update Switch component statistics
  574. * @veb: the VEB being updated
  575. **/
  576. static void i40e_update_veb_stats(struct i40e_veb *veb)
  577. {
  578. struct i40e_pf *pf = veb->pf;
  579. struct i40e_hw *hw = &pf->hw;
  580. struct i40e_eth_stats *oes;
  581. struct i40e_eth_stats *es; /* device's eth stats */
  582. struct i40e_veb_tc_stats *veb_oes;
  583. struct i40e_veb_tc_stats *veb_es;
  584. int i, idx = 0;
  585. idx = veb->stats_idx;
  586. es = &veb->stats;
  587. oes = &veb->stats_offsets;
  588. veb_es = &veb->tc_stats;
  589. veb_oes = &veb->tc_stats_offsets;
  590. /* Gather up the stats that the hw collects */
  591. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  592. veb->stat_offsets_loaded,
  593. &oes->tx_discards, &es->tx_discards);
  594. if (hw->revision_id > 0)
  595. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  596. veb->stat_offsets_loaded,
  597. &oes->rx_unknown_protocol,
  598. &es->rx_unknown_protocol);
  599. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  600. veb->stat_offsets_loaded,
  601. &oes->rx_bytes, &es->rx_bytes);
  602. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  603. veb->stat_offsets_loaded,
  604. &oes->rx_unicast, &es->rx_unicast);
  605. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  606. veb->stat_offsets_loaded,
  607. &oes->rx_multicast, &es->rx_multicast);
  608. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  609. veb->stat_offsets_loaded,
  610. &oes->rx_broadcast, &es->rx_broadcast);
  611. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  612. veb->stat_offsets_loaded,
  613. &oes->tx_bytes, &es->tx_bytes);
  614. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  615. veb->stat_offsets_loaded,
  616. &oes->tx_unicast, &es->tx_unicast);
  617. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  618. veb->stat_offsets_loaded,
  619. &oes->tx_multicast, &es->tx_multicast);
  620. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  621. veb->stat_offsets_loaded,
  622. &oes->tx_broadcast, &es->tx_broadcast);
  623. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  624. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  625. I40E_GLVEBTC_RPCL(i, idx),
  626. veb->stat_offsets_loaded,
  627. &veb_oes->tc_rx_packets[i],
  628. &veb_es->tc_rx_packets[i]);
  629. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  630. I40E_GLVEBTC_RBCL(i, idx),
  631. veb->stat_offsets_loaded,
  632. &veb_oes->tc_rx_bytes[i],
  633. &veb_es->tc_rx_bytes[i]);
  634. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  635. I40E_GLVEBTC_TPCL(i, idx),
  636. veb->stat_offsets_loaded,
  637. &veb_oes->tc_tx_packets[i],
  638. &veb_es->tc_tx_packets[i]);
  639. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  640. I40E_GLVEBTC_TBCL(i, idx),
  641. veb->stat_offsets_loaded,
  642. &veb_oes->tc_tx_bytes[i],
  643. &veb_es->tc_tx_bytes[i]);
  644. }
  645. veb->stat_offsets_loaded = true;
  646. }
  647. #ifdef I40E_FCOE
  648. /**
  649. * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
  650. * @vsi: the VSI that is capable of doing FCoE
  651. **/
  652. static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
  653. {
  654. struct i40e_pf *pf = vsi->back;
  655. struct i40e_hw *hw = &pf->hw;
  656. struct i40e_fcoe_stats *ofs;
  657. struct i40e_fcoe_stats *fs; /* device's eth stats */
  658. int idx;
  659. if (vsi->type != I40E_VSI_FCOE)
  660. return;
  661. idx = hw->pf_id + I40E_FCOE_PF_STAT_OFFSET;
  662. fs = &vsi->fcoe_stats;
  663. ofs = &vsi->fcoe_stats_offsets;
  664. i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
  665. vsi->fcoe_stat_offsets_loaded,
  666. &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
  667. i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
  668. vsi->fcoe_stat_offsets_loaded,
  669. &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
  670. i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
  671. vsi->fcoe_stat_offsets_loaded,
  672. &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
  673. i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
  674. vsi->fcoe_stat_offsets_loaded,
  675. &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
  676. i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
  677. vsi->fcoe_stat_offsets_loaded,
  678. &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
  679. i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
  680. vsi->fcoe_stat_offsets_loaded,
  681. &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
  682. i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
  683. vsi->fcoe_stat_offsets_loaded,
  684. &ofs->fcoe_last_error, &fs->fcoe_last_error);
  685. i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
  686. vsi->fcoe_stat_offsets_loaded,
  687. &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
  688. vsi->fcoe_stat_offsets_loaded = true;
  689. }
  690. #endif
  691. /**
  692. * i40e_update_vsi_stats - Update the vsi statistics counters.
  693. * @vsi: the VSI to be updated
  694. *
  695. * There are a few instances where we store the same stat in a
  696. * couple of different structs. This is partly because we have
  697. * the netdev stats that need to be filled out, which is slightly
  698. * different from the "eth_stats" defined by the chip and used in
  699. * VF communications. We sort it out here.
  700. **/
  701. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  702. {
  703. struct i40e_pf *pf = vsi->back;
  704. struct rtnl_link_stats64 *ons;
  705. struct rtnl_link_stats64 *ns; /* netdev stats */
  706. struct i40e_eth_stats *oes;
  707. struct i40e_eth_stats *es; /* device's eth stats */
  708. u32 tx_restart, tx_busy;
  709. u64 tx_lost_interrupt;
  710. struct i40e_ring *p;
  711. u32 rx_page, rx_buf;
  712. u64 bytes, packets;
  713. unsigned int start;
  714. u64 tx_linearize;
  715. u64 tx_force_wb;
  716. u64 rx_p, rx_b;
  717. u64 tx_p, tx_b;
  718. u16 q;
  719. if (test_bit(__I40E_DOWN, &vsi->state) ||
  720. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  721. return;
  722. ns = i40e_get_vsi_stats_struct(vsi);
  723. ons = &vsi->net_stats_offsets;
  724. es = &vsi->eth_stats;
  725. oes = &vsi->eth_stats_offsets;
  726. /* Gather up the netdev and vsi stats that the driver collects
  727. * on the fly during packet processing
  728. */
  729. rx_b = rx_p = 0;
  730. tx_b = tx_p = 0;
  731. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  732. tx_lost_interrupt = 0;
  733. rx_page = 0;
  734. rx_buf = 0;
  735. rcu_read_lock();
  736. for (q = 0; q < vsi->num_queue_pairs; q++) {
  737. /* locate Tx ring */
  738. p = ACCESS_ONCE(vsi->tx_rings[q]);
  739. do {
  740. start = u64_stats_fetch_begin_irq(&p->syncp);
  741. packets = p->stats.packets;
  742. bytes = p->stats.bytes;
  743. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  744. tx_b += bytes;
  745. tx_p += packets;
  746. tx_restart += p->tx_stats.restart_queue;
  747. tx_busy += p->tx_stats.tx_busy;
  748. tx_linearize += p->tx_stats.tx_linearize;
  749. tx_force_wb += p->tx_stats.tx_force_wb;
  750. tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
  751. /* Rx queue is part of the same block as Tx queue */
  752. p = &p[1];
  753. do {
  754. start = u64_stats_fetch_begin_irq(&p->syncp);
  755. packets = p->stats.packets;
  756. bytes = p->stats.bytes;
  757. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  758. rx_b += bytes;
  759. rx_p += packets;
  760. rx_buf += p->rx_stats.alloc_buff_failed;
  761. rx_page += p->rx_stats.alloc_page_failed;
  762. }
  763. rcu_read_unlock();
  764. vsi->tx_restart = tx_restart;
  765. vsi->tx_busy = tx_busy;
  766. vsi->tx_linearize = tx_linearize;
  767. vsi->tx_force_wb = tx_force_wb;
  768. vsi->tx_lost_interrupt = tx_lost_interrupt;
  769. vsi->rx_page_failed = rx_page;
  770. vsi->rx_buf_failed = rx_buf;
  771. ns->rx_packets = rx_p;
  772. ns->rx_bytes = rx_b;
  773. ns->tx_packets = tx_p;
  774. ns->tx_bytes = tx_b;
  775. /* update netdev stats from eth stats */
  776. i40e_update_eth_stats(vsi);
  777. ons->tx_errors = oes->tx_errors;
  778. ns->tx_errors = es->tx_errors;
  779. ons->multicast = oes->rx_multicast;
  780. ns->multicast = es->rx_multicast;
  781. ons->rx_dropped = oes->rx_discards;
  782. ns->rx_dropped = es->rx_discards;
  783. ons->tx_dropped = oes->tx_discards;
  784. ns->tx_dropped = es->tx_discards;
  785. /* pull in a couple PF stats if this is the main vsi */
  786. if (vsi == pf->vsi[pf->lan_vsi]) {
  787. ns->rx_crc_errors = pf->stats.crc_errors;
  788. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  789. ns->rx_length_errors = pf->stats.rx_length_errors;
  790. }
  791. }
  792. /**
  793. * i40e_update_pf_stats - Update the PF statistics counters.
  794. * @pf: the PF to be updated
  795. **/
  796. static void i40e_update_pf_stats(struct i40e_pf *pf)
  797. {
  798. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  799. struct i40e_hw_port_stats *nsd = &pf->stats;
  800. struct i40e_hw *hw = &pf->hw;
  801. u32 val;
  802. int i;
  803. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  804. I40E_GLPRT_GORCL(hw->port),
  805. pf->stat_offsets_loaded,
  806. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  807. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  808. I40E_GLPRT_GOTCL(hw->port),
  809. pf->stat_offsets_loaded,
  810. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  811. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  812. pf->stat_offsets_loaded,
  813. &osd->eth.rx_discards,
  814. &nsd->eth.rx_discards);
  815. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  816. I40E_GLPRT_UPRCL(hw->port),
  817. pf->stat_offsets_loaded,
  818. &osd->eth.rx_unicast,
  819. &nsd->eth.rx_unicast);
  820. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  821. I40E_GLPRT_MPRCL(hw->port),
  822. pf->stat_offsets_loaded,
  823. &osd->eth.rx_multicast,
  824. &nsd->eth.rx_multicast);
  825. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  826. I40E_GLPRT_BPRCL(hw->port),
  827. pf->stat_offsets_loaded,
  828. &osd->eth.rx_broadcast,
  829. &nsd->eth.rx_broadcast);
  830. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  831. I40E_GLPRT_UPTCL(hw->port),
  832. pf->stat_offsets_loaded,
  833. &osd->eth.tx_unicast,
  834. &nsd->eth.tx_unicast);
  835. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  836. I40E_GLPRT_MPTCL(hw->port),
  837. pf->stat_offsets_loaded,
  838. &osd->eth.tx_multicast,
  839. &nsd->eth.tx_multicast);
  840. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  841. I40E_GLPRT_BPTCL(hw->port),
  842. pf->stat_offsets_loaded,
  843. &osd->eth.tx_broadcast,
  844. &nsd->eth.tx_broadcast);
  845. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  846. pf->stat_offsets_loaded,
  847. &osd->tx_dropped_link_down,
  848. &nsd->tx_dropped_link_down);
  849. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  850. pf->stat_offsets_loaded,
  851. &osd->crc_errors, &nsd->crc_errors);
  852. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  853. pf->stat_offsets_loaded,
  854. &osd->illegal_bytes, &nsd->illegal_bytes);
  855. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  856. pf->stat_offsets_loaded,
  857. &osd->mac_local_faults,
  858. &nsd->mac_local_faults);
  859. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  860. pf->stat_offsets_loaded,
  861. &osd->mac_remote_faults,
  862. &nsd->mac_remote_faults);
  863. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  864. pf->stat_offsets_loaded,
  865. &osd->rx_length_errors,
  866. &nsd->rx_length_errors);
  867. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  868. pf->stat_offsets_loaded,
  869. &osd->link_xon_rx, &nsd->link_xon_rx);
  870. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  871. pf->stat_offsets_loaded,
  872. &osd->link_xon_tx, &nsd->link_xon_tx);
  873. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  874. pf->stat_offsets_loaded,
  875. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  876. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  877. pf->stat_offsets_loaded,
  878. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  879. for (i = 0; i < 8; i++) {
  880. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  881. pf->stat_offsets_loaded,
  882. &osd->priority_xoff_rx[i],
  883. &nsd->priority_xoff_rx[i]);
  884. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  885. pf->stat_offsets_loaded,
  886. &osd->priority_xon_rx[i],
  887. &nsd->priority_xon_rx[i]);
  888. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  889. pf->stat_offsets_loaded,
  890. &osd->priority_xon_tx[i],
  891. &nsd->priority_xon_tx[i]);
  892. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  893. pf->stat_offsets_loaded,
  894. &osd->priority_xoff_tx[i],
  895. &nsd->priority_xoff_tx[i]);
  896. i40e_stat_update32(hw,
  897. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  898. pf->stat_offsets_loaded,
  899. &osd->priority_xon_2_xoff[i],
  900. &nsd->priority_xon_2_xoff[i]);
  901. }
  902. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  903. I40E_GLPRT_PRC64L(hw->port),
  904. pf->stat_offsets_loaded,
  905. &osd->rx_size_64, &nsd->rx_size_64);
  906. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  907. I40E_GLPRT_PRC127L(hw->port),
  908. pf->stat_offsets_loaded,
  909. &osd->rx_size_127, &nsd->rx_size_127);
  910. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  911. I40E_GLPRT_PRC255L(hw->port),
  912. pf->stat_offsets_loaded,
  913. &osd->rx_size_255, &nsd->rx_size_255);
  914. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  915. I40E_GLPRT_PRC511L(hw->port),
  916. pf->stat_offsets_loaded,
  917. &osd->rx_size_511, &nsd->rx_size_511);
  918. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  919. I40E_GLPRT_PRC1023L(hw->port),
  920. pf->stat_offsets_loaded,
  921. &osd->rx_size_1023, &nsd->rx_size_1023);
  922. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  923. I40E_GLPRT_PRC1522L(hw->port),
  924. pf->stat_offsets_loaded,
  925. &osd->rx_size_1522, &nsd->rx_size_1522);
  926. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  927. I40E_GLPRT_PRC9522L(hw->port),
  928. pf->stat_offsets_loaded,
  929. &osd->rx_size_big, &nsd->rx_size_big);
  930. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  931. I40E_GLPRT_PTC64L(hw->port),
  932. pf->stat_offsets_loaded,
  933. &osd->tx_size_64, &nsd->tx_size_64);
  934. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  935. I40E_GLPRT_PTC127L(hw->port),
  936. pf->stat_offsets_loaded,
  937. &osd->tx_size_127, &nsd->tx_size_127);
  938. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  939. I40E_GLPRT_PTC255L(hw->port),
  940. pf->stat_offsets_loaded,
  941. &osd->tx_size_255, &nsd->tx_size_255);
  942. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  943. I40E_GLPRT_PTC511L(hw->port),
  944. pf->stat_offsets_loaded,
  945. &osd->tx_size_511, &nsd->tx_size_511);
  946. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  947. I40E_GLPRT_PTC1023L(hw->port),
  948. pf->stat_offsets_loaded,
  949. &osd->tx_size_1023, &nsd->tx_size_1023);
  950. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  951. I40E_GLPRT_PTC1522L(hw->port),
  952. pf->stat_offsets_loaded,
  953. &osd->tx_size_1522, &nsd->tx_size_1522);
  954. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  955. I40E_GLPRT_PTC9522L(hw->port),
  956. pf->stat_offsets_loaded,
  957. &osd->tx_size_big, &nsd->tx_size_big);
  958. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  959. pf->stat_offsets_loaded,
  960. &osd->rx_undersize, &nsd->rx_undersize);
  961. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  962. pf->stat_offsets_loaded,
  963. &osd->rx_fragments, &nsd->rx_fragments);
  964. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  965. pf->stat_offsets_loaded,
  966. &osd->rx_oversize, &nsd->rx_oversize);
  967. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  968. pf->stat_offsets_loaded,
  969. &osd->rx_jabber, &nsd->rx_jabber);
  970. /* FDIR stats */
  971. i40e_stat_update32(hw,
  972. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
  973. pf->stat_offsets_loaded,
  974. &osd->fd_atr_match, &nsd->fd_atr_match);
  975. i40e_stat_update32(hw,
  976. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
  977. pf->stat_offsets_loaded,
  978. &osd->fd_sb_match, &nsd->fd_sb_match);
  979. i40e_stat_update32(hw,
  980. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
  981. pf->stat_offsets_loaded,
  982. &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
  983. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  984. nsd->tx_lpi_status =
  985. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  986. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  987. nsd->rx_lpi_status =
  988. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  989. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  990. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  991. pf->stat_offsets_loaded,
  992. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  993. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  994. pf->stat_offsets_loaded,
  995. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  996. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  997. !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
  998. nsd->fd_sb_status = true;
  999. else
  1000. nsd->fd_sb_status = false;
  1001. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  1002. !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
  1003. nsd->fd_atr_status = true;
  1004. else
  1005. nsd->fd_atr_status = false;
  1006. pf->stat_offsets_loaded = true;
  1007. }
  1008. /**
  1009. * i40e_update_stats - Update the various statistics counters.
  1010. * @vsi: the VSI to be updated
  1011. *
  1012. * Update the various stats for this VSI and its related entities.
  1013. **/
  1014. void i40e_update_stats(struct i40e_vsi *vsi)
  1015. {
  1016. struct i40e_pf *pf = vsi->back;
  1017. if (vsi == pf->vsi[pf->lan_vsi])
  1018. i40e_update_pf_stats(pf);
  1019. i40e_update_vsi_stats(vsi);
  1020. #ifdef I40E_FCOE
  1021. i40e_update_fcoe_stats(vsi);
  1022. #endif
  1023. }
  1024. /**
  1025. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  1026. * @vsi: the VSI to be searched
  1027. * @macaddr: the MAC address
  1028. * @vlan: the vlan
  1029. *
  1030. * Returns ptr to the filter object or NULL
  1031. **/
  1032. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  1033. const u8 *macaddr, s16 vlan)
  1034. {
  1035. struct i40e_mac_filter *f;
  1036. u64 key;
  1037. if (!vsi || !macaddr)
  1038. return NULL;
  1039. key = i40e_addr_to_hkey(macaddr);
  1040. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1041. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  1042. (vlan == f->vlan))
  1043. return f;
  1044. }
  1045. return NULL;
  1046. }
  1047. /**
  1048. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1049. * @vsi: the VSI to be searched
  1050. * @macaddr: the MAC address we are searching for
  1051. *
  1052. * Returns the first filter with the provided MAC address or NULL if
  1053. * MAC address was not found
  1054. **/
  1055. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1056. {
  1057. struct i40e_mac_filter *f;
  1058. u64 key;
  1059. if (!vsi || !macaddr)
  1060. return NULL;
  1061. key = i40e_addr_to_hkey(macaddr);
  1062. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1063. if ((ether_addr_equal(macaddr, f->macaddr)))
  1064. return f;
  1065. }
  1066. return NULL;
  1067. }
  1068. /**
  1069. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1070. * @vsi: the VSI to be searched
  1071. *
  1072. * Returns true if VSI is in vlan mode or false otherwise
  1073. **/
  1074. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1075. {
  1076. /* If we have a PVID, always operate in VLAN mode */
  1077. if (vsi->info.pvid)
  1078. return true;
  1079. /* We need to operate in VLAN mode whenever we have any filters with
  1080. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1081. * time, incurring search cost repeatedly. However, we can notice two
  1082. * things:
  1083. *
  1084. * 1) the only place where we can gain a VLAN filter is in
  1085. * i40e_add_filter.
  1086. *
  1087. * 2) the only place where filters are actually removed is in
  1088. * i40e_sync_filters_subtask.
  1089. *
  1090. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1091. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1092. * we have to perform the full search after deleting filters in
  1093. * i40e_sync_filters_subtask, but we already have to search
  1094. * filters here and can perform the check at the same time. This
  1095. * results in avoiding embedding a loop for VLAN mode inside another
  1096. * loop over all the filters, and should maintain correctness as noted
  1097. * above.
  1098. */
  1099. return vsi->has_vlan_filter;
  1100. }
  1101. /**
  1102. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1103. * @vsi: the VSI to configure
  1104. * @tmp_add_list: list of filters ready to be added
  1105. * @tmp_del_list: list of filters ready to be deleted
  1106. * @vlan_filters: the number of active VLAN filters
  1107. *
  1108. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1109. * behave as expected. If we have any active VLAN filters remaining or about
  1110. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1111. * so that they only match against untagged traffic. If we no longer have any
  1112. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1113. * so that they match against both tagged and untagged traffic. In this way,
  1114. * we ensure that we correctly receive the desired traffic. This ensures that
  1115. * when we have an active VLAN we will receive only untagged traffic and
  1116. * traffic matching active VLANs. If we have no active VLANs then we will
  1117. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1118. *
  1119. * Finally, in a similar fashion, this function also corrects filters when
  1120. * there is an active PVID assigned to this VSI.
  1121. *
  1122. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1123. *
  1124. * This function is only expected to be called from within
  1125. * i40e_sync_vsi_filters.
  1126. *
  1127. * NOTE: This function expects to be called while under the
  1128. * mac_filter_hash_lock
  1129. */
  1130. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1131. struct hlist_head *tmp_add_list,
  1132. struct hlist_head *tmp_del_list,
  1133. int vlan_filters)
  1134. {
  1135. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1136. struct i40e_mac_filter *f, *add_head;
  1137. struct i40e_new_mac_filter *new;
  1138. struct hlist_node *h;
  1139. int bkt, new_vlan;
  1140. /* To determine if a particular filter needs to be replaced we
  1141. * have the three following conditions:
  1142. *
  1143. * a) if we have a PVID assigned, then all filters which are
  1144. * not marked as VLAN=PVID must be replaced with filters that
  1145. * are.
  1146. * b) otherwise, if we have any active VLANS, all filters
  1147. * which are marked as VLAN=-1 must be replaced with
  1148. * filters marked as VLAN=0
  1149. * c) finally, if we do not have any active VLANS, all filters
  1150. * which are marked as VLAN=0 must be replaced with filters
  1151. * marked as VLAN=-1
  1152. */
  1153. /* Update the filters about to be added in place */
  1154. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1155. if (pvid && new->f->vlan != pvid)
  1156. new->f->vlan = pvid;
  1157. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1158. new->f->vlan = 0;
  1159. else if (!vlan_filters && new->f->vlan == 0)
  1160. new->f->vlan = I40E_VLAN_ANY;
  1161. }
  1162. /* Update the remaining active filters */
  1163. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1164. /* Combine the checks for whether a filter needs to be changed
  1165. * and then determine the new VLAN inside the if block, in
  1166. * order to avoid duplicating code for adding the new filter
  1167. * then deleting the old filter.
  1168. */
  1169. if ((pvid && f->vlan != pvid) ||
  1170. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1171. (!vlan_filters && f->vlan == 0)) {
  1172. /* Determine the new vlan we will be adding */
  1173. if (pvid)
  1174. new_vlan = pvid;
  1175. else if (vlan_filters)
  1176. new_vlan = 0;
  1177. else
  1178. new_vlan = I40E_VLAN_ANY;
  1179. /* Create the new filter */
  1180. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1181. if (!add_head)
  1182. return -ENOMEM;
  1183. /* Create a temporary i40e_new_mac_filter */
  1184. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1185. if (!new)
  1186. return -ENOMEM;
  1187. new->f = add_head;
  1188. new->state = add_head->state;
  1189. /* Add the new filter to the tmp list */
  1190. hlist_add_head(&new->hlist, tmp_add_list);
  1191. /* Put the original filter into the delete list */
  1192. f->state = I40E_FILTER_REMOVE;
  1193. hash_del(&f->hlist);
  1194. hlist_add_head(&f->hlist, tmp_del_list);
  1195. }
  1196. }
  1197. vsi->has_vlan_filter = !!vlan_filters;
  1198. return 0;
  1199. }
  1200. /**
  1201. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1202. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1203. * @macaddr: the MAC address
  1204. *
  1205. * Remove whatever filter the firmware set up so the driver can manage
  1206. * its own filtering intelligently.
  1207. **/
  1208. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1209. {
  1210. struct i40e_aqc_remove_macvlan_element_data element;
  1211. struct i40e_pf *pf = vsi->back;
  1212. /* Only appropriate for the PF main VSI */
  1213. if (vsi->type != I40E_VSI_MAIN)
  1214. return;
  1215. memset(&element, 0, sizeof(element));
  1216. ether_addr_copy(element.mac_addr, macaddr);
  1217. element.vlan_tag = 0;
  1218. /* Ignore error returns, some firmware does it this way... */
  1219. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1220. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1221. memset(&element, 0, sizeof(element));
  1222. ether_addr_copy(element.mac_addr, macaddr);
  1223. element.vlan_tag = 0;
  1224. /* ...and some firmware does it this way. */
  1225. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1226. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1227. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1228. }
  1229. /**
  1230. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1231. * @vsi: the VSI to be searched
  1232. * @macaddr: the MAC address
  1233. * @vlan: the vlan
  1234. *
  1235. * Returns ptr to the filter object or NULL when no memory available.
  1236. *
  1237. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1238. * being held.
  1239. **/
  1240. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1241. const u8 *macaddr, s16 vlan)
  1242. {
  1243. struct i40e_mac_filter *f;
  1244. u64 key;
  1245. if (!vsi || !macaddr)
  1246. return NULL;
  1247. f = i40e_find_filter(vsi, macaddr, vlan);
  1248. if (!f) {
  1249. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1250. if (!f)
  1251. return NULL;
  1252. /* Update the boolean indicating if we need to function in
  1253. * VLAN mode.
  1254. */
  1255. if (vlan >= 0)
  1256. vsi->has_vlan_filter = true;
  1257. ether_addr_copy(f->macaddr, macaddr);
  1258. f->vlan = vlan;
  1259. /* If we're in overflow promisc mode, set the state directly
  1260. * to failed, so we don't bother to try sending the filter
  1261. * to the hardware.
  1262. */
  1263. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
  1264. f->state = I40E_FILTER_FAILED;
  1265. else
  1266. f->state = I40E_FILTER_NEW;
  1267. INIT_HLIST_NODE(&f->hlist);
  1268. key = i40e_addr_to_hkey(macaddr);
  1269. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1270. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1271. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1272. }
  1273. /* If we're asked to add a filter that has been marked for removal, it
  1274. * is safe to simply restore it to active state. __i40e_del_filter
  1275. * will have simply deleted any filters which were previously marked
  1276. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1277. * previously been ACTIVE. Since we haven't yet run the sync filters
  1278. * task, just restore this filter to the ACTIVE state so that the
  1279. * sync task leaves it in place
  1280. */
  1281. if (f->state == I40E_FILTER_REMOVE)
  1282. f->state = I40E_FILTER_ACTIVE;
  1283. return f;
  1284. }
  1285. /**
  1286. * __i40e_del_filter - Remove a specific filter from the VSI
  1287. * @vsi: VSI to remove from
  1288. * @f: the filter to remove from the list
  1289. *
  1290. * This function should be called instead of i40e_del_filter only if you know
  1291. * the exact filter you will remove already, such as via i40e_find_filter or
  1292. * i40e_find_mac.
  1293. *
  1294. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1295. * being held.
  1296. * ANOTHER NOTE: This function MUST be called from within the context of
  1297. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1298. * instead of list_for_each_entry().
  1299. **/
  1300. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1301. {
  1302. if (!f)
  1303. return;
  1304. /* If the filter was never added to firmware then we can just delete it
  1305. * directly and we don't want to set the status to remove or else an
  1306. * admin queue command will unnecessarily fire.
  1307. */
  1308. if ((f->state == I40E_FILTER_FAILED) ||
  1309. (f->state == I40E_FILTER_NEW)) {
  1310. hash_del(&f->hlist);
  1311. kfree(f);
  1312. } else {
  1313. f->state = I40E_FILTER_REMOVE;
  1314. }
  1315. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1316. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1317. }
  1318. /**
  1319. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1320. * @vsi: the VSI to be searched
  1321. * @macaddr: the MAC address
  1322. * @vlan: the VLAN
  1323. *
  1324. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1325. * being held.
  1326. * ANOTHER NOTE: This function MUST be called from within the context of
  1327. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1328. * instead of list_for_each_entry().
  1329. **/
  1330. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1331. {
  1332. struct i40e_mac_filter *f;
  1333. if (!vsi || !macaddr)
  1334. return;
  1335. f = i40e_find_filter(vsi, macaddr, vlan);
  1336. __i40e_del_filter(vsi, f);
  1337. }
  1338. /**
  1339. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1340. * @vsi: the VSI to be searched
  1341. * @macaddr: the mac address to be filtered
  1342. *
  1343. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1344. * go through all the macvlan filters and add a macvlan filter for each
  1345. * unique vlan that already exists. If a PVID has been assigned, instead only
  1346. * add the macaddr to that VLAN.
  1347. *
  1348. * Returns last filter added on success, else NULL
  1349. **/
  1350. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1351. const u8 *macaddr)
  1352. {
  1353. struct i40e_mac_filter *f, *add = NULL;
  1354. struct hlist_node *h;
  1355. int bkt;
  1356. if (vsi->info.pvid)
  1357. return i40e_add_filter(vsi, macaddr,
  1358. le16_to_cpu(vsi->info.pvid));
  1359. if (!i40e_is_vsi_in_vlan(vsi))
  1360. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1361. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1362. if (f->state == I40E_FILTER_REMOVE)
  1363. continue;
  1364. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1365. if (!add)
  1366. return NULL;
  1367. }
  1368. return add;
  1369. }
  1370. /**
  1371. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1372. * @vsi: the VSI to be searched
  1373. * @macaddr: the mac address to be removed
  1374. *
  1375. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1376. * associated with.
  1377. *
  1378. * Returns 0 for success, or error
  1379. **/
  1380. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1381. {
  1382. struct i40e_mac_filter *f;
  1383. struct hlist_node *h;
  1384. bool found = false;
  1385. int bkt;
  1386. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1387. "Missing mac_filter_hash_lock\n");
  1388. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1389. if (ether_addr_equal(macaddr, f->macaddr)) {
  1390. __i40e_del_filter(vsi, f);
  1391. found = true;
  1392. }
  1393. }
  1394. if (found)
  1395. return 0;
  1396. else
  1397. return -ENOENT;
  1398. }
  1399. /**
  1400. * i40e_set_mac - NDO callback to set mac address
  1401. * @netdev: network interface device structure
  1402. * @p: pointer to an address structure
  1403. *
  1404. * Returns 0 on success, negative on failure
  1405. **/
  1406. #ifdef I40E_FCOE
  1407. int i40e_set_mac(struct net_device *netdev, void *p)
  1408. #else
  1409. static int i40e_set_mac(struct net_device *netdev, void *p)
  1410. #endif
  1411. {
  1412. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1413. struct i40e_vsi *vsi = np->vsi;
  1414. struct i40e_pf *pf = vsi->back;
  1415. struct i40e_hw *hw = &pf->hw;
  1416. struct sockaddr *addr = p;
  1417. if (!is_valid_ether_addr(addr->sa_data))
  1418. return -EADDRNOTAVAIL;
  1419. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1420. netdev_info(netdev, "already using mac address %pM\n",
  1421. addr->sa_data);
  1422. return 0;
  1423. }
  1424. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1425. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1426. return -EADDRNOTAVAIL;
  1427. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1428. netdev_info(netdev, "returning to hw mac address %pM\n",
  1429. hw->mac.addr);
  1430. else
  1431. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1432. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1433. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1434. i40e_add_mac_filter(vsi, addr->sa_data);
  1435. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1436. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1437. if (vsi->type == I40E_VSI_MAIN) {
  1438. i40e_status ret;
  1439. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1440. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1441. addr->sa_data, NULL);
  1442. if (ret)
  1443. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1444. i40e_stat_str(hw, ret),
  1445. i40e_aq_str(hw, hw->aq.asq_last_status));
  1446. }
  1447. /* schedule our worker thread which will take care of
  1448. * applying the new filter changes
  1449. */
  1450. i40e_service_event_schedule(vsi->back);
  1451. return 0;
  1452. }
  1453. /**
  1454. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1455. * @vsi: the VSI being setup
  1456. * @ctxt: VSI context structure
  1457. * @enabled_tc: Enabled TCs bitmap
  1458. * @is_add: True if called before Add VSI
  1459. *
  1460. * Setup VSI queue mapping for enabled traffic classes.
  1461. **/
  1462. #ifdef I40E_FCOE
  1463. void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1464. struct i40e_vsi_context *ctxt,
  1465. u8 enabled_tc,
  1466. bool is_add)
  1467. #else
  1468. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1469. struct i40e_vsi_context *ctxt,
  1470. u8 enabled_tc,
  1471. bool is_add)
  1472. #endif
  1473. {
  1474. struct i40e_pf *pf = vsi->back;
  1475. u16 sections = 0;
  1476. u8 netdev_tc = 0;
  1477. u16 numtc = 0;
  1478. u16 qcount;
  1479. u8 offset;
  1480. u16 qmap;
  1481. int i;
  1482. u16 num_tc_qps = 0;
  1483. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1484. offset = 0;
  1485. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1486. /* Find numtc from enabled TC bitmap */
  1487. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1488. if (enabled_tc & BIT(i)) /* TC is enabled */
  1489. numtc++;
  1490. }
  1491. if (!numtc) {
  1492. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1493. numtc = 1;
  1494. }
  1495. } else {
  1496. /* At least TC0 is enabled in case of non-DCB case */
  1497. numtc = 1;
  1498. }
  1499. vsi->tc_config.numtc = numtc;
  1500. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1501. /* Number of queues per enabled TC */
  1502. qcount = vsi->alloc_queue_pairs;
  1503. num_tc_qps = qcount / numtc;
  1504. num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
  1505. /* Setup queue offset/count for all TCs for given VSI */
  1506. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1507. /* See if the given TC is enabled for the given VSI */
  1508. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1509. /* TC is enabled */
  1510. int pow, num_qps;
  1511. switch (vsi->type) {
  1512. case I40E_VSI_MAIN:
  1513. qcount = min_t(int, pf->alloc_rss_size,
  1514. num_tc_qps);
  1515. break;
  1516. #ifdef I40E_FCOE
  1517. case I40E_VSI_FCOE:
  1518. qcount = num_tc_qps;
  1519. break;
  1520. #endif
  1521. case I40E_VSI_FDIR:
  1522. case I40E_VSI_SRIOV:
  1523. case I40E_VSI_VMDQ2:
  1524. default:
  1525. qcount = num_tc_qps;
  1526. WARN_ON(i != 0);
  1527. break;
  1528. }
  1529. vsi->tc_config.tc_info[i].qoffset = offset;
  1530. vsi->tc_config.tc_info[i].qcount = qcount;
  1531. /* find the next higher power-of-2 of num queue pairs */
  1532. num_qps = qcount;
  1533. pow = 0;
  1534. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1535. pow++;
  1536. num_qps >>= 1;
  1537. }
  1538. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1539. qmap =
  1540. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1541. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1542. offset += qcount;
  1543. } else {
  1544. /* TC is not enabled so set the offset to
  1545. * default queue and allocate one queue
  1546. * for the given TC.
  1547. */
  1548. vsi->tc_config.tc_info[i].qoffset = 0;
  1549. vsi->tc_config.tc_info[i].qcount = 1;
  1550. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1551. qmap = 0;
  1552. }
  1553. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1554. }
  1555. /* Set actual Tx/Rx queue pairs */
  1556. vsi->num_queue_pairs = offset;
  1557. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1558. if (vsi->req_queue_pairs > 0)
  1559. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1560. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1561. vsi->num_queue_pairs = pf->num_lan_msix;
  1562. }
  1563. /* Scheduler section valid can only be set for ADD VSI */
  1564. if (is_add) {
  1565. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1566. ctxt->info.up_enable_bits = enabled_tc;
  1567. }
  1568. if (vsi->type == I40E_VSI_SRIOV) {
  1569. ctxt->info.mapping_flags |=
  1570. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1571. for (i = 0; i < vsi->num_queue_pairs; i++)
  1572. ctxt->info.queue_mapping[i] =
  1573. cpu_to_le16(vsi->base_queue + i);
  1574. } else {
  1575. ctxt->info.mapping_flags |=
  1576. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1577. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1578. }
  1579. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1580. }
  1581. /**
  1582. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1583. * @netdev: the netdevice
  1584. * @addr: address to add
  1585. *
  1586. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1587. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1588. */
  1589. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1590. {
  1591. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1592. struct i40e_vsi *vsi = np->vsi;
  1593. if (i40e_add_mac_filter(vsi, addr))
  1594. return 0;
  1595. else
  1596. return -ENOMEM;
  1597. }
  1598. /**
  1599. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1600. * @netdev: the netdevice
  1601. * @addr: address to add
  1602. *
  1603. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1604. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1605. */
  1606. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1607. {
  1608. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1609. struct i40e_vsi *vsi = np->vsi;
  1610. i40e_del_mac_filter(vsi, addr);
  1611. return 0;
  1612. }
  1613. /**
  1614. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1615. * @netdev: network interface device structure
  1616. **/
  1617. #ifdef I40E_FCOE
  1618. void i40e_set_rx_mode(struct net_device *netdev)
  1619. #else
  1620. static void i40e_set_rx_mode(struct net_device *netdev)
  1621. #endif
  1622. {
  1623. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1624. struct i40e_vsi *vsi = np->vsi;
  1625. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1626. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1627. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1628. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1629. /* check for other flag changes */
  1630. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1631. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1632. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1633. }
  1634. /* schedule our worker thread which will take care of
  1635. * applying the new filter changes
  1636. */
  1637. i40e_service_event_schedule(vsi->back);
  1638. }
  1639. /**
  1640. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1641. * @vsi: Pointer to VSI struct
  1642. * @from: Pointer to list which contains MAC filter entries - changes to
  1643. * those entries needs to be undone.
  1644. *
  1645. * MAC filter entries from this list were slated for deletion.
  1646. **/
  1647. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1648. struct hlist_head *from)
  1649. {
  1650. struct i40e_mac_filter *f;
  1651. struct hlist_node *h;
  1652. hlist_for_each_entry_safe(f, h, from, hlist) {
  1653. u64 key = i40e_addr_to_hkey(f->macaddr);
  1654. /* Move the element back into MAC filter list*/
  1655. hlist_del(&f->hlist);
  1656. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1657. }
  1658. }
  1659. /**
  1660. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1661. * @vsi: Pointer to vsi struct
  1662. * @from: Pointer to list which contains MAC filter entries - changes to
  1663. * those entries needs to be undone.
  1664. *
  1665. * MAC filter entries from this list were slated for addition.
  1666. **/
  1667. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1668. struct hlist_head *from)
  1669. {
  1670. struct i40e_new_mac_filter *new;
  1671. struct hlist_node *h;
  1672. hlist_for_each_entry_safe(new, h, from, hlist) {
  1673. /* We can simply free the wrapper structure */
  1674. hlist_del(&new->hlist);
  1675. kfree(new);
  1676. }
  1677. }
  1678. /**
  1679. * i40e_next_entry - Get the next non-broadcast filter from a list
  1680. * @next: pointer to filter in list
  1681. *
  1682. * Returns the next non-broadcast filter in the list. Required so that we
  1683. * ignore broadcast filters within the list, since these are not handled via
  1684. * the normal firmware update path.
  1685. */
  1686. static
  1687. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1688. {
  1689. while (next) {
  1690. next = hlist_entry(next->hlist.next,
  1691. typeof(struct i40e_new_mac_filter),
  1692. hlist);
  1693. /* keep going if we found a broadcast filter */
  1694. if (next && is_broadcast_ether_addr(next->f->macaddr))
  1695. continue;
  1696. break;
  1697. }
  1698. return next;
  1699. }
  1700. /**
  1701. * i40e_update_filter_state - Update filter state based on return data
  1702. * from firmware
  1703. * @count: Number of filters added
  1704. * @add_list: return data from fw
  1705. * @head: pointer to first filter in current batch
  1706. *
  1707. * MAC filter entries from list were slated to be added to device. Returns
  1708. * number of successful filters. Note that 0 does NOT mean success!
  1709. **/
  1710. static int
  1711. i40e_update_filter_state(int count,
  1712. struct i40e_aqc_add_macvlan_element_data *add_list,
  1713. struct i40e_new_mac_filter *add_head)
  1714. {
  1715. int retval = 0;
  1716. int i;
  1717. for (i = 0; i < count; i++) {
  1718. /* Always check status of each filter. We don't need to check
  1719. * the firmware return status because we pre-set the filter
  1720. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1721. * request to the adminq. Thus, if it no longer matches then
  1722. * we know the filter is active.
  1723. */
  1724. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1725. add_head->state = I40E_FILTER_FAILED;
  1726. } else {
  1727. add_head->state = I40E_FILTER_ACTIVE;
  1728. retval++;
  1729. }
  1730. add_head = i40e_next_filter(add_head);
  1731. if (!add_head)
  1732. break;
  1733. }
  1734. return retval;
  1735. }
  1736. /**
  1737. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1738. * @vsi: ptr to the VSI
  1739. * @vsi_name: name to display in messages
  1740. * @list: the list of filters to send to firmware
  1741. * @num_del: the number of filters to delete
  1742. * @retval: Set to -EIO on failure to delete
  1743. *
  1744. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1745. * *retval instead of a return value so that success does not force ret_val to
  1746. * be set to 0. This ensures that a sequence of calls to this function
  1747. * preserve the previous value of *retval on successful delete.
  1748. */
  1749. static
  1750. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1751. struct i40e_aqc_remove_macvlan_element_data *list,
  1752. int num_del, int *retval)
  1753. {
  1754. struct i40e_hw *hw = &vsi->back->hw;
  1755. i40e_status aq_ret;
  1756. int aq_err;
  1757. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1758. aq_err = hw->aq.asq_last_status;
  1759. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1760. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1761. *retval = -EIO;
  1762. dev_info(&vsi->back->pdev->dev,
  1763. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1764. vsi_name, i40e_stat_str(hw, aq_ret),
  1765. i40e_aq_str(hw, aq_err));
  1766. }
  1767. }
  1768. /**
  1769. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1770. * @vsi: ptr to the VSI
  1771. * @vsi_name: name to display in messages
  1772. * @list: the list of filters to send to firmware
  1773. * @add_head: Position in the add hlist
  1774. * @num_add: the number of filters to add
  1775. * @promisc_change: set to true on exit if promiscuous mode was forced on
  1776. *
  1777. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1778. * promisc_changed to true if the firmware has run out of space for more
  1779. * filters.
  1780. */
  1781. static
  1782. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1783. struct i40e_aqc_add_macvlan_element_data *list,
  1784. struct i40e_new_mac_filter *add_head,
  1785. int num_add, bool *promisc_changed)
  1786. {
  1787. struct i40e_hw *hw = &vsi->back->hw;
  1788. int aq_err, fcnt;
  1789. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1790. aq_err = hw->aq.asq_last_status;
  1791. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1792. if (fcnt != num_add) {
  1793. *promisc_changed = true;
  1794. set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1795. dev_warn(&vsi->back->pdev->dev,
  1796. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1797. i40e_aq_str(hw, aq_err),
  1798. vsi_name);
  1799. }
  1800. }
  1801. /**
  1802. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1803. * @vsi: pointer to the VSI
  1804. * @f: filter data
  1805. *
  1806. * This function sets or clears the promiscuous broadcast flags for VLAN
  1807. * filters in order to properly receive broadcast frames. Assumes that only
  1808. * broadcast filters are passed.
  1809. *
  1810. * Returns status indicating success or failure;
  1811. **/
  1812. static i40e_status
  1813. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1814. struct i40e_mac_filter *f)
  1815. {
  1816. bool enable = f->state == I40E_FILTER_NEW;
  1817. struct i40e_hw *hw = &vsi->back->hw;
  1818. i40e_status aq_ret;
  1819. if (f->vlan == I40E_VLAN_ANY) {
  1820. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1821. vsi->seid,
  1822. enable,
  1823. NULL);
  1824. } else {
  1825. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1826. vsi->seid,
  1827. enable,
  1828. f->vlan,
  1829. NULL);
  1830. }
  1831. if (aq_ret)
  1832. dev_warn(&vsi->back->pdev->dev,
  1833. "Error %s setting broadcast promiscuous mode on %s\n",
  1834. i40e_aq_str(hw, hw->aq.asq_last_status),
  1835. vsi_name);
  1836. return aq_ret;
  1837. }
  1838. /**
  1839. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1840. * @vsi: ptr to the VSI
  1841. *
  1842. * Push any outstanding VSI filter changes through the AdminQ.
  1843. *
  1844. * Returns 0 or error value
  1845. **/
  1846. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1847. {
  1848. struct hlist_head tmp_add_list, tmp_del_list;
  1849. struct i40e_mac_filter *f;
  1850. struct i40e_new_mac_filter *new, *add_head = NULL;
  1851. struct i40e_hw *hw = &vsi->back->hw;
  1852. unsigned int failed_filters = 0;
  1853. unsigned int vlan_filters = 0;
  1854. bool promisc_changed = false;
  1855. char vsi_name[16] = "PF";
  1856. int filter_list_len = 0;
  1857. i40e_status aq_ret = 0;
  1858. u32 changed_flags = 0;
  1859. struct hlist_node *h;
  1860. struct i40e_pf *pf;
  1861. int num_add = 0;
  1862. int num_del = 0;
  1863. int retval = 0;
  1864. u16 cmd_flags;
  1865. int list_size;
  1866. int bkt;
  1867. /* empty array typed pointers, kcalloc later */
  1868. struct i40e_aqc_add_macvlan_element_data *add_list;
  1869. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1870. while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
  1871. usleep_range(1000, 2000);
  1872. pf = vsi->back;
  1873. if (vsi->netdev) {
  1874. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1875. vsi->current_netdev_flags = vsi->netdev->flags;
  1876. }
  1877. INIT_HLIST_HEAD(&tmp_add_list);
  1878. INIT_HLIST_HEAD(&tmp_del_list);
  1879. if (vsi->type == I40E_VSI_SRIOV)
  1880. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  1881. else if (vsi->type != I40E_VSI_MAIN)
  1882. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  1883. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1884. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1885. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1886. /* Create a list of filters to delete. */
  1887. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1888. if (f->state == I40E_FILTER_REMOVE) {
  1889. /* Move the element into temporary del_list */
  1890. hash_del(&f->hlist);
  1891. hlist_add_head(&f->hlist, &tmp_del_list);
  1892. /* Avoid counting removed filters */
  1893. continue;
  1894. }
  1895. if (f->state == I40E_FILTER_NEW) {
  1896. /* Create a temporary i40e_new_mac_filter */
  1897. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1898. if (!new)
  1899. goto err_no_memory_locked;
  1900. /* Store pointer to the real filter */
  1901. new->f = f;
  1902. new->state = f->state;
  1903. /* Add it to the hash list */
  1904. hlist_add_head(&new->hlist, &tmp_add_list);
  1905. }
  1906. /* Count the number of active (current and new) VLAN
  1907. * filters we have now. Does not count filters which
  1908. * are marked for deletion.
  1909. */
  1910. if (f->vlan > 0)
  1911. vlan_filters++;
  1912. }
  1913. retval = i40e_correct_mac_vlan_filters(vsi,
  1914. &tmp_add_list,
  1915. &tmp_del_list,
  1916. vlan_filters);
  1917. if (retval)
  1918. goto err_no_memory_locked;
  1919. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1920. }
  1921. /* Now process 'del_list' outside the lock */
  1922. if (!hlist_empty(&tmp_del_list)) {
  1923. filter_list_len = hw->aq.asq_buf_size /
  1924. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1925. list_size = filter_list_len *
  1926. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1927. del_list = kzalloc(list_size, GFP_ATOMIC);
  1928. if (!del_list)
  1929. goto err_no_memory;
  1930. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  1931. cmd_flags = 0;
  1932. /* handle broadcast filters by updating the broadcast
  1933. * promiscuous flag and release filter list.
  1934. */
  1935. if (is_broadcast_ether_addr(f->macaddr)) {
  1936. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  1937. hlist_del(&f->hlist);
  1938. kfree(f);
  1939. continue;
  1940. }
  1941. /* add to delete list */
  1942. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  1943. if (f->vlan == I40E_VLAN_ANY) {
  1944. del_list[num_del].vlan_tag = 0;
  1945. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1946. } else {
  1947. del_list[num_del].vlan_tag =
  1948. cpu_to_le16((u16)(f->vlan));
  1949. }
  1950. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1951. del_list[num_del].flags = cmd_flags;
  1952. num_del++;
  1953. /* flush a full buffer */
  1954. if (num_del == filter_list_len) {
  1955. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1956. num_del, &retval);
  1957. memset(del_list, 0, list_size);
  1958. num_del = 0;
  1959. }
  1960. /* Release memory for MAC filter entries which were
  1961. * synced up with HW.
  1962. */
  1963. hlist_del(&f->hlist);
  1964. kfree(f);
  1965. }
  1966. if (num_del) {
  1967. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1968. num_del, &retval);
  1969. }
  1970. kfree(del_list);
  1971. del_list = NULL;
  1972. }
  1973. if (!hlist_empty(&tmp_add_list)) {
  1974. /* Do all the adds now. */
  1975. filter_list_len = hw->aq.asq_buf_size /
  1976. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1977. list_size = filter_list_len *
  1978. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1979. add_list = kzalloc(list_size, GFP_ATOMIC);
  1980. if (!add_list)
  1981. goto err_no_memory;
  1982. num_add = 0;
  1983. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1984. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1985. &vsi->state)) {
  1986. new->state = I40E_FILTER_FAILED;
  1987. continue;
  1988. }
  1989. /* handle broadcast filters by updating the broadcast
  1990. * promiscuous flag instead of adding a MAC filter.
  1991. */
  1992. if (is_broadcast_ether_addr(new->f->macaddr)) {
  1993. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  1994. new->f))
  1995. new->state = I40E_FILTER_FAILED;
  1996. else
  1997. new->state = I40E_FILTER_ACTIVE;
  1998. continue;
  1999. }
  2000. /* add to add array */
  2001. if (num_add == 0)
  2002. add_head = new;
  2003. cmd_flags = 0;
  2004. ether_addr_copy(add_list[num_add].mac_addr,
  2005. new->f->macaddr);
  2006. if (new->f->vlan == I40E_VLAN_ANY) {
  2007. add_list[num_add].vlan_tag = 0;
  2008. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  2009. } else {
  2010. add_list[num_add].vlan_tag =
  2011. cpu_to_le16((u16)(new->f->vlan));
  2012. }
  2013. add_list[num_add].queue_number = 0;
  2014. /* set invalid match method for later detection */
  2015. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  2016. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  2017. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  2018. num_add++;
  2019. /* flush a full buffer */
  2020. if (num_add == filter_list_len) {
  2021. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  2022. add_head, num_add,
  2023. &promisc_changed);
  2024. memset(add_list, 0, list_size);
  2025. num_add = 0;
  2026. }
  2027. }
  2028. if (num_add) {
  2029. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  2030. num_add, &promisc_changed);
  2031. }
  2032. /* Now move all of the filters from the temp add list back to
  2033. * the VSI's list.
  2034. */
  2035. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2036. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2037. /* Only update the state if we're still NEW */
  2038. if (new->f->state == I40E_FILTER_NEW)
  2039. new->f->state = new->state;
  2040. hlist_del(&new->hlist);
  2041. kfree(new);
  2042. }
  2043. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2044. kfree(add_list);
  2045. add_list = NULL;
  2046. }
  2047. /* Determine the number of active and failed filters. */
  2048. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2049. vsi->active_filters = 0;
  2050. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  2051. if (f->state == I40E_FILTER_ACTIVE)
  2052. vsi->active_filters++;
  2053. else if (f->state == I40E_FILTER_FAILED)
  2054. failed_filters++;
  2055. }
  2056. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2057. /* If promiscuous mode has changed, we need to calculate a new
  2058. * threshold for when we are safe to exit
  2059. */
  2060. if (promisc_changed)
  2061. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  2062. /* Check if we are able to exit overflow promiscuous mode. We can
  2063. * safely exit if we didn't just enter, we no longer have any failed
  2064. * filters, and we have reduced filters below the threshold value.
  2065. */
  2066. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
  2067. !promisc_changed && !failed_filters &&
  2068. (vsi->active_filters < vsi->promisc_threshold)) {
  2069. dev_info(&pf->pdev->dev,
  2070. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  2071. vsi_name);
  2072. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  2073. promisc_changed = true;
  2074. vsi->promisc_threshold = 0;
  2075. }
  2076. /* if the VF is not trusted do not do promisc */
  2077. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2078. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  2079. goto out;
  2080. }
  2081. /* check for changes in promiscuous modes */
  2082. if (changed_flags & IFF_ALLMULTI) {
  2083. bool cur_multipromisc;
  2084. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2085. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2086. vsi->seid,
  2087. cur_multipromisc,
  2088. NULL);
  2089. if (aq_ret) {
  2090. retval = i40e_aq_rc_to_posix(aq_ret,
  2091. hw->aq.asq_last_status);
  2092. dev_info(&pf->pdev->dev,
  2093. "set multi promisc failed on %s, err %s aq_err %s\n",
  2094. vsi_name,
  2095. i40e_stat_str(hw, aq_ret),
  2096. i40e_aq_str(hw, hw->aq.asq_last_status));
  2097. }
  2098. }
  2099. if ((changed_flags & IFF_PROMISC) ||
  2100. (promisc_changed &&
  2101. test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
  2102. bool cur_promisc;
  2103. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2104. test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  2105. &vsi->state));
  2106. if ((vsi->type == I40E_VSI_MAIN) &&
  2107. (pf->lan_veb != I40E_NO_VEB) &&
  2108. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  2109. /* set defport ON for Main VSI instead of true promisc
  2110. * this way we will get all unicast/multicast and VLAN
  2111. * promisc behavior but will not get VF or VMDq traffic
  2112. * replicated on the Main VSI.
  2113. */
  2114. if (pf->cur_promisc != cur_promisc) {
  2115. pf->cur_promisc = cur_promisc;
  2116. if (cur_promisc)
  2117. aq_ret =
  2118. i40e_aq_set_default_vsi(hw,
  2119. vsi->seid,
  2120. NULL);
  2121. else
  2122. aq_ret =
  2123. i40e_aq_clear_default_vsi(hw,
  2124. vsi->seid,
  2125. NULL);
  2126. if (aq_ret) {
  2127. retval = i40e_aq_rc_to_posix(aq_ret,
  2128. hw->aq.asq_last_status);
  2129. dev_info(&pf->pdev->dev,
  2130. "Set default VSI failed on %s, err %s, aq_err %s\n",
  2131. vsi_name,
  2132. i40e_stat_str(hw, aq_ret),
  2133. i40e_aq_str(hw,
  2134. hw->aq.asq_last_status));
  2135. }
  2136. }
  2137. } else {
  2138. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  2139. hw,
  2140. vsi->seid,
  2141. cur_promisc, NULL,
  2142. true);
  2143. if (aq_ret) {
  2144. retval =
  2145. i40e_aq_rc_to_posix(aq_ret,
  2146. hw->aq.asq_last_status);
  2147. dev_info(&pf->pdev->dev,
  2148. "set unicast promisc failed on %s, err %s, aq_err %s\n",
  2149. vsi_name,
  2150. i40e_stat_str(hw, aq_ret),
  2151. i40e_aq_str(hw,
  2152. hw->aq.asq_last_status));
  2153. }
  2154. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  2155. hw,
  2156. vsi->seid,
  2157. cur_promisc, NULL);
  2158. if (aq_ret) {
  2159. retval =
  2160. i40e_aq_rc_to_posix(aq_ret,
  2161. hw->aq.asq_last_status);
  2162. dev_info(&pf->pdev->dev,
  2163. "set multicast promisc failed on %s, err %s, aq_err %s\n",
  2164. vsi_name,
  2165. i40e_stat_str(hw, aq_ret),
  2166. i40e_aq_str(hw,
  2167. hw->aq.asq_last_status));
  2168. }
  2169. }
  2170. aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
  2171. vsi->seid,
  2172. cur_promisc, NULL);
  2173. if (aq_ret) {
  2174. retval = i40e_aq_rc_to_posix(aq_ret,
  2175. pf->hw.aq.asq_last_status);
  2176. dev_info(&pf->pdev->dev,
  2177. "set brdcast promisc failed, err %s, aq_err %s\n",
  2178. i40e_stat_str(hw, aq_ret),
  2179. i40e_aq_str(hw,
  2180. hw->aq.asq_last_status));
  2181. }
  2182. }
  2183. out:
  2184. /* if something went wrong then set the changed flag so we try again */
  2185. if (retval)
  2186. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2187. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2188. return retval;
  2189. err_no_memory:
  2190. /* Restore elements on the temporary add and delete lists */
  2191. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2192. err_no_memory_locked:
  2193. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2194. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2195. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2196. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2197. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2198. return -ENOMEM;
  2199. }
  2200. /**
  2201. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2202. * @pf: board private structure
  2203. **/
  2204. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2205. {
  2206. int v;
  2207. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  2208. return;
  2209. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  2210. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2211. if (pf->vsi[v] &&
  2212. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2213. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2214. if (ret) {
  2215. /* come back and try again later */
  2216. pf->flags |= I40E_FLAG_FILTER_SYNC;
  2217. break;
  2218. }
  2219. }
  2220. }
  2221. }
  2222. /**
  2223. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2224. * @netdev: network interface device structure
  2225. * @new_mtu: new value for maximum frame size
  2226. *
  2227. * Returns 0 on success, negative on failure
  2228. **/
  2229. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2230. {
  2231. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2232. struct i40e_vsi *vsi = np->vsi;
  2233. netdev_info(netdev, "changing MTU from %d to %d\n",
  2234. netdev->mtu, new_mtu);
  2235. netdev->mtu = new_mtu;
  2236. if (netif_running(netdev))
  2237. i40e_vsi_reinit_locked(vsi);
  2238. i40e_notify_client_of_l2_param_changes(vsi);
  2239. return 0;
  2240. }
  2241. /**
  2242. * i40e_ioctl - Access the hwtstamp interface
  2243. * @netdev: network interface device structure
  2244. * @ifr: interface request data
  2245. * @cmd: ioctl command
  2246. **/
  2247. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2248. {
  2249. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2250. struct i40e_pf *pf = np->vsi->back;
  2251. switch (cmd) {
  2252. case SIOCGHWTSTAMP:
  2253. return i40e_ptp_get_ts_config(pf, ifr);
  2254. case SIOCSHWTSTAMP:
  2255. return i40e_ptp_set_ts_config(pf, ifr);
  2256. default:
  2257. return -EOPNOTSUPP;
  2258. }
  2259. }
  2260. /**
  2261. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2262. * @vsi: the vsi being adjusted
  2263. **/
  2264. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2265. {
  2266. struct i40e_vsi_context ctxt;
  2267. i40e_status ret;
  2268. if ((vsi->info.valid_sections &
  2269. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2270. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2271. return; /* already enabled */
  2272. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2273. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2274. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2275. ctxt.seid = vsi->seid;
  2276. ctxt.info = vsi->info;
  2277. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2278. if (ret) {
  2279. dev_info(&vsi->back->pdev->dev,
  2280. "update vlan stripping failed, err %s aq_err %s\n",
  2281. i40e_stat_str(&vsi->back->hw, ret),
  2282. i40e_aq_str(&vsi->back->hw,
  2283. vsi->back->hw.aq.asq_last_status));
  2284. }
  2285. }
  2286. /**
  2287. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2288. * @vsi: the vsi being adjusted
  2289. **/
  2290. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2291. {
  2292. struct i40e_vsi_context ctxt;
  2293. i40e_status ret;
  2294. if ((vsi->info.valid_sections &
  2295. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2296. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2297. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2298. return; /* already disabled */
  2299. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2300. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2301. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2302. ctxt.seid = vsi->seid;
  2303. ctxt.info = vsi->info;
  2304. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2305. if (ret) {
  2306. dev_info(&vsi->back->pdev->dev,
  2307. "update vlan stripping failed, err %s aq_err %s\n",
  2308. i40e_stat_str(&vsi->back->hw, ret),
  2309. i40e_aq_str(&vsi->back->hw,
  2310. vsi->back->hw.aq.asq_last_status));
  2311. }
  2312. }
  2313. /**
  2314. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  2315. * @netdev: network interface to be adjusted
  2316. * @features: netdev features to test if VLAN offload is enabled or not
  2317. **/
  2318. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  2319. {
  2320. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2321. struct i40e_vsi *vsi = np->vsi;
  2322. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2323. i40e_vlan_stripping_enable(vsi);
  2324. else
  2325. i40e_vlan_stripping_disable(vsi);
  2326. }
  2327. /**
  2328. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2329. * @vsi: the vsi being configured
  2330. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2331. *
  2332. * This is a helper function for adding a new MAC/VLAN filter with the
  2333. * specified VLAN for each existing MAC address already in the hash table.
  2334. * This function does *not* perform any accounting to update filters based on
  2335. * VLAN mode.
  2336. *
  2337. * NOTE: this function expects to be called while under the
  2338. * mac_filter_hash_lock
  2339. **/
  2340. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2341. {
  2342. struct i40e_mac_filter *f, *add_f;
  2343. struct hlist_node *h;
  2344. int bkt;
  2345. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2346. if (f->state == I40E_FILTER_REMOVE)
  2347. continue;
  2348. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2349. if (!add_f) {
  2350. dev_info(&vsi->back->pdev->dev,
  2351. "Could not add vlan filter %d for %pM\n",
  2352. vid, f->macaddr);
  2353. return -ENOMEM;
  2354. }
  2355. }
  2356. return 0;
  2357. }
  2358. /**
  2359. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2360. * @vsi: the VSI being configured
  2361. * @vid: VLAN id to be added
  2362. **/
  2363. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2364. {
  2365. int err;
  2366. if (!vid || vsi->info.pvid)
  2367. return -EINVAL;
  2368. /* Locked once because all functions invoked below iterates list*/
  2369. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2370. err = i40e_add_vlan_all_mac(vsi, vid);
  2371. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2372. if (err)
  2373. return err;
  2374. /* schedule our worker thread which will take care of
  2375. * applying the new filter changes
  2376. */
  2377. i40e_service_event_schedule(vsi->back);
  2378. return 0;
  2379. }
  2380. /**
  2381. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2382. * @vsi: the vsi being configured
  2383. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2384. *
  2385. * This function should be used to remove all VLAN filters which match the
  2386. * given VID. It does not schedule the service event and does not take the
  2387. * mac_filter_hash_lock so it may be combined with other operations under
  2388. * a single invocation of the mac_filter_hash_lock.
  2389. *
  2390. * NOTE: this function expects to be called while under the
  2391. * mac_filter_hash_lock
  2392. */
  2393. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2394. {
  2395. struct i40e_mac_filter *f;
  2396. struct hlist_node *h;
  2397. int bkt;
  2398. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2399. if (f->vlan == vid)
  2400. __i40e_del_filter(vsi, f);
  2401. }
  2402. }
  2403. /**
  2404. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2405. * @vsi: the VSI being configured
  2406. * @vid: VLAN id to be removed
  2407. **/
  2408. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2409. {
  2410. if (!vid || vsi->info.pvid)
  2411. return;
  2412. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2413. i40e_rm_vlan_all_mac(vsi, vid);
  2414. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2415. /* schedule our worker thread which will take care of
  2416. * applying the new filter changes
  2417. */
  2418. i40e_service_event_schedule(vsi->back);
  2419. }
  2420. /**
  2421. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2422. * @netdev: network interface to be adjusted
  2423. * @vid: vlan id to be added
  2424. *
  2425. * net_device_ops implementation for adding vlan ids
  2426. **/
  2427. #ifdef I40E_FCOE
  2428. int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2429. __always_unused __be16 proto, u16 vid)
  2430. #else
  2431. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2432. __always_unused __be16 proto, u16 vid)
  2433. #endif
  2434. {
  2435. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2436. struct i40e_vsi *vsi = np->vsi;
  2437. int ret = 0;
  2438. if (vid >= VLAN_N_VID)
  2439. return -EINVAL;
  2440. /* If the network stack called us with vid = 0 then
  2441. * it is asking to receive priority tagged packets with
  2442. * vlan id 0. Our HW receives them by default when configured
  2443. * to receive untagged packets so there is no need to add an
  2444. * extra filter for vlan 0 tagged packets.
  2445. */
  2446. if (vid)
  2447. ret = i40e_vsi_add_vlan(vsi, vid);
  2448. if (!ret)
  2449. set_bit(vid, vsi->active_vlans);
  2450. return ret;
  2451. }
  2452. /**
  2453. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2454. * @netdev: network interface to be adjusted
  2455. * @vid: vlan id to be removed
  2456. *
  2457. * net_device_ops implementation for removing vlan ids
  2458. **/
  2459. #ifdef I40E_FCOE
  2460. int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2461. __always_unused __be16 proto, u16 vid)
  2462. #else
  2463. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2464. __always_unused __be16 proto, u16 vid)
  2465. #endif
  2466. {
  2467. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2468. struct i40e_vsi *vsi = np->vsi;
  2469. /* return code is ignored as there is nothing a user
  2470. * can do about failure to remove and a log message was
  2471. * already printed from the other function
  2472. */
  2473. i40e_vsi_kill_vlan(vsi, vid);
  2474. clear_bit(vid, vsi->active_vlans);
  2475. return 0;
  2476. }
  2477. /**
  2478. * i40e_macaddr_init - explicitly write the mac address filters
  2479. *
  2480. * @vsi: pointer to the vsi
  2481. * @macaddr: the MAC address
  2482. *
  2483. * This is needed when the macaddr has been obtained by other
  2484. * means than the default, e.g., from Open Firmware or IDPROM.
  2485. * Returns 0 on success, negative on failure
  2486. **/
  2487. static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
  2488. {
  2489. int ret;
  2490. struct i40e_aqc_add_macvlan_element_data element;
  2491. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  2492. I40E_AQC_WRITE_TYPE_LAA_WOL,
  2493. macaddr, NULL);
  2494. if (ret) {
  2495. dev_info(&vsi->back->pdev->dev,
  2496. "Addr change for VSI failed: %d\n", ret);
  2497. return -EADDRNOTAVAIL;
  2498. }
  2499. memset(&element, 0, sizeof(element));
  2500. ether_addr_copy(element.mac_addr, macaddr);
  2501. element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
  2502. ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
  2503. if (ret) {
  2504. dev_info(&vsi->back->pdev->dev,
  2505. "add filter failed err %s aq_err %s\n",
  2506. i40e_stat_str(&vsi->back->hw, ret),
  2507. i40e_aq_str(&vsi->back->hw,
  2508. vsi->back->hw.aq.asq_last_status));
  2509. }
  2510. return ret;
  2511. }
  2512. /**
  2513. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2514. * @vsi: the vsi being brought back up
  2515. **/
  2516. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2517. {
  2518. u16 vid;
  2519. if (!vsi->netdev)
  2520. return;
  2521. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  2522. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2523. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  2524. vid);
  2525. }
  2526. /**
  2527. * i40e_vsi_add_pvid - Add pvid for the VSI
  2528. * @vsi: the vsi being adjusted
  2529. * @vid: the vlan id to set as a PVID
  2530. **/
  2531. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2532. {
  2533. struct i40e_vsi_context ctxt;
  2534. i40e_status ret;
  2535. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2536. vsi->info.pvid = cpu_to_le16(vid);
  2537. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2538. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2539. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2540. ctxt.seid = vsi->seid;
  2541. ctxt.info = vsi->info;
  2542. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2543. if (ret) {
  2544. dev_info(&vsi->back->pdev->dev,
  2545. "add pvid failed, err %s aq_err %s\n",
  2546. i40e_stat_str(&vsi->back->hw, ret),
  2547. i40e_aq_str(&vsi->back->hw,
  2548. vsi->back->hw.aq.asq_last_status));
  2549. return -ENOENT;
  2550. }
  2551. return 0;
  2552. }
  2553. /**
  2554. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2555. * @vsi: the vsi being adjusted
  2556. *
  2557. * Just use the vlan_rx_register() service to put it back to normal
  2558. **/
  2559. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2560. {
  2561. i40e_vlan_stripping_disable(vsi);
  2562. vsi->info.pvid = 0;
  2563. }
  2564. /**
  2565. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2566. * @vsi: ptr to the VSI
  2567. *
  2568. * If this function returns with an error, then it's possible one or
  2569. * more of the rings is populated (while the rest are not). It is the
  2570. * callers duty to clean those orphaned rings.
  2571. *
  2572. * Return 0 on success, negative on failure
  2573. **/
  2574. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2575. {
  2576. int i, err = 0;
  2577. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2578. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2579. return err;
  2580. }
  2581. /**
  2582. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2583. * @vsi: ptr to the VSI
  2584. *
  2585. * Free VSI's transmit software resources
  2586. **/
  2587. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2588. {
  2589. int i;
  2590. if (!vsi->tx_rings)
  2591. return;
  2592. for (i = 0; i < vsi->num_queue_pairs; i++)
  2593. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2594. i40e_free_tx_resources(vsi->tx_rings[i]);
  2595. }
  2596. /**
  2597. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2598. * @vsi: ptr to the VSI
  2599. *
  2600. * If this function returns with an error, then it's possible one or
  2601. * more of the rings is populated (while the rest are not). It is the
  2602. * callers duty to clean those orphaned rings.
  2603. *
  2604. * Return 0 on success, negative on failure
  2605. **/
  2606. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2607. {
  2608. int i, err = 0;
  2609. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2610. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2611. #ifdef I40E_FCOE
  2612. i40e_fcoe_setup_ddp_resources(vsi);
  2613. #endif
  2614. return err;
  2615. }
  2616. /**
  2617. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2618. * @vsi: ptr to the VSI
  2619. *
  2620. * Free all receive software resources
  2621. **/
  2622. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2623. {
  2624. int i;
  2625. if (!vsi->rx_rings)
  2626. return;
  2627. for (i = 0; i < vsi->num_queue_pairs; i++)
  2628. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2629. i40e_free_rx_resources(vsi->rx_rings[i]);
  2630. #ifdef I40E_FCOE
  2631. i40e_fcoe_free_ddp_resources(vsi);
  2632. #endif
  2633. }
  2634. /**
  2635. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2636. * @ring: The Tx ring to configure
  2637. *
  2638. * This enables/disables XPS for a given Tx descriptor ring
  2639. * based on the TCs enabled for the VSI that ring belongs to.
  2640. **/
  2641. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2642. {
  2643. struct i40e_vsi *vsi = ring->vsi;
  2644. cpumask_var_t mask;
  2645. if (!ring->q_vector || !ring->netdev)
  2646. return;
  2647. /* Single TC mode enable XPS */
  2648. if (vsi->tc_config.numtc <= 1) {
  2649. if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
  2650. netif_set_xps_queue(ring->netdev,
  2651. &ring->q_vector->affinity_mask,
  2652. ring->queue_index);
  2653. } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
  2654. /* Disable XPS to allow selection based on TC */
  2655. bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
  2656. netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
  2657. free_cpumask_var(mask);
  2658. }
  2659. /* schedule our worker thread which will take care of
  2660. * applying the new filter changes
  2661. */
  2662. i40e_service_event_schedule(vsi->back);
  2663. }
  2664. /**
  2665. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2666. * @ring: The Tx ring to configure
  2667. *
  2668. * Configure the Tx descriptor ring in the HMC context.
  2669. **/
  2670. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2671. {
  2672. struct i40e_vsi *vsi = ring->vsi;
  2673. u16 pf_q = vsi->base_queue + ring->queue_index;
  2674. struct i40e_hw *hw = &vsi->back->hw;
  2675. struct i40e_hmc_obj_txq tx_ctx;
  2676. i40e_status err = 0;
  2677. u32 qtx_ctl = 0;
  2678. /* some ATR related tx ring init */
  2679. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2680. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2681. ring->atr_count = 0;
  2682. } else {
  2683. ring->atr_sample_rate = 0;
  2684. }
  2685. /* configure XPS */
  2686. i40e_config_xps_tx_ring(ring);
  2687. /* clear the context structure first */
  2688. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2689. tx_ctx.new_context = 1;
  2690. tx_ctx.base = (ring->dma / 128);
  2691. tx_ctx.qlen = ring->count;
  2692. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2693. I40E_FLAG_FD_ATR_ENABLED));
  2694. #ifdef I40E_FCOE
  2695. tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
  2696. #endif
  2697. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2698. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2699. if (vsi->type != I40E_VSI_FDIR)
  2700. tx_ctx.head_wb_ena = 1;
  2701. tx_ctx.head_wb_addr = ring->dma +
  2702. (ring->count * sizeof(struct i40e_tx_desc));
  2703. /* As part of VSI creation/update, FW allocates certain
  2704. * Tx arbitration queue sets for each TC enabled for
  2705. * the VSI. The FW returns the handles to these queue
  2706. * sets as part of the response buffer to Add VSI,
  2707. * Update VSI, etc. AQ commands. It is expected that
  2708. * these queue set handles be associated with the Tx
  2709. * queues by the driver as part of the TX queue context
  2710. * initialization. This has to be done regardless of
  2711. * DCB as by default everything is mapped to TC0.
  2712. */
  2713. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2714. tx_ctx.rdylist_act = 0;
  2715. /* clear the context in the HMC */
  2716. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2717. if (err) {
  2718. dev_info(&vsi->back->pdev->dev,
  2719. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2720. ring->queue_index, pf_q, err);
  2721. return -ENOMEM;
  2722. }
  2723. /* set the context in the HMC */
  2724. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2725. if (err) {
  2726. dev_info(&vsi->back->pdev->dev,
  2727. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2728. ring->queue_index, pf_q, err);
  2729. return -ENOMEM;
  2730. }
  2731. /* Now associate this queue with this PCI function */
  2732. if (vsi->type == I40E_VSI_VMDQ2) {
  2733. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2734. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2735. I40E_QTX_CTL_VFVM_INDX_MASK;
  2736. } else {
  2737. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2738. }
  2739. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2740. I40E_QTX_CTL_PF_INDX_MASK);
  2741. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2742. i40e_flush(hw);
  2743. /* cache tail off for easier writes later */
  2744. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2745. return 0;
  2746. }
  2747. /**
  2748. * i40e_configure_rx_ring - Configure a receive ring context
  2749. * @ring: The Rx ring to configure
  2750. *
  2751. * Configure the Rx descriptor ring in the HMC context.
  2752. **/
  2753. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2754. {
  2755. struct i40e_vsi *vsi = ring->vsi;
  2756. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2757. u16 pf_q = vsi->base_queue + ring->queue_index;
  2758. struct i40e_hw *hw = &vsi->back->hw;
  2759. struct i40e_hmc_obj_rxq rx_ctx;
  2760. i40e_status err = 0;
  2761. ring->state = 0;
  2762. /* clear the context structure first */
  2763. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2764. ring->rx_buf_len = vsi->rx_buf_len;
  2765. rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
  2766. rx_ctx.base = (ring->dma / 128);
  2767. rx_ctx.qlen = ring->count;
  2768. /* use 32 byte descriptors */
  2769. rx_ctx.dsize = 1;
  2770. /* descriptor type is always zero
  2771. * rx_ctx.dtype = 0;
  2772. */
  2773. rx_ctx.hsplit_0 = 0;
  2774. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2775. if (hw->revision_id == 0)
  2776. rx_ctx.lrxqthresh = 0;
  2777. else
  2778. rx_ctx.lrxqthresh = 2;
  2779. rx_ctx.crcstrip = 1;
  2780. rx_ctx.l2tsel = 1;
  2781. /* this controls whether VLAN is stripped from inner headers */
  2782. rx_ctx.showiv = 0;
  2783. #ifdef I40E_FCOE
  2784. rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
  2785. #endif
  2786. /* set the prefena field to 1 because the manual says to */
  2787. rx_ctx.prefena = 1;
  2788. /* clear the context in the HMC */
  2789. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2790. if (err) {
  2791. dev_info(&vsi->back->pdev->dev,
  2792. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2793. ring->queue_index, pf_q, err);
  2794. return -ENOMEM;
  2795. }
  2796. /* set the context in the HMC */
  2797. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2798. if (err) {
  2799. dev_info(&vsi->back->pdev->dev,
  2800. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2801. ring->queue_index, pf_q, err);
  2802. return -ENOMEM;
  2803. }
  2804. /* cache tail for quicker writes, and clear the reg before use */
  2805. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2806. writel(0, ring->tail);
  2807. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2808. return 0;
  2809. }
  2810. /**
  2811. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2812. * @vsi: VSI structure describing this set of rings and resources
  2813. *
  2814. * Configure the Tx VSI for operation.
  2815. **/
  2816. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2817. {
  2818. int err = 0;
  2819. u16 i;
  2820. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2821. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2822. return err;
  2823. }
  2824. /**
  2825. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2826. * @vsi: the VSI being configured
  2827. *
  2828. * Configure the Rx VSI for operation.
  2829. **/
  2830. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2831. {
  2832. int err = 0;
  2833. u16 i;
  2834. if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
  2835. vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
  2836. + ETH_FCS_LEN + VLAN_HLEN;
  2837. else
  2838. vsi->max_frame = I40E_RXBUFFER_2048;
  2839. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2840. #ifdef I40E_FCOE
  2841. /* setup rx buffer for FCoE */
  2842. if ((vsi->type == I40E_VSI_FCOE) &&
  2843. (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
  2844. vsi->rx_buf_len = I40E_RXBUFFER_3072;
  2845. vsi->max_frame = I40E_RXBUFFER_3072;
  2846. }
  2847. #endif /* I40E_FCOE */
  2848. /* round up for the chip's needs */
  2849. vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
  2850. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2851. /* set up individual rings */
  2852. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2853. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2854. return err;
  2855. }
  2856. /**
  2857. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2858. * @vsi: ptr to the VSI
  2859. **/
  2860. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2861. {
  2862. struct i40e_ring *tx_ring, *rx_ring;
  2863. u16 qoffset, qcount;
  2864. int i, n;
  2865. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2866. /* Reset the TC information */
  2867. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2868. rx_ring = vsi->rx_rings[i];
  2869. tx_ring = vsi->tx_rings[i];
  2870. rx_ring->dcb_tc = 0;
  2871. tx_ring->dcb_tc = 0;
  2872. }
  2873. }
  2874. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2875. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2876. continue;
  2877. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2878. qcount = vsi->tc_config.tc_info[n].qcount;
  2879. for (i = qoffset; i < (qoffset + qcount); i++) {
  2880. rx_ring = vsi->rx_rings[i];
  2881. tx_ring = vsi->tx_rings[i];
  2882. rx_ring->dcb_tc = n;
  2883. tx_ring->dcb_tc = n;
  2884. }
  2885. }
  2886. }
  2887. /**
  2888. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2889. * @vsi: ptr to the VSI
  2890. **/
  2891. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2892. {
  2893. struct i40e_pf *pf = vsi->back;
  2894. int err;
  2895. if (vsi->netdev)
  2896. i40e_set_rx_mode(vsi->netdev);
  2897. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  2898. err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  2899. if (err) {
  2900. dev_warn(&pf->pdev->dev,
  2901. "could not set up macaddr; err %d\n", err);
  2902. }
  2903. }
  2904. }
  2905. /**
  2906. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2907. * @vsi: Pointer to the targeted VSI
  2908. *
  2909. * This function replays the hlist on the hw where all the SB Flow Director
  2910. * filters were saved.
  2911. **/
  2912. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2913. {
  2914. struct i40e_fdir_filter *filter;
  2915. struct i40e_pf *pf = vsi->back;
  2916. struct hlist_node *node;
  2917. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2918. return;
  2919. hlist_for_each_entry_safe(filter, node,
  2920. &pf->fdir_filter_list, fdir_node) {
  2921. i40e_add_del_fdir(vsi, filter, true);
  2922. }
  2923. }
  2924. /**
  2925. * i40e_vsi_configure - Set up the VSI for action
  2926. * @vsi: the VSI being configured
  2927. **/
  2928. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2929. {
  2930. int err;
  2931. i40e_set_vsi_rx_mode(vsi);
  2932. i40e_restore_vlan(vsi);
  2933. i40e_vsi_config_dcb_rings(vsi);
  2934. err = i40e_vsi_configure_tx(vsi);
  2935. if (!err)
  2936. err = i40e_vsi_configure_rx(vsi);
  2937. return err;
  2938. }
  2939. /**
  2940. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2941. * @vsi: the VSI being configured
  2942. **/
  2943. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2944. {
  2945. struct i40e_pf *pf = vsi->back;
  2946. struct i40e_hw *hw = &pf->hw;
  2947. u16 vector;
  2948. int i, q;
  2949. u32 qp;
  2950. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2951. * and PFINT_LNKLSTn registers, e.g.:
  2952. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2953. */
  2954. qp = vsi->base_queue;
  2955. vector = vsi->base_vector;
  2956. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2957. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  2958. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2959. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
  2960. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2961. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2962. q_vector->rx.itr);
  2963. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
  2964. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2965. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2966. q_vector->tx.itr);
  2967. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  2968. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  2969. /* Linked list for the queuepairs assigned to this vector */
  2970. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2971. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2972. u32 val;
  2973. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2974. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2975. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2976. (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  2977. (I40E_QUEUE_TYPE_TX
  2978. << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2979. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2980. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2981. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2982. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2983. ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
  2984. (I40E_QUEUE_TYPE_RX
  2985. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2986. /* Terminate the linked list */
  2987. if (q == (q_vector->num_ringpairs - 1))
  2988. val |= (I40E_QUEUE_END_OF_LIST
  2989. << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2990. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2991. qp++;
  2992. }
  2993. }
  2994. i40e_flush(hw);
  2995. }
  2996. /**
  2997. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2998. * @hw: ptr to the hardware info
  2999. **/
  3000. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  3001. {
  3002. struct i40e_hw *hw = &pf->hw;
  3003. u32 val;
  3004. /* clear things first */
  3005. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  3006. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  3007. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  3008. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  3009. I40E_PFINT_ICR0_ENA_GRST_MASK |
  3010. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  3011. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  3012. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  3013. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  3014. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3015. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  3016. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3017. if (pf->flags & I40E_FLAG_PTP)
  3018. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3019. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  3020. /* SW_ITR_IDX = 0, but don't change INTENA */
  3021. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  3022. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  3023. /* OTHER_ITR_IDX = 0 */
  3024. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  3025. }
  3026. /**
  3027. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  3028. * @vsi: the VSI being configured
  3029. **/
  3030. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  3031. {
  3032. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3033. struct i40e_pf *pf = vsi->back;
  3034. struct i40e_hw *hw = &pf->hw;
  3035. u32 val;
  3036. /* set the ITR configuration */
  3037. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  3038. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
  3039. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  3040. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  3041. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
  3042. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  3043. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  3044. i40e_enable_misc_int_causes(pf);
  3045. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  3046. wr32(hw, I40E_PFINT_LNKLST0, 0);
  3047. /* Associate the queue pair to the vector and enable the queue int */
  3048. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3049. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3050. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3051. wr32(hw, I40E_QINT_RQCTL(0), val);
  3052. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3053. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3054. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3055. wr32(hw, I40E_QINT_TQCTL(0), val);
  3056. i40e_flush(hw);
  3057. }
  3058. /**
  3059. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  3060. * @pf: board private structure
  3061. **/
  3062. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  3063. {
  3064. struct i40e_hw *hw = &pf->hw;
  3065. wr32(hw, I40E_PFINT_DYN_CTL0,
  3066. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  3067. i40e_flush(hw);
  3068. }
  3069. /**
  3070. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  3071. * @pf: board private structure
  3072. * @clearpba: true when all pending interrupt events should be cleared
  3073. **/
  3074. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
  3075. {
  3076. struct i40e_hw *hw = &pf->hw;
  3077. u32 val;
  3078. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3079. (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
  3080. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  3081. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  3082. i40e_flush(hw);
  3083. }
  3084. /**
  3085. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3086. * @irq: interrupt number
  3087. * @data: pointer to a q_vector
  3088. **/
  3089. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3090. {
  3091. struct i40e_q_vector *q_vector = data;
  3092. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3093. return IRQ_HANDLED;
  3094. napi_schedule_irqoff(&q_vector->napi);
  3095. return IRQ_HANDLED;
  3096. }
  3097. /**
  3098. * i40e_irq_affinity_notify - Callback for affinity changes
  3099. * @notify: context as to what irq was changed
  3100. * @mask: the new affinity mask
  3101. *
  3102. * This is a callback function used by the irq_set_affinity_notifier function
  3103. * so that we may register to receive changes to the irq affinity masks.
  3104. **/
  3105. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3106. const cpumask_t *mask)
  3107. {
  3108. struct i40e_q_vector *q_vector =
  3109. container_of(notify, struct i40e_q_vector, affinity_notify);
  3110. q_vector->affinity_mask = *mask;
  3111. }
  3112. /**
  3113. * i40e_irq_affinity_release - Callback for affinity notifier release
  3114. * @ref: internal core kernel usage
  3115. *
  3116. * This is a callback function used by the irq_set_affinity_notifier function
  3117. * to inform the current notification subscriber that they will no longer
  3118. * receive notifications.
  3119. **/
  3120. static void i40e_irq_affinity_release(struct kref *ref) {}
  3121. /**
  3122. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3123. * @vsi: the VSI being configured
  3124. * @basename: name for the vector
  3125. *
  3126. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3127. **/
  3128. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3129. {
  3130. int q_vectors = vsi->num_q_vectors;
  3131. struct i40e_pf *pf = vsi->back;
  3132. int base = vsi->base_vector;
  3133. int rx_int_idx = 0;
  3134. int tx_int_idx = 0;
  3135. int vector, err;
  3136. int irq_num;
  3137. for (vector = 0; vector < q_vectors; vector++) {
  3138. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3139. irq_num = pf->msix_entries[base + vector].vector;
  3140. if (q_vector->tx.ring && q_vector->rx.ring) {
  3141. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3142. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3143. tx_int_idx++;
  3144. } else if (q_vector->rx.ring) {
  3145. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3146. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3147. } else if (q_vector->tx.ring) {
  3148. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3149. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3150. } else {
  3151. /* skip this unused q_vector */
  3152. continue;
  3153. }
  3154. err = request_irq(irq_num,
  3155. vsi->irq_handler,
  3156. 0,
  3157. q_vector->name,
  3158. q_vector);
  3159. if (err) {
  3160. dev_info(&pf->pdev->dev,
  3161. "MSIX request_irq failed, error: %d\n", err);
  3162. goto free_queue_irqs;
  3163. }
  3164. /* register for affinity change notifications */
  3165. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3166. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3167. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3168. /* assign the mask for this irq */
  3169. irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
  3170. }
  3171. vsi->irqs_ready = true;
  3172. return 0;
  3173. free_queue_irqs:
  3174. while (vector) {
  3175. vector--;
  3176. irq_num = pf->msix_entries[base + vector].vector;
  3177. irq_set_affinity_notifier(irq_num, NULL);
  3178. irq_set_affinity_hint(irq_num, NULL);
  3179. free_irq(irq_num, &vsi->q_vectors[vector]);
  3180. }
  3181. return err;
  3182. }
  3183. /**
  3184. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3185. * @vsi: the VSI being un-configured
  3186. **/
  3187. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3188. {
  3189. struct i40e_pf *pf = vsi->back;
  3190. struct i40e_hw *hw = &pf->hw;
  3191. int base = vsi->base_vector;
  3192. int i;
  3193. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3194. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
  3195. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
  3196. }
  3197. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3198. for (i = vsi->base_vector;
  3199. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3200. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3201. i40e_flush(hw);
  3202. for (i = 0; i < vsi->num_q_vectors; i++)
  3203. synchronize_irq(pf->msix_entries[i + base].vector);
  3204. } else {
  3205. /* Legacy and MSI mode - this stops all interrupt handling */
  3206. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3207. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3208. i40e_flush(hw);
  3209. synchronize_irq(pf->pdev->irq);
  3210. }
  3211. }
  3212. /**
  3213. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3214. * @vsi: the VSI being configured
  3215. **/
  3216. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3217. {
  3218. struct i40e_pf *pf = vsi->back;
  3219. int i;
  3220. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3221. for (i = 0; i < vsi->num_q_vectors; i++)
  3222. i40e_irq_dynamic_enable(vsi, i);
  3223. } else {
  3224. i40e_irq_dynamic_enable_icr0(pf, true);
  3225. }
  3226. i40e_flush(&pf->hw);
  3227. return 0;
  3228. }
  3229. /**
  3230. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  3231. * @pf: board private structure
  3232. **/
  3233. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  3234. {
  3235. /* Disable ICR 0 */
  3236. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3237. i40e_flush(&pf->hw);
  3238. }
  3239. /**
  3240. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3241. * @irq: interrupt number
  3242. * @data: pointer to a q_vector
  3243. *
  3244. * This is the handler used for all MSI/Legacy interrupts, and deals
  3245. * with both queue and non-queue interrupts. This is also used in
  3246. * MSIX mode to handle the non-queue interrupts.
  3247. **/
  3248. static irqreturn_t i40e_intr(int irq, void *data)
  3249. {
  3250. struct i40e_pf *pf = (struct i40e_pf *)data;
  3251. struct i40e_hw *hw = &pf->hw;
  3252. irqreturn_t ret = IRQ_NONE;
  3253. u32 icr0, icr0_remaining;
  3254. u32 val, ena_mask;
  3255. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3256. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3257. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3258. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3259. goto enable_intr;
  3260. /* if interrupt but no bits showing, must be SWINT */
  3261. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3262. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3263. pf->sw_int_count++;
  3264. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3265. (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3266. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3267. icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3268. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3269. }
  3270. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3271. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3272. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3273. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3274. /* We do not have a way to disarm Queue causes while leaving
  3275. * interrupt enabled for all other causes, ideally
  3276. * interrupt should be disabled while we are in NAPI but
  3277. * this is not a performance path and napi_schedule()
  3278. * can deal with rescheduling.
  3279. */
  3280. if (!test_bit(__I40E_DOWN, &pf->state))
  3281. napi_schedule_irqoff(&q_vector->napi);
  3282. }
  3283. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3284. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3285. set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  3286. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3287. }
  3288. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3289. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3290. set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  3291. }
  3292. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3293. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3294. set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  3295. }
  3296. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3297. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3298. set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  3299. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3300. val = rd32(hw, I40E_GLGEN_RSTAT);
  3301. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3302. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3303. if (val == I40E_RESET_CORER) {
  3304. pf->corer_count++;
  3305. } else if (val == I40E_RESET_GLOBR) {
  3306. pf->globr_count++;
  3307. } else if (val == I40E_RESET_EMPR) {
  3308. pf->empr_count++;
  3309. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
  3310. }
  3311. }
  3312. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3313. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3314. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3315. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3316. rd32(hw, I40E_PFHMC_ERRORINFO),
  3317. rd32(hw, I40E_PFHMC_ERRORDATA));
  3318. }
  3319. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3320. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3321. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3322. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3323. i40e_ptp_tx_hwtstamp(pf);
  3324. }
  3325. }
  3326. /* If a critical error is pending we have no choice but to reset the
  3327. * device.
  3328. * Report and mask out any remaining unexpected interrupts.
  3329. */
  3330. icr0_remaining = icr0 & ena_mask;
  3331. if (icr0_remaining) {
  3332. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3333. icr0_remaining);
  3334. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3335. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3336. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3337. dev_info(&pf->pdev->dev, "device will be reset\n");
  3338. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  3339. i40e_service_event_schedule(pf);
  3340. }
  3341. ena_mask &= ~icr0_remaining;
  3342. }
  3343. ret = IRQ_HANDLED;
  3344. enable_intr:
  3345. /* re-enable interrupt causes */
  3346. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3347. if (!test_bit(__I40E_DOWN, &pf->state)) {
  3348. i40e_service_event_schedule(pf);
  3349. i40e_irq_dynamic_enable_icr0(pf, false);
  3350. }
  3351. return ret;
  3352. }
  3353. /**
  3354. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3355. * @tx_ring: tx ring to clean
  3356. * @budget: how many cleans we're allowed
  3357. *
  3358. * Returns true if there's any budget left (e.g. the clean is finished)
  3359. **/
  3360. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3361. {
  3362. struct i40e_vsi *vsi = tx_ring->vsi;
  3363. u16 i = tx_ring->next_to_clean;
  3364. struct i40e_tx_buffer *tx_buf;
  3365. struct i40e_tx_desc *tx_desc;
  3366. tx_buf = &tx_ring->tx_bi[i];
  3367. tx_desc = I40E_TX_DESC(tx_ring, i);
  3368. i -= tx_ring->count;
  3369. do {
  3370. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3371. /* if next_to_watch is not set then there is no work pending */
  3372. if (!eop_desc)
  3373. break;
  3374. /* prevent any other reads prior to eop_desc */
  3375. read_barrier_depends();
  3376. /* if the descriptor isn't done, no work yet to do */
  3377. if (!(eop_desc->cmd_type_offset_bsz &
  3378. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3379. break;
  3380. /* clear next_to_watch to prevent false hangs */
  3381. tx_buf->next_to_watch = NULL;
  3382. tx_desc->buffer_addr = 0;
  3383. tx_desc->cmd_type_offset_bsz = 0;
  3384. /* move past filter desc */
  3385. tx_buf++;
  3386. tx_desc++;
  3387. i++;
  3388. if (unlikely(!i)) {
  3389. i -= tx_ring->count;
  3390. tx_buf = tx_ring->tx_bi;
  3391. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3392. }
  3393. /* unmap skb header data */
  3394. dma_unmap_single(tx_ring->dev,
  3395. dma_unmap_addr(tx_buf, dma),
  3396. dma_unmap_len(tx_buf, len),
  3397. DMA_TO_DEVICE);
  3398. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3399. kfree(tx_buf->raw_buf);
  3400. tx_buf->raw_buf = NULL;
  3401. tx_buf->tx_flags = 0;
  3402. tx_buf->next_to_watch = NULL;
  3403. dma_unmap_len_set(tx_buf, len, 0);
  3404. tx_desc->buffer_addr = 0;
  3405. tx_desc->cmd_type_offset_bsz = 0;
  3406. /* move us past the eop_desc for start of next FD desc */
  3407. tx_buf++;
  3408. tx_desc++;
  3409. i++;
  3410. if (unlikely(!i)) {
  3411. i -= tx_ring->count;
  3412. tx_buf = tx_ring->tx_bi;
  3413. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3414. }
  3415. /* update budget accounting */
  3416. budget--;
  3417. } while (likely(budget));
  3418. i += tx_ring->count;
  3419. tx_ring->next_to_clean = i;
  3420. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3421. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3422. return budget > 0;
  3423. }
  3424. /**
  3425. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3426. * @irq: interrupt number
  3427. * @data: pointer to a q_vector
  3428. **/
  3429. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3430. {
  3431. struct i40e_q_vector *q_vector = data;
  3432. struct i40e_vsi *vsi;
  3433. if (!q_vector->tx.ring)
  3434. return IRQ_HANDLED;
  3435. vsi = q_vector->tx.ring->vsi;
  3436. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3437. return IRQ_HANDLED;
  3438. }
  3439. /**
  3440. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3441. * @vsi: the VSI being configured
  3442. * @v_idx: vector index
  3443. * @qp_idx: queue pair index
  3444. **/
  3445. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3446. {
  3447. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3448. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3449. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3450. tx_ring->q_vector = q_vector;
  3451. tx_ring->next = q_vector->tx.ring;
  3452. q_vector->tx.ring = tx_ring;
  3453. q_vector->tx.count++;
  3454. rx_ring->q_vector = q_vector;
  3455. rx_ring->next = q_vector->rx.ring;
  3456. q_vector->rx.ring = rx_ring;
  3457. q_vector->rx.count++;
  3458. }
  3459. /**
  3460. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3461. * @vsi: the VSI being configured
  3462. *
  3463. * This function maps descriptor rings to the queue-specific vectors
  3464. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3465. * one vector per queue pair, but on a constrained vector budget, we
  3466. * group the queue pairs as "efficiently" as possible.
  3467. **/
  3468. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3469. {
  3470. int qp_remaining = vsi->num_queue_pairs;
  3471. int q_vectors = vsi->num_q_vectors;
  3472. int num_ringpairs;
  3473. int v_start = 0;
  3474. int qp_idx = 0;
  3475. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3476. * group them so there are multiple queues per vector.
  3477. * It is also important to go through all the vectors available to be
  3478. * sure that if we don't use all the vectors, that the remaining vectors
  3479. * are cleared. This is especially important when decreasing the
  3480. * number of queues in use.
  3481. */
  3482. for (; v_start < q_vectors; v_start++) {
  3483. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3484. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3485. q_vector->num_ringpairs = num_ringpairs;
  3486. q_vector->rx.count = 0;
  3487. q_vector->tx.count = 0;
  3488. q_vector->rx.ring = NULL;
  3489. q_vector->tx.ring = NULL;
  3490. while (num_ringpairs--) {
  3491. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3492. qp_idx++;
  3493. qp_remaining--;
  3494. }
  3495. }
  3496. }
  3497. /**
  3498. * i40e_vsi_request_irq - Request IRQ from the OS
  3499. * @vsi: the VSI being configured
  3500. * @basename: name for the vector
  3501. **/
  3502. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3503. {
  3504. struct i40e_pf *pf = vsi->back;
  3505. int err;
  3506. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3507. err = i40e_vsi_request_irq_msix(vsi, basename);
  3508. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3509. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3510. pf->int_name, pf);
  3511. else
  3512. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3513. pf->int_name, pf);
  3514. if (err)
  3515. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3516. return err;
  3517. }
  3518. #ifdef CONFIG_NET_POLL_CONTROLLER
  3519. /**
  3520. * i40e_netpoll - A Polling 'interrupt' handler
  3521. * @netdev: network interface device structure
  3522. *
  3523. * This is used by netconsole to send skbs without having to re-enable
  3524. * interrupts. It's not called while the normal interrupt routine is executing.
  3525. **/
  3526. #ifdef I40E_FCOE
  3527. void i40e_netpoll(struct net_device *netdev)
  3528. #else
  3529. static void i40e_netpoll(struct net_device *netdev)
  3530. #endif
  3531. {
  3532. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3533. struct i40e_vsi *vsi = np->vsi;
  3534. struct i40e_pf *pf = vsi->back;
  3535. int i;
  3536. /* if interface is down do nothing */
  3537. if (test_bit(__I40E_DOWN, &vsi->state))
  3538. return;
  3539. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3540. for (i = 0; i < vsi->num_q_vectors; i++)
  3541. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3542. } else {
  3543. i40e_intr(pf->pdev->irq, netdev);
  3544. }
  3545. }
  3546. #endif
  3547. /**
  3548. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3549. * @pf: the PF being configured
  3550. * @pf_q: the PF queue
  3551. * @enable: enable or disable state of the queue
  3552. *
  3553. * This routine will wait for the given Tx queue of the PF to reach the
  3554. * enabled or disabled state.
  3555. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3556. * multiple retries; else will return 0 in case of success.
  3557. **/
  3558. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3559. {
  3560. int i;
  3561. u32 tx_reg;
  3562. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3563. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3564. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3565. break;
  3566. usleep_range(10, 20);
  3567. }
  3568. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3569. return -ETIMEDOUT;
  3570. return 0;
  3571. }
  3572. /**
  3573. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3574. * @vsi: the VSI being configured
  3575. * @enable: start or stop the rings
  3576. **/
  3577. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3578. {
  3579. struct i40e_pf *pf = vsi->back;
  3580. struct i40e_hw *hw = &pf->hw;
  3581. int i, j, pf_q, ret = 0;
  3582. u32 tx_reg;
  3583. pf_q = vsi->base_queue;
  3584. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3585. /* warn the TX unit of coming changes */
  3586. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3587. if (!enable)
  3588. usleep_range(10, 20);
  3589. for (j = 0; j < 50; j++) {
  3590. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3591. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3592. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3593. break;
  3594. usleep_range(1000, 2000);
  3595. }
  3596. /* Skip if the queue is already in the requested state */
  3597. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3598. continue;
  3599. /* turn on/off the queue */
  3600. if (enable) {
  3601. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3602. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3603. } else {
  3604. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3605. }
  3606. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3607. /* No waiting for the Tx queue to disable */
  3608. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3609. continue;
  3610. /* wait for the change to finish */
  3611. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3612. if (ret) {
  3613. dev_info(&pf->pdev->dev,
  3614. "VSI seid %d Tx ring %d %sable timeout\n",
  3615. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3616. break;
  3617. }
  3618. }
  3619. if (hw->revision_id == 0)
  3620. mdelay(50);
  3621. return ret;
  3622. }
  3623. /**
  3624. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3625. * @pf: the PF being configured
  3626. * @pf_q: the PF queue
  3627. * @enable: enable or disable state of the queue
  3628. *
  3629. * This routine will wait for the given Rx queue of the PF to reach the
  3630. * enabled or disabled state.
  3631. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3632. * multiple retries; else will return 0 in case of success.
  3633. **/
  3634. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3635. {
  3636. int i;
  3637. u32 rx_reg;
  3638. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3639. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3640. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3641. break;
  3642. usleep_range(10, 20);
  3643. }
  3644. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3645. return -ETIMEDOUT;
  3646. return 0;
  3647. }
  3648. /**
  3649. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3650. * @vsi: the VSI being configured
  3651. * @enable: start or stop the rings
  3652. **/
  3653. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3654. {
  3655. struct i40e_pf *pf = vsi->back;
  3656. struct i40e_hw *hw = &pf->hw;
  3657. int i, j, pf_q, ret = 0;
  3658. u32 rx_reg;
  3659. pf_q = vsi->base_queue;
  3660. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3661. for (j = 0; j < 50; j++) {
  3662. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3663. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3664. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3665. break;
  3666. usleep_range(1000, 2000);
  3667. }
  3668. /* Skip if the queue is already in the requested state */
  3669. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3670. continue;
  3671. /* turn on/off the queue */
  3672. if (enable)
  3673. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3674. else
  3675. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3676. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3677. /* No waiting for the Tx queue to disable */
  3678. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3679. continue;
  3680. /* wait for the change to finish */
  3681. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3682. if (ret) {
  3683. dev_info(&pf->pdev->dev,
  3684. "VSI seid %d Rx ring %d %sable timeout\n",
  3685. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3686. break;
  3687. }
  3688. }
  3689. return ret;
  3690. }
  3691. /**
  3692. * i40e_vsi_start_rings - Start a VSI's rings
  3693. * @vsi: the VSI being configured
  3694. **/
  3695. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3696. {
  3697. int ret = 0;
  3698. /* do rx first for enable and last for disable */
  3699. ret = i40e_vsi_control_rx(vsi, true);
  3700. if (ret)
  3701. return ret;
  3702. ret = i40e_vsi_control_tx(vsi, true);
  3703. return ret;
  3704. }
  3705. /**
  3706. * i40e_vsi_stop_rings - Stop a VSI's rings
  3707. * @vsi: the VSI being configured
  3708. **/
  3709. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3710. {
  3711. /* do rx first for enable and last for disable
  3712. * Ignore return value, we need to shutdown whatever we can
  3713. */
  3714. i40e_vsi_control_tx(vsi, false);
  3715. i40e_vsi_control_rx(vsi, false);
  3716. }
  3717. /**
  3718. * i40e_vsi_free_irq - Free the irq association with the OS
  3719. * @vsi: the VSI being configured
  3720. **/
  3721. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3722. {
  3723. struct i40e_pf *pf = vsi->back;
  3724. struct i40e_hw *hw = &pf->hw;
  3725. int base = vsi->base_vector;
  3726. u32 val, qp;
  3727. int i;
  3728. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3729. if (!vsi->q_vectors)
  3730. return;
  3731. if (!vsi->irqs_ready)
  3732. return;
  3733. vsi->irqs_ready = false;
  3734. for (i = 0; i < vsi->num_q_vectors; i++) {
  3735. int irq_num;
  3736. u16 vector;
  3737. vector = i + base;
  3738. irq_num = pf->msix_entries[vector].vector;
  3739. /* free only the irqs that were actually requested */
  3740. if (!vsi->q_vectors[i] ||
  3741. !vsi->q_vectors[i]->num_ringpairs)
  3742. continue;
  3743. /* clear the affinity notifier in the IRQ descriptor */
  3744. irq_set_affinity_notifier(irq_num, NULL);
  3745. /* clear the affinity_mask in the IRQ descriptor */
  3746. irq_set_affinity_hint(irq_num, NULL);
  3747. synchronize_irq(irq_num);
  3748. free_irq(irq_num, vsi->q_vectors[i]);
  3749. /* Tear down the interrupt queue link list
  3750. *
  3751. * We know that they come in pairs and always
  3752. * the Rx first, then the Tx. To clear the
  3753. * link list, stick the EOL value into the
  3754. * next_q field of the registers.
  3755. */
  3756. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3757. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3758. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3759. val |= I40E_QUEUE_END_OF_LIST
  3760. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3761. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3762. while (qp != I40E_QUEUE_END_OF_LIST) {
  3763. u32 next;
  3764. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3765. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3766. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3767. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3768. I40E_QINT_RQCTL_INTEVENT_MASK);
  3769. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3770. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3771. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3772. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3773. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3774. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3775. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3776. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3777. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3778. I40E_QINT_TQCTL_INTEVENT_MASK);
  3779. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3780. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3781. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3782. qp = next;
  3783. }
  3784. }
  3785. } else {
  3786. free_irq(pf->pdev->irq, pf);
  3787. val = rd32(hw, I40E_PFINT_LNKLST0);
  3788. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3789. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3790. val |= I40E_QUEUE_END_OF_LIST
  3791. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  3792. wr32(hw, I40E_PFINT_LNKLST0, val);
  3793. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3794. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3795. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3796. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3797. I40E_QINT_RQCTL_INTEVENT_MASK);
  3798. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3799. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3800. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3801. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3802. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3803. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3804. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3805. I40E_QINT_TQCTL_INTEVENT_MASK);
  3806. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3807. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3808. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3809. }
  3810. }
  3811. /**
  3812. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  3813. * @vsi: the VSI being configured
  3814. * @v_idx: Index of vector to be freed
  3815. *
  3816. * This function frees the memory allocated to the q_vector. In addition if
  3817. * NAPI is enabled it will delete any references to the NAPI struct prior
  3818. * to freeing the q_vector.
  3819. **/
  3820. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  3821. {
  3822. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3823. struct i40e_ring *ring;
  3824. if (!q_vector)
  3825. return;
  3826. /* disassociate q_vector from rings */
  3827. i40e_for_each_ring(ring, q_vector->tx)
  3828. ring->q_vector = NULL;
  3829. i40e_for_each_ring(ring, q_vector->rx)
  3830. ring->q_vector = NULL;
  3831. /* only VSI w/ an associated netdev is set up w/ NAPI */
  3832. if (vsi->netdev)
  3833. netif_napi_del(&q_vector->napi);
  3834. vsi->q_vectors[v_idx] = NULL;
  3835. kfree_rcu(q_vector, rcu);
  3836. }
  3837. /**
  3838. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  3839. * @vsi: the VSI being un-configured
  3840. *
  3841. * This frees the memory allocated to the q_vectors and
  3842. * deletes references to the NAPI struct.
  3843. **/
  3844. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  3845. {
  3846. int v_idx;
  3847. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  3848. i40e_free_q_vector(vsi, v_idx);
  3849. }
  3850. /**
  3851. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  3852. * @pf: board private structure
  3853. **/
  3854. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  3855. {
  3856. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  3857. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3858. pci_disable_msix(pf->pdev);
  3859. kfree(pf->msix_entries);
  3860. pf->msix_entries = NULL;
  3861. kfree(pf->irq_pile);
  3862. pf->irq_pile = NULL;
  3863. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  3864. pci_disable_msi(pf->pdev);
  3865. }
  3866. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  3867. }
  3868. /**
  3869. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  3870. * @pf: board private structure
  3871. *
  3872. * We go through and clear interrupt specific resources and reset the structure
  3873. * to pre-load conditions
  3874. **/
  3875. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  3876. {
  3877. int i;
  3878. i40e_stop_misc_vector(pf);
  3879. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3880. synchronize_irq(pf->msix_entries[0].vector);
  3881. free_irq(pf->msix_entries[0].vector, pf);
  3882. }
  3883. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  3884. I40E_IWARP_IRQ_PILE_ID);
  3885. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  3886. for (i = 0; i < pf->num_alloc_vsi; i++)
  3887. if (pf->vsi[i])
  3888. i40e_vsi_free_q_vectors(pf->vsi[i]);
  3889. i40e_reset_interrupt_capability(pf);
  3890. }
  3891. /**
  3892. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3893. * @vsi: the VSI being configured
  3894. **/
  3895. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  3896. {
  3897. int q_idx;
  3898. if (!vsi->netdev)
  3899. return;
  3900. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3901. napi_enable(&vsi->q_vectors[q_idx]->napi);
  3902. }
  3903. /**
  3904. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  3905. * @vsi: the VSI being configured
  3906. **/
  3907. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  3908. {
  3909. int q_idx;
  3910. if (!vsi->netdev)
  3911. return;
  3912. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3913. napi_disable(&vsi->q_vectors[q_idx]->napi);
  3914. }
  3915. /**
  3916. * i40e_vsi_close - Shut down a VSI
  3917. * @vsi: the vsi to be quelled
  3918. **/
  3919. static void i40e_vsi_close(struct i40e_vsi *vsi)
  3920. {
  3921. bool reset = false;
  3922. if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
  3923. i40e_down(vsi);
  3924. i40e_vsi_free_irq(vsi);
  3925. i40e_vsi_free_tx_resources(vsi);
  3926. i40e_vsi_free_rx_resources(vsi);
  3927. vsi->current_netdev_flags = 0;
  3928. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  3929. reset = true;
  3930. i40e_notify_client_of_netdev_close(vsi, reset);
  3931. }
  3932. /**
  3933. * i40e_quiesce_vsi - Pause a given VSI
  3934. * @vsi: the VSI being paused
  3935. **/
  3936. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  3937. {
  3938. if (test_bit(__I40E_DOWN, &vsi->state))
  3939. return;
  3940. /* No need to disable FCoE VSI when Tx suspended */
  3941. if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
  3942. vsi->type == I40E_VSI_FCOE) {
  3943. dev_dbg(&vsi->back->pdev->dev,
  3944. "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
  3945. return;
  3946. }
  3947. set_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3948. if (vsi->netdev && netif_running(vsi->netdev))
  3949. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  3950. else
  3951. i40e_vsi_close(vsi);
  3952. }
  3953. /**
  3954. * i40e_unquiesce_vsi - Resume a given VSI
  3955. * @vsi: the VSI being resumed
  3956. **/
  3957. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  3958. {
  3959. if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
  3960. return;
  3961. clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3962. if (vsi->netdev && netif_running(vsi->netdev))
  3963. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  3964. else
  3965. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  3966. }
  3967. /**
  3968. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  3969. * @pf: the PF
  3970. **/
  3971. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  3972. {
  3973. int v;
  3974. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3975. if (pf->vsi[v])
  3976. i40e_quiesce_vsi(pf->vsi[v]);
  3977. }
  3978. }
  3979. /**
  3980. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  3981. * @pf: the PF
  3982. **/
  3983. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  3984. {
  3985. int v;
  3986. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3987. if (pf->vsi[v])
  3988. i40e_unquiesce_vsi(pf->vsi[v]);
  3989. }
  3990. }
  3991. #ifdef CONFIG_I40E_DCB
  3992. /**
  3993. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  3994. * @vsi: the VSI being configured
  3995. *
  3996. * This function waits for the given VSI's queues to be disabled.
  3997. **/
  3998. static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  3999. {
  4000. struct i40e_pf *pf = vsi->back;
  4001. int i, pf_q, ret;
  4002. pf_q = vsi->base_queue;
  4003. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4004. /* Check and wait for the disable status of the queue */
  4005. ret = i40e_pf_txq_wait(pf, pf_q, false);
  4006. if (ret) {
  4007. dev_info(&pf->pdev->dev,
  4008. "VSI seid %d Tx ring %d disable timeout\n",
  4009. vsi->seid, pf_q);
  4010. return ret;
  4011. }
  4012. }
  4013. pf_q = vsi->base_queue;
  4014. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4015. /* Check and wait for the disable status of the queue */
  4016. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  4017. if (ret) {
  4018. dev_info(&pf->pdev->dev,
  4019. "VSI seid %d Rx ring %d disable timeout\n",
  4020. vsi->seid, pf_q);
  4021. return ret;
  4022. }
  4023. }
  4024. return 0;
  4025. }
  4026. /**
  4027. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  4028. * @pf: the PF
  4029. *
  4030. * This function waits for the queues to be in disabled state for all the
  4031. * VSIs that are managed by this PF.
  4032. **/
  4033. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  4034. {
  4035. int v, ret = 0;
  4036. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4037. /* No need to wait for FCoE VSI queues */
  4038. if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
  4039. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  4040. if (ret)
  4041. break;
  4042. }
  4043. }
  4044. return ret;
  4045. }
  4046. #endif
  4047. /**
  4048. * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
  4049. * @q_idx: TX queue number
  4050. * @vsi: Pointer to VSI struct
  4051. *
  4052. * This function checks specified queue for given VSI. Detects hung condition.
  4053. * Sets hung bit since it is two step process. Before next run of service task
  4054. * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
  4055. * hung condition remain unchanged and during subsequent run, this function
  4056. * issues SW interrupt to recover from hung condition.
  4057. **/
  4058. static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
  4059. {
  4060. struct i40e_ring *tx_ring = NULL;
  4061. struct i40e_pf *pf;
  4062. u32 head, val, tx_pending_hw;
  4063. int i;
  4064. pf = vsi->back;
  4065. /* now that we have an index, find the tx_ring struct */
  4066. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4067. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  4068. if (q_idx == vsi->tx_rings[i]->queue_index) {
  4069. tx_ring = vsi->tx_rings[i];
  4070. break;
  4071. }
  4072. }
  4073. }
  4074. if (!tx_ring)
  4075. return;
  4076. /* Read interrupt register */
  4077. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4078. val = rd32(&pf->hw,
  4079. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  4080. tx_ring->vsi->base_vector - 1));
  4081. else
  4082. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  4083. head = i40e_get_head(tx_ring);
  4084. tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
  4085. /* HW is done executing descriptors, updated HEAD write back,
  4086. * but SW hasn't processed those descriptors. If interrupt is
  4087. * not generated from this point ON, it could result into
  4088. * dev_watchdog detecting timeout on those netdev_queue,
  4089. * hence proactively trigger SW interrupt.
  4090. */
  4091. if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
  4092. /* NAPI Poll didn't run and clear since it was set */
  4093. if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
  4094. &tx_ring->q_vector->hung_detected)) {
  4095. netdev_info(vsi->netdev, "VSI_seid %d, Hung TX queue %d, tx_pending_hw: %d, NTC:0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x\n",
  4096. vsi->seid, q_idx, tx_pending_hw,
  4097. tx_ring->next_to_clean, head,
  4098. tx_ring->next_to_use,
  4099. readl(tx_ring->tail));
  4100. netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
  4101. vsi->seid, q_idx, val);
  4102. i40e_force_wb(vsi, tx_ring->q_vector);
  4103. } else {
  4104. /* First Chance - detected possible hung */
  4105. set_bit(I40E_Q_VECTOR_HUNG_DETECT,
  4106. &tx_ring->q_vector->hung_detected);
  4107. }
  4108. }
  4109. /* This is the case where we have interrupts missing,
  4110. * so the tx_pending in HW will most likely be 0, but we
  4111. * will have tx_pending in SW since the WB happened but the
  4112. * interrupt got lost.
  4113. */
  4114. if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
  4115. (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
  4116. local_bh_disable();
  4117. if (napi_reschedule(&tx_ring->q_vector->napi))
  4118. tx_ring->tx_stats.tx_lost_interrupt++;
  4119. local_bh_enable();
  4120. }
  4121. }
  4122. /**
  4123. * i40e_detect_recover_hung - Function to detect and recover hung_queues
  4124. * @pf: pointer to PF struct
  4125. *
  4126. * LAN VSI has netdev and netdev has TX queues. This function is to check
  4127. * each of those TX queues if they are hung, trigger recovery by issuing
  4128. * SW interrupt.
  4129. **/
  4130. static void i40e_detect_recover_hung(struct i40e_pf *pf)
  4131. {
  4132. struct net_device *netdev;
  4133. struct i40e_vsi *vsi;
  4134. int i;
  4135. /* Only for LAN VSI */
  4136. vsi = pf->vsi[pf->lan_vsi];
  4137. if (!vsi)
  4138. return;
  4139. /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
  4140. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  4141. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  4142. return;
  4143. /* Make sure type is MAIN VSI */
  4144. if (vsi->type != I40E_VSI_MAIN)
  4145. return;
  4146. netdev = vsi->netdev;
  4147. if (!netdev)
  4148. return;
  4149. /* Bail out if netif_carrier is not OK */
  4150. if (!netif_carrier_ok(netdev))
  4151. return;
  4152. /* Go thru' TX queues for netdev */
  4153. for (i = 0; i < netdev->num_tx_queues; i++) {
  4154. struct netdev_queue *q;
  4155. q = netdev_get_tx_queue(netdev, i);
  4156. if (q)
  4157. i40e_detect_recover_hung_queue(i, vsi);
  4158. }
  4159. }
  4160. /**
  4161. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4162. * @pf: pointer to PF
  4163. *
  4164. * Get TC map for ISCSI PF type that will include iSCSI TC
  4165. * and LAN TC.
  4166. **/
  4167. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4168. {
  4169. struct i40e_dcb_app_priority_table app;
  4170. struct i40e_hw *hw = &pf->hw;
  4171. u8 enabled_tc = 1; /* TC0 is always enabled */
  4172. u8 tc, i;
  4173. /* Get the iSCSI APP TLV */
  4174. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4175. for (i = 0; i < dcbcfg->numapps; i++) {
  4176. app = dcbcfg->app[i];
  4177. if (app.selector == I40E_APP_SEL_TCPIP &&
  4178. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4179. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4180. enabled_tc |= BIT(tc);
  4181. break;
  4182. }
  4183. }
  4184. return enabled_tc;
  4185. }
  4186. /**
  4187. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4188. * @dcbcfg: the corresponding DCBx configuration structure
  4189. *
  4190. * Return the number of TCs from given DCBx configuration
  4191. **/
  4192. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4193. {
  4194. int i, tc_unused = 0;
  4195. u8 num_tc = 0;
  4196. u8 ret = 0;
  4197. /* Scan the ETS Config Priority Table to find
  4198. * traffic class enabled for a given priority
  4199. * and create a bitmask of enabled TCs
  4200. */
  4201. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4202. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4203. /* Now scan the bitmask to check for
  4204. * contiguous TCs starting with TC0
  4205. */
  4206. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4207. if (num_tc & BIT(i)) {
  4208. if (!tc_unused) {
  4209. ret++;
  4210. } else {
  4211. pr_err("Non-contiguous TC - Disabling DCB\n");
  4212. return 1;
  4213. }
  4214. } else {
  4215. tc_unused = 1;
  4216. }
  4217. }
  4218. /* There is always at least TC0 */
  4219. if (!ret)
  4220. ret = 1;
  4221. return ret;
  4222. }
  4223. /**
  4224. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4225. * @dcbcfg: the corresponding DCBx configuration structure
  4226. *
  4227. * Query the current DCB configuration and return the number of
  4228. * traffic classes enabled from the given DCBX config
  4229. **/
  4230. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4231. {
  4232. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4233. u8 enabled_tc = 1;
  4234. u8 i;
  4235. for (i = 0; i < num_tc; i++)
  4236. enabled_tc |= BIT(i);
  4237. return enabled_tc;
  4238. }
  4239. /**
  4240. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4241. * @pf: PF being queried
  4242. *
  4243. * Return number of traffic classes enabled for the given PF
  4244. **/
  4245. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4246. {
  4247. struct i40e_hw *hw = &pf->hw;
  4248. u8 i, enabled_tc = 1;
  4249. u8 num_tc = 0;
  4250. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4251. /* If DCB is not enabled then always in single TC */
  4252. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4253. return 1;
  4254. /* SFP mode will be enabled for all TCs on port */
  4255. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4256. return i40e_dcb_get_num_tc(dcbcfg);
  4257. /* MFP mode return count of enabled TCs for this PF */
  4258. if (pf->hw.func_caps.iscsi)
  4259. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4260. else
  4261. return 1; /* Only TC0 */
  4262. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4263. if (enabled_tc & BIT(i))
  4264. num_tc++;
  4265. }
  4266. return num_tc;
  4267. }
  4268. /**
  4269. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4270. * @pf: PF being queried
  4271. *
  4272. * Return a bitmap for enabled traffic classes for this PF.
  4273. **/
  4274. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4275. {
  4276. /* If DCB is not enabled for this PF then just return default TC */
  4277. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4278. return I40E_DEFAULT_TRAFFIC_CLASS;
  4279. /* SFP mode we want PF to be enabled for all TCs */
  4280. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4281. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4282. /* MFP enabled and iSCSI PF type */
  4283. if (pf->hw.func_caps.iscsi)
  4284. return i40e_get_iscsi_tc_map(pf);
  4285. else
  4286. return I40E_DEFAULT_TRAFFIC_CLASS;
  4287. }
  4288. /**
  4289. * i40e_vsi_get_bw_info - Query VSI BW Information
  4290. * @vsi: the VSI being queried
  4291. *
  4292. * Returns 0 on success, negative value on failure
  4293. **/
  4294. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4295. {
  4296. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4297. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4298. struct i40e_pf *pf = vsi->back;
  4299. struct i40e_hw *hw = &pf->hw;
  4300. i40e_status ret;
  4301. u32 tc_bw_max;
  4302. int i;
  4303. /* Get the VSI level BW configuration */
  4304. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4305. if (ret) {
  4306. dev_info(&pf->pdev->dev,
  4307. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4308. i40e_stat_str(&pf->hw, ret),
  4309. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4310. return -EINVAL;
  4311. }
  4312. /* Get the VSI level BW configuration per TC */
  4313. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4314. NULL);
  4315. if (ret) {
  4316. dev_info(&pf->pdev->dev,
  4317. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4318. i40e_stat_str(&pf->hw, ret),
  4319. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4320. return -EINVAL;
  4321. }
  4322. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4323. dev_info(&pf->pdev->dev,
  4324. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4325. bw_config.tc_valid_bits,
  4326. bw_ets_config.tc_valid_bits);
  4327. /* Still continuing */
  4328. }
  4329. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4330. vsi->bw_max_quanta = bw_config.max_bw;
  4331. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4332. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4333. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4334. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4335. vsi->bw_ets_limit_credits[i] =
  4336. le16_to_cpu(bw_ets_config.credits[i]);
  4337. /* 3 bits out of 4 for each TC */
  4338. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4339. }
  4340. return 0;
  4341. }
  4342. /**
  4343. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4344. * @vsi: the VSI being configured
  4345. * @enabled_tc: TC bitmap
  4346. * @bw_credits: BW shared credits per TC
  4347. *
  4348. * Returns 0 on success, negative value on failure
  4349. **/
  4350. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4351. u8 *bw_share)
  4352. {
  4353. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4354. i40e_status ret;
  4355. int i;
  4356. bw_data.tc_valid_bits = enabled_tc;
  4357. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4358. bw_data.tc_bw_credits[i] = bw_share[i];
  4359. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4360. NULL);
  4361. if (ret) {
  4362. dev_info(&vsi->back->pdev->dev,
  4363. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4364. vsi->back->hw.aq.asq_last_status);
  4365. return -EINVAL;
  4366. }
  4367. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4368. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4369. return 0;
  4370. }
  4371. /**
  4372. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4373. * @vsi: the VSI being configured
  4374. * @enabled_tc: TC map to be enabled
  4375. *
  4376. **/
  4377. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4378. {
  4379. struct net_device *netdev = vsi->netdev;
  4380. struct i40e_pf *pf = vsi->back;
  4381. struct i40e_hw *hw = &pf->hw;
  4382. u8 netdev_tc = 0;
  4383. int i;
  4384. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4385. if (!netdev)
  4386. return;
  4387. if (!enabled_tc) {
  4388. netdev_reset_tc(netdev);
  4389. return;
  4390. }
  4391. /* Set up actual enabled TCs on the VSI */
  4392. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4393. return;
  4394. /* set per TC queues for the VSI */
  4395. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4396. /* Only set TC queues for enabled tcs
  4397. *
  4398. * e.g. For a VSI that has TC0 and TC3 enabled the
  4399. * enabled_tc bitmap would be 0x00001001; the driver
  4400. * will set the numtc for netdev as 2 that will be
  4401. * referenced by the netdev layer as TC 0 and 1.
  4402. */
  4403. if (vsi->tc_config.enabled_tc & BIT(i))
  4404. netdev_set_tc_queue(netdev,
  4405. vsi->tc_config.tc_info[i].netdev_tc,
  4406. vsi->tc_config.tc_info[i].qcount,
  4407. vsi->tc_config.tc_info[i].qoffset);
  4408. }
  4409. /* Assign UP2TC map for the VSI */
  4410. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4411. /* Get the actual TC# for the UP */
  4412. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4413. /* Get the mapped netdev TC# for the UP */
  4414. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4415. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4416. }
  4417. }
  4418. /**
  4419. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4420. * @vsi: the VSI being configured
  4421. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4422. **/
  4423. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4424. struct i40e_vsi_context *ctxt)
  4425. {
  4426. /* copy just the sections touched not the entire info
  4427. * since not all sections are valid as returned by
  4428. * update vsi params
  4429. */
  4430. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4431. memcpy(&vsi->info.queue_mapping,
  4432. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4433. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4434. sizeof(vsi->info.tc_mapping));
  4435. }
  4436. /**
  4437. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4438. * @vsi: VSI to be configured
  4439. * @enabled_tc: TC bitmap
  4440. *
  4441. * This configures a particular VSI for TCs that are mapped to the
  4442. * given TC bitmap. It uses default bandwidth share for TCs across
  4443. * VSIs to configure TC for a particular VSI.
  4444. *
  4445. * NOTE:
  4446. * It is expected that the VSI queues have been quisced before calling
  4447. * this function.
  4448. **/
  4449. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4450. {
  4451. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4452. struct i40e_vsi_context ctxt;
  4453. int ret = 0;
  4454. int i;
  4455. /* Check if enabled_tc is same as existing or new TCs */
  4456. if (vsi->tc_config.enabled_tc == enabled_tc)
  4457. return ret;
  4458. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4459. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4460. if (enabled_tc & BIT(i))
  4461. bw_share[i] = 1;
  4462. }
  4463. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4464. if (ret) {
  4465. dev_info(&vsi->back->pdev->dev,
  4466. "Failed configuring TC map %d for VSI %d\n",
  4467. enabled_tc, vsi->seid);
  4468. goto out;
  4469. }
  4470. /* Update Queue Pairs Mapping for currently enabled UPs */
  4471. ctxt.seid = vsi->seid;
  4472. ctxt.pf_num = vsi->back->hw.pf_id;
  4473. ctxt.vf_num = 0;
  4474. ctxt.uplink_seid = vsi->uplink_seid;
  4475. ctxt.info = vsi->info;
  4476. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4477. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4478. ctxt.info.valid_sections |=
  4479. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4480. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4481. }
  4482. /* Update the VSI after updating the VSI queue-mapping information */
  4483. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  4484. if (ret) {
  4485. dev_info(&vsi->back->pdev->dev,
  4486. "Update vsi tc config failed, err %s aq_err %s\n",
  4487. i40e_stat_str(&vsi->back->hw, ret),
  4488. i40e_aq_str(&vsi->back->hw,
  4489. vsi->back->hw.aq.asq_last_status));
  4490. goto out;
  4491. }
  4492. /* update the local VSI info with updated queue map */
  4493. i40e_vsi_update_queue_map(vsi, &ctxt);
  4494. vsi->info.valid_sections = 0;
  4495. /* Update current VSI BW information */
  4496. ret = i40e_vsi_get_bw_info(vsi);
  4497. if (ret) {
  4498. dev_info(&vsi->back->pdev->dev,
  4499. "Failed updating vsi bw info, err %s aq_err %s\n",
  4500. i40e_stat_str(&vsi->back->hw, ret),
  4501. i40e_aq_str(&vsi->back->hw,
  4502. vsi->back->hw.aq.asq_last_status));
  4503. goto out;
  4504. }
  4505. /* Update the netdev TC setup */
  4506. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4507. out:
  4508. return ret;
  4509. }
  4510. /**
  4511. * i40e_veb_config_tc - Configure TCs for given VEB
  4512. * @veb: given VEB
  4513. * @enabled_tc: TC bitmap
  4514. *
  4515. * Configures given TC bitmap for VEB (switching) element
  4516. **/
  4517. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  4518. {
  4519. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  4520. struct i40e_pf *pf = veb->pf;
  4521. int ret = 0;
  4522. int i;
  4523. /* No TCs or already enabled TCs just return */
  4524. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  4525. return ret;
  4526. bw_data.tc_valid_bits = enabled_tc;
  4527. /* bw_data.absolute_credits is not set (relative) */
  4528. /* Enable ETS TCs with equal BW Share for now */
  4529. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4530. if (enabled_tc & BIT(i))
  4531. bw_data.tc_bw_share_credits[i] = 1;
  4532. }
  4533. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  4534. &bw_data, NULL);
  4535. if (ret) {
  4536. dev_info(&pf->pdev->dev,
  4537. "VEB bw config failed, err %s aq_err %s\n",
  4538. i40e_stat_str(&pf->hw, ret),
  4539. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4540. goto out;
  4541. }
  4542. /* Update the BW information */
  4543. ret = i40e_veb_get_bw_info(veb);
  4544. if (ret) {
  4545. dev_info(&pf->pdev->dev,
  4546. "Failed getting veb bw config, err %s aq_err %s\n",
  4547. i40e_stat_str(&pf->hw, ret),
  4548. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4549. }
  4550. out:
  4551. return ret;
  4552. }
  4553. #ifdef CONFIG_I40E_DCB
  4554. /**
  4555. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  4556. * @pf: PF struct
  4557. *
  4558. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  4559. * the caller would've quiesce all the VSIs before calling
  4560. * this function
  4561. **/
  4562. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  4563. {
  4564. u8 tc_map = 0;
  4565. int ret;
  4566. u8 v;
  4567. /* Enable the TCs available on PF to all VEBs */
  4568. tc_map = i40e_pf_get_tc_map(pf);
  4569. for (v = 0; v < I40E_MAX_VEB; v++) {
  4570. if (!pf->veb[v])
  4571. continue;
  4572. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  4573. if (ret) {
  4574. dev_info(&pf->pdev->dev,
  4575. "Failed configuring TC for VEB seid=%d\n",
  4576. pf->veb[v]->seid);
  4577. /* Will try to configure as many components */
  4578. }
  4579. }
  4580. /* Update each VSI */
  4581. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4582. if (!pf->vsi[v])
  4583. continue;
  4584. /* - Enable all TCs for the LAN VSI
  4585. #ifdef I40E_FCOE
  4586. * - For FCoE VSI only enable the TC configured
  4587. * as per the APP TLV
  4588. #endif
  4589. * - For all others keep them at TC0 for now
  4590. */
  4591. if (v == pf->lan_vsi)
  4592. tc_map = i40e_pf_get_tc_map(pf);
  4593. else
  4594. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  4595. #ifdef I40E_FCOE
  4596. if (pf->vsi[v]->type == I40E_VSI_FCOE)
  4597. tc_map = i40e_get_fcoe_tc_map(pf);
  4598. #endif /* #ifdef I40E_FCOE */
  4599. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  4600. if (ret) {
  4601. dev_info(&pf->pdev->dev,
  4602. "Failed configuring TC for VSI seid=%d\n",
  4603. pf->vsi[v]->seid);
  4604. /* Will try to configure as many components */
  4605. } else {
  4606. /* Re-configure VSI vectors based on updated TC map */
  4607. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  4608. if (pf->vsi[v]->netdev)
  4609. i40e_dcbnl_set_all(pf->vsi[v]);
  4610. }
  4611. }
  4612. }
  4613. /**
  4614. * i40e_resume_port_tx - Resume port Tx
  4615. * @pf: PF struct
  4616. *
  4617. * Resume a port's Tx and issue a PF reset in case of failure to
  4618. * resume.
  4619. **/
  4620. static int i40e_resume_port_tx(struct i40e_pf *pf)
  4621. {
  4622. struct i40e_hw *hw = &pf->hw;
  4623. int ret;
  4624. ret = i40e_aq_resume_port_tx(hw, NULL);
  4625. if (ret) {
  4626. dev_info(&pf->pdev->dev,
  4627. "Resume Port Tx failed, err %s aq_err %s\n",
  4628. i40e_stat_str(&pf->hw, ret),
  4629. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4630. /* Schedule PF reset to recover */
  4631. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  4632. i40e_service_event_schedule(pf);
  4633. }
  4634. return ret;
  4635. }
  4636. /**
  4637. * i40e_init_pf_dcb - Initialize DCB configuration
  4638. * @pf: PF being configured
  4639. *
  4640. * Query the current DCB configuration and cache it
  4641. * in the hardware structure
  4642. **/
  4643. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  4644. {
  4645. struct i40e_hw *hw = &pf->hw;
  4646. int err = 0;
  4647. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
  4648. if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
  4649. goto out;
  4650. /* Get the initial DCB configuration */
  4651. err = i40e_init_dcb(hw);
  4652. if (!err) {
  4653. /* Device/Function is not DCBX capable */
  4654. if ((!hw->func_caps.dcb) ||
  4655. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  4656. dev_info(&pf->pdev->dev,
  4657. "DCBX offload is not supported or is disabled for this PF.\n");
  4658. if (pf->flags & I40E_FLAG_MFP_ENABLED)
  4659. goto out;
  4660. } else {
  4661. /* When status is not DISABLED then DCBX in FW */
  4662. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  4663. DCB_CAP_DCBX_VER_IEEE;
  4664. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  4665. /* Enable DCB tagging only when more than one TC
  4666. * or explicitly disable if only one TC
  4667. */
  4668. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  4669. pf->flags |= I40E_FLAG_DCB_ENABLED;
  4670. else
  4671. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  4672. dev_dbg(&pf->pdev->dev,
  4673. "DCBX offload is supported for this PF.\n");
  4674. }
  4675. } else {
  4676. dev_info(&pf->pdev->dev,
  4677. "Query for DCB configuration failed, err %s aq_err %s\n",
  4678. i40e_stat_str(&pf->hw, err),
  4679. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4680. }
  4681. out:
  4682. return err;
  4683. }
  4684. #endif /* CONFIG_I40E_DCB */
  4685. #define SPEED_SIZE 14
  4686. #define FC_SIZE 8
  4687. /**
  4688. * i40e_print_link_message - print link up or down
  4689. * @vsi: the VSI for which link needs a message
  4690. */
  4691. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  4692. {
  4693. enum i40e_aq_link_speed new_speed;
  4694. char *speed = "Unknown";
  4695. char *fc = "Unknown";
  4696. char *fec = "";
  4697. char *an = "";
  4698. new_speed = vsi->back->hw.phy.link_info.link_speed;
  4699. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  4700. return;
  4701. vsi->current_isup = isup;
  4702. vsi->current_speed = new_speed;
  4703. if (!isup) {
  4704. netdev_info(vsi->netdev, "NIC Link is Down\n");
  4705. return;
  4706. }
  4707. /* Warn user if link speed on NPAR enabled partition is not at
  4708. * least 10GB
  4709. */
  4710. if (vsi->back->hw.func_caps.npar_enable &&
  4711. (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  4712. vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  4713. netdev_warn(vsi->netdev,
  4714. "The partition detected link speed that is less than 10Gbps\n");
  4715. switch (vsi->back->hw.phy.link_info.link_speed) {
  4716. case I40E_LINK_SPEED_40GB:
  4717. speed = "40 G";
  4718. break;
  4719. case I40E_LINK_SPEED_20GB:
  4720. speed = "20 G";
  4721. break;
  4722. case I40E_LINK_SPEED_25GB:
  4723. speed = "25 G";
  4724. break;
  4725. case I40E_LINK_SPEED_10GB:
  4726. speed = "10 G";
  4727. break;
  4728. case I40E_LINK_SPEED_1GB:
  4729. speed = "1000 M";
  4730. break;
  4731. case I40E_LINK_SPEED_100MB:
  4732. speed = "100 M";
  4733. break;
  4734. default:
  4735. break;
  4736. }
  4737. switch (vsi->back->hw.fc.current_mode) {
  4738. case I40E_FC_FULL:
  4739. fc = "RX/TX";
  4740. break;
  4741. case I40E_FC_TX_PAUSE:
  4742. fc = "TX";
  4743. break;
  4744. case I40E_FC_RX_PAUSE:
  4745. fc = "RX";
  4746. break;
  4747. default:
  4748. fc = "None";
  4749. break;
  4750. }
  4751. if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  4752. fec = ", FEC: None";
  4753. an = ", Autoneg: False";
  4754. if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  4755. an = ", Autoneg: True";
  4756. if (vsi->back->hw.phy.link_info.fec_info &
  4757. I40E_AQ_CONFIG_FEC_KR_ENA)
  4758. fec = ", FEC: CL74 FC-FEC/BASE-R";
  4759. else if (vsi->back->hw.phy.link_info.fec_info &
  4760. I40E_AQ_CONFIG_FEC_RS_ENA)
  4761. fec = ", FEC: CL108 RS-FEC";
  4762. }
  4763. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
  4764. speed, fec, an, fc);
  4765. }
  4766. /**
  4767. * i40e_up_complete - Finish the last steps of bringing up a connection
  4768. * @vsi: the VSI being configured
  4769. **/
  4770. static int i40e_up_complete(struct i40e_vsi *vsi)
  4771. {
  4772. struct i40e_pf *pf = vsi->back;
  4773. int err;
  4774. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4775. i40e_vsi_configure_msix(vsi);
  4776. else
  4777. i40e_configure_msi_and_legacy(vsi);
  4778. /* start rings */
  4779. err = i40e_vsi_start_rings(vsi);
  4780. if (err)
  4781. return err;
  4782. clear_bit(__I40E_DOWN, &vsi->state);
  4783. i40e_napi_enable_all(vsi);
  4784. i40e_vsi_enable_irq(vsi);
  4785. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  4786. (vsi->netdev)) {
  4787. i40e_print_link_message(vsi, true);
  4788. netif_tx_start_all_queues(vsi->netdev);
  4789. netif_carrier_on(vsi->netdev);
  4790. } else if (vsi->netdev) {
  4791. i40e_print_link_message(vsi, false);
  4792. /* need to check for qualified module here*/
  4793. if ((pf->hw.phy.link_info.link_info &
  4794. I40E_AQ_MEDIA_AVAILABLE) &&
  4795. (!(pf->hw.phy.link_info.an_info &
  4796. I40E_AQ_QUALIFIED_MODULE)))
  4797. netdev_err(vsi->netdev,
  4798. "the driver failed to link because an unqualified module was detected.");
  4799. }
  4800. /* replay FDIR SB filters */
  4801. if (vsi->type == I40E_VSI_FDIR) {
  4802. /* reset fd counters */
  4803. pf->fd_add_err = pf->fd_atr_cnt = 0;
  4804. if (pf->fd_tcp_rule > 0) {
  4805. pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
  4806. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  4807. dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
  4808. pf->fd_tcp_rule = 0;
  4809. }
  4810. i40e_fdir_filter_restore(vsi);
  4811. }
  4812. /* On the next run of the service_task, notify any clients of the new
  4813. * opened netdev
  4814. */
  4815. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  4816. i40e_service_event_schedule(pf);
  4817. return 0;
  4818. }
  4819. /**
  4820. * i40e_vsi_reinit_locked - Reset the VSI
  4821. * @vsi: the VSI being configured
  4822. *
  4823. * Rebuild the ring structs after some configuration
  4824. * has changed, e.g. MTU size.
  4825. **/
  4826. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  4827. {
  4828. struct i40e_pf *pf = vsi->back;
  4829. WARN_ON(in_interrupt());
  4830. while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
  4831. usleep_range(1000, 2000);
  4832. i40e_down(vsi);
  4833. i40e_up(vsi);
  4834. clear_bit(__I40E_CONFIG_BUSY, &pf->state);
  4835. }
  4836. /**
  4837. * i40e_up - Bring the connection back up after being down
  4838. * @vsi: the VSI being configured
  4839. **/
  4840. int i40e_up(struct i40e_vsi *vsi)
  4841. {
  4842. int err;
  4843. err = i40e_vsi_configure(vsi);
  4844. if (!err)
  4845. err = i40e_up_complete(vsi);
  4846. return err;
  4847. }
  4848. /**
  4849. * i40e_down - Shutdown the connection processing
  4850. * @vsi: the VSI being stopped
  4851. **/
  4852. void i40e_down(struct i40e_vsi *vsi)
  4853. {
  4854. int i;
  4855. /* It is assumed that the caller of this function
  4856. * sets the vsi->state __I40E_DOWN bit.
  4857. */
  4858. if (vsi->netdev) {
  4859. netif_carrier_off(vsi->netdev);
  4860. netif_tx_disable(vsi->netdev);
  4861. }
  4862. i40e_vsi_disable_irq(vsi);
  4863. i40e_vsi_stop_rings(vsi);
  4864. i40e_napi_disable_all(vsi);
  4865. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4866. i40e_clean_tx_ring(vsi->tx_rings[i]);
  4867. i40e_clean_rx_ring(vsi->rx_rings[i]);
  4868. }
  4869. i40e_notify_client_of_netdev_close(vsi, false);
  4870. }
  4871. /**
  4872. * i40e_setup_tc - configure multiple traffic classes
  4873. * @netdev: net device to configure
  4874. * @tc: number of traffic classes to enable
  4875. **/
  4876. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  4877. {
  4878. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4879. struct i40e_vsi *vsi = np->vsi;
  4880. struct i40e_pf *pf = vsi->back;
  4881. u8 enabled_tc = 0;
  4882. int ret = -EINVAL;
  4883. int i;
  4884. /* Check if DCB enabled to continue */
  4885. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4886. netdev_info(netdev, "DCB is not enabled for adapter\n");
  4887. goto exit;
  4888. }
  4889. /* Check if MFP enabled */
  4890. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  4891. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  4892. goto exit;
  4893. }
  4894. /* Check whether tc count is within enabled limit */
  4895. if (tc > i40e_pf_get_num_tc(pf)) {
  4896. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  4897. goto exit;
  4898. }
  4899. /* Generate TC map for number of tc requested */
  4900. for (i = 0; i < tc; i++)
  4901. enabled_tc |= BIT(i);
  4902. /* Requesting same TC configuration as already enabled */
  4903. if (enabled_tc == vsi->tc_config.enabled_tc)
  4904. return 0;
  4905. /* Quiesce VSI queues */
  4906. i40e_quiesce_vsi(vsi);
  4907. /* Configure VSI for enabled TCs */
  4908. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  4909. if (ret) {
  4910. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  4911. vsi->seid);
  4912. goto exit;
  4913. }
  4914. /* Unquiesce VSI */
  4915. i40e_unquiesce_vsi(vsi);
  4916. exit:
  4917. return ret;
  4918. }
  4919. #ifdef I40E_FCOE
  4920. int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
  4921. struct tc_to_netdev *tc)
  4922. #else
  4923. static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
  4924. struct tc_to_netdev *tc)
  4925. #endif
  4926. {
  4927. if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
  4928. return -EINVAL;
  4929. return i40e_setup_tc(netdev, tc->tc);
  4930. }
  4931. /**
  4932. * i40e_open - Called when a network interface is made active
  4933. * @netdev: network interface device structure
  4934. *
  4935. * The open entry point is called when a network interface is made
  4936. * active by the system (IFF_UP). At this point all resources needed
  4937. * for transmit and receive operations are allocated, the interrupt
  4938. * handler is registered with the OS, the netdev watchdog subtask is
  4939. * enabled, and the stack is notified that the interface is ready.
  4940. *
  4941. * Returns 0 on success, negative value on failure
  4942. **/
  4943. int i40e_open(struct net_device *netdev)
  4944. {
  4945. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4946. struct i40e_vsi *vsi = np->vsi;
  4947. struct i40e_pf *pf = vsi->back;
  4948. int err;
  4949. /* disallow open during test or if eeprom is broken */
  4950. if (test_bit(__I40E_TESTING, &pf->state) ||
  4951. test_bit(__I40E_BAD_EEPROM, &pf->state))
  4952. return -EBUSY;
  4953. netif_carrier_off(netdev);
  4954. err = i40e_vsi_open(vsi);
  4955. if (err)
  4956. return err;
  4957. /* configure global TSO hardware offload settings */
  4958. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  4959. TCP_FLAG_FIN) >> 16);
  4960. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  4961. TCP_FLAG_FIN |
  4962. TCP_FLAG_CWR) >> 16);
  4963. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  4964. udp_tunnel_get_rx_info(netdev);
  4965. return 0;
  4966. }
  4967. /**
  4968. * i40e_vsi_open -
  4969. * @vsi: the VSI to open
  4970. *
  4971. * Finish initialization of the VSI.
  4972. *
  4973. * Returns 0 on success, negative value on failure
  4974. **/
  4975. int i40e_vsi_open(struct i40e_vsi *vsi)
  4976. {
  4977. struct i40e_pf *pf = vsi->back;
  4978. char int_name[I40E_INT_NAME_STR_LEN];
  4979. int err;
  4980. /* allocate descriptors */
  4981. err = i40e_vsi_setup_tx_resources(vsi);
  4982. if (err)
  4983. goto err_setup_tx;
  4984. err = i40e_vsi_setup_rx_resources(vsi);
  4985. if (err)
  4986. goto err_setup_rx;
  4987. err = i40e_vsi_configure(vsi);
  4988. if (err)
  4989. goto err_setup_rx;
  4990. if (vsi->netdev) {
  4991. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  4992. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  4993. err = i40e_vsi_request_irq(vsi, int_name);
  4994. if (err)
  4995. goto err_setup_rx;
  4996. /* Notify the stack of the actual queue counts. */
  4997. err = netif_set_real_num_tx_queues(vsi->netdev,
  4998. vsi->num_queue_pairs);
  4999. if (err)
  5000. goto err_set_queues;
  5001. err = netif_set_real_num_rx_queues(vsi->netdev,
  5002. vsi->num_queue_pairs);
  5003. if (err)
  5004. goto err_set_queues;
  5005. } else if (vsi->type == I40E_VSI_FDIR) {
  5006. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  5007. dev_driver_string(&pf->pdev->dev),
  5008. dev_name(&pf->pdev->dev));
  5009. err = i40e_vsi_request_irq(vsi, int_name);
  5010. } else {
  5011. err = -EINVAL;
  5012. goto err_setup_rx;
  5013. }
  5014. err = i40e_up_complete(vsi);
  5015. if (err)
  5016. goto err_up_complete;
  5017. return 0;
  5018. err_up_complete:
  5019. i40e_down(vsi);
  5020. err_set_queues:
  5021. i40e_vsi_free_irq(vsi);
  5022. err_setup_rx:
  5023. i40e_vsi_free_rx_resources(vsi);
  5024. err_setup_tx:
  5025. i40e_vsi_free_tx_resources(vsi);
  5026. if (vsi == pf->vsi[pf->lan_vsi])
  5027. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  5028. return err;
  5029. }
  5030. /**
  5031. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  5032. * @pf: Pointer to PF
  5033. *
  5034. * This function destroys the hlist where all the Flow Director
  5035. * filters were saved.
  5036. **/
  5037. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  5038. {
  5039. struct i40e_fdir_filter *filter;
  5040. struct hlist_node *node2;
  5041. hlist_for_each_entry_safe(filter, node2,
  5042. &pf->fdir_filter_list, fdir_node) {
  5043. hlist_del(&filter->fdir_node);
  5044. kfree(filter);
  5045. }
  5046. pf->fdir_pf_active_filters = 0;
  5047. }
  5048. /**
  5049. * i40e_close - Disables a network interface
  5050. * @netdev: network interface device structure
  5051. *
  5052. * The close entry point is called when an interface is de-activated
  5053. * by the OS. The hardware is still under the driver's control, but
  5054. * this netdev interface is disabled.
  5055. *
  5056. * Returns 0, this is not allowed to fail
  5057. **/
  5058. int i40e_close(struct net_device *netdev)
  5059. {
  5060. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5061. struct i40e_vsi *vsi = np->vsi;
  5062. i40e_vsi_close(vsi);
  5063. return 0;
  5064. }
  5065. /**
  5066. * i40e_do_reset - Start a PF or Core Reset sequence
  5067. * @pf: board private structure
  5068. * @reset_flags: which reset is requested
  5069. *
  5070. * The essential difference in resets is that the PF Reset
  5071. * doesn't clear the packet buffers, doesn't reset the PE
  5072. * firmware, and doesn't bother the other PFs on the chip.
  5073. **/
  5074. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
  5075. {
  5076. u32 val;
  5077. WARN_ON(in_interrupt());
  5078. /* do the biggest reset indicated */
  5079. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  5080. /* Request a Global Reset
  5081. *
  5082. * This will start the chip's countdown to the actual full
  5083. * chip reset event, and a warning interrupt to be sent
  5084. * to all PFs, including the requestor. Our handler
  5085. * for the warning interrupt will deal with the shutdown
  5086. * and recovery of the switch setup.
  5087. */
  5088. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  5089. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  5090. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  5091. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5092. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  5093. /* Request a Core Reset
  5094. *
  5095. * Same as Global Reset, except does *not* include the MAC/PHY
  5096. */
  5097. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  5098. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  5099. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  5100. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5101. i40e_flush(&pf->hw);
  5102. } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
  5103. /* Request a PF Reset
  5104. *
  5105. * Resets only the PF-specific registers
  5106. *
  5107. * This goes directly to the tear-down and rebuild of
  5108. * the switch, since we need to do all the recovery as
  5109. * for the Core Reset.
  5110. */
  5111. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  5112. i40e_handle_reset_warning(pf);
  5113. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  5114. int v;
  5115. /* Find the VSI(s) that requested a re-init */
  5116. dev_info(&pf->pdev->dev,
  5117. "VSI reinit requested\n");
  5118. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5119. struct i40e_vsi *vsi = pf->vsi[v];
  5120. if (vsi != NULL &&
  5121. test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
  5122. i40e_vsi_reinit_locked(pf->vsi[v]);
  5123. clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  5124. }
  5125. }
  5126. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  5127. int v;
  5128. /* Find the VSI(s) that needs to be brought down */
  5129. dev_info(&pf->pdev->dev, "VSI down requested\n");
  5130. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5131. struct i40e_vsi *vsi = pf->vsi[v];
  5132. if (vsi != NULL &&
  5133. test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
  5134. set_bit(__I40E_DOWN, &vsi->state);
  5135. i40e_down(vsi);
  5136. clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
  5137. }
  5138. }
  5139. } else {
  5140. dev_info(&pf->pdev->dev,
  5141. "bad reset request 0x%08x\n", reset_flags);
  5142. }
  5143. }
  5144. #ifdef CONFIG_I40E_DCB
  5145. /**
  5146. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  5147. * @pf: board private structure
  5148. * @old_cfg: current DCB config
  5149. * @new_cfg: new DCB config
  5150. **/
  5151. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  5152. struct i40e_dcbx_config *old_cfg,
  5153. struct i40e_dcbx_config *new_cfg)
  5154. {
  5155. bool need_reconfig = false;
  5156. /* Check if ETS configuration has changed */
  5157. if (memcmp(&new_cfg->etscfg,
  5158. &old_cfg->etscfg,
  5159. sizeof(new_cfg->etscfg))) {
  5160. /* If Priority Table has changed reconfig is needed */
  5161. if (memcmp(&new_cfg->etscfg.prioritytable,
  5162. &old_cfg->etscfg.prioritytable,
  5163. sizeof(new_cfg->etscfg.prioritytable))) {
  5164. need_reconfig = true;
  5165. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  5166. }
  5167. if (memcmp(&new_cfg->etscfg.tcbwtable,
  5168. &old_cfg->etscfg.tcbwtable,
  5169. sizeof(new_cfg->etscfg.tcbwtable)))
  5170. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  5171. if (memcmp(&new_cfg->etscfg.tsatable,
  5172. &old_cfg->etscfg.tsatable,
  5173. sizeof(new_cfg->etscfg.tsatable)))
  5174. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  5175. }
  5176. /* Check if PFC configuration has changed */
  5177. if (memcmp(&new_cfg->pfc,
  5178. &old_cfg->pfc,
  5179. sizeof(new_cfg->pfc))) {
  5180. need_reconfig = true;
  5181. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  5182. }
  5183. /* Check if APP Table has changed */
  5184. if (memcmp(&new_cfg->app,
  5185. &old_cfg->app,
  5186. sizeof(new_cfg->app))) {
  5187. need_reconfig = true;
  5188. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  5189. }
  5190. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  5191. return need_reconfig;
  5192. }
  5193. /**
  5194. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  5195. * @pf: board private structure
  5196. * @e: event info posted on ARQ
  5197. **/
  5198. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  5199. struct i40e_arq_event_info *e)
  5200. {
  5201. struct i40e_aqc_lldp_get_mib *mib =
  5202. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  5203. struct i40e_hw *hw = &pf->hw;
  5204. struct i40e_dcbx_config tmp_dcbx_cfg;
  5205. bool need_reconfig = false;
  5206. int ret = 0;
  5207. u8 type;
  5208. /* Not DCB capable or capability disabled */
  5209. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  5210. return ret;
  5211. /* Ignore if event is not for Nearest Bridge */
  5212. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  5213. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  5214. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  5215. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  5216. return ret;
  5217. /* Check MIB Type and return if event for Remote MIB update */
  5218. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  5219. dev_dbg(&pf->pdev->dev,
  5220. "LLDP event mib type %s\n", type ? "remote" : "local");
  5221. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  5222. /* Update the remote cached instance and return */
  5223. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  5224. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  5225. &hw->remote_dcbx_config);
  5226. goto exit;
  5227. }
  5228. /* Store the old configuration */
  5229. tmp_dcbx_cfg = hw->local_dcbx_config;
  5230. /* Reset the old DCBx configuration data */
  5231. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  5232. /* Get updated DCBX data from firmware */
  5233. ret = i40e_get_dcb_config(&pf->hw);
  5234. if (ret) {
  5235. dev_info(&pf->pdev->dev,
  5236. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  5237. i40e_stat_str(&pf->hw, ret),
  5238. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5239. goto exit;
  5240. }
  5241. /* No change detected in DCBX configs */
  5242. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  5243. sizeof(tmp_dcbx_cfg))) {
  5244. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  5245. goto exit;
  5246. }
  5247. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  5248. &hw->local_dcbx_config);
  5249. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  5250. if (!need_reconfig)
  5251. goto exit;
  5252. /* Enable DCB tagging only when more than one TC */
  5253. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5254. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5255. else
  5256. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5257. set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  5258. /* Reconfiguration needed quiesce all VSIs */
  5259. i40e_pf_quiesce_all_vsi(pf);
  5260. /* Changes in configuration update VEB/VSI */
  5261. i40e_dcb_reconfigure(pf);
  5262. ret = i40e_resume_port_tx(pf);
  5263. clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  5264. /* In case of error no point in resuming VSIs */
  5265. if (ret)
  5266. goto exit;
  5267. /* Wait for the PF's queues to be disabled */
  5268. ret = i40e_pf_wait_queues_disabled(pf);
  5269. if (ret) {
  5270. /* Schedule PF reset to recover */
  5271. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5272. i40e_service_event_schedule(pf);
  5273. } else {
  5274. i40e_pf_unquiesce_all_vsi(pf);
  5275. /* Notify the client for the DCB changes */
  5276. i40e_notify_client_of_l2_param_changes(pf->vsi[pf->lan_vsi]);
  5277. }
  5278. exit:
  5279. return ret;
  5280. }
  5281. #endif /* CONFIG_I40E_DCB */
  5282. /**
  5283. * i40e_do_reset_safe - Protected reset path for userland calls.
  5284. * @pf: board private structure
  5285. * @reset_flags: which reset is requested
  5286. *
  5287. **/
  5288. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  5289. {
  5290. rtnl_lock();
  5291. i40e_do_reset(pf, reset_flags);
  5292. rtnl_unlock();
  5293. }
  5294. /**
  5295. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  5296. * @pf: board private structure
  5297. * @e: event info posted on ARQ
  5298. *
  5299. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  5300. * and VF queues
  5301. **/
  5302. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  5303. struct i40e_arq_event_info *e)
  5304. {
  5305. struct i40e_aqc_lan_overflow *data =
  5306. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  5307. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  5308. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  5309. struct i40e_hw *hw = &pf->hw;
  5310. struct i40e_vf *vf;
  5311. u16 vf_id;
  5312. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  5313. queue, qtx_ctl);
  5314. /* Queue belongs to VF, find the VF and issue VF reset */
  5315. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  5316. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  5317. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  5318. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  5319. vf_id -= hw->func_caps.vf_base_id;
  5320. vf = &pf->vf[vf_id];
  5321. i40e_vc_notify_vf_reset(vf);
  5322. /* Allow VF to process pending reset notification */
  5323. msleep(20);
  5324. i40e_reset_vf(vf, false);
  5325. }
  5326. }
  5327. /**
  5328. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  5329. * @pf: board private structure
  5330. **/
  5331. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  5332. {
  5333. u32 val, fcnt_prog;
  5334. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5335. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  5336. return fcnt_prog;
  5337. }
  5338. /**
  5339. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  5340. * @pf: board private structure
  5341. **/
  5342. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  5343. {
  5344. u32 val, fcnt_prog;
  5345. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5346. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  5347. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  5348. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  5349. return fcnt_prog;
  5350. }
  5351. /**
  5352. * i40e_get_global_fd_count - Get total FD filters programmed on device
  5353. * @pf: board private structure
  5354. **/
  5355. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  5356. {
  5357. u32 val, fcnt_prog;
  5358. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  5359. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  5360. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  5361. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  5362. return fcnt_prog;
  5363. }
  5364. /**
  5365. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  5366. * @pf: board private structure
  5367. **/
  5368. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  5369. {
  5370. struct i40e_fdir_filter *filter;
  5371. u32 fcnt_prog, fcnt_avail;
  5372. struct hlist_node *node;
  5373. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5374. return;
  5375. /* Check if, FD SB or ATR was auto disabled and if there is enough room
  5376. * to re-enable
  5377. */
  5378. fcnt_prog = i40e_get_global_fd_count(pf);
  5379. fcnt_avail = pf->fdir_pf_filter_count;
  5380. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  5381. (pf->fd_add_err == 0) ||
  5382. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
  5383. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  5384. (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
  5385. pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5386. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5387. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  5388. }
  5389. }
  5390. /* Wait for some more space to be available to turn on ATR. We also
  5391. * must check that no existing ntuple rules for TCP are in effect
  5392. */
  5393. if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
  5394. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5395. (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5396. (pf->fd_tcp_rule == 0)) {
  5397. pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5398. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5399. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  5400. }
  5401. }
  5402. /* if hw had a problem adding a filter, delete it */
  5403. if (pf->fd_inv > 0) {
  5404. hlist_for_each_entry_safe(filter, node,
  5405. &pf->fdir_filter_list, fdir_node) {
  5406. if (filter->fd_id == pf->fd_inv) {
  5407. hlist_del(&filter->fdir_node);
  5408. kfree(filter);
  5409. pf->fdir_pf_active_filters--;
  5410. }
  5411. }
  5412. }
  5413. }
  5414. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  5415. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  5416. /**
  5417. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  5418. * @pf: board private structure
  5419. **/
  5420. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  5421. {
  5422. unsigned long min_flush_time;
  5423. int flush_wait_retry = 50;
  5424. bool disable_atr = false;
  5425. int fd_room;
  5426. int reg;
  5427. if (!time_after(jiffies, pf->fd_flush_timestamp +
  5428. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  5429. return;
  5430. /* If the flush is happening too quick and we have mostly SB rules we
  5431. * should not re-enable ATR for some time.
  5432. */
  5433. min_flush_time = pf->fd_flush_timestamp +
  5434. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  5435. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  5436. if (!(time_after(jiffies, min_flush_time)) &&
  5437. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  5438. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5439. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  5440. disable_atr = true;
  5441. }
  5442. pf->fd_flush_timestamp = jiffies;
  5443. pf->auto_disable_flags |= I40E_FLAG_FD_ATR_ENABLED;
  5444. /* flush all filters */
  5445. wr32(&pf->hw, I40E_PFQF_CTL_1,
  5446. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  5447. i40e_flush(&pf->hw);
  5448. pf->fd_flush_cnt++;
  5449. pf->fd_add_err = 0;
  5450. do {
  5451. /* Check FD flush status every 5-6msec */
  5452. usleep_range(5000, 6000);
  5453. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  5454. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  5455. break;
  5456. } while (flush_wait_retry--);
  5457. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  5458. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  5459. } else {
  5460. /* replay sideband filters */
  5461. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  5462. if (!disable_atr)
  5463. pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5464. clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
  5465. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5466. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  5467. }
  5468. }
  5469. /**
  5470. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  5471. * @pf: board private structure
  5472. **/
  5473. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  5474. {
  5475. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  5476. }
  5477. /* We can see up to 256 filter programming desc in transit if the filters are
  5478. * being applied really fast; before we see the first
  5479. * filter miss error on Rx queue 0. Accumulating enough error messages before
  5480. * reacting will make sure we don't cause flush too often.
  5481. */
  5482. #define I40E_MAX_FD_PROGRAM_ERROR 256
  5483. /**
  5484. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  5485. * @pf: board private structure
  5486. **/
  5487. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  5488. {
  5489. /* if interface is down do nothing */
  5490. if (test_bit(__I40E_DOWN, &pf->state))
  5491. return;
  5492. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5493. i40e_fdir_flush_and_replay(pf);
  5494. i40e_fdir_check_and_reenable(pf);
  5495. }
  5496. /**
  5497. * i40e_vsi_link_event - notify VSI of a link event
  5498. * @vsi: vsi to be notified
  5499. * @link_up: link up or down
  5500. **/
  5501. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  5502. {
  5503. if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
  5504. return;
  5505. switch (vsi->type) {
  5506. case I40E_VSI_MAIN:
  5507. #ifdef I40E_FCOE
  5508. case I40E_VSI_FCOE:
  5509. #endif
  5510. if (!vsi->netdev || !vsi->netdev_registered)
  5511. break;
  5512. if (link_up) {
  5513. netif_carrier_on(vsi->netdev);
  5514. netif_tx_wake_all_queues(vsi->netdev);
  5515. } else {
  5516. netif_carrier_off(vsi->netdev);
  5517. netif_tx_stop_all_queues(vsi->netdev);
  5518. }
  5519. break;
  5520. case I40E_VSI_SRIOV:
  5521. case I40E_VSI_VMDQ2:
  5522. case I40E_VSI_CTRL:
  5523. case I40E_VSI_IWARP:
  5524. case I40E_VSI_MIRROR:
  5525. default:
  5526. /* there is no notification for other VSIs */
  5527. break;
  5528. }
  5529. }
  5530. /**
  5531. * i40e_veb_link_event - notify elements on the veb of a link event
  5532. * @veb: veb to be notified
  5533. * @link_up: link up or down
  5534. **/
  5535. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  5536. {
  5537. struct i40e_pf *pf;
  5538. int i;
  5539. if (!veb || !veb->pf)
  5540. return;
  5541. pf = veb->pf;
  5542. /* depth first... */
  5543. for (i = 0; i < I40E_MAX_VEB; i++)
  5544. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  5545. i40e_veb_link_event(pf->veb[i], link_up);
  5546. /* ... now the local VSIs */
  5547. for (i = 0; i < pf->num_alloc_vsi; i++)
  5548. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  5549. i40e_vsi_link_event(pf->vsi[i], link_up);
  5550. }
  5551. /**
  5552. * i40e_link_event - Update netif_carrier status
  5553. * @pf: board private structure
  5554. **/
  5555. static void i40e_link_event(struct i40e_pf *pf)
  5556. {
  5557. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5558. u8 new_link_speed, old_link_speed;
  5559. i40e_status status;
  5560. bool new_link, old_link;
  5561. /* save off old link status information */
  5562. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  5563. /* set this to force the get_link_status call to refresh state */
  5564. pf->hw.phy.get_link_info = true;
  5565. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  5566. status = i40e_get_link_status(&pf->hw, &new_link);
  5567. /* On success, disable temp link polling */
  5568. if (status == I40E_SUCCESS) {
  5569. if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
  5570. pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
  5571. } else {
  5572. /* Enable link polling temporarily until i40e_get_link_status
  5573. * returns I40E_SUCCESS
  5574. */
  5575. pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
  5576. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  5577. status);
  5578. return;
  5579. }
  5580. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  5581. new_link_speed = pf->hw.phy.link_info.link_speed;
  5582. if (new_link == old_link &&
  5583. new_link_speed == old_link_speed &&
  5584. (test_bit(__I40E_DOWN, &vsi->state) ||
  5585. new_link == netif_carrier_ok(vsi->netdev)))
  5586. return;
  5587. if (!test_bit(__I40E_DOWN, &vsi->state))
  5588. i40e_print_link_message(vsi, new_link);
  5589. /* Notify the base of the switch tree connected to
  5590. * the link. Floating VEBs are not notified.
  5591. */
  5592. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  5593. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  5594. else
  5595. i40e_vsi_link_event(vsi, new_link);
  5596. if (pf->vf)
  5597. i40e_vc_notify_link_state(pf);
  5598. if (pf->flags & I40E_FLAG_PTP)
  5599. i40e_ptp_set_increment(pf);
  5600. }
  5601. /**
  5602. * i40e_watchdog_subtask - periodic checks not using event driven response
  5603. * @pf: board private structure
  5604. **/
  5605. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  5606. {
  5607. int i;
  5608. /* if interface is down do nothing */
  5609. if (test_bit(__I40E_DOWN, &pf->state) ||
  5610. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5611. return;
  5612. /* make sure we don't do these things too often */
  5613. if (time_before(jiffies, (pf->service_timer_previous +
  5614. pf->service_timer_period)))
  5615. return;
  5616. pf->service_timer_previous = jiffies;
  5617. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  5618. (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
  5619. i40e_link_event(pf);
  5620. /* Update the stats for active netdevs so the network stack
  5621. * can look at updated numbers whenever it cares to
  5622. */
  5623. for (i = 0; i < pf->num_alloc_vsi; i++)
  5624. if (pf->vsi[i] && pf->vsi[i]->netdev)
  5625. i40e_update_stats(pf->vsi[i]);
  5626. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  5627. /* Update the stats for the active switching components */
  5628. for (i = 0; i < I40E_MAX_VEB; i++)
  5629. if (pf->veb[i])
  5630. i40e_update_veb_stats(pf->veb[i]);
  5631. }
  5632. i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
  5633. }
  5634. /**
  5635. * i40e_reset_subtask - Set up for resetting the device and driver
  5636. * @pf: board private structure
  5637. **/
  5638. static void i40e_reset_subtask(struct i40e_pf *pf)
  5639. {
  5640. u32 reset_flags = 0;
  5641. rtnl_lock();
  5642. if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
  5643. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  5644. clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
  5645. }
  5646. if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
  5647. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  5648. clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5649. }
  5650. if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
  5651. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  5652. clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  5653. }
  5654. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
  5655. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  5656. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  5657. }
  5658. if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
  5659. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  5660. clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
  5661. }
  5662. /* If there's a recovery already waiting, it takes
  5663. * precedence before starting a new reset sequence.
  5664. */
  5665. if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
  5666. i40e_handle_reset_warning(pf);
  5667. goto unlock;
  5668. }
  5669. /* If we're already down or resetting, just bail */
  5670. if (reset_flags &&
  5671. !test_bit(__I40E_DOWN, &pf->state) &&
  5672. !test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5673. i40e_do_reset(pf, reset_flags);
  5674. unlock:
  5675. rtnl_unlock();
  5676. }
  5677. /**
  5678. * i40e_handle_link_event - Handle link event
  5679. * @pf: board private structure
  5680. * @e: event info posted on ARQ
  5681. **/
  5682. static void i40e_handle_link_event(struct i40e_pf *pf,
  5683. struct i40e_arq_event_info *e)
  5684. {
  5685. struct i40e_aqc_get_link_status *status =
  5686. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  5687. /* Do a new status request to re-enable LSE reporting
  5688. * and load new status information into the hw struct
  5689. * This completely ignores any state information
  5690. * in the ARQ event info, instead choosing to always
  5691. * issue the AQ update link status command.
  5692. */
  5693. i40e_link_event(pf);
  5694. /* check for unqualified module, if link is down */
  5695. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  5696. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  5697. (!(status->link_info & I40E_AQ_LINK_UP)))
  5698. dev_err(&pf->pdev->dev,
  5699. "The driver failed to link because an unqualified module was detected.\n");
  5700. }
  5701. /**
  5702. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  5703. * @pf: board private structure
  5704. **/
  5705. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  5706. {
  5707. struct i40e_arq_event_info event;
  5708. struct i40e_hw *hw = &pf->hw;
  5709. u16 pending, i = 0;
  5710. i40e_status ret;
  5711. u16 opcode;
  5712. u32 oldval;
  5713. u32 val;
  5714. /* Do not run clean AQ when PF reset fails */
  5715. if (test_bit(__I40E_RESET_FAILED, &pf->state))
  5716. return;
  5717. /* check for error indications */
  5718. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  5719. oldval = val;
  5720. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  5721. if (hw->debug_mask & I40E_DEBUG_AQ)
  5722. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  5723. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  5724. }
  5725. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  5726. if (hw->debug_mask & I40E_DEBUG_AQ)
  5727. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  5728. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  5729. pf->arq_overflows++;
  5730. }
  5731. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  5732. if (hw->debug_mask & I40E_DEBUG_AQ)
  5733. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  5734. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  5735. }
  5736. if (oldval != val)
  5737. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  5738. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  5739. oldval = val;
  5740. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  5741. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5742. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  5743. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  5744. }
  5745. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  5746. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5747. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  5748. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  5749. }
  5750. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  5751. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5752. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  5753. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  5754. }
  5755. if (oldval != val)
  5756. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  5757. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  5758. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  5759. if (!event.msg_buf)
  5760. return;
  5761. do {
  5762. ret = i40e_clean_arq_element(hw, &event, &pending);
  5763. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  5764. break;
  5765. else if (ret) {
  5766. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  5767. break;
  5768. }
  5769. opcode = le16_to_cpu(event.desc.opcode);
  5770. switch (opcode) {
  5771. case i40e_aqc_opc_get_link_status:
  5772. i40e_handle_link_event(pf, &event);
  5773. break;
  5774. case i40e_aqc_opc_send_msg_to_pf:
  5775. ret = i40e_vc_process_vf_msg(pf,
  5776. le16_to_cpu(event.desc.retval),
  5777. le32_to_cpu(event.desc.cookie_high),
  5778. le32_to_cpu(event.desc.cookie_low),
  5779. event.msg_buf,
  5780. event.msg_len);
  5781. break;
  5782. case i40e_aqc_opc_lldp_update_mib:
  5783. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  5784. #ifdef CONFIG_I40E_DCB
  5785. rtnl_lock();
  5786. ret = i40e_handle_lldp_event(pf, &event);
  5787. rtnl_unlock();
  5788. #endif /* CONFIG_I40E_DCB */
  5789. break;
  5790. case i40e_aqc_opc_event_lan_overflow:
  5791. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  5792. i40e_handle_lan_overflow_event(pf, &event);
  5793. break;
  5794. case i40e_aqc_opc_send_msg_to_peer:
  5795. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  5796. break;
  5797. case i40e_aqc_opc_nvm_erase:
  5798. case i40e_aqc_opc_nvm_update:
  5799. case i40e_aqc_opc_oem_post_update:
  5800. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  5801. "ARQ NVM operation 0x%04x completed\n",
  5802. opcode);
  5803. break;
  5804. default:
  5805. dev_info(&pf->pdev->dev,
  5806. "ARQ: Unknown event 0x%04x ignored\n",
  5807. opcode);
  5808. break;
  5809. }
  5810. } while (pending && (i++ < pf->adminq_work_limit));
  5811. clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  5812. /* re-enable Admin queue interrupt cause */
  5813. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  5814. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  5815. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  5816. i40e_flush(hw);
  5817. kfree(event.msg_buf);
  5818. }
  5819. /**
  5820. * i40e_verify_eeprom - make sure eeprom is good to use
  5821. * @pf: board private structure
  5822. **/
  5823. static void i40e_verify_eeprom(struct i40e_pf *pf)
  5824. {
  5825. int err;
  5826. err = i40e_diag_eeprom_test(&pf->hw);
  5827. if (err) {
  5828. /* retry in case of garbage read */
  5829. err = i40e_diag_eeprom_test(&pf->hw);
  5830. if (err) {
  5831. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  5832. err);
  5833. set_bit(__I40E_BAD_EEPROM, &pf->state);
  5834. }
  5835. }
  5836. if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  5837. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  5838. clear_bit(__I40E_BAD_EEPROM, &pf->state);
  5839. }
  5840. }
  5841. /**
  5842. * i40e_enable_pf_switch_lb
  5843. * @pf: pointer to the PF structure
  5844. *
  5845. * enable switch loop back or die - no point in a return value
  5846. **/
  5847. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  5848. {
  5849. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5850. struct i40e_vsi_context ctxt;
  5851. int ret;
  5852. ctxt.seid = pf->main_vsi_seid;
  5853. ctxt.pf_num = pf->hw.pf_id;
  5854. ctxt.vf_num = 0;
  5855. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5856. if (ret) {
  5857. dev_info(&pf->pdev->dev,
  5858. "couldn't get PF vsi config, err %s aq_err %s\n",
  5859. i40e_stat_str(&pf->hw, ret),
  5860. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5861. return;
  5862. }
  5863. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5864. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5865. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5866. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5867. if (ret) {
  5868. dev_info(&pf->pdev->dev,
  5869. "update vsi switch failed, err %s aq_err %s\n",
  5870. i40e_stat_str(&pf->hw, ret),
  5871. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5872. }
  5873. }
  5874. /**
  5875. * i40e_disable_pf_switch_lb
  5876. * @pf: pointer to the PF structure
  5877. *
  5878. * disable switch loop back or die - no point in a return value
  5879. **/
  5880. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  5881. {
  5882. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5883. struct i40e_vsi_context ctxt;
  5884. int ret;
  5885. ctxt.seid = pf->main_vsi_seid;
  5886. ctxt.pf_num = pf->hw.pf_id;
  5887. ctxt.vf_num = 0;
  5888. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5889. if (ret) {
  5890. dev_info(&pf->pdev->dev,
  5891. "couldn't get PF vsi config, err %s aq_err %s\n",
  5892. i40e_stat_str(&pf->hw, ret),
  5893. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5894. return;
  5895. }
  5896. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5897. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5898. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5899. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5900. if (ret) {
  5901. dev_info(&pf->pdev->dev,
  5902. "update vsi switch failed, err %s aq_err %s\n",
  5903. i40e_stat_str(&pf->hw, ret),
  5904. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5905. }
  5906. }
  5907. /**
  5908. * i40e_config_bridge_mode - Configure the HW bridge mode
  5909. * @veb: pointer to the bridge instance
  5910. *
  5911. * Configure the loop back mode for the LAN VSI that is downlink to the
  5912. * specified HW bridge instance. It is expected this function is called
  5913. * when a new HW bridge is instantiated.
  5914. **/
  5915. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  5916. {
  5917. struct i40e_pf *pf = veb->pf;
  5918. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  5919. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  5920. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  5921. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  5922. i40e_disable_pf_switch_lb(pf);
  5923. else
  5924. i40e_enable_pf_switch_lb(pf);
  5925. }
  5926. /**
  5927. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  5928. * @veb: pointer to the VEB instance
  5929. *
  5930. * This is a recursive function that first builds the attached VSIs then
  5931. * recurses in to build the next layer of VEB. We track the connections
  5932. * through our own index numbers because the seid's from the HW could
  5933. * change across the reset.
  5934. **/
  5935. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  5936. {
  5937. struct i40e_vsi *ctl_vsi = NULL;
  5938. struct i40e_pf *pf = veb->pf;
  5939. int v, veb_idx;
  5940. int ret;
  5941. /* build VSI that owns this VEB, temporarily attached to base VEB */
  5942. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  5943. if (pf->vsi[v] &&
  5944. pf->vsi[v]->veb_idx == veb->idx &&
  5945. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  5946. ctl_vsi = pf->vsi[v];
  5947. break;
  5948. }
  5949. }
  5950. if (!ctl_vsi) {
  5951. dev_info(&pf->pdev->dev,
  5952. "missing owner VSI for veb_idx %d\n", veb->idx);
  5953. ret = -ENOENT;
  5954. goto end_reconstitute;
  5955. }
  5956. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  5957. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5958. ret = i40e_add_vsi(ctl_vsi);
  5959. if (ret) {
  5960. dev_info(&pf->pdev->dev,
  5961. "rebuild of veb_idx %d owner VSI failed: %d\n",
  5962. veb->idx, ret);
  5963. goto end_reconstitute;
  5964. }
  5965. i40e_vsi_reset_stats(ctl_vsi);
  5966. /* create the VEB in the switch and move the VSI onto the VEB */
  5967. ret = i40e_add_veb(veb, ctl_vsi);
  5968. if (ret)
  5969. goto end_reconstitute;
  5970. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  5971. veb->bridge_mode = BRIDGE_MODE_VEB;
  5972. else
  5973. veb->bridge_mode = BRIDGE_MODE_VEPA;
  5974. i40e_config_bridge_mode(veb);
  5975. /* create the remaining VSIs attached to this VEB */
  5976. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5977. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  5978. continue;
  5979. if (pf->vsi[v]->veb_idx == veb->idx) {
  5980. struct i40e_vsi *vsi = pf->vsi[v];
  5981. vsi->uplink_seid = veb->seid;
  5982. ret = i40e_add_vsi(vsi);
  5983. if (ret) {
  5984. dev_info(&pf->pdev->dev,
  5985. "rebuild of vsi_idx %d failed: %d\n",
  5986. v, ret);
  5987. goto end_reconstitute;
  5988. }
  5989. i40e_vsi_reset_stats(vsi);
  5990. }
  5991. }
  5992. /* create any VEBs attached to this VEB - RECURSION */
  5993. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  5994. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  5995. pf->veb[veb_idx]->uplink_seid = veb->seid;
  5996. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  5997. if (ret)
  5998. break;
  5999. }
  6000. }
  6001. end_reconstitute:
  6002. return ret;
  6003. }
  6004. /**
  6005. * i40e_get_capabilities - get info about the HW
  6006. * @pf: the PF struct
  6007. **/
  6008. static int i40e_get_capabilities(struct i40e_pf *pf)
  6009. {
  6010. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  6011. u16 data_size;
  6012. int buf_len;
  6013. int err;
  6014. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  6015. do {
  6016. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  6017. if (!cap_buf)
  6018. return -ENOMEM;
  6019. /* this loads the data into the hw struct for us */
  6020. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  6021. &data_size,
  6022. i40e_aqc_opc_list_func_capabilities,
  6023. NULL);
  6024. /* data loaded, buffer no longer needed */
  6025. kfree(cap_buf);
  6026. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  6027. /* retry with a larger buffer */
  6028. buf_len = data_size;
  6029. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  6030. dev_info(&pf->pdev->dev,
  6031. "capability discovery failed, err %s aq_err %s\n",
  6032. i40e_stat_str(&pf->hw, err),
  6033. i40e_aq_str(&pf->hw,
  6034. pf->hw.aq.asq_last_status));
  6035. return -ENODEV;
  6036. }
  6037. } while (err);
  6038. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  6039. dev_info(&pf->pdev->dev,
  6040. "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",
  6041. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  6042. pf->hw.func_caps.num_msix_vectors,
  6043. pf->hw.func_caps.num_msix_vectors_vf,
  6044. pf->hw.func_caps.fd_filters_guaranteed,
  6045. pf->hw.func_caps.fd_filters_best_effort,
  6046. pf->hw.func_caps.num_tx_qp,
  6047. pf->hw.func_caps.num_vsis);
  6048. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  6049. + pf->hw.func_caps.num_vfs)
  6050. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  6051. dev_info(&pf->pdev->dev,
  6052. "got num_vsis %d, setting num_vsis to %d\n",
  6053. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  6054. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  6055. }
  6056. return 0;
  6057. }
  6058. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  6059. /**
  6060. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  6061. * @pf: board private structure
  6062. **/
  6063. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  6064. {
  6065. struct i40e_vsi *vsi;
  6066. /* quick workaround for an NVM issue that leaves a critical register
  6067. * uninitialized
  6068. */
  6069. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  6070. static const u32 hkey[] = {
  6071. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  6072. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  6073. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  6074. 0x95b3a76d};
  6075. int i;
  6076. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  6077. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  6078. }
  6079. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  6080. return;
  6081. /* find existing VSI and see if it needs configuring */
  6082. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  6083. /* create a new VSI if none exists */
  6084. if (!vsi) {
  6085. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  6086. pf->vsi[pf->lan_vsi]->seid, 0);
  6087. if (!vsi) {
  6088. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  6089. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6090. return;
  6091. }
  6092. }
  6093. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  6094. }
  6095. /**
  6096. * i40e_fdir_teardown - release the Flow Director resources
  6097. * @pf: board private structure
  6098. **/
  6099. static void i40e_fdir_teardown(struct i40e_pf *pf)
  6100. {
  6101. struct i40e_vsi *vsi;
  6102. i40e_fdir_filter_exit(pf);
  6103. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  6104. if (vsi)
  6105. i40e_vsi_release(vsi);
  6106. }
  6107. /**
  6108. * i40e_prep_for_reset - prep for the core to reset
  6109. * @pf: board private structure
  6110. *
  6111. * Close up the VFs and other things in prep for PF Reset.
  6112. **/
  6113. static void i40e_prep_for_reset(struct i40e_pf *pf)
  6114. {
  6115. struct i40e_hw *hw = &pf->hw;
  6116. i40e_status ret = 0;
  6117. u32 v;
  6118. clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  6119. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  6120. return;
  6121. if (i40e_check_asq_alive(&pf->hw))
  6122. i40e_vc_notify_reset(pf);
  6123. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  6124. /* quiesce the VSIs and their queues that are not already DOWN */
  6125. i40e_pf_quiesce_all_vsi(pf);
  6126. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6127. if (pf->vsi[v])
  6128. pf->vsi[v]->seid = 0;
  6129. }
  6130. i40e_shutdown_adminq(&pf->hw);
  6131. /* call shutdown HMC */
  6132. if (hw->hmc.hmc_obj) {
  6133. ret = i40e_shutdown_lan_hmc(hw);
  6134. if (ret)
  6135. dev_warn(&pf->pdev->dev,
  6136. "shutdown_lan_hmc failed: %d\n", ret);
  6137. }
  6138. }
  6139. /**
  6140. * i40e_send_version - update firmware with driver version
  6141. * @pf: PF struct
  6142. */
  6143. static void i40e_send_version(struct i40e_pf *pf)
  6144. {
  6145. struct i40e_driver_version dv;
  6146. dv.major_version = DRV_VERSION_MAJOR;
  6147. dv.minor_version = DRV_VERSION_MINOR;
  6148. dv.build_version = DRV_VERSION_BUILD;
  6149. dv.subbuild_version = 0;
  6150. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  6151. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  6152. }
  6153. /**
  6154. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  6155. * @pf: board private structure
  6156. * @reinit: if the Main VSI needs to re-initialized.
  6157. **/
  6158. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
  6159. {
  6160. struct i40e_hw *hw = &pf->hw;
  6161. u8 set_fc_aq_fail = 0;
  6162. i40e_status ret;
  6163. u32 val;
  6164. u32 v;
  6165. /* Now we wait for GRST to settle out.
  6166. * We don't have to delete the VEBs or VSIs from the hw switch
  6167. * because the reset will make them disappear.
  6168. */
  6169. ret = i40e_pf_reset(hw);
  6170. if (ret) {
  6171. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  6172. set_bit(__I40E_RESET_FAILED, &pf->state);
  6173. goto clear_recovery;
  6174. }
  6175. pf->pfr_count++;
  6176. if (test_bit(__I40E_DOWN, &pf->state))
  6177. goto clear_recovery;
  6178. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  6179. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  6180. ret = i40e_init_adminq(&pf->hw);
  6181. if (ret) {
  6182. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  6183. i40e_stat_str(&pf->hw, ret),
  6184. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6185. goto clear_recovery;
  6186. }
  6187. /* re-verify the eeprom if we just had an EMP reset */
  6188. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
  6189. i40e_verify_eeprom(pf);
  6190. i40e_clear_pxe_mode(hw);
  6191. ret = i40e_get_capabilities(pf);
  6192. if (ret)
  6193. goto end_core_reset;
  6194. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  6195. hw->func_caps.num_rx_qp,
  6196. pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
  6197. if (ret) {
  6198. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  6199. goto end_core_reset;
  6200. }
  6201. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  6202. if (ret) {
  6203. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  6204. goto end_core_reset;
  6205. }
  6206. #ifdef CONFIG_I40E_DCB
  6207. ret = i40e_init_pf_dcb(pf);
  6208. if (ret) {
  6209. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  6210. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  6211. /* Continue without DCB enabled */
  6212. }
  6213. #endif /* CONFIG_I40E_DCB */
  6214. #ifdef I40E_FCOE
  6215. i40e_init_pf_fcoe(pf);
  6216. #endif
  6217. /* do basic switch setup */
  6218. ret = i40e_setup_pf_switch(pf, reinit);
  6219. if (ret)
  6220. goto end_core_reset;
  6221. /* The driver only wants link up/down and module qualification
  6222. * reports from firmware. Note the negative logic.
  6223. */
  6224. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  6225. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  6226. I40E_AQ_EVENT_MEDIA_NA |
  6227. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  6228. if (ret)
  6229. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  6230. i40e_stat_str(&pf->hw, ret),
  6231. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6232. /* make sure our flow control settings are restored */
  6233. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  6234. if (ret)
  6235. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  6236. i40e_stat_str(&pf->hw, ret),
  6237. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6238. /* Rebuild the VSIs and VEBs that existed before reset.
  6239. * They are still in our local switch element arrays, so only
  6240. * need to rebuild the switch model in the HW.
  6241. *
  6242. * If there were VEBs but the reconstitution failed, we'll try
  6243. * try to recover minimal use by getting the basic PF VSI working.
  6244. */
  6245. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  6246. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  6247. /* find the one VEB connected to the MAC, and find orphans */
  6248. for (v = 0; v < I40E_MAX_VEB; v++) {
  6249. if (!pf->veb[v])
  6250. continue;
  6251. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  6252. pf->veb[v]->uplink_seid == 0) {
  6253. ret = i40e_reconstitute_veb(pf->veb[v]);
  6254. if (!ret)
  6255. continue;
  6256. /* If Main VEB failed, we're in deep doodoo,
  6257. * so give up rebuilding the switch and set up
  6258. * for minimal rebuild of PF VSI.
  6259. * If orphan failed, we'll report the error
  6260. * but try to keep going.
  6261. */
  6262. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  6263. dev_info(&pf->pdev->dev,
  6264. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  6265. ret);
  6266. pf->vsi[pf->lan_vsi]->uplink_seid
  6267. = pf->mac_seid;
  6268. break;
  6269. } else if (pf->veb[v]->uplink_seid == 0) {
  6270. dev_info(&pf->pdev->dev,
  6271. "rebuild of orphan VEB failed: %d\n",
  6272. ret);
  6273. }
  6274. }
  6275. }
  6276. }
  6277. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  6278. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  6279. /* no VEB, so rebuild only the Main VSI */
  6280. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  6281. if (ret) {
  6282. dev_info(&pf->pdev->dev,
  6283. "rebuild of Main VSI failed: %d\n", ret);
  6284. goto end_core_reset;
  6285. }
  6286. }
  6287. /* Reconfigure hardware for allowing smaller MSS in the case
  6288. * of TSO, so that we avoid the MDD being fired and causing
  6289. * a reset in the case of small MSS+TSO.
  6290. */
  6291. #define I40E_REG_MSS 0x000E64DC
  6292. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  6293. #define I40E_64BYTE_MSS 0x400000
  6294. val = rd32(hw, I40E_REG_MSS);
  6295. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  6296. val &= ~I40E_REG_MSS_MIN_MASK;
  6297. val |= I40E_64BYTE_MSS;
  6298. wr32(hw, I40E_REG_MSS, val);
  6299. }
  6300. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  6301. msleep(75);
  6302. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  6303. if (ret)
  6304. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  6305. i40e_stat_str(&pf->hw, ret),
  6306. i40e_aq_str(&pf->hw,
  6307. pf->hw.aq.asq_last_status));
  6308. }
  6309. /* reinit the misc interrupt */
  6310. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6311. ret = i40e_setup_misc_vector(pf);
  6312. /* Add a filter to drop all Flow control frames from any VSI from being
  6313. * transmitted. By doing so we stop a malicious VF from sending out
  6314. * PAUSE or PFC frames and potentially controlling traffic for other
  6315. * PF/VF VSIs.
  6316. * The FW can still send Flow control frames if enabled.
  6317. */
  6318. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  6319. pf->main_vsi_seid);
  6320. /* restart the VSIs that were rebuilt and running before the reset */
  6321. i40e_pf_unquiesce_all_vsi(pf);
  6322. if (pf->num_alloc_vfs) {
  6323. for (v = 0; v < pf->num_alloc_vfs; v++)
  6324. i40e_reset_vf(&pf->vf[v], true);
  6325. }
  6326. /* tell the firmware that we're starting */
  6327. i40e_send_version(pf);
  6328. end_core_reset:
  6329. clear_bit(__I40E_RESET_FAILED, &pf->state);
  6330. clear_recovery:
  6331. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  6332. }
  6333. /**
  6334. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  6335. * @pf: board private structure
  6336. *
  6337. * Close up the VFs and other things in prep for a Core Reset,
  6338. * then get ready to rebuild the world.
  6339. **/
  6340. static void i40e_handle_reset_warning(struct i40e_pf *pf)
  6341. {
  6342. i40e_prep_for_reset(pf);
  6343. i40e_reset_and_rebuild(pf, false);
  6344. }
  6345. /**
  6346. * i40e_handle_mdd_event
  6347. * @pf: pointer to the PF structure
  6348. *
  6349. * Called from the MDD irq handler to identify possibly malicious vfs
  6350. **/
  6351. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  6352. {
  6353. struct i40e_hw *hw = &pf->hw;
  6354. bool mdd_detected = false;
  6355. bool pf_mdd_detected = false;
  6356. struct i40e_vf *vf;
  6357. u32 reg;
  6358. int i;
  6359. if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
  6360. return;
  6361. /* find what triggered the MDD event */
  6362. reg = rd32(hw, I40E_GL_MDET_TX);
  6363. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  6364. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  6365. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  6366. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  6367. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  6368. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  6369. I40E_GL_MDET_TX_EVENT_SHIFT;
  6370. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  6371. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  6372. pf->hw.func_caps.base_queue;
  6373. if (netif_msg_tx_err(pf))
  6374. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  6375. event, queue, pf_num, vf_num);
  6376. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  6377. mdd_detected = true;
  6378. }
  6379. reg = rd32(hw, I40E_GL_MDET_RX);
  6380. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  6381. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  6382. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  6383. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  6384. I40E_GL_MDET_RX_EVENT_SHIFT;
  6385. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  6386. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  6387. pf->hw.func_caps.base_queue;
  6388. if (netif_msg_rx_err(pf))
  6389. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  6390. event, queue, func);
  6391. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  6392. mdd_detected = true;
  6393. }
  6394. if (mdd_detected) {
  6395. reg = rd32(hw, I40E_PF_MDET_TX);
  6396. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  6397. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  6398. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  6399. pf_mdd_detected = true;
  6400. }
  6401. reg = rd32(hw, I40E_PF_MDET_RX);
  6402. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  6403. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  6404. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  6405. pf_mdd_detected = true;
  6406. }
  6407. /* Queue belongs to the PF, initiate a reset */
  6408. if (pf_mdd_detected) {
  6409. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  6410. i40e_service_event_schedule(pf);
  6411. }
  6412. }
  6413. /* see if one of the VFs needs its hand slapped */
  6414. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  6415. vf = &(pf->vf[i]);
  6416. reg = rd32(hw, I40E_VP_MDET_TX(i));
  6417. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  6418. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  6419. vf->num_mdd_events++;
  6420. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  6421. i);
  6422. }
  6423. reg = rd32(hw, I40E_VP_MDET_RX(i));
  6424. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  6425. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  6426. vf->num_mdd_events++;
  6427. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  6428. i);
  6429. }
  6430. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  6431. dev_info(&pf->pdev->dev,
  6432. "Too many MDD events on VF %d, disabled\n", i);
  6433. dev_info(&pf->pdev->dev,
  6434. "Use PF Control I/F to re-enable the VF\n");
  6435. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  6436. }
  6437. }
  6438. /* re-enable mdd interrupt cause */
  6439. clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  6440. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  6441. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  6442. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  6443. i40e_flush(hw);
  6444. }
  6445. /**
  6446. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  6447. * @pf: board private structure
  6448. **/
  6449. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  6450. {
  6451. struct i40e_hw *hw = &pf->hw;
  6452. i40e_status ret;
  6453. __be16 port;
  6454. int i;
  6455. if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
  6456. return;
  6457. pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
  6458. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6459. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  6460. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  6461. port = pf->udp_ports[i].index;
  6462. if (port)
  6463. ret = i40e_aq_add_udp_tunnel(hw, port,
  6464. pf->udp_ports[i].type,
  6465. NULL, NULL);
  6466. else
  6467. ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
  6468. if (ret) {
  6469. dev_dbg(&pf->pdev->dev,
  6470. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  6471. pf->udp_ports[i].type ? "vxlan" : "geneve",
  6472. port ? "add" : "delete",
  6473. ntohs(port), i,
  6474. i40e_stat_str(&pf->hw, ret),
  6475. i40e_aq_str(&pf->hw,
  6476. pf->hw.aq.asq_last_status));
  6477. pf->udp_ports[i].index = 0;
  6478. }
  6479. }
  6480. }
  6481. }
  6482. /**
  6483. * i40e_service_task - Run the driver's async subtasks
  6484. * @work: pointer to work_struct containing our data
  6485. **/
  6486. static void i40e_service_task(struct work_struct *work)
  6487. {
  6488. struct i40e_pf *pf = container_of(work,
  6489. struct i40e_pf,
  6490. service_task);
  6491. unsigned long start_time = jiffies;
  6492. /* don't bother with service tasks if a reset is in progress */
  6493. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  6494. return;
  6495. }
  6496. if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
  6497. return;
  6498. i40e_detect_recover_hung(pf);
  6499. i40e_sync_filters_subtask(pf);
  6500. i40e_reset_subtask(pf);
  6501. i40e_handle_mdd_event(pf);
  6502. i40e_vc_process_vflr_event(pf);
  6503. i40e_watchdog_subtask(pf);
  6504. i40e_fdir_reinit_subtask(pf);
  6505. i40e_client_subtask(pf);
  6506. i40e_sync_filters_subtask(pf);
  6507. i40e_sync_udp_filters_subtask(pf);
  6508. i40e_clean_adminq_subtask(pf);
  6509. /* flush memory to make sure state is correct before next watchdog */
  6510. smp_mb__before_atomic();
  6511. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  6512. /* If the tasks have taken longer than one timer cycle or there
  6513. * is more work to be done, reschedule the service task now
  6514. * rather than wait for the timer to tick again.
  6515. */
  6516. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  6517. test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
  6518. test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
  6519. test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  6520. i40e_service_event_schedule(pf);
  6521. }
  6522. /**
  6523. * i40e_service_timer - timer callback
  6524. * @data: pointer to PF struct
  6525. **/
  6526. static void i40e_service_timer(unsigned long data)
  6527. {
  6528. struct i40e_pf *pf = (struct i40e_pf *)data;
  6529. mod_timer(&pf->service_timer,
  6530. round_jiffies(jiffies + pf->service_timer_period));
  6531. i40e_service_event_schedule(pf);
  6532. }
  6533. /**
  6534. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  6535. * @vsi: the VSI being configured
  6536. **/
  6537. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  6538. {
  6539. struct i40e_pf *pf = vsi->back;
  6540. switch (vsi->type) {
  6541. case I40E_VSI_MAIN:
  6542. vsi->alloc_queue_pairs = pf->num_lan_qps;
  6543. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6544. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6545. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6546. vsi->num_q_vectors = pf->num_lan_msix;
  6547. else
  6548. vsi->num_q_vectors = 1;
  6549. break;
  6550. case I40E_VSI_FDIR:
  6551. vsi->alloc_queue_pairs = 1;
  6552. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  6553. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6554. vsi->num_q_vectors = pf->num_fdsb_msix;
  6555. break;
  6556. case I40E_VSI_VMDQ2:
  6557. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  6558. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6559. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6560. vsi->num_q_vectors = pf->num_vmdq_msix;
  6561. break;
  6562. case I40E_VSI_SRIOV:
  6563. vsi->alloc_queue_pairs = pf->num_vf_qps;
  6564. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6565. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6566. break;
  6567. #ifdef I40E_FCOE
  6568. case I40E_VSI_FCOE:
  6569. vsi->alloc_queue_pairs = pf->num_fcoe_qps;
  6570. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6571. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6572. vsi->num_q_vectors = pf->num_fcoe_msix;
  6573. break;
  6574. #endif /* I40E_FCOE */
  6575. default:
  6576. WARN_ON(1);
  6577. return -ENODATA;
  6578. }
  6579. return 0;
  6580. }
  6581. /**
  6582. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  6583. * @type: VSI pointer
  6584. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  6585. *
  6586. * On error: returns error code (negative)
  6587. * On success: returns 0
  6588. **/
  6589. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  6590. {
  6591. int size;
  6592. int ret = 0;
  6593. /* allocate memory for both Tx and Rx ring pointers */
  6594. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
  6595. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  6596. if (!vsi->tx_rings)
  6597. return -ENOMEM;
  6598. vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
  6599. if (alloc_qvectors) {
  6600. /* allocate memory for q_vector pointers */
  6601. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  6602. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  6603. if (!vsi->q_vectors) {
  6604. ret = -ENOMEM;
  6605. goto err_vectors;
  6606. }
  6607. }
  6608. return ret;
  6609. err_vectors:
  6610. kfree(vsi->tx_rings);
  6611. return ret;
  6612. }
  6613. /**
  6614. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  6615. * @pf: board private structure
  6616. * @type: type of VSI
  6617. *
  6618. * On error: returns error code (negative)
  6619. * On success: returns vsi index in PF (positive)
  6620. **/
  6621. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  6622. {
  6623. int ret = -ENODEV;
  6624. struct i40e_vsi *vsi;
  6625. int vsi_idx;
  6626. int i;
  6627. /* Need to protect the allocation of the VSIs at the PF level */
  6628. mutex_lock(&pf->switch_mutex);
  6629. /* VSI list may be fragmented if VSI creation/destruction has
  6630. * been happening. We can afford to do a quick scan to look
  6631. * for any free VSIs in the list.
  6632. *
  6633. * find next empty vsi slot, looping back around if necessary
  6634. */
  6635. i = pf->next_vsi;
  6636. while (i < pf->num_alloc_vsi && pf->vsi[i])
  6637. i++;
  6638. if (i >= pf->num_alloc_vsi) {
  6639. i = 0;
  6640. while (i < pf->next_vsi && pf->vsi[i])
  6641. i++;
  6642. }
  6643. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  6644. vsi_idx = i; /* Found one! */
  6645. } else {
  6646. ret = -ENODEV;
  6647. goto unlock_pf; /* out of VSI slots! */
  6648. }
  6649. pf->next_vsi = ++i;
  6650. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  6651. if (!vsi) {
  6652. ret = -ENOMEM;
  6653. goto unlock_pf;
  6654. }
  6655. vsi->type = type;
  6656. vsi->back = pf;
  6657. set_bit(__I40E_DOWN, &vsi->state);
  6658. vsi->flags = 0;
  6659. vsi->idx = vsi_idx;
  6660. vsi->int_rate_limit = 0;
  6661. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  6662. pf->rss_table_size : 64;
  6663. vsi->netdev_registered = false;
  6664. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  6665. hash_init(vsi->mac_filter_hash);
  6666. vsi->irqs_ready = false;
  6667. ret = i40e_set_num_rings_in_vsi(vsi);
  6668. if (ret)
  6669. goto err_rings;
  6670. ret = i40e_vsi_alloc_arrays(vsi, true);
  6671. if (ret)
  6672. goto err_rings;
  6673. /* Setup default MSIX irq handler for VSI */
  6674. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  6675. /* Initialize VSI lock */
  6676. spin_lock_init(&vsi->mac_filter_hash_lock);
  6677. pf->vsi[vsi_idx] = vsi;
  6678. ret = vsi_idx;
  6679. goto unlock_pf;
  6680. err_rings:
  6681. pf->next_vsi = i - 1;
  6682. kfree(vsi);
  6683. unlock_pf:
  6684. mutex_unlock(&pf->switch_mutex);
  6685. return ret;
  6686. }
  6687. /**
  6688. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  6689. * @type: VSI pointer
  6690. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  6691. *
  6692. * On error: returns error code (negative)
  6693. * On success: returns 0
  6694. **/
  6695. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  6696. {
  6697. /* free the ring and vector containers */
  6698. if (free_qvectors) {
  6699. kfree(vsi->q_vectors);
  6700. vsi->q_vectors = NULL;
  6701. }
  6702. kfree(vsi->tx_rings);
  6703. vsi->tx_rings = NULL;
  6704. vsi->rx_rings = NULL;
  6705. }
  6706. /**
  6707. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  6708. * and lookup table
  6709. * @vsi: Pointer to VSI structure
  6710. */
  6711. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  6712. {
  6713. if (!vsi)
  6714. return;
  6715. kfree(vsi->rss_hkey_user);
  6716. vsi->rss_hkey_user = NULL;
  6717. kfree(vsi->rss_lut_user);
  6718. vsi->rss_lut_user = NULL;
  6719. }
  6720. /**
  6721. * i40e_vsi_clear - Deallocate the VSI provided
  6722. * @vsi: the VSI being un-configured
  6723. **/
  6724. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  6725. {
  6726. struct i40e_pf *pf;
  6727. if (!vsi)
  6728. return 0;
  6729. if (!vsi->back)
  6730. goto free_vsi;
  6731. pf = vsi->back;
  6732. mutex_lock(&pf->switch_mutex);
  6733. if (!pf->vsi[vsi->idx]) {
  6734. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  6735. vsi->idx, vsi->idx, vsi, vsi->type);
  6736. goto unlock_vsi;
  6737. }
  6738. if (pf->vsi[vsi->idx] != vsi) {
  6739. dev_err(&pf->pdev->dev,
  6740. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  6741. pf->vsi[vsi->idx]->idx,
  6742. pf->vsi[vsi->idx],
  6743. pf->vsi[vsi->idx]->type,
  6744. vsi->idx, vsi, vsi->type);
  6745. goto unlock_vsi;
  6746. }
  6747. /* updates the PF for this cleared vsi */
  6748. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  6749. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  6750. i40e_vsi_free_arrays(vsi, true);
  6751. i40e_clear_rss_config_user(vsi);
  6752. pf->vsi[vsi->idx] = NULL;
  6753. if (vsi->idx < pf->next_vsi)
  6754. pf->next_vsi = vsi->idx;
  6755. unlock_vsi:
  6756. mutex_unlock(&pf->switch_mutex);
  6757. free_vsi:
  6758. kfree(vsi);
  6759. return 0;
  6760. }
  6761. /**
  6762. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  6763. * @vsi: the VSI being cleaned
  6764. **/
  6765. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  6766. {
  6767. int i;
  6768. if (vsi->tx_rings && vsi->tx_rings[0]) {
  6769. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6770. kfree_rcu(vsi->tx_rings[i], rcu);
  6771. vsi->tx_rings[i] = NULL;
  6772. vsi->rx_rings[i] = NULL;
  6773. }
  6774. }
  6775. }
  6776. /**
  6777. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  6778. * @vsi: the VSI being configured
  6779. **/
  6780. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  6781. {
  6782. struct i40e_ring *tx_ring, *rx_ring;
  6783. struct i40e_pf *pf = vsi->back;
  6784. int i;
  6785. /* Set basic values in the rings to be used later during open() */
  6786. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6787. /* allocate space for both Tx and Rx in one shot */
  6788. tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
  6789. if (!tx_ring)
  6790. goto err_out;
  6791. tx_ring->queue_index = i;
  6792. tx_ring->reg_idx = vsi->base_queue + i;
  6793. tx_ring->ring_active = false;
  6794. tx_ring->vsi = vsi;
  6795. tx_ring->netdev = vsi->netdev;
  6796. tx_ring->dev = &pf->pdev->dev;
  6797. tx_ring->count = vsi->num_desc;
  6798. tx_ring->size = 0;
  6799. tx_ring->dcb_tc = 0;
  6800. if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
  6801. tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  6802. tx_ring->tx_itr_setting = pf->tx_itr_default;
  6803. vsi->tx_rings[i] = tx_ring;
  6804. rx_ring = &tx_ring[1];
  6805. rx_ring->queue_index = i;
  6806. rx_ring->reg_idx = vsi->base_queue + i;
  6807. rx_ring->ring_active = false;
  6808. rx_ring->vsi = vsi;
  6809. rx_ring->netdev = vsi->netdev;
  6810. rx_ring->dev = &pf->pdev->dev;
  6811. rx_ring->count = vsi->num_desc;
  6812. rx_ring->size = 0;
  6813. rx_ring->dcb_tc = 0;
  6814. rx_ring->rx_itr_setting = pf->rx_itr_default;
  6815. vsi->rx_rings[i] = rx_ring;
  6816. }
  6817. return 0;
  6818. err_out:
  6819. i40e_vsi_clear_rings(vsi);
  6820. return -ENOMEM;
  6821. }
  6822. /**
  6823. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  6824. * @pf: board private structure
  6825. * @vectors: the number of MSI-X vectors to request
  6826. *
  6827. * Returns the number of vectors reserved, or error
  6828. **/
  6829. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  6830. {
  6831. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  6832. I40E_MIN_MSIX, vectors);
  6833. if (vectors < 0) {
  6834. dev_info(&pf->pdev->dev,
  6835. "MSI-X vector reservation failed: %d\n", vectors);
  6836. vectors = 0;
  6837. }
  6838. return vectors;
  6839. }
  6840. /**
  6841. * i40e_init_msix - Setup the MSIX capability
  6842. * @pf: board private structure
  6843. *
  6844. * Work with the OS to set up the MSIX vectors needed.
  6845. *
  6846. * Returns the number of vectors reserved or negative on failure
  6847. **/
  6848. static int i40e_init_msix(struct i40e_pf *pf)
  6849. {
  6850. struct i40e_hw *hw = &pf->hw;
  6851. int vectors_left;
  6852. int v_budget, i;
  6853. int v_actual;
  6854. int iwarp_requested = 0;
  6855. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  6856. return -ENODEV;
  6857. /* The number of vectors we'll request will be comprised of:
  6858. * - Add 1 for "other" cause for Admin Queue events, etc.
  6859. * - The number of LAN queue pairs
  6860. * - Queues being used for RSS.
  6861. * We don't need as many as max_rss_size vectors.
  6862. * use rss_size instead in the calculation since that
  6863. * is governed by number of cpus in the system.
  6864. * - assumes symmetric Tx/Rx pairing
  6865. * - The number of VMDq pairs
  6866. * - The CPU count within the NUMA node if iWARP is enabled
  6867. #ifdef I40E_FCOE
  6868. * - The number of FCOE qps.
  6869. #endif
  6870. * Once we count this up, try the request.
  6871. *
  6872. * If we can't get what we want, we'll simplify to nearly nothing
  6873. * and try again. If that still fails, we punt.
  6874. */
  6875. vectors_left = hw->func_caps.num_msix_vectors;
  6876. v_budget = 0;
  6877. /* reserve one vector for miscellaneous handler */
  6878. if (vectors_left) {
  6879. v_budget++;
  6880. vectors_left--;
  6881. }
  6882. /* reserve vectors for the main PF traffic queues */
  6883. pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
  6884. vectors_left -= pf->num_lan_msix;
  6885. v_budget += pf->num_lan_msix;
  6886. /* reserve one vector for sideband flow director */
  6887. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6888. if (vectors_left) {
  6889. pf->num_fdsb_msix = 1;
  6890. v_budget++;
  6891. vectors_left--;
  6892. } else {
  6893. pf->num_fdsb_msix = 0;
  6894. }
  6895. }
  6896. #ifdef I40E_FCOE
  6897. /* can we reserve enough for FCoE? */
  6898. if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
  6899. if (!vectors_left)
  6900. pf->num_fcoe_msix = 0;
  6901. else if (vectors_left >= pf->num_fcoe_qps)
  6902. pf->num_fcoe_msix = pf->num_fcoe_qps;
  6903. else
  6904. pf->num_fcoe_msix = 1;
  6905. v_budget += pf->num_fcoe_msix;
  6906. vectors_left -= pf->num_fcoe_msix;
  6907. }
  6908. #endif
  6909. /* can we reserve enough for iWARP? */
  6910. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6911. iwarp_requested = pf->num_iwarp_msix;
  6912. if (!vectors_left)
  6913. pf->num_iwarp_msix = 0;
  6914. else if (vectors_left < pf->num_iwarp_msix)
  6915. pf->num_iwarp_msix = 1;
  6916. v_budget += pf->num_iwarp_msix;
  6917. vectors_left -= pf->num_iwarp_msix;
  6918. }
  6919. /* any vectors left over go for VMDq support */
  6920. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  6921. int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
  6922. int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
  6923. if (!vectors_left) {
  6924. pf->num_vmdq_msix = 0;
  6925. pf->num_vmdq_qps = 0;
  6926. } else {
  6927. /* if we're short on vectors for what's desired, we limit
  6928. * the queues per vmdq. If this is still more than are
  6929. * available, the user will need to change the number of
  6930. * queues/vectors used by the PF later with the ethtool
  6931. * channels command
  6932. */
  6933. if (vmdq_vecs < vmdq_vecs_wanted)
  6934. pf->num_vmdq_qps = 1;
  6935. pf->num_vmdq_msix = pf->num_vmdq_qps;
  6936. v_budget += vmdq_vecs;
  6937. vectors_left -= vmdq_vecs;
  6938. }
  6939. }
  6940. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  6941. GFP_KERNEL);
  6942. if (!pf->msix_entries)
  6943. return -ENOMEM;
  6944. for (i = 0; i < v_budget; i++)
  6945. pf->msix_entries[i].entry = i;
  6946. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  6947. if (v_actual < I40E_MIN_MSIX) {
  6948. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  6949. kfree(pf->msix_entries);
  6950. pf->msix_entries = NULL;
  6951. pci_disable_msix(pf->pdev);
  6952. return -ENODEV;
  6953. } else if (v_actual == I40E_MIN_MSIX) {
  6954. /* Adjust for minimal MSIX use */
  6955. pf->num_vmdq_vsis = 0;
  6956. pf->num_vmdq_qps = 0;
  6957. pf->num_lan_qps = 1;
  6958. pf->num_lan_msix = 1;
  6959. } else if (!vectors_left) {
  6960. /* If we have limited resources, we will start with no vectors
  6961. * for the special features and then allocate vectors to some
  6962. * of these features based on the policy and at the end disable
  6963. * the features that did not get any vectors.
  6964. */
  6965. int vec;
  6966. dev_info(&pf->pdev->dev,
  6967. "MSI-X vector limit reached, attempting to redistribute vectors\n");
  6968. /* reserve the misc vector */
  6969. vec = v_actual - 1;
  6970. /* Scale vector usage down */
  6971. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  6972. pf->num_vmdq_vsis = 1;
  6973. pf->num_vmdq_qps = 1;
  6974. #ifdef I40E_FCOE
  6975. pf->num_fcoe_qps = 0;
  6976. pf->num_fcoe_msix = 0;
  6977. #endif
  6978. /* partition out the remaining vectors */
  6979. switch (vec) {
  6980. case 2:
  6981. pf->num_lan_msix = 1;
  6982. break;
  6983. case 3:
  6984. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6985. pf->num_lan_msix = 1;
  6986. pf->num_iwarp_msix = 1;
  6987. } else {
  6988. pf->num_lan_msix = 2;
  6989. }
  6990. #ifdef I40E_FCOE
  6991. /* give one vector to FCoE */
  6992. if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
  6993. pf->num_lan_msix = 1;
  6994. pf->num_fcoe_msix = 1;
  6995. }
  6996. #endif
  6997. break;
  6998. default:
  6999. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  7000. pf->num_iwarp_msix = min_t(int, (vec / 3),
  7001. iwarp_requested);
  7002. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  7003. I40E_DEFAULT_NUM_VMDQ_VSI);
  7004. } else {
  7005. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  7006. I40E_DEFAULT_NUM_VMDQ_VSI);
  7007. }
  7008. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7009. pf->num_fdsb_msix = 1;
  7010. vec--;
  7011. }
  7012. pf->num_lan_msix = min_t(int,
  7013. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  7014. pf->num_lan_msix);
  7015. pf->num_lan_qps = pf->num_lan_msix;
  7016. #ifdef I40E_FCOE
  7017. /* give one vector to FCoE */
  7018. if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
  7019. pf->num_fcoe_msix = 1;
  7020. vec--;
  7021. }
  7022. #endif
  7023. break;
  7024. }
  7025. }
  7026. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  7027. (pf->num_fdsb_msix == 0)) {
  7028. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  7029. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7030. }
  7031. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  7032. (pf->num_vmdq_msix == 0)) {
  7033. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  7034. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  7035. }
  7036. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  7037. (pf->num_iwarp_msix == 0)) {
  7038. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  7039. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  7040. }
  7041. #ifdef I40E_FCOE
  7042. if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
  7043. dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
  7044. pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
  7045. }
  7046. #endif
  7047. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  7048. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  7049. pf->num_lan_msix,
  7050. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  7051. pf->num_fdsb_msix,
  7052. pf->num_iwarp_msix);
  7053. return v_actual;
  7054. }
  7055. /**
  7056. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  7057. * @vsi: the VSI being configured
  7058. * @v_idx: index of the vector in the vsi struct
  7059. * @cpu: cpu to be used on affinity_mask
  7060. *
  7061. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  7062. **/
  7063. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  7064. {
  7065. struct i40e_q_vector *q_vector;
  7066. /* allocate q_vector */
  7067. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  7068. if (!q_vector)
  7069. return -ENOMEM;
  7070. q_vector->vsi = vsi;
  7071. q_vector->v_idx = v_idx;
  7072. cpumask_set_cpu(cpu, &q_vector->affinity_mask);
  7073. if (vsi->netdev)
  7074. netif_napi_add(vsi->netdev, &q_vector->napi,
  7075. i40e_napi_poll, NAPI_POLL_WEIGHT);
  7076. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  7077. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  7078. /* tie q_vector and vsi together */
  7079. vsi->q_vectors[v_idx] = q_vector;
  7080. return 0;
  7081. }
  7082. /**
  7083. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  7084. * @vsi: the VSI being configured
  7085. *
  7086. * We allocate one q_vector per queue interrupt. If allocation fails we
  7087. * return -ENOMEM.
  7088. **/
  7089. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  7090. {
  7091. struct i40e_pf *pf = vsi->back;
  7092. int err, v_idx, num_q_vectors, current_cpu;
  7093. /* if not MSIX, give the one vector only to the LAN VSI */
  7094. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  7095. num_q_vectors = vsi->num_q_vectors;
  7096. else if (vsi == pf->vsi[pf->lan_vsi])
  7097. num_q_vectors = 1;
  7098. else
  7099. return -EINVAL;
  7100. current_cpu = cpumask_first(cpu_online_mask);
  7101. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  7102. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  7103. if (err)
  7104. goto err_out;
  7105. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  7106. if (unlikely(current_cpu >= nr_cpu_ids))
  7107. current_cpu = cpumask_first(cpu_online_mask);
  7108. }
  7109. return 0;
  7110. err_out:
  7111. while (v_idx--)
  7112. i40e_free_q_vector(vsi, v_idx);
  7113. return err;
  7114. }
  7115. /**
  7116. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  7117. * @pf: board private structure to initialize
  7118. **/
  7119. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  7120. {
  7121. int vectors = 0;
  7122. ssize_t size;
  7123. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  7124. vectors = i40e_init_msix(pf);
  7125. if (vectors < 0) {
  7126. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  7127. I40E_FLAG_IWARP_ENABLED |
  7128. #ifdef I40E_FCOE
  7129. I40E_FLAG_FCOE_ENABLED |
  7130. #endif
  7131. I40E_FLAG_RSS_ENABLED |
  7132. I40E_FLAG_DCB_CAPABLE |
  7133. I40E_FLAG_DCB_ENABLED |
  7134. I40E_FLAG_SRIOV_ENABLED |
  7135. I40E_FLAG_FD_SB_ENABLED |
  7136. I40E_FLAG_FD_ATR_ENABLED |
  7137. I40E_FLAG_VMDQ_ENABLED);
  7138. /* rework the queue expectations without MSIX */
  7139. i40e_determine_queue_usage(pf);
  7140. }
  7141. }
  7142. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  7143. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  7144. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  7145. vectors = pci_enable_msi(pf->pdev);
  7146. if (vectors < 0) {
  7147. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  7148. vectors);
  7149. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  7150. }
  7151. vectors = 1; /* one MSI or Legacy vector */
  7152. }
  7153. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  7154. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  7155. /* set up vector assignment tracking */
  7156. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  7157. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  7158. if (!pf->irq_pile) {
  7159. dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
  7160. return -ENOMEM;
  7161. }
  7162. pf->irq_pile->num_entries = vectors;
  7163. pf->irq_pile->search_hint = 0;
  7164. /* track first vector for misc interrupts, ignore return */
  7165. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  7166. return 0;
  7167. }
  7168. /**
  7169. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  7170. * @pf: board private structure
  7171. *
  7172. * This sets up the handler for MSIX 0, which is used to manage the
  7173. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  7174. * when in MSI or Legacy interrupt mode.
  7175. **/
  7176. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  7177. {
  7178. struct i40e_hw *hw = &pf->hw;
  7179. int err = 0;
  7180. /* Only request the irq if this is the first time through, and
  7181. * not when we're rebuilding after a Reset
  7182. */
  7183. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  7184. err = request_irq(pf->msix_entries[0].vector,
  7185. i40e_intr, 0, pf->int_name, pf);
  7186. if (err) {
  7187. dev_info(&pf->pdev->dev,
  7188. "request_irq for %s failed: %d\n",
  7189. pf->int_name, err);
  7190. return -EFAULT;
  7191. }
  7192. }
  7193. i40e_enable_misc_int_causes(pf);
  7194. /* associate no queues to the misc vector */
  7195. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  7196. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  7197. i40e_flush(hw);
  7198. i40e_irq_dynamic_enable_icr0(pf, true);
  7199. return err;
  7200. }
  7201. /**
  7202. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  7203. * @vsi: vsi structure
  7204. * @seed: RSS hash seed
  7205. **/
  7206. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7207. u8 *lut, u16 lut_size)
  7208. {
  7209. struct i40e_pf *pf = vsi->back;
  7210. struct i40e_hw *hw = &pf->hw;
  7211. int ret = 0;
  7212. if (seed) {
  7213. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  7214. (struct i40e_aqc_get_set_rss_key_data *)seed;
  7215. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  7216. if (ret) {
  7217. dev_info(&pf->pdev->dev,
  7218. "Cannot set RSS key, err %s aq_err %s\n",
  7219. i40e_stat_str(hw, ret),
  7220. i40e_aq_str(hw, hw->aq.asq_last_status));
  7221. return ret;
  7222. }
  7223. }
  7224. if (lut) {
  7225. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7226. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7227. if (ret) {
  7228. dev_info(&pf->pdev->dev,
  7229. "Cannot set RSS lut, err %s aq_err %s\n",
  7230. i40e_stat_str(hw, ret),
  7231. i40e_aq_str(hw, hw->aq.asq_last_status));
  7232. return ret;
  7233. }
  7234. }
  7235. return ret;
  7236. }
  7237. /**
  7238. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  7239. * @vsi: Pointer to vsi structure
  7240. * @seed: Buffter to store the hash keys
  7241. * @lut: Buffer to store the lookup table entries
  7242. * @lut_size: Size of buffer to store the lookup table entries
  7243. *
  7244. * Return 0 on success, negative on failure
  7245. */
  7246. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7247. u8 *lut, u16 lut_size)
  7248. {
  7249. struct i40e_pf *pf = vsi->back;
  7250. struct i40e_hw *hw = &pf->hw;
  7251. int ret = 0;
  7252. if (seed) {
  7253. ret = i40e_aq_get_rss_key(hw, vsi->id,
  7254. (struct i40e_aqc_get_set_rss_key_data *)seed);
  7255. if (ret) {
  7256. dev_info(&pf->pdev->dev,
  7257. "Cannot get RSS key, err %s aq_err %s\n",
  7258. i40e_stat_str(&pf->hw, ret),
  7259. i40e_aq_str(&pf->hw,
  7260. pf->hw.aq.asq_last_status));
  7261. return ret;
  7262. }
  7263. }
  7264. if (lut) {
  7265. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7266. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7267. if (ret) {
  7268. dev_info(&pf->pdev->dev,
  7269. "Cannot get RSS lut, err %s aq_err %s\n",
  7270. i40e_stat_str(&pf->hw, ret),
  7271. i40e_aq_str(&pf->hw,
  7272. pf->hw.aq.asq_last_status));
  7273. return ret;
  7274. }
  7275. }
  7276. return ret;
  7277. }
  7278. /**
  7279. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  7280. * @vsi: VSI structure
  7281. **/
  7282. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  7283. {
  7284. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7285. struct i40e_pf *pf = vsi->back;
  7286. u8 *lut;
  7287. int ret;
  7288. if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
  7289. return 0;
  7290. if (!vsi->rss_size)
  7291. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7292. vsi->num_queue_pairs);
  7293. if (!vsi->rss_size)
  7294. return -EINVAL;
  7295. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7296. if (!lut)
  7297. return -ENOMEM;
  7298. /* Use the user configured hash keys and lookup table if there is one,
  7299. * otherwise use default
  7300. */
  7301. if (vsi->rss_lut_user)
  7302. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7303. else
  7304. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7305. if (vsi->rss_hkey_user)
  7306. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7307. else
  7308. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7309. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  7310. kfree(lut);
  7311. return ret;
  7312. }
  7313. /**
  7314. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  7315. * @vsi: Pointer to vsi structure
  7316. * @seed: RSS hash seed
  7317. * @lut: Lookup table
  7318. * @lut_size: Lookup table size
  7319. *
  7320. * Returns 0 on success, negative on failure
  7321. **/
  7322. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  7323. const u8 *lut, u16 lut_size)
  7324. {
  7325. struct i40e_pf *pf = vsi->back;
  7326. struct i40e_hw *hw = &pf->hw;
  7327. u16 vf_id = vsi->vf_id;
  7328. u8 i;
  7329. /* Fill out hash function seed */
  7330. if (seed) {
  7331. u32 *seed_dw = (u32 *)seed;
  7332. if (vsi->type == I40E_VSI_MAIN) {
  7333. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7334. i40e_write_rx_ctl(hw, I40E_PFQF_HKEY(i),
  7335. seed_dw[i]);
  7336. } else if (vsi->type == I40E_VSI_SRIOV) {
  7337. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  7338. i40e_write_rx_ctl(hw,
  7339. I40E_VFQF_HKEY1(i, vf_id),
  7340. seed_dw[i]);
  7341. } else {
  7342. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  7343. }
  7344. }
  7345. if (lut) {
  7346. u32 *lut_dw = (u32 *)lut;
  7347. if (vsi->type == I40E_VSI_MAIN) {
  7348. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7349. return -EINVAL;
  7350. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7351. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  7352. } else if (vsi->type == I40E_VSI_SRIOV) {
  7353. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  7354. return -EINVAL;
  7355. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7356. i40e_write_rx_ctl(hw,
  7357. I40E_VFQF_HLUT1(i, vf_id),
  7358. lut_dw[i]);
  7359. } else {
  7360. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7361. }
  7362. }
  7363. i40e_flush(hw);
  7364. return 0;
  7365. }
  7366. /**
  7367. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  7368. * @vsi: Pointer to VSI structure
  7369. * @seed: Buffer to store the keys
  7370. * @lut: Buffer to store the lookup table entries
  7371. * @lut_size: Size of buffer to store the lookup table entries
  7372. *
  7373. * Returns 0 on success, negative on failure
  7374. */
  7375. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  7376. u8 *lut, u16 lut_size)
  7377. {
  7378. struct i40e_pf *pf = vsi->back;
  7379. struct i40e_hw *hw = &pf->hw;
  7380. u16 i;
  7381. if (seed) {
  7382. u32 *seed_dw = (u32 *)seed;
  7383. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7384. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  7385. }
  7386. if (lut) {
  7387. u32 *lut_dw = (u32 *)lut;
  7388. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7389. return -EINVAL;
  7390. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7391. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  7392. }
  7393. return 0;
  7394. }
  7395. /**
  7396. * i40e_config_rss - Configure RSS keys and lut
  7397. * @vsi: Pointer to VSI structure
  7398. * @seed: RSS hash seed
  7399. * @lut: Lookup table
  7400. * @lut_size: Lookup table size
  7401. *
  7402. * Returns 0 on success, negative on failure
  7403. */
  7404. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7405. {
  7406. struct i40e_pf *pf = vsi->back;
  7407. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7408. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  7409. else
  7410. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  7411. }
  7412. /**
  7413. * i40e_get_rss - Get RSS keys and lut
  7414. * @vsi: Pointer to VSI structure
  7415. * @seed: Buffer to store the keys
  7416. * @lut: Buffer to store the lookup table entries
  7417. * lut_size: Size of buffer to store the lookup table entries
  7418. *
  7419. * Returns 0 on success, negative on failure
  7420. */
  7421. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7422. {
  7423. struct i40e_pf *pf = vsi->back;
  7424. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7425. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  7426. else
  7427. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  7428. }
  7429. /**
  7430. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  7431. * @pf: Pointer to board private structure
  7432. * @lut: Lookup table
  7433. * @rss_table_size: Lookup table size
  7434. * @rss_size: Range of queue number for hashing
  7435. */
  7436. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  7437. u16 rss_table_size, u16 rss_size)
  7438. {
  7439. u16 i;
  7440. for (i = 0; i < rss_table_size; i++)
  7441. lut[i] = i % rss_size;
  7442. }
  7443. /**
  7444. * i40e_pf_config_rss - Prepare for RSS if used
  7445. * @pf: board private structure
  7446. **/
  7447. static int i40e_pf_config_rss(struct i40e_pf *pf)
  7448. {
  7449. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7450. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7451. u8 *lut;
  7452. struct i40e_hw *hw = &pf->hw;
  7453. u32 reg_val;
  7454. u64 hena;
  7455. int ret;
  7456. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  7457. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  7458. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  7459. hena |= i40e_pf_get_default_rss_hena(pf);
  7460. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  7461. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  7462. /* Determine the RSS table size based on the hardware capabilities */
  7463. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  7464. reg_val = (pf->rss_table_size == 512) ?
  7465. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  7466. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  7467. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  7468. /* Determine the RSS size of the VSI */
  7469. if (!vsi->rss_size)
  7470. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7471. vsi->num_queue_pairs);
  7472. if (!vsi->rss_size)
  7473. return -EINVAL;
  7474. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7475. if (!lut)
  7476. return -ENOMEM;
  7477. /* Use user configured lut if there is one, otherwise use default */
  7478. if (vsi->rss_lut_user)
  7479. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7480. else
  7481. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7482. /* Use user configured hash key if there is one, otherwise
  7483. * use default.
  7484. */
  7485. if (vsi->rss_hkey_user)
  7486. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7487. else
  7488. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7489. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  7490. kfree(lut);
  7491. return ret;
  7492. }
  7493. /**
  7494. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  7495. * @pf: board private structure
  7496. * @queue_count: the requested queue count for rss.
  7497. *
  7498. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  7499. * count which may be different from the requested queue count.
  7500. **/
  7501. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  7502. {
  7503. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7504. int new_rss_size;
  7505. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  7506. return 0;
  7507. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  7508. if (queue_count != vsi->num_queue_pairs) {
  7509. vsi->req_queue_pairs = queue_count;
  7510. i40e_prep_for_reset(pf);
  7511. pf->alloc_rss_size = new_rss_size;
  7512. i40e_reset_and_rebuild(pf, true);
  7513. /* Discard the user configured hash keys and lut, if less
  7514. * queues are enabled.
  7515. */
  7516. if (queue_count < vsi->rss_size) {
  7517. i40e_clear_rss_config_user(vsi);
  7518. dev_dbg(&pf->pdev->dev,
  7519. "discard user configured hash keys and lut\n");
  7520. }
  7521. /* Reset vsi->rss_size, as number of enabled queues changed */
  7522. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7523. vsi->num_queue_pairs);
  7524. i40e_pf_config_rss(pf);
  7525. }
  7526. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  7527. vsi->req_queue_pairs, pf->rss_size_max);
  7528. return pf->alloc_rss_size;
  7529. }
  7530. /**
  7531. * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
  7532. * @pf: board private structure
  7533. **/
  7534. i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
  7535. {
  7536. i40e_status status;
  7537. bool min_valid, max_valid;
  7538. u32 max_bw, min_bw;
  7539. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  7540. &min_valid, &max_valid);
  7541. if (!status) {
  7542. if (min_valid)
  7543. pf->npar_min_bw = min_bw;
  7544. if (max_valid)
  7545. pf->npar_max_bw = max_bw;
  7546. }
  7547. return status;
  7548. }
  7549. /**
  7550. * i40e_set_npar_bw_setting - Set BW settings for this PF partition
  7551. * @pf: board private structure
  7552. **/
  7553. i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
  7554. {
  7555. struct i40e_aqc_configure_partition_bw_data bw_data;
  7556. i40e_status status;
  7557. /* Set the valid bit for this PF */
  7558. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  7559. bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
  7560. bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
  7561. /* Set the new bandwidths */
  7562. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  7563. return status;
  7564. }
  7565. /**
  7566. * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
  7567. * @pf: board private structure
  7568. **/
  7569. i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
  7570. {
  7571. /* Commit temporary BW setting to permanent NVM image */
  7572. enum i40e_admin_queue_err last_aq_status;
  7573. i40e_status ret;
  7574. u16 nvm_word;
  7575. if (pf->hw.partition_id != 1) {
  7576. dev_info(&pf->pdev->dev,
  7577. "Commit BW only works on partition 1! This is partition %d",
  7578. pf->hw.partition_id);
  7579. ret = I40E_NOT_SUPPORTED;
  7580. goto bw_commit_out;
  7581. }
  7582. /* Acquire NVM for read access */
  7583. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  7584. last_aq_status = pf->hw.aq.asq_last_status;
  7585. if (ret) {
  7586. dev_info(&pf->pdev->dev,
  7587. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  7588. i40e_stat_str(&pf->hw, ret),
  7589. i40e_aq_str(&pf->hw, last_aq_status));
  7590. goto bw_commit_out;
  7591. }
  7592. /* Read word 0x10 of NVM - SW compatibility word 1 */
  7593. ret = i40e_aq_read_nvm(&pf->hw,
  7594. I40E_SR_NVM_CONTROL_WORD,
  7595. 0x10, sizeof(nvm_word), &nvm_word,
  7596. false, NULL);
  7597. /* Save off last admin queue command status before releasing
  7598. * the NVM
  7599. */
  7600. last_aq_status = pf->hw.aq.asq_last_status;
  7601. i40e_release_nvm(&pf->hw);
  7602. if (ret) {
  7603. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  7604. i40e_stat_str(&pf->hw, ret),
  7605. i40e_aq_str(&pf->hw, last_aq_status));
  7606. goto bw_commit_out;
  7607. }
  7608. /* Wait a bit for NVM release to complete */
  7609. msleep(50);
  7610. /* Acquire NVM for write access */
  7611. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  7612. last_aq_status = pf->hw.aq.asq_last_status;
  7613. if (ret) {
  7614. dev_info(&pf->pdev->dev,
  7615. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  7616. i40e_stat_str(&pf->hw, ret),
  7617. i40e_aq_str(&pf->hw, last_aq_status));
  7618. goto bw_commit_out;
  7619. }
  7620. /* Write it back out unchanged to initiate update NVM,
  7621. * which will force a write of the shadow (alt) RAM to
  7622. * the NVM - thus storing the bandwidth values permanently.
  7623. */
  7624. ret = i40e_aq_update_nvm(&pf->hw,
  7625. I40E_SR_NVM_CONTROL_WORD,
  7626. 0x10, sizeof(nvm_word),
  7627. &nvm_word, true, NULL);
  7628. /* Save off last admin queue command status before releasing
  7629. * the NVM
  7630. */
  7631. last_aq_status = pf->hw.aq.asq_last_status;
  7632. i40e_release_nvm(&pf->hw);
  7633. if (ret)
  7634. dev_info(&pf->pdev->dev,
  7635. "BW settings NOT SAVED, err %s aq_err %s\n",
  7636. i40e_stat_str(&pf->hw, ret),
  7637. i40e_aq_str(&pf->hw, last_aq_status));
  7638. bw_commit_out:
  7639. return ret;
  7640. }
  7641. /**
  7642. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  7643. * @pf: board private structure to initialize
  7644. *
  7645. * i40e_sw_init initializes the Adapter private data structure.
  7646. * Fields are initialized based on PCI device information and
  7647. * OS network device settings (MTU size).
  7648. **/
  7649. static int i40e_sw_init(struct i40e_pf *pf)
  7650. {
  7651. int err = 0;
  7652. int size;
  7653. /* Set default capability flags */
  7654. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  7655. I40E_FLAG_MSI_ENABLED |
  7656. I40E_FLAG_MSIX_ENABLED;
  7657. /* Set default ITR */
  7658. pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
  7659. pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
  7660. /* Depending on PF configurations, it is possible that the RSS
  7661. * maximum might end up larger than the available queues
  7662. */
  7663. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  7664. pf->alloc_rss_size = 1;
  7665. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  7666. pf->rss_size_max = min_t(int, pf->rss_size_max,
  7667. pf->hw.func_caps.num_tx_qp);
  7668. if (pf->hw.func_caps.rss) {
  7669. pf->flags |= I40E_FLAG_RSS_ENABLED;
  7670. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  7671. num_online_cpus());
  7672. }
  7673. /* MFP mode enabled */
  7674. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  7675. pf->flags |= I40E_FLAG_MFP_ENABLED;
  7676. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  7677. if (i40e_get_npar_bw_setting(pf))
  7678. dev_warn(&pf->pdev->dev,
  7679. "Could not get NPAR bw settings\n");
  7680. else
  7681. dev_info(&pf->pdev->dev,
  7682. "Min BW = %8.8x, Max BW = %8.8x\n",
  7683. pf->npar_min_bw, pf->npar_max_bw);
  7684. }
  7685. /* FW/NVM is not yet fixed in this regard */
  7686. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  7687. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  7688. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  7689. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  7690. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  7691. pf->hw.num_partitions > 1)
  7692. dev_info(&pf->pdev->dev,
  7693. "Flow Director Sideband mode Disabled in MFP mode\n");
  7694. else
  7695. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7696. pf->fdir_pf_filter_count =
  7697. pf->hw.func_caps.fd_filters_guaranteed;
  7698. pf->hw.fdir_shared_filter_count =
  7699. pf->hw.func_caps.fd_filters_best_effort;
  7700. }
  7701. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7702. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  7703. (pf->hw.aq.fw_maj_ver < 4))) {
  7704. pf->flags |= I40E_FLAG_RESTART_AUTONEG;
  7705. /* No DCB support for FW < v4.33 */
  7706. pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
  7707. }
  7708. /* Disable FW LLDP if FW < v4.3 */
  7709. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7710. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  7711. (pf->hw.aq.fw_maj_ver < 4)))
  7712. pf->flags |= I40E_FLAG_STOP_FW_LLDP;
  7713. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  7714. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7715. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  7716. (pf->hw.aq.fw_maj_ver >= 5)))
  7717. pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
  7718. if (pf->hw.func_caps.vmdq) {
  7719. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  7720. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  7721. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  7722. }
  7723. if (pf->hw.func_caps.iwarp) {
  7724. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  7725. /* IWARP needs one extra vector for CQP just like MISC.*/
  7726. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  7727. }
  7728. #ifdef I40E_FCOE
  7729. i40e_init_pf_fcoe(pf);
  7730. #endif /* I40E_FCOE */
  7731. #ifdef CONFIG_PCI_IOV
  7732. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  7733. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  7734. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  7735. pf->num_req_vfs = min_t(int,
  7736. pf->hw.func_caps.num_vfs,
  7737. I40E_MAX_VF_COUNT);
  7738. }
  7739. #endif /* CONFIG_PCI_IOV */
  7740. if (pf->hw.mac.type == I40E_MAC_X722) {
  7741. pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
  7742. | I40E_FLAG_128_QP_RSS_CAPABLE
  7743. | I40E_FLAG_HW_ATR_EVICT_CAPABLE
  7744. | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
  7745. | I40E_FLAG_WB_ON_ITR_CAPABLE
  7746. | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
  7747. | I40E_FLAG_NO_PCI_LINK_CHECK
  7748. | I40E_FLAG_USE_SET_LLDP_MIB
  7749. | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
  7750. | I40E_FLAG_PTP_L4_CAPABLE
  7751. | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
  7752. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  7753. ((pf->hw.aq.api_maj_ver == 1) &&
  7754. (pf->hw.aq.api_min_ver > 4))) {
  7755. /* Supported in FW API version higher than 1.4 */
  7756. pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
  7757. pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7758. } else {
  7759. pf->auto_disable_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7760. }
  7761. pf->eeprom_version = 0xDEAD;
  7762. pf->lan_veb = I40E_NO_VEB;
  7763. pf->lan_vsi = I40E_NO_VSI;
  7764. /* By default FW has this off for performance reasons */
  7765. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  7766. /* set up queue assignment tracking */
  7767. size = sizeof(struct i40e_lump_tracking)
  7768. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  7769. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  7770. if (!pf->qp_pile) {
  7771. err = -ENOMEM;
  7772. goto sw_init_done;
  7773. }
  7774. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  7775. pf->qp_pile->search_hint = 0;
  7776. pf->tx_timeout_recovery_level = 1;
  7777. mutex_init(&pf->switch_mutex);
  7778. /* If NPAR is enabled nudge the Tx scheduler */
  7779. if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
  7780. i40e_set_npar_bw_setting(pf);
  7781. sw_init_done:
  7782. return err;
  7783. }
  7784. /**
  7785. * i40e_set_ntuple - set the ntuple feature flag and take action
  7786. * @pf: board private structure to initialize
  7787. * @features: the feature set that the stack is suggesting
  7788. *
  7789. * returns a bool to indicate if reset needs to happen
  7790. **/
  7791. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  7792. {
  7793. bool need_reset = false;
  7794. /* Check if Flow Director n-tuple support was enabled or disabled. If
  7795. * the state changed, we need to reset.
  7796. */
  7797. if (features & NETIF_F_NTUPLE) {
  7798. /* Enable filters and mark for reset */
  7799. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7800. need_reset = true;
  7801. /* enable FD_SB only if there is MSI-X vector */
  7802. if (pf->num_fdsb_msix > 0)
  7803. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7804. } else {
  7805. /* turn off filters, mark for reset and clear SW filter list */
  7806. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7807. need_reset = true;
  7808. i40e_fdir_filter_exit(pf);
  7809. }
  7810. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7811. pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7812. /* reset fd counters */
  7813. pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
  7814. pf->fdir_pf_active_filters = 0;
  7815. /* if ATR was auto disabled it can be re-enabled. */
  7816. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7817. (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
  7818. pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  7819. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7820. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  7821. }
  7822. }
  7823. return need_reset;
  7824. }
  7825. /**
  7826. * i40e_clear_rss_lut - clear the rx hash lookup table
  7827. * @vsi: the VSI being configured
  7828. **/
  7829. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  7830. {
  7831. struct i40e_pf *pf = vsi->back;
  7832. struct i40e_hw *hw = &pf->hw;
  7833. u16 vf_id = vsi->vf_id;
  7834. u8 i;
  7835. if (vsi->type == I40E_VSI_MAIN) {
  7836. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7837. wr32(hw, I40E_PFQF_HLUT(i), 0);
  7838. } else if (vsi->type == I40E_VSI_SRIOV) {
  7839. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7840. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  7841. } else {
  7842. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7843. }
  7844. }
  7845. /**
  7846. * i40e_set_features - set the netdev feature flags
  7847. * @netdev: ptr to the netdev being adjusted
  7848. * @features: the feature set that the stack is suggesting
  7849. **/
  7850. static int i40e_set_features(struct net_device *netdev,
  7851. netdev_features_t features)
  7852. {
  7853. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7854. struct i40e_vsi *vsi = np->vsi;
  7855. struct i40e_pf *pf = vsi->back;
  7856. bool need_reset;
  7857. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  7858. i40e_pf_config_rss(pf);
  7859. else if (!(features & NETIF_F_RXHASH) &&
  7860. netdev->features & NETIF_F_RXHASH)
  7861. i40e_clear_rss_lut(vsi);
  7862. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  7863. i40e_vlan_stripping_enable(vsi);
  7864. else
  7865. i40e_vlan_stripping_disable(vsi);
  7866. need_reset = i40e_set_ntuple(pf, features);
  7867. if (need_reset)
  7868. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  7869. return 0;
  7870. }
  7871. /**
  7872. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  7873. * @pf: board private structure
  7874. * @port: The UDP port to look up
  7875. *
  7876. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  7877. **/
  7878. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, __be16 port)
  7879. {
  7880. u8 i;
  7881. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  7882. if (pf->udp_ports[i].index == port)
  7883. return i;
  7884. }
  7885. return i;
  7886. }
  7887. /**
  7888. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  7889. * @netdev: This physical port's netdev
  7890. * @ti: Tunnel endpoint information
  7891. **/
  7892. static void i40e_udp_tunnel_add(struct net_device *netdev,
  7893. struct udp_tunnel_info *ti)
  7894. {
  7895. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7896. struct i40e_vsi *vsi = np->vsi;
  7897. struct i40e_pf *pf = vsi->back;
  7898. __be16 port = ti->port;
  7899. u8 next_idx;
  7900. u8 idx;
  7901. idx = i40e_get_udp_port_idx(pf, port);
  7902. /* Check if port already exists */
  7903. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7904. netdev_info(netdev, "port %d already offloaded\n",
  7905. ntohs(port));
  7906. return;
  7907. }
  7908. /* Now check if there is space to add the new port */
  7909. next_idx = i40e_get_udp_port_idx(pf, 0);
  7910. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7911. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  7912. ntohs(port));
  7913. return;
  7914. }
  7915. switch (ti->type) {
  7916. case UDP_TUNNEL_TYPE_VXLAN:
  7917. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  7918. break;
  7919. case UDP_TUNNEL_TYPE_GENEVE:
  7920. if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
  7921. return;
  7922. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  7923. break;
  7924. default:
  7925. return;
  7926. }
  7927. /* New port: add it and mark its index in the bitmap */
  7928. pf->udp_ports[next_idx].index = port;
  7929. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  7930. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7931. }
  7932. /**
  7933. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  7934. * @netdev: This physical port's netdev
  7935. * @ti: Tunnel endpoint information
  7936. **/
  7937. static void i40e_udp_tunnel_del(struct net_device *netdev,
  7938. struct udp_tunnel_info *ti)
  7939. {
  7940. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7941. struct i40e_vsi *vsi = np->vsi;
  7942. struct i40e_pf *pf = vsi->back;
  7943. __be16 port = ti->port;
  7944. u8 idx;
  7945. idx = i40e_get_udp_port_idx(pf, port);
  7946. /* Check if port already exists */
  7947. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  7948. goto not_found;
  7949. switch (ti->type) {
  7950. case UDP_TUNNEL_TYPE_VXLAN:
  7951. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  7952. goto not_found;
  7953. break;
  7954. case UDP_TUNNEL_TYPE_GENEVE:
  7955. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  7956. goto not_found;
  7957. break;
  7958. default:
  7959. goto not_found;
  7960. }
  7961. /* if port exists, set it to 0 (mark for deletion)
  7962. * and make it pending
  7963. */
  7964. pf->udp_ports[idx].index = 0;
  7965. pf->pending_udp_bitmap |= BIT_ULL(idx);
  7966. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7967. return;
  7968. not_found:
  7969. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  7970. ntohs(port));
  7971. }
  7972. static int i40e_get_phys_port_id(struct net_device *netdev,
  7973. struct netdev_phys_item_id *ppid)
  7974. {
  7975. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7976. struct i40e_pf *pf = np->vsi->back;
  7977. struct i40e_hw *hw = &pf->hw;
  7978. if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
  7979. return -EOPNOTSUPP;
  7980. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  7981. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  7982. return 0;
  7983. }
  7984. /**
  7985. * i40e_ndo_fdb_add - add an entry to the hardware database
  7986. * @ndm: the input from the stack
  7987. * @tb: pointer to array of nladdr (unused)
  7988. * @dev: the net device pointer
  7989. * @addr: the MAC address entry being added
  7990. * @flags: instructions from stack about fdb operation
  7991. */
  7992. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  7993. struct net_device *dev,
  7994. const unsigned char *addr, u16 vid,
  7995. u16 flags)
  7996. {
  7997. struct i40e_netdev_priv *np = netdev_priv(dev);
  7998. struct i40e_pf *pf = np->vsi->back;
  7999. int err = 0;
  8000. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  8001. return -EOPNOTSUPP;
  8002. if (vid) {
  8003. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  8004. return -EINVAL;
  8005. }
  8006. /* Hardware does not support aging addresses so if a
  8007. * ndm_state is given only allow permanent addresses
  8008. */
  8009. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  8010. netdev_info(dev, "FDB only supports static addresses\n");
  8011. return -EINVAL;
  8012. }
  8013. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  8014. err = dev_uc_add_excl(dev, addr);
  8015. else if (is_multicast_ether_addr(addr))
  8016. err = dev_mc_add_excl(dev, addr);
  8017. else
  8018. err = -EINVAL;
  8019. /* Only return duplicate errors if NLM_F_EXCL is set */
  8020. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  8021. err = 0;
  8022. return err;
  8023. }
  8024. /**
  8025. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  8026. * @dev: the netdev being configured
  8027. * @nlh: RTNL message
  8028. *
  8029. * Inserts a new hardware bridge if not already created and
  8030. * enables the bridging mode requested (VEB or VEPA). If the
  8031. * hardware bridge has already been inserted and the request
  8032. * is to change the mode then that requires a PF reset to
  8033. * allow rebuild of the components with required hardware
  8034. * bridge mode enabled.
  8035. **/
  8036. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  8037. struct nlmsghdr *nlh,
  8038. u16 flags)
  8039. {
  8040. struct i40e_netdev_priv *np = netdev_priv(dev);
  8041. struct i40e_vsi *vsi = np->vsi;
  8042. struct i40e_pf *pf = vsi->back;
  8043. struct i40e_veb *veb = NULL;
  8044. struct nlattr *attr, *br_spec;
  8045. int i, rem;
  8046. /* Only for PF VSI for now */
  8047. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  8048. return -EOPNOTSUPP;
  8049. /* Find the HW bridge for PF VSI */
  8050. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8051. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8052. veb = pf->veb[i];
  8053. }
  8054. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  8055. nla_for_each_nested(attr, br_spec, rem) {
  8056. __u16 mode;
  8057. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  8058. continue;
  8059. mode = nla_get_u16(attr);
  8060. if ((mode != BRIDGE_MODE_VEPA) &&
  8061. (mode != BRIDGE_MODE_VEB))
  8062. return -EINVAL;
  8063. /* Insert a new HW bridge */
  8064. if (!veb) {
  8065. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8066. vsi->tc_config.enabled_tc);
  8067. if (veb) {
  8068. veb->bridge_mode = mode;
  8069. i40e_config_bridge_mode(veb);
  8070. } else {
  8071. /* No Bridge HW offload available */
  8072. return -ENOENT;
  8073. }
  8074. break;
  8075. } else if (mode != veb->bridge_mode) {
  8076. /* Existing HW bridge but different mode needs reset */
  8077. veb->bridge_mode = mode;
  8078. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  8079. if (mode == BRIDGE_MODE_VEB)
  8080. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  8081. else
  8082. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8083. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  8084. break;
  8085. }
  8086. }
  8087. return 0;
  8088. }
  8089. /**
  8090. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  8091. * @skb: skb buff
  8092. * @pid: process id
  8093. * @seq: RTNL message seq #
  8094. * @dev: the netdev being configured
  8095. * @filter_mask: unused
  8096. * @nlflags: netlink flags passed in
  8097. *
  8098. * Return the mode in which the hardware bridge is operating in
  8099. * i.e VEB or VEPA.
  8100. **/
  8101. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  8102. struct net_device *dev,
  8103. u32 __always_unused filter_mask,
  8104. int nlflags)
  8105. {
  8106. struct i40e_netdev_priv *np = netdev_priv(dev);
  8107. struct i40e_vsi *vsi = np->vsi;
  8108. struct i40e_pf *pf = vsi->back;
  8109. struct i40e_veb *veb = NULL;
  8110. int i;
  8111. /* Only for PF VSI for now */
  8112. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  8113. return -EOPNOTSUPP;
  8114. /* Find the HW bridge for the PF VSI */
  8115. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8116. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8117. veb = pf->veb[i];
  8118. }
  8119. if (!veb)
  8120. return 0;
  8121. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  8122. 0, 0, nlflags, filter_mask, NULL);
  8123. }
  8124. /**
  8125. * i40e_features_check - Validate encapsulated packet conforms to limits
  8126. * @skb: skb buff
  8127. * @dev: This physical port's netdev
  8128. * @features: Offload features that the stack believes apply
  8129. **/
  8130. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  8131. struct net_device *dev,
  8132. netdev_features_t features)
  8133. {
  8134. size_t len;
  8135. /* No point in doing any of this if neither checksum nor GSO are
  8136. * being requested for this frame. We can rule out both by just
  8137. * checking for CHECKSUM_PARTIAL
  8138. */
  8139. if (skb->ip_summed != CHECKSUM_PARTIAL)
  8140. return features;
  8141. /* We cannot support GSO if the MSS is going to be less than
  8142. * 64 bytes. If it is then we need to drop support for GSO.
  8143. */
  8144. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  8145. features &= ~NETIF_F_GSO_MASK;
  8146. /* MACLEN can support at most 63 words */
  8147. len = skb_network_header(skb) - skb->data;
  8148. if (len & ~(63 * 2))
  8149. goto out_err;
  8150. /* IPLEN and EIPLEN can support at most 127 dwords */
  8151. len = skb_transport_header(skb) - skb_network_header(skb);
  8152. if (len & ~(127 * 4))
  8153. goto out_err;
  8154. if (skb->encapsulation) {
  8155. /* L4TUNLEN can support 127 words */
  8156. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  8157. if (len & ~(127 * 2))
  8158. goto out_err;
  8159. /* IPLEN can support at most 127 dwords */
  8160. len = skb_inner_transport_header(skb) -
  8161. skb_inner_network_header(skb);
  8162. if (len & ~(127 * 4))
  8163. goto out_err;
  8164. }
  8165. /* No need to validate L4LEN as TCP is the only protocol with a
  8166. * a flexible value and we support all possible values supported
  8167. * by TCP, which is at most 15 dwords
  8168. */
  8169. return features;
  8170. out_err:
  8171. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  8172. }
  8173. static const struct net_device_ops i40e_netdev_ops = {
  8174. .ndo_open = i40e_open,
  8175. .ndo_stop = i40e_close,
  8176. .ndo_start_xmit = i40e_lan_xmit_frame,
  8177. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  8178. .ndo_set_rx_mode = i40e_set_rx_mode,
  8179. .ndo_validate_addr = eth_validate_addr,
  8180. .ndo_set_mac_address = i40e_set_mac,
  8181. .ndo_change_mtu = i40e_change_mtu,
  8182. .ndo_do_ioctl = i40e_ioctl,
  8183. .ndo_tx_timeout = i40e_tx_timeout,
  8184. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  8185. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  8186. #ifdef CONFIG_NET_POLL_CONTROLLER
  8187. .ndo_poll_controller = i40e_netpoll,
  8188. #endif
  8189. .ndo_setup_tc = __i40e_setup_tc,
  8190. #ifdef I40E_FCOE
  8191. .ndo_fcoe_enable = i40e_fcoe_enable,
  8192. .ndo_fcoe_disable = i40e_fcoe_disable,
  8193. #endif
  8194. .ndo_set_features = i40e_set_features,
  8195. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  8196. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  8197. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  8198. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  8199. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  8200. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  8201. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  8202. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  8203. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  8204. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  8205. .ndo_fdb_add = i40e_ndo_fdb_add,
  8206. .ndo_features_check = i40e_features_check,
  8207. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  8208. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  8209. };
  8210. /**
  8211. * i40e_config_netdev - Setup the netdev flags
  8212. * @vsi: the VSI being configured
  8213. *
  8214. * Returns 0 on success, negative value on failure
  8215. **/
  8216. static int i40e_config_netdev(struct i40e_vsi *vsi)
  8217. {
  8218. struct i40e_pf *pf = vsi->back;
  8219. struct i40e_hw *hw = &pf->hw;
  8220. struct i40e_netdev_priv *np;
  8221. struct net_device *netdev;
  8222. u8 broadcast[ETH_ALEN];
  8223. u8 mac_addr[ETH_ALEN];
  8224. int etherdev_size;
  8225. etherdev_size = sizeof(struct i40e_netdev_priv);
  8226. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  8227. if (!netdev)
  8228. return -ENOMEM;
  8229. vsi->netdev = netdev;
  8230. np = netdev_priv(netdev);
  8231. np->vsi = vsi;
  8232. netdev->hw_enc_features |= NETIF_F_SG |
  8233. NETIF_F_IP_CSUM |
  8234. NETIF_F_IPV6_CSUM |
  8235. NETIF_F_HIGHDMA |
  8236. NETIF_F_SOFT_FEATURES |
  8237. NETIF_F_TSO |
  8238. NETIF_F_TSO_ECN |
  8239. NETIF_F_TSO6 |
  8240. NETIF_F_GSO_GRE |
  8241. NETIF_F_GSO_GRE_CSUM |
  8242. NETIF_F_GSO_IPXIP4 |
  8243. NETIF_F_GSO_IPXIP6 |
  8244. NETIF_F_GSO_UDP_TUNNEL |
  8245. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  8246. NETIF_F_GSO_PARTIAL |
  8247. NETIF_F_SCTP_CRC |
  8248. NETIF_F_RXHASH |
  8249. NETIF_F_RXCSUM |
  8250. 0;
  8251. if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
  8252. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  8253. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  8254. /* record features VLANs can make use of */
  8255. netdev->vlan_features |= netdev->hw_enc_features |
  8256. NETIF_F_TSO_MANGLEID;
  8257. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  8258. netdev->hw_features |= NETIF_F_NTUPLE;
  8259. netdev->hw_features |= netdev->hw_enc_features |
  8260. NETIF_F_HW_VLAN_CTAG_TX |
  8261. NETIF_F_HW_VLAN_CTAG_RX;
  8262. netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  8263. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  8264. if (vsi->type == I40E_VSI_MAIN) {
  8265. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  8266. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  8267. /* The following steps are necessary to prevent reception
  8268. * of tagged packets - some older NVM configurations load a
  8269. * default a MAC-VLAN filter that accepts any tagged packet
  8270. * which must be replaced by a normal filter.
  8271. */
  8272. i40e_rm_default_mac_filter(vsi, mac_addr);
  8273. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8274. i40e_add_mac_filter(vsi, mac_addr);
  8275. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8276. } else {
  8277. /* relate the VSI_VMDQ name to the VSI_MAIN name */
  8278. snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
  8279. pf->vsi[pf->lan_vsi]->netdev->name);
  8280. random_ether_addr(mac_addr);
  8281. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8282. i40e_add_mac_filter(vsi, mac_addr);
  8283. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8284. }
  8285. /* Add the broadcast filter so that we initially will receive
  8286. * broadcast packets. Note that when a new VLAN is first added the
  8287. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  8288. * specific filters as part of transitioning into "vlan" operation.
  8289. * When more VLANs are added, the driver will copy each existing MAC
  8290. * filter and add it for the new VLAN.
  8291. *
  8292. * Broadcast filters are handled specially by
  8293. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  8294. * promiscuous bit instead of adding this directly as a MAC/VLAN
  8295. * filter. The subtask will update the correct broadcast promiscuous
  8296. * bits as VLANs become active or inactive.
  8297. */
  8298. eth_broadcast_addr(broadcast);
  8299. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8300. i40e_add_mac_filter(vsi, broadcast);
  8301. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8302. ether_addr_copy(netdev->dev_addr, mac_addr);
  8303. ether_addr_copy(netdev->perm_addr, mac_addr);
  8304. netdev->priv_flags |= IFF_UNICAST_FLT;
  8305. netdev->priv_flags |= IFF_SUPP_NOFCS;
  8306. /* Setup netdev TC information */
  8307. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  8308. netdev->netdev_ops = &i40e_netdev_ops;
  8309. netdev->watchdog_timeo = 5 * HZ;
  8310. i40e_set_ethtool_ops(netdev);
  8311. #ifdef I40E_FCOE
  8312. i40e_fcoe_config_netdev(netdev, vsi);
  8313. #endif
  8314. /* MTU range: 68 - 9706 */
  8315. netdev->min_mtu = ETH_MIN_MTU;
  8316. netdev->max_mtu = I40E_MAX_RXBUFFER -
  8317. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  8318. return 0;
  8319. }
  8320. /**
  8321. * i40e_vsi_delete - Delete a VSI from the switch
  8322. * @vsi: the VSI being removed
  8323. *
  8324. * Returns 0 on success, negative value on failure
  8325. **/
  8326. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  8327. {
  8328. /* remove default VSI is not allowed */
  8329. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  8330. return;
  8331. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  8332. }
  8333. /**
  8334. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  8335. * @vsi: the VSI being queried
  8336. *
  8337. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  8338. **/
  8339. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  8340. {
  8341. struct i40e_veb *veb;
  8342. struct i40e_pf *pf = vsi->back;
  8343. /* Uplink is not a bridge so default to VEB */
  8344. if (vsi->veb_idx == I40E_NO_VEB)
  8345. return 1;
  8346. veb = pf->veb[vsi->veb_idx];
  8347. if (!veb) {
  8348. dev_info(&pf->pdev->dev,
  8349. "There is no veb associated with the bridge\n");
  8350. return -ENOENT;
  8351. }
  8352. /* Uplink is a bridge in VEPA mode */
  8353. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  8354. return 0;
  8355. } else {
  8356. /* Uplink is a bridge in VEB mode */
  8357. return 1;
  8358. }
  8359. /* VEPA is now default bridge, so return 0 */
  8360. return 0;
  8361. }
  8362. /**
  8363. * i40e_add_vsi - Add a VSI to the switch
  8364. * @vsi: the VSI being configured
  8365. *
  8366. * This initializes a VSI context depending on the VSI type to be added and
  8367. * passes it down to the add_vsi aq command.
  8368. **/
  8369. static int i40e_add_vsi(struct i40e_vsi *vsi)
  8370. {
  8371. int ret = -ENODEV;
  8372. struct i40e_pf *pf = vsi->back;
  8373. struct i40e_hw *hw = &pf->hw;
  8374. struct i40e_vsi_context ctxt;
  8375. struct i40e_mac_filter *f;
  8376. struct hlist_node *h;
  8377. int bkt;
  8378. u8 enabled_tc = 0x1; /* TC0 enabled */
  8379. int f_count = 0;
  8380. memset(&ctxt, 0, sizeof(ctxt));
  8381. switch (vsi->type) {
  8382. case I40E_VSI_MAIN:
  8383. /* The PF's main VSI is already setup as part of the
  8384. * device initialization, so we'll not bother with
  8385. * the add_vsi call, but we will retrieve the current
  8386. * VSI context.
  8387. */
  8388. ctxt.seid = pf->main_vsi_seid;
  8389. ctxt.pf_num = pf->hw.pf_id;
  8390. ctxt.vf_num = 0;
  8391. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  8392. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8393. if (ret) {
  8394. dev_info(&pf->pdev->dev,
  8395. "couldn't get PF vsi config, err %s aq_err %s\n",
  8396. i40e_stat_str(&pf->hw, ret),
  8397. i40e_aq_str(&pf->hw,
  8398. pf->hw.aq.asq_last_status));
  8399. return -ENOENT;
  8400. }
  8401. vsi->info = ctxt.info;
  8402. vsi->info.valid_sections = 0;
  8403. vsi->seid = ctxt.seid;
  8404. vsi->id = ctxt.vsi_number;
  8405. enabled_tc = i40e_pf_get_tc_map(pf);
  8406. /* MFP mode setup queue map and update VSI */
  8407. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  8408. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  8409. memset(&ctxt, 0, sizeof(ctxt));
  8410. ctxt.seid = pf->main_vsi_seid;
  8411. ctxt.pf_num = pf->hw.pf_id;
  8412. ctxt.vf_num = 0;
  8413. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  8414. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  8415. if (ret) {
  8416. dev_info(&pf->pdev->dev,
  8417. "update vsi failed, err %s aq_err %s\n",
  8418. i40e_stat_str(&pf->hw, ret),
  8419. i40e_aq_str(&pf->hw,
  8420. pf->hw.aq.asq_last_status));
  8421. ret = -ENOENT;
  8422. goto err;
  8423. }
  8424. /* update the local VSI info queue map */
  8425. i40e_vsi_update_queue_map(vsi, &ctxt);
  8426. vsi->info.valid_sections = 0;
  8427. } else {
  8428. /* Default/Main VSI is only enabled for TC0
  8429. * reconfigure it to enable all TCs that are
  8430. * available on the port in SFP mode.
  8431. * For MFP case the iSCSI PF would use this
  8432. * flow to enable LAN+iSCSI TC.
  8433. */
  8434. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  8435. if (ret) {
  8436. dev_info(&pf->pdev->dev,
  8437. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  8438. enabled_tc,
  8439. i40e_stat_str(&pf->hw, ret),
  8440. i40e_aq_str(&pf->hw,
  8441. pf->hw.aq.asq_last_status));
  8442. ret = -ENOENT;
  8443. }
  8444. }
  8445. break;
  8446. case I40E_VSI_FDIR:
  8447. ctxt.pf_num = hw->pf_id;
  8448. ctxt.vf_num = 0;
  8449. ctxt.uplink_seid = vsi->uplink_seid;
  8450. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8451. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8452. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  8453. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  8454. ctxt.info.valid_sections |=
  8455. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8456. ctxt.info.switch_id =
  8457. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8458. }
  8459. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8460. break;
  8461. case I40E_VSI_VMDQ2:
  8462. ctxt.pf_num = hw->pf_id;
  8463. ctxt.vf_num = 0;
  8464. ctxt.uplink_seid = vsi->uplink_seid;
  8465. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8466. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  8467. /* This VSI is connected to VEB so the switch_id
  8468. * should be set to zero by default.
  8469. */
  8470. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8471. ctxt.info.valid_sections |=
  8472. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8473. ctxt.info.switch_id =
  8474. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8475. }
  8476. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8477. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8478. break;
  8479. case I40E_VSI_SRIOV:
  8480. ctxt.pf_num = hw->pf_id;
  8481. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  8482. ctxt.uplink_seid = vsi->uplink_seid;
  8483. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8484. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  8485. /* This VSI is connected to VEB so the switch_id
  8486. * should be set to zero by default.
  8487. */
  8488. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8489. ctxt.info.valid_sections |=
  8490. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8491. ctxt.info.switch_id =
  8492. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8493. }
  8494. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  8495. ctxt.info.valid_sections |=
  8496. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  8497. ctxt.info.queueing_opt_flags |=
  8498. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  8499. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  8500. }
  8501. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  8502. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  8503. if (pf->vf[vsi->vf_id].spoofchk) {
  8504. ctxt.info.valid_sections |=
  8505. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  8506. ctxt.info.sec_flags |=
  8507. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  8508. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  8509. }
  8510. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8511. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8512. break;
  8513. #ifdef I40E_FCOE
  8514. case I40E_VSI_FCOE:
  8515. ret = i40e_fcoe_vsi_init(vsi, &ctxt);
  8516. if (ret) {
  8517. dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
  8518. return ret;
  8519. }
  8520. break;
  8521. #endif /* I40E_FCOE */
  8522. case I40E_VSI_IWARP:
  8523. /* send down message to iWARP */
  8524. break;
  8525. default:
  8526. return -ENODEV;
  8527. }
  8528. if (vsi->type != I40E_VSI_MAIN) {
  8529. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  8530. if (ret) {
  8531. dev_info(&vsi->back->pdev->dev,
  8532. "add vsi failed, err %s aq_err %s\n",
  8533. i40e_stat_str(&pf->hw, ret),
  8534. i40e_aq_str(&pf->hw,
  8535. pf->hw.aq.asq_last_status));
  8536. ret = -ENOENT;
  8537. goto err;
  8538. }
  8539. vsi->info = ctxt.info;
  8540. vsi->info.valid_sections = 0;
  8541. vsi->seid = ctxt.seid;
  8542. vsi->id = ctxt.vsi_number;
  8543. }
  8544. vsi->active_filters = 0;
  8545. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  8546. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8547. /* If macvlan filters already exist, force them to get loaded */
  8548. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  8549. f->state = I40E_FILTER_NEW;
  8550. f_count++;
  8551. }
  8552. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8553. if (f_count) {
  8554. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  8555. pf->flags |= I40E_FLAG_FILTER_SYNC;
  8556. }
  8557. /* Update VSI BW information */
  8558. ret = i40e_vsi_get_bw_info(vsi);
  8559. if (ret) {
  8560. dev_info(&pf->pdev->dev,
  8561. "couldn't get vsi bw info, err %s aq_err %s\n",
  8562. i40e_stat_str(&pf->hw, ret),
  8563. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8564. /* VSI is already added so not tearing that up */
  8565. ret = 0;
  8566. }
  8567. err:
  8568. return ret;
  8569. }
  8570. /**
  8571. * i40e_vsi_release - Delete a VSI and free its resources
  8572. * @vsi: the VSI being removed
  8573. *
  8574. * Returns 0 on success or < 0 on error
  8575. **/
  8576. int i40e_vsi_release(struct i40e_vsi *vsi)
  8577. {
  8578. struct i40e_mac_filter *f;
  8579. struct hlist_node *h;
  8580. struct i40e_veb *veb = NULL;
  8581. struct i40e_pf *pf;
  8582. u16 uplink_seid;
  8583. int i, n, bkt;
  8584. pf = vsi->back;
  8585. /* release of a VEB-owner or last VSI is not allowed */
  8586. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  8587. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  8588. vsi->seid, vsi->uplink_seid);
  8589. return -ENODEV;
  8590. }
  8591. if (vsi == pf->vsi[pf->lan_vsi] &&
  8592. !test_bit(__I40E_DOWN, &pf->state)) {
  8593. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  8594. return -ENODEV;
  8595. }
  8596. uplink_seid = vsi->uplink_seid;
  8597. if (vsi->type != I40E_VSI_SRIOV) {
  8598. if (vsi->netdev_registered) {
  8599. vsi->netdev_registered = false;
  8600. if (vsi->netdev) {
  8601. /* results in a call to i40e_close() */
  8602. unregister_netdev(vsi->netdev);
  8603. }
  8604. } else {
  8605. i40e_vsi_close(vsi);
  8606. }
  8607. i40e_vsi_disable_irq(vsi);
  8608. }
  8609. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8610. /* clear the sync flag on all filters */
  8611. if (vsi->netdev) {
  8612. __dev_uc_unsync(vsi->netdev, NULL);
  8613. __dev_mc_unsync(vsi->netdev, NULL);
  8614. }
  8615. /* make sure any remaining filters are marked for deletion */
  8616. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  8617. __i40e_del_filter(vsi, f);
  8618. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8619. i40e_sync_vsi_filters(vsi);
  8620. i40e_vsi_delete(vsi);
  8621. i40e_vsi_free_q_vectors(vsi);
  8622. if (vsi->netdev) {
  8623. free_netdev(vsi->netdev);
  8624. vsi->netdev = NULL;
  8625. }
  8626. i40e_vsi_clear_rings(vsi);
  8627. i40e_vsi_clear(vsi);
  8628. /* If this was the last thing on the VEB, except for the
  8629. * controlling VSI, remove the VEB, which puts the controlling
  8630. * VSI onto the next level down in the switch.
  8631. *
  8632. * Well, okay, there's one more exception here: don't remove
  8633. * the orphan VEBs yet. We'll wait for an explicit remove request
  8634. * from up the network stack.
  8635. */
  8636. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  8637. if (pf->vsi[i] &&
  8638. pf->vsi[i]->uplink_seid == uplink_seid &&
  8639. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8640. n++; /* count the VSIs */
  8641. }
  8642. }
  8643. for (i = 0; i < I40E_MAX_VEB; i++) {
  8644. if (!pf->veb[i])
  8645. continue;
  8646. if (pf->veb[i]->uplink_seid == uplink_seid)
  8647. n++; /* count the VEBs */
  8648. if (pf->veb[i]->seid == uplink_seid)
  8649. veb = pf->veb[i];
  8650. }
  8651. if (n == 0 && veb && veb->uplink_seid != 0)
  8652. i40e_veb_release(veb);
  8653. return 0;
  8654. }
  8655. /**
  8656. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  8657. * @vsi: ptr to the VSI
  8658. *
  8659. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  8660. * corresponding SW VSI structure and initializes num_queue_pairs for the
  8661. * newly allocated VSI.
  8662. *
  8663. * Returns 0 on success or negative on failure
  8664. **/
  8665. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  8666. {
  8667. int ret = -ENOENT;
  8668. struct i40e_pf *pf = vsi->back;
  8669. if (vsi->q_vectors[0]) {
  8670. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  8671. vsi->seid);
  8672. return -EEXIST;
  8673. }
  8674. if (vsi->base_vector) {
  8675. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  8676. vsi->seid, vsi->base_vector);
  8677. return -EEXIST;
  8678. }
  8679. ret = i40e_vsi_alloc_q_vectors(vsi);
  8680. if (ret) {
  8681. dev_info(&pf->pdev->dev,
  8682. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  8683. vsi->num_q_vectors, vsi->seid, ret);
  8684. vsi->num_q_vectors = 0;
  8685. goto vector_setup_out;
  8686. }
  8687. /* In Legacy mode, we do not have to get any other vector since we
  8688. * piggyback on the misc/ICR0 for queue interrupts.
  8689. */
  8690. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8691. return ret;
  8692. if (vsi->num_q_vectors)
  8693. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  8694. vsi->num_q_vectors, vsi->idx);
  8695. if (vsi->base_vector < 0) {
  8696. dev_info(&pf->pdev->dev,
  8697. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  8698. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  8699. i40e_vsi_free_q_vectors(vsi);
  8700. ret = -ENOENT;
  8701. goto vector_setup_out;
  8702. }
  8703. vector_setup_out:
  8704. return ret;
  8705. }
  8706. /**
  8707. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  8708. * @vsi: pointer to the vsi.
  8709. *
  8710. * This re-allocates a vsi's queue resources.
  8711. *
  8712. * Returns pointer to the successfully allocated and configured VSI sw struct
  8713. * on success, otherwise returns NULL on failure.
  8714. **/
  8715. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  8716. {
  8717. struct i40e_pf *pf;
  8718. u8 enabled_tc;
  8719. int ret;
  8720. if (!vsi)
  8721. return NULL;
  8722. pf = vsi->back;
  8723. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8724. i40e_vsi_clear_rings(vsi);
  8725. i40e_vsi_free_arrays(vsi, false);
  8726. i40e_set_num_rings_in_vsi(vsi);
  8727. ret = i40e_vsi_alloc_arrays(vsi, false);
  8728. if (ret)
  8729. goto err_vsi;
  8730. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
  8731. if (ret < 0) {
  8732. dev_info(&pf->pdev->dev,
  8733. "failed to get tracking for %d queues for VSI %d err %d\n",
  8734. vsi->alloc_queue_pairs, vsi->seid, ret);
  8735. goto err_vsi;
  8736. }
  8737. vsi->base_queue = ret;
  8738. /* Update the FW view of the VSI. Force a reset of TC and queue
  8739. * layout configurations.
  8740. */
  8741. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  8742. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  8743. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  8744. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  8745. if (vsi->type == I40E_VSI_MAIN)
  8746. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  8747. /* assign it some queues */
  8748. ret = i40e_alloc_rings(vsi);
  8749. if (ret)
  8750. goto err_rings;
  8751. /* map all of the rings to the q_vectors */
  8752. i40e_vsi_map_rings_to_vectors(vsi);
  8753. return vsi;
  8754. err_rings:
  8755. i40e_vsi_free_q_vectors(vsi);
  8756. if (vsi->netdev_registered) {
  8757. vsi->netdev_registered = false;
  8758. unregister_netdev(vsi->netdev);
  8759. free_netdev(vsi->netdev);
  8760. vsi->netdev = NULL;
  8761. }
  8762. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8763. err_vsi:
  8764. i40e_vsi_clear(vsi);
  8765. return NULL;
  8766. }
  8767. /**
  8768. * i40e_vsi_setup - Set up a VSI by a given type
  8769. * @pf: board private structure
  8770. * @type: VSI type
  8771. * @uplink_seid: the switch element to link to
  8772. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  8773. *
  8774. * This allocates the sw VSI structure and its queue resources, then add a VSI
  8775. * to the identified VEB.
  8776. *
  8777. * Returns pointer to the successfully allocated and configure VSI sw struct on
  8778. * success, otherwise returns NULL on failure.
  8779. **/
  8780. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  8781. u16 uplink_seid, u32 param1)
  8782. {
  8783. struct i40e_vsi *vsi = NULL;
  8784. struct i40e_veb *veb = NULL;
  8785. int ret, i;
  8786. int v_idx;
  8787. /* The requested uplink_seid must be either
  8788. * - the PF's port seid
  8789. * no VEB is needed because this is the PF
  8790. * or this is a Flow Director special case VSI
  8791. * - seid of an existing VEB
  8792. * - seid of a VSI that owns an existing VEB
  8793. * - seid of a VSI that doesn't own a VEB
  8794. * a new VEB is created and the VSI becomes the owner
  8795. * - seid of the PF VSI, which is what creates the first VEB
  8796. * this is a special case of the previous
  8797. *
  8798. * Find which uplink_seid we were given and create a new VEB if needed
  8799. */
  8800. for (i = 0; i < I40E_MAX_VEB; i++) {
  8801. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  8802. veb = pf->veb[i];
  8803. break;
  8804. }
  8805. }
  8806. if (!veb && uplink_seid != pf->mac_seid) {
  8807. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8808. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  8809. vsi = pf->vsi[i];
  8810. break;
  8811. }
  8812. }
  8813. if (!vsi) {
  8814. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  8815. uplink_seid);
  8816. return NULL;
  8817. }
  8818. if (vsi->uplink_seid == pf->mac_seid)
  8819. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  8820. vsi->tc_config.enabled_tc);
  8821. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  8822. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8823. vsi->tc_config.enabled_tc);
  8824. if (veb) {
  8825. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  8826. dev_info(&vsi->back->pdev->dev,
  8827. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  8828. return NULL;
  8829. }
  8830. /* We come up by default in VEPA mode if SRIOV is not
  8831. * already enabled, in which case we can't force VEPA
  8832. * mode.
  8833. */
  8834. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  8835. veb->bridge_mode = BRIDGE_MODE_VEPA;
  8836. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8837. }
  8838. i40e_config_bridge_mode(veb);
  8839. }
  8840. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8841. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8842. veb = pf->veb[i];
  8843. }
  8844. if (!veb) {
  8845. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  8846. return NULL;
  8847. }
  8848. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  8849. uplink_seid = veb->seid;
  8850. }
  8851. /* get vsi sw struct */
  8852. v_idx = i40e_vsi_mem_alloc(pf, type);
  8853. if (v_idx < 0)
  8854. goto err_alloc;
  8855. vsi = pf->vsi[v_idx];
  8856. if (!vsi)
  8857. goto err_alloc;
  8858. vsi->type = type;
  8859. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  8860. if (type == I40E_VSI_MAIN)
  8861. pf->lan_vsi = v_idx;
  8862. else if (type == I40E_VSI_SRIOV)
  8863. vsi->vf_id = param1;
  8864. /* assign it some queues */
  8865. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
  8866. vsi->idx);
  8867. if (ret < 0) {
  8868. dev_info(&pf->pdev->dev,
  8869. "failed to get tracking for %d queues for VSI %d err=%d\n",
  8870. vsi->alloc_queue_pairs, vsi->seid, ret);
  8871. goto err_vsi;
  8872. }
  8873. vsi->base_queue = ret;
  8874. /* get a VSI from the hardware */
  8875. vsi->uplink_seid = uplink_seid;
  8876. ret = i40e_add_vsi(vsi);
  8877. if (ret)
  8878. goto err_vsi;
  8879. switch (vsi->type) {
  8880. /* setup the netdev if needed */
  8881. case I40E_VSI_MAIN:
  8882. /* Apply relevant filters if a platform-specific mac
  8883. * address was selected.
  8884. */
  8885. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  8886. ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  8887. if (ret) {
  8888. dev_warn(&pf->pdev->dev,
  8889. "could not set up macaddr; err %d\n",
  8890. ret);
  8891. }
  8892. }
  8893. case I40E_VSI_VMDQ2:
  8894. case I40E_VSI_FCOE:
  8895. ret = i40e_config_netdev(vsi);
  8896. if (ret)
  8897. goto err_netdev;
  8898. ret = register_netdev(vsi->netdev);
  8899. if (ret)
  8900. goto err_netdev;
  8901. vsi->netdev_registered = true;
  8902. netif_carrier_off(vsi->netdev);
  8903. #ifdef CONFIG_I40E_DCB
  8904. /* Setup DCB netlink interface */
  8905. i40e_dcbnl_setup(vsi);
  8906. #endif /* CONFIG_I40E_DCB */
  8907. /* fall through */
  8908. case I40E_VSI_FDIR:
  8909. /* set up vectors and rings if needed */
  8910. ret = i40e_vsi_setup_vectors(vsi);
  8911. if (ret)
  8912. goto err_msix;
  8913. ret = i40e_alloc_rings(vsi);
  8914. if (ret)
  8915. goto err_rings;
  8916. /* map all of the rings to the q_vectors */
  8917. i40e_vsi_map_rings_to_vectors(vsi);
  8918. i40e_vsi_reset_stats(vsi);
  8919. break;
  8920. default:
  8921. /* no netdev or rings for the other VSI types */
  8922. break;
  8923. }
  8924. if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
  8925. (vsi->type == I40E_VSI_VMDQ2)) {
  8926. ret = i40e_vsi_config_rss(vsi);
  8927. }
  8928. return vsi;
  8929. err_rings:
  8930. i40e_vsi_free_q_vectors(vsi);
  8931. err_msix:
  8932. if (vsi->netdev_registered) {
  8933. vsi->netdev_registered = false;
  8934. unregister_netdev(vsi->netdev);
  8935. free_netdev(vsi->netdev);
  8936. vsi->netdev = NULL;
  8937. }
  8938. err_netdev:
  8939. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8940. err_vsi:
  8941. i40e_vsi_clear(vsi);
  8942. err_alloc:
  8943. return NULL;
  8944. }
  8945. /**
  8946. * i40e_veb_get_bw_info - Query VEB BW information
  8947. * @veb: the veb to query
  8948. *
  8949. * Query the Tx scheduler BW configuration data for given VEB
  8950. **/
  8951. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  8952. {
  8953. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  8954. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  8955. struct i40e_pf *pf = veb->pf;
  8956. struct i40e_hw *hw = &pf->hw;
  8957. u32 tc_bw_max;
  8958. int ret = 0;
  8959. int i;
  8960. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  8961. &bw_data, NULL);
  8962. if (ret) {
  8963. dev_info(&pf->pdev->dev,
  8964. "query veb bw config failed, err %s aq_err %s\n",
  8965. i40e_stat_str(&pf->hw, ret),
  8966. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8967. goto out;
  8968. }
  8969. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  8970. &ets_data, NULL);
  8971. if (ret) {
  8972. dev_info(&pf->pdev->dev,
  8973. "query veb bw ets config failed, err %s aq_err %s\n",
  8974. i40e_stat_str(&pf->hw, ret),
  8975. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8976. goto out;
  8977. }
  8978. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  8979. veb->bw_max_quanta = ets_data.tc_bw_max;
  8980. veb->is_abs_credits = bw_data.absolute_credits_enable;
  8981. veb->enabled_tc = ets_data.tc_valid_bits;
  8982. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  8983. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  8984. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  8985. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  8986. veb->bw_tc_limit_credits[i] =
  8987. le16_to_cpu(bw_data.tc_bw_limits[i]);
  8988. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  8989. }
  8990. out:
  8991. return ret;
  8992. }
  8993. /**
  8994. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  8995. * @pf: board private structure
  8996. *
  8997. * On error: returns error code (negative)
  8998. * On success: returns vsi index in PF (positive)
  8999. **/
  9000. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  9001. {
  9002. int ret = -ENOENT;
  9003. struct i40e_veb *veb;
  9004. int i;
  9005. /* Need to protect the allocation of switch elements at the PF level */
  9006. mutex_lock(&pf->switch_mutex);
  9007. /* VEB list may be fragmented if VEB creation/destruction has
  9008. * been happening. We can afford to do a quick scan to look
  9009. * for any free slots in the list.
  9010. *
  9011. * find next empty veb slot, looping back around if necessary
  9012. */
  9013. i = 0;
  9014. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  9015. i++;
  9016. if (i >= I40E_MAX_VEB) {
  9017. ret = -ENOMEM;
  9018. goto err_alloc_veb; /* out of VEB slots! */
  9019. }
  9020. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  9021. if (!veb) {
  9022. ret = -ENOMEM;
  9023. goto err_alloc_veb;
  9024. }
  9025. veb->pf = pf;
  9026. veb->idx = i;
  9027. veb->enabled_tc = 1;
  9028. pf->veb[i] = veb;
  9029. ret = i;
  9030. err_alloc_veb:
  9031. mutex_unlock(&pf->switch_mutex);
  9032. return ret;
  9033. }
  9034. /**
  9035. * i40e_switch_branch_release - Delete a branch of the switch tree
  9036. * @branch: where to start deleting
  9037. *
  9038. * This uses recursion to find the tips of the branch to be
  9039. * removed, deleting until we get back to and can delete this VEB.
  9040. **/
  9041. static void i40e_switch_branch_release(struct i40e_veb *branch)
  9042. {
  9043. struct i40e_pf *pf = branch->pf;
  9044. u16 branch_seid = branch->seid;
  9045. u16 veb_idx = branch->idx;
  9046. int i;
  9047. /* release any VEBs on this VEB - RECURSION */
  9048. for (i = 0; i < I40E_MAX_VEB; i++) {
  9049. if (!pf->veb[i])
  9050. continue;
  9051. if (pf->veb[i]->uplink_seid == branch->seid)
  9052. i40e_switch_branch_release(pf->veb[i]);
  9053. }
  9054. /* Release the VSIs on this VEB, but not the owner VSI.
  9055. *
  9056. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  9057. * the VEB itself, so don't use (*branch) after this loop.
  9058. */
  9059. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9060. if (!pf->vsi[i])
  9061. continue;
  9062. if (pf->vsi[i]->uplink_seid == branch_seid &&
  9063. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  9064. i40e_vsi_release(pf->vsi[i]);
  9065. }
  9066. }
  9067. /* There's one corner case where the VEB might not have been
  9068. * removed, so double check it here and remove it if needed.
  9069. * This case happens if the veb was created from the debugfs
  9070. * commands and no VSIs were added to it.
  9071. */
  9072. if (pf->veb[veb_idx])
  9073. i40e_veb_release(pf->veb[veb_idx]);
  9074. }
  9075. /**
  9076. * i40e_veb_clear - remove veb struct
  9077. * @veb: the veb to remove
  9078. **/
  9079. static void i40e_veb_clear(struct i40e_veb *veb)
  9080. {
  9081. if (!veb)
  9082. return;
  9083. if (veb->pf) {
  9084. struct i40e_pf *pf = veb->pf;
  9085. mutex_lock(&pf->switch_mutex);
  9086. if (pf->veb[veb->idx] == veb)
  9087. pf->veb[veb->idx] = NULL;
  9088. mutex_unlock(&pf->switch_mutex);
  9089. }
  9090. kfree(veb);
  9091. }
  9092. /**
  9093. * i40e_veb_release - Delete a VEB and free its resources
  9094. * @veb: the VEB being removed
  9095. **/
  9096. void i40e_veb_release(struct i40e_veb *veb)
  9097. {
  9098. struct i40e_vsi *vsi = NULL;
  9099. struct i40e_pf *pf;
  9100. int i, n = 0;
  9101. pf = veb->pf;
  9102. /* find the remaining VSI and check for extras */
  9103. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9104. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  9105. n++;
  9106. vsi = pf->vsi[i];
  9107. }
  9108. }
  9109. if (n != 1) {
  9110. dev_info(&pf->pdev->dev,
  9111. "can't remove VEB %d with %d VSIs left\n",
  9112. veb->seid, n);
  9113. return;
  9114. }
  9115. /* move the remaining VSI to uplink veb */
  9116. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  9117. if (veb->uplink_seid) {
  9118. vsi->uplink_seid = veb->uplink_seid;
  9119. if (veb->uplink_seid == pf->mac_seid)
  9120. vsi->veb_idx = I40E_NO_VEB;
  9121. else
  9122. vsi->veb_idx = veb->veb_idx;
  9123. } else {
  9124. /* floating VEB */
  9125. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  9126. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  9127. }
  9128. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9129. i40e_veb_clear(veb);
  9130. }
  9131. /**
  9132. * i40e_add_veb - create the VEB in the switch
  9133. * @veb: the VEB to be instantiated
  9134. * @vsi: the controlling VSI
  9135. **/
  9136. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  9137. {
  9138. struct i40e_pf *pf = veb->pf;
  9139. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  9140. int ret;
  9141. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  9142. veb->enabled_tc, false,
  9143. &veb->seid, enable_stats, NULL);
  9144. /* get a VEB from the hardware */
  9145. if (ret) {
  9146. dev_info(&pf->pdev->dev,
  9147. "couldn't add VEB, err %s aq_err %s\n",
  9148. i40e_stat_str(&pf->hw, ret),
  9149. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9150. return -EPERM;
  9151. }
  9152. /* get statistics counter */
  9153. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  9154. &veb->stats_idx, NULL, NULL, NULL);
  9155. if (ret) {
  9156. dev_info(&pf->pdev->dev,
  9157. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  9158. i40e_stat_str(&pf->hw, ret),
  9159. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9160. return -EPERM;
  9161. }
  9162. ret = i40e_veb_get_bw_info(veb);
  9163. if (ret) {
  9164. dev_info(&pf->pdev->dev,
  9165. "couldn't get VEB bw info, err %s aq_err %s\n",
  9166. i40e_stat_str(&pf->hw, ret),
  9167. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9168. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9169. return -ENOENT;
  9170. }
  9171. vsi->uplink_seid = veb->seid;
  9172. vsi->veb_idx = veb->idx;
  9173. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  9174. return 0;
  9175. }
  9176. /**
  9177. * i40e_veb_setup - Set up a VEB
  9178. * @pf: board private structure
  9179. * @flags: VEB setup flags
  9180. * @uplink_seid: the switch element to link to
  9181. * @vsi_seid: the initial VSI seid
  9182. * @enabled_tc: Enabled TC bit-map
  9183. *
  9184. * This allocates the sw VEB structure and links it into the switch
  9185. * It is possible and legal for this to be a duplicate of an already
  9186. * existing VEB. It is also possible for both uplink and vsi seids
  9187. * to be zero, in order to create a floating VEB.
  9188. *
  9189. * Returns pointer to the successfully allocated VEB sw struct on
  9190. * success, otherwise returns NULL on failure.
  9191. **/
  9192. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  9193. u16 uplink_seid, u16 vsi_seid,
  9194. u8 enabled_tc)
  9195. {
  9196. struct i40e_veb *veb, *uplink_veb = NULL;
  9197. int vsi_idx, veb_idx;
  9198. int ret;
  9199. /* if one seid is 0, the other must be 0 to create a floating relay */
  9200. if ((uplink_seid == 0 || vsi_seid == 0) &&
  9201. (uplink_seid + vsi_seid != 0)) {
  9202. dev_info(&pf->pdev->dev,
  9203. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  9204. uplink_seid, vsi_seid);
  9205. return NULL;
  9206. }
  9207. /* make sure there is such a vsi and uplink */
  9208. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  9209. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  9210. break;
  9211. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  9212. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  9213. vsi_seid);
  9214. return NULL;
  9215. }
  9216. if (uplink_seid && uplink_seid != pf->mac_seid) {
  9217. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  9218. if (pf->veb[veb_idx] &&
  9219. pf->veb[veb_idx]->seid == uplink_seid) {
  9220. uplink_veb = pf->veb[veb_idx];
  9221. break;
  9222. }
  9223. }
  9224. if (!uplink_veb) {
  9225. dev_info(&pf->pdev->dev,
  9226. "uplink seid %d not found\n", uplink_seid);
  9227. return NULL;
  9228. }
  9229. }
  9230. /* get veb sw struct */
  9231. veb_idx = i40e_veb_mem_alloc(pf);
  9232. if (veb_idx < 0)
  9233. goto err_alloc;
  9234. veb = pf->veb[veb_idx];
  9235. veb->flags = flags;
  9236. veb->uplink_seid = uplink_seid;
  9237. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  9238. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  9239. /* create the VEB in the switch */
  9240. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  9241. if (ret)
  9242. goto err_veb;
  9243. if (vsi_idx == pf->lan_vsi)
  9244. pf->lan_veb = veb->idx;
  9245. return veb;
  9246. err_veb:
  9247. i40e_veb_clear(veb);
  9248. err_alloc:
  9249. return NULL;
  9250. }
  9251. /**
  9252. * i40e_setup_pf_switch_element - set PF vars based on switch type
  9253. * @pf: board private structure
  9254. * @ele: element we are building info from
  9255. * @num_reported: total number of elements
  9256. * @printconfig: should we print the contents
  9257. *
  9258. * helper function to assist in extracting a few useful SEID values.
  9259. **/
  9260. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  9261. struct i40e_aqc_switch_config_element_resp *ele,
  9262. u16 num_reported, bool printconfig)
  9263. {
  9264. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  9265. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  9266. u8 element_type = ele->element_type;
  9267. u16 seid = le16_to_cpu(ele->seid);
  9268. if (printconfig)
  9269. dev_info(&pf->pdev->dev,
  9270. "type=%d seid=%d uplink=%d downlink=%d\n",
  9271. element_type, seid, uplink_seid, downlink_seid);
  9272. switch (element_type) {
  9273. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  9274. pf->mac_seid = seid;
  9275. break;
  9276. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  9277. /* Main VEB? */
  9278. if (uplink_seid != pf->mac_seid)
  9279. break;
  9280. if (pf->lan_veb == I40E_NO_VEB) {
  9281. int v;
  9282. /* find existing or else empty VEB */
  9283. for (v = 0; v < I40E_MAX_VEB; v++) {
  9284. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  9285. pf->lan_veb = v;
  9286. break;
  9287. }
  9288. }
  9289. if (pf->lan_veb == I40E_NO_VEB) {
  9290. v = i40e_veb_mem_alloc(pf);
  9291. if (v < 0)
  9292. break;
  9293. pf->lan_veb = v;
  9294. }
  9295. }
  9296. pf->veb[pf->lan_veb]->seid = seid;
  9297. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  9298. pf->veb[pf->lan_veb]->pf = pf;
  9299. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  9300. break;
  9301. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  9302. if (num_reported != 1)
  9303. break;
  9304. /* This is immediately after a reset so we can assume this is
  9305. * the PF's VSI
  9306. */
  9307. pf->mac_seid = uplink_seid;
  9308. pf->pf_seid = downlink_seid;
  9309. pf->main_vsi_seid = seid;
  9310. if (printconfig)
  9311. dev_info(&pf->pdev->dev,
  9312. "pf_seid=%d main_vsi_seid=%d\n",
  9313. pf->pf_seid, pf->main_vsi_seid);
  9314. break;
  9315. case I40E_SWITCH_ELEMENT_TYPE_PF:
  9316. case I40E_SWITCH_ELEMENT_TYPE_VF:
  9317. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  9318. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  9319. case I40E_SWITCH_ELEMENT_TYPE_PE:
  9320. case I40E_SWITCH_ELEMENT_TYPE_PA:
  9321. /* ignore these for now */
  9322. break;
  9323. default:
  9324. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  9325. element_type, seid);
  9326. break;
  9327. }
  9328. }
  9329. /**
  9330. * i40e_fetch_switch_configuration - Get switch config from firmware
  9331. * @pf: board private structure
  9332. * @printconfig: should we print the contents
  9333. *
  9334. * Get the current switch configuration from the device and
  9335. * extract a few useful SEID values.
  9336. **/
  9337. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  9338. {
  9339. struct i40e_aqc_get_switch_config_resp *sw_config;
  9340. u16 next_seid = 0;
  9341. int ret = 0;
  9342. u8 *aq_buf;
  9343. int i;
  9344. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  9345. if (!aq_buf)
  9346. return -ENOMEM;
  9347. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  9348. do {
  9349. u16 num_reported, num_total;
  9350. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  9351. I40E_AQ_LARGE_BUF,
  9352. &next_seid, NULL);
  9353. if (ret) {
  9354. dev_info(&pf->pdev->dev,
  9355. "get switch config failed err %s aq_err %s\n",
  9356. i40e_stat_str(&pf->hw, ret),
  9357. i40e_aq_str(&pf->hw,
  9358. pf->hw.aq.asq_last_status));
  9359. kfree(aq_buf);
  9360. return -ENOENT;
  9361. }
  9362. num_reported = le16_to_cpu(sw_config->header.num_reported);
  9363. num_total = le16_to_cpu(sw_config->header.num_total);
  9364. if (printconfig)
  9365. dev_info(&pf->pdev->dev,
  9366. "header: %d reported %d total\n",
  9367. num_reported, num_total);
  9368. for (i = 0; i < num_reported; i++) {
  9369. struct i40e_aqc_switch_config_element_resp *ele =
  9370. &sw_config->element[i];
  9371. i40e_setup_pf_switch_element(pf, ele, num_reported,
  9372. printconfig);
  9373. }
  9374. } while (next_seid != 0);
  9375. kfree(aq_buf);
  9376. return ret;
  9377. }
  9378. /**
  9379. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  9380. * @pf: board private structure
  9381. * @reinit: if the Main VSI needs to re-initialized.
  9382. *
  9383. * Returns 0 on success, negative value on failure
  9384. **/
  9385. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  9386. {
  9387. u16 flags = 0;
  9388. int ret;
  9389. /* find out what's out there already */
  9390. ret = i40e_fetch_switch_configuration(pf, false);
  9391. if (ret) {
  9392. dev_info(&pf->pdev->dev,
  9393. "couldn't fetch switch config, err %s aq_err %s\n",
  9394. i40e_stat_str(&pf->hw, ret),
  9395. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9396. return ret;
  9397. }
  9398. i40e_pf_reset_stats(pf);
  9399. /* set the switch config bit for the whole device to
  9400. * support limited promisc or true promisc
  9401. * when user requests promisc. The default is limited
  9402. * promisc.
  9403. */
  9404. if ((pf->hw.pf_id == 0) &&
  9405. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
  9406. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9407. if (pf->hw.pf_id == 0) {
  9408. u16 valid_flags;
  9409. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9410. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
  9411. NULL);
  9412. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  9413. dev_info(&pf->pdev->dev,
  9414. "couldn't set switch config bits, err %s aq_err %s\n",
  9415. i40e_stat_str(&pf->hw, ret),
  9416. i40e_aq_str(&pf->hw,
  9417. pf->hw.aq.asq_last_status));
  9418. /* not a fatal problem, just keep going */
  9419. }
  9420. }
  9421. /* first time setup */
  9422. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  9423. struct i40e_vsi *vsi = NULL;
  9424. u16 uplink_seid;
  9425. /* Set up the PF VSI associated with the PF's main VSI
  9426. * that is already in the HW switch
  9427. */
  9428. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  9429. uplink_seid = pf->veb[pf->lan_veb]->seid;
  9430. else
  9431. uplink_seid = pf->mac_seid;
  9432. if (pf->lan_vsi == I40E_NO_VSI)
  9433. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  9434. else if (reinit)
  9435. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  9436. if (!vsi) {
  9437. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  9438. i40e_fdir_teardown(pf);
  9439. return -EAGAIN;
  9440. }
  9441. } else {
  9442. /* force a reset of TC and queue layout configurations */
  9443. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  9444. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  9445. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  9446. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  9447. }
  9448. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  9449. i40e_fdir_sb_setup(pf);
  9450. /* Setup static PF queue filter control settings */
  9451. ret = i40e_setup_pf_filter_control(pf);
  9452. if (ret) {
  9453. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  9454. ret);
  9455. /* Failure here should not stop continuing other steps */
  9456. }
  9457. /* enable RSS in the HW, even for only one queue, as the stack can use
  9458. * the hash
  9459. */
  9460. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  9461. i40e_pf_config_rss(pf);
  9462. /* fill in link information and enable LSE reporting */
  9463. i40e_link_event(pf);
  9464. /* Initialize user-specific link properties */
  9465. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  9466. I40E_AQ_AN_COMPLETED) ? true : false);
  9467. i40e_ptp_init(pf);
  9468. return ret;
  9469. }
  9470. /**
  9471. * i40e_determine_queue_usage - Work out queue distribution
  9472. * @pf: board private structure
  9473. **/
  9474. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  9475. {
  9476. int queues_left;
  9477. pf->num_lan_qps = 0;
  9478. #ifdef I40E_FCOE
  9479. pf->num_fcoe_qps = 0;
  9480. #endif
  9481. /* Find the max queues to be put into basic use. We'll always be
  9482. * using TC0, whether or not DCB is running, and TC0 will get the
  9483. * big RSS set.
  9484. */
  9485. queues_left = pf->hw.func_caps.num_tx_qp;
  9486. if ((queues_left == 1) ||
  9487. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  9488. /* one qp for PF, no queues for anything else */
  9489. queues_left = 0;
  9490. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9491. /* make sure all the fancies are disabled */
  9492. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9493. I40E_FLAG_IWARP_ENABLED |
  9494. #ifdef I40E_FCOE
  9495. I40E_FLAG_FCOE_ENABLED |
  9496. #endif
  9497. I40E_FLAG_FD_SB_ENABLED |
  9498. I40E_FLAG_FD_ATR_ENABLED |
  9499. I40E_FLAG_DCB_CAPABLE |
  9500. I40E_FLAG_DCB_ENABLED |
  9501. I40E_FLAG_SRIOV_ENABLED |
  9502. I40E_FLAG_VMDQ_ENABLED);
  9503. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  9504. I40E_FLAG_FD_SB_ENABLED |
  9505. I40E_FLAG_FD_ATR_ENABLED |
  9506. I40E_FLAG_DCB_CAPABLE))) {
  9507. /* one qp for PF */
  9508. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9509. queues_left -= pf->num_lan_qps;
  9510. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9511. I40E_FLAG_IWARP_ENABLED |
  9512. #ifdef I40E_FCOE
  9513. I40E_FLAG_FCOE_ENABLED |
  9514. #endif
  9515. I40E_FLAG_FD_SB_ENABLED |
  9516. I40E_FLAG_FD_ATR_ENABLED |
  9517. I40E_FLAG_DCB_ENABLED |
  9518. I40E_FLAG_VMDQ_ENABLED);
  9519. } else {
  9520. /* Not enough queues for all TCs */
  9521. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  9522. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  9523. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  9524. I40E_FLAG_DCB_ENABLED);
  9525. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  9526. }
  9527. pf->num_lan_qps = max_t(int, pf->rss_size_max,
  9528. num_online_cpus());
  9529. pf->num_lan_qps = min_t(int, pf->num_lan_qps,
  9530. pf->hw.func_caps.num_tx_qp);
  9531. queues_left -= pf->num_lan_qps;
  9532. }
  9533. #ifdef I40E_FCOE
  9534. if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
  9535. if (I40E_DEFAULT_FCOE <= queues_left) {
  9536. pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
  9537. } else if (I40E_MINIMUM_FCOE <= queues_left) {
  9538. pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
  9539. } else {
  9540. pf->num_fcoe_qps = 0;
  9541. pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
  9542. dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
  9543. }
  9544. queues_left -= pf->num_fcoe_qps;
  9545. }
  9546. #endif
  9547. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9548. if (queues_left > 1) {
  9549. queues_left -= 1; /* save 1 queue for FD */
  9550. } else {
  9551. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9552. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  9553. }
  9554. }
  9555. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9556. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  9557. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  9558. (queues_left / pf->num_vf_qps));
  9559. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  9560. }
  9561. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9562. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  9563. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  9564. (queues_left / pf->num_vmdq_qps));
  9565. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  9566. }
  9567. pf->queues_left = queues_left;
  9568. dev_dbg(&pf->pdev->dev,
  9569. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  9570. pf->hw.func_caps.num_tx_qp,
  9571. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  9572. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  9573. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  9574. queues_left);
  9575. #ifdef I40E_FCOE
  9576. dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
  9577. #endif
  9578. }
  9579. /**
  9580. * i40e_setup_pf_filter_control - Setup PF static filter control
  9581. * @pf: PF to be setup
  9582. *
  9583. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  9584. * settings. If PE/FCoE are enabled then it will also set the per PF
  9585. * based filter sizes required for them. It also enables Flow director,
  9586. * ethertype and macvlan type filter settings for the pf.
  9587. *
  9588. * Returns 0 on success, negative on failure
  9589. **/
  9590. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  9591. {
  9592. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  9593. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  9594. /* Flow Director is enabled */
  9595. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  9596. settings->enable_fdir = true;
  9597. /* Ethtype and MACVLAN filters enabled for PF */
  9598. settings->enable_ethtype = true;
  9599. settings->enable_macvlan = true;
  9600. if (i40e_set_filter_control(&pf->hw, settings))
  9601. return -ENOENT;
  9602. return 0;
  9603. }
  9604. #define INFO_STRING_LEN 255
  9605. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  9606. static void i40e_print_features(struct i40e_pf *pf)
  9607. {
  9608. struct i40e_hw *hw = &pf->hw;
  9609. char *buf;
  9610. int i;
  9611. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  9612. if (!buf)
  9613. return;
  9614. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  9615. #ifdef CONFIG_PCI_IOV
  9616. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  9617. #endif
  9618. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  9619. pf->hw.func_caps.num_vsis,
  9620. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  9621. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  9622. i += snprintf(&buf[i], REMAIN(i), " RSS");
  9623. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  9624. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  9625. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9626. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  9627. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  9628. }
  9629. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  9630. i += snprintf(&buf[i], REMAIN(i), " DCB");
  9631. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  9632. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  9633. if (pf->flags & I40E_FLAG_PTP)
  9634. i += snprintf(&buf[i], REMAIN(i), " PTP");
  9635. #ifdef I40E_FCOE
  9636. if (pf->flags & I40E_FLAG_FCOE_ENABLED)
  9637. i += snprintf(&buf[i], REMAIN(i), " FCOE");
  9638. #endif
  9639. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  9640. i += snprintf(&buf[i], REMAIN(i), " VEB");
  9641. else
  9642. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  9643. dev_info(&pf->pdev->dev, "%s\n", buf);
  9644. kfree(buf);
  9645. WARN_ON(i > INFO_STRING_LEN);
  9646. }
  9647. /**
  9648. * i40e_get_platform_mac_addr - get platform-specific MAC address
  9649. *
  9650. * @pdev: PCI device information struct
  9651. * @pf: board private structure
  9652. *
  9653. * Look up the MAC address in Open Firmware on systems that support it,
  9654. * and use IDPROM on SPARC if no OF address is found. On return, the
  9655. * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
  9656. * has been selected.
  9657. **/
  9658. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  9659. {
  9660. pf->flags &= ~I40E_FLAG_PF_MAC;
  9661. if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  9662. pf->flags |= I40E_FLAG_PF_MAC;
  9663. }
  9664. /**
  9665. * i40e_probe - Device initialization routine
  9666. * @pdev: PCI device information struct
  9667. * @ent: entry in i40e_pci_tbl
  9668. *
  9669. * i40e_probe initializes a PF identified by a pci_dev structure.
  9670. * The OS initialization, configuring of the PF private structure,
  9671. * and a hardware reset occur.
  9672. *
  9673. * Returns 0 on success, negative on failure
  9674. **/
  9675. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  9676. {
  9677. struct i40e_aq_get_phy_abilities_resp abilities;
  9678. struct i40e_pf *pf;
  9679. struct i40e_hw *hw;
  9680. static u16 pfs_found;
  9681. u16 wol_nvm_bits;
  9682. u16 link_status;
  9683. int err;
  9684. u32 val;
  9685. u32 i;
  9686. u8 set_fc_aq_fail;
  9687. err = pci_enable_device_mem(pdev);
  9688. if (err)
  9689. return err;
  9690. /* set up for high or low dma */
  9691. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  9692. if (err) {
  9693. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  9694. if (err) {
  9695. dev_err(&pdev->dev,
  9696. "DMA configuration failed: 0x%x\n", err);
  9697. goto err_dma;
  9698. }
  9699. }
  9700. /* set up pci connections */
  9701. err = pci_request_mem_regions(pdev, i40e_driver_name);
  9702. if (err) {
  9703. dev_info(&pdev->dev,
  9704. "pci_request_selected_regions failed %d\n", err);
  9705. goto err_pci_reg;
  9706. }
  9707. pci_enable_pcie_error_reporting(pdev);
  9708. pci_set_master(pdev);
  9709. /* Now that we have a PCI connection, we need to do the
  9710. * low level device setup. This is primarily setting up
  9711. * the Admin Queue structures and then querying for the
  9712. * device's current profile information.
  9713. */
  9714. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  9715. if (!pf) {
  9716. err = -ENOMEM;
  9717. goto err_pf_alloc;
  9718. }
  9719. pf->next_vsi = 0;
  9720. pf->pdev = pdev;
  9721. set_bit(__I40E_DOWN, &pf->state);
  9722. hw = &pf->hw;
  9723. hw->back = pf;
  9724. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  9725. I40E_MAX_CSR_SPACE);
  9726. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  9727. if (!hw->hw_addr) {
  9728. err = -EIO;
  9729. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  9730. (unsigned int)pci_resource_start(pdev, 0),
  9731. pf->ioremap_len, err);
  9732. goto err_ioremap;
  9733. }
  9734. hw->vendor_id = pdev->vendor;
  9735. hw->device_id = pdev->device;
  9736. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  9737. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  9738. hw->subsystem_device_id = pdev->subsystem_device;
  9739. hw->bus.device = PCI_SLOT(pdev->devfn);
  9740. hw->bus.func = PCI_FUNC(pdev->devfn);
  9741. hw->bus.bus_id = pdev->bus->number;
  9742. pf->instance = pfs_found;
  9743. /* set up the locks for the AQ, do this only once in probe
  9744. * and destroy them only once in remove
  9745. */
  9746. mutex_init(&hw->aq.asq_mutex);
  9747. mutex_init(&hw->aq.arq_mutex);
  9748. pf->msg_enable = netif_msg_init(debug,
  9749. NETIF_MSG_DRV |
  9750. NETIF_MSG_PROBE |
  9751. NETIF_MSG_LINK);
  9752. if (debug < -1)
  9753. pf->hw.debug_mask = debug;
  9754. /* do a special CORER for clearing PXE mode once at init */
  9755. if (hw->revision_id == 0 &&
  9756. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  9757. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  9758. i40e_flush(hw);
  9759. msleep(200);
  9760. pf->corer_count++;
  9761. i40e_clear_pxe_mode(hw);
  9762. }
  9763. /* Reset here to make sure all is clean and to define PF 'n' */
  9764. i40e_clear_hw(hw);
  9765. err = i40e_pf_reset(hw);
  9766. if (err) {
  9767. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  9768. goto err_pf_reset;
  9769. }
  9770. pf->pfr_count++;
  9771. hw->aq.num_arq_entries = I40E_AQ_LEN;
  9772. hw->aq.num_asq_entries = I40E_AQ_LEN;
  9773. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9774. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9775. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  9776. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  9777. "%s-%s:misc",
  9778. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  9779. err = i40e_init_shared_code(hw);
  9780. if (err) {
  9781. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  9782. err);
  9783. goto err_pf_reset;
  9784. }
  9785. /* set up a default setting for link flow control */
  9786. pf->hw.fc.requested_mode = I40E_FC_NONE;
  9787. err = i40e_init_adminq(hw);
  9788. if (err) {
  9789. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  9790. dev_info(&pdev->dev,
  9791. "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");
  9792. else
  9793. dev_info(&pdev->dev,
  9794. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  9795. goto err_pf_reset;
  9796. }
  9797. /* provide nvm, fw, api versions */
  9798. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  9799. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  9800. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  9801. i40e_nvm_version_str(hw));
  9802. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  9803. hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
  9804. dev_info(&pdev->dev,
  9805. "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");
  9806. else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
  9807. hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
  9808. dev_info(&pdev->dev,
  9809. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  9810. i40e_verify_eeprom(pf);
  9811. /* Rev 0 hardware was never productized */
  9812. if (hw->revision_id < 1)
  9813. 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");
  9814. i40e_clear_pxe_mode(hw);
  9815. err = i40e_get_capabilities(pf);
  9816. if (err)
  9817. goto err_adminq_setup;
  9818. err = i40e_sw_init(pf);
  9819. if (err) {
  9820. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  9821. goto err_sw_init;
  9822. }
  9823. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  9824. hw->func_caps.num_rx_qp,
  9825. pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
  9826. if (err) {
  9827. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  9828. goto err_init_lan_hmc;
  9829. }
  9830. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  9831. if (err) {
  9832. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  9833. err = -ENOENT;
  9834. goto err_configure_lan_hmc;
  9835. }
  9836. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  9837. * Ignore error return codes because if it was already disabled via
  9838. * hardware settings this will fail
  9839. */
  9840. if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
  9841. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  9842. i40e_aq_stop_lldp(hw, true, NULL);
  9843. }
  9844. i40e_get_mac_addr(hw, hw->mac.addr);
  9845. /* allow a platform config to override the HW addr */
  9846. i40e_get_platform_mac_addr(pdev, pf);
  9847. if (!is_valid_ether_addr(hw->mac.addr)) {
  9848. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  9849. err = -EIO;
  9850. goto err_mac_addr;
  9851. }
  9852. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  9853. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  9854. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  9855. if (is_valid_ether_addr(hw->mac.port_addr))
  9856. pf->flags |= I40E_FLAG_PORT_ID_VALID;
  9857. #ifdef I40E_FCOE
  9858. err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
  9859. if (err)
  9860. dev_info(&pdev->dev,
  9861. "(non-fatal) SAN MAC retrieval failed: %d\n", err);
  9862. if (!is_valid_ether_addr(hw->mac.san_addr)) {
  9863. dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
  9864. hw->mac.san_addr);
  9865. ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
  9866. }
  9867. dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
  9868. #endif /* I40E_FCOE */
  9869. pci_set_drvdata(pdev, pf);
  9870. pci_save_state(pdev);
  9871. #ifdef CONFIG_I40E_DCB
  9872. err = i40e_init_pf_dcb(pf);
  9873. if (err) {
  9874. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  9875. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  9876. /* Continue without DCB enabled */
  9877. }
  9878. #endif /* CONFIG_I40E_DCB */
  9879. /* set up periodic task facility */
  9880. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  9881. pf->service_timer_period = HZ;
  9882. INIT_WORK(&pf->service_task, i40e_service_task);
  9883. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  9884. pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
  9885. /* NVM bit on means WoL disabled for the port */
  9886. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  9887. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  9888. pf->wol_en = false;
  9889. else
  9890. pf->wol_en = true;
  9891. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  9892. /* set up the main switch operations */
  9893. i40e_determine_queue_usage(pf);
  9894. err = i40e_init_interrupt_scheme(pf);
  9895. if (err)
  9896. goto err_switch_setup;
  9897. /* The number of VSIs reported by the FW is the minimum guaranteed
  9898. * to us; HW supports far more and we share the remaining pool with
  9899. * the other PFs. We allocate space for more than the guarantee with
  9900. * the understanding that we might not get them all later.
  9901. */
  9902. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  9903. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  9904. else
  9905. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  9906. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  9907. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  9908. GFP_KERNEL);
  9909. if (!pf->vsi) {
  9910. err = -ENOMEM;
  9911. goto err_switch_setup;
  9912. }
  9913. #ifdef CONFIG_PCI_IOV
  9914. /* prep for VF support */
  9915. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9916. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9917. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9918. if (pci_num_vf(pdev))
  9919. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  9920. }
  9921. #endif
  9922. err = i40e_setup_pf_switch(pf, false);
  9923. if (err) {
  9924. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  9925. goto err_vsis;
  9926. }
  9927. /* Make sure flow control is set according to current settings */
  9928. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  9929. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  9930. dev_dbg(&pf->pdev->dev,
  9931. "Set fc with err %s aq_err %s on get_phy_cap\n",
  9932. i40e_stat_str(hw, err),
  9933. i40e_aq_str(hw, hw->aq.asq_last_status));
  9934. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  9935. dev_dbg(&pf->pdev->dev,
  9936. "Set fc with err %s aq_err %s on set_phy_config\n",
  9937. i40e_stat_str(hw, err),
  9938. i40e_aq_str(hw, hw->aq.asq_last_status));
  9939. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  9940. dev_dbg(&pf->pdev->dev,
  9941. "Set fc with err %s aq_err %s on get_link_info\n",
  9942. i40e_stat_str(hw, err),
  9943. i40e_aq_str(hw, hw->aq.asq_last_status));
  9944. /* if FDIR VSI was set up, start it now */
  9945. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9946. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  9947. i40e_vsi_open(pf->vsi[i]);
  9948. break;
  9949. }
  9950. }
  9951. /* The driver only wants link up/down and module qualification
  9952. * reports from firmware. Note the negative logic.
  9953. */
  9954. err = i40e_aq_set_phy_int_mask(&pf->hw,
  9955. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  9956. I40E_AQ_EVENT_MEDIA_NA |
  9957. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  9958. if (err)
  9959. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  9960. i40e_stat_str(&pf->hw, err),
  9961. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9962. /* Reconfigure hardware for allowing smaller MSS in the case
  9963. * of TSO, so that we avoid the MDD being fired and causing
  9964. * a reset in the case of small MSS+TSO.
  9965. */
  9966. val = rd32(hw, I40E_REG_MSS);
  9967. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  9968. val &= ~I40E_REG_MSS_MIN_MASK;
  9969. val |= I40E_64BYTE_MSS;
  9970. wr32(hw, I40E_REG_MSS, val);
  9971. }
  9972. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  9973. msleep(75);
  9974. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  9975. if (err)
  9976. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  9977. i40e_stat_str(&pf->hw, err),
  9978. i40e_aq_str(&pf->hw,
  9979. pf->hw.aq.asq_last_status));
  9980. }
  9981. /* The main driver is (mostly) up and happy. We need to set this state
  9982. * before setting up the misc vector or we get a race and the vector
  9983. * ends up disabled forever.
  9984. */
  9985. clear_bit(__I40E_DOWN, &pf->state);
  9986. /* In case of MSIX we are going to setup the misc vector right here
  9987. * to handle admin queue events etc. In case of legacy and MSI
  9988. * the misc functionality and queue processing is combined in
  9989. * the same vector and that gets setup at open.
  9990. */
  9991. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9992. err = i40e_setup_misc_vector(pf);
  9993. if (err) {
  9994. dev_info(&pdev->dev,
  9995. "setup of misc vector failed: %d\n", err);
  9996. goto err_vsis;
  9997. }
  9998. }
  9999. #ifdef CONFIG_PCI_IOV
  10000. /* prep for VF support */
  10001. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  10002. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  10003. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  10004. /* disable link interrupts for VFs */
  10005. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  10006. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  10007. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  10008. i40e_flush(hw);
  10009. if (pci_num_vf(pdev)) {
  10010. dev_info(&pdev->dev,
  10011. "Active VFs found, allocating resources.\n");
  10012. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  10013. if (err)
  10014. dev_info(&pdev->dev,
  10015. "Error %d allocating resources for existing VFs\n",
  10016. err);
  10017. }
  10018. }
  10019. #endif /* CONFIG_PCI_IOV */
  10020. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  10021. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  10022. pf->num_iwarp_msix,
  10023. I40E_IWARP_IRQ_PILE_ID);
  10024. if (pf->iwarp_base_vector < 0) {
  10025. dev_info(&pdev->dev,
  10026. "failed to get tracking for %d vectors for IWARP err=%d\n",
  10027. pf->num_iwarp_msix, pf->iwarp_base_vector);
  10028. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  10029. }
  10030. }
  10031. i40e_dbg_pf_init(pf);
  10032. /* tell the firmware that we're starting */
  10033. i40e_send_version(pf);
  10034. /* since everything's happy, start the service_task timer */
  10035. mod_timer(&pf->service_timer,
  10036. round_jiffies(jiffies + pf->service_timer_period));
  10037. /* add this PF to client device list and launch a client service task */
  10038. err = i40e_lan_add_device(pf);
  10039. if (err)
  10040. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  10041. err);
  10042. #ifdef I40E_FCOE
  10043. /* create FCoE interface */
  10044. i40e_fcoe_vsi_setup(pf);
  10045. #endif
  10046. #define PCI_SPEED_SIZE 8
  10047. #define PCI_WIDTH_SIZE 8
  10048. /* Devices on the IOSF bus do not have this information
  10049. * and will report PCI Gen 1 x 1 by default so don't bother
  10050. * checking them.
  10051. */
  10052. if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
  10053. char speed[PCI_SPEED_SIZE] = "Unknown";
  10054. char width[PCI_WIDTH_SIZE] = "Unknown";
  10055. /* Get the negotiated link width and speed from PCI config
  10056. * space
  10057. */
  10058. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  10059. &link_status);
  10060. i40e_set_pci_config_data(hw, link_status);
  10061. switch (hw->bus.speed) {
  10062. case i40e_bus_speed_8000:
  10063. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  10064. case i40e_bus_speed_5000:
  10065. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  10066. case i40e_bus_speed_2500:
  10067. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  10068. default:
  10069. break;
  10070. }
  10071. switch (hw->bus.width) {
  10072. case i40e_bus_width_pcie_x8:
  10073. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  10074. case i40e_bus_width_pcie_x4:
  10075. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  10076. case i40e_bus_width_pcie_x2:
  10077. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  10078. case i40e_bus_width_pcie_x1:
  10079. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  10080. default:
  10081. break;
  10082. }
  10083. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  10084. speed, width);
  10085. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  10086. hw->bus.speed < i40e_bus_speed_8000) {
  10087. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  10088. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  10089. }
  10090. }
  10091. /* get the requested speeds from the fw */
  10092. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  10093. if (err)
  10094. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  10095. i40e_stat_str(&pf->hw, err),
  10096. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10097. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  10098. /* get the supported phy types from the fw */
  10099. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  10100. if (err)
  10101. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  10102. i40e_stat_str(&pf->hw, err),
  10103. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10104. /* Add a filter to drop all Flow control frames from any VSI from being
  10105. * transmitted. By doing so we stop a malicious VF from sending out
  10106. * PAUSE or PFC frames and potentially controlling traffic for other
  10107. * PF/VF VSIs.
  10108. * The FW can still send Flow control frames if enabled.
  10109. */
  10110. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  10111. pf->main_vsi_seid);
  10112. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  10113. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  10114. pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
  10115. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  10116. pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
  10117. /* print a string summarizing features */
  10118. i40e_print_features(pf);
  10119. return 0;
  10120. /* Unwind what we've done if something failed in the setup */
  10121. err_vsis:
  10122. set_bit(__I40E_DOWN, &pf->state);
  10123. i40e_clear_interrupt_scheme(pf);
  10124. kfree(pf->vsi);
  10125. err_switch_setup:
  10126. i40e_reset_interrupt_capability(pf);
  10127. del_timer_sync(&pf->service_timer);
  10128. err_mac_addr:
  10129. err_configure_lan_hmc:
  10130. (void)i40e_shutdown_lan_hmc(hw);
  10131. err_init_lan_hmc:
  10132. kfree(pf->qp_pile);
  10133. err_sw_init:
  10134. err_adminq_setup:
  10135. err_pf_reset:
  10136. iounmap(hw->hw_addr);
  10137. err_ioremap:
  10138. kfree(pf);
  10139. err_pf_alloc:
  10140. pci_disable_pcie_error_reporting(pdev);
  10141. pci_release_mem_regions(pdev);
  10142. err_pci_reg:
  10143. err_dma:
  10144. pci_disable_device(pdev);
  10145. return err;
  10146. }
  10147. /**
  10148. * i40e_remove - Device removal routine
  10149. * @pdev: PCI device information struct
  10150. *
  10151. * i40e_remove is called by the PCI subsystem to alert the driver
  10152. * that is should release a PCI device. This could be caused by a
  10153. * Hot-Plug event, or because the driver is going to be removed from
  10154. * memory.
  10155. **/
  10156. static void i40e_remove(struct pci_dev *pdev)
  10157. {
  10158. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10159. struct i40e_hw *hw = &pf->hw;
  10160. i40e_status ret_code;
  10161. int i;
  10162. i40e_dbg_pf_exit(pf);
  10163. i40e_ptp_stop(pf);
  10164. /* Disable RSS in hw */
  10165. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  10166. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  10167. /* no more scheduling of any task */
  10168. set_bit(__I40E_SUSPENDED, &pf->state);
  10169. set_bit(__I40E_DOWN, &pf->state);
  10170. if (pf->service_timer.data)
  10171. del_timer_sync(&pf->service_timer);
  10172. if (pf->service_task.func)
  10173. cancel_work_sync(&pf->service_task);
  10174. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  10175. i40e_free_vfs(pf);
  10176. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  10177. }
  10178. i40e_fdir_teardown(pf);
  10179. /* If there is a switch structure or any orphans, remove them.
  10180. * This will leave only the PF's VSI remaining.
  10181. */
  10182. for (i = 0; i < I40E_MAX_VEB; i++) {
  10183. if (!pf->veb[i])
  10184. continue;
  10185. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  10186. pf->veb[i]->uplink_seid == 0)
  10187. i40e_switch_branch_release(pf->veb[i]);
  10188. }
  10189. /* Now we can shutdown the PF's VSI, just before we kill
  10190. * adminq and hmc.
  10191. */
  10192. if (pf->vsi[pf->lan_vsi])
  10193. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  10194. /* remove attached clients */
  10195. ret_code = i40e_lan_del_device(pf);
  10196. if (ret_code) {
  10197. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  10198. ret_code);
  10199. }
  10200. /* shutdown and destroy the HMC */
  10201. if (hw->hmc.hmc_obj) {
  10202. ret_code = i40e_shutdown_lan_hmc(hw);
  10203. if (ret_code)
  10204. dev_warn(&pdev->dev,
  10205. "Failed to destroy the HMC resources: %d\n",
  10206. ret_code);
  10207. }
  10208. /* shutdown the adminq */
  10209. i40e_shutdown_adminq(hw);
  10210. /* destroy the locks only once, here */
  10211. mutex_destroy(&hw->aq.arq_mutex);
  10212. mutex_destroy(&hw->aq.asq_mutex);
  10213. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  10214. i40e_clear_interrupt_scheme(pf);
  10215. for (i = 0; i < pf->num_alloc_vsi; i++) {
  10216. if (pf->vsi[i]) {
  10217. i40e_vsi_clear_rings(pf->vsi[i]);
  10218. i40e_vsi_clear(pf->vsi[i]);
  10219. pf->vsi[i] = NULL;
  10220. }
  10221. }
  10222. for (i = 0; i < I40E_MAX_VEB; i++) {
  10223. kfree(pf->veb[i]);
  10224. pf->veb[i] = NULL;
  10225. }
  10226. kfree(pf->qp_pile);
  10227. kfree(pf->vsi);
  10228. iounmap(hw->hw_addr);
  10229. kfree(pf);
  10230. pci_release_mem_regions(pdev);
  10231. pci_disable_pcie_error_reporting(pdev);
  10232. pci_disable_device(pdev);
  10233. }
  10234. /**
  10235. * i40e_pci_error_detected - warning that something funky happened in PCI land
  10236. * @pdev: PCI device information struct
  10237. *
  10238. * Called to warn that something happened and the error handling steps
  10239. * are in progress. Allows the driver to quiesce things, be ready for
  10240. * remediation.
  10241. **/
  10242. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  10243. enum pci_channel_state error)
  10244. {
  10245. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10246. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  10247. if (!pf) {
  10248. dev_info(&pdev->dev,
  10249. "Cannot recover - error happened during device probe\n");
  10250. return PCI_ERS_RESULT_DISCONNECT;
  10251. }
  10252. /* shutdown all operations */
  10253. if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
  10254. rtnl_lock();
  10255. i40e_prep_for_reset(pf);
  10256. rtnl_unlock();
  10257. }
  10258. /* Request a slot reset */
  10259. return PCI_ERS_RESULT_NEED_RESET;
  10260. }
  10261. /**
  10262. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  10263. * @pdev: PCI device information struct
  10264. *
  10265. * Called to find if the driver can work with the device now that
  10266. * the pci slot has been reset. If a basic connection seems good
  10267. * (registers are readable and have sane content) then return a
  10268. * happy little PCI_ERS_RESULT_xxx.
  10269. **/
  10270. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  10271. {
  10272. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10273. pci_ers_result_t result;
  10274. int err;
  10275. u32 reg;
  10276. dev_dbg(&pdev->dev, "%s\n", __func__);
  10277. if (pci_enable_device_mem(pdev)) {
  10278. dev_info(&pdev->dev,
  10279. "Cannot re-enable PCI device after reset.\n");
  10280. result = PCI_ERS_RESULT_DISCONNECT;
  10281. } else {
  10282. pci_set_master(pdev);
  10283. pci_restore_state(pdev);
  10284. pci_save_state(pdev);
  10285. pci_wake_from_d3(pdev, false);
  10286. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  10287. if (reg == 0)
  10288. result = PCI_ERS_RESULT_RECOVERED;
  10289. else
  10290. result = PCI_ERS_RESULT_DISCONNECT;
  10291. }
  10292. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  10293. if (err) {
  10294. dev_info(&pdev->dev,
  10295. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  10296. err);
  10297. /* non-fatal, continue */
  10298. }
  10299. return result;
  10300. }
  10301. /**
  10302. * i40e_pci_error_resume - restart operations after PCI error recovery
  10303. * @pdev: PCI device information struct
  10304. *
  10305. * Called to allow the driver to bring things back up after PCI error
  10306. * and/or reset recovery has finished.
  10307. **/
  10308. static void i40e_pci_error_resume(struct pci_dev *pdev)
  10309. {
  10310. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10311. dev_dbg(&pdev->dev, "%s\n", __func__);
  10312. if (test_bit(__I40E_SUSPENDED, &pf->state))
  10313. return;
  10314. rtnl_lock();
  10315. i40e_handle_reset_warning(pf);
  10316. rtnl_unlock();
  10317. }
  10318. /**
  10319. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  10320. * using the mac_address_write admin q function
  10321. * @pf: pointer to i40e_pf struct
  10322. **/
  10323. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  10324. {
  10325. struct i40e_hw *hw = &pf->hw;
  10326. i40e_status ret;
  10327. u8 mac_addr[6];
  10328. u16 flags = 0;
  10329. /* Get current MAC address in case it's an LAA */
  10330. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  10331. ether_addr_copy(mac_addr,
  10332. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  10333. } else {
  10334. dev_err(&pf->pdev->dev,
  10335. "Failed to retrieve MAC address; using default\n");
  10336. ether_addr_copy(mac_addr, hw->mac.addr);
  10337. }
  10338. /* The FW expects the mac address write cmd to first be called with
  10339. * one of these flags before calling it again with the multicast
  10340. * enable flags.
  10341. */
  10342. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  10343. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  10344. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  10345. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10346. if (ret) {
  10347. dev_err(&pf->pdev->dev,
  10348. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  10349. return;
  10350. }
  10351. flags = I40E_AQC_MC_MAG_EN
  10352. | I40E_AQC_WOL_PRESERVE_ON_PFR
  10353. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  10354. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10355. if (ret)
  10356. dev_err(&pf->pdev->dev,
  10357. "Failed to enable Multicast Magic Packet wake up\n");
  10358. }
  10359. /**
  10360. * i40e_shutdown - PCI callback for shutting down
  10361. * @pdev: PCI device information struct
  10362. **/
  10363. static void i40e_shutdown(struct pci_dev *pdev)
  10364. {
  10365. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10366. struct i40e_hw *hw = &pf->hw;
  10367. set_bit(__I40E_SUSPENDED, &pf->state);
  10368. set_bit(__I40E_DOWN, &pf->state);
  10369. rtnl_lock();
  10370. i40e_prep_for_reset(pf);
  10371. rtnl_unlock();
  10372. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10373. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10374. del_timer_sync(&pf->service_timer);
  10375. cancel_work_sync(&pf->service_task);
  10376. i40e_fdir_teardown(pf);
  10377. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10378. i40e_enable_mc_magic_wake(pf);
  10379. rtnl_lock();
  10380. i40e_prep_for_reset(pf);
  10381. rtnl_unlock();
  10382. wr32(hw, I40E_PFPM_APM,
  10383. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10384. wr32(hw, I40E_PFPM_WUFC,
  10385. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10386. i40e_clear_interrupt_scheme(pf);
  10387. if (system_state == SYSTEM_POWER_OFF) {
  10388. pci_wake_from_d3(pdev, pf->wol_en);
  10389. pci_set_power_state(pdev, PCI_D3hot);
  10390. }
  10391. }
  10392. #ifdef CONFIG_PM
  10393. /**
  10394. * i40e_suspend - PCI callback for moving to D3
  10395. * @pdev: PCI device information struct
  10396. **/
  10397. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  10398. {
  10399. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10400. struct i40e_hw *hw = &pf->hw;
  10401. int retval = 0;
  10402. set_bit(__I40E_SUSPENDED, &pf->state);
  10403. set_bit(__I40E_DOWN, &pf->state);
  10404. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10405. i40e_enable_mc_magic_wake(pf);
  10406. rtnl_lock();
  10407. i40e_prep_for_reset(pf);
  10408. rtnl_unlock();
  10409. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10410. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10411. i40e_stop_misc_vector(pf);
  10412. retval = pci_save_state(pdev);
  10413. if (retval)
  10414. return retval;
  10415. pci_wake_from_d3(pdev, pf->wol_en);
  10416. pci_set_power_state(pdev, PCI_D3hot);
  10417. return retval;
  10418. }
  10419. /**
  10420. * i40e_resume - PCI callback for waking up from D3
  10421. * @pdev: PCI device information struct
  10422. **/
  10423. static int i40e_resume(struct pci_dev *pdev)
  10424. {
  10425. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10426. u32 err;
  10427. pci_set_power_state(pdev, PCI_D0);
  10428. pci_restore_state(pdev);
  10429. /* pci_restore_state() clears dev->state_saves, so
  10430. * call pci_save_state() again to restore it.
  10431. */
  10432. pci_save_state(pdev);
  10433. err = pci_enable_device_mem(pdev);
  10434. if (err) {
  10435. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  10436. return err;
  10437. }
  10438. pci_set_master(pdev);
  10439. /* no wakeup events while running */
  10440. pci_wake_from_d3(pdev, false);
  10441. /* handling the reset will rebuild the device state */
  10442. if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
  10443. clear_bit(__I40E_DOWN, &pf->state);
  10444. rtnl_lock();
  10445. i40e_reset_and_rebuild(pf, false);
  10446. rtnl_unlock();
  10447. }
  10448. return 0;
  10449. }
  10450. #endif
  10451. static const struct pci_error_handlers i40e_err_handler = {
  10452. .error_detected = i40e_pci_error_detected,
  10453. .slot_reset = i40e_pci_error_slot_reset,
  10454. .resume = i40e_pci_error_resume,
  10455. };
  10456. static struct pci_driver i40e_driver = {
  10457. .name = i40e_driver_name,
  10458. .id_table = i40e_pci_tbl,
  10459. .probe = i40e_probe,
  10460. .remove = i40e_remove,
  10461. #ifdef CONFIG_PM
  10462. .suspend = i40e_suspend,
  10463. .resume = i40e_resume,
  10464. #endif
  10465. .shutdown = i40e_shutdown,
  10466. .err_handler = &i40e_err_handler,
  10467. .sriov_configure = i40e_pci_sriov_configure,
  10468. };
  10469. /**
  10470. * i40e_init_module - Driver registration routine
  10471. *
  10472. * i40e_init_module is the first routine called when the driver is
  10473. * loaded. All it does is register with the PCI subsystem.
  10474. **/
  10475. static int __init i40e_init_module(void)
  10476. {
  10477. pr_info("%s: %s - version %s\n", i40e_driver_name,
  10478. i40e_driver_string, i40e_driver_version_str);
  10479. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  10480. /* we will see if single thread per module is enough for now,
  10481. * it can't be any worse than using the system workqueue which
  10482. * was already single threaded
  10483. */
  10484. i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
  10485. i40e_driver_name);
  10486. if (!i40e_wq) {
  10487. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  10488. return -ENOMEM;
  10489. }
  10490. i40e_dbg_init();
  10491. return pci_register_driver(&i40e_driver);
  10492. }
  10493. module_init(i40e_init_module);
  10494. /**
  10495. * i40e_exit_module - Driver exit cleanup routine
  10496. *
  10497. * i40e_exit_module is called just before the driver is removed
  10498. * from memory.
  10499. **/
  10500. static void __exit i40e_exit_module(void)
  10501. {
  10502. pci_unregister_driver(&i40e_driver);
  10503. destroy_workqueue(i40e_wq);
  10504. i40e_dbg_exit();
  10505. }
  10506. module_exit(i40e_exit_module);