i40e_main.c 305 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2015 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 1
  37. #define DRV_VERSION_MINOR 4
  38. #define DRV_VERSION_BUILD 7
  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_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  54. u16 rss_table_size, u16 rss_size);
  55. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  56. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  57. /* i40e_pci_tbl - PCI Device ID Table
  58. *
  59. * Last entry must be all 0s
  60. *
  61. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  62. * Class, Class Mask, private data (not used) }
  63. */
  64. static const struct pci_device_id i40e_pci_tbl[] = {
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_A), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 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. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  74. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  75. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  76. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  81. /* required last entry */
  82. {0, }
  83. };
  84. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  85. #define I40E_MAX_VF_COUNT 128
  86. static int debug = -1;
  87. module_param(debug, int, 0);
  88. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  89. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  90. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  91. MODULE_LICENSE("GPL");
  92. MODULE_VERSION(DRV_VERSION);
  93. /**
  94. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  95. * @hw: pointer to the HW structure
  96. * @mem: ptr to mem struct to fill out
  97. * @size: size of memory requested
  98. * @alignment: what to align the allocation to
  99. **/
  100. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  101. u64 size, u32 alignment)
  102. {
  103. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  104. mem->size = ALIGN(size, alignment);
  105. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  106. &mem->pa, GFP_KERNEL);
  107. if (!mem->va)
  108. return -ENOMEM;
  109. return 0;
  110. }
  111. /**
  112. * i40e_free_dma_mem_d - OS specific memory free for shared code
  113. * @hw: pointer to the HW structure
  114. * @mem: ptr to mem struct to free
  115. **/
  116. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  117. {
  118. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  119. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  120. mem->va = NULL;
  121. mem->pa = 0;
  122. mem->size = 0;
  123. return 0;
  124. }
  125. /**
  126. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  127. * @hw: pointer to the HW structure
  128. * @mem: ptr to mem struct to fill out
  129. * @size: size of memory requested
  130. **/
  131. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  132. u32 size)
  133. {
  134. mem->size = size;
  135. mem->va = kzalloc(size, GFP_KERNEL);
  136. if (!mem->va)
  137. return -ENOMEM;
  138. return 0;
  139. }
  140. /**
  141. * i40e_free_virt_mem_d - OS specific memory free for shared code
  142. * @hw: pointer to the HW structure
  143. * @mem: ptr to mem struct to free
  144. **/
  145. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  146. {
  147. /* it's ok to kfree a NULL pointer */
  148. kfree(mem->va);
  149. mem->va = NULL;
  150. mem->size = 0;
  151. return 0;
  152. }
  153. /**
  154. * i40e_get_lump - find a lump of free generic resource
  155. * @pf: board private structure
  156. * @pile: the pile of resource to search
  157. * @needed: the number of items needed
  158. * @id: an owner id to stick on the items assigned
  159. *
  160. * Returns the base item index of the lump, or negative for error
  161. *
  162. * The search_hint trick and lack of advanced fit-finding only work
  163. * because we're highly likely to have all the same size lump requests.
  164. * Linear search time and any fragmentation should be minimal.
  165. **/
  166. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  167. u16 needed, u16 id)
  168. {
  169. int ret = -ENOMEM;
  170. int i, j;
  171. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  172. dev_info(&pf->pdev->dev,
  173. "param err: pile=%p needed=%d id=0x%04x\n",
  174. pile, needed, id);
  175. return -EINVAL;
  176. }
  177. /* start the linear search with an imperfect hint */
  178. i = pile->search_hint;
  179. while (i < pile->num_entries) {
  180. /* skip already allocated entries */
  181. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  182. i++;
  183. continue;
  184. }
  185. /* do we have enough in this lump? */
  186. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  187. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  188. break;
  189. }
  190. if (j == needed) {
  191. /* there was enough, so assign it to the requestor */
  192. for (j = 0; j < needed; j++)
  193. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  194. ret = i;
  195. pile->search_hint = i + j;
  196. break;
  197. }
  198. /* not enough, so skip over it and continue looking */
  199. i += j;
  200. }
  201. return ret;
  202. }
  203. /**
  204. * i40e_put_lump - return a lump of generic resource
  205. * @pile: the pile of resource to search
  206. * @index: the base item index
  207. * @id: the owner id of the items assigned
  208. *
  209. * Returns the count of items in the lump
  210. **/
  211. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  212. {
  213. int valid_id = (id | I40E_PILE_VALID_BIT);
  214. int count = 0;
  215. int i;
  216. if (!pile || index >= pile->num_entries)
  217. return -EINVAL;
  218. for (i = index;
  219. i < pile->num_entries && pile->list[i] == valid_id;
  220. i++) {
  221. pile->list[i] = 0;
  222. count++;
  223. }
  224. if (count && index < pile->search_hint)
  225. pile->search_hint = index;
  226. return count;
  227. }
  228. /**
  229. * i40e_find_vsi_from_id - searches for the vsi with the given id
  230. * @pf - the pf structure to search for the vsi
  231. * @id - id of the vsi it is searching for
  232. **/
  233. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  234. {
  235. int i;
  236. for (i = 0; i < pf->num_alloc_vsi; i++)
  237. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  238. return pf->vsi[i];
  239. return NULL;
  240. }
  241. /**
  242. * i40e_service_event_schedule - Schedule the service task to wake up
  243. * @pf: board private structure
  244. *
  245. * If not already scheduled, this puts the task into the work queue
  246. **/
  247. static void i40e_service_event_schedule(struct i40e_pf *pf)
  248. {
  249. if (!test_bit(__I40E_DOWN, &pf->state) &&
  250. !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
  251. !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
  252. schedule_work(&pf->service_task);
  253. }
  254. /**
  255. * i40e_tx_timeout - Respond to a Tx Hang
  256. * @netdev: network interface device structure
  257. *
  258. * If any port has noticed a Tx timeout, it is likely that the whole
  259. * device is munged, not just the one netdev port, so go for the full
  260. * reset.
  261. **/
  262. #ifdef I40E_FCOE
  263. void i40e_tx_timeout(struct net_device *netdev)
  264. #else
  265. static void i40e_tx_timeout(struct net_device *netdev)
  266. #endif
  267. {
  268. struct i40e_netdev_priv *np = netdev_priv(netdev);
  269. struct i40e_vsi *vsi = np->vsi;
  270. struct i40e_pf *pf = vsi->back;
  271. struct i40e_ring *tx_ring = NULL;
  272. unsigned int i, hung_queue = 0;
  273. u32 head, val;
  274. pf->tx_timeout_count++;
  275. /* find the stopped queue the same way the stack does */
  276. for (i = 0; i < netdev->num_tx_queues; i++) {
  277. struct netdev_queue *q;
  278. unsigned long trans_start;
  279. q = netdev_get_tx_queue(netdev, i);
  280. trans_start = q->trans_start ? : netdev->trans_start;
  281. if (netif_xmit_stopped(q) &&
  282. time_after(jiffies,
  283. (trans_start + netdev->watchdog_timeo))) {
  284. hung_queue = i;
  285. break;
  286. }
  287. }
  288. if (i == netdev->num_tx_queues) {
  289. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  290. } else {
  291. /* now that we have an index, find the tx_ring struct */
  292. for (i = 0; i < vsi->num_queue_pairs; i++) {
  293. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  294. if (hung_queue ==
  295. vsi->tx_rings[i]->queue_index) {
  296. tx_ring = vsi->tx_rings[i];
  297. break;
  298. }
  299. }
  300. }
  301. }
  302. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  303. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  304. else if (time_before(jiffies,
  305. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  306. return; /* don't do any new action before the next timeout */
  307. if (tx_ring) {
  308. head = i40e_get_head(tx_ring);
  309. /* Read interrupt register */
  310. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  311. val = rd32(&pf->hw,
  312. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  313. tx_ring->vsi->base_vector - 1));
  314. else
  315. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  316. netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
  317. vsi->seid, hung_queue, tx_ring->next_to_clean,
  318. head, tx_ring->next_to_use,
  319. readl(tx_ring->tail), val);
  320. }
  321. pf->tx_timeout_last_recovery = jiffies;
  322. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  323. pf->tx_timeout_recovery_level, hung_queue);
  324. switch (pf->tx_timeout_recovery_level) {
  325. case 1:
  326. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  327. break;
  328. case 2:
  329. set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  330. break;
  331. case 3:
  332. set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  333. break;
  334. default:
  335. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  336. break;
  337. }
  338. i40e_service_event_schedule(pf);
  339. pf->tx_timeout_recovery_level++;
  340. }
  341. /**
  342. * i40e_release_rx_desc - Store the new tail and head values
  343. * @rx_ring: ring to bump
  344. * @val: new head index
  345. **/
  346. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  347. {
  348. rx_ring->next_to_use = val;
  349. /* Force memory writes to complete before letting h/w
  350. * know there are new descriptors to fetch. (Only
  351. * applicable for weak-ordered memory model archs,
  352. * such as IA-64).
  353. */
  354. wmb();
  355. writel(val, rx_ring->tail);
  356. }
  357. /**
  358. * i40e_get_vsi_stats_struct - Get System Network Statistics
  359. * @vsi: the VSI we care about
  360. *
  361. * Returns the address of the device statistics structure.
  362. * The statistics are actually updated from the service task.
  363. **/
  364. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  365. {
  366. return &vsi->net_stats;
  367. }
  368. /**
  369. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  370. * @netdev: network interface device structure
  371. *
  372. * Returns the address of the device statistics structure.
  373. * The statistics are actually updated from the service task.
  374. **/
  375. #ifdef I40E_FCOE
  376. struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
  377. struct net_device *netdev,
  378. struct rtnl_link_stats64 *stats)
  379. #else
  380. static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
  381. struct net_device *netdev,
  382. struct rtnl_link_stats64 *stats)
  383. #endif
  384. {
  385. struct i40e_netdev_priv *np = netdev_priv(netdev);
  386. struct i40e_ring *tx_ring, *rx_ring;
  387. struct i40e_vsi *vsi = np->vsi;
  388. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  389. int i;
  390. if (test_bit(__I40E_DOWN, &vsi->state))
  391. return stats;
  392. if (!vsi->tx_rings)
  393. return stats;
  394. rcu_read_lock();
  395. for (i = 0; i < vsi->num_queue_pairs; i++) {
  396. u64 bytes, packets;
  397. unsigned int start;
  398. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  399. if (!tx_ring)
  400. continue;
  401. do {
  402. start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
  403. packets = tx_ring->stats.packets;
  404. bytes = tx_ring->stats.bytes;
  405. } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
  406. stats->tx_packets += packets;
  407. stats->tx_bytes += bytes;
  408. rx_ring = &tx_ring[1];
  409. do {
  410. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  411. packets = rx_ring->stats.packets;
  412. bytes = rx_ring->stats.bytes;
  413. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  414. stats->rx_packets += packets;
  415. stats->rx_bytes += bytes;
  416. }
  417. rcu_read_unlock();
  418. /* following stats updated by i40e_watchdog_subtask() */
  419. stats->multicast = vsi_stats->multicast;
  420. stats->tx_errors = vsi_stats->tx_errors;
  421. stats->tx_dropped = vsi_stats->tx_dropped;
  422. stats->rx_errors = vsi_stats->rx_errors;
  423. stats->rx_dropped = vsi_stats->rx_dropped;
  424. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  425. stats->rx_length_errors = vsi_stats->rx_length_errors;
  426. return stats;
  427. }
  428. /**
  429. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  430. * @vsi: the VSI to have its stats reset
  431. **/
  432. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  433. {
  434. struct rtnl_link_stats64 *ns;
  435. int i;
  436. if (!vsi)
  437. return;
  438. ns = i40e_get_vsi_stats_struct(vsi);
  439. memset(ns, 0, sizeof(*ns));
  440. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  441. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  442. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  443. if (vsi->rx_rings && vsi->rx_rings[0]) {
  444. for (i = 0; i < vsi->num_queue_pairs; i++) {
  445. memset(&vsi->rx_rings[i]->stats, 0,
  446. sizeof(vsi->rx_rings[i]->stats));
  447. memset(&vsi->rx_rings[i]->rx_stats, 0,
  448. sizeof(vsi->rx_rings[i]->rx_stats));
  449. memset(&vsi->tx_rings[i]->stats, 0,
  450. sizeof(vsi->tx_rings[i]->stats));
  451. memset(&vsi->tx_rings[i]->tx_stats, 0,
  452. sizeof(vsi->tx_rings[i]->tx_stats));
  453. }
  454. }
  455. vsi->stat_offsets_loaded = false;
  456. }
  457. /**
  458. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  459. * @pf: the PF to be reset
  460. **/
  461. void i40e_pf_reset_stats(struct i40e_pf *pf)
  462. {
  463. int i;
  464. memset(&pf->stats, 0, sizeof(pf->stats));
  465. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  466. pf->stat_offsets_loaded = false;
  467. for (i = 0; i < I40E_MAX_VEB; i++) {
  468. if (pf->veb[i]) {
  469. memset(&pf->veb[i]->stats, 0,
  470. sizeof(pf->veb[i]->stats));
  471. memset(&pf->veb[i]->stats_offsets, 0,
  472. sizeof(pf->veb[i]->stats_offsets));
  473. pf->veb[i]->stat_offsets_loaded = false;
  474. }
  475. }
  476. }
  477. /**
  478. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  479. * @hw: ptr to the hardware info
  480. * @hireg: the high 32 bit reg to read
  481. * @loreg: the low 32 bit reg to read
  482. * @offset_loaded: has the initial offset been loaded yet
  483. * @offset: ptr to current offset value
  484. * @stat: ptr to the stat
  485. *
  486. * Since the device stats are not reset at PFReset, they likely will not
  487. * be zeroed when the driver starts. We'll save the first values read
  488. * and use them as offsets to be subtracted from the raw values in order
  489. * to report stats that count from zero. In the process, we also manage
  490. * the potential roll-over.
  491. **/
  492. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  493. bool offset_loaded, u64 *offset, u64 *stat)
  494. {
  495. u64 new_data;
  496. if (hw->device_id == I40E_DEV_ID_QEMU) {
  497. new_data = rd32(hw, loreg);
  498. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  499. } else {
  500. new_data = rd64(hw, loreg);
  501. }
  502. if (!offset_loaded)
  503. *offset = new_data;
  504. if (likely(new_data >= *offset))
  505. *stat = new_data - *offset;
  506. else
  507. *stat = (new_data + BIT_ULL(48)) - *offset;
  508. *stat &= 0xFFFFFFFFFFFFULL;
  509. }
  510. /**
  511. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  512. * @hw: ptr to the hardware info
  513. * @reg: the hw reg to read
  514. * @offset_loaded: has the initial offset been loaded yet
  515. * @offset: ptr to current offset value
  516. * @stat: ptr to the stat
  517. **/
  518. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  519. bool offset_loaded, u64 *offset, u64 *stat)
  520. {
  521. u32 new_data;
  522. new_data = rd32(hw, reg);
  523. if (!offset_loaded)
  524. *offset = new_data;
  525. if (likely(new_data >= *offset))
  526. *stat = (u32)(new_data - *offset);
  527. else
  528. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  529. }
  530. /**
  531. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  532. * @vsi: the VSI to be updated
  533. **/
  534. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  535. {
  536. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  537. struct i40e_pf *pf = vsi->back;
  538. struct i40e_hw *hw = &pf->hw;
  539. struct i40e_eth_stats *oes;
  540. struct i40e_eth_stats *es; /* device's eth stats */
  541. es = &vsi->eth_stats;
  542. oes = &vsi->eth_stats_offsets;
  543. /* Gather up the stats that the hw collects */
  544. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  545. vsi->stat_offsets_loaded,
  546. &oes->tx_errors, &es->tx_errors);
  547. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  548. vsi->stat_offsets_loaded,
  549. &oes->rx_discards, &es->rx_discards);
  550. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  551. vsi->stat_offsets_loaded,
  552. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  553. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  554. vsi->stat_offsets_loaded,
  555. &oes->tx_errors, &es->tx_errors);
  556. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  557. I40E_GLV_GORCL(stat_idx),
  558. vsi->stat_offsets_loaded,
  559. &oes->rx_bytes, &es->rx_bytes);
  560. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  561. I40E_GLV_UPRCL(stat_idx),
  562. vsi->stat_offsets_loaded,
  563. &oes->rx_unicast, &es->rx_unicast);
  564. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  565. I40E_GLV_MPRCL(stat_idx),
  566. vsi->stat_offsets_loaded,
  567. &oes->rx_multicast, &es->rx_multicast);
  568. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  569. I40E_GLV_BPRCL(stat_idx),
  570. vsi->stat_offsets_loaded,
  571. &oes->rx_broadcast, &es->rx_broadcast);
  572. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  573. I40E_GLV_GOTCL(stat_idx),
  574. vsi->stat_offsets_loaded,
  575. &oes->tx_bytes, &es->tx_bytes);
  576. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  577. I40E_GLV_UPTCL(stat_idx),
  578. vsi->stat_offsets_loaded,
  579. &oes->tx_unicast, &es->tx_unicast);
  580. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  581. I40E_GLV_MPTCL(stat_idx),
  582. vsi->stat_offsets_loaded,
  583. &oes->tx_multicast, &es->tx_multicast);
  584. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  585. I40E_GLV_BPTCL(stat_idx),
  586. vsi->stat_offsets_loaded,
  587. &oes->tx_broadcast, &es->tx_broadcast);
  588. vsi->stat_offsets_loaded = true;
  589. }
  590. /**
  591. * i40e_update_veb_stats - Update Switch component statistics
  592. * @veb: the VEB being updated
  593. **/
  594. static void i40e_update_veb_stats(struct i40e_veb *veb)
  595. {
  596. struct i40e_pf *pf = veb->pf;
  597. struct i40e_hw *hw = &pf->hw;
  598. struct i40e_eth_stats *oes;
  599. struct i40e_eth_stats *es; /* device's eth stats */
  600. struct i40e_veb_tc_stats *veb_oes;
  601. struct i40e_veb_tc_stats *veb_es;
  602. int i, idx = 0;
  603. idx = veb->stats_idx;
  604. es = &veb->stats;
  605. oes = &veb->stats_offsets;
  606. veb_es = &veb->tc_stats;
  607. veb_oes = &veb->tc_stats_offsets;
  608. /* Gather up the stats that the hw collects */
  609. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  610. veb->stat_offsets_loaded,
  611. &oes->tx_discards, &es->tx_discards);
  612. if (hw->revision_id > 0)
  613. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  614. veb->stat_offsets_loaded,
  615. &oes->rx_unknown_protocol,
  616. &es->rx_unknown_protocol);
  617. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  618. veb->stat_offsets_loaded,
  619. &oes->rx_bytes, &es->rx_bytes);
  620. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  621. veb->stat_offsets_loaded,
  622. &oes->rx_unicast, &es->rx_unicast);
  623. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  624. veb->stat_offsets_loaded,
  625. &oes->rx_multicast, &es->rx_multicast);
  626. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  627. veb->stat_offsets_loaded,
  628. &oes->rx_broadcast, &es->rx_broadcast);
  629. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  630. veb->stat_offsets_loaded,
  631. &oes->tx_bytes, &es->tx_bytes);
  632. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  633. veb->stat_offsets_loaded,
  634. &oes->tx_unicast, &es->tx_unicast);
  635. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  636. veb->stat_offsets_loaded,
  637. &oes->tx_multicast, &es->tx_multicast);
  638. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  639. veb->stat_offsets_loaded,
  640. &oes->tx_broadcast, &es->tx_broadcast);
  641. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  642. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  643. I40E_GLVEBTC_RPCL(i, idx),
  644. veb->stat_offsets_loaded,
  645. &veb_oes->tc_rx_packets[i],
  646. &veb_es->tc_rx_packets[i]);
  647. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  648. I40E_GLVEBTC_RBCL(i, idx),
  649. veb->stat_offsets_loaded,
  650. &veb_oes->tc_rx_bytes[i],
  651. &veb_es->tc_rx_bytes[i]);
  652. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  653. I40E_GLVEBTC_TPCL(i, idx),
  654. veb->stat_offsets_loaded,
  655. &veb_oes->tc_tx_packets[i],
  656. &veb_es->tc_tx_packets[i]);
  657. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  658. I40E_GLVEBTC_TBCL(i, idx),
  659. veb->stat_offsets_loaded,
  660. &veb_oes->tc_tx_bytes[i],
  661. &veb_es->tc_tx_bytes[i]);
  662. }
  663. veb->stat_offsets_loaded = true;
  664. }
  665. #ifdef I40E_FCOE
  666. /**
  667. * i40e_update_fcoe_stats - Update FCoE-specific ethernet statistics counters.
  668. * @vsi: the VSI that is capable of doing FCoE
  669. **/
  670. static void i40e_update_fcoe_stats(struct i40e_vsi *vsi)
  671. {
  672. struct i40e_pf *pf = vsi->back;
  673. struct i40e_hw *hw = &pf->hw;
  674. struct i40e_fcoe_stats *ofs;
  675. struct i40e_fcoe_stats *fs; /* device's eth stats */
  676. int idx;
  677. if (vsi->type != I40E_VSI_FCOE)
  678. return;
  679. idx = (pf->pf_seid - I40E_BASE_PF_SEID) + I40E_FCOE_PF_STAT_OFFSET;
  680. fs = &vsi->fcoe_stats;
  681. ofs = &vsi->fcoe_stats_offsets;
  682. i40e_stat_update32(hw, I40E_GL_FCOEPRC(idx),
  683. vsi->fcoe_stat_offsets_loaded,
  684. &ofs->rx_fcoe_packets, &fs->rx_fcoe_packets);
  685. i40e_stat_update48(hw, I40E_GL_FCOEDWRCH(idx), I40E_GL_FCOEDWRCL(idx),
  686. vsi->fcoe_stat_offsets_loaded,
  687. &ofs->rx_fcoe_dwords, &fs->rx_fcoe_dwords);
  688. i40e_stat_update32(hw, I40E_GL_FCOERPDC(idx),
  689. vsi->fcoe_stat_offsets_loaded,
  690. &ofs->rx_fcoe_dropped, &fs->rx_fcoe_dropped);
  691. i40e_stat_update32(hw, I40E_GL_FCOEPTC(idx),
  692. vsi->fcoe_stat_offsets_loaded,
  693. &ofs->tx_fcoe_packets, &fs->tx_fcoe_packets);
  694. i40e_stat_update48(hw, I40E_GL_FCOEDWTCH(idx), I40E_GL_FCOEDWTCL(idx),
  695. vsi->fcoe_stat_offsets_loaded,
  696. &ofs->tx_fcoe_dwords, &fs->tx_fcoe_dwords);
  697. i40e_stat_update32(hw, I40E_GL_FCOECRC(idx),
  698. vsi->fcoe_stat_offsets_loaded,
  699. &ofs->fcoe_bad_fccrc, &fs->fcoe_bad_fccrc);
  700. i40e_stat_update32(hw, I40E_GL_FCOELAST(idx),
  701. vsi->fcoe_stat_offsets_loaded,
  702. &ofs->fcoe_last_error, &fs->fcoe_last_error);
  703. i40e_stat_update32(hw, I40E_GL_FCOEDDPC(idx),
  704. vsi->fcoe_stat_offsets_loaded,
  705. &ofs->fcoe_ddp_count, &fs->fcoe_ddp_count);
  706. vsi->fcoe_stat_offsets_loaded = true;
  707. }
  708. #endif
  709. /**
  710. * i40e_update_vsi_stats - Update the vsi statistics counters.
  711. * @vsi: the VSI to be updated
  712. *
  713. * There are a few instances where we store the same stat in a
  714. * couple of different structs. This is partly because we have
  715. * the netdev stats that need to be filled out, which is slightly
  716. * different from the "eth_stats" defined by the chip and used in
  717. * VF communications. We sort it out here.
  718. **/
  719. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  720. {
  721. struct i40e_pf *pf = vsi->back;
  722. struct rtnl_link_stats64 *ons;
  723. struct rtnl_link_stats64 *ns; /* netdev stats */
  724. struct i40e_eth_stats *oes;
  725. struct i40e_eth_stats *es; /* device's eth stats */
  726. u32 tx_restart, tx_busy;
  727. struct i40e_ring *p;
  728. u32 rx_page, rx_buf;
  729. u64 bytes, packets;
  730. unsigned int start;
  731. u64 tx_linearize;
  732. u64 tx_force_wb;
  733. u64 rx_p, rx_b;
  734. u64 tx_p, tx_b;
  735. u16 q;
  736. if (test_bit(__I40E_DOWN, &vsi->state) ||
  737. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  738. return;
  739. ns = i40e_get_vsi_stats_struct(vsi);
  740. ons = &vsi->net_stats_offsets;
  741. es = &vsi->eth_stats;
  742. oes = &vsi->eth_stats_offsets;
  743. /* Gather up the netdev and vsi stats that the driver collects
  744. * on the fly during packet processing
  745. */
  746. rx_b = rx_p = 0;
  747. tx_b = tx_p = 0;
  748. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  749. rx_page = 0;
  750. rx_buf = 0;
  751. rcu_read_lock();
  752. for (q = 0; q < vsi->num_queue_pairs; q++) {
  753. /* locate Tx ring */
  754. p = ACCESS_ONCE(vsi->tx_rings[q]);
  755. do {
  756. start = u64_stats_fetch_begin_irq(&p->syncp);
  757. packets = p->stats.packets;
  758. bytes = p->stats.bytes;
  759. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  760. tx_b += bytes;
  761. tx_p += packets;
  762. tx_restart += p->tx_stats.restart_queue;
  763. tx_busy += p->tx_stats.tx_busy;
  764. tx_linearize += p->tx_stats.tx_linearize;
  765. tx_force_wb += p->tx_stats.tx_force_wb;
  766. /* Rx queue is part of the same block as Tx queue */
  767. p = &p[1];
  768. do {
  769. start = u64_stats_fetch_begin_irq(&p->syncp);
  770. packets = p->stats.packets;
  771. bytes = p->stats.bytes;
  772. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  773. rx_b += bytes;
  774. rx_p += packets;
  775. rx_buf += p->rx_stats.alloc_buff_failed;
  776. rx_page += p->rx_stats.alloc_page_failed;
  777. }
  778. rcu_read_unlock();
  779. vsi->tx_restart = tx_restart;
  780. vsi->tx_busy = tx_busy;
  781. vsi->tx_linearize = tx_linearize;
  782. vsi->tx_force_wb = tx_force_wb;
  783. vsi->rx_page_failed = rx_page;
  784. vsi->rx_buf_failed = rx_buf;
  785. ns->rx_packets = rx_p;
  786. ns->rx_bytes = rx_b;
  787. ns->tx_packets = tx_p;
  788. ns->tx_bytes = tx_b;
  789. /* update netdev stats from eth stats */
  790. i40e_update_eth_stats(vsi);
  791. ons->tx_errors = oes->tx_errors;
  792. ns->tx_errors = es->tx_errors;
  793. ons->multicast = oes->rx_multicast;
  794. ns->multicast = es->rx_multicast;
  795. ons->rx_dropped = oes->rx_discards;
  796. ns->rx_dropped = es->rx_discards;
  797. ons->tx_dropped = oes->tx_discards;
  798. ns->tx_dropped = es->tx_discards;
  799. /* pull in a couple PF stats if this is the main vsi */
  800. if (vsi == pf->vsi[pf->lan_vsi]) {
  801. ns->rx_crc_errors = pf->stats.crc_errors;
  802. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  803. ns->rx_length_errors = pf->stats.rx_length_errors;
  804. }
  805. }
  806. /**
  807. * i40e_update_pf_stats - Update the PF statistics counters.
  808. * @pf: the PF to be updated
  809. **/
  810. static void i40e_update_pf_stats(struct i40e_pf *pf)
  811. {
  812. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  813. struct i40e_hw_port_stats *nsd = &pf->stats;
  814. struct i40e_hw *hw = &pf->hw;
  815. u32 val;
  816. int i;
  817. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  818. I40E_GLPRT_GORCL(hw->port),
  819. pf->stat_offsets_loaded,
  820. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  821. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  822. I40E_GLPRT_GOTCL(hw->port),
  823. pf->stat_offsets_loaded,
  824. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  825. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  826. pf->stat_offsets_loaded,
  827. &osd->eth.rx_discards,
  828. &nsd->eth.rx_discards);
  829. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  830. I40E_GLPRT_UPRCL(hw->port),
  831. pf->stat_offsets_loaded,
  832. &osd->eth.rx_unicast,
  833. &nsd->eth.rx_unicast);
  834. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  835. I40E_GLPRT_MPRCL(hw->port),
  836. pf->stat_offsets_loaded,
  837. &osd->eth.rx_multicast,
  838. &nsd->eth.rx_multicast);
  839. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  840. I40E_GLPRT_BPRCL(hw->port),
  841. pf->stat_offsets_loaded,
  842. &osd->eth.rx_broadcast,
  843. &nsd->eth.rx_broadcast);
  844. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  845. I40E_GLPRT_UPTCL(hw->port),
  846. pf->stat_offsets_loaded,
  847. &osd->eth.tx_unicast,
  848. &nsd->eth.tx_unicast);
  849. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  850. I40E_GLPRT_MPTCL(hw->port),
  851. pf->stat_offsets_loaded,
  852. &osd->eth.tx_multicast,
  853. &nsd->eth.tx_multicast);
  854. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  855. I40E_GLPRT_BPTCL(hw->port),
  856. pf->stat_offsets_loaded,
  857. &osd->eth.tx_broadcast,
  858. &nsd->eth.tx_broadcast);
  859. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  860. pf->stat_offsets_loaded,
  861. &osd->tx_dropped_link_down,
  862. &nsd->tx_dropped_link_down);
  863. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  864. pf->stat_offsets_loaded,
  865. &osd->crc_errors, &nsd->crc_errors);
  866. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  867. pf->stat_offsets_loaded,
  868. &osd->illegal_bytes, &nsd->illegal_bytes);
  869. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  870. pf->stat_offsets_loaded,
  871. &osd->mac_local_faults,
  872. &nsd->mac_local_faults);
  873. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  874. pf->stat_offsets_loaded,
  875. &osd->mac_remote_faults,
  876. &nsd->mac_remote_faults);
  877. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  878. pf->stat_offsets_loaded,
  879. &osd->rx_length_errors,
  880. &nsd->rx_length_errors);
  881. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  882. pf->stat_offsets_loaded,
  883. &osd->link_xon_rx, &nsd->link_xon_rx);
  884. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  885. pf->stat_offsets_loaded,
  886. &osd->link_xon_tx, &nsd->link_xon_tx);
  887. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  888. pf->stat_offsets_loaded,
  889. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  890. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  891. pf->stat_offsets_loaded,
  892. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  893. for (i = 0; i < 8; i++) {
  894. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  895. pf->stat_offsets_loaded,
  896. &osd->priority_xoff_rx[i],
  897. &nsd->priority_xoff_rx[i]);
  898. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  899. pf->stat_offsets_loaded,
  900. &osd->priority_xon_rx[i],
  901. &nsd->priority_xon_rx[i]);
  902. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  903. pf->stat_offsets_loaded,
  904. &osd->priority_xon_tx[i],
  905. &nsd->priority_xon_tx[i]);
  906. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  907. pf->stat_offsets_loaded,
  908. &osd->priority_xoff_tx[i],
  909. &nsd->priority_xoff_tx[i]);
  910. i40e_stat_update32(hw,
  911. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  912. pf->stat_offsets_loaded,
  913. &osd->priority_xon_2_xoff[i],
  914. &nsd->priority_xon_2_xoff[i]);
  915. }
  916. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  917. I40E_GLPRT_PRC64L(hw->port),
  918. pf->stat_offsets_loaded,
  919. &osd->rx_size_64, &nsd->rx_size_64);
  920. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  921. I40E_GLPRT_PRC127L(hw->port),
  922. pf->stat_offsets_loaded,
  923. &osd->rx_size_127, &nsd->rx_size_127);
  924. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  925. I40E_GLPRT_PRC255L(hw->port),
  926. pf->stat_offsets_loaded,
  927. &osd->rx_size_255, &nsd->rx_size_255);
  928. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  929. I40E_GLPRT_PRC511L(hw->port),
  930. pf->stat_offsets_loaded,
  931. &osd->rx_size_511, &nsd->rx_size_511);
  932. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  933. I40E_GLPRT_PRC1023L(hw->port),
  934. pf->stat_offsets_loaded,
  935. &osd->rx_size_1023, &nsd->rx_size_1023);
  936. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  937. I40E_GLPRT_PRC1522L(hw->port),
  938. pf->stat_offsets_loaded,
  939. &osd->rx_size_1522, &nsd->rx_size_1522);
  940. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  941. I40E_GLPRT_PRC9522L(hw->port),
  942. pf->stat_offsets_loaded,
  943. &osd->rx_size_big, &nsd->rx_size_big);
  944. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  945. I40E_GLPRT_PTC64L(hw->port),
  946. pf->stat_offsets_loaded,
  947. &osd->tx_size_64, &nsd->tx_size_64);
  948. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  949. I40E_GLPRT_PTC127L(hw->port),
  950. pf->stat_offsets_loaded,
  951. &osd->tx_size_127, &nsd->tx_size_127);
  952. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  953. I40E_GLPRT_PTC255L(hw->port),
  954. pf->stat_offsets_loaded,
  955. &osd->tx_size_255, &nsd->tx_size_255);
  956. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  957. I40E_GLPRT_PTC511L(hw->port),
  958. pf->stat_offsets_loaded,
  959. &osd->tx_size_511, &nsd->tx_size_511);
  960. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  961. I40E_GLPRT_PTC1023L(hw->port),
  962. pf->stat_offsets_loaded,
  963. &osd->tx_size_1023, &nsd->tx_size_1023);
  964. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  965. I40E_GLPRT_PTC1522L(hw->port),
  966. pf->stat_offsets_loaded,
  967. &osd->tx_size_1522, &nsd->tx_size_1522);
  968. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  969. I40E_GLPRT_PTC9522L(hw->port),
  970. pf->stat_offsets_loaded,
  971. &osd->tx_size_big, &nsd->tx_size_big);
  972. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  973. pf->stat_offsets_loaded,
  974. &osd->rx_undersize, &nsd->rx_undersize);
  975. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  976. pf->stat_offsets_loaded,
  977. &osd->rx_fragments, &nsd->rx_fragments);
  978. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  979. pf->stat_offsets_loaded,
  980. &osd->rx_oversize, &nsd->rx_oversize);
  981. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  982. pf->stat_offsets_loaded,
  983. &osd->rx_jabber, &nsd->rx_jabber);
  984. /* FDIR stats */
  985. i40e_stat_update32(hw,
  986. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
  987. pf->stat_offsets_loaded,
  988. &osd->fd_atr_match, &nsd->fd_atr_match);
  989. i40e_stat_update32(hw,
  990. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
  991. pf->stat_offsets_loaded,
  992. &osd->fd_sb_match, &nsd->fd_sb_match);
  993. i40e_stat_update32(hw,
  994. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
  995. pf->stat_offsets_loaded,
  996. &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
  997. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  998. nsd->tx_lpi_status =
  999. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  1000. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  1001. nsd->rx_lpi_status =
  1002. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  1003. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  1004. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  1005. pf->stat_offsets_loaded,
  1006. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  1007. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  1008. pf->stat_offsets_loaded,
  1009. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  1010. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  1011. !(pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED))
  1012. nsd->fd_sb_status = true;
  1013. else
  1014. nsd->fd_sb_status = false;
  1015. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  1016. !(pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
  1017. nsd->fd_atr_status = true;
  1018. else
  1019. nsd->fd_atr_status = false;
  1020. pf->stat_offsets_loaded = true;
  1021. }
  1022. /**
  1023. * i40e_update_stats - Update the various statistics counters.
  1024. * @vsi: the VSI to be updated
  1025. *
  1026. * Update the various stats for this VSI and its related entities.
  1027. **/
  1028. void i40e_update_stats(struct i40e_vsi *vsi)
  1029. {
  1030. struct i40e_pf *pf = vsi->back;
  1031. if (vsi == pf->vsi[pf->lan_vsi])
  1032. i40e_update_pf_stats(pf);
  1033. i40e_update_vsi_stats(vsi);
  1034. #ifdef I40E_FCOE
  1035. i40e_update_fcoe_stats(vsi);
  1036. #endif
  1037. }
  1038. /**
  1039. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  1040. * @vsi: the VSI to be searched
  1041. * @macaddr: the MAC address
  1042. * @vlan: the vlan
  1043. * @is_vf: make sure its a VF filter, else doesn't matter
  1044. * @is_netdev: make sure its a netdev filter, else doesn't matter
  1045. *
  1046. * Returns ptr to the filter object or NULL
  1047. **/
  1048. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  1049. u8 *macaddr, s16 vlan,
  1050. bool is_vf, bool is_netdev)
  1051. {
  1052. struct i40e_mac_filter *f;
  1053. if (!vsi || !macaddr)
  1054. return NULL;
  1055. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1056. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  1057. (vlan == f->vlan) &&
  1058. (!is_vf || f->is_vf) &&
  1059. (!is_netdev || f->is_netdev))
  1060. return f;
  1061. }
  1062. return NULL;
  1063. }
  1064. /**
  1065. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1066. * @vsi: the VSI to be searched
  1067. * @macaddr: the MAC address we are searching for
  1068. * @is_vf: make sure its a VF filter, else doesn't matter
  1069. * @is_netdev: make sure its a netdev filter, else doesn't matter
  1070. *
  1071. * Returns the first filter with the provided MAC address or NULL if
  1072. * MAC address was not found
  1073. **/
  1074. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
  1075. bool is_vf, bool is_netdev)
  1076. {
  1077. struct i40e_mac_filter *f;
  1078. if (!vsi || !macaddr)
  1079. return NULL;
  1080. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1081. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  1082. (!is_vf || f->is_vf) &&
  1083. (!is_netdev || f->is_netdev))
  1084. return f;
  1085. }
  1086. return NULL;
  1087. }
  1088. /**
  1089. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1090. * @vsi: the VSI to be searched
  1091. *
  1092. * Returns true if VSI is in vlan mode or false otherwise
  1093. **/
  1094. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1095. {
  1096. struct i40e_mac_filter *f;
  1097. /* Only -1 for all the filters denotes not in vlan mode
  1098. * so we have to go through all the list in order to make sure
  1099. */
  1100. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1101. if (f->vlan >= 0 || vsi->info.pvid)
  1102. return true;
  1103. }
  1104. return false;
  1105. }
  1106. /**
  1107. * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
  1108. * @vsi: the VSI to be searched
  1109. * @macaddr: the mac address to be filtered
  1110. * @is_vf: true if it is a VF
  1111. * @is_netdev: true if it is a netdev
  1112. *
  1113. * Goes through all the macvlan filters and adds a
  1114. * macvlan filter for each unique vlan that already exists
  1115. *
  1116. * Returns first filter found on success, else NULL
  1117. **/
  1118. struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
  1119. bool is_vf, bool is_netdev)
  1120. {
  1121. struct i40e_mac_filter *f;
  1122. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1123. if (vsi->info.pvid)
  1124. f->vlan = le16_to_cpu(vsi->info.pvid);
  1125. if (!i40e_find_filter(vsi, macaddr, f->vlan,
  1126. is_vf, is_netdev)) {
  1127. if (!i40e_add_filter(vsi, macaddr, f->vlan,
  1128. is_vf, is_netdev))
  1129. return NULL;
  1130. }
  1131. }
  1132. return list_first_entry_or_null(&vsi->mac_filter_list,
  1133. struct i40e_mac_filter, list);
  1134. }
  1135. /**
  1136. * i40e_del_mac_all_vlan - Remove a MAC filter from all VLANS
  1137. * @vsi: the VSI to be searched
  1138. * @macaddr: the mac address to be removed
  1139. * @is_vf: true if it is a VF
  1140. * @is_netdev: true if it is a netdev
  1141. *
  1142. * Removes a given MAC address from a VSI, regardless of VLAN
  1143. *
  1144. * Returns 0 for success, or error
  1145. **/
  1146. int i40e_del_mac_all_vlan(struct i40e_vsi *vsi, u8 *macaddr,
  1147. bool is_vf, bool is_netdev)
  1148. {
  1149. struct i40e_mac_filter *f = NULL;
  1150. int changed = 0;
  1151. WARN(!spin_is_locked(&vsi->mac_filter_list_lock),
  1152. "Missing mac_filter_list_lock\n");
  1153. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1154. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  1155. (is_vf == f->is_vf) &&
  1156. (is_netdev == f->is_netdev)) {
  1157. f->counter--;
  1158. f->changed = true;
  1159. changed = 1;
  1160. }
  1161. }
  1162. if (changed) {
  1163. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1164. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1165. return 0;
  1166. }
  1167. return -ENOENT;
  1168. }
  1169. /**
  1170. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1171. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1172. * @macaddr: the MAC address
  1173. *
  1174. * Some older firmware configurations set up a default promiscuous VLAN
  1175. * filter that needs to be removed.
  1176. **/
  1177. static int i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1178. {
  1179. struct i40e_aqc_remove_macvlan_element_data element;
  1180. struct i40e_pf *pf = vsi->back;
  1181. i40e_status ret;
  1182. /* Only appropriate for the PF main VSI */
  1183. if (vsi->type != I40E_VSI_MAIN)
  1184. return -EINVAL;
  1185. memset(&element, 0, sizeof(element));
  1186. ether_addr_copy(element.mac_addr, macaddr);
  1187. element.vlan_tag = 0;
  1188. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1189. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1190. ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1191. if (ret)
  1192. return -ENOENT;
  1193. return 0;
  1194. }
  1195. /**
  1196. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1197. * @vsi: the VSI to be searched
  1198. * @macaddr: the MAC address
  1199. * @vlan: the vlan
  1200. * @is_vf: make sure its a VF filter, else doesn't matter
  1201. * @is_netdev: make sure its a netdev filter, else doesn't matter
  1202. *
  1203. * Returns ptr to the filter object or NULL when no memory available.
  1204. *
  1205. * NOTE: This function is expected to be called with mac_filter_list_lock
  1206. * being held.
  1207. **/
  1208. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1209. u8 *macaddr, s16 vlan,
  1210. bool is_vf, bool is_netdev)
  1211. {
  1212. struct i40e_mac_filter *f;
  1213. if (!vsi || !macaddr)
  1214. return NULL;
  1215. f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
  1216. if (!f) {
  1217. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1218. if (!f)
  1219. goto add_filter_out;
  1220. ether_addr_copy(f->macaddr, macaddr);
  1221. f->vlan = vlan;
  1222. f->changed = true;
  1223. INIT_LIST_HEAD(&f->list);
  1224. list_add(&f->list, &vsi->mac_filter_list);
  1225. }
  1226. /* increment counter and add a new flag if needed */
  1227. if (is_vf) {
  1228. if (!f->is_vf) {
  1229. f->is_vf = true;
  1230. f->counter++;
  1231. }
  1232. } else if (is_netdev) {
  1233. if (!f->is_netdev) {
  1234. f->is_netdev = true;
  1235. f->counter++;
  1236. }
  1237. } else {
  1238. f->counter++;
  1239. }
  1240. /* changed tells sync_filters_subtask to
  1241. * push the filter down to the firmware
  1242. */
  1243. if (f->changed) {
  1244. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1245. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1246. }
  1247. add_filter_out:
  1248. return f;
  1249. }
  1250. /**
  1251. * i40e_del_filter - Remove a mac/vlan filter from the VSI
  1252. * @vsi: the VSI to be searched
  1253. * @macaddr: the MAC address
  1254. * @vlan: the vlan
  1255. * @is_vf: make sure it's a VF filter, else doesn't matter
  1256. * @is_netdev: make sure it's a netdev filter, else doesn't matter
  1257. *
  1258. * NOTE: This function is expected to be called with mac_filter_list_lock
  1259. * being held.
  1260. **/
  1261. void i40e_del_filter(struct i40e_vsi *vsi,
  1262. u8 *macaddr, s16 vlan,
  1263. bool is_vf, bool is_netdev)
  1264. {
  1265. struct i40e_mac_filter *f;
  1266. if (!vsi || !macaddr)
  1267. return;
  1268. f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
  1269. if (!f || f->counter == 0)
  1270. return;
  1271. if (is_vf) {
  1272. if (f->is_vf) {
  1273. f->is_vf = false;
  1274. f->counter--;
  1275. }
  1276. } else if (is_netdev) {
  1277. if (f->is_netdev) {
  1278. f->is_netdev = false;
  1279. f->counter--;
  1280. }
  1281. } else {
  1282. /* make sure we don't remove a filter in use by VF or netdev */
  1283. int min_f = 0;
  1284. min_f += (f->is_vf ? 1 : 0);
  1285. min_f += (f->is_netdev ? 1 : 0);
  1286. if (f->counter > min_f)
  1287. f->counter--;
  1288. }
  1289. /* counter == 0 tells sync_filters_subtask to
  1290. * remove the filter from the firmware's list
  1291. */
  1292. if (f->counter == 0) {
  1293. f->changed = true;
  1294. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1295. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1296. }
  1297. }
  1298. /**
  1299. * i40e_set_mac - NDO callback to set mac address
  1300. * @netdev: network interface device structure
  1301. * @p: pointer to an address structure
  1302. *
  1303. * Returns 0 on success, negative on failure
  1304. **/
  1305. #ifdef I40E_FCOE
  1306. int i40e_set_mac(struct net_device *netdev, void *p)
  1307. #else
  1308. static int i40e_set_mac(struct net_device *netdev, void *p)
  1309. #endif
  1310. {
  1311. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1312. struct i40e_vsi *vsi = np->vsi;
  1313. struct i40e_pf *pf = vsi->back;
  1314. struct i40e_hw *hw = &pf->hw;
  1315. struct sockaddr *addr = p;
  1316. struct i40e_mac_filter *f;
  1317. if (!is_valid_ether_addr(addr->sa_data))
  1318. return -EADDRNOTAVAIL;
  1319. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1320. netdev_info(netdev, "already using mac address %pM\n",
  1321. addr->sa_data);
  1322. return 0;
  1323. }
  1324. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1325. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1326. return -EADDRNOTAVAIL;
  1327. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1328. netdev_info(netdev, "returning to hw mac address %pM\n",
  1329. hw->mac.addr);
  1330. else
  1331. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1332. if (vsi->type == I40E_VSI_MAIN) {
  1333. i40e_status ret;
  1334. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1335. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1336. addr->sa_data, NULL);
  1337. if (ret) {
  1338. netdev_info(netdev,
  1339. "Addr change for Main VSI failed: %d\n",
  1340. ret);
  1341. return -EADDRNOTAVAIL;
  1342. }
  1343. }
  1344. if (ether_addr_equal(netdev->dev_addr, hw->mac.addr)) {
  1345. struct i40e_aqc_remove_macvlan_element_data element;
  1346. memset(&element, 0, sizeof(element));
  1347. ether_addr_copy(element.mac_addr, netdev->dev_addr);
  1348. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1349. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1350. } else {
  1351. spin_lock_bh(&vsi->mac_filter_list_lock);
  1352. i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
  1353. false, false);
  1354. spin_unlock_bh(&vsi->mac_filter_list_lock);
  1355. }
  1356. if (ether_addr_equal(addr->sa_data, hw->mac.addr)) {
  1357. struct i40e_aqc_add_macvlan_element_data element;
  1358. memset(&element, 0, sizeof(element));
  1359. ether_addr_copy(element.mac_addr, hw->mac.addr);
  1360. element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
  1361. i40e_aq_add_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1362. } else {
  1363. spin_lock_bh(&vsi->mac_filter_list_lock);
  1364. f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY,
  1365. false, false);
  1366. if (f)
  1367. f->is_laa = true;
  1368. spin_unlock_bh(&vsi->mac_filter_list_lock);
  1369. }
  1370. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1371. return i40e_sync_vsi_filters(vsi);
  1372. }
  1373. /**
  1374. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1375. * @vsi: the VSI being setup
  1376. * @ctxt: VSI context structure
  1377. * @enabled_tc: Enabled TCs bitmap
  1378. * @is_add: True if called before Add VSI
  1379. *
  1380. * Setup VSI queue mapping for enabled traffic classes.
  1381. **/
  1382. #ifdef I40E_FCOE
  1383. void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1384. struct i40e_vsi_context *ctxt,
  1385. u8 enabled_tc,
  1386. bool is_add)
  1387. #else
  1388. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1389. struct i40e_vsi_context *ctxt,
  1390. u8 enabled_tc,
  1391. bool is_add)
  1392. #endif
  1393. {
  1394. struct i40e_pf *pf = vsi->back;
  1395. u16 sections = 0;
  1396. u8 netdev_tc = 0;
  1397. u16 numtc = 0;
  1398. u16 qcount;
  1399. u8 offset;
  1400. u16 qmap;
  1401. int i;
  1402. u16 num_tc_qps = 0;
  1403. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1404. offset = 0;
  1405. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1406. /* Find numtc from enabled TC bitmap */
  1407. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1408. if (enabled_tc & BIT(i)) /* TC is enabled */
  1409. numtc++;
  1410. }
  1411. if (!numtc) {
  1412. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1413. numtc = 1;
  1414. }
  1415. } else {
  1416. /* At least TC0 is enabled in case of non-DCB case */
  1417. numtc = 1;
  1418. }
  1419. vsi->tc_config.numtc = numtc;
  1420. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1421. /* Number of queues per enabled TC */
  1422. /* In MFP case we can have a much lower count of MSIx
  1423. * vectors available and so we need to lower the used
  1424. * q count.
  1425. */
  1426. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1427. qcount = min_t(int, vsi->alloc_queue_pairs, pf->num_lan_msix);
  1428. else
  1429. qcount = vsi->alloc_queue_pairs;
  1430. num_tc_qps = qcount / numtc;
  1431. num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
  1432. /* Setup queue offset/count for all TCs for given VSI */
  1433. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1434. /* See if the given TC is enabled for the given VSI */
  1435. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1436. /* TC is enabled */
  1437. int pow, num_qps;
  1438. switch (vsi->type) {
  1439. case I40E_VSI_MAIN:
  1440. qcount = min_t(int, pf->alloc_rss_size,
  1441. num_tc_qps);
  1442. break;
  1443. #ifdef I40E_FCOE
  1444. case I40E_VSI_FCOE:
  1445. qcount = num_tc_qps;
  1446. break;
  1447. #endif
  1448. case I40E_VSI_FDIR:
  1449. case I40E_VSI_SRIOV:
  1450. case I40E_VSI_VMDQ2:
  1451. default:
  1452. qcount = num_tc_qps;
  1453. WARN_ON(i != 0);
  1454. break;
  1455. }
  1456. vsi->tc_config.tc_info[i].qoffset = offset;
  1457. vsi->tc_config.tc_info[i].qcount = qcount;
  1458. /* find the next higher power-of-2 of num queue pairs */
  1459. num_qps = qcount;
  1460. pow = 0;
  1461. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1462. pow++;
  1463. num_qps >>= 1;
  1464. }
  1465. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1466. qmap =
  1467. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1468. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1469. offset += qcount;
  1470. } else {
  1471. /* TC is not enabled so set the offset to
  1472. * default queue and allocate one queue
  1473. * for the given TC.
  1474. */
  1475. vsi->tc_config.tc_info[i].qoffset = 0;
  1476. vsi->tc_config.tc_info[i].qcount = 1;
  1477. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1478. qmap = 0;
  1479. }
  1480. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1481. }
  1482. /* Set actual Tx/Rx queue pairs */
  1483. vsi->num_queue_pairs = offset;
  1484. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1485. if (vsi->req_queue_pairs > 0)
  1486. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1487. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1488. vsi->num_queue_pairs = pf->num_lan_msix;
  1489. }
  1490. /* Scheduler section valid can only be set for ADD VSI */
  1491. if (is_add) {
  1492. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1493. ctxt->info.up_enable_bits = enabled_tc;
  1494. }
  1495. if (vsi->type == I40E_VSI_SRIOV) {
  1496. ctxt->info.mapping_flags |=
  1497. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1498. for (i = 0; i < vsi->num_queue_pairs; i++)
  1499. ctxt->info.queue_mapping[i] =
  1500. cpu_to_le16(vsi->base_queue + i);
  1501. } else {
  1502. ctxt->info.mapping_flags |=
  1503. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1504. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1505. }
  1506. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1507. }
  1508. /**
  1509. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1510. * @netdev: network interface device structure
  1511. **/
  1512. #ifdef I40E_FCOE
  1513. void i40e_set_rx_mode(struct net_device *netdev)
  1514. #else
  1515. static void i40e_set_rx_mode(struct net_device *netdev)
  1516. #endif
  1517. {
  1518. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1519. struct i40e_mac_filter *f, *ftmp;
  1520. struct i40e_vsi *vsi = np->vsi;
  1521. struct netdev_hw_addr *uca;
  1522. struct netdev_hw_addr *mca;
  1523. struct netdev_hw_addr *ha;
  1524. spin_lock_bh(&vsi->mac_filter_list_lock);
  1525. /* add addr if not already in the filter list */
  1526. netdev_for_each_uc_addr(uca, netdev) {
  1527. if (!i40e_find_mac(vsi, uca->addr, false, true)) {
  1528. if (i40e_is_vsi_in_vlan(vsi))
  1529. i40e_put_mac_in_vlan(vsi, uca->addr,
  1530. false, true);
  1531. else
  1532. i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
  1533. false, true);
  1534. }
  1535. }
  1536. netdev_for_each_mc_addr(mca, netdev) {
  1537. if (!i40e_find_mac(vsi, mca->addr, false, true)) {
  1538. if (i40e_is_vsi_in_vlan(vsi))
  1539. i40e_put_mac_in_vlan(vsi, mca->addr,
  1540. false, true);
  1541. else
  1542. i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
  1543. false, true);
  1544. }
  1545. }
  1546. /* remove filter if not in netdev list */
  1547. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1548. if (!f->is_netdev)
  1549. continue;
  1550. netdev_for_each_mc_addr(mca, netdev)
  1551. if (ether_addr_equal(mca->addr, f->macaddr))
  1552. goto bottom_of_search_loop;
  1553. netdev_for_each_uc_addr(uca, netdev)
  1554. if (ether_addr_equal(uca->addr, f->macaddr))
  1555. goto bottom_of_search_loop;
  1556. for_each_dev_addr(netdev, ha)
  1557. if (ether_addr_equal(ha->addr, f->macaddr))
  1558. goto bottom_of_search_loop;
  1559. /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
  1560. i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY, false, true);
  1561. bottom_of_search_loop:
  1562. continue;
  1563. }
  1564. spin_unlock_bh(&vsi->mac_filter_list_lock);
  1565. /* check for other flag changes */
  1566. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1567. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1568. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1569. }
  1570. }
  1571. /**
  1572. * i40e_mac_filter_entry_clone - Clones a MAC filter entry
  1573. * @src: source MAC filter entry to be clones
  1574. *
  1575. * Returns the pointer to newly cloned MAC filter entry or NULL
  1576. * in case of error
  1577. **/
  1578. static struct i40e_mac_filter *i40e_mac_filter_entry_clone(
  1579. struct i40e_mac_filter *src)
  1580. {
  1581. struct i40e_mac_filter *f;
  1582. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1583. if (!f)
  1584. return NULL;
  1585. *f = *src;
  1586. INIT_LIST_HEAD(&f->list);
  1587. return f;
  1588. }
  1589. /**
  1590. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1591. * @vsi: pointer to vsi struct
  1592. * @from: Pointer to list which contains MAC filter entries - changes to
  1593. * those entries needs to be undone.
  1594. *
  1595. * MAC filter entries from list were slated to be removed from device.
  1596. **/
  1597. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1598. struct list_head *from)
  1599. {
  1600. struct i40e_mac_filter *f, *ftmp;
  1601. list_for_each_entry_safe(f, ftmp, from, list) {
  1602. f->changed = true;
  1603. /* Move the element back into MAC filter list*/
  1604. list_move_tail(&f->list, &vsi->mac_filter_list);
  1605. }
  1606. }
  1607. /**
  1608. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1609. * @vsi: pointer to vsi struct
  1610. *
  1611. * MAC filter entries from list were slated to be added from device.
  1612. **/
  1613. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi)
  1614. {
  1615. struct i40e_mac_filter *f, *ftmp;
  1616. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1617. if (!f->changed && f->counter)
  1618. f->changed = true;
  1619. }
  1620. }
  1621. /**
  1622. * i40e_cleanup_add_list - Deletes the element from add list and release
  1623. * memory
  1624. * @add_list: Pointer to list which contains MAC filter entries
  1625. **/
  1626. static void i40e_cleanup_add_list(struct list_head *add_list)
  1627. {
  1628. struct i40e_mac_filter *f, *ftmp;
  1629. list_for_each_entry_safe(f, ftmp, add_list, list) {
  1630. list_del(&f->list);
  1631. kfree(f);
  1632. }
  1633. }
  1634. /**
  1635. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1636. * @vsi: ptr to the VSI
  1637. *
  1638. * Push any outstanding VSI filter changes through the AdminQ.
  1639. *
  1640. * Returns 0 or error value
  1641. **/
  1642. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1643. {
  1644. struct list_head tmp_del_list, tmp_add_list;
  1645. struct i40e_mac_filter *f, *ftmp, *fclone;
  1646. bool promisc_forced_on = false;
  1647. bool add_happened = false;
  1648. int filter_list_len = 0;
  1649. u32 changed_flags = 0;
  1650. i40e_status aq_ret = 0;
  1651. bool err_cond = false;
  1652. int retval = 0;
  1653. struct i40e_pf *pf;
  1654. int num_add = 0;
  1655. int num_del = 0;
  1656. int aq_err = 0;
  1657. u16 cmd_flags;
  1658. /* empty array typed pointers, kcalloc later */
  1659. struct i40e_aqc_add_macvlan_element_data *add_list;
  1660. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1661. while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
  1662. usleep_range(1000, 2000);
  1663. pf = vsi->back;
  1664. if (vsi->netdev) {
  1665. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1666. vsi->current_netdev_flags = vsi->netdev->flags;
  1667. }
  1668. INIT_LIST_HEAD(&tmp_del_list);
  1669. INIT_LIST_HEAD(&tmp_add_list);
  1670. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1671. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1672. spin_lock_bh(&vsi->mac_filter_list_lock);
  1673. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1674. if (!f->changed)
  1675. continue;
  1676. if (f->counter != 0)
  1677. continue;
  1678. f->changed = false;
  1679. /* Move the element into temporary del_list */
  1680. list_move_tail(&f->list, &tmp_del_list);
  1681. }
  1682. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1683. if (!f->changed)
  1684. continue;
  1685. if (f->counter == 0)
  1686. continue;
  1687. f->changed = false;
  1688. /* Clone MAC filter entry and add into temporary list */
  1689. fclone = i40e_mac_filter_entry_clone(f);
  1690. if (!fclone) {
  1691. err_cond = true;
  1692. break;
  1693. }
  1694. list_add_tail(&fclone->list, &tmp_add_list);
  1695. }
  1696. /* if failed to clone MAC filter entry - undo */
  1697. if (err_cond) {
  1698. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  1699. i40e_undo_add_filter_entries(vsi);
  1700. }
  1701. spin_unlock_bh(&vsi->mac_filter_list_lock);
  1702. if (err_cond) {
  1703. i40e_cleanup_add_list(&tmp_add_list);
  1704. retval = -ENOMEM;
  1705. goto out;
  1706. }
  1707. }
  1708. /* Now process 'del_list' outside the lock */
  1709. if (!list_empty(&tmp_del_list)) {
  1710. filter_list_len = pf->hw.aq.asq_buf_size /
  1711. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1712. del_list = kcalloc(filter_list_len,
  1713. sizeof(struct i40e_aqc_remove_macvlan_element_data),
  1714. GFP_KERNEL);
  1715. if (!del_list) {
  1716. i40e_cleanup_add_list(&tmp_add_list);
  1717. /* Undo VSI's MAC filter entry element updates */
  1718. spin_lock_bh(&vsi->mac_filter_list_lock);
  1719. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  1720. i40e_undo_add_filter_entries(vsi);
  1721. spin_unlock_bh(&vsi->mac_filter_list_lock);
  1722. retval = -ENOMEM;
  1723. goto out;
  1724. }
  1725. list_for_each_entry_safe(f, ftmp, &tmp_del_list, list) {
  1726. cmd_flags = 0;
  1727. /* add to delete list */
  1728. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  1729. del_list[num_del].vlan_tag =
  1730. cpu_to_le16((u16)(f->vlan ==
  1731. I40E_VLAN_ANY ? 0 : f->vlan));
  1732. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1733. del_list[num_del].flags = cmd_flags;
  1734. num_del++;
  1735. /* flush a full buffer */
  1736. if (num_del == filter_list_len) {
  1737. aq_ret = i40e_aq_remove_macvlan(&pf->hw,
  1738. vsi->seid,
  1739. del_list,
  1740. num_del,
  1741. NULL);
  1742. aq_err = pf->hw.aq.asq_last_status;
  1743. num_del = 0;
  1744. memset(del_list, 0, sizeof(*del_list));
  1745. if (aq_ret && aq_err != I40E_AQ_RC_ENOENT) {
  1746. retval = -EIO;
  1747. dev_err(&pf->pdev->dev,
  1748. "ignoring delete macvlan error, err %s, aq_err %s while flushing a full buffer\n",
  1749. i40e_stat_str(&pf->hw, aq_ret),
  1750. i40e_aq_str(&pf->hw, aq_err));
  1751. }
  1752. }
  1753. /* Release memory for MAC filter entries which were
  1754. * synced up with HW.
  1755. */
  1756. list_del(&f->list);
  1757. kfree(f);
  1758. }
  1759. if (num_del) {
  1760. aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
  1761. del_list, num_del,
  1762. NULL);
  1763. aq_err = pf->hw.aq.asq_last_status;
  1764. num_del = 0;
  1765. if (aq_ret && aq_err != I40E_AQ_RC_ENOENT)
  1766. dev_info(&pf->pdev->dev,
  1767. "ignoring delete macvlan error, err %s aq_err %s\n",
  1768. i40e_stat_str(&pf->hw, aq_ret),
  1769. i40e_aq_str(&pf->hw, aq_err));
  1770. }
  1771. kfree(del_list);
  1772. del_list = NULL;
  1773. }
  1774. if (!list_empty(&tmp_add_list)) {
  1775. /* do all the adds now */
  1776. filter_list_len = pf->hw.aq.asq_buf_size /
  1777. sizeof(struct i40e_aqc_add_macvlan_element_data),
  1778. add_list = kcalloc(filter_list_len,
  1779. sizeof(struct i40e_aqc_add_macvlan_element_data),
  1780. GFP_KERNEL);
  1781. if (!add_list) {
  1782. /* Purge element from temporary lists */
  1783. i40e_cleanup_add_list(&tmp_add_list);
  1784. /* Undo add filter entries from VSI MAC filter list */
  1785. spin_lock_bh(&vsi->mac_filter_list_lock);
  1786. i40e_undo_add_filter_entries(vsi);
  1787. spin_unlock_bh(&vsi->mac_filter_list_lock);
  1788. retval = -ENOMEM;
  1789. goto out;
  1790. }
  1791. list_for_each_entry_safe(f, ftmp, &tmp_add_list, list) {
  1792. add_happened = true;
  1793. cmd_flags = 0;
  1794. /* add to add array */
  1795. ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
  1796. add_list[num_add].vlan_tag =
  1797. cpu_to_le16(
  1798. (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
  1799. add_list[num_add].queue_number = 0;
  1800. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  1801. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  1802. num_add++;
  1803. /* flush a full buffer */
  1804. if (num_add == filter_list_len) {
  1805. aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
  1806. add_list, num_add,
  1807. NULL);
  1808. aq_err = pf->hw.aq.asq_last_status;
  1809. num_add = 0;
  1810. if (aq_ret)
  1811. break;
  1812. memset(add_list, 0, sizeof(*add_list));
  1813. }
  1814. /* Entries from tmp_add_list were cloned from MAC
  1815. * filter list, hence clean those cloned entries
  1816. */
  1817. list_del(&f->list);
  1818. kfree(f);
  1819. }
  1820. if (num_add) {
  1821. aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
  1822. add_list, num_add, NULL);
  1823. aq_err = pf->hw.aq.asq_last_status;
  1824. num_add = 0;
  1825. }
  1826. kfree(add_list);
  1827. add_list = NULL;
  1828. if (add_happened && aq_ret && aq_err != I40E_AQ_RC_EINVAL) {
  1829. retval = i40e_aq_rc_to_posix(aq_ret, aq_err);
  1830. dev_info(&pf->pdev->dev,
  1831. "add filter failed, err %s aq_err %s\n",
  1832. i40e_stat_str(&pf->hw, aq_ret),
  1833. i40e_aq_str(&pf->hw, aq_err));
  1834. if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
  1835. !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1836. &vsi->state)) {
  1837. promisc_forced_on = true;
  1838. set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1839. &vsi->state);
  1840. dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
  1841. }
  1842. }
  1843. }
  1844. /* check for changes in promiscuous modes */
  1845. if (changed_flags & IFF_ALLMULTI) {
  1846. bool cur_multipromisc;
  1847. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  1848. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  1849. vsi->seid,
  1850. cur_multipromisc,
  1851. NULL);
  1852. if (aq_ret) {
  1853. retval = i40e_aq_rc_to_posix(aq_ret,
  1854. pf->hw.aq.asq_last_status);
  1855. dev_info(&pf->pdev->dev,
  1856. "set multi promisc failed, err %s aq_err %s\n",
  1857. i40e_stat_str(&pf->hw, aq_ret),
  1858. i40e_aq_str(&pf->hw,
  1859. pf->hw.aq.asq_last_status));
  1860. }
  1861. }
  1862. if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
  1863. bool cur_promisc;
  1864. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  1865. test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1866. &vsi->state));
  1867. if (vsi->type == I40E_VSI_MAIN && pf->lan_veb != I40E_NO_VEB) {
  1868. /* set defport ON for Main VSI instead of true promisc
  1869. * this way we will get all unicast/multicast and VLAN
  1870. * promisc behavior but will not get VF or VMDq traffic
  1871. * replicated on the Main VSI.
  1872. */
  1873. if (pf->cur_promisc != cur_promisc) {
  1874. pf->cur_promisc = cur_promisc;
  1875. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  1876. }
  1877. } else {
  1878. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  1879. &vsi->back->hw,
  1880. vsi->seid,
  1881. cur_promisc, NULL);
  1882. if (aq_ret) {
  1883. retval =
  1884. i40e_aq_rc_to_posix(aq_ret,
  1885. pf->hw.aq.asq_last_status);
  1886. dev_info(&pf->pdev->dev,
  1887. "set unicast promisc failed, err %d, aq_err %d\n",
  1888. aq_ret, pf->hw.aq.asq_last_status);
  1889. }
  1890. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  1891. &vsi->back->hw,
  1892. vsi->seid,
  1893. cur_promisc, NULL);
  1894. if (aq_ret) {
  1895. retval =
  1896. i40e_aq_rc_to_posix(aq_ret,
  1897. pf->hw.aq.asq_last_status);
  1898. dev_info(&pf->pdev->dev,
  1899. "set multicast promisc failed, err %d, aq_err %d\n",
  1900. aq_ret, pf->hw.aq.asq_last_status);
  1901. }
  1902. }
  1903. aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
  1904. vsi->seid,
  1905. cur_promisc, NULL);
  1906. if (aq_ret) {
  1907. retval = i40e_aq_rc_to_posix(aq_ret,
  1908. pf->hw.aq.asq_last_status);
  1909. dev_info(&pf->pdev->dev,
  1910. "set brdcast promisc failed, err %s, aq_err %s\n",
  1911. i40e_stat_str(&pf->hw, aq_ret),
  1912. i40e_aq_str(&pf->hw,
  1913. pf->hw.aq.asq_last_status));
  1914. }
  1915. }
  1916. out:
  1917. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  1918. return retval;
  1919. }
  1920. /**
  1921. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  1922. * @pf: board private structure
  1923. **/
  1924. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  1925. {
  1926. int v;
  1927. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  1928. return;
  1929. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  1930. for (v = 0; v < pf->num_alloc_vsi; v++) {
  1931. if (pf->vsi[v] &&
  1932. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  1933. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  1934. if (ret) {
  1935. /* come back and try again later */
  1936. pf->flags |= I40E_FLAG_FILTER_SYNC;
  1937. break;
  1938. }
  1939. }
  1940. }
  1941. }
  1942. /**
  1943. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  1944. * @netdev: network interface device structure
  1945. * @new_mtu: new value for maximum frame size
  1946. *
  1947. * Returns 0 on success, negative on failure
  1948. **/
  1949. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  1950. {
  1951. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1952. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  1953. struct i40e_vsi *vsi = np->vsi;
  1954. /* MTU < 68 is an error and causes problems on some kernels */
  1955. if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
  1956. return -EINVAL;
  1957. netdev_info(netdev, "changing MTU from %d to %d\n",
  1958. netdev->mtu, new_mtu);
  1959. netdev->mtu = new_mtu;
  1960. if (netif_running(netdev))
  1961. i40e_vsi_reinit_locked(vsi);
  1962. return 0;
  1963. }
  1964. /**
  1965. * i40e_ioctl - Access the hwtstamp interface
  1966. * @netdev: network interface device structure
  1967. * @ifr: interface request data
  1968. * @cmd: ioctl command
  1969. **/
  1970. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1971. {
  1972. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1973. struct i40e_pf *pf = np->vsi->back;
  1974. switch (cmd) {
  1975. case SIOCGHWTSTAMP:
  1976. return i40e_ptp_get_ts_config(pf, ifr);
  1977. case SIOCSHWTSTAMP:
  1978. return i40e_ptp_set_ts_config(pf, ifr);
  1979. default:
  1980. return -EOPNOTSUPP;
  1981. }
  1982. }
  1983. /**
  1984. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  1985. * @vsi: the vsi being adjusted
  1986. **/
  1987. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  1988. {
  1989. struct i40e_vsi_context ctxt;
  1990. i40e_status ret;
  1991. if ((vsi->info.valid_sections &
  1992. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  1993. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  1994. return; /* already enabled */
  1995. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  1996. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  1997. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  1998. ctxt.seid = vsi->seid;
  1999. ctxt.info = vsi->info;
  2000. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2001. if (ret) {
  2002. dev_info(&vsi->back->pdev->dev,
  2003. "update vlan stripping failed, err %s aq_err %s\n",
  2004. i40e_stat_str(&vsi->back->hw, ret),
  2005. i40e_aq_str(&vsi->back->hw,
  2006. vsi->back->hw.aq.asq_last_status));
  2007. }
  2008. }
  2009. /**
  2010. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2011. * @vsi: the vsi being adjusted
  2012. **/
  2013. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2014. {
  2015. struct i40e_vsi_context ctxt;
  2016. i40e_status ret;
  2017. if ((vsi->info.valid_sections &
  2018. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2019. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2020. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2021. return; /* already disabled */
  2022. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2023. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2024. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2025. ctxt.seid = vsi->seid;
  2026. ctxt.info = vsi->info;
  2027. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2028. if (ret) {
  2029. dev_info(&vsi->back->pdev->dev,
  2030. "update vlan stripping failed, err %s aq_err %s\n",
  2031. i40e_stat_str(&vsi->back->hw, ret),
  2032. i40e_aq_str(&vsi->back->hw,
  2033. vsi->back->hw.aq.asq_last_status));
  2034. }
  2035. }
  2036. /**
  2037. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  2038. * @netdev: network interface to be adjusted
  2039. * @features: netdev features to test if VLAN offload is enabled or not
  2040. **/
  2041. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  2042. {
  2043. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2044. struct i40e_vsi *vsi = np->vsi;
  2045. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2046. i40e_vlan_stripping_enable(vsi);
  2047. else
  2048. i40e_vlan_stripping_disable(vsi);
  2049. }
  2050. /**
  2051. * i40e_vsi_add_vlan - Add vsi membership for given vlan
  2052. * @vsi: the vsi being configured
  2053. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2054. **/
  2055. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
  2056. {
  2057. struct i40e_mac_filter *f, *add_f;
  2058. bool is_netdev, is_vf;
  2059. is_vf = (vsi->type == I40E_VSI_SRIOV);
  2060. is_netdev = !!(vsi->netdev);
  2061. /* Locked once because all functions invoked below iterates list*/
  2062. spin_lock_bh(&vsi->mac_filter_list_lock);
  2063. if (is_netdev) {
  2064. add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
  2065. is_vf, is_netdev);
  2066. if (!add_f) {
  2067. dev_info(&vsi->back->pdev->dev,
  2068. "Could not add vlan filter %d for %pM\n",
  2069. vid, vsi->netdev->dev_addr);
  2070. spin_unlock_bh(&vsi->mac_filter_list_lock);
  2071. return -ENOMEM;
  2072. }
  2073. }
  2074. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  2075. add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
  2076. if (!add_f) {
  2077. dev_info(&vsi->back->pdev->dev,
  2078. "Could not add vlan filter %d for %pM\n",
  2079. vid, f->macaddr);
  2080. spin_unlock_bh(&vsi->mac_filter_list_lock);
  2081. return -ENOMEM;
  2082. }
  2083. }
  2084. /* Now if we add a vlan tag, make sure to check if it is the first
  2085. * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
  2086. * with 0, so we now accept untagged and specified tagged traffic
  2087. * (and not any taged and untagged)
  2088. */
  2089. if (vid > 0) {
  2090. if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
  2091. I40E_VLAN_ANY,
  2092. is_vf, is_netdev)) {
  2093. i40e_del_filter(vsi, vsi->netdev->dev_addr,
  2094. I40E_VLAN_ANY, is_vf, is_netdev);
  2095. add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
  2096. is_vf, is_netdev);
  2097. if (!add_f) {
  2098. dev_info(&vsi->back->pdev->dev,
  2099. "Could not add filter 0 for %pM\n",
  2100. vsi->netdev->dev_addr);
  2101. spin_unlock_bh(&vsi->mac_filter_list_lock);
  2102. return -ENOMEM;
  2103. }
  2104. }
  2105. }
  2106. /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
  2107. if (vid > 0 && !vsi->info.pvid) {
  2108. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  2109. if (!i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
  2110. is_vf, is_netdev))
  2111. continue;
  2112. i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
  2113. is_vf, is_netdev);
  2114. add_f = i40e_add_filter(vsi, f->macaddr,
  2115. 0, is_vf, is_netdev);
  2116. if (!add_f) {
  2117. dev_info(&vsi->back->pdev->dev,
  2118. "Could not add filter 0 for %pM\n",
  2119. f->macaddr);
  2120. spin_unlock_bh(&vsi->mac_filter_list_lock);
  2121. return -ENOMEM;
  2122. }
  2123. }
  2124. }
  2125. spin_unlock_bh(&vsi->mac_filter_list_lock);
  2126. /* schedule our worker thread which will take care of
  2127. * applying the new filter changes
  2128. */
  2129. i40e_service_event_schedule(vsi->back);
  2130. return 0;
  2131. }
  2132. /**
  2133. * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
  2134. * @vsi: the vsi being configured
  2135. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2136. *
  2137. * Return: 0 on success or negative otherwise
  2138. **/
  2139. int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
  2140. {
  2141. struct net_device *netdev = vsi->netdev;
  2142. struct i40e_mac_filter *f, *add_f;
  2143. bool is_vf, is_netdev;
  2144. int filter_count = 0;
  2145. is_vf = (vsi->type == I40E_VSI_SRIOV);
  2146. is_netdev = !!(netdev);
  2147. /* Locked once because all functions invoked below iterates list */
  2148. spin_lock_bh(&vsi->mac_filter_list_lock);
  2149. if (is_netdev)
  2150. i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
  2151. list_for_each_entry(f, &vsi->mac_filter_list, list)
  2152. i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
  2153. /* go through all the filters for this VSI and if there is only
  2154. * vid == 0 it means there are no other filters, so vid 0 must
  2155. * be replaced with -1. This signifies that we should from now
  2156. * on accept any traffic (with any tag present, or untagged)
  2157. */
  2158. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  2159. if (is_netdev) {
  2160. if (f->vlan &&
  2161. ether_addr_equal(netdev->dev_addr, f->macaddr))
  2162. filter_count++;
  2163. }
  2164. if (f->vlan)
  2165. filter_count++;
  2166. }
  2167. if (!filter_count && is_netdev) {
  2168. i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
  2169. f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
  2170. is_vf, is_netdev);
  2171. if (!f) {
  2172. dev_info(&vsi->back->pdev->dev,
  2173. "Could not add filter %d for %pM\n",
  2174. I40E_VLAN_ANY, netdev->dev_addr);
  2175. spin_unlock_bh(&vsi->mac_filter_list_lock);
  2176. return -ENOMEM;
  2177. }
  2178. }
  2179. if (!filter_count) {
  2180. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  2181. i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
  2182. add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
  2183. is_vf, is_netdev);
  2184. if (!add_f) {
  2185. dev_info(&vsi->back->pdev->dev,
  2186. "Could not add filter %d for %pM\n",
  2187. I40E_VLAN_ANY, f->macaddr);
  2188. spin_unlock_bh(&vsi->mac_filter_list_lock);
  2189. return -ENOMEM;
  2190. }
  2191. }
  2192. }
  2193. spin_unlock_bh(&vsi->mac_filter_list_lock);
  2194. /* schedule our worker thread which will take care of
  2195. * applying the new filter changes
  2196. */
  2197. i40e_service_event_schedule(vsi->back);
  2198. return 0;
  2199. }
  2200. /**
  2201. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2202. * @netdev: network interface to be adjusted
  2203. * @vid: vlan id to be added
  2204. *
  2205. * net_device_ops implementation for adding vlan ids
  2206. **/
  2207. #ifdef I40E_FCOE
  2208. int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2209. __always_unused __be16 proto, u16 vid)
  2210. #else
  2211. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2212. __always_unused __be16 proto, u16 vid)
  2213. #endif
  2214. {
  2215. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2216. struct i40e_vsi *vsi = np->vsi;
  2217. int ret = 0;
  2218. if (vid > 4095)
  2219. return -EINVAL;
  2220. netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
  2221. /* If the network stack called us with vid = 0 then
  2222. * it is asking to receive priority tagged packets with
  2223. * vlan id 0. Our HW receives them by default when configured
  2224. * to receive untagged packets so there is no need to add an
  2225. * extra filter for vlan 0 tagged packets.
  2226. */
  2227. if (vid)
  2228. ret = i40e_vsi_add_vlan(vsi, vid);
  2229. if (!ret && (vid < VLAN_N_VID))
  2230. set_bit(vid, vsi->active_vlans);
  2231. return ret;
  2232. }
  2233. /**
  2234. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2235. * @netdev: network interface to be adjusted
  2236. * @vid: vlan id to be removed
  2237. *
  2238. * net_device_ops implementation for removing vlan ids
  2239. **/
  2240. #ifdef I40E_FCOE
  2241. int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2242. __always_unused __be16 proto, u16 vid)
  2243. #else
  2244. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2245. __always_unused __be16 proto, u16 vid)
  2246. #endif
  2247. {
  2248. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2249. struct i40e_vsi *vsi = np->vsi;
  2250. netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
  2251. /* return code is ignored as there is nothing a user
  2252. * can do about failure to remove and a log message was
  2253. * already printed from the other function
  2254. */
  2255. i40e_vsi_kill_vlan(vsi, vid);
  2256. clear_bit(vid, vsi->active_vlans);
  2257. return 0;
  2258. }
  2259. /**
  2260. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2261. * @vsi: the vsi being brought back up
  2262. **/
  2263. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2264. {
  2265. u16 vid;
  2266. if (!vsi->netdev)
  2267. return;
  2268. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  2269. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2270. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  2271. vid);
  2272. }
  2273. /**
  2274. * i40e_vsi_add_pvid - Add pvid for the VSI
  2275. * @vsi: the vsi being adjusted
  2276. * @vid: the vlan id to set as a PVID
  2277. **/
  2278. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2279. {
  2280. struct i40e_vsi_context ctxt;
  2281. i40e_status ret;
  2282. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2283. vsi->info.pvid = cpu_to_le16(vid);
  2284. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2285. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2286. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2287. ctxt.seid = vsi->seid;
  2288. ctxt.info = vsi->info;
  2289. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2290. if (ret) {
  2291. dev_info(&vsi->back->pdev->dev,
  2292. "add pvid failed, err %s aq_err %s\n",
  2293. i40e_stat_str(&vsi->back->hw, ret),
  2294. i40e_aq_str(&vsi->back->hw,
  2295. vsi->back->hw.aq.asq_last_status));
  2296. return -ENOENT;
  2297. }
  2298. return 0;
  2299. }
  2300. /**
  2301. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2302. * @vsi: the vsi being adjusted
  2303. *
  2304. * Just use the vlan_rx_register() service to put it back to normal
  2305. **/
  2306. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2307. {
  2308. i40e_vlan_stripping_disable(vsi);
  2309. vsi->info.pvid = 0;
  2310. }
  2311. /**
  2312. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2313. * @vsi: ptr to the VSI
  2314. *
  2315. * If this function returns with an error, then it's possible one or
  2316. * more of the rings is populated (while the rest are not). It is the
  2317. * callers duty to clean those orphaned rings.
  2318. *
  2319. * Return 0 on success, negative on failure
  2320. **/
  2321. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2322. {
  2323. int i, err = 0;
  2324. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2325. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2326. return err;
  2327. }
  2328. /**
  2329. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2330. * @vsi: ptr to the VSI
  2331. *
  2332. * Free VSI's transmit software resources
  2333. **/
  2334. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2335. {
  2336. int i;
  2337. if (!vsi->tx_rings)
  2338. return;
  2339. for (i = 0; i < vsi->num_queue_pairs; i++)
  2340. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2341. i40e_free_tx_resources(vsi->tx_rings[i]);
  2342. }
  2343. /**
  2344. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2345. * @vsi: ptr to the VSI
  2346. *
  2347. * If this function returns with an error, then it's possible one or
  2348. * more of the rings is populated (while the rest are not). It is the
  2349. * callers duty to clean those orphaned rings.
  2350. *
  2351. * Return 0 on success, negative on failure
  2352. **/
  2353. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2354. {
  2355. int i, err = 0;
  2356. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2357. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2358. #ifdef I40E_FCOE
  2359. i40e_fcoe_setup_ddp_resources(vsi);
  2360. #endif
  2361. return err;
  2362. }
  2363. /**
  2364. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2365. * @vsi: ptr to the VSI
  2366. *
  2367. * Free all receive software resources
  2368. **/
  2369. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2370. {
  2371. int i;
  2372. if (!vsi->rx_rings)
  2373. return;
  2374. for (i = 0; i < vsi->num_queue_pairs; i++)
  2375. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2376. i40e_free_rx_resources(vsi->rx_rings[i]);
  2377. #ifdef I40E_FCOE
  2378. i40e_fcoe_free_ddp_resources(vsi);
  2379. #endif
  2380. }
  2381. /**
  2382. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2383. * @ring: The Tx ring to configure
  2384. *
  2385. * This enables/disables XPS for a given Tx descriptor ring
  2386. * based on the TCs enabled for the VSI that ring belongs to.
  2387. **/
  2388. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2389. {
  2390. struct i40e_vsi *vsi = ring->vsi;
  2391. cpumask_var_t mask;
  2392. if (!ring->q_vector || !ring->netdev)
  2393. return;
  2394. /* Single TC mode enable XPS */
  2395. if (vsi->tc_config.numtc <= 1) {
  2396. if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
  2397. netif_set_xps_queue(ring->netdev,
  2398. &ring->q_vector->affinity_mask,
  2399. ring->queue_index);
  2400. } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
  2401. /* Disable XPS to allow selection based on TC */
  2402. bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
  2403. netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
  2404. free_cpumask_var(mask);
  2405. }
  2406. /* schedule our worker thread which will take care of
  2407. * applying the new filter changes
  2408. */
  2409. i40e_service_event_schedule(vsi->back);
  2410. }
  2411. /**
  2412. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2413. * @ring: The Tx ring to configure
  2414. *
  2415. * Configure the Tx descriptor ring in the HMC context.
  2416. **/
  2417. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2418. {
  2419. struct i40e_vsi *vsi = ring->vsi;
  2420. u16 pf_q = vsi->base_queue + ring->queue_index;
  2421. struct i40e_hw *hw = &vsi->back->hw;
  2422. struct i40e_hmc_obj_txq tx_ctx;
  2423. i40e_status err = 0;
  2424. u32 qtx_ctl = 0;
  2425. /* some ATR related tx ring init */
  2426. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2427. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2428. ring->atr_count = 0;
  2429. } else {
  2430. ring->atr_sample_rate = 0;
  2431. }
  2432. /* configure XPS */
  2433. i40e_config_xps_tx_ring(ring);
  2434. /* clear the context structure first */
  2435. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2436. tx_ctx.new_context = 1;
  2437. tx_ctx.base = (ring->dma / 128);
  2438. tx_ctx.qlen = ring->count;
  2439. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2440. I40E_FLAG_FD_ATR_ENABLED));
  2441. #ifdef I40E_FCOE
  2442. tx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
  2443. #endif
  2444. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2445. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2446. if (vsi->type != I40E_VSI_FDIR)
  2447. tx_ctx.head_wb_ena = 1;
  2448. tx_ctx.head_wb_addr = ring->dma +
  2449. (ring->count * sizeof(struct i40e_tx_desc));
  2450. /* As part of VSI creation/update, FW allocates certain
  2451. * Tx arbitration queue sets for each TC enabled for
  2452. * the VSI. The FW returns the handles to these queue
  2453. * sets as part of the response buffer to Add VSI,
  2454. * Update VSI, etc. AQ commands. It is expected that
  2455. * these queue set handles be associated with the Tx
  2456. * queues by the driver as part of the TX queue context
  2457. * initialization. This has to be done regardless of
  2458. * DCB as by default everything is mapped to TC0.
  2459. */
  2460. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2461. tx_ctx.rdylist_act = 0;
  2462. /* clear the context in the HMC */
  2463. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2464. if (err) {
  2465. dev_info(&vsi->back->pdev->dev,
  2466. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2467. ring->queue_index, pf_q, err);
  2468. return -ENOMEM;
  2469. }
  2470. /* set the context in the HMC */
  2471. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2472. if (err) {
  2473. dev_info(&vsi->back->pdev->dev,
  2474. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2475. ring->queue_index, pf_q, err);
  2476. return -ENOMEM;
  2477. }
  2478. /* Now associate this queue with this PCI function */
  2479. if (vsi->type == I40E_VSI_VMDQ2) {
  2480. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2481. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2482. I40E_QTX_CTL_VFVM_INDX_MASK;
  2483. } else {
  2484. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2485. }
  2486. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2487. I40E_QTX_CTL_PF_INDX_MASK);
  2488. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2489. i40e_flush(hw);
  2490. /* cache tail off for easier writes later */
  2491. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2492. return 0;
  2493. }
  2494. /**
  2495. * i40e_configure_rx_ring - Configure a receive ring context
  2496. * @ring: The Rx ring to configure
  2497. *
  2498. * Configure the Rx descriptor ring in the HMC context.
  2499. **/
  2500. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2501. {
  2502. struct i40e_vsi *vsi = ring->vsi;
  2503. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2504. u16 pf_q = vsi->base_queue + ring->queue_index;
  2505. struct i40e_hw *hw = &vsi->back->hw;
  2506. struct i40e_hmc_obj_rxq rx_ctx;
  2507. i40e_status err = 0;
  2508. ring->state = 0;
  2509. /* clear the context structure first */
  2510. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2511. ring->rx_buf_len = vsi->rx_buf_len;
  2512. ring->rx_hdr_len = vsi->rx_hdr_len;
  2513. rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
  2514. rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
  2515. rx_ctx.base = (ring->dma / 128);
  2516. rx_ctx.qlen = ring->count;
  2517. if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
  2518. set_ring_16byte_desc_enabled(ring);
  2519. rx_ctx.dsize = 0;
  2520. } else {
  2521. rx_ctx.dsize = 1;
  2522. }
  2523. rx_ctx.dtype = vsi->dtype;
  2524. if (vsi->dtype) {
  2525. set_ring_ps_enabled(ring);
  2526. rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
  2527. I40E_RX_SPLIT_IP |
  2528. I40E_RX_SPLIT_TCP_UDP |
  2529. I40E_RX_SPLIT_SCTP;
  2530. } else {
  2531. rx_ctx.hsplit_0 = 0;
  2532. }
  2533. rx_ctx.rxmax = min_t(u16, vsi->max_frame,
  2534. (chain_len * ring->rx_buf_len));
  2535. if (hw->revision_id == 0)
  2536. rx_ctx.lrxqthresh = 0;
  2537. else
  2538. rx_ctx.lrxqthresh = 2;
  2539. rx_ctx.crcstrip = 1;
  2540. rx_ctx.l2tsel = 1;
  2541. /* this controls whether VLAN is stripped from inner headers */
  2542. rx_ctx.showiv = 0;
  2543. #ifdef I40E_FCOE
  2544. rx_ctx.fc_ena = (vsi->type == I40E_VSI_FCOE);
  2545. #endif
  2546. /* set the prefena field to 1 because the manual says to */
  2547. rx_ctx.prefena = 1;
  2548. /* clear the context in the HMC */
  2549. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2550. if (err) {
  2551. dev_info(&vsi->back->pdev->dev,
  2552. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2553. ring->queue_index, pf_q, err);
  2554. return -ENOMEM;
  2555. }
  2556. /* set the context in the HMC */
  2557. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2558. if (err) {
  2559. dev_info(&vsi->back->pdev->dev,
  2560. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2561. ring->queue_index, pf_q, err);
  2562. return -ENOMEM;
  2563. }
  2564. /* cache tail for quicker writes, and clear the reg before use */
  2565. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2566. writel(0, ring->tail);
  2567. if (ring_is_ps_enabled(ring)) {
  2568. i40e_alloc_rx_headers(ring);
  2569. i40e_alloc_rx_buffers_ps(ring, I40E_DESC_UNUSED(ring));
  2570. } else {
  2571. i40e_alloc_rx_buffers_1buf(ring, I40E_DESC_UNUSED(ring));
  2572. }
  2573. return 0;
  2574. }
  2575. /**
  2576. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2577. * @vsi: VSI structure describing this set of rings and resources
  2578. *
  2579. * Configure the Tx VSI for operation.
  2580. **/
  2581. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2582. {
  2583. int err = 0;
  2584. u16 i;
  2585. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2586. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2587. return err;
  2588. }
  2589. /**
  2590. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2591. * @vsi: the VSI being configured
  2592. *
  2593. * Configure the Rx VSI for operation.
  2594. **/
  2595. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2596. {
  2597. int err = 0;
  2598. u16 i;
  2599. if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
  2600. vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
  2601. + ETH_FCS_LEN + VLAN_HLEN;
  2602. else
  2603. vsi->max_frame = I40E_RXBUFFER_2048;
  2604. /* figure out correct receive buffer length */
  2605. switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
  2606. I40E_FLAG_RX_PS_ENABLED)) {
  2607. case I40E_FLAG_RX_1BUF_ENABLED:
  2608. vsi->rx_hdr_len = 0;
  2609. vsi->rx_buf_len = vsi->max_frame;
  2610. vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
  2611. break;
  2612. case I40E_FLAG_RX_PS_ENABLED:
  2613. vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
  2614. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2615. vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
  2616. break;
  2617. default:
  2618. vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
  2619. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2620. vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
  2621. break;
  2622. }
  2623. #ifdef I40E_FCOE
  2624. /* setup rx buffer for FCoE */
  2625. if ((vsi->type == I40E_VSI_FCOE) &&
  2626. (vsi->back->flags & I40E_FLAG_FCOE_ENABLED)) {
  2627. vsi->rx_hdr_len = 0;
  2628. vsi->rx_buf_len = I40E_RXBUFFER_3072;
  2629. vsi->max_frame = I40E_RXBUFFER_3072;
  2630. vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
  2631. }
  2632. #endif /* I40E_FCOE */
  2633. /* round up for the chip's needs */
  2634. vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
  2635. BIT_ULL(I40E_RXQ_CTX_HBUFF_SHIFT));
  2636. vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
  2637. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2638. /* set up individual rings */
  2639. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2640. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2641. return err;
  2642. }
  2643. /**
  2644. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2645. * @vsi: ptr to the VSI
  2646. **/
  2647. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2648. {
  2649. struct i40e_ring *tx_ring, *rx_ring;
  2650. u16 qoffset, qcount;
  2651. int i, n;
  2652. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2653. /* Reset the TC information */
  2654. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2655. rx_ring = vsi->rx_rings[i];
  2656. tx_ring = vsi->tx_rings[i];
  2657. rx_ring->dcb_tc = 0;
  2658. tx_ring->dcb_tc = 0;
  2659. }
  2660. }
  2661. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2662. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2663. continue;
  2664. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2665. qcount = vsi->tc_config.tc_info[n].qcount;
  2666. for (i = qoffset; i < (qoffset + qcount); i++) {
  2667. rx_ring = vsi->rx_rings[i];
  2668. tx_ring = vsi->tx_rings[i];
  2669. rx_ring->dcb_tc = n;
  2670. tx_ring->dcb_tc = n;
  2671. }
  2672. }
  2673. }
  2674. /**
  2675. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2676. * @vsi: ptr to the VSI
  2677. **/
  2678. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2679. {
  2680. if (vsi->netdev)
  2681. i40e_set_rx_mode(vsi->netdev);
  2682. }
  2683. /**
  2684. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2685. * @vsi: Pointer to the targeted VSI
  2686. *
  2687. * This function replays the hlist on the hw where all the SB Flow Director
  2688. * filters were saved.
  2689. **/
  2690. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2691. {
  2692. struct i40e_fdir_filter *filter;
  2693. struct i40e_pf *pf = vsi->back;
  2694. struct hlist_node *node;
  2695. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2696. return;
  2697. hlist_for_each_entry_safe(filter, node,
  2698. &pf->fdir_filter_list, fdir_node) {
  2699. i40e_add_del_fdir(vsi, filter, true);
  2700. }
  2701. }
  2702. /**
  2703. * i40e_vsi_configure - Set up the VSI for action
  2704. * @vsi: the VSI being configured
  2705. **/
  2706. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2707. {
  2708. int err;
  2709. i40e_set_vsi_rx_mode(vsi);
  2710. i40e_restore_vlan(vsi);
  2711. i40e_vsi_config_dcb_rings(vsi);
  2712. err = i40e_vsi_configure_tx(vsi);
  2713. if (!err)
  2714. err = i40e_vsi_configure_rx(vsi);
  2715. return err;
  2716. }
  2717. /**
  2718. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2719. * @vsi: the VSI being configured
  2720. **/
  2721. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2722. {
  2723. struct i40e_pf *pf = vsi->back;
  2724. struct i40e_hw *hw = &pf->hw;
  2725. u16 vector;
  2726. int i, q;
  2727. u32 qp;
  2728. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2729. * and PFINT_LNKLSTn registers, e.g.:
  2730. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2731. */
  2732. qp = vsi->base_queue;
  2733. vector = vsi->base_vector;
  2734. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2735. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  2736. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2737. q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
  2738. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2739. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2740. q_vector->rx.itr);
  2741. q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
  2742. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2743. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2744. q_vector->tx.itr);
  2745. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  2746. INTRL_USEC_TO_REG(vsi->int_rate_limit));
  2747. /* Linked list for the queuepairs assigned to this vector */
  2748. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2749. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2750. u32 val;
  2751. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2752. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2753. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2754. (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  2755. (I40E_QUEUE_TYPE_TX
  2756. << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2757. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2758. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2759. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2760. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2761. ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
  2762. (I40E_QUEUE_TYPE_RX
  2763. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2764. /* Terminate the linked list */
  2765. if (q == (q_vector->num_ringpairs - 1))
  2766. val |= (I40E_QUEUE_END_OF_LIST
  2767. << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2768. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2769. qp++;
  2770. }
  2771. }
  2772. i40e_flush(hw);
  2773. }
  2774. /**
  2775. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2776. * @hw: ptr to the hardware info
  2777. **/
  2778. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  2779. {
  2780. struct i40e_hw *hw = &pf->hw;
  2781. u32 val;
  2782. /* clear things first */
  2783. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  2784. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  2785. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  2786. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  2787. I40E_PFINT_ICR0_ENA_GRST_MASK |
  2788. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  2789. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  2790. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  2791. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  2792. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2793. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  2794. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  2795. if (pf->flags & I40E_FLAG_PTP)
  2796. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  2797. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  2798. /* SW_ITR_IDX = 0, but don't change INTENA */
  2799. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  2800. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  2801. /* OTHER_ITR_IDX = 0 */
  2802. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  2803. }
  2804. /**
  2805. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  2806. * @vsi: the VSI being configured
  2807. **/
  2808. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  2809. {
  2810. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  2811. struct i40e_pf *pf = vsi->back;
  2812. struct i40e_hw *hw = &pf->hw;
  2813. u32 val;
  2814. /* set the ITR configuration */
  2815. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2816. q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
  2817. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2818. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  2819. q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
  2820. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2821. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  2822. i40e_enable_misc_int_causes(pf);
  2823. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  2824. wr32(hw, I40E_PFINT_LNKLST0, 0);
  2825. /* Associate the queue pair to the vector and enable the queue int */
  2826. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2827. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2828. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2829. wr32(hw, I40E_QINT_RQCTL(0), val);
  2830. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2831. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2832. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2833. wr32(hw, I40E_QINT_TQCTL(0), val);
  2834. i40e_flush(hw);
  2835. }
  2836. /**
  2837. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  2838. * @pf: board private structure
  2839. **/
  2840. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  2841. {
  2842. struct i40e_hw *hw = &pf->hw;
  2843. wr32(hw, I40E_PFINT_DYN_CTL0,
  2844. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2845. i40e_flush(hw);
  2846. }
  2847. /**
  2848. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  2849. * @pf: board private structure
  2850. **/
  2851. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  2852. {
  2853. struct i40e_hw *hw = &pf->hw;
  2854. u32 val;
  2855. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  2856. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  2857. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  2858. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  2859. i40e_flush(hw);
  2860. }
  2861. /**
  2862. * i40e_irq_dynamic_disable - Disable default interrupt generation settings
  2863. * @vsi: pointer to a vsi
  2864. * @vector: disable a particular Hw Interrupt vector
  2865. **/
  2866. void i40e_irq_dynamic_disable(struct i40e_vsi *vsi, int vector)
  2867. {
  2868. struct i40e_pf *pf = vsi->back;
  2869. struct i40e_hw *hw = &pf->hw;
  2870. u32 val;
  2871. val = I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT;
  2872. wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
  2873. i40e_flush(hw);
  2874. }
  2875. /**
  2876. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  2877. * @irq: interrupt number
  2878. * @data: pointer to a q_vector
  2879. **/
  2880. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  2881. {
  2882. struct i40e_q_vector *q_vector = data;
  2883. if (!q_vector->tx.ring && !q_vector->rx.ring)
  2884. return IRQ_HANDLED;
  2885. napi_schedule_irqoff(&q_vector->napi);
  2886. return IRQ_HANDLED;
  2887. }
  2888. /**
  2889. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  2890. * @vsi: the VSI being configured
  2891. * @basename: name for the vector
  2892. *
  2893. * Allocates MSI-X vectors and requests interrupts from the kernel.
  2894. **/
  2895. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  2896. {
  2897. int q_vectors = vsi->num_q_vectors;
  2898. struct i40e_pf *pf = vsi->back;
  2899. int base = vsi->base_vector;
  2900. int rx_int_idx = 0;
  2901. int tx_int_idx = 0;
  2902. int vector, err;
  2903. for (vector = 0; vector < q_vectors; vector++) {
  2904. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  2905. if (q_vector->tx.ring && q_vector->rx.ring) {
  2906. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2907. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  2908. tx_int_idx++;
  2909. } else if (q_vector->rx.ring) {
  2910. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2911. "%s-%s-%d", basename, "rx", rx_int_idx++);
  2912. } else if (q_vector->tx.ring) {
  2913. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2914. "%s-%s-%d", basename, "tx", tx_int_idx++);
  2915. } else {
  2916. /* skip this unused q_vector */
  2917. continue;
  2918. }
  2919. err = request_irq(pf->msix_entries[base + vector].vector,
  2920. vsi->irq_handler,
  2921. 0,
  2922. q_vector->name,
  2923. q_vector);
  2924. if (err) {
  2925. dev_info(&pf->pdev->dev,
  2926. "MSIX request_irq failed, error: %d\n", err);
  2927. goto free_queue_irqs;
  2928. }
  2929. /* assign the mask for this irq */
  2930. irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
  2931. &q_vector->affinity_mask);
  2932. }
  2933. vsi->irqs_ready = true;
  2934. return 0;
  2935. free_queue_irqs:
  2936. while (vector) {
  2937. vector--;
  2938. irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
  2939. NULL);
  2940. free_irq(pf->msix_entries[base + vector].vector,
  2941. &(vsi->q_vectors[vector]));
  2942. }
  2943. return err;
  2944. }
  2945. /**
  2946. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  2947. * @vsi: the VSI being un-configured
  2948. **/
  2949. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  2950. {
  2951. struct i40e_pf *pf = vsi->back;
  2952. struct i40e_hw *hw = &pf->hw;
  2953. int base = vsi->base_vector;
  2954. int i;
  2955. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2956. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
  2957. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
  2958. }
  2959. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2960. for (i = vsi->base_vector;
  2961. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  2962. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  2963. i40e_flush(hw);
  2964. for (i = 0; i < vsi->num_q_vectors; i++)
  2965. synchronize_irq(pf->msix_entries[i + base].vector);
  2966. } else {
  2967. /* Legacy and MSI mode - this stops all interrupt handling */
  2968. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  2969. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  2970. i40e_flush(hw);
  2971. synchronize_irq(pf->pdev->irq);
  2972. }
  2973. }
  2974. /**
  2975. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  2976. * @vsi: the VSI being configured
  2977. **/
  2978. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  2979. {
  2980. struct i40e_pf *pf = vsi->back;
  2981. int i;
  2982. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2983. for (i = 0; i < vsi->num_q_vectors; i++)
  2984. i40e_irq_dynamic_enable(vsi, i);
  2985. } else {
  2986. i40e_irq_dynamic_enable_icr0(pf);
  2987. }
  2988. i40e_flush(&pf->hw);
  2989. return 0;
  2990. }
  2991. /**
  2992. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  2993. * @pf: board private structure
  2994. **/
  2995. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  2996. {
  2997. /* Disable ICR 0 */
  2998. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  2999. i40e_flush(&pf->hw);
  3000. }
  3001. /**
  3002. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3003. * @irq: interrupt number
  3004. * @data: pointer to a q_vector
  3005. *
  3006. * This is the handler used for all MSI/Legacy interrupts, and deals
  3007. * with both queue and non-queue interrupts. This is also used in
  3008. * MSIX mode to handle the non-queue interrupts.
  3009. **/
  3010. static irqreturn_t i40e_intr(int irq, void *data)
  3011. {
  3012. struct i40e_pf *pf = (struct i40e_pf *)data;
  3013. struct i40e_hw *hw = &pf->hw;
  3014. irqreturn_t ret = IRQ_NONE;
  3015. u32 icr0, icr0_remaining;
  3016. u32 val, ena_mask;
  3017. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3018. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3019. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3020. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3021. goto enable_intr;
  3022. /* if interrupt but no bits showing, must be SWINT */
  3023. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3024. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3025. pf->sw_int_count++;
  3026. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3027. (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3028. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3029. icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3030. dev_info(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3031. }
  3032. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3033. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3034. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3035. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3036. /* temporarily disable queue cause for NAPI processing */
  3037. u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
  3038. qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  3039. wr32(hw, I40E_QINT_RQCTL(0), qval);
  3040. qval = rd32(hw, I40E_QINT_TQCTL(0));
  3041. qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  3042. wr32(hw, I40E_QINT_TQCTL(0), qval);
  3043. if (!test_bit(__I40E_DOWN, &pf->state))
  3044. napi_schedule_irqoff(&q_vector->napi);
  3045. }
  3046. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3047. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3048. set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  3049. }
  3050. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3051. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3052. set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  3053. }
  3054. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3055. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3056. set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  3057. }
  3058. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3059. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3060. set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  3061. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3062. val = rd32(hw, I40E_GLGEN_RSTAT);
  3063. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3064. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3065. if (val == I40E_RESET_CORER) {
  3066. pf->corer_count++;
  3067. } else if (val == I40E_RESET_GLOBR) {
  3068. pf->globr_count++;
  3069. } else if (val == I40E_RESET_EMPR) {
  3070. pf->empr_count++;
  3071. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
  3072. }
  3073. }
  3074. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3075. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3076. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3077. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3078. rd32(hw, I40E_PFHMC_ERRORINFO),
  3079. rd32(hw, I40E_PFHMC_ERRORDATA));
  3080. }
  3081. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3082. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3083. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3084. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3085. i40e_ptp_tx_hwtstamp(pf);
  3086. }
  3087. }
  3088. /* If a critical error is pending we have no choice but to reset the
  3089. * device.
  3090. * Report and mask out any remaining unexpected interrupts.
  3091. */
  3092. icr0_remaining = icr0 & ena_mask;
  3093. if (icr0_remaining) {
  3094. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3095. icr0_remaining);
  3096. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3097. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3098. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3099. dev_info(&pf->pdev->dev, "device will be reset\n");
  3100. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  3101. i40e_service_event_schedule(pf);
  3102. }
  3103. ena_mask &= ~icr0_remaining;
  3104. }
  3105. ret = IRQ_HANDLED;
  3106. enable_intr:
  3107. /* re-enable interrupt causes */
  3108. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3109. if (!test_bit(__I40E_DOWN, &pf->state)) {
  3110. i40e_service_event_schedule(pf);
  3111. i40e_irq_dynamic_enable_icr0(pf);
  3112. }
  3113. return ret;
  3114. }
  3115. /**
  3116. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3117. * @tx_ring: tx ring to clean
  3118. * @budget: how many cleans we're allowed
  3119. *
  3120. * Returns true if there's any budget left (e.g. the clean is finished)
  3121. **/
  3122. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3123. {
  3124. struct i40e_vsi *vsi = tx_ring->vsi;
  3125. u16 i = tx_ring->next_to_clean;
  3126. struct i40e_tx_buffer *tx_buf;
  3127. struct i40e_tx_desc *tx_desc;
  3128. tx_buf = &tx_ring->tx_bi[i];
  3129. tx_desc = I40E_TX_DESC(tx_ring, i);
  3130. i -= tx_ring->count;
  3131. do {
  3132. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3133. /* if next_to_watch is not set then there is no work pending */
  3134. if (!eop_desc)
  3135. break;
  3136. /* prevent any other reads prior to eop_desc */
  3137. read_barrier_depends();
  3138. /* if the descriptor isn't done, no work yet to do */
  3139. if (!(eop_desc->cmd_type_offset_bsz &
  3140. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3141. break;
  3142. /* clear next_to_watch to prevent false hangs */
  3143. tx_buf->next_to_watch = NULL;
  3144. tx_desc->buffer_addr = 0;
  3145. tx_desc->cmd_type_offset_bsz = 0;
  3146. /* move past filter desc */
  3147. tx_buf++;
  3148. tx_desc++;
  3149. i++;
  3150. if (unlikely(!i)) {
  3151. i -= tx_ring->count;
  3152. tx_buf = tx_ring->tx_bi;
  3153. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3154. }
  3155. /* unmap skb header data */
  3156. dma_unmap_single(tx_ring->dev,
  3157. dma_unmap_addr(tx_buf, dma),
  3158. dma_unmap_len(tx_buf, len),
  3159. DMA_TO_DEVICE);
  3160. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3161. kfree(tx_buf->raw_buf);
  3162. tx_buf->raw_buf = NULL;
  3163. tx_buf->tx_flags = 0;
  3164. tx_buf->next_to_watch = NULL;
  3165. dma_unmap_len_set(tx_buf, len, 0);
  3166. tx_desc->buffer_addr = 0;
  3167. tx_desc->cmd_type_offset_bsz = 0;
  3168. /* move us past the eop_desc for start of next FD desc */
  3169. tx_buf++;
  3170. tx_desc++;
  3171. i++;
  3172. if (unlikely(!i)) {
  3173. i -= tx_ring->count;
  3174. tx_buf = tx_ring->tx_bi;
  3175. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3176. }
  3177. /* update budget accounting */
  3178. budget--;
  3179. } while (likely(budget));
  3180. i += tx_ring->count;
  3181. tx_ring->next_to_clean = i;
  3182. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3183. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3184. return budget > 0;
  3185. }
  3186. /**
  3187. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3188. * @irq: interrupt number
  3189. * @data: pointer to a q_vector
  3190. **/
  3191. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3192. {
  3193. struct i40e_q_vector *q_vector = data;
  3194. struct i40e_vsi *vsi;
  3195. if (!q_vector->tx.ring)
  3196. return IRQ_HANDLED;
  3197. vsi = q_vector->tx.ring->vsi;
  3198. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3199. return IRQ_HANDLED;
  3200. }
  3201. /**
  3202. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3203. * @vsi: the VSI being configured
  3204. * @v_idx: vector index
  3205. * @qp_idx: queue pair index
  3206. **/
  3207. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3208. {
  3209. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3210. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3211. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3212. tx_ring->q_vector = q_vector;
  3213. tx_ring->next = q_vector->tx.ring;
  3214. q_vector->tx.ring = tx_ring;
  3215. q_vector->tx.count++;
  3216. rx_ring->q_vector = q_vector;
  3217. rx_ring->next = q_vector->rx.ring;
  3218. q_vector->rx.ring = rx_ring;
  3219. q_vector->rx.count++;
  3220. }
  3221. /**
  3222. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3223. * @vsi: the VSI being configured
  3224. *
  3225. * This function maps descriptor rings to the queue-specific vectors
  3226. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3227. * one vector per queue pair, but on a constrained vector budget, we
  3228. * group the queue pairs as "efficiently" as possible.
  3229. **/
  3230. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3231. {
  3232. int qp_remaining = vsi->num_queue_pairs;
  3233. int q_vectors = vsi->num_q_vectors;
  3234. int num_ringpairs;
  3235. int v_start = 0;
  3236. int qp_idx = 0;
  3237. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3238. * group them so there are multiple queues per vector.
  3239. * It is also important to go through all the vectors available to be
  3240. * sure that if we don't use all the vectors, that the remaining vectors
  3241. * are cleared. This is especially important when decreasing the
  3242. * number of queues in use.
  3243. */
  3244. for (; v_start < q_vectors; v_start++) {
  3245. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3246. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3247. q_vector->num_ringpairs = num_ringpairs;
  3248. q_vector->rx.count = 0;
  3249. q_vector->tx.count = 0;
  3250. q_vector->rx.ring = NULL;
  3251. q_vector->tx.ring = NULL;
  3252. while (num_ringpairs--) {
  3253. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3254. qp_idx++;
  3255. qp_remaining--;
  3256. }
  3257. }
  3258. }
  3259. /**
  3260. * i40e_vsi_request_irq - Request IRQ from the OS
  3261. * @vsi: the VSI being configured
  3262. * @basename: name for the vector
  3263. **/
  3264. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3265. {
  3266. struct i40e_pf *pf = vsi->back;
  3267. int err;
  3268. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3269. err = i40e_vsi_request_irq_msix(vsi, basename);
  3270. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3271. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3272. pf->int_name, pf);
  3273. else
  3274. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3275. pf->int_name, pf);
  3276. if (err)
  3277. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3278. return err;
  3279. }
  3280. #ifdef CONFIG_NET_POLL_CONTROLLER
  3281. /**
  3282. * i40e_netpoll - A Polling 'interrupt'handler
  3283. * @netdev: network interface device structure
  3284. *
  3285. * This is used by netconsole to send skbs without having to re-enable
  3286. * interrupts. It's not called while the normal interrupt routine is executing.
  3287. **/
  3288. #ifdef I40E_FCOE
  3289. void i40e_netpoll(struct net_device *netdev)
  3290. #else
  3291. static void i40e_netpoll(struct net_device *netdev)
  3292. #endif
  3293. {
  3294. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3295. struct i40e_vsi *vsi = np->vsi;
  3296. struct i40e_pf *pf = vsi->back;
  3297. int i;
  3298. /* if interface is down do nothing */
  3299. if (test_bit(__I40E_DOWN, &vsi->state))
  3300. return;
  3301. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3302. for (i = 0; i < vsi->num_q_vectors; i++)
  3303. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3304. } else {
  3305. i40e_intr(pf->pdev->irq, netdev);
  3306. }
  3307. }
  3308. #endif
  3309. /**
  3310. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3311. * @pf: the PF being configured
  3312. * @pf_q: the PF queue
  3313. * @enable: enable or disable state of the queue
  3314. *
  3315. * This routine will wait for the given Tx queue of the PF to reach the
  3316. * enabled or disabled state.
  3317. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3318. * multiple retries; else will return 0 in case of success.
  3319. **/
  3320. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3321. {
  3322. int i;
  3323. u32 tx_reg;
  3324. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3325. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3326. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3327. break;
  3328. usleep_range(10, 20);
  3329. }
  3330. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3331. return -ETIMEDOUT;
  3332. return 0;
  3333. }
  3334. /**
  3335. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3336. * @vsi: the VSI being configured
  3337. * @enable: start or stop the rings
  3338. **/
  3339. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3340. {
  3341. struct i40e_pf *pf = vsi->back;
  3342. struct i40e_hw *hw = &pf->hw;
  3343. int i, j, pf_q, ret = 0;
  3344. u32 tx_reg;
  3345. pf_q = vsi->base_queue;
  3346. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3347. /* warn the TX unit of coming changes */
  3348. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3349. if (!enable)
  3350. usleep_range(10, 20);
  3351. for (j = 0; j < 50; j++) {
  3352. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3353. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3354. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3355. break;
  3356. usleep_range(1000, 2000);
  3357. }
  3358. /* Skip if the queue is already in the requested state */
  3359. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3360. continue;
  3361. /* turn on/off the queue */
  3362. if (enable) {
  3363. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3364. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3365. } else {
  3366. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3367. }
  3368. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3369. /* No waiting for the Tx queue to disable */
  3370. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3371. continue;
  3372. /* wait for the change to finish */
  3373. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3374. if (ret) {
  3375. dev_info(&pf->pdev->dev,
  3376. "VSI seid %d Tx ring %d %sable timeout\n",
  3377. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3378. break;
  3379. }
  3380. }
  3381. if (hw->revision_id == 0)
  3382. mdelay(50);
  3383. return ret;
  3384. }
  3385. /**
  3386. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3387. * @pf: the PF being configured
  3388. * @pf_q: the PF queue
  3389. * @enable: enable or disable state of the queue
  3390. *
  3391. * This routine will wait for the given Rx queue of the PF to reach the
  3392. * enabled or disabled state.
  3393. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3394. * multiple retries; else will return 0 in case of success.
  3395. **/
  3396. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3397. {
  3398. int i;
  3399. u32 rx_reg;
  3400. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3401. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3402. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3403. break;
  3404. usleep_range(10, 20);
  3405. }
  3406. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3407. return -ETIMEDOUT;
  3408. return 0;
  3409. }
  3410. /**
  3411. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3412. * @vsi: the VSI being configured
  3413. * @enable: start or stop the rings
  3414. **/
  3415. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3416. {
  3417. struct i40e_pf *pf = vsi->back;
  3418. struct i40e_hw *hw = &pf->hw;
  3419. int i, j, pf_q, ret = 0;
  3420. u32 rx_reg;
  3421. pf_q = vsi->base_queue;
  3422. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3423. for (j = 0; j < 50; j++) {
  3424. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3425. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3426. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3427. break;
  3428. usleep_range(1000, 2000);
  3429. }
  3430. /* Skip if the queue is already in the requested state */
  3431. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3432. continue;
  3433. /* turn on/off the queue */
  3434. if (enable)
  3435. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3436. else
  3437. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3438. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3439. /* wait for the change to finish */
  3440. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3441. if (ret) {
  3442. dev_info(&pf->pdev->dev,
  3443. "VSI seid %d Rx ring %d %sable timeout\n",
  3444. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3445. break;
  3446. }
  3447. }
  3448. return ret;
  3449. }
  3450. /**
  3451. * i40e_vsi_control_rings - Start or stop a VSI's rings
  3452. * @vsi: the VSI being configured
  3453. * @enable: start or stop the rings
  3454. **/
  3455. int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
  3456. {
  3457. int ret = 0;
  3458. /* do rx first for enable and last for disable */
  3459. if (request) {
  3460. ret = i40e_vsi_control_rx(vsi, request);
  3461. if (ret)
  3462. return ret;
  3463. ret = i40e_vsi_control_tx(vsi, request);
  3464. } else {
  3465. /* Ignore return value, we need to shutdown whatever we can */
  3466. i40e_vsi_control_tx(vsi, request);
  3467. i40e_vsi_control_rx(vsi, request);
  3468. }
  3469. return ret;
  3470. }
  3471. /**
  3472. * i40e_vsi_free_irq - Free the irq association with the OS
  3473. * @vsi: the VSI being configured
  3474. **/
  3475. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3476. {
  3477. struct i40e_pf *pf = vsi->back;
  3478. struct i40e_hw *hw = &pf->hw;
  3479. int base = vsi->base_vector;
  3480. u32 val, qp;
  3481. int i;
  3482. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3483. if (!vsi->q_vectors)
  3484. return;
  3485. if (!vsi->irqs_ready)
  3486. return;
  3487. vsi->irqs_ready = false;
  3488. for (i = 0; i < vsi->num_q_vectors; i++) {
  3489. u16 vector = i + base;
  3490. /* free only the irqs that were actually requested */
  3491. if (!vsi->q_vectors[i] ||
  3492. !vsi->q_vectors[i]->num_ringpairs)
  3493. continue;
  3494. /* clear the affinity_mask in the IRQ descriptor */
  3495. irq_set_affinity_hint(pf->msix_entries[vector].vector,
  3496. NULL);
  3497. free_irq(pf->msix_entries[vector].vector,
  3498. vsi->q_vectors[i]);
  3499. /* Tear down the interrupt queue link list
  3500. *
  3501. * We know that they come in pairs and always
  3502. * the Rx first, then the Tx. To clear the
  3503. * link list, stick the EOL value into the
  3504. * next_q field of the registers.
  3505. */
  3506. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3507. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3508. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3509. val |= I40E_QUEUE_END_OF_LIST
  3510. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3511. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3512. while (qp != I40E_QUEUE_END_OF_LIST) {
  3513. u32 next;
  3514. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3515. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3516. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3517. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3518. I40E_QINT_RQCTL_INTEVENT_MASK);
  3519. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3520. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3521. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3522. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3523. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3524. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3525. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3526. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3527. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3528. I40E_QINT_TQCTL_INTEVENT_MASK);
  3529. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3530. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3531. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3532. qp = next;
  3533. }
  3534. }
  3535. } else {
  3536. free_irq(pf->pdev->irq, pf);
  3537. val = rd32(hw, I40E_PFINT_LNKLST0);
  3538. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3539. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3540. val |= I40E_QUEUE_END_OF_LIST
  3541. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  3542. wr32(hw, I40E_PFINT_LNKLST0, val);
  3543. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3544. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3545. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3546. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3547. I40E_QINT_RQCTL_INTEVENT_MASK);
  3548. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3549. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3550. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3551. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3552. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3553. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3554. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3555. I40E_QINT_TQCTL_INTEVENT_MASK);
  3556. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3557. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3558. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3559. }
  3560. }
  3561. /**
  3562. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  3563. * @vsi: the VSI being configured
  3564. * @v_idx: Index of vector to be freed
  3565. *
  3566. * This function frees the memory allocated to the q_vector. In addition if
  3567. * NAPI is enabled it will delete any references to the NAPI struct prior
  3568. * to freeing the q_vector.
  3569. **/
  3570. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  3571. {
  3572. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3573. struct i40e_ring *ring;
  3574. if (!q_vector)
  3575. return;
  3576. /* disassociate q_vector from rings */
  3577. i40e_for_each_ring(ring, q_vector->tx)
  3578. ring->q_vector = NULL;
  3579. i40e_for_each_ring(ring, q_vector->rx)
  3580. ring->q_vector = NULL;
  3581. /* only VSI w/ an associated netdev is set up w/ NAPI */
  3582. if (vsi->netdev)
  3583. netif_napi_del(&q_vector->napi);
  3584. vsi->q_vectors[v_idx] = NULL;
  3585. kfree_rcu(q_vector, rcu);
  3586. }
  3587. /**
  3588. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  3589. * @vsi: the VSI being un-configured
  3590. *
  3591. * This frees the memory allocated to the q_vectors and
  3592. * deletes references to the NAPI struct.
  3593. **/
  3594. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  3595. {
  3596. int v_idx;
  3597. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  3598. i40e_free_q_vector(vsi, v_idx);
  3599. }
  3600. /**
  3601. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  3602. * @pf: board private structure
  3603. **/
  3604. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  3605. {
  3606. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  3607. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3608. pci_disable_msix(pf->pdev);
  3609. kfree(pf->msix_entries);
  3610. pf->msix_entries = NULL;
  3611. kfree(pf->irq_pile);
  3612. pf->irq_pile = NULL;
  3613. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  3614. pci_disable_msi(pf->pdev);
  3615. }
  3616. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  3617. }
  3618. /**
  3619. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  3620. * @pf: board private structure
  3621. *
  3622. * We go through and clear interrupt specific resources and reset the structure
  3623. * to pre-load conditions
  3624. **/
  3625. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  3626. {
  3627. int i;
  3628. i40e_stop_misc_vector(pf);
  3629. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3630. synchronize_irq(pf->msix_entries[0].vector);
  3631. free_irq(pf->msix_entries[0].vector, pf);
  3632. }
  3633. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  3634. for (i = 0; i < pf->num_alloc_vsi; i++)
  3635. if (pf->vsi[i])
  3636. i40e_vsi_free_q_vectors(pf->vsi[i]);
  3637. i40e_reset_interrupt_capability(pf);
  3638. }
  3639. /**
  3640. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3641. * @vsi: the VSI being configured
  3642. **/
  3643. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  3644. {
  3645. int q_idx;
  3646. if (!vsi->netdev)
  3647. return;
  3648. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3649. napi_enable(&vsi->q_vectors[q_idx]->napi);
  3650. }
  3651. /**
  3652. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  3653. * @vsi: the VSI being configured
  3654. **/
  3655. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  3656. {
  3657. int q_idx;
  3658. if (!vsi->netdev)
  3659. return;
  3660. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3661. napi_disable(&vsi->q_vectors[q_idx]->napi);
  3662. }
  3663. /**
  3664. * i40e_vsi_close - Shut down a VSI
  3665. * @vsi: the vsi to be quelled
  3666. **/
  3667. static void i40e_vsi_close(struct i40e_vsi *vsi)
  3668. {
  3669. if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
  3670. i40e_down(vsi);
  3671. i40e_vsi_free_irq(vsi);
  3672. i40e_vsi_free_tx_resources(vsi);
  3673. i40e_vsi_free_rx_resources(vsi);
  3674. vsi->current_netdev_flags = 0;
  3675. }
  3676. /**
  3677. * i40e_quiesce_vsi - Pause a given VSI
  3678. * @vsi: the VSI being paused
  3679. **/
  3680. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  3681. {
  3682. if (test_bit(__I40E_DOWN, &vsi->state))
  3683. return;
  3684. /* No need to disable FCoE VSI when Tx suspended */
  3685. if ((test_bit(__I40E_PORT_TX_SUSPENDED, &vsi->back->state)) &&
  3686. vsi->type == I40E_VSI_FCOE) {
  3687. dev_dbg(&vsi->back->pdev->dev,
  3688. "VSI seid %d skipping FCoE VSI disable\n", vsi->seid);
  3689. return;
  3690. }
  3691. set_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3692. if (vsi->netdev && netif_running(vsi->netdev))
  3693. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  3694. else
  3695. i40e_vsi_close(vsi);
  3696. }
  3697. /**
  3698. * i40e_unquiesce_vsi - Resume a given VSI
  3699. * @vsi: the VSI being resumed
  3700. **/
  3701. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  3702. {
  3703. if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
  3704. return;
  3705. clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3706. if (vsi->netdev && netif_running(vsi->netdev))
  3707. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  3708. else
  3709. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  3710. }
  3711. /**
  3712. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  3713. * @pf: the PF
  3714. **/
  3715. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  3716. {
  3717. int v;
  3718. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3719. if (pf->vsi[v])
  3720. i40e_quiesce_vsi(pf->vsi[v]);
  3721. }
  3722. }
  3723. /**
  3724. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  3725. * @pf: the PF
  3726. **/
  3727. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  3728. {
  3729. int v;
  3730. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3731. if (pf->vsi[v])
  3732. i40e_unquiesce_vsi(pf->vsi[v]);
  3733. }
  3734. }
  3735. #ifdef CONFIG_I40E_DCB
  3736. /**
  3737. * i40e_vsi_wait_txq_disabled - Wait for VSI's queues to be disabled
  3738. * @vsi: the VSI being configured
  3739. *
  3740. * This function waits for the given VSI's Tx queues to be disabled.
  3741. **/
  3742. static int i40e_vsi_wait_txq_disabled(struct i40e_vsi *vsi)
  3743. {
  3744. struct i40e_pf *pf = vsi->back;
  3745. int i, pf_q, ret;
  3746. pf_q = vsi->base_queue;
  3747. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3748. /* Check and wait for the disable status of the queue */
  3749. ret = i40e_pf_txq_wait(pf, pf_q, false);
  3750. if (ret) {
  3751. dev_info(&pf->pdev->dev,
  3752. "VSI seid %d Tx ring %d disable timeout\n",
  3753. vsi->seid, pf_q);
  3754. return ret;
  3755. }
  3756. }
  3757. return 0;
  3758. }
  3759. /**
  3760. * i40e_pf_wait_txq_disabled - Wait for all queues of PF VSIs to be disabled
  3761. * @pf: the PF
  3762. *
  3763. * This function waits for the Tx queues to be in disabled state for all the
  3764. * VSIs that are managed by this PF.
  3765. **/
  3766. static int i40e_pf_wait_txq_disabled(struct i40e_pf *pf)
  3767. {
  3768. int v, ret = 0;
  3769. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3770. /* No need to wait for FCoE VSI queues */
  3771. if (pf->vsi[v] && pf->vsi[v]->type != I40E_VSI_FCOE) {
  3772. ret = i40e_vsi_wait_txq_disabled(pf->vsi[v]);
  3773. if (ret)
  3774. break;
  3775. }
  3776. }
  3777. return ret;
  3778. }
  3779. #endif
  3780. /**
  3781. * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
  3782. * @q_idx: TX queue number
  3783. * @vsi: Pointer to VSI struct
  3784. *
  3785. * This function checks specified queue for given VSI. Detects hung condition.
  3786. * Sets hung bit since it is two step process. Before next run of service task
  3787. * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
  3788. * hung condition remain unchanged and during subsequent run, this function
  3789. * issues SW interrupt to recover from hung condition.
  3790. **/
  3791. static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
  3792. {
  3793. struct i40e_ring *tx_ring = NULL;
  3794. struct i40e_pf *pf;
  3795. u32 head, val, tx_pending;
  3796. int i;
  3797. pf = vsi->back;
  3798. /* now that we have an index, find the tx_ring struct */
  3799. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3800. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  3801. if (q_idx == vsi->tx_rings[i]->queue_index) {
  3802. tx_ring = vsi->tx_rings[i];
  3803. break;
  3804. }
  3805. }
  3806. }
  3807. if (!tx_ring)
  3808. return;
  3809. /* Read interrupt register */
  3810. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3811. val = rd32(&pf->hw,
  3812. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  3813. tx_ring->vsi->base_vector - 1));
  3814. else
  3815. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  3816. /* Bail out if interrupts are disabled because napi_poll
  3817. * execution in-progress or will get scheduled soon.
  3818. * napi_poll cleans TX and RX queues and updates 'next_to_clean'.
  3819. */
  3820. if (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))
  3821. return;
  3822. head = i40e_get_head(tx_ring);
  3823. tx_pending = i40e_get_tx_pending(tx_ring);
  3824. /* HW is done executing descriptors, updated HEAD write back,
  3825. * but SW hasn't processed those descriptors. If interrupt is
  3826. * not generated from this point ON, it could result into
  3827. * dev_watchdog detecting timeout on those netdev_queue,
  3828. * hence proactively trigger SW interrupt.
  3829. */
  3830. if (tx_pending) {
  3831. /* NAPI Poll didn't run and clear since it was set */
  3832. if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
  3833. &tx_ring->q_vector->hung_detected)) {
  3834. netdev_info(vsi->netdev, "VSI_seid %d, Hung TX queue %d, tx_pending: %d, NTC:0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x\n",
  3835. vsi->seid, q_idx, tx_pending,
  3836. tx_ring->next_to_clean, head,
  3837. tx_ring->next_to_use,
  3838. readl(tx_ring->tail));
  3839. netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
  3840. vsi->seid, q_idx, val);
  3841. i40e_force_wb(vsi, tx_ring->q_vector);
  3842. } else {
  3843. /* First Chance - detected possible hung */
  3844. set_bit(I40E_Q_VECTOR_HUNG_DETECT,
  3845. &tx_ring->q_vector->hung_detected);
  3846. }
  3847. }
  3848. }
  3849. /**
  3850. * i40e_detect_recover_hung - Function to detect and recover hung_queues
  3851. * @pf: pointer to PF struct
  3852. *
  3853. * LAN VSI has netdev and netdev has TX queues. This function is to check
  3854. * each of those TX queues if they are hung, trigger recovery by issuing
  3855. * SW interrupt.
  3856. **/
  3857. static void i40e_detect_recover_hung(struct i40e_pf *pf)
  3858. {
  3859. struct net_device *netdev;
  3860. struct i40e_vsi *vsi;
  3861. int i;
  3862. /* Only for LAN VSI */
  3863. vsi = pf->vsi[pf->lan_vsi];
  3864. if (!vsi)
  3865. return;
  3866. /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
  3867. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  3868. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  3869. return;
  3870. /* Make sure type is MAIN VSI */
  3871. if (vsi->type != I40E_VSI_MAIN)
  3872. return;
  3873. netdev = vsi->netdev;
  3874. if (!netdev)
  3875. return;
  3876. /* Bail out if netif_carrier is not OK */
  3877. if (!netif_carrier_ok(netdev))
  3878. return;
  3879. /* Go thru' TX queues for netdev */
  3880. for (i = 0; i < netdev->num_tx_queues; i++) {
  3881. struct netdev_queue *q;
  3882. q = netdev_get_tx_queue(netdev, i);
  3883. if (q)
  3884. i40e_detect_recover_hung_queue(i, vsi);
  3885. }
  3886. }
  3887. /**
  3888. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  3889. * @pf: pointer to PF
  3890. *
  3891. * Get TC map for ISCSI PF type that will include iSCSI TC
  3892. * and LAN TC.
  3893. **/
  3894. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  3895. {
  3896. struct i40e_dcb_app_priority_table app;
  3897. struct i40e_hw *hw = &pf->hw;
  3898. u8 enabled_tc = 1; /* TC0 is always enabled */
  3899. u8 tc, i;
  3900. /* Get the iSCSI APP TLV */
  3901. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  3902. for (i = 0; i < dcbcfg->numapps; i++) {
  3903. app = dcbcfg->app[i];
  3904. if (app.selector == I40E_APP_SEL_TCPIP &&
  3905. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  3906. tc = dcbcfg->etscfg.prioritytable[app.priority];
  3907. enabled_tc |= BIT(tc);
  3908. break;
  3909. }
  3910. }
  3911. return enabled_tc;
  3912. }
  3913. /**
  3914. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  3915. * @dcbcfg: the corresponding DCBx configuration structure
  3916. *
  3917. * Return the number of TCs from given DCBx configuration
  3918. **/
  3919. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  3920. {
  3921. u8 num_tc = 0;
  3922. int i;
  3923. /* Scan the ETS Config Priority Table to find
  3924. * traffic class enabled for a given priority
  3925. * and use the traffic class index to get the
  3926. * number of traffic classes enabled
  3927. */
  3928. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  3929. if (dcbcfg->etscfg.prioritytable[i] > num_tc)
  3930. num_tc = dcbcfg->etscfg.prioritytable[i];
  3931. }
  3932. /* Traffic class index starts from zero so
  3933. * increment to return the actual count
  3934. */
  3935. return num_tc + 1;
  3936. }
  3937. /**
  3938. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  3939. * @dcbcfg: the corresponding DCBx configuration structure
  3940. *
  3941. * Query the current DCB configuration and return the number of
  3942. * traffic classes enabled from the given DCBX config
  3943. **/
  3944. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  3945. {
  3946. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  3947. u8 enabled_tc = 1;
  3948. u8 i;
  3949. for (i = 0; i < num_tc; i++)
  3950. enabled_tc |= BIT(i);
  3951. return enabled_tc;
  3952. }
  3953. /**
  3954. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  3955. * @pf: PF being queried
  3956. *
  3957. * Return number of traffic classes enabled for the given PF
  3958. **/
  3959. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  3960. {
  3961. struct i40e_hw *hw = &pf->hw;
  3962. u8 i, enabled_tc;
  3963. u8 num_tc = 0;
  3964. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  3965. /* If DCB is not enabled then always in single TC */
  3966. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  3967. return 1;
  3968. /* SFP mode will be enabled for all TCs on port */
  3969. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  3970. return i40e_dcb_get_num_tc(dcbcfg);
  3971. /* MFP mode return count of enabled TCs for this PF */
  3972. if (pf->hw.func_caps.iscsi)
  3973. enabled_tc = i40e_get_iscsi_tc_map(pf);
  3974. else
  3975. return 1; /* Only TC0 */
  3976. /* At least have TC0 */
  3977. enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  3978. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3979. if (enabled_tc & BIT(i))
  3980. num_tc++;
  3981. }
  3982. return num_tc;
  3983. }
  3984. /**
  3985. * i40e_pf_get_default_tc - Get bitmap for first enabled TC
  3986. * @pf: PF being queried
  3987. *
  3988. * Return a bitmap for first enabled traffic class for this PF.
  3989. **/
  3990. static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
  3991. {
  3992. u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
  3993. u8 i = 0;
  3994. if (!enabled_tc)
  3995. return 0x1; /* TC0 */
  3996. /* Find the first enabled TC */
  3997. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3998. if (enabled_tc & BIT(i))
  3999. break;
  4000. }
  4001. return BIT(i);
  4002. }
  4003. /**
  4004. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4005. * @pf: PF being queried
  4006. *
  4007. * Return a bitmap for enabled traffic classes for this PF.
  4008. **/
  4009. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4010. {
  4011. /* If DCB is not enabled for this PF then just return default TC */
  4012. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4013. return i40e_pf_get_default_tc(pf);
  4014. /* SFP mode we want PF to be enabled for all TCs */
  4015. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4016. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4017. /* MFP enabled and iSCSI PF type */
  4018. if (pf->hw.func_caps.iscsi)
  4019. return i40e_get_iscsi_tc_map(pf);
  4020. else
  4021. return i40e_pf_get_default_tc(pf);
  4022. }
  4023. /**
  4024. * i40e_vsi_get_bw_info - Query VSI BW Information
  4025. * @vsi: the VSI being queried
  4026. *
  4027. * Returns 0 on success, negative value on failure
  4028. **/
  4029. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4030. {
  4031. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4032. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4033. struct i40e_pf *pf = vsi->back;
  4034. struct i40e_hw *hw = &pf->hw;
  4035. i40e_status ret;
  4036. u32 tc_bw_max;
  4037. int i;
  4038. /* Get the VSI level BW configuration */
  4039. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4040. if (ret) {
  4041. dev_info(&pf->pdev->dev,
  4042. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4043. i40e_stat_str(&pf->hw, ret),
  4044. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4045. return -EINVAL;
  4046. }
  4047. /* Get the VSI level BW configuration per TC */
  4048. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4049. NULL);
  4050. if (ret) {
  4051. dev_info(&pf->pdev->dev,
  4052. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4053. i40e_stat_str(&pf->hw, ret),
  4054. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4055. return -EINVAL;
  4056. }
  4057. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4058. dev_info(&pf->pdev->dev,
  4059. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4060. bw_config.tc_valid_bits,
  4061. bw_ets_config.tc_valid_bits);
  4062. /* Still continuing */
  4063. }
  4064. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4065. vsi->bw_max_quanta = bw_config.max_bw;
  4066. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4067. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4068. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4069. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4070. vsi->bw_ets_limit_credits[i] =
  4071. le16_to_cpu(bw_ets_config.credits[i]);
  4072. /* 3 bits out of 4 for each TC */
  4073. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4074. }
  4075. return 0;
  4076. }
  4077. /**
  4078. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4079. * @vsi: the VSI being configured
  4080. * @enabled_tc: TC bitmap
  4081. * @bw_credits: BW shared credits per TC
  4082. *
  4083. * Returns 0 on success, negative value on failure
  4084. **/
  4085. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4086. u8 *bw_share)
  4087. {
  4088. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4089. i40e_status ret;
  4090. int i;
  4091. bw_data.tc_valid_bits = enabled_tc;
  4092. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4093. bw_data.tc_bw_credits[i] = bw_share[i];
  4094. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4095. NULL);
  4096. if (ret) {
  4097. dev_info(&vsi->back->pdev->dev,
  4098. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4099. vsi->back->hw.aq.asq_last_status);
  4100. return -EINVAL;
  4101. }
  4102. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4103. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4104. return 0;
  4105. }
  4106. /**
  4107. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4108. * @vsi: the VSI being configured
  4109. * @enabled_tc: TC map to be enabled
  4110. *
  4111. **/
  4112. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4113. {
  4114. struct net_device *netdev = vsi->netdev;
  4115. struct i40e_pf *pf = vsi->back;
  4116. struct i40e_hw *hw = &pf->hw;
  4117. u8 netdev_tc = 0;
  4118. int i;
  4119. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4120. if (!netdev)
  4121. return;
  4122. if (!enabled_tc) {
  4123. netdev_reset_tc(netdev);
  4124. return;
  4125. }
  4126. /* Set up actual enabled TCs on the VSI */
  4127. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4128. return;
  4129. /* set per TC queues for the VSI */
  4130. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4131. /* Only set TC queues for enabled tcs
  4132. *
  4133. * e.g. For a VSI that has TC0 and TC3 enabled the
  4134. * enabled_tc bitmap would be 0x00001001; the driver
  4135. * will set the numtc for netdev as 2 that will be
  4136. * referenced by the netdev layer as TC 0 and 1.
  4137. */
  4138. if (vsi->tc_config.enabled_tc & BIT(i))
  4139. netdev_set_tc_queue(netdev,
  4140. vsi->tc_config.tc_info[i].netdev_tc,
  4141. vsi->tc_config.tc_info[i].qcount,
  4142. vsi->tc_config.tc_info[i].qoffset);
  4143. }
  4144. /* Assign UP2TC map for the VSI */
  4145. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4146. /* Get the actual TC# for the UP */
  4147. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4148. /* Get the mapped netdev TC# for the UP */
  4149. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4150. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4151. }
  4152. }
  4153. /**
  4154. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4155. * @vsi: the VSI being configured
  4156. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4157. **/
  4158. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4159. struct i40e_vsi_context *ctxt)
  4160. {
  4161. /* copy just the sections touched not the entire info
  4162. * since not all sections are valid as returned by
  4163. * update vsi params
  4164. */
  4165. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4166. memcpy(&vsi->info.queue_mapping,
  4167. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4168. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4169. sizeof(vsi->info.tc_mapping));
  4170. }
  4171. /**
  4172. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4173. * @vsi: VSI to be configured
  4174. * @enabled_tc: TC bitmap
  4175. *
  4176. * This configures a particular VSI for TCs that are mapped to the
  4177. * given TC bitmap. It uses default bandwidth share for TCs across
  4178. * VSIs to configure TC for a particular VSI.
  4179. *
  4180. * NOTE:
  4181. * It is expected that the VSI queues have been quisced before calling
  4182. * this function.
  4183. **/
  4184. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4185. {
  4186. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4187. struct i40e_vsi_context ctxt;
  4188. int ret = 0;
  4189. int i;
  4190. /* Check if enabled_tc is same as existing or new TCs */
  4191. if (vsi->tc_config.enabled_tc == enabled_tc)
  4192. return ret;
  4193. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4194. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4195. if (enabled_tc & BIT(i))
  4196. bw_share[i] = 1;
  4197. }
  4198. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4199. if (ret) {
  4200. dev_info(&vsi->back->pdev->dev,
  4201. "Failed configuring TC map %d for VSI %d\n",
  4202. enabled_tc, vsi->seid);
  4203. goto out;
  4204. }
  4205. /* Update Queue Pairs Mapping for currently enabled UPs */
  4206. ctxt.seid = vsi->seid;
  4207. ctxt.pf_num = vsi->back->hw.pf_id;
  4208. ctxt.vf_num = 0;
  4209. ctxt.uplink_seid = vsi->uplink_seid;
  4210. ctxt.info = vsi->info;
  4211. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4212. /* Update the VSI after updating the VSI queue-mapping information */
  4213. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  4214. if (ret) {
  4215. dev_info(&vsi->back->pdev->dev,
  4216. "Update vsi tc config failed, err %s aq_err %s\n",
  4217. i40e_stat_str(&vsi->back->hw, ret),
  4218. i40e_aq_str(&vsi->back->hw,
  4219. vsi->back->hw.aq.asq_last_status));
  4220. goto out;
  4221. }
  4222. /* update the local VSI info with updated queue map */
  4223. i40e_vsi_update_queue_map(vsi, &ctxt);
  4224. vsi->info.valid_sections = 0;
  4225. /* Update current VSI BW information */
  4226. ret = i40e_vsi_get_bw_info(vsi);
  4227. if (ret) {
  4228. dev_info(&vsi->back->pdev->dev,
  4229. "Failed updating vsi bw info, err %s aq_err %s\n",
  4230. i40e_stat_str(&vsi->back->hw, ret),
  4231. i40e_aq_str(&vsi->back->hw,
  4232. vsi->back->hw.aq.asq_last_status));
  4233. goto out;
  4234. }
  4235. /* Update the netdev TC setup */
  4236. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4237. out:
  4238. return ret;
  4239. }
  4240. /**
  4241. * i40e_veb_config_tc - Configure TCs for given VEB
  4242. * @veb: given VEB
  4243. * @enabled_tc: TC bitmap
  4244. *
  4245. * Configures given TC bitmap for VEB (switching) element
  4246. **/
  4247. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  4248. {
  4249. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  4250. struct i40e_pf *pf = veb->pf;
  4251. int ret = 0;
  4252. int i;
  4253. /* No TCs or already enabled TCs just return */
  4254. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  4255. return ret;
  4256. bw_data.tc_valid_bits = enabled_tc;
  4257. /* bw_data.absolute_credits is not set (relative) */
  4258. /* Enable ETS TCs with equal BW Share for now */
  4259. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4260. if (enabled_tc & BIT(i))
  4261. bw_data.tc_bw_share_credits[i] = 1;
  4262. }
  4263. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  4264. &bw_data, NULL);
  4265. if (ret) {
  4266. dev_info(&pf->pdev->dev,
  4267. "VEB bw config failed, err %s aq_err %s\n",
  4268. i40e_stat_str(&pf->hw, ret),
  4269. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4270. goto out;
  4271. }
  4272. /* Update the BW information */
  4273. ret = i40e_veb_get_bw_info(veb);
  4274. if (ret) {
  4275. dev_info(&pf->pdev->dev,
  4276. "Failed getting veb bw config, err %s aq_err %s\n",
  4277. i40e_stat_str(&pf->hw, ret),
  4278. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4279. }
  4280. out:
  4281. return ret;
  4282. }
  4283. #ifdef CONFIG_I40E_DCB
  4284. /**
  4285. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  4286. * @pf: PF struct
  4287. *
  4288. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  4289. * the caller would've quiesce all the VSIs before calling
  4290. * this function
  4291. **/
  4292. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  4293. {
  4294. u8 tc_map = 0;
  4295. int ret;
  4296. u8 v;
  4297. /* Enable the TCs available on PF to all VEBs */
  4298. tc_map = i40e_pf_get_tc_map(pf);
  4299. for (v = 0; v < I40E_MAX_VEB; v++) {
  4300. if (!pf->veb[v])
  4301. continue;
  4302. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  4303. if (ret) {
  4304. dev_info(&pf->pdev->dev,
  4305. "Failed configuring TC for VEB seid=%d\n",
  4306. pf->veb[v]->seid);
  4307. /* Will try to configure as many components */
  4308. }
  4309. }
  4310. /* Update each VSI */
  4311. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4312. if (!pf->vsi[v])
  4313. continue;
  4314. /* - Enable all TCs for the LAN VSI
  4315. #ifdef I40E_FCOE
  4316. * - For FCoE VSI only enable the TC configured
  4317. * as per the APP TLV
  4318. #endif
  4319. * - For all others keep them at TC0 for now
  4320. */
  4321. if (v == pf->lan_vsi)
  4322. tc_map = i40e_pf_get_tc_map(pf);
  4323. else
  4324. tc_map = i40e_pf_get_default_tc(pf);
  4325. #ifdef I40E_FCOE
  4326. if (pf->vsi[v]->type == I40E_VSI_FCOE)
  4327. tc_map = i40e_get_fcoe_tc_map(pf);
  4328. #endif /* #ifdef I40E_FCOE */
  4329. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  4330. if (ret) {
  4331. dev_info(&pf->pdev->dev,
  4332. "Failed configuring TC for VSI seid=%d\n",
  4333. pf->vsi[v]->seid);
  4334. /* Will try to configure as many components */
  4335. } else {
  4336. /* Re-configure VSI vectors based on updated TC map */
  4337. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  4338. if (pf->vsi[v]->netdev)
  4339. i40e_dcbnl_set_all(pf->vsi[v]);
  4340. }
  4341. }
  4342. }
  4343. /**
  4344. * i40e_resume_port_tx - Resume port Tx
  4345. * @pf: PF struct
  4346. *
  4347. * Resume a port's Tx and issue a PF reset in case of failure to
  4348. * resume.
  4349. **/
  4350. static int i40e_resume_port_tx(struct i40e_pf *pf)
  4351. {
  4352. struct i40e_hw *hw = &pf->hw;
  4353. int ret;
  4354. ret = i40e_aq_resume_port_tx(hw, NULL);
  4355. if (ret) {
  4356. dev_info(&pf->pdev->dev,
  4357. "Resume Port Tx failed, err %s aq_err %s\n",
  4358. i40e_stat_str(&pf->hw, ret),
  4359. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4360. /* Schedule PF reset to recover */
  4361. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  4362. i40e_service_event_schedule(pf);
  4363. }
  4364. return ret;
  4365. }
  4366. /**
  4367. * i40e_init_pf_dcb - Initialize DCB configuration
  4368. * @pf: PF being configured
  4369. *
  4370. * Query the current DCB configuration and cache it
  4371. * in the hardware structure
  4372. **/
  4373. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  4374. {
  4375. struct i40e_hw *hw = &pf->hw;
  4376. int err = 0;
  4377. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
  4378. if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  4379. (pf->hw.aq.fw_maj_ver < 4))
  4380. goto out;
  4381. /* Get the initial DCB configuration */
  4382. err = i40e_init_dcb(hw);
  4383. if (!err) {
  4384. /* Device/Function is not DCBX capable */
  4385. if ((!hw->func_caps.dcb) ||
  4386. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  4387. dev_info(&pf->pdev->dev,
  4388. "DCBX offload is not supported or is disabled for this PF.\n");
  4389. if (pf->flags & I40E_FLAG_MFP_ENABLED)
  4390. goto out;
  4391. } else {
  4392. /* When status is not DISABLED then DCBX in FW */
  4393. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  4394. DCB_CAP_DCBX_VER_IEEE;
  4395. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  4396. /* Enable DCB tagging only when more than one TC */
  4397. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  4398. pf->flags |= I40E_FLAG_DCB_ENABLED;
  4399. dev_dbg(&pf->pdev->dev,
  4400. "DCBX offload is supported for this PF.\n");
  4401. }
  4402. } else {
  4403. dev_info(&pf->pdev->dev,
  4404. "Query for DCB configuration failed, err %s aq_err %s\n",
  4405. i40e_stat_str(&pf->hw, err),
  4406. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4407. }
  4408. out:
  4409. return err;
  4410. }
  4411. #endif /* CONFIG_I40E_DCB */
  4412. #define SPEED_SIZE 14
  4413. #define FC_SIZE 8
  4414. /**
  4415. * i40e_print_link_message - print link up or down
  4416. * @vsi: the VSI for which link needs a message
  4417. */
  4418. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  4419. {
  4420. char *speed = "Unknown";
  4421. char *fc = "Unknown";
  4422. if (vsi->current_isup == isup)
  4423. return;
  4424. vsi->current_isup = isup;
  4425. if (!isup) {
  4426. netdev_info(vsi->netdev, "NIC Link is Down\n");
  4427. return;
  4428. }
  4429. /* Warn user if link speed on NPAR enabled partition is not at
  4430. * least 10GB
  4431. */
  4432. if (vsi->back->hw.func_caps.npar_enable &&
  4433. (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  4434. vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  4435. netdev_warn(vsi->netdev,
  4436. "The partition detected link speed that is less than 10Gbps\n");
  4437. switch (vsi->back->hw.phy.link_info.link_speed) {
  4438. case I40E_LINK_SPEED_40GB:
  4439. speed = "40 G";
  4440. break;
  4441. case I40E_LINK_SPEED_20GB:
  4442. speed = "20 G";
  4443. break;
  4444. case I40E_LINK_SPEED_10GB:
  4445. speed = "10 G";
  4446. break;
  4447. case I40E_LINK_SPEED_1GB:
  4448. speed = "1000 M";
  4449. break;
  4450. case I40E_LINK_SPEED_100MB:
  4451. speed = "100 M";
  4452. break;
  4453. default:
  4454. break;
  4455. }
  4456. switch (vsi->back->hw.fc.current_mode) {
  4457. case I40E_FC_FULL:
  4458. fc = "RX/TX";
  4459. break;
  4460. case I40E_FC_TX_PAUSE:
  4461. fc = "TX";
  4462. break;
  4463. case I40E_FC_RX_PAUSE:
  4464. fc = "RX";
  4465. break;
  4466. default:
  4467. fc = "None";
  4468. break;
  4469. }
  4470. netdev_info(vsi->netdev, "NIC Link is Up %sbps Full Duplex, Flow Control: %s\n",
  4471. speed, fc);
  4472. }
  4473. /**
  4474. * i40e_up_complete - Finish the last steps of bringing up a connection
  4475. * @vsi: the VSI being configured
  4476. **/
  4477. static int i40e_up_complete(struct i40e_vsi *vsi)
  4478. {
  4479. struct i40e_pf *pf = vsi->back;
  4480. int err;
  4481. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4482. i40e_vsi_configure_msix(vsi);
  4483. else
  4484. i40e_configure_msi_and_legacy(vsi);
  4485. /* start rings */
  4486. err = i40e_vsi_control_rings(vsi, true);
  4487. if (err)
  4488. return err;
  4489. clear_bit(__I40E_DOWN, &vsi->state);
  4490. i40e_napi_enable_all(vsi);
  4491. i40e_vsi_enable_irq(vsi);
  4492. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  4493. (vsi->netdev)) {
  4494. i40e_print_link_message(vsi, true);
  4495. netif_tx_start_all_queues(vsi->netdev);
  4496. netif_carrier_on(vsi->netdev);
  4497. } else if (vsi->netdev) {
  4498. i40e_print_link_message(vsi, false);
  4499. /* need to check for qualified module here*/
  4500. if ((pf->hw.phy.link_info.link_info &
  4501. I40E_AQ_MEDIA_AVAILABLE) &&
  4502. (!(pf->hw.phy.link_info.an_info &
  4503. I40E_AQ_QUALIFIED_MODULE)))
  4504. netdev_err(vsi->netdev,
  4505. "the driver failed to link because an unqualified module was detected.");
  4506. }
  4507. /* replay FDIR SB filters */
  4508. if (vsi->type == I40E_VSI_FDIR) {
  4509. /* reset fd counters */
  4510. pf->fd_add_err = pf->fd_atr_cnt = 0;
  4511. if (pf->fd_tcp_rule > 0) {
  4512. pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  4513. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  4514. dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 exist\n");
  4515. pf->fd_tcp_rule = 0;
  4516. }
  4517. i40e_fdir_filter_restore(vsi);
  4518. }
  4519. i40e_service_event_schedule(pf);
  4520. return 0;
  4521. }
  4522. /**
  4523. * i40e_vsi_reinit_locked - Reset the VSI
  4524. * @vsi: the VSI being configured
  4525. *
  4526. * Rebuild the ring structs after some configuration
  4527. * has changed, e.g. MTU size.
  4528. **/
  4529. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  4530. {
  4531. struct i40e_pf *pf = vsi->back;
  4532. WARN_ON(in_interrupt());
  4533. while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
  4534. usleep_range(1000, 2000);
  4535. i40e_down(vsi);
  4536. /* Give a VF some time to respond to the reset. The
  4537. * two second wait is based upon the watchdog cycle in
  4538. * the VF driver.
  4539. */
  4540. if (vsi->type == I40E_VSI_SRIOV)
  4541. msleep(2000);
  4542. i40e_up(vsi);
  4543. clear_bit(__I40E_CONFIG_BUSY, &pf->state);
  4544. }
  4545. /**
  4546. * i40e_up - Bring the connection back up after being down
  4547. * @vsi: the VSI being configured
  4548. **/
  4549. int i40e_up(struct i40e_vsi *vsi)
  4550. {
  4551. int err;
  4552. err = i40e_vsi_configure(vsi);
  4553. if (!err)
  4554. err = i40e_up_complete(vsi);
  4555. return err;
  4556. }
  4557. /**
  4558. * i40e_down - Shutdown the connection processing
  4559. * @vsi: the VSI being stopped
  4560. **/
  4561. void i40e_down(struct i40e_vsi *vsi)
  4562. {
  4563. int i;
  4564. /* It is assumed that the caller of this function
  4565. * sets the vsi->state __I40E_DOWN bit.
  4566. */
  4567. if (vsi->netdev) {
  4568. netif_carrier_off(vsi->netdev);
  4569. netif_tx_disable(vsi->netdev);
  4570. }
  4571. i40e_vsi_disable_irq(vsi);
  4572. i40e_vsi_control_rings(vsi, false);
  4573. i40e_napi_disable_all(vsi);
  4574. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4575. i40e_clean_tx_ring(vsi->tx_rings[i]);
  4576. i40e_clean_rx_ring(vsi->rx_rings[i]);
  4577. }
  4578. }
  4579. /**
  4580. * i40e_setup_tc - configure multiple traffic classes
  4581. * @netdev: net device to configure
  4582. * @tc: number of traffic classes to enable
  4583. **/
  4584. #ifdef I40E_FCOE
  4585. int i40e_setup_tc(struct net_device *netdev, u8 tc)
  4586. #else
  4587. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  4588. #endif
  4589. {
  4590. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4591. struct i40e_vsi *vsi = np->vsi;
  4592. struct i40e_pf *pf = vsi->back;
  4593. u8 enabled_tc = 0;
  4594. int ret = -EINVAL;
  4595. int i;
  4596. /* Check if DCB enabled to continue */
  4597. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4598. netdev_info(netdev, "DCB is not enabled for adapter\n");
  4599. goto exit;
  4600. }
  4601. /* Check if MFP enabled */
  4602. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  4603. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  4604. goto exit;
  4605. }
  4606. /* Check whether tc count is within enabled limit */
  4607. if (tc > i40e_pf_get_num_tc(pf)) {
  4608. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  4609. goto exit;
  4610. }
  4611. /* Generate TC map for number of tc requested */
  4612. for (i = 0; i < tc; i++)
  4613. enabled_tc |= BIT(i);
  4614. /* Requesting same TC configuration as already enabled */
  4615. if (enabled_tc == vsi->tc_config.enabled_tc)
  4616. return 0;
  4617. /* Quiesce VSI queues */
  4618. i40e_quiesce_vsi(vsi);
  4619. /* Configure VSI for enabled TCs */
  4620. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  4621. if (ret) {
  4622. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  4623. vsi->seid);
  4624. goto exit;
  4625. }
  4626. /* Unquiesce VSI */
  4627. i40e_unquiesce_vsi(vsi);
  4628. exit:
  4629. return ret;
  4630. }
  4631. /**
  4632. * i40e_open - Called when a network interface is made active
  4633. * @netdev: network interface device structure
  4634. *
  4635. * The open entry point is called when a network interface is made
  4636. * active by the system (IFF_UP). At this point all resources needed
  4637. * for transmit and receive operations are allocated, the interrupt
  4638. * handler is registered with the OS, the netdev watchdog subtask is
  4639. * enabled, and the stack is notified that the interface is ready.
  4640. *
  4641. * Returns 0 on success, negative value on failure
  4642. **/
  4643. int i40e_open(struct net_device *netdev)
  4644. {
  4645. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4646. struct i40e_vsi *vsi = np->vsi;
  4647. struct i40e_pf *pf = vsi->back;
  4648. int err;
  4649. /* disallow open during test or if eeprom is broken */
  4650. if (test_bit(__I40E_TESTING, &pf->state) ||
  4651. test_bit(__I40E_BAD_EEPROM, &pf->state))
  4652. return -EBUSY;
  4653. netif_carrier_off(netdev);
  4654. err = i40e_vsi_open(vsi);
  4655. if (err)
  4656. return err;
  4657. /* configure global TSO hardware offload settings */
  4658. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  4659. TCP_FLAG_FIN) >> 16);
  4660. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  4661. TCP_FLAG_FIN |
  4662. TCP_FLAG_CWR) >> 16);
  4663. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  4664. #ifdef CONFIG_I40E_VXLAN
  4665. vxlan_get_rx_port(netdev);
  4666. #endif
  4667. return 0;
  4668. }
  4669. /**
  4670. * i40e_vsi_open -
  4671. * @vsi: the VSI to open
  4672. *
  4673. * Finish initialization of the VSI.
  4674. *
  4675. * Returns 0 on success, negative value on failure
  4676. **/
  4677. int i40e_vsi_open(struct i40e_vsi *vsi)
  4678. {
  4679. struct i40e_pf *pf = vsi->back;
  4680. char int_name[I40E_INT_NAME_STR_LEN];
  4681. int err;
  4682. /* allocate descriptors */
  4683. err = i40e_vsi_setup_tx_resources(vsi);
  4684. if (err)
  4685. goto err_setup_tx;
  4686. err = i40e_vsi_setup_rx_resources(vsi);
  4687. if (err)
  4688. goto err_setup_rx;
  4689. err = i40e_vsi_configure(vsi);
  4690. if (err)
  4691. goto err_setup_rx;
  4692. if (vsi->netdev) {
  4693. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  4694. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  4695. err = i40e_vsi_request_irq(vsi, int_name);
  4696. if (err)
  4697. goto err_setup_rx;
  4698. /* Notify the stack of the actual queue counts. */
  4699. err = netif_set_real_num_tx_queues(vsi->netdev,
  4700. vsi->num_queue_pairs);
  4701. if (err)
  4702. goto err_set_queues;
  4703. err = netif_set_real_num_rx_queues(vsi->netdev,
  4704. vsi->num_queue_pairs);
  4705. if (err)
  4706. goto err_set_queues;
  4707. } else if (vsi->type == I40E_VSI_FDIR) {
  4708. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  4709. dev_driver_string(&pf->pdev->dev),
  4710. dev_name(&pf->pdev->dev));
  4711. err = i40e_vsi_request_irq(vsi, int_name);
  4712. } else {
  4713. err = -EINVAL;
  4714. goto err_setup_rx;
  4715. }
  4716. err = i40e_up_complete(vsi);
  4717. if (err)
  4718. goto err_up_complete;
  4719. return 0;
  4720. err_up_complete:
  4721. i40e_down(vsi);
  4722. err_set_queues:
  4723. i40e_vsi_free_irq(vsi);
  4724. err_setup_rx:
  4725. i40e_vsi_free_rx_resources(vsi);
  4726. err_setup_tx:
  4727. i40e_vsi_free_tx_resources(vsi);
  4728. if (vsi == pf->vsi[pf->lan_vsi])
  4729. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  4730. return err;
  4731. }
  4732. /**
  4733. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  4734. * @pf: Pointer to PF
  4735. *
  4736. * This function destroys the hlist where all the Flow Director
  4737. * filters were saved.
  4738. **/
  4739. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  4740. {
  4741. struct i40e_fdir_filter *filter;
  4742. struct hlist_node *node2;
  4743. hlist_for_each_entry_safe(filter, node2,
  4744. &pf->fdir_filter_list, fdir_node) {
  4745. hlist_del(&filter->fdir_node);
  4746. kfree(filter);
  4747. }
  4748. pf->fdir_pf_active_filters = 0;
  4749. }
  4750. /**
  4751. * i40e_close - Disables a network interface
  4752. * @netdev: network interface device structure
  4753. *
  4754. * The close entry point is called when an interface is de-activated
  4755. * by the OS. The hardware is still under the driver's control, but
  4756. * this netdev interface is disabled.
  4757. *
  4758. * Returns 0, this is not allowed to fail
  4759. **/
  4760. #ifdef I40E_FCOE
  4761. int i40e_close(struct net_device *netdev)
  4762. #else
  4763. static int i40e_close(struct net_device *netdev)
  4764. #endif
  4765. {
  4766. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4767. struct i40e_vsi *vsi = np->vsi;
  4768. i40e_vsi_close(vsi);
  4769. return 0;
  4770. }
  4771. /**
  4772. * i40e_do_reset - Start a PF or Core Reset sequence
  4773. * @pf: board private structure
  4774. * @reset_flags: which reset is requested
  4775. *
  4776. * The essential difference in resets is that the PF Reset
  4777. * doesn't clear the packet buffers, doesn't reset the PE
  4778. * firmware, and doesn't bother the other PFs on the chip.
  4779. **/
  4780. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
  4781. {
  4782. u32 val;
  4783. WARN_ON(in_interrupt());
  4784. if (i40e_check_asq_alive(&pf->hw))
  4785. i40e_vc_notify_reset(pf);
  4786. /* do the biggest reset indicated */
  4787. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  4788. /* Request a Global Reset
  4789. *
  4790. * This will start the chip's countdown to the actual full
  4791. * chip reset event, and a warning interrupt to be sent
  4792. * to all PFs, including the requestor. Our handler
  4793. * for the warning interrupt will deal with the shutdown
  4794. * and recovery of the switch setup.
  4795. */
  4796. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  4797. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  4798. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  4799. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  4800. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  4801. /* Request a Core Reset
  4802. *
  4803. * Same as Global Reset, except does *not* include the MAC/PHY
  4804. */
  4805. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  4806. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  4807. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  4808. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  4809. i40e_flush(&pf->hw);
  4810. } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
  4811. /* Request a PF Reset
  4812. *
  4813. * Resets only the PF-specific registers
  4814. *
  4815. * This goes directly to the tear-down and rebuild of
  4816. * the switch, since we need to do all the recovery as
  4817. * for the Core Reset.
  4818. */
  4819. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  4820. i40e_handle_reset_warning(pf);
  4821. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  4822. int v;
  4823. /* Find the VSI(s) that requested a re-init */
  4824. dev_info(&pf->pdev->dev,
  4825. "VSI reinit requested\n");
  4826. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4827. struct i40e_vsi *vsi = pf->vsi[v];
  4828. if (vsi != NULL &&
  4829. test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
  4830. i40e_vsi_reinit_locked(pf->vsi[v]);
  4831. clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  4832. }
  4833. }
  4834. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  4835. int v;
  4836. /* Find the VSI(s) that needs to be brought down */
  4837. dev_info(&pf->pdev->dev, "VSI down requested\n");
  4838. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4839. struct i40e_vsi *vsi = pf->vsi[v];
  4840. if (vsi != NULL &&
  4841. test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
  4842. set_bit(__I40E_DOWN, &vsi->state);
  4843. i40e_down(vsi);
  4844. clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
  4845. }
  4846. }
  4847. } else {
  4848. dev_info(&pf->pdev->dev,
  4849. "bad reset request 0x%08x\n", reset_flags);
  4850. }
  4851. }
  4852. #ifdef CONFIG_I40E_DCB
  4853. /**
  4854. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  4855. * @pf: board private structure
  4856. * @old_cfg: current DCB config
  4857. * @new_cfg: new DCB config
  4858. **/
  4859. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  4860. struct i40e_dcbx_config *old_cfg,
  4861. struct i40e_dcbx_config *new_cfg)
  4862. {
  4863. bool need_reconfig = false;
  4864. /* Check if ETS configuration has changed */
  4865. if (memcmp(&new_cfg->etscfg,
  4866. &old_cfg->etscfg,
  4867. sizeof(new_cfg->etscfg))) {
  4868. /* If Priority Table has changed reconfig is needed */
  4869. if (memcmp(&new_cfg->etscfg.prioritytable,
  4870. &old_cfg->etscfg.prioritytable,
  4871. sizeof(new_cfg->etscfg.prioritytable))) {
  4872. need_reconfig = true;
  4873. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  4874. }
  4875. if (memcmp(&new_cfg->etscfg.tcbwtable,
  4876. &old_cfg->etscfg.tcbwtable,
  4877. sizeof(new_cfg->etscfg.tcbwtable)))
  4878. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  4879. if (memcmp(&new_cfg->etscfg.tsatable,
  4880. &old_cfg->etscfg.tsatable,
  4881. sizeof(new_cfg->etscfg.tsatable)))
  4882. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  4883. }
  4884. /* Check if PFC configuration has changed */
  4885. if (memcmp(&new_cfg->pfc,
  4886. &old_cfg->pfc,
  4887. sizeof(new_cfg->pfc))) {
  4888. need_reconfig = true;
  4889. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  4890. }
  4891. /* Check if APP Table has changed */
  4892. if (memcmp(&new_cfg->app,
  4893. &old_cfg->app,
  4894. sizeof(new_cfg->app))) {
  4895. need_reconfig = true;
  4896. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  4897. }
  4898. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  4899. return need_reconfig;
  4900. }
  4901. /**
  4902. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  4903. * @pf: board private structure
  4904. * @e: event info posted on ARQ
  4905. **/
  4906. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  4907. struct i40e_arq_event_info *e)
  4908. {
  4909. struct i40e_aqc_lldp_get_mib *mib =
  4910. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  4911. struct i40e_hw *hw = &pf->hw;
  4912. struct i40e_dcbx_config tmp_dcbx_cfg;
  4913. bool need_reconfig = false;
  4914. int ret = 0;
  4915. u8 type;
  4916. /* Not DCB capable or capability disabled */
  4917. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  4918. return ret;
  4919. /* Ignore if event is not for Nearest Bridge */
  4920. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  4921. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  4922. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  4923. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  4924. return ret;
  4925. /* Check MIB Type and return if event for Remote MIB update */
  4926. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  4927. dev_dbg(&pf->pdev->dev,
  4928. "LLDP event mib type %s\n", type ? "remote" : "local");
  4929. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  4930. /* Update the remote cached instance and return */
  4931. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  4932. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  4933. &hw->remote_dcbx_config);
  4934. goto exit;
  4935. }
  4936. /* Store the old configuration */
  4937. tmp_dcbx_cfg = hw->local_dcbx_config;
  4938. /* Reset the old DCBx configuration data */
  4939. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  4940. /* Get updated DCBX data from firmware */
  4941. ret = i40e_get_dcb_config(&pf->hw);
  4942. if (ret) {
  4943. dev_info(&pf->pdev->dev,
  4944. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  4945. i40e_stat_str(&pf->hw, ret),
  4946. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4947. goto exit;
  4948. }
  4949. /* No change detected in DCBX configs */
  4950. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  4951. sizeof(tmp_dcbx_cfg))) {
  4952. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  4953. goto exit;
  4954. }
  4955. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  4956. &hw->local_dcbx_config);
  4957. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  4958. if (!need_reconfig)
  4959. goto exit;
  4960. /* Enable DCB tagging only when more than one TC */
  4961. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  4962. pf->flags |= I40E_FLAG_DCB_ENABLED;
  4963. else
  4964. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  4965. set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  4966. /* Reconfiguration needed quiesce all VSIs */
  4967. i40e_pf_quiesce_all_vsi(pf);
  4968. /* Changes in configuration update VEB/VSI */
  4969. i40e_dcb_reconfigure(pf);
  4970. ret = i40e_resume_port_tx(pf);
  4971. clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  4972. /* In case of error no point in resuming VSIs */
  4973. if (ret)
  4974. goto exit;
  4975. /* Wait for the PF's Tx queues to be disabled */
  4976. ret = i40e_pf_wait_txq_disabled(pf);
  4977. if (ret) {
  4978. /* Schedule PF reset to recover */
  4979. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  4980. i40e_service_event_schedule(pf);
  4981. } else {
  4982. i40e_pf_unquiesce_all_vsi(pf);
  4983. }
  4984. exit:
  4985. return ret;
  4986. }
  4987. #endif /* CONFIG_I40E_DCB */
  4988. /**
  4989. * i40e_do_reset_safe - Protected reset path for userland calls.
  4990. * @pf: board private structure
  4991. * @reset_flags: which reset is requested
  4992. *
  4993. **/
  4994. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  4995. {
  4996. rtnl_lock();
  4997. i40e_do_reset(pf, reset_flags);
  4998. rtnl_unlock();
  4999. }
  5000. /**
  5001. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  5002. * @pf: board private structure
  5003. * @e: event info posted on ARQ
  5004. *
  5005. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  5006. * and VF queues
  5007. **/
  5008. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  5009. struct i40e_arq_event_info *e)
  5010. {
  5011. struct i40e_aqc_lan_overflow *data =
  5012. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  5013. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  5014. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  5015. struct i40e_hw *hw = &pf->hw;
  5016. struct i40e_vf *vf;
  5017. u16 vf_id;
  5018. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  5019. queue, qtx_ctl);
  5020. /* Queue belongs to VF, find the VF and issue VF reset */
  5021. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  5022. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  5023. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  5024. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  5025. vf_id -= hw->func_caps.vf_base_id;
  5026. vf = &pf->vf[vf_id];
  5027. i40e_vc_notify_vf_reset(vf);
  5028. /* Allow VF to process pending reset notification */
  5029. msleep(20);
  5030. i40e_reset_vf(vf, false);
  5031. }
  5032. }
  5033. /**
  5034. * i40e_service_event_complete - Finish up the service event
  5035. * @pf: board private structure
  5036. **/
  5037. static void i40e_service_event_complete(struct i40e_pf *pf)
  5038. {
  5039. WARN_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
  5040. /* flush memory to make sure state is correct before next watchog */
  5041. smp_mb__before_atomic();
  5042. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  5043. }
  5044. /**
  5045. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  5046. * @pf: board private structure
  5047. **/
  5048. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  5049. {
  5050. u32 val, fcnt_prog;
  5051. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5052. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  5053. return fcnt_prog;
  5054. }
  5055. /**
  5056. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  5057. * @pf: board private structure
  5058. **/
  5059. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  5060. {
  5061. u32 val, fcnt_prog;
  5062. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5063. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  5064. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  5065. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  5066. return fcnt_prog;
  5067. }
  5068. /**
  5069. * i40e_get_global_fd_count - Get total FD filters programmed on device
  5070. * @pf: board private structure
  5071. **/
  5072. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  5073. {
  5074. u32 val, fcnt_prog;
  5075. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  5076. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  5077. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  5078. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  5079. return fcnt_prog;
  5080. }
  5081. /**
  5082. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  5083. * @pf: board private structure
  5084. **/
  5085. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  5086. {
  5087. struct i40e_fdir_filter *filter;
  5088. u32 fcnt_prog, fcnt_avail;
  5089. struct hlist_node *node;
  5090. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5091. return;
  5092. /* Check if, FD SB or ATR was auto disabled and if there is enough room
  5093. * to re-enable
  5094. */
  5095. fcnt_prog = i40e_get_global_fd_count(pf);
  5096. fcnt_avail = pf->fdir_pf_filter_count;
  5097. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  5098. (pf->fd_add_err == 0) ||
  5099. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
  5100. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  5101. (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
  5102. pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5103. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5104. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  5105. }
  5106. }
  5107. /* Wait for some more space to be available to turn on ATR */
  5108. if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
  5109. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5110. (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
  5111. pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5112. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5113. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
  5114. }
  5115. }
  5116. /* if hw had a problem adding a filter, delete it */
  5117. if (pf->fd_inv > 0) {
  5118. hlist_for_each_entry_safe(filter, node,
  5119. &pf->fdir_filter_list, fdir_node) {
  5120. if (filter->fd_id == pf->fd_inv) {
  5121. hlist_del(&filter->fdir_node);
  5122. kfree(filter);
  5123. pf->fdir_pf_active_filters--;
  5124. }
  5125. }
  5126. }
  5127. }
  5128. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  5129. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  5130. /**
  5131. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  5132. * @pf: board private structure
  5133. **/
  5134. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  5135. {
  5136. unsigned long min_flush_time;
  5137. int flush_wait_retry = 50;
  5138. bool disable_atr = false;
  5139. int fd_room;
  5140. int reg;
  5141. if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
  5142. return;
  5143. if (!time_after(jiffies, pf->fd_flush_timestamp +
  5144. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  5145. return;
  5146. /* If the flush is happening too quick and we have mostly SB rules we
  5147. * should not re-enable ATR for some time.
  5148. */
  5149. min_flush_time = pf->fd_flush_timestamp +
  5150. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  5151. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  5152. if (!(time_after(jiffies, min_flush_time)) &&
  5153. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  5154. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5155. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  5156. disable_atr = true;
  5157. }
  5158. pf->fd_flush_timestamp = jiffies;
  5159. pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5160. /* flush all filters */
  5161. wr32(&pf->hw, I40E_PFQF_CTL_1,
  5162. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  5163. i40e_flush(&pf->hw);
  5164. pf->fd_flush_cnt++;
  5165. pf->fd_add_err = 0;
  5166. do {
  5167. /* Check FD flush status every 5-6msec */
  5168. usleep_range(5000, 6000);
  5169. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  5170. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  5171. break;
  5172. } while (flush_wait_retry--);
  5173. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  5174. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  5175. } else {
  5176. /* replay sideband filters */
  5177. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  5178. if (!disable_atr)
  5179. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  5180. clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
  5181. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5182. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  5183. }
  5184. }
  5185. /**
  5186. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  5187. * @pf: board private structure
  5188. **/
  5189. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  5190. {
  5191. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  5192. }
  5193. /* We can see up to 256 filter programming desc in transit if the filters are
  5194. * being applied really fast; before we see the first
  5195. * filter miss error on Rx queue 0. Accumulating enough error messages before
  5196. * reacting will make sure we don't cause flush too often.
  5197. */
  5198. #define I40E_MAX_FD_PROGRAM_ERROR 256
  5199. /**
  5200. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  5201. * @pf: board private structure
  5202. **/
  5203. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  5204. {
  5205. /* if interface is down do nothing */
  5206. if (test_bit(__I40E_DOWN, &pf->state))
  5207. return;
  5208. if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)))
  5209. return;
  5210. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5211. i40e_fdir_flush_and_replay(pf);
  5212. i40e_fdir_check_and_reenable(pf);
  5213. }
  5214. /**
  5215. * i40e_vsi_link_event - notify VSI of a link event
  5216. * @vsi: vsi to be notified
  5217. * @link_up: link up or down
  5218. **/
  5219. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  5220. {
  5221. if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
  5222. return;
  5223. switch (vsi->type) {
  5224. case I40E_VSI_MAIN:
  5225. #ifdef I40E_FCOE
  5226. case I40E_VSI_FCOE:
  5227. #endif
  5228. if (!vsi->netdev || !vsi->netdev_registered)
  5229. break;
  5230. if (link_up) {
  5231. netif_carrier_on(vsi->netdev);
  5232. netif_tx_wake_all_queues(vsi->netdev);
  5233. } else {
  5234. netif_carrier_off(vsi->netdev);
  5235. netif_tx_stop_all_queues(vsi->netdev);
  5236. }
  5237. break;
  5238. case I40E_VSI_SRIOV:
  5239. case I40E_VSI_VMDQ2:
  5240. case I40E_VSI_CTRL:
  5241. case I40E_VSI_MIRROR:
  5242. default:
  5243. /* there is no notification for other VSIs */
  5244. break;
  5245. }
  5246. }
  5247. /**
  5248. * i40e_veb_link_event - notify elements on the veb of a link event
  5249. * @veb: veb to be notified
  5250. * @link_up: link up or down
  5251. **/
  5252. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  5253. {
  5254. struct i40e_pf *pf;
  5255. int i;
  5256. if (!veb || !veb->pf)
  5257. return;
  5258. pf = veb->pf;
  5259. /* depth first... */
  5260. for (i = 0; i < I40E_MAX_VEB; i++)
  5261. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  5262. i40e_veb_link_event(pf->veb[i], link_up);
  5263. /* ... now the local VSIs */
  5264. for (i = 0; i < pf->num_alloc_vsi; i++)
  5265. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  5266. i40e_vsi_link_event(pf->vsi[i], link_up);
  5267. }
  5268. /**
  5269. * i40e_link_event - Update netif_carrier status
  5270. * @pf: board private structure
  5271. **/
  5272. static void i40e_link_event(struct i40e_pf *pf)
  5273. {
  5274. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5275. u8 new_link_speed, old_link_speed;
  5276. i40e_status status;
  5277. bool new_link, old_link;
  5278. /* save off old link status information */
  5279. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  5280. /* set this to force the get_link_status call to refresh state */
  5281. pf->hw.phy.get_link_info = true;
  5282. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  5283. status = i40e_get_link_status(&pf->hw, &new_link);
  5284. if (status) {
  5285. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  5286. status);
  5287. return;
  5288. }
  5289. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  5290. new_link_speed = pf->hw.phy.link_info.link_speed;
  5291. if (new_link == old_link &&
  5292. new_link_speed == old_link_speed &&
  5293. (test_bit(__I40E_DOWN, &vsi->state) ||
  5294. new_link == netif_carrier_ok(vsi->netdev)))
  5295. return;
  5296. if (!test_bit(__I40E_DOWN, &vsi->state))
  5297. i40e_print_link_message(vsi, new_link);
  5298. /* Notify the base of the switch tree connected to
  5299. * the link. Floating VEBs are not notified.
  5300. */
  5301. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  5302. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  5303. else
  5304. i40e_vsi_link_event(vsi, new_link);
  5305. if (pf->vf)
  5306. i40e_vc_notify_link_state(pf);
  5307. if (pf->flags & I40E_FLAG_PTP)
  5308. i40e_ptp_set_increment(pf);
  5309. }
  5310. /**
  5311. * i40e_watchdog_subtask - periodic checks not using event driven response
  5312. * @pf: board private structure
  5313. **/
  5314. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  5315. {
  5316. int i;
  5317. /* if interface is down do nothing */
  5318. if (test_bit(__I40E_DOWN, &pf->state) ||
  5319. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5320. return;
  5321. /* make sure we don't do these things too often */
  5322. if (time_before(jiffies, (pf->service_timer_previous +
  5323. pf->service_timer_period)))
  5324. return;
  5325. pf->service_timer_previous = jiffies;
  5326. if (pf->flags & I40E_FLAG_LINK_POLLING_ENABLED)
  5327. i40e_link_event(pf);
  5328. /* Update the stats for active netdevs so the network stack
  5329. * can look at updated numbers whenever it cares to
  5330. */
  5331. for (i = 0; i < pf->num_alloc_vsi; i++)
  5332. if (pf->vsi[i] && pf->vsi[i]->netdev)
  5333. i40e_update_stats(pf->vsi[i]);
  5334. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  5335. /* Update the stats for the active switching components */
  5336. for (i = 0; i < I40E_MAX_VEB; i++)
  5337. if (pf->veb[i])
  5338. i40e_update_veb_stats(pf->veb[i]);
  5339. }
  5340. i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
  5341. }
  5342. /**
  5343. * i40e_reset_subtask - Set up for resetting the device and driver
  5344. * @pf: board private structure
  5345. **/
  5346. static void i40e_reset_subtask(struct i40e_pf *pf)
  5347. {
  5348. u32 reset_flags = 0;
  5349. rtnl_lock();
  5350. if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
  5351. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  5352. clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
  5353. }
  5354. if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
  5355. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  5356. clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5357. }
  5358. if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
  5359. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  5360. clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  5361. }
  5362. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
  5363. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  5364. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  5365. }
  5366. if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
  5367. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  5368. clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
  5369. }
  5370. /* If there's a recovery already waiting, it takes
  5371. * precedence before starting a new reset sequence.
  5372. */
  5373. if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
  5374. i40e_handle_reset_warning(pf);
  5375. goto unlock;
  5376. }
  5377. /* If we're already down or resetting, just bail */
  5378. if (reset_flags &&
  5379. !test_bit(__I40E_DOWN, &pf->state) &&
  5380. !test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5381. i40e_do_reset(pf, reset_flags);
  5382. unlock:
  5383. rtnl_unlock();
  5384. }
  5385. /**
  5386. * i40e_handle_link_event - Handle link event
  5387. * @pf: board private structure
  5388. * @e: event info posted on ARQ
  5389. **/
  5390. static void i40e_handle_link_event(struct i40e_pf *pf,
  5391. struct i40e_arq_event_info *e)
  5392. {
  5393. struct i40e_aqc_get_link_status *status =
  5394. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  5395. /* Do a new status request to re-enable LSE reporting
  5396. * and load new status information into the hw struct
  5397. * This completely ignores any state information
  5398. * in the ARQ event info, instead choosing to always
  5399. * issue the AQ update link status command.
  5400. */
  5401. i40e_link_event(pf);
  5402. /* check for unqualified module, if link is down */
  5403. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  5404. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  5405. (!(status->link_info & I40E_AQ_LINK_UP)))
  5406. dev_err(&pf->pdev->dev,
  5407. "The driver failed to link because an unqualified module was detected.\n");
  5408. }
  5409. /**
  5410. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  5411. * @pf: board private structure
  5412. **/
  5413. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  5414. {
  5415. struct i40e_arq_event_info event;
  5416. struct i40e_hw *hw = &pf->hw;
  5417. u16 pending, i = 0;
  5418. i40e_status ret;
  5419. u16 opcode;
  5420. u32 oldval;
  5421. u32 val;
  5422. /* Do not run clean AQ when PF reset fails */
  5423. if (test_bit(__I40E_RESET_FAILED, &pf->state))
  5424. return;
  5425. /* check for error indications */
  5426. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  5427. oldval = val;
  5428. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  5429. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  5430. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  5431. }
  5432. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  5433. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  5434. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  5435. }
  5436. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  5437. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  5438. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  5439. }
  5440. if (oldval != val)
  5441. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  5442. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  5443. oldval = val;
  5444. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  5445. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  5446. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  5447. }
  5448. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  5449. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  5450. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  5451. }
  5452. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  5453. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  5454. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  5455. }
  5456. if (oldval != val)
  5457. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  5458. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  5459. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  5460. if (!event.msg_buf)
  5461. return;
  5462. do {
  5463. ret = i40e_clean_arq_element(hw, &event, &pending);
  5464. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  5465. break;
  5466. else if (ret) {
  5467. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  5468. break;
  5469. }
  5470. opcode = le16_to_cpu(event.desc.opcode);
  5471. switch (opcode) {
  5472. case i40e_aqc_opc_get_link_status:
  5473. i40e_handle_link_event(pf, &event);
  5474. break;
  5475. case i40e_aqc_opc_send_msg_to_pf:
  5476. ret = i40e_vc_process_vf_msg(pf,
  5477. le16_to_cpu(event.desc.retval),
  5478. le32_to_cpu(event.desc.cookie_high),
  5479. le32_to_cpu(event.desc.cookie_low),
  5480. event.msg_buf,
  5481. event.msg_len);
  5482. break;
  5483. case i40e_aqc_opc_lldp_update_mib:
  5484. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  5485. #ifdef CONFIG_I40E_DCB
  5486. rtnl_lock();
  5487. ret = i40e_handle_lldp_event(pf, &event);
  5488. rtnl_unlock();
  5489. #endif /* CONFIG_I40E_DCB */
  5490. break;
  5491. case i40e_aqc_opc_event_lan_overflow:
  5492. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  5493. i40e_handle_lan_overflow_event(pf, &event);
  5494. break;
  5495. case i40e_aqc_opc_send_msg_to_peer:
  5496. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  5497. break;
  5498. case i40e_aqc_opc_nvm_erase:
  5499. case i40e_aqc_opc_nvm_update:
  5500. case i40e_aqc_opc_oem_post_update:
  5501. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "ARQ NVM operation completed\n");
  5502. break;
  5503. default:
  5504. dev_info(&pf->pdev->dev,
  5505. "ARQ Error: Unknown event 0x%04x received\n",
  5506. opcode);
  5507. break;
  5508. }
  5509. } while (pending && (i++ < pf->adminq_work_limit));
  5510. clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  5511. /* re-enable Admin queue interrupt cause */
  5512. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  5513. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  5514. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  5515. i40e_flush(hw);
  5516. kfree(event.msg_buf);
  5517. }
  5518. /**
  5519. * i40e_verify_eeprom - make sure eeprom is good to use
  5520. * @pf: board private structure
  5521. **/
  5522. static void i40e_verify_eeprom(struct i40e_pf *pf)
  5523. {
  5524. int err;
  5525. err = i40e_diag_eeprom_test(&pf->hw);
  5526. if (err) {
  5527. /* retry in case of garbage read */
  5528. err = i40e_diag_eeprom_test(&pf->hw);
  5529. if (err) {
  5530. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  5531. err);
  5532. set_bit(__I40E_BAD_EEPROM, &pf->state);
  5533. }
  5534. }
  5535. if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  5536. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  5537. clear_bit(__I40E_BAD_EEPROM, &pf->state);
  5538. }
  5539. }
  5540. /**
  5541. * i40e_enable_pf_switch_lb
  5542. * @pf: pointer to the PF structure
  5543. *
  5544. * enable switch loop back or die - no point in a return value
  5545. **/
  5546. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  5547. {
  5548. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5549. struct i40e_vsi_context ctxt;
  5550. int ret;
  5551. ctxt.seid = pf->main_vsi_seid;
  5552. ctxt.pf_num = pf->hw.pf_id;
  5553. ctxt.vf_num = 0;
  5554. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5555. if (ret) {
  5556. dev_info(&pf->pdev->dev,
  5557. "couldn't get PF vsi config, err %s aq_err %s\n",
  5558. i40e_stat_str(&pf->hw, ret),
  5559. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5560. return;
  5561. }
  5562. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5563. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5564. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5565. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5566. if (ret) {
  5567. dev_info(&pf->pdev->dev,
  5568. "update vsi switch failed, err %s aq_err %s\n",
  5569. i40e_stat_str(&pf->hw, ret),
  5570. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5571. }
  5572. }
  5573. /**
  5574. * i40e_disable_pf_switch_lb
  5575. * @pf: pointer to the PF structure
  5576. *
  5577. * disable switch loop back or die - no point in a return value
  5578. **/
  5579. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  5580. {
  5581. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5582. struct i40e_vsi_context ctxt;
  5583. int ret;
  5584. ctxt.seid = pf->main_vsi_seid;
  5585. ctxt.pf_num = pf->hw.pf_id;
  5586. ctxt.vf_num = 0;
  5587. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5588. if (ret) {
  5589. dev_info(&pf->pdev->dev,
  5590. "couldn't get PF vsi config, err %s aq_err %s\n",
  5591. i40e_stat_str(&pf->hw, ret),
  5592. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5593. return;
  5594. }
  5595. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5596. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5597. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5598. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5599. if (ret) {
  5600. dev_info(&pf->pdev->dev,
  5601. "update vsi switch failed, err %s aq_err %s\n",
  5602. i40e_stat_str(&pf->hw, ret),
  5603. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5604. }
  5605. }
  5606. /**
  5607. * i40e_config_bridge_mode - Configure the HW bridge mode
  5608. * @veb: pointer to the bridge instance
  5609. *
  5610. * Configure the loop back mode for the LAN VSI that is downlink to the
  5611. * specified HW bridge instance. It is expected this function is called
  5612. * when a new HW bridge is instantiated.
  5613. **/
  5614. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  5615. {
  5616. struct i40e_pf *pf = veb->pf;
  5617. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  5618. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  5619. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  5620. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  5621. i40e_disable_pf_switch_lb(pf);
  5622. else
  5623. i40e_enable_pf_switch_lb(pf);
  5624. }
  5625. /**
  5626. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  5627. * @veb: pointer to the VEB instance
  5628. *
  5629. * This is a recursive function that first builds the attached VSIs then
  5630. * recurses in to build the next layer of VEB. We track the connections
  5631. * through our own index numbers because the seid's from the HW could
  5632. * change across the reset.
  5633. **/
  5634. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  5635. {
  5636. struct i40e_vsi *ctl_vsi = NULL;
  5637. struct i40e_pf *pf = veb->pf;
  5638. int v, veb_idx;
  5639. int ret;
  5640. /* build VSI that owns this VEB, temporarily attached to base VEB */
  5641. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  5642. if (pf->vsi[v] &&
  5643. pf->vsi[v]->veb_idx == veb->idx &&
  5644. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  5645. ctl_vsi = pf->vsi[v];
  5646. break;
  5647. }
  5648. }
  5649. if (!ctl_vsi) {
  5650. dev_info(&pf->pdev->dev,
  5651. "missing owner VSI for veb_idx %d\n", veb->idx);
  5652. ret = -ENOENT;
  5653. goto end_reconstitute;
  5654. }
  5655. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  5656. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5657. ret = i40e_add_vsi(ctl_vsi);
  5658. if (ret) {
  5659. dev_info(&pf->pdev->dev,
  5660. "rebuild of veb_idx %d owner VSI failed: %d\n",
  5661. veb->idx, ret);
  5662. goto end_reconstitute;
  5663. }
  5664. i40e_vsi_reset_stats(ctl_vsi);
  5665. /* create the VEB in the switch and move the VSI onto the VEB */
  5666. ret = i40e_add_veb(veb, ctl_vsi);
  5667. if (ret)
  5668. goto end_reconstitute;
  5669. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  5670. veb->bridge_mode = BRIDGE_MODE_VEB;
  5671. else
  5672. veb->bridge_mode = BRIDGE_MODE_VEPA;
  5673. i40e_config_bridge_mode(veb);
  5674. /* create the remaining VSIs attached to this VEB */
  5675. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5676. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  5677. continue;
  5678. if (pf->vsi[v]->veb_idx == veb->idx) {
  5679. struct i40e_vsi *vsi = pf->vsi[v];
  5680. vsi->uplink_seid = veb->seid;
  5681. ret = i40e_add_vsi(vsi);
  5682. if (ret) {
  5683. dev_info(&pf->pdev->dev,
  5684. "rebuild of vsi_idx %d failed: %d\n",
  5685. v, ret);
  5686. goto end_reconstitute;
  5687. }
  5688. i40e_vsi_reset_stats(vsi);
  5689. }
  5690. }
  5691. /* create any VEBs attached to this VEB - RECURSION */
  5692. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  5693. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  5694. pf->veb[veb_idx]->uplink_seid = veb->seid;
  5695. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  5696. if (ret)
  5697. break;
  5698. }
  5699. }
  5700. end_reconstitute:
  5701. return ret;
  5702. }
  5703. /**
  5704. * i40e_get_capabilities - get info about the HW
  5705. * @pf: the PF struct
  5706. **/
  5707. static int i40e_get_capabilities(struct i40e_pf *pf)
  5708. {
  5709. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  5710. u16 data_size;
  5711. int buf_len;
  5712. int err;
  5713. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  5714. do {
  5715. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  5716. if (!cap_buf)
  5717. return -ENOMEM;
  5718. /* this loads the data into the hw struct for us */
  5719. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  5720. &data_size,
  5721. i40e_aqc_opc_list_func_capabilities,
  5722. NULL);
  5723. /* data loaded, buffer no longer needed */
  5724. kfree(cap_buf);
  5725. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  5726. /* retry with a larger buffer */
  5727. buf_len = data_size;
  5728. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  5729. dev_info(&pf->pdev->dev,
  5730. "capability discovery failed, err %s aq_err %s\n",
  5731. i40e_stat_str(&pf->hw, err),
  5732. i40e_aq_str(&pf->hw,
  5733. pf->hw.aq.asq_last_status));
  5734. return -ENODEV;
  5735. }
  5736. } while (err);
  5737. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  5738. dev_info(&pf->pdev->dev,
  5739. "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",
  5740. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  5741. pf->hw.func_caps.num_msix_vectors,
  5742. pf->hw.func_caps.num_msix_vectors_vf,
  5743. pf->hw.func_caps.fd_filters_guaranteed,
  5744. pf->hw.func_caps.fd_filters_best_effort,
  5745. pf->hw.func_caps.num_tx_qp,
  5746. pf->hw.func_caps.num_vsis);
  5747. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  5748. + pf->hw.func_caps.num_vfs)
  5749. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  5750. dev_info(&pf->pdev->dev,
  5751. "got num_vsis %d, setting num_vsis to %d\n",
  5752. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  5753. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  5754. }
  5755. return 0;
  5756. }
  5757. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  5758. /**
  5759. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  5760. * @pf: board private structure
  5761. **/
  5762. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  5763. {
  5764. struct i40e_vsi *vsi;
  5765. int i;
  5766. /* quick workaround for an NVM issue that leaves a critical register
  5767. * uninitialized
  5768. */
  5769. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  5770. static const u32 hkey[] = {
  5771. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  5772. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  5773. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  5774. 0x95b3a76d};
  5775. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  5776. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  5777. }
  5778. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  5779. return;
  5780. /* find existing VSI and see if it needs configuring */
  5781. vsi = NULL;
  5782. for (i = 0; i < pf->num_alloc_vsi; i++) {
  5783. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  5784. vsi = pf->vsi[i];
  5785. break;
  5786. }
  5787. }
  5788. /* create a new VSI if none exists */
  5789. if (!vsi) {
  5790. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  5791. pf->vsi[pf->lan_vsi]->seid, 0);
  5792. if (!vsi) {
  5793. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  5794. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5795. return;
  5796. }
  5797. }
  5798. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  5799. }
  5800. /**
  5801. * i40e_fdir_teardown - release the Flow Director resources
  5802. * @pf: board private structure
  5803. **/
  5804. static void i40e_fdir_teardown(struct i40e_pf *pf)
  5805. {
  5806. int i;
  5807. i40e_fdir_filter_exit(pf);
  5808. for (i = 0; i < pf->num_alloc_vsi; i++) {
  5809. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  5810. i40e_vsi_release(pf->vsi[i]);
  5811. break;
  5812. }
  5813. }
  5814. }
  5815. /**
  5816. * i40e_prep_for_reset - prep for the core to reset
  5817. * @pf: board private structure
  5818. *
  5819. * Close up the VFs and other things in prep for PF Reset.
  5820. **/
  5821. static void i40e_prep_for_reset(struct i40e_pf *pf)
  5822. {
  5823. struct i40e_hw *hw = &pf->hw;
  5824. i40e_status ret = 0;
  5825. u32 v;
  5826. clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  5827. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  5828. return;
  5829. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  5830. /* quiesce the VSIs and their queues that are not already DOWN */
  5831. i40e_pf_quiesce_all_vsi(pf);
  5832. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5833. if (pf->vsi[v])
  5834. pf->vsi[v]->seid = 0;
  5835. }
  5836. i40e_shutdown_adminq(&pf->hw);
  5837. /* call shutdown HMC */
  5838. if (hw->hmc.hmc_obj) {
  5839. ret = i40e_shutdown_lan_hmc(hw);
  5840. if (ret)
  5841. dev_warn(&pf->pdev->dev,
  5842. "shutdown_lan_hmc failed: %d\n", ret);
  5843. }
  5844. }
  5845. /**
  5846. * i40e_send_version - update firmware with driver version
  5847. * @pf: PF struct
  5848. */
  5849. static void i40e_send_version(struct i40e_pf *pf)
  5850. {
  5851. struct i40e_driver_version dv;
  5852. dv.major_version = DRV_VERSION_MAJOR;
  5853. dv.minor_version = DRV_VERSION_MINOR;
  5854. dv.build_version = DRV_VERSION_BUILD;
  5855. dv.subbuild_version = 0;
  5856. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  5857. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  5858. }
  5859. /**
  5860. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  5861. * @pf: board private structure
  5862. * @reinit: if the Main VSI needs to re-initialized.
  5863. **/
  5864. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
  5865. {
  5866. struct i40e_hw *hw = &pf->hw;
  5867. u8 set_fc_aq_fail = 0;
  5868. i40e_status ret;
  5869. u32 val;
  5870. u32 v;
  5871. /* Now we wait for GRST to settle out.
  5872. * We don't have to delete the VEBs or VSIs from the hw switch
  5873. * because the reset will make them disappear.
  5874. */
  5875. ret = i40e_pf_reset(hw);
  5876. if (ret) {
  5877. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  5878. set_bit(__I40E_RESET_FAILED, &pf->state);
  5879. goto clear_recovery;
  5880. }
  5881. pf->pfr_count++;
  5882. if (test_bit(__I40E_DOWN, &pf->state))
  5883. goto clear_recovery;
  5884. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  5885. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  5886. ret = i40e_init_adminq(&pf->hw);
  5887. if (ret) {
  5888. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  5889. i40e_stat_str(&pf->hw, ret),
  5890. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5891. goto clear_recovery;
  5892. }
  5893. /* re-verify the eeprom if we just had an EMP reset */
  5894. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
  5895. i40e_verify_eeprom(pf);
  5896. i40e_clear_pxe_mode(hw);
  5897. ret = i40e_get_capabilities(pf);
  5898. if (ret)
  5899. goto end_core_reset;
  5900. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  5901. hw->func_caps.num_rx_qp,
  5902. pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
  5903. if (ret) {
  5904. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  5905. goto end_core_reset;
  5906. }
  5907. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  5908. if (ret) {
  5909. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  5910. goto end_core_reset;
  5911. }
  5912. #ifdef CONFIG_I40E_DCB
  5913. ret = i40e_init_pf_dcb(pf);
  5914. if (ret) {
  5915. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  5916. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  5917. /* Continue without DCB enabled */
  5918. }
  5919. #endif /* CONFIG_I40E_DCB */
  5920. #ifdef I40E_FCOE
  5921. i40e_init_pf_fcoe(pf);
  5922. #endif
  5923. /* do basic switch setup */
  5924. ret = i40e_setup_pf_switch(pf, reinit);
  5925. if (ret)
  5926. goto end_core_reset;
  5927. /* driver is only interested in link up/down and module qualification
  5928. * reports from firmware
  5929. */
  5930. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  5931. I40E_AQ_EVENT_LINK_UPDOWN |
  5932. I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
  5933. if (ret)
  5934. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  5935. i40e_stat_str(&pf->hw, ret),
  5936. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5937. /* make sure our flow control settings are restored */
  5938. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  5939. if (ret)
  5940. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  5941. i40e_stat_str(&pf->hw, ret),
  5942. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5943. /* Rebuild the VSIs and VEBs that existed before reset.
  5944. * They are still in our local switch element arrays, so only
  5945. * need to rebuild the switch model in the HW.
  5946. *
  5947. * If there were VEBs but the reconstitution failed, we'll try
  5948. * try to recover minimal use by getting the basic PF VSI working.
  5949. */
  5950. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  5951. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  5952. /* find the one VEB connected to the MAC, and find orphans */
  5953. for (v = 0; v < I40E_MAX_VEB; v++) {
  5954. if (!pf->veb[v])
  5955. continue;
  5956. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  5957. pf->veb[v]->uplink_seid == 0) {
  5958. ret = i40e_reconstitute_veb(pf->veb[v]);
  5959. if (!ret)
  5960. continue;
  5961. /* If Main VEB failed, we're in deep doodoo,
  5962. * so give up rebuilding the switch and set up
  5963. * for minimal rebuild of PF VSI.
  5964. * If orphan failed, we'll report the error
  5965. * but try to keep going.
  5966. */
  5967. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  5968. dev_info(&pf->pdev->dev,
  5969. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  5970. ret);
  5971. pf->vsi[pf->lan_vsi]->uplink_seid
  5972. = pf->mac_seid;
  5973. break;
  5974. } else if (pf->veb[v]->uplink_seid == 0) {
  5975. dev_info(&pf->pdev->dev,
  5976. "rebuild of orphan VEB failed: %d\n",
  5977. ret);
  5978. }
  5979. }
  5980. }
  5981. }
  5982. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  5983. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  5984. /* no VEB, so rebuild only the Main VSI */
  5985. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  5986. if (ret) {
  5987. dev_info(&pf->pdev->dev,
  5988. "rebuild of Main VSI failed: %d\n", ret);
  5989. goto end_core_reset;
  5990. }
  5991. }
  5992. /* Reconfigure hardware for allowing smaller MSS in the case
  5993. * of TSO, so that we avoid the MDD being fired and causing
  5994. * a reset in the case of small MSS+TSO.
  5995. */
  5996. #define I40E_REG_MSS 0x000E64DC
  5997. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  5998. #define I40E_64BYTE_MSS 0x400000
  5999. val = rd32(hw, I40E_REG_MSS);
  6000. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  6001. val &= ~I40E_REG_MSS_MIN_MASK;
  6002. val |= I40E_64BYTE_MSS;
  6003. wr32(hw, I40E_REG_MSS, val);
  6004. }
  6005. if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  6006. (pf->hw.aq.fw_maj_ver < 4)) {
  6007. msleep(75);
  6008. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  6009. if (ret)
  6010. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  6011. i40e_stat_str(&pf->hw, ret),
  6012. i40e_aq_str(&pf->hw,
  6013. pf->hw.aq.asq_last_status));
  6014. }
  6015. /* reinit the misc interrupt */
  6016. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6017. ret = i40e_setup_misc_vector(pf);
  6018. /* Add a filter to drop all Flow control frames from any VSI from being
  6019. * transmitted. By doing so we stop a malicious VF from sending out
  6020. * PAUSE or PFC frames and potentially controlling traffic for other
  6021. * PF/VF VSIs.
  6022. * The FW can still send Flow control frames if enabled.
  6023. */
  6024. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  6025. pf->main_vsi_seid);
  6026. /* restart the VSIs that were rebuilt and running before the reset */
  6027. i40e_pf_unquiesce_all_vsi(pf);
  6028. if (pf->num_alloc_vfs) {
  6029. for (v = 0; v < pf->num_alloc_vfs; v++)
  6030. i40e_reset_vf(&pf->vf[v], true);
  6031. }
  6032. /* tell the firmware that we're starting */
  6033. i40e_send_version(pf);
  6034. end_core_reset:
  6035. clear_bit(__I40E_RESET_FAILED, &pf->state);
  6036. clear_recovery:
  6037. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  6038. }
  6039. /**
  6040. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  6041. * @pf: board private structure
  6042. *
  6043. * Close up the VFs and other things in prep for a Core Reset,
  6044. * then get ready to rebuild the world.
  6045. **/
  6046. static void i40e_handle_reset_warning(struct i40e_pf *pf)
  6047. {
  6048. i40e_prep_for_reset(pf);
  6049. i40e_reset_and_rebuild(pf, false);
  6050. }
  6051. /**
  6052. * i40e_handle_mdd_event
  6053. * @pf: pointer to the PF structure
  6054. *
  6055. * Called from the MDD irq handler to identify possibly malicious vfs
  6056. **/
  6057. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  6058. {
  6059. struct i40e_hw *hw = &pf->hw;
  6060. bool mdd_detected = false;
  6061. bool pf_mdd_detected = false;
  6062. struct i40e_vf *vf;
  6063. u32 reg;
  6064. int i;
  6065. if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
  6066. return;
  6067. /* find what triggered the MDD event */
  6068. reg = rd32(hw, I40E_GL_MDET_TX);
  6069. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  6070. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  6071. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  6072. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  6073. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  6074. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  6075. I40E_GL_MDET_TX_EVENT_SHIFT;
  6076. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  6077. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  6078. pf->hw.func_caps.base_queue;
  6079. if (netif_msg_tx_err(pf))
  6080. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  6081. event, queue, pf_num, vf_num);
  6082. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  6083. mdd_detected = true;
  6084. }
  6085. reg = rd32(hw, I40E_GL_MDET_RX);
  6086. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  6087. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  6088. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  6089. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  6090. I40E_GL_MDET_RX_EVENT_SHIFT;
  6091. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  6092. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  6093. pf->hw.func_caps.base_queue;
  6094. if (netif_msg_rx_err(pf))
  6095. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  6096. event, queue, func);
  6097. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  6098. mdd_detected = true;
  6099. }
  6100. if (mdd_detected) {
  6101. reg = rd32(hw, I40E_PF_MDET_TX);
  6102. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  6103. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  6104. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  6105. pf_mdd_detected = true;
  6106. }
  6107. reg = rd32(hw, I40E_PF_MDET_RX);
  6108. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  6109. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  6110. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  6111. pf_mdd_detected = true;
  6112. }
  6113. /* Queue belongs to the PF, initiate a reset */
  6114. if (pf_mdd_detected) {
  6115. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  6116. i40e_service_event_schedule(pf);
  6117. }
  6118. }
  6119. /* see if one of the VFs needs its hand slapped */
  6120. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  6121. vf = &(pf->vf[i]);
  6122. reg = rd32(hw, I40E_VP_MDET_TX(i));
  6123. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  6124. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  6125. vf->num_mdd_events++;
  6126. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  6127. i);
  6128. }
  6129. reg = rd32(hw, I40E_VP_MDET_RX(i));
  6130. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  6131. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  6132. vf->num_mdd_events++;
  6133. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  6134. i);
  6135. }
  6136. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  6137. dev_info(&pf->pdev->dev,
  6138. "Too many MDD events on VF %d, disabled\n", i);
  6139. dev_info(&pf->pdev->dev,
  6140. "Use PF Control I/F to re-enable the VF\n");
  6141. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  6142. }
  6143. }
  6144. /* re-enable mdd interrupt cause */
  6145. clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  6146. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  6147. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  6148. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  6149. i40e_flush(hw);
  6150. }
  6151. #ifdef CONFIG_I40E_VXLAN
  6152. /**
  6153. * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
  6154. * @pf: board private structure
  6155. **/
  6156. static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
  6157. {
  6158. struct i40e_hw *hw = &pf->hw;
  6159. i40e_status ret;
  6160. __be16 port;
  6161. int i;
  6162. if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
  6163. return;
  6164. pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
  6165. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6166. if (pf->pending_vxlan_bitmap & BIT_ULL(i)) {
  6167. pf->pending_vxlan_bitmap &= ~BIT_ULL(i);
  6168. port = pf->vxlan_ports[i];
  6169. if (port)
  6170. ret = i40e_aq_add_udp_tunnel(hw, ntohs(port),
  6171. I40E_AQC_TUNNEL_TYPE_VXLAN,
  6172. NULL, NULL);
  6173. else
  6174. ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
  6175. if (ret) {
  6176. dev_info(&pf->pdev->dev,
  6177. "%s vxlan port %d, index %d failed, err %s aq_err %s\n",
  6178. port ? "add" : "delete",
  6179. ntohs(port), i,
  6180. i40e_stat_str(&pf->hw, ret),
  6181. i40e_aq_str(&pf->hw,
  6182. pf->hw.aq.asq_last_status));
  6183. pf->vxlan_ports[i] = 0;
  6184. }
  6185. }
  6186. }
  6187. }
  6188. #endif
  6189. /**
  6190. * i40e_service_task - Run the driver's async subtasks
  6191. * @work: pointer to work_struct containing our data
  6192. **/
  6193. static void i40e_service_task(struct work_struct *work)
  6194. {
  6195. struct i40e_pf *pf = container_of(work,
  6196. struct i40e_pf,
  6197. service_task);
  6198. unsigned long start_time = jiffies;
  6199. /* don't bother with service tasks if a reset is in progress */
  6200. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  6201. i40e_service_event_complete(pf);
  6202. return;
  6203. }
  6204. i40e_detect_recover_hung(pf);
  6205. i40e_reset_subtask(pf);
  6206. i40e_handle_mdd_event(pf);
  6207. i40e_vc_process_vflr_event(pf);
  6208. i40e_watchdog_subtask(pf);
  6209. i40e_fdir_reinit_subtask(pf);
  6210. i40e_sync_filters_subtask(pf);
  6211. #ifdef CONFIG_I40E_VXLAN
  6212. i40e_sync_vxlan_filters_subtask(pf);
  6213. #endif
  6214. i40e_clean_adminq_subtask(pf);
  6215. i40e_service_event_complete(pf);
  6216. /* If the tasks have taken longer than one timer cycle or there
  6217. * is more work to be done, reschedule the service task now
  6218. * rather than wait for the timer to tick again.
  6219. */
  6220. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  6221. test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
  6222. test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
  6223. test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  6224. i40e_service_event_schedule(pf);
  6225. }
  6226. /**
  6227. * i40e_service_timer - timer callback
  6228. * @data: pointer to PF struct
  6229. **/
  6230. static void i40e_service_timer(unsigned long data)
  6231. {
  6232. struct i40e_pf *pf = (struct i40e_pf *)data;
  6233. mod_timer(&pf->service_timer,
  6234. round_jiffies(jiffies + pf->service_timer_period));
  6235. i40e_service_event_schedule(pf);
  6236. }
  6237. /**
  6238. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  6239. * @vsi: the VSI being configured
  6240. **/
  6241. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  6242. {
  6243. struct i40e_pf *pf = vsi->back;
  6244. switch (vsi->type) {
  6245. case I40E_VSI_MAIN:
  6246. vsi->alloc_queue_pairs = pf->num_lan_qps;
  6247. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6248. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6249. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6250. vsi->num_q_vectors = pf->num_lan_msix;
  6251. else
  6252. vsi->num_q_vectors = 1;
  6253. break;
  6254. case I40E_VSI_FDIR:
  6255. vsi->alloc_queue_pairs = 1;
  6256. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  6257. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6258. vsi->num_q_vectors = 1;
  6259. break;
  6260. case I40E_VSI_VMDQ2:
  6261. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  6262. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6263. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6264. vsi->num_q_vectors = pf->num_vmdq_msix;
  6265. break;
  6266. case I40E_VSI_SRIOV:
  6267. vsi->alloc_queue_pairs = pf->num_vf_qps;
  6268. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6269. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6270. break;
  6271. #ifdef I40E_FCOE
  6272. case I40E_VSI_FCOE:
  6273. vsi->alloc_queue_pairs = pf->num_fcoe_qps;
  6274. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6275. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6276. vsi->num_q_vectors = pf->num_fcoe_msix;
  6277. break;
  6278. #endif /* I40E_FCOE */
  6279. default:
  6280. WARN_ON(1);
  6281. return -ENODATA;
  6282. }
  6283. return 0;
  6284. }
  6285. /**
  6286. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  6287. * @type: VSI pointer
  6288. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  6289. *
  6290. * On error: returns error code (negative)
  6291. * On success: returns 0
  6292. **/
  6293. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  6294. {
  6295. int size;
  6296. int ret = 0;
  6297. /* allocate memory for both Tx and Rx ring pointers */
  6298. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
  6299. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  6300. if (!vsi->tx_rings)
  6301. return -ENOMEM;
  6302. vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
  6303. if (alloc_qvectors) {
  6304. /* allocate memory for q_vector pointers */
  6305. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  6306. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  6307. if (!vsi->q_vectors) {
  6308. ret = -ENOMEM;
  6309. goto err_vectors;
  6310. }
  6311. }
  6312. return ret;
  6313. err_vectors:
  6314. kfree(vsi->tx_rings);
  6315. return ret;
  6316. }
  6317. /**
  6318. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  6319. * @pf: board private structure
  6320. * @type: type of VSI
  6321. *
  6322. * On error: returns error code (negative)
  6323. * On success: returns vsi index in PF (positive)
  6324. **/
  6325. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  6326. {
  6327. int ret = -ENODEV;
  6328. struct i40e_vsi *vsi;
  6329. int vsi_idx;
  6330. int i;
  6331. /* Need to protect the allocation of the VSIs at the PF level */
  6332. mutex_lock(&pf->switch_mutex);
  6333. /* VSI list may be fragmented if VSI creation/destruction has
  6334. * been happening. We can afford to do a quick scan to look
  6335. * for any free VSIs in the list.
  6336. *
  6337. * find next empty vsi slot, looping back around if necessary
  6338. */
  6339. i = pf->next_vsi;
  6340. while (i < pf->num_alloc_vsi && pf->vsi[i])
  6341. i++;
  6342. if (i >= pf->num_alloc_vsi) {
  6343. i = 0;
  6344. while (i < pf->next_vsi && pf->vsi[i])
  6345. i++;
  6346. }
  6347. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  6348. vsi_idx = i; /* Found one! */
  6349. } else {
  6350. ret = -ENODEV;
  6351. goto unlock_pf; /* out of VSI slots! */
  6352. }
  6353. pf->next_vsi = ++i;
  6354. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  6355. if (!vsi) {
  6356. ret = -ENOMEM;
  6357. goto unlock_pf;
  6358. }
  6359. vsi->type = type;
  6360. vsi->back = pf;
  6361. set_bit(__I40E_DOWN, &vsi->state);
  6362. vsi->flags = 0;
  6363. vsi->idx = vsi_idx;
  6364. vsi->rx_itr_setting = pf->rx_itr_default;
  6365. vsi->tx_itr_setting = pf->tx_itr_default;
  6366. vsi->int_rate_limit = 0;
  6367. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  6368. pf->rss_table_size : 64;
  6369. vsi->netdev_registered = false;
  6370. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  6371. INIT_LIST_HEAD(&vsi->mac_filter_list);
  6372. vsi->irqs_ready = false;
  6373. ret = i40e_set_num_rings_in_vsi(vsi);
  6374. if (ret)
  6375. goto err_rings;
  6376. ret = i40e_vsi_alloc_arrays(vsi, true);
  6377. if (ret)
  6378. goto err_rings;
  6379. /* Setup default MSIX irq handler for VSI */
  6380. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  6381. /* Initialize VSI lock */
  6382. spin_lock_init(&vsi->mac_filter_list_lock);
  6383. pf->vsi[vsi_idx] = vsi;
  6384. ret = vsi_idx;
  6385. goto unlock_pf;
  6386. err_rings:
  6387. pf->next_vsi = i - 1;
  6388. kfree(vsi);
  6389. unlock_pf:
  6390. mutex_unlock(&pf->switch_mutex);
  6391. return ret;
  6392. }
  6393. /**
  6394. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  6395. * @type: VSI pointer
  6396. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  6397. *
  6398. * On error: returns error code (negative)
  6399. * On success: returns 0
  6400. **/
  6401. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  6402. {
  6403. /* free the ring and vector containers */
  6404. if (free_qvectors) {
  6405. kfree(vsi->q_vectors);
  6406. vsi->q_vectors = NULL;
  6407. }
  6408. kfree(vsi->tx_rings);
  6409. vsi->tx_rings = NULL;
  6410. vsi->rx_rings = NULL;
  6411. }
  6412. /**
  6413. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  6414. * and lookup table
  6415. * @vsi: Pointer to VSI structure
  6416. */
  6417. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  6418. {
  6419. if (!vsi)
  6420. return;
  6421. kfree(vsi->rss_hkey_user);
  6422. vsi->rss_hkey_user = NULL;
  6423. kfree(vsi->rss_lut_user);
  6424. vsi->rss_lut_user = NULL;
  6425. }
  6426. /**
  6427. * i40e_vsi_clear - Deallocate the VSI provided
  6428. * @vsi: the VSI being un-configured
  6429. **/
  6430. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  6431. {
  6432. struct i40e_pf *pf;
  6433. if (!vsi)
  6434. return 0;
  6435. if (!vsi->back)
  6436. goto free_vsi;
  6437. pf = vsi->back;
  6438. mutex_lock(&pf->switch_mutex);
  6439. if (!pf->vsi[vsi->idx]) {
  6440. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  6441. vsi->idx, vsi->idx, vsi, vsi->type);
  6442. goto unlock_vsi;
  6443. }
  6444. if (pf->vsi[vsi->idx] != vsi) {
  6445. dev_err(&pf->pdev->dev,
  6446. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  6447. pf->vsi[vsi->idx]->idx,
  6448. pf->vsi[vsi->idx],
  6449. pf->vsi[vsi->idx]->type,
  6450. vsi->idx, vsi, vsi->type);
  6451. goto unlock_vsi;
  6452. }
  6453. /* updates the PF for this cleared vsi */
  6454. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  6455. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  6456. i40e_vsi_free_arrays(vsi, true);
  6457. i40e_clear_rss_config_user(vsi);
  6458. pf->vsi[vsi->idx] = NULL;
  6459. if (vsi->idx < pf->next_vsi)
  6460. pf->next_vsi = vsi->idx;
  6461. unlock_vsi:
  6462. mutex_unlock(&pf->switch_mutex);
  6463. free_vsi:
  6464. kfree(vsi);
  6465. return 0;
  6466. }
  6467. /**
  6468. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  6469. * @vsi: the VSI being cleaned
  6470. **/
  6471. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  6472. {
  6473. int i;
  6474. if (vsi->tx_rings && vsi->tx_rings[0]) {
  6475. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6476. kfree_rcu(vsi->tx_rings[i], rcu);
  6477. vsi->tx_rings[i] = NULL;
  6478. vsi->rx_rings[i] = NULL;
  6479. }
  6480. }
  6481. }
  6482. /**
  6483. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  6484. * @vsi: the VSI being configured
  6485. **/
  6486. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  6487. {
  6488. struct i40e_ring *tx_ring, *rx_ring;
  6489. struct i40e_pf *pf = vsi->back;
  6490. int i;
  6491. /* Set basic values in the rings to be used later during open() */
  6492. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6493. /* allocate space for both Tx and Rx in one shot */
  6494. tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
  6495. if (!tx_ring)
  6496. goto err_out;
  6497. tx_ring->queue_index = i;
  6498. tx_ring->reg_idx = vsi->base_queue + i;
  6499. tx_ring->ring_active = false;
  6500. tx_ring->vsi = vsi;
  6501. tx_ring->netdev = vsi->netdev;
  6502. tx_ring->dev = &pf->pdev->dev;
  6503. tx_ring->count = vsi->num_desc;
  6504. tx_ring->size = 0;
  6505. tx_ring->dcb_tc = 0;
  6506. if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
  6507. tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  6508. if (vsi->back->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE)
  6509. tx_ring->flags |= I40E_TXR_FLAGS_OUTER_UDP_CSUM;
  6510. vsi->tx_rings[i] = tx_ring;
  6511. rx_ring = &tx_ring[1];
  6512. rx_ring->queue_index = i;
  6513. rx_ring->reg_idx = vsi->base_queue + i;
  6514. rx_ring->ring_active = false;
  6515. rx_ring->vsi = vsi;
  6516. rx_ring->netdev = vsi->netdev;
  6517. rx_ring->dev = &pf->pdev->dev;
  6518. rx_ring->count = vsi->num_desc;
  6519. rx_ring->size = 0;
  6520. rx_ring->dcb_tc = 0;
  6521. if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
  6522. set_ring_16byte_desc_enabled(rx_ring);
  6523. else
  6524. clear_ring_16byte_desc_enabled(rx_ring);
  6525. vsi->rx_rings[i] = rx_ring;
  6526. }
  6527. return 0;
  6528. err_out:
  6529. i40e_vsi_clear_rings(vsi);
  6530. return -ENOMEM;
  6531. }
  6532. /**
  6533. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  6534. * @pf: board private structure
  6535. * @vectors: the number of MSI-X vectors to request
  6536. *
  6537. * Returns the number of vectors reserved, or error
  6538. **/
  6539. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  6540. {
  6541. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  6542. I40E_MIN_MSIX, vectors);
  6543. if (vectors < 0) {
  6544. dev_info(&pf->pdev->dev,
  6545. "MSI-X vector reservation failed: %d\n", vectors);
  6546. vectors = 0;
  6547. }
  6548. return vectors;
  6549. }
  6550. /**
  6551. * i40e_init_msix - Setup the MSIX capability
  6552. * @pf: board private structure
  6553. *
  6554. * Work with the OS to set up the MSIX vectors needed.
  6555. *
  6556. * Returns the number of vectors reserved or negative on failure
  6557. **/
  6558. static int i40e_init_msix(struct i40e_pf *pf)
  6559. {
  6560. struct i40e_hw *hw = &pf->hw;
  6561. int vectors_left;
  6562. int v_budget, i;
  6563. int v_actual;
  6564. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  6565. return -ENODEV;
  6566. /* The number of vectors we'll request will be comprised of:
  6567. * - Add 1 for "other" cause for Admin Queue events, etc.
  6568. * - The number of LAN queue pairs
  6569. * - Queues being used for RSS.
  6570. * We don't need as many as max_rss_size vectors.
  6571. * use rss_size instead in the calculation since that
  6572. * is governed by number of cpus in the system.
  6573. * - assumes symmetric Tx/Rx pairing
  6574. * - The number of VMDq pairs
  6575. #ifdef I40E_FCOE
  6576. * - The number of FCOE qps.
  6577. #endif
  6578. * Once we count this up, try the request.
  6579. *
  6580. * If we can't get what we want, we'll simplify to nearly nothing
  6581. * and try again. If that still fails, we punt.
  6582. */
  6583. vectors_left = hw->func_caps.num_msix_vectors;
  6584. v_budget = 0;
  6585. /* reserve one vector for miscellaneous handler */
  6586. if (vectors_left) {
  6587. v_budget++;
  6588. vectors_left--;
  6589. }
  6590. /* reserve vectors for the main PF traffic queues */
  6591. pf->num_lan_msix = min_t(int, num_online_cpus(), vectors_left);
  6592. vectors_left -= pf->num_lan_msix;
  6593. v_budget += pf->num_lan_msix;
  6594. /* reserve one vector for sideband flow director */
  6595. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6596. if (vectors_left) {
  6597. v_budget++;
  6598. vectors_left--;
  6599. } else {
  6600. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6601. }
  6602. }
  6603. #ifdef I40E_FCOE
  6604. /* can we reserve enough for FCoE? */
  6605. if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
  6606. if (!vectors_left)
  6607. pf->num_fcoe_msix = 0;
  6608. else if (vectors_left >= pf->num_fcoe_qps)
  6609. pf->num_fcoe_msix = pf->num_fcoe_qps;
  6610. else
  6611. pf->num_fcoe_msix = 1;
  6612. v_budget += pf->num_fcoe_msix;
  6613. vectors_left -= pf->num_fcoe_msix;
  6614. }
  6615. #endif
  6616. /* any vectors left over go for VMDq support */
  6617. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  6618. int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
  6619. int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
  6620. /* if we're short on vectors for what's desired, we limit
  6621. * the queues per vmdq. If this is still more than are
  6622. * available, the user will need to change the number of
  6623. * queues/vectors used by the PF later with the ethtool
  6624. * channels command
  6625. */
  6626. if (vmdq_vecs < vmdq_vecs_wanted)
  6627. pf->num_vmdq_qps = 1;
  6628. pf->num_vmdq_msix = pf->num_vmdq_qps;
  6629. v_budget += vmdq_vecs;
  6630. vectors_left -= vmdq_vecs;
  6631. }
  6632. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  6633. GFP_KERNEL);
  6634. if (!pf->msix_entries)
  6635. return -ENOMEM;
  6636. for (i = 0; i < v_budget; i++)
  6637. pf->msix_entries[i].entry = i;
  6638. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  6639. if (v_actual != v_budget) {
  6640. /* If we have limited resources, we will start with no vectors
  6641. * for the special features and then allocate vectors to some
  6642. * of these features based on the policy and at the end disable
  6643. * the features that did not get any vectors.
  6644. */
  6645. #ifdef I40E_FCOE
  6646. pf->num_fcoe_qps = 0;
  6647. pf->num_fcoe_msix = 0;
  6648. #endif
  6649. pf->num_vmdq_msix = 0;
  6650. }
  6651. if (v_actual < I40E_MIN_MSIX) {
  6652. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  6653. kfree(pf->msix_entries);
  6654. pf->msix_entries = NULL;
  6655. return -ENODEV;
  6656. } else if (v_actual == I40E_MIN_MSIX) {
  6657. /* Adjust for minimal MSIX use */
  6658. pf->num_vmdq_vsis = 0;
  6659. pf->num_vmdq_qps = 0;
  6660. pf->num_lan_qps = 1;
  6661. pf->num_lan_msix = 1;
  6662. } else if (v_actual != v_budget) {
  6663. int vec;
  6664. /* reserve the misc vector */
  6665. vec = v_actual - 1;
  6666. /* Scale vector usage down */
  6667. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  6668. pf->num_vmdq_vsis = 1;
  6669. pf->num_vmdq_qps = 1;
  6670. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6671. /* partition out the remaining vectors */
  6672. switch (vec) {
  6673. case 2:
  6674. pf->num_lan_msix = 1;
  6675. break;
  6676. case 3:
  6677. #ifdef I40E_FCOE
  6678. /* give one vector to FCoE */
  6679. if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
  6680. pf->num_lan_msix = 1;
  6681. pf->num_fcoe_msix = 1;
  6682. }
  6683. #else
  6684. pf->num_lan_msix = 2;
  6685. #endif
  6686. break;
  6687. default:
  6688. #ifdef I40E_FCOE
  6689. /* give one vector to FCoE */
  6690. if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
  6691. pf->num_fcoe_msix = 1;
  6692. vec--;
  6693. }
  6694. #endif
  6695. /* give the rest to the PF */
  6696. pf->num_lan_msix = min_t(int, vec, pf->num_lan_qps);
  6697. break;
  6698. }
  6699. }
  6700. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  6701. (pf->num_vmdq_msix == 0)) {
  6702. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  6703. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  6704. }
  6705. #ifdef I40E_FCOE
  6706. if ((pf->flags & I40E_FLAG_FCOE_ENABLED) && (pf->num_fcoe_msix == 0)) {
  6707. dev_info(&pf->pdev->dev, "FCOE disabled, not enough MSI-X vectors\n");
  6708. pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
  6709. }
  6710. #endif
  6711. return v_actual;
  6712. }
  6713. /**
  6714. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  6715. * @vsi: the VSI being configured
  6716. * @v_idx: index of the vector in the vsi struct
  6717. *
  6718. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  6719. **/
  6720. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
  6721. {
  6722. struct i40e_q_vector *q_vector;
  6723. /* allocate q_vector */
  6724. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  6725. if (!q_vector)
  6726. return -ENOMEM;
  6727. q_vector->vsi = vsi;
  6728. q_vector->v_idx = v_idx;
  6729. cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
  6730. if (vsi->netdev)
  6731. netif_napi_add(vsi->netdev, &q_vector->napi,
  6732. i40e_napi_poll, NAPI_POLL_WEIGHT);
  6733. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  6734. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  6735. /* tie q_vector and vsi together */
  6736. vsi->q_vectors[v_idx] = q_vector;
  6737. return 0;
  6738. }
  6739. /**
  6740. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  6741. * @vsi: the VSI being configured
  6742. *
  6743. * We allocate one q_vector per queue interrupt. If allocation fails we
  6744. * return -ENOMEM.
  6745. **/
  6746. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  6747. {
  6748. struct i40e_pf *pf = vsi->back;
  6749. int v_idx, num_q_vectors;
  6750. int err;
  6751. /* if not MSIX, give the one vector only to the LAN VSI */
  6752. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6753. num_q_vectors = vsi->num_q_vectors;
  6754. else if (vsi == pf->vsi[pf->lan_vsi])
  6755. num_q_vectors = 1;
  6756. else
  6757. return -EINVAL;
  6758. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  6759. err = i40e_vsi_alloc_q_vector(vsi, v_idx);
  6760. if (err)
  6761. goto err_out;
  6762. }
  6763. return 0;
  6764. err_out:
  6765. while (v_idx--)
  6766. i40e_free_q_vector(vsi, v_idx);
  6767. return err;
  6768. }
  6769. /**
  6770. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  6771. * @pf: board private structure to initialize
  6772. **/
  6773. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  6774. {
  6775. int vectors = 0;
  6776. ssize_t size;
  6777. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  6778. vectors = i40e_init_msix(pf);
  6779. if (vectors < 0) {
  6780. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  6781. #ifdef I40E_FCOE
  6782. I40E_FLAG_FCOE_ENABLED |
  6783. #endif
  6784. I40E_FLAG_RSS_ENABLED |
  6785. I40E_FLAG_DCB_CAPABLE |
  6786. I40E_FLAG_SRIOV_ENABLED |
  6787. I40E_FLAG_FD_SB_ENABLED |
  6788. I40E_FLAG_FD_ATR_ENABLED |
  6789. I40E_FLAG_VMDQ_ENABLED);
  6790. /* rework the queue expectations without MSIX */
  6791. i40e_determine_queue_usage(pf);
  6792. }
  6793. }
  6794. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  6795. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  6796. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  6797. vectors = pci_enable_msi(pf->pdev);
  6798. if (vectors < 0) {
  6799. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  6800. vectors);
  6801. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  6802. }
  6803. vectors = 1; /* one MSI or Legacy vector */
  6804. }
  6805. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  6806. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  6807. /* set up vector assignment tracking */
  6808. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  6809. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  6810. if (!pf->irq_pile) {
  6811. dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
  6812. return -ENOMEM;
  6813. }
  6814. pf->irq_pile->num_entries = vectors;
  6815. pf->irq_pile->search_hint = 0;
  6816. /* track first vector for misc interrupts, ignore return */
  6817. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  6818. return 0;
  6819. }
  6820. /**
  6821. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  6822. * @pf: board private structure
  6823. *
  6824. * This sets up the handler for MSIX 0, which is used to manage the
  6825. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  6826. * when in MSI or Legacy interrupt mode.
  6827. **/
  6828. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  6829. {
  6830. struct i40e_hw *hw = &pf->hw;
  6831. int err = 0;
  6832. /* Only request the irq if this is the first time through, and
  6833. * not when we're rebuilding after a Reset
  6834. */
  6835. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  6836. err = request_irq(pf->msix_entries[0].vector,
  6837. i40e_intr, 0, pf->int_name, pf);
  6838. if (err) {
  6839. dev_info(&pf->pdev->dev,
  6840. "request_irq for %s failed: %d\n",
  6841. pf->int_name, err);
  6842. return -EFAULT;
  6843. }
  6844. }
  6845. i40e_enable_misc_int_causes(pf);
  6846. /* associate no queues to the misc vector */
  6847. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  6848. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  6849. i40e_flush(hw);
  6850. i40e_irq_dynamic_enable_icr0(pf);
  6851. return err;
  6852. }
  6853. /**
  6854. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  6855. * @vsi: vsi structure
  6856. * @seed: RSS hash seed
  6857. **/
  6858. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  6859. u8 *lut, u16 lut_size)
  6860. {
  6861. struct i40e_aqc_get_set_rss_key_data rss_key;
  6862. struct i40e_pf *pf = vsi->back;
  6863. struct i40e_hw *hw = &pf->hw;
  6864. bool pf_lut = false;
  6865. u8 *rss_lut;
  6866. int ret, i;
  6867. memset(&rss_key, 0, sizeof(rss_key));
  6868. memcpy(&rss_key, seed, sizeof(rss_key));
  6869. rss_lut = kzalloc(pf->rss_table_size, GFP_KERNEL);
  6870. if (!rss_lut)
  6871. return -ENOMEM;
  6872. /* Populate the LUT with max no. of queues in round robin fashion */
  6873. for (i = 0; i < vsi->rss_table_size; i++)
  6874. rss_lut[i] = i % vsi->rss_size;
  6875. ret = i40e_aq_set_rss_key(hw, vsi->id, &rss_key);
  6876. if (ret) {
  6877. dev_info(&pf->pdev->dev,
  6878. "Cannot set RSS key, err %s aq_err %s\n",
  6879. i40e_stat_str(&pf->hw, ret),
  6880. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6881. goto config_rss_aq_out;
  6882. }
  6883. if (vsi->type == I40E_VSI_MAIN)
  6884. pf_lut = true;
  6885. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, rss_lut,
  6886. vsi->rss_table_size);
  6887. if (ret)
  6888. dev_info(&pf->pdev->dev,
  6889. "Cannot set RSS lut, err %s aq_err %s\n",
  6890. i40e_stat_str(&pf->hw, ret),
  6891. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6892. config_rss_aq_out:
  6893. kfree(rss_lut);
  6894. return ret;
  6895. }
  6896. /**
  6897. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  6898. * @vsi: VSI structure
  6899. **/
  6900. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  6901. {
  6902. u8 seed[I40E_HKEY_ARRAY_SIZE];
  6903. struct i40e_pf *pf = vsi->back;
  6904. u8 *lut;
  6905. int ret;
  6906. if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
  6907. return 0;
  6908. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  6909. if (!lut)
  6910. return -ENOMEM;
  6911. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  6912. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  6913. vsi->rss_size = min_t(int, pf->alloc_rss_size, vsi->num_queue_pairs);
  6914. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  6915. kfree(lut);
  6916. return ret;
  6917. }
  6918. /**
  6919. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  6920. * @vsi: Pointer to vsi structure
  6921. * @seed: RSS hash seed
  6922. * @lut: Lookup table
  6923. * @lut_size: Lookup table size
  6924. *
  6925. * Returns 0 on success, negative on failure
  6926. **/
  6927. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  6928. const u8 *lut, u16 lut_size)
  6929. {
  6930. struct i40e_pf *pf = vsi->back;
  6931. struct i40e_hw *hw = &pf->hw;
  6932. u8 i;
  6933. /* Fill out hash function seed */
  6934. if (seed) {
  6935. u32 *seed_dw = (u32 *)seed;
  6936. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  6937. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  6938. }
  6939. if (lut) {
  6940. u32 *lut_dw = (u32 *)lut;
  6941. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  6942. return -EINVAL;
  6943. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  6944. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  6945. }
  6946. i40e_flush(hw);
  6947. return 0;
  6948. }
  6949. /**
  6950. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  6951. * @vsi: Pointer to VSI structure
  6952. * @seed: Buffer to store the keys
  6953. * @lut: Buffer to store the lookup table entries
  6954. * @lut_size: Size of buffer to store the lookup table entries
  6955. *
  6956. * Returns 0 on success, negative on failure
  6957. */
  6958. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  6959. u8 *lut, u16 lut_size)
  6960. {
  6961. struct i40e_pf *pf = vsi->back;
  6962. struct i40e_hw *hw = &pf->hw;
  6963. u16 i;
  6964. if (seed) {
  6965. u32 *seed_dw = (u32 *)seed;
  6966. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  6967. seed_dw[i] = rd32(hw, I40E_PFQF_HKEY(i));
  6968. }
  6969. if (lut) {
  6970. u32 *lut_dw = (u32 *)lut;
  6971. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  6972. return -EINVAL;
  6973. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  6974. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  6975. }
  6976. return 0;
  6977. }
  6978. /**
  6979. * i40e_config_rss - Configure RSS keys and lut
  6980. * @vsi: Pointer to VSI structure
  6981. * @seed: RSS hash seed
  6982. * @lut: Lookup table
  6983. * @lut_size: Lookup table size
  6984. *
  6985. * Returns 0 on success, negative on failure
  6986. */
  6987. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  6988. {
  6989. struct i40e_pf *pf = vsi->back;
  6990. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  6991. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  6992. else
  6993. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  6994. }
  6995. /**
  6996. * i40e_get_rss - Get RSS keys and lut
  6997. * @vsi: Pointer to VSI structure
  6998. * @seed: Buffer to store the keys
  6999. * @lut: Buffer to store the lookup table entries
  7000. * lut_size: Size of buffer to store the lookup table entries
  7001. *
  7002. * Returns 0 on success, negative on failure
  7003. */
  7004. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7005. {
  7006. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  7007. }
  7008. /**
  7009. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  7010. * @pf: Pointer to board private structure
  7011. * @lut: Lookup table
  7012. * @rss_table_size: Lookup table size
  7013. * @rss_size: Range of queue number for hashing
  7014. */
  7015. static void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  7016. u16 rss_table_size, u16 rss_size)
  7017. {
  7018. u16 i;
  7019. for (i = 0; i < rss_table_size; i++)
  7020. lut[i] = i % rss_size;
  7021. }
  7022. /**
  7023. * i40e_pf_config_rss - Prepare for RSS if used
  7024. * @pf: board private structure
  7025. **/
  7026. static int i40e_pf_config_rss(struct i40e_pf *pf)
  7027. {
  7028. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7029. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7030. u8 *lut;
  7031. struct i40e_hw *hw = &pf->hw;
  7032. u32 reg_val;
  7033. u64 hena;
  7034. int ret;
  7035. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  7036. hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
  7037. ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
  7038. hena |= i40e_pf_get_default_rss_hena(pf);
  7039. wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
  7040. wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  7041. /* Determine the RSS table size based on the hardware capabilities */
  7042. reg_val = rd32(hw, I40E_PFQF_CTL_0);
  7043. reg_val = (pf->rss_table_size == 512) ?
  7044. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  7045. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  7046. wr32(hw, I40E_PFQF_CTL_0, reg_val);
  7047. /* Determine the RSS size of the VSI */
  7048. if (!vsi->rss_size)
  7049. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7050. vsi->num_queue_pairs);
  7051. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7052. if (!lut)
  7053. return -ENOMEM;
  7054. /* Use user configured lut if there is one, otherwise use default */
  7055. if (vsi->rss_lut_user)
  7056. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7057. else
  7058. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7059. /* Use user configured hash key if there is one, otherwise
  7060. * use default.
  7061. */
  7062. if (vsi->rss_hkey_user)
  7063. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7064. else
  7065. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7066. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  7067. kfree(lut);
  7068. return ret;
  7069. }
  7070. /**
  7071. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  7072. * @pf: board private structure
  7073. * @queue_count: the requested queue count for rss.
  7074. *
  7075. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  7076. * count which may be different from the requested queue count.
  7077. **/
  7078. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  7079. {
  7080. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7081. int new_rss_size;
  7082. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  7083. return 0;
  7084. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  7085. if (queue_count != vsi->num_queue_pairs) {
  7086. vsi->req_queue_pairs = queue_count;
  7087. i40e_prep_for_reset(pf);
  7088. pf->alloc_rss_size = new_rss_size;
  7089. i40e_reset_and_rebuild(pf, true);
  7090. /* Discard the user configured hash keys and lut, if less
  7091. * queues are enabled.
  7092. */
  7093. if (queue_count < vsi->rss_size) {
  7094. i40e_clear_rss_config_user(vsi);
  7095. dev_dbg(&pf->pdev->dev,
  7096. "discard user configured hash keys and lut\n");
  7097. }
  7098. /* Reset vsi->rss_size, as number of enabled queues changed */
  7099. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7100. vsi->num_queue_pairs);
  7101. i40e_pf_config_rss(pf);
  7102. }
  7103. dev_info(&pf->pdev->dev, "RSS count/HW max RSS count: %d/%d\n",
  7104. pf->alloc_rss_size, pf->rss_size_max);
  7105. return pf->alloc_rss_size;
  7106. }
  7107. /**
  7108. * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
  7109. * @pf: board private structure
  7110. **/
  7111. i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
  7112. {
  7113. i40e_status status;
  7114. bool min_valid, max_valid;
  7115. u32 max_bw, min_bw;
  7116. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  7117. &min_valid, &max_valid);
  7118. if (!status) {
  7119. if (min_valid)
  7120. pf->npar_min_bw = min_bw;
  7121. if (max_valid)
  7122. pf->npar_max_bw = max_bw;
  7123. }
  7124. return status;
  7125. }
  7126. /**
  7127. * i40e_set_npar_bw_setting - Set BW settings for this PF partition
  7128. * @pf: board private structure
  7129. **/
  7130. i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
  7131. {
  7132. struct i40e_aqc_configure_partition_bw_data bw_data;
  7133. i40e_status status;
  7134. /* Set the valid bit for this PF */
  7135. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  7136. bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
  7137. bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
  7138. /* Set the new bandwidths */
  7139. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  7140. return status;
  7141. }
  7142. /**
  7143. * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
  7144. * @pf: board private structure
  7145. **/
  7146. i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
  7147. {
  7148. /* Commit temporary BW setting to permanent NVM image */
  7149. enum i40e_admin_queue_err last_aq_status;
  7150. i40e_status ret;
  7151. u16 nvm_word;
  7152. if (pf->hw.partition_id != 1) {
  7153. dev_info(&pf->pdev->dev,
  7154. "Commit BW only works on partition 1! This is partition %d",
  7155. pf->hw.partition_id);
  7156. ret = I40E_NOT_SUPPORTED;
  7157. goto bw_commit_out;
  7158. }
  7159. /* Acquire NVM for read access */
  7160. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  7161. last_aq_status = pf->hw.aq.asq_last_status;
  7162. if (ret) {
  7163. dev_info(&pf->pdev->dev,
  7164. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  7165. i40e_stat_str(&pf->hw, ret),
  7166. i40e_aq_str(&pf->hw, last_aq_status));
  7167. goto bw_commit_out;
  7168. }
  7169. /* Read word 0x10 of NVM - SW compatibility word 1 */
  7170. ret = i40e_aq_read_nvm(&pf->hw,
  7171. I40E_SR_NVM_CONTROL_WORD,
  7172. 0x10, sizeof(nvm_word), &nvm_word,
  7173. false, NULL);
  7174. /* Save off last admin queue command status before releasing
  7175. * the NVM
  7176. */
  7177. last_aq_status = pf->hw.aq.asq_last_status;
  7178. i40e_release_nvm(&pf->hw);
  7179. if (ret) {
  7180. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  7181. i40e_stat_str(&pf->hw, ret),
  7182. i40e_aq_str(&pf->hw, last_aq_status));
  7183. goto bw_commit_out;
  7184. }
  7185. /* Wait a bit for NVM release to complete */
  7186. msleep(50);
  7187. /* Acquire NVM for write access */
  7188. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  7189. last_aq_status = pf->hw.aq.asq_last_status;
  7190. if (ret) {
  7191. dev_info(&pf->pdev->dev,
  7192. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  7193. i40e_stat_str(&pf->hw, ret),
  7194. i40e_aq_str(&pf->hw, last_aq_status));
  7195. goto bw_commit_out;
  7196. }
  7197. /* Write it back out unchanged to initiate update NVM,
  7198. * which will force a write of the shadow (alt) RAM to
  7199. * the NVM - thus storing the bandwidth values permanently.
  7200. */
  7201. ret = i40e_aq_update_nvm(&pf->hw,
  7202. I40E_SR_NVM_CONTROL_WORD,
  7203. 0x10, sizeof(nvm_word),
  7204. &nvm_word, true, NULL);
  7205. /* Save off last admin queue command status before releasing
  7206. * the NVM
  7207. */
  7208. last_aq_status = pf->hw.aq.asq_last_status;
  7209. i40e_release_nvm(&pf->hw);
  7210. if (ret)
  7211. dev_info(&pf->pdev->dev,
  7212. "BW settings NOT SAVED, err %s aq_err %s\n",
  7213. i40e_stat_str(&pf->hw, ret),
  7214. i40e_aq_str(&pf->hw, last_aq_status));
  7215. bw_commit_out:
  7216. return ret;
  7217. }
  7218. /**
  7219. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  7220. * @pf: board private structure to initialize
  7221. *
  7222. * i40e_sw_init initializes the Adapter private data structure.
  7223. * Fields are initialized based on PCI device information and
  7224. * OS network device settings (MTU size).
  7225. **/
  7226. static int i40e_sw_init(struct i40e_pf *pf)
  7227. {
  7228. int err = 0;
  7229. int size;
  7230. pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
  7231. (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
  7232. pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
  7233. if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
  7234. if (I40E_DEBUG_USER & debug)
  7235. pf->hw.debug_mask = debug;
  7236. pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
  7237. I40E_DEFAULT_MSG_ENABLE);
  7238. }
  7239. /* Set default capability flags */
  7240. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  7241. I40E_FLAG_MSI_ENABLED |
  7242. I40E_FLAG_LINK_POLLING_ENABLED |
  7243. I40E_FLAG_MSIX_ENABLED;
  7244. if (iommu_present(&pci_bus_type))
  7245. pf->flags |= I40E_FLAG_RX_PS_ENABLED;
  7246. else
  7247. pf->flags |= I40E_FLAG_RX_1BUF_ENABLED;
  7248. /* Set default ITR */
  7249. pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
  7250. pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
  7251. /* Depending on PF configurations, it is possible that the RSS
  7252. * maximum might end up larger than the available queues
  7253. */
  7254. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  7255. pf->alloc_rss_size = 1;
  7256. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  7257. pf->rss_size_max = min_t(int, pf->rss_size_max,
  7258. pf->hw.func_caps.num_tx_qp);
  7259. if (pf->hw.func_caps.rss) {
  7260. pf->flags |= I40E_FLAG_RSS_ENABLED;
  7261. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  7262. num_online_cpus());
  7263. }
  7264. /* MFP mode enabled */
  7265. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  7266. pf->flags |= I40E_FLAG_MFP_ENABLED;
  7267. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  7268. if (i40e_get_npar_bw_setting(pf))
  7269. dev_warn(&pf->pdev->dev,
  7270. "Could not get NPAR bw settings\n");
  7271. else
  7272. dev_info(&pf->pdev->dev,
  7273. "Min BW = %8.8x, Max BW = %8.8x\n",
  7274. pf->npar_min_bw, pf->npar_max_bw);
  7275. }
  7276. /* FW/NVM is not yet fixed in this regard */
  7277. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  7278. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  7279. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  7280. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  7281. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  7282. pf->hw.num_partitions > 1)
  7283. dev_info(&pf->pdev->dev,
  7284. "Flow Director Sideband mode Disabled in MFP mode\n");
  7285. else
  7286. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7287. pf->fdir_pf_filter_count =
  7288. pf->hw.func_caps.fd_filters_guaranteed;
  7289. pf->hw.fdir_shared_filter_count =
  7290. pf->hw.func_caps.fd_filters_best_effort;
  7291. }
  7292. if (pf->hw.func_caps.vmdq) {
  7293. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  7294. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  7295. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  7296. }
  7297. #ifdef I40E_FCOE
  7298. i40e_init_pf_fcoe(pf);
  7299. #endif /* I40E_FCOE */
  7300. #ifdef CONFIG_PCI_IOV
  7301. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  7302. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  7303. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  7304. pf->num_req_vfs = min_t(int,
  7305. pf->hw.func_caps.num_vfs,
  7306. I40E_MAX_VF_COUNT);
  7307. }
  7308. #endif /* CONFIG_PCI_IOV */
  7309. if (pf->hw.mac.type == I40E_MAC_X722) {
  7310. pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE |
  7311. I40E_FLAG_128_QP_RSS_CAPABLE |
  7312. I40E_FLAG_HW_ATR_EVICT_CAPABLE |
  7313. I40E_FLAG_OUTER_UDP_CSUM_CAPABLE |
  7314. I40E_FLAG_WB_ON_ITR_CAPABLE |
  7315. I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE;
  7316. }
  7317. pf->eeprom_version = 0xDEAD;
  7318. pf->lan_veb = I40E_NO_VEB;
  7319. pf->lan_vsi = I40E_NO_VSI;
  7320. /* By default FW has this off for performance reasons */
  7321. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  7322. /* set up queue assignment tracking */
  7323. size = sizeof(struct i40e_lump_tracking)
  7324. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  7325. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  7326. if (!pf->qp_pile) {
  7327. err = -ENOMEM;
  7328. goto sw_init_done;
  7329. }
  7330. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  7331. pf->qp_pile->search_hint = 0;
  7332. pf->tx_timeout_recovery_level = 1;
  7333. mutex_init(&pf->switch_mutex);
  7334. /* If NPAR is enabled nudge the Tx scheduler */
  7335. if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
  7336. i40e_set_npar_bw_setting(pf);
  7337. sw_init_done:
  7338. return err;
  7339. }
  7340. /**
  7341. * i40e_set_ntuple - set the ntuple feature flag and take action
  7342. * @pf: board private structure to initialize
  7343. * @features: the feature set that the stack is suggesting
  7344. *
  7345. * returns a bool to indicate if reset needs to happen
  7346. **/
  7347. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  7348. {
  7349. bool need_reset = false;
  7350. /* Check if Flow Director n-tuple support was enabled or disabled. If
  7351. * the state changed, we need to reset.
  7352. */
  7353. if (features & NETIF_F_NTUPLE) {
  7354. /* Enable filters and mark for reset */
  7355. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7356. need_reset = true;
  7357. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7358. } else {
  7359. /* turn off filters, mark for reset and clear SW filter list */
  7360. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7361. need_reset = true;
  7362. i40e_fdir_filter_exit(pf);
  7363. }
  7364. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7365. pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7366. /* reset fd counters */
  7367. pf->fd_add_err = pf->fd_atr_cnt = pf->fd_tcp_rule = 0;
  7368. pf->fdir_pf_active_filters = 0;
  7369. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  7370. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7371. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  7372. /* if ATR was auto disabled it can be re-enabled. */
  7373. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7374. (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
  7375. pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  7376. }
  7377. return need_reset;
  7378. }
  7379. /**
  7380. * i40e_set_features - set the netdev feature flags
  7381. * @netdev: ptr to the netdev being adjusted
  7382. * @features: the feature set that the stack is suggesting
  7383. **/
  7384. static int i40e_set_features(struct net_device *netdev,
  7385. netdev_features_t features)
  7386. {
  7387. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7388. struct i40e_vsi *vsi = np->vsi;
  7389. struct i40e_pf *pf = vsi->back;
  7390. bool need_reset;
  7391. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  7392. i40e_vlan_stripping_enable(vsi);
  7393. else
  7394. i40e_vlan_stripping_disable(vsi);
  7395. need_reset = i40e_set_ntuple(pf, features);
  7396. if (need_reset)
  7397. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  7398. return 0;
  7399. }
  7400. #ifdef CONFIG_I40E_VXLAN
  7401. /**
  7402. * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
  7403. * @pf: board private structure
  7404. * @port: The UDP port to look up
  7405. *
  7406. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  7407. **/
  7408. static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
  7409. {
  7410. u8 i;
  7411. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  7412. if (pf->vxlan_ports[i] == port)
  7413. return i;
  7414. }
  7415. return i;
  7416. }
  7417. /**
  7418. * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
  7419. * @netdev: This physical port's netdev
  7420. * @sa_family: Socket Family that VXLAN is notifying us about
  7421. * @port: New UDP port number that VXLAN started listening to
  7422. **/
  7423. static void i40e_add_vxlan_port(struct net_device *netdev,
  7424. sa_family_t sa_family, __be16 port)
  7425. {
  7426. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7427. struct i40e_vsi *vsi = np->vsi;
  7428. struct i40e_pf *pf = vsi->back;
  7429. u8 next_idx;
  7430. u8 idx;
  7431. if (sa_family == AF_INET6)
  7432. return;
  7433. idx = i40e_get_vxlan_port_idx(pf, port);
  7434. /* Check if port already exists */
  7435. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7436. netdev_info(netdev, "vxlan port %d already offloaded\n",
  7437. ntohs(port));
  7438. return;
  7439. }
  7440. /* Now check if there is space to add the new port */
  7441. next_idx = i40e_get_vxlan_port_idx(pf, 0);
  7442. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7443. netdev_info(netdev, "maximum number of vxlan UDP ports reached, not adding port %d\n",
  7444. ntohs(port));
  7445. return;
  7446. }
  7447. /* New port: add it and mark its index in the bitmap */
  7448. pf->vxlan_ports[next_idx] = port;
  7449. pf->pending_vxlan_bitmap |= BIT_ULL(next_idx);
  7450. pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
  7451. }
  7452. /**
  7453. * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
  7454. * @netdev: This physical port's netdev
  7455. * @sa_family: Socket Family that VXLAN is notifying us about
  7456. * @port: UDP port number that VXLAN stopped listening to
  7457. **/
  7458. static void i40e_del_vxlan_port(struct net_device *netdev,
  7459. sa_family_t sa_family, __be16 port)
  7460. {
  7461. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7462. struct i40e_vsi *vsi = np->vsi;
  7463. struct i40e_pf *pf = vsi->back;
  7464. u8 idx;
  7465. if (sa_family == AF_INET6)
  7466. return;
  7467. idx = i40e_get_vxlan_port_idx(pf, port);
  7468. /* Check if port already exists */
  7469. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7470. /* if port exists, set it to 0 (mark for deletion)
  7471. * and make it pending
  7472. */
  7473. pf->vxlan_ports[idx] = 0;
  7474. pf->pending_vxlan_bitmap |= BIT_ULL(idx);
  7475. pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
  7476. } else {
  7477. netdev_warn(netdev, "vxlan port %d was not found, not deleting\n",
  7478. ntohs(port));
  7479. }
  7480. }
  7481. #endif
  7482. static int i40e_get_phys_port_id(struct net_device *netdev,
  7483. struct netdev_phys_item_id *ppid)
  7484. {
  7485. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7486. struct i40e_pf *pf = np->vsi->back;
  7487. struct i40e_hw *hw = &pf->hw;
  7488. if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
  7489. return -EOPNOTSUPP;
  7490. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  7491. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  7492. return 0;
  7493. }
  7494. /**
  7495. * i40e_ndo_fdb_add - add an entry to the hardware database
  7496. * @ndm: the input from the stack
  7497. * @tb: pointer to array of nladdr (unused)
  7498. * @dev: the net device pointer
  7499. * @addr: the MAC address entry being added
  7500. * @flags: instructions from stack about fdb operation
  7501. */
  7502. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  7503. struct net_device *dev,
  7504. const unsigned char *addr, u16 vid,
  7505. u16 flags)
  7506. {
  7507. struct i40e_netdev_priv *np = netdev_priv(dev);
  7508. struct i40e_pf *pf = np->vsi->back;
  7509. int err = 0;
  7510. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  7511. return -EOPNOTSUPP;
  7512. if (vid) {
  7513. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  7514. return -EINVAL;
  7515. }
  7516. /* Hardware does not support aging addresses so if a
  7517. * ndm_state is given only allow permanent addresses
  7518. */
  7519. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  7520. netdev_info(dev, "FDB only supports static addresses\n");
  7521. return -EINVAL;
  7522. }
  7523. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  7524. err = dev_uc_add_excl(dev, addr);
  7525. else if (is_multicast_ether_addr(addr))
  7526. err = dev_mc_add_excl(dev, addr);
  7527. else
  7528. err = -EINVAL;
  7529. /* Only return duplicate errors if NLM_F_EXCL is set */
  7530. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  7531. err = 0;
  7532. return err;
  7533. }
  7534. /**
  7535. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  7536. * @dev: the netdev being configured
  7537. * @nlh: RTNL message
  7538. *
  7539. * Inserts a new hardware bridge if not already created and
  7540. * enables the bridging mode requested (VEB or VEPA). If the
  7541. * hardware bridge has already been inserted and the request
  7542. * is to change the mode then that requires a PF reset to
  7543. * allow rebuild of the components with required hardware
  7544. * bridge mode enabled.
  7545. **/
  7546. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  7547. struct nlmsghdr *nlh,
  7548. u16 flags)
  7549. {
  7550. struct i40e_netdev_priv *np = netdev_priv(dev);
  7551. struct i40e_vsi *vsi = np->vsi;
  7552. struct i40e_pf *pf = vsi->back;
  7553. struct i40e_veb *veb = NULL;
  7554. struct nlattr *attr, *br_spec;
  7555. int i, rem;
  7556. /* Only for PF VSI for now */
  7557. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  7558. return -EOPNOTSUPP;
  7559. /* Find the HW bridge for PF VSI */
  7560. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  7561. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  7562. veb = pf->veb[i];
  7563. }
  7564. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  7565. nla_for_each_nested(attr, br_spec, rem) {
  7566. __u16 mode;
  7567. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  7568. continue;
  7569. mode = nla_get_u16(attr);
  7570. if ((mode != BRIDGE_MODE_VEPA) &&
  7571. (mode != BRIDGE_MODE_VEB))
  7572. return -EINVAL;
  7573. /* Insert a new HW bridge */
  7574. if (!veb) {
  7575. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  7576. vsi->tc_config.enabled_tc);
  7577. if (veb) {
  7578. veb->bridge_mode = mode;
  7579. i40e_config_bridge_mode(veb);
  7580. } else {
  7581. /* No Bridge HW offload available */
  7582. return -ENOENT;
  7583. }
  7584. break;
  7585. } else if (mode != veb->bridge_mode) {
  7586. /* Existing HW bridge but different mode needs reset */
  7587. veb->bridge_mode = mode;
  7588. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  7589. if (mode == BRIDGE_MODE_VEB)
  7590. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  7591. else
  7592. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  7593. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  7594. break;
  7595. }
  7596. }
  7597. return 0;
  7598. }
  7599. /**
  7600. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  7601. * @skb: skb buff
  7602. * @pid: process id
  7603. * @seq: RTNL message seq #
  7604. * @dev: the netdev being configured
  7605. * @filter_mask: unused
  7606. * @nlflags: netlink flags passed in
  7607. *
  7608. * Return the mode in which the hardware bridge is operating in
  7609. * i.e VEB or VEPA.
  7610. **/
  7611. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  7612. struct net_device *dev,
  7613. u32 __always_unused filter_mask,
  7614. int nlflags)
  7615. {
  7616. struct i40e_netdev_priv *np = netdev_priv(dev);
  7617. struct i40e_vsi *vsi = np->vsi;
  7618. struct i40e_pf *pf = vsi->back;
  7619. struct i40e_veb *veb = NULL;
  7620. int i;
  7621. /* Only for PF VSI for now */
  7622. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  7623. return -EOPNOTSUPP;
  7624. /* Find the HW bridge for the PF VSI */
  7625. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  7626. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  7627. veb = pf->veb[i];
  7628. }
  7629. if (!veb)
  7630. return 0;
  7631. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  7632. nlflags, 0, 0, filter_mask, NULL);
  7633. }
  7634. #define I40E_MAX_TUNNEL_HDR_LEN 80
  7635. /**
  7636. * i40e_features_check - Validate encapsulated packet conforms to limits
  7637. * @skb: skb buff
  7638. * @dev: This physical port's netdev
  7639. * @features: Offload features that the stack believes apply
  7640. **/
  7641. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  7642. struct net_device *dev,
  7643. netdev_features_t features)
  7644. {
  7645. if (skb->encapsulation &&
  7646. (skb_inner_mac_header(skb) - skb_transport_header(skb) >
  7647. I40E_MAX_TUNNEL_HDR_LEN))
  7648. return features & ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK);
  7649. return features;
  7650. }
  7651. static const struct net_device_ops i40e_netdev_ops = {
  7652. .ndo_open = i40e_open,
  7653. .ndo_stop = i40e_close,
  7654. .ndo_start_xmit = i40e_lan_xmit_frame,
  7655. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  7656. .ndo_set_rx_mode = i40e_set_rx_mode,
  7657. .ndo_validate_addr = eth_validate_addr,
  7658. .ndo_set_mac_address = i40e_set_mac,
  7659. .ndo_change_mtu = i40e_change_mtu,
  7660. .ndo_do_ioctl = i40e_ioctl,
  7661. .ndo_tx_timeout = i40e_tx_timeout,
  7662. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  7663. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  7664. #ifdef CONFIG_NET_POLL_CONTROLLER
  7665. .ndo_poll_controller = i40e_netpoll,
  7666. #endif
  7667. .ndo_setup_tc = i40e_setup_tc,
  7668. #ifdef I40E_FCOE
  7669. .ndo_fcoe_enable = i40e_fcoe_enable,
  7670. .ndo_fcoe_disable = i40e_fcoe_disable,
  7671. #endif
  7672. .ndo_set_features = i40e_set_features,
  7673. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  7674. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  7675. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  7676. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  7677. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  7678. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  7679. #ifdef CONFIG_I40E_VXLAN
  7680. .ndo_add_vxlan_port = i40e_add_vxlan_port,
  7681. .ndo_del_vxlan_port = i40e_del_vxlan_port,
  7682. #endif
  7683. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  7684. .ndo_fdb_add = i40e_ndo_fdb_add,
  7685. .ndo_features_check = i40e_features_check,
  7686. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  7687. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  7688. };
  7689. /**
  7690. * i40e_config_netdev - Setup the netdev flags
  7691. * @vsi: the VSI being configured
  7692. *
  7693. * Returns 0 on success, negative value on failure
  7694. **/
  7695. static int i40e_config_netdev(struct i40e_vsi *vsi)
  7696. {
  7697. u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  7698. struct i40e_pf *pf = vsi->back;
  7699. struct i40e_hw *hw = &pf->hw;
  7700. struct i40e_netdev_priv *np;
  7701. struct net_device *netdev;
  7702. u8 mac_addr[ETH_ALEN];
  7703. int etherdev_size;
  7704. etherdev_size = sizeof(struct i40e_netdev_priv);
  7705. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  7706. if (!netdev)
  7707. return -ENOMEM;
  7708. vsi->netdev = netdev;
  7709. np = netdev_priv(netdev);
  7710. np->vsi = vsi;
  7711. netdev->hw_enc_features |= NETIF_F_IP_CSUM |
  7712. NETIF_F_GSO_UDP_TUNNEL |
  7713. NETIF_F_GSO_GRE |
  7714. NETIF_F_TSO;
  7715. netdev->features = NETIF_F_SG |
  7716. NETIF_F_IP_CSUM |
  7717. NETIF_F_SCTP_CSUM |
  7718. NETIF_F_HIGHDMA |
  7719. NETIF_F_GSO_UDP_TUNNEL |
  7720. NETIF_F_GSO_GRE |
  7721. NETIF_F_HW_VLAN_CTAG_TX |
  7722. NETIF_F_HW_VLAN_CTAG_RX |
  7723. NETIF_F_HW_VLAN_CTAG_FILTER |
  7724. NETIF_F_IPV6_CSUM |
  7725. NETIF_F_TSO |
  7726. NETIF_F_TSO_ECN |
  7727. NETIF_F_TSO6 |
  7728. NETIF_F_RXCSUM |
  7729. NETIF_F_RXHASH |
  7730. 0;
  7731. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  7732. netdev->features |= NETIF_F_NTUPLE;
  7733. /* copy netdev features into list of user selectable features */
  7734. netdev->hw_features |= netdev->features;
  7735. if (vsi->type == I40E_VSI_MAIN) {
  7736. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  7737. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  7738. /* The following steps are necessary to prevent reception
  7739. * of tagged packets - some older NVM configurations load a
  7740. * default a MAC-VLAN filter that accepts any tagged packet
  7741. * which must be replaced by a normal filter.
  7742. */
  7743. if (!i40e_rm_default_mac_filter(vsi, mac_addr)) {
  7744. spin_lock_bh(&vsi->mac_filter_list_lock);
  7745. i40e_add_filter(vsi, mac_addr,
  7746. I40E_VLAN_ANY, false, true);
  7747. spin_unlock_bh(&vsi->mac_filter_list_lock);
  7748. }
  7749. } else {
  7750. /* relate the VSI_VMDQ name to the VSI_MAIN name */
  7751. snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
  7752. pf->vsi[pf->lan_vsi]->netdev->name);
  7753. random_ether_addr(mac_addr);
  7754. spin_lock_bh(&vsi->mac_filter_list_lock);
  7755. i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
  7756. spin_unlock_bh(&vsi->mac_filter_list_lock);
  7757. }
  7758. spin_lock_bh(&vsi->mac_filter_list_lock);
  7759. i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
  7760. spin_unlock_bh(&vsi->mac_filter_list_lock);
  7761. ether_addr_copy(netdev->dev_addr, mac_addr);
  7762. ether_addr_copy(netdev->perm_addr, mac_addr);
  7763. /* vlan gets same features (except vlan offload)
  7764. * after any tweaks for specific VSI types
  7765. */
  7766. netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
  7767. NETIF_F_HW_VLAN_CTAG_RX |
  7768. NETIF_F_HW_VLAN_CTAG_FILTER);
  7769. netdev->priv_flags |= IFF_UNICAST_FLT;
  7770. netdev->priv_flags |= IFF_SUPP_NOFCS;
  7771. /* Setup netdev TC information */
  7772. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  7773. netdev->netdev_ops = &i40e_netdev_ops;
  7774. netdev->watchdog_timeo = 5 * HZ;
  7775. i40e_set_ethtool_ops(netdev);
  7776. #ifdef I40E_FCOE
  7777. i40e_fcoe_config_netdev(netdev, vsi);
  7778. #endif
  7779. return 0;
  7780. }
  7781. /**
  7782. * i40e_vsi_delete - Delete a VSI from the switch
  7783. * @vsi: the VSI being removed
  7784. *
  7785. * Returns 0 on success, negative value on failure
  7786. **/
  7787. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  7788. {
  7789. /* remove default VSI is not allowed */
  7790. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  7791. return;
  7792. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  7793. }
  7794. /**
  7795. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  7796. * @vsi: the VSI being queried
  7797. *
  7798. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  7799. **/
  7800. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  7801. {
  7802. struct i40e_veb *veb;
  7803. struct i40e_pf *pf = vsi->back;
  7804. /* Uplink is not a bridge so default to VEB */
  7805. if (vsi->veb_idx == I40E_NO_VEB)
  7806. return 1;
  7807. veb = pf->veb[vsi->veb_idx];
  7808. if (!veb) {
  7809. dev_info(&pf->pdev->dev,
  7810. "There is no veb associated with the bridge\n");
  7811. return -ENOENT;
  7812. }
  7813. /* Uplink is a bridge in VEPA mode */
  7814. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  7815. return 0;
  7816. } else {
  7817. /* Uplink is a bridge in VEB mode */
  7818. return 1;
  7819. }
  7820. /* VEPA is now default bridge, so return 0 */
  7821. return 0;
  7822. }
  7823. /**
  7824. * i40e_add_vsi - Add a VSI to the switch
  7825. * @vsi: the VSI being configured
  7826. *
  7827. * This initializes a VSI context depending on the VSI type to be added and
  7828. * passes it down to the add_vsi aq command.
  7829. **/
  7830. static int i40e_add_vsi(struct i40e_vsi *vsi)
  7831. {
  7832. int ret = -ENODEV;
  7833. u8 laa_macaddr[ETH_ALEN];
  7834. bool found_laa_mac_filter = false;
  7835. struct i40e_pf *pf = vsi->back;
  7836. struct i40e_hw *hw = &pf->hw;
  7837. struct i40e_vsi_context ctxt;
  7838. struct i40e_mac_filter *f, *ftmp;
  7839. u8 enabled_tc = 0x1; /* TC0 enabled */
  7840. int f_count = 0;
  7841. memset(&ctxt, 0, sizeof(ctxt));
  7842. switch (vsi->type) {
  7843. case I40E_VSI_MAIN:
  7844. /* The PF's main VSI is already setup as part of the
  7845. * device initialization, so we'll not bother with
  7846. * the add_vsi call, but we will retrieve the current
  7847. * VSI context.
  7848. */
  7849. ctxt.seid = pf->main_vsi_seid;
  7850. ctxt.pf_num = pf->hw.pf_id;
  7851. ctxt.vf_num = 0;
  7852. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7853. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7854. if (ret) {
  7855. dev_info(&pf->pdev->dev,
  7856. "couldn't get PF vsi config, err %s aq_err %s\n",
  7857. i40e_stat_str(&pf->hw, ret),
  7858. i40e_aq_str(&pf->hw,
  7859. pf->hw.aq.asq_last_status));
  7860. return -ENOENT;
  7861. }
  7862. vsi->info = ctxt.info;
  7863. vsi->info.valid_sections = 0;
  7864. vsi->seid = ctxt.seid;
  7865. vsi->id = ctxt.vsi_number;
  7866. enabled_tc = i40e_pf_get_tc_map(pf);
  7867. /* MFP mode setup queue map and update VSI */
  7868. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  7869. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  7870. memset(&ctxt, 0, sizeof(ctxt));
  7871. ctxt.seid = pf->main_vsi_seid;
  7872. ctxt.pf_num = pf->hw.pf_id;
  7873. ctxt.vf_num = 0;
  7874. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  7875. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  7876. if (ret) {
  7877. dev_info(&pf->pdev->dev,
  7878. "update vsi failed, err %s aq_err %s\n",
  7879. i40e_stat_str(&pf->hw, ret),
  7880. i40e_aq_str(&pf->hw,
  7881. pf->hw.aq.asq_last_status));
  7882. ret = -ENOENT;
  7883. goto err;
  7884. }
  7885. /* update the local VSI info queue map */
  7886. i40e_vsi_update_queue_map(vsi, &ctxt);
  7887. vsi->info.valid_sections = 0;
  7888. } else {
  7889. /* Default/Main VSI is only enabled for TC0
  7890. * reconfigure it to enable all TCs that are
  7891. * available on the port in SFP mode.
  7892. * For MFP case the iSCSI PF would use this
  7893. * flow to enable LAN+iSCSI TC.
  7894. */
  7895. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  7896. if (ret) {
  7897. dev_info(&pf->pdev->dev,
  7898. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  7899. enabled_tc,
  7900. i40e_stat_str(&pf->hw, ret),
  7901. i40e_aq_str(&pf->hw,
  7902. pf->hw.aq.asq_last_status));
  7903. ret = -ENOENT;
  7904. }
  7905. }
  7906. break;
  7907. case I40E_VSI_FDIR:
  7908. ctxt.pf_num = hw->pf_id;
  7909. ctxt.vf_num = 0;
  7910. ctxt.uplink_seid = vsi->uplink_seid;
  7911. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  7912. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7913. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  7914. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  7915. ctxt.info.valid_sections |=
  7916. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7917. ctxt.info.switch_id =
  7918. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7919. }
  7920. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  7921. break;
  7922. case I40E_VSI_VMDQ2:
  7923. ctxt.pf_num = hw->pf_id;
  7924. ctxt.vf_num = 0;
  7925. ctxt.uplink_seid = vsi->uplink_seid;
  7926. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  7927. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  7928. /* This VSI is connected to VEB so the switch_id
  7929. * should be set to zero by default.
  7930. */
  7931. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  7932. ctxt.info.valid_sections |=
  7933. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7934. ctxt.info.switch_id =
  7935. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7936. }
  7937. /* Setup the VSI tx/rx queue map for TC0 only for now */
  7938. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  7939. break;
  7940. case I40E_VSI_SRIOV:
  7941. ctxt.pf_num = hw->pf_id;
  7942. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  7943. ctxt.uplink_seid = vsi->uplink_seid;
  7944. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  7945. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  7946. /* This VSI is connected to VEB so the switch_id
  7947. * should be set to zero by default.
  7948. */
  7949. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  7950. ctxt.info.valid_sections |=
  7951. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7952. ctxt.info.switch_id =
  7953. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7954. }
  7955. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  7956. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  7957. if (pf->vf[vsi->vf_id].spoofchk) {
  7958. ctxt.info.valid_sections |=
  7959. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  7960. ctxt.info.sec_flags |=
  7961. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  7962. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  7963. }
  7964. /* Setup the VSI tx/rx queue map for TC0 only for now */
  7965. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  7966. break;
  7967. #ifdef I40E_FCOE
  7968. case I40E_VSI_FCOE:
  7969. ret = i40e_fcoe_vsi_init(vsi, &ctxt);
  7970. if (ret) {
  7971. dev_info(&pf->pdev->dev, "failed to initialize FCoE VSI\n");
  7972. return ret;
  7973. }
  7974. break;
  7975. #endif /* I40E_FCOE */
  7976. default:
  7977. return -ENODEV;
  7978. }
  7979. if (vsi->type != I40E_VSI_MAIN) {
  7980. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  7981. if (ret) {
  7982. dev_info(&vsi->back->pdev->dev,
  7983. "add vsi failed, err %s aq_err %s\n",
  7984. i40e_stat_str(&pf->hw, ret),
  7985. i40e_aq_str(&pf->hw,
  7986. pf->hw.aq.asq_last_status));
  7987. ret = -ENOENT;
  7988. goto err;
  7989. }
  7990. vsi->info = ctxt.info;
  7991. vsi->info.valid_sections = 0;
  7992. vsi->seid = ctxt.seid;
  7993. vsi->id = ctxt.vsi_number;
  7994. }
  7995. spin_lock_bh(&vsi->mac_filter_list_lock);
  7996. /* If macvlan filters already exist, force them to get loaded */
  7997. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  7998. f->changed = true;
  7999. f_count++;
  8000. /* Expected to have only one MAC filter entry for LAA in list */
  8001. if (f->is_laa && vsi->type == I40E_VSI_MAIN) {
  8002. ether_addr_copy(laa_macaddr, f->macaddr);
  8003. found_laa_mac_filter = true;
  8004. }
  8005. }
  8006. spin_unlock_bh(&vsi->mac_filter_list_lock);
  8007. if (found_laa_mac_filter) {
  8008. struct i40e_aqc_remove_macvlan_element_data element;
  8009. memset(&element, 0, sizeof(element));
  8010. ether_addr_copy(element.mac_addr, laa_macaddr);
  8011. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  8012. ret = i40e_aq_remove_macvlan(hw, vsi->seid,
  8013. &element, 1, NULL);
  8014. if (ret) {
  8015. /* some older FW has a different default */
  8016. element.flags |=
  8017. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  8018. i40e_aq_remove_macvlan(hw, vsi->seid,
  8019. &element, 1, NULL);
  8020. }
  8021. i40e_aq_mac_address_write(hw,
  8022. I40E_AQC_WRITE_TYPE_LAA_WOL,
  8023. laa_macaddr, NULL);
  8024. }
  8025. if (f_count) {
  8026. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  8027. pf->flags |= I40E_FLAG_FILTER_SYNC;
  8028. }
  8029. /* Update VSI BW information */
  8030. ret = i40e_vsi_get_bw_info(vsi);
  8031. if (ret) {
  8032. dev_info(&pf->pdev->dev,
  8033. "couldn't get vsi bw info, err %s aq_err %s\n",
  8034. i40e_stat_str(&pf->hw, ret),
  8035. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8036. /* VSI is already added so not tearing that up */
  8037. ret = 0;
  8038. }
  8039. err:
  8040. return ret;
  8041. }
  8042. /**
  8043. * i40e_vsi_release - Delete a VSI and free its resources
  8044. * @vsi: the VSI being removed
  8045. *
  8046. * Returns 0 on success or < 0 on error
  8047. **/
  8048. int i40e_vsi_release(struct i40e_vsi *vsi)
  8049. {
  8050. struct i40e_mac_filter *f, *ftmp;
  8051. struct i40e_veb *veb = NULL;
  8052. struct i40e_pf *pf;
  8053. u16 uplink_seid;
  8054. int i, n;
  8055. pf = vsi->back;
  8056. /* release of a VEB-owner or last VSI is not allowed */
  8057. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  8058. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  8059. vsi->seid, vsi->uplink_seid);
  8060. return -ENODEV;
  8061. }
  8062. if (vsi == pf->vsi[pf->lan_vsi] &&
  8063. !test_bit(__I40E_DOWN, &pf->state)) {
  8064. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  8065. return -ENODEV;
  8066. }
  8067. uplink_seid = vsi->uplink_seid;
  8068. if (vsi->type != I40E_VSI_SRIOV) {
  8069. if (vsi->netdev_registered) {
  8070. vsi->netdev_registered = false;
  8071. if (vsi->netdev) {
  8072. /* results in a call to i40e_close() */
  8073. unregister_netdev(vsi->netdev);
  8074. }
  8075. } else {
  8076. i40e_vsi_close(vsi);
  8077. }
  8078. i40e_vsi_disable_irq(vsi);
  8079. }
  8080. spin_lock_bh(&vsi->mac_filter_list_lock);
  8081. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
  8082. i40e_del_filter(vsi, f->macaddr, f->vlan,
  8083. f->is_vf, f->is_netdev);
  8084. spin_unlock_bh(&vsi->mac_filter_list_lock);
  8085. i40e_sync_vsi_filters(vsi);
  8086. i40e_vsi_delete(vsi);
  8087. i40e_vsi_free_q_vectors(vsi);
  8088. if (vsi->netdev) {
  8089. free_netdev(vsi->netdev);
  8090. vsi->netdev = NULL;
  8091. }
  8092. i40e_vsi_clear_rings(vsi);
  8093. i40e_vsi_clear(vsi);
  8094. /* If this was the last thing on the VEB, except for the
  8095. * controlling VSI, remove the VEB, which puts the controlling
  8096. * VSI onto the next level down in the switch.
  8097. *
  8098. * Well, okay, there's one more exception here: don't remove
  8099. * the orphan VEBs yet. We'll wait for an explicit remove request
  8100. * from up the network stack.
  8101. */
  8102. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  8103. if (pf->vsi[i] &&
  8104. pf->vsi[i]->uplink_seid == uplink_seid &&
  8105. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8106. n++; /* count the VSIs */
  8107. }
  8108. }
  8109. for (i = 0; i < I40E_MAX_VEB; i++) {
  8110. if (!pf->veb[i])
  8111. continue;
  8112. if (pf->veb[i]->uplink_seid == uplink_seid)
  8113. n++; /* count the VEBs */
  8114. if (pf->veb[i]->seid == uplink_seid)
  8115. veb = pf->veb[i];
  8116. }
  8117. if (n == 0 && veb && veb->uplink_seid != 0)
  8118. i40e_veb_release(veb);
  8119. return 0;
  8120. }
  8121. /**
  8122. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  8123. * @vsi: ptr to the VSI
  8124. *
  8125. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  8126. * corresponding SW VSI structure and initializes num_queue_pairs for the
  8127. * newly allocated VSI.
  8128. *
  8129. * Returns 0 on success or negative on failure
  8130. **/
  8131. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  8132. {
  8133. int ret = -ENOENT;
  8134. struct i40e_pf *pf = vsi->back;
  8135. if (vsi->q_vectors[0]) {
  8136. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  8137. vsi->seid);
  8138. return -EEXIST;
  8139. }
  8140. if (vsi->base_vector) {
  8141. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  8142. vsi->seid, vsi->base_vector);
  8143. return -EEXIST;
  8144. }
  8145. ret = i40e_vsi_alloc_q_vectors(vsi);
  8146. if (ret) {
  8147. dev_info(&pf->pdev->dev,
  8148. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  8149. vsi->num_q_vectors, vsi->seid, ret);
  8150. vsi->num_q_vectors = 0;
  8151. goto vector_setup_out;
  8152. }
  8153. /* In Legacy mode, we do not have to get any other vector since we
  8154. * piggyback on the misc/ICR0 for queue interrupts.
  8155. */
  8156. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8157. return ret;
  8158. if (vsi->num_q_vectors)
  8159. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  8160. vsi->num_q_vectors, vsi->idx);
  8161. if (vsi->base_vector < 0) {
  8162. dev_info(&pf->pdev->dev,
  8163. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  8164. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  8165. i40e_vsi_free_q_vectors(vsi);
  8166. ret = -ENOENT;
  8167. goto vector_setup_out;
  8168. }
  8169. vector_setup_out:
  8170. return ret;
  8171. }
  8172. /**
  8173. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  8174. * @vsi: pointer to the vsi.
  8175. *
  8176. * This re-allocates a vsi's queue resources.
  8177. *
  8178. * Returns pointer to the successfully allocated and configured VSI sw struct
  8179. * on success, otherwise returns NULL on failure.
  8180. **/
  8181. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  8182. {
  8183. struct i40e_pf *pf = vsi->back;
  8184. u8 enabled_tc;
  8185. int ret;
  8186. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8187. i40e_vsi_clear_rings(vsi);
  8188. i40e_vsi_free_arrays(vsi, false);
  8189. i40e_set_num_rings_in_vsi(vsi);
  8190. ret = i40e_vsi_alloc_arrays(vsi, false);
  8191. if (ret)
  8192. goto err_vsi;
  8193. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
  8194. if (ret < 0) {
  8195. dev_info(&pf->pdev->dev,
  8196. "failed to get tracking for %d queues for VSI %d err %d\n",
  8197. vsi->alloc_queue_pairs, vsi->seid, ret);
  8198. goto err_vsi;
  8199. }
  8200. vsi->base_queue = ret;
  8201. /* Update the FW view of the VSI. Force a reset of TC and queue
  8202. * layout configurations.
  8203. */
  8204. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  8205. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  8206. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  8207. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  8208. /* assign it some queues */
  8209. ret = i40e_alloc_rings(vsi);
  8210. if (ret)
  8211. goto err_rings;
  8212. /* map all of the rings to the q_vectors */
  8213. i40e_vsi_map_rings_to_vectors(vsi);
  8214. return vsi;
  8215. err_rings:
  8216. i40e_vsi_free_q_vectors(vsi);
  8217. if (vsi->netdev_registered) {
  8218. vsi->netdev_registered = false;
  8219. unregister_netdev(vsi->netdev);
  8220. free_netdev(vsi->netdev);
  8221. vsi->netdev = NULL;
  8222. }
  8223. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8224. err_vsi:
  8225. i40e_vsi_clear(vsi);
  8226. return NULL;
  8227. }
  8228. /**
  8229. * i40e_vsi_setup - Set up a VSI by a given type
  8230. * @pf: board private structure
  8231. * @type: VSI type
  8232. * @uplink_seid: the switch element to link to
  8233. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  8234. *
  8235. * This allocates the sw VSI structure and its queue resources, then add a VSI
  8236. * to the identified VEB.
  8237. *
  8238. * Returns pointer to the successfully allocated and configure VSI sw struct on
  8239. * success, otherwise returns NULL on failure.
  8240. **/
  8241. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  8242. u16 uplink_seid, u32 param1)
  8243. {
  8244. struct i40e_vsi *vsi = NULL;
  8245. struct i40e_veb *veb = NULL;
  8246. int ret, i;
  8247. int v_idx;
  8248. /* The requested uplink_seid must be either
  8249. * - the PF's port seid
  8250. * no VEB is needed because this is the PF
  8251. * or this is a Flow Director special case VSI
  8252. * - seid of an existing VEB
  8253. * - seid of a VSI that owns an existing VEB
  8254. * - seid of a VSI that doesn't own a VEB
  8255. * a new VEB is created and the VSI becomes the owner
  8256. * - seid of the PF VSI, which is what creates the first VEB
  8257. * this is a special case of the previous
  8258. *
  8259. * Find which uplink_seid we were given and create a new VEB if needed
  8260. */
  8261. for (i = 0; i < I40E_MAX_VEB; i++) {
  8262. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  8263. veb = pf->veb[i];
  8264. break;
  8265. }
  8266. }
  8267. if (!veb && uplink_seid != pf->mac_seid) {
  8268. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8269. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  8270. vsi = pf->vsi[i];
  8271. break;
  8272. }
  8273. }
  8274. if (!vsi) {
  8275. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  8276. uplink_seid);
  8277. return NULL;
  8278. }
  8279. if (vsi->uplink_seid == pf->mac_seid)
  8280. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  8281. vsi->tc_config.enabled_tc);
  8282. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  8283. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8284. vsi->tc_config.enabled_tc);
  8285. if (veb) {
  8286. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  8287. dev_info(&vsi->back->pdev->dev,
  8288. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  8289. return NULL;
  8290. }
  8291. /* We come up by default in VEPA mode if SRIOV is not
  8292. * already enabled, in which case we can't force VEPA
  8293. * mode.
  8294. */
  8295. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  8296. veb->bridge_mode = BRIDGE_MODE_VEPA;
  8297. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8298. }
  8299. i40e_config_bridge_mode(veb);
  8300. }
  8301. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8302. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8303. veb = pf->veb[i];
  8304. }
  8305. if (!veb) {
  8306. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  8307. return NULL;
  8308. }
  8309. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  8310. uplink_seid = veb->seid;
  8311. }
  8312. /* get vsi sw struct */
  8313. v_idx = i40e_vsi_mem_alloc(pf, type);
  8314. if (v_idx < 0)
  8315. goto err_alloc;
  8316. vsi = pf->vsi[v_idx];
  8317. if (!vsi)
  8318. goto err_alloc;
  8319. vsi->type = type;
  8320. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  8321. if (type == I40E_VSI_MAIN)
  8322. pf->lan_vsi = v_idx;
  8323. else if (type == I40E_VSI_SRIOV)
  8324. vsi->vf_id = param1;
  8325. /* assign it some queues */
  8326. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
  8327. vsi->idx);
  8328. if (ret < 0) {
  8329. dev_info(&pf->pdev->dev,
  8330. "failed to get tracking for %d queues for VSI %d err=%d\n",
  8331. vsi->alloc_queue_pairs, vsi->seid, ret);
  8332. goto err_vsi;
  8333. }
  8334. vsi->base_queue = ret;
  8335. /* get a VSI from the hardware */
  8336. vsi->uplink_seid = uplink_seid;
  8337. ret = i40e_add_vsi(vsi);
  8338. if (ret)
  8339. goto err_vsi;
  8340. switch (vsi->type) {
  8341. /* setup the netdev if needed */
  8342. case I40E_VSI_MAIN:
  8343. case I40E_VSI_VMDQ2:
  8344. case I40E_VSI_FCOE:
  8345. ret = i40e_config_netdev(vsi);
  8346. if (ret)
  8347. goto err_netdev;
  8348. ret = register_netdev(vsi->netdev);
  8349. if (ret)
  8350. goto err_netdev;
  8351. vsi->netdev_registered = true;
  8352. netif_carrier_off(vsi->netdev);
  8353. #ifdef CONFIG_I40E_DCB
  8354. /* Setup DCB netlink interface */
  8355. i40e_dcbnl_setup(vsi);
  8356. #endif /* CONFIG_I40E_DCB */
  8357. /* fall through */
  8358. case I40E_VSI_FDIR:
  8359. /* set up vectors and rings if needed */
  8360. ret = i40e_vsi_setup_vectors(vsi);
  8361. if (ret)
  8362. goto err_msix;
  8363. ret = i40e_alloc_rings(vsi);
  8364. if (ret)
  8365. goto err_rings;
  8366. /* map all of the rings to the q_vectors */
  8367. i40e_vsi_map_rings_to_vectors(vsi);
  8368. i40e_vsi_reset_stats(vsi);
  8369. break;
  8370. default:
  8371. /* no netdev or rings for the other VSI types */
  8372. break;
  8373. }
  8374. if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
  8375. (vsi->type == I40E_VSI_VMDQ2)) {
  8376. ret = i40e_vsi_config_rss(vsi);
  8377. }
  8378. return vsi;
  8379. err_rings:
  8380. i40e_vsi_free_q_vectors(vsi);
  8381. err_msix:
  8382. if (vsi->netdev_registered) {
  8383. vsi->netdev_registered = false;
  8384. unregister_netdev(vsi->netdev);
  8385. free_netdev(vsi->netdev);
  8386. vsi->netdev = NULL;
  8387. }
  8388. err_netdev:
  8389. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8390. err_vsi:
  8391. i40e_vsi_clear(vsi);
  8392. err_alloc:
  8393. return NULL;
  8394. }
  8395. /**
  8396. * i40e_veb_get_bw_info - Query VEB BW information
  8397. * @veb: the veb to query
  8398. *
  8399. * Query the Tx scheduler BW configuration data for given VEB
  8400. **/
  8401. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  8402. {
  8403. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  8404. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  8405. struct i40e_pf *pf = veb->pf;
  8406. struct i40e_hw *hw = &pf->hw;
  8407. u32 tc_bw_max;
  8408. int ret = 0;
  8409. int i;
  8410. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  8411. &bw_data, NULL);
  8412. if (ret) {
  8413. dev_info(&pf->pdev->dev,
  8414. "query veb bw config failed, err %s aq_err %s\n",
  8415. i40e_stat_str(&pf->hw, ret),
  8416. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8417. goto out;
  8418. }
  8419. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  8420. &ets_data, NULL);
  8421. if (ret) {
  8422. dev_info(&pf->pdev->dev,
  8423. "query veb bw ets config failed, err %s aq_err %s\n",
  8424. i40e_stat_str(&pf->hw, ret),
  8425. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8426. goto out;
  8427. }
  8428. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  8429. veb->bw_max_quanta = ets_data.tc_bw_max;
  8430. veb->is_abs_credits = bw_data.absolute_credits_enable;
  8431. veb->enabled_tc = ets_data.tc_valid_bits;
  8432. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  8433. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  8434. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  8435. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  8436. veb->bw_tc_limit_credits[i] =
  8437. le16_to_cpu(bw_data.tc_bw_limits[i]);
  8438. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  8439. }
  8440. out:
  8441. return ret;
  8442. }
  8443. /**
  8444. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  8445. * @pf: board private structure
  8446. *
  8447. * On error: returns error code (negative)
  8448. * On success: returns vsi index in PF (positive)
  8449. **/
  8450. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  8451. {
  8452. int ret = -ENOENT;
  8453. struct i40e_veb *veb;
  8454. int i;
  8455. /* Need to protect the allocation of switch elements at the PF level */
  8456. mutex_lock(&pf->switch_mutex);
  8457. /* VEB list may be fragmented if VEB creation/destruction has
  8458. * been happening. We can afford to do a quick scan to look
  8459. * for any free slots in the list.
  8460. *
  8461. * find next empty veb slot, looping back around if necessary
  8462. */
  8463. i = 0;
  8464. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  8465. i++;
  8466. if (i >= I40E_MAX_VEB) {
  8467. ret = -ENOMEM;
  8468. goto err_alloc_veb; /* out of VEB slots! */
  8469. }
  8470. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  8471. if (!veb) {
  8472. ret = -ENOMEM;
  8473. goto err_alloc_veb;
  8474. }
  8475. veb->pf = pf;
  8476. veb->idx = i;
  8477. veb->enabled_tc = 1;
  8478. pf->veb[i] = veb;
  8479. ret = i;
  8480. err_alloc_veb:
  8481. mutex_unlock(&pf->switch_mutex);
  8482. return ret;
  8483. }
  8484. /**
  8485. * i40e_switch_branch_release - Delete a branch of the switch tree
  8486. * @branch: where to start deleting
  8487. *
  8488. * This uses recursion to find the tips of the branch to be
  8489. * removed, deleting until we get back to and can delete this VEB.
  8490. **/
  8491. static void i40e_switch_branch_release(struct i40e_veb *branch)
  8492. {
  8493. struct i40e_pf *pf = branch->pf;
  8494. u16 branch_seid = branch->seid;
  8495. u16 veb_idx = branch->idx;
  8496. int i;
  8497. /* release any VEBs on this VEB - RECURSION */
  8498. for (i = 0; i < I40E_MAX_VEB; i++) {
  8499. if (!pf->veb[i])
  8500. continue;
  8501. if (pf->veb[i]->uplink_seid == branch->seid)
  8502. i40e_switch_branch_release(pf->veb[i]);
  8503. }
  8504. /* Release the VSIs on this VEB, but not the owner VSI.
  8505. *
  8506. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  8507. * the VEB itself, so don't use (*branch) after this loop.
  8508. */
  8509. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8510. if (!pf->vsi[i])
  8511. continue;
  8512. if (pf->vsi[i]->uplink_seid == branch_seid &&
  8513. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8514. i40e_vsi_release(pf->vsi[i]);
  8515. }
  8516. }
  8517. /* There's one corner case where the VEB might not have been
  8518. * removed, so double check it here and remove it if needed.
  8519. * This case happens if the veb was created from the debugfs
  8520. * commands and no VSIs were added to it.
  8521. */
  8522. if (pf->veb[veb_idx])
  8523. i40e_veb_release(pf->veb[veb_idx]);
  8524. }
  8525. /**
  8526. * i40e_veb_clear - remove veb struct
  8527. * @veb: the veb to remove
  8528. **/
  8529. static void i40e_veb_clear(struct i40e_veb *veb)
  8530. {
  8531. if (!veb)
  8532. return;
  8533. if (veb->pf) {
  8534. struct i40e_pf *pf = veb->pf;
  8535. mutex_lock(&pf->switch_mutex);
  8536. if (pf->veb[veb->idx] == veb)
  8537. pf->veb[veb->idx] = NULL;
  8538. mutex_unlock(&pf->switch_mutex);
  8539. }
  8540. kfree(veb);
  8541. }
  8542. /**
  8543. * i40e_veb_release - Delete a VEB and free its resources
  8544. * @veb: the VEB being removed
  8545. **/
  8546. void i40e_veb_release(struct i40e_veb *veb)
  8547. {
  8548. struct i40e_vsi *vsi = NULL;
  8549. struct i40e_pf *pf;
  8550. int i, n = 0;
  8551. pf = veb->pf;
  8552. /* find the remaining VSI and check for extras */
  8553. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8554. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  8555. n++;
  8556. vsi = pf->vsi[i];
  8557. }
  8558. }
  8559. if (n != 1) {
  8560. dev_info(&pf->pdev->dev,
  8561. "can't remove VEB %d with %d VSIs left\n",
  8562. veb->seid, n);
  8563. return;
  8564. }
  8565. /* move the remaining VSI to uplink veb */
  8566. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  8567. if (veb->uplink_seid) {
  8568. vsi->uplink_seid = veb->uplink_seid;
  8569. if (veb->uplink_seid == pf->mac_seid)
  8570. vsi->veb_idx = I40E_NO_VEB;
  8571. else
  8572. vsi->veb_idx = veb->veb_idx;
  8573. } else {
  8574. /* floating VEB */
  8575. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  8576. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  8577. }
  8578. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  8579. i40e_veb_clear(veb);
  8580. }
  8581. /**
  8582. * i40e_add_veb - create the VEB in the switch
  8583. * @veb: the VEB to be instantiated
  8584. * @vsi: the controlling VSI
  8585. **/
  8586. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  8587. {
  8588. struct i40e_pf *pf = veb->pf;
  8589. bool is_default = veb->pf->cur_promisc;
  8590. bool is_cloud = false;
  8591. int ret;
  8592. /* get a VEB from the hardware */
  8593. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  8594. veb->enabled_tc, is_default,
  8595. is_cloud, &veb->seid, NULL);
  8596. if (ret) {
  8597. dev_info(&pf->pdev->dev,
  8598. "couldn't add VEB, err %s aq_err %s\n",
  8599. i40e_stat_str(&pf->hw, ret),
  8600. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8601. return -EPERM;
  8602. }
  8603. /* get statistics counter */
  8604. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  8605. &veb->stats_idx, NULL, NULL, NULL);
  8606. if (ret) {
  8607. dev_info(&pf->pdev->dev,
  8608. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  8609. i40e_stat_str(&pf->hw, ret),
  8610. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8611. return -EPERM;
  8612. }
  8613. ret = i40e_veb_get_bw_info(veb);
  8614. if (ret) {
  8615. dev_info(&pf->pdev->dev,
  8616. "couldn't get VEB bw info, err %s aq_err %s\n",
  8617. i40e_stat_str(&pf->hw, ret),
  8618. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8619. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  8620. return -ENOENT;
  8621. }
  8622. vsi->uplink_seid = veb->seid;
  8623. vsi->veb_idx = veb->idx;
  8624. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  8625. return 0;
  8626. }
  8627. /**
  8628. * i40e_veb_setup - Set up a VEB
  8629. * @pf: board private structure
  8630. * @flags: VEB setup flags
  8631. * @uplink_seid: the switch element to link to
  8632. * @vsi_seid: the initial VSI seid
  8633. * @enabled_tc: Enabled TC bit-map
  8634. *
  8635. * This allocates the sw VEB structure and links it into the switch
  8636. * It is possible and legal for this to be a duplicate of an already
  8637. * existing VEB. It is also possible for both uplink and vsi seids
  8638. * to be zero, in order to create a floating VEB.
  8639. *
  8640. * Returns pointer to the successfully allocated VEB sw struct on
  8641. * success, otherwise returns NULL on failure.
  8642. **/
  8643. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  8644. u16 uplink_seid, u16 vsi_seid,
  8645. u8 enabled_tc)
  8646. {
  8647. struct i40e_veb *veb, *uplink_veb = NULL;
  8648. int vsi_idx, veb_idx;
  8649. int ret;
  8650. /* if one seid is 0, the other must be 0 to create a floating relay */
  8651. if ((uplink_seid == 0 || vsi_seid == 0) &&
  8652. (uplink_seid + vsi_seid != 0)) {
  8653. dev_info(&pf->pdev->dev,
  8654. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  8655. uplink_seid, vsi_seid);
  8656. return NULL;
  8657. }
  8658. /* make sure there is such a vsi and uplink */
  8659. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  8660. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  8661. break;
  8662. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  8663. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  8664. vsi_seid);
  8665. return NULL;
  8666. }
  8667. if (uplink_seid && uplink_seid != pf->mac_seid) {
  8668. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  8669. if (pf->veb[veb_idx] &&
  8670. pf->veb[veb_idx]->seid == uplink_seid) {
  8671. uplink_veb = pf->veb[veb_idx];
  8672. break;
  8673. }
  8674. }
  8675. if (!uplink_veb) {
  8676. dev_info(&pf->pdev->dev,
  8677. "uplink seid %d not found\n", uplink_seid);
  8678. return NULL;
  8679. }
  8680. }
  8681. /* get veb sw struct */
  8682. veb_idx = i40e_veb_mem_alloc(pf);
  8683. if (veb_idx < 0)
  8684. goto err_alloc;
  8685. veb = pf->veb[veb_idx];
  8686. veb->flags = flags;
  8687. veb->uplink_seid = uplink_seid;
  8688. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  8689. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  8690. /* create the VEB in the switch */
  8691. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  8692. if (ret)
  8693. goto err_veb;
  8694. if (vsi_idx == pf->lan_vsi)
  8695. pf->lan_veb = veb->idx;
  8696. return veb;
  8697. err_veb:
  8698. i40e_veb_clear(veb);
  8699. err_alloc:
  8700. return NULL;
  8701. }
  8702. /**
  8703. * i40e_setup_pf_switch_element - set PF vars based on switch type
  8704. * @pf: board private structure
  8705. * @ele: element we are building info from
  8706. * @num_reported: total number of elements
  8707. * @printconfig: should we print the contents
  8708. *
  8709. * helper function to assist in extracting a few useful SEID values.
  8710. **/
  8711. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  8712. struct i40e_aqc_switch_config_element_resp *ele,
  8713. u16 num_reported, bool printconfig)
  8714. {
  8715. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  8716. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  8717. u8 element_type = ele->element_type;
  8718. u16 seid = le16_to_cpu(ele->seid);
  8719. if (printconfig)
  8720. dev_info(&pf->pdev->dev,
  8721. "type=%d seid=%d uplink=%d downlink=%d\n",
  8722. element_type, seid, uplink_seid, downlink_seid);
  8723. switch (element_type) {
  8724. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  8725. pf->mac_seid = seid;
  8726. break;
  8727. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  8728. /* Main VEB? */
  8729. if (uplink_seid != pf->mac_seid)
  8730. break;
  8731. if (pf->lan_veb == I40E_NO_VEB) {
  8732. int v;
  8733. /* find existing or else empty VEB */
  8734. for (v = 0; v < I40E_MAX_VEB; v++) {
  8735. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  8736. pf->lan_veb = v;
  8737. break;
  8738. }
  8739. }
  8740. if (pf->lan_veb == I40E_NO_VEB) {
  8741. v = i40e_veb_mem_alloc(pf);
  8742. if (v < 0)
  8743. break;
  8744. pf->lan_veb = v;
  8745. }
  8746. }
  8747. pf->veb[pf->lan_veb]->seid = seid;
  8748. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  8749. pf->veb[pf->lan_veb]->pf = pf;
  8750. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  8751. break;
  8752. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  8753. if (num_reported != 1)
  8754. break;
  8755. /* This is immediately after a reset so we can assume this is
  8756. * the PF's VSI
  8757. */
  8758. pf->mac_seid = uplink_seid;
  8759. pf->pf_seid = downlink_seid;
  8760. pf->main_vsi_seid = seid;
  8761. if (printconfig)
  8762. dev_info(&pf->pdev->dev,
  8763. "pf_seid=%d main_vsi_seid=%d\n",
  8764. pf->pf_seid, pf->main_vsi_seid);
  8765. break;
  8766. case I40E_SWITCH_ELEMENT_TYPE_PF:
  8767. case I40E_SWITCH_ELEMENT_TYPE_VF:
  8768. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  8769. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  8770. case I40E_SWITCH_ELEMENT_TYPE_PE:
  8771. case I40E_SWITCH_ELEMENT_TYPE_PA:
  8772. /* ignore these for now */
  8773. break;
  8774. default:
  8775. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  8776. element_type, seid);
  8777. break;
  8778. }
  8779. }
  8780. /**
  8781. * i40e_fetch_switch_configuration - Get switch config from firmware
  8782. * @pf: board private structure
  8783. * @printconfig: should we print the contents
  8784. *
  8785. * Get the current switch configuration from the device and
  8786. * extract a few useful SEID values.
  8787. **/
  8788. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  8789. {
  8790. struct i40e_aqc_get_switch_config_resp *sw_config;
  8791. u16 next_seid = 0;
  8792. int ret = 0;
  8793. u8 *aq_buf;
  8794. int i;
  8795. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  8796. if (!aq_buf)
  8797. return -ENOMEM;
  8798. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  8799. do {
  8800. u16 num_reported, num_total;
  8801. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  8802. I40E_AQ_LARGE_BUF,
  8803. &next_seid, NULL);
  8804. if (ret) {
  8805. dev_info(&pf->pdev->dev,
  8806. "get switch config failed err %s aq_err %s\n",
  8807. i40e_stat_str(&pf->hw, ret),
  8808. i40e_aq_str(&pf->hw,
  8809. pf->hw.aq.asq_last_status));
  8810. kfree(aq_buf);
  8811. return -ENOENT;
  8812. }
  8813. num_reported = le16_to_cpu(sw_config->header.num_reported);
  8814. num_total = le16_to_cpu(sw_config->header.num_total);
  8815. if (printconfig)
  8816. dev_info(&pf->pdev->dev,
  8817. "header: %d reported %d total\n",
  8818. num_reported, num_total);
  8819. for (i = 0; i < num_reported; i++) {
  8820. struct i40e_aqc_switch_config_element_resp *ele =
  8821. &sw_config->element[i];
  8822. i40e_setup_pf_switch_element(pf, ele, num_reported,
  8823. printconfig);
  8824. }
  8825. } while (next_seid != 0);
  8826. kfree(aq_buf);
  8827. return ret;
  8828. }
  8829. /**
  8830. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  8831. * @pf: board private structure
  8832. * @reinit: if the Main VSI needs to re-initialized.
  8833. *
  8834. * Returns 0 on success, negative value on failure
  8835. **/
  8836. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  8837. {
  8838. int ret;
  8839. /* find out what's out there already */
  8840. ret = i40e_fetch_switch_configuration(pf, false);
  8841. if (ret) {
  8842. dev_info(&pf->pdev->dev,
  8843. "couldn't fetch switch config, err %s aq_err %s\n",
  8844. i40e_stat_str(&pf->hw, ret),
  8845. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8846. return ret;
  8847. }
  8848. i40e_pf_reset_stats(pf);
  8849. /* first time setup */
  8850. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  8851. struct i40e_vsi *vsi = NULL;
  8852. u16 uplink_seid;
  8853. /* Set up the PF VSI associated with the PF's main VSI
  8854. * that is already in the HW switch
  8855. */
  8856. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  8857. uplink_seid = pf->veb[pf->lan_veb]->seid;
  8858. else
  8859. uplink_seid = pf->mac_seid;
  8860. if (pf->lan_vsi == I40E_NO_VSI)
  8861. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  8862. else if (reinit)
  8863. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  8864. if (!vsi) {
  8865. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  8866. i40e_fdir_teardown(pf);
  8867. return -EAGAIN;
  8868. }
  8869. } else {
  8870. /* force a reset of TC and queue layout configurations */
  8871. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  8872. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  8873. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  8874. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  8875. }
  8876. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  8877. i40e_fdir_sb_setup(pf);
  8878. /* Setup static PF queue filter control settings */
  8879. ret = i40e_setup_pf_filter_control(pf);
  8880. if (ret) {
  8881. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  8882. ret);
  8883. /* Failure here should not stop continuing other steps */
  8884. }
  8885. /* enable RSS in the HW, even for only one queue, as the stack can use
  8886. * the hash
  8887. */
  8888. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  8889. i40e_pf_config_rss(pf);
  8890. /* fill in link information and enable LSE reporting */
  8891. i40e_update_link_info(&pf->hw);
  8892. i40e_link_event(pf);
  8893. /* Initialize user-specific link properties */
  8894. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  8895. I40E_AQ_AN_COMPLETED) ? true : false);
  8896. i40e_ptp_init(pf);
  8897. return ret;
  8898. }
  8899. /**
  8900. * i40e_determine_queue_usage - Work out queue distribution
  8901. * @pf: board private structure
  8902. **/
  8903. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  8904. {
  8905. int queues_left;
  8906. pf->num_lan_qps = 0;
  8907. #ifdef I40E_FCOE
  8908. pf->num_fcoe_qps = 0;
  8909. #endif
  8910. /* Find the max queues to be put into basic use. We'll always be
  8911. * using TC0, whether or not DCB is running, and TC0 will get the
  8912. * big RSS set.
  8913. */
  8914. queues_left = pf->hw.func_caps.num_tx_qp;
  8915. if ((queues_left == 1) ||
  8916. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  8917. /* one qp for PF, no queues for anything else */
  8918. queues_left = 0;
  8919. pf->alloc_rss_size = pf->num_lan_qps = 1;
  8920. /* make sure all the fancies are disabled */
  8921. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  8922. #ifdef I40E_FCOE
  8923. I40E_FLAG_FCOE_ENABLED |
  8924. #endif
  8925. I40E_FLAG_FD_SB_ENABLED |
  8926. I40E_FLAG_FD_ATR_ENABLED |
  8927. I40E_FLAG_DCB_CAPABLE |
  8928. I40E_FLAG_SRIOV_ENABLED |
  8929. I40E_FLAG_VMDQ_ENABLED);
  8930. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  8931. I40E_FLAG_FD_SB_ENABLED |
  8932. I40E_FLAG_FD_ATR_ENABLED |
  8933. I40E_FLAG_DCB_CAPABLE))) {
  8934. /* one qp for PF */
  8935. pf->alloc_rss_size = pf->num_lan_qps = 1;
  8936. queues_left -= pf->num_lan_qps;
  8937. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  8938. #ifdef I40E_FCOE
  8939. I40E_FLAG_FCOE_ENABLED |
  8940. #endif
  8941. I40E_FLAG_FD_SB_ENABLED |
  8942. I40E_FLAG_FD_ATR_ENABLED |
  8943. I40E_FLAG_DCB_ENABLED |
  8944. I40E_FLAG_VMDQ_ENABLED);
  8945. } else {
  8946. /* Not enough queues for all TCs */
  8947. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  8948. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  8949. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  8950. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  8951. }
  8952. pf->num_lan_qps = max_t(int, pf->rss_size_max,
  8953. num_online_cpus());
  8954. pf->num_lan_qps = min_t(int, pf->num_lan_qps,
  8955. pf->hw.func_caps.num_tx_qp);
  8956. queues_left -= pf->num_lan_qps;
  8957. }
  8958. #ifdef I40E_FCOE
  8959. if (pf->flags & I40E_FLAG_FCOE_ENABLED) {
  8960. if (I40E_DEFAULT_FCOE <= queues_left) {
  8961. pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
  8962. } else if (I40E_MINIMUM_FCOE <= queues_left) {
  8963. pf->num_fcoe_qps = I40E_MINIMUM_FCOE;
  8964. } else {
  8965. pf->num_fcoe_qps = 0;
  8966. pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
  8967. dev_info(&pf->pdev->dev, "not enough queues for FCoE. FCoE feature will be disabled\n");
  8968. }
  8969. queues_left -= pf->num_fcoe_qps;
  8970. }
  8971. #endif
  8972. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  8973. if (queues_left > 1) {
  8974. queues_left -= 1; /* save 1 queue for FD */
  8975. } else {
  8976. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  8977. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  8978. }
  8979. }
  8980. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  8981. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  8982. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  8983. (queues_left / pf->num_vf_qps));
  8984. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  8985. }
  8986. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  8987. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  8988. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  8989. (queues_left / pf->num_vmdq_qps));
  8990. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  8991. }
  8992. pf->queues_left = queues_left;
  8993. dev_dbg(&pf->pdev->dev,
  8994. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  8995. pf->hw.func_caps.num_tx_qp,
  8996. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  8997. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  8998. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  8999. queues_left);
  9000. #ifdef I40E_FCOE
  9001. dev_dbg(&pf->pdev->dev, "fcoe queues = %d\n", pf->num_fcoe_qps);
  9002. #endif
  9003. }
  9004. /**
  9005. * i40e_setup_pf_filter_control - Setup PF static filter control
  9006. * @pf: PF to be setup
  9007. *
  9008. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  9009. * settings. If PE/FCoE are enabled then it will also set the per PF
  9010. * based filter sizes required for them. It also enables Flow director,
  9011. * ethertype and macvlan type filter settings for the pf.
  9012. *
  9013. * Returns 0 on success, negative on failure
  9014. **/
  9015. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  9016. {
  9017. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  9018. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  9019. /* Flow Director is enabled */
  9020. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  9021. settings->enable_fdir = true;
  9022. /* Ethtype and MACVLAN filters enabled for PF */
  9023. settings->enable_ethtype = true;
  9024. settings->enable_macvlan = true;
  9025. if (i40e_set_filter_control(&pf->hw, settings))
  9026. return -ENOENT;
  9027. return 0;
  9028. }
  9029. #define INFO_STRING_LEN 255
  9030. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  9031. static void i40e_print_features(struct i40e_pf *pf)
  9032. {
  9033. struct i40e_hw *hw = &pf->hw;
  9034. char *buf;
  9035. int i;
  9036. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  9037. if (!buf)
  9038. return;
  9039. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  9040. #ifdef CONFIG_PCI_IOV
  9041. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  9042. #endif
  9043. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d RX: %s",
  9044. pf->hw.func_caps.num_vsis,
  9045. pf->vsi[pf->lan_vsi]->num_queue_pairs,
  9046. pf->flags & I40E_FLAG_RX_PS_ENABLED ? "PS" : "1BUF");
  9047. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  9048. i += snprintf(&buf[i], REMAIN(i), " RSS");
  9049. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  9050. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  9051. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9052. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  9053. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  9054. }
  9055. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  9056. i += snprintf(&buf[i], REMAIN(i), " DCB");
  9057. #if IS_ENABLED(CONFIG_VXLAN)
  9058. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  9059. #endif
  9060. if (pf->flags & I40E_FLAG_PTP)
  9061. i += snprintf(&buf[i], REMAIN(i), " PTP");
  9062. #ifdef I40E_FCOE
  9063. if (pf->flags & I40E_FLAG_FCOE_ENABLED)
  9064. i += snprintf(&buf[i], REMAIN(i), " FCOE");
  9065. #endif
  9066. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  9067. i += snprintf(&buf[i], REMAIN(i), " VEB");
  9068. else
  9069. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  9070. dev_info(&pf->pdev->dev, "%s\n", buf);
  9071. kfree(buf);
  9072. WARN_ON(i > INFO_STRING_LEN);
  9073. }
  9074. /**
  9075. * i40e_probe - Device initialization routine
  9076. * @pdev: PCI device information struct
  9077. * @ent: entry in i40e_pci_tbl
  9078. *
  9079. * i40e_probe initializes a PF identified by a pci_dev structure.
  9080. * The OS initialization, configuring of the PF private structure,
  9081. * and a hardware reset occur.
  9082. *
  9083. * Returns 0 on success, negative on failure
  9084. **/
  9085. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  9086. {
  9087. struct i40e_aq_get_phy_abilities_resp abilities;
  9088. struct i40e_pf *pf;
  9089. struct i40e_hw *hw;
  9090. static u16 pfs_found;
  9091. u16 wol_nvm_bits;
  9092. u16 link_status;
  9093. int err;
  9094. u32 len;
  9095. u32 val;
  9096. u32 i;
  9097. u8 set_fc_aq_fail;
  9098. err = pci_enable_device_mem(pdev);
  9099. if (err)
  9100. return err;
  9101. /* set up for high or low dma */
  9102. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  9103. if (err) {
  9104. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  9105. if (err) {
  9106. dev_err(&pdev->dev,
  9107. "DMA configuration failed: 0x%x\n", err);
  9108. goto err_dma;
  9109. }
  9110. }
  9111. /* set up pci connections */
  9112. err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
  9113. IORESOURCE_MEM), i40e_driver_name);
  9114. if (err) {
  9115. dev_info(&pdev->dev,
  9116. "pci_request_selected_regions failed %d\n", err);
  9117. goto err_pci_reg;
  9118. }
  9119. pci_enable_pcie_error_reporting(pdev);
  9120. pci_set_master(pdev);
  9121. /* Now that we have a PCI connection, we need to do the
  9122. * low level device setup. This is primarily setting up
  9123. * the Admin Queue structures and then querying for the
  9124. * device's current profile information.
  9125. */
  9126. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  9127. if (!pf) {
  9128. err = -ENOMEM;
  9129. goto err_pf_alloc;
  9130. }
  9131. pf->next_vsi = 0;
  9132. pf->pdev = pdev;
  9133. set_bit(__I40E_DOWN, &pf->state);
  9134. hw = &pf->hw;
  9135. hw->back = pf;
  9136. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  9137. I40E_MAX_CSR_SPACE);
  9138. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  9139. if (!hw->hw_addr) {
  9140. err = -EIO;
  9141. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  9142. (unsigned int)pci_resource_start(pdev, 0),
  9143. pf->ioremap_len, err);
  9144. goto err_ioremap;
  9145. }
  9146. hw->vendor_id = pdev->vendor;
  9147. hw->device_id = pdev->device;
  9148. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  9149. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  9150. hw->subsystem_device_id = pdev->subsystem_device;
  9151. hw->bus.device = PCI_SLOT(pdev->devfn);
  9152. hw->bus.func = PCI_FUNC(pdev->devfn);
  9153. pf->instance = pfs_found;
  9154. if (debug != -1) {
  9155. pf->msg_enable = pf->hw.debug_mask;
  9156. pf->msg_enable = debug;
  9157. }
  9158. /* do a special CORER for clearing PXE mode once at init */
  9159. if (hw->revision_id == 0 &&
  9160. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  9161. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  9162. i40e_flush(hw);
  9163. msleep(200);
  9164. pf->corer_count++;
  9165. i40e_clear_pxe_mode(hw);
  9166. }
  9167. /* Reset here to make sure all is clean and to define PF 'n' */
  9168. i40e_clear_hw(hw);
  9169. err = i40e_pf_reset(hw);
  9170. if (err) {
  9171. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  9172. goto err_pf_reset;
  9173. }
  9174. pf->pfr_count++;
  9175. hw->aq.num_arq_entries = I40E_AQ_LEN;
  9176. hw->aq.num_asq_entries = I40E_AQ_LEN;
  9177. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9178. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9179. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  9180. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  9181. "%s-%s:misc",
  9182. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  9183. err = i40e_init_shared_code(hw);
  9184. if (err) {
  9185. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  9186. err);
  9187. goto err_pf_reset;
  9188. }
  9189. /* set up a default setting for link flow control */
  9190. pf->hw.fc.requested_mode = I40E_FC_NONE;
  9191. err = i40e_init_adminq(hw);
  9192. if (err) {
  9193. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  9194. dev_info(&pdev->dev,
  9195. "The driver for the device stopped because the NVM image is newer than expected. You must install the most recent version of the network driver.\n");
  9196. else
  9197. dev_info(&pdev->dev,
  9198. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  9199. goto err_pf_reset;
  9200. }
  9201. /* provide nvm, fw, api versions */
  9202. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  9203. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  9204. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  9205. i40e_nvm_version_str(hw));
  9206. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  9207. hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
  9208. dev_info(&pdev->dev,
  9209. "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
  9210. else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
  9211. hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
  9212. dev_info(&pdev->dev,
  9213. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  9214. i40e_verify_eeprom(pf);
  9215. /* Rev 0 hardware was never productized */
  9216. if (hw->revision_id < 1)
  9217. 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");
  9218. i40e_clear_pxe_mode(hw);
  9219. err = i40e_get_capabilities(pf);
  9220. if (err)
  9221. goto err_adminq_setup;
  9222. err = i40e_sw_init(pf);
  9223. if (err) {
  9224. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  9225. goto err_sw_init;
  9226. }
  9227. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  9228. hw->func_caps.num_rx_qp,
  9229. pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
  9230. if (err) {
  9231. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  9232. goto err_init_lan_hmc;
  9233. }
  9234. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  9235. if (err) {
  9236. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  9237. err = -ENOENT;
  9238. goto err_configure_lan_hmc;
  9239. }
  9240. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  9241. * Ignore error return codes because if it was already disabled via
  9242. * hardware settings this will fail
  9243. */
  9244. if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  9245. (pf->hw.aq.fw_maj_ver < 4)) {
  9246. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  9247. i40e_aq_stop_lldp(hw, true, NULL);
  9248. }
  9249. i40e_get_mac_addr(hw, hw->mac.addr);
  9250. if (!is_valid_ether_addr(hw->mac.addr)) {
  9251. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  9252. err = -EIO;
  9253. goto err_mac_addr;
  9254. }
  9255. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  9256. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  9257. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  9258. if (is_valid_ether_addr(hw->mac.port_addr))
  9259. pf->flags |= I40E_FLAG_PORT_ID_VALID;
  9260. #ifdef I40E_FCOE
  9261. err = i40e_get_san_mac_addr(hw, hw->mac.san_addr);
  9262. if (err)
  9263. dev_info(&pdev->dev,
  9264. "(non-fatal) SAN MAC retrieval failed: %d\n", err);
  9265. if (!is_valid_ether_addr(hw->mac.san_addr)) {
  9266. dev_warn(&pdev->dev, "invalid SAN MAC address %pM, falling back to LAN MAC\n",
  9267. hw->mac.san_addr);
  9268. ether_addr_copy(hw->mac.san_addr, hw->mac.addr);
  9269. }
  9270. dev_info(&pf->pdev->dev, "SAN MAC: %pM\n", hw->mac.san_addr);
  9271. #endif /* I40E_FCOE */
  9272. pci_set_drvdata(pdev, pf);
  9273. pci_save_state(pdev);
  9274. #ifdef CONFIG_I40E_DCB
  9275. err = i40e_init_pf_dcb(pf);
  9276. if (err) {
  9277. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  9278. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  9279. /* Continue without DCB enabled */
  9280. }
  9281. #endif /* CONFIG_I40E_DCB */
  9282. /* set up periodic task facility */
  9283. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  9284. pf->service_timer_period = HZ;
  9285. INIT_WORK(&pf->service_task, i40e_service_task);
  9286. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  9287. pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
  9288. /* NVM bit on means WoL disabled for the port */
  9289. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  9290. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  9291. pf->wol_en = false;
  9292. else
  9293. pf->wol_en = true;
  9294. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  9295. /* set up the main switch operations */
  9296. i40e_determine_queue_usage(pf);
  9297. err = i40e_init_interrupt_scheme(pf);
  9298. if (err)
  9299. goto err_switch_setup;
  9300. /* The number of VSIs reported by the FW is the minimum guaranteed
  9301. * to us; HW supports far more and we share the remaining pool with
  9302. * the other PFs. We allocate space for more than the guarantee with
  9303. * the understanding that we might not get them all later.
  9304. */
  9305. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  9306. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  9307. else
  9308. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  9309. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  9310. len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
  9311. pf->vsi = kzalloc(len, GFP_KERNEL);
  9312. if (!pf->vsi) {
  9313. err = -ENOMEM;
  9314. goto err_switch_setup;
  9315. }
  9316. #ifdef CONFIG_PCI_IOV
  9317. /* prep for VF support */
  9318. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9319. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9320. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9321. if (pci_num_vf(pdev))
  9322. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  9323. }
  9324. #endif
  9325. err = i40e_setup_pf_switch(pf, false);
  9326. if (err) {
  9327. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  9328. goto err_vsis;
  9329. }
  9330. /* Make sure flow control is set according to current settings */
  9331. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  9332. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  9333. dev_dbg(&pf->pdev->dev,
  9334. "Set fc with err %s aq_err %s on get_phy_cap\n",
  9335. i40e_stat_str(hw, err),
  9336. i40e_aq_str(hw, hw->aq.asq_last_status));
  9337. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  9338. dev_dbg(&pf->pdev->dev,
  9339. "Set fc with err %s aq_err %s on set_phy_config\n",
  9340. i40e_stat_str(hw, err),
  9341. i40e_aq_str(hw, hw->aq.asq_last_status));
  9342. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  9343. dev_dbg(&pf->pdev->dev,
  9344. "Set fc with err %s aq_err %s on get_link_info\n",
  9345. i40e_stat_str(hw, err),
  9346. i40e_aq_str(hw, hw->aq.asq_last_status));
  9347. /* if FDIR VSI was set up, start it now */
  9348. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9349. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  9350. i40e_vsi_open(pf->vsi[i]);
  9351. break;
  9352. }
  9353. }
  9354. /* driver is only interested in link up/down and module qualification
  9355. * reports from firmware
  9356. */
  9357. err = i40e_aq_set_phy_int_mask(&pf->hw,
  9358. I40E_AQ_EVENT_LINK_UPDOWN |
  9359. I40E_AQ_EVENT_MODULE_QUAL_FAIL, NULL);
  9360. if (err)
  9361. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  9362. i40e_stat_str(&pf->hw, err),
  9363. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9364. /* Reconfigure hardware for allowing smaller MSS in the case
  9365. * of TSO, so that we avoid the MDD being fired and causing
  9366. * a reset in the case of small MSS+TSO.
  9367. */
  9368. val = rd32(hw, I40E_REG_MSS);
  9369. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  9370. val &= ~I40E_REG_MSS_MIN_MASK;
  9371. val |= I40E_64BYTE_MSS;
  9372. wr32(hw, I40E_REG_MSS, val);
  9373. }
  9374. if (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  9375. (pf->hw.aq.fw_maj_ver < 4)) {
  9376. msleep(75);
  9377. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  9378. if (err)
  9379. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  9380. i40e_stat_str(&pf->hw, err),
  9381. i40e_aq_str(&pf->hw,
  9382. pf->hw.aq.asq_last_status));
  9383. }
  9384. /* The main driver is (mostly) up and happy. We need to set this state
  9385. * before setting up the misc vector or we get a race and the vector
  9386. * ends up disabled forever.
  9387. */
  9388. clear_bit(__I40E_DOWN, &pf->state);
  9389. /* In case of MSIX we are going to setup the misc vector right here
  9390. * to handle admin queue events etc. In case of legacy and MSI
  9391. * the misc functionality and queue processing is combined in
  9392. * the same vector and that gets setup at open.
  9393. */
  9394. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9395. err = i40e_setup_misc_vector(pf);
  9396. if (err) {
  9397. dev_info(&pdev->dev,
  9398. "setup of misc vector failed: %d\n", err);
  9399. goto err_vsis;
  9400. }
  9401. }
  9402. #ifdef CONFIG_PCI_IOV
  9403. /* prep for VF support */
  9404. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9405. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9406. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9407. u32 val;
  9408. /* disable link interrupts for VFs */
  9409. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  9410. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  9411. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  9412. i40e_flush(hw);
  9413. if (pci_num_vf(pdev)) {
  9414. dev_info(&pdev->dev,
  9415. "Active VFs found, allocating resources.\n");
  9416. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  9417. if (err)
  9418. dev_info(&pdev->dev,
  9419. "Error %d allocating resources for existing VFs\n",
  9420. err);
  9421. }
  9422. }
  9423. #endif /* CONFIG_PCI_IOV */
  9424. pfs_found++;
  9425. i40e_dbg_pf_init(pf);
  9426. /* tell the firmware that we're starting */
  9427. i40e_send_version(pf);
  9428. /* since everything's happy, start the service_task timer */
  9429. mod_timer(&pf->service_timer,
  9430. round_jiffies(jiffies + pf->service_timer_period));
  9431. #ifdef I40E_FCOE
  9432. /* create FCoE interface */
  9433. i40e_fcoe_vsi_setup(pf);
  9434. #endif
  9435. #define PCI_SPEED_SIZE 8
  9436. #define PCI_WIDTH_SIZE 8
  9437. /* Devices on the IOSF bus do not have this information
  9438. * and will report PCI Gen 1 x 1 by default so don't bother
  9439. * checking them.
  9440. */
  9441. if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
  9442. char speed[PCI_SPEED_SIZE] = "Unknown";
  9443. char width[PCI_WIDTH_SIZE] = "Unknown";
  9444. /* Get the negotiated link width and speed from PCI config
  9445. * space
  9446. */
  9447. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  9448. &link_status);
  9449. i40e_set_pci_config_data(hw, link_status);
  9450. switch (hw->bus.speed) {
  9451. case i40e_bus_speed_8000:
  9452. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  9453. case i40e_bus_speed_5000:
  9454. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  9455. case i40e_bus_speed_2500:
  9456. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  9457. default:
  9458. break;
  9459. }
  9460. switch (hw->bus.width) {
  9461. case i40e_bus_width_pcie_x8:
  9462. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  9463. case i40e_bus_width_pcie_x4:
  9464. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  9465. case i40e_bus_width_pcie_x2:
  9466. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  9467. case i40e_bus_width_pcie_x1:
  9468. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  9469. default:
  9470. break;
  9471. }
  9472. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  9473. speed, width);
  9474. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  9475. hw->bus.speed < i40e_bus_speed_8000) {
  9476. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  9477. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  9478. }
  9479. }
  9480. /* get the requested speeds from the fw */
  9481. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  9482. if (err)
  9483. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  9484. i40e_stat_str(&pf->hw, err),
  9485. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9486. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  9487. /* get the supported phy types from the fw */
  9488. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  9489. if (err)
  9490. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  9491. i40e_stat_str(&pf->hw, err),
  9492. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9493. pf->hw.phy.phy_types = le32_to_cpu(abilities.phy_type);
  9494. /* Add a filter to drop all Flow control frames from any VSI from being
  9495. * transmitted. By doing so we stop a malicious VF from sending out
  9496. * PAUSE or PFC frames and potentially controlling traffic for other
  9497. * PF/VF VSIs.
  9498. * The FW can still send Flow control frames if enabled.
  9499. */
  9500. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  9501. pf->main_vsi_seid);
  9502. /* print a string summarizing features */
  9503. i40e_print_features(pf);
  9504. return 0;
  9505. /* Unwind what we've done if something failed in the setup */
  9506. err_vsis:
  9507. set_bit(__I40E_DOWN, &pf->state);
  9508. i40e_clear_interrupt_scheme(pf);
  9509. kfree(pf->vsi);
  9510. err_switch_setup:
  9511. i40e_reset_interrupt_capability(pf);
  9512. del_timer_sync(&pf->service_timer);
  9513. err_mac_addr:
  9514. err_configure_lan_hmc:
  9515. (void)i40e_shutdown_lan_hmc(hw);
  9516. err_init_lan_hmc:
  9517. kfree(pf->qp_pile);
  9518. err_sw_init:
  9519. err_adminq_setup:
  9520. (void)i40e_shutdown_adminq(hw);
  9521. err_pf_reset:
  9522. iounmap(hw->hw_addr);
  9523. err_ioremap:
  9524. kfree(pf);
  9525. err_pf_alloc:
  9526. pci_disable_pcie_error_reporting(pdev);
  9527. pci_release_selected_regions(pdev,
  9528. pci_select_bars(pdev, IORESOURCE_MEM));
  9529. err_pci_reg:
  9530. err_dma:
  9531. pci_disable_device(pdev);
  9532. return err;
  9533. }
  9534. /**
  9535. * i40e_remove - Device removal routine
  9536. * @pdev: PCI device information struct
  9537. *
  9538. * i40e_remove is called by the PCI subsystem to alert the driver
  9539. * that is should release a PCI device. This could be caused by a
  9540. * Hot-Plug event, or because the driver is going to be removed from
  9541. * memory.
  9542. **/
  9543. static void i40e_remove(struct pci_dev *pdev)
  9544. {
  9545. struct i40e_pf *pf = pci_get_drvdata(pdev);
  9546. struct i40e_hw *hw = &pf->hw;
  9547. i40e_status ret_code;
  9548. int i;
  9549. i40e_dbg_pf_exit(pf);
  9550. i40e_ptp_stop(pf);
  9551. /* Disable RSS in hw */
  9552. wr32(hw, I40E_PFQF_HENA(0), 0);
  9553. wr32(hw, I40E_PFQF_HENA(1), 0);
  9554. /* no more scheduling of any task */
  9555. set_bit(__I40E_DOWN, &pf->state);
  9556. del_timer_sync(&pf->service_timer);
  9557. cancel_work_sync(&pf->service_task);
  9558. i40e_fdir_teardown(pf);
  9559. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  9560. i40e_free_vfs(pf);
  9561. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  9562. }
  9563. i40e_fdir_teardown(pf);
  9564. /* If there is a switch structure or any orphans, remove them.
  9565. * This will leave only the PF's VSI remaining.
  9566. */
  9567. for (i = 0; i < I40E_MAX_VEB; i++) {
  9568. if (!pf->veb[i])
  9569. continue;
  9570. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  9571. pf->veb[i]->uplink_seid == 0)
  9572. i40e_switch_branch_release(pf->veb[i]);
  9573. }
  9574. /* Now we can shutdown the PF's VSI, just before we kill
  9575. * adminq and hmc.
  9576. */
  9577. if (pf->vsi[pf->lan_vsi])
  9578. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  9579. /* shutdown and destroy the HMC */
  9580. if (pf->hw.hmc.hmc_obj) {
  9581. ret_code = i40e_shutdown_lan_hmc(&pf->hw);
  9582. if (ret_code)
  9583. dev_warn(&pdev->dev,
  9584. "Failed to destroy the HMC resources: %d\n",
  9585. ret_code);
  9586. }
  9587. /* shutdown the adminq */
  9588. ret_code = i40e_shutdown_adminq(&pf->hw);
  9589. if (ret_code)
  9590. dev_warn(&pdev->dev,
  9591. "Failed to destroy the Admin Queue resources: %d\n",
  9592. ret_code);
  9593. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  9594. i40e_clear_interrupt_scheme(pf);
  9595. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9596. if (pf->vsi[i]) {
  9597. i40e_vsi_clear_rings(pf->vsi[i]);
  9598. i40e_vsi_clear(pf->vsi[i]);
  9599. pf->vsi[i] = NULL;
  9600. }
  9601. }
  9602. for (i = 0; i < I40E_MAX_VEB; i++) {
  9603. kfree(pf->veb[i]);
  9604. pf->veb[i] = NULL;
  9605. }
  9606. kfree(pf->qp_pile);
  9607. kfree(pf->vsi);
  9608. iounmap(pf->hw.hw_addr);
  9609. kfree(pf);
  9610. pci_release_selected_regions(pdev,
  9611. pci_select_bars(pdev, IORESOURCE_MEM));
  9612. pci_disable_pcie_error_reporting(pdev);
  9613. pci_disable_device(pdev);
  9614. }
  9615. /**
  9616. * i40e_pci_error_detected - warning that something funky happened in PCI land
  9617. * @pdev: PCI device information struct
  9618. *
  9619. * Called to warn that something happened and the error handling steps
  9620. * are in progress. Allows the driver to quiesce things, be ready for
  9621. * remediation.
  9622. **/
  9623. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  9624. enum pci_channel_state error)
  9625. {
  9626. struct i40e_pf *pf = pci_get_drvdata(pdev);
  9627. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  9628. /* shutdown all operations */
  9629. if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
  9630. rtnl_lock();
  9631. i40e_prep_for_reset(pf);
  9632. rtnl_unlock();
  9633. }
  9634. /* Request a slot reset */
  9635. return PCI_ERS_RESULT_NEED_RESET;
  9636. }
  9637. /**
  9638. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  9639. * @pdev: PCI device information struct
  9640. *
  9641. * Called to find if the driver can work with the device now that
  9642. * the pci slot has been reset. If a basic connection seems good
  9643. * (registers are readable and have sane content) then return a
  9644. * happy little PCI_ERS_RESULT_xxx.
  9645. **/
  9646. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  9647. {
  9648. struct i40e_pf *pf = pci_get_drvdata(pdev);
  9649. pci_ers_result_t result;
  9650. int err;
  9651. u32 reg;
  9652. dev_dbg(&pdev->dev, "%s\n", __func__);
  9653. if (pci_enable_device_mem(pdev)) {
  9654. dev_info(&pdev->dev,
  9655. "Cannot re-enable PCI device after reset.\n");
  9656. result = PCI_ERS_RESULT_DISCONNECT;
  9657. } else {
  9658. pci_set_master(pdev);
  9659. pci_restore_state(pdev);
  9660. pci_save_state(pdev);
  9661. pci_wake_from_d3(pdev, false);
  9662. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  9663. if (reg == 0)
  9664. result = PCI_ERS_RESULT_RECOVERED;
  9665. else
  9666. result = PCI_ERS_RESULT_DISCONNECT;
  9667. }
  9668. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  9669. if (err) {
  9670. dev_info(&pdev->dev,
  9671. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  9672. err);
  9673. /* non-fatal, continue */
  9674. }
  9675. return result;
  9676. }
  9677. /**
  9678. * i40e_pci_error_resume - restart operations after PCI error recovery
  9679. * @pdev: PCI device information struct
  9680. *
  9681. * Called to allow the driver to bring things back up after PCI error
  9682. * and/or reset recovery has finished.
  9683. **/
  9684. static void i40e_pci_error_resume(struct pci_dev *pdev)
  9685. {
  9686. struct i40e_pf *pf = pci_get_drvdata(pdev);
  9687. dev_dbg(&pdev->dev, "%s\n", __func__);
  9688. if (test_bit(__I40E_SUSPENDED, &pf->state))
  9689. return;
  9690. rtnl_lock();
  9691. i40e_handle_reset_warning(pf);
  9692. rtnl_unlock();
  9693. }
  9694. /**
  9695. * i40e_shutdown - PCI callback for shutting down
  9696. * @pdev: PCI device information struct
  9697. **/
  9698. static void i40e_shutdown(struct pci_dev *pdev)
  9699. {
  9700. struct i40e_pf *pf = pci_get_drvdata(pdev);
  9701. struct i40e_hw *hw = &pf->hw;
  9702. set_bit(__I40E_SUSPENDED, &pf->state);
  9703. set_bit(__I40E_DOWN, &pf->state);
  9704. rtnl_lock();
  9705. i40e_prep_for_reset(pf);
  9706. rtnl_unlock();
  9707. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  9708. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  9709. del_timer_sync(&pf->service_timer);
  9710. cancel_work_sync(&pf->service_task);
  9711. i40e_fdir_teardown(pf);
  9712. rtnl_lock();
  9713. i40e_prep_for_reset(pf);
  9714. rtnl_unlock();
  9715. wr32(hw, I40E_PFPM_APM,
  9716. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  9717. wr32(hw, I40E_PFPM_WUFC,
  9718. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  9719. i40e_clear_interrupt_scheme(pf);
  9720. if (system_state == SYSTEM_POWER_OFF) {
  9721. pci_wake_from_d3(pdev, pf->wol_en);
  9722. pci_set_power_state(pdev, PCI_D3hot);
  9723. }
  9724. }
  9725. #ifdef CONFIG_PM
  9726. /**
  9727. * i40e_suspend - PCI callback for moving to D3
  9728. * @pdev: PCI device information struct
  9729. **/
  9730. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  9731. {
  9732. struct i40e_pf *pf = pci_get_drvdata(pdev);
  9733. struct i40e_hw *hw = &pf->hw;
  9734. set_bit(__I40E_SUSPENDED, &pf->state);
  9735. set_bit(__I40E_DOWN, &pf->state);
  9736. rtnl_lock();
  9737. i40e_prep_for_reset(pf);
  9738. rtnl_unlock();
  9739. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  9740. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  9741. pci_wake_from_d3(pdev, pf->wol_en);
  9742. pci_set_power_state(pdev, PCI_D3hot);
  9743. return 0;
  9744. }
  9745. /**
  9746. * i40e_resume - PCI callback for waking up from D3
  9747. * @pdev: PCI device information struct
  9748. **/
  9749. static int i40e_resume(struct pci_dev *pdev)
  9750. {
  9751. struct i40e_pf *pf = pci_get_drvdata(pdev);
  9752. u32 err;
  9753. pci_set_power_state(pdev, PCI_D0);
  9754. pci_restore_state(pdev);
  9755. /* pci_restore_state() clears dev->state_saves, so
  9756. * call pci_save_state() again to restore it.
  9757. */
  9758. pci_save_state(pdev);
  9759. err = pci_enable_device_mem(pdev);
  9760. if (err) {
  9761. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  9762. return err;
  9763. }
  9764. pci_set_master(pdev);
  9765. /* no wakeup events while running */
  9766. pci_wake_from_d3(pdev, false);
  9767. /* handling the reset will rebuild the device state */
  9768. if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
  9769. clear_bit(__I40E_DOWN, &pf->state);
  9770. rtnl_lock();
  9771. i40e_reset_and_rebuild(pf, false);
  9772. rtnl_unlock();
  9773. }
  9774. return 0;
  9775. }
  9776. #endif
  9777. static const struct pci_error_handlers i40e_err_handler = {
  9778. .error_detected = i40e_pci_error_detected,
  9779. .slot_reset = i40e_pci_error_slot_reset,
  9780. .resume = i40e_pci_error_resume,
  9781. };
  9782. static struct pci_driver i40e_driver = {
  9783. .name = i40e_driver_name,
  9784. .id_table = i40e_pci_tbl,
  9785. .probe = i40e_probe,
  9786. .remove = i40e_remove,
  9787. #ifdef CONFIG_PM
  9788. .suspend = i40e_suspend,
  9789. .resume = i40e_resume,
  9790. #endif
  9791. .shutdown = i40e_shutdown,
  9792. .err_handler = &i40e_err_handler,
  9793. .sriov_configure = i40e_pci_sriov_configure,
  9794. };
  9795. /**
  9796. * i40e_init_module - Driver registration routine
  9797. *
  9798. * i40e_init_module is the first routine called when the driver is
  9799. * loaded. All it does is register with the PCI subsystem.
  9800. **/
  9801. static int __init i40e_init_module(void)
  9802. {
  9803. pr_info("%s: %s - version %s\n", i40e_driver_name,
  9804. i40e_driver_string, i40e_driver_version_str);
  9805. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  9806. i40e_dbg_init();
  9807. return pci_register_driver(&i40e_driver);
  9808. }
  9809. module_init(i40e_init_module);
  9810. /**
  9811. * i40e_exit_module - Driver exit cleanup routine
  9812. *
  9813. * i40e_exit_module is called just before the driver is removed
  9814. * from memory.
  9815. **/
  9816. static void __exit i40e_exit_module(void)
  9817. {
  9818. pci_unregister_driver(&i40e_driver);
  9819. i40e_dbg_exit();
  9820. }
  9821. module_exit(i40e_exit_module);