i40e_main.c 238 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2014 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. /* Local includes */
  27. #include "i40e.h"
  28. #include "i40e_diag.h"
  29. #ifdef CONFIG_I40E_VXLAN
  30. #include <net/vxlan.h>
  31. #endif
  32. const char i40e_driver_name[] = "i40e";
  33. static const char i40e_driver_string[] =
  34. "Intel(R) Ethernet Connection XL710 Network Driver";
  35. #define DRV_KERN "-k"
  36. #define DRV_VERSION_MAJOR 0
  37. #define DRV_VERSION_MINOR 4
  38. #define DRV_VERSION_BUILD 3
  39. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  40. __stringify(DRV_VERSION_MINOR) "." \
  41. __stringify(DRV_VERSION_BUILD) DRV_KERN
  42. const char i40e_driver_version_str[] = DRV_VERSION;
  43. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  44. /* a bit of forward declarations */
  45. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  46. static void i40e_handle_reset_warning(struct i40e_pf *pf);
  47. static int i40e_add_vsi(struct i40e_vsi *vsi);
  48. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  49. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  50. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  51. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  52. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  53. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  54. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  55. /* i40e_pci_tbl - PCI Device ID Table
  56. *
  57. * Last entry must be all 0s
  58. *
  59. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  60. * Class, Class Mask, private data (not used) }
  61. */
  62. static DEFINE_PCI_DEVICE_TABLE(i40e_pci_tbl) = {
  63. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X710), 0},
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_A), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_D), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  71. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  72. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  73. /* required last entry */
  74. {0, }
  75. };
  76. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  77. #define I40E_MAX_VF_COUNT 128
  78. static int debug = -1;
  79. module_param(debug, int, 0);
  80. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  81. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  82. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  83. MODULE_LICENSE("GPL");
  84. MODULE_VERSION(DRV_VERSION);
  85. /**
  86. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  87. * @hw: pointer to the HW structure
  88. * @mem: ptr to mem struct to fill out
  89. * @size: size of memory requested
  90. * @alignment: what to align the allocation to
  91. **/
  92. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  93. u64 size, u32 alignment)
  94. {
  95. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  96. mem->size = ALIGN(size, alignment);
  97. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  98. &mem->pa, GFP_KERNEL);
  99. if (!mem->va)
  100. return -ENOMEM;
  101. return 0;
  102. }
  103. /**
  104. * i40e_free_dma_mem_d - OS specific memory free for shared code
  105. * @hw: pointer to the HW structure
  106. * @mem: ptr to mem struct to free
  107. **/
  108. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  109. {
  110. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  111. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  112. mem->va = NULL;
  113. mem->pa = 0;
  114. mem->size = 0;
  115. return 0;
  116. }
  117. /**
  118. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  119. * @hw: pointer to the HW structure
  120. * @mem: ptr to mem struct to fill out
  121. * @size: size of memory requested
  122. **/
  123. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  124. u32 size)
  125. {
  126. mem->size = size;
  127. mem->va = kzalloc(size, GFP_KERNEL);
  128. if (!mem->va)
  129. return -ENOMEM;
  130. return 0;
  131. }
  132. /**
  133. * i40e_free_virt_mem_d - OS specific memory free for shared code
  134. * @hw: pointer to the HW structure
  135. * @mem: ptr to mem struct to free
  136. **/
  137. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  138. {
  139. /* it's ok to kfree a NULL pointer */
  140. kfree(mem->va);
  141. mem->va = NULL;
  142. mem->size = 0;
  143. return 0;
  144. }
  145. /**
  146. * i40e_get_lump - find a lump of free generic resource
  147. * @pf: board private structure
  148. * @pile: the pile of resource to search
  149. * @needed: the number of items needed
  150. * @id: an owner id to stick on the items assigned
  151. *
  152. * Returns the base item index of the lump, or negative for error
  153. *
  154. * The search_hint trick and lack of advanced fit-finding only work
  155. * because we're highly likely to have all the same size lump requests.
  156. * Linear search time and any fragmentation should be minimal.
  157. **/
  158. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  159. u16 needed, u16 id)
  160. {
  161. int ret = -ENOMEM;
  162. int i, j;
  163. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  164. dev_info(&pf->pdev->dev,
  165. "param err: pile=%p needed=%d id=0x%04x\n",
  166. pile, needed, id);
  167. return -EINVAL;
  168. }
  169. /* start the linear search with an imperfect hint */
  170. i = pile->search_hint;
  171. while (i < pile->num_entries) {
  172. /* skip already allocated entries */
  173. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  174. i++;
  175. continue;
  176. }
  177. /* do we have enough in this lump? */
  178. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  179. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  180. break;
  181. }
  182. if (j == needed) {
  183. /* there was enough, so assign it to the requestor */
  184. for (j = 0; j < needed; j++)
  185. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  186. ret = i;
  187. pile->search_hint = i + j;
  188. break;
  189. } else {
  190. /* not enough, so skip over it and continue looking */
  191. i += j;
  192. }
  193. }
  194. return ret;
  195. }
  196. /**
  197. * i40e_put_lump - return a lump of generic resource
  198. * @pile: the pile of resource to search
  199. * @index: the base item index
  200. * @id: the owner id of the items assigned
  201. *
  202. * Returns the count of items in the lump
  203. **/
  204. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  205. {
  206. int valid_id = (id | I40E_PILE_VALID_BIT);
  207. int count = 0;
  208. int i;
  209. if (!pile || index >= pile->num_entries)
  210. return -EINVAL;
  211. for (i = index;
  212. i < pile->num_entries && pile->list[i] == valid_id;
  213. i++) {
  214. pile->list[i] = 0;
  215. count++;
  216. }
  217. if (count && index < pile->search_hint)
  218. pile->search_hint = index;
  219. return count;
  220. }
  221. /**
  222. * i40e_service_event_schedule - Schedule the service task to wake up
  223. * @pf: board private structure
  224. *
  225. * If not already scheduled, this puts the task into the work queue
  226. **/
  227. static void i40e_service_event_schedule(struct i40e_pf *pf)
  228. {
  229. if (!test_bit(__I40E_DOWN, &pf->state) &&
  230. !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
  231. !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
  232. schedule_work(&pf->service_task);
  233. }
  234. /**
  235. * i40e_tx_timeout - Respond to a Tx Hang
  236. * @netdev: network interface device structure
  237. *
  238. * If any port has noticed a Tx timeout, it is likely that the whole
  239. * device is munged, not just the one netdev port, so go for the full
  240. * reset.
  241. **/
  242. static void i40e_tx_timeout(struct net_device *netdev)
  243. {
  244. struct i40e_netdev_priv *np = netdev_priv(netdev);
  245. struct i40e_vsi *vsi = np->vsi;
  246. struct i40e_pf *pf = vsi->back;
  247. pf->tx_timeout_count++;
  248. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  249. pf->tx_timeout_recovery_level = 0;
  250. pf->tx_timeout_last_recovery = jiffies;
  251. netdev_info(netdev, "tx_timeout recovery level %d\n",
  252. pf->tx_timeout_recovery_level);
  253. switch (pf->tx_timeout_recovery_level) {
  254. case 0:
  255. /* disable and re-enable queues for the VSI */
  256. if (in_interrupt()) {
  257. set_bit(__I40E_REINIT_REQUESTED, &pf->state);
  258. set_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  259. } else {
  260. i40e_vsi_reinit_locked(vsi);
  261. }
  262. break;
  263. case 1:
  264. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  265. break;
  266. case 2:
  267. set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  268. break;
  269. case 3:
  270. set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  271. break;
  272. default:
  273. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  274. set_bit(__I40E_DOWN, &vsi->state);
  275. i40e_down(vsi);
  276. break;
  277. }
  278. i40e_service_event_schedule(pf);
  279. pf->tx_timeout_recovery_level++;
  280. }
  281. /**
  282. * i40e_release_rx_desc - Store the new tail and head values
  283. * @rx_ring: ring to bump
  284. * @val: new head index
  285. **/
  286. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  287. {
  288. rx_ring->next_to_use = val;
  289. /* Force memory writes to complete before letting h/w
  290. * know there are new descriptors to fetch. (Only
  291. * applicable for weak-ordered memory model archs,
  292. * such as IA-64).
  293. */
  294. wmb();
  295. writel(val, rx_ring->tail);
  296. }
  297. /**
  298. * i40e_get_vsi_stats_struct - Get System Network Statistics
  299. * @vsi: the VSI we care about
  300. *
  301. * Returns the address of the device statistics structure.
  302. * The statistics are actually updated from the service task.
  303. **/
  304. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  305. {
  306. return &vsi->net_stats;
  307. }
  308. /**
  309. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  310. * @netdev: network interface device structure
  311. *
  312. * Returns the address of the device statistics structure.
  313. * The statistics are actually updated from the service task.
  314. **/
  315. static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
  316. struct net_device *netdev,
  317. struct rtnl_link_stats64 *stats)
  318. {
  319. struct i40e_netdev_priv *np = netdev_priv(netdev);
  320. struct i40e_ring *tx_ring, *rx_ring;
  321. struct i40e_vsi *vsi = np->vsi;
  322. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  323. int i;
  324. if (test_bit(__I40E_DOWN, &vsi->state))
  325. return stats;
  326. if (!vsi->tx_rings)
  327. return stats;
  328. rcu_read_lock();
  329. for (i = 0; i < vsi->num_queue_pairs; i++) {
  330. u64 bytes, packets;
  331. unsigned int start;
  332. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  333. if (!tx_ring)
  334. continue;
  335. do {
  336. start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
  337. packets = tx_ring->stats.packets;
  338. bytes = tx_ring->stats.bytes;
  339. } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
  340. stats->tx_packets += packets;
  341. stats->tx_bytes += bytes;
  342. rx_ring = &tx_ring[1];
  343. do {
  344. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  345. packets = rx_ring->stats.packets;
  346. bytes = rx_ring->stats.bytes;
  347. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  348. stats->rx_packets += packets;
  349. stats->rx_bytes += bytes;
  350. }
  351. rcu_read_unlock();
  352. /* following stats updated by i40e_watchdog_subtask() */
  353. stats->multicast = vsi_stats->multicast;
  354. stats->tx_errors = vsi_stats->tx_errors;
  355. stats->tx_dropped = vsi_stats->tx_dropped;
  356. stats->rx_errors = vsi_stats->rx_errors;
  357. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  358. stats->rx_length_errors = vsi_stats->rx_length_errors;
  359. return stats;
  360. }
  361. /**
  362. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  363. * @vsi: the VSI to have its stats reset
  364. **/
  365. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  366. {
  367. struct rtnl_link_stats64 *ns;
  368. int i;
  369. if (!vsi)
  370. return;
  371. ns = i40e_get_vsi_stats_struct(vsi);
  372. memset(ns, 0, sizeof(*ns));
  373. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  374. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  375. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  376. if (vsi->rx_rings && vsi->rx_rings[0]) {
  377. for (i = 0; i < vsi->num_queue_pairs; i++) {
  378. memset(&vsi->rx_rings[i]->stats, 0 ,
  379. sizeof(vsi->rx_rings[i]->stats));
  380. memset(&vsi->rx_rings[i]->rx_stats, 0 ,
  381. sizeof(vsi->rx_rings[i]->rx_stats));
  382. memset(&vsi->tx_rings[i]->stats, 0 ,
  383. sizeof(vsi->tx_rings[i]->stats));
  384. memset(&vsi->tx_rings[i]->tx_stats, 0,
  385. sizeof(vsi->tx_rings[i]->tx_stats));
  386. }
  387. }
  388. vsi->stat_offsets_loaded = false;
  389. }
  390. /**
  391. * i40e_pf_reset_stats - Reset all of the stats for the given pf
  392. * @pf: the PF to be reset
  393. **/
  394. void i40e_pf_reset_stats(struct i40e_pf *pf)
  395. {
  396. memset(&pf->stats, 0, sizeof(pf->stats));
  397. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  398. pf->stat_offsets_loaded = false;
  399. }
  400. /**
  401. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  402. * @hw: ptr to the hardware info
  403. * @hireg: the high 32 bit reg to read
  404. * @loreg: the low 32 bit reg to read
  405. * @offset_loaded: has the initial offset been loaded yet
  406. * @offset: ptr to current offset value
  407. * @stat: ptr to the stat
  408. *
  409. * Since the device stats are not reset at PFReset, they likely will not
  410. * be zeroed when the driver starts. We'll save the first values read
  411. * and use them as offsets to be subtracted from the raw values in order
  412. * to report stats that count from zero. In the process, we also manage
  413. * the potential roll-over.
  414. **/
  415. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  416. bool offset_loaded, u64 *offset, u64 *stat)
  417. {
  418. u64 new_data;
  419. if (hw->device_id == I40E_DEV_ID_QEMU) {
  420. new_data = rd32(hw, loreg);
  421. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  422. } else {
  423. new_data = rd64(hw, loreg);
  424. }
  425. if (!offset_loaded)
  426. *offset = new_data;
  427. if (likely(new_data >= *offset))
  428. *stat = new_data - *offset;
  429. else
  430. *stat = (new_data + ((u64)1 << 48)) - *offset;
  431. *stat &= 0xFFFFFFFFFFFFULL;
  432. }
  433. /**
  434. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  435. * @hw: ptr to the hardware info
  436. * @reg: the hw reg to read
  437. * @offset_loaded: has the initial offset been loaded yet
  438. * @offset: ptr to current offset value
  439. * @stat: ptr to the stat
  440. **/
  441. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  442. bool offset_loaded, u64 *offset, u64 *stat)
  443. {
  444. u32 new_data;
  445. new_data = rd32(hw, reg);
  446. if (!offset_loaded)
  447. *offset = new_data;
  448. if (likely(new_data >= *offset))
  449. *stat = (u32)(new_data - *offset);
  450. else
  451. *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
  452. }
  453. /**
  454. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  455. * @vsi: the VSI to be updated
  456. **/
  457. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  458. {
  459. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  460. struct i40e_pf *pf = vsi->back;
  461. struct i40e_hw *hw = &pf->hw;
  462. struct i40e_eth_stats *oes;
  463. struct i40e_eth_stats *es; /* device's eth stats */
  464. es = &vsi->eth_stats;
  465. oes = &vsi->eth_stats_offsets;
  466. /* Gather up the stats that the hw collects */
  467. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  468. vsi->stat_offsets_loaded,
  469. &oes->tx_errors, &es->tx_errors);
  470. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  471. vsi->stat_offsets_loaded,
  472. &oes->rx_discards, &es->rx_discards);
  473. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  474. vsi->stat_offsets_loaded,
  475. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  476. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  477. vsi->stat_offsets_loaded,
  478. &oes->tx_errors, &es->tx_errors);
  479. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  480. I40E_GLV_GORCL(stat_idx),
  481. vsi->stat_offsets_loaded,
  482. &oes->rx_bytes, &es->rx_bytes);
  483. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  484. I40E_GLV_UPRCL(stat_idx),
  485. vsi->stat_offsets_loaded,
  486. &oes->rx_unicast, &es->rx_unicast);
  487. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  488. I40E_GLV_MPRCL(stat_idx),
  489. vsi->stat_offsets_loaded,
  490. &oes->rx_multicast, &es->rx_multicast);
  491. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  492. I40E_GLV_BPRCL(stat_idx),
  493. vsi->stat_offsets_loaded,
  494. &oes->rx_broadcast, &es->rx_broadcast);
  495. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  496. I40E_GLV_GOTCL(stat_idx),
  497. vsi->stat_offsets_loaded,
  498. &oes->tx_bytes, &es->tx_bytes);
  499. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  500. I40E_GLV_UPTCL(stat_idx),
  501. vsi->stat_offsets_loaded,
  502. &oes->tx_unicast, &es->tx_unicast);
  503. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  504. I40E_GLV_MPTCL(stat_idx),
  505. vsi->stat_offsets_loaded,
  506. &oes->tx_multicast, &es->tx_multicast);
  507. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  508. I40E_GLV_BPTCL(stat_idx),
  509. vsi->stat_offsets_loaded,
  510. &oes->tx_broadcast, &es->tx_broadcast);
  511. vsi->stat_offsets_loaded = true;
  512. }
  513. /**
  514. * i40e_update_veb_stats - Update Switch component statistics
  515. * @veb: the VEB being updated
  516. **/
  517. static void i40e_update_veb_stats(struct i40e_veb *veb)
  518. {
  519. struct i40e_pf *pf = veb->pf;
  520. struct i40e_hw *hw = &pf->hw;
  521. struct i40e_eth_stats *oes;
  522. struct i40e_eth_stats *es; /* device's eth stats */
  523. int idx = 0;
  524. idx = veb->stats_idx;
  525. es = &veb->stats;
  526. oes = &veb->stats_offsets;
  527. /* Gather up the stats that the hw collects */
  528. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  529. veb->stat_offsets_loaded,
  530. &oes->tx_discards, &es->tx_discards);
  531. if (hw->revision_id > 0)
  532. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  533. veb->stat_offsets_loaded,
  534. &oes->rx_unknown_protocol,
  535. &es->rx_unknown_protocol);
  536. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  537. veb->stat_offsets_loaded,
  538. &oes->rx_bytes, &es->rx_bytes);
  539. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  540. veb->stat_offsets_loaded,
  541. &oes->rx_unicast, &es->rx_unicast);
  542. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  543. veb->stat_offsets_loaded,
  544. &oes->rx_multicast, &es->rx_multicast);
  545. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  546. veb->stat_offsets_loaded,
  547. &oes->rx_broadcast, &es->rx_broadcast);
  548. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  549. veb->stat_offsets_loaded,
  550. &oes->tx_bytes, &es->tx_bytes);
  551. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  552. veb->stat_offsets_loaded,
  553. &oes->tx_unicast, &es->tx_unicast);
  554. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  555. veb->stat_offsets_loaded,
  556. &oes->tx_multicast, &es->tx_multicast);
  557. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  558. veb->stat_offsets_loaded,
  559. &oes->tx_broadcast, &es->tx_broadcast);
  560. veb->stat_offsets_loaded = true;
  561. }
  562. /**
  563. * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
  564. * @pf: the corresponding PF
  565. *
  566. * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
  567. **/
  568. static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
  569. {
  570. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  571. struct i40e_hw_port_stats *nsd = &pf->stats;
  572. struct i40e_hw *hw = &pf->hw;
  573. u64 xoff = 0;
  574. u16 i, v;
  575. if ((hw->fc.current_mode != I40E_FC_FULL) &&
  576. (hw->fc.current_mode != I40E_FC_RX_PAUSE))
  577. return;
  578. xoff = nsd->link_xoff_rx;
  579. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  580. pf->stat_offsets_loaded,
  581. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  582. /* No new LFC xoff rx */
  583. if (!(nsd->link_xoff_rx - xoff))
  584. return;
  585. /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
  586. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  587. struct i40e_vsi *vsi = pf->vsi[v];
  588. if (!vsi)
  589. continue;
  590. for (i = 0; i < vsi->num_queue_pairs; i++) {
  591. struct i40e_ring *ring = vsi->tx_rings[i];
  592. clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
  593. }
  594. }
  595. }
  596. /**
  597. * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
  598. * @pf: the corresponding PF
  599. *
  600. * Update the Rx XOFF counter (PAUSE frames) in PFC mode
  601. **/
  602. static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
  603. {
  604. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  605. struct i40e_hw_port_stats *nsd = &pf->stats;
  606. bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
  607. struct i40e_dcbx_config *dcb_cfg;
  608. struct i40e_hw *hw = &pf->hw;
  609. u16 i, v;
  610. u8 tc;
  611. dcb_cfg = &hw->local_dcbx_config;
  612. /* See if DCB enabled with PFC TC */
  613. if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
  614. !(dcb_cfg->pfc.pfcenable)) {
  615. i40e_update_link_xoff_rx(pf);
  616. return;
  617. }
  618. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  619. u64 prio_xoff = nsd->priority_xoff_rx[i];
  620. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  621. pf->stat_offsets_loaded,
  622. &osd->priority_xoff_rx[i],
  623. &nsd->priority_xoff_rx[i]);
  624. /* No new PFC xoff rx */
  625. if (!(nsd->priority_xoff_rx[i] - prio_xoff))
  626. continue;
  627. /* Get the TC for given priority */
  628. tc = dcb_cfg->etscfg.prioritytable[i];
  629. xoff[tc] = true;
  630. }
  631. /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
  632. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  633. struct i40e_vsi *vsi = pf->vsi[v];
  634. if (!vsi)
  635. continue;
  636. for (i = 0; i < vsi->num_queue_pairs; i++) {
  637. struct i40e_ring *ring = vsi->tx_rings[i];
  638. tc = ring->dcb_tc;
  639. if (xoff[tc])
  640. clear_bit(__I40E_HANG_CHECK_ARMED,
  641. &ring->state);
  642. }
  643. }
  644. }
  645. /**
  646. * i40e_update_vsi_stats - Update the vsi statistics counters.
  647. * @vsi: the VSI to be updated
  648. *
  649. * There are a few instances where we store the same stat in a
  650. * couple of different structs. This is partly because we have
  651. * the netdev stats that need to be filled out, which is slightly
  652. * different from the "eth_stats" defined by the chip and used in
  653. * VF communications. We sort it out here.
  654. **/
  655. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  656. {
  657. struct i40e_pf *pf = vsi->back;
  658. struct rtnl_link_stats64 *ons;
  659. struct rtnl_link_stats64 *ns; /* netdev stats */
  660. struct i40e_eth_stats *oes;
  661. struct i40e_eth_stats *es; /* device's eth stats */
  662. u32 tx_restart, tx_busy;
  663. u32 rx_page, rx_buf;
  664. u64 rx_p, rx_b;
  665. u64 tx_p, tx_b;
  666. u16 q;
  667. if (test_bit(__I40E_DOWN, &vsi->state) ||
  668. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  669. return;
  670. ns = i40e_get_vsi_stats_struct(vsi);
  671. ons = &vsi->net_stats_offsets;
  672. es = &vsi->eth_stats;
  673. oes = &vsi->eth_stats_offsets;
  674. /* Gather up the netdev and vsi stats that the driver collects
  675. * on the fly during packet processing
  676. */
  677. rx_b = rx_p = 0;
  678. tx_b = tx_p = 0;
  679. tx_restart = tx_busy = 0;
  680. rx_page = 0;
  681. rx_buf = 0;
  682. rcu_read_lock();
  683. for (q = 0; q < vsi->num_queue_pairs; q++) {
  684. struct i40e_ring *p;
  685. u64 bytes, packets;
  686. unsigned int start;
  687. /* locate Tx ring */
  688. p = ACCESS_ONCE(vsi->tx_rings[q]);
  689. do {
  690. start = u64_stats_fetch_begin_irq(&p->syncp);
  691. packets = p->stats.packets;
  692. bytes = p->stats.bytes;
  693. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  694. tx_b += bytes;
  695. tx_p += packets;
  696. tx_restart += p->tx_stats.restart_queue;
  697. tx_busy += p->tx_stats.tx_busy;
  698. /* Rx queue is part of the same block as Tx queue */
  699. p = &p[1];
  700. do {
  701. start = u64_stats_fetch_begin_irq(&p->syncp);
  702. packets = p->stats.packets;
  703. bytes = p->stats.bytes;
  704. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  705. rx_b += bytes;
  706. rx_p += packets;
  707. rx_buf += p->rx_stats.alloc_buff_failed;
  708. rx_page += p->rx_stats.alloc_page_failed;
  709. }
  710. rcu_read_unlock();
  711. vsi->tx_restart = tx_restart;
  712. vsi->tx_busy = tx_busy;
  713. vsi->rx_page_failed = rx_page;
  714. vsi->rx_buf_failed = rx_buf;
  715. ns->rx_packets = rx_p;
  716. ns->rx_bytes = rx_b;
  717. ns->tx_packets = tx_p;
  718. ns->tx_bytes = tx_b;
  719. /* update netdev stats from eth stats */
  720. i40e_update_eth_stats(vsi);
  721. ons->tx_errors = oes->tx_errors;
  722. ns->tx_errors = es->tx_errors;
  723. ons->multicast = oes->rx_multicast;
  724. ns->multicast = es->rx_multicast;
  725. ons->rx_dropped = oes->rx_discards;
  726. ns->rx_dropped = es->rx_discards;
  727. ons->tx_dropped = oes->tx_discards;
  728. ns->tx_dropped = es->tx_discards;
  729. /* pull in a couple PF stats if this is the main vsi */
  730. if (vsi == pf->vsi[pf->lan_vsi]) {
  731. ns->rx_crc_errors = pf->stats.crc_errors;
  732. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  733. ns->rx_length_errors = pf->stats.rx_length_errors;
  734. }
  735. }
  736. /**
  737. * i40e_update_pf_stats - Update the pf statistics counters.
  738. * @pf: the PF to be updated
  739. **/
  740. static void i40e_update_pf_stats(struct i40e_pf *pf)
  741. {
  742. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  743. struct i40e_hw_port_stats *nsd = &pf->stats;
  744. struct i40e_hw *hw = &pf->hw;
  745. u32 val;
  746. int i;
  747. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  748. I40E_GLPRT_GORCL(hw->port),
  749. pf->stat_offsets_loaded,
  750. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  751. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  752. I40E_GLPRT_GOTCL(hw->port),
  753. pf->stat_offsets_loaded,
  754. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  755. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  756. pf->stat_offsets_loaded,
  757. &osd->eth.rx_discards,
  758. &nsd->eth.rx_discards);
  759. i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
  760. pf->stat_offsets_loaded,
  761. &osd->eth.tx_discards,
  762. &nsd->eth.tx_discards);
  763. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  764. I40E_GLPRT_UPRCL(hw->port),
  765. pf->stat_offsets_loaded,
  766. &osd->eth.rx_unicast,
  767. &nsd->eth.rx_unicast);
  768. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  769. I40E_GLPRT_MPRCL(hw->port),
  770. pf->stat_offsets_loaded,
  771. &osd->eth.rx_multicast,
  772. &nsd->eth.rx_multicast);
  773. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  774. I40E_GLPRT_BPRCL(hw->port),
  775. pf->stat_offsets_loaded,
  776. &osd->eth.rx_broadcast,
  777. &nsd->eth.rx_broadcast);
  778. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  779. I40E_GLPRT_UPTCL(hw->port),
  780. pf->stat_offsets_loaded,
  781. &osd->eth.tx_unicast,
  782. &nsd->eth.tx_unicast);
  783. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  784. I40E_GLPRT_MPTCL(hw->port),
  785. pf->stat_offsets_loaded,
  786. &osd->eth.tx_multicast,
  787. &nsd->eth.tx_multicast);
  788. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  789. I40E_GLPRT_BPTCL(hw->port),
  790. pf->stat_offsets_loaded,
  791. &osd->eth.tx_broadcast,
  792. &nsd->eth.tx_broadcast);
  793. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  794. pf->stat_offsets_loaded,
  795. &osd->tx_dropped_link_down,
  796. &nsd->tx_dropped_link_down);
  797. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  798. pf->stat_offsets_loaded,
  799. &osd->crc_errors, &nsd->crc_errors);
  800. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  801. pf->stat_offsets_loaded,
  802. &osd->illegal_bytes, &nsd->illegal_bytes);
  803. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  804. pf->stat_offsets_loaded,
  805. &osd->mac_local_faults,
  806. &nsd->mac_local_faults);
  807. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  808. pf->stat_offsets_loaded,
  809. &osd->mac_remote_faults,
  810. &nsd->mac_remote_faults);
  811. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  812. pf->stat_offsets_loaded,
  813. &osd->rx_length_errors,
  814. &nsd->rx_length_errors);
  815. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  816. pf->stat_offsets_loaded,
  817. &osd->link_xon_rx, &nsd->link_xon_rx);
  818. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  819. pf->stat_offsets_loaded,
  820. &osd->link_xon_tx, &nsd->link_xon_tx);
  821. i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
  822. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  823. pf->stat_offsets_loaded,
  824. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  825. for (i = 0; i < 8; i++) {
  826. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  827. pf->stat_offsets_loaded,
  828. &osd->priority_xon_rx[i],
  829. &nsd->priority_xon_rx[i]);
  830. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  831. pf->stat_offsets_loaded,
  832. &osd->priority_xon_tx[i],
  833. &nsd->priority_xon_tx[i]);
  834. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  835. pf->stat_offsets_loaded,
  836. &osd->priority_xoff_tx[i],
  837. &nsd->priority_xoff_tx[i]);
  838. i40e_stat_update32(hw,
  839. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  840. pf->stat_offsets_loaded,
  841. &osd->priority_xon_2_xoff[i],
  842. &nsd->priority_xon_2_xoff[i]);
  843. }
  844. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  845. I40E_GLPRT_PRC64L(hw->port),
  846. pf->stat_offsets_loaded,
  847. &osd->rx_size_64, &nsd->rx_size_64);
  848. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  849. I40E_GLPRT_PRC127L(hw->port),
  850. pf->stat_offsets_loaded,
  851. &osd->rx_size_127, &nsd->rx_size_127);
  852. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  853. I40E_GLPRT_PRC255L(hw->port),
  854. pf->stat_offsets_loaded,
  855. &osd->rx_size_255, &nsd->rx_size_255);
  856. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  857. I40E_GLPRT_PRC511L(hw->port),
  858. pf->stat_offsets_loaded,
  859. &osd->rx_size_511, &nsd->rx_size_511);
  860. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  861. I40E_GLPRT_PRC1023L(hw->port),
  862. pf->stat_offsets_loaded,
  863. &osd->rx_size_1023, &nsd->rx_size_1023);
  864. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  865. I40E_GLPRT_PRC1522L(hw->port),
  866. pf->stat_offsets_loaded,
  867. &osd->rx_size_1522, &nsd->rx_size_1522);
  868. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  869. I40E_GLPRT_PRC9522L(hw->port),
  870. pf->stat_offsets_loaded,
  871. &osd->rx_size_big, &nsd->rx_size_big);
  872. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  873. I40E_GLPRT_PTC64L(hw->port),
  874. pf->stat_offsets_loaded,
  875. &osd->tx_size_64, &nsd->tx_size_64);
  876. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  877. I40E_GLPRT_PTC127L(hw->port),
  878. pf->stat_offsets_loaded,
  879. &osd->tx_size_127, &nsd->tx_size_127);
  880. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  881. I40E_GLPRT_PTC255L(hw->port),
  882. pf->stat_offsets_loaded,
  883. &osd->tx_size_255, &nsd->tx_size_255);
  884. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  885. I40E_GLPRT_PTC511L(hw->port),
  886. pf->stat_offsets_loaded,
  887. &osd->tx_size_511, &nsd->tx_size_511);
  888. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  889. I40E_GLPRT_PTC1023L(hw->port),
  890. pf->stat_offsets_loaded,
  891. &osd->tx_size_1023, &nsd->tx_size_1023);
  892. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  893. I40E_GLPRT_PTC1522L(hw->port),
  894. pf->stat_offsets_loaded,
  895. &osd->tx_size_1522, &nsd->tx_size_1522);
  896. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  897. I40E_GLPRT_PTC9522L(hw->port),
  898. pf->stat_offsets_loaded,
  899. &osd->tx_size_big, &nsd->tx_size_big);
  900. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  901. pf->stat_offsets_loaded,
  902. &osd->rx_undersize, &nsd->rx_undersize);
  903. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  904. pf->stat_offsets_loaded,
  905. &osd->rx_fragments, &nsd->rx_fragments);
  906. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  907. pf->stat_offsets_loaded,
  908. &osd->rx_oversize, &nsd->rx_oversize);
  909. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  910. pf->stat_offsets_loaded,
  911. &osd->rx_jabber, &nsd->rx_jabber);
  912. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  913. nsd->tx_lpi_status =
  914. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  915. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  916. nsd->rx_lpi_status =
  917. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  918. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  919. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  920. pf->stat_offsets_loaded,
  921. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  922. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  923. pf->stat_offsets_loaded,
  924. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  925. pf->stat_offsets_loaded = true;
  926. }
  927. /**
  928. * i40e_update_stats - Update the various statistics counters.
  929. * @vsi: the VSI to be updated
  930. *
  931. * Update the various stats for this VSI and its related entities.
  932. **/
  933. void i40e_update_stats(struct i40e_vsi *vsi)
  934. {
  935. struct i40e_pf *pf = vsi->back;
  936. if (vsi == pf->vsi[pf->lan_vsi])
  937. i40e_update_pf_stats(pf);
  938. i40e_update_vsi_stats(vsi);
  939. }
  940. /**
  941. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  942. * @vsi: the VSI to be searched
  943. * @macaddr: the MAC address
  944. * @vlan: the vlan
  945. * @is_vf: make sure its a vf filter, else doesn't matter
  946. * @is_netdev: make sure its a netdev filter, else doesn't matter
  947. *
  948. * Returns ptr to the filter object or NULL
  949. **/
  950. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  951. u8 *macaddr, s16 vlan,
  952. bool is_vf, bool is_netdev)
  953. {
  954. struct i40e_mac_filter *f;
  955. if (!vsi || !macaddr)
  956. return NULL;
  957. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  958. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  959. (vlan == f->vlan) &&
  960. (!is_vf || f->is_vf) &&
  961. (!is_netdev || f->is_netdev))
  962. return f;
  963. }
  964. return NULL;
  965. }
  966. /**
  967. * i40e_find_mac - Find a mac addr in the macvlan filters list
  968. * @vsi: the VSI to be searched
  969. * @macaddr: the MAC address we are searching for
  970. * @is_vf: make sure its a vf filter, else doesn't matter
  971. * @is_netdev: make sure its a netdev filter, else doesn't matter
  972. *
  973. * Returns the first filter with the provided MAC address or NULL if
  974. * MAC address was not found
  975. **/
  976. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
  977. bool is_vf, bool is_netdev)
  978. {
  979. struct i40e_mac_filter *f;
  980. if (!vsi || !macaddr)
  981. return NULL;
  982. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  983. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  984. (!is_vf || f->is_vf) &&
  985. (!is_netdev || f->is_netdev))
  986. return f;
  987. }
  988. return NULL;
  989. }
  990. /**
  991. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  992. * @vsi: the VSI to be searched
  993. *
  994. * Returns true if VSI is in vlan mode or false otherwise
  995. **/
  996. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  997. {
  998. struct i40e_mac_filter *f;
  999. /* Only -1 for all the filters denotes not in vlan mode
  1000. * so we have to go through all the list in order to make sure
  1001. */
  1002. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1003. if (f->vlan >= 0)
  1004. return true;
  1005. }
  1006. return false;
  1007. }
  1008. /**
  1009. * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
  1010. * @vsi: the VSI to be searched
  1011. * @macaddr: the mac address to be filtered
  1012. * @is_vf: true if it is a vf
  1013. * @is_netdev: true if it is a netdev
  1014. *
  1015. * Goes through all the macvlan filters and adds a
  1016. * macvlan filter for each unique vlan that already exists
  1017. *
  1018. * Returns first filter found on success, else NULL
  1019. **/
  1020. struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
  1021. bool is_vf, bool is_netdev)
  1022. {
  1023. struct i40e_mac_filter *f;
  1024. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1025. if (!i40e_find_filter(vsi, macaddr, f->vlan,
  1026. is_vf, is_netdev)) {
  1027. if (!i40e_add_filter(vsi, macaddr, f->vlan,
  1028. is_vf, is_netdev))
  1029. return NULL;
  1030. }
  1031. }
  1032. return list_first_entry_or_null(&vsi->mac_filter_list,
  1033. struct i40e_mac_filter, list);
  1034. }
  1035. /**
  1036. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1037. * @vsi: the VSI to be searched
  1038. * @macaddr: the MAC address
  1039. * @vlan: the vlan
  1040. * @is_vf: make sure its a vf filter, else doesn't matter
  1041. * @is_netdev: make sure its a netdev filter, else doesn't matter
  1042. *
  1043. * Returns ptr to the filter object or NULL when no memory available.
  1044. **/
  1045. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1046. u8 *macaddr, s16 vlan,
  1047. bool is_vf, bool is_netdev)
  1048. {
  1049. struct i40e_mac_filter *f;
  1050. if (!vsi || !macaddr)
  1051. return NULL;
  1052. f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
  1053. if (!f) {
  1054. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1055. if (!f)
  1056. goto add_filter_out;
  1057. memcpy(f->macaddr, macaddr, ETH_ALEN);
  1058. f->vlan = vlan;
  1059. f->changed = true;
  1060. INIT_LIST_HEAD(&f->list);
  1061. list_add(&f->list, &vsi->mac_filter_list);
  1062. }
  1063. /* increment counter and add a new flag if needed */
  1064. if (is_vf) {
  1065. if (!f->is_vf) {
  1066. f->is_vf = true;
  1067. f->counter++;
  1068. }
  1069. } else if (is_netdev) {
  1070. if (!f->is_netdev) {
  1071. f->is_netdev = true;
  1072. f->counter++;
  1073. }
  1074. } else {
  1075. f->counter++;
  1076. }
  1077. /* changed tells sync_filters_subtask to
  1078. * push the filter down to the firmware
  1079. */
  1080. if (f->changed) {
  1081. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1082. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1083. }
  1084. add_filter_out:
  1085. return f;
  1086. }
  1087. /**
  1088. * i40e_del_filter - Remove a mac/vlan filter from the VSI
  1089. * @vsi: the VSI to be searched
  1090. * @macaddr: the MAC address
  1091. * @vlan: the vlan
  1092. * @is_vf: make sure it's a vf filter, else doesn't matter
  1093. * @is_netdev: make sure it's a netdev filter, else doesn't matter
  1094. **/
  1095. void i40e_del_filter(struct i40e_vsi *vsi,
  1096. u8 *macaddr, s16 vlan,
  1097. bool is_vf, bool is_netdev)
  1098. {
  1099. struct i40e_mac_filter *f;
  1100. if (!vsi || !macaddr)
  1101. return;
  1102. f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
  1103. if (!f || f->counter == 0)
  1104. return;
  1105. if (is_vf) {
  1106. if (f->is_vf) {
  1107. f->is_vf = false;
  1108. f->counter--;
  1109. }
  1110. } else if (is_netdev) {
  1111. if (f->is_netdev) {
  1112. f->is_netdev = false;
  1113. f->counter--;
  1114. }
  1115. } else {
  1116. /* make sure we don't remove a filter in use by vf or netdev */
  1117. int min_f = 0;
  1118. min_f += (f->is_vf ? 1 : 0);
  1119. min_f += (f->is_netdev ? 1 : 0);
  1120. if (f->counter > min_f)
  1121. f->counter--;
  1122. }
  1123. /* counter == 0 tells sync_filters_subtask to
  1124. * remove the filter from the firmware's list
  1125. */
  1126. if (f->counter == 0) {
  1127. f->changed = true;
  1128. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1129. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1130. }
  1131. }
  1132. /**
  1133. * i40e_set_mac - NDO callback to set mac address
  1134. * @netdev: network interface device structure
  1135. * @p: pointer to an address structure
  1136. *
  1137. * Returns 0 on success, negative on failure
  1138. **/
  1139. static int i40e_set_mac(struct net_device *netdev, void *p)
  1140. {
  1141. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1142. struct i40e_vsi *vsi = np->vsi;
  1143. struct sockaddr *addr = p;
  1144. struct i40e_mac_filter *f;
  1145. if (!is_valid_ether_addr(addr->sa_data))
  1146. return -EADDRNOTAVAIL;
  1147. netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
  1148. if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
  1149. return 0;
  1150. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1151. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1152. return -EADDRNOTAVAIL;
  1153. if (vsi->type == I40E_VSI_MAIN) {
  1154. i40e_status ret;
  1155. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1156. I40E_AQC_WRITE_TYPE_LAA_ONLY,
  1157. addr->sa_data, NULL);
  1158. if (ret) {
  1159. netdev_info(netdev,
  1160. "Addr change for Main VSI failed: %d\n",
  1161. ret);
  1162. return -EADDRNOTAVAIL;
  1163. }
  1164. memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
  1165. }
  1166. /* In order to be sure to not drop any packets, add the new address
  1167. * then delete the old one.
  1168. */
  1169. f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
  1170. if (!f)
  1171. return -ENOMEM;
  1172. i40e_sync_vsi_filters(vsi);
  1173. i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
  1174. i40e_sync_vsi_filters(vsi);
  1175. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1176. return 0;
  1177. }
  1178. /**
  1179. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1180. * @vsi: the VSI being setup
  1181. * @ctxt: VSI context structure
  1182. * @enabled_tc: Enabled TCs bitmap
  1183. * @is_add: True if called before Add VSI
  1184. *
  1185. * Setup VSI queue mapping for enabled traffic classes.
  1186. **/
  1187. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1188. struct i40e_vsi_context *ctxt,
  1189. u8 enabled_tc,
  1190. bool is_add)
  1191. {
  1192. struct i40e_pf *pf = vsi->back;
  1193. u16 sections = 0;
  1194. u8 netdev_tc = 0;
  1195. u16 numtc = 0;
  1196. u16 qcount;
  1197. u8 offset;
  1198. u16 qmap;
  1199. int i;
  1200. u16 num_tc_qps = 0;
  1201. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1202. offset = 0;
  1203. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1204. /* Find numtc from enabled TC bitmap */
  1205. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1206. if (enabled_tc & (1 << i)) /* TC is enabled */
  1207. numtc++;
  1208. }
  1209. if (!numtc) {
  1210. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1211. numtc = 1;
  1212. }
  1213. } else {
  1214. /* At least TC0 is enabled in case of non-DCB case */
  1215. numtc = 1;
  1216. }
  1217. vsi->tc_config.numtc = numtc;
  1218. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1219. /* Number of queues per enabled TC */
  1220. num_tc_qps = rounddown_pow_of_two(vsi->alloc_queue_pairs/numtc);
  1221. num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
  1222. /* Setup queue offset/count for all TCs for given VSI */
  1223. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1224. /* See if the given TC is enabled for the given VSI */
  1225. if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
  1226. int pow, num_qps;
  1227. switch (vsi->type) {
  1228. case I40E_VSI_MAIN:
  1229. qcount = min_t(int, pf->rss_size, num_tc_qps);
  1230. break;
  1231. case I40E_VSI_FDIR:
  1232. case I40E_VSI_SRIOV:
  1233. case I40E_VSI_VMDQ2:
  1234. default:
  1235. qcount = num_tc_qps;
  1236. WARN_ON(i != 0);
  1237. break;
  1238. }
  1239. vsi->tc_config.tc_info[i].qoffset = offset;
  1240. vsi->tc_config.tc_info[i].qcount = qcount;
  1241. /* find the power-of-2 of the number of queue pairs */
  1242. num_qps = qcount;
  1243. pow = 0;
  1244. while (num_qps && ((1 << pow) < qcount)) {
  1245. pow++;
  1246. num_qps >>= 1;
  1247. }
  1248. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1249. qmap =
  1250. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1251. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1252. offset += qcount;
  1253. } else {
  1254. /* TC is not enabled so set the offset to
  1255. * default queue and allocate one queue
  1256. * for the given TC.
  1257. */
  1258. vsi->tc_config.tc_info[i].qoffset = 0;
  1259. vsi->tc_config.tc_info[i].qcount = 1;
  1260. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1261. qmap = 0;
  1262. }
  1263. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1264. }
  1265. /* Set actual Tx/Rx queue pairs */
  1266. vsi->num_queue_pairs = offset;
  1267. /* Scheduler section valid can only be set for ADD VSI */
  1268. if (is_add) {
  1269. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1270. ctxt->info.up_enable_bits = enabled_tc;
  1271. }
  1272. if (vsi->type == I40E_VSI_SRIOV) {
  1273. ctxt->info.mapping_flags |=
  1274. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1275. for (i = 0; i < vsi->num_queue_pairs; i++)
  1276. ctxt->info.queue_mapping[i] =
  1277. cpu_to_le16(vsi->base_queue + i);
  1278. } else {
  1279. ctxt->info.mapping_flags |=
  1280. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1281. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1282. }
  1283. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1284. }
  1285. /**
  1286. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1287. * @netdev: network interface device structure
  1288. **/
  1289. static void i40e_set_rx_mode(struct net_device *netdev)
  1290. {
  1291. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1292. struct i40e_mac_filter *f, *ftmp;
  1293. struct i40e_vsi *vsi = np->vsi;
  1294. struct netdev_hw_addr *uca;
  1295. struct netdev_hw_addr *mca;
  1296. struct netdev_hw_addr *ha;
  1297. /* add addr if not already in the filter list */
  1298. netdev_for_each_uc_addr(uca, netdev) {
  1299. if (!i40e_find_mac(vsi, uca->addr, false, true)) {
  1300. if (i40e_is_vsi_in_vlan(vsi))
  1301. i40e_put_mac_in_vlan(vsi, uca->addr,
  1302. false, true);
  1303. else
  1304. i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
  1305. false, true);
  1306. }
  1307. }
  1308. netdev_for_each_mc_addr(mca, netdev) {
  1309. if (!i40e_find_mac(vsi, mca->addr, false, true)) {
  1310. if (i40e_is_vsi_in_vlan(vsi))
  1311. i40e_put_mac_in_vlan(vsi, mca->addr,
  1312. false, true);
  1313. else
  1314. i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
  1315. false, true);
  1316. }
  1317. }
  1318. /* remove filter if not in netdev list */
  1319. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1320. bool found = false;
  1321. if (!f->is_netdev)
  1322. continue;
  1323. if (is_multicast_ether_addr(f->macaddr)) {
  1324. netdev_for_each_mc_addr(mca, netdev) {
  1325. if (ether_addr_equal(mca->addr, f->macaddr)) {
  1326. found = true;
  1327. break;
  1328. }
  1329. }
  1330. } else {
  1331. netdev_for_each_uc_addr(uca, netdev) {
  1332. if (ether_addr_equal(uca->addr, f->macaddr)) {
  1333. found = true;
  1334. break;
  1335. }
  1336. }
  1337. for_each_dev_addr(netdev, ha) {
  1338. if (ether_addr_equal(ha->addr, f->macaddr)) {
  1339. found = true;
  1340. break;
  1341. }
  1342. }
  1343. }
  1344. if (!found)
  1345. i40e_del_filter(
  1346. vsi, f->macaddr, I40E_VLAN_ANY, false, true);
  1347. }
  1348. /* check for other flag changes */
  1349. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1350. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1351. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1352. }
  1353. }
  1354. /**
  1355. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1356. * @vsi: ptr to the VSI
  1357. *
  1358. * Push any outstanding VSI filter changes through the AdminQ.
  1359. *
  1360. * Returns 0 or error value
  1361. **/
  1362. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1363. {
  1364. struct i40e_mac_filter *f, *ftmp;
  1365. bool promisc_forced_on = false;
  1366. bool add_happened = false;
  1367. int filter_list_len = 0;
  1368. u32 changed_flags = 0;
  1369. i40e_status aq_ret = 0;
  1370. struct i40e_pf *pf;
  1371. int num_add = 0;
  1372. int num_del = 0;
  1373. u16 cmd_flags;
  1374. /* empty array typed pointers, kcalloc later */
  1375. struct i40e_aqc_add_macvlan_element_data *add_list;
  1376. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1377. while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
  1378. usleep_range(1000, 2000);
  1379. pf = vsi->back;
  1380. if (vsi->netdev) {
  1381. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1382. vsi->current_netdev_flags = vsi->netdev->flags;
  1383. }
  1384. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1385. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1386. filter_list_len = pf->hw.aq.asq_buf_size /
  1387. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1388. del_list = kcalloc(filter_list_len,
  1389. sizeof(struct i40e_aqc_remove_macvlan_element_data),
  1390. GFP_KERNEL);
  1391. if (!del_list)
  1392. return -ENOMEM;
  1393. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1394. if (!f->changed)
  1395. continue;
  1396. if (f->counter != 0)
  1397. continue;
  1398. f->changed = false;
  1399. cmd_flags = 0;
  1400. /* add to delete list */
  1401. memcpy(del_list[num_del].mac_addr,
  1402. f->macaddr, ETH_ALEN);
  1403. del_list[num_del].vlan_tag =
  1404. cpu_to_le16((u16)(f->vlan ==
  1405. I40E_VLAN_ANY ? 0 : f->vlan));
  1406. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1407. del_list[num_del].flags = cmd_flags;
  1408. num_del++;
  1409. /* unlink from filter list */
  1410. list_del(&f->list);
  1411. kfree(f);
  1412. /* flush a full buffer */
  1413. if (num_del == filter_list_len) {
  1414. aq_ret = i40e_aq_remove_macvlan(&pf->hw,
  1415. vsi->seid, del_list, num_del,
  1416. NULL);
  1417. num_del = 0;
  1418. memset(del_list, 0, sizeof(*del_list));
  1419. if (aq_ret)
  1420. dev_info(&pf->pdev->dev,
  1421. "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
  1422. aq_ret,
  1423. pf->hw.aq.asq_last_status);
  1424. }
  1425. }
  1426. if (num_del) {
  1427. aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
  1428. del_list, num_del, NULL);
  1429. num_del = 0;
  1430. if (aq_ret)
  1431. dev_info(&pf->pdev->dev,
  1432. "ignoring delete macvlan error, err %d, aq_err %d\n",
  1433. aq_ret, pf->hw.aq.asq_last_status);
  1434. }
  1435. kfree(del_list);
  1436. del_list = NULL;
  1437. /* do all the adds now */
  1438. filter_list_len = pf->hw.aq.asq_buf_size /
  1439. sizeof(struct i40e_aqc_add_macvlan_element_data),
  1440. add_list = kcalloc(filter_list_len,
  1441. sizeof(struct i40e_aqc_add_macvlan_element_data),
  1442. GFP_KERNEL);
  1443. if (!add_list)
  1444. return -ENOMEM;
  1445. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1446. if (!f->changed)
  1447. continue;
  1448. if (f->counter == 0)
  1449. continue;
  1450. f->changed = false;
  1451. add_happened = true;
  1452. cmd_flags = 0;
  1453. /* add to add array */
  1454. memcpy(add_list[num_add].mac_addr,
  1455. f->macaddr, ETH_ALEN);
  1456. add_list[num_add].vlan_tag =
  1457. cpu_to_le16(
  1458. (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
  1459. add_list[num_add].queue_number = 0;
  1460. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  1461. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  1462. num_add++;
  1463. /* flush a full buffer */
  1464. if (num_add == filter_list_len) {
  1465. aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
  1466. add_list, num_add,
  1467. NULL);
  1468. num_add = 0;
  1469. if (aq_ret)
  1470. break;
  1471. memset(add_list, 0, sizeof(*add_list));
  1472. }
  1473. }
  1474. if (num_add) {
  1475. aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
  1476. add_list, num_add, NULL);
  1477. num_add = 0;
  1478. }
  1479. kfree(add_list);
  1480. add_list = NULL;
  1481. if (add_happened && (!aq_ret)) {
  1482. /* do nothing */;
  1483. } else if (add_happened && (aq_ret)) {
  1484. dev_info(&pf->pdev->dev,
  1485. "add filter failed, err %d, aq_err %d\n",
  1486. aq_ret, pf->hw.aq.asq_last_status);
  1487. if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
  1488. !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1489. &vsi->state)) {
  1490. promisc_forced_on = true;
  1491. set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1492. &vsi->state);
  1493. dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
  1494. }
  1495. }
  1496. }
  1497. /* check for changes in promiscuous modes */
  1498. if (changed_flags & IFF_ALLMULTI) {
  1499. bool cur_multipromisc;
  1500. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  1501. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  1502. vsi->seid,
  1503. cur_multipromisc,
  1504. NULL);
  1505. if (aq_ret)
  1506. dev_info(&pf->pdev->dev,
  1507. "set multi promisc failed, err %d, aq_err %d\n",
  1508. aq_ret, pf->hw.aq.asq_last_status);
  1509. }
  1510. if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
  1511. bool cur_promisc;
  1512. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  1513. test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1514. &vsi->state));
  1515. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
  1516. vsi->seid,
  1517. cur_promisc, NULL);
  1518. if (aq_ret)
  1519. dev_info(&pf->pdev->dev,
  1520. "set uni promisc failed, err %d, aq_err %d\n",
  1521. aq_ret, pf->hw.aq.asq_last_status);
  1522. aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
  1523. vsi->seid,
  1524. cur_promisc, NULL);
  1525. if (aq_ret)
  1526. dev_info(&pf->pdev->dev,
  1527. "set brdcast promisc failed, err %d, aq_err %d\n",
  1528. aq_ret, pf->hw.aq.asq_last_status);
  1529. }
  1530. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  1531. return 0;
  1532. }
  1533. /**
  1534. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  1535. * @pf: board private structure
  1536. **/
  1537. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  1538. {
  1539. int v;
  1540. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  1541. return;
  1542. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  1543. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  1544. if (pf->vsi[v] &&
  1545. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
  1546. i40e_sync_vsi_filters(pf->vsi[v]);
  1547. }
  1548. }
  1549. /**
  1550. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  1551. * @netdev: network interface device structure
  1552. * @new_mtu: new value for maximum frame size
  1553. *
  1554. * Returns 0 on success, negative on failure
  1555. **/
  1556. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  1557. {
  1558. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1559. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  1560. struct i40e_vsi *vsi = np->vsi;
  1561. /* MTU < 68 is an error and causes problems on some kernels */
  1562. if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
  1563. return -EINVAL;
  1564. netdev_info(netdev, "changing MTU from %d to %d\n",
  1565. netdev->mtu, new_mtu);
  1566. netdev->mtu = new_mtu;
  1567. if (netif_running(netdev))
  1568. i40e_vsi_reinit_locked(vsi);
  1569. return 0;
  1570. }
  1571. /**
  1572. * i40e_ioctl - Access the hwtstamp interface
  1573. * @netdev: network interface device structure
  1574. * @ifr: interface request data
  1575. * @cmd: ioctl command
  1576. **/
  1577. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1578. {
  1579. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1580. struct i40e_pf *pf = np->vsi->back;
  1581. switch (cmd) {
  1582. case SIOCGHWTSTAMP:
  1583. return i40e_ptp_get_ts_config(pf, ifr);
  1584. case SIOCSHWTSTAMP:
  1585. return i40e_ptp_set_ts_config(pf, ifr);
  1586. default:
  1587. return -EOPNOTSUPP;
  1588. }
  1589. }
  1590. /**
  1591. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  1592. * @vsi: the vsi being adjusted
  1593. **/
  1594. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  1595. {
  1596. struct i40e_vsi_context ctxt;
  1597. i40e_status ret;
  1598. if ((vsi->info.valid_sections &
  1599. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  1600. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  1601. return; /* already enabled */
  1602. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  1603. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  1604. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  1605. ctxt.seid = vsi->seid;
  1606. memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
  1607. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  1608. if (ret) {
  1609. dev_info(&vsi->back->pdev->dev,
  1610. "%s: update vsi failed, aq_err=%d\n",
  1611. __func__, vsi->back->hw.aq.asq_last_status);
  1612. }
  1613. }
  1614. /**
  1615. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  1616. * @vsi: the vsi being adjusted
  1617. **/
  1618. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  1619. {
  1620. struct i40e_vsi_context ctxt;
  1621. i40e_status ret;
  1622. if ((vsi->info.valid_sections &
  1623. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  1624. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  1625. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  1626. return; /* already disabled */
  1627. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  1628. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  1629. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  1630. ctxt.seid = vsi->seid;
  1631. memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
  1632. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  1633. if (ret) {
  1634. dev_info(&vsi->back->pdev->dev,
  1635. "%s: update vsi failed, aq_err=%d\n",
  1636. __func__, vsi->back->hw.aq.asq_last_status);
  1637. }
  1638. }
  1639. /**
  1640. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  1641. * @netdev: network interface to be adjusted
  1642. * @features: netdev features to test if VLAN offload is enabled or not
  1643. **/
  1644. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  1645. {
  1646. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1647. struct i40e_vsi *vsi = np->vsi;
  1648. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1649. i40e_vlan_stripping_enable(vsi);
  1650. else
  1651. i40e_vlan_stripping_disable(vsi);
  1652. }
  1653. /**
  1654. * i40e_vsi_add_vlan - Add vsi membership for given vlan
  1655. * @vsi: the vsi being configured
  1656. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  1657. **/
  1658. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
  1659. {
  1660. struct i40e_mac_filter *f, *add_f;
  1661. bool is_netdev, is_vf;
  1662. is_vf = (vsi->type == I40E_VSI_SRIOV);
  1663. is_netdev = !!(vsi->netdev);
  1664. if (is_netdev) {
  1665. add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
  1666. is_vf, is_netdev);
  1667. if (!add_f) {
  1668. dev_info(&vsi->back->pdev->dev,
  1669. "Could not add vlan filter %d for %pM\n",
  1670. vid, vsi->netdev->dev_addr);
  1671. return -ENOMEM;
  1672. }
  1673. }
  1674. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1675. add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
  1676. if (!add_f) {
  1677. dev_info(&vsi->back->pdev->dev,
  1678. "Could not add vlan filter %d for %pM\n",
  1679. vid, f->macaddr);
  1680. return -ENOMEM;
  1681. }
  1682. }
  1683. /* Now if we add a vlan tag, make sure to check if it is the first
  1684. * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
  1685. * with 0, so we now accept untagged and specified tagged traffic
  1686. * (and not any taged and untagged)
  1687. */
  1688. if (vid > 0) {
  1689. if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
  1690. I40E_VLAN_ANY,
  1691. is_vf, is_netdev)) {
  1692. i40e_del_filter(vsi, vsi->netdev->dev_addr,
  1693. I40E_VLAN_ANY, is_vf, is_netdev);
  1694. add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
  1695. is_vf, is_netdev);
  1696. if (!add_f) {
  1697. dev_info(&vsi->back->pdev->dev,
  1698. "Could not add filter 0 for %pM\n",
  1699. vsi->netdev->dev_addr);
  1700. return -ENOMEM;
  1701. }
  1702. }
  1703. }
  1704. /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
  1705. if (vid > 0 && !vsi->info.pvid) {
  1706. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1707. if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
  1708. is_vf, is_netdev)) {
  1709. i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
  1710. is_vf, is_netdev);
  1711. add_f = i40e_add_filter(vsi, f->macaddr,
  1712. 0, is_vf, is_netdev);
  1713. if (!add_f) {
  1714. dev_info(&vsi->back->pdev->dev,
  1715. "Could not add filter 0 for %pM\n",
  1716. f->macaddr);
  1717. return -ENOMEM;
  1718. }
  1719. }
  1720. }
  1721. }
  1722. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1723. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1724. return 0;
  1725. return i40e_sync_vsi_filters(vsi);
  1726. }
  1727. /**
  1728. * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
  1729. * @vsi: the vsi being configured
  1730. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  1731. *
  1732. * Return: 0 on success or negative otherwise
  1733. **/
  1734. int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
  1735. {
  1736. struct net_device *netdev = vsi->netdev;
  1737. struct i40e_mac_filter *f, *add_f;
  1738. bool is_vf, is_netdev;
  1739. int filter_count = 0;
  1740. is_vf = (vsi->type == I40E_VSI_SRIOV);
  1741. is_netdev = !!(netdev);
  1742. if (is_netdev)
  1743. i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
  1744. list_for_each_entry(f, &vsi->mac_filter_list, list)
  1745. i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
  1746. /* go through all the filters for this VSI and if there is only
  1747. * vid == 0 it means there are no other filters, so vid 0 must
  1748. * be replaced with -1. This signifies that we should from now
  1749. * on accept any traffic (with any tag present, or untagged)
  1750. */
  1751. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1752. if (is_netdev) {
  1753. if (f->vlan &&
  1754. ether_addr_equal(netdev->dev_addr, f->macaddr))
  1755. filter_count++;
  1756. }
  1757. if (f->vlan)
  1758. filter_count++;
  1759. }
  1760. if (!filter_count && is_netdev) {
  1761. i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
  1762. f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
  1763. is_vf, is_netdev);
  1764. if (!f) {
  1765. dev_info(&vsi->back->pdev->dev,
  1766. "Could not add filter %d for %pM\n",
  1767. I40E_VLAN_ANY, netdev->dev_addr);
  1768. return -ENOMEM;
  1769. }
  1770. }
  1771. if (!filter_count) {
  1772. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1773. i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
  1774. add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
  1775. is_vf, is_netdev);
  1776. if (!add_f) {
  1777. dev_info(&vsi->back->pdev->dev,
  1778. "Could not add filter %d for %pM\n",
  1779. I40E_VLAN_ANY, f->macaddr);
  1780. return -ENOMEM;
  1781. }
  1782. }
  1783. }
  1784. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1785. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1786. return 0;
  1787. return i40e_sync_vsi_filters(vsi);
  1788. }
  1789. /**
  1790. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  1791. * @netdev: network interface to be adjusted
  1792. * @vid: vlan id to be added
  1793. *
  1794. * net_device_ops implementation for adding vlan ids
  1795. **/
  1796. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  1797. __always_unused __be16 proto, u16 vid)
  1798. {
  1799. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1800. struct i40e_vsi *vsi = np->vsi;
  1801. int ret = 0;
  1802. if (vid > 4095)
  1803. return -EINVAL;
  1804. netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
  1805. /* If the network stack called us with vid = 0 then
  1806. * it is asking to receive priority tagged packets with
  1807. * vlan id 0. Our HW receives them by default when configured
  1808. * to receive untagged packets so there is no need to add an
  1809. * extra filter for vlan 0 tagged packets.
  1810. */
  1811. if (vid)
  1812. ret = i40e_vsi_add_vlan(vsi, vid);
  1813. if (!ret && (vid < VLAN_N_VID))
  1814. set_bit(vid, vsi->active_vlans);
  1815. return ret;
  1816. }
  1817. /**
  1818. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  1819. * @netdev: network interface to be adjusted
  1820. * @vid: vlan id to be removed
  1821. *
  1822. * net_device_ops implementation for removing vlan ids
  1823. **/
  1824. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  1825. __always_unused __be16 proto, u16 vid)
  1826. {
  1827. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1828. struct i40e_vsi *vsi = np->vsi;
  1829. netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
  1830. /* return code is ignored as there is nothing a user
  1831. * can do about failure to remove and a log message was
  1832. * already printed from the other function
  1833. */
  1834. i40e_vsi_kill_vlan(vsi, vid);
  1835. clear_bit(vid, vsi->active_vlans);
  1836. return 0;
  1837. }
  1838. /**
  1839. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  1840. * @vsi: the vsi being brought back up
  1841. **/
  1842. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  1843. {
  1844. u16 vid;
  1845. if (!vsi->netdev)
  1846. return;
  1847. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  1848. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  1849. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  1850. vid);
  1851. }
  1852. /**
  1853. * i40e_vsi_add_pvid - Add pvid for the VSI
  1854. * @vsi: the vsi being adjusted
  1855. * @vid: the vlan id to set as a PVID
  1856. **/
  1857. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  1858. {
  1859. struct i40e_vsi_context ctxt;
  1860. i40e_status aq_ret;
  1861. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  1862. vsi->info.pvid = cpu_to_le16(vid);
  1863. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  1864. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  1865. I40E_AQ_VSI_PVLAN_EMOD_STR;
  1866. ctxt.seid = vsi->seid;
  1867. memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
  1868. aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  1869. if (aq_ret) {
  1870. dev_info(&vsi->back->pdev->dev,
  1871. "%s: update vsi failed, aq_err=%d\n",
  1872. __func__, vsi->back->hw.aq.asq_last_status);
  1873. return -ENOENT;
  1874. }
  1875. return 0;
  1876. }
  1877. /**
  1878. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  1879. * @vsi: the vsi being adjusted
  1880. *
  1881. * Just use the vlan_rx_register() service to put it back to normal
  1882. **/
  1883. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  1884. {
  1885. i40e_vlan_stripping_disable(vsi);
  1886. vsi->info.pvid = 0;
  1887. }
  1888. /**
  1889. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  1890. * @vsi: ptr to the VSI
  1891. *
  1892. * If this function returns with an error, then it's possible one or
  1893. * more of the rings is populated (while the rest are not). It is the
  1894. * callers duty to clean those orphaned rings.
  1895. *
  1896. * Return 0 on success, negative on failure
  1897. **/
  1898. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  1899. {
  1900. int i, err = 0;
  1901. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  1902. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  1903. return err;
  1904. }
  1905. /**
  1906. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  1907. * @vsi: ptr to the VSI
  1908. *
  1909. * Free VSI's transmit software resources
  1910. **/
  1911. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  1912. {
  1913. int i;
  1914. if (!vsi->tx_rings)
  1915. return;
  1916. for (i = 0; i < vsi->num_queue_pairs; i++)
  1917. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  1918. i40e_free_tx_resources(vsi->tx_rings[i]);
  1919. }
  1920. /**
  1921. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  1922. * @vsi: ptr to the VSI
  1923. *
  1924. * If this function returns with an error, then it's possible one or
  1925. * more of the rings is populated (while the rest are not). It is the
  1926. * callers duty to clean those orphaned rings.
  1927. *
  1928. * Return 0 on success, negative on failure
  1929. **/
  1930. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  1931. {
  1932. int i, err = 0;
  1933. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  1934. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  1935. return err;
  1936. }
  1937. /**
  1938. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  1939. * @vsi: ptr to the VSI
  1940. *
  1941. * Free all receive software resources
  1942. **/
  1943. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  1944. {
  1945. int i;
  1946. if (!vsi->rx_rings)
  1947. return;
  1948. for (i = 0; i < vsi->num_queue_pairs; i++)
  1949. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  1950. i40e_free_rx_resources(vsi->rx_rings[i]);
  1951. }
  1952. /**
  1953. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  1954. * @ring: The Tx ring to configure
  1955. *
  1956. * Configure the Tx descriptor ring in the HMC context.
  1957. **/
  1958. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  1959. {
  1960. struct i40e_vsi *vsi = ring->vsi;
  1961. u16 pf_q = vsi->base_queue + ring->queue_index;
  1962. struct i40e_hw *hw = &vsi->back->hw;
  1963. struct i40e_hmc_obj_txq tx_ctx;
  1964. i40e_status err = 0;
  1965. u32 qtx_ctl = 0;
  1966. /* some ATR related tx ring init */
  1967. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  1968. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  1969. ring->atr_count = 0;
  1970. } else {
  1971. ring->atr_sample_rate = 0;
  1972. }
  1973. /* initialize XPS */
  1974. if (ring->q_vector && ring->netdev &&
  1975. vsi->tc_config.numtc <= 1 &&
  1976. !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
  1977. netif_set_xps_queue(ring->netdev,
  1978. &ring->q_vector->affinity_mask,
  1979. ring->queue_index);
  1980. /* clear the context structure first */
  1981. memset(&tx_ctx, 0, sizeof(tx_ctx));
  1982. tx_ctx.new_context = 1;
  1983. tx_ctx.base = (ring->dma / 128);
  1984. tx_ctx.qlen = ring->count;
  1985. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  1986. I40E_FLAG_FD_ATR_ENABLED));
  1987. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  1988. /* FDIR VSI tx ring can still use RS bit and writebacks */
  1989. if (vsi->type != I40E_VSI_FDIR)
  1990. tx_ctx.head_wb_ena = 1;
  1991. tx_ctx.head_wb_addr = ring->dma +
  1992. (ring->count * sizeof(struct i40e_tx_desc));
  1993. /* As part of VSI creation/update, FW allocates certain
  1994. * Tx arbitration queue sets for each TC enabled for
  1995. * the VSI. The FW returns the handles to these queue
  1996. * sets as part of the response buffer to Add VSI,
  1997. * Update VSI, etc. AQ commands. It is expected that
  1998. * these queue set handles be associated with the Tx
  1999. * queues by the driver as part of the TX queue context
  2000. * initialization. This has to be done regardless of
  2001. * DCB as by default everything is mapped to TC0.
  2002. */
  2003. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2004. tx_ctx.rdylist_act = 0;
  2005. /* clear the context in the HMC */
  2006. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2007. if (err) {
  2008. dev_info(&vsi->back->pdev->dev,
  2009. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2010. ring->queue_index, pf_q, err);
  2011. return -ENOMEM;
  2012. }
  2013. /* set the context in the HMC */
  2014. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2015. if (err) {
  2016. dev_info(&vsi->back->pdev->dev,
  2017. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2018. ring->queue_index, pf_q, err);
  2019. return -ENOMEM;
  2020. }
  2021. /* Now associate this queue with this PCI function */
  2022. if (vsi->type == I40E_VSI_VMDQ2)
  2023. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2024. else
  2025. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2026. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2027. I40E_QTX_CTL_PF_INDX_MASK);
  2028. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2029. i40e_flush(hw);
  2030. clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
  2031. /* cache tail off for easier writes later */
  2032. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2033. return 0;
  2034. }
  2035. /**
  2036. * i40e_configure_rx_ring - Configure a receive ring context
  2037. * @ring: The Rx ring to configure
  2038. *
  2039. * Configure the Rx descriptor ring in the HMC context.
  2040. **/
  2041. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2042. {
  2043. struct i40e_vsi *vsi = ring->vsi;
  2044. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2045. u16 pf_q = vsi->base_queue + ring->queue_index;
  2046. struct i40e_hw *hw = &vsi->back->hw;
  2047. struct i40e_hmc_obj_rxq rx_ctx;
  2048. i40e_status err = 0;
  2049. ring->state = 0;
  2050. /* clear the context structure first */
  2051. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2052. ring->rx_buf_len = vsi->rx_buf_len;
  2053. ring->rx_hdr_len = vsi->rx_hdr_len;
  2054. rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
  2055. rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
  2056. rx_ctx.base = (ring->dma / 128);
  2057. rx_ctx.qlen = ring->count;
  2058. if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
  2059. set_ring_16byte_desc_enabled(ring);
  2060. rx_ctx.dsize = 0;
  2061. } else {
  2062. rx_ctx.dsize = 1;
  2063. }
  2064. rx_ctx.dtype = vsi->dtype;
  2065. if (vsi->dtype) {
  2066. set_ring_ps_enabled(ring);
  2067. rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
  2068. I40E_RX_SPLIT_IP |
  2069. I40E_RX_SPLIT_TCP_UDP |
  2070. I40E_RX_SPLIT_SCTP;
  2071. } else {
  2072. rx_ctx.hsplit_0 = 0;
  2073. }
  2074. rx_ctx.rxmax = min_t(u16, vsi->max_frame,
  2075. (chain_len * ring->rx_buf_len));
  2076. rx_ctx.tphrdesc_ena = 1;
  2077. rx_ctx.tphwdesc_ena = 1;
  2078. rx_ctx.tphdata_ena = 1;
  2079. rx_ctx.tphhead_ena = 1;
  2080. if (hw->revision_id == 0)
  2081. rx_ctx.lrxqthresh = 0;
  2082. else
  2083. rx_ctx.lrxqthresh = 2;
  2084. rx_ctx.crcstrip = 1;
  2085. rx_ctx.l2tsel = 1;
  2086. rx_ctx.showiv = 1;
  2087. /* set the prefena field to 1 because the manual says to */
  2088. rx_ctx.prefena = 1;
  2089. /* clear the context in the HMC */
  2090. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2091. if (err) {
  2092. dev_info(&vsi->back->pdev->dev,
  2093. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2094. ring->queue_index, pf_q, err);
  2095. return -ENOMEM;
  2096. }
  2097. /* set the context in the HMC */
  2098. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2099. if (err) {
  2100. dev_info(&vsi->back->pdev->dev,
  2101. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2102. ring->queue_index, pf_q, err);
  2103. return -ENOMEM;
  2104. }
  2105. /* cache tail for quicker writes, and clear the reg before use */
  2106. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2107. writel(0, ring->tail);
  2108. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2109. return 0;
  2110. }
  2111. /**
  2112. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2113. * @vsi: VSI structure describing this set of rings and resources
  2114. *
  2115. * Configure the Tx VSI for operation.
  2116. **/
  2117. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2118. {
  2119. int err = 0;
  2120. u16 i;
  2121. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2122. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2123. return err;
  2124. }
  2125. /**
  2126. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2127. * @vsi: the VSI being configured
  2128. *
  2129. * Configure the Rx VSI for operation.
  2130. **/
  2131. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2132. {
  2133. int err = 0;
  2134. u16 i;
  2135. if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
  2136. vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
  2137. + ETH_FCS_LEN + VLAN_HLEN;
  2138. else
  2139. vsi->max_frame = I40E_RXBUFFER_2048;
  2140. /* figure out correct receive buffer length */
  2141. switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
  2142. I40E_FLAG_RX_PS_ENABLED)) {
  2143. case I40E_FLAG_RX_1BUF_ENABLED:
  2144. vsi->rx_hdr_len = 0;
  2145. vsi->rx_buf_len = vsi->max_frame;
  2146. vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
  2147. break;
  2148. case I40E_FLAG_RX_PS_ENABLED:
  2149. vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
  2150. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2151. vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
  2152. break;
  2153. default:
  2154. vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
  2155. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2156. vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
  2157. break;
  2158. }
  2159. /* round up for the chip's needs */
  2160. vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
  2161. (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
  2162. vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
  2163. (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
  2164. /* set up individual rings */
  2165. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2166. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2167. return err;
  2168. }
  2169. /**
  2170. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2171. * @vsi: ptr to the VSI
  2172. **/
  2173. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2174. {
  2175. struct i40e_ring *tx_ring, *rx_ring;
  2176. u16 qoffset, qcount;
  2177. int i, n;
  2178. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
  2179. return;
  2180. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2181. if (!(vsi->tc_config.enabled_tc & (1 << n)))
  2182. continue;
  2183. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2184. qcount = vsi->tc_config.tc_info[n].qcount;
  2185. for (i = qoffset; i < (qoffset + qcount); i++) {
  2186. rx_ring = vsi->rx_rings[i];
  2187. tx_ring = vsi->tx_rings[i];
  2188. rx_ring->dcb_tc = n;
  2189. tx_ring->dcb_tc = n;
  2190. }
  2191. }
  2192. }
  2193. /**
  2194. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2195. * @vsi: ptr to the VSI
  2196. **/
  2197. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2198. {
  2199. if (vsi->netdev)
  2200. i40e_set_rx_mode(vsi->netdev);
  2201. }
  2202. /**
  2203. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2204. * @vsi: Pointer to the targeted VSI
  2205. *
  2206. * This function replays the hlist on the hw where all the SB Flow Director
  2207. * filters were saved.
  2208. **/
  2209. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2210. {
  2211. struct i40e_fdir_filter *filter;
  2212. struct i40e_pf *pf = vsi->back;
  2213. struct hlist_node *node;
  2214. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2215. return;
  2216. hlist_for_each_entry_safe(filter, node,
  2217. &pf->fdir_filter_list, fdir_node) {
  2218. i40e_add_del_fdir(vsi, filter, true);
  2219. }
  2220. }
  2221. /**
  2222. * i40e_vsi_configure - Set up the VSI for action
  2223. * @vsi: the VSI being configured
  2224. **/
  2225. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2226. {
  2227. int err;
  2228. i40e_set_vsi_rx_mode(vsi);
  2229. i40e_restore_vlan(vsi);
  2230. i40e_vsi_config_dcb_rings(vsi);
  2231. err = i40e_vsi_configure_tx(vsi);
  2232. if (!err)
  2233. err = i40e_vsi_configure_rx(vsi);
  2234. return err;
  2235. }
  2236. /**
  2237. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2238. * @vsi: the VSI being configured
  2239. **/
  2240. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2241. {
  2242. struct i40e_pf *pf = vsi->back;
  2243. struct i40e_q_vector *q_vector;
  2244. struct i40e_hw *hw = &pf->hw;
  2245. u16 vector;
  2246. int i, q;
  2247. u32 val;
  2248. u32 qp;
  2249. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2250. * and PFINT_LNKLSTn registers, e.g.:
  2251. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2252. */
  2253. qp = vsi->base_queue;
  2254. vector = vsi->base_vector;
  2255. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2256. q_vector = vsi->q_vectors[i];
  2257. q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
  2258. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2259. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2260. q_vector->rx.itr);
  2261. q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
  2262. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2263. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2264. q_vector->tx.itr);
  2265. /* Linked list for the queuepairs assigned to this vector */
  2266. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2267. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2268. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2269. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2270. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2271. (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  2272. (I40E_QUEUE_TYPE_TX
  2273. << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2274. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2275. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2276. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2277. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2278. ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
  2279. (I40E_QUEUE_TYPE_RX
  2280. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2281. /* Terminate the linked list */
  2282. if (q == (q_vector->num_ringpairs - 1))
  2283. val |= (I40E_QUEUE_END_OF_LIST
  2284. << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2285. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2286. qp++;
  2287. }
  2288. }
  2289. i40e_flush(hw);
  2290. }
  2291. /**
  2292. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2293. * @hw: ptr to the hardware info
  2294. **/
  2295. static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
  2296. {
  2297. u32 val;
  2298. /* clear things first */
  2299. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  2300. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  2301. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  2302. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  2303. I40E_PFINT_ICR0_ENA_GRST_MASK |
  2304. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  2305. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  2306. I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
  2307. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  2308. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  2309. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2310. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  2311. /* SW_ITR_IDX = 0, but don't change INTENA */
  2312. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  2313. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  2314. /* OTHER_ITR_IDX = 0 */
  2315. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  2316. }
  2317. /**
  2318. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  2319. * @vsi: the VSI being configured
  2320. **/
  2321. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  2322. {
  2323. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  2324. struct i40e_pf *pf = vsi->back;
  2325. struct i40e_hw *hw = &pf->hw;
  2326. u32 val;
  2327. /* set the ITR configuration */
  2328. q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
  2329. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2330. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  2331. q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
  2332. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2333. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  2334. i40e_enable_misc_int_causes(hw);
  2335. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  2336. wr32(hw, I40E_PFINT_LNKLST0, 0);
  2337. /* Associate the queue pair to the vector and enable the queue int */
  2338. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2339. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2340. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2341. wr32(hw, I40E_QINT_RQCTL(0), val);
  2342. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2343. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2344. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2345. wr32(hw, I40E_QINT_TQCTL(0), val);
  2346. i40e_flush(hw);
  2347. }
  2348. /**
  2349. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  2350. * @pf: board private structure
  2351. **/
  2352. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  2353. {
  2354. struct i40e_hw *hw = &pf->hw;
  2355. wr32(hw, I40E_PFINT_DYN_CTL0,
  2356. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2357. i40e_flush(hw);
  2358. }
  2359. /**
  2360. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  2361. * @pf: board private structure
  2362. **/
  2363. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  2364. {
  2365. struct i40e_hw *hw = &pf->hw;
  2366. u32 val;
  2367. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  2368. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  2369. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  2370. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  2371. i40e_flush(hw);
  2372. }
  2373. /**
  2374. * i40e_irq_dynamic_enable - Enable default interrupt generation settings
  2375. * @vsi: pointer to a vsi
  2376. * @vector: enable a particular Hw Interrupt vector
  2377. **/
  2378. void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
  2379. {
  2380. struct i40e_pf *pf = vsi->back;
  2381. struct i40e_hw *hw = &pf->hw;
  2382. u32 val;
  2383. val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  2384. I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
  2385. (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2386. wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
  2387. /* skip the flush */
  2388. }
  2389. /**
  2390. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  2391. * @irq: interrupt number
  2392. * @data: pointer to a q_vector
  2393. **/
  2394. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  2395. {
  2396. struct i40e_q_vector *q_vector = data;
  2397. if (!q_vector->tx.ring && !q_vector->rx.ring)
  2398. return IRQ_HANDLED;
  2399. napi_schedule(&q_vector->napi);
  2400. return IRQ_HANDLED;
  2401. }
  2402. /**
  2403. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  2404. * @vsi: the VSI being configured
  2405. * @basename: name for the vector
  2406. *
  2407. * Allocates MSI-X vectors and requests interrupts from the kernel.
  2408. **/
  2409. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  2410. {
  2411. int q_vectors = vsi->num_q_vectors;
  2412. struct i40e_pf *pf = vsi->back;
  2413. int base = vsi->base_vector;
  2414. int rx_int_idx = 0;
  2415. int tx_int_idx = 0;
  2416. int vector, err;
  2417. for (vector = 0; vector < q_vectors; vector++) {
  2418. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  2419. if (q_vector->tx.ring && q_vector->rx.ring) {
  2420. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2421. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  2422. tx_int_idx++;
  2423. } else if (q_vector->rx.ring) {
  2424. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2425. "%s-%s-%d", basename, "rx", rx_int_idx++);
  2426. } else if (q_vector->tx.ring) {
  2427. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2428. "%s-%s-%d", basename, "tx", tx_int_idx++);
  2429. } else {
  2430. /* skip this unused q_vector */
  2431. continue;
  2432. }
  2433. err = request_irq(pf->msix_entries[base + vector].vector,
  2434. vsi->irq_handler,
  2435. 0,
  2436. q_vector->name,
  2437. q_vector);
  2438. if (err) {
  2439. dev_info(&pf->pdev->dev,
  2440. "%s: request_irq failed, error: %d\n",
  2441. __func__, err);
  2442. goto free_queue_irqs;
  2443. }
  2444. /* assign the mask for this irq */
  2445. irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
  2446. &q_vector->affinity_mask);
  2447. }
  2448. vsi->irqs_ready = true;
  2449. return 0;
  2450. free_queue_irqs:
  2451. while (vector) {
  2452. vector--;
  2453. irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
  2454. NULL);
  2455. free_irq(pf->msix_entries[base + vector].vector,
  2456. &(vsi->q_vectors[vector]));
  2457. }
  2458. return err;
  2459. }
  2460. /**
  2461. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  2462. * @vsi: the VSI being un-configured
  2463. **/
  2464. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  2465. {
  2466. struct i40e_pf *pf = vsi->back;
  2467. struct i40e_hw *hw = &pf->hw;
  2468. int base = vsi->base_vector;
  2469. int i;
  2470. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2471. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
  2472. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
  2473. }
  2474. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2475. for (i = vsi->base_vector;
  2476. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  2477. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  2478. i40e_flush(hw);
  2479. for (i = 0; i < vsi->num_q_vectors; i++)
  2480. synchronize_irq(pf->msix_entries[i + base].vector);
  2481. } else {
  2482. /* Legacy and MSI mode - this stops all interrupt handling */
  2483. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  2484. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  2485. i40e_flush(hw);
  2486. synchronize_irq(pf->pdev->irq);
  2487. }
  2488. }
  2489. /**
  2490. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  2491. * @vsi: the VSI being configured
  2492. **/
  2493. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  2494. {
  2495. struct i40e_pf *pf = vsi->back;
  2496. int i;
  2497. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2498. for (i = vsi->base_vector;
  2499. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  2500. i40e_irq_dynamic_enable(vsi, i);
  2501. } else {
  2502. i40e_irq_dynamic_enable_icr0(pf);
  2503. }
  2504. i40e_flush(&pf->hw);
  2505. return 0;
  2506. }
  2507. /**
  2508. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  2509. * @pf: board private structure
  2510. **/
  2511. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  2512. {
  2513. /* Disable ICR 0 */
  2514. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  2515. i40e_flush(&pf->hw);
  2516. }
  2517. /**
  2518. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  2519. * @irq: interrupt number
  2520. * @data: pointer to a q_vector
  2521. *
  2522. * This is the handler used for all MSI/Legacy interrupts, and deals
  2523. * with both queue and non-queue interrupts. This is also used in
  2524. * MSIX mode to handle the non-queue interrupts.
  2525. **/
  2526. static irqreturn_t i40e_intr(int irq, void *data)
  2527. {
  2528. struct i40e_pf *pf = (struct i40e_pf *)data;
  2529. struct i40e_hw *hw = &pf->hw;
  2530. irqreturn_t ret = IRQ_NONE;
  2531. u32 icr0, icr0_remaining;
  2532. u32 val, ena_mask;
  2533. icr0 = rd32(hw, I40E_PFINT_ICR0);
  2534. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  2535. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  2536. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  2537. goto enable_intr;
  2538. /* if interrupt but no bits showing, must be SWINT */
  2539. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  2540. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  2541. pf->sw_int_count++;
  2542. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  2543. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  2544. /* temporarily disable queue cause for NAPI processing */
  2545. u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
  2546. qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  2547. wr32(hw, I40E_QINT_RQCTL(0), qval);
  2548. qval = rd32(hw, I40E_QINT_TQCTL(0));
  2549. qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  2550. wr32(hw, I40E_QINT_TQCTL(0), qval);
  2551. if (!test_bit(__I40E_DOWN, &pf->state))
  2552. napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
  2553. }
  2554. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  2555. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2556. set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  2557. }
  2558. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  2559. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  2560. set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  2561. }
  2562. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  2563. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  2564. set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  2565. }
  2566. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  2567. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  2568. set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  2569. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  2570. val = rd32(hw, I40E_GLGEN_RSTAT);
  2571. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  2572. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  2573. if (val == I40E_RESET_CORER) {
  2574. pf->corer_count++;
  2575. } else if (val == I40E_RESET_GLOBR) {
  2576. pf->globr_count++;
  2577. } else if (val == I40E_RESET_EMPR) {
  2578. pf->empr_count++;
  2579. set_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
  2580. }
  2581. }
  2582. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  2583. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  2584. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  2585. }
  2586. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  2587. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  2588. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  2589. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  2590. i40e_ptp_tx_hwtstamp(pf);
  2591. }
  2592. }
  2593. /* If a critical error is pending we have no choice but to reset the
  2594. * device.
  2595. * Report and mask out any remaining unexpected interrupts.
  2596. */
  2597. icr0_remaining = icr0 & ena_mask;
  2598. if (icr0_remaining) {
  2599. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  2600. icr0_remaining);
  2601. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  2602. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  2603. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  2604. dev_info(&pf->pdev->dev, "device will be reset\n");
  2605. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  2606. i40e_service_event_schedule(pf);
  2607. }
  2608. ena_mask &= ~icr0_remaining;
  2609. }
  2610. ret = IRQ_HANDLED;
  2611. enable_intr:
  2612. /* re-enable interrupt causes */
  2613. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  2614. if (!test_bit(__I40E_DOWN, &pf->state)) {
  2615. i40e_service_event_schedule(pf);
  2616. i40e_irq_dynamic_enable_icr0(pf);
  2617. }
  2618. return ret;
  2619. }
  2620. /**
  2621. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  2622. * @tx_ring: tx ring to clean
  2623. * @budget: how many cleans we're allowed
  2624. *
  2625. * Returns true if there's any budget left (e.g. the clean is finished)
  2626. **/
  2627. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  2628. {
  2629. struct i40e_vsi *vsi = tx_ring->vsi;
  2630. u16 i = tx_ring->next_to_clean;
  2631. struct i40e_tx_buffer *tx_buf;
  2632. struct i40e_tx_desc *tx_desc;
  2633. tx_buf = &tx_ring->tx_bi[i];
  2634. tx_desc = I40E_TX_DESC(tx_ring, i);
  2635. i -= tx_ring->count;
  2636. do {
  2637. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  2638. /* if next_to_watch is not set then there is no work pending */
  2639. if (!eop_desc)
  2640. break;
  2641. /* prevent any other reads prior to eop_desc */
  2642. read_barrier_depends();
  2643. /* if the descriptor isn't done, no work yet to do */
  2644. if (!(eop_desc->cmd_type_offset_bsz &
  2645. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  2646. break;
  2647. /* clear next_to_watch to prevent false hangs */
  2648. tx_buf->next_to_watch = NULL;
  2649. /* unmap skb header data */
  2650. dma_unmap_single(tx_ring->dev,
  2651. dma_unmap_addr(tx_buf, dma),
  2652. dma_unmap_len(tx_buf, len),
  2653. DMA_TO_DEVICE);
  2654. dma_unmap_len_set(tx_buf, len, 0);
  2655. /* move to the next desc and buffer to clean */
  2656. tx_buf++;
  2657. tx_desc++;
  2658. i++;
  2659. if (unlikely(!i)) {
  2660. i -= tx_ring->count;
  2661. tx_buf = tx_ring->tx_bi;
  2662. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2663. }
  2664. /* update budget accounting */
  2665. budget--;
  2666. } while (likely(budget));
  2667. i += tx_ring->count;
  2668. tx_ring->next_to_clean = i;
  2669. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
  2670. i40e_irq_dynamic_enable(vsi,
  2671. tx_ring->q_vector->v_idx + vsi->base_vector);
  2672. }
  2673. return budget > 0;
  2674. }
  2675. /**
  2676. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  2677. * @irq: interrupt number
  2678. * @data: pointer to a q_vector
  2679. **/
  2680. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  2681. {
  2682. struct i40e_q_vector *q_vector = data;
  2683. struct i40e_vsi *vsi;
  2684. if (!q_vector->tx.ring)
  2685. return IRQ_HANDLED;
  2686. vsi = q_vector->tx.ring->vsi;
  2687. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  2688. return IRQ_HANDLED;
  2689. }
  2690. /**
  2691. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  2692. * @vsi: the VSI being configured
  2693. * @v_idx: vector index
  2694. * @qp_idx: queue pair index
  2695. **/
  2696. static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  2697. {
  2698. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  2699. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  2700. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  2701. tx_ring->q_vector = q_vector;
  2702. tx_ring->next = q_vector->tx.ring;
  2703. q_vector->tx.ring = tx_ring;
  2704. q_vector->tx.count++;
  2705. rx_ring->q_vector = q_vector;
  2706. rx_ring->next = q_vector->rx.ring;
  2707. q_vector->rx.ring = rx_ring;
  2708. q_vector->rx.count++;
  2709. }
  2710. /**
  2711. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  2712. * @vsi: the VSI being configured
  2713. *
  2714. * This function maps descriptor rings to the queue-specific vectors
  2715. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  2716. * one vector per queue pair, but on a constrained vector budget, we
  2717. * group the queue pairs as "efficiently" as possible.
  2718. **/
  2719. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  2720. {
  2721. int qp_remaining = vsi->num_queue_pairs;
  2722. int q_vectors = vsi->num_q_vectors;
  2723. int num_ringpairs;
  2724. int v_start = 0;
  2725. int qp_idx = 0;
  2726. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  2727. * group them so there are multiple queues per vector.
  2728. */
  2729. for (; v_start < q_vectors && qp_remaining; v_start++) {
  2730. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  2731. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  2732. q_vector->num_ringpairs = num_ringpairs;
  2733. q_vector->rx.count = 0;
  2734. q_vector->tx.count = 0;
  2735. q_vector->rx.ring = NULL;
  2736. q_vector->tx.ring = NULL;
  2737. while (num_ringpairs--) {
  2738. map_vector_to_qp(vsi, v_start, qp_idx);
  2739. qp_idx++;
  2740. qp_remaining--;
  2741. }
  2742. }
  2743. }
  2744. /**
  2745. * i40e_vsi_request_irq - Request IRQ from the OS
  2746. * @vsi: the VSI being configured
  2747. * @basename: name for the vector
  2748. **/
  2749. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  2750. {
  2751. struct i40e_pf *pf = vsi->back;
  2752. int err;
  2753. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  2754. err = i40e_vsi_request_irq_msix(vsi, basename);
  2755. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  2756. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  2757. pf->misc_int_name, pf);
  2758. else
  2759. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  2760. pf->misc_int_name, pf);
  2761. if (err)
  2762. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  2763. return err;
  2764. }
  2765. #ifdef CONFIG_NET_POLL_CONTROLLER
  2766. /**
  2767. * i40e_netpoll - A Polling 'interrupt'handler
  2768. * @netdev: network interface device structure
  2769. *
  2770. * This is used by netconsole to send skbs without having to re-enable
  2771. * interrupts. It's not called while the normal interrupt routine is executing.
  2772. **/
  2773. static void i40e_netpoll(struct net_device *netdev)
  2774. {
  2775. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2776. struct i40e_vsi *vsi = np->vsi;
  2777. struct i40e_pf *pf = vsi->back;
  2778. int i;
  2779. /* if interface is down do nothing */
  2780. if (test_bit(__I40E_DOWN, &vsi->state))
  2781. return;
  2782. pf->flags |= I40E_FLAG_IN_NETPOLL;
  2783. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2784. for (i = 0; i < vsi->num_q_vectors; i++)
  2785. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  2786. } else {
  2787. i40e_intr(pf->pdev->irq, netdev);
  2788. }
  2789. pf->flags &= ~I40E_FLAG_IN_NETPOLL;
  2790. }
  2791. #endif
  2792. /**
  2793. * i40e_vsi_control_tx - Start or stop a VSI's rings
  2794. * @vsi: the VSI being configured
  2795. * @enable: start or stop the rings
  2796. **/
  2797. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  2798. {
  2799. struct i40e_pf *pf = vsi->back;
  2800. struct i40e_hw *hw = &pf->hw;
  2801. int i, j, pf_q;
  2802. u32 tx_reg;
  2803. pf_q = vsi->base_queue;
  2804. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  2805. /* warn the TX unit of coming changes */
  2806. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  2807. if (!enable)
  2808. udelay(10);
  2809. for (j = 0; j < 50; j++) {
  2810. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  2811. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  2812. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  2813. break;
  2814. usleep_range(1000, 2000);
  2815. }
  2816. /* Skip if the queue is already in the requested state */
  2817. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  2818. continue;
  2819. /* turn on/off the queue */
  2820. if (enable) {
  2821. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  2822. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  2823. } else {
  2824. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  2825. }
  2826. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  2827. /* wait for the change to finish */
  2828. for (j = 0; j < 10; j++) {
  2829. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  2830. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  2831. break;
  2832. udelay(10);
  2833. }
  2834. if (j >= 10) {
  2835. dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
  2836. pf_q, (enable ? "en" : "dis"));
  2837. return -ETIMEDOUT;
  2838. }
  2839. }
  2840. if (hw->revision_id == 0)
  2841. mdelay(50);
  2842. return 0;
  2843. }
  2844. /**
  2845. * i40e_vsi_control_rx - Start or stop a VSI's rings
  2846. * @vsi: the VSI being configured
  2847. * @enable: start or stop the rings
  2848. **/
  2849. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  2850. {
  2851. struct i40e_pf *pf = vsi->back;
  2852. struct i40e_hw *hw = &pf->hw;
  2853. int i, j, pf_q;
  2854. u32 rx_reg;
  2855. pf_q = vsi->base_queue;
  2856. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  2857. for (j = 0; j < 50; j++) {
  2858. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  2859. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  2860. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  2861. break;
  2862. usleep_range(1000, 2000);
  2863. }
  2864. /* Skip if the queue is already in the requested state */
  2865. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  2866. continue;
  2867. /* turn on/off the queue */
  2868. if (enable)
  2869. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  2870. else
  2871. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  2872. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  2873. /* wait for the change to finish */
  2874. for (j = 0; j < 10; j++) {
  2875. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  2876. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  2877. break;
  2878. udelay(10);
  2879. }
  2880. if (j >= 10) {
  2881. dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
  2882. pf_q, (enable ? "en" : "dis"));
  2883. return -ETIMEDOUT;
  2884. }
  2885. }
  2886. return 0;
  2887. }
  2888. /**
  2889. * i40e_vsi_control_rings - Start or stop a VSI's rings
  2890. * @vsi: the VSI being configured
  2891. * @enable: start or stop the rings
  2892. **/
  2893. int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
  2894. {
  2895. int ret = 0;
  2896. /* do rx first for enable and last for disable */
  2897. if (request) {
  2898. ret = i40e_vsi_control_rx(vsi, request);
  2899. if (ret)
  2900. return ret;
  2901. ret = i40e_vsi_control_tx(vsi, request);
  2902. } else {
  2903. /* Ignore return value, we need to shutdown whatever we can */
  2904. i40e_vsi_control_tx(vsi, request);
  2905. i40e_vsi_control_rx(vsi, request);
  2906. }
  2907. return ret;
  2908. }
  2909. /**
  2910. * i40e_vsi_free_irq - Free the irq association with the OS
  2911. * @vsi: the VSI being configured
  2912. **/
  2913. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  2914. {
  2915. struct i40e_pf *pf = vsi->back;
  2916. struct i40e_hw *hw = &pf->hw;
  2917. int base = vsi->base_vector;
  2918. u32 val, qp;
  2919. int i;
  2920. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2921. if (!vsi->q_vectors)
  2922. return;
  2923. if (!vsi->irqs_ready)
  2924. return;
  2925. vsi->irqs_ready = false;
  2926. for (i = 0; i < vsi->num_q_vectors; i++) {
  2927. u16 vector = i + base;
  2928. /* free only the irqs that were actually requested */
  2929. if (!vsi->q_vectors[i] ||
  2930. !vsi->q_vectors[i]->num_ringpairs)
  2931. continue;
  2932. /* clear the affinity_mask in the IRQ descriptor */
  2933. irq_set_affinity_hint(pf->msix_entries[vector].vector,
  2934. NULL);
  2935. free_irq(pf->msix_entries[vector].vector,
  2936. vsi->q_vectors[i]);
  2937. /* Tear down the interrupt queue link list
  2938. *
  2939. * We know that they come in pairs and always
  2940. * the Rx first, then the Tx. To clear the
  2941. * link list, stick the EOL value into the
  2942. * next_q field of the registers.
  2943. */
  2944. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  2945. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  2946. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  2947. val |= I40E_QUEUE_END_OF_LIST
  2948. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  2949. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  2950. while (qp != I40E_QUEUE_END_OF_LIST) {
  2951. u32 next;
  2952. val = rd32(hw, I40E_QINT_RQCTL(qp));
  2953. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  2954. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  2955. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2956. I40E_QINT_RQCTL_INTEVENT_MASK);
  2957. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  2958. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  2959. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2960. val = rd32(hw, I40E_QINT_TQCTL(qp));
  2961. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  2962. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  2963. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  2964. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  2965. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2966. I40E_QINT_TQCTL_INTEVENT_MASK);
  2967. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  2968. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  2969. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2970. qp = next;
  2971. }
  2972. }
  2973. } else {
  2974. free_irq(pf->pdev->irq, pf);
  2975. val = rd32(hw, I40E_PFINT_LNKLST0);
  2976. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  2977. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  2978. val |= I40E_QUEUE_END_OF_LIST
  2979. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  2980. wr32(hw, I40E_PFINT_LNKLST0, val);
  2981. val = rd32(hw, I40E_QINT_RQCTL(qp));
  2982. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  2983. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  2984. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2985. I40E_QINT_RQCTL_INTEVENT_MASK);
  2986. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  2987. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  2988. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2989. val = rd32(hw, I40E_QINT_TQCTL(qp));
  2990. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  2991. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  2992. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2993. I40E_QINT_TQCTL_INTEVENT_MASK);
  2994. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  2995. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  2996. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2997. }
  2998. }
  2999. /**
  3000. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  3001. * @vsi: the VSI being configured
  3002. * @v_idx: Index of vector to be freed
  3003. *
  3004. * This function frees the memory allocated to the q_vector. In addition if
  3005. * NAPI is enabled it will delete any references to the NAPI struct prior
  3006. * to freeing the q_vector.
  3007. **/
  3008. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  3009. {
  3010. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3011. struct i40e_ring *ring;
  3012. if (!q_vector)
  3013. return;
  3014. /* disassociate q_vector from rings */
  3015. i40e_for_each_ring(ring, q_vector->tx)
  3016. ring->q_vector = NULL;
  3017. i40e_for_each_ring(ring, q_vector->rx)
  3018. ring->q_vector = NULL;
  3019. /* only VSI w/ an associated netdev is set up w/ NAPI */
  3020. if (vsi->netdev)
  3021. netif_napi_del(&q_vector->napi);
  3022. vsi->q_vectors[v_idx] = NULL;
  3023. kfree_rcu(q_vector, rcu);
  3024. }
  3025. /**
  3026. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  3027. * @vsi: the VSI being un-configured
  3028. *
  3029. * This frees the memory allocated to the q_vectors and
  3030. * deletes references to the NAPI struct.
  3031. **/
  3032. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  3033. {
  3034. int v_idx;
  3035. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  3036. i40e_free_q_vector(vsi, v_idx);
  3037. }
  3038. /**
  3039. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  3040. * @pf: board private structure
  3041. **/
  3042. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  3043. {
  3044. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  3045. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3046. pci_disable_msix(pf->pdev);
  3047. kfree(pf->msix_entries);
  3048. pf->msix_entries = NULL;
  3049. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  3050. pci_disable_msi(pf->pdev);
  3051. }
  3052. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  3053. }
  3054. /**
  3055. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  3056. * @pf: board private structure
  3057. *
  3058. * We go through and clear interrupt specific resources and reset the structure
  3059. * to pre-load conditions
  3060. **/
  3061. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  3062. {
  3063. int i;
  3064. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  3065. for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
  3066. if (pf->vsi[i])
  3067. i40e_vsi_free_q_vectors(pf->vsi[i]);
  3068. i40e_reset_interrupt_capability(pf);
  3069. }
  3070. /**
  3071. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3072. * @vsi: the VSI being configured
  3073. **/
  3074. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  3075. {
  3076. int q_idx;
  3077. if (!vsi->netdev)
  3078. return;
  3079. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3080. napi_enable(&vsi->q_vectors[q_idx]->napi);
  3081. }
  3082. /**
  3083. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  3084. * @vsi: the VSI being configured
  3085. **/
  3086. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  3087. {
  3088. int q_idx;
  3089. if (!vsi->netdev)
  3090. return;
  3091. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3092. napi_disable(&vsi->q_vectors[q_idx]->napi);
  3093. }
  3094. /**
  3095. * i40e_vsi_close - Shut down a VSI
  3096. * @vsi: the vsi to be quelled
  3097. **/
  3098. static void i40e_vsi_close(struct i40e_vsi *vsi)
  3099. {
  3100. if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
  3101. i40e_down(vsi);
  3102. i40e_vsi_free_irq(vsi);
  3103. i40e_vsi_free_tx_resources(vsi);
  3104. i40e_vsi_free_rx_resources(vsi);
  3105. }
  3106. /**
  3107. * i40e_quiesce_vsi - Pause a given VSI
  3108. * @vsi: the VSI being paused
  3109. **/
  3110. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  3111. {
  3112. if (test_bit(__I40E_DOWN, &vsi->state))
  3113. return;
  3114. set_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3115. if (vsi->netdev && netif_running(vsi->netdev)) {
  3116. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  3117. } else {
  3118. i40e_vsi_close(vsi);
  3119. }
  3120. }
  3121. /**
  3122. * i40e_unquiesce_vsi - Resume a given VSI
  3123. * @vsi: the VSI being resumed
  3124. **/
  3125. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  3126. {
  3127. if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
  3128. return;
  3129. clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3130. if (vsi->netdev && netif_running(vsi->netdev))
  3131. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  3132. else
  3133. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  3134. }
  3135. /**
  3136. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  3137. * @pf: the PF
  3138. **/
  3139. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  3140. {
  3141. int v;
  3142. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3143. if (pf->vsi[v])
  3144. i40e_quiesce_vsi(pf->vsi[v]);
  3145. }
  3146. }
  3147. /**
  3148. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  3149. * @pf: the PF
  3150. **/
  3151. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  3152. {
  3153. int v;
  3154. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3155. if (pf->vsi[v])
  3156. i40e_unquiesce_vsi(pf->vsi[v]);
  3157. }
  3158. }
  3159. /**
  3160. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  3161. * @dcbcfg: the corresponding DCBx configuration structure
  3162. *
  3163. * Return the number of TCs from given DCBx configuration
  3164. **/
  3165. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  3166. {
  3167. u8 num_tc = 0;
  3168. int i;
  3169. /* Scan the ETS Config Priority Table to find
  3170. * traffic class enabled for a given priority
  3171. * and use the traffic class index to get the
  3172. * number of traffic classes enabled
  3173. */
  3174. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  3175. if (dcbcfg->etscfg.prioritytable[i] > num_tc)
  3176. num_tc = dcbcfg->etscfg.prioritytable[i];
  3177. }
  3178. /* Traffic class index starts from zero so
  3179. * increment to return the actual count
  3180. */
  3181. return num_tc + 1;
  3182. }
  3183. /**
  3184. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  3185. * @dcbcfg: the corresponding DCBx configuration structure
  3186. *
  3187. * Query the current DCB configuration and return the number of
  3188. * traffic classes enabled from the given DCBX config
  3189. **/
  3190. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  3191. {
  3192. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  3193. u8 enabled_tc = 1;
  3194. u8 i;
  3195. for (i = 0; i < num_tc; i++)
  3196. enabled_tc |= 1 << i;
  3197. return enabled_tc;
  3198. }
  3199. /**
  3200. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  3201. * @pf: PF being queried
  3202. *
  3203. * Return number of traffic classes enabled for the given PF
  3204. **/
  3205. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  3206. {
  3207. struct i40e_hw *hw = &pf->hw;
  3208. u8 i, enabled_tc;
  3209. u8 num_tc = 0;
  3210. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  3211. /* If DCB is not enabled then always in single TC */
  3212. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  3213. return 1;
  3214. /* MFP mode return count of enabled TCs for this PF */
  3215. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  3216. enabled_tc = pf->hw.func_caps.enabled_tcmap;
  3217. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3218. if (enabled_tc & (1 << i))
  3219. num_tc++;
  3220. }
  3221. return num_tc;
  3222. }
  3223. /* SFP mode will be enabled for all TCs on port */
  3224. return i40e_dcb_get_num_tc(dcbcfg);
  3225. }
  3226. /**
  3227. * i40e_pf_get_default_tc - Get bitmap for first enabled TC
  3228. * @pf: PF being queried
  3229. *
  3230. * Return a bitmap for first enabled traffic class for this PF.
  3231. **/
  3232. static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
  3233. {
  3234. u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
  3235. u8 i = 0;
  3236. if (!enabled_tc)
  3237. return 0x1; /* TC0 */
  3238. /* Find the first enabled TC */
  3239. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3240. if (enabled_tc & (1 << i))
  3241. break;
  3242. }
  3243. return 1 << i;
  3244. }
  3245. /**
  3246. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  3247. * @pf: PF being queried
  3248. *
  3249. * Return a bitmap for enabled traffic classes for this PF.
  3250. **/
  3251. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  3252. {
  3253. /* If DCB is not enabled for this PF then just return default TC */
  3254. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  3255. return i40e_pf_get_default_tc(pf);
  3256. /* MFP mode will have enabled TCs set by FW */
  3257. if (pf->flags & I40E_FLAG_MFP_ENABLED)
  3258. return pf->hw.func_caps.enabled_tcmap;
  3259. /* SFP mode we want PF to be enabled for all TCs */
  3260. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  3261. }
  3262. /**
  3263. * i40e_vsi_get_bw_info - Query VSI BW Information
  3264. * @vsi: the VSI being queried
  3265. *
  3266. * Returns 0 on success, negative value on failure
  3267. **/
  3268. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  3269. {
  3270. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  3271. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  3272. struct i40e_pf *pf = vsi->back;
  3273. struct i40e_hw *hw = &pf->hw;
  3274. i40e_status aq_ret;
  3275. u32 tc_bw_max;
  3276. int i;
  3277. /* Get the VSI level BW configuration */
  3278. aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  3279. if (aq_ret) {
  3280. dev_info(&pf->pdev->dev,
  3281. "couldn't get pf vsi bw config, err %d, aq_err %d\n",
  3282. aq_ret, pf->hw.aq.asq_last_status);
  3283. return -EINVAL;
  3284. }
  3285. /* Get the VSI level BW configuration per TC */
  3286. aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  3287. NULL);
  3288. if (aq_ret) {
  3289. dev_info(&pf->pdev->dev,
  3290. "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
  3291. aq_ret, pf->hw.aq.asq_last_status);
  3292. return -EINVAL;
  3293. }
  3294. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  3295. dev_info(&pf->pdev->dev,
  3296. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  3297. bw_config.tc_valid_bits,
  3298. bw_ets_config.tc_valid_bits);
  3299. /* Still continuing */
  3300. }
  3301. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  3302. vsi->bw_max_quanta = bw_config.max_bw;
  3303. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  3304. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  3305. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3306. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  3307. vsi->bw_ets_limit_credits[i] =
  3308. le16_to_cpu(bw_ets_config.credits[i]);
  3309. /* 3 bits out of 4 for each TC */
  3310. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  3311. }
  3312. return 0;
  3313. }
  3314. /**
  3315. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  3316. * @vsi: the VSI being configured
  3317. * @enabled_tc: TC bitmap
  3318. * @bw_credits: BW shared credits per TC
  3319. *
  3320. * Returns 0 on success, negative value on failure
  3321. **/
  3322. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  3323. u8 *bw_share)
  3324. {
  3325. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  3326. i40e_status aq_ret;
  3327. int i;
  3328. bw_data.tc_valid_bits = enabled_tc;
  3329. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  3330. bw_data.tc_bw_credits[i] = bw_share[i];
  3331. aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  3332. NULL);
  3333. if (aq_ret) {
  3334. dev_info(&vsi->back->pdev->dev,
  3335. "AQ command Config VSI BW allocation per TC failed = %d\n",
  3336. vsi->back->hw.aq.asq_last_status);
  3337. return -EINVAL;
  3338. }
  3339. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  3340. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  3341. return 0;
  3342. }
  3343. /**
  3344. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  3345. * @vsi: the VSI being configured
  3346. * @enabled_tc: TC map to be enabled
  3347. *
  3348. **/
  3349. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  3350. {
  3351. struct net_device *netdev = vsi->netdev;
  3352. struct i40e_pf *pf = vsi->back;
  3353. struct i40e_hw *hw = &pf->hw;
  3354. u8 netdev_tc = 0;
  3355. int i;
  3356. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  3357. if (!netdev)
  3358. return;
  3359. if (!enabled_tc) {
  3360. netdev_reset_tc(netdev);
  3361. return;
  3362. }
  3363. /* Set up actual enabled TCs on the VSI */
  3364. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  3365. return;
  3366. /* set per TC queues for the VSI */
  3367. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3368. /* Only set TC queues for enabled tcs
  3369. *
  3370. * e.g. For a VSI that has TC0 and TC3 enabled the
  3371. * enabled_tc bitmap would be 0x00001001; the driver
  3372. * will set the numtc for netdev as 2 that will be
  3373. * referenced by the netdev layer as TC 0 and 1.
  3374. */
  3375. if (vsi->tc_config.enabled_tc & (1 << i))
  3376. netdev_set_tc_queue(netdev,
  3377. vsi->tc_config.tc_info[i].netdev_tc,
  3378. vsi->tc_config.tc_info[i].qcount,
  3379. vsi->tc_config.tc_info[i].qoffset);
  3380. }
  3381. /* Assign UP2TC map for the VSI */
  3382. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  3383. /* Get the actual TC# for the UP */
  3384. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  3385. /* Get the mapped netdev TC# for the UP */
  3386. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  3387. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  3388. }
  3389. }
  3390. /**
  3391. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  3392. * @vsi: the VSI being configured
  3393. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  3394. **/
  3395. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  3396. struct i40e_vsi_context *ctxt)
  3397. {
  3398. /* copy just the sections touched not the entire info
  3399. * since not all sections are valid as returned by
  3400. * update vsi params
  3401. */
  3402. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  3403. memcpy(&vsi->info.queue_mapping,
  3404. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  3405. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  3406. sizeof(vsi->info.tc_mapping));
  3407. }
  3408. /**
  3409. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  3410. * @vsi: VSI to be configured
  3411. * @enabled_tc: TC bitmap
  3412. *
  3413. * This configures a particular VSI for TCs that are mapped to the
  3414. * given TC bitmap. It uses default bandwidth share for TCs across
  3415. * VSIs to configure TC for a particular VSI.
  3416. *
  3417. * NOTE:
  3418. * It is expected that the VSI queues have been quisced before calling
  3419. * this function.
  3420. **/
  3421. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  3422. {
  3423. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  3424. struct i40e_vsi_context ctxt;
  3425. int ret = 0;
  3426. int i;
  3427. /* Check if enabled_tc is same as existing or new TCs */
  3428. if (vsi->tc_config.enabled_tc == enabled_tc)
  3429. return ret;
  3430. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  3431. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3432. if (enabled_tc & (1 << i))
  3433. bw_share[i] = 1;
  3434. }
  3435. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  3436. if (ret) {
  3437. dev_info(&vsi->back->pdev->dev,
  3438. "Failed configuring TC map %d for VSI %d\n",
  3439. enabled_tc, vsi->seid);
  3440. goto out;
  3441. }
  3442. /* Update Queue Pairs Mapping for currently enabled UPs */
  3443. ctxt.seid = vsi->seid;
  3444. ctxt.pf_num = vsi->back->hw.pf_id;
  3445. ctxt.vf_num = 0;
  3446. ctxt.uplink_seid = vsi->uplink_seid;
  3447. memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
  3448. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  3449. /* Update the VSI after updating the VSI queue-mapping information */
  3450. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  3451. if (ret) {
  3452. dev_info(&vsi->back->pdev->dev,
  3453. "update vsi failed, aq_err=%d\n",
  3454. vsi->back->hw.aq.asq_last_status);
  3455. goto out;
  3456. }
  3457. /* update the local VSI info with updated queue map */
  3458. i40e_vsi_update_queue_map(vsi, &ctxt);
  3459. vsi->info.valid_sections = 0;
  3460. /* Update current VSI BW information */
  3461. ret = i40e_vsi_get_bw_info(vsi);
  3462. if (ret) {
  3463. dev_info(&vsi->back->pdev->dev,
  3464. "Failed updating vsi bw info, aq_err=%d\n",
  3465. vsi->back->hw.aq.asq_last_status);
  3466. goto out;
  3467. }
  3468. /* Update the netdev TC setup */
  3469. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  3470. out:
  3471. return ret;
  3472. }
  3473. /**
  3474. * i40e_veb_config_tc - Configure TCs for given VEB
  3475. * @veb: given VEB
  3476. * @enabled_tc: TC bitmap
  3477. *
  3478. * Configures given TC bitmap for VEB (switching) element
  3479. **/
  3480. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  3481. {
  3482. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  3483. struct i40e_pf *pf = veb->pf;
  3484. int ret = 0;
  3485. int i;
  3486. /* No TCs or already enabled TCs just return */
  3487. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  3488. return ret;
  3489. bw_data.tc_valid_bits = enabled_tc;
  3490. /* bw_data.absolute_credits is not set (relative) */
  3491. /* Enable ETS TCs with equal BW Share for now */
  3492. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3493. if (enabled_tc & (1 << i))
  3494. bw_data.tc_bw_share_credits[i] = 1;
  3495. }
  3496. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  3497. &bw_data, NULL);
  3498. if (ret) {
  3499. dev_info(&pf->pdev->dev,
  3500. "veb bw config failed, aq_err=%d\n",
  3501. pf->hw.aq.asq_last_status);
  3502. goto out;
  3503. }
  3504. /* Update the BW information */
  3505. ret = i40e_veb_get_bw_info(veb);
  3506. if (ret) {
  3507. dev_info(&pf->pdev->dev,
  3508. "Failed getting veb bw config, aq_err=%d\n",
  3509. pf->hw.aq.asq_last_status);
  3510. }
  3511. out:
  3512. return ret;
  3513. }
  3514. #ifdef CONFIG_I40E_DCB
  3515. /**
  3516. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  3517. * @pf: PF struct
  3518. *
  3519. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  3520. * the caller would've quiesce all the VSIs before calling
  3521. * this function
  3522. **/
  3523. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  3524. {
  3525. u8 tc_map = 0;
  3526. int ret;
  3527. u8 v;
  3528. /* Enable the TCs available on PF to all VEBs */
  3529. tc_map = i40e_pf_get_tc_map(pf);
  3530. for (v = 0; v < I40E_MAX_VEB; v++) {
  3531. if (!pf->veb[v])
  3532. continue;
  3533. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  3534. if (ret) {
  3535. dev_info(&pf->pdev->dev,
  3536. "Failed configuring TC for VEB seid=%d\n",
  3537. pf->veb[v]->seid);
  3538. /* Will try to configure as many components */
  3539. }
  3540. }
  3541. /* Update each VSI */
  3542. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3543. if (!pf->vsi[v])
  3544. continue;
  3545. /* - Enable all TCs for the LAN VSI
  3546. * - For all others keep them at TC0 for now
  3547. */
  3548. if (v == pf->lan_vsi)
  3549. tc_map = i40e_pf_get_tc_map(pf);
  3550. else
  3551. tc_map = i40e_pf_get_default_tc(pf);
  3552. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  3553. if (ret) {
  3554. dev_info(&pf->pdev->dev,
  3555. "Failed configuring TC for VSI seid=%d\n",
  3556. pf->vsi[v]->seid);
  3557. /* Will try to configure as many components */
  3558. } else {
  3559. /* Re-configure VSI vectors based on updated TC map */
  3560. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  3561. if (pf->vsi[v]->netdev)
  3562. i40e_dcbnl_set_all(pf->vsi[v]);
  3563. }
  3564. }
  3565. }
  3566. /**
  3567. * i40e_init_pf_dcb - Initialize DCB configuration
  3568. * @pf: PF being configured
  3569. *
  3570. * Query the current DCB configuration and cache it
  3571. * in the hardware structure
  3572. **/
  3573. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  3574. {
  3575. struct i40e_hw *hw = &pf->hw;
  3576. int err = 0;
  3577. if (pf->hw.func_caps.npar_enable)
  3578. goto out;
  3579. /* Get the initial DCB configuration */
  3580. err = i40e_init_dcb(hw);
  3581. if (!err) {
  3582. /* Device/Function is not DCBX capable */
  3583. if ((!hw->func_caps.dcb) ||
  3584. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  3585. dev_info(&pf->pdev->dev,
  3586. "DCBX offload is not supported or is disabled for this PF.\n");
  3587. if (pf->flags & I40E_FLAG_MFP_ENABLED)
  3588. goto out;
  3589. } else {
  3590. /* When status is not DISABLED then DCBX in FW */
  3591. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  3592. DCB_CAP_DCBX_VER_IEEE;
  3593. pf->flags |= I40E_FLAG_DCB_ENABLED;
  3594. }
  3595. } else {
  3596. dev_info(&pf->pdev->dev, "AQ Querying DCB configuration failed: %d\n",
  3597. pf->hw.aq.asq_last_status);
  3598. }
  3599. out:
  3600. return err;
  3601. }
  3602. #endif /* CONFIG_I40E_DCB */
  3603. #define SPEED_SIZE 14
  3604. #define FC_SIZE 8
  3605. /**
  3606. * i40e_print_link_message - print link up or down
  3607. * @vsi: the VSI for which link needs a message
  3608. */
  3609. static void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  3610. {
  3611. char speed[SPEED_SIZE] = "Unknown";
  3612. char fc[FC_SIZE] = "RX/TX";
  3613. if (!isup) {
  3614. netdev_info(vsi->netdev, "NIC Link is Down\n");
  3615. return;
  3616. }
  3617. switch (vsi->back->hw.phy.link_info.link_speed) {
  3618. case I40E_LINK_SPEED_40GB:
  3619. strncpy(speed, "40 Gbps", SPEED_SIZE);
  3620. break;
  3621. case I40E_LINK_SPEED_10GB:
  3622. strncpy(speed, "10 Gbps", SPEED_SIZE);
  3623. break;
  3624. case I40E_LINK_SPEED_1GB:
  3625. strncpy(speed, "1000 Mbps", SPEED_SIZE);
  3626. break;
  3627. default:
  3628. break;
  3629. }
  3630. switch (vsi->back->hw.fc.current_mode) {
  3631. case I40E_FC_FULL:
  3632. strncpy(fc, "RX/TX", FC_SIZE);
  3633. break;
  3634. case I40E_FC_TX_PAUSE:
  3635. strncpy(fc, "TX", FC_SIZE);
  3636. break;
  3637. case I40E_FC_RX_PAUSE:
  3638. strncpy(fc, "RX", FC_SIZE);
  3639. break;
  3640. default:
  3641. strncpy(fc, "None", FC_SIZE);
  3642. break;
  3643. }
  3644. netdev_info(vsi->netdev, "NIC Link is Up %s Full Duplex, Flow Control: %s\n",
  3645. speed, fc);
  3646. }
  3647. /**
  3648. * i40e_up_complete - Finish the last steps of bringing up a connection
  3649. * @vsi: the VSI being configured
  3650. **/
  3651. static int i40e_up_complete(struct i40e_vsi *vsi)
  3652. {
  3653. struct i40e_pf *pf = vsi->back;
  3654. int err;
  3655. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3656. i40e_vsi_configure_msix(vsi);
  3657. else
  3658. i40e_configure_msi_and_legacy(vsi);
  3659. /* start rings */
  3660. err = i40e_vsi_control_rings(vsi, true);
  3661. if (err)
  3662. return err;
  3663. clear_bit(__I40E_DOWN, &vsi->state);
  3664. i40e_napi_enable_all(vsi);
  3665. i40e_vsi_enable_irq(vsi);
  3666. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  3667. (vsi->netdev)) {
  3668. i40e_print_link_message(vsi, true);
  3669. netif_tx_start_all_queues(vsi->netdev);
  3670. netif_carrier_on(vsi->netdev);
  3671. } else if (vsi->netdev) {
  3672. i40e_print_link_message(vsi, false);
  3673. }
  3674. /* replay FDIR SB filters */
  3675. if (vsi->type == I40E_VSI_FDIR)
  3676. i40e_fdir_filter_restore(vsi);
  3677. i40e_service_event_schedule(pf);
  3678. return 0;
  3679. }
  3680. /**
  3681. * i40e_vsi_reinit_locked - Reset the VSI
  3682. * @vsi: the VSI being configured
  3683. *
  3684. * Rebuild the ring structs after some configuration
  3685. * has changed, e.g. MTU size.
  3686. **/
  3687. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  3688. {
  3689. struct i40e_pf *pf = vsi->back;
  3690. WARN_ON(in_interrupt());
  3691. while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
  3692. usleep_range(1000, 2000);
  3693. i40e_down(vsi);
  3694. /* Give a VF some time to respond to the reset. The
  3695. * two second wait is based upon the watchdog cycle in
  3696. * the VF driver.
  3697. */
  3698. if (vsi->type == I40E_VSI_SRIOV)
  3699. msleep(2000);
  3700. i40e_up(vsi);
  3701. clear_bit(__I40E_CONFIG_BUSY, &pf->state);
  3702. }
  3703. /**
  3704. * i40e_up - Bring the connection back up after being down
  3705. * @vsi: the VSI being configured
  3706. **/
  3707. int i40e_up(struct i40e_vsi *vsi)
  3708. {
  3709. int err;
  3710. err = i40e_vsi_configure(vsi);
  3711. if (!err)
  3712. err = i40e_up_complete(vsi);
  3713. return err;
  3714. }
  3715. /**
  3716. * i40e_down - Shutdown the connection processing
  3717. * @vsi: the VSI being stopped
  3718. **/
  3719. void i40e_down(struct i40e_vsi *vsi)
  3720. {
  3721. int i;
  3722. /* It is assumed that the caller of this function
  3723. * sets the vsi->state __I40E_DOWN bit.
  3724. */
  3725. if (vsi->netdev) {
  3726. netif_carrier_off(vsi->netdev);
  3727. netif_tx_disable(vsi->netdev);
  3728. }
  3729. i40e_vsi_disable_irq(vsi);
  3730. i40e_vsi_control_rings(vsi, false);
  3731. i40e_napi_disable_all(vsi);
  3732. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3733. i40e_clean_tx_ring(vsi->tx_rings[i]);
  3734. i40e_clean_rx_ring(vsi->rx_rings[i]);
  3735. }
  3736. }
  3737. /**
  3738. * i40e_setup_tc - configure multiple traffic classes
  3739. * @netdev: net device to configure
  3740. * @tc: number of traffic classes to enable
  3741. **/
  3742. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  3743. {
  3744. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3745. struct i40e_vsi *vsi = np->vsi;
  3746. struct i40e_pf *pf = vsi->back;
  3747. u8 enabled_tc = 0;
  3748. int ret = -EINVAL;
  3749. int i;
  3750. /* Check if DCB enabled to continue */
  3751. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  3752. netdev_info(netdev, "DCB is not enabled for adapter\n");
  3753. goto exit;
  3754. }
  3755. /* Check if MFP enabled */
  3756. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  3757. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  3758. goto exit;
  3759. }
  3760. /* Check whether tc count is within enabled limit */
  3761. if (tc > i40e_pf_get_num_tc(pf)) {
  3762. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  3763. goto exit;
  3764. }
  3765. /* Generate TC map for number of tc requested */
  3766. for (i = 0; i < tc; i++)
  3767. enabled_tc |= (1 << i);
  3768. /* Requesting same TC configuration as already enabled */
  3769. if (enabled_tc == vsi->tc_config.enabled_tc)
  3770. return 0;
  3771. /* Quiesce VSI queues */
  3772. i40e_quiesce_vsi(vsi);
  3773. /* Configure VSI for enabled TCs */
  3774. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  3775. if (ret) {
  3776. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  3777. vsi->seid);
  3778. goto exit;
  3779. }
  3780. /* Unquiesce VSI */
  3781. i40e_unquiesce_vsi(vsi);
  3782. exit:
  3783. return ret;
  3784. }
  3785. /**
  3786. * i40e_open - Called when a network interface is made active
  3787. * @netdev: network interface device structure
  3788. *
  3789. * The open entry point is called when a network interface is made
  3790. * active by the system (IFF_UP). At this point all resources needed
  3791. * for transmit and receive operations are allocated, the interrupt
  3792. * handler is registered with the OS, the netdev watchdog subtask is
  3793. * enabled, and the stack is notified that the interface is ready.
  3794. *
  3795. * Returns 0 on success, negative value on failure
  3796. **/
  3797. static int i40e_open(struct net_device *netdev)
  3798. {
  3799. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3800. struct i40e_vsi *vsi = np->vsi;
  3801. struct i40e_pf *pf = vsi->back;
  3802. int err;
  3803. /* disallow open during test or if eeprom is broken */
  3804. if (test_bit(__I40E_TESTING, &pf->state) ||
  3805. test_bit(__I40E_BAD_EEPROM, &pf->state))
  3806. return -EBUSY;
  3807. netif_carrier_off(netdev);
  3808. err = i40e_vsi_open(vsi);
  3809. if (err)
  3810. return err;
  3811. /* configure global TSO hardware offload settings */
  3812. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  3813. TCP_FLAG_FIN) >> 16);
  3814. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  3815. TCP_FLAG_FIN |
  3816. TCP_FLAG_CWR) >> 16);
  3817. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  3818. #ifdef CONFIG_I40E_VXLAN
  3819. vxlan_get_rx_port(netdev);
  3820. #endif
  3821. return 0;
  3822. }
  3823. /**
  3824. * i40e_vsi_open -
  3825. * @vsi: the VSI to open
  3826. *
  3827. * Finish initialization of the VSI.
  3828. *
  3829. * Returns 0 on success, negative value on failure
  3830. **/
  3831. int i40e_vsi_open(struct i40e_vsi *vsi)
  3832. {
  3833. struct i40e_pf *pf = vsi->back;
  3834. char int_name[IFNAMSIZ];
  3835. int err;
  3836. /* allocate descriptors */
  3837. err = i40e_vsi_setup_tx_resources(vsi);
  3838. if (err)
  3839. goto err_setup_tx;
  3840. err = i40e_vsi_setup_rx_resources(vsi);
  3841. if (err)
  3842. goto err_setup_rx;
  3843. err = i40e_vsi_configure(vsi);
  3844. if (err)
  3845. goto err_setup_rx;
  3846. if (vsi->netdev) {
  3847. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  3848. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  3849. err = i40e_vsi_request_irq(vsi, int_name);
  3850. if (err)
  3851. goto err_setup_rx;
  3852. /* Notify the stack of the actual queue counts. */
  3853. err = netif_set_real_num_tx_queues(vsi->netdev,
  3854. vsi->num_queue_pairs);
  3855. if (err)
  3856. goto err_set_queues;
  3857. err = netif_set_real_num_rx_queues(vsi->netdev,
  3858. vsi->num_queue_pairs);
  3859. if (err)
  3860. goto err_set_queues;
  3861. } else if (vsi->type == I40E_VSI_FDIR) {
  3862. snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
  3863. dev_driver_string(&pf->pdev->dev));
  3864. err = i40e_vsi_request_irq(vsi, int_name);
  3865. } else {
  3866. err = -EINVAL;
  3867. goto err_setup_rx;
  3868. }
  3869. err = i40e_up_complete(vsi);
  3870. if (err)
  3871. goto err_up_complete;
  3872. return 0;
  3873. err_up_complete:
  3874. i40e_down(vsi);
  3875. err_set_queues:
  3876. i40e_vsi_free_irq(vsi);
  3877. err_setup_rx:
  3878. i40e_vsi_free_rx_resources(vsi);
  3879. err_setup_tx:
  3880. i40e_vsi_free_tx_resources(vsi);
  3881. if (vsi == pf->vsi[pf->lan_vsi])
  3882. i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
  3883. return err;
  3884. }
  3885. /**
  3886. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  3887. * @pf: Pointer to pf
  3888. *
  3889. * This function destroys the hlist where all the Flow Director
  3890. * filters were saved.
  3891. **/
  3892. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  3893. {
  3894. struct i40e_fdir_filter *filter;
  3895. struct hlist_node *node2;
  3896. hlist_for_each_entry_safe(filter, node2,
  3897. &pf->fdir_filter_list, fdir_node) {
  3898. hlist_del(&filter->fdir_node);
  3899. kfree(filter);
  3900. }
  3901. pf->fdir_pf_active_filters = 0;
  3902. }
  3903. /**
  3904. * i40e_close - Disables a network interface
  3905. * @netdev: network interface device structure
  3906. *
  3907. * The close entry point is called when an interface is de-activated
  3908. * by the OS. The hardware is still under the driver's control, but
  3909. * this netdev interface is disabled.
  3910. *
  3911. * Returns 0, this is not allowed to fail
  3912. **/
  3913. static int i40e_close(struct net_device *netdev)
  3914. {
  3915. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3916. struct i40e_vsi *vsi = np->vsi;
  3917. i40e_vsi_close(vsi);
  3918. return 0;
  3919. }
  3920. /**
  3921. * i40e_do_reset - Start a PF or Core Reset sequence
  3922. * @pf: board private structure
  3923. * @reset_flags: which reset is requested
  3924. *
  3925. * The essential difference in resets is that the PF Reset
  3926. * doesn't clear the packet buffers, doesn't reset the PE
  3927. * firmware, and doesn't bother the other PFs on the chip.
  3928. **/
  3929. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
  3930. {
  3931. u32 val;
  3932. WARN_ON(in_interrupt());
  3933. if (i40e_check_asq_alive(&pf->hw))
  3934. i40e_vc_notify_reset(pf);
  3935. /* do the biggest reset indicated */
  3936. if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
  3937. /* Request a Global Reset
  3938. *
  3939. * This will start the chip's countdown to the actual full
  3940. * chip reset event, and a warning interrupt to be sent
  3941. * to all PFs, including the requestor. Our handler
  3942. * for the warning interrupt will deal with the shutdown
  3943. * and recovery of the switch setup.
  3944. */
  3945. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  3946. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  3947. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  3948. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  3949. } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
  3950. /* Request a Core Reset
  3951. *
  3952. * Same as Global Reset, except does *not* include the MAC/PHY
  3953. */
  3954. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  3955. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  3956. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  3957. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  3958. i40e_flush(&pf->hw);
  3959. } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
  3960. /* Request a Firmware Reset
  3961. *
  3962. * Same as Global reset, plus restarting the
  3963. * embedded firmware engine.
  3964. */
  3965. /* enable EMP Reset */
  3966. val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
  3967. val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
  3968. wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
  3969. /* force the reset */
  3970. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  3971. val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
  3972. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  3973. i40e_flush(&pf->hw);
  3974. } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
  3975. /* Request a PF Reset
  3976. *
  3977. * Resets only the PF-specific registers
  3978. *
  3979. * This goes directly to the tear-down and rebuild of
  3980. * the switch, since we need to do all the recovery as
  3981. * for the Core Reset.
  3982. */
  3983. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  3984. i40e_handle_reset_warning(pf);
  3985. } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
  3986. int v;
  3987. /* Find the VSI(s) that requested a re-init */
  3988. dev_info(&pf->pdev->dev,
  3989. "VSI reinit requested\n");
  3990. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3991. struct i40e_vsi *vsi = pf->vsi[v];
  3992. if (vsi != NULL &&
  3993. test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
  3994. i40e_vsi_reinit_locked(pf->vsi[v]);
  3995. clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  3996. }
  3997. }
  3998. /* no further action needed, so return now */
  3999. return;
  4000. } else {
  4001. dev_info(&pf->pdev->dev,
  4002. "bad reset request 0x%08x\n", reset_flags);
  4003. return;
  4004. }
  4005. }
  4006. #ifdef CONFIG_I40E_DCB
  4007. /**
  4008. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  4009. * @pf: board private structure
  4010. * @old_cfg: current DCB config
  4011. * @new_cfg: new DCB config
  4012. **/
  4013. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  4014. struct i40e_dcbx_config *old_cfg,
  4015. struct i40e_dcbx_config *new_cfg)
  4016. {
  4017. bool need_reconfig = false;
  4018. /* Check if ETS configuration has changed */
  4019. if (memcmp(&new_cfg->etscfg,
  4020. &old_cfg->etscfg,
  4021. sizeof(new_cfg->etscfg))) {
  4022. /* If Priority Table has changed reconfig is needed */
  4023. if (memcmp(&new_cfg->etscfg.prioritytable,
  4024. &old_cfg->etscfg.prioritytable,
  4025. sizeof(new_cfg->etscfg.prioritytable))) {
  4026. need_reconfig = true;
  4027. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  4028. }
  4029. if (memcmp(&new_cfg->etscfg.tcbwtable,
  4030. &old_cfg->etscfg.tcbwtable,
  4031. sizeof(new_cfg->etscfg.tcbwtable)))
  4032. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  4033. if (memcmp(&new_cfg->etscfg.tsatable,
  4034. &old_cfg->etscfg.tsatable,
  4035. sizeof(new_cfg->etscfg.tsatable)))
  4036. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  4037. }
  4038. /* Check if PFC configuration has changed */
  4039. if (memcmp(&new_cfg->pfc,
  4040. &old_cfg->pfc,
  4041. sizeof(new_cfg->pfc))) {
  4042. need_reconfig = true;
  4043. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  4044. }
  4045. /* Check if APP Table has changed */
  4046. if (memcmp(&new_cfg->app,
  4047. &old_cfg->app,
  4048. sizeof(new_cfg->app))) {
  4049. need_reconfig = true;
  4050. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  4051. }
  4052. return need_reconfig;
  4053. }
  4054. /**
  4055. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  4056. * @pf: board private structure
  4057. * @e: event info posted on ARQ
  4058. **/
  4059. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  4060. struct i40e_arq_event_info *e)
  4061. {
  4062. struct i40e_aqc_lldp_get_mib *mib =
  4063. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  4064. struct i40e_hw *hw = &pf->hw;
  4065. struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
  4066. struct i40e_dcbx_config tmp_dcbx_cfg;
  4067. bool need_reconfig = false;
  4068. int ret = 0;
  4069. u8 type;
  4070. /* Ignore if event is not for Nearest Bridge */
  4071. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  4072. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  4073. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  4074. return ret;
  4075. /* Check MIB Type and return if event for Remote MIB update */
  4076. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  4077. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  4078. /* Update the remote cached instance and return */
  4079. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  4080. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  4081. &hw->remote_dcbx_config);
  4082. goto exit;
  4083. }
  4084. /* Convert/store the DCBX data from LLDPDU temporarily */
  4085. memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
  4086. ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
  4087. if (ret) {
  4088. /* Error in LLDPDU parsing return */
  4089. dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
  4090. goto exit;
  4091. }
  4092. /* No change detected in DCBX configs */
  4093. if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
  4094. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  4095. goto exit;
  4096. }
  4097. need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
  4098. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
  4099. /* Overwrite the new configuration */
  4100. *dcbx_cfg = tmp_dcbx_cfg;
  4101. if (!need_reconfig)
  4102. goto exit;
  4103. /* Reconfiguration needed quiesce all VSIs */
  4104. i40e_pf_quiesce_all_vsi(pf);
  4105. /* Changes in configuration update VEB/VSI */
  4106. i40e_dcb_reconfigure(pf);
  4107. i40e_pf_unquiesce_all_vsi(pf);
  4108. exit:
  4109. return ret;
  4110. }
  4111. #endif /* CONFIG_I40E_DCB */
  4112. /**
  4113. * i40e_do_reset_safe - Protected reset path for userland calls.
  4114. * @pf: board private structure
  4115. * @reset_flags: which reset is requested
  4116. *
  4117. **/
  4118. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  4119. {
  4120. rtnl_lock();
  4121. i40e_do_reset(pf, reset_flags);
  4122. rtnl_unlock();
  4123. }
  4124. /**
  4125. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  4126. * @pf: board private structure
  4127. * @e: event info posted on ARQ
  4128. *
  4129. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  4130. * and VF queues
  4131. **/
  4132. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  4133. struct i40e_arq_event_info *e)
  4134. {
  4135. struct i40e_aqc_lan_overflow *data =
  4136. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  4137. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  4138. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  4139. struct i40e_hw *hw = &pf->hw;
  4140. struct i40e_vf *vf;
  4141. u16 vf_id;
  4142. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  4143. queue, qtx_ctl);
  4144. /* Queue belongs to VF, find the VF and issue VF reset */
  4145. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  4146. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  4147. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  4148. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  4149. vf_id -= hw->func_caps.vf_base_id;
  4150. vf = &pf->vf[vf_id];
  4151. i40e_vc_notify_vf_reset(vf);
  4152. /* Allow VF to process pending reset notification */
  4153. msleep(20);
  4154. i40e_reset_vf(vf, false);
  4155. }
  4156. }
  4157. /**
  4158. * i40e_service_event_complete - Finish up the service event
  4159. * @pf: board private structure
  4160. **/
  4161. static void i40e_service_event_complete(struct i40e_pf *pf)
  4162. {
  4163. BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
  4164. /* flush memory to make sure state is correct before next watchog */
  4165. smp_mb__before_clear_bit();
  4166. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  4167. }
  4168. /**
  4169. * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
  4170. * @pf: board private structure
  4171. **/
  4172. int i40e_get_current_fd_count(struct i40e_pf *pf)
  4173. {
  4174. int val, fcnt_prog;
  4175. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  4176. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  4177. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  4178. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  4179. return fcnt_prog;
  4180. }
  4181. /**
  4182. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  4183. * @pf: board private structure
  4184. **/
  4185. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  4186. {
  4187. u32 fcnt_prog, fcnt_avail;
  4188. /* Check if, FD SB or ATR was auto disabled and if there is enough room
  4189. * to re-enable
  4190. */
  4191. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  4192. (pf->flags & I40E_FLAG_FD_SB_ENABLED))
  4193. return;
  4194. fcnt_prog = i40e_get_current_fd_count(pf);
  4195. fcnt_avail = i40e_get_fd_cnt_all(pf);
  4196. if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
  4197. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  4198. (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
  4199. pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  4200. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  4201. }
  4202. }
  4203. /* Wait for some more space to be available to turn on ATR */
  4204. if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
  4205. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  4206. (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
  4207. pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  4208. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
  4209. }
  4210. }
  4211. }
  4212. /**
  4213. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  4214. * @pf: board private structure
  4215. **/
  4216. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  4217. {
  4218. if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
  4219. return;
  4220. /* if interface is down do nothing */
  4221. if (test_bit(__I40E_DOWN, &pf->state))
  4222. return;
  4223. i40e_fdir_check_and_reenable(pf);
  4224. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  4225. (pf->flags & I40E_FLAG_FD_SB_ENABLED))
  4226. pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
  4227. }
  4228. /**
  4229. * i40e_vsi_link_event - notify VSI of a link event
  4230. * @vsi: vsi to be notified
  4231. * @link_up: link up or down
  4232. **/
  4233. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  4234. {
  4235. if (!vsi)
  4236. return;
  4237. switch (vsi->type) {
  4238. case I40E_VSI_MAIN:
  4239. if (!vsi->netdev || !vsi->netdev_registered)
  4240. break;
  4241. if (link_up) {
  4242. netif_carrier_on(vsi->netdev);
  4243. netif_tx_wake_all_queues(vsi->netdev);
  4244. } else {
  4245. netif_carrier_off(vsi->netdev);
  4246. netif_tx_stop_all_queues(vsi->netdev);
  4247. }
  4248. break;
  4249. case I40E_VSI_SRIOV:
  4250. break;
  4251. case I40E_VSI_VMDQ2:
  4252. case I40E_VSI_CTRL:
  4253. case I40E_VSI_MIRROR:
  4254. default:
  4255. /* there is no notification for other VSIs */
  4256. break;
  4257. }
  4258. }
  4259. /**
  4260. * i40e_veb_link_event - notify elements on the veb of a link event
  4261. * @veb: veb to be notified
  4262. * @link_up: link up or down
  4263. **/
  4264. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  4265. {
  4266. struct i40e_pf *pf;
  4267. int i;
  4268. if (!veb || !veb->pf)
  4269. return;
  4270. pf = veb->pf;
  4271. /* depth first... */
  4272. for (i = 0; i < I40E_MAX_VEB; i++)
  4273. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  4274. i40e_veb_link_event(pf->veb[i], link_up);
  4275. /* ... now the local VSIs */
  4276. for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
  4277. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  4278. i40e_vsi_link_event(pf->vsi[i], link_up);
  4279. }
  4280. /**
  4281. * i40e_link_event - Update netif_carrier status
  4282. * @pf: board private structure
  4283. **/
  4284. static void i40e_link_event(struct i40e_pf *pf)
  4285. {
  4286. bool new_link, old_link;
  4287. new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
  4288. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  4289. if (new_link == old_link)
  4290. return;
  4291. if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
  4292. i40e_print_link_message(pf->vsi[pf->lan_vsi], new_link);
  4293. /* Notify the base of the switch tree connected to
  4294. * the link. Floating VEBs are not notified.
  4295. */
  4296. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  4297. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  4298. else
  4299. i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
  4300. if (pf->vf)
  4301. i40e_vc_notify_link_state(pf);
  4302. if (pf->flags & I40E_FLAG_PTP)
  4303. i40e_ptp_set_increment(pf);
  4304. }
  4305. /**
  4306. * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
  4307. * @pf: board private structure
  4308. *
  4309. * Set the per-queue flags to request a check for stuck queues in the irq
  4310. * clean functions, then force interrupts to be sure the irq clean is called.
  4311. **/
  4312. static void i40e_check_hang_subtask(struct i40e_pf *pf)
  4313. {
  4314. int i, v;
  4315. /* If we're down or resetting, just bail */
  4316. if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
  4317. return;
  4318. /* for each VSI/netdev
  4319. * for each Tx queue
  4320. * set the check flag
  4321. * for each q_vector
  4322. * force an interrupt
  4323. */
  4324. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4325. struct i40e_vsi *vsi = pf->vsi[v];
  4326. int armed = 0;
  4327. if (!pf->vsi[v] ||
  4328. test_bit(__I40E_DOWN, &vsi->state) ||
  4329. (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
  4330. continue;
  4331. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4332. set_check_for_tx_hang(vsi->tx_rings[i]);
  4333. if (test_bit(__I40E_HANG_CHECK_ARMED,
  4334. &vsi->tx_rings[i]->state))
  4335. armed++;
  4336. }
  4337. if (armed) {
  4338. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  4339. wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
  4340. (I40E_PFINT_DYN_CTL0_INTENA_MASK |
  4341. I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
  4342. } else {
  4343. u16 vec = vsi->base_vector - 1;
  4344. u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
  4345. I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
  4346. for (i = 0; i < vsi->num_q_vectors; i++, vec++)
  4347. wr32(&vsi->back->hw,
  4348. I40E_PFINT_DYN_CTLN(vec), val);
  4349. }
  4350. i40e_flush(&vsi->back->hw);
  4351. }
  4352. }
  4353. }
  4354. /**
  4355. * i40e_watchdog_subtask - Check and bring link up
  4356. * @pf: board private structure
  4357. **/
  4358. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  4359. {
  4360. int i;
  4361. /* if interface is down do nothing */
  4362. if (test_bit(__I40E_DOWN, &pf->state) ||
  4363. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  4364. return;
  4365. /* Update the stats for active netdevs so the network stack
  4366. * can look at updated numbers whenever it cares to
  4367. */
  4368. for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
  4369. if (pf->vsi[i] && pf->vsi[i]->netdev)
  4370. i40e_update_stats(pf->vsi[i]);
  4371. /* Update the stats for the active switching components */
  4372. for (i = 0; i < I40E_MAX_VEB; i++)
  4373. if (pf->veb[i])
  4374. i40e_update_veb_stats(pf->veb[i]);
  4375. i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
  4376. }
  4377. /**
  4378. * i40e_reset_subtask - Set up for resetting the device and driver
  4379. * @pf: board private structure
  4380. **/
  4381. static void i40e_reset_subtask(struct i40e_pf *pf)
  4382. {
  4383. u32 reset_flags = 0;
  4384. rtnl_lock();
  4385. if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
  4386. reset_flags |= (1 << __I40E_REINIT_REQUESTED);
  4387. clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
  4388. }
  4389. if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
  4390. reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
  4391. clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  4392. }
  4393. if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
  4394. reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
  4395. clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  4396. }
  4397. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
  4398. reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
  4399. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  4400. }
  4401. /* If there's a recovery already waiting, it takes
  4402. * precedence before starting a new reset sequence.
  4403. */
  4404. if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
  4405. i40e_handle_reset_warning(pf);
  4406. goto unlock;
  4407. }
  4408. /* If we're already down or resetting, just bail */
  4409. if (reset_flags &&
  4410. !test_bit(__I40E_DOWN, &pf->state) &&
  4411. !test_bit(__I40E_CONFIG_BUSY, &pf->state))
  4412. i40e_do_reset(pf, reset_flags);
  4413. unlock:
  4414. rtnl_unlock();
  4415. }
  4416. /**
  4417. * i40e_handle_link_event - Handle link event
  4418. * @pf: board private structure
  4419. * @e: event info posted on ARQ
  4420. **/
  4421. static void i40e_handle_link_event(struct i40e_pf *pf,
  4422. struct i40e_arq_event_info *e)
  4423. {
  4424. struct i40e_hw *hw = &pf->hw;
  4425. struct i40e_aqc_get_link_status *status =
  4426. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  4427. struct i40e_link_status *hw_link_info = &hw->phy.link_info;
  4428. /* save off old link status information */
  4429. memcpy(&pf->hw.phy.link_info_old, hw_link_info,
  4430. sizeof(pf->hw.phy.link_info_old));
  4431. /* update link status */
  4432. hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
  4433. hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
  4434. hw_link_info->link_info = status->link_info;
  4435. hw_link_info->an_info = status->an_info;
  4436. hw_link_info->ext_info = status->ext_info;
  4437. hw_link_info->lse_enable =
  4438. le16_to_cpu(status->command_flags) &
  4439. I40E_AQ_LSE_ENABLE;
  4440. /* process the event */
  4441. i40e_link_event(pf);
  4442. /* Do a new status request to re-enable LSE reporting
  4443. * and load new status information into the hw struct,
  4444. * then see if the status changed while processing the
  4445. * initial event.
  4446. */
  4447. i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
  4448. i40e_link_event(pf);
  4449. }
  4450. /**
  4451. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  4452. * @pf: board private structure
  4453. **/
  4454. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  4455. {
  4456. struct i40e_arq_event_info event;
  4457. struct i40e_hw *hw = &pf->hw;
  4458. u16 pending, i = 0;
  4459. i40e_status ret;
  4460. u16 opcode;
  4461. u32 val;
  4462. if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
  4463. return;
  4464. event.msg_size = I40E_MAX_AQ_BUF_SIZE;
  4465. event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
  4466. if (!event.msg_buf)
  4467. return;
  4468. do {
  4469. event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
  4470. ret = i40e_clean_arq_element(hw, &event, &pending);
  4471. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
  4472. dev_info(&pf->pdev->dev, "No ARQ event found\n");
  4473. break;
  4474. } else if (ret) {
  4475. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  4476. break;
  4477. }
  4478. opcode = le16_to_cpu(event.desc.opcode);
  4479. switch (opcode) {
  4480. case i40e_aqc_opc_get_link_status:
  4481. i40e_handle_link_event(pf, &event);
  4482. break;
  4483. case i40e_aqc_opc_send_msg_to_pf:
  4484. ret = i40e_vc_process_vf_msg(pf,
  4485. le16_to_cpu(event.desc.retval),
  4486. le32_to_cpu(event.desc.cookie_high),
  4487. le32_to_cpu(event.desc.cookie_low),
  4488. event.msg_buf,
  4489. event.msg_size);
  4490. break;
  4491. case i40e_aqc_opc_lldp_update_mib:
  4492. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  4493. #ifdef CONFIG_I40E_DCB
  4494. rtnl_lock();
  4495. ret = i40e_handle_lldp_event(pf, &event);
  4496. rtnl_unlock();
  4497. #endif /* CONFIG_I40E_DCB */
  4498. break;
  4499. case i40e_aqc_opc_event_lan_overflow:
  4500. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  4501. i40e_handle_lan_overflow_event(pf, &event);
  4502. break;
  4503. case i40e_aqc_opc_send_msg_to_peer:
  4504. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  4505. break;
  4506. default:
  4507. dev_info(&pf->pdev->dev,
  4508. "ARQ Error: Unknown event 0x%04x received\n",
  4509. opcode);
  4510. break;
  4511. }
  4512. } while (pending && (i++ < pf->adminq_work_limit));
  4513. clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  4514. /* re-enable Admin queue interrupt cause */
  4515. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  4516. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  4517. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  4518. i40e_flush(hw);
  4519. kfree(event.msg_buf);
  4520. }
  4521. /**
  4522. * i40e_verify_eeprom - make sure eeprom is good to use
  4523. * @pf: board private structure
  4524. **/
  4525. static void i40e_verify_eeprom(struct i40e_pf *pf)
  4526. {
  4527. int err;
  4528. err = i40e_diag_eeprom_test(&pf->hw);
  4529. if (err) {
  4530. /* retry in case of garbage read */
  4531. err = i40e_diag_eeprom_test(&pf->hw);
  4532. if (err) {
  4533. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  4534. err);
  4535. set_bit(__I40E_BAD_EEPROM, &pf->state);
  4536. }
  4537. }
  4538. if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  4539. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  4540. clear_bit(__I40E_BAD_EEPROM, &pf->state);
  4541. }
  4542. }
  4543. /**
  4544. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  4545. * @veb: pointer to the VEB instance
  4546. *
  4547. * This is a recursive function that first builds the attached VSIs then
  4548. * recurses in to build the next layer of VEB. We track the connections
  4549. * through our own index numbers because the seid's from the HW could
  4550. * change across the reset.
  4551. **/
  4552. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  4553. {
  4554. struct i40e_vsi *ctl_vsi = NULL;
  4555. struct i40e_pf *pf = veb->pf;
  4556. int v, veb_idx;
  4557. int ret;
  4558. /* build VSI that owns this VEB, temporarily attached to base VEB */
  4559. for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
  4560. if (pf->vsi[v] &&
  4561. pf->vsi[v]->veb_idx == veb->idx &&
  4562. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  4563. ctl_vsi = pf->vsi[v];
  4564. break;
  4565. }
  4566. }
  4567. if (!ctl_vsi) {
  4568. dev_info(&pf->pdev->dev,
  4569. "missing owner VSI for veb_idx %d\n", veb->idx);
  4570. ret = -ENOENT;
  4571. goto end_reconstitute;
  4572. }
  4573. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  4574. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  4575. ret = i40e_add_vsi(ctl_vsi);
  4576. if (ret) {
  4577. dev_info(&pf->pdev->dev,
  4578. "rebuild of owner VSI failed: %d\n", ret);
  4579. goto end_reconstitute;
  4580. }
  4581. i40e_vsi_reset_stats(ctl_vsi);
  4582. /* create the VEB in the switch and move the VSI onto the VEB */
  4583. ret = i40e_add_veb(veb, ctl_vsi);
  4584. if (ret)
  4585. goto end_reconstitute;
  4586. /* create the remaining VSIs attached to this VEB */
  4587. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4588. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  4589. continue;
  4590. if (pf->vsi[v]->veb_idx == veb->idx) {
  4591. struct i40e_vsi *vsi = pf->vsi[v];
  4592. vsi->uplink_seid = veb->seid;
  4593. ret = i40e_add_vsi(vsi);
  4594. if (ret) {
  4595. dev_info(&pf->pdev->dev,
  4596. "rebuild of vsi_idx %d failed: %d\n",
  4597. v, ret);
  4598. goto end_reconstitute;
  4599. }
  4600. i40e_vsi_reset_stats(vsi);
  4601. }
  4602. }
  4603. /* create any VEBs attached to this VEB - RECURSION */
  4604. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  4605. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  4606. pf->veb[veb_idx]->uplink_seid = veb->seid;
  4607. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  4608. if (ret)
  4609. break;
  4610. }
  4611. }
  4612. end_reconstitute:
  4613. return ret;
  4614. }
  4615. /**
  4616. * i40e_get_capabilities - get info about the HW
  4617. * @pf: the PF struct
  4618. **/
  4619. static int i40e_get_capabilities(struct i40e_pf *pf)
  4620. {
  4621. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  4622. u16 data_size;
  4623. int buf_len;
  4624. int err;
  4625. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  4626. do {
  4627. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  4628. if (!cap_buf)
  4629. return -ENOMEM;
  4630. /* this loads the data into the hw struct for us */
  4631. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  4632. &data_size,
  4633. i40e_aqc_opc_list_func_capabilities,
  4634. NULL);
  4635. /* data loaded, buffer no longer needed */
  4636. kfree(cap_buf);
  4637. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  4638. /* retry with a larger buffer */
  4639. buf_len = data_size;
  4640. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  4641. dev_info(&pf->pdev->dev,
  4642. "capability discovery failed: aq=%d\n",
  4643. pf->hw.aq.asq_last_status);
  4644. return -ENODEV;
  4645. }
  4646. } while (err);
  4647. if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
  4648. (pf->hw.aq.fw_maj_ver < 2)) {
  4649. pf->hw.func_caps.num_msix_vectors++;
  4650. pf->hw.func_caps.num_msix_vectors_vf++;
  4651. }
  4652. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  4653. dev_info(&pf->pdev->dev,
  4654. "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",
  4655. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  4656. pf->hw.func_caps.num_msix_vectors,
  4657. pf->hw.func_caps.num_msix_vectors_vf,
  4658. pf->hw.func_caps.fd_filters_guaranteed,
  4659. pf->hw.func_caps.fd_filters_best_effort,
  4660. pf->hw.func_caps.num_tx_qp,
  4661. pf->hw.func_caps.num_vsis);
  4662. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  4663. + pf->hw.func_caps.num_vfs)
  4664. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  4665. dev_info(&pf->pdev->dev,
  4666. "got num_vsis %d, setting num_vsis to %d\n",
  4667. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  4668. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  4669. }
  4670. return 0;
  4671. }
  4672. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  4673. /**
  4674. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  4675. * @pf: board private structure
  4676. **/
  4677. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  4678. {
  4679. struct i40e_vsi *vsi;
  4680. int i;
  4681. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  4682. return;
  4683. /* find existing VSI and see if it needs configuring */
  4684. vsi = NULL;
  4685. for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  4686. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  4687. vsi = pf->vsi[i];
  4688. break;
  4689. }
  4690. }
  4691. /* create a new VSI if none exists */
  4692. if (!vsi) {
  4693. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  4694. pf->vsi[pf->lan_vsi]->seid, 0);
  4695. if (!vsi) {
  4696. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  4697. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  4698. return;
  4699. }
  4700. }
  4701. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  4702. }
  4703. /**
  4704. * i40e_fdir_teardown - release the Flow Director resources
  4705. * @pf: board private structure
  4706. **/
  4707. static void i40e_fdir_teardown(struct i40e_pf *pf)
  4708. {
  4709. int i;
  4710. i40e_fdir_filter_exit(pf);
  4711. for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  4712. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  4713. i40e_vsi_release(pf->vsi[i]);
  4714. break;
  4715. }
  4716. }
  4717. }
  4718. /**
  4719. * i40e_prep_for_reset - prep for the core to reset
  4720. * @pf: board private structure
  4721. *
  4722. * Close up the VFs and other things in prep for pf Reset.
  4723. **/
  4724. static int i40e_prep_for_reset(struct i40e_pf *pf)
  4725. {
  4726. struct i40e_hw *hw = &pf->hw;
  4727. i40e_status ret = 0;
  4728. u32 v;
  4729. clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  4730. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  4731. return 0;
  4732. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  4733. /* quiesce the VSIs and their queues that are not already DOWN */
  4734. i40e_pf_quiesce_all_vsi(pf);
  4735. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4736. if (pf->vsi[v])
  4737. pf->vsi[v]->seid = 0;
  4738. }
  4739. i40e_shutdown_adminq(&pf->hw);
  4740. /* call shutdown HMC */
  4741. if (hw->hmc.hmc_obj) {
  4742. ret = i40e_shutdown_lan_hmc(hw);
  4743. if (ret) {
  4744. dev_warn(&pf->pdev->dev,
  4745. "shutdown_lan_hmc failed: %d\n", ret);
  4746. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  4747. }
  4748. }
  4749. return ret;
  4750. }
  4751. /**
  4752. * i40e_send_version - update firmware with driver version
  4753. * @pf: PF struct
  4754. */
  4755. static void i40e_send_version(struct i40e_pf *pf)
  4756. {
  4757. struct i40e_driver_version dv;
  4758. dv.major_version = DRV_VERSION_MAJOR;
  4759. dv.minor_version = DRV_VERSION_MINOR;
  4760. dv.build_version = DRV_VERSION_BUILD;
  4761. dv.subbuild_version = 0;
  4762. strncpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  4763. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  4764. }
  4765. /**
  4766. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  4767. * @pf: board private structure
  4768. * @reinit: if the Main VSI needs to re-initialized.
  4769. **/
  4770. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
  4771. {
  4772. struct i40e_hw *hw = &pf->hw;
  4773. i40e_status ret;
  4774. u32 v;
  4775. /* Now we wait for GRST to settle out.
  4776. * We don't have to delete the VEBs or VSIs from the hw switch
  4777. * because the reset will make them disappear.
  4778. */
  4779. ret = i40e_pf_reset(hw);
  4780. if (ret) {
  4781. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  4782. goto end_core_reset;
  4783. }
  4784. pf->pfr_count++;
  4785. if (test_bit(__I40E_DOWN, &pf->state))
  4786. goto end_core_reset;
  4787. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  4788. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  4789. ret = i40e_init_adminq(&pf->hw);
  4790. if (ret) {
  4791. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
  4792. goto end_core_reset;
  4793. }
  4794. /* re-verify the eeprom if we just had an EMP reset */
  4795. if (test_bit(__I40E_EMP_RESET_REQUESTED, &pf->state)) {
  4796. clear_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
  4797. i40e_verify_eeprom(pf);
  4798. }
  4799. ret = i40e_get_capabilities(pf);
  4800. if (ret) {
  4801. dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
  4802. ret);
  4803. goto end_core_reset;
  4804. }
  4805. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  4806. hw->func_caps.num_rx_qp,
  4807. pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
  4808. if (ret) {
  4809. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  4810. goto end_core_reset;
  4811. }
  4812. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  4813. if (ret) {
  4814. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  4815. goto end_core_reset;
  4816. }
  4817. #ifdef CONFIG_I40E_DCB
  4818. ret = i40e_init_pf_dcb(pf);
  4819. if (ret) {
  4820. dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
  4821. goto end_core_reset;
  4822. }
  4823. #endif /* CONFIG_I40E_DCB */
  4824. /* do basic switch setup */
  4825. ret = i40e_setup_pf_switch(pf, reinit);
  4826. if (ret)
  4827. goto end_core_reset;
  4828. /* Rebuild the VSIs and VEBs that existed before reset.
  4829. * They are still in our local switch element arrays, so only
  4830. * need to rebuild the switch model in the HW.
  4831. *
  4832. * If there were VEBs but the reconstitution failed, we'll try
  4833. * try to recover minimal use by getting the basic PF VSI working.
  4834. */
  4835. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  4836. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  4837. /* find the one VEB connected to the MAC, and find orphans */
  4838. for (v = 0; v < I40E_MAX_VEB; v++) {
  4839. if (!pf->veb[v])
  4840. continue;
  4841. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  4842. pf->veb[v]->uplink_seid == 0) {
  4843. ret = i40e_reconstitute_veb(pf->veb[v]);
  4844. if (!ret)
  4845. continue;
  4846. /* If Main VEB failed, we're in deep doodoo,
  4847. * so give up rebuilding the switch and set up
  4848. * for minimal rebuild of PF VSI.
  4849. * If orphan failed, we'll report the error
  4850. * but try to keep going.
  4851. */
  4852. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  4853. dev_info(&pf->pdev->dev,
  4854. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  4855. ret);
  4856. pf->vsi[pf->lan_vsi]->uplink_seid
  4857. = pf->mac_seid;
  4858. break;
  4859. } else if (pf->veb[v]->uplink_seid == 0) {
  4860. dev_info(&pf->pdev->dev,
  4861. "rebuild of orphan VEB failed: %d\n",
  4862. ret);
  4863. }
  4864. }
  4865. }
  4866. }
  4867. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  4868. dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  4869. /* no VEB, so rebuild only the Main VSI */
  4870. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  4871. if (ret) {
  4872. dev_info(&pf->pdev->dev,
  4873. "rebuild of Main VSI failed: %d\n", ret);
  4874. goto end_core_reset;
  4875. }
  4876. }
  4877. /* reinit the misc interrupt */
  4878. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4879. ret = i40e_setup_misc_vector(pf);
  4880. /* restart the VSIs that were rebuilt and running before the reset */
  4881. i40e_pf_unquiesce_all_vsi(pf);
  4882. if (pf->num_alloc_vfs) {
  4883. for (v = 0; v < pf->num_alloc_vfs; v++)
  4884. i40e_reset_vf(&pf->vf[v], true);
  4885. }
  4886. /* tell the firmware that we're starting */
  4887. i40e_send_version(pf);
  4888. dev_info(&pf->pdev->dev, "reset complete\n");
  4889. end_core_reset:
  4890. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  4891. }
  4892. /**
  4893. * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
  4894. * @pf: board private structure
  4895. *
  4896. * Close up the VFs and other things in prep for a Core Reset,
  4897. * then get ready to rebuild the world.
  4898. **/
  4899. static void i40e_handle_reset_warning(struct i40e_pf *pf)
  4900. {
  4901. i40e_status ret;
  4902. ret = i40e_prep_for_reset(pf);
  4903. if (!ret)
  4904. i40e_reset_and_rebuild(pf, false);
  4905. }
  4906. /**
  4907. * i40e_handle_mdd_event
  4908. * @pf: pointer to the pf structure
  4909. *
  4910. * Called from the MDD irq handler to identify possibly malicious vfs
  4911. **/
  4912. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  4913. {
  4914. struct i40e_hw *hw = &pf->hw;
  4915. bool mdd_detected = false;
  4916. struct i40e_vf *vf;
  4917. u32 reg;
  4918. int i;
  4919. if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
  4920. return;
  4921. /* find what triggered the MDD event */
  4922. reg = rd32(hw, I40E_GL_MDET_TX);
  4923. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  4924. u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
  4925. >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
  4926. u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
  4927. >> I40E_GL_MDET_TX_EVENT_SHIFT;
  4928. u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
  4929. >> I40E_GL_MDET_TX_QUEUE_SHIFT;
  4930. dev_info(&pf->pdev->dev,
  4931. "Malicious Driver Detection event 0x%02x on TX queue %d of function 0x%02x\n",
  4932. event, queue, func);
  4933. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  4934. mdd_detected = true;
  4935. }
  4936. reg = rd32(hw, I40E_GL_MDET_RX);
  4937. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  4938. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
  4939. >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
  4940. u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
  4941. >> I40E_GL_MDET_RX_EVENT_SHIFT;
  4942. u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
  4943. >> I40E_GL_MDET_RX_QUEUE_SHIFT;
  4944. dev_info(&pf->pdev->dev,
  4945. "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  4946. event, queue, func);
  4947. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  4948. mdd_detected = true;
  4949. }
  4950. /* see if one of the VFs needs its hand slapped */
  4951. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  4952. vf = &(pf->vf[i]);
  4953. reg = rd32(hw, I40E_VP_MDET_TX(i));
  4954. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  4955. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  4956. vf->num_mdd_events++;
  4957. dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
  4958. }
  4959. reg = rd32(hw, I40E_VP_MDET_RX(i));
  4960. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  4961. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  4962. vf->num_mdd_events++;
  4963. dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
  4964. }
  4965. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  4966. dev_info(&pf->pdev->dev,
  4967. "Too many MDD events on VF %d, disabled\n", i);
  4968. dev_info(&pf->pdev->dev,
  4969. "Use PF Control I/F to re-enable the VF\n");
  4970. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  4971. }
  4972. }
  4973. /* re-enable mdd interrupt cause */
  4974. clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  4975. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  4976. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  4977. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  4978. i40e_flush(hw);
  4979. }
  4980. #ifdef CONFIG_I40E_VXLAN
  4981. /**
  4982. * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
  4983. * @pf: board private structure
  4984. **/
  4985. static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
  4986. {
  4987. struct i40e_hw *hw = &pf->hw;
  4988. i40e_status ret;
  4989. u8 filter_index;
  4990. __be16 port;
  4991. int i;
  4992. if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
  4993. return;
  4994. pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
  4995. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  4996. if (pf->pending_vxlan_bitmap & (1 << i)) {
  4997. pf->pending_vxlan_bitmap &= ~(1 << i);
  4998. port = pf->vxlan_ports[i];
  4999. ret = port ?
  5000. i40e_aq_add_udp_tunnel(hw, ntohs(port),
  5001. I40E_AQC_TUNNEL_TYPE_VXLAN,
  5002. &filter_index, NULL)
  5003. : i40e_aq_del_udp_tunnel(hw, i, NULL);
  5004. if (ret) {
  5005. dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
  5006. port ? "adding" : "deleting",
  5007. ntohs(port), port ? i : i);
  5008. pf->vxlan_ports[i] = 0;
  5009. } else {
  5010. dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
  5011. port ? "Added" : "Deleted",
  5012. ntohs(port), port ? i : filter_index);
  5013. }
  5014. }
  5015. }
  5016. }
  5017. #endif
  5018. /**
  5019. * i40e_service_task - Run the driver's async subtasks
  5020. * @work: pointer to work_struct containing our data
  5021. **/
  5022. static void i40e_service_task(struct work_struct *work)
  5023. {
  5024. struct i40e_pf *pf = container_of(work,
  5025. struct i40e_pf,
  5026. service_task);
  5027. unsigned long start_time = jiffies;
  5028. i40e_reset_subtask(pf);
  5029. i40e_handle_mdd_event(pf);
  5030. i40e_vc_process_vflr_event(pf);
  5031. i40e_watchdog_subtask(pf);
  5032. i40e_fdir_reinit_subtask(pf);
  5033. i40e_check_hang_subtask(pf);
  5034. i40e_sync_filters_subtask(pf);
  5035. #ifdef CONFIG_I40E_VXLAN
  5036. i40e_sync_vxlan_filters_subtask(pf);
  5037. #endif
  5038. i40e_clean_adminq_subtask(pf);
  5039. i40e_service_event_complete(pf);
  5040. /* If the tasks have taken longer than one timer cycle or there
  5041. * is more work to be done, reschedule the service task now
  5042. * rather than wait for the timer to tick again.
  5043. */
  5044. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  5045. test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
  5046. test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
  5047. test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  5048. i40e_service_event_schedule(pf);
  5049. }
  5050. /**
  5051. * i40e_service_timer - timer callback
  5052. * @data: pointer to PF struct
  5053. **/
  5054. static void i40e_service_timer(unsigned long data)
  5055. {
  5056. struct i40e_pf *pf = (struct i40e_pf *)data;
  5057. mod_timer(&pf->service_timer,
  5058. round_jiffies(jiffies + pf->service_timer_period));
  5059. i40e_service_event_schedule(pf);
  5060. }
  5061. /**
  5062. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  5063. * @vsi: the VSI being configured
  5064. **/
  5065. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  5066. {
  5067. struct i40e_pf *pf = vsi->back;
  5068. switch (vsi->type) {
  5069. case I40E_VSI_MAIN:
  5070. vsi->alloc_queue_pairs = pf->num_lan_qps;
  5071. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  5072. I40E_REQ_DESCRIPTOR_MULTIPLE);
  5073. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5074. vsi->num_q_vectors = pf->num_lan_msix;
  5075. else
  5076. vsi->num_q_vectors = 1;
  5077. break;
  5078. case I40E_VSI_FDIR:
  5079. vsi->alloc_queue_pairs = 1;
  5080. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  5081. I40E_REQ_DESCRIPTOR_MULTIPLE);
  5082. vsi->num_q_vectors = 1;
  5083. break;
  5084. case I40E_VSI_VMDQ2:
  5085. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  5086. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  5087. I40E_REQ_DESCRIPTOR_MULTIPLE);
  5088. vsi->num_q_vectors = pf->num_vmdq_msix;
  5089. break;
  5090. case I40E_VSI_SRIOV:
  5091. vsi->alloc_queue_pairs = pf->num_vf_qps;
  5092. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  5093. I40E_REQ_DESCRIPTOR_MULTIPLE);
  5094. break;
  5095. default:
  5096. WARN_ON(1);
  5097. return -ENODATA;
  5098. }
  5099. return 0;
  5100. }
  5101. /**
  5102. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  5103. * @type: VSI pointer
  5104. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  5105. *
  5106. * On error: returns error code (negative)
  5107. * On success: returns 0
  5108. **/
  5109. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  5110. {
  5111. int size;
  5112. int ret = 0;
  5113. /* allocate memory for both Tx and Rx ring pointers */
  5114. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
  5115. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  5116. if (!vsi->tx_rings)
  5117. return -ENOMEM;
  5118. vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
  5119. if (alloc_qvectors) {
  5120. /* allocate memory for q_vector pointers */
  5121. size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
  5122. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  5123. if (!vsi->q_vectors) {
  5124. ret = -ENOMEM;
  5125. goto err_vectors;
  5126. }
  5127. }
  5128. return ret;
  5129. err_vectors:
  5130. kfree(vsi->tx_rings);
  5131. return ret;
  5132. }
  5133. /**
  5134. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  5135. * @pf: board private structure
  5136. * @type: type of VSI
  5137. *
  5138. * On error: returns error code (negative)
  5139. * On success: returns vsi index in PF (positive)
  5140. **/
  5141. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  5142. {
  5143. int ret = -ENODEV;
  5144. struct i40e_vsi *vsi;
  5145. int vsi_idx;
  5146. int i;
  5147. /* Need to protect the allocation of the VSIs at the PF level */
  5148. mutex_lock(&pf->switch_mutex);
  5149. /* VSI list may be fragmented if VSI creation/destruction has
  5150. * been happening. We can afford to do a quick scan to look
  5151. * for any free VSIs in the list.
  5152. *
  5153. * find next empty vsi slot, looping back around if necessary
  5154. */
  5155. i = pf->next_vsi;
  5156. while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
  5157. i++;
  5158. if (i >= pf->hw.func_caps.num_vsis) {
  5159. i = 0;
  5160. while (i < pf->next_vsi && pf->vsi[i])
  5161. i++;
  5162. }
  5163. if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
  5164. vsi_idx = i; /* Found one! */
  5165. } else {
  5166. ret = -ENODEV;
  5167. goto unlock_pf; /* out of VSI slots! */
  5168. }
  5169. pf->next_vsi = ++i;
  5170. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  5171. if (!vsi) {
  5172. ret = -ENOMEM;
  5173. goto unlock_pf;
  5174. }
  5175. vsi->type = type;
  5176. vsi->back = pf;
  5177. set_bit(__I40E_DOWN, &vsi->state);
  5178. vsi->flags = 0;
  5179. vsi->idx = vsi_idx;
  5180. vsi->rx_itr_setting = pf->rx_itr_default;
  5181. vsi->tx_itr_setting = pf->tx_itr_default;
  5182. vsi->netdev_registered = false;
  5183. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  5184. INIT_LIST_HEAD(&vsi->mac_filter_list);
  5185. vsi->irqs_ready = false;
  5186. ret = i40e_set_num_rings_in_vsi(vsi);
  5187. if (ret)
  5188. goto err_rings;
  5189. ret = i40e_vsi_alloc_arrays(vsi, true);
  5190. if (ret)
  5191. goto err_rings;
  5192. /* Setup default MSIX irq handler for VSI */
  5193. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  5194. pf->vsi[vsi_idx] = vsi;
  5195. ret = vsi_idx;
  5196. goto unlock_pf;
  5197. err_rings:
  5198. pf->next_vsi = i - 1;
  5199. kfree(vsi);
  5200. unlock_pf:
  5201. mutex_unlock(&pf->switch_mutex);
  5202. return ret;
  5203. }
  5204. /**
  5205. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  5206. * @type: VSI pointer
  5207. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  5208. *
  5209. * On error: returns error code (negative)
  5210. * On success: returns 0
  5211. **/
  5212. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  5213. {
  5214. /* free the ring and vector containers */
  5215. if (free_qvectors) {
  5216. kfree(vsi->q_vectors);
  5217. vsi->q_vectors = NULL;
  5218. }
  5219. kfree(vsi->tx_rings);
  5220. vsi->tx_rings = NULL;
  5221. vsi->rx_rings = NULL;
  5222. }
  5223. /**
  5224. * i40e_vsi_clear - Deallocate the VSI provided
  5225. * @vsi: the VSI being un-configured
  5226. **/
  5227. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  5228. {
  5229. struct i40e_pf *pf;
  5230. if (!vsi)
  5231. return 0;
  5232. if (!vsi->back)
  5233. goto free_vsi;
  5234. pf = vsi->back;
  5235. mutex_lock(&pf->switch_mutex);
  5236. if (!pf->vsi[vsi->idx]) {
  5237. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  5238. vsi->idx, vsi->idx, vsi, vsi->type);
  5239. goto unlock_vsi;
  5240. }
  5241. if (pf->vsi[vsi->idx] != vsi) {
  5242. dev_err(&pf->pdev->dev,
  5243. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  5244. pf->vsi[vsi->idx]->idx,
  5245. pf->vsi[vsi->idx],
  5246. pf->vsi[vsi->idx]->type,
  5247. vsi->idx, vsi, vsi->type);
  5248. goto unlock_vsi;
  5249. }
  5250. /* updates the pf for this cleared vsi */
  5251. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  5252. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  5253. i40e_vsi_free_arrays(vsi, true);
  5254. pf->vsi[vsi->idx] = NULL;
  5255. if (vsi->idx < pf->next_vsi)
  5256. pf->next_vsi = vsi->idx;
  5257. unlock_vsi:
  5258. mutex_unlock(&pf->switch_mutex);
  5259. free_vsi:
  5260. kfree(vsi);
  5261. return 0;
  5262. }
  5263. /**
  5264. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  5265. * @vsi: the VSI being cleaned
  5266. **/
  5267. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  5268. {
  5269. int i;
  5270. if (vsi->tx_rings && vsi->tx_rings[0]) {
  5271. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  5272. kfree_rcu(vsi->tx_rings[i], rcu);
  5273. vsi->tx_rings[i] = NULL;
  5274. vsi->rx_rings[i] = NULL;
  5275. }
  5276. }
  5277. }
  5278. /**
  5279. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  5280. * @vsi: the VSI being configured
  5281. **/
  5282. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  5283. {
  5284. struct i40e_ring *tx_ring, *rx_ring;
  5285. struct i40e_pf *pf = vsi->back;
  5286. int i;
  5287. /* Set basic values in the rings to be used later during open() */
  5288. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  5289. /* allocate space for both Tx and Rx in one shot */
  5290. tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
  5291. if (!tx_ring)
  5292. goto err_out;
  5293. tx_ring->queue_index = i;
  5294. tx_ring->reg_idx = vsi->base_queue + i;
  5295. tx_ring->ring_active = false;
  5296. tx_ring->vsi = vsi;
  5297. tx_ring->netdev = vsi->netdev;
  5298. tx_ring->dev = &pf->pdev->dev;
  5299. tx_ring->count = vsi->num_desc;
  5300. tx_ring->size = 0;
  5301. tx_ring->dcb_tc = 0;
  5302. vsi->tx_rings[i] = tx_ring;
  5303. rx_ring = &tx_ring[1];
  5304. rx_ring->queue_index = i;
  5305. rx_ring->reg_idx = vsi->base_queue + i;
  5306. rx_ring->ring_active = false;
  5307. rx_ring->vsi = vsi;
  5308. rx_ring->netdev = vsi->netdev;
  5309. rx_ring->dev = &pf->pdev->dev;
  5310. rx_ring->count = vsi->num_desc;
  5311. rx_ring->size = 0;
  5312. rx_ring->dcb_tc = 0;
  5313. if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
  5314. set_ring_16byte_desc_enabled(rx_ring);
  5315. else
  5316. clear_ring_16byte_desc_enabled(rx_ring);
  5317. vsi->rx_rings[i] = rx_ring;
  5318. }
  5319. return 0;
  5320. err_out:
  5321. i40e_vsi_clear_rings(vsi);
  5322. return -ENOMEM;
  5323. }
  5324. /**
  5325. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  5326. * @pf: board private structure
  5327. * @vectors: the number of MSI-X vectors to request
  5328. *
  5329. * Returns the number of vectors reserved, or error
  5330. **/
  5331. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  5332. {
  5333. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  5334. I40E_MIN_MSIX, vectors);
  5335. if (vectors < 0) {
  5336. dev_info(&pf->pdev->dev,
  5337. "MSI-X vector reservation failed: %d\n", vectors);
  5338. vectors = 0;
  5339. }
  5340. return vectors;
  5341. }
  5342. /**
  5343. * i40e_init_msix - Setup the MSIX capability
  5344. * @pf: board private structure
  5345. *
  5346. * Work with the OS to set up the MSIX vectors needed.
  5347. *
  5348. * Returns 0 on success, negative on failure
  5349. **/
  5350. static int i40e_init_msix(struct i40e_pf *pf)
  5351. {
  5352. i40e_status err = 0;
  5353. struct i40e_hw *hw = &pf->hw;
  5354. int v_budget, i;
  5355. int vec;
  5356. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  5357. return -ENODEV;
  5358. /* The number of vectors we'll request will be comprised of:
  5359. * - Add 1 for "other" cause for Admin Queue events, etc.
  5360. * - The number of LAN queue pairs
  5361. * - Queues being used for RSS.
  5362. * We don't need as many as max_rss_size vectors.
  5363. * use rss_size instead in the calculation since that
  5364. * is governed by number of cpus in the system.
  5365. * - assumes symmetric Tx/Rx pairing
  5366. * - The number of VMDq pairs
  5367. * Once we count this up, try the request.
  5368. *
  5369. * If we can't get what we want, we'll simplify to nearly nothing
  5370. * and try again. If that still fails, we punt.
  5371. */
  5372. pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
  5373. pf->num_vmdq_msix = pf->num_vmdq_qps;
  5374. v_budget = 1 + pf->num_lan_msix;
  5375. v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
  5376. if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
  5377. v_budget++;
  5378. /* Scale down if necessary, and the rings will share vectors */
  5379. v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
  5380. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  5381. GFP_KERNEL);
  5382. if (!pf->msix_entries)
  5383. return -ENOMEM;
  5384. for (i = 0; i < v_budget; i++)
  5385. pf->msix_entries[i].entry = i;
  5386. vec = i40e_reserve_msix_vectors(pf, v_budget);
  5387. if (vec < I40E_MIN_MSIX) {
  5388. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  5389. kfree(pf->msix_entries);
  5390. pf->msix_entries = NULL;
  5391. return -ENODEV;
  5392. } else if (vec == I40E_MIN_MSIX) {
  5393. /* Adjust for minimal MSIX use */
  5394. dev_info(&pf->pdev->dev, "Features disabled, not enough MSI-X vectors\n");
  5395. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  5396. pf->num_vmdq_vsis = 0;
  5397. pf->num_vmdq_qps = 0;
  5398. pf->num_vmdq_msix = 0;
  5399. pf->num_lan_qps = 1;
  5400. pf->num_lan_msix = 1;
  5401. } else if (vec != v_budget) {
  5402. /* Scale vector usage down */
  5403. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  5404. vec--; /* reserve the misc vector */
  5405. /* partition out the remaining vectors */
  5406. switch (vec) {
  5407. case 2:
  5408. pf->num_vmdq_vsis = 1;
  5409. pf->num_lan_msix = 1;
  5410. break;
  5411. case 3:
  5412. pf->num_vmdq_vsis = 1;
  5413. pf->num_lan_msix = 2;
  5414. break;
  5415. default:
  5416. pf->num_lan_msix = min_t(int, (vec / 2),
  5417. pf->num_lan_qps);
  5418. pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
  5419. I40E_DEFAULT_NUM_VMDQ_VSI);
  5420. break;
  5421. }
  5422. }
  5423. return err;
  5424. }
  5425. /**
  5426. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  5427. * @vsi: the VSI being configured
  5428. * @v_idx: index of the vector in the vsi struct
  5429. *
  5430. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  5431. **/
  5432. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
  5433. {
  5434. struct i40e_q_vector *q_vector;
  5435. /* allocate q_vector */
  5436. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  5437. if (!q_vector)
  5438. return -ENOMEM;
  5439. q_vector->vsi = vsi;
  5440. q_vector->v_idx = v_idx;
  5441. cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
  5442. if (vsi->netdev)
  5443. netif_napi_add(vsi->netdev, &q_vector->napi,
  5444. i40e_napi_poll, vsi->work_limit);
  5445. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  5446. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  5447. /* tie q_vector and vsi together */
  5448. vsi->q_vectors[v_idx] = q_vector;
  5449. return 0;
  5450. }
  5451. /**
  5452. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  5453. * @vsi: the VSI being configured
  5454. *
  5455. * We allocate one q_vector per queue interrupt. If allocation fails we
  5456. * return -ENOMEM.
  5457. **/
  5458. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  5459. {
  5460. struct i40e_pf *pf = vsi->back;
  5461. int v_idx, num_q_vectors;
  5462. int err;
  5463. /* if not MSIX, give the one vector only to the LAN VSI */
  5464. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5465. num_q_vectors = vsi->num_q_vectors;
  5466. else if (vsi == pf->vsi[pf->lan_vsi])
  5467. num_q_vectors = 1;
  5468. else
  5469. return -EINVAL;
  5470. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  5471. err = i40e_vsi_alloc_q_vector(vsi, v_idx);
  5472. if (err)
  5473. goto err_out;
  5474. }
  5475. return 0;
  5476. err_out:
  5477. while (v_idx--)
  5478. i40e_free_q_vector(vsi, v_idx);
  5479. return err;
  5480. }
  5481. /**
  5482. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  5483. * @pf: board private structure to initialize
  5484. **/
  5485. static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
  5486. {
  5487. int err = 0;
  5488. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  5489. err = i40e_init_msix(pf);
  5490. if (err) {
  5491. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  5492. I40E_FLAG_RSS_ENABLED |
  5493. I40E_FLAG_DCB_ENABLED |
  5494. I40E_FLAG_SRIOV_ENABLED |
  5495. I40E_FLAG_FD_SB_ENABLED |
  5496. I40E_FLAG_FD_ATR_ENABLED |
  5497. I40E_FLAG_VMDQ_ENABLED);
  5498. /* rework the queue expectations without MSIX */
  5499. i40e_determine_queue_usage(pf);
  5500. }
  5501. }
  5502. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  5503. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  5504. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  5505. err = pci_enable_msi(pf->pdev);
  5506. if (err) {
  5507. dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
  5508. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  5509. }
  5510. }
  5511. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  5512. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  5513. /* track first vector for misc interrupts */
  5514. err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
  5515. }
  5516. /**
  5517. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  5518. * @pf: board private structure
  5519. *
  5520. * This sets up the handler for MSIX 0, which is used to manage the
  5521. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  5522. * when in MSI or Legacy interrupt mode.
  5523. **/
  5524. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  5525. {
  5526. struct i40e_hw *hw = &pf->hw;
  5527. int err = 0;
  5528. /* Only request the irq if this is the first time through, and
  5529. * not when we're rebuilding after a Reset
  5530. */
  5531. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  5532. err = request_irq(pf->msix_entries[0].vector,
  5533. i40e_intr, 0, pf->misc_int_name, pf);
  5534. if (err) {
  5535. dev_info(&pf->pdev->dev,
  5536. "request_irq for %s failed: %d\n",
  5537. pf->misc_int_name, err);
  5538. return -EFAULT;
  5539. }
  5540. }
  5541. i40e_enable_misc_int_causes(hw);
  5542. /* associate no queues to the misc vector */
  5543. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  5544. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  5545. i40e_flush(hw);
  5546. i40e_irq_dynamic_enable_icr0(pf);
  5547. return err;
  5548. }
  5549. /**
  5550. * i40e_config_rss - Prepare for RSS if used
  5551. * @pf: board private structure
  5552. **/
  5553. static int i40e_config_rss(struct i40e_pf *pf)
  5554. {
  5555. /* Set of random keys generated using kernel random number generator */
  5556. static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
  5557. 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
  5558. 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
  5559. 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
  5560. struct i40e_hw *hw = &pf->hw;
  5561. u32 lut = 0;
  5562. int i, j;
  5563. u64 hena;
  5564. /* Fill out hash function seed */
  5565. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  5566. wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
  5567. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  5568. hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
  5569. ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
  5570. hena |= I40E_DEFAULT_RSS_HENA;
  5571. wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
  5572. wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  5573. /* Populate the LUT with max no. of queues in round robin fashion */
  5574. for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
  5575. /* The assumption is that lan qp count will be the highest
  5576. * qp count for any PF VSI that needs RSS.
  5577. * If multiple VSIs need RSS support, all the qp counts
  5578. * for those VSIs should be a power of 2 for RSS to work.
  5579. * If LAN VSI is the only consumer for RSS then this requirement
  5580. * is not necessary.
  5581. */
  5582. if (j == pf->rss_size)
  5583. j = 0;
  5584. /* lut = 4-byte sliding window of 4 lut entries */
  5585. lut = (lut << 8) | (j &
  5586. ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
  5587. /* On i = 3, we have 4 entries in lut; write to the register */
  5588. if ((i & 3) == 3)
  5589. wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
  5590. }
  5591. i40e_flush(hw);
  5592. return 0;
  5593. }
  5594. /**
  5595. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  5596. * @pf: board private structure
  5597. * @queue_count: the requested queue count for rss.
  5598. *
  5599. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  5600. * count which may be different from the requested queue count.
  5601. **/
  5602. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  5603. {
  5604. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  5605. return 0;
  5606. queue_count = min_t(int, queue_count, pf->rss_size_max);
  5607. queue_count = rounddown_pow_of_two(queue_count);
  5608. if (queue_count != pf->rss_size) {
  5609. i40e_prep_for_reset(pf);
  5610. pf->rss_size = queue_count;
  5611. i40e_reset_and_rebuild(pf, true);
  5612. i40e_config_rss(pf);
  5613. }
  5614. dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
  5615. return pf->rss_size;
  5616. }
  5617. /**
  5618. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  5619. * @pf: board private structure to initialize
  5620. *
  5621. * i40e_sw_init initializes the Adapter private data structure.
  5622. * Fields are initialized based on PCI device information and
  5623. * OS network device settings (MTU size).
  5624. **/
  5625. static int i40e_sw_init(struct i40e_pf *pf)
  5626. {
  5627. int err = 0;
  5628. int size;
  5629. pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
  5630. (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
  5631. pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
  5632. if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
  5633. if (I40E_DEBUG_USER & debug)
  5634. pf->hw.debug_mask = debug;
  5635. pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
  5636. I40E_DEFAULT_MSG_ENABLE);
  5637. }
  5638. /* Set default capability flags */
  5639. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  5640. I40E_FLAG_MSI_ENABLED |
  5641. I40E_FLAG_MSIX_ENABLED |
  5642. I40E_FLAG_RX_1BUF_ENABLED;
  5643. /* Set default ITR */
  5644. pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
  5645. pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
  5646. /* Depending on PF configurations, it is possible that the RSS
  5647. * maximum might end up larger than the available queues
  5648. */
  5649. pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
  5650. pf->rss_size_max = min_t(int, pf->rss_size_max,
  5651. pf->hw.func_caps.num_tx_qp);
  5652. if (pf->hw.func_caps.rss) {
  5653. pf->flags |= I40E_FLAG_RSS_ENABLED;
  5654. pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
  5655. pf->rss_size = rounddown_pow_of_two(pf->rss_size);
  5656. } else {
  5657. pf->rss_size = 1;
  5658. }
  5659. /* MFP mode enabled */
  5660. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
  5661. pf->flags |= I40E_FLAG_MFP_ENABLED;
  5662. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  5663. }
  5664. /* FW/NVM is not yet fixed in this regard */
  5665. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  5666. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  5667. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  5668. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  5669. if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  5670. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  5671. } else {
  5672. dev_info(&pf->pdev->dev,
  5673. "Flow Director Sideband mode Disabled in MFP mode\n");
  5674. }
  5675. pf->fdir_pf_filter_count =
  5676. pf->hw.func_caps.fd_filters_guaranteed;
  5677. pf->hw.fdir_shared_filter_count =
  5678. pf->hw.func_caps.fd_filters_best_effort;
  5679. }
  5680. if (pf->hw.func_caps.vmdq) {
  5681. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  5682. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  5683. pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
  5684. }
  5685. #ifdef CONFIG_PCI_IOV
  5686. if (pf->hw.func_caps.num_vfs) {
  5687. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  5688. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  5689. pf->num_req_vfs = min_t(int,
  5690. pf->hw.func_caps.num_vfs,
  5691. I40E_MAX_VF_COUNT);
  5692. }
  5693. #endif /* CONFIG_PCI_IOV */
  5694. pf->eeprom_version = 0xDEAD;
  5695. pf->lan_veb = I40E_NO_VEB;
  5696. pf->lan_vsi = I40E_NO_VSI;
  5697. /* set up queue assignment tracking */
  5698. size = sizeof(struct i40e_lump_tracking)
  5699. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  5700. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  5701. if (!pf->qp_pile) {
  5702. err = -ENOMEM;
  5703. goto sw_init_done;
  5704. }
  5705. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  5706. pf->qp_pile->search_hint = 0;
  5707. /* set up vector assignment tracking */
  5708. size = sizeof(struct i40e_lump_tracking)
  5709. + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
  5710. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  5711. if (!pf->irq_pile) {
  5712. kfree(pf->qp_pile);
  5713. err = -ENOMEM;
  5714. goto sw_init_done;
  5715. }
  5716. pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
  5717. pf->irq_pile->search_hint = 0;
  5718. mutex_init(&pf->switch_mutex);
  5719. sw_init_done:
  5720. return err;
  5721. }
  5722. /**
  5723. * i40e_set_ntuple - set the ntuple feature flag and take action
  5724. * @pf: board private structure to initialize
  5725. * @features: the feature set that the stack is suggesting
  5726. *
  5727. * returns a bool to indicate if reset needs to happen
  5728. **/
  5729. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  5730. {
  5731. bool need_reset = false;
  5732. /* Check if Flow Director n-tuple support was enabled or disabled. If
  5733. * the state changed, we need to reset.
  5734. */
  5735. if (features & NETIF_F_NTUPLE) {
  5736. /* Enable filters and mark for reset */
  5737. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  5738. need_reset = true;
  5739. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  5740. } else {
  5741. /* turn off filters, mark for reset and clear SW filter list */
  5742. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  5743. need_reset = true;
  5744. i40e_fdir_filter_exit(pf);
  5745. }
  5746. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5747. /* if ATR was disabled it can be re-enabled. */
  5748. if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
  5749. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  5750. }
  5751. return need_reset;
  5752. }
  5753. /**
  5754. * i40e_set_features - set the netdev feature flags
  5755. * @netdev: ptr to the netdev being adjusted
  5756. * @features: the feature set that the stack is suggesting
  5757. **/
  5758. static int i40e_set_features(struct net_device *netdev,
  5759. netdev_features_t features)
  5760. {
  5761. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5762. struct i40e_vsi *vsi = np->vsi;
  5763. struct i40e_pf *pf = vsi->back;
  5764. bool need_reset;
  5765. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  5766. i40e_vlan_stripping_enable(vsi);
  5767. else
  5768. i40e_vlan_stripping_disable(vsi);
  5769. need_reset = i40e_set_ntuple(pf, features);
  5770. if (need_reset)
  5771. i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
  5772. return 0;
  5773. }
  5774. #ifdef CONFIG_I40E_VXLAN
  5775. /**
  5776. * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
  5777. * @pf: board private structure
  5778. * @port: The UDP port to look up
  5779. *
  5780. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  5781. **/
  5782. static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
  5783. {
  5784. u8 i;
  5785. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  5786. if (pf->vxlan_ports[i] == port)
  5787. return i;
  5788. }
  5789. return i;
  5790. }
  5791. /**
  5792. * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
  5793. * @netdev: This physical port's netdev
  5794. * @sa_family: Socket Family that VXLAN is notifying us about
  5795. * @port: New UDP port number that VXLAN started listening to
  5796. **/
  5797. static void i40e_add_vxlan_port(struct net_device *netdev,
  5798. sa_family_t sa_family, __be16 port)
  5799. {
  5800. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5801. struct i40e_vsi *vsi = np->vsi;
  5802. struct i40e_pf *pf = vsi->back;
  5803. u8 next_idx;
  5804. u8 idx;
  5805. if (sa_family == AF_INET6)
  5806. return;
  5807. idx = i40e_get_vxlan_port_idx(pf, port);
  5808. /* Check if port already exists */
  5809. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  5810. netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
  5811. return;
  5812. }
  5813. /* Now check if there is space to add the new port */
  5814. next_idx = i40e_get_vxlan_port_idx(pf, 0);
  5815. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  5816. netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
  5817. ntohs(port));
  5818. return;
  5819. }
  5820. /* New port: add it and mark its index in the bitmap */
  5821. pf->vxlan_ports[next_idx] = port;
  5822. pf->pending_vxlan_bitmap |= (1 << next_idx);
  5823. pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
  5824. }
  5825. /**
  5826. * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
  5827. * @netdev: This physical port's netdev
  5828. * @sa_family: Socket Family that VXLAN is notifying us about
  5829. * @port: UDP port number that VXLAN stopped listening to
  5830. **/
  5831. static void i40e_del_vxlan_port(struct net_device *netdev,
  5832. sa_family_t sa_family, __be16 port)
  5833. {
  5834. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5835. struct i40e_vsi *vsi = np->vsi;
  5836. struct i40e_pf *pf = vsi->back;
  5837. u8 idx;
  5838. if (sa_family == AF_INET6)
  5839. return;
  5840. idx = i40e_get_vxlan_port_idx(pf, port);
  5841. /* Check if port already exists */
  5842. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  5843. /* if port exists, set it to 0 (mark for deletion)
  5844. * and make it pending
  5845. */
  5846. pf->vxlan_ports[idx] = 0;
  5847. pf->pending_vxlan_bitmap |= (1 << idx);
  5848. pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
  5849. } else {
  5850. netdev_warn(netdev, "Port %d was not found, not deleting\n",
  5851. ntohs(port));
  5852. }
  5853. }
  5854. #endif
  5855. #ifdef HAVE_FDB_OPS
  5856. #ifdef USE_CONST_DEV_UC_CHAR
  5857. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  5858. struct net_device *dev,
  5859. const unsigned char *addr,
  5860. u16 flags)
  5861. #else
  5862. static int i40e_ndo_fdb_add(struct ndmsg *ndm,
  5863. struct net_device *dev,
  5864. unsigned char *addr,
  5865. u16 flags)
  5866. #endif
  5867. {
  5868. struct i40e_netdev_priv *np = netdev_priv(dev);
  5869. struct i40e_pf *pf = np->vsi->back;
  5870. int err = 0;
  5871. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  5872. return -EOPNOTSUPP;
  5873. /* Hardware does not support aging addresses so if a
  5874. * ndm_state is given only allow permanent addresses
  5875. */
  5876. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  5877. netdev_info(dev, "FDB only supports static addresses\n");
  5878. return -EINVAL;
  5879. }
  5880. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  5881. err = dev_uc_add_excl(dev, addr);
  5882. else if (is_multicast_ether_addr(addr))
  5883. err = dev_mc_add_excl(dev, addr);
  5884. else
  5885. err = -EINVAL;
  5886. /* Only return duplicate errors if NLM_F_EXCL is set */
  5887. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  5888. err = 0;
  5889. return err;
  5890. }
  5891. #ifndef USE_DEFAULT_FDB_DEL_DUMP
  5892. #ifdef USE_CONST_DEV_UC_CHAR
  5893. static int i40e_ndo_fdb_del(struct ndmsg *ndm,
  5894. struct net_device *dev,
  5895. const unsigned char *addr)
  5896. #else
  5897. static int i40e_ndo_fdb_del(struct ndmsg *ndm,
  5898. struct net_device *dev,
  5899. unsigned char *addr)
  5900. #endif
  5901. {
  5902. struct i40e_netdev_priv *np = netdev_priv(dev);
  5903. struct i40e_pf *pf = np->vsi->back;
  5904. int err = -EOPNOTSUPP;
  5905. if (ndm->ndm_state & NUD_PERMANENT) {
  5906. netdev_info(dev, "FDB only supports static addresses\n");
  5907. return -EINVAL;
  5908. }
  5909. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  5910. if (is_unicast_ether_addr(addr))
  5911. err = dev_uc_del(dev, addr);
  5912. else if (is_multicast_ether_addr(addr))
  5913. err = dev_mc_del(dev, addr);
  5914. else
  5915. err = -EINVAL;
  5916. }
  5917. return err;
  5918. }
  5919. static int i40e_ndo_fdb_dump(struct sk_buff *skb,
  5920. struct netlink_callback *cb,
  5921. struct net_device *dev,
  5922. int idx)
  5923. {
  5924. struct i40e_netdev_priv *np = netdev_priv(dev);
  5925. struct i40e_pf *pf = np->vsi->back;
  5926. if (pf->flags & I40E_FLAG_SRIOV_ENABLED)
  5927. idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
  5928. return idx;
  5929. }
  5930. #endif /* USE_DEFAULT_FDB_DEL_DUMP */
  5931. #endif /* HAVE_FDB_OPS */
  5932. static const struct net_device_ops i40e_netdev_ops = {
  5933. .ndo_open = i40e_open,
  5934. .ndo_stop = i40e_close,
  5935. .ndo_start_xmit = i40e_lan_xmit_frame,
  5936. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  5937. .ndo_set_rx_mode = i40e_set_rx_mode,
  5938. .ndo_validate_addr = eth_validate_addr,
  5939. .ndo_set_mac_address = i40e_set_mac,
  5940. .ndo_change_mtu = i40e_change_mtu,
  5941. .ndo_do_ioctl = i40e_ioctl,
  5942. .ndo_tx_timeout = i40e_tx_timeout,
  5943. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  5944. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  5945. #ifdef CONFIG_NET_POLL_CONTROLLER
  5946. .ndo_poll_controller = i40e_netpoll,
  5947. #endif
  5948. .ndo_setup_tc = i40e_setup_tc,
  5949. .ndo_set_features = i40e_set_features,
  5950. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  5951. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  5952. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  5953. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  5954. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  5955. #ifdef CONFIG_I40E_VXLAN
  5956. .ndo_add_vxlan_port = i40e_add_vxlan_port,
  5957. .ndo_del_vxlan_port = i40e_del_vxlan_port,
  5958. #endif
  5959. #ifdef HAVE_FDB_OPS
  5960. .ndo_fdb_add = i40e_ndo_fdb_add,
  5961. #ifndef USE_DEFAULT_FDB_DEL_DUMP
  5962. .ndo_fdb_del = i40e_ndo_fdb_del,
  5963. .ndo_fdb_dump = i40e_ndo_fdb_dump,
  5964. #endif
  5965. #endif
  5966. };
  5967. /**
  5968. * i40e_config_netdev - Setup the netdev flags
  5969. * @vsi: the VSI being configured
  5970. *
  5971. * Returns 0 on success, negative value on failure
  5972. **/
  5973. static int i40e_config_netdev(struct i40e_vsi *vsi)
  5974. {
  5975. u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  5976. struct i40e_pf *pf = vsi->back;
  5977. struct i40e_hw *hw = &pf->hw;
  5978. struct i40e_netdev_priv *np;
  5979. struct net_device *netdev;
  5980. u8 mac_addr[ETH_ALEN];
  5981. int etherdev_size;
  5982. etherdev_size = sizeof(struct i40e_netdev_priv);
  5983. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  5984. if (!netdev)
  5985. return -ENOMEM;
  5986. vsi->netdev = netdev;
  5987. np = netdev_priv(netdev);
  5988. np->vsi = vsi;
  5989. netdev->hw_enc_features |= NETIF_F_IP_CSUM |
  5990. NETIF_F_GSO_UDP_TUNNEL |
  5991. NETIF_F_TSO;
  5992. netdev->features = NETIF_F_SG |
  5993. NETIF_F_IP_CSUM |
  5994. NETIF_F_SCTP_CSUM |
  5995. NETIF_F_HIGHDMA |
  5996. NETIF_F_GSO_UDP_TUNNEL |
  5997. NETIF_F_HW_VLAN_CTAG_TX |
  5998. NETIF_F_HW_VLAN_CTAG_RX |
  5999. NETIF_F_HW_VLAN_CTAG_FILTER |
  6000. NETIF_F_IPV6_CSUM |
  6001. NETIF_F_TSO |
  6002. NETIF_F_TSO_ECN |
  6003. NETIF_F_TSO6 |
  6004. NETIF_F_RXCSUM |
  6005. NETIF_F_RXHASH |
  6006. 0;
  6007. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  6008. netdev->features |= NETIF_F_NTUPLE;
  6009. /* copy netdev features into list of user selectable features */
  6010. netdev->hw_features |= netdev->features;
  6011. if (vsi->type == I40E_VSI_MAIN) {
  6012. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  6013. memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
  6014. } else {
  6015. /* relate the VSI_VMDQ name to the VSI_MAIN name */
  6016. snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
  6017. pf->vsi[pf->lan_vsi]->netdev->name);
  6018. random_ether_addr(mac_addr);
  6019. i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
  6020. }
  6021. i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
  6022. memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
  6023. memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
  6024. /* vlan gets same features (except vlan offload)
  6025. * after any tweaks for specific VSI types
  6026. */
  6027. netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
  6028. NETIF_F_HW_VLAN_CTAG_RX |
  6029. NETIF_F_HW_VLAN_CTAG_FILTER);
  6030. netdev->priv_flags |= IFF_UNICAST_FLT;
  6031. netdev->priv_flags |= IFF_SUPP_NOFCS;
  6032. /* Setup netdev TC information */
  6033. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  6034. netdev->netdev_ops = &i40e_netdev_ops;
  6035. netdev->watchdog_timeo = 5 * HZ;
  6036. i40e_set_ethtool_ops(netdev);
  6037. return 0;
  6038. }
  6039. /**
  6040. * i40e_vsi_delete - Delete a VSI from the switch
  6041. * @vsi: the VSI being removed
  6042. *
  6043. * Returns 0 on success, negative value on failure
  6044. **/
  6045. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  6046. {
  6047. /* remove default VSI is not allowed */
  6048. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  6049. return;
  6050. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  6051. }
  6052. /**
  6053. * i40e_add_vsi - Add a VSI to the switch
  6054. * @vsi: the VSI being configured
  6055. *
  6056. * This initializes a VSI context depending on the VSI type to be added and
  6057. * passes it down to the add_vsi aq command.
  6058. **/
  6059. static int i40e_add_vsi(struct i40e_vsi *vsi)
  6060. {
  6061. int ret = -ENODEV;
  6062. struct i40e_mac_filter *f, *ftmp;
  6063. struct i40e_pf *pf = vsi->back;
  6064. struct i40e_hw *hw = &pf->hw;
  6065. struct i40e_vsi_context ctxt;
  6066. u8 enabled_tc = 0x1; /* TC0 enabled */
  6067. int f_count = 0;
  6068. memset(&ctxt, 0, sizeof(ctxt));
  6069. switch (vsi->type) {
  6070. case I40E_VSI_MAIN:
  6071. /* The PF's main VSI is already setup as part of the
  6072. * device initialization, so we'll not bother with
  6073. * the add_vsi call, but we will retrieve the current
  6074. * VSI context.
  6075. */
  6076. ctxt.seid = pf->main_vsi_seid;
  6077. ctxt.pf_num = pf->hw.pf_id;
  6078. ctxt.vf_num = 0;
  6079. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  6080. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  6081. if (ret) {
  6082. dev_info(&pf->pdev->dev,
  6083. "couldn't get pf vsi config, err %d, aq_err %d\n",
  6084. ret, pf->hw.aq.asq_last_status);
  6085. return -ENOENT;
  6086. }
  6087. memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
  6088. vsi->info.valid_sections = 0;
  6089. vsi->seid = ctxt.seid;
  6090. vsi->id = ctxt.vsi_number;
  6091. enabled_tc = i40e_pf_get_tc_map(pf);
  6092. /* MFP mode setup queue map and update VSI */
  6093. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  6094. memset(&ctxt, 0, sizeof(ctxt));
  6095. ctxt.seid = pf->main_vsi_seid;
  6096. ctxt.pf_num = pf->hw.pf_id;
  6097. ctxt.vf_num = 0;
  6098. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  6099. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  6100. if (ret) {
  6101. dev_info(&pf->pdev->dev,
  6102. "update vsi failed, aq_err=%d\n",
  6103. pf->hw.aq.asq_last_status);
  6104. ret = -ENOENT;
  6105. goto err;
  6106. }
  6107. /* update the local VSI info queue map */
  6108. i40e_vsi_update_queue_map(vsi, &ctxt);
  6109. vsi->info.valid_sections = 0;
  6110. } else {
  6111. /* Default/Main VSI is only enabled for TC0
  6112. * reconfigure it to enable all TCs that are
  6113. * available on the port in SFP mode.
  6114. */
  6115. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  6116. if (ret) {
  6117. dev_info(&pf->pdev->dev,
  6118. "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
  6119. enabled_tc, ret,
  6120. pf->hw.aq.asq_last_status);
  6121. ret = -ENOENT;
  6122. }
  6123. }
  6124. break;
  6125. case I40E_VSI_FDIR:
  6126. ctxt.pf_num = hw->pf_id;
  6127. ctxt.vf_num = 0;
  6128. ctxt.uplink_seid = vsi->uplink_seid;
  6129. ctxt.connection_type = 0x1; /* regular data port */
  6130. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  6131. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  6132. break;
  6133. case I40E_VSI_VMDQ2:
  6134. ctxt.pf_num = hw->pf_id;
  6135. ctxt.vf_num = 0;
  6136. ctxt.uplink_seid = vsi->uplink_seid;
  6137. ctxt.connection_type = 0x1; /* regular data port */
  6138. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  6139. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  6140. /* This VSI is connected to VEB so the switch_id
  6141. * should be set to zero by default.
  6142. */
  6143. ctxt.info.switch_id = 0;
  6144. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
  6145. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  6146. /* Setup the VSI tx/rx queue map for TC0 only for now */
  6147. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  6148. break;
  6149. case I40E_VSI_SRIOV:
  6150. ctxt.pf_num = hw->pf_id;
  6151. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  6152. ctxt.uplink_seid = vsi->uplink_seid;
  6153. ctxt.connection_type = 0x1; /* regular data port */
  6154. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  6155. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  6156. /* This VSI is connected to VEB so the switch_id
  6157. * should be set to zero by default.
  6158. */
  6159. ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  6160. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  6161. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  6162. /* Setup the VSI tx/rx queue map for TC0 only for now */
  6163. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  6164. break;
  6165. default:
  6166. return -ENODEV;
  6167. }
  6168. if (vsi->type != I40E_VSI_MAIN) {
  6169. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  6170. if (ret) {
  6171. dev_info(&vsi->back->pdev->dev,
  6172. "add vsi failed, aq_err=%d\n",
  6173. vsi->back->hw.aq.asq_last_status);
  6174. ret = -ENOENT;
  6175. goto err;
  6176. }
  6177. memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
  6178. vsi->info.valid_sections = 0;
  6179. vsi->seid = ctxt.seid;
  6180. vsi->id = ctxt.vsi_number;
  6181. }
  6182. /* If macvlan filters already exist, force them to get loaded */
  6183. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  6184. f->changed = true;
  6185. f_count++;
  6186. }
  6187. if (f_count) {
  6188. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  6189. pf->flags |= I40E_FLAG_FILTER_SYNC;
  6190. }
  6191. /* Update VSI BW information */
  6192. ret = i40e_vsi_get_bw_info(vsi);
  6193. if (ret) {
  6194. dev_info(&pf->pdev->dev,
  6195. "couldn't get vsi bw info, err %d, aq_err %d\n",
  6196. ret, pf->hw.aq.asq_last_status);
  6197. /* VSI is already added so not tearing that up */
  6198. ret = 0;
  6199. }
  6200. err:
  6201. return ret;
  6202. }
  6203. /**
  6204. * i40e_vsi_release - Delete a VSI and free its resources
  6205. * @vsi: the VSI being removed
  6206. *
  6207. * Returns 0 on success or < 0 on error
  6208. **/
  6209. int i40e_vsi_release(struct i40e_vsi *vsi)
  6210. {
  6211. struct i40e_mac_filter *f, *ftmp;
  6212. struct i40e_veb *veb = NULL;
  6213. struct i40e_pf *pf;
  6214. u16 uplink_seid;
  6215. int i, n;
  6216. pf = vsi->back;
  6217. /* release of a VEB-owner or last VSI is not allowed */
  6218. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  6219. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  6220. vsi->seid, vsi->uplink_seid);
  6221. return -ENODEV;
  6222. }
  6223. if (vsi == pf->vsi[pf->lan_vsi] &&
  6224. !test_bit(__I40E_DOWN, &pf->state)) {
  6225. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  6226. return -ENODEV;
  6227. }
  6228. uplink_seid = vsi->uplink_seid;
  6229. if (vsi->type != I40E_VSI_SRIOV) {
  6230. if (vsi->netdev_registered) {
  6231. vsi->netdev_registered = false;
  6232. if (vsi->netdev) {
  6233. /* results in a call to i40e_close() */
  6234. unregister_netdev(vsi->netdev);
  6235. }
  6236. } else {
  6237. i40e_vsi_close(vsi);
  6238. }
  6239. i40e_vsi_disable_irq(vsi);
  6240. }
  6241. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
  6242. i40e_del_filter(vsi, f->macaddr, f->vlan,
  6243. f->is_vf, f->is_netdev);
  6244. i40e_sync_vsi_filters(vsi);
  6245. i40e_vsi_delete(vsi);
  6246. i40e_vsi_free_q_vectors(vsi);
  6247. if (vsi->netdev) {
  6248. free_netdev(vsi->netdev);
  6249. vsi->netdev = NULL;
  6250. }
  6251. i40e_vsi_clear_rings(vsi);
  6252. i40e_vsi_clear(vsi);
  6253. /* If this was the last thing on the VEB, except for the
  6254. * controlling VSI, remove the VEB, which puts the controlling
  6255. * VSI onto the next level down in the switch.
  6256. *
  6257. * Well, okay, there's one more exception here: don't remove
  6258. * the orphan VEBs yet. We'll wait for an explicit remove request
  6259. * from up the network stack.
  6260. */
  6261. for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  6262. if (pf->vsi[i] &&
  6263. pf->vsi[i]->uplink_seid == uplink_seid &&
  6264. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  6265. n++; /* count the VSIs */
  6266. }
  6267. }
  6268. for (i = 0; i < I40E_MAX_VEB; i++) {
  6269. if (!pf->veb[i])
  6270. continue;
  6271. if (pf->veb[i]->uplink_seid == uplink_seid)
  6272. n++; /* count the VEBs */
  6273. if (pf->veb[i]->seid == uplink_seid)
  6274. veb = pf->veb[i];
  6275. }
  6276. if (n == 0 && veb && veb->uplink_seid != 0)
  6277. i40e_veb_release(veb);
  6278. return 0;
  6279. }
  6280. /**
  6281. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  6282. * @vsi: ptr to the VSI
  6283. *
  6284. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  6285. * corresponding SW VSI structure and initializes num_queue_pairs for the
  6286. * newly allocated VSI.
  6287. *
  6288. * Returns 0 on success or negative on failure
  6289. **/
  6290. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  6291. {
  6292. int ret = -ENOENT;
  6293. struct i40e_pf *pf = vsi->back;
  6294. if (vsi->q_vectors[0]) {
  6295. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  6296. vsi->seid);
  6297. return -EEXIST;
  6298. }
  6299. if (vsi->base_vector) {
  6300. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  6301. vsi->seid, vsi->base_vector);
  6302. return -EEXIST;
  6303. }
  6304. ret = i40e_vsi_alloc_q_vectors(vsi);
  6305. if (ret) {
  6306. dev_info(&pf->pdev->dev,
  6307. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  6308. vsi->num_q_vectors, vsi->seid, ret);
  6309. vsi->num_q_vectors = 0;
  6310. goto vector_setup_out;
  6311. }
  6312. if (vsi->num_q_vectors)
  6313. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  6314. vsi->num_q_vectors, vsi->idx);
  6315. if (vsi->base_vector < 0) {
  6316. dev_info(&pf->pdev->dev,
  6317. "failed to get queue tracking for VSI %d, err=%d\n",
  6318. vsi->seid, vsi->base_vector);
  6319. i40e_vsi_free_q_vectors(vsi);
  6320. ret = -ENOENT;
  6321. goto vector_setup_out;
  6322. }
  6323. vector_setup_out:
  6324. return ret;
  6325. }
  6326. /**
  6327. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  6328. * @vsi: pointer to the vsi.
  6329. *
  6330. * This re-allocates a vsi's queue resources.
  6331. *
  6332. * Returns pointer to the successfully allocated and configured VSI sw struct
  6333. * on success, otherwise returns NULL on failure.
  6334. **/
  6335. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  6336. {
  6337. struct i40e_pf *pf = vsi->back;
  6338. u8 enabled_tc;
  6339. int ret;
  6340. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  6341. i40e_vsi_clear_rings(vsi);
  6342. i40e_vsi_free_arrays(vsi, false);
  6343. i40e_set_num_rings_in_vsi(vsi);
  6344. ret = i40e_vsi_alloc_arrays(vsi, false);
  6345. if (ret)
  6346. goto err_vsi;
  6347. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
  6348. if (ret < 0) {
  6349. dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
  6350. vsi->seid, ret);
  6351. goto err_vsi;
  6352. }
  6353. vsi->base_queue = ret;
  6354. /* Update the FW view of the VSI. Force a reset of TC and queue
  6355. * layout configurations.
  6356. */
  6357. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  6358. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  6359. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  6360. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  6361. /* assign it some queues */
  6362. ret = i40e_alloc_rings(vsi);
  6363. if (ret)
  6364. goto err_rings;
  6365. /* map all of the rings to the q_vectors */
  6366. i40e_vsi_map_rings_to_vectors(vsi);
  6367. return vsi;
  6368. err_rings:
  6369. i40e_vsi_free_q_vectors(vsi);
  6370. if (vsi->netdev_registered) {
  6371. vsi->netdev_registered = false;
  6372. unregister_netdev(vsi->netdev);
  6373. free_netdev(vsi->netdev);
  6374. vsi->netdev = NULL;
  6375. }
  6376. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  6377. err_vsi:
  6378. i40e_vsi_clear(vsi);
  6379. return NULL;
  6380. }
  6381. /**
  6382. * i40e_vsi_setup - Set up a VSI by a given type
  6383. * @pf: board private structure
  6384. * @type: VSI type
  6385. * @uplink_seid: the switch element to link to
  6386. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  6387. *
  6388. * This allocates the sw VSI structure and its queue resources, then add a VSI
  6389. * to the identified VEB.
  6390. *
  6391. * Returns pointer to the successfully allocated and configure VSI sw struct on
  6392. * success, otherwise returns NULL on failure.
  6393. **/
  6394. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  6395. u16 uplink_seid, u32 param1)
  6396. {
  6397. struct i40e_vsi *vsi = NULL;
  6398. struct i40e_veb *veb = NULL;
  6399. int ret, i;
  6400. int v_idx;
  6401. /* The requested uplink_seid must be either
  6402. * - the PF's port seid
  6403. * no VEB is needed because this is the PF
  6404. * or this is a Flow Director special case VSI
  6405. * - seid of an existing VEB
  6406. * - seid of a VSI that owns an existing VEB
  6407. * - seid of a VSI that doesn't own a VEB
  6408. * a new VEB is created and the VSI becomes the owner
  6409. * - seid of the PF VSI, which is what creates the first VEB
  6410. * this is a special case of the previous
  6411. *
  6412. * Find which uplink_seid we were given and create a new VEB if needed
  6413. */
  6414. for (i = 0; i < I40E_MAX_VEB; i++) {
  6415. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  6416. veb = pf->veb[i];
  6417. break;
  6418. }
  6419. }
  6420. if (!veb && uplink_seid != pf->mac_seid) {
  6421. for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  6422. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  6423. vsi = pf->vsi[i];
  6424. break;
  6425. }
  6426. }
  6427. if (!vsi) {
  6428. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  6429. uplink_seid);
  6430. return NULL;
  6431. }
  6432. if (vsi->uplink_seid == pf->mac_seid)
  6433. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  6434. vsi->tc_config.enabled_tc);
  6435. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  6436. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  6437. vsi->tc_config.enabled_tc);
  6438. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  6439. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  6440. veb = pf->veb[i];
  6441. }
  6442. if (!veb) {
  6443. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  6444. return NULL;
  6445. }
  6446. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  6447. uplink_seid = veb->seid;
  6448. }
  6449. /* get vsi sw struct */
  6450. v_idx = i40e_vsi_mem_alloc(pf, type);
  6451. if (v_idx < 0)
  6452. goto err_alloc;
  6453. vsi = pf->vsi[v_idx];
  6454. if (!vsi)
  6455. goto err_alloc;
  6456. vsi->type = type;
  6457. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  6458. if (type == I40E_VSI_MAIN)
  6459. pf->lan_vsi = v_idx;
  6460. else if (type == I40E_VSI_SRIOV)
  6461. vsi->vf_id = param1;
  6462. /* assign it some queues */
  6463. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
  6464. vsi->idx);
  6465. if (ret < 0) {
  6466. dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
  6467. vsi->seid, ret);
  6468. goto err_vsi;
  6469. }
  6470. vsi->base_queue = ret;
  6471. /* get a VSI from the hardware */
  6472. vsi->uplink_seid = uplink_seid;
  6473. ret = i40e_add_vsi(vsi);
  6474. if (ret)
  6475. goto err_vsi;
  6476. switch (vsi->type) {
  6477. /* setup the netdev if needed */
  6478. case I40E_VSI_MAIN:
  6479. case I40E_VSI_VMDQ2:
  6480. ret = i40e_config_netdev(vsi);
  6481. if (ret)
  6482. goto err_netdev;
  6483. ret = register_netdev(vsi->netdev);
  6484. if (ret)
  6485. goto err_netdev;
  6486. vsi->netdev_registered = true;
  6487. netif_carrier_off(vsi->netdev);
  6488. #ifdef CONFIG_I40E_DCB
  6489. /* Setup DCB netlink interface */
  6490. i40e_dcbnl_setup(vsi);
  6491. #endif /* CONFIG_I40E_DCB */
  6492. /* fall through */
  6493. case I40E_VSI_FDIR:
  6494. /* set up vectors and rings if needed */
  6495. ret = i40e_vsi_setup_vectors(vsi);
  6496. if (ret)
  6497. goto err_msix;
  6498. ret = i40e_alloc_rings(vsi);
  6499. if (ret)
  6500. goto err_rings;
  6501. /* map all of the rings to the q_vectors */
  6502. i40e_vsi_map_rings_to_vectors(vsi);
  6503. i40e_vsi_reset_stats(vsi);
  6504. break;
  6505. default:
  6506. /* no netdev or rings for the other VSI types */
  6507. break;
  6508. }
  6509. return vsi;
  6510. err_rings:
  6511. i40e_vsi_free_q_vectors(vsi);
  6512. err_msix:
  6513. if (vsi->netdev_registered) {
  6514. vsi->netdev_registered = false;
  6515. unregister_netdev(vsi->netdev);
  6516. free_netdev(vsi->netdev);
  6517. vsi->netdev = NULL;
  6518. }
  6519. err_netdev:
  6520. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  6521. err_vsi:
  6522. i40e_vsi_clear(vsi);
  6523. err_alloc:
  6524. return NULL;
  6525. }
  6526. /**
  6527. * i40e_veb_get_bw_info - Query VEB BW information
  6528. * @veb: the veb to query
  6529. *
  6530. * Query the Tx scheduler BW configuration data for given VEB
  6531. **/
  6532. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  6533. {
  6534. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  6535. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  6536. struct i40e_pf *pf = veb->pf;
  6537. struct i40e_hw *hw = &pf->hw;
  6538. u32 tc_bw_max;
  6539. int ret = 0;
  6540. int i;
  6541. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  6542. &bw_data, NULL);
  6543. if (ret) {
  6544. dev_info(&pf->pdev->dev,
  6545. "query veb bw config failed, aq_err=%d\n",
  6546. hw->aq.asq_last_status);
  6547. goto out;
  6548. }
  6549. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  6550. &ets_data, NULL);
  6551. if (ret) {
  6552. dev_info(&pf->pdev->dev,
  6553. "query veb bw ets config failed, aq_err=%d\n",
  6554. hw->aq.asq_last_status);
  6555. goto out;
  6556. }
  6557. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  6558. veb->bw_max_quanta = ets_data.tc_bw_max;
  6559. veb->is_abs_credits = bw_data.absolute_credits_enable;
  6560. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  6561. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  6562. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  6563. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  6564. veb->bw_tc_limit_credits[i] =
  6565. le16_to_cpu(bw_data.tc_bw_limits[i]);
  6566. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  6567. }
  6568. out:
  6569. return ret;
  6570. }
  6571. /**
  6572. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  6573. * @pf: board private structure
  6574. *
  6575. * On error: returns error code (negative)
  6576. * On success: returns vsi index in PF (positive)
  6577. **/
  6578. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  6579. {
  6580. int ret = -ENOENT;
  6581. struct i40e_veb *veb;
  6582. int i;
  6583. /* Need to protect the allocation of switch elements at the PF level */
  6584. mutex_lock(&pf->switch_mutex);
  6585. /* VEB list may be fragmented if VEB creation/destruction has
  6586. * been happening. We can afford to do a quick scan to look
  6587. * for any free slots in the list.
  6588. *
  6589. * find next empty veb slot, looping back around if necessary
  6590. */
  6591. i = 0;
  6592. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  6593. i++;
  6594. if (i >= I40E_MAX_VEB) {
  6595. ret = -ENOMEM;
  6596. goto err_alloc_veb; /* out of VEB slots! */
  6597. }
  6598. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  6599. if (!veb) {
  6600. ret = -ENOMEM;
  6601. goto err_alloc_veb;
  6602. }
  6603. veb->pf = pf;
  6604. veb->idx = i;
  6605. veb->enabled_tc = 1;
  6606. pf->veb[i] = veb;
  6607. ret = i;
  6608. err_alloc_veb:
  6609. mutex_unlock(&pf->switch_mutex);
  6610. return ret;
  6611. }
  6612. /**
  6613. * i40e_switch_branch_release - Delete a branch of the switch tree
  6614. * @branch: where to start deleting
  6615. *
  6616. * This uses recursion to find the tips of the branch to be
  6617. * removed, deleting until we get back to and can delete this VEB.
  6618. **/
  6619. static void i40e_switch_branch_release(struct i40e_veb *branch)
  6620. {
  6621. struct i40e_pf *pf = branch->pf;
  6622. u16 branch_seid = branch->seid;
  6623. u16 veb_idx = branch->idx;
  6624. int i;
  6625. /* release any VEBs on this VEB - RECURSION */
  6626. for (i = 0; i < I40E_MAX_VEB; i++) {
  6627. if (!pf->veb[i])
  6628. continue;
  6629. if (pf->veb[i]->uplink_seid == branch->seid)
  6630. i40e_switch_branch_release(pf->veb[i]);
  6631. }
  6632. /* Release the VSIs on this VEB, but not the owner VSI.
  6633. *
  6634. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  6635. * the VEB itself, so don't use (*branch) after this loop.
  6636. */
  6637. for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  6638. if (!pf->vsi[i])
  6639. continue;
  6640. if (pf->vsi[i]->uplink_seid == branch_seid &&
  6641. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  6642. i40e_vsi_release(pf->vsi[i]);
  6643. }
  6644. }
  6645. /* There's one corner case where the VEB might not have been
  6646. * removed, so double check it here and remove it if needed.
  6647. * This case happens if the veb was created from the debugfs
  6648. * commands and no VSIs were added to it.
  6649. */
  6650. if (pf->veb[veb_idx])
  6651. i40e_veb_release(pf->veb[veb_idx]);
  6652. }
  6653. /**
  6654. * i40e_veb_clear - remove veb struct
  6655. * @veb: the veb to remove
  6656. **/
  6657. static void i40e_veb_clear(struct i40e_veb *veb)
  6658. {
  6659. if (!veb)
  6660. return;
  6661. if (veb->pf) {
  6662. struct i40e_pf *pf = veb->pf;
  6663. mutex_lock(&pf->switch_mutex);
  6664. if (pf->veb[veb->idx] == veb)
  6665. pf->veb[veb->idx] = NULL;
  6666. mutex_unlock(&pf->switch_mutex);
  6667. }
  6668. kfree(veb);
  6669. }
  6670. /**
  6671. * i40e_veb_release - Delete a VEB and free its resources
  6672. * @veb: the VEB being removed
  6673. **/
  6674. void i40e_veb_release(struct i40e_veb *veb)
  6675. {
  6676. struct i40e_vsi *vsi = NULL;
  6677. struct i40e_pf *pf;
  6678. int i, n = 0;
  6679. pf = veb->pf;
  6680. /* find the remaining VSI and check for extras */
  6681. for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  6682. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  6683. n++;
  6684. vsi = pf->vsi[i];
  6685. }
  6686. }
  6687. if (n != 1) {
  6688. dev_info(&pf->pdev->dev,
  6689. "can't remove VEB %d with %d VSIs left\n",
  6690. veb->seid, n);
  6691. return;
  6692. }
  6693. /* move the remaining VSI to uplink veb */
  6694. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  6695. if (veb->uplink_seid) {
  6696. vsi->uplink_seid = veb->uplink_seid;
  6697. if (veb->uplink_seid == pf->mac_seid)
  6698. vsi->veb_idx = I40E_NO_VEB;
  6699. else
  6700. vsi->veb_idx = veb->veb_idx;
  6701. } else {
  6702. /* floating VEB */
  6703. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  6704. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  6705. }
  6706. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  6707. i40e_veb_clear(veb);
  6708. }
  6709. /**
  6710. * i40e_add_veb - create the VEB in the switch
  6711. * @veb: the VEB to be instantiated
  6712. * @vsi: the controlling VSI
  6713. **/
  6714. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  6715. {
  6716. bool is_default = false;
  6717. bool is_cloud = false;
  6718. int ret;
  6719. /* get a VEB from the hardware */
  6720. ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
  6721. veb->enabled_tc, is_default,
  6722. is_cloud, &veb->seid, NULL);
  6723. if (ret) {
  6724. dev_info(&veb->pf->pdev->dev,
  6725. "couldn't add VEB, err %d, aq_err %d\n",
  6726. ret, veb->pf->hw.aq.asq_last_status);
  6727. return -EPERM;
  6728. }
  6729. /* get statistics counter */
  6730. ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
  6731. &veb->stats_idx, NULL, NULL, NULL);
  6732. if (ret) {
  6733. dev_info(&veb->pf->pdev->dev,
  6734. "couldn't get VEB statistics idx, err %d, aq_err %d\n",
  6735. ret, veb->pf->hw.aq.asq_last_status);
  6736. return -EPERM;
  6737. }
  6738. ret = i40e_veb_get_bw_info(veb);
  6739. if (ret) {
  6740. dev_info(&veb->pf->pdev->dev,
  6741. "couldn't get VEB bw info, err %d, aq_err %d\n",
  6742. ret, veb->pf->hw.aq.asq_last_status);
  6743. i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
  6744. return -ENOENT;
  6745. }
  6746. vsi->uplink_seid = veb->seid;
  6747. vsi->veb_idx = veb->idx;
  6748. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  6749. return 0;
  6750. }
  6751. /**
  6752. * i40e_veb_setup - Set up a VEB
  6753. * @pf: board private structure
  6754. * @flags: VEB setup flags
  6755. * @uplink_seid: the switch element to link to
  6756. * @vsi_seid: the initial VSI seid
  6757. * @enabled_tc: Enabled TC bit-map
  6758. *
  6759. * This allocates the sw VEB structure and links it into the switch
  6760. * It is possible and legal for this to be a duplicate of an already
  6761. * existing VEB. It is also possible for both uplink and vsi seids
  6762. * to be zero, in order to create a floating VEB.
  6763. *
  6764. * Returns pointer to the successfully allocated VEB sw struct on
  6765. * success, otherwise returns NULL on failure.
  6766. **/
  6767. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  6768. u16 uplink_seid, u16 vsi_seid,
  6769. u8 enabled_tc)
  6770. {
  6771. struct i40e_veb *veb, *uplink_veb = NULL;
  6772. int vsi_idx, veb_idx;
  6773. int ret;
  6774. /* if one seid is 0, the other must be 0 to create a floating relay */
  6775. if ((uplink_seid == 0 || vsi_seid == 0) &&
  6776. (uplink_seid + vsi_seid != 0)) {
  6777. dev_info(&pf->pdev->dev,
  6778. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  6779. uplink_seid, vsi_seid);
  6780. return NULL;
  6781. }
  6782. /* make sure there is such a vsi and uplink */
  6783. for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
  6784. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  6785. break;
  6786. if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
  6787. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  6788. vsi_seid);
  6789. return NULL;
  6790. }
  6791. if (uplink_seid && uplink_seid != pf->mac_seid) {
  6792. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  6793. if (pf->veb[veb_idx] &&
  6794. pf->veb[veb_idx]->seid == uplink_seid) {
  6795. uplink_veb = pf->veb[veb_idx];
  6796. break;
  6797. }
  6798. }
  6799. if (!uplink_veb) {
  6800. dev_info(&pf->pdev->dev,
  6801. "uplink seid %d not found\n", uplink_seid);
  6802. return NULL;
  6803. }
  6804. }
  6805. /* get veb sw struct */
  6806. veb_idx = i40e_veb_mem_alloc(pf);
  6807. if (veb_idx < 0)
  6808. goto err_alloc;
  6809. veb = pf->veb[veb_idx];
  6810. veb->flags = flags;
  6811. veb->uplink_seid = uplink_seid;
  6812. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  6813. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  6814. /* create the VEB in the switch */
  6815. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  6816. if (ret)
  6817. goto err_veb;
  6818. if (vsi_idx == pf->lan_vsi)
  6819. pf->lan_veb = veb->idx;
  6820. return veb;
  6821. err_veb:
  6822. i40e_veb_clear(veb);
  6823. err_alloc:
  6824. return NULL;
  6825. }
  6826. /**
  6827. * i40e_setup_pf_switch_element - set pf vars based on switch type
  6828. * @pf: board private structure
  6829. * @ele: element we are building info from
  6830. * @num_reported: total number of elements
  6831. * @printconfig: should we print the contents
  6832. *
  6833. * helper function to assist in extracting a few useful SEID values.
  6834. **/
  6835. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  6836. struct i40e_aqc_switch_config_element_resp *ele,
  6837. u16 num_reported, bool printconfig)
  6838. {
  6839. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  6840. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  6841. u8 element_type = ele->element_type;
  6842. u16 seid = le16_to_cpu(ele->seid);
  6843. if (printconfig)
  6844. dev_info(&pf->pdev->dev,
  6845. "type=%d seid=%d uplink=%d downlink=%d\n",
  6846. element_type, seid, uplink_seid, downlink_seid);
  6847. switch (element_type) {
  6848. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  6849. pf->mac_seid = seid;
  6850. break;
  6851. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  6852. /* Main VEB? */
  6853. if (uplink_seid != pf->mac_seid)
  6854. break;
  6855. if (pf->lan_veb == I40E_NO_VEB) {
  6856. int v;
  6857. /* find existing or else empty VEB */
  6858. for (v = 0; v < I40E_MAX_VEB; v++) {
  6859. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  6860. pf->lan_veb = v;
  6861. break;
  6862. }
  6863. }
  6864. if (pf->lan_veb == I40E_NO_VEB) {
  6865. v = i40e_veb_mem_alloc(pf);
  6866. if (v < 0)
  6867. break;
  6868. pf->lan_veb = v;
  6869. }
  6870. }
  6871. pf->veb[pf->lan_veb]->seid = seid;
  6872. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  6873. pf->veb[pf->lan_veb]->pf = pf;
  6874. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  6875. break;
  6876. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  6877. if (num_reported != 1)
  6878. break;
  6879. /* This is immediately after a reset so we can assume this is
  6880. * the PF's VSI
  6881. */
  6882. pf->mac_seid = uplink_seid;
  6883. pf->pf_seid = downlink_seid;
  6884. pf->main_vsi_seid = seid;
  6885. if (printconfig)
  6886. dev_info(&pf->pdev->dev,
  6887. "pf_seid=%d main_vsi_seid=%d\n",
  6888. pf->pf_seid, pf->main_vsi_seid);
  6889. break;
  6890. case I40E_SWITCH_ELEMENT_TYPE_PF:
  6891. case I40E_SWITCH_ELEMENT_TYPE_VF:
  6892. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  6893. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  6894. case I40E_SWITCH_ELEMENT_TYPE_PE:
  6895. case I40E_SWITCH_ELEMENT_TYPE_PA:
  6896. /* ignore these for now */
  6897. break;
  6898. default:
  6899. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  6900. element_type, seid);
  6901. break;
  6902. }
  6903. }
  6904. /**
  6905. * i40e_fetch_switch_configuration - Get switch config from firmware
  6906. * @pf: board private structure
  6907. * @printconfig: should we print the contents
  6908. *
  6909. * Get the current switch configuration from the device and
  6910. * extract a few useful SEID values.
  6911. **/
  6912. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  6913. {
  6914. struct i40e_aqc_get_switch_config_resp *sw_config;
  6915. u16 next_seid = 0;
  6916. int ret = 0;
  6917. u8 *aq_buf;
  6918. int i;
  6919. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  6920. if (!aq_buf)
  6921. return -ENOMEM;
  6922. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  6923. do {
  6924. u16 num_reported, num_total;
  6925. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  6926. I40E_AQ_LARGE_BUF,
  6927. &next_seid, NULL);
  6928. if (ret) {
  6929. dev_info(&pf->pdev->dev,
  6930. "get switch config failed %d aq_err=%x\n",
  6931. ret, pf->hw.aq.asq_last_status);
  6932. kfree(aq_buf);
  6933. return -ENOENT;
  6934. }
  6935. num_reported = le16_to_cpu(sw_config->header.num_reported);
  6936. num_total = le16_to_cpu(sw_config->header.num_total);
  6937. if (printconfig)
  6938. dev_info(&pf->pdev->dev,
  6939. "header: %d reported %d total\n",
  6940. num_reported, num_total);
  6941. if (num_reported) {
  6942. int sz = sizeof(*sw_config) * num_reported;
  6943. kfree(pf->sw_config);
  6944. pf->sw_config = kzalloc(sz, GFP_KERNEL);
  6945. if (pf->sw_config)
  6946. memcpy(pf->sw_config, sw_config, sz);
  6947. }
  6948. for (i = 0; i < num_reported; i++) {
  6949. struct i40e_aqc_switch_config_element_resp *ele =
  6950. &sw_config->element[i];
  6951. i40e_setup_pf_switch_element(pf, ele, num_reported,
  6952. printconfig);
  6953. }
  6954. } while (next_seid != 0);
  6955. kfree(aq_buf);
  6956. return ret;
  6957. }
  6958. /**
  6959. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  6960. * @pf: board private structure
  6961. * @reinit: if the Main VSI needs to re-initialized.
  6962. *
  6963. * Returns 0 on success, negative value on failure
  6964. **/
  6965. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  6966. {
  6967. u32 rxfc = 0, txfc = 0, rxfc_reg;
  6968. int ret;
  6969. /* find out what's out there already */
  6970. ret = i40e_fetch_switch_configuration(pf, false);
  6971. if (ret) {
  6972. dev_info(&pf->pdev->dev,
  6973. "couldn't fetch switch config, err %d, aq_err %d\n",
  6974. ret, pf->hw.aq.asq_last_status);
  6975. return ret;
  6976. }
  6977. i40e_pf_reset_stats(pf);
  6978. /* first time setup */
  6979. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  6980. struct i40e_vsi *vsi = NULL;
  6981. u16 uplink_seid;
  6982. /* Set up the PF VSI associated with the PF's main VSI
  6983. * that is already in the HW switch
  6984. */
  6985. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  6986. uplink_seid = pf->veb[pf->lan_veb]->seid;
  6987. else
  6988. uplink_seid = pf->mac_seid;
  6989. if (pf->lan_vsi == I40E_NO_VSI)
  6990. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  6991. else if (reinit)
  6992. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  6993. if (!vsi) {
  6994. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  6995. i40e_fdir_teardown(pf);
  6996. return -EAGAIN;
  6997. }
  6998. } else {
  6999. /* force a reset of TC and queue layout configurations */
  7000. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  7001. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  7002. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  7003. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  7004. }
  7005. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  7006. i40e_fdir_sb_setup(pf);
  7007. /* Setup static PF queue filter control settings */
  7008. ret = i40e_setup_pf_filter_control(pf);
  7009. if (ret) {
  7010. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  7011. ret);
  7012. /* Failure here should not stop continuing other steps */
  7013. }
  7014. /* enable RSS in the HW, even for only one queue, as the stack can use
  7015. * the hash
  7016. */
  7017. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  7018. i40e_config_rss(pf);
  7019. /* fill in link information and enable LSE reporting */
  7020. i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
  7021. i40e_link_event(pf);
  7022. /* Initialize user-specific link properties */
  7023. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  7024. I40E_AQ_AN_COMPLETED) ? true : false);
  7025. /* requested_mode is set in probe or by ethtool */
  7026. if (!pf->fc_autoneg_status)
  7027. goto no_autoneg;
  7028. if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
  7029. (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
  7030. pf->hw.fc.current_mode = I40E_FC_FULL;
  7031. else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
  7032. pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
  7033. else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
  7034. pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
  7035. else
  7036. pf->hw.fc.current_mode = I40E_FC_NONE;
  7037. /* sync the flow control settings with the auto-neg values */
  7038. switch (pf->hw.fc.current_mode) {
  7039. case I40E_FC_FULL:
  7040. txfc = 1;
  7041. rxfc = 1;
  7042. break;
  7043. case I40E_FC_TX_PAUSE:
  7044. txfc = 1;
  7045. rxfc = 0;
  7046. break;
  7047. case I40E_FC_RX_PAUSE:
  7048. txfc = 0;
  7049. rxfc = 1;
  7050. break;
  7051. case I40E_FC_NONE:
  7052. case I40E_FC_DEFAULT:
  7053. txfc = 0;
  7054. rxfc = 0;
  7055. break;
  7056. case I40E_FC_PFC:
  7057. /* TBD */
  7058. break;
  7059. /* no default case, we have to handle all possibilities here */
  7060. }
  7061. wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
  7062. rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
  7063. ~I40E_PRTDCB_MFLCN_RFCE_MASK;
  7064. rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
  7065. wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
  7066. goto fc_complete;
  7067. no_autoneg:
  7068. /* disable L2 flow control, user can turn it on if they wish */
  7069. wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
  7070. wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
  7071. ~I40E_PRTDCB_MFLCN_RFCE_MASK);
  7072. fc_complete:
  7073. i40e_ptp_init(pf);
  7074. return ret;
  7075. }
  7076. /**
  7077. * i40e_determine_queue_usage - Work out queue distribution
  7078. * @pf: board private structure
  7079. **/
  7080. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  7081. {
  7082. int queues_left;
  7083. pf->num_lan_qps = 0;
  7084. /* Find the max queues to be put into basic use. We'll always be
  7085. * using TC0, whether or not DCB is running, and TC0 will get the
  7086. * big RSS set.
  7087. */
  7088. queues_left = pf->hw.func_caps.num_tx_qp;
  7089. if ((queues_left == 1) ||
  7090. !(pf->flags & I40E_FLAG_MSIX_ENABLED) ||
  7091. !(pf->flags & (I40E_FLAG_RSS_ENABLED | I40E_FLAG_FD_SB_ENABLED |
  7092. I40E_FLAG_DCB_ENABLED))) {
  7093. /* one qp for PF, no queues for anything else */
  7094. queues_left = 0;
  7095. pf->rss_size = pf->num_lan_qps = 1;
  7096. /* make sure all the fancies are disabled */
  7097. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  7098. I40E_FLAG_FD_SB_ENABLED |
  7099. I40E_FLAG_FD_ATR_ENABLED |
  7100. I40E_FLAG_DCB_ENABLED |
  7101. I40E_FLAG_SRIOV_ENABLED |
  7102. I40E_FLAG_VMDQ_ENABLED);
  7103. } else {
  7104. /* Not enough queues for all TCs */
  7105. if ((pf->flags & I40E_FLAG_DCB_ENABLED) &&
  7106. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  7107. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  7108. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  7109. }
  7110. pf->num_lan_qps = pf->rss_size_max;
  7111. queues_left -= pf->num_lan_qps;
  7112. }
  7113. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7114. if (queues_left > 1) {
  7115. queues_left -= 1; /* save 1 queue for FD */
  7116. } else {
  7117. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7118. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  7119. }
  7120. }
  7121. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  7122. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  7123. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  7124. (queues_left / pf->num_vf_qps));
  7125. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  7126. }
  7127. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  7128. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  7129. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  7130. (queues_left / pf->num_vmdq_qps));
  7131. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  7132. }
  7133. pf->queues_left = queues_left;
  7134. }
  7135. /**
  7136. * i40e_setup_pf_filter_control - Setup PF static filter control
  7137. * @pf: PF to be setup
  7138. *
  7139. * i40e_setup_pf_filter_control sets up a pf's initial filter control
  7140. * settings. If PE/FCoE are enabled then it will also set the per PF
  7141. * based filter sizes required for them. It also enables Flow director,
  7142. * ethertype and macvlan type filter settings for the pf.
  7143. *
  7144. * Returns 0 on success, negative on failure
  7145. **/
  7146. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  7147. {
  7148. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  7149. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  7150. /* Flow Director is enabled */
  7151. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  7152. settings->enable_fdir = true;
  7153. /* Ethtype and MACVLAN filters enabled for PF */
  7154. settings->enable_ethtype = true;
  7155. settings->enable_macvlan = true;
  7156. if (i40e_set_filter_control(&pf->hw, settings))
  7157. return -ENOENT;
  7158. return 0;
  7159. }
  7160. #define INFO_STRING_LEN 255
  7161. static void i40e_print_features(struct i40e_pf *pf)
  7162. {
  7163. struct i40e_hw *hw = &pf->hw;
  7164. char *buf, *string;
  7165. string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
  7166. if (!string) {
  7167. dev_err(&pf->pdev->dev, "Features string allocation failed\n");
  7168. return;
  7169. }
  7170. buf = string;
  7171. buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
  7172. #ifdef CONFIG_PCI_IOV
  7173. buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
  7174. #endif
  7175. buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
  7176. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  7177. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  7178. buf += sprintf(buf, "RSS ");
  7179. buf += sprintf(buf, "FDir ");
  7180. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  7181. buf += sprintf(buf, "ATR ");
  7182. if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
  7183. buf += sprintf(buf, "NTUPLE ");
  7184. if (pf->flags & I40E_FLAG_DCB_ENABLED)
  7185. buf += sprintf(buf, "DCB ");
  7186. if (pf->flags & I40E_FLAG_PTP)
  7187. buf += sprintf(buf, "PTP ");
  7188. BUG_ON(buf > (string + INFO_STRING_LEN));
  7189. dev_info(&pf->pdev->dev, "%s\n", string);
  7190. kfree(string);
  7191. }
  7192. /**
  7193. * i40e_probe - Device initialization routine
  7194. * @pdev: PCI device information struct
  7195. * @ent: entry in i40e_pci_tbl
  7196. *
  7197. * i40e_probe initializes a pf identified by a pci_dev structure.
  7198. * The OS initialization, configuring of the pf private structure,
  7199. * and a hardware reset occur.
  7200. *
  7201. * Returns 0 on success, negative on failure
  7202. **/
  7203. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  7204. {
  7205. struct i40e_pf *pf;
  7206. struct i40e_hw *hw;
  7207. static u16 pfs_found;
  7208. u16 link_status;
  7209. int err = 0;
  7210. u32 len;
  7211. u32 i;
  7212. err = pci_enable_device_mem(pdev);
  7213. if (err)
  7214. return err;
  7215. /* set up for high or low dma */
  7216. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  7217. if (err) {
  7218. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  7219. if (err) {
  7220. dev_err(&pdev->dev,
  7221. "DMA configuration failed: 0x%x\n", err);
  7222. goto err_dma;
  7223. }
  7224. }
  7225. /* set up pci connections */
  7226. err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
  7227. IORESOURCE_MEM), i40e_driver_name);
  7228. if (err) {
  7229. dev_info(&pdev->dev,
  7230. "pci_request_selected_regions failed %d\n", err);
  7231. goto err_pci_reg;
  7232. }
  7233. pci_enable_pcie_error_reporting(pdev);
  7234. pci_set_master(pdev);
  7235. /* Now that we have a PCI connection, we need to do the
  7236. * low level device setup. This is primarily setting up
  7237. * the Admin Queue structures and then querying for the
  7238. * device's current profile information.
  7239. */
  7240. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  7241. if (!pf) {
  7242. err = -ENOMEM;
  7243. goto err_pf_alloc;
  7244. }
  7245. pf->next_vsi = 0;
  7246. pf->pdev = pdev;
  7247. set_bit(__I40E_DOWN, &pf->state);
  7248. hw = &pf->hw;
  7249. hw->back = pf;
  7250. hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
  7251. pci_resource_len(pdev, 0));
  7252. if (!hw->hw_addr) {
  7253. err = -EIO;
  7254. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  7255. (unsigned int)pci_resource_start(pdev, 0),
  7256. (unsigned int)pci_resource_len(pdev, 0), err);
  7257. goto err_ioremap;
  7258. }
  7259. hw->vendor_id = pdev->vendor;
  7260. hw->device_id = pdev->device;
  7261. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  7262. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  7263. hw->subsystem_device_id = pdev->subsystem_device;
  7264. hw->bus.device = PCI_SLOT(pdev->devfn);
  7265. hw->bus.func = PCI_FUNC(pdev->devfn);
  7266. pf->instance = pfs_found;
  7267. /* do a special CORER for clearing PXE mode once at init */
  7268. if (hw->revision_id == 0 &&
  7269. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  7270. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  7271. i40e_flush(hw);
  7272. msleep(200);
  7273. pf->corer_count++;
  7274. i40e_clear_pxe_mode(hw);
  7275. }
  7276. /* Reset here to make sure all is clean and to define PF 'n' */
  7277. err = i40e_pf_reset(hw);
  7278. if (err) {
  7279. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  7280. goto err_pf_reset;
  7281. }
  7282. pf->pfr_count++;
  7283. hw->aq.num_arq_entries = I40E_AQ_LEN;
  7284. hw->aq.num_asq_entries = I40E_AQ_LEN;
  7285. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  7286. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  7287. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  7288. snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
  7289. "%s-pf%d:misc",
  7290. dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
  7291. err = i40e_init_shared_code(hw);
  7292. if (err) {
  7293. dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
  7294. goto err_pf_reset;
  7295. }
  7296. /* set up a default setting for link flow control */
  7297. pf->hw.fc.requested_mode = I40E_FC_NONE;
  7298. err = i40e_init_adminq(hw);
  7299. dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
  7300. if (err) {
  7301. dev_info(&pdev->dev,
  7302. "init_adminq failed: %d expecting API %02x.%02x\n",
  7303. err,
  7304. I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
  7305. goto err_pf_reset;
  7306. }
  7307. i40e_verify_eeprom(pf);
  7308. /* Rev 0 hardware was never productized */
  7309. if (hw->revision_id < 1)
  7310. 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");
  7311. i40e_clear_pxe_mode(hw);
  7312. err = i40e_get_capabilities(pf);
  7313. if (err)
  7314. goto err_adminq_setup;
  7315. err = i40e_sw_init(pf);
  7316. if (err) {
  7317. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  7318. goto err_sw_init;
  7319. }
  7320. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  7321. hw->func_caps.num_rx_qp,
  7322. pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
  7323. if (err) {
  7324. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  7325. goto err_init_lan_hmc;
  7326. }
  7327. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  7328. if (err) {
  7329. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  7330. err = -ENOENT;
  7331. goto err_configure_lan_hmc;
  7332. }
  7333. i40e_get_mac_addr(hw, hw->mac.addr);
  7334. if (!is_valid_ether_addr(hw->mac.addr)) {
  7335. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  7336. err = -EIO;
  7337. goto err_mac_addr;
  7338. }
  7339. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  7340. memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
  7341. pci_set_drvdata(pdev, pf);
  7342. pci_save_state(pdev);
  7343. #ifdef CONFIG_I40E_DCB
  7344. err = i40e_init_pf_dcb(pf);
  7345. if (err) {
  7346. dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
  7347. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  7348. /* Continue without DCB enabled */
  7349. }
  7350. #endif /* CONFIG_I40E_DCB */
  7351. /* set up periodic task facility */
  7352. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  7353. pf->service_timer_period = HZ;
  7354. INIT_WORK(&pf->service_task, i40e_service_task);
  7355. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  7356. pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
  7357. pf->link_check_timeout = jiffies;
  7358. /* WoL defaults to disabled */
  7359. pf->wol_en = false;
  7360. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  7361. /* set up the main switch operations */
  7362. i40e_determine_queue_usage(pf);
  7363. i40e_init_interrupt_scheme(pf);
  7364. /* Set up the *vsi struct based on the number of VSIs in the HW,
  7365. * and set up our local tracking of the MAIN PF vsi.
  7366. */
  7367. len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
  7368. pf->vsi = kzalloc(len, GFP_KERNEL);
  7369. if (!pf->vsi) {
  7370. err = -ENOMEM;
  7371. goto err_switch_setup;
  7372. }
  7373. err = i40e_setup_pf_switch(pf, false);
  7374. if (err) {
  7375. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  7376. goto err_vsis;
  7377. }
  7378. /* if FDIR VSI was set up, start it now */
  7379. for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  7380. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  7381. i40e_vsi_open(pf->vsi[i]);
  7382. break;
  7383. }
  7384. }
  7385. /* The main driver is (mostly) up and happy. We need to set this state
  7386. * before setting up the misc vector or we get a race and the vector
  7387. * ends up disabled forever.
  7388. */
  7389. clear_bit(__I40E_DOWN, &pf->state);
  7390. /* In case of MSIX we are going to setup the misc vector right here
  7391. * to handle admin queue events etc. In case of legacy and MSI
  7392. * the misc functionality and queue processing is combined in
  7393. * the same vector and that gets setup at open.
  7394. */
  7395. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  7396. err = i40e_setup_misc_vector(pf);
  7397. if (err) {
  7398. dev_info(&pdev->dev,
  7399. "setup of misc vector failed: %d\n", err);
  7400. goto err_vsis;
  7401. }
  7402. }
  7403. #ifdef CONFIG_PCI_IOV
  7404. /* prep for VF support */
  7405. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  7406. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  7407. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  7408. u32 val;
  7409. /* disable link interrupts for VFs */
  7410. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  7411. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  7412. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  7413. i40e_flush(hw);
  7414. if (pci_num_vf(pdev)) {
  7415. dev_info(&pdev->dev,
  7416. "Active VFs found, allocating resources.\n");
  7417. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  7418. if (err)
  7419. dev_info(&pdev->dev,
  7420. "Error %d allocating resources for existing VFs\n",
  7421. err);
  7422. }
  7423. }
  7424. #endif /* CONFIG_PCI_IOV */
  7425. pfs_found++;
  7426. i40e_dbg_pf_init(pf);
  7427. /* tell the firmware that we're starting */
  7428. i40e_send_version(pf);
  7429. /* since everything's happy, start the service_task timer */
  7430. mod_timer(&pf->service_timer,
  7431. round_jiffies(jiffies + pf->service_timer_period));
  7432. /* Get the negotiated link width and speed from PCI config space */
  7433. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
  7434. i40e_set_pci_config_data(hw, link_status);
  7435. dev_info(&pdev->dev, "PCI-Express: %s %s\n",
  7436. (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
  7437. hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
  7438. hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
  7439. "Unknown"),
  7440. (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
  7441. hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
  7442. hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
  7443. hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
  7444. "Unknown"));
  7445. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  7446. hw->bus.speed < i40e_bus_speed_8000) {
  7447. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  7448. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  7449. }
  7450. /* print a string summarizing features */
  7451. i40e_print_features(pf);
  7452. return 0;
  7453. /* Unwind what we've done if something failed in the setup */
  7454. err_vsis:
  7455. set_bit(__I40E_DOWN, &pf->state);
  7456. i40e_clear_interrupt_scheme(pf);
  7457. kfree(pf->vsi);
  7458. err_switch_setup:
  7459. i40e_reset_interrupt_capability(pf);
  7460. del_timer_sync(&pf->service_timer);
  7461. err_mac_addr:
  7462. err_configure_lan_hmc:
  7463. (void)i40e_shutdown_lan_hmc(hw);
  7464. err_init_lan_hmc:
  7465. kfree(pf->qp_pile);
  7466. kfree(pf->irq_pile);
  7467. err_sw_init:
  7468. err_adminq_setup:
  7469. (void)i40e_shutdown_adminq(hw);
  7470. err_pf_reset:
  7471. iounmap(hw->hw_addr);
  7472. err_ioremap:
  7473. kfree(pf);
  7474. err_pf_alloc:
  7475. pci_disable_pcie_error_reporting(pdev);
  7476. pci_release_selected_regions(pdev,
  7477. pci_select_bars(pdev, IORESOURCE_MEM));
  7478. err_pci_reg:
  7479. err_dma:
  7480. pci_disable_device(pdev);
  7481. return err;
  7482. }
  7483. /**
  7484. * i40e_remove - Device removal routine
  7485. * @pdev: PCI device information struct
  7486. *
  7487. * i40e_remove is called by the PCI subsystem to alert the driver
  7488. * that is should release a PCI device. This could be caused by a
  7489. * Hot-Plug event, or because the driver is going to be removed from
  7490. * memory.
  7491. **/
  7492. static void i40e_remove(struct pci_dev *pdev)
  7493. {
  7494. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7495. i40e_status ret_code;
  7496. u32 reg;
  7497. int i;
  7498. i40e_dbg_pf_exit(pf);
  7499. i40e_ptp_stop(pf);
  7500. /* no more scheduling of any task */
  7501. set_bit(__I40E_DOWN, &pf->state);
  7502. del_timer_sync(&pf->service_timer);
  7503. cancel_work_sync(&pf->service_task);
  7504. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  7505. i40e_free_vfs(pf);
  7506. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  7507. }
  7508. i40e_fdir_teardown(pf);
  7509. /* If there is a switch structure or any orphans, remove them.
  7510. * This will leave only the PF's VSI remaining.
  7511. */
  7512. for (i = 0; i < I40E_MAX_VEB; i++) {
  7513. if (!pf->veb[i])
  7514. continue;
  7515. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  7516. pf->veb[i]->uplink_seid == 0)
  7517. i40e_switch_branch_release(pf->veb[i]);
  7518. }
  7519. /* Now we can shutdown the PF's VSI, just before we kill
  7520. * adminq and hmc.
  7521. */
  7522. if (pf->vsi[pf->lan_vsi])
  7523. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  7524. i40e_stop_misc_vector(pf);
  7525. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  7526. synchronize_irq(pf->msix_entries[0].vector);
  7527. free_irq(pf->msix_entries[0].vector, pf);
  7528. }
  7529. /* shutdown and destroy the HMC */
  7530. if (pf->hw.hmc.hmc_obj) {
  7531. ret_code = i40e_shutdown_lan_hmc(&pf->hw);
  7532. if (ret_code)
  7533. dev_warn(&pdev->dev,
  7534. "Failed to destroy the HMC resources: %d\n",
  7535. ret_code);
  7536. }
  7537. /* shutdown the adminq */
  7538. ret_code = i40e_shutdown_adminq(&pf->hw);
  7539. if (ret_code)
  7540. dev_warn(&pdev->dev,
  7541. "Failed to destroy the Admin Queue resources: %d\n",
  7542. ret_code);
  7543. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  7544. i40e_clear_interrupt_scheme(pf);
  7545. for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  7546. if (pf->vsi[i]) {
  7547. i40e_vsi_clear_rings(pf->vsi[i]);
  7548. i40e_vsi_clear(pf->vsi[i]);
  7549. pf->vsi[i] = NULL;
  7550. }
  7551. }
  7552. for (i = 0; i < I40E_MAX_VEB; i++) {
  7553. kfree(pf->veb[i]);
  7554. pf->veb[i] = NULL;
  7555. }
  7556. kfree(pf->qp_pile);
  7557. kfree(pf->irq_pile);
  7558. kfree(pf->sw_config);
  7559. kfree(pf->vsi);
  7560. /* force a PF reset to clean anything leftover */
  7561. reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
  7562. wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
  7563. i40e_flush(&pf->hw);
  7564. iounmap(pf->hw.hw_addr);
  7565. kfree(pf);
  7566. pci_release_selected_regions(pdev,
  7567. pci_select_bars(pdev, IORESOURCE_MEM));
  7568. pci_disable_pcie_error_reporting(pdev);
  7569. pci_disable_device(pdev);
  7570. }
  7571. /**
  7572. * i40e_pci_error_detected - warning that something funky happened in PCI land
  7573. * @pdev: PCI device information struct
  7574. *
  7575. * Called to warn that something happened and the error handling steps
  7576. * are in progress. Allows the driver to quiesce things, be ready for
  7577. * remediation.
  7578. **/
  7579. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  7580. enum pci_channel_state error)
  7581. {
  7582. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7583. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  7584. /* shutdown all operations */
  7585. if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
  7586. rtnl_lock();
  7587. i40e_prep_for_reset(pf);
  7588. rtnl_unlock();
  7589. }
  7590. /* Request a slot reset */
  7591. return PCI_ERS_RESULT_NEED_RESET;
  7592. }
  7593. /**
  7594. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  7595. * @pdev: PCI device information struct
  7596. *
  7597. * Called to find if the driver can work with the device now that
  7598. * the pci slot has been reset. If a basic connection seems good
  7599. * (registers are readable and have sane content) then return a
  7600. * happy little PCI_ERS_RESULT_xxx.
  7601. **/
  7602. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  7603. {
  7604. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7605. pci_ers_result_t result;
  7606. int err;
  7607. u32 reg;
  7608. dev_info(&pdev->dev, "%s\n", __func__);
  7609. if (pci_enable_device_mem(pdev)) {
  7610. dev_info(&pdev->dev,
  7611. "Cannot re-enable PCI device after reset.\n");
  7612. result = PCI_ERS_RESULT_DISCONNECT;
  7613. } else {
  7614. pci_set_master(pdev);
  7615. pci_restore_state(pdev);
  7616. pci_save_state(pdev);
  7617. pci_wake_from_d3(pdev, false);
  7618. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  7619. if (reg == 0)
  7620. result = PCI_ERS_RESULT_RECOVERED;
  7621. else
  7622. result = PCI_ERS_RESULT_DISCONNECT;
  7623. }
  7624. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  7625. if (err) {
  7626. dev_info(&pdev->dev,
  7627. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  7628. err);
  7629. /* non-fatal, continue */
  7630. }
  7631. return result;
  7632. }
  7633. /**
  7634. * i40e_pci_error_resume - restart operations after PCI error recovery
  7635. * @pdev: PCI device information struct
  7636. *
  7637. * Called to allow the driver to bring things back up after PCI error
  7638. * and/or reset recovery has finished.
  7639. **/
  7640. static void i40e_pci_error_resume(struct pci_dev *pdev)
  7641. {
  7642. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7643. dev_info(&pdev->dev, "%s\n", __func__);
  7644. if (test_bit(__I40E_SUSPENDED, &pf->state))
  7645. return;
  7646. rtnl_lock();
  7647. i40e_handle_reset_warning(pf);
  7648. rtnl_lock();
  7649. }
  7650. /**
  7651. * i40e_shutdown - PCI callback for shutting down
  7652. * @pdev: PCI device information struct
  7653. **/
  7654. static void i40e_shutdown(struct pci_dev *pdev)
  7655. {
  7656. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7657. struct i40e_hw *hw = &pf->hw;
  7658. set_bit(__I40E_SUSPENDED, &pf->state);
  7659. set_bit(__I40E_DOWN, &pf->state);
  7660. rtnl_lock();
  7661. i40e_prep_for_reset(pf);
  7662. rtnl_unlock();
  7663. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  7664. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  7665. if (system_state == SYSTEM_POWER_OFF) {
  7666. pci_wake_from_d3(pdev, pf->wol_en);
  7667. pci_set_power_state(pdev, PCI_D3hot);
  7668. }
  7669. }
  7670. #ifdef CONFIG_PM
  7671. /**
  7672. * i40e_suspend - PCI callback for moving to D3
  7673. * @pdev: PCI device information struct
  7674. **/
  7675. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  7676. {
  7677. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7678. struct i40e_hw *hw = &pf->hw;
  7679. set_bit(__I40E_SUSPENDED, &pf->state);
  7680. set_bit(__I40E_DOWN, &pf->state);
  7681. rtnl_lock();
  7682. i40e_prep_for_reset(pf);
  7683. rtnl_unlock();
  7684. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  7685. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  7686. pci_wake_from_d3(pdev, pf->wol_en);
  7687. pci_set_power_state(pdev, PCI_D3hot);
  7688. return 0;
  7689. }
  7690. /**
  7691. * i40e_resume - PCI callback for waking up from D3
  7692. * @pdev: PCI device information struct
  7693. **/
  7694. static int i40e_resume(struct pci_dev *pdev)
  7695. {
  7696. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7697. u32 err;
  7698. pci_set_power_state(pdev, PCI_D0);
  7699. pci_restore_state(pdev);
  7700. /* pci_restore_state() clears dev->state_saves, so
  7701. * call pci_save_state() again to restore it.
  7702. */
  7703. pci_save_state(pdev);
  7704. err = pci_enable_device_mem(pdev);
  7705. if (err) {
  7706. dev_err(&pdev->dev,
  7707. "%s: Cannot enable PCI device from suspend\n",
  7708. __func__);
  7709. return err;
  7710. }
  7711. pci_set_master(pdev);
  7712. /* no wakeup events while running */
  7713. pci_wake_from_d3(pdev, false);
  7714. /* handling the reset will rebuild the device state */
  7715. if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
  7716. clear_bit(__I40E_DOWN, &pf->state);
  7717. rtnl_lock();
  7718. i40e_reset_and_rebuild(pf, false);
  7719. rtnl_unlock();
  7720. }
  7721. return 0;
  7722. }
  7723. #endif
  7724. static const struct pci_error_handlers i40e_err_handler = {
  7725. .error_detected = i40e_pci_error_detected,
  7726. .slot_reset = i40e_pci_error_slot_reset,
  7727. .resume = i40e_pci_error_resume,
  7728. };
  7729. static struct pci_driver i40e_driver = {
  7730. .name = i40e_driver_name,
  7731. .id_table = i40e_pci_tbl,
  7732. .probe = i40e_probe,
  7733. .remove = i40e_remove,
  7734. #ifdef CONFIG_PM
  7735. .suspend = i40e_suspend,
  7736. .resume = i40e_resume,
  7737. #endif
  7738. .shutdown = i40e_shutdown,
  7739. .err_handler = &i40e_err_handler,
  7740. .sriov_configure = i40e_pci_sriov_configure,
  7741. };
  7742. /**
  7743. * i40e_init_module - Driver registration routine
  7744. *
  7745. * i40e_init_module is the first routine called when the driver is
  7746. * loaded. All it does is register with the PCI subsystem.
  7747. **/
  7748. static int __init i40e_init_module(void)
  7749. {
  7750. pr_info("%s: %s - version %s\n", i40e_driver_name,
  7751. i40e_driver_string, i40e_driver_version_str);
  7752. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  7753. i40e_dbg_init();
  7754. return pci_register_driver(&i40e_driver);
  7755. }
  7756. module_init(i40e_init_module);
  7757. /**
  7758. * i40e_exit_module - Driver exit cleanup routine
  7759. *
  7760. * i40e_exit_module is called just before the driver is removed
  7761. * from memory.
  7762. **/
  7763. static void __exit i40e_exit_module(void)
  7764. {
  7765. pci_unregister_driver(&i40e_driver);
  7766. i40e_dbg_exit();
  7767. }
  7768. module_exit(i40e_exit_module);