i40e_main.c 326 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include <linux/etherdevice.h>
  27. #include <linux/of_net.h>
  28. #include <linux/pci.h>
  29. /* Local includes */
  30. #include "i40e.h"
  31. #include "i40e_diag.h"
  32. #include <net/udp_tunnel.h>
  33. const char i40e_driver_name[] = "i40e";
  34. static const char i40e_driver_string[] =
  35. "Intel(R) Ethernet Connection XL710 Network Driver";
  36. #define DRV_KERN "-k"
  37. #define DRV_VERSION_MAJOR 2
  38. #define DRV_VERSION_MINOR 1
  39. #define DRV_VERSION_BUILD 7
  40. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  41. __stringify(DRV_VERSION_MINOR) "." \
  42. __stringify(DRV_VERSION_BUILD) DRV_KERN
  43. const char i40e_driver_version_str[] = DRV_VERSION;
  44. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  45. /* a bit of forward declarations */
  46. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  47. static void i40e_handle_reset_warning(struct i40e_pf *pf);
  48. static int i40e_add_vsi(struct i40e_vsi *vsi);
  49. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  50. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  51. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  52. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  53. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  54. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  55. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  56. /* i40e_pci_tbl - PCI Device ID Table
  57. *
  58. * Last entry must be all 0s
  59. *
  60. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  61. * Class, Class Mask, private data (not used) }
  62. */
  63. static const struct pci_device_id i40e_pci_tbl[] = {
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  71. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  72. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  73. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  74. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  75. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  76. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  81. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  82. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  83. /* required last entry */
  84. {0, }
  85. };
  86. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  87. #define I40E_MAX_VF_COUNT 128
  88. static int debug = -1;
  89. module_param(debug, uint, 0);
  90. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  91. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  92. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  93. MODULE_LICENSE("GPL");
  94. MODULE_VERSION(DRV_VERSION);
  95. static struct workqueue_struct *i40e_wq;
  96. /**
  97. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  98. * @hw: pointer to the HW structure
  99. * @mem: ptr to mem struct to fill out
  100. * @size: size of memory requested
  101. * @alignment: what to align the allocation to
  102. **/
  103. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  104. u64 size, u32 alignment)
  105. {
  106. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  107. mem->size = ALIGN(size, alignment);
  108. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  109. &mem->pa, GFP_KERNEL);
  110. if (!mem->va)
  111. return -ENOMEM;
  112. return 0;
  113. }
  114. /**
  115. * i40e_free_dma_mem_d - OS specific memory free for shared code
  116. * @hw: pointer to the HW structure
  117. * @mem: ptr to mem struct to free
  118. **/
  119. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  120. {
  121. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  122. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  123. mem->va = NULL;
  124. mem->pa = 0;
  125. mem->size = 0;
  126. return 0;
  127. }
  128. /**
  129. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  130. * @hw: pointer to the HW structure
  131. * @mem: ptr to mem struct to fill out
  132. * @size: size of memory requested
  133. **/
  134. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  135. u32 size)
  136. {
  137. mem->size = size;
  138. mem->va = kzalloc(size, GFP_KERNEL);
  139. if (!mem->va)
  140. return -ENOMEM;
  141. return 0;
  142. }
  143. /**
  144. * i40e_free_virt_mem_d - OS specific memory free for shared code
  145. * @hw: pointer to the HW structure
  146. * @mem: ptr to mem struct to free
  147. **/
  148. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  149. {
  150. /* it's ok to kfree a NULL pointer */
  151. kfree(mem->va);
  152. mem->va = NULL;
  153. mem->size = 0;
  154. return 0;
  155. }
  156. /**
  157. * i40e_get_lump - find a lump of free generic resource
  158. * @pf: board private structure
  159. * @pile: the pile of resource to search
  160. * @needed: the number of items needed
  161. * @id: an owner id to stick on the items assigned
  162. *
  163. * Returns the base item index of the lump, or negative for error
  164. *
  165. * The search_hint trick and lack of advanced fit-finding only work
  166. * because we're highly likely to have all the same size lump requests.
  167. * Linear search time and any fragmentation should be minimal.
  168. **/
  169. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  170. u16 needed, u16 id)
  171. {
  172. int ret = -ENOMEM;
  173. int i, j;
  174. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  175. dev_info(&pf->pdev->dev,
  176. "param err: pile=%p needed=%d id=0x%04x\n",
  177. pile, needed, id);
  178. return -EINVAL;
  179. }
  180. /* start the linear search with an imperfect hint */
  181. i = pile->search_hint;
  182. while (i < pile->num_entries) {
  183. /* skip already allocated entries */
  184. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  185. i++;
  186. continue;
  187. }
  188. /* do we have enough in this lump? */
  189. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  190. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  191. break;
  192. }
  193. if (j == needed) {
  194. /* there was enough, so assign it to the requestor */
  195. for (j = 0; j < needed; j++)
  196. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  197. ret = i;
  198. pile->search_hint = i + j;
  199. break;
  200. }
  201. /* not enough, so skip over it and continue looking */
  202. i += j;
  203. }
  204. return ret;
  205. }
  206. /**
  207. * i40e_put_lump - return a lump of generic resource
  208. * @pile: the pile of resource to search
  209. * @index: the base item index
  210. * @id: the owner id of the items assigned
  211. *
  212. * Returns the count of items in the lump
  213. **/
  214. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  215. {
  216. int valid_id = (id | I40E_PILE_VALID_BIT);
  217. int count = 0;
  218. int i;
  219. if (!pile || index >= pile->num_entries)
  220. return -EINVAL;
  221. for (i = index;
  222. i < pile->num_entries && pile->list[i] == valid_id;
  223. i++) {
  224. pile->list[i] = 0;
  225. count++;
  226. }
  227. if (count && index < pile->search_hint)
  228. pile->search_hint = index;
  229. return count;
  230. }
  231. /**
  232. * i40e_find_vsi_from_id - searches for the vsi with the given id
  233. * @pf - the pf structure to search for the vsi
  234. * @id - id of the vsi it is searching for
  235. **/
  236. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  237. {
  238. int i;
  239. for (i = 0; i < pf->num_alloc_vsi; i++)
  240. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  241. return pf->vsi[i];
  242. return NULL;
  243. }
  244. /**
  245. * i40e_service_event_schedule - Schedule the service task to wake up
  246. * @pf: board private structure
  247. *
  248. * If not already scheduled, this puts the task into the work queue
  249. **/
  250. void i40e_service_event_schedule(struct i40e_pf *pf)
  251. {
  252. if (!test_bit(__I40E_DOWN, &pf->state) &&
  253. !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  254. queue_work(i40e_wq, &pf->service_task);
  255. }
  256. /**
  257. * i40e_tx_timeout - Respond to a Tx Hang
  258. * @netdev: network interface device structure
  259. *
  260. * If any port has noticed a Tx timeout, it is likely that the whole
  261. * device is munged, not just the one netdev port, so go for the full
  262. * reset.
  263. **/
  264. static void i40e_tx_timeout(struct net_device *netdev)
  265. {
  266. struct i40e_netdev_priv *np = netdev_priv(netdev);
  267. struct i40e_vsi *vsi = np->vsi;
  268. struct i40e_pf *pf = vsi->back;
  269. struct i40e_ring *tx_ring = NULL;
  270. unsigned int i, hung_queue = 0;
  271. u32 head, val;
  272. pf->tx_timeout_count++;
  273. /* find the stopped queue the same way the stack does */
  274. for (i = 0; i < netdev->num_tx_queues; i++) {
  275. struct netdev_queue *q;
  276. unsigned long trans_start;
  277. q = netdev_get_tx_queue(netdev, i);
  278. trans_start = q->trans_start;
  279. if (netif_xmit_stopped(q) &&
  280. time_after(jiffies,
  281. (trans_start + netdev->watchdog_timeo))) {
  282. hung_queue = i;
  283. break;
  284. }
  285. }
  286. if (i == netdev->num_tx_queues) {
  287. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  288. } else {
  289. /* now that we have an index, find the tx_ring struct */
  290. for (i = 0; i < vsi->num_queue_pairs; i++) {
  291. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  292. if (hung_queue ==
  293. vsi->tx_rings[i]->queue_index) {
  294. tx_ring = vsi->tx_rings[i];
  295. break;
  296. }
  297. }
  298. }
  299. }
  300. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  301. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  302. else if (time_before(jiffies,
  303. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  304. return; /* don't do any new action before the next timeout */
  305. if (tx_ring) {
  306. head = i40e_get_head(tx_ring);
  307. /* Read interrupt register */
  308. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  309. val = rd32(&pf->hw,
  310. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  311. tx_ring->vsi->base_vector - 1));
  312. else
  313. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  314. 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",
  315. vsi->seid, hung_queue, tx_ring->next_to_clean,
  316. head, tx_ring->next_to_use,
  317. readl(tx_ring->tail), val);
  318. }
  319. pf->tx_timeout_last_recovery = jiffies;
  320. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  321. pf->tx_timeout_recovery_level, hung_queue);
  322. switch (pf->tx_timeout_recovery_level) {
  323. case 1:
  324. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  325. break;
  326. case 2:
  327. set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  328. break;
  329. case 3:
  330. set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  331. break;
  332. default:
  333. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  334. break;
  335. }
  336. i40e_service_event_schedule(pf);
  337. pf->tx_timeout_recovery_level++;
  338. }
  339. /**
  340. * i40e_get_vsi_stats_struct - Get System Network Statistics
  341. * @vsi: the VSI we care about
  342. *
  343. * Returns the address of the device statistics structure.
  344. * The statistics are actually updated from the service task.
  345. **/
  346. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  347. {
  348. return &vsi->net_stats;
  349. }
  350. /**
  351. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  352. * @netdev: network interface device structure
  353. *
  354. * Returns the address of the device statistics structure.
  355. * The statistics are actually updated from the service task.
  356. **/
  357. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  358. struct rtnl_link_stats64 *stats)
  359. {
  360. struct i40e_netdev_priv *np = netdev_priv(netdev);
  361. struct i40e_ring *tx_ring, *rx_ring;
  362. struct i40e_vsi *vsi = np->vsi;
  363. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  364. int i;
  365. if (test_bit(__I40E_DOWN, &vsi->state))
  366. return;
  367. if (!vsi->tx_rings)
  368. return;
  369. rcu_read_lock();
  370. for (i = 0; i < vsi->num_queue_pairs; i++) {
  371. u64 bytes, packets;
  372. unsigned int start;
  373. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  374. if (!tx_ring)
  375. continue;
  376. do {
  377. start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
  378. packets = tx_ring->stats.packets;
  379. bytes = tx_ring->stats.bytes;
  380. } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
  381. stats->tx_packets += packets;
  382. stats->tx_bytes += bytes;
  383. rx_ring = &tx_ring[1];
  384. do {
  385. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  386. packets = rx_ring->stats.packets;
  387. bytes = rx_ring->stats.bytes;
  388. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  389. stats->rx_packets += packets;
  390. stats->rx_bytes += bytes;
  391. }
  392. rcu_read_unlock();
  393. /* following stats updated by i40e_watchdog_subtask() */
  394. stats->multicast = vsi_stats->multicast;
  395. stats->tx_errors = vsi_stats->tx_errors;
  396. stats->tx_dropped = vsi_stats->tx_dropped;
  397. stats->rx_errors = vsi_stats->rx_errors;
  398. stats->rx_dropped = vsi_stats->rx_dropped;
  399. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  400. stats->rx_length_errors = vsi_stats->rx_length_errors;
  401. }
  402. /**
  403. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  404. * @vsi: the VSI to have its stats reset
  405. **/
  406. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  407. {
  408. struct rtnl_link_stats64 *ns;
  409. int i;
  410. if (!vsi)
  411. return;
  412. ns = i40e_get_vsi_stats_struct(vsi);
  413. memset(ns, 0, sizeof(*ns));
  414. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  415. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  416. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  417. if (vsi->rx_rings && vsi->rx_rings[0]) {
  418. for (i = 0; i < vsi->num_queue_pairs; i++) {
  419. memset(&vsi->rx_rings[i]->stats, 0,
  420. sizeof(vsi->rx_rings[i]->stats));
  421. memset(&vsi->rx_rings[i]->rx_stats, 0,
  422. sizeof(vsi->rx_rings[i]->rx_stats));
  423. memset(&vsi->tx_rings[i]->stats, 0,
  424. sizeof(vsi->tx_rings[i]->stats));
  425. memset(&vsi->tx_rings[i]->tx_stats, 0,
  426. sizeof(vsi->tx_rings[i]->tx_stats));
  427. }
  428. }
  429. vsi->stat_offsets_loaded = false;
  430. }
  431. /**
  432. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  433. * @pf: the PF to be reset
  434. **/
  435. void i40e_pf_reset_stats(struct i40e_pf *pf)
  436. {
  437. int i;
  438. memset(&pf->stats, 0, sizeof(pf->stats));
  439. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  440. pf->stat_offsets_loaded = false;
  441. for (i = 0; i < I40E_MAX_VEB; i++) {
  442. if (pf->veb[i]) {
  443. memset(&pf->veb[i]->stats, 0,
  444. sizeof(pf->veb[i]->stats));
  445. memset(&pf->veb[i]->stats_offsets, 0,
  446. sizeof(pf->veb[i]->stats_offsets));
  447. pf->veb[i]->stat_offsets_loaded = false;
  448. }
  449. }
  450. pf->hw_csum_rx_error = 0;
  451. }
  452. /**
  453. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  454. * @hw: ptr to the hardware info
  455. * @hireg: the high 32 bit reg to read
  456. * @loreg: the low 32 bit reg to read
  457. * @offset_loaded: has the initial offset been loaded yet
  458. * @offset: ptr to current offset value
  459. * @stat: ptr to the stat
  460. *
  461. * Since the device stats are not reset at PFReset, they likely will not
  462. * be zeroed when the driver starts. We'll save the first values read
  463. * and use them as offsets to be subtracted from the raw values in order
  464. * to report stats that count from zero. In the process, we also manage
  465. * the potential roll-over.
  466. **/
  467. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  468. bool offset_loaded, u64 *offset, u64 *stat)
  469. {
  470. u64 new_data;
  471. if (hw->device_id == I40E_DEV_ID_QEMU) {
  472. new_data = rd32(hw, loreg);
  473. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  474. } else {
  475. new_data = rd64(hw, loreg);
  476. }
  477. if (!offset_loaded)
  478. *offset = new_data;
  479. if (likely(new_data >= *offset))
  480. *stat = new_data - *offset;
  481. else
  482. *stat = (new_data + BIT_ULL(48)) - *offset;
  483. *stat &= 0xFFFFFFFFFFFFULL;
  484. }
  485. /**
  486. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  487. * @hw: ptr to the hardware info
  488. * @reg: the hw reg to read
  489. * @offset_loaded: has the initial offset been loaded yet
  490. * @offset: ptr to current offset value
  491. * @stat: ptr to the stat
  492. **/
  493. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  494. bool offset_loaded, u64 *offset, u64 *stat)
  495. {
  496. u32 new_data;
  497. new_data = rd32(hw, reg);
  498. if (!offset_loaded)
  499. *offset = new_data;
  500. if (likely(new_data >= *offset))
  501. *stat = (u32)(new_data - *offset);
  502. else
  503. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  504. }
  505. /**
  506. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  507. * @vsi: the VSI to be updated
  508. **/
  509. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  510. {
  511. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  512. struct i40e_pf *pf = vsi->back;
  513. struct i40e_hw *hw = &pf->hw;
  514. struct i40e_eth_stats *oes;
  515. struct i40e_eth_stats *es; /* device's eth stats */
  516. es = &vsi->eth_stats;
  517. oes = &vsi->eth_stats_offsets;
  518. /* Gather up the stats that the hw collects */
  519. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  520. vsi->stat_offsets_loaded,
  521. &oes->tx_errors, &es->tx_errors);
  522. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  523. vsi->stat_offsets_loaded,
  524. &oes->rx_discards, &es->rx_discards);
  525. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  526. vsi->stat_offsets_loaded,
  527. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  528. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  529. vsi->stat_offsets_loaded,
  530. &oes->tx_errors, &es->tx_errors);
  531. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  532. I40E_GLV_GORCL(stat_idx),
  533. vsi->stat_offsets_loaded,
  534. &oes->rx_bytes, &es->rx_bytes);
  535. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  536. I40E_GLV_UPRCL(stat_idx),
  537. vsi->stat_offsets_loaded,
  538. &oes->rx_unicast, &es->rx_unicast);
  539. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  540. I40E_GLV_MPRCL(stat_idx),
  541. vsi->stat_offsets_loaded,
  542. &oes->rx_multicast, &es->rx_multicast);
  543. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  544. I40E_GLV_BPRCL(stat_idx),
  545. vsi->stat_offsets_loaded,
  546. &oes->rx_broadcast, &es->rx_broadcast);
  547. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  548. I40E_GLV_GOTCL(stat_idx),
  549. vsi->stat_offsets_loaded,
  550. &oes->tx_bytes, &es->tx_bytes);
  551. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  552. I40E_GLV_UPTCL(stat_idx),
  553. vsi->stat_offsets_loaded,
  554. &oes->tx_unicast, &es->tx_unicast);
  555. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  556. I40E_GLV_MPTCL(stat_idx),
  557. vsi->stat_offsets_loaded,
  558. &oes->tx_multicast, &es->tx_multicast);
  559. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  560. I40E_GLV_BPTCL(stat_idx),
  561. vsi->stat_offsets_loaded,
  562. &oes->tx_broadcast, &es->tx_broadcast);
  563. vsi->stat_offsets_loaded = true;
  564. }
  565. /**
  566. * i40e_update_veb_stats - Update Switch component statistics
  567. * @veb: the VEB being updated
  568. **/
  569. static void i40e_update_veb_stats(struct i40e_veb *veb)
  570. {
  571. struct i40e_pf *pf = veb->pf;
  572. struct i40e_hw *hw = &pf->hw;
  573. struct i40e_eth_stats *oes;
  574. struct i40e_eth_stats *es; /* device's eth stats */
  575. struct i40e_veb_tc_stats *veb_oes;
  576. struct i40e_veb_tc_stats *veb_es;
  577. int i, idx = 0;
  578. idx = veb->stats_idx;
  579. es = &veb->stats;
  580. oes = &veb->stats_offsets;
  581. veb_es = &veb->tc_stats;
  582. veb_oes = &veb->tc_stats_offsets;
  583. /* Gather up the stats that the hw collects */
  584. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  585. veb->stat_offsets_loaded,
  586. &oes->tx_discards, &es->tx_discards);
  587. if (hw->revision_id > 0)
  588. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  589. veb->stat_offsets_loaded,
  590. &oes->rx_unknown_protocol,
  591. &es->rx_unknown_protocol);
  592. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  593. veb->stat_offsets_loaded,
  594. &oes->rx_bytes, &es->rx_bytes);
  595. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  596. veb->stat_offsets_loaded,
  597. &oes->rx_unicast, &es->rx_unicast);
  598. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  599. veb->stat_offsets_loaded,
  600. &oes->rx_multicast, &es->rx_multicast);
  601. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  602. veb->stat_offsets_loaded,
  603. &oes->rx_broadcast, &es->rx_broadcast);
  604. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  605. veb->stat_offsets_loaded,
  606. &oes->tx_bytes, &es->tx_bytes);
  607. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  608. veb->stat_offsets_loaded,
  609. &oes->tx_unicast, &es->tx_unicast);
  610. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  611. veb->stat_offsets_loaded,
  612. &oes->tx_multicast, &es->tx_multicast);
  613. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  614. veb->stat_offsets_loaded,
  615. &oes->tx_broadcast, &es->tx_broadcast);
  616. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  617. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  618. I40E_GLVEBTC_RPCL(i, idx),
  619. veb->stat_offsets_loaded,
  620. &veb_oes->tc_rx_packets[i],
  621. &veb_es->tc_rx_packets[i]);
  622. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  623. I40E_GLVEBTC_RBCL(i, idx),
  624. veb->stat_offsets_loaded,
  625. &veb_oes->tc_rx_bytes[i],
  626. &veb_es->tc_rx_bytes[i]);
  627. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  628. I40E_GLVEBTC_TPCL(i, idx),
  629. veb->stat_offsets_loaded,
  630. &veb_oes->tc_tx_packets[i],
  631. &veb_es->tc_tx_packets[i]);
  632. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  633. I40E_GLVEBTC_TBCL(i, idx),
  634. veb->stat_offsets_loaded,
  635. &veb_oes->tc_tx_bytes[i],
  636. &veb_es->tc_tx_bytes[i]);
  637. }
  638. veb->stat_offsets_loaded = true;
  639. }
  640. /**
  641. * i40e_update_vsi_stats - Update the vsi statistics counters.
  642. * @vsi: the VSI to be updated
  643. *
  644. * There are a few instances where we store the same stat in a
  645. * couple of different structs. This is partly because we have
  646. * the netdev stats that need to be filled out, which is slightly
  647. * different from the "eth_stats" defined by the chip and used in
  648. * VF communications. We sort it out here.
  649. **/
  650. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  651. {
  652. struct i40e_pf *pf = vsi->back;
  653. struct rtnl_link_stats64 *ons;
  654. struct rtnl_link_stats64 *ns; /* netdev stats */
  655. struct i40e_eth_stats *oes;
  656. struct i40e_eth_stats *es; /* device's eth stats */
  657. u32 tx_restart, tx_busy;
  658. u64 tx_lost_interrupt;
  659. struct i40e_ring *p;
  660. u32 rx_page, rx_buf;
  661. u64 bytes, packets;
  662. unsigned int start;
  663. u64 tx_linearize;
  664. u64 tx_force_wb;
  665. u64 rx_p, rx_b;
  666. u64 tx_p, tx_b;
  667. u16 q;
  668. if (test_bit(__I40E_DOWN, &vsi->state) ||
  669. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  670. return;
  671. ns = i40e_get_vsi_stats_struct(vsi);
  672. ons = &vsi->net_stats_offsets;
  673. es = &vsi->eth_stats;
  674. oes = &vsi->eth_stats_offsets;
  675. /* Gather up the netdev and vsi stats that the driver collects
  676. * on the fly during packet processing
  677. */
  678. rx_b = rx_p = 0;
  679. tx_b = tx_p = 0;
  680. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  681. tx_lost_interrupt = 0;
  682. rx_page = 0;
  683. rx_buf = 0;
  684. rcu_read_lock();
  685. for (q = 0; q < vsi->num_queue_pairs; q++) {
  686. /* locate Tx ring */
  687. p = ACCESS_ONCE(vsi->tx_rings[q]);
  688. do {
  689. start = u64_stats_fetch_begin_irq(&p->syncp);
  690. packets = p->stats.packets;
  691. bytes = p->stats.bytes;
  692. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  693. tx_b += bytes;
  694. tx_p += packets;
  695. tx_restart += p->tx_stats.restart_queue;
  696. tx_busy += p->tx_stats.tx_busy;
  697. tx_linearize += p->tx_stats.tx_linearize;
  698. tx_force_wb += p->tx_stats.tx_force_wb;
  699. tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
  700. /* Rx queue is part of the same block as Tx queue */
  701. p = &p[1];
  702. do {
  703. start = u64_stats_fetch_begin_irq(&p->syncp);
  704. packets = p->stats.packets;
  705. bytes = p->stats.bytes;
  706. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  707. rx_b += bytes;
  708. rx_p += packets;
  709. rx_buf += p->rx_stats.alloc_buff_failed;
  710. rx_page += p->rx_stats.alloc_page_failed;
  711. }
  712. rcu_read_unlock();
  713. vsi->tx_restart = tx_restart;
  714. vsi->tx_busy = tx_busy;
  715. vsi->tx_linearize = tx_linearize;
  716. vsi->tx_force_wb = tx_force_wb;
  717. vsi->tx_lost_interrupt = tx_lost_interrupt;
  718. vsi->rx_page_failed = rx_page;
  719. vsi->rx_buf_failed = rx_buf;
  720. ns->rx_packets = rx_p;
  721. ns->rx_bytes = rx_b;
  722. ns->tx_packets = tx_p;
  723. ns->tx_bytes = tx_b;
  724. /* update netdev stats from eth stats */
  725. i40e_update_eth_stats(vsi);
  726. ons->tx_errors = oes->tx_errors;
  727. ns->tx_errors = es->tx_errors;
  728. ons->multicast = oes->rx_multicast;
  729. ns->multicast = es->rx_multicast;
  730. ons->rx_dropped = oes->rx_discards;
  731. ns->rx_dropped = es->rx_discards;
  732. ons->tx_dropped = oes->tx_discards;
  733. ns->tx_dropped = es->tx_discards;
  734. /* pull in a couple PF stats if this is the main vsi */
  735. if (vsi == pf->vsi[pf->lan_vsi]) {
  736. ns->rx_crc_errors = pf->stats.crc_errors;
  737. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  738. ns->rx_length_errors = pf->stats.rx_length_errors;
  739. }
  740. }
  741. /**
  742. * i40e_update_pf_stats - Update the PF statistics counters.
  743. * @pf: the PF to be updated
  744. **/
  745. static void i40e_update_pf_stats(struct i40e_pf *pf)
  746. {
  747. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  748. struct i40e_hw_port_stats *nsd = &pf->stats;
  749. struct i40e_hw *hw = &pf->hw;
  750. u32 val;
  751. int i;
  752. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  753. I40E_GLPRT_GORCL(hw->port),
  754. pf->stat_offsets_loaded,
  755. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  756. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  757. I40E_GLPRT_GOTCL(hw->port),
  758. pf->stat_offsets_loaded,
  759. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  760. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  761. pf->stat_offsets_loaded,
  762. &osd->eth.rx_discards,
  763. &nsd->eth.rx_discards);
  764. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  765. I40E_GLPRT_UPRCL(hw->port),
  766. pf->stat_offsets_loaded,
  767. &osd->eth.rx_unicast,
  768. &nsd->eth.rx_unicast);
  769. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  770. I40E_GLPRT_MPRCL(hw->port),
  771. pf->stat_offsets_loaded,
  772. &osd->eth.rx_multicast,
  773. &nsd->eth.rx_multicast);
  774. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  775. I40E_GLPRT_BPRCL(hw->port),
  776. pf->stat_offsets_loaded,
  777. &osd->eth.rx_broadcast,
  778. &nsd->eth.rx_broadcast);
  779. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  780. I40E_GLPRT_UPTCL(hw->port),
  781. pf->stat_offsets_loaded,
  782. &osd->eth.tx_unicast,
  783. &nsd->eth.tx_unicast);
  784. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  785. I40E_GLPRT_MPTCL(hw->port),
  786. pf->stat_offsets_loaded,
  787. &osd->eth.tx_multicast,
  788. &nsd->eth.tx_multicast);
  789. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  790. I40E_GLPRT_BPTCL(hw->port),
  791. pf->stat_offsets_loaded,
  792. &osd->eth.tx_broadcast,
  793. &nsd->eth.tx_broadcast);
  794. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  795. pf->stat_offsets_loaded,
  796. &osd->tx_dropped_link_down,
  797. &nsd->tx_dropped_link_down);
  798. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  799. pf->stat_offsets_loaded,
  800. &osd->crc_errors, &nsd->crc_errors);
  801. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  802. pf->stat_offsets_loaded,
  803. &osd->illegal_bytes, &nsd->illegal_bytes);
  804. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  805. pf->stat_offsets_loaded,
  806. &osd->mac_local_faults,
  807. &nsd->mac_local_faults);
  808. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  809. pf->stat_offsets_loaded,
  810. &osd->mac_remote_faults,
  811. &nsd->mac_remote_faults);
  812. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  813. pf->stat_offsets_loaded,
  814. &osd->rx_length_errors,
  815. &nsd->rx_length_errors);
  816. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  817. pf->stat_offsets_loaded,
  818. &osd->link_xon_rx, &nsd->link_xon_rx);
  819. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  820. pf->stat_offsets_loaded,
  821. &osd->link_xon_tx, &nsd->link_xon_tx);
  822. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  823. pf->stat_offsets_loaded,
  824. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  825. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  826. pf->stat_offsets_loaded,
  827. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  828. for (i = 0; i < 8; i++) {
  829. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  830. pf->stat_offsets_loaded,
  831. &osd->priority_xoff_rx[i],
  832. &nsd->priority_xoff_rx[i]);
  833. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  834. pf->stat_offsets_loaded,
  835. &osd->priority_xon_rx[i],
  836. &nsd->priority_xon_rx[i]);
  837. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  838. pf->stat_offsets_loaded,
  839. &osd->priority_xon_tx[i],
  840. &nsd->priority_xon_tx[i]);
  841. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  842. pf->stat_offsets_loaded,
  843. &osd->priority_xoff_tx[i],
  844. &nsd->priority_xoff_tx[i]);
  845. i40e_stat_update32(hw,
  846. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  847. pf->stat_offsets_loaded,
  848. &osd->priority_xon_2_xoff[i],
  849. &nsd->priority_xon_2_xoff[i]);
  850. }
  851. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  852. I40E_GLPRT_PRC64L(hw->port),
  853. pf->stat_offsets_loaded,
  854. &osd->rx_size_64, &nsd->rx_size_64);
  855. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  856. I40E_GLPRT_PRC127L(hw->port),
  857. pf->stat_offsets_loaded,
  858. &osd->rx_size_127, &nsd->rx_size_127);
  859. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  860. I40E_GLPRT_PRC255L(hw->port),
  861. pf->stat_offsets_loaded,
  862. &osd->rx_size_255, &nsd->rx_size_255);
  863. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  864. I40E_GLPRT_PRC511L(hw->port),
  865. pf->stat_offsets_loaded,
  866. &osd->rx_size_511, &nsd->rx_size_511);
  867. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  868. I40E_GLPRT_PRC1023L(hw->port),
  869. pf->stat_offsets_loaded,
  870. &osd->rx_size_1023, &nsd->rx_size_1023);
  871. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  872. I40E_GLPRT_PRC1522L(hw->port),
  873. pf->stat_offsets_loaded,
  874. &osd->rx_size_1522, &nsd->rx_size_1522);
  875. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  876. I40E_GLPRT_PRC9522L(hw->port),
  877. pf->stat_offsets_loaded,
  878. &osd->rx_size_big, &nsd->rx_size_big);
  879. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  880. I40E_GLPRT_PTC64L(hw->port),
  881. pf->stat_offsets_loaded,
  882. &osd->tx_size_64, &nsd->tx_size_64);
  883. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  884. I40E_GLPRT_PTC127L(hw->port),
  885. pf->stat_offsets_loaded,
  886. &osd->tx_size_127, &nsd->tx_size_127);
  887. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  888. I40E_GLPRT_PTC255L(hw->port),
  889. pf->stat_offsets_loaded,
  890. &osd->tx_size_255, &nsd->tx_size_255);
  891. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  892. I40E_GLPRT_PTC511L(hw->port),
  893. pf->stat_offsets_loaded,
  894. &osd->tx_size_511, &nsd->tx_size_511);
  895. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  896. I40E_GLPRT_PTC1023L(hw->port),
  897. pf->stat_offsets_loaded,
  898. &osd->tx_size_1023, &nsd->tx_size_1023);
  899. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  900. I40E_GLPRT_PTC1522L(hw->port),
  901. pf->stat_offsets_loaded,
  902. &osd->tx_size_1522, &nsd->tx_size_1522);
  903. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  904. I40E_GLPRT_PTC9522L(hw->port),
  905. pf->stat_offsets_loaded,
  906. &osd->tx_size_big, &nsd->tx_size_big);
  907. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  908. pf->stat_offsets_loaded,
  909. &osd->rx_undersize, &nsd->rx_undersize);
  910. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  911. pf->stat_offsets_loaded,
  912. &osd->rx_fragments, &nsd->rx_fragments);
  913. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  914. pf->stat_offsets_loaded,
  915. &osd->rx_oversize, &nsd->rx_oversize);
  916. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  917. pf->stat_offsets_loaded,
  918. &osd->rx_jabber, &nsd->rx_jabber);
  919. /* FDIR stats */
  920. i40e_stat_update32(hw,
  921. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
  922. pf->stat_offsets_loaded,
  923. &osd->fd_atr_match, &nsd->fd_atr_match);
  924. i40e_stat_update32(hw,
  925. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
  926. pf->stat_offsets_loaded,
  927. &osd->fd_sb_match, &nsd->fd_sb_match);
  928. i40e_stat_update32(hw,
  929. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
  930. pf->stat_offsets_loaded,
  931. &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
  932. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  933. nsd->tx_lpi_status =
  934. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  935. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  936. nsd->rx_lpi_status =
  937. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  938. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  939. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  940. pf->stat_offsets_loaded,
  941. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  942. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  943. pf->stat_offsets_loaded,
  944. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  945. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  946. !(pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED))
  947. nsd->fd_sb_status = true;
  948. else
  949. nsd->fd_sb_status = false;
  950. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  951. !(pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED))
  952. nsd->fd_atr_status = true;
  953. else
  954. nsd->fd_atr_status = false;
  955. pf->stat_offsets_loaded = true;
  956. }
  957. /**
  958. * i40e_update_stats - Update the various statistics counters.
  959. * @vsi: the VSI to be updated
  960. *
  961. * Update the various stats for this VSI and its related entities.
  962. **/
  963. void i40e_update_stats(struct i40e_vsi *vsi)
  964. {
  965. struct i40e_pf *pf = vsi->back;
  966. if (vsi == pf->vsi[pf->lan_vsi])
  967. i40e_update_pf_stats(pf);
  968. i40e_update_vsi_stats(vsi);
  969. }
  970. /**
  971. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  972. * @vsi: the VSI to be searched
  973. * @macaddr: the MAC address
  974. * @vlan: the vlan
  975. *
  976. * Returns ptr to the filter object or NULL
  977. **/
  978. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  979. const u8 *macaddr, s16 vlan)
  980. {
  981. struct i40e_mac_filter *f;
  982. u64 key;
  983. if (!vsi || !macaddr)
  984. return NULL;
  985. key = i40e_addr_to_hkey(macaddr);
  986. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  987. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  988. (vlan == f->vlan))
  989. return f;
  990. }
  991. return NULL;
  992. }
  993. /**
  994. * i40e_find_mac - Find a mac addr in the macvlan filters list
  995. * @vsi: the VSI to be searched
  996. * @macaddr: the MAC address we are searching for
  997. *
  998. * Returns the first filter with the provided MAC address or NULL if
  999. * MAC address was not found
  1000. **/
  1001. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1002. {
  1003. struct i40e_mac_filter *f;
  1004. u64 key;
  1005. if (!vsi || !macaddr)
  1006. return NULL;
  1007. key = i40e_addr_to_hkey(macaddr);
  1008. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1009. if ((ether_addr_equal(macaddr, f->macaddr)))
  1010. return f;
  1011. }
  1012. return NULL;
  1013. }
  1014. /**
  1015. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1016. * @vsi: the VSI to be searched
  1017. *
  1018. * Returns true if VSI is in vlan mode or false otherwise
  1019. **/
  1020. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1021. {
  1022. /* If we have a PVID, always operate in VLAN mode */
  1023. if (vsi->info.pvid)
  1024. return true;
  1025. /* We need to operate in VLAN mode whenever we have any filters with
  1026. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1027. * time, incurring search cost repeatedly. However, we can notice two
  1028. * things:
  1029. *
  1030. * 1) the only place where we can gain a VLAN filter is in
  1031. * i40e_add_filter.
  1032. *
  1033. * 2) the only place where filters are actually removed is in
  1034. * i40e_sync_filters_subtask.
  1035. *
  1036. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1037. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1038. * we have to perform the full search after deleting filters in
  1039. * i40e_sync_filters_subtask, but we already have to search
  1040. * filters here and can perform the check at the same time. This
  1041. * results in avoiding embedding a loop for VLAN mode inside another
  1042. * loop over all the filters, and should maintain correctness as noted
  1043. * above.
  1044. */
  1045. return vsi->has_vlan_filter;
  1046. }
  1047. /**
  1048. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1049. * @vsi: the VSI to configure
  1050. * @tmp_add_list: list of filters ready to be added
  1051. * @tmp_del_list: list of filters ready to be deleted
  1052. * @vlan_filters: the number of active VLAN filters
  1053. *
  1054. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1055. * behave as expected. If we have any active VLAN filters remaining or about
  1056. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1057. * so that they only match against untagged traffic. If we no longer have any
  1058. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1059. * so that they match against both tagged and untagged traffic. In this way,
  1060. * we ensure that we correctly receive the desired traffic. This ensures that
  1061. * when we have an active VLAN we will receive only untagged traffic and
  1062. * traffic matching active VLANs. If we have no active VLANs then we will
  1063. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1064. *
  1065. * Finally, in a similar fashion, this function also corrects filters when
  1066. * there is an active PVID assigned to this VSI.
  1067. *
  1068. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1069. *
  1070. * This function is only expected to be called from within
  1071. * i40e_sync_vsi_filters.
  1072. *
  1073. * NOTE: This function expects to be called while under the
  1074. * mac_filter_hash_lock
  1075. */
  1076. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1077. struct hlist_head *tmp_add_list,
  1078. struct hlist_head *tmp_del_list,
  1079. int vlan_filters)
  1080. {
  1081. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1082. struct i40e_mac_filter *f, *add_head;
  1083. struct i40e_new_mac_filter *new;
  1084. struct hlist_node *h;
  1085. int bkt, new_vlan;
  1086. /* To determine if a particular filter needs to be replaced we
  1087. * have the three following conditions:
  1088. *
  1089. * a) if we have a PVID assigned, then all filters which are
  1090. * not marked as VLAN=PVID must be replaced with filters that
  1091. * are.
  1092. * b) otherwise, if we have any active VLANS, all filters
  1093. * which are marked as VLAN=-1 must be replaced with
  1094. * filters marked as VLAN=0
  1095. * c) finally, if we do not have any active VLANS, all filters
  1096. * which are marked as VLAN=0 must be replaced with filters
  1097. * marked as VLAN=-1
  1098. */
  1099. /* Update the filters about to be added in place */
  1100. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1101. if (pvid && new->f->vlan != pvid)
  1102. new->f->vlan = pvid;
  1103. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1104. new->f->vlan = 0;
  1105. else if (!vlan_filters && new->f->vlan == 0)
  1106. new->f->vlan = I40E_VLAN_ANY;
  1107. }
  1108. /* Update the remaining active filters */
  1109. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1110. /* Combine the checks for whether a filter needs to be changed
  1111. * and then determine the new VLAN inside the if block, in
  1112. * order to avoid duplicating code for adding the new filter
  1113. * then deleting the old filter.
  1114. */
  1115. if ((pvid && f->vlan != pvid) ||
  1116. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1117. (!vlan_filters && f->vlan == 0)) {
  1118. /* Determine the new vlan we will be adding */
  1119. if (pvid)
  1120. new_vlan = pvid;
  1121. else if (vlan_filters)
  1122. new_vlan = 0;
  1123. else
  1124. new_vlan = I40E_VLAN_ANY;
  1125. /* Create the new filter */
  1126. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1127. if (!add_head)
  1128. return -ENOMEM;
  1129. /* Create a temporary i40e_new_mac_filter */
  1130. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1131. if (!new)
  1132. return -ENOMEM;
  1133. new->f = add_head;
  1134. new->state = add_head->state;
  1135. /* Add the new filter to the tmp list */
  1136. hlist_add_head(&new->hlist, tmp_add_list);
  1137. /* Put the original filter into the delete list */
  1138. f->state = I40E_FILTER_REMOVE;
  1139. hash_del(&f->hlist);
  1140. hlist_add_head(&f->hlist, tmp_del_list);
  1141. }
  1142. }
  1143. vsi->has_vlan_filter = !!vlan_filters;
  1144. return 0;
  1145. }
  1146. /**
  1147. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1148. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1149. * @macaddr: the MAC address
  1150. *
  1151. * Remove whatever filter the firmware set up so the driver can manage
  1152. * its own filtering intelligently.
  1153. **/
  1154. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1155. {
  1156. struct i40e_aqc_remove_macvlan_element_data element;
  1157. struct i40e_pf *pf = vsi->back;
  1158. /* Only appropriate for the PF main VSI */
  1159. if (vsi->type != I40E_VSI_MAIN)
  1160. return;
  1161. memset(&element, 0, sizeof(element));
  1162. ether_addr_copy(element.mac_addr, macaddr);
  1163. element.vlan_tag = 0;
  1164. /* Ignore error returns, some firmware does it this way... */
  1165. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1166. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1167. memset(&element, 0, sizeof(element));
  1168. ether_addr_copy(element.mac_addr, macaddr);
  1169. element.vlan_tag = 0;
  1170. /* ...and some firmware does it this way. */
  1171. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1172. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1173. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1174. }
  1175. /**
  1176. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1177. * @vsi: the VSI to be searched
  1178. * @macaddr: the MAC address
  1179. * @vlan: the vlan
  1180. *
  1181. * Returns ptr to the filter object or NULL when no memory available.
  1182. *
  1183. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1184. * being held.
  1185. **/
  1186. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1187. const u8 *macaddr, s16 vlan)
  1188. {
  1189. struct i40e_mac_filter *f;
  1190. u64 key;
  1191. if (!vsi || !macaddr)
  1192. return NULL;
  1193. f = i40e_find_filter(vsi, macaddr, vlan);
  1194. if (!f) {
  1195. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1196. if (!f)
  1197. return NULL;
  1198. /* Update the boolean indicating if we need to function in
  1199. * VLAN mode.
  1200. */
  1201. if (vlan >= 0)
  1202. vsi->has_vlan_filter = true;
  1203. ether_addr_copy(f->macaddr, macaddr);
  1204. f->vlan = vlan;
  1205. /* If we're in overflow promisc mode, set the state directly
  1206. * to failed, so we don't bother to try sending the filter
  1207. * to the hardware.
  1208. */
  1209. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
  1210. f->state = I40E_FILTER_FAILED;
  1211. else
  1212. f->state = I40E_FILTER_NEW;
  1213. INIT_HLIST_NODE(&f->hlist);
  1214. key = i40e_addr_to_hkey(macaddr);
  1215. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1216. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1217. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1218. }
  1219. /* If we're asked to add a filter that has been marked for removal, it
  1220. * is safe to simply restore it to active state. __i40e_del_filter
  1221. * will have simply deleted any filters which were previously marked
  1222. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1223. * previously been ACTIVE. Since we haven't yet run the sync filters
  1224. * task, just restore this filter to the ACTIVE state so that the
  1225. * sync task leaves it in place
  1226. */
  1227. if (f->state == I40E_FILTER_REMOVE)
  1228. f->state = I40E_FILTER_ACTIVE;
  1229. return f;
  1230. }
  1231. /**
  1232. * __i40e_del_filter - Remove a specific filter from the VSI
  1233. * @vsi: VSI to remove from
  1234. * @f: the filter to remove from the list
  1235. *
  1236. * This function should be called instead of i40e_del_filter only if you know
  1237. * the exact filter you will remove already, such as via i40e_find_filter or
  1238. * i40e_find_mac.
  1239. *
  1240. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1241. * being held.
  1242. * ANOTHER NOTE: This function MUST be called from within the context of
  1243. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1244. * instead of list_for_each_entry().
  1245. **/
  1246. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1247. {
  1248. if (!f)
  1249. return;
  1250. /* If the filter was never added to firmware then we can just delete it
  1251. * directly and we don't want to set the status to remove or else an
  1252. * admin queue command will unnecessarily fire.
  1253. */
  1254. if ((f->state == I40E_FILTER_FAILED) ||
  1255. (f->state == I40E_FILTER_NEW)) {
  1256. hash_del(&f->hlist);
  1257. kfree(f);
  1258. } else {
  1259. f->state = I40E_FILTER_REMOVE;
  1260. }
  1261. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1262. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1263. }
  1264. /**
  1265. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1266. * @vsi: the VSI to be searched
  1267. * @macaddr: the MAC address
  1268. * @vlan: the VLAN
  1269. *
  1270. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1271. * being held.
  1272. * ANOTHER NOTE: This function MUST be called from within the context of
  1273. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1274. * instead of list_for_each_entry().
  1275. **/
  1276. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1277. {
  1278. struct i40e_mac_filter *f;
  1279. if (!vsi || !macaddr)
  1280. return;
  1281. f = i40e_find_filter(vsi, macaddr, vlan);
  1282. __i40e_del_filter(vsi, f);
  1283. }
  1284. /**
  1285. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1286. * @vsi: the VSI to be searched
  1287. * @macaddr: the mac address to be filtered
  1288. *
  1289. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1290. * go through all the macvlan filters and add a macvlan filter for each
  1291. * unique vlan that already exists. If a PVID has been assigned, instead only
  1292. * add the macaddr to that VLAN.
  1293. *
  1294. * Returns last filter added on success, else NULL
  1295. **/
  1296. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1297. const u8 *macaddr)
  1298. {
  1299. struct i40e_mac_filter *f, *add = NULL;
  1300. struct hlist_node *h;
  1301. int bkt;
  1302. if (vsi->info.pvid)
  1303. return i40e_add_filter(vsi, macaddr,
  1304. le16_to_cpu(vsi->info.pvid));
  1305. if (!i40e_is_vsi_in_vlan(vsi))
  1306. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1307. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1308. if (f->state == I40E_FILTER_REMOVE)
  1309. continue;
  1310. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1311. if (!add)
  1312. return NULL;
  1313. }
  1314. return add;
  1315. }
  1316. /**
  1317. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1318. * @vsi: the VSI to be searched
  1319. * @macaddr: the mac address to be removed
  1320. *
  1321. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1322. * associated with.
  1323. *
  1324. * Returns 0 for success, or error
  1325. **/
  1326. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1327. {
  1328. struct i40e_mac_filter *f;
  1329. struct hlist_node *h;
  1330. bool found = false;
  1331. int bkt;
  1332. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1333. "Missing mac_filter_hash_lock\n");
  1334. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1335. if (ether_addr_equal(macaddr, f->macaddr)) {
  1336. __i40e_del_filter(vsi, f);
  1337. found = true;
  1338. }
  1339. }
  1340. if (found)
  1341. return 0;
  1342. else
  1343. return -ENOENT;
  1344. }
  1345. /**
  1346. * i40e_set_mac - NDO callback to set mac address
  1347. * @netdev: network interface device structure
  1348. * @p: pointer to an address structure
  1349. *
  1350. * Returns 0 on success, negative on failure
  1351. **/
  1352. static int i40e_set_mac(struct net_device *netdev, void *p)
  1353. {
  1354. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1355. struct i40e_vsi *vsi = np->vsi;
  1356. struct i40e_pf *pf = vsi->back;
  1357. struct i40e_hw *hw = &pf->hw;
  1358. struct sockaddr *addr = p;
  1359. if (!is_valid_ether_addr(addr->sa_data))
  1360. return -EADDRNOTAVAIL;
  1361. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1362. netdev_info(netdev, "already using mac address %pM\n",
  1363. addr->sa_data);
  1364. return 0;
  1365. }
  1366. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1367. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1368. return -EADDRNOTAVAIL;
  1369. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1370. netdev_info(netdev, "returning to hw mac address %pM\n",
  1371. hw->mac.addr);
  1372. else
  1373. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1374. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1375. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1376. i40e_add_mac_filter(vsi, addr->sa_data);
  1377. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1378. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1379. if (vsi->type == I40E_VSI_MAIN) {
  1380. i40e_status ret;
  1381. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1382. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1383. addr->sa_data, NULL);
  1384. if (ret)
  1385. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1386. i40e_stat_str(hw, ret),
  1387. i40e_aq_str(hw, hw->aq.asq_last_status));
  1388. }
  1389. /* schedule our worker thread which will take care of
  1390. * applying the new filter changes
  1391. */
  1392. i40e_service_event_schedule(vsi->back);
  1393. return 0;
  1394. }
  1395. /**
  1396. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1397. * @vsi: the VSI being setup
  1398. * @ctxt: VSI context structure
  1399. * @enabled_tc: Enabled TCs bitmap
  1400. * @is_add: True if called before Add VSI
  1401. *
  1402. * Setup VSI queue mapping for enabled traffic classes.
  1403. **/
  1404. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1405. struct i40e_vsi_context *ctxt,
  1406. u8 enabled_tc,
  1407. bool is_add)
  1408. {
  1409. struct i40e_pf *pf = vsi->back;
  1410. u16 sections = 0;
  1411. u8 netdev_tc = 0;
  1412. u16 numtc = 0;
  1413. u16 qcount;
  1414. u8 offset;
  1415. u16 qmap;
  1416. int i;
  1417. u16 num_tc_qps = 0;
  1418. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1419. offset = 0;
  1420. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1421. /* Find numtc from enabled TC bitmap */
  1422. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1423. if (enabled_tc & BIT(i)) /* TC is enabled */
  1424. numtc++;
  1425. }
  1426. if (!numtc) {
  1427. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1428. numtc = 1;
  1429. }
  1430. } else {
  1431. /* At least TC0 is enabled in case of non-DCB case */
  1432. numtc = 1;
  1433. }
  1434. vsi->tc_config.numtc = numtc;
  1435. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1436. /* Number of queues per enabled TC */
  1437. qcount = vsi->alloc_queue_pairs;
  1438. num_tc_qps = qcount / numtc;
  1439. num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
  1440. /* Setup queue offset/count for all TCs for given VSI */
  1441. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1442. /* See if the given TC is enabled for the given VSI */
  1443. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1444. /* TC is enabled */
  1445. int pow, num_qps;
  1446. switch (vsi->type) {
  1447. case I40E_VSI_MAIN:
  1448. qcount = min_t(int, pf->alloc_rss_size,
  1449. num_tc_qps);
  1450. break;
  1451. case I40E_VSI_FDIR:
  1452. case I40E_VSI_SRIOV:
  1453. case I40E_VSI_VMDQ2:
  1454. default:
  1455. qcount = num_tc_qps;
  1456. WARN_ON(i != 0);
  1457. break;
  1458. }
  1459. vsi->tc_config.tc_info[i].qoffset = offset;
  1460. vsi->tc_config.tc_info[i].qcount = qcount;
  1461. /* find the next higher power-of-2 of num queue pairs */
  1462. num_qps = qcount;
  1463. pow = 0;
  1464. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1465. pow++;
  1466. num_qps >>= 1;
  1467. }
  1468. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1469. qmap =
  1470. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1471. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1472. offset += qcount;
  1473. } else {
  1474. /* TC is not enabled so set the offset to
  1475. * default queue and allocate one queue
  1476. * for the given TC.
  1477. */
  1478. vsi->tc_config.tc_info[i].qoffset = 0;
  1479. vsi->tc_config.tc_info[i].qcount = 1;
  1480. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1481. qmap = 0;
  1482. }
  1483. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1484. }
  1485. /* Set actual Tx/Rx queue pairs */
  1486. vsi->num_queue_pairs = offset;
  1487. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1488. if (vsi->req_queue_pairs > 0)
  1489. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1490. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1491. vsi->num_queue_pairs = pf->num_lan_msix;
  1492. }
  1493. /* Scheduler section valid can only be set for ADD VSI */
  1494. if (is_add) {
  1495. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1496. ctxt->info.up_enable_bits = enabled_tc;
  1497. }
  1498. if (vsi->type == I40E_VSI_SRIOV) {
  1499. ctxt->info.mapping_flags |=
  1500. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1501. for (i = 0; i < vsi->num_queue_pairs; i++)
  1502. ctxt->info.queue_mapping[i] =
  1503. cpu_to_le16(vsi->base_queue + i);
  1504. } else {
  1505. ctxt->info.mapping_flags |=
  1506. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1507. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1508. }
  1509. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1510. }
  1511. /**
  1512. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1513. * @netdev: the netdevice
  1514. * @addr: address to add
  1515. *
  1516. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1517. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1518. */
  1519. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1520. {
  1521. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1522. struct i40e_vsi *vsi = np->vsi;
  1523. if (i40e_add_mac_filter(vsi, addr))
  1524. return 0;
  1525. else
  1526. return -ENOMEM;
  1527. }
  1528. /**
  1529. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1530. * @netdev: the netdevice
  1531. * @addr: address to add
  1532. *
  1533. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1534. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1535. */
  1536. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1537. {
  1538. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1539. struct i40e_vsi *vsi = np->vsi;
  1540. i40e_del_mac_filter(vsi, addr);
  1541. return 0;
  1542. }
  1543. /**
  1544. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1545. * @netdev: network interface device structure
  1546. **/
  1547. static void i40e_set_rx_mode(struct net_device *netdev)
  1548. {
  1549. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1550. struct i40e_vsi *vsi = np->vsi;
  1551. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1552. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1553. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1554. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1555. /* check for other flag changes */
  1556. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1557. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1558. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1559. }
  1560. /* schedule our worker thread which will take care of
  1561. * applying the new filter changes
  1562. */
  1563. i40e_service_event_schedule(vsi->back);
  1564. }
  1565. /**
  1566. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1567. * @vsi: Pointer to VSI struct
  1568. * @from: Pointer to list which contains MAC filter entries - changes to
  1569. * those entries needs to be undone.
  1570. *
  1571. * MAC filter entries from this list were slated for deletion.
  1572. **/
  1573. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1574. struct hlist_head *from)
  1575. {
  1576. struct i40e_mac_filter *f;
  1577. struct hlist_node *h;
  1578. hlist_for_each_entry_safe(f, h, from, hlist) {
  1579. u64 key = i40e_addr_to_hkey(f->macaddr);
  1580. /* Move the element back into MAC filter list*/
  1581. hlist_del(&f->hlist);
  1582. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1583. }
  1584. }
  1585. /**
  1586. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1587. * @vsi: Pointer to vsi struct
  1588. * @from: Pointer to list which contains MAC filter entries - changes to
  1589. * those entries needs to be undone.
  1590. *
  1591. * MAC filter entries from this list were slated for addition.
  1592. **/
  1593. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1594. struct hlist_head *from)
  1595. {
  1596. struct i40e_new_mac_filter *new;
  1597. struct hlist_node *h;
  1598. hlist_for_each_entry_safe(new, h, from, hlist) {
  1599. /* We can simply free the wrapper structure */
  1600. hlist_del(&new->hlist);
  1601. kfree(new);
  1602. }
  1603. }
  1604. /**
  1605. * i40e_next_entry - Get the next non-broadcast filter from a list
  1606. * @next: pointer to filter in list
  1607. *
  1608. * Returns the next non-broadcast filter in the list. Required so that we
  1609. * ignore broadcast filters within the list, since these are not handled via
  1610. * the normal firmware update path.
  1611. */
  1612. static
  1613. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1614. {
  1615. hlist_for_each_entry_continue(next, hlist) {
  1616. if (!is_broadcast_ether_addr(next->f->macaddr))
  1617. return next;
  1618. }
  1619. return NULL;
  1620. }
  1621. /**
  1622. * i40e_update_filter_state - Update filter state based on return data
  1623. * from firmware
  1624. * @count: Number of filters added
  1625. * @add_list: return data from fw
  1626. * @head: pointer to first filter in current batch
  1627. *
  1628. * MAC filter entries from list were slated to be added to device. Returns
  1629. * number of successful filters. Note that 0 does NOT mean success!
  1630. **/
  1631. static int
  1632. i40e_update_filter_state(int count,
  1633. struct i40e_aqc_add_macvlan_element_data *add_list,
  1634. struct i40e_new_mac_filter *add_head)
  1635. {
  1636. int retval = 0;
  1637. int i;
  1638. for (i = 0; i < count; i++) {
  1639. /* Always check status of each filter. We don't need to check
  1640. * the firmware return status because we pre-set the filter
  1641. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1642. * request to the adminq. Thus, if it no longer matches then
  1643. * we know the filter is active.
  1644. */
  1645. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1646. add_head->state = I40E_FILTER_FAILED;
  1647. } else {
  1648. add_head->state = I40E_FILTER_ACTIVE;
  1649. retval++;
  1650. }
  1651. add_head = i40e_next_filter(add_head);
  1652. if (!add_head)
  1653. break;
  1654. }
  1655. return retval;
  1656. }
  1657. /**
  1658. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1659. * @vsi: ptr to the VSI
  1660. * @vsi_name: name to display in messages
  1661. * @list: the list of filters to send to firmware
  1662. * @num_del: the number of filters to delete
  1663. * @retval: Set to -EIO on failure to delete
  1664. *
  1665. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1666. * *retval instead of a return value so that success does not force ret_val to
  1667. * be set to 0. This ensures that a sequence of calls to this function
  1668. * preserve the previous value of *retval on successful delete.
  1669. */
  1670. static
  1671. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1672. struct i40e_aqc_remove_macvlan_element_data *list,
  1673. int num_del, int *retval)
  1674. {
  1675. struct i40e_hw *hw = &vsi->back->hw;
  1676. i40e_status aq_ret;
  1677. int aq_err;
  1678. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1679. aq_err = hw->aq.asq_last_status;
  1680. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1681. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1682. *retval = -EIO;
  1683. dev_info(&vsi->back->pdev->dev,
  1684. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1685. vsi_name, i40e_stat_str(hw, aq_ret),
  1686. i40e_aq_str(hw, aq_err));
  1687. }
  1688. }
  1689. /**
  1690. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1691. * @vsi: ptr to the VSI
  1692. * @vsi_name: name to display in messages
  1693. * @list: the list of filters to send to firmware
  1694. * @add_head: Position in the add hlist
  1695. * @num_add: the number of filters to add
  1696. * @promisc_change: set to true on exit if promiscuous mode was forced on
  1697. *
  1698. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1699. * promisc_changed to true if the firmware has run out of space for more
  1700. * filters.
  1701. */
  1702. static
  1703. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1704. struct i40e_aqc_add_macvlan_element_data *list,
  1705. struct i40e_new_mac_filter *add_head,
  1706. int num_add, bool *promisc_changed)
  1707. {
  1708. struct i40e_hw *hw = &vsi->back->hw;
  1709. int aq_err, fcnt;
  1710. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1711. aq_err = hw->aq.asq_last_status;
  1712. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1713. if (fcnt != num_add) {
  1714. *promisc_changed = true;
  1715. set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1716. dev_warn(&vsi->back->pdev->dev,
  1717. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1718. i40e_aq_str(hw, aq_err),
  1719. vsi_name);
  1720. }
  1721. }
  1722. /**
  1723. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1724. * @vsi: pointer to the VSI
  1725. * @f: filter data
  1726. *
  1727. * This function sets or clears the promiscuous broadcast flags for VLAN
  1728. * filters in order to properly receive broadcast frames. Assumes that only
  1729. * broadcast filters are passed.
  1730. *
  1731. * Returns status indicating success or failure;
  1732. **/
  1733. static i40e_status
  1734. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1735. struct i40e_mac_filter *f)
  1736. {
  1737. bool enable = f->state == I40E_FILTER_NEW;
  1738. struct i40e_hw *hw = &vsi->back->hw;
  1739. i40e_status aq_ret;
  1740. if (f->vlan == I40E_VLAN_ANY) {
  1741. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1742. vsi->seid,
  1743. enable,
  1744. NULL);
  1745. } else {
  1746. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1747. vsi->seid,
  1748. enable,
  1749. f->vlan,
  1750. NULL);
  1751. }
  1752. if (aq_ret)
  1753. dev_warn(&vsi->back->pdev->dev,
  1754. "Error %s setting broadcast promiscuous mode on %s\n",
  1755. i40e_aq_str(hw, hw->aq.asq_last_status),
  1756. vsi_name);
  1757. return aq_ret;
  1758. }
  1759. /**
  1760. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1761. * @vsi: ptr to the VSI
  1762. *
  1763. * Push any outstanding VSI filter changes through the AdminQ.
  1764. *
  1765. * Returns 0 or error value
  1766. **/
  1767. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1768. {
  1769. struct hlist_head tmp_add_list, tmp_del_list;
  1770. struct i40e_mac_filter *f;
  1771. struct i40e_new_mac_filter *new, *add_head = NULL;
  1772. struct i40e_hw *hw = &vsi->back->hw;
  1773. unsigned int failed_filters = 0;
  1774. unsigned int vlan_filters = 0;
  1775. bool promisc_changed = false;
  1776. char vsi_name[16] = "PF";
  1777. int filter_list_len = 0;
  1778. i40e_status aq_ret = 0;
  1779. u32 changed_flags = 0;
  1780. struct hlist_node *h;
  1781. struct i40e_pf *pf;
  1782. int num_add = 0;
  1783. int num_del = 0;
  1784. int retval = 0;
  1785. u16 cmd_flags;
  1786. int list_size;
  1787. int bkt;
  1788. /* empty array typed pointers, kcalloc later */
  1789. struct i40e_aqc_add_macvlan_element_data *add_list;
  1790. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1791. while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
  1792. usleep_range(1000, 2000);
  1793. pf = vsi->back;
  1794. if (vsi->netdev) {
  1795. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1796. vsi->current_netdev_flags = vsi->netdev->flags;
  1797. }
  1798. INIT_HLIST_HEAD(&tmp_add_list);
  1799. INIT_HLIST_HEAD(&tmp_del_list);
  1800. if (vsi->type == I40E_VSI_SRIOV)
  1801. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  1802. else if (vsi->type != I40E_VSI_MAIN)
  1803. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  1804. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1805. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1806. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1807. /* Create a list of filters to delete. */
  1808. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1809. if (f->state == I40E_FILTER_REMOVE) {
  1810. /* Move the element into temporary del_list */
  1811. hash_del(&f->hlist);
  1812. hlist_add_head(&f->hlist, &tmp_del_list);
  1813. /* Avoid counting removed filters */
  1814. continue;
  1815. }
  1816. if (f->state == I40E_FILTER_NEW) {
  1817. /* Create a temporary i40e_new_mac_filter */
  1818. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1819. if (!new)
  1820. goto err_no_memory_locked;
  1821. /* Store pointer to the real filter */
  1822. new->f = f;
  1823. new->state = f->state;
  1824. /* Add it to the hash list */
  1825. hlist_add_head(&new->hlist, &tmp_add_list);
  1826. }
  1827. /* Count the number of active (current and new) VLAN
  1828. * filters we have now. Does not count filters which
  1829. * are marked for deletion.
  1830. */
  1831. if (f->vlan > 0)
  1832. vlan_filters++;
  1833. }
  1834. retval = i40e_correct_mac_vlan_filters(vsi,
  1835. &tmp_add_list,
  1836. &tmp_del_list,
  1837. vlan_filters);
  1838. if (retval)
  1839. goto err_no_memory_locked;
  1840. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1841. }
  1842. /* Now process 'del_list' outside the lock */
  1843. if (!hlist_empty(&tmp_del_list)) {
  1844. filter_list_len = hw->aq.asq_buf_size /
  1845. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1846. list_size = filter_list_len *
  1847. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1848. del_list = kzalloc(list_size, GFP_ATOMIC);
  1849. if (!del_list)
  1850. goto err_no_memory;
  1851. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  1852. cmd_flags = 0;
  1853. /* handle broadcast filters by updating the broadcast
  1854. * promiscuous flag and release filter list.
  1855. */
  1856. if (is_broadcast_ether_addr(f->macaddr)) {
  1857. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  1858. hlist_del(&f->hlist);
  1859. kfree(f);
  1860. continue;
  1861. }
  1862. /* add to delete list */
  1863. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  1864. if (f->vlan == I40E_VLAN_ANY) {
  1865. del_list[num_del].vlan_tag = 0;
  1866. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1867. } else {
  1868. del_list[num_del].vlan_tag =
  1869. cpu_to_le16((u16)(f->vlan));
  1870. }
  1871. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1872. del_list[num_del].flags = cmd_flags;
  1873. num_del++;
  1874. /* flush a full buffer */
  1875. if (num_del == filter_list_len) {
  1876. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1877. num_del, &retval);
  1878. memset(del_list, 0, list_size);
  1879. num_del = 0;
  1880. }
  1881. /* Release memory for MAC filter entries which were
  1882. * synced up with HW.
  1883. */
  1884. hlist_del(&f->hlist);
  1885. kfree(f);
  1886. }
  1887. if (num_del) {
  1888. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1889. num_del, &retval);
  1890. }
  1891. kfree(del_list);
  1892. del_list = NULL;
  1893. }
  1894. if (!hlist_empty(&tmp_add_list)) {
  1895. /* Do all the adds now. */
  1896. filter_list_len = hw->aq.asq_buf_size /
  1897. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1898. list_size = filter_list_len *
  1899. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1900. add_list = kzalloc(list_size, GFP_ATOMIC);
  1901. if (!add_list)
  1902. goto err_no_memory;
  1903. num_add = 0;
  1904. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1905. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1906. &vsi->state)) {
  1907. new->state = I40E_FILTER_FAILED;
  1908. continue;
  1909. }
  1910. /* handle broadcast filters by updating the broadcast
  1911. * promiscuous flag instead of adding a MAC filter.
  1912. */
  1913. if (is_broadcast_ether_addr(new->f->macaddr)) {
  1914. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  1915. new->f))
  1916. new->state = I40E_FILTER_FAILED;
  1917. else
  1918. new->state = I40E_FILTER_ACTIVE;
  1919. continue;
  1920. }
  1921. /* add to add array */
  1922. if (num_add == 0)
  1923. add_head = new;
  1924. cmd_flags = 0;
  1925. ether_addr_copy(add_list[num_add].mac_addr,
  1926. new->f->macaddr);
  1927. if (new->f->vlan == I40E_VLAN_ANY) {
  1928. add_list[num_add].vlan_tag = 0;
  1929. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  1930. } else {
  1931. add_list[num_add].vlan_tag =
  1932. cpu_to_le16((u16)(new->f->vlan));
  1933. }
  1934. add_list[num_add].queue_number = 0;
  1935. /* set invalid match method for later detection */
  1936. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  1937. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  1938. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  1939. num_add++;
  1940. /* flush a full buffer */
  1941. if (num_add == filter_list_len) {
  1942. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  1943. add_head, num_add,
  1944. &promisc_changed);
  1945. memset(add_list, 0, list_size);
  1946. num_add = 0;
  1947. }
  1948. }
  1949. if (num_add) {
  1950. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  1951. num_add, &promisc_changed);
  1952. }
  1953. /* Now move all of the filters from the temp add list back to
  1954. * the VSI's list.
  1955. */
  1956. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1957. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1958. /* Only update the state if we're still NEW */
  1959. if (new->f->state == I40E_FILTER_NEW)
  1960. new->f->state = new->state;
  1961. hlist_del(&new->hlist);
  1962. kfree(new);
  1963. }
  1964. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1965. kfree(add_list);
  1966. add_list = NULL;
  1967. }
  1968. /* Determine the number of active and failed filters. */
  1969. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1970. vsi->active_filters = 0;
  1971. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1972. if (f->state == I40E_FILTER_ACTIVE)
  1973. vsi->active_filters++;
  1974. else if (f->state == I40E_FILTER_FAILED)
  1975. failed_filters++;
  1976. }
  1977. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1978. /* If promiscuous mode has changed, we need to calculate a new
  1979. * threshold for when we are safe to exit
  1980. */
  1981. if (promisc_changed)
  1982. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  1983. /* Check if we are able to exit overflow promiscuous mode. We can
  1984. * safely exit if we didn't just enter, we no longer have any failed
  1985. * filters, and we have reduced filters below the threshold value.
  1986. */
  1987. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
  1988. !promisc_changed && !failed_filters &&
  1989. (vsi->active_filters < vsi->promisc_threshold)) {
  1990. dev_info(&pf->pdev->dev,
  1991. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  1992. vsi_name);
  1993. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1994. promisc_changed = true;
  1995. vsi->promisc_threshold = 0;
  1996. }
  1997. /* if the VF is not trusted do not do promisc */
  1998. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  1999. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  2000. goto out;
  2001. }
  2002. /* check for changes in promiscuous modes */
  2003. if (changed_flags & IFF_ALLMULTI) {
  2004. bool cur_multipromisc;
  2005. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2006. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2007. vsi->seid,
  2008. cur_multipromisc,
  2009. NULL);
  2010. if (aq_ret) {
  2011. retval = i40e_aq_rc_to_posix(aq_ret,
  2012. hw->aq.asq_last_status);
  2013. dev_info(&pf->pdev->dev,
  2014. "set multi promisc failed on %s, err %s aq_err %s\n",
  2015. vsi_name,
  2016. i40e_stat_str(hw, aq_ret),
  2017. i40e_aq_str(hw, hw->aq.asq_last_status));
  2018. }
  2019. }
  2020. if ((changed_flags & IFF_PROMISC) ||
  2021. (promisc_changed &&
  2022. test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
  2023. bool cur_promisc;
  2024. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2025. test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  2026. &vsi->state));
  2027. if ((vsi->type == I40E_VSI_MAIN) &&
  2028. (pf->lan_veb != I40E_NO_VEB) &&
  2029. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  2030. /* set defport ON for Main VSI instead of true promisc
  2031. * this way we will get all unicast/multicast and VLAN
  2032. * promisc behavior but will not get VF or VMDq traffic
  2033. * replicated on the Main VSI.
  2034. */
  2035. if (pf->cur_promisc != cur_promisc) {
  2036. pf->cur_promisc = cur_promisc;
  2037. if (cur_promisc)
  2038. aq_ret =
  2039. i40e_aq_set_default_vsi(hw,
  2040. vsi->seid,
  2041. NULL);
  2042. else
  2043. aq_ret =
  2044. i40e_aq_clear_default_vsi(hw,
  2045. vsi->seid,
  2046. NULL);
  2047. if (aq_ret) {
  2048. retval = i40e_aq_rc_to_posix(aq_ret,
  2049. hw->aq.asq_last_status);
  2050. dev_info(&pf->pdev->dev,
  2051. "Set default VSI failed on %s, err %s, aq_err %s\n",
  2052. vsi_name,
  2053. i40e_stat_str(hw, aq_ret),
  2054. i40e_aq_str(hw,
  2055. hw->aq.asq_last_status));
  2056. }
  2057. }
  2058. } else {
  2059. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  2060. hw,
  2061. vsi->seid,
  2062. cur_promisc, NULL,
  2063. true);
  2064. if (aq_ret) {
  2065. retval =
  2066. i40e_aq_rc_to_posix(aq_ret,
  2067. hw->aq.asq_last_status);
  2068. dev_info(&pf->pdev->dev,
  2069. "set unicast promisc failed on %s, err %s, aq_err %s\n",
  2070. vsi_name,
  2071. i40e_stat_str(hw, aq_ret),
  2072. i40e_aq_str(hw,
  2073. hw->aq.asq_last_status));
  2074. }
  2075. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  2076. hw,
  2077. vsi->seid,
  2078. cur_promisc, NULL);
  2079. if (aq_ret) {
  2080. retval =
  2081. i40e_aq_rc_to_posix(aq_ret,
  2082. hw->aq.asq_last_status);
  2083. dev_info(&pf->pdev->dev,
  2084. "set multicast promisc failed on %s, err %s, aq_err %s\n",
  2085. vsi_name,
  2086. i40e_stat_str(hw, aq_ret),
  2087. i40e_aq_str(hw,
  2088. hw->aq.asq_last_status));
  2089. }
  2090. }
  2091. aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
  2092. vsi->seid,
  2093. cur_promisc, NULL);
  2094. if (aq_ret) {
  2095. retval = i40e_aq_rc_to_posix(aq_ret,
  2096. pf->hw.aq.asq_last_status);
  2097. dev_info(&pf->pdev->dev,
  2098. "set brdcast promisc failed, err %s, aq_err %s\n",
  2099. i40e_stat_str(hw, aq_ret),
  2100. i40e_aq_str(hw,
  2101. hw->aq.asq_last_status));
  2102. }
  2103. }
  2104. out:
  2105. /* if something went wrong then set the changed flag so we try again */
  2106. if (retval)
  2107. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2108. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2109. return retval;
  2110. err_no_memory:
  2111. /* Restore elements on the temporary add and delete lists */
  2112. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2113. err_no_memory_locked:
  2114. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2115. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2116. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2117. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2118. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2119. return -ENOMEM;
  2120. }
  2121. /**
  2122. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2123. * @pf: board private structure
  2124. **/
  2125. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2126. {
  2127. int v;
  2128. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  2129. return;
  2130. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  2131. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2132. if (pf->vsi[v] &&
  2133. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2134. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2135. if (ret) {
  2136. /* come back and try again later */
  2137. pf->flags |= I40E_FLAG_FILTER_SYNC;
  2138. break;
  2139. }
  2140. }
  2141. }
  2142. }
  2143. /**
  2144. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2145. * @netdev: network interface device structure
  2146. * @new_mtu: new value for maximum frame size
  2147. *
  2148. * Returns 0 on success, negative on failure
  2149. **/
  2150. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2151. {
  2152. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2153. struct i40e_vsi *vsi = np->vsi;
  2154. struct i40e_pf *pf = vsi->back;
  2155. netdev_info(netdev, "changing MTU from %d to %d\n",
  2156. netdev->mtu, new_mtu);
  2157. netdev->mtu = new_mtu;
  2158. if (netif_running(netdev))
  2159. i40e_vsi_reinit_locked(vsi);
  2160. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  2161. I40E_FLAG_CLIENT_L2_CHANGE);
  2162. return 0;
  2163. }
  2164. /**
  2165. * i40e_ioctl - Access the hwtstamp interface
  2166. * @netdev: network interface device structure
  2167. * @ifr: interface request data
  2168. * @cmd: ioctl command
  2169. **/
  2170. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2171. {
  2172. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2173. struct i40e_pf *pf = np->vsi->back;
  2174. switch (cmd) {
  2175. case SIOCGHWTSTAMP:
  2176. return i40e_ptp_get_ts_config(pf, ifr);
  2177. case SIOCSHWTSTAMP:
  2178. return i40e_ptp_set_ts_config(pf, ifr);
  2179. default:
  2180. return -EOPNOTSUPP;
  2181. }
  2182. }
  2183. /**
  2184. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2185. * @vsi: the vsi being adjusted
  2186. **/
  2187. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2188. {
  2189. struct i40e_vsi_context ctxt;
  2190. i40e_status ret;
  2191. if ((vsi->info.valid_sections &
  2192. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2193. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2194. return; /* already enabled */
  2195. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2196. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2197. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2198. ctxt.seid = vsi->seid;
  2199. ctxt.info = vsi->info;
  2200. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2201. if (ret) {
  2202. dev_info(&vsi->back->pdev->dev,
  2203. "update vlan stripping failed, err %s aq_err %s\n",
  2204. i40e_stat_str(&vsi->back->hw, ret),
  2205. i40e_aq_str(&vsi->back->hw,
  2206. vsi->back->hw.aq.asq_last_status));
  2207. }
  2208. }
  2209. /**
  2210. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2211. * @vsi: the vsi being adjusted
  2212. **/
  2213. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2214. {
  2215. struct i40e_vsi_context ctxt;
  2216. i40e_status ret;
  2217. if ((vsi->info.valid_sections &
  2218. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2219. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2220. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2221. return; /* already disabled */
  2222. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2223. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2224. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2225. ctxt.seid = vsi->seid;
  2226. ctxt.info = vsi->info;
  2227. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2228. if (ret) {
  2229. dev_info(&vsi->back->pdev->dev,
  2230. "update vlan stripping failed, err %s aq_err %s\n",
  2231. i40e_stat_str(&vsi->back->hw, ret),
  2232. i40e_aq_str(&vsi->back->hw,
  2233. vsi->back->hw.aq.asq_last_status));
  2234. }
  2235. }
  2236. /**
  2237. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  2238. * @netdev: network interface to be adjusted
  2239. * @features: netdev features to test if VLAN offload is enabled or not
  2240. **/
  2241. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  2242. {
  2243. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2244. struct i40e_vsi *vsi = np->vsi;
  2245. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2246. i40e_vlan_stripping_enable(vsi);
  2247. else
  2248. i40e_vlan_stripping_disable(vsi);
  2249. }
  2250. /**
  2251. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2252. * @vsi: the vsi being configured
  2253. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2254. *
  2255. * This is a helper function for adding a new MAC/VLAN filter with the
  2256. * specified VLAN for each existing MAC address already in the hash table.
  2257. * This function does *not* perform any accounting to update filters based on
  2258. * VLAN mode.
  2259. *
  2260. * NOTE: this function expects to be called while under the
  2261. * mac_filter_hash_lock
  2262. **/
  2263. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2264. {
  2265. struct i40e_mac_filter *f, *add_f;
  2266. struct hlist_node *h;
  2267. int bkt;
  2268. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2269. if (f->state == I40E_FILTER_REMOVE)
  2270. continue;
  2271. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2272. if (!add_f) {
  2273. dev_info(&vsi->back->pdev->dev,
  2274. "Could not add vlan filter %d for %pM\n",
  2275. vid, f->macaddr);
  2276. return -ENOMEM;
  2277. }
  2278. }
  2279. return 0;
  2280. }
  2281. /**
  2282. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2283. * @vsi: the VSI being configured
  2284. * @vid: VLAN id to be added
  2285. **/
  2286. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2287. {
  2288. int err;
  2289. if (!vid || vsi->info.pvid)
  2290. return -EINVAL;
  2291. /* Locked once because all functions invoked below iterates list*/
  2292. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2293. err = i40e_add_vlan_all_mac(vsi, vid);
  2294. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2295. if (err)
  2296. return err;
  2297. /* schedule our worker thread which will take care of
  2298. * applying the new filter changes
  2299. */
  2300. i40e_service_event_schedule(vsi->back);
  2301. return 0;
  2302. }
  2303. /**
  2304. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2305. * @vsi: the vsi being configured
  2306. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2307. *
  2308. * This function should be used to remove all VLAN filters which match the
  2309. * given VID. It does not schedule the service event and does not take the
  2310. * mac_filter_hash_lock so it may be combined with other operations under
  2311. * a single invocation of the mac_filter_hash_lock.
  2312. *
  2313. * NOTE: this function expects to be called while under the
  2314. * mac_filter_hash_lock
  2315. */
  2316. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2317. {
  2318. struct i40e_mac_filter *f;
  2319. struct hlist_node *h;
  2320. int bkt;
  2321. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2322. if (f->vlan == vid)
  2323. __i40e_del_filter(vsi, f);
  2324. }
  2325. }
  2326. /**
  2327. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2328. * @vsi: the VSI being configured
  2329. * @vid: VLAN id to be removed
  2330. **/
  2331. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2332. {
  2333. if (!vid || vsi->info.pvid)
  2334. return;
  2335. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2336. i40e_rm_vlan_all_mac(vsi, vid);
  2337. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2338. /* schedule our worker thread which will take care of
  2339. * applying the new filter changes
  2340. */
  2341. i40e_service_event_schedule(vsi->back);
  2342. }
  2343. /**
  2344. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2345. * @netdev: network interface to be adjusted
  2346. * @vid: vlan id to be added
  2347. *
  2348. * net_device_ops implementation for adding vlan ids
  2349. **/
  2350. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2351. __always_unused __be16 proto, u16 vid)
  2352. {
  2353. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2354. struct i40e_vsi *vsi = np->vsi;
  2355. int ret = 0;
  2356. if (vid >= VLAN_N_VID)
  2357. return -EINVAL;
  2358. /* If the network stack called us with vid = 0 then
  2359. * it is asking to receive priority tagged packets with
  2360. * vlan id 0. Our HW receives them by default when configured
  2361. * to receive untagged packets so there is no need to add an
  2362. * extra filter for vlan 0 tagged packets.
  2363. */
  2364. if (vid)
  2365. ret = i40e_vsi_add_vlan(vsi, vid);
  2366. if (!ret)
  2367. set_bit(vid, vsi->active_vlans);
  2368. return ret;
  2369. }
  2370. /**
  2371. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2372. * @netdev: network interface to be adjusted
  2373. * @vid: vlan id to be removed
  2374. *
  2375. * net_device_ops implementation for removing vlan ids
  2376. **/
  2377. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2378. __always_unused __be16 proto, u16 vid)
  2379. {
  2380. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2381. struct i40e_vsi *vsi = np->vsi;
  2382. /* return code is ignored as there is nothing a user
  2383. * can do about failure to remove and a log message was
  2384. * already printed from the other function
  2385. */
  2386. i40e_vsi_kill_vlan(vsi, vid);
  2387. clear_bit(vid, vsi->active_vlans);
  2388. return 0;
  2389. }
  2390. /**
  2391. * i40e_macaddr_init - explicitly write the mac address filters
  2392. *
  2393. * @vsi: pointer to the vsi
  2394. * @macaddr: the MAC address
  2395. *
  2396. * This is needed when the macaddr has been obtained by other
  2397. * means than the default, e.g., from Open Firmware or IDPROM.
  2398. * Returns 0 on success, negative on failure
  2399. **/
  2400. static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
  2401. {
  2402. int ret;
  2403. struct i40e_aqc_add_macvlan_element_data element;
  2404. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  2405. I40E_AQC_WRITE_TYPE_LAA_WOL,
  2406. macaddr, NULL);
  2407. if (ret) {
  2408. dev_info(&vsi->back->pdev->dev,
  2409. "Addr change for VSI failed: %d\n", ret);
  2410. return -EADDRNOTAVAIL;
  2411. }
  2412. memset(&element, 0, sizeof(element));
  2413. ether_addr_copy(element.mac_addr, macaddr);
  2414. element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
  2415. ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
  2416. if (ret) {
  2417. dev_info(&vsi->back->pdev->dev,
  2418. "add filter failed err %s aq_err %s\n",
  2419. i40e_stat_str(&vsi->back->hw, ret),
  2420. i40e_aq_str(&vsi->back->hw,
  2421. vsi->back->hw.aq.asq_last_status));
  2422. }
  2423. return ret;
  2424. }
  2425. /**
  2426. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2427. * @vsi: the vsi being brought back up
  2428. **/
  2429. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2430. {
  2431. u16 vid;
  2432. if (!vsi->netdev)
  2433. return;
  2434. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  2435. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2436. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  2437. vid);
  2438. }
  2439. /**
  2440. * i40e_vsi_add_pvid - Add pvid for the VSI
  2441. * @vsi: the vsi being adjusted
  2442. * @vid: the vlan id to set as a PVID
  2443. **/
  2444. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2445. {
  2446. struct i40e_vsi_context ctxt;
  2447. i40e_status ret;
  2448. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2449. vsi->info.pvid = cpu_to_le16(vid);
  2450. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2451. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2452. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2453. ctxt.seid = vsi->seid;
  2454. ctxt.info = vsi->info;
  2455. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2456. if (ret) {
  2457. dev_info(&vsi->back->pdev->dev,
  2458. "add pvid failed, err %s aq_err %s\n",
  2459. i40e_stat_str(&vsi->back->hw, ret),
  2460. i40e_aq_str(&vsi->back->hw,
  2461. vsi->back->hw.aq.asq_last_status));
  2462. return -ENOENT;
  2463. }
  2464. return 0;
  2465. }
  2466. /**
  2467. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2468. * @vsi: the vsi being adjusted
  2469. *
  2470. * Just use the vlan_rx_register() service to put it back to normal
  2471. **/
  2472. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2473. {
  2474. i40e_vlan_stripping_disable(vsi);
  2475. vsi->info.pvid = 0;
  2476. }
  2477. /**
  2478. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2479. * @vsi: ptr to the VSI
  2480. *
  2481. * If this function returns with an error, then it's possible one or
  2482. * more of the rings is populated (while the rest are not). It is the
  2483. * callers duty to clean those orphaned rings.
  2484. *
  2485. * Return 0 on success, negative on failure
  2486. **/
  2487. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2488. {
  2489. int i, err = 0;
  2490. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2491. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2492. return err;
  2493. }
  2494. /**
  2495. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2496. * @vsi: ptr to the VSI
  2497. *
  2498. * Free VSI's transmit software resources
  2499. **/
  2500. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2501. {
  2502. int i;
  2503. if (!vsi->tx_rings)
  2504. return;
  2505. for (i = 0; i < vsi->num_queue_pairs; i++)
  2506. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2507. i40e_free_tx_resources(vsi->tx_rings[i]);
  2508. }
  2509. /**
  2510. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2511. * @vsi: ptr to the VSI
  2512. *
  2513. * If this function returns with an error, then it's possible one or
  2514. * more of the rings is populated (while the rest are not). It is the
  2515. * callers duty to clean those orphaned rings.
  2516. *
  2517. * Return 0 on success, negative on failure
  2518. **/
  2519. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2520. {
  2521. int i, err = 0;
  2522. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2523. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2524. return err;
  2525. }
  2526. /**
  2527. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2528. * @vsi: ptr to the VSI
  2529. *
  2530. * Free all receive software resources
  2531. **/
  2532. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2533. {
  2534. int i;
  2535. if (!vsi->rx_rings)
  2536. return;
  2537. for (i = 0; i < vsi->num_queue_pairs; i++)
  2538. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2539. i40e_free_rx_resources(vsi->rx_rings[i]);
  2540. }
  2541. /**
  2542. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2543. * @ring: The Tx ring to configure
  2544. *
  2545. * This enables/disables XPS for a given Tx descriptor ring
  2546. * based on the TCs enabled for the VSI that ring belongs to.
  2547. **/
  2548. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2549. {
  2550. struct i40e_vsi *vsi = ring->vsi;
  2551. cpumask_var_t mask;
  2552. if (!ring->q_vector || !ring->netdev)
  2553. return;
  2554. /* Single TC mode enable XPS */
  2555. if (vsi->tc_config.numtc <= 1) {
  2556. if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
  2557. netif_set_xps_queue(ring->netdev,
  2558. &ring->q_vector->affinity_mask,
  2559. ring->queue_index);
  2560. } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
  2561. /* Disable XPS to allow selection based on TC */
  2562. bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
  2563. netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
  2564. free_cpumask_var(mask);
  2565. }
  2566. /* schedule our worker thread which will take care of
  2567. * applying the new filter changes
  2568. */
  2569. i40e_service_event_schedule(vsi->back);
  2570. }
  2571. /**
  2572. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2573. * @ring: The Tx ring to configure
  2574. *
  2575. * Configure the Tx descriptor ring in the HMC context.
  2576. **/
  2577. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2578. {
  2579. struct i40e_vsi *vsi = ring->vsi;
  2580. u16 pf_q = vsi->base_queue + ring->queue_index;
  2581. struct i40e_hw *hw = &vsi->back->hw;
  2582. struct i40e_hmc_obj_txq tx_ctx;
  2583. i40e_status err = 0;
  2584. u32 qtx_ctl = 0;
  2585. /* some ATR related tx ring init */
  2586. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2587. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2588. ring->atr_count = 0;
  2589. } else {
  2590. ring->atr_sample_rate = 0;
  2591. }
  2592. /* configure XPS */
  2593. i40e_config_xps_tx_ring(ring);
  2594. /* clear the context structure first */
  2595. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2596. tx_ctx.new_context = 1;
  2597. tx_ctx.base = (ring->dma / 128);
  2598. tx_ctx.qlen = ring->count;
  2599. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2600. I40E_FLAG_FD_ATR_ENABLED));
  2601. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2602. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2603. if (vsi->type != I40E_VSI_FDIR)
  2604. tx_ctx.head_wb_ena = 1;
  2605. tx_ctx.head_wb_addr = ring->dma +
  2606. (ring->count * sizeof(struct i40e_tx_desc));
  2607. /* As part of VSI creation/update, FW allocates certain
  2608. * Tx arbitration queue sets for each TC enabled for
  2609. * the VSI. The FW returns the handles to these queue
  2610. * sets as part of the response buffer to Add VSI,
  2611. * Update VSI, etc. AQ commands. It is expected that
  2612. * these queue set handles be associated with the Tx
  2613. * queues by the driver as part of the TX queue context
  2614. * initialization. This has to be done regardless of
  2615. * DCB as by default everything is mapped to TC0.
  2616. */
  2617. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2618. tx_ctx.rdylist_act = 0;
  2619. /* clear the context in the HMC */
  2620. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2621. if (err) {
  2622. dev_info(&vsi->back->pdev->dev,
  2623. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2624. ring->queue_index, pf_q, err);
  2625. return -ENOMEM;
  2626. }
  2627. /* set the context in the HMC */
  2628. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2629. if (err) {
  2630. dev_info(&vsi->back->pdev->dev,
  2631. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2632. ring->queue_index, pf_q, err);
  2633. return -ENOMEM;
  2634. }
  2635. /* Now associate this queue with this PCI function */
  2636. if (vsi->type == I40E_VSI_VMDQ2) {
  2637. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2638. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2639. I40E_QTX_CTL_VFVM_INDX_MASK;
  2640. } else {
  2641. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2642. }
  2643. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2644. I40E_QTX_CTL_PF_INDX_MASK);
  2645. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2646. i40e_flush(hw);
  2647. /* cache tail off for easier writes later */
  2648. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2649. return 0;
  2650. }
  2651. /**
  2652. * i40e_configure_rx_ring - Configure a receive ring context
  2653. * @ring: The Rx ring to configure
  2654. *
  2655. * Configure the Rx descriptor ring in the HMC context.
  2656. **/
  2657. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2658. {
  2659. struct i40e_vsi *vsi = ring->vsi;
  2660. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2661. u16 pf_q = vsi->base_queue + ring->queue_index;
  2662. struct i40e_hw *hw = &vsi->back->hw;
  2663. struct i40e_hmc_obj_rxq rx_ctx;
  2664. i40e_status err = 0;
  2665. ring->state = 0;
  2666. /* clear the context structure first */
  2667. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2668. ring->rx_buf_len = vsi->rx_buf_len;
  2669. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2670. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2671. rx_ctx.base = (ring->dma / 128);
  2672. rx_ctx.qlen = ring->count;
  2673. /* use 32 byte descriptors */
  2674. rx_ctx.dsize = 1;
  2675. /* descriptor type is always zero
  2676. * rx_ctx.dtype = 0;
  2677. */
  2678. rx_ctx.hsplit_0 = 0;
  2679. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2680. if (hw->revision_id == 0)
  2681. rx_ctx.lrxqthresh = 0;
  2682. else
  2683. rx_ctx.lrxqthresh = 2;
  2684. rx_ctx.crcstrip = 1;
  2685. rx_ctx.l2tsel = 1;
  2686. /* this controls whether VLAN is stripped from inner headers */
  2687. rx_ctx.showiv = 0;
  2688. /* set the prefena field to 1 because the manual says to */
  2689. rx_ctx.prefena = 1;
  2690. /* clear the context in the HMC */
  2691. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2692. if (err) {
  2693. dev_info(&vsi->back->pdev->dev,
  2694. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2695. ring->queue_index, pf_q, err);
  2696. return -ENOMEM;
  2697. }
  2698. /* set the context in the HMC */
  2699. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2700. if (err) {
  2701. dev_info(&vsi->back->pdev->dev,
  2702. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2703. ring->queue_index, pf_q, err);
  2704. return -ENOMEM;
  2705. }
  2706. /* cache tail for quicker writes, and clear the reg before use */
  2707. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2708. writel(0, ring->tail);
  2709. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2710. return 0;
  2711. }
  2712. /**
  2713. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2714. * @vsi: VSI structure describing this set of rings and resources
  2715. *
  2716. * Configure the Tx VSI for operation.
  2717. **/
  2718. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2719. {
  2720. int err = 0;
  2721. u16 i;
  2722. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2723. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2724. return err;
  2725. }
  2726. /**
  2727. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2728. * @vsi: the VSI being configured
  2729. *
  2730. * Configure the Rx VSI for operation.
  2731. **/
  2732. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2733. {
  2734. int err = 0;
  2735. u16 i;
  2736. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2737. vsi->max_frame = I40E_MAX_RXBUFFER;
  2738. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2739. #if (PAGE_SIZE < 8192)
  2740. } else if (vsi->netdev->mtu <= ETH_DATA_LEN) {
  2741. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2742. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2743. #endif
  2744. } else {
  2745. vsi->max_frame = I40E_MAX_RXBUFFER;
  2746. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2747. }
  2748. /* set up individual rings */
  2749. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2750. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2751. return err;
  2752. }
  2753. /**
  2754. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2755. * @vsi: ptr to the VSI
  2756. **/
  2757. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2758. {
  2759. struct i40e_ring *tx_ring, *rx_ring;
  2760. u16 qoffset, qcount;
  2761. int i, n;
  2762. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2763. /* Reset the TC information */
  2764. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2765. rx_ring = vsi->rx_rings[i];
  2766. tx_ring = vsi->tx_rings[i];
  2767. rx_ring->dcb_tc = 0;
  2768. tx_ring->dcb_tc = 0;
  2769. }
  2770. }
  2771. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2772. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2773. continue;
  2774. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2775. qcount = vsi->tc_config.tc_info[n].qcount;
  2776. for (i = qoffset; i < (qoffset + qcount); i++) {
  2777. rx_ring = vsi->rx_rings[i];
  2778. tx_ring = vsi->tx_rings[i];
  2779. rx_ring->dcb_tc = n;
  2780. tx_ring->dcb_tc = n;
  2781. }
  2782. }
  2783. }
  2784. /**
  2785. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2786. * @vsi: ptr to the VSI
  2787. **/
  2788. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2789. {
  2790. struct i40e_pf *pf = vsi->back;
  2791. int err;
  2792. if (vsi->netdev)
  2793. i40e_set_rx_mode(vsi->netdev);
  2794. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  2795. err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  2796. if (err) {
  2797. dev_warn(&pf->pdev->dev,
  2798. "could not set up macaddr; err %d\n", err);
  2799. }
  2800. }
  2801. }
  2802. /**
  2803. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2804. * @vsi: Pointer to the targeted VSI
  2805. *
  2806. * This function replays the hlist on the hw where all the SB Flow Director
  2807. * filters were saved.
  2808. **/
  2809. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2810. {
  2811. struct i40e_fdir_filter *filter;
  2812. struct i40e_pf *pf = vsi->back;
  2813. struct hlist_node *node;
  2814. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2815. return;
  2816. /* Reset FDir counters as we're replaying all existing filters */
  2817. pf->fd_tcp4_filter_cnt = 0;
  2818. pf->fd_udp4_filter_cnt = 0;
  2819. pf->fd_sctp4_filter_cnt = 0;
  2820. pf->fd_ip4_filter_cnt = 0;
  2821. hlist_for_each_entry_safe(filter, node,
  2822. &pf->fdir_filter_list, fdir_node) {
  2823. i40e_add_del_fdir(vsi, filter, true);
  2824. }
  2825. }
  2826. /**
  2827. * i40e_vsi_configure - Set up the VSI for action
  2828. * @vsi: the VSI being configured
  2829. **/
  2830. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2831. {
  2832. int err;
  2833. i40e_set_vsi_rx_mode(vsi);
  2834. i40e_restore_vlan(vsi);
  2835. i40e_vsi_config_dcb_rings(vsi);
  2836. err = i40e_vsi_configure_tx(vsi);
  2837. if (!err)
  2838. err = i40e_vsi_configure_rx(vsi);
  2839. return err;
  2840. }
  2841. /**
  2842. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2843. * @vsi: the VSI being configured
  2844. **/
  2845. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2846. {
  2847. struct i40e_pf *pf = vsi->back;
  2848. struct i40e_hw *hw = &pf->hw;
  2849. u16 vector;
  2850. int i, q;
  2851. u32 qp;
  2852. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2853. * and PFINT_LNKLSTn registers, e.g.:
  2854. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2855. */
  2856. qp = vsi->base_queue;
  2857. vector = vsi->base_vector;
  2858. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2859. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  2860. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2861. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
  2862. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2863. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2864. q_vector->rx.itr);
  2865. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
  2866. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2867. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2868. q_vector->tx.itr);
  2869. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  2870. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  2871. /* Linked list for the queuepairs assigned to this vector */
  2872. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2873. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2874. u32 val;
  2875. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2876. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2877. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2878. (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  2879. (I40E_QUEUE_TYPE_TX
  2880. << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2881. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2882. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2883. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2884. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2885. ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
  2886. (I40E_QUEUE_TYPE_RX
  2887. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2888. /* Terminate the linked list */
  2889. if (q == (q_vector->num_ringpairs - 1))
  2890. val |= (I40E_QUEUE_END_OF_LIST
  2891. << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2892. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2893. qp++;
  2894. }
  2895. }
  2896. i40e_flush(hw);
  2897. }
  2898. /**
  2899. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2900. * @hw: ptr to the hardware info
  2901. **/
  2902. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  2903. {
  2904. struct i40e_hw *hw = &pf->hw;
  2905. u32 val;
  2906. /* clear things first */
  2907. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  2908. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  2909. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  2910. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  2911. I40E_PFINT_ICR0_ENA_GRST_MASK |
  2912. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  2913. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  2914. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  2915. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  2916. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2917. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  2918. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  2919. if (pf->flags & I40E_FLAG_PTP)
  2920. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  2921. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  2922. /* SW_ITR_IDX = 0, but don't change INTENA */
  2923. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  2924. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  2925. /* OTHER_ITR_IDX = 0 */
  2926. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  2927. }
  2928. /**
  2929. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  2930. * @vsi: the VSI being configured
  2931. **/
  2932. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  2933. {
  2934. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  2935. struct i40e_pf *pf = vsi->back;
  2936. struct i40e_hw *hw = &pf->hw;
  2937. u32 val;
  2938. /* set the ITR configuration */
  2939. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2940. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
  2941. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2942. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  2943. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
  2944. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2945. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  2946. i40e_enable_misc_int_causes(pf);
  2947. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  2948. wr32(hw, I40E_PFINT_LNKLST0, 0);
  2949. /* Associate the queue pair to the vector and enable the queue int */
  2950. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2951. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2952. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2953. wr32(hw, I40E_QINT_RQCTL(0), val);
  2954. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2955. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2956. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2957. wr32(hw, I40E_QINT_TQCTL(0), val);
  2958. i40e_flush(hw);
  2959. }
  2960. /**
  2961. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  2962. * @pf: board private structure
  2963. **/
  2964. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  2965. {
  2966. struct i40e_hw *hw = &pf->hw;
  2967. wr32(hw, I40E_PFINT_DYN_CTL0,
  2968. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2969. i40e_flush(hw);
  2970. }
  2971. /**
  2972. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  2973. * @pf: board private structure
  2974. * @clearpba: true when all pending interrupt events should be cleared
  2975. **/
  2976. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
  2977. {
  2978. struct i40e_hw *hw = &pf->hw;
  2979. u32 val;
  2980. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  2981. (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
  2982. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  2983. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  2984. i40e_flush(hw);
  2985. }
  2986. /**
  2987. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  2988. * @irq: interrupt number
  2989. * @data: pointer to a q_vector
  2990. **/
  2991. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  2992. {
  2993. struct i40e_q_vector *q_vector = data;
  2994. if (!q_vector->tx.ring && !q_vector->rx.ring)
  2995. return IRQ_HANDLED;
  2996. napi_schedule_irqoff(&q_vector->napi);
  2997. return IRQ_HANDLED;
  2998. }
  2999. /**
  3000. * i40e_irq_affinity_notify - Callback for affinity changes
  3001. * @notify: context as to what irq was changed
  3002. * @mask: the new affinity mask
  3003. *
  3004. * This is a callback function used by the irq_set_affinity_notifier function
  3005. * so that we may register to receive changes to the irq affinity masks.
  3006. **/
  3007. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3008. const cpumask_t *mask)
  3009. {
  3010. struct i40e_q_vector *q_vector =
  3011. container_of(notify, struct i40e_q_vector, affinity_notify);
  3012. q_vector->affinity_mask = *mask;
  3013. }
  3014. /**
  3015. * i40e_irq_affinity_release - Callback for affinity notifier release
  3016. * @ref: internal core kernel usage
  3017. *
  3018. * This is a callback function used by the irq_set_affinity_notifier function
  3019. * to inform the current notification subscriber that they will no longer
  3020. * receive notifications.
  3021. **/
  3022. static void i40e_irq_affinity_release(struct kref *ref) {}
  3023. /**
  3024. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3025. * @vsi: the VSI being configured
  3026. * @basename: name for the vector
  3027. *
  3028. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3029. **/
  3030. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3031. {
  3032. int q_vectors = vsi->num_q_vectors;
  3033. struct i40e_pf *pf = vsi->back;
  3034. int base = vsi->base_vector;
  3035. int rx_int_idx = 0;
  3036. int tx_int_idx = 0;
  3037. int vector, err;
  3038. int irq_num;
  3039. for (vector = 0; vector < q_vectors; vector++) {
  3040. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3041. irq_num = pf->msix_entries[base + vector].vector;
  3042. if (q_vector->tx.ring && q_vector->rx.ring) {
  3043. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3044. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3045. tx_int_idx++;
  3046. } else if (q_vector->rx.ring) {
  3047. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3048. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3049. } else if (q_vector->tx.ring) {
  3050. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3051. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3052. } else {
  3053. /* skip this unused q_vector */
  3054. continue;
  3055. }
  3056. err = request_irq(irq_num,
  3057. vsi->irq_handler,
  3058. 0,
  3059. q_vector->name,
  3060. q_vector);
  3061. if (err) {
  3062. dev_info(&pf->pdev->dev,
  3063. "MSIX request_irq failed, error: %d\n", err);
  3064. goto free_queue_irqs;
  3065. }
  3066. /* register for affinity change notifications */
  3067. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3068. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3069. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3070. /* assign the mask for this irq */
  3071. irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
  3072. }
  3073. vsi->irqs_ready = true;
  3074. return 0;
  3075. free_queue_irqs:
  3076. while (vector) {
  3077. vector--;
  3078. irq_num = pf->msix_entries[base + vector].vector;
  3079. irq_set_affinity_notifier(irq_num, NULL);
  3080. irq_set_affinity_hint(irq_num, NULL);
  3081. free_irq(irq_num, &vsi->q_vectors[vector]);
  3082. }
  3083. return err;
  3084. }
  3085. /**
  3086. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3087. * @vsi: the VSI being un-configured
  3088. **/
  3089. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3090. {
  3091. struct i40e_pf *pf = vsi->back;
  3092. struct i40e_hw *hw = &pf->hw;
  3093. int base = vsi->base_vector;
  3094. int i;
  3095. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3096. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
  3097. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
  3098. }
  3099. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3100. for (i = vsi->base_vector;
  3101. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3102. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3103. i40e_flush(hw);
  3104. for (i = 0; i < vsi->num_q_vectors; i++)
  3105. synchronize_irq(pf->msix_entries[i + base].vector);
  3106. } else {
  3107. /* Legacy and MSI mode - this stops all interrupt handling */
  3108. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3109. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3110. i40e_flush(hw);
  3111. synchronize_irq(pf->pdev->irq);
  3112. }
  3113. }
  3114. /**
  3115. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3116. * @vsi: the VSI being configured
  3117. **/
  3118. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3119. {
  3120. struct i40e_pf *pf = vsi->back;
  3121. int i;
  3122. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3123. for (i = 0; i < vsi->num_q_vectors; i++)
  3124. i40e_irq_dynamic_enable(vsi, i);
  3125. } else {
  3126. i40e_irq_dynamic_enable_icr0(pf, true);
  3127. }
  3128. i40e_flush(&pf->hw);
  3129. return 0;
  3130. }
  3131. /**
  3132. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  3133. * @pf: board private structure
  3134. **/
  3135. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  3136. {
  3137. /* Disable ICR 0 */
  3138. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3139. i40e_flush(&pf->hw);
  3140. }
  3141. /**
  3142. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3143. * @irq: interrupt number
  3144. * @data: pointer to a q_vector
  3145. *
  3146. * This is the handler used for all MSI/Legacy interrupts, and deals
  3147. * with both queue and non-queue interrupts. This is also used in
  3148. * MSIX mode to handle the non-queue interrupts.
  3149. **/
  3150. static irqreturn_t i40e_intr(int irq, void *data)
  3151. {
  3152. struct i40e_pf *pf = (struct i40e_pf *)data;
  3153. struct i40e_hw *hw = &pf->hw;
  3154. irqreturn_t ret = IRQ_NONE;
  3155. u32 icr0, icr0_remaining;
  3156. u32 val, ena_mask;
  3157. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3158. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3159. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3160. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3161. goto enable_intr;
  3162. /* if interrupt but no bits showing, must be SWINT */
  3163. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3164. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3165. pf->sw_int_count++;
  3166. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3167. (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3168. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3169. icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3170. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3171. }
  3172. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3173. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3174. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3175. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3176. /* We do not have a way to disarm Queue causes while leaving
  3177. * interrupt enabled for all other causes, ideally
  3178. * interrupt should be disabled while we are in NAPI but
  3179. * this is not a performance path and napi_schedule()
  3180. * can deal with rescheduling.
  3181. */
  3182. if (!test_bit(__I40E_DOWN, &pf->state))
  3183. napi_schedule_irqoff(&q_vector->napi);
  3184. }
  3185. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3186. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3187. set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  3188. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3189. }
  3190. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3191. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3192. set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  3193. }
  3194. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3195. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3196. set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  3197. }
  3198. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3199. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3200. set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  3201. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3202. val = rd32(hw, I40E_GLGEN_RSTAT);
  3203. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3204. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3205. if (val == I40E_RESET_CORER) {
  3206. pf->corer_count++;
  3207. } else if (val == I40E_RESET_GLOBR) {
  3208. pf->globr_count++;
  3209. } else if (val == I40E_RESET_EMPR) {
  3210. pf->empr_count++;
  3211. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
  3212. }
  3213. }
  3214. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3215. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3216. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3217. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3218. rd32(hw, I40E_PFHMC_ERRORINFO),
  3219. rd32(hw, I40E_PFHMC_ERRORDATA));
  3220. }
  3221. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3222. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3223. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3224. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3225. i40e_ptp_tx_hwtstamp(pf);
  3226. }
  3227. }
  3228. /* If a critical error is pending we have no choice but to reset the
  3229. * device.
  3230. * Report and mask out any remaining unexpected interrupts.
  3231. */
  3232. icr0_remaining = icr0 & ena_mask;
  3233. if (icr0_remaining) {
  3234. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3235. icr0_remaining);
  3236. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3237. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3238. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3239. dev_info(&pf->pdev->dev, "device will be reset\n");
  3240. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  3241. i40e_service_event_schedule(pf);
  3242. }
  3243. ena_mask &= ~icr0_remaining;
  3244. }
  3245. ret = IRQ_HANDLED;
  3246. enable_intr:
  3247. /* re-enable interrupt causes */
  3248. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3249. if (!test_bit(__I40E_DOWN, &pf->state)) {
  3250. i40e_service_event_schedule(pf);
  3251. i40e_irq_dynamic_enable_icr0(pf, false);
  3252. }
  3253. return ret;
  3254. }
  3255. /**
  3256. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3257. * @tx_ring: tx ring to clean
  3258. * @budget: how many cleans we're allowed
  3259. *
  3260. * Returns true if there's any budget left (e.g. the clean is finished)
  3261. **/
  3262. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3263. {
  3264. struct i40e_vsi *vsi = tx_ring->vsi;
  3265. u16 i = tx_ring->next_to_clean;
  3266. struct i40e_tx_buffer *tx_buf;
  3267. struct i40e_tx_desc *tx_desc;
  3268. tx_buf = &tx_ring->tx_bi[i];
  3269. tx_desc = I40E_TX_DESC(tx_ring, i);
  3270. i -= tx_ring->count;
  3271. do {
  3272. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3273. /* if next_to_watch is not set then there is no work pending */
  3274. if (!eop_desc)
  3275. break;
  3276. /* prevent any other reads prior to eop_desc */
  3277. read_barrier_depends();
  3278. /* if the descriptor isn't done, no work yet to do */
  3279. if (!(eop_desc->cmd_type_offset_bsz &
  3280. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3281. break;
  3282. /* clear next_to_watch to prevent false hangs */
  3283. tx_buf->next_to_watch = NULL;
  3284. tx_desc->buffer_addr = 0;
  3285. tx_desc->cmd_type_offset_bsz = 0;
  3286. /* move past filter desc */
  3287. tx_buf++;
  3288. tx_desc++;
  3289. i++;
  3290. if (unlikely(!i)) {
  3291. i -= tx_ring->count;
  3292. tx_buf = tx_ring->tx_bi;
  3293. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3294. }
  3295. /* unmap skb header data */
  3296. dma_unmap_single(tx_ring->dev,
  3297. dma_unmap_addr(tx_buf, dma),
  3298. dma_unmap_len(tx_buf, len),
  3299. DMA_TO_DEVICE);
  3300. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3301. kfree(tx_buf->raw_buf);
  3302. tx_buf->raw_buf = NULL;
  3303. tx_buf->tx_flags = 0;
  3304. tx_buf->next_to_watch = NULL;
  3305. dma_unmap_len_set(tx_buf, len, 0);
  3306. tx_desc->buffer_addr = 0;
  3307. tx_desc->cmd_type_offset_bsz = 0;
  3308. /* move us past the eop_desc for start of next FD desc */
  3309. tx_buf++;
  3310. tx_desc++;
  3311. i++;
  3312. if (unlikely(!i)) {
  3313. i -= tx_ring->count;
  3314. tx_buf = tx_ring->tx_bi;
  3315. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3316. }
  3317. /* update budget accounting */
  3318. budget--;
  3319. } while (likely(budget));
  3320. i += tx_ring->count;
  3321. tx_ring->next_to_clean = i;
  3322. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3323. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3324. return budget > 0;
  3325. }
  3326. /**
  3327. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3328. * @irq: interrupt number
  3329. * @data: pointer to a q_vector
  3330. **/
  3331. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3332. {
  3333. struct i40e_q_vector *q_vector = data;
  3334. struct i40e_vsi *vsi;
  3335. if (!q_vector->tx.ring)
  3336. return IRQ_HANDLED;
  3337. vsi = q_vector->tx.ring->vsi;
  3338. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3339. return IRQ_HANDLED;
  3340. }
  3341. /**
  3342. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3343. * @vsi: the VSI being configured
  3344. * @v_idx: vector index
  3345. * @qp_idx: queue pair index
  3346. **/
  3347. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3348. {
  3349. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3350. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3351. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3352. tx_ring->q_vector = q_vector;
  3353. tx_ring->next = q_vector->tx.ring;
  3354. q_vector->tx.ring = tx_ring;
  3355. q_vector->tx.count++;
  3356. rx_ring->q_vector = q_vector;
  3357. rx_ring->next = q_vector->rx.ring;
  3358. q_vector->rx.ring = rx_ring;
  3359. q_vector->rx.count++;
  3360. }
  3361. /**
  3362. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3363. * @vsi: the VSI being configured
  3364. *
  3365. * This function maps descriptor rings to the queue-specific vectors
  3366. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3367. * one vector per queue pair, but on a constrained vector budget, we
  3368. * group the queue pairs as "efficiently" as possible.
  3369. **/
  3370. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3371. {
  3372. int qp_remaining = vsi->num_queue_pairs;
  3373. int q_vectors = vsi->num_q_vectors;
  3374. int num_ringpairs;
  3375. int v_start = 0;
  3376. int qp_idx = 0;
  3377. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3378. * group them so there are multiple queues per vector.
  3379. * It is also important to go through all the vectors available to be
  3380. * sure that if we don't use all the vectors, that the remaining vectors
  3381. * are cleared. This is especially important when decreasing the
  3382. * number of queues in use.
  3383. */
  3384. for (; v_start < q_vectors; v_start++) {
  3385. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3386. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3387. q_vector->num_ringpairs = num_ringpairs;
  3388. q_vector->rx.count = 0;
  3389. q_vector->tx.count = 0;
  3390. q_vector->rx.ring = NULL;
  3391. q_vector->tx.ring = NULL;
  3392. while (num_ringpairs--) {
  3393. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3394. qp_idx++;
  3395. qp_remaining--;
  3396. }
  3397. }
  3398. }
  3399. /**
  3400. * i40e_vsi_request_irq - Request IRQ from the OS
  3401. * @vsi: the VSI being configured
  3402. * @basename: name for the vector
  3403. **/
  3404. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3405. {
  3406. struct i40e_pf *pf = vsi->back;
  3407. int err;
  3408. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3409. err = i40e_vsi_request_irq_msix(vsi, basename);
  3410. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3411. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3412. pf->int_name, pf);
  3413. else
  3414. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3415. pf->int_name, pf);
  3416. if (err)
  3417. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3418. return err;
  3419. }
  3420. #ifdef CONFIG_NET_POLL_CONTROLLER
  3421. /**
  3422. * i40e_netpoll - A Polling 'interrupt' handler
  3423. * @netdev: network interface device structure
  3424. *
  3425. * This is used by netconsole to send skbs without having to re-enable
  3426. * interrupts. It's not called while the normal interrupt routine is executing.
  3427. **/
  3428. static void i40e_netpoll(struct net_device *netdev)
  3429. {
  3430. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3431. struct i40e_vsi *vsi = np->vsi;
  3432. struct i40e_pf *pf = vsi->back;
  3433. int i;
  3434. /* if interface is down do nothing */
  3435. if (test_bit(__I40E_DOWN, &vsi->state))
  3436. return;
  3437. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3438. for (i = 0; i < vsi->num_q_vectors; i++)
  3439. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3440. } else {
  3441. i40e_intr(pf->pdev->irq, netdev);
  3442. }
  3443. }
  3444. #endif
  3445. /**
  3446. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3447. * @pf: the PF being configured
  3448. * @pf_q: the PF queue
  3449. * @enable: enable or disable state of the queue
  3450. *
  3451. * This routine will wait for the given Tx queue of the PF to reach the
  3452. * enabled or disabled state.
  3453. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3454. * multiple retries; else will return 0 in case of success.
  3455. **/
  3456. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3457. {
  3458. int i;
  3459. u32 tx_reg;
  3460. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3461. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3462. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3463. break;
  3464. usleep_range(10, 20);
  3465. }
  3466. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3467. return -ETIMEDOUT;
  3468. return 0;
  3469. }
  3470. /**
  3471. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3472. * @vsi: the VSI being configured
  3473. * @enable: start or stop the rings
  3474. **/
  3475. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3476. {
  3477. struct i40e_pf *pf = vsi->back;
  3478. struct i40e_hw *hw = &pf->hw;
  3479. int i, j, pf_q, ret = 0;
  3480. u32 tx_reg;
  3481. pf_q = vsi->base_queue;
  3482. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3483. /* warn the TX unit of coming changes */
  3484. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3485. if (!enable)
  3486. usleep_range(10, 20);
  3487. for (j = 0; j < 50; j++) {
  3488. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3489. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3490. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3491. break;
  3492. usleep_range(1000, 2000);
  3493. }
  3494. /* Skip if the queue is already in the requested state */
  3495. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3496. continue;
  3497. /* turn on/off the queue */
  3498. if (enable) {
  3499. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3500. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3501. } else {
  3502. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3503. }
  3504. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3505. /* No waiting for the Tx queue to disable */
  3506. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3507. continue;
  3508. /* wait for the change to finish */
  3509. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3510. if (ret) {
  3511. dev_info(&pf->pdev->dev,
  3512. "VSI seid %d Tx ring %d %sable timeout\n",
  3513. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3514. break;
  3515. }
  3516. }
  3517. return ret;
  3518. }
  3519. /**
  3520. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3521. * @pf: the PF being configured
  3522. * @pf_q: the PF queue
  3523. * @enable: enable or disable state of the queue
  3524. *
  3525. * This routine will wait for the given Rx queue of the PF to reach the
  3526. * enabled or disabled state.
  3527. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3528. * multiple retries; else will return 0 in case of success.
  3529. **/
  3530. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3531. {
  3532. int i;
  3533. u32 rx_reg;
  3534. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3535. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3536. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3537. break;
  3538. usleep_range(10, 20);
  3539. }
  3540. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3541. return -ETIMEDOUT;
  3542. return 0;
  3543. }
  3544. /**
  3545. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3546. * @vsi: the VSI being configured
  3547. * @enable: start or stop the rings
  3548. **/
  3549. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3550. {
  3551. struct i40e_pf *pf = vsi->back;
  3552. struct i40e_hw *hw = &pf->hw;
  3553. int i, j, pf_q, ret = 0;
  3554. u32 rx_reg;
  3555. pf_q = vsi->base_queue;
  3556. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3557. for (j = 0; j < 50; j++) {
  3558. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3559. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3560. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3561. break;
  3562. usleep_range(1000, 2000);
  3563. }
  3564. /* Skip if the queue is already in the requested state */
  3565. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3566. continue;
  3567. /* turn on/off the queue */
  3568. if (enable)
  3569. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3570. else
  3571. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3572. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3573. /* No waiting for the Tx queue to disable */
  3574. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3575. continue;
  3576. /* wait for the change to finish */
  3577. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3578. if (ret) {
  3579. dev_info(&pf->pdev->dev,
  3580. "VSI seid %d Rx ring %d %sable timeout\n",
  3581. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3582. break;
  3583. }
  3584. }
  3585. /* Due to HW errata, on Rx disable only, the register can indicate done
  3586. * before it really is. Needs 50ms to be sure
  3587. */
  3588. if (!enable)
  3589. mdelay(50);
  3590. return ret;
  3591. }
  3592. /**
  3593. * i40e_vsi_start_rings - Start a VSI's rings
  3594. * @vsi: the VSI being configured
  3595. **/
  3596. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3597. {
  3598. int ret = 0;
  3599. /* do rx first for enable and last for disable */
  3600. ret = i40e_vsi_control_rx(vsi, true);
  3601. if (ret)
  3602. return ret;
  3603. ret = i40e_vsi_control_tx(vsi, true);
  3604. return ret;
  3605. }
  3606. /**
  3607. * i40e_vsi_stop_rings - Stop a VSI's rings
  3608. * @vsi: the VSI being configured
  3609. **/
  3610. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3611. {
  3612. /* do rx first for enable and last for disable
  3613. * Ignore return value, we need to shutdown whatever we can
  3614. */
  3615. i40e_vsi_control_tx(vsi, false);
  3616. i40e_vsi_control_rx(vsi, false);
  3617. }
  3618. /**
  3619. * i40e_vsi_free_irq - Free the irq association with the OS
  3620. * @vsi: the VSI being configured
  3621. **/
  3622. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3623. {
  3624. struct i40e_pf *pf = vsi->back;
  3625. struct i40e_hw *hw = &pf->hw;
  3626. int base = vsi->base_vector;
  3627. u32 val, qp;
  3628. int i;
  3629. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3630. if (!vsi->q_vectors)
  3631. return;
  3632. if (!vsi->irqs_ready)
  3633. return;
  3634. vsi->irqs_ready = false;
  3635. for (i = 0; i < vsi->num_q_vectors; i++) {
  3636. int irq_num;
  3637. u16 vector;
  3638. vector = i + base;
  3639. irq_num = pf->msix_entries[vector].vector;
  3640. /* free only the irqs that were actually requested */
  3641. if (!vsi->q_vectors[i] ||
  3642. !vsi->q_vectors[i]->num_ringpairs)
  3643. continue;
  3644. /* clear the affinity notifier in the IRQ descriptor */
  3645. irq_set_affinity_notifier(irq_num, NULL);
  3646. /* clear the affinity_mask in the IRQ descriptor */
  3647. irq_set_affinity_hint(irq_num, NULL);
  3648. synchronize_irq(irq_num);
  3649. free_irq(irq_num, vsi->q_vectors[i]);
  3650. /* Tear down the interrupt queue link list
  3651. *
  3652. * We know that they come in pairs and always
  3653. * the Rx first, then the Tx. To clear the
  3654. * link list, stick the EOL value into the
  3655. * next_q field of the registers.
  3656. */
  3657. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3658. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3659. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3660. val |= I40E_QUEUE_END_OF_LIST
  3661. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3662. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3663. while (qp != I40E_QUEUE_END_OF_LIST) {
  3664. u32 next;
  3665. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3666. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3667. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3668. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3669. I40E_QINT_RQCTL_INTEVENT_MASK);
  3670. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3671. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3672. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3673. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3674. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3675. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3676. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3677. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3678. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3679. I40E_QINT_TQCTL_INTEVENT_MASK);
  3680. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3681. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3682. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3683. qp = next;
  3684. }
  3685. }
  3686. } else {
  3687. free_irq(pf->pdev->irq, pf);
  3688. val = rd32(hw, I40E_PFINT_LNKLST0);
  3689. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3690. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3691. val |= I40E_QUEUE_END_OF_LIST
  3692. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  3693. wr32(hw, I40E_PFINT_LNKLST0, val);
  3694. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3695. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3696. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3697. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3698. I40E_QINT_RQCTL_INTEVENT_MASK);
  3699. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3700. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3701. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3702. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3703. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3704. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3705. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3706. I40E_QINT_TQCTL_INTEVENT_MASK);
  3707. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3708. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3709. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3710. }
  3711. }
  3712. /**
  3713. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  3714. * @vsi: the VSI being configured
  3715. * @v_idx: Index of vector to be freed
  3716. *
  3717. * This function frees the memory allocated to the q_vector. In addition if
  3718. * NAPI is enabled it will delete any references to the NAPI struct prior
  3719. * to freeing the q_vector.
  3720. **/
  3721. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  3722. {
  3723. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3724. struct i40e_ring *ring;
  3725. if (!q_vector)
  3726. return;
  3727. /* disassociate q_vector from rings */
  3728. i40e_for_each_ring(ring, q_vector->tx)
  3729. ring->q_vector = NULL;
  3730. i40e_for_each_ring(ring, q_vector->rx)
  3731. ring->q_vector = NULL;
  3732. /* only VSI w/ an associated netdev is set up w/ NAPI */
  3733. if (vsi->netdev)
  3734. netif_napi_del(&q_vector->napi);
  3735. vsi->q_vectors[v_idx] = NULL;
  3736. kfree_rcu(q_vector, rcu);
  3737. }
  3738. /**
  3739. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  3740. * @vsi: the VSI being un-configured
  3741. *
  3742. * This frees the memory allocated to the q_vectors and
  3743. * deletes references to the NAPI struct.
  3744. **/
  3745. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  3746. {
  3747. int v_idx;
  3748. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  3749. i40e_free_q_vector(vsi, v_idx);
  3750. }
  3751. /**
  3752. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  3753. * @pf: board private structure
  3754. **/
  3755. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  3756. {
  3757. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  3758. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3759. pci_disable_msix(pf->pdev);
  3760. kfree(pf->msix_entries);
  3761. pf->msix_entries = NULL;
  3762. kfree(pf->irq_pile);
  3763. pf->irq_pile = NULL;
  3764. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  3765. pci_disable_msi(pf->pdev);
  3766. }
  3767. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  3768. }
  3769. /**
  3770. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  3771. * @pf: board private structure
  3772. *
  3773. * We go through and clear interrupt specific resources and reset the structure
  3774. * to pre-load conditions
  3775. **/
  3776. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  3777. {
  3778. int i;
  3779. i40e_stop_misc_vector(pf);
  3780. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3781. synchronize_irq(pf->msix_entries[0].vector);
  3782. free_irq(pf->msix_entries[0].vector, pf);
  3783. }
  3784. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  3785. I40E_IWARP_IRQ_PILE_ID);
  3786. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  3787. for (i = 0; i < pf->num_alloc_vsi; i++)
  3788. if (pf->vsi[i])
  3789. i40e_vsi_free_q_vectors(pf->vsi[i]);
  3790. i40e_reset_interrupt_capability(pf);
  3791. }
  3792. /**
  3793. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3794. * @vsi: the VSI being configured
  3795. **/
  3796. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  3797. {
  3798. int q_idx;
  3799. if (!vsi->netdev)
  3800. return;
  3801. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3802. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  3803. if (q_vector->rx.ring || q_vector->tx.ring)
  3804. napi_enable(&q_vector->napi);
  3805. }
  3806. }
  3807. /**
  3808. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  3809. * @vsi: the VSI being configured
  3810. **/
  3811. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  3812. {
  3813. int q_idx;
  3814. if (!vsi->netdev)
  3815. return;
  3816. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3817. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  3818. if (q_vector->rx.ring || q_vector->tx.ring)
  3819. napi_disable(&q_vector->napi);
  3820. }
  3821. }
  3822. /**
  3823. * i40e_vsi_close - Shut down a VSI
  3824. * @vsi: the vsi to be quelled
  3825. **/
  3826. static void i40e_vsi_close(struct i40e_vsi *vsi)
  3827. {
  3828. struct i40e_pf *pf = vsi->back;
  3829. if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
  3830. i40e_down(vsi);
  3831. i40e_vsi_free_irq(vsi);
  3832. i40e_vsi_free_tx_resources(vsi);
  3833. i40e_vsi_free_rx_resources(vsi);
  3834. vsi->current_netdev_flags = 0;
  3835. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  3836. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3837. pf->flags |= I40E_FLAG_CLIENT_RESET;
  3838. }
  3839. /**
  3840. * i40e_quiesce_vsi - Pause a given VSI
  3841. * @vsi: the VSI being paused
  3842. **/
  3843. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  3844. {
  3845. if (test_bit(__I40E_DOWN, &vsi->state))
  3846. return;
  3847. set_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3848. if (vsi->netdev && netif_running(vsi->netdev))
  3849. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  3850. else
  3851. i40e_vsi_close(vsi);
  3852. }
  3853. /**
  3854. * i40e_unquiesce_vsi - Resume a given VSI
  3855. * @vsi: the VSI being resumed
  3856. **/
  3857. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  3858. {
  3859. if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
  3860. return;
  3861. clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3862. if (vsi->netdev && netif_running(vsi->netdev))
  3863. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  3864. else
  3865. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  3866. }
  3867. /**
  3868. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  3869. * @pf: the PF
  3870. **/
  3871. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  3872. {
  3873. int v;
  3874. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3875. if (pf->vsi[v])
  3876. i40e_quiesce_vsi(pf->vsi[v]);
  3877. }
  3878. }
  3879. /**
  3880. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  3881. * @pf: the PF
  3882. **/
  3883. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  3884. {
  3885. int v;
  3886. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3887. if (pf->vsi[v])
  3888. i40e_unquiesce_vsi(pf->vsi[v]);
  3889. }
  3890. }
  3891. #ifdef CONFIG_I40E_DCB
  3892. /**
  3893. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  3894. * @vsi: the VSI being configured
  3895. *
  3896. * This function waits for the given VSI's queues to be disabled.
  3897. **/
  3898. static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  3899. {
  3900. struct i40e_pf *pf = vsi->back;
  3901. int i, pf_q, ret;
  3902. pf_q = vsi->base_queue;
  3903. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3904. /* Check and wait for the disable status of the queue */
  3905. ret = i40e_pf_txq_wait(pf, pf_q, false);
  3906. if (ret) {
  3907. dev_info(&pf->pdev->dev,
  3908. "VSI seid %d Tx ring %d disable timeout\n",
  3909. vsi->seid, pf_q);
  3910. return ret;
  3911. }
  3912. }
  3913. pf_q = vsi->base_queue;
  3914. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3915. /* Check and wait for the disable status of the queue */
  3916. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  3917. if (ret) {
  3918. dev_info(&pf->pdev->dev,
  3919. "VSI seid %d Rx ring %d disable timeout\n",
  3920. vsi->seid, pf_q);
  3921. return ret;
  3922. }
  3923. }
  3924. return 0;
  3925. }
  3926. /**
  3927. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  3928. * @pf: the PF
  3929. *
  3930. * This function waits for the queues to be in disabled state for all the
  3931. * VSIs that are managed by this PF.
  3932. **/
  3933. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  3934. {
  3935. int v, ret = 0;
  3936. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3937. if (pf->vsi[v]) {
  3938. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  3939. if (ret)
  3940. break;
  3941. }
  3942. }
  3943. return ret;
  3944. }
  3945. #endif
  3946. /**
  3947. * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
  3948. * @q_idx: TX queue number
  3949. * @vsi: Pointer to VSI struct
  3950. *
  3951. * This function checks specified queue for given VSI. Detects hung condition.
  3952. * Sets hung bit since it is two step process. Before next run of service task
  3953. * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
  3954. * hung condition remain unchanged and during subsequent run, this function
  3955. * issues SW interrupt to recover from hung condition.
  3956. **/
  3957. static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
  3958. {
  3959. struct i40e_ring *tx_ring = NULL;
  3960. struct i40e_pf *pf;
  3961. u32 head, val, tx_pending_hw;
  3962. int i;
  3963. pf = vsi->back;
  3964. /* now that we have an index, find the tx_ring struct */
  3965. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3966. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  3967. if (q_idx == vsi->tx_rings[i]->queue_index) {
  3968. tx_ring = vsi->tx_rings[i];
  3969. break;
  3970. }
  3971. }
  3972. }
  3973. if (!tx_ring)
  3974. return;
  3975. /* Read interrupt register */
  3976. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3977. val = rd32(&pf->hw,
  3978. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  3979. tx_ring->vsi->base_vector - 1));
  3980. else
  3981. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  3982. head = i40e_get_head(tx_ring);
  3983. tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
  3984. /* HW is done executing descriptors, updated HEAD write back,
  3985. * but SW hasn't processed those descriptors. If interrupt is
  3986. * not generated from this point ON, it could result into
  3987. * dev_watchdog detecting timeout on those netdev_queue,
  3988. * hence proactively trigger SW interrupt.
  3989. */
  3990. if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
  3991. /* NAPI Poll didn't run and clear since it was set */
  3992. if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
  3993. &tx_ring->q_vector->hung_detected)) {
  3994. netdev_info(vsi->netdev, "VSI_seid %d, Hung TX queue %d, tx_pending_hw: %d, NTC:0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x\n",
  3995. vsi->seid, q_idx, tx_pending_hw,
  3996. tx_ring->next_to_clean, head,
  3997. tx_ring->next_to_use,
  3998. readl(tx_ring->tail));
  3999. netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
  4000. vsi->seid, q_idx, val);
  4001. i40e_force_wb(vsi, tx_ring->q_vector);
  4002. } else {
  4003. /* First Chance - detected possible hung */
  4004. set_bit(I40E_Q_VECTOR_HUNG_DETECT,
  4005. &tx_ring->q_vector->hung_detected);
  4006. }
  4007. }
  4008. /* This is the case where we have interrupts missing,
  4009. * so the tx_pending in HW will most likely be 0, but we
  4010. * will have tx_pending in SW since the WB happened but the
  4011. * interrupt got lost.
  4012. */
  4013. if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
  4014. (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
  4015. local_bh_disable();
  4016. if (napi_reschedule(&tx_ring->q_vector->napi))
  4017. tx_ring->tx_stats.tx_lost_interrupt++;
  4018. local_bh_enable();
  4019. }
  4020. }
  4021. /**
  4022. * i40e_detect_recover_hung - Function to detect and recover hung_queues
  4023. * @pf: pointer to PF struct
  4024. *
  4025. * LAN VSI has netdev and netdev has TX queues. This function is to check
  4026. * each of those TX queues if they are hung, trigger recovery by issuing
  4027. * SW interrupt.
  4028. **/
  4029. static void i40e_detect_recover_hung(struct i40e_pf *pf)
  4030. {
  4031. struct net_device *netdev;
  4032. struct i40e_vsi *vsi;
  4033. int i;
  4034. /* Only for LAN VSI */
  4035. vsi = pf->vsi[pf->lan_vsi];
  4036. if (!vsi)
  4037. return;
  4038. /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
  4039. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  4040. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  4041. return;
  4042. /* Make sure type is MAIN VSI */
  4043. if (vsi->type != I40E_VSI_MAIN)
  4044. return;
  4045. netdev = vsi->netdev;
  4046. if (!netdev)
  4047. return;
  4048. /* Bail out if netif_carrier is not OK */
  4049. if (!netif_carrier_ok(netdev))
  4050. return;
  4051. /* Go thru' TX queues for netdev */
  4052. for (i = 0; i < netdev->num_tx_queues; i++) {
  4053. struct netdev_queue *q;
  4054. q = netdev_get_tx_queue(netdev, i);
  4055. if (q)
  4056. i40e_detect_recover_hung_queue(i, vsi);
  4057. }
  4058. }
  4059. /**
  4060. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4061. * @pf: pointer to PF
  4062. *
  4063. * Get TC map for ISCSI PF type that will include iSCSI TC
  4064. * and LAN TC.
  4065. **/
  4066. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4067. {
  4068. struct i40e_dcb_app_priority_table app;
  4069. struct i40e_hw *hw = &pf->hw;
  4070. u8 enabled_tc = 1; /* TC0 is always enabled */
  4071. u8 tc, i;
  4072. /* Get the iSCSI APP TLV */
  4073. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4074. for (i = 0; i < dcbcfg->numapps; i++) {
  4075. app = dcbcfg->app[i];
  4076. if (app.selector == I40E_APP_SEL_TCPIP &&
  4077. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4078. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4079. enabled_tc |= BIT(tc);
  4080. break;
  4081. }
  4082. }
  4083. return enabled_tc;
  4084. }
  4085. /**
  4086. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4087. * @dcbcfg: the corresponding DCBx configuration structure
  4088. *
  4089. * Return the number of TCs from given DCBx configuration
  4090. **/
  4091. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4092. {
  4093. int i, tc_unused = 0;
  4094. u8 num_tc = 0;
  4095. u8 ret = 0;
  4096. /* Scan the ETS Config Priority Table to find
  4097. * traffic class enabled for a given priority
  4098. * and create a bitmask of enabled TCs
  4099. */
  4100. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4101. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4102. /* Now scan the bitmask to check for
  4103. * contiguous TCs starting with TC0
  4104. */
  4105. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4106. if (num_tc & BIT(i)) {
  4107. if (!tc_unused) {
  4108. ret++;
  4109. } else {
  4110. pr_err("Non-contiguous TC - Disabling DCB\n");
  4111. return 1;
  4112. }
  4113. } else {
  4114. tc_unused = 1;
  4115. }
  4116. }
  4117. /* There is always at least TC0 */
  4118. if (!ret)
  4119. ret = 1;
  4120. return ret;
  4121. }
  4122. /**
  4123. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4124. * @dcbcfg: the corresponding DCBx configuration structure
  4125. *
  4126. * Query the current DCB configuration and return the number of
  4127. * traffic classes enabled from the given DCBX config
  4128. **/
  4129. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4130. {
  4131. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4132. u8 enabled_tc = 1;
  4133. u8 i;
  4134. for (i = 0; i < num_tc; i++)
  4135. enabled_tc |= BIT(i);
  4136. return enabled_tc;
  4137. }
  4138. /**
  4139. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4140. * @pf: PF being queried
  4141. *
  4142. * Return number of traffic classes enabled for the given PF
  4143. **/
  4144. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4145. {
  4146. struct i40e_hw *hw = &pf->hw;
  4147. u8 i, enabled_tc = 1;
  4148. u8 num_tc = 0;
  4149. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4150. /* If DCB is not enabled then always in single TC */
  4151. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4152. return 1;
  4153. /* SFP mode will be enabled for all TCs on port */
  4154. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4155. return i40e_dcb_get_num_tc(dcbcfg);
  4156. /* MFP mode return count of enabled TCs for this PF */
  4157. if (pf->hw.func_caps.iscsi)
  4158. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4159. else
  4160. return 1; /* Only TC0 */
  4161. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4162. if (enabled_tc & BIT(i))
  4163. num_tc++;
  4164. }
  4165. return num_tc;
  4166. }
  4167. /**
  4168. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4169. * @pf: PF being queried
  4170. *
  4171. * Return a bitmap for enabled traffic classes for this PF.
  4172. **/
  4173. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4174. {
  4175. /* If DCB is not enabled for this PF then just return default TC */
  4176. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4177. return I40E_DEFAULT_TRAFFIC_CLASS;
  4178. /* SFP mode we want PF to be enabled for all TCs */
  4179. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4180. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4181. /* MFP enabled and iSCSI PF type */
  4182. if (pf->hw.func_caps.iscsi)
  4183. return i40e_get_iscsi_tc_map(pf);
  4184. else
  4185. return I40E_DEFAULT_TRAFFIC_CLASS;
  4186. }
  4187. /**
  4188. * i40e_vsi_get_bw_info - Query VSI BW Information
  4189. * @vsi: the VSI being queried
  4190. *
  4191. * Returns 0 on success, negative value on failure
  4192. **/
  4193. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4194. {
  4195. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4196. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4197. struct i40e_pf *pf = vsi->back;
  4198. struct i40e_hw *hw = &pf->hw;
  4199. i40e_status ret;
  4200. u32 tc_bw_max;
  4201. int i;
  4202. /* Get the VSI level BW configuration */
  4203. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4204. if (ret) {
  4205. dev_info(&pf->pdev->dev,
  4206. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4207. i40e_stat_str(&pf->hw, ret),
  4208. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4209. return -EINVAL;
  4210. }
  4211. /* Get the VSI level BW configuration per TC */
  4212. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4213. NULL);
  4214. if (ret) {
  4215. dev_info(&pf->pdev->dev,
  4216. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4217. i40e_stat_str(&pf->hw, ret),
  4218. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4219. return -EINVAL;
  4220. }
  4221. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4222. dev_info(&pf->pdev->dev,
  4223. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4224. bw_config.tc_valid_bits,
  4225. bw_ets_config.tc_valid_bits);
  4226. /* Still continuing */
  4227. }
  4228. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4229. vsi->bw_max_quanta = bw_config.max_bw;
  4230. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4231. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4232. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4233. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4234. vsi->bw_ets_limit_credits[i] =
  4235. le16_to_cpu(bw_ets_config.credits[i]);
  4236. /* 3 bits out of 4 for each TC */
  4237. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4238. }
  4239. return 0;
  4240. }
  4241. /**
  4242. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4243. * @vsi: the VSI being configured
  4244. * @enabled_tc: TC bitmap
  4245. * @bw_credits: BW shared credits per TC
  4246. *
  4247. * Returns 0 on success, negative value on failure
  4248. **/
  4249. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4250. u8 *bw_share)
  4251. {
  4252. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4253. i40e_status ret;
  4254. int i;
  4255. bw_data.tc_valid_bits = enabled_tc;
  4256. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4257. bw_data.tc_bw_credits[i] = bw_share[i];
  4258. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4259. NULL);
  4260. if (ret) {
  4261. dev_info(&vsi->back->pdev->dev,
  4262. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4263. vsi->back->hw.aq.asq_last_status);
  4264. return -EINVAL;
  4265. }
  4266. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4267. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4268. return 0;
  4269. }
  4270. /**
  4271. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4272. * @vsi: the VSI being configured
  4273. * @enabled_tc: TC map to be enabled
  4274. *
  4275. **/
  4276. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4277. {
  4278. struct net_device *netdev = vsi->netdev;
  4279. struct i40e_pf *pf = vsi->back;
  4280. struct i40e_hw *hw = &pf->hw;
  4281. u8 netdev_tc = 0;
  4282. int i;
  4283. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4284. if (!netdev)
  4285. return;
  4286. if (!enabled_tc) {
  4287. netdev_reset_tc(netdev);
  4288. return;
  4289. }
  4290. /* Set up actual enabled TCs on the VSI */
  4291. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4292. return;
  4293. /* set per TC queues for the VSI */
  4294. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4295. /* Only set TC queues for enabled tcs
  4296. *
  4297. * e.g. For a VSI that has TC0 and TC3 enabled the
  4298. * enabled_tc bitmap would be 0x00001001; the driver
  4299. * will set the numtc for netdev as 2 that will be
  4300. * referenced by the netdev layer as TC 0 and 1.
  4301. */
  4302. if (vsi->tc_config.enabled_tc & BIT(i))
  4303. netdev_set_tc_queue(netdev,
  4304. vsi->tc_config.tc_info[i].netdev_tc,
  4305. vsi->tc_config.tc_info[i].qcount,
  4306. vsi->tc_config.tc_info[i].qoffset);
  4307. }
  4308. /* Assign UP2TC map for the VSI */
  4309. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4310. /* Get the actual TC# for the UP */
  4311. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4312. /* Get the mapped netdev TC# for the UP */
  4313. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4314. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4315. }
  4316. }
  4317. /**
  4318. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4319. * @vsi: the VSI being configured
  4320. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4321. **/
  4322. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4323. struct i40e_vsi_context *ctxt)
  4324. {
  4325. /* copy just the sections touched not the entire info
  4326. * since not all sections are valid as returned by
  4327. * update vsi params
  4328. */
  4329. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4330. memcpy(&vsi->info.queue_mapping,
  4331. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4332. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4333. sizeof(vsi->info.tc_mapping));
  4334. }
  4335. /**
  4336. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4337. * @vsi: VSI to be configured
  4338. * @enabled_tc: TC bitmap
  4339. *
  4340. * This configures a particular VSI for TCs that are mapped to the
  4341. * given TC bitmap. It uses default bandwidth share for TCs across
  4342. * VSIs to configure TC for a particular VSI.
  4343. *
  4344. * NOTE:
  4345. * It is expected that the VSI queues have been quisced before calling
  4346. * this function.
  4347. **/
  4348. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4349. {
  4350. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4351. struct i40e_vsi_context ctxt;
  4352. int ret = 0;
  4353. int i;
  4354. /* Check if enabled_tc is same as existing or new TCs */
  4355. if (vsi->tc_config.enabled_tc == enabled_tc)
  4356. return ret;
  4357. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4358. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4359. if (enabled_tc & BIT(i))
  4360. bw_share[i] = 1;
  4361. }
  4362. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4363. if (ret) {
  4364. dev_info(&vsi->back->pdev->dev,
  4365. "Failed configuring TC map %d for VSI %d\n",
  4366. enabled_tc, vsi->seid);
  4367. goto out;
  4368. }
  4369. /* Update Queue Pairs Mapping for currently enabled UPs */
  4370. ctxt.seid = vsi->seid;
  4371. ctxt.pf_num = vsi->back->hw.pf_id;
  4372. ctxt.vf_num = 0;
  4373. ctxt.uplink_seid = vsi->uplink_seid;
  4374. ctxt.info = vsi->info;
  4375. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4376. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4377. ctxt.info.valid_sections |=
  4378. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4379. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4380. }
  4381. /* Update the VSI after updating the VSI queue-mapping information */
  4382. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  4383. if (ret) {
  4384. dev_info(&vsi->back->pdev->dev,
  4385. "Update vsi tc config failed, err %s aq_err %s\n",
  4386. i40e_stat_str(&vsi->back->hw, ret),
  4387. i40e_aq_str(&vsi->back->hw,
  4388. vsi->back->hw.aq.asq_last_status));
  4389. goto out;
  4390. }
  4391. /* update the local VSI info with updated queue map */
  4392. i40e_vsi_update_queue_map(vsi, &ctxt);
  4393. vsi->info.valid_sections = 0;
  4394. /* Update current VSI BW information */
  4395. ret = i40e_vsi_get_bw_info(vsi);
  4396. if (ret) {
  4397. dev_info(&vsi->back->pdev->dev,
  4398. "Failed updating vsi bw info, err %s aq_err %s\n",
  4399. i40e_stat_str(&vsi->back->hw, ret),
  4400. i40e_aq_str(&vsi->back->hw,
  4401. vsi->back->hw.aq.asq_last_status));
  4402. goto out;
  4403. }
  4404. /* Update the netdev TC setup */
  4405. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4406. out:
  4407. return ret;
  4408. }
  4409. /**
  4410. * i40e_veb_config_tc - Configure TCs for given VEB
  4411. * @veb: given VEB
  4412. * @enabled_tc: TC bitmap
  4413. *
  4414. * Configures given TC bitmap for VEB (switching) element
  4415. **/
  4416. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  4417. {
  4418. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  4419. struct i40e_pf *pf = veb->pf;
  4420. int ret = 0;
  4421. int i;
  4422. /* No TCs or already enabled TCs just return */
  4423. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  4424. return ret;
  4425. bw_data.tc_valid_bits = enabled_tc;
  4426. /* bw_data.absolute_credits is not set (relative) */
  4427. /* Enable ETS TCs with equal BW Share for now */
  4428. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4429. if (enabled_tc & BIT(i))
  4430. bw_data.tc_bw_share_credits[i] = 1;
  4431. }
  4432. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  4433. &bw_data, NULL);
  4434. if (ret) {
  4435. dev_info(&pf->pdev->dev,
  4436. "VEB bw config failed, err %s aq_err %s\n",
  4437. i40e_stat_str(&pf->hw, ret),
  4438. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4439. goto out;
  4440. }
  4441. /* Update the BW information */
  4442. ret = i40e_veb_get_bw_info(veb);
  4443. if (ret) {
  4444. dev_info(&pf->pdev->dev,
  4445. "Failed getting veb bw config, err %s aq_err %s\n",
  4446. i40e_stat_str(&pf->hw, ret),
  4447. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4448. }
  4449. out:
  4450. return ret;
  4451. }
  4452. #ifdef CONFIG_I40E_DCB
  4453. /**
  4454. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  4455. * @pf: PF struct
  4456. *
  4457. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  4458. * the caller would've quiesce all the VSIs before calling
  4459. * this function
  4460. **/
  4461. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  4462. {
  4463. u8 tc_map = 0;
  4464. int ret;
  4465. u8 v;
  4466. /* Enable the TCs available on PF to all VEBs */
  4467. tc_map = i40e_pf_get_tc_map(pf);
  4468. for (v = 0; v < I40E_MAX_VEB; v++) {
  4469. if (!pf->veb[v])
  4470. continue;
  4471. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  4472. if (ret) {
  4473. dev_info(&pf->pdev->dev,
  4474. "Failed configuring TC for VEB seid=%d\n",
  4475. pf->veb[v]->seid);
  4476. /* Will try to configure as many components */
  4477. }
  4478. }
  4479. /* Update each VSI */
  4480. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4481. if (!pf->vsi[v])
  4482. continue;
  4483. /* - Enable all TCs for the LAN VSI
  4484. * - For all others keep them at TC0 for now
  4485. */
  4486. if (v == pf->lan_vsi)
  4487. tc_map = i40e_pf_get_tc_map(pf);
  4488. else
  4489. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  4490. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  4491. if (ret) {
  4492. dev_info(&pf->pdev->dev,
  4493. "Failed configuring TC for VSI seid=%d\n",
  4494. pf->vsi[v]->seid);
  4495. /* Will try to configure as many components */
  4496. } else {
  4497. /* Re-configure VSI vectors based on updated TC map */
  4498. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  4499. if (pf->vsi[v]->netdev)
  4500. i40e_dcbnl_set_all(pf->vsi[v]);
  4501. }
  4502. }
  4503. }
  4504. /**
  4505. * i40e_resume_port_tx - Resume port Tx
  4506. * @pf: PF struct
  4507. *
  4508. * Resume a port's Tx and issue a PF reset in case of failure to
  4509. * resume.
  4510. **/
  4511. static int i40e_resume_port_tx(struct i40e_pf *pf)
  4512. {
  4513. struct i40e_hw *hw = &pf->hw;
  4514. int ret;
  4515. ret = i40e_aq_resume_port_tx(hw, NULL);
  4516. if (ret) {
  4517. dev_info(&pf->pdev->dev,
  4518. "Resume Port Tx failed, err %s aq_err %s\n",
  4519. i40e_stat_str(&pf->hw, ret),
  4520. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4521. /* Schedule PF reset to recover */
  4522. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  4523. i40e_service_event_schedule(pf);
  4524. }
  4525. return ret;
  4526. }
  4527. /**
  4528. * i40e_init_pf_dcb - Initialize DCB configuration
  4529. * @pf: PF being configured
  4530. *
  4531. * Query the current DCB configuration and cache it
  4532. * in the hardware structure
  4533. **/
  4534. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  4535. {
  4536. struct i40e_hw *hw = &pf->hw;
  4537. int err = 0;
  4538. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
  4539. if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
  4540. goto out;
  4541. /* Get the initial DCB configuration */
  4542. err = i40e_init_dcb(hw);
  4543. if (!err) {
  4544. /* Device/Function is not DCBX capable */
  4545. if ((!hw->func_caps.dcb) ||
  4546. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  4547. dev_info(&pf->pdev->dev,
  4548. "DCBX offload is not supported or is disabled for this PF.\n");
  4549. } else {
  4550. /* When status is not DISABLED then DCBX in FW */
  4551. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  4552. DCB_CAP_DCBX_VER_IEEE;
  4553. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  4554. /* Enable DCB tagging only when more than one TC
  4555. * or explicitly disable if only one TC
  4556. */
  4557. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  4558. pf->flags |= I40E_FLAG_DCB_ENABLED;
  4559. else
  4560. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  4561. dev_dbg(&pf->pdev->dev,
  4562. "DCBX offload is supported for this PF.\n");
  4563. }
  4564. } else {
  4565. dev_info(&pf->pdev->dev,
  4566. "Query for DCB configuration failed, err %s aq_err %s\n",
  4567. i40e_stat_str(&pf->hw, err),
  4568. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4569. }
  4570. out:
  4571. return err;
  4572. }
  4573. #endif /* CONFIG_I40E_DCB */
  4574. #define SPEED_SIZE 14
  4575. #define FC_SIZE 8
  4576. /**
  4577. * i40e_print_link_message - print link up or down
  4578. * @vsi: the VSI for which link needs a message
  4579. */
  4580. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  4581. {
  4582. enum i40e_aq_link_speed new_speed;
  4583. char *speed = "Unknown";
  4584. char *fc = "Unknown";
  4585. char *fec = "";
  4586. char *an = "";
  4587. new_speed = vsi->back->hw.phy.link_info.link_speed;
  4588. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  4589. return;
  4590. vsi->current_isup = isup;
  4591. vsi->current_speed = new_speed;
  4592. if (!isup) {
  4593. netdev_info(vsi->netdev, "NIC Link is Down\n");
  4594. return;
  4595. }
  4596. /* Warn user if link speed on NPAR enabled partition is not at
  4597. * least 10GB
  4598. */
  4599. if (vsi->back->hw.func_caps.npar_enable &&
  4600. (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  4601. vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  4602. netdev_warn(vsi->netdev,
  4603. "The partition detected link speed that is less than 10Gbps\n");
  4604. switch (vsi->back->hw.phy.link_info.link_speed) {
  4605. case I40E_LINK_SPEED_40GB:
  4606. speed = "40 G";
  4607. break;
  4608. case I40E_LINK_SPEED_20GB:
  4609. speed = "20 G";
  4610. break;
  4611. case I40E_LINK_SPEED_25GB:
  4612. speed = "25 G";
  4613. break;
  4614. case I40E_LINK_SPEED_10GB:
  4615. speed = "10 G";
  4616. break;
  4617. case I40E_LINK_SPEED_1GB:
  4618. speed = "1000 M";
  4619. break;
  4620. case I40E_LINK_SPEED_100MB:
  4621. speed = "100 M";
  4622. break;
  4623. default:
  4624. break;
  4625. }
  4626. switch (vsi->back->hw.fc.current_mode) {
  4627. case I40E_FC_FULL:
  4628. fc = "RX/TX";
  4629. break;
  4630. case I40E_FC_TX_PAUSE:
  4631. fc = "TX";
  4632. break;
  4633. case I40E_FC_RX_PAUSE:
  4634. fc = "RX";
  4635. break;
  4636. default:
  4637. fc = "None";
  4638. break;
  4639. }
  4640. if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  4641. fec = ", FEC: None";
  4642. an = ", Autoneg: False";
  4643. if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  4644. an = ", Autoneg: True";
  4645. if (vsi->back->hw.phy.link_info.fec_info &
  4646. I40E_AQ_CONFIG_FEC_KR_ENA)
  4647. fec = ", FEC: CL74 FC-FEC/BASE-R";
  4648. else if (vsi->back->hw.phy.link_info.fec_info &
  4649. I40E_AQ_CONFIG_FEC_RS_ENA)
  4650. fec = ", FEC: CL108 RS-FEC";
  4651. }
  4652. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
  4653. speed, fec, an, fc);
  4654. }
  4655. /**
  4656. * i40e_up_complete - Finish the last steps of bringing up a connection
  4657. * @vsi: the VSI being configured
  4658. **/
  4659. static int i40e_up_complete(struct i40e_vsi *vsi)
  4660. {
  4661. struct i40e_pf *pf = vsi->back;
  4662. int err;
  4663. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4664. i40e_vsi_configure_msix(vsi);
  4665. else
  4666. i40e_configure_msi_and_legacy(vsi);
  4667. /* start rings */
  4668. err = i40e_vsi_start_rings(vsi);
  4669. if (err)
  4670. return err;
  4671. clear_bit(__I40E_DOWN, &vsi->state);
  4672. i40e_napi_enable_all(vsi);
  4673. i40e_vsi_enable_irq(vsi);
  4674. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  4675. (vsi->netdev)) {
  4676. i40e_print_link_message(vsi, true);
  4677. netif_tx_start_all_queues(vsi->netdev);
  4678. netif_carrier_on(vsi->netdev);
  4679. } else if (vsi->netdev) {
  4680. i40e_print_link_message(vsi, false);
  4681. /* need to check for qualified module here*/
  4682. if ((pf->hw.phy.link_info.link_info &
  4683. I40E_AQ_MEDIA_AVAILABLE) &&
  4684. (!(pf->hw.phy.link_info.an_info &
  4685. I40E_AQ_QUALIFIED_MODULE)))
  4686. netdev_err(vsi->netdev,
  4687. "the driver failed to link because an unqualified module was detected.");
  4688. }
  4689. /* replay FDIR SB filters */
  4690. if (vsi->type == I40E_VSI_FDIR) {
  4691. /* reset fd counters */
  4692. pf->fd_add_err = 0;
  4693. pf->fd_atr_cnt = 0;
  4694. i40e_fdir_filter_restore(vsi);
  4695. }
  4696. /* On the next run of the service_task, notify any clients of the new
  4697. * opened netdev
  4698. */
  4699. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  4700. i40e_service_event_schedule(pf);
  4701. return 0;
  4702. }
  4703. /**
  4704. * i40e_vsi_reinit_locked - Reset the VSI
  4705. * @vsi: the VSI being configured
  4706. *
  4707. * Rebuild the ring structs after some configuration
  4708. * has changed, e.g. MTU size.
  4709. **/
  4710. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  4711. {
  4712. struct i40e_pf *pf = vsi->back;
  4713. WARN_ON(in_interrupt());
  4714. while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
  4715. usleep_range(1000, 2000);
  4716. i40e_down(vsi);
  4717. i40e_up(vsi);
  4718. clear_bit(__I40E_CONFIG_BUSY, &pf->state);
  4719. }
  4720. /**
  4721. * i40e_up - Bring the connection back up after being down
  4722. * @vsi: the VSI being configured
  4723. **/
  4724. int i40e_up(struct i40e_vsi *vsi)
  4725. {
  4726. int err;
  4727. err = i40e_vsi_configure(vsi);
  4728. if (!err)
  4729. err = i40e_up_complete(vsi);
  4730. return err;
  4731. }
  4732. /**
  4733. * i40e_down - Shutdown the connection processing
  4734. * @vsi: the VSI being stopped
  4735. **/
  4736. void i40e_down(struct i40e_vsi *vsi)
  4737. {
  4738. int i;
  4739. /* It is assumed that the caller of this function
  4740. * sets the vsi->state __I40E_DOWN bit.
  4741. */
  4742. if (vsi->netdev) {
  4743. netif_carrier_off(vsi->netdev);
  4744. netif_tx_disable(vsi->netdev);
  4745. }
  4746. i40e_vsi_disable_irq(vsi);
  4747. i40e_vsi_stop_rings(vsi);
  4748. i40e_napi_disable_all(vsi);
  4749. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4750. i40e_clean_tx_ring(vsi->tx_rings[i]);
  4751. i40e_clean_rx_ring(vsi->rx_rings[i]);
  4752. }
  4753. }
  4754. /**
  4755. * i40e_setup_tc - configure multiple traffic classes
  4756. * @netdev: net device to configure
  4757. * @tc: number of traffic classes to enable
  4758. **/
  4759. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  4760. {
  4761. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4762. struct i40e_vsi *vsi = np->vsi;
  4763. struct i40e_pf *pf = vsi->back;
  4764. u8 enabled_tc = 0;
  4765. int ret = -EINVAL;
  4766. int i;
  4767. /* Check if DCB enabled to continue */
  4768. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4769. netdev_info(netdev, "DCB is not enabled for adapter\n");
  4770. goto exit;
  4771. }
  4772. /* Check if MFP enabled */
  4773. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  4774. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  4775. goto exit;
  4776. }
  4777. /* Check whether tc count is within enabled limit */
  4778. if (tc > i40e_pf_get_num_tc(pf)) {
  4779. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  4780. goto exit;
  4781. }
  4782. /* Generate TC map for number of tc requested */
  4783. for (i = 0; i < tc; i++)
  4784. enabled_tc |= BIT(i);
  4785. /* Requesting same TC configuration as already enabled */
  4786. if (enabled_tc == vsi->tc_config.enabled_tc)
  4787. return 0;
  4788. /* Quiesce VSI queues */
  4789. i40e_quiesce_vsi(vsi);
  4790. /* Configure VSI for enabled TCs */
  4791. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  4792. if (ret) {
  4793. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  4794. vsi->seid);
  4795. goto exit;
  4796. }
  4797. /* Unquiesce VSI */
  4798. i40e_unquiesce_vsi(vsi);
  4799. exit:
  4800. return ret;
  4801. }
  4802. static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
  4803. struct tc_to_netdev *tc)
  4804. {
  4805. if (tc->type != TC_SETUP_MQPRIO)
  4806. return -EINVAL;
  4807. tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
  4808. return i40e_setup_tc(netdev, tc->mqprio->num_tc);
  4809. }
  4810. /**
  4811. * i40e_open - Called when a network interface is made active
  4812. * @netdev: network interface device structure
  4813. *
  4814. * The open entry point is called when a network interface is made
  4815. * active by the system (IFF_UP). At this point all resources needed
  4816. * for transmit and receive operations are allocated, the interrupt
  4817. * handler is registered with the OS, the netdev watchdog subtask is
  4818. * enabled, and the stack is notified that the interface is ready.
  4819. *
  4820. * Returns 0 on success, negative value on failure
  4821. **/
  4822. int i40e_open(struct net_device *netdev)
  4823. {
  4824. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4825. struct i40e_vsi *vsi = np->vsi;
  4826. struct i40e_pf *pf = vsi->back;
  4827. int err;
  4828. /* disallow open during test or if eeprom is broken */
  4829. if (test_bit(__I40E_TESTING, &pf->state) ||
  4830. test_bit(__I40E_BAD_EEPROM, &pf->state))
  4831. return -EBUSY;
  4832. netif_carrier_off(netdev);
  4833. err = i40e_vsi_open(vsi);
  4834. if (err)
  4835. return err;
  4836. /* configure global TSO hardware offload settings */
  4837. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  4838. TCP_FLAG_FIN) >> 16);
  4839. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  4840. TCP_FLAG_FIN |
  4841. TCP_FLAG_CWR) >> 16);
  4842. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  4843. udp_tunnel_get_rx_info(netdev);
  4844. return 0;
  4845. }
  4846. /**
  4847. * i40e_vsi_open -
  4848. * @vsi: the VSI to open
  4849. *
  4850. * Finish initialization of the VSI.
  4851. *
  4852. * Returns 0 on success, negative value on failure
  4853. **/
  4854. int i40e_vsi_open(struct i40e_vsi *vsi)
  4855. {
  4856. struct i40e_pf *pf = vsi->back;
  4857. char int_name[I40E_INT_NAME_STR_LEN];
  4858. int err;
  4859. /* allocate descriptors */
  4860. err = i40e_vsi_setup_tx_resources(vsi);
  4861. if (err)
  4862. goto err_setup_tx;
  4863. err = i40e_vsi_setup_rx_resources(vsi);
  4864. if (err)
  4865. goto err_setup_rx;
  4866. err = i40e_vsi_configure(vsi);
  4867. if (err)
  4868. goto err_setup_rx;
  4869. if (vsi->netdev) {
  4870. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  4871. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  4872. err = i40e_vsi_request_irq(vsi, int_name);
  4873. if (err)
  4874. goto err_setup_rx;
  4875. /* Notify the stack of the actual queue counts. */
  4876. err = netif_set_real_num_tx_queues(vsi->netdev,
  4877. vsi->num_queue_pairs);
  4878. if (err)
  4879. goto err_set_queues;
  4880. err = netif_set_real_num_rx_queues(vsi->netdev,
  4881. vsi->num_queue_pairs);
  4882. if (err)
  4883. goto err_set_queues;
  4884. } else if (vsi->type == I40E_VSI_FDIR) {
  4885. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  4886. dev_driver_string(&pf->pdev->dev),
  4887. dev_name(&pf->pdev->dev));
  4888. err = i40e_vsi_request_irq(vsi, int_name);
  4889. } else {
  4890. err = -EINVAL;
  4891. goto err_setup_rx;
  4892. }
  4893. err = i40e_up_complete(vsi);
  4894. if (err)
  4895. goto err_up_complete;
  4896. return 0;
  4897. err_up_complete:
  4898. i40e_down(vsi);
  4899. err_set_queues:
  4900. i40e_vsi_free_irq(vsi);
  4901. err_setup_rx:
  4902. i40e_vsi_free_rx_resources(vsi);
  4903. err_setup_tx:
  4904. i40e_vsi_free_tx_resources(vsi);
  4905. if (vsi == pf->vsi[pf->lan_vsi])
  4906. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  4907. return err;
  4908. }
  4909. /**
  4910. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  4911. * @pf: Pointer to PF
  4912. *
  4913. * This function destroys the hlist where all the Flow Director
  4914. * filters were saved.
  4915. **/
  4916. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  4917. {
  4918. struct i40e_fdir_filter *filter;
  4919. struct i40e_flex_pit *pit_entry, *tmp;
  4920. struct hlist_node *node2;
  4921. hlist_for_each_entry_safe(filter, node2,
  4922. &pf->fdir_filter_list, fdir_node) {
  4923. hlist_del(&filter->fdir_node);
  4924. kfree(filter);
  4925. }
  4926. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  4927. list_del(&pit_entry->list);
  4928. kfree(pit_entry);
  4929. }
  4930. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  4931. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  4932. list_del(&pit_entry->list);
  4933. kfree(pit_entry);
  4934. }
  4935. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  4936. pf->fdir_pf_active_filters = 0;
  4937. pf->fd_tcp4_filter_cnt = 0;
  4938. pf->fd_udp4_filter_cnt = 0;
  4939. pf->fd_sctp4_filter_cnt = 0;
  4940. pf->fd_ip4_filter_cnt = 0;
  4941. /* Reprogram the default input set for TCP/IPv4 */
  4942. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  4943. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4944. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4945. /* Reprogram the default input set for UDP/IPv4 */
  4946. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  4947. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4948. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4949. /* Reprogram the default input set for SCTP/IPv4 */
  4950. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  4951. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4952. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4953. /* Reprogram the default input set for Other/IPv4 */
  4954. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  4955. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  4956. }
  4957. /**
  4958. * i40e_close - Disables a network interface
  4959. * @netdev: network interface device structure
  4960. *
  4961. * The close entry point is called when an interface is de-activated
  4962. * by the OS. The hardware is still under the driver's control, but
  4963. * this netdev interface is disabled.
  4964. *
  4965. * Returns 0, this is not allowed to fail
  4966. **/
  4967. int i40e_close(struct net_device *netdev)
  4968. {
  4969. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4970. struct i40e_vsi *vsi = np->vsi;
  4971. i40e_vsi_close(vsi);
  4972. return 0;
  4973. }
  4974. /**
  4975. * i40e_do_reset - Start a PF or Core Reset sequence
  4976. * @pf: board private structure
  4977. * @reset_flags: which reset is requested
  4978. *
  4979. * The essential difference in resets is that the PF Reset
  4980. * doesn't clear the packet buffers, doesn't reset the PE
  4981. * firmware, and doesn't bother the other PFs on the chip.
  4982. **/
  4983. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
  4984. {
  4985. u32 val;
  4986. WARN_ON(in_interrupt());
  4987. /* do the biggest reset indicated */
  4988. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  4989. /* Request a Global Reset
  4990. *
  4991. * This will start the chip's countdown to the actual full
  4992. * chip reset event, and a warning interrupt to be sent
  4993. * to all PFs, including the requestor. Our handler
  4994. * for the warning interrupt will deal with the shutdown
  4995. * and recovery of the switch setup.
  4996. */
  4997. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  4998. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  4999. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  5000. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5001. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  5002. /* Request a Core Reset
  5003. *
  5004. * Same as Global Reset, except does *not* include the MAC/PHY
  5005. */
  5006. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  5007. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  5008. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  5009. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5010. i40e_flush(&pf->hw);
  5011. } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
  5012. /* Request a PF Reset
  5013. *
  5014. * Resets only the PF-specific registers
  5015. *
  5016. * This goes directly to the tear-down and rebuild of
  5017. * the switch, since we need to do all the recovery as
  5018. * for the Core Reset.
  5019. */
  5020. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  5021. i40e_handle_reset_warning(pf);
  5022. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  5023. int v;
  5024. /* Find the VSI(s) that requested a re-init */
  5025. dev_info(&pf->pdev->dev,
  5026. "VSI reinit requested\n");
  5027. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5028. struct i40e_vsi *vsi = pf->vsi[v];
  5029. if (vsi != NULL &&
  5030. test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
  5031. i40e_vsi_reinit_locked(pf->vsi[v]);
  5032. clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  5033. }
  5034. }
  5035. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  5036. int v;
  5037. /* Find the VSI(s) that needs to be brought down */
  5038. dev_info(&pf->pdev->dev, "VSI down requested\n");
  5039. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5040. struct i40e_vsi *vsi = pf->vsi[v];
  5041. if (vsi != NULL &&
  5042. test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
  5043. set_bit(__I40E_DOWN, &vsi->state);
  5044. i40e_down(vsi);
  5045. clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
  5046. }
  5047. }
  5048. } else {
  5049. dev_info(&pf->pdev->dev,
  5050. "bad reset request 0x%08x\n", reset_flags);
  5051. }
  5052. }
  5053. #ifdef CONFIG_I40E_DCB
  5054. /**
  5055. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  5056. * @pf: board private structure
  5057. * @old_cfg: current DCB config
  5058. * @new_cfg: new DCB config
  5059. **/
  5060. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  5061. struct i40e_dcbx_config *old_cfg,
  5062. struct i40e_dcbx_config *new_cfg)
  5063. {
  5064. bool need_reconfig = false;
  5065. /* Check if ETS configuration has changed */
  5066. if (memcmp(&new_cfg->etscfg,
  5067. &old_cfg->etscfg,
  5068. sizeof(new_cfg->etscfg))) {
  5069. /* If Priority Table has changed reconfig is needed */
  5070. if (memcmp(&new_cfg->etscfg.prioritytable,
  5071. &old_cfg->etscfg.prioritytable,
  5072. sizeof(new_cfg->etscfg.prioritytable))) {
  5073. need_reconfig = true;
  5074. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  5075. }
  5076. if (memcmp(&new_cfg->etscfg.tcbwtable,
  5077. &old_cfg->etscfg.tcbwtable,
  5078. sizeof(new_cfg->etscfg.tcbwtable)))
  5079. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  5080. if (memcmp(&new_cfg->etscfg.tsatable,
  5081. &old_cfg->etscfg.tsatable,
  5082. sizeof(new_cfg->etscfg.tsatable)))
  5083. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  5084. }
  5085. /* Check if PFC configuration has changed */
  5086. if (memcmp(&new_cfg->pfc,
  5087. &old_cfg->pfc,
  5088. sizeof(new_cfg->pfc))) {
  5089. need_reconfig = true;
  5090. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  5091. }
  5092. /* Check if APP Table has changed */
  5093. if (memcmp(&new_cfg->app,
  5094. &old_cfg->app,
  5095. sizeof(new_cfg->app))) {
  5096. need_reconfig = true;
  5097. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  5098. }
  5099. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  5100. return need_reconfig;
  5101. }
  5102. /**
  5103. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  5104. * @pf: board private structure
  5105. * @e: event info posted on ARQ
  5106. **/
  5107. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  5108. struct i40e_arq_event_info *e)
  5109. {
  5110. struct i40e_aqc_lldp_get_mib *mib =
  5111. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  5112. struct i40e_hw *hw = &pf->hw;
  5113. struct i40e_dcbx_config tmp_dcbx_cfg;
  5114. bool need_reconfig = false;
  5115. int ret = 0;
  5116. u8 type;
  5117. /* Not DCB capable or capability disabled */
  5118. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  5119. return ret;
  5120. /* Ignore if event is not for Nearest Bridge */
  5121. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  5122. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  5123. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  5124. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  5125. return ret;
  5126. /* Check MIB Type and return if event for Remote MIB update */
  5127. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  5128. dev_dbg(&pf->pdev->dev,
  5129. "LLDP event mib type %s\n", type ? "remote" : "local");
  5130. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  5131. /* Update the remote cached instance and return */
  5132. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  5133. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  5134. &hw->remote_dcbx_config);
  5135. goto exit;
  5136. }
  5137. /* Store the old configuration */
  5138. tmp_dcbx_cfg = hw->local_dcbx_config;
  5139. /* Reset the old DCBx configuration data */
  5140. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  5141. /* Get updated DCBX data from firmware */
  5142. ret = i40e_get_dcb_config(&pf->hw);
  5143. if (ret) {
  5144. dev_info(&pf->pdev->dev,
  5145. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  5146. i40e_stat_str(&pf->hw, ret),
  5147. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5148. goto exit;
  5149. }
  5150. /* No change detected in DCBX configs */
  5151. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  5152. sizeof(tmp_dcbx_cfg))) {
  5153. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  5154. goto exit;
  5155. }
  5156. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  5157. &hw->local_dcbx_config);
  5158. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  5159. if (!need_reconfig)
  5160. goto exit;
  5161. /* Enable DCB tagging only when more than one TC */
  5162. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5163. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5164. else
  5165. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5166. set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  5167. /* Reconfiguration needed quiesce all VSIs */
  5168. i40e_pf_quiesce_all_vsi(pf);
  5169. /* Changes in configuration update VEB/VSI */
  5170. i40e_dcb_reconfigure(pf);
  5171. ret = i40e_resume_port_tx(pf);
  5172. clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  5173. /* In case of error no point in resuming VSIs */
  5174. if (ret)
  5175. goto exit;
  5176. /* Wait for the PF's queues to be disabled */
  5177. ret = i40e_pf_wait_queues_disabled(pf);
  5178. if (ret) {
  5179. /* Schedule PF reset to recover */
  5180. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5181. i40e_service_event_schedule(pf);
  5182. } else {
  5183. i40e_pf_unquiesce_all_vsi(pf);
  5184. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  5185. I40E_FLAG_CLIENT_L2_CHANGE);
  5186. }
  5187. exit:
  5188. return ret;
  5189. }
  5190. #endif /* CONFIG_I40E_DCB */
  5191. /**
  5192. * i40e_do_reset_safe - Protected reset path for userland calls.
  5193. * @pf: board private structure
  5194. * @reset_flags: which reset is requested
  5195. *
  5196. **/
  5197. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  5198. {
  5199. rtnl_lock();
  5200. i40e_do_reset(pf, reset_flags);
  5201. rtnl_unlock();
  5202. }
  5203. /**
  5204. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  5205. * @pf: board private structure
  5206. * @e: event info posted on ARQ
  5207. *
  5208. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  5209. * and VF queues
  5210. **/
  5211. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  5212. struct i40e_arq_event_info *e)
  5213. {
  5214. struct i40e_aqc_lan_overflow *data =
  5215. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  5216. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  5217. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  5218. struct i40e_hw *hw = &pf->hw;
  5219. struct i40e_vf *vf;
  5220. u16 vf_id;
  5221. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  5222. queue, qtx_ctl);
  5223. /* Queue belongs to VF, find the VF and issue VF reset */
  5224. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  5225. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  5226. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  5227. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  5228. vf_id -= hw->func_caps.vf_base_id;
  5229. vf = &pf->vf[vf_id];
  5230. i40e_vc_notify_vf_reset(vf);
  5231. /* Allow VF to process pending reset notification */
  5232. msleep(20);
  5233. i40e_reset_vf(vf, false);
  5234. }
  5235. }
  5236. /**
  5237. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  5238. * @pf: board private structure
  5239. **/
  5240. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  5241. {
  5242. u32 val, fcnt_prog;
  5243. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5244. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  5245. return fcnt_prog;
  5246. }
  5247. /**
  5248. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  5249. * @pf: board private structure
  5250. **/
  5251. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  5252. {
  5253. u32 val, fcnt_prog;
  5254. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5255. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  5256. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  5257. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  5258. return fcnt_prog;
  5259. }
  5260. /**
  5261. * i40e_get_global_fd_count - Get total FD filters programmed on device
  5262. * @pf: board private structure
  5263. **/
  5264. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  5265. {
  5266. u32 val, fcnt_prog;
  5267. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  5268. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  5269. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  5270. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  5271. return fcnt_prog;
  5272. }
  5273. /**
  5274. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  5275. * @pf: board private structure
  5276. **/
  5277. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  5278. {
  5279. struct i40e_fdir_filter *filter;
  5280. u32 fcnt_prog, fcnt_avail;
  5281. struct hlist_node *node;
  5282. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5283. return;
  5284. /* Check if, FD SB or ATR was auto disabled and if there is enough room
  5285. * to re-enable
  5286. */
  5287. fcnt_prog = i40e_get_global_fd_count(pf);
  5288. fcnt_avail = pf->fdir_pf_filter_count;
  5289. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  5290. (pf->fd_add_err == 0) ||
  5291. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
  5292. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  5293. (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
  5294. pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5295. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5296. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  5297. }
  5298. }
  5299. /* Wait for some more space to be available to turn on ATR. We also
  5300. * must check that no existing ntuple rules for TCP are in effect
  5301. */
  5302. if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
  5303. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5304. (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5305. (pf->fd_tcp4_filter_cnt == 0)) {
  5306. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5307. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5308. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  5309. }
  5310. }
  5311. /* if hw had a problem adding a filter, delete it */
  5312. if (pf->fd_inv > 0) {
  5313. hlist_for_each_entry_safe(filter, node,
  5314. &pf->fdir_filter_list, fdir_node) {
  5315. if (filter->fd_id == pf->fd_inv) {
  5316. hlist_del(&filter->fdir_node);
  5317. kfree(filter);
  5318. pf->fdir_pf_active_filters--;
  5319. }
  5320. }
  5321. }
  5322. }
  5323. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  5324. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  5325. /**
  5326. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  5327. * @pf: board private structure
  5328. **/
  5329. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  5330. {
  5331. unsigned long min_flush_time;
  5332. int flush_wait_retry = 50;
  5333. bool disable_atr = false;
  5334. int fd_room;
  5335. int reg;
  5336. if (!time_after(jiffies, pf->fd_flush_timestamp +
  5337. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  5338. return;
  5339. /* If the flush is happening too quick and we have mostly SB rules we
  5340. * should not re-enable ATR for some time.
  5341. */
  5342. min_flush_time = pf->fd_flush_timestamp +
  5343. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  5344. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  5345. if (!(time_after(jiffies, min_flush_time)) &&
  5346. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  5347. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5348. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  5349. disable_atr = true;
  5350. }
  5351. pf->fd_flush_timestamp = jiffies;
  5352. pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
  5353. /* flush all filters */
  5354. wr32(&pf->hw, I40E_PFQF_CTL_1,
  5355. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  5356. i40e_flush(&pf->hw);
  5357. pf->fd_flush_cnt++;
  5358. pf->fd_add_err = 0;
  5359. do {
  5360. /* Check FD flush status every 5-6msec */
  5361. usleep_range(5000, 6000);
  5362. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  5363. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  5364. break;
  5365. } while (flush_wait_retry--);
  5366. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  5367. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  5368. } else {
  5369. /* replay sideband filters */
  5370. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  5371. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  5372. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5373. clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
  5374. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5375. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  5376. }
  5377. }
  5378. /**
  5379. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  5380. * @pf: board private structure
  5381. **/
  5382. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  5383. {
  5384. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  5385. }
  5386. /* We can see up to 256 filter programming desc in transit if the filters are
  5387. * being applied really fast; before we see the first
  5388. * filter miss error on Rx queue 0. Accumulating enough error messages before
  5389. * reacting will make sure we don't cause flush too often.
  5390. */
  5391. #define I40E_MAX_FD_PROGRAM_ERROR 256
  5392. /**
  5393. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  5394. * @pf: board private structure
  5395. **/
  5396. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  5397. {
  5398. /* if interface is down do nothing */
  5399. if (test_bit(__I40E_DOWN, &pf->state))
  5400. return;
  5401. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5402. i40e_fdir_flush_and_replay(pf);
  5403. i40e_fdir_check_and_reenable(pf);
  5404. }
  5405. /**
  5406. * i40e_vsi_link_event - notify VSI of a link event
  5407. * @vsi: vsi to be notified
  5408. * @link_up: link up or down
  5409. **/
  5410. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  5411. {
  5412. if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
  5413. return;
  5414. switch (vsi->type) {
  5415. case I40E_VSI_MAIN:
  5416. if (!vsi->netdev || !vsi->netdev_registered)
  5417. break;
  5418. if (link_up) {
  5419. netif_carrier_on(vsi->netdev);
  5420. netif_tx_wake_all_queues(vsi->netdev);
  5421. } else {
  5422. netif_carrier_off(vsi->netdev);
  5423. netif_tx_stop_all_queues(vsi->netdev);
  5424. }
  5425. break;
  5426. case I40E_VSI_SRIOV:
  5427. case I40E_VSI_VMDQ2:
  5428. case I40E_VSI_CTRL:
  5429. case I40E_VSI_IWARP:
  5430. case I40E_VSI_MIRROR:
  5431. default:
  5432. /* there is no notification for other VSIs */
  5433. break;
  5434. }
  5435. }
  5436. /**
  5437. * i40e_veb_link_event - notify elements on the veb of a link event
  5438. * @veb: veb to be notified
  5439. * @link_up: link up or down
  5440. **/
  5441. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  5442. {
  5443. struct i40e_pf *pf;
  5444. int i;
  5445. if (!veb || !veb->pf)
  5446. return;
  5447. pf = veb->pf;
  5448. /* depth first... */
  5449. for (i = 0; i < I40E_MAX_VEB; i++)
  5450. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  5451. i40e_veb_link_event(pf->veb[i], link_up);
  5452. /* ... now the local VSIs */
  5453. for (i = 0; i < pf->num_alloc_vsi; i++)
  5454. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  5455. i40e_vsi_link_event(pf->vsi[i], link_up);
  5456. }
  5457. /**
  5458. * i40e_link_event - Update netif_carrier status
  5459. * @pf: board private structure
  5460. **/
  5461. static void i40e_link_event(struct i40e_pf *pf)
  5462. {
  5463. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5464. u8 new_link_speed, old_link_speed;
  5465. i40e_status status;
  5466. bool new_link, old_link;
  5467. /* save off old link status information */
  5468. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  5469. /* set this to force the get_link_status call to refresh state */
  5470. pf->hw.phy.get_link_info = true;
  5471. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  5472. status = i40e_get_link_status(&pf->hw, &new_link);
  5473. /* On success, disable temp link polling */
  5474. if (status == I40E_SUCCESS) {
  5475. if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
  5476. pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
  5477. } else {
  5478. /* Enable link polling temporarily until i40e_get_link_status
  5479. * returns I40E_SUCCESS
  5480. */
  5481. pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
  5482. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  5483. status);
  5484. return;
  5485. }
  5486. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  5487. new_link_speed = pf->hw.phy.link_info.link_speed;
  5488. if (new_link == old_link &&
  5489. new_link_speed == old_link_speed &&
  5490. (test_bit(__I40E_DOWN, &vsi->state) ||
  5491. new_link == netif_carrier_ok(vsi->netdev)))
  5492. return;
  5493. if (!test_bit(__I40E_DOWN, &vsi->state))
  5494. i40e_print_link_message(vsi, new_link);
  5495. /* Notify the base of the switch tree connected to
  5496. * the link. Floating VEBs are not notified.
  5497. */
  5498. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  5499. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  5500. else
  5501. i40e_vsi_link_event(vsi, new_link);
  5502. if (pf->vf)
  5503. i40e_vc_notify_link_state(pf);
  5504. if (pf->flags & I40E_FLAG_PTP)
  5505. i40e_ptp_set_increment(pf);
  5506. }
  5507. /**
  5508. * i40e_watchdog_subtask - periodic checks not using event driven response
  5509. * @pf: board private structure
  5510. **/
  5511. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  5512. {
  5513. int i;
  5514. /* if interface is down do nothing */
  5515. if (test_bit(__I40E_DOWN, &pf->state) ||
  5516. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5517. return;
  5518. /* make sure we don't do these things too often */
  5519. if (time_before(jiffies, (pf->service_timer_previous +
  5520. pf->service_timer_period)))
  5521. return;
  5522. pf->service_timer_previous = jiffies;
  5523. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  5524. (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
  5525. i40e_link_event(pf);
  5526. /* Update the stats for active netdevs so the network stack
  5527. * can look at updated numbers whenever it cares to
  5528. */
  5529. for (i = 0; i < pf->num_alloc_vsi; i++)
  5530. if (pf->vsi[i] && pf->vsi[i]->netdev)
  5531. i40e_update_stats(pf->vsi[i]);
  5532. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  5533. /* Update the stats for the active switching components */
  5534. for (i = 0; i < I40E_MAX_VEB; i++)
  5535. if (pf->veb[i])
  5536. i40e_update_veb_stats(pf->veb[i]);
  5537. }
  5538. i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
  5539. }
  5540. /**
  5541. * i40e_reset_subtask - Set up for resetting the device and driver
  5542. * @pf: board private structure
  5543. **/
  5544. static void i40e_reset_subtask(struct i40e_pf *pf)
  5545. {
  5546. u32 reset_flags = 0;
  5547. rtnl_lock();
  5548. if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
  5549. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  5550. clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
  5551. }
  5552. if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
  5553. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  5554. clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5555. }
  5556. if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
  5557. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  5558. clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  5559. }
  5560. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
  5561. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  5562. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  5563. }
  5564. if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
  5565. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  5566. clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
  5567. }
  5568. /* If there's a recovery already waiting, it takes
  5569. * precedence before starting a new reset sequence.
  5570. */
  5571. if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
  5572. i40e_handle_reset_warning(pf);
  5573. goto unlock;
  5574. }
  5575. /* If we're already down or resetting, just bail */
  5576. if (reset_flags &&
  5577. !test_bit(__I40E_DOWN, &pf->state) &&
  5578. !test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5579. i40e_do_reset(pf, reset_flags);
  5580. unlock:
  5581. rtnl_unlock();
  5582. }
  5583. /**
  5584. * i40e_handle_link_event - Handle link event
  5585. * @pf: board private structure
  5586. * @e: event info posted on ARQ
  5587. **/
  5588. static void i40e_handle_link_event(struct i40e_pf *pf,
  5589. struct i40e_arq_event_info *e)
  5590. {
  5591. struct i40e_aqc_get_link_status *status =
  5592. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  5593. /* Do a new status request to re-enable LSE reporting
  5594. * and load new status information into the hw struct
  5595. * This completely ignores any state information
  5596. * in the ARQ event info, instead choosing to always
  5597. * issue the AQ update link status command.
  5598. */
  5599. i40e_link_event(pf);
  5600. /* check for unqualified module, if link is down */
  5601. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  5602. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  5603. (!(status->link_info & I40E_AQ_LINK_UP)))
  5604. dev_err(&pf->pdev->dev,
  5605. "The driver failed to link because an unqualified module was detected.\n");
  5606. }
  5607. /**
  5608. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  5609. * @pf: board private structure
  5610. **/
  5611. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  5612. {
  5613. struct i40e_arq_event_info event;
  5614. struct i40e_hw *hw = &pf->hw;
  5615. u16 pending, i = 0;
  5616. i40e_status ret;
  5617. u16 opcode;
  5618. u32 oldval;
  5619. u32 val;
  5620. /* Do not run clean AQ when PF reset fails */
  5621. if (test_bit(__I40E_RESET_FAILED, &pf->state))
  5622. return;
  5623. /* check for error indications */
  5624. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  5625. oldval = val;
  5626. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  5627. if (hw->debug_mask & I40E_DEBUG_AQ)
  5628. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  5629. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  5630. }
  5631. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  5632. if (hw->debug_mask & I40E_DEBUG_AQ)
  5633. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  5634. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  5635. pf->arq_overflows++;
  5636. }
  5637. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  5638. if (hw->debug_mask & I40E_DEBUG_AQ)
  5639. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  5640. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  5641. }
  5642. if (oldval != val)
  5643. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  5644. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  5645. oldval = val;
  5646. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  5647. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5648. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  5649. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  5650. }
  5651. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  5652. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5653. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  5654. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  5655. }
  5656. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  5657. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5658. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  5659. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  5660. }
  5661. if (oldval != val)
  5662. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  5663. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  5664. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  5665. if (!event.msg_buf)
  5666. return;
  5667. do {
  5668. ret = i40e_clean_arq_element(hw, &event, &pending);
  5669. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  5670. break;
  5671. else if (ret) {
  5672. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  5673. break;
  5674. }
  5675. opcode = le16_to_cpu(event.desc.opcode);
  5676. switch (opcode) {
  5677. case i40e_aqc_opc_get_link_status:
  5678. i40e_handle_link_event(pf, &event);
  5679. break;
  5680. case i40e_aqc_opc_send_msg_to_pf:
  5681. ret = i40e_vc_process_vf_msg(pf,
  5682. le16_to_cpu(event.desc.retval),
  5683. le32_to_cpu(event.desc.cookie_high),
  5684. le32_to_cpu(event.desc.cookie_low),
  5685. event.msg_buf,
  5686. event.msg_len);
  5687. break;
  5688. case i40e_aqc_opc_lldp_update_mib:
  5689. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  5690. #ifdef CONFIG_I40E_DCB
  5691. rtnl_lock();
  5692. ret = i40e_handle_lldp_event(pf, &event);
  5693. rtnl_unlock();
  5694. #endif /* CONFIG_I40E_DCB */
  5695. break;
  5696. case i40e_aqc_opc_event_lan_overflow:
  5697. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  5698. i40e_handle_lan_overflow_event(pf, &event);
  5699. break;
  5700. case i40e_aqc_opc_send_msg_to_peer:
  5701. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  5702. break;
  5703. case i40e_aqc_opc_nvm_erase:
  5704. case i40e_aqc_opc_nvm_update:
  5705. case i40e_aqc_opc_oem_post_update:
  5706. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  5707. "ARQ NVM operation 0x%04x completed\n",
  5708. opcode);
  5709. break;
  5710. default:
  5711. dev_info(&pf->pdev->dev,
  5712. "ARQ: Unknown event 0x%04x ignored\n",
  5713. opcode);
  5714. break;
  5715. }
  5716. } while (i++ < pf->adminq_work_limit);
  5717. if (i < pf->adminq_work_limit)
  5718. clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  5719. /* re-enable Admin queue interrupt cause */
  5720. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  5721. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  5722. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  5723. i40e_flush(hw);
  5724. kfree(event.msg_buf);
  5725. }
  5726. /**
  5727. * i40e_verify_eeprom - make sure eeprom is good to use
  5728. * @pf: board private structure
  5729. **/
  5730. static void i40e_verify_eeprom(struct i40e_pf *pf)
  5731. {
  5732. int err;
  5733. err = i40e_diag_eeprom_test(&pf->hw);
  5734. if (err) {
  5735. /* retry in case of garbage read */
  5736. err = i40e_diag_eeprom_test(&pf->hw);
  5737. if (err) {
  5738. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  5739. err);
  5740. set_bit(__I40E_BAD_EEPROM, &pf->state);
  5741. }
  5742. }
  5743. if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  5744. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  5745. clear_bit(__I40E_BAD_EEPROM, &pf->state);
  5746. }
  5747. }
  5748. /**
  5749. * i40e_enable_pf_switch_lb
  5750. * @pf: pointer to the PF structure
  5751. *
  5752. * enable switch loop back or die - no point in a return value
  5753. **/
  5754. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  5755. {
  5756. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5757. struct i40e_vsi_context ctxt;
  5758. int ret;
  5759. ctxt.seid = pf->main_vsi_seid;
  5760. ctxt.pf_num = pf->hw.pf_id;
  5761. ctxt.vf_num = 0;
  5762. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5763. if (ret) {
  5764. dev_info(&pf->pdev->dev,
  5765. "couldn't get PF vsi config, err %s aq_err %s\n",
  5766. i40e_stat_str(&pf->hw, ret),
  5767. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5768. return;
  5769. }
  5770. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5771. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5772. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5773. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5774. if (ret) {
  5775. dev_info(&pf->pdev->dev,
  5776. "update vsi switch failed, err %s aq_err %s\n",
  5777. i40e_stat_str(&pf->hw, ret),
  5778. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5779. }
  5780. }
  5781. /**
  5782. * i40e_disable_pf_switch_lb
  5783. * @pf: pointer to the PF structure
  5784. *
  5785. * disable switch loop back or die - no point in a return value
  5786. **/
  5787. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  5788. {
  5789. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5790. struct i40e_vsi_context ctxt;
  5791. int ret;
  5792. ctxt.seid = pf->main_vsi_seid;
  5793. ctxt.pf_num = pf->hw.pf_id;
  5794. ctxt.vf_num = 0;
  5795. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5796. if (ret) {
  5797. dev_info(&pf->pdev->dev,
  5798. "couldn't get PF vsi config, err %s aq_err %s\n",
  5799. i40e_stat_str(&pf->hw, ret),
  5800. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5801. return;
  5802. }
  5803. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5804. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5805. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5806. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5807. if (ret) {
  5808. dev_info(&pf->pdev->dev,
  5809. "update vsi switch failed, err %s aq_err %s\n",
  5810. i40e_stat_str(&pf->hw, ret),
  5811. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5812. }
  5813. }
  5814. /**
  5815. * i40e_config_bridge_mode - Configure the HW bridge mode
  5816. * @veb: pointer to the bridge instance
  5817. *
  5818. * Configure the loop back mode for the LAN VSI that is downlink to the
  5819. * specified HW bridge instance. It is expected this function is called
  5820. * when a new HW bridge is instantiated.
  5821. **/
  5822. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  5823. {
  5824. struct i40e_pf *pf = veb->pf;
  5825. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  5826. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  5827. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  5828. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  5829. i40e_disable_pf_switch_lb(pf);
  5830. else
  5831. i40e_enable_pf_switch_lb(pf);
  5832. }
  5833. /**
  5834. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  5835. * @veb: pointer to the VEB instance
  5836. *
  5837. * This is a recursive function that first builds the attached VSIs then
  5838. * recurses in to build the next layer of VEB. We track the connections
  5839. * through our own index numbers because the seid's from the HW could
  5840. * change across the reset.
  5841. **/
  5842. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  5843. {
  5844. struct i40e_vsi *ctl_vsi = NULL;
  5845. struct i40e_pf *pf = veb->pf;
  5846. int v, veb_idx;
  5847. int ret;
  5848. /* build VSI that owns this VEB, temporarily attached to base VEB */
  5849. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  5850. if (pf->vsi[v] &&
  5851. pf->vsi[v]->veb_idx == veb->idx &&
  5852. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  5853. ctl_vsi = pf->vsi[v];
  5854. break;
  5855. }
  5856. }
  5857. if (!ctl_vsi) {
  5858. dev_info(&pf->pdev->dev,
  5859. "missing owner VSI for veb_idx %d\n", veb->idx);
  5860. ret = -ENOENT;
  5861. goto end_reconstitute;
  5862. }
  5863. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  5864. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5865. ret = i40e_add_vsi(ctl_vsi);
  5866. if (ret) {
  5867. dev_info(&pf->pdev->dev,
  5868. "rebuild of veb_idx %d owner VSI failed: %d\n",
  5869. veb->idx, ret);
  5870. goto end_reconstitute;
  5871. }
  5872. i40e_vsi_reset_stats(ctl_vsi);
  5873. /* create the VEB in the switch and move the VSI onto the VEB */
  5874. ret = i40e_add_veb(veb, ctl_vsi);
  5875. if (ret)
  5876. goto end_reconstitute;
  5877. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  5878. veb->bridge_mode = BRIDGE_MODE_VEB;
  5879. else
  5880. veb->bridge_mode = BRIDGE_MODE_VEPA;
  5881. i40e_config_bridge_mode(veb);
  5882. /* create the remaining VSIs attached to this VEB */
  5883. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5884. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  5885. continue;
  5886. if (pf->vsi[v]->veb_idx == veb->idx) {
  5887. struct i40e_vsi *vsi = pf->vsi[v];
  5888. vsi->uplink_seid = veb->seid;
  5889. ret = i40e_add_vsi(vsi);
  5890. if (ret) {
  5891. dev_info(&pf->pdev->dev,
  5892. "rebuild of vsi_idx %d failed: %d\n",
  5893. v, ret);
  5894. goto end_reconstitute;
  5895. }
  5896. i40e_vsi_reset_stats(vsi);
  5897. }
  5898. }
  5899. /* create any VEBs attached to this VEB - RECURSION */
  5900. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  5901. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  5902. pf->veb[veb_idx]->uplink_seid = veb->seid;
  5903. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  5904. if (ret)
  5905. break;
  5906. }
  5907. }
  5908. end_reconstitute:
  5909. return ret;
  5910. }
  5911. /**
  5912. * i40e_get_capabilities - get info about the HW
  5913. * @pf: the PF struct
  5914. **/
  5915. static int i40e_get_capabilities(struct i40e_pf *pf)
  5916. {
  5917. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  5918. u16 data_size;
  5919. int buf_len;
  5920. int err;
  5921. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  5922. do {
  5923. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  5924. if (!cap_buf)
  5925. return -ENOMEM;
  5926. /* this loads the data into the hw struct for us */
  5927. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  5928. &data_size,
  5929. i40e_aqc_opc_list_func_capabilities,
  5930. NULL);
  5931. /* data loaded, buffer no longer needed */
  5932. kfree(cap_buf);
  5933. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  5934. /* retry with a larger buffer */
  5935. buf_len = data_size;
  5936. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  5937. dev_info(&pf->pdev->dev,
  5938. "capability discovery failed, err %s aq_err %s\n",
  5939. i40e_stat_str(&pf->hw, err),
  5940. i40e_aq_str(&pf->hw,
  5941. pf->hw.aq.asq_last_status));
  5942. return -ENODEV;
  5943. }
  5944. } while (err);
  5945. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  5946. dev_info(&pf->pdev->dev,
  5947. "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",
  5948. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  5949. pf->hw.func_caps.num_msix_vectors,
  5950. pf->hw.func_caps.num_msix_vectors_vf,
  5951. pf->hw.func_caps.fd_filters_guaranteed,
  5952. pf->hw.func_caps.fd_filters_best_effort,
  5953. pf->hw.func_caps.num_tx_qp,
  5954. pf->hw.func_caps.num_vsis);
  5955. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  5956. + pf->hw.func_caps.num_vfs)
  5957. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  5958. dev_info(&pf->pdev->dev,
  5959. "got num_vsis %d, setting num_vsis to %d\n",
  5960. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  5961. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  5962. }
  5963. return 0;
  5964. }
  5965. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  5966. /**
  5967. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  5968. * @pf: board private structure
  5969. **/
  5970. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  5971. {
  5972. struct i40e_vsi *vsi;
  5973. /* quick workaround for an NVM issue that leaves a critical register
  5974. * uninitialized
  5975. */
  5976. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  5977. static const u32 hkey[] = {
  5978. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  5979. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  5980. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  5981. 0x95b3a76d};
  5982. int i;
  5983. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  5984. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  5985. }
  5986. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  5987. return;
  5988. /* find existing VSI and see if it needs configuring */
  5989. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  5990. /* create a new VSI if none exists */
  5991. if (!vsi) {
  5992. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  5993. pf->vsi[pf->lan_vsi]->seid, 0);
  5994. if (!vsi) {
  5995. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  5996. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5997. return;
  5998. }
  5999. }
  6000. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  6001. }
  6002. /**
  6003. * i40e_fdir_teardown - release the Flow Director resources
  6004. * @pf: board private structure
  6005. **/
  6006. static void i40e_fdir_teardown(struct i40e_pf *pf)
  6007. {
  6008. struct i40e_vsi *vsi;
  6009. i40e_fdir_filter_exit(pf);
  6010. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  6011. if (vsi)
  6012. i40e_vsi_release(vsi);
  6013. }
  6014. /**
  6015. * i40e_prep_for_reset - prep for the core to reset
  6016. * @pf: board private structure
  6017. *
  6018. * Close up the VFs and other things in prep for PF Reset.
  6019. **/
  6020. static void i40e_prep_for_reset(struct i40e_pf *pf)
  6021. {
  6022. struct i40e_hw *hw = &pf->hw;
  6023. i40e_status ret = 0;
  6024. u32 v;
  6025. clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  6026. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  6027. return;
  6028. if (i40e_check_asq_alive(&pf->hw))
  6029. i40e_vc_notify_reset(pf);
  6030. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  6031. /* quiesce the VSIs and their queues that are not already DOWN */
  6032. i40e_pf_quiesce_all_vsi(pf);
  6033. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6034. if (pf->vsi[v])
  6035. pf->vsi[v]->seid = 0;
  6036. }
  6037. i40e_shutdown_adminq(&pf->hw);
  6038. /* call shutdown HMC */
  6039. if (hw->hmc.hmc_obj) {
  6040. ret = i40e_shutdown_lan_hmc(hw);
  6041. if (ret)
  6042. dev_warn(&pf->pdev->dev,
  6043. "shutdown_lan_hmc failed: %d\n", ret);
  6044. }
  6045. }
  6046. /**
  6047. * i40e_send_version - update firmware with driver version
  6048. * @pf: PF struct
  6049. */
  6050. static void i40e_send_version(struct i40e_pf *pf)
  6051. {
  6052. struct i40e_driver_version dv;
  6053. dv.major_version = DRV_VERSION_MAJOR;
  6054. dv.minor_version = DRV_VERSION_MINOR;
  6055. dv.build_version = DRV_VERSION_BUILD;
  6056. dv.subbuild_version = 0;
  6057. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  6058. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  6059. }
  6060. /**
  6061. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  6062. * @pf: board private structure
  6063. * @reinit: if the Main VSI needs to re-initialized.
  6064. **/
  6065. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
  6066. {
  6067. struct i40e_hw *hw = &pf->hw;
  6068. u8 set_fc_aq_fail = 0;
  6069. i40e_status ret;
  6070. u32 val;
  6071. u32 v;
  6072. /* Now we wait for GRST to settle out.
  6073. * We don't have to delete the VEBs or VSIs from the hw switch
  6074. * because the reset will make them disappear.
  6075. */
  6076. ret = i40e_pf_reset(hw);
  6077. if (ret) {
  6078. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  6079. set_bit(__I40E_RESET_FAILED, &pf->state);
  6080. goto clear_recovery;
  6081. }
  6082. pf->pfr_count++;
  6083. if (test_bit(__I40E_DOWN, &pf->state))
  6084. goto clear_recovery;
  6085. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  6086. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  6087. ret = i40e_init_adminq(&pf->hw);
  6088. if (ret) {
  6089. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  6090. i40e_stat_str(&pf->hw, ret),
  6091. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6092. goto clear_recovery;
  6093. }
  6094. /* re-verify the eeprom if we just had an EMP reset */
  6095. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
  6096. i40e_verify_eeprom(pf);
  6097. i40e_clear_pxe_mode(hw);
  6098. ret = i40e_get_capabilities(pf);
  6099. if (ret)
  6100. goto end_core_reset;
  6101. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  6102. hw->func_caps.num_rx_qp, 0, 0);
  6103. if (ret) {
  6104. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  6105. goto end_core_reset;
  6106. }
  6107. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  6108. if (ret) {
  6109. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  6110. goto end_core_reset;
  6111. }
  6112. #ifdef CONFIG_I40E_DCB
  6113. ret = i40e_init_pf_dcb(pf);
  6114. if (ret) {
  6115. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  6116. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  6117. /* Continue without DCB enabled */
  6118. }
  6119. #endif /* CONFIG_I40E_DCB */
  6120. /* do basic switch setup */
  6121. ret = i40e_setup_pf_switch(pf, reinit);
  6122. if (ret)
  6123. goto end_core_reset;
  6124. /* The driver only wants link up/down and module qualification
  6125. * reports from firmware. Note the negative logic.
  6126. */
  6127. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  6128. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  6129. I40E_AQ_EVENT_MEDIA_NA |
  6130. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  6131. if (ret)
  6132. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  6133. i40e_stat_str(&pf->hw, ret),
  6134. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6135. /* make sure our flow control settings are restored */
  6136. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  6137. if (ret)
  6138. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  6139. i40e_stat_str(&pf->hw, ret),
  6140. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6141. /* Rebuild the VSIs and VEBs that existed before reset.
  6142. * They are still in our local switch element arrays, so only
  6143. * need to rebuild the switch model in the HW.
  6144. *
  6145. * If there were VEBs but the reconstitution failed, we'll try
  6146. * try to recover minimal use by getting the basic PF VSI working.
  6147. */
  6148. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  6149. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  6150. /* find the one VEB connected to the MAC, and find orphans */
  6151. for (v = 0; v < I40E_MAX_VEB; v++) {
  6152. if (!pf->veb[v])
  6153. continue;
  6154. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  6155. pf->veb[v]->uplink_seid == 0) {
  6156. ret = i40e_reconstitute_veb(pf->veb[v]);
  6157. if (!ret)
  6158. continue;
  6159. /* If Main VEB failed, we're in deep doodoo,
  6160. * so give up rebuilding the switch and set up
  6161. * for minimal rebuild of PF VSI.
  6162. * If orphan failed, we'll report the error
  6163. * but try to keep going.
  6164. */
  6165. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  6166. dev_info(&pf->pdev->dev,
  6167. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  6168. ret);
  6169. pf->vsi[pf->lan_vsi]->uplink_seid
  6170. = pf->mac_seid;
  6171. break;
  6172. } else if (pf->veb[v]->uplink_seid == 0) {
  6173. dev_info(&pf->pdev->dev,
  6174. "rebuild of orphan VEB failed: %d\n",
  6175. ret);
  6176. }
  6177. }
  6178. }
  6179. }
  6180. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  6181. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  6182. /* no VEB, so rebuild only the Main VSI */
  6183. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  6184. if (ret) {
  6185. dev_info(&pf->pdev->dev,
  6186. "rebuild of Main VSI failed: %d\n", ret);
  6187. goto end_core_reset;
  6188. }
  6189. }
  6190. /* Reconfigure hardware for allowing smaller MSS in the case
  6191. * of TSO, so that we avoid the MDD being fired and causing
  6192. * a reset in the case of small MSS+TSO.
  6193. */
  6194. #define I40E_REG_MSS 0x000E64DC
  6195. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  6196. #define I40E_64BYTE_MSS 0x400000
  6197. val = rd32(hw, I40E_REG_MSS);
  6198. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  6199. val &= ~I40E_REG_MSS_MIN_MASK;
  6200. val |= I40E_64BYTE_MSS;
  6201. wr32(hw, I40E_REG_MSS, val);
  6202. }
  6203. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  6204. msleep(75);
  6205. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  6206. if (ret)
  6207. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  6208. i40e_stat_str(&pf->hw, ret),
  6209. i40e_aq_str(&pf->hw,
  6210. pf->hw.aq.asq_last_status));
  6211. }
  6212. /* reinit the misc interrupt */
  6213. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6214. ret = i40e_setup_misc_vector(pf);
  6215. /* Add a filter to drop all Flow control frames from any VSI from being
  6216. * transmitted. By doing so we stop a malicious VF from sending out
  6217. * PAUSE or PFC frames and potentially controlling traffic for other
  6218. * PF/VF VSIs.
  6219. * The FW can still send Flow control frames if enabled.
  6220. */
  6221. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  6222. pf->main_vsi_seid);
  6223. /* restart the VSIs that were rebuilt and running before the reset */
  6224. i40e_pf_unquiesce_all_vsi(pf);
  6225. if (pf->num_alloc_vfs) {
  6226. for (v = 0; v < pf->num_alloc_vfs; v++)
  6227. i40e_reset_vf(&pf->vf[v], true);
  6228. }
  6229. /* tell the firmware that we're starting */
  6230. i40e_send_version(pf);
  6231. end_core_reset:
  6232. clear_bit(__I40E_RESET_FAILED, &pf->state);
  6233. clear_recovery:
  6234. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  6235. }
  6236. /**
  6237. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  6238. * @pf: board private structure
  6239. *
  6240. * Close up the VFs and other things in prep for a Core Reset,
  6241. * then get ready to rebuild the world.
  6242. **/
  6243. static void i40e_handle_reset_warning(struct i40e_pf *pf)
  6244. {
  6245. i40e_prep_for_reset(pf);
  6246. i40e_reset_and_rebuild(pf, false);
  6247. }
  6248. /**
  6249. * i40e_handle_mdd_event
  6250. * @pf: pointer to the PF structure
  6251. *
  6252. * Called from the MDD irq handler to identify possibly malicious vfs
  6253. **/
  6254. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  6255. {
  6256. struct i40e_hw *hw = &pf->hw;
  6257. bool mdd_detected = false;
  6258. bool pf_mdd_detected = false;
  6259. struct i40e_vf *vf;
  6260. u32 reg;
  6261. int i;
  6262. if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
  6263. return;
  6264. /* find what triggered the MDD event */
  6265. reg = rd32(hw, I40E_GL_MDET_TX);
  6266. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  6267. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  6268. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  6269. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  6270. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  6271. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  6272. I40E_GL_MDET_TX_EVENT_SHIFT;
  6273. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  6274. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  6275. pf->hw.func_caps.base_queue;
  6276. if (netif_msg_tx_err(pf))
  6277. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  6278. event, queue, pf_num, vf_num);
  6279. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  6280. mdd_detected = true;
  6281. }
  6282. reg = rd32(hw, I40E_GL_MDET_RX);
  6283. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  6284. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  6285. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  6286. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  6287. I40E_GL_MDET_RX_EVENT_SHIFT;
  6288. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  6289. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  6290. pf->hw.func_caps.base_queue;
  6291. if (netif_msg_rx_err(pf))
  6292. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  6293. event, queue, func);
  6294. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  6295. mdd_detected = true;
  6296. }
  6297. if (mdd_detected) {
  6298. reg = rd32(hw, I40E_PF_MDET_TX);
  6299. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  6300. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  6301. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  6302. pf_mdd_detected = true;
  6303. }
  6304. reg = rd32(hw, I40E_PF_MDET_RX);
  6305. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  6306. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  6307. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  6308. pf_mdd_detected = true;
  6309. }
  6310. /* Queue belongs to the PF, initiate a reset */
  6311. if (pf_mdd_detected) {
  6312. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  6313. i40e_service_event_schedule(pf);
  6314. }
  6315. }
  6316. /* see if one of the VFs needs its hand slapped */
  6317. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  6318. vf = &(pf->vf[i]);
  6319. reg = rd32(hw, I40E_VP_MDET_TX(i));
  6320. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  6321. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  6322. vf->num_mdd_events++;
  6323. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  6324. i);
  6325. }
  6326. reg = rd32(hw, I40E_VP_MDET_RX(i));
  6327. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  6328. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  6329. vf->num_mdd_events++;
  6330. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  6331. i);
  6332. }
  6333. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  6334. dev_info(&pf->pdev->dev,
  6335. "Too many MDD events on VF %d, disabled\n", i);
  6336. dev_info(&pf->pdev->dev,
  6337. "Use PF Control I/F to re-enable the VF\n");
  6338. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  6339. }
  6340. }
  6341. /* re-enable mdd interrupt cause */
  6342. clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  6343. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  6344. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  6345. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  6346. i40e_flush(hw);
  6347. }
  6348. /**
  6349. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  6350. * @pf: board private structure
  6351. **/
  6352. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  6353. {
  6354. struct i40e_hw *hw = &pf->hw;
  6355. i40e_status ret;
  6356. u16 port;
  6357. int i;
  6358. if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
  6359. return;
  6360. pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
  6361. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6362. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  6363. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  6364. port = pf->udp_ports[i].index;
  6365. if (port)
  6366. ret = i40e_aq_add_udp_tunnel(hw, port,
  6367. pf->udp_ports[i].type,
  6368. NULL, NULL);
  6369. else
  6370. ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
  6371. if (ret) {
  6372. dev_dbg(&pf->pdev->dev,
  6373. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  6374. pf->udp_ports[i].type ? "vxlan" : "geneve",
  6375. port ? "add" : "delete",
  6376. port, i,
  6377. i40e_stat_str(&pf->hw, ret),
  6378. i40e_aq_str(&pf->hw,
  6379. pf->hw.aq.asq_last_status));
  6380. pf->udp_ports[i].index = 0;
  6381. }
  6382. }
  6383. }
  6384. }
  6385. /**
  6386. * i40e_service_task - Run the driver's async subtasks
  6387. * @work: pointer to work_struct containing our data
  6388. **/
  6389. static void i40e_service_task(struct work_struct *work)
  6390. {
  6391. struct i40e_pf *pf = container_of(work,
  6392. struct i40e_pf,
  6393. service_task);
  6394. unsigned long start_time = jiffies;
  6395. /* don't bother with service tasks if a reset is in progress */
  6396. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  6397. return;
  6398. }
  6399. if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
  6400. return;
  6401. i40e_detect_recover_hung(pf);
  6402. i40e_sync_filters_subtask(pf);
  6403. i40e_reset_subtask(pf);
  6404. i40e_handle_mdd_event(pf);
  6405. i40e_vc_process_vflr_event(pf);
  6406. i40e_watchdog_subtask(pf);
  6407. i40e_fdir_reinit_subtask(pf);
  6408. if (pf->flags & I40E_FLAG_CLIENT_RESET) {
  6409. /* Client subtask will reopen next time through. */
  6410. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  6411. pf->flags &= ~I40E_FLAG_CLIENT_RESET;
  6412. } else {
  6413. i40e_client_subtask(pf);
  6414. if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
  6415. i40e_notify_client_of_l2_param_changes(
  6416. pf->vsi[pf->lan_vsi]);
  6417. pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
  6418. }
  6419. }
  6420. i40e_sync_filters_subtask(pf);
  6421. i40e_sync_udp_filters_subtask(pf);
  6422. i40e_clean_adminq_subtask(pf);
  6423. /* flush memory to make sure state is correct before next watchdog */
  6424. smp_mb__before_atomic();
  6425. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  6426. /* If the tasks have taken longer than one timer cycle or there
  6427. * is more work to be done, reschedule the service task now
  6428. * rather than wait for the timer to tick again.
  6429. */
  6430. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  6431. test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
  6432. test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
  6433. test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  6434. i40e_service_event_schedule(pf);
  6435. }
  6436. /**
  6437. * i40e_service_timer - timer callback
  6438. * @data: pointer to PF struct
  6439. **/
  6440. static void i40e_service_timer(unsigned long data)
  6441. {
  6442. struct i40e_pf *pf = (struct i40e_pf *)data;
  6443. mod_timer(&pf->service_timer,
  6444. round_jiffies(jiffies + pf->service_timer_period));
  6445. i40e_service_event_schedule(pf);
  6446. }
  6447. /**
  6448. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  6449. * @vsi: the VSI being configured
  6450. **/
  6451. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  6452. {
  6453. struct i40e_pf *pf = vsi->back;
  6454. switch (vsi->type) {
  6455. case I40E_VSI_MAIN:
  6456. vsi->alloc_queue_pairs = pf->num_lan_qps;
  6457. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6458. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6459. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6460. vsi->num_q_vectors = pf->num_lan_msix;
  6461. else
  6462. vsi->num_q_vectors = 1;
  6463. break;
  6464. case I40E_VSI_FDIR:
  6465. vsi->alloc_queue_pairs = 1;
  6466. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  6467. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6468. vsi->num_q_vectors = pf->num_fdsb_msix;
  6469. break;
  6470. case I40E_VSI_VMDQ2:
  6471. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  6472. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6473. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6474. vsi->num_q_vectors = pf->num_vmdq_msix;
  6475. break;
  6476. case I40E_VSI_SRIOV:
  6477. vsi->alloc_queue_pairs = pf->num_vf_qps;
  6478. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6479. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6480. break;
  6481. default:
  6482. WARN_ON(1);
  6483. return -ENODATA;
  6484. }
  6485. return 0;
  6486. }
  6487. /**
  6488. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  6489. * @type: VSI pointer
  6490. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  6491. *
  6492. * On error: returns error code (negative)
  6493. * On success: returns 0
  6494. **/
  6495. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  6496. {
  6497. int size;
  6498. int ret = 0;
  6499. /* allocate memory for both Tx and Rx ring pointers */
  6500. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
  6501. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  6502. if (!vsi->tx_rings)
  6503. return -ENOMEM;
  6504. vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
  6505. if (alloc_qvectors) {
  6506. /* allocate memory for q_vector pointers */
  6507. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  6508. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  6509. if (!vsi->q_vectors) {
  6510. ret = -ENOMEM;
  6511. goto err_vectors;
  6512. }
  6513. }
  6514. return ret;
  6515. err_vectors:
  6516. kfree(vsi->tx_rings);
  6517. return ret;
  6518. }
  6519. /**
  6520. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  6521. * @pf: board private structure
  6522. * @type: type of VSI
  6523. *
  6524. * On error: returns error code (negative)
  6525. * On success: returns vsi index in PF (positive)
  6526. **/
  6527. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  6528. {
  6529. int ret = -ENODEV;
  6530. struct i40e_vsi *vsi;
  6531. int vsi_idx;
  6532. int i;
  6533. /* Need to protect the allocation of the VSIs at the PF level */
  6534. mutex_lock(&pf->switch_mutex);
  6535. /* VSI list may be fragmented if VSI creation/destruction has
  6536. * been happening. We can afford to do a quick scan to look
  6537. * for any free VSIs in the list.
  6538. *
  6539. * find next empty vsi slot, looping back around if necessary
  6540. */
  6541. i = pf->next_vsi;
  6542. while (i < pf->num_alloc_vsi && pf->vsi[i])
  6543. i++;
  6544. if (i >= pf->num_alloc_vsi) {
  6545. i = 0;
  6546. while (i < pf->next_vsi && pf->vsi[i])
  6547. i++;
  6548. }
  6549. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  6550. vsi_idx = i; /* Found one! */
  6551. } else {
  6552. ret = -ENODEV;
  6553. goto unlock_pf; /* out of VSI slots! */
  6554. }
  6555. pf->next_vsi = ++i;
  6556. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  6557. if (!vsi) {
  6558. ret = -ENOMEM;
  6559. goto unlock_pf;
  6560. }
  6561. vsi->type = type;
  6562. vsi->back = pf;
  6563. set_bit(__I40E_DOWN, &vsi->state);
  6564. vsi->flags = 0;
  6565. vsi->idx = vsi_idx;
  6566. vsi->int_rate_limit = 0;
  6567. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  6568. pf->rss_table_size : 64;
  6569. vsi->netdev_registered = false;
  6570. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  6571. hash_init(vsi->mac_filter_hash);
  6572. vsi->irqs_ready = false;
  6573. ret = i40e_set_num_rings_in_vsi(vsi);
  6574. if (ret)
  6575. goto err_rings;
  6576. ret = i40e_vsi_alloc_arrays(vsi, true);
  6577. if (ret)
  6578. goto err_rings;
  6579. /* Setup default MSIX irq handler for VSI */
  6580. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  6581. /* Initialize VSI lock */
  6582. spin_lock_init(&vsi->mac_filter_hash_lock);
  6583. pf->vsi[vsi_idx] = vsi;
  6584. ret = vsi_idx;
  6585. goto unlock_pf;
  6586. err_rings:
  6587. pf->next_vsi = i - 1;
  6588. kfree(vsi);
  6589. unlock_pf:
  6590. mutex_unlock(&pf->switch_mutex);
  6591. return ret;
  6592. }
  6593. /**
  6594. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  6595. * @type: VSI pointer
  6596. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  6597. *
  6598. * On error: returns error code (negative)
  6599. * On success: returns 0
  6600. **/
  6601. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  6602. {
  6603. /* free the ring and vector containers */
  6604. if (free_qvectors) {
  6605. kfree(vsi->q_vectors);
  6606. vsi->q_vectors = NULL;
  6607. }
  6608. kfree(vsi->tx_rings);
  6609. vsi->tx_rings = NULL;
  6610. vsi->rx_rings = NULL;
  6611. }
  6612. /**
  6613. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  6614. * and lookup table
  6615. * @vsi: Pointer to VSI structure
  6616. */
  6617. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  6618. {
  6619. if (!vsi)
  6620. return;
  6621. kfree(vsi->rss_hkey_user);
  6622. vsi->rss_hkey_user = NULL;
  6623. kfree(vsi->rss_lut_user);
  6624. vsi->rss_lut_user = NULL;
  6625. }
  6626. /**
  6627. * i40e_vsi_clear - Deallocate the VSI provided
  6628. * @vsi: the VSI being un-configured
  6629. **/
  6630. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  6631. {
  6632. struct i40e_pf *pf;
  6633. if (!vsi)
  6634. return 0;
  6635. if (!vsi->back)
  6636. goto free_vsi;
  6637. pf = vsi->back;
  6638. mutex_lock(&pf->switch_mutex);
  6639. if (!pf->vsi[vsi->idx]) {
  6640. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  6641. vsi->idx, vsi->idx, vsi, vsi->type);
  6642. goto unlock_vsi;
  6643. }
  6644. if (pf->vsi[vsi->idx] != vsi) {
  6645. dev_err(&pf->pdev->dev,
  6646. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  6647. pf->vsi[vsi->idx]->idx,
  6648. pf->vsi[vsi->idx],
  6649. pf->vsi[vsi->idx]->type,
  6650. vsi->idx, vsi, vsi->type);
  6651. goto unlock_vsi;
  6652. }
  6653. /* updates the PF for this cleared vsi */
  6654. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  6655. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  6656. i40e_vsi_free_arrays(vsi, true);
  6657. i40e_clear_rss_config_user(vsi);
  6658. pf->vsi[vsi->idx] = NULL;
  6659. if (vsi->idx < pf->next_vsi)
  6660. pf->next_vsi = vsi->idx;
  6661. unlock_vsi:
  6662. mutex_unlock(&pf->switch_mutex);
  6663. free_vsi:
  6664. kfree(vsi);
  6665. return 0;
  6666. }
  6667. /**
  6668. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  6669. * @vsi: the VSI being cleaned
  6670. **/
  6671. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  6672. {
  6673. int i;
  6674. if (vsi->tx_rings && vsi->tx_rings[0]) {
  6675. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6676. kfree_rcu(vsi->tx_rings[i], rcu);
  6677. vsi->tx_rings[i] = NULL;
  6678. vsi->rx_rings[i] = NULL;
  6679. }
  6680. }
  6681. }
  6682. /**
  6683. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  6684. * @vsi: the VSI being configured
  6685. **/
  6686. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  6687. {
  6688. struct i40e_ring *tx_ring, *rx_ring;
  6689. struct i40e_pf *pf = vsi->back;
  6690. int i;
  6691. /* Set basic values in the rings to be used later during open() */
  6692. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6693. /* allocate space for both Tx and Rx in one shot */
  6694. tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
  6695. if (!tx_ring)
  6696. goto err_out;
  6697. tx_ring->queue_index = i;
  6698. tx_ring->reg_idx = vsi->base_queue + i;
  6699. tx_ring->ring_active = false;
  6700. tx_ring->vsi = vsi;
  6701. tx_ring->netdev = vsi->netdev;
  6702. tx_ring->dev = &pf->pdev->dev;
  6703. tx_ring->count = vsi->num_desc;
  6704. tx_ring->size = 0;
  6705. tx_ring->dcb_tc = 0;
  6706. if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
  6707. tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  6708. tx_ring->tx_itr_setting = pf->tx_itr_default;
  6709. vsi->tx_rings[i] = tx_ring;
  6710. rx_ring = &tx_ring[1];
  6711. rx_ring->queue_index = i;
  6712. rx_ring->reg_idx = vsi->base_queue + i;
  6713. rx_ring->ring_active = false;
  6714. rx_ring->vsi = vsi;
  6715. rx_ring->netdev = vsi->netdev;
  6716. rx_ring->dev = &pf->pdev->dev;
  6717. rx_ring->count = vsi->num_desc;
  6718. rx_ring->size = 0;
  6719. rx_ring->dcb_tc = 0;
  6720. rx_ring->rx_itr_setting = pf->rx_itr_default;
  6721. vsi->rx_rings[i] = rx_ring;
  6722. }
  6723. return 0;
  6724. err_out:
  6725. i40e_vsi_clear_rings(vsi);
  6726. return -ENOMEM;
  6727. }
  6728. /**
  6729. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  6730. * @pf: board private structure
  6731. * @vectors: the number of MSI-X vectors to request
  6732. *
  6733. * Returns the number of vectors reserved, or error
  6734. **/
  6735. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  6736. {
  6737. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  6738. I40E_MIN_MSIX, vectors);
  6739. if (vectors < 0) {
  6740. dev_info(&pf->pdev->dev,
  6741. "MSI-X vector reservation failed: %d\n", vectors);
  6742. vectors = 0;
  6743. }
  6744. return vectors;
  6745. }
  6746. /**
  6747. * i40e_init_msix - Setup the MSIX capability
  6748. * @pf: board private structure
  6749. *
  6750. * Work with the OS to set up the MSIX vectors needed.
  6751. *
  6752. * Returns the number of vectors reserved or negative on failure
  6753. **/
  6754. static int i40e_init_msix(struct i40e_pf *pf)
  6755. {
  6756. struct i40e_hw *hw = &pf->hw;
  6757. int cpus, extra_vectors;
  6758. int vectors_left;
  6759. int v_budget, i;
  6760. int v_actual;
  6761. int iwarp_requested = 0;
  6762. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  6763. return -ENODEV;
  6764. /* The number of vectors we'll request will be comprised of:
  6765. * - Add 1 for "other" cause for Admin Queue events, etc.
  6766. * - The number of LAN queue pairs
  6767. * - Queues being used for RSS.
  6768. * We don't need as many as max_rss_size vectors.
  6769. * use rss_size instead in the calculation since that
  6770. * is governed by number of cpus in the system.
  6771. * - assumes symmetric Tx/Rx pairing
  6772. * - The number of VMDq pairs
  6773. * - The CPU count within the NUMA node if iWARP is enabled
  6774. * Once we count this up, try the request.
  6775. *
  6776. * If we can't get what we want, we'll simplify to nearly nothing
  6777. * and try again. If that still fails, we punt.
  6778. */
  6779. vectors_left = hw->func_caps.num_msix_vectors;
  6780. v_budget = 0;
  6781. /* reserve one vector for miscellaneous handler */
  6782. if (vectors_left) {
  6783. v_budget++;
  6784. vectors_left--;
  6785. }
  6786. /* reserve some vectors for the main PF traffic queues. Initially we
  6787. * only reserve at most 50% of the available vectors, in the case that
  6788. * the number of online CPUs is large. This ensures that we can enable
  6789. * extra features as well. Once we've enabled the other features, we
  6790. * will use any remaining vectors to reach as close as we can to the
  6791. * number of online CPUs.
  6792. */
  6793. cpus = num_online_cpus();
  6794. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  6795. vectors_left -= pf->num_lan_msix;
  6796. /* reserve one vector for sideband flow director */
  6797. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6798. if (vectors_left) {
  6799. pf->num_fdsb_msix = 1;
  6800. v_budget++;
  6801. vectors_left--;
  6802. } else {
  6803. pf->num_fdsb_msix = 0;
  6804. }
  6805. }
  6806. /* can we reserve enough for iWARP? */
  6807. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6808. iwarp_requested = pf->num_iwarp_msix;
  6809. if (!vectors_left)
  6810. pf->num_iwarp_msix = 0;
  6811. else if (vectors_left < pf->num_iwarp_msix)
  6812. pf->num_iwarp_msix = 1;
  6813. v_budget += pf->num_iwarp_msix;
  6814. vectors_left -= pf->num_iwarp_msix;
  6815. }
  6816. /* any vectors left over go for VMDq support */
  6817. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  6818. int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
  6819. int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
  6820. if (!vectors_left) {
  6821. pf->num_vmdq_msix = 0;
  6822. pf->num_vmdq_qps = 0;
  6823. } else {
  6824. /* if we're short on vectors for what's desired, we limit
  6825. * the queues per vmdq. If this is still more than are
  6826. * available, the user will need to change the number of
  6827. * queues/vectors used by the PF later with the ethtool
  6828. * channels command
  6829. */
  6830. if (vmdq_vecs < vmdq_vecs_wanted)
  6831. pf->num_vmdq_qps = 1;
  6832. pf->num_vmdq_msix = pf->num_vmdq_qps;
  6833. v_budget += vmdq_vecs;
  6834. vectors_left -= vmdq_vecs;
  6835. }
  6836. }
  6837. /* On systems with a large number of SMP cores, we previously limited
  6838. * the number of vectors for num_lan_msix to be at most 50% of the
  6839. * available vectors, to allow for other features. Now, we add back
  6840. * the remaining vectors. However, we ensure that the total
  6841. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  6842. * calculate the number of vectors we can add without going over the
  6843. * cap of CPUs. For systems with a small number of CPUs this will be
  6844. * zero.
  6845. */
  6846. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  6847. pf->num_lan_msix += extra_vectors;
  6848. vectors_left -= extra_vectors;
  6849. WARN(vectors_left < 0,
  6850. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  6851. v_budget += pf->num_lan_msix;
  6852. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  6853. GFP_KERNEL);
  6854. if (!pf->msix_entries)
  6855. return -ENOMEM;
  6856. for (i = 0; i < v_budget; i++)
  6857. pf->msix_entries[i].entry = i;
  6858. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  6859. if (v_actual < I40E_MIN_MSIX) {
  6860. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  6861. kfree(pf->msix_entries);
  6862. pf->msix_entries = NULL;
  6863. pci_disable_msix(pf->pdev);
  6864. return -ENODEV;
  6865. } else if (v_actual == I40E_MIN_MSIX) {
  6866. /* Adjust for minimal MSIX use */
  6867. pf->num_vmdq_vsis = 0;
  6868. pf->num_vmdq_qps = 0;
  6869. pf->num_lan_qps = 1;
  6870. pf->num_lan_msix = 1;
  6871. } else if (!vectors_left) {
  6872. /* If we have limited resources, we will start with no vectors
  6873. * for the special features and then allocate vectors to some
  6874. * of these features based on the policy and at the end disable
  6875. * the features that did not get any vectors.
  6876. */
  6877. int vec;
  6878. dev_info(&pf->pdev->dev,
  6879. "MSI-X vector limit reached, attempting to redistribute vectors\n");
  6880. /* reserve the misc vector */
  6881. vec = v_actual - 1;
  6882. /* Scale vector usage down */
  6883. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  6884. pf->num_vmdq_vsis = 1;
  6885. pf->num_vmdq_qps = 1;
  6886. /* partition out the remaining vectors */
  6887. switch (vec) {
  6888. case 2:
  6889. pf->num_lan_msix = 1;
  6890. break;
  6891. case 3:
  6892. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6893. pf->num_lan_msix = 1;
  6894. pf->num_iwarp_msix = 1;
  6895. } else {
  6896. pf->num_lan_msix = 2;
  6897. }
  6898. break;
  6899. default:
  6900. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6901. pf->num_iwarp_msix = min_t(int, (vec / 3),
  6902. iwarp_requested);
  6903. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  6904. I40E_DEFAULT_NUM_VMDQ_VSI);
  6905. } else {
  6906. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  6907. I40E_DEFAULT_NUM_VMDQ_VSI);
  6908. }
  6909. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6910. pf->num_fdsb_msix = 1;
  6911. vec--;
  6912. }
  6913. pf->num_lan_msix = min_t(int,
  6914. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  6915. pf->num_lan_msix);
  6916. pf->num_lan_qps = pf->num_lan_msix;
  6917. break;
  6918. }
  6919. }
  6920. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  6921. (pf->num_fdsb_msix == 0)) {
  6922. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  6923. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6924. }
  6925. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  6926. (pf->num_vmdq_msix == 0)) {
  6927. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  6928. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  6929. }
  6930. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  6931. (pf->num_iwarp_msix == 0)) {
  6932. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  6933. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  6934. }
  6935. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  6936. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  6937. pf->num_lan_msix,
  6938. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  6939. pf->num_fdsb_msix,
  6940. pf->num_iwarp_msix);
  6941. return v_actual;
  6942. }
  6943. /**
  6944. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  6945. * @vsi: the VSI being configured
  6946. * @v_idx: index of the vector in the vsi struct
  6947. * @cpu: cpu to be used on affinity_mask
  6948. *
  6949. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  6950. **/
  6951. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  6952. {
  6953. struct i40e_q_vector *q_vector;
  6954. /* allocate q_vector */
  6955. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  6956. if (!q_vector)
  6957. return -ENOMEM;
  6958. q_vector->vsi = vsi;
  6959. q_vector->v_idx = v_idx;
  6960. cpumask_set_cpu(cpu, &q_vector->affinity_mask);
  6961. if (vsi->netdev)
  6962. netif_napi_add(vsi->netdev, &q_vector->napi,
  6963. i40e_napi_poll, NAPI_POLL_WEIGHT);
  6964. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  6965. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  6966. /* tie q_vector and vsi together */
  6967. vsi->q_vectors[v_idx] = q_vector;
  6968. return 0;
  6969. }
  6970. /**
  6971. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  6972. * @vsi: the VSI being configured
  6973. *
  6974. * We allocate one q_vector per queue interrupt. If allocation fails we
  6975. * return -ENOMEM.
  6976. **/
  6977. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  6978. {
  6979. struct i40e_pf *pf = vsi->back;
  6980. int err, v_idx, num_q_vectors, current_cpu;
  6981. /* if not MSIX, give the one vector only to the LAN VSI */
  6982. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6983. num_q_vectors = vsi->num_q_vectors;
  6984. else if (vsi == pf->vsi[pf->lan_vsi])
  6985. num_q_vectors = 1;
  6986. else
  6987. return -EINVAL;
  6988. current_cpu = cpumask_first(cpu_online_mask);
  6989. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  6990. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  6991. if (err)
  6992. goto err_out;
  6993. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  6994. if (unlikely(current_cpu >= nr_cpu_ids))
  6995. current_cpu = cpumask_first(cpu_online_mask);
  6996. }
  6997. return 0;
  6998. err_out:
  6999. while (v_idx--)
  7000. i40e_free_q_vector(vsi, v_idx);
  7001. return err;
  7002. }
  7003. /**
  7004. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  7005. * @pf: board private structure to initialize
  7006. **/
  7007. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  7008. {
  7009. int vectors = 0;
  7010. ssize_t size;
  7011. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  7012. vectors = i40e_init_msix(pf);
  7013. if (vectors < 0) {
  7014. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  7015. I40E_FLAG_IWARP_ENABLED |
  7016. I40E_FLAG_RSS_ENABLED |
  7017. I40E_FLAG_DCB_CAPABLE |
  7018. I40E_FLAG_DCB_ENABLED |
  7019. I40E_FLAG_SRIOV_ENABLED |
  7020. I40E_FLAG_FD_SB_ENABLED |
  7021. I40E_FLAG_FD_ATR_ENABLED |
  7022. I40E_FLAG_VMDQ_ENABLED);
  7023. /* rework the queue expectations without MSIX */
  7024. i40e_determine_queue_usage(pf);
  7025. }
  7026. }
  7027. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  7028. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  7029. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  7030. vectors = pci_enable_msi(pf->pdev);
  7031. if (vectors < 0) {
  7032. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  7033. vectors);
  7034. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  7035. }
  7036. vectors = 1; /* one MSI or Legacy vector */
  7037. }
  7038. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  7039. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  7040. /* set up vector assignment tracking */
  7041. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  7042. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  7043. if (!pf->irq_pile) {
  7044. dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
  7045. return -ENOMEM;
  7046. }
  7047. pf->irq_pile->num_entries = vectors;
  7048. pf->irq_pile->search_hint = 0;
  7049. /* track first vector for misc interrupts, ignore return */
  7050. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  7051. return 0;
  7052. }
  7053. /**
  7054. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  7055. * @pf: board private structure
  7056. *
  7057. * This sets up the handler for MSIX 0, which is used to manage the
  7058. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  7059. * when in MSI or Legacy interrupt mode.
  7060. **/
  7061. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  7062. {
  7063. struct i40e_hw *hw = &pf->hw;
  7064. int err = 0;
  7065. /* Only request the irq if this is the first time through, and
  7066. * not when we're rebuilding after a Reset
  7067. */
  7068. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  7069. err = request_irq(pf->msix_entries[0].vector,
  7070. i40e_intr, 0, pf->int_name, pf);
  7071. if (err) {
  7072. dev_info(&pf->pdev->dev,
  7073. "request_irq for %s failed: %d\n",
  7074. pf->int_name, err);
  7075. return -EFAULT;
  7076. }
  7077. }
  7078. i40e_enable_misc_int_causes(pf);
  7079. /* associate no queues to the misc vector */
  7080. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  7081. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  7082. i40e_flush(hw);
  7083. i40e_irq_dynamic_enable_icr0(pf, true);
  7084. return err;
  7085. }
  7086. /**
  7087. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  7088. * @vsi: vsi structure
  7089. * @seed: RSS hash seed
  7090. **/
  7091. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7092. u8 *lut, u16 lut_size)
  7093. {
  7094. struct i40e_pf *pf = vsi->back;
  7095. struct i40e_hw *hw = &pf->hw;
  7096. int ret = 0;
  7097. if (seed) {
  7098. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  7099. (struct i40e_aqc_get_set_rss_key_data *)seed;
  7100. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  7101. if (ret) {
  7102. dev_info(&pf->pdev->dev,
  7103. "Cannot set RSS key, err %s aq_err %s\n",
  7104. i40e_stat_str(hw, ret),
  7105. i40e_aq_str(hw, hw->aq.asq_last_status));
  7106. return ret;
  7107. }
  7108. }
  7109. if (lut) {
  7110. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7111. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7112. if (ret) {
  7113. dev_info(&pf->pdev->dev,
  7114. "Cannot set RSS lut, err %s aq_err %s\n",
  7115. i40e_stat_str(hw, ret),
  7116. i40e_aq_str(hw, hw->aq.asq_last_status));
  7117. return ret;
  7118. }
  7119. }
  7120. return ret;
  7121. }
  7122. /**
  7123. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  7124. * @vsi: Pointer to vsi structure
  7125. * @seed: Buffter to store the hash keys
  7126. * @lut: Buffer to store the lookup table entries
  7127. * @lut_size: Size of buffer to store the lookup table entries
  7128. *
  7129. * Return 0 on success, negative on failure
  7130. */
  7131. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7132. u8 *lut, u16 lut_size)
  7133. {
  7134. struct i40e_pf *pf = vsi->back;
  7135. struct i40e_hw *hw = &pf->hw;
  7136. int ret = 0;
  7137. if (seed) {
  7138. ret = i40e_aq_get_rss_key(hw, vsi->id,
  7139. (struct i40e_aqc_get_set_rss_key_data *)seed);
  7140. if (ret) {
  7141. dev_info(&pf->pdev->dev,
  7142. "Cannot get RSS key, err %s aq_err %s\n",
  7143. i40e_stat_str(&pf->hw, ret),
  7144. i40e_aq_str(&pf->hw,
  7145. pf->hw.aq.asq_last_status));
  7146. return ret;
  7147. }
  7148. }
  7149. if (lut) {
  7150. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7151. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7152. if (ret) {
  7153. dev_info(&pf->pdev->dev,
  7154. "Cannot get RSS lut, err %s aq_err %s\n",
  7155. i40e_stat_str(&pf->hw, ret),
  7156. i40e_aq_str(&pf->hw,
  7157. pf->hw.aq.asq_last_status));
  7158. return ret;
  7159. }
  7160. }
  7161. return ret;
  7162. }
  7163. /**
  7164. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  7165. * @vsi: VSI structure
  7166. **/
  7167. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  7168. {
  7169. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7170. struct i40e_pf *pf = vsi->back;
  7171. u8 *lut;
  7172. int ret;
  7173. if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
  7174. return 0;
  7175. if (!vsi->rss_size)
  7176. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7177. vsi->num_queue_pairs);
  7178. if (!vsi->rss_size)
  7179. return -EINVAL;
  7180. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7181. if (!lut)
  7182. return -ENOMEM;
  7183. /* Use the user configured hash keys and lookup table if there is one,
  7184. * otherwise use default
  7185. */
  7186. if (vsi->rss_lut_user)
  7187. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7188. else
  7189. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7190. if (vsi->rss_hkey_user)
  7191. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7192. else
  7193. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7194. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  7195. kfree(lut);
  7196. return ret;
  7197. }
  7198. /**
  7199. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  7200. * @vsi: Pointer to vsi structure
  7201. * @seed: RSS hash seed
  7202. * @lut: Lookup table
  7203. * @lut_size: Lookup table size
  7204. *
  7205. * Returns 0 on success, negative on failure
  7206. **/
  7207. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  7208. const u8 *lut, u16 lut_size)
  7209. {
  7210. struct i40e_pf *pf = vsi->back;
  7211. struct i40e_hw *hw = &pf->hw;
  7212. u16 vf_id = vsi->vf_id;
  7213. u8 i;
  7214. /* Fill out hash function seed */
  7215. if (seed) {
  7216. u32 *seed_dw = (u32 *)seed;
  7217. if (vsi->type == I40E_VSI_MAIN) {
  7218. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7219. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  7220. } else if (vsi->type == I40E_VSI_SRIOV) {
  7221. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  7222. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  7223. } else {
  7224. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  7225. }
  7226. }
  7227. if (lut) {
  7228. u32 *lut_dw = (u32 *)lut;
  7229. if (vsi->type == I40E_VSI_MAIN) {
  7230. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7231. return -EINVAL;
  7232. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7233. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  7234. } else if (vsi->type == I40E_VSI_SRIOV) {
  7235. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  7236. return -EINVAL;
  7237. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7238. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  7239. } else {
  7240. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7241. }
  7242. }
  7243. i40e_flush(hw);
  7244. return 0;
  7245. }
  7246. /**
  7247. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  7248. * @vsi: Pointer to VSI structure
  7249. * @seed: Buffer to store the keys
  7250. * @lut: Buffer to store the lookup table entries
  7251. * @lut_size: Size of buffer to store the lookup table entries
  7252. *
  7253. * Returns 0 on success, negative on failure
  7254. */
  7255. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  7256. u8 *lut, u16 lut_size)
  7257. {
  7258. struct i40e_pf *pf = vsi->back;
  7259. struct i40e_hw *hw = &pf->hw;
  7260. u16 i;
  7261. if (seed) {
  7262. u32 *seed_dw = (u32 *)seed;
  7263. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7264. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  7265. }
  7266. if (lut) {
  7267. u32 *lut_dw = (u32 *)lut;
  7268. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7269. return -EINVAL;
  7270. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7271. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  7272. }
  7273. return 0;
  7274. }
  7275. /**
  7276. * i40e_config_rss - Configure RSS keys and lut
  7277. * @vsi: Pointer to VSI structure
  7278. * @seed: RSS hash seed
  7279. * @lut: Lookup table
  7280. * @lut_size: Lookup table size
  7281. *
  7282. * Returns 0 on success, negative on failure
  7283. */
  7284. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7285. {
  7286. struct i40e_pf *pf = vsi->back;
  7287. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7288. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  7289. else
  7290. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  7291. }
  7292. /**
  7293. * i40e_get_rss - Get RSS keys and lut
  7294. * @vsi: Pointer to VSI structure
  7295. * @seed: Buffer to store the keys
  7296. * @lut: Buffer to store the lookup table entries
  7297. * lut_size: Size of buffer to store the lookup table entries
  7298. *
  7299. * Returns 0 on success, negative on failure
  7300. */
  7301. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7302. {
  7303. struct i40e_pf *pf = vsi->back;
  7304. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7305. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  7306. else
  7307. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  7308. }
  7309. /**
  7310. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  7311. * @pf: Pointer to board private structure
  7312. * @lut: Lookup table
  7313. * @rss_table_size: Lookup table size
  7314. * @rss_size: Range of queue number for hashing
  7315. */
  7316. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  7317. u16 rss_table_size, u16 rss_size)
  7318. {
  7319. u16 i;
  7320. for (i = 0; i < rss_table_size; i++)
  7321. lut[i] = i % rss_size;
  7322. }
  7323. /**
  7324. * i40e_pf_config_rss - Prepare for RSS if used
  7325. * @pf: board private structure
  7326. **/
  7327. static int i40e_pf_config_rss(struct i40e_pf *pf)
  7328. {
  7329. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7330. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7331. u8 *lut;
  7332. struct i40e_hw *hw = &pf->hw;
  7333. u32 reg_val;
  7334. u64 hena;
  7335. int ret;
  7336. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  7337. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  7338. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  7339. hena |= i40e_pf_get_default_rss_hena(pf);
  7340. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  7341. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  7342. /* Determine the RSS table size based on the hardware capabilities */
  7343. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  7344. reg_val = (pf->rss_table_size == 512) ?
  7345. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  7346. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  7347. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  7348. /* Determine the RSS size of the VSI */
  7349. if (!vsi->rss_size) {
  7350. u16 qcount;
  7351. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7352. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7353. }
  7354. if (!vsi->rss_size)
  7355. return -EINVAL;
  7356. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7357. if (!lut)
  7358. return -ENOMEM;
  7359. /* Use user configured lut if there is one, otherwise use default */
  7360. if (vsi->rss_lut_user)
  7361. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7362. else
  7363. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7364. /* Use user configured hash key if there is one, otherwise
  7365. * use default.
  7366. */
  7367. if (vsi->rss_hkey_user)
  7368. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7369. else
  7370. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7371. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  7372. kfree(lut);
  7373. return ret;
  7374. }
  7375. /**
  7376. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  7377. * @pf: board private structure
  7378. * @queue_count: the requested queue count for rss.
  7379. *
  7380. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  7381. * count which may be different from the requested queue count.
  7382. **/
  7383. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  7384. {
  7385. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7386. int new_rss_size;
  7387. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  7388. return 0;
  7389. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  7390. if (queue_count != vsi->num_queue_pairs) {
  7391. u16 qcount;
  7392. vsi->req_queue_pairs = queue_count;
  7393. i40e_prep_for_reset(pf);
  7394. pf->alloc_rss_size = new_rss_size;
  7395. i40e_reset_and_rebuild(pf, true);
  7396. /* Discard the user configured hash keys and lut, if less
  7397. * queues are enabled.
  7398. */
  7399. if (queue_count < vsi->rss_size) {
  7400. i40e_clear_rss_config_user(vsi);
  7401. dev_dbg(&pf->pdev->dev,
  7402. "discard user configured hash keys and lut\n");
  7403. }
  7404. /* Reset vsi->rss_size, as number of enabled queues changed */
  7405. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7406. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7407. i40e_pf_config_rss(pf);
  7408. }
  7409. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  7410. vsi->req_queue_pairs, pf->rss_size_max);
  7411. return pf->alloc_rss_size;
  7412. }
  7413. /**
  7414. * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
  7415. * @pf: board private structure
  7416. **/
  7417. i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
  7418. {
  7419. i40e_status status;
  7420. bool min_valid, max_valid;
  7421. u32 max_bw, min_bw;
  7422. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  7423. &min_valid, &max_valid);
  7424. if (!status) {
  7425. if (min_valid)
  7426. pf->npar_min_bw = min_bw;
  7427. if (max_valid)
  7428. pf->npar_max_bw = max_bw;
  7429. }
  7430. return status;
  7431. }
  7432. /**
  7433. * i40e_set_npar_bw_setting - Set BW settings for this PF partition
  7434. * @pf: board private structure
  7435. **/
  7436. i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
  7437. {
  7438. struct i40e_aqc_configure_partition_bw_data bw_data;
  7439. i40e_status status;
  7440. /* Set the valid bit for this PF */
  7441. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  7442. bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
  7443. bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
  7444. /* Set the new bandwidths */
  7445. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  7446. return status;
  7447. }
  7448. /**
  7449. * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
  7450. * @pf: board private structure
  7451. **/
  7452. i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
  7453. {
  7454. /* Commit temporary BW setting to permanent NVM image */
  7455. enum i40e_admin_queue_err last_aq_status;
  7456. i40e_status ret;
  7457. u16 nvm_word;
  7458. if (pf->hw.partition_id != 1) {
  7459. dev_info(&pf->pdev->dev,
  7460. "Commit BW only works on partition 1! This is partition %d",
  7461. pf->hw.partition_id);
  7462. ret = I40E_NOT_SUPPORTED;
  7463. goto bw_commit_out;
  7464. }
  7465. /* Acquire NVM for read access */
  7466. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  7467. last_aq_status = pf->hw.aq.asq_last_status;
  7468. if (ret) {
  7469. dev_info(&pf->pdev->dev,
  7470. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  7471. i40e_stat_str(&pf->hw, ret),
  7472. i40e_aq_str(&pf->hw, last_aq_status));
  7473. goto bw_commit_out;
  7474. }
  7475. /* Read word 0x10 of NVM - SW compatibility word 1 */
  7476. ret = i40e_aq_read_nvm(&pf->hw,
  7477. I40E_SR_NVM_CONTROL_WORD,
  7478. 0x10, sizeof(nvm_word), &nvm_word,
  7479. false, NULL);
  7480. /* Save off last admin queue command status before releasing
  7481. * the NVM
  7482. */
  7483. last_aq_status = pf->hw.aq.asq_last_status;
  7484. i40e_release_nvm(&pf->hw);
  7485. if (ret) {
  7486. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  7487. i40e_stat_str(&pf->hw, ret),
  7488. i40e_aq_str(&pf->hw, last_aq_status));
  7489. goto bw_commit_out;
  7490. }
  7491. /* Wait a bit for NVM release to complete */
  7492. msleep(50);
  7493. /* Acquire NVM for write access */
  7494. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  7495. last_aq_status = pf->hw.aq.asq_last_status;
  7496. if (ret) {
  7497. dev_info(&pf->pdev->dev,
  7498. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  7499. i40e_stat_str(&pf->hw, ret),
  7500. i40e_aq_str(&pf->hw, last_aq_status));
  7501. goto bw_commit_out;
  7502. }
  7503. /* Write it back out unchanged to initiate update NVM,
  7504. * which will force a write of the shadow (alt) RAM to
  7505. * the NVM - thus storing the bandwidth values permanently.
  7506. */
  7507. ret = i40e_aq_update_nvm(&pf->hw,
  7508. I40E_SR_NVM_CONTROL_WORD,
  7509. 0x10, sizeof(nvm_word),
  7510. &nvm_word, true, NULL);
  7511. /* Save off last admin queue command status before releasing
  7512. * the NVM
  7513. */
  7514. last_aq_status = pf->hw.aq.asq_last_status;
  7515. i40e_release_nvm(&pf->hw);
  7516. if (ret)
  7517. dev_info(&pf->pdev->dev,
  7518. "BW settings NOT SAVED, err %s aq_err %s\n",
  7519. i40e_stat_str(&pf->hw, ret),
  7520. i40e_aq_str(&pf->hw, last_aq_status));
  7521. bw_commit_out:
  7522. return ret;
  7523. }
  7524. /**
  7525. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  7526. * @pf: board private structure to initialize
  7527. *
  7528. * i40e_sw_init initializes the Adapter private data structure.
  7529. * Fields are initialized based on PCI device information and
  7530. * OS network device settings (MTU size).
  7531. **/
  7532. static int i40e_sw_init(struct i40e_pf *pf)
  7533. {
  7534. int err = 0;
  7535. int size;
  7536. /* Set default capability flags */
  7537. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  7538. I40E_FLAG_MSI_ENABLED |
  7539. I40E_FLAG_MSIX_ENABLED;
  7540. /* Set default ITR */
  7541. pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
  7542. pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
  7543. /* Depending on PF configurations, it is possible that the RSS
  7544. * maximum might end up larger than the available queues
  7545. */
  7546. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  7547. pf->alloc_rss_size = 1;
  7548. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  7549. pf->rss_size_max = min_t(int, pf->rss_size_max,
  7550. pf->hw.func_caps.num_tx_qp);
  7551. if (pf->hw.func_caps.rss) {
  7552. pf->flags |= I40E_FLAG_RSS_ENABLED;
  7553. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  7554. num_online_cpus());
  7555. }
  7556. /* MFP mode enabled */
  7557. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  7558. pf->flags |= I40E_FLAG_MFP_ENABLED;
  7559. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  7560. if (i40e_get_npar_bw_setting(pf))
  7561. dev_warn(&pf->pdev->dev,
  7562. "Could not get NPAR bw settings\n");
  7563. else
  7564. dev_info(&pf->pdev->dev,
  7565. "Min BW = %8.8x, Max BW = %8.8x\n",
  7566. pf->npar_min_bw, pf->npar_max_bw);
  7567. }
  7568. /* FW/NVM is not yet fixed in this regard */
  7569. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  7570. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  7571. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  7572. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  7573. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  7574. pf->hw.num_partitions > 1)
  7575. dev_info(&pf->pdev->dev,
  7576. "Flow Director Sideband mode Disabled in MFP mode\n");
  7577. else
  7578. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7579. pf->fdir_pf_filter_count =
  7580. pf->hw.func_caps.fd_filters_guaranteed;
  7581. pf->hw.fdir_shared_filter_count =
  7582. pf->hw.func_caps.fd_filters_best_effort;
  7583. }
  7584. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7585. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  7586. (pf->hw.aq.fw_maj_ver < 4))) {
  7587. pf->flags |= I40E_FLAG_RESTART_AUTONEG;
  7588. /* No DCB support for FW < v4.33 */
  7589. pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
  7590. }
  7591. /* Disable FW LLDP if FW < v4.3 */
  7592. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7593. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  7594. (pf->hw.aq.fw_maj_ver < 4)))
  7595. pf->flags |= I40E_FLAG_STOP_FW_LLDP;
  7596. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  7597. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7598. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  7599. (pf->hw.aq.fw_maj_ver >= 5)))
  7600. pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
  7601. if (pf->hw.func_caps.vmdq) {
  7602. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  7603. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  7604. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  7605. }
  7606. if (pf->hw.func_caps.iwarp) {
  7607. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  7608. /* IWARP needs one extra vector for CQP just like MISC.*/
  7609. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  7610. }
  7611. #ifdef CONFIG_PCI_IOV
  7612. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  7613. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  7614. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  7615. pf->num_req_vfs = min_t(int,
  7616. pf->hw.func_caps.num_vfs,
  7617. I40E_MAX_VF_COUNT);
  7618. }
  7619. #endif /* CONFIG_PCI_IOV */
  7620. if (pf->hw.mac.type == I40E_MAC_X722) {
  7621. pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
  7622. | I40E_FLAG_128_QP_RSS_CAPABLE
  7623. | I40E_FLAG_HW_ATR_EVICT_CAPABLE
  7624. | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
  7625. | I40E_FLAG_WB_ON_ITR_CAPABLE
  7626. | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
  7627. | I40E_FLAG_NO_PCI_LINK_CHECK
  7628. | I40E_FLAG_USE_SET_LLDP_MIB
  7629. | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
  7630. | I40E_FLAG_PTP_L4_CAPABLE
  7631. | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
  7632. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  7633. ((pf->hw.aq.api_maj_ver == 1) &&
  7634. (pf->hw.aq.api_min_ver > 4))) {
  7635. /* Supported in FW API version higher than 1.4 */
  7636. pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
  7637. pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7638. } else {
  7639. pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7640. }
  7641. pf->eeprom_version = 0xDEAD;
  7642. pf->lan_veb = I40E_NO_VEB;
  7643. pf->lan_vsi = I40E_NO_VSI;
  7644. /* By default FW has this off for performance reasons */
  7645. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  7646. /* set up queue assignment tracking */
  7647. size = sizeof(struct i40e_lump_tracking)
  7648. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  7649. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  7650. if (!pf->qp_pile) {
  7651. err = -ENOMEM;
  7652. goto sw_init_done;
  7653. }
  7654. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  7655. pf->qp_pile->search_hint = 0;
  7656. pf->tx_timeout_recovery_level = 1;
  7657. mutex_init(&pf->switch_mutex);
  7658. /* If NPAR is enabled nudge the Tx scheduler */
  7659. if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
  7660. i40e_set_npar_bw_setting(pf);
  7661. sw_init_done:
  7662. return err;
  7663. }
  7664. /**
  7665. * i40e_set_ntuple - set the ntuple feature flag and take action
  7666. * @pf: board private structure to initialize
  7667. * @features: the feature set that the stack is suggesting
  7668. *
  7669. * returns a bool to indicate if reset needs to happen
  7670. **/
  7671. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  7672. {
  7673. bool need_reset = false;
  7674. /* Check if Flow Director n-tuple support was enabled or disabled. If
  7675. * the state changed, we need to reset.
  7676. */
  7677. if (features & NETIF_F_NTUPLE) {
  7678. /* Enable filters and mark for reset */
  7679. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7680. need_reset = true;
  7681. /* enable FD_SB only if there is MSI-X vector */
  7682. if (pf->num_fdsb_msix > 0)
  7683. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7684. } else {
  7685. /* turn off filters, mark for reset and clear SW filter list */
  7686. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7687. need_reset = true;
  7688. i40e_fdir_filter_exit(pf);
  7689. }
  7690. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7691. pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7692. /* reset fd counters */
  7693. pf->fd_add_err = 0;
  7694. pf->fd_atr_cnt = 0;
  7695. /* if ATR was auto disabled it can be re-enabled. */
  7696. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7697. (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
  7698. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  7699. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7700. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  7701. }
  7702. }
  7703. return need_reset;
  7704. }
  7705. /**
  7706. * i40e_clear_rss_lut - clear the rx hash lookup table
  7707. * @vsi: the VSI being configured
  7708. **/
  7709. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  7710. {
  7711. struct i40e_pf *pf = vsi->back;
  7712. struct i40e_hw *hw = &pf->hw;
  7713. u16 vf_id = vsi->vf_id;
  7714. u8 i;
  7715. if (vsi->type == I40E_VSI_MAIN) {
  7716. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7717. wr32(hw, I40E_PFQF_HLUT(i), 0);
  7718. } else if (vsi->type == I40E_VSI_SRIOV) {
  7719. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7720. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  7721. } else {
  7722. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7723. }
  7724. }
  7725. /**
  7726. * i40e_set_features - set the netdev feature flags
  7727. * @netdev: ptr to the netdev being adjusted
  7728. * @features: the feature set that the stack is suggesting
  7729. **/
  7730. static int i40e_set_features(struct net_device *netdev,
  7731. netdev_features_t features)
  7732. {
  7733. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7734. struct i40e_vsi *vsi = np->vsi;
  7735. struct i40e_pf *pf = vsi->back;
  7736. bool need_reset;
  7737. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  7738. i40e_pf_config_rss(pf);
  7739. else if (!(features & NETIF_F_RXHASH) &&
  7740. netdev->features & NETIF_F_RXHASH)
  7741. i40e_clear_rss_lut(vsi);
  7742. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  7743. i40e_vlan_stripping_enable(vsi);
  7744. else
  7745. i40e_vlan_stripping_disable(vsi);
  7746. need_reset = i40e_set_ntuple(pf, features);
  7747. if (need_reset)
  7748. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  7749. return 0;
  7750. }
  7751. /**
  7752. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  7753. * @pf: board private structure
  7754. * @port: The UDP port to look up
  7755. *
  7756. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  7757. **/
  7758. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  7759. {
  7760. u8 i;
  7761. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  7762. if (pf->udp_ports[i].index == port)
  7763. return i;
  7764. }
  7765. return i;
  7766. }
  7767. /**
  7768. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  7769. * @netdev: This physical port's netdev
  7770. * @ti: Tunnel endpoint information
  7771. **/
  7772. static void i40e_udp_tunnel_add(struct net_device *netdev,
  7773. struct udp_tunnel_info *ti)
  7774. {
  7775. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7776. struct i40e_vsi *vsi = np->vsi;
  7777. struct i40e_pf *pf = vsi->back;
  7778. u16 port = ntohs(ti->port);
  7779. u8 next_idx;
  7780. u8 idx;
  7781. idx = i40e_get_udp_port_idx(pf, port);
  7782. /* Check if port already exists */
  7783. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7784. netdev_info(netdev, "port %d already offloaded\n", port);
  7785. return;
  7786. }
  7787. /* Now check if there is space to add the new port */
  7788. next_idx = i40e_get_udp_port_idx(pf, 0);
  7789. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7790. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  7791. port);
  7792. return;
  7793. }
  7794. switch (ti->type) {
  7795. case UDP_TUNNEL_TYPE_VXLAN:
  7796. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  7797. break;
  7798. case UDP_TUNNEL_TYPE_GENEVE:
  7799. if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
  7800. return;
  7801. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  7802. break;
  7803. default:
  7804. return;
  7805. }
  7806. /* New port: add it and mark its index in the bitmap */
  7807. pf->udp_ports[next_idx].index = port;
  7808. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  7809. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7810. }
  7811. /**
  7812. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  7813. * @netdev: This physical port's netdev
  7814. * @ti: Tunnel endpoint information
  7815. **/
  7816. static void i40e_udp_tunnel_del(struct net_device *netdev,
  7817. struct udp_tunnel_info *ti)
  7818. {
  7819. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7820. struct i40e_vsi *vsi = np->vsi;
  7821. struct i40e_pf *pf = vsi->back;
  7822. u16 port = ntohs(ti->port);
  7823. u8 idx;
  7824. idx = i40e_get_udp_port_idx(pf, port);
  7825. /* Check if port already exists */
  7826. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  7827. goto not_found;
  7828. switch (ti->type) {
  7829. case UDP_TUNNEL_TYPE_VXLAN:
  7830. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  7831. goto not_found;
  7832. break;
  7833. case UDP_TUNNEL_TYPE_GENEVE:
  7834. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  7835. goto not_found;
  7836. break;
  7837. default:
  7838. goto not_found;
  7839. }
  7840. /* if port exists, set it to 0 (mark for deletion)
  7841. * and make it pending
  7842. */
  7843. pf->udp_ports[idx].index = 0;
  7844. pf->pending_udp_bitmap |= BIT_ULL(idx);
  7845. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7846. return;
  7847. not_found:
  7848. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  7849. port);
  7850. }
  7851. static int i40e_get_phys_port_id(struct net_device *netdev,
  7852. struct netdev_phys_item_id *ppid)
  7853. {
  7854. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7855. struct i40e_pf *pf = np->vsi->back;
  7856. struct i40e_hw *hw = &pf->hw;
  7857. if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
  7858. return -EOPNOTSUPP;
  7859. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  7860. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  7861. return 0;
  7862. }
  7863. /**
  7864. * i40e_ndo_fdb_add - add an entry to the hardware database
  7865. * @ndm: the input from the stack
  7866. * @tb: pointer to array of nladdr (unused)
  7867. * @dev: the net device pointer
  7868. * @addr: the MAC address entry being added
  7869. * @flags: instructions from stack about fdb operation
  7870. */
  7871. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  7872. struct net_device *dev,
  7873. const unsigned char *addr, u16 vid,
  7874. u16 flags)
  7875. {
  7876. struct i40e_netdev_priv *np = netdev_priv(dev);
  7877. struct i40e_pf *pf = np->vsi->back;
  7878. int err = 0;
  7879. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  7880. return -EOPNOTSUPP;
  7881. if (vid) {
  7882. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  7883. return -EINVAL;
  7884. }
  7885. /* Hardware does not support aging addresses so if a
  7886. * ndm_state is given only allow permanent addresses
  7887. */
  7888. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  7889. netdev_info(dev, "FDB only supports static addresses\n");
  7890. return -EINVAL;
  7891. }
  7892. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  7893. err = dev_uc_add_excl(dev, addr);
  7894. else if (is_multicast_ether_addr(addr))
  7895. err = dev_mc_add_excl(dev, addr);
  7896. else
  7897. err = -EINVAL;
  7898. /* Only return duplicate errors if NLM_F_EXCL is set */
  7899. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  7900. err = 0;
  7901. return err;
  7902. }
  7903. /**
  7904. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  7905. * @dev: the netdev being configured
  7906. * @nlh: RTNL message
  7907. *
  7908. * Inserts a new hardware bridge if not already created and
  7909. * enables the bridging mode requested (VEB or VEPA). If the
  7910. * hardware bridge has already been inserted and the request
  7911. * is to change the mode then that requires a PF reset to
  7912. * allow rebuild of the components with required hardware
  7913. * bridge mode enabled.
  7914. **/
  7915. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  7916. struct nlmsghdr *nlh,
  7917. u16 flags)
  7918. {
  7919. struct i40e_netdev_priv *np = netdev_priv(dev);
  7920. struct i40e_vsi *vsi = np->vsi;
  7921. struct i40e_pf *pf = vsi->back;
  7922. struct i40e_veb *veb = NULL;
  7923. struct nlattr *attr, *br_spec;
  7924. int i, rem;
  7925. /* Only for PF VSI for now */
  7926. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  7927. return -EOPNOTSUPP;
  7928. /* Find the HW bridge for PF VSI */
  7929. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  7930. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  7931. veb = pf->veb[i];
  7932. }
  7933. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  7934. nla_for_each_nested(attr, br_spec, rem) {
  7935. __u16 mode;
  7936. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  7937. continue;
  7938. mode = nla_get_u16(attr);
  7939. if ((mode != BRIDGE_MODE_VEPA) &&
  7940. (mode != BRIDGE_MODE_VEB))
  7941. return -EINVAL;
  7942. /* Insert a new HW bridge */
  7943. if (!veb) {
  7944. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  7945. vsi->tc_config.enabled_tc);
  7946. if (veb) {
  7947. veb->bridge_mode = mode;
  7948. i40e_config_bridge_mode(veb);
  7949. } else {
  7950. /* No Bridge HW offload available */
  7951. return -ENOENT;
  7952. }
  7953. break;
  7954. } else if (mode != veb->bridge_mode) {
  7955. /* Existing HW bridge but different mode needs reset */
  7956. veb->bridge_mode = mode;
  7957. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  7958. if (mode == BRIDGE_MODE_VEB)
  7959. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  7960. else
  7961. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  7962. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  7963. break;
  7964. }
  7965. }
  7966. return 0;
  7967. }
  7968. /**
  7969. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  7970. * @skb: skb buff
  7971. * @pid: process id
  7972. * @seq: RTNL message seq #
  7973. * @dev: the netdev being configured
  7974. * @filter_mask: unused
  7975. * @nlflags: netlink flags passed in
  7976. *
  7977. * Return the mode in which the hardware bridge is operating in
  7978. * i.e VEB or VEPA.
  7979. **/
  7980. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  7981. struct net_device *dev,
  7982. u32 __always_unused filter_mask,
  7983. int nlflags)
  7984. {
  7985. struct i40e_netdev_priv *np = netdev_priv(dev);
  7986. struct i40e_vsi *vsi = np->vsi;
  7987. struct i40e_pf *pf = vsi->back;
  7988. struct i40e_veb *veb = NULL;
  7989. int i;
  7990. /* Only for PF VSI for now */
  7991. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  7992. return -EOPNOTSUPP;
  7993. /* Find the HW bridge for the PF VSI */
  7994. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  7995. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  7996. veb = pf->veb[i];
  7997. }
  7998. if (!veb)
  7999. return 0;
  8000. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  8001. 0, 0, nlflags, filter_mask, NULL);
  8002. }
  8003. /**
  8004. * i40e_features_check - Validate encapsulated packet conforms to limits
  8005. * @skb: skb buff
  8006. * @dev: This physical port's netdev
  8007. * @features: Offload features that the stack believes apply
  8008. **/
  8009. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  8010. struct net_device *dev,
  8011. netdev_features_t features)
  8012. {
  8013. size_t len;
  8014. /* No point in doing any of this if neither checksum nor GSO are
  8015. * being requested for this frame. We can rule out both by just
  8016. * checking for CHECKSUM_PARTIAL
  8017. */
  8018. if (skb->ip_summed != CHECKSUM_PARTIAL)
  8019. return features;
  8020. /* We cannot support GSO if the MSS is going to be less than
  8021. * 64 bytes. If it is then we need to drop support for GSO.
  8022. */
  8023. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  8024. features &= ~NETIF_F_GSO_MASK;
  8025. /* MACLEN can support at most 63 words */
  8026. len = skb_network_header(skb) - skb->data;
  8027. if (len & ~(63 * 2))
  8028. goto out_err;
  8029. /* IPLEN and EIPLEN can support at most 127 dwords */
  8030. len = skb_transport_header(skb) - skb_network_header(skb);
  8031. if (len & ~(127 * 4))
  8032. goto out_err;
  8033. if (skb->encapsulation) {
  8034. /* L4TUNLEN can support 127 words */
  8035. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  8036. if (len & ~(127 * 2))
  8037. goto out_err;
  8038. /* IPLEN can support at most 127 dwords */
  8039. len = skb_inner_transport_header(skb) -
  8040. skb_inner_network_header(skb);
  8041. if (len & ~(127 * 4))
  8042. goto out_err;
  8043. }
  8044. /* No need to validate L4LEN as TCP is the only protocol with a
  8045. * a flexible value and we support all possible values supported
  8046. * by TCP, which is at most 15 dwords
  8047. */
  8048. return features;
  8049. out_err:
  8050. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  8051. }
  8052. static const struct net_device_ops i40e_netdev_ops = {
  8053. .ndo_open = i40e_open,
  8054. .ndo_stop = i40e_close,
  8055. .ndo_start_xmit = i40e_lan_xmit_frame,
  8056. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  8057. .ndo_set_rx_mode = i40e_set_rx_mode,
  8058. .ndo_validate_addr = eth_validate_addr,
  8059. .ndo_set_mac_address = i40e_set_mac,
  8060. .ndo_change_mtu = i40e_change_mtu,
  8061. .ndo_do_ioctl = i40e_ioctl,
  8062. .ndo_tx_timeout = i40e_tx_timeout,
  8063. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  8064. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  8065. #ifdef CONFIG_NET_POLL_CONTROLLER
  8066. .ndo_poll_controller = i40e_netpoll,
  8067. #endif
  8068. .ndo_setup_tc = __i40e_setup_tc,
  8069. .ndo_set_features = i40e_set_features,
  8070. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  8071. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  8072. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  8073. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  8074. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  8075. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  8076. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  8077. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  8078. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  8079. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  8080. .ndo_fdb_add = i40e_ndo_fdb_add,
  8081. .ndo_features_check = i40e_features_check,
  8082. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  8083. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  8084. };
  8085. /**
  8086. * i40e_config_netdev - Setup the netdev flags
  8087. * @vsi: the VSI being configured
  8088. *
  8089. * Returns 0 on success, negative value on failure
  8090. **/
  8091. static int i40e_config_netdev(struct i40e_vsi *vsi)
  8092. {
  8093. struct i40e_pf *pf = vsi->back;
  8094. struct i40e_hw *hw = &pf->hw;
  8095. struct i40e_netdev_priv *np;
  8096. struct net_device *netdev;
  8097. u8 broadcast[ETH_ALEN];
  8098. u8 mac_addr[ETH_ALEN];
  8099. int etherdev_size;
  8100. netdev_features_t hw_enc_features;
  8101. netdev_features_t hw_features;
  8102. etherdev_size = sizeof(struct i40e_netdev_priv);
  8103. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  8104. if (!netdev)
  8105. return -ENOMEM;
  8106. vsi->netdev = netdev;
  8107. np = netdev_priv(netdev);
  8108. np->vsi = vsi;
  8109. hw_enc_features = NETIF_F_SG |
  8110. NETIF_F_IP_CSUM |
  8111. NETIF_F_IPV6_CSUM |
  8112. NETIF_F_HIGHDMA |
  8113. NETIF_F_SOFT_FEATURES |
  8114. NETIF_F_TSO |
  8115. NETIF_F_TSO_ECN |
  8116. NETIF_F_TSO6 |
  8117. NETIF_F_GSO_GRE |
  8118. NETIF_F_GSO_GRE_CSUM |
  8119. NETIF_F_GSO_PARTIAL |
  8120. NETIF_F_GSO_UDP_TUNNEL |
  8121. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  8122. NETIF_F_SCTP_CRC |
  8123. NETIF_F_RXHASH |
  8124. NETIF_F_RXCSUM |
  8125. 0;
  8126. if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
  8127. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  8128. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  8129. netdev->hw_enc_features |= hw_enc_features;
  8130. /* record features VLANs can make use of */
  8131. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  8132. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  8133. netdev->hw_features |= NETIF_F_NTUPLE;
  8134. hw_features = hw_enc_features |
  8135. NETIF_F_HW_VLAN_CTAG_TX |
  8136. NETIF_F_HW_VLAN_CTAG_RX;
  8137. netdev->hw_features |= hw_features;
  8138. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  8139. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  8140. if (vsi->type == I40E_VSI_MAIN) {
  8141. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  8142. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  8143. /* The following steps are necessary to properly keep track of
  8144. * MAC-VLAN filters loaded into firmware - first we remove
  8145. * filter that is automatically generated by firmware and then
  8146. * add new filter both to the driver hash table and firmware.
  8147. */
  8148. i40e_rm_default_mac_filter(vsi, mac_addr);
  8149. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8150. i40e_add_mac_filter(vsi, mac_addr);
  8151. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8152. } else {
  8153. /* relate the VSI_VMDQ name to the VSI_MAIN name */
  8154. snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
  8155. pf->vsi[pf->lan_vsi]->netdev->name);
  8156. random_ether_addr(mac_addr);
  8157. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8158. i40e_add_mac_filter(vsi, mac_addr);
  8159. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8160. }
  8161. /* Add the broadcast filter so that we initially will receive
  8162. * broadcast packets. Note that when a new VLAN is first added the
  8163. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  8164. * specific filters as part of transitioning into "vlan" operation.
  8165. * When more VLANs are added, the driver will copy each existing MAC
  8166. * filter and add it for the new VLAN.
  8167. *
  8168. * Broadcast filters are handled specially by
  8169. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  8170. * promiscuous bit instead of adding this directly as a MAC/VLAN
  8171. * filter. The subtask will update the correct broadcast promiscuous
  8172. * bits as VLANs become active or inactive.
  8173. */
  8174. eth_broadcast_addr(broadcast);
  8175. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8176. i40e_add_mac_filter(vsi, broadcast);
  8177. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8178. ether_addr_copy(netdev->dev_addr, mac_addr);
  8179. ether_addr_copy(netdev->perm_addr, mac_addr);
  8180. netdev->priv_flags |= IFF_UNICAST_FLT;
  8181. netdev->priv_flags |= IFF_SUPP_NOFCS;
  8182. /* Setup netdev TC information */
  8183. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  8184. netdev->netdev_ops = &i40e_netdev_ops;
  8185. netdev->watchdog_timeo = 5 * HZ;
  8186. i40e_set_ethtool_ops(netdev);
  8187. /* MTU range: 68 - 9706 */
  8188. netdev->min_mtu = ETH_MIN_MTU;
  8189. netdev->max_mtu = I40E_MAX_RXBUFFER -
  8190. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  8191. return 0;
  8192. }
  8193. /**
  8194. * i40e_vsi_delete - Delete a VSI from the switch
  8195. * @vsi: the VSI being removed
  8196. *
  8197. * Returns 0 on success, negative value on failure
  8198. **/
  8199. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  8200. {
  8201. /* remove default VSI is not allowed */
  8202. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  8203. return;
  8204. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  8205. }
  8206. /**
  8207. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  8208. * @vsi: the VSI being queried
  8209. *
  8210. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  8211. **/
  8212. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  8213. {
  8214. struct i40e_veb *veb;
  8215. struct i40e_pf *pf = vsi->back;
  8216. /* Uplink is not a bridge so default to VEB */
  8217. if (vsi->veb_idx == I40E_NO_VEB)
  8218. return 1;
  8219. veb = pf->veb[vsi->veb_idx];
  8220. if (!veb) {
  8221. dev_info(&pf->pdev->dev,
  8222. "There is no veb associated with the bridge\n");
  8223. return -ENOENT;
  8224. }
  8225. /* Uplink is a bridge in VEPA mode */
  8226. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  8227. return 0;
  8228. } else {
  8229. /* Uplink is a bridge in VEB mode */
  8230. return 1;
  8231. }
  8232. /* VEPA is now default bridge, so return 0 */
  8233. return 0;
  8234. }
  8235. /**
  8236. * i40e_add_vsi - Add a VSI to the switch
  8237. * @vsi: the VSI being configured
  8238. *
  8239. * This initializes a VSI context depending on the VSI type to be added and
  8240. * passes it down to the add_vsi aq command.
  8241. **/
  8242. static int i40e_add_vsi(struct i40e_vsi *vsi)
  8243. {
  8244. int ret = -ENODEV;
  8245. struct i40e_pf *pf = vsi->back;
  8246. struct i40e_hw *hw = &pf->hw;
  8247. struct i40e_vsi_context ctxt;
  8248. struct i40e_mac_filter *f;
  8249. struct hlist_node *h;
  8250. int bkt;
  8251. u8 enabled_tc = 0x1; /* TC0 enabled */
  8252. int f_count = 0;
  8253. memset(&ctxt, 0, sizeof(ctxt));
  8254. switch (vsi->type) {
  8255. case I40E_VSI_MAIN:
  8256. /* The PF's main VSI is already setup as part of the
  8257. * device initialization, so we'll not bother with
  8258. * the add_vsi call, but we will retrieve the current
  8259. * VSI context.
  8260. */
  8261. ctxt.seid = pf->main_vsi_seid;
  8262. ctxt.pf_num = pf->hw.pf_id;
  8263. ctxt.vf_num = 0;
  8264. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  8265. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8266. if (ret) {
  8267. dev_info(&pf->pdev->dev,
  8268. "couldn't get PF vsi config, err %s aq_err %s\n",
  8269. i40e_stat_str(&pf->hw, ret),
  8270. i40e_aq_str(&pf->hw,
  8271. pf->hw.aq.asq_last_status));
  8272. return -ENOENT;
  8273. }
  8274. vsi->info = ctxt.info;
  8275. vsi->info.valid_sections = 0;
  8276. vsi->seid = ctxt.seid;
  8277. vsi->id = ctxt.vsi_number;
  8278. enabled_tc = i40e_pf_get_tc_map(pf);
  8279. /* MFP mode setup queue map and update VSI */
  8280. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  8281. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  8282. memset(&ctxt, 0, sizeof(ctxt));
  8283. ctxt.seid = pf->main_vsi_seid;
  8284. ctxt.pf_num = pf->hw.pf_id;
  8285. ctxt.vf_num = 0;
  8286. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  8287. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  8288. if (ret) {
  8289. dev_info(&pf->pdev->dev,
  8290. "update vsi failed, err %s aq_err %s\n",
  8291. i40e_stat_str(&pf->hw, ret),
  8292. i40e_aq_str(&pf->hw,
  8293. pf->hw.aq.asq_last_status));
  8294. ret = -ENOENT;
  8295. goto err;
  8296. }
  8297. /* update the local VSI info queue map */
  8298. i40e_vsi_update_queue_map(vsi, &ctxt);
  8299. vsi->info.valid_sections = 0;
  8300. } else {
  8301. /* Default/Main VSI is only enabled for TC0
  8302. * reconfigure it to enable all TCs that are
  8303. * available on the port in SFP mode.
  8304. * For MFP case the iSCSI PF would use this
  8305. * flow to enable LAN+iSCSI TC.
  8306. */
  8307. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  8308. if (ret) {
  8309. dev_info(&pf->pdev->dev,
  8310. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  8311. enabled_tc,
  8312. i40e_stat_str(&pf->hw, ret),
  8313. i40e_aq_str(&pf->hw,
  8314. pf->hw.aq.asq_last_status));
  8315. ret = -ENOENT;
  8316. }
  8317. }
  8318. break;
  8319. case I40E_VSI_FDIR:
  8320. ctxt.pf_num = hw->pf_id;
  8321. ctxt.vf_num = 0;
  8322. ctxt.uplink_seid = vsi->uplink_seid;
  8323. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8324. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8325. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  8326. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  8327. ctxt.info.valid_sections |=
  8328. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8329. ctxt.info.switch_id =
  8330. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8331. }
  8332. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8333. break;
  8334. case I40E_VSI_VMDQ2:
  8335. ctxt.pf_num = hw->pf_id;
  8336. ctxt.vf_num = 0;
  8337. ctxt.uplink_seid = vsi->uplink_seid;
  8338. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8339. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  8340. /* This VSI is connected to VEB so the switch_id
  8341. * should be set to zero by default.
  8342. */
  8343. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8344. ctxt.info.valid_sections |=
  8345. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8346. ctxt.info.switch_id =
  8347. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8348. }
  8349. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8350. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8351. break;
  8352. case I40E_VSI_SRIOV:
  8353. ctxt.pf_num = hw->pf_id;
  8354. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  8355. ctxt.uplink_seid = vsi->uplink_seid;
  8356. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8357. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  8358. /* This VSI is connected to VEB so the switch_id
  8359. * should be set to zero by default.
  8360. */
  8361. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8362. ctxt.info.valid_sections |=
  8363. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8364. ctxt.info.switch_id =
  8365. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8366. }
  8367. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  8368. ctxt.info.valid_sections |=
  8369. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  8370. ctxt.info.queueing_opt_flags |=
  8371. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  8372. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  8373. }
  8374. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  8375. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  8376. if (pf->vf[vsi->vf_id].spoofchk) {
  8377. ctxt.info.valid_sections |=
  8378. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  8379. ctxt.info.sec_flags |=
  8380. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  8381. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  8382. }
  8383. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8384. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8385. break;
  8386. case I40E_VSI_IWARP:
  8387. /* send down message to iWARP */
  8388. break;
  8389. default:
  8390. return -ENODEV;
  8391. }
  8392. if (vsi->type != I40E_VSI_MAIN) {
  8393. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  8394. if (ret) {
  8395. dev_info(&vsi->back->pdev->dev,
  8396. "add vsi failed, err %s aq_err %s\n",
  8397. i40e_stat_str(&pf->hw, ret),
  8398. i40e_aq_str(&pf->hw,
  8399. pf->hw.aq.asq_last_status));
  8400. ret = -ENOENT;
  8401. goto err;
  8402. }
  8403. vsi->info = ctxt.info;
  8404. vsi->info.valid_sections = 0;
  8405. vsi->seid = ctxt.seid;
  8406. vsi->id = ctxt.vsi_number;
  8407. }
  8408. vsi->active_filters = 0;
  8409. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  8410. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8411. /* If macvlan filters already exist, force them to get loaded */
  8412. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  8413. f->state = I40E_FILTER_NEW;
  8414. f_count++;
  8415. }
  8416. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8417. if (f_count) {
  8418. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  8419. pf->flags |= I40E_FLAG_FILTER_SYNC;
  8420. }
  8421. /* Update VSI BW information */
  8422. ret = i40e_vsi_get_bw_info(vsi);
  8423. if (ret) {
  8424. dev_info(&pf->pdev->dev,
  8425. "couldn't get vsi bw info, err %s aq_err %s\n",
  8426. i40e_stat_str(&pf->hw, ret),
  8427. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8428. /* VSI is already added so not tearing that up */
  8429. ret = 0;
  8430. }
  8431. err:
  8432. return ret;
  8433. }
  8434. /**
  8435. * i40e_vsi_release - Delete a VSI and free its resources
  8436. * @vsi: the VSI being removed
  8437. *
  8438. * Returns 0 on success or < 0 on error
  8439. **/
  8440. int i40e_vsi_release(struct i40e_vsi *vsi)
  8441. {
  8442. struct i40e_mac_filter *f;
  8443. struct hlist_node *h;
  8444. struct i40e_veb *veb = NULL;
  8445. struct i40e_pf *pf;
  8446. u16 uplink_seid;
  8447. int i, n, bkt;
  8448. pf = vsi->back;
  8449. /* release of a VEB-owner or last VSI is not allowed */
  8450. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  8451. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  8452. vsi->seid, vsi->uplink_seid);
  8453. return -ENODEV;
  8454. }
  8455. if (vsi == pf->vsi[pf->lan_vsi] &&
  8456. !test_bit(__I40E_DOWN, &pf->state)) {
  8457. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  8458. return -ENODEV;
  8459. }
  8460. uplink_seid = vsi->uplink_seid;
  8461. if (vsi->type != I40E_VSI_SRIOV) {
  8462. if (vsi->netdev_registered) {
  8463. vsi->netdev_registered = false;
  8464. if (vsi->netdev) {
  8465. /* results in a call to i40e_close() */
  8466. unregister_netdev(vsi->netdev);
  8467. }
  8468. } else {
  8469. i40e_vsi_close(vsi);
  8470. }
  8471. i40e_vsi_disable_irq(vsi);
  8472. }
  8473. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8474. /* clear the sync flag on all filters */
  8475. if (vsi->netdev) {
  8476. __dev_uc_unsync(vsi->netdev, NULL);
  8477. __dev_mc_unsync(vsi->netdev, NULL);
  8478. }
  8479. /* make sure any remaining filters are marked for deletion */
  8480. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  8481. __i40e_del_filter(vsi, f);
  8482. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8483. i40e_sync_vsi_filters(vsi);
  8484. i40e_vsi_delete(vsi);
  8485. i40e_vsi_free_q_vectors(vsi);
  8486. if (vsi->netdev) {
  8487. free_netdev(vsi->netdev);
  8488. vsi->netdev = NULL;
  8489. }
  8490. i40e_vsi_clear_rings(vsi);
  8491. i40e_vsi_clear(vsi);
  8492. /* If this was the last thing on the VEB, except for the
  8493. * controlling VSI, remove the VEB, which puts the controlling
  8494. * VSI onto the next level down in the switch.
  8495. *
  8496. * Well, okay, there's one more exception here: don't remove
  8497. * the orphan VEBs yet. We'll wait for an explicit remove request
  8498. * from up the network stack.
  8499. */
  8500. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  8501. if (pf->vsi[i] &&
  8502. pf->vsi[i]->uplink_seid == uplink_seid &&
  8503. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8504. n++; /* count the VSIs */
  8505. }
  8506. }
  8507. for (i = 0; i < I40E_MAX_VEB; i++) {
  8508. if (!pf->veb[i])
  8509. continue;
  8510. if (pf->veb[i]->uplink_seid == uplink_seid)
  8511. n++; /* count the VEBs */
  8512. if (pf->veb[i]->seid == uplink_seid)
  8513. veb = pf->veb[i];
  8514. }
  8515. if (n == 0 && veb && veb->uplink_seid != 0)
  8516. i40e_veb_release(veb);
  8517. return 0;
  8518. }
  8519. /**
  8520. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  8521. * @vsi: ptr to the VSI
  8522. *
  8523. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  8524. * corresponding SW VSI structure and initializes num_queue_pairs for the
  8525. * newly allocated VSI.
  8526. *
  8527. * Returns 0 on success or negative on failure
  8528. **/
  8529. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  8530. {
  8531. int ret = -ENOENT;
  8532. struct i40e_pf *pf = vsi->back;
  8533. if (vsi->q_vectors[0]) {
  8534. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  8535. vsi->seid);
  8536. return -EEXIST;
  8537. }
  8538. if (vsi->base_vector) {
  8539. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  8540. vsi->seid, vsi->base_vector);
  8541. return -EEXIST;
  8542. }
  8543. ret = i40e_vsi_alloc_q_vectors(vsi);
  8544. if (ret) {
  8545. dev_info(&pf->pdev->dev,
  8546. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  8547. vsi->num_q_vectors, vsi->seid, ret);
  8548. vsi->num_q_vectors = 0;
  8549. goto vector_setup_out;
  8550. }
  8551. /* In Legacy mode, we do not have to get any other vector since we
  8552. * piggyback on the misc/ICR0 for queue interrupts.
  8553. */
  8554. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8555. return ret;
  8556. if (vsi->num_q_vectors)
  8557. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  8558. vsi->num_q_vectors, vsi->idx);
  8559. if (vsi->base_vector < 0) {
  8560. dev_info(&pf->pdev->dev,
  8561. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  8562. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  8563. i40e_vsi_free_q_vectors(vsi);
  8564. ret = -ENOENT;
  8565. goto vector_setup_out;
  8566. }
  8567. vector_setup_out:
  8568. return ret;
  8569. }
  8570. /**
  8571. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  8572. * @vsi: pointer to the vsi.
  8573. *
  8574. * This re-allocates a vsi's queue resources.
  8575. *
  8576. * Returns pointer to the successfully allocated and configured VSI sw struct
  8577. * on success, otherwise returns NULL on failure.
  8578. **/
  8579. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  8580. {
  8581. struct i40e_pf *pf;
  8582. u8 enabled_tc;
  8583. int ret;
  8584. if (!vsi)
  8585. return NULL;
  8586. pf = vsi->back;
  8587. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8588. i40e_vsi_clear_rings(vsi);
  8589. i40e_vsi_free_arrays(vsi, false);
  8590. i40e_set_num_rings_in_vsi(vsi);
  8591. ret = i40e_vsi_alloc_arrays(vsi, false);
  8592. if (ret)
  8593. goto err_vsi;
  8594. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
  8595. if (ret < 0) {
  8596. dev_info(&pf->pdev->dev,
  8597. "failed to get tracking for %d queues for VSI %d err %d\n",
  8598. vsi->alloc_queue_pairs, vsi->seid, ret);
  8599. goto err_vsi;
  8600. }
  8601. vsi->base_queue = ret;
  8602. /* Update the FW view of the VSI. Force a reset of TC and queue
  8603. * layout configurations.
  8604. */
  8605. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  8606. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  8607. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  8608. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  8609. if (vsi->type == I40E_VSI_MAIN)
  8610. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  8611. /* assign it some queues */
  8612. ret = i40e_alloc_rings(vsi);
  8613. if (ret)
  8614. goto err_rings;
  8615. /* map all of the rings to the q_vectors */
  8616. i40e_vsi_map_rings_to_vectors(vsi);
  8617. return vsi;
  8618. err_rings:
  8619. i40e_vsi_free_q_vectors(vsi);
  8620. if (vsi->netdev_registered) {
  8621. vsi->netdev_registered = false;
  8622. unregister_netdev(vsi->netdev);
  8623. free_netdev(vsi->netdev);
  8624. vsi->netdev = NULL;
  8625. }
  8626. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8627. err_vsi:
  8628. i40e_vsi_clear(vsi);
  8629. return NULL;
  8630. }
  8631. /**
  8632. * i40e_vsi_setup - Set up a VSI by a given type
  8633. * @pf: board private structure
  8634. * @type: VSI type
  8635. * @uplink_seid: the switch element to link to
  8636. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  8637. *
  8638. * This allocates the sw VSI structure and its queue resources, then add a VSI
  8639. * to the identified VEB.
  8640. *
  8641. * Returns pointer to the successfully allocated and configure VSI sw struct on
  8642. * success, otherwise returns NULL on failure.
  8643. **/
  8644. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  8645. u16 uplink_seid, u32 param1)
  8646. {
  8647. struct i40e_vsi *vsi = NULL;
  8648. struct i40e_veb *veb = NULL;
  8649. int ret, i;
  8650. int v_idx;
  8651. /* The requested uplink_seid must be either
  8652. * - the PF's port seid
  8653. * no VEB is needed because this is the PF
  8654. * or this is a Flow Director special case VSI
  8655. * - seid of an existing VEB
  8656. * - seid of a VSI that owns an existing VEB
  8657. * - seid of a VSI that doesn't own a VEB
  8658. * a new VEB is created and the VSI becomes the owner
  8659. * - seid of the PF VSI, which is what creates the first VEB
  8660. * this is a special case of the previous
  8661. *
  8662. * Find which uplink_seid we were given and create a new VEB if needed
  8663. */
  8664. for (i = 0; i < I40E_MAX_VEB; i++) {
  8665. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  8666. veb = pf->veb[i];
  8667. break;
  8668. }
  8669. }
  8670. if (!veb && uplink_seid != pf->mac_seid) {
  8671. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8672. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  8673. vsi = pf->vsi[i];
  8674. break;
  8675. }
  8676. }
  8677. if (!vsi) {
  8678. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  8679. uplink_seid);
  8680. return NULL;
  8681. }
  8682. if (vsi->uplink_seid == pf->mac_seid)
  8683. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  8684. vsi->tc_config.enabled_tc);
  8685. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  8686. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8687. vsi->tc_config.enabled_tc);
  8688. if (veb) {
  8689. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  8690. dev_info(&vsi->back->pdev->dev,
  8691. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  8692. return NULL;
  8693. }
  8694. /* We come up by default in VEPA mode if SRIOV is not
  8695. * already enabled, in which case we can't force VEPA
  8696. * mode.
  8697. */
  8698. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  8699. veb->bridge_mode = BRIDGE_MODE_VEPA;
  8700. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8701. }
  8702. i40e_config_bridge_mode(veb);
  8703. }
  8704. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8705. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8706. veb = pf->veb[i];
  8707. }
  8708. if (!veb) {
  8709. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  8710. return NULL;
  8711. }
  8712. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  8713. uplink_seid = veb->seid;
  8714. }
  8715. /* get vsi sw struct */
  8716. v_idx = i40e_vsi_mem_alloc(pf, type);
  8717. if (v_idx < 0)
  8718. goto err_alloc;
  8719. vsi = pf->vsi[v_idx];
  8720. if (!vsi)
  8721. goto err_alloc;
  8722. vsi->type = type;
  8723. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  8724. if (type == I40E_VSI_MAIN)
  8725. pf->lan_vsi = v_idx;
  8726. else if (type == I40E_VSI_SRIOV)
  8727. vsi->vf_id = param1;
  8728. /* assign it some queues */
  8729. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
  8730. vsi->idx);
  8731. if (ret < 0) {
  8732. dev_info(&pf->pdev->dev,
  8733. "failed to get tracking for %d queues for VSI %d err=%d\n",
  8734. vsi->alloc_queue_pairs, vsi->seid, ret);
  8735. goto err_vsi;
  8736. }
  8737. vsi->base_queue = ret;
  8738. /* get a VSI from the hardware */
  8739. vsi->uplink_seid = uplink_seid;
  8740. ret = i40e_add_vsi(vsi);
  8741. if (ret)
  8742. goto err_vsi;
  8743. switch (vsi->type) {
  8744. /* setup the netdev if needed */
  8745. case I40E_VSI_MAIN:
  8746. /* Apply relevant filters if a platform-specific mac
  8747. * address was selected.
  8748. */
  8749. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  8750. ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  8751. if (ret) {
  8752. dev_warn(&pf->pdev->dev,
  8753. "could not set up macaddr; err %d\n",
  8754. ret);
  8755. }
  8756. }
  8757. case I40E_VSI_VMDQ2:
  8758. ret = i40e_config_netdev(vsi);
  8759. if (ret)
  8760. goto err_netdev;
  8761. ret = register_netdev(vsi->netdev);
  8762. if (ret)
  8763. goto err_netdev;
  8764. vsi->netdev_registered = true;
  8765. netif_carrier_off(vsi->netdev);
  8766. #ifdef CONFIG_I40E_DCB
  8767. /* Setup DCB netlink interface */
  8768. i40e_dcbnl_setup(vsi);
  8769. #endif /* CONFIG_I40E_DCB */
  8770. /* fall through */
  8771. case I40E_VSI_FDIR:
  8772. /* set up vectors and rings if needed */
  8773. ret = i40e_vsi_setup_vectors(vsi);
  8774. if (ret)
  8775. goto err_msix;
  8776. ret = i40e_alloc_rings(vsi);
  8777. if (ret)
  8778. goto err_rings;
  8779. /* map all of the rings to the q_vectors */
  8780. i40e_vsi_map_rings_to_vectors(vsi);
  8781. i40e_vsi_reset_stats(vsi);
  8782. break;
  8783. default:
  8784. /* no netdev or rings for the other VSI types */
  8785. break;
  8786. }
  8787. if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
  8788. (vsi->type == I40E_VSI_VMDQ2)) {
  8789. ret = i40e_vsi_config_rss(vsi);
  8790. }
  8791. return vsi;
  8792. err_rings:
  8793. i40e_vsi_free_q_vectors(vsi);
  8794. err_msix:
  8795. if (vsi->netdev_registered) {
  8796. vsi->netdev_registered = false;
  8797. unregister_netdev(vsi->netdev);
  8798. free_netdev(vsi->netdev);
  8799. vsi->netdev = NULL;
  8800. }
  8801. err_netdev:
  8802. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8803. err_vsi:
  8804. i40e_vsi_clear(vsi);
  8805. err_alloc:
  8806. return NULL;
  8807. }
  8808. /**
  8809. * i40e_veb_get_bw_info - Query VEB BW information
  8810. * @veb: the veb to query
  8811. *
  8812. * Query the Tx scheduler BW configuration data for given VEB
  8813. **/
  8814. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  8815. {
  8816. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  8817. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  8818. struct i40e_pf *pf = veb->pf;
  8819. struct i40e_hw *hw = &pf->hw;
  8820. u32 tc_bw_max;
  8821. int ret = 0;
  8822. int i;
  8823. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  8824. &bw_data, NULL);
  8825. if (ret) {
  8826. dev_info(&pf->pdev->dev,
  8827. "query veb bw config failed, err %s aq_err %s\n",
  8828. i40e_stat_str(&pf->hw, ret),
  8829. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8830. goto out;
  8831. }
  8832. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  8833. &ets_data, NULL);
  8834. if (ret) {
  8835. dev_info(&pf->pdev->dev,
  8836. "query veb bw ets config failed, err %s aq_err %s\n",
  8837. i40e_stat_str(&pf->hw, ret),
  8838. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8839. goto out;
  8840. }
  8841. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  8842. veb->bw_max_quanta = ets_data.tc_bw_max;
  8843. veb->is_abs_credits = bw_data.absolute_credits_enable;
  8844. veb->enabled_tc = ets_data.tc_valid_bits;
  8845. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  8846. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  8847. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  8848. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  8849. veb->bw_tc_limit_credits[i] =
  8850. le16_to_cpu(bw_data.tc_bw_limits[i]);
  8851. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  8852. }
  8853. out:
  8854. return ret;
  8855. }
  8856. /**
  8857. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  8858. * @pf: board private structure
  8859. *
  8860. * On error: returns error code (negative)
  8861. * On success: returns vsi index in PF (positive)
  8862. **/
  8863. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  8864. {
  8865. int ret = -ENOENT;
  8866. struct i40e_veb *veb;
  8867. int i;
  8868. /* Need to protect the allocation of switch elements at the PF level */
  8869. mutex_lock(&pf->switch_mutex);
  8870. /* VEB list may be fragmented if VEB creation/destruction has
  8871. * been happening. We can afford to do a quick scan to look
  8872. * for any free slots in the list.
  8873. *
  8874. * find next empty veb slot, looping back around if necessary
  8875. */
  8876. i = 0;
  8877. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  8878. i++;
  8879. if (i >= I40E_MAX_VEB) {
  8880. ret = -ENOMEM;
  8881. goto err_alloc_veb; /* out of VEB slots! */
  8882. }
  8883. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  8884. if (!veb) {
  8885. ret = -ENOMEM;
  8886. goto err_alloc_veb;
  8887. }
  8888. veb->pf = pf;
  8889. veb->idx = i;
  8890. veb->enabled_tc = 1;
  8891. pf->veb[i] = veb;
  8892. ret = i;
  8893. err_alloc_veb:
  8894. mutex_unlock(&pf->switch_mutex);
  8895. return ret;
  8896. }
  8897. /**
  8898. * i40e_switch_branch_release - Delete a branch of the switch tree
  8899. * @branch: where to start deleting
  8900. *
  8901. * This uses recursion to find the tips of the branch to be
  8902. * removed, deleting until we get back to and can delete this VEB.
  8903. **/
  8904. static void i40e_switch_branch_release(struct i40e_veb *branch)
  8905. {
  8906. struct i40e_pf *pf = branch->pf;
  8907. u16 branch_seid = branch->seid;
  8908. u16 veb_idx = branch->idx;
  8909. int i;
  8910. /* release any VEBs on this VEB - RECURSION */
  8911. for (i = 0; i < I40E_MAX_VEB; i++) {
  8912. if (!pf->veb[i])
  8913. continue;
  8914. if (pf->veb[i]->uplink_seid == branch->seid)
  8915. i40e_switch_branch_release(pf->veb[i]);
  8916. }
  8917. /* Release the VSIs on this VEB, but not the owner VSI.
  8918. *
  8919. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  8920. * the VEB itself, so don't use (*branch) after this loop.
  8921. */
  8922. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8923. if (!pf->vsi[i])
  8924. continue;
  8925. if (pf->vsi[i]->uplink_seid == branch_seid &&
  8926. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8927. i40e_vsi_release(pf->vsi[i]);
  8928. }
  8929. }
  8930. /* There's one corner case where the VEB might not have been
  8931. * removed, so double check it here and remove it if needed.
  8932. * This case happens if the veb was created from the debugfs
  8933. * commands and no VSIs were added to it.
  8934. */
  8935. if (pf->veb[veb_idx])
  8936. i40e_veb_release(pf->veb[veb_idx]);
  8937. }
  8938. /**
  8939. * i40e_veb_clear - remove veb struct
  8940. * @veb: the veb to remove
  8941. **/
  8942. static void i40e_veb_clear(struct i40e_veb *veb)
  8943. {
  8944. if (!veb)
  8945. return;
  8946. if (veb->pf) {
  8947. struct i40e_pf *pf = veb->pf;
  8948. mutex_lock(&pf->switch_mutex);
  8949. if (pf->veb[veb->idx] == veb)
  8950. pf->veb[veb->idx] = NULL;
  8951. mutex_unlock(&pf->switch_mutex);
  8952. }
  8953. kfree(veb);
  8954. }
  8955. /**
  8956. * i40e_veb_release - Delete a VEB and free its resources
  8957. * @veb: the VEB being removed
  8958. **/
  8959. void i40e_veb_release(struct i40e_veb *veb)
  8960. {
  8961. struct i40e_vsi *vsi = NULL;
  8962. struct i40e_pf *pf;
  8963. int i, n = 0;
  8964. pf = veb->pf;
  8965. /* find the remaining VSI and check for extras */
  8966. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8967. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  8968. n++;
  8969. vsi = pf->vsi[i];
  8970. }
  8971. }
  8972. if (n != 1) {
  8973. dev_info(&pf->pdev->dev,
  8974. "can't remove VEB %d with %d VSIs left\n",
  8975. veb->seid, n);
  8976. return;
  8977. }
  8978. /* move the remaining VSI to uplink veb */
  8979. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  8980. if (veb->uplink_seid) {
  8981. vsi->uplink_seid = veb->uplink_seid;
  8982. if (veb->uplink_seid == pf->mac_seid)
  8983. vsi->veb_idx = I40E_NO_VEB;
  8984. else
  8985. vsi->veb_idx = veb->veb_idx;
  8986. } else {
  8987. /* floating VEB */
  8988. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  8989. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  8990. }
  8991. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  8992. i40e_veb_clear(veb);
  8993. }
  8994. /**
  8995. * i40e_add_veb - create the VEB in the switch
  8996. * @veb: the VEB to be instantiated
  8997. * @vsi: the controlling VSI
  8998. **/
  8999. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  9000. {
  9001. struct i40e_pf *pf = veb->pf;
  9002. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  9003. int ret;
  9004. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  9005. veb->enabled_tc, false,
  9006. &veb->seid, enable_stats, NULL);
  9007. /* get a VEB from the hardware */
  9008. if (ret) {
  9009. dev_info(&pf->pdev->dev,
  9010. "couldn't add VEB, err %s aq_err %s\n",
  9011. i40e_stat_str(&pf->hw, ret),
  9012. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9013. return -EPERM;
  9014. }
  9015. /* get statistics counter */
  9016. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  9017. &veb->stats_idx, NULL, NULL, NULL);
  9018. if (ret) {
  9019. dev_info(&pf->pdev->dev,
  9020. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  9021. i40e_stat_str(&pf->hw, ret),
  9022. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9023. return -EPERM;
  9024. }
  9025. ret = i40e_veb_get_bw_info(veb);
  9026. if (ret) {
  9027. dev_info(&pf->pdev->dev,
  9028. "couldn't get VEB bw info, err %s aq_err %s\n",
  9029. i40e_stat_str(&pf->hw, ret),
  9030. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9031. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9032. return -ENOENT;
  9033. }
  9034. vsi->uplink_seid = veb->seid;
  9035. vsi->veb_idx = veb->idx;
  9036. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  9037. return 0;
  9038. }
  9039. /**
  9040. * i40e_veb_setup - Set up a VEB
  9041. * @pf: board private structure
  9042. * @flags: VEB setup flags
  9043. * @uplink_seid: the switch element to link to
  9044. * @vsi_seid: the initial VSI seid
  9045. * @enabled_tc: Enabled TC bit-map
  9046. *
  9047. * This allocates the sw VEB structure and links it into the switch
  9048. * It is possible and legal for this to be a duplicate of an already
  9049. * existing VEB. It is also possible for both uplink and vsi seids
  9050. * to be zero, in order to create a floating VEB.
  9051. *
  9052. * Returns pointer to the successfully allocated VEB sw struct on
  9053. * success, otherwise returns NULL on failure.
  9054. **/
  9055. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  9056. u16 uplink_seid, u16 vsi_seid,
  9057. u8 enabled_tc)
  9058. {
  9059. struct i40e_veb *veb, *uplink_veb = NULL;
  9060. int vsi_idx, veb_idx;
  9061. int ret;
  9062. /* if one seid is 0, the other must be 0 to create a floating relay */
  9063. if ((uplink_seid == 0 || vsi_seid == 0) &&
  9064. (uplink_seid + vsi_seid != 0)) {
  9065. dev_info(&pf->pdev->dev,
  9066. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  9067. uplink_seid, vsi_seid);
  9068. return NULL;
  9069. }
  9070. /* make sure there is such a vsi and uplink */
  9071. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  9072. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  9073. break;
  9074. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  9075. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  9076. vsi_seid);
  9077. return NULL;
  9078. }
  9079. if (uplink_seid && uplink_seid != pf->mac_seid) {
  9080. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  9081. if (pf->veb[veb_idx] &&
  9082. pf->veb[veb_idx]->seid == uplink_seid) {
  9083. uplink_veb = pf->veb[veb_idx];
  9084. break;
  9085. }
  9086. }
  9087. if (!uplink_veb) {
  9088. dev_info(&pf->pdev->dev,
  9089. "uplink seid %d not found\n", uplink_seid);
  9090. return NULL;
  9091. }
  9092. }
  9093. /* get veb sw struct */
  9094. veb_idx = i40e_veb_mem_alloc(pf);
  9095. if (veb_idx < 0)
  9096. goto err_alloc;
  9097. veb = pf->veb[veb_idx];
  9098. veb->flags = flags;
  9099. veb->uplink_seid = uplink_seid;
  9100. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  9101. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  9102. /* create the VEB in the switch */
  9103. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  9104. if (ret)
  9105. goto err_veb;
  9106. if (vsi_idx == pf->lan_vsi)
  9107. pf->lan_veb = veb->idx;
  9108. return veb;
  9109. err_veb:
  9110. i40e_veb_clear(veb);
  9111. err_alloc:
  9112. return NULL;
  9113. }
  9114. /**
  9115. * i40e_setup_pf_switch_element - set PF vars based on switch type
  9116. * @pf: board private structure
  9117. * @ele: element we are building info from
  9118. * @num_reported: total number of elements
  9119. * @printconfig: should we print the contents
  9120. *
  9121. * helper function to assist in extracting a few useful SEID values.
  9122. **/
  9123. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  9124. struct i40e_aqc_switch_config_element_resp *ele,
  9125. u16 num_reported, bool printconfig)
  9126. {
  9127. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  9128. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  9129. u8 element_type = ele->element_type;
  9130. u16 seid = le16_to_cpu(ele->seid);
  9131. if (printconfig)
  9132. dev_info(&pf->pdev->dev,
  9133. "type=%d seid=%d uplink=%d downlink=%d\n",
  9134. element_type, seid, uplink_seid, downlink_seid);
  9135. switch (element_type) {
  9136. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  9137. pf->mac_seid = seid;
  9138. break;
  9139. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  9140. /* Main VEB? */
  9141. if (uplink_seid != pf->mac_seid)
  9142. break;
  9143. if (pf->lan_veb == I40E_NO_VEB) {
  9144. int v;
  9145. /* find existing or else empty VEB */
  9146. for (v = 0; v < I40E_MAX_VEB; v++) {
  9147. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  9148. pf->lan_veb = v;
  9149. break;
  9150. }
  9151. }
  9152. if (pf->lan_veb == I40E_NO_VEB) {
  9153. v = i40e_veb_mem_alloc(pf);
  9154. if (v < 0)
  9155. break;
  9156. pf->lan_veb = v;
  9157. }
  9158. }
  9159. pf->veb[pf->lan_veb]->seid = seid;
  9160. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  9161. pf->veb[pf->lan_veb]->pf = pf;
  9162. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  9163. break;
  9164. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  9165. if (num_reported != 1)
  9166. break;
  9167. /* This is immediately after a reset so we can assume this is
  9168. * the PF's VSI
  9169. */
  9170. pf->mac_seid = uplink_seid;
  9171. pf->pf_seid = downlink_seid;
  9172. pf->main_vsi_seid = seid;
  9173. if (printconfig)
  9174. dev_info(&pf->pdev->dev,
  9175. "pf_seid=%d main_vsi_seid=%d\n",
  9176. pf->pf_seid, pf->main_vsi_seid);
  9177. break;
  9178. case I40E_SWITCH_ELEMENT_TYPE_PF:
  9179. case I40E_SWITCH_ELEMENT_TYPE_VF:
  9180. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  9181. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  9182. case I40E_SWITCH_ELEMENT_TYPE_PE:
  9183. case I40E_SWITCH_ELEMENT_TYPE_PA:
  9184. /* ignore these for now */
  9185. break;
  9186. default:
  9187. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  9188. element_type, seid);
  9189. break;
  9190. }
  9191. }
  9192. /**
  9193. * i40e_fetch_switch_configuration - Get switch config from firmware
  9194. * @pf: board private structure
  9195. * @printconfig: should we print the contents
  9196. *
  9197. * Get the current switch configuration from the device and
  9198. * extract a few useful SEID values.
  9199. **/
  9200. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  9201. {
  9202. struct i40e_aqc_get_switch_config_resp *sw_config;
  9203. u16 next_seid = 0;
  9204. int ret = 0;
  9205. u8 *aq_buf;
  9206. int i;
  9207. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  9208. if (!aq_buf)
  9209. return -ENOMEM;
  9210. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  9211. do {
  9212. u16 num_reported, num_total;
  9213. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  9214. I40E_AQ_LARGE_BUF,
  9215. &next_seid, NULL);
  9216. if (ret) {
  9217. dev_info(&pf->pdev->dev,
  9218. "get switch config failed err %s aq_err %s\n",
  9219. i40e_stat_str(&pf->hw, ret),
  9220. i40e_aq_str(&pf->hw,
  9221. pf->hw.aq.asq_last_status));
  9222. kfree(aq_buf);
  9223. return -ENOENT;
  9224. }
  9225. num_reported = le16_to_cpu(sw_config->header.num_reported);
  9226. num_total = le16_to_cpu(sw_config->header.num_total);
  9227. if (printconfig)
  9228. dev_info(&pf->pdev->dev,
  9229. "header: %d reported %d total\n",
  9230. num_reported, num_total);
  9231. for (i = 0; i < num_reported; i++) {
  9232. struct i40e_aqc_switch_config_element_resp *ele =
  9233. &sw_config->element[i];
  9234. i40e_setup_pf_switch_element(pf, ele, num_reported,
  9235. printconfig);
  9236. }
  9237. } while (next_seid != 0);
  9238. kfree(aq_buf);
  9239. return ret;
  9240. }
  9241. /**
  9242. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  9243. * @pf: board private structure
  9244. * @reinit: if the Main VSI needs to re-initialized.
  9245. *
  9246. * Returns 0 on success, negative value on failure
  9247. **/
  9248. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  9249. {
  9250. u16 flags = 0;
  9251. int ret;
  9252. /* find out what's out there already */
  9253. ret = i40e_fetch_switch_configuration(pf, false);
  9254. if (ret) {
  9255. dev_info(&pf->pdev->dev,
  9256. "couldn't fetch switch config, err %s aq_err %s\n",
  9257. i40e_stat_str(&pf->hw, ret),
  9258. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9259. return ret;
  9260. }
  9261. i40e_pf_reset_stats(pf);
  9262. /* set the switch config bit for the whole device to
  9263. * support limited promisc or true promisc
  9264. * when user requests promisc. The default is limited
  9265. * promisc.
  9266. */
  9267. if ((pf->hw.pf_id == 0) &&
  9268. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
  9269. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9270. if (pf->hw.pf_id == 0) {
  9271. u16 valid_flags;
  9272. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9273. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
  9274. NULL);
  9275. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  9276. dev_info(&pf->pdev->dev,
  9277. "couldn't set switch config bits, err %s aq_err %s\n",
  9278. i40e_stat_str(&pf->hw, ret),
  9279. i40e_aq_str(&pf->hw,
  9280. pf->hw.aq.asq_last_status));
  9281. /* not a fatal problem, just keep going */
  9282. }
  9283. }
  9284. /* first time setup */
  9285. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  9286. struct i40e_vsi *vsi = NULL;
  9287. u16 uplink_seid;
  9288. /* Set up the PF VSI associated with the PF's main VSI
  9289. * that is already in the HW switch
  9290. */
  9291. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  9292. uplink_seid = pf->veb[pf->lan_veb]->seid;
  9293. else
  9294. uplink_seid = pf->mac_seid;
  9295. if (pf->lan_vsi == I40E_NO_VSI)
  9296. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  9297. else if (reinit)
  9298. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  9299. if (!vsi) {
  9300. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  9301. i40e_fdir_teardown(pf);
  9302. return -EAGAIN;
  9303. }
  9304. } else {
  9305. /* force a reset of TC and queue layout configurations */
  9306. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  9307. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  9308. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  9309. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  9310. }
  9311. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  9312. i40e_fdir_sb_setup(pf);
  9313. /* Setup static PF queue filter control settings */
  9314. ret = i40e_setup_pf_filter_control(pf);
  9315. if (ret) {
  9316. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  9317. ret);
  9318. /* Failure here should not stop continuing other steps */
  9319. }
  9320. /* enable RSS in the HW, even for only one queue, as the stack can use
  9321. * the hash
  9322. */
  9323. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  9324. i40e_pf_config_rss(pf);
  9325. /* fill in link information and enable LSE reporting */
  9326. i40e_link_event(pf);
  9327. /* Initialize user-specific link properties */
  9328. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  9329. I40E_AQ_AN_COMPLETED) ? true : false);
  9330. i40e_ptp_init(pf);
  9331. return ret;
  9332. }
  9333. /**
  9334. * i40e_determine_queue_usage - Work out queue distribution
  9335. * @pf: board private structure
  9336. **/
  9337. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  9338. {
  9339. int queues_left;
  9340. pf->num_lan_qps = 0;
  9341. /* Find the max queues to be put into basic use. We'll always be
  9342. * using TC0, whether or not DCB is running, and TC0 will get the
  9343. * big RSS set.
  9344. */
  9345. queues_left = pf->hw.func_caps.num_tx_qp;
  9346. if ((queues_left == 1) ||
  9347. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  9348. /* one qp for PF, no queues for anything else */
  9349. queues_left = 0;
  9350. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9351. /* make sure all the fancies are disabled */
  9352. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9353. I40E_FLAG_IWARP_ENABLED |
  9354. I40E_FLAG_FD_SB_ENABLED |
  9355. I40E_FLAG_FD_ATR_ENABLED |
  9356. I40E_FLAG_DCB_CAPABLE |
  9357. I40E_FLAG_DCB_ENABLED |
  9358. I40E_FLAG_SRIOV_ENABLED |
  9359. I40E_FLAG_VMDQ_ENABLED);
  9360. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  9361. I40E_FLAG_FD_SB_ENABLED |
  9362. I40E_FLAG_FD_ATR_ENABLED |
  9363. I40E_FLAG_DCB_CAPABLE))) {
  9364. /* one qp for PF */
  9365. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9366. queues_left -= pf->num_lan_qps;
  9367. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9368. I40E_FLAG_IWARP_ENABLED |
  9369. I40E_FLAG_FD_SB_ENABLED |
  9370. I40E_FLAG_FD_ATR_ENABLED |
  9371. I40E_FLAG_DCB_ENABLED |
  9372. I40E_FLAG_VMDQ_ENABLED);
  9373. } else {
  9374. /* Not enough queues for all TCs */
  9375. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  9376. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  9377. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  9378. I40E_FLAG_DCB_ENABLED);
  9379. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  9380. }
  9381. pf->num_lan_qps = max_t(int, pf->rss_size_max,
  9382. num_online_cpus());
  9383. pf->num_lan_qps = min_t(int, pf->num_lan_qps,
  9384. pf->hw.func_caps.num_tx_qp);
  9385. queues_left -= pf->num_lan_qps;
  9386. }
  9387. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9388. if (queues_left > 1) {
  9389. queues_left -= 1; /* save 1 queue for FD */
  9390. } else {
  9391. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9392. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  9393. }
  9394. }
  9395. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9396. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  9397. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  9398. (queues_left / pf->num_vf_qps));
  9399. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  9400. }
  9401. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9402. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  9403. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  9404. (queues_left / pf->num_vmdq_qps));
  9405. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  9406. }
  9407. pf->queues_left = queues_left;
  9408. dev_dbg(&pf->pdev->dev,
  9409. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  9410. pf->hw.func_caps.num_tx_qp,
  9411. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  9412. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  9413. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  9414. queues_left);
  9415. }
  9416. /**
  9417. * i40e_setup_pf_filter_control - Setup PF static filter control
  9418. * @pf: PF to be setup
  9419. *
  9420. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  9421. * settings. If PE/FCoE are enabled then it will also set the per PF
  9422. * based filter sizes required for them. It also enables Flow director,
  9423. * ethertype and macvlan type filter settings for the pf.
  9424. *
  9425. * Returns 0 on success, negative on failure
  9426. **/
  9427. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  9428. {
  9429. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  9430. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  9431. /* Flow Director is enabled */
  9432. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  9433. settings->enable_fdir = true;
  9434. /* Ethtype and MACVLAN filters enabled for PF */
  9435. settings->enable_ethtype = true;
  9436. settings->enable_macvlan = true;
  9437. if (i40e_set_filter_control(&pf->hw, settings))
  9438. return -ENOENT;
  9439. return 0;
  9440. }
  9441. #define INFO_STRING_LEN 255
  9442. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  9443. static void i40e_print_features(struct i40e_pf *pf)
  9444. {
  9445. struct i40e_hw *hw = &pf->hw;
  9446. char *buf;
  9447. int i;
  9448. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  9449. if (!buf)
  9450. return;
  9451. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  9452. #ifdef CONFIG_PCI_IOV
  9453. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  9454. #endif
  9455. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  9456. pf->hw.func_caps.num_vsis,
  9457. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  9458. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  9459. i += snprintf(&buf[i], REMAIN(i), " RSS");
  9460. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  9461. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  9462. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9463. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  9464. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  9465. }
  9466. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  9467. i += snprintf(&buf[i], REMAIN(i), " DCB");
  9468. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  9469. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  9470. if (pf->flags & I40E_FLAG_PTP)
  9471. i += snprintf(&buf[i], REMAIN(i), " PTP");
  9472. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  9473. i += snprintf(&buf[i], REMAIN(i), " VEB");
  9474. else
  9475. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  9476. dev_info(&pf->pdev->dev, "%s\n", buf);
  9477. kfree(buf);
  9478. WARN_ON(i > INFO_STRING_LEN);
  9479. }
  9480. /**
  9481. * i40e_get_platform_mac_addr - get platform-specific MAC address
  9482. *
  9483. * @pdev: PCI device information struct
  9484. * @pf: board private structure
  9485. *
  9486. * Look up the MAC address in Open Firmware on systems that support it,
  9487. * and use IDPROM on SPARC if no OF address is found. On return, the
  9488. * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
  9489. * has been selected.
  9490. **/
  9491. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  9492. {
  9493. pf->flags &= ~I40E_FLAG_PF_MAC;
  9494. if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  9495. pf->flags |= I40E_FLAG_PF_MAC;
  9496. }
  9497. /**
  9498. * i40e_probe - Device initialization routine
  9499. * @pdev: PCI device information struct
  9500. * @ent: entry in i40e_pci_tbl
  9501. *
  9502. * i40e_probe initializes a PF identified by a pci_dev structure.
  9503. * The OS initialization, configuring of the PF private structure,
  9504. * and a hardware reset occur.
  9505. *
  9506. * Returns 0 on success, negative on failure
  9507. **/
  9508. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  9509. {
  9510. struct i40e_aq_get_phy_abilities_resp abilities;
  9511. struct i40e_pf *pf;
  9512. struct i40e_hw *hw;
  9513. static u16 pfs_found;
  9514. u16 wol_nvm_bits;
  9515. u16 link_status;
  9516. int err;
  9517. u32 val;
  9518. u32 i;
  9519. u8 set_fc_aq_fail;
  9520. err = pci_enable_device_mem(pdev);
  9521. if (err)
  9522. return err;
  9523. /* set up for high or low dma */
  9524. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  9525. if (err) {
  9526. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  9527. if (err) {
  9528. dev_err(&pdev->dev,
  9529. "DMA configuration failed: 0x%x\n", err);
  9530. goto err_dma;
  9531. }
  9532. }
  9533. /* set up pci connections */
  9534. err = pci_request_mem_regions(pdev, i40e_driver_name);
  9535. if (err) {
  9536. dev_info(&pdev->dev,
  9537. "pci_request_selected_regions failed %d\n", err);
  9538. goto err_pci_reg;
  9539. }
  9540. pci_enable_pcie_error_reporting(pdev);
  9541. pci_set_master(pdev);
  9542. /* Now that we have a PCI connection, we need to do the
  9543. * low level device setup. This is primarily setting up
  9544. * the Admin Queue structures and then querying for the
  9545. * device's current profile information.
  9546. */
  9547. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  9548. if (!pf) {
  9549. err = -ENOMEM;
  9550. goto err_pf_alloc;
  9551. }
  9552. pf->next_vsi = 0;
  9553. pf->pdev = pdev;
  9554. set_bit(__I40E_DOWN, &pf->state);
  9555. hw = &pf->hw;
  9556. hw->back = pf;
  9557. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  9558. I40E_MAX_CSR_SPACE);
  9559. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  9560. if (!hw->hw_addr) {
  9561. err = -EIO;
  9562. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  9563. (unsigned int)pci_resource_start(pdev, 0),
  9564. pf->ioremap_len, err);
  9565. goto err_ioremap;
  9566. }
  9567. hw->vendor_id = pdev->vendor;
  9568. hw->device_id = pdev->device;
  9569. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  9570. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  9571. hw->subsystem_device_id = pdev->subsystem_device;
  9572. hw->bus.device = PCI_SLOT(pdev->devfn);
  9573. hw->bus.func = PCI_FUNC(pdev->devfn);
  9574. hw->bus.bus_id = pdev->bus->number;
  9575. pf->instance = pfs_found;
  9576. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  9577. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  9578. /* set up the locks for the AQ, do this only once in probe
  9579. * and destroy them only once in remove
  9580. */
  9581. mutex_init(&hw->aq.asq_mutex);
  9582. mutex_init(&hw->aq.arq_mutex);
  9583. pf->msg_enable = netif_msg_init(debug,
  9584. NETIF_MSG_DRV |
  9585. NETIF_MSG_PROBE |
  9586. NETIF_MSG_LINK);
  9587. if (debug < -1)
  9588. pf->hw.debug_mask = debug;
  9589. /* do a special CORER for clearing PXE mode once at init */
  9590. if (hw->revision_id == 0 &&
  9591. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  9592. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  9593. i40e_flush(hw);
  9594. msleep(200);
  9595. pf->corer_count++;
  9596. i40e_clear_pxe_mode(hw);
  9597. }
  9598. /* Reset here to make sure all is clean and to define PF 'n' */
  9599. i40e_clear_hw(hw);
  9600. err = i40e_pf_reset(hw);
  9601. if (err) {
  9602. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  9603. goto err_pf_reset;
  9604. }
  9605. pf->pfr_count++;
  9606. hw->aq.num_arq_entries = I40E_AQ_LEN;
  9607. hw->aq.num_asq_entries = I40E_AQ_LEN;
  9608. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9609. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9610. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  9611. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  9612. "%s-%s:misc",
  9613. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  9614. err = i40e_init_shared_code(hw);
  9615. if (err) {
  9616. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  9617. err);
  9618. goto err_pf_reset;
  9619. }
  9620. /* set up a default setting for link flow control */
  9621. pf->hw.fc.requested_mode = I40E_FC_NONE;
  9622. err = i40e_init_adminq(hw);
  9623. if (err) {
  9624. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  9625. dev_info(&pdev->dev,
  9626. "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");
  9627. else
  9628. dev_info(&pdev->dev,
  9629. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  9630. goto err_pf_reset;
  9631. }
  9632. /* provide nvm, fw, api versions */
  9633. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  9634. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  9635. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  9636. i40e_nvm_version_str(hw));
  9637. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  9638. hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
  9639. dev_info(&pdev->dev,
  9640. "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");
  9641. else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
  9642. hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
  9643. dev_info(&pdev->dev,
  9644. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  9645. i40e_verify_eeprom(pf);
  9646. /* Rev 0 hardware was never productized */
  9647. if (hw->revision_id < 1)
  9648. 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");
  9649. i40e_clear_pxe_mode(hw);
  9650. err = i40e_get_capabilities(pf);
  9651. if (err)
  9652. goto err_adminq_setup;
  9653. err = i40e_sw_init(pf);
  9654. if (err) {
  9655. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  9656. goto err_sw_init;
  9657. }
  9658. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  9659. hw->func_caps.num_rx_qp, 0, 0);
  9660. if (err) {
  9661. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  9662. goto err_init_lan_hmc;
  9663. }
  9664. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  9665. if (err) {
  9666. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  9667. err = -ENOENT;
  9668. goto err_configure_lan_hmc;
  9669. }
  9670. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  9671. * Ignore error return codes because if it was already disabled via
  9672. * hardware settings this will fail
  9673. */
  9674. if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
  9675. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  9676. i40e_aq_stop_lldp(hw, true, NULL);
  9677. }
  9678. i40e_get_mac_addr(hw, hw->mac.addr);
  9679. /* allow a platform config to override the HW addr */
  9680. i40e_get_platform_mac_addr(pdev, pf);
  9681. if (!is_valid_ether_addr(hw->mac.addr)) {
  9682. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  9683. err = -EIO;
  9684. goto err_mac_addr;
  9685. }
  9686. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  9687. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  9688. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  9689. if (is_valid_ether_addr(hw->mac.port_addr))
  9690. pf->flags |= I40E_FLAG_PORT_ID_VALID;
  9691. pci_set_drvdata(pdev, pf);
  9692. pci_save_state(pdev);
  9693. #ifdef CONFIG_I40E_DCB
  9694. err = i40e_init_pf_dcb(pf);
  9695. if (err) {
  9696. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  9697. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  9698. /* Continue without DCB enabled */
  9699. }
  9700. #endif /* CONFIG_I40E_DCB */
  9701. /* set up periodic task facility */
  9702. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  9703. pf->service_timer_period = HZ;
  9704. INIT_WORK(&pf->service_task, i40e_service_task);
  9705. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  9706. pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
  9707. /* NVM bit on means WoL disabled for the port */
  9708. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  9709. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  9710. pf->wol_en = false;
  9711. else
  9712. pf->wol_en = true;
  9713. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  9714. /* set up the main switch operations */
  9715. i40e_determine_queue_usage(pf);
  9716. err = i40e_init_interrupt_scheme(pf);
  9717. if (err)
  9718. goto err_switch_setup;
  9719. /* The number of VSIs reported by the FW is the minimum guaranteed
  9720. * to us; HW supports far more and we share the remaining pool with
  9721. * the other PFs. We allocate space for more than the guarantee with
  9722. * the understanding that we might not get them all later.
  9723. */
  9724. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  9725. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  9726. else
  9727. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  9728. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  9729. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  9730. GFP_KERNEL);
  9731. if (!pf->vsi) {
  9732. err = -ENOMEM;
  9733. goto err_switch_setup;
  9734. }
  9735. #ifdef CONFIG_PCI_IOV
  9736. /* prep for VF support */
  9737. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9738. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9739. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9740. if (pci_num_vf(pdev))
  9741. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  9742. }
  9743. #endif
  9744. err = i40e_setup_pf_switch(pf, false);
  9745. if (err) {
  9746. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  9747. goto err_vsis;
  9748. }
  9749. /* Make sure flow control is set according to current settings */
  9750. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  9751. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  9752. dev_dbg(&pf->pdev->dev,
  9753. "Set fc with err %s aq_err %s on get_phy_cap\n",
  9754. i40e_stat_str(hw, err),
  9755. i40e_aq_str(hw, hw->aq.asq_last_status));
  9756. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  9757. dev_dbg(&pf->pdev->dev,
  9758. "Set fc with err %s aq_err %s on set_phy_config\n",
  9759. i40e_stat_str(hw, err),
  9760. i40e_aq_str(hw, hw->aq.asq_last_status));
  9761. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  9762. dev_dbg(&pf->pdev->dev,
  9763. "Set fc with err %s aq_err %s on get_link_info\n",
  9764. i40e_stat_str(hw, err),
  9765. i40e_aq_str(hw, hw->aq.asq_last_status));
  9766. /* if FDIR VSI was set up, start it now */
  9767. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9768. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  9769. i40e_vsi_open(pf->vsi[i]);
  9770. break;
  9771. }
  9772. }
  9773. /* The driver only wants link up/down and module qualification
  9774. * reports from firmware. Note the negative logic.
  9775. */
  9776. err = i40e_aq_set_phy_int_mask(&pf->hw,
  9777. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  9778. I40E_AQ_EVENT_MEDIA_NA |
  9779. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  9780. if (err)
  9781. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  9782. i40e_stat_str(&pf->hw, err),
  9783. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9784. /* Reconfigure hardware for allowing smaller MSS in the case
  9785. * of TSO, so that we avoid the MDD being fired and causing
  9786. * a reset in the case of small MSS+TSO.
  9787. */
  9788. val = rd32(hw, I40E_REG_MSS);
  9789. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  9790. val &= ~I40E_REG_MSS_MIN_MASK;
  9791. val |= I40E_64BYTE_MSS;
  9792. wr32(hw, I40E_REG_MSS, val);
  9793. }
  9794. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  9795. msleep(75);
  9796. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  9797. if (err)
  9798. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  9799. i40e_stat_str(&pf->hw, err),
  9800. i40e_aq_str(&pf->hw,
  9801. pf->hw.aq.asq_last_status));
  9802. }
  9803. /* The main driver is (mostly) up and happy. We need to set this state
  9804. * before setting up the misc vector or we get a race and the vector
  9805. * ends up disabled forever.
  9806. */
  9807. clear_bit(__I40E_DOWN, &pf->state);
  9808. /* In case of MSIX we are going to setup the misc vector right here
  9809. * to handle admin queue events etc. In case of legacy and MSI
  9810. * the misc functionality and queue processing is combined in
  9811. * the same vector and that gets setup at open.
  9812. */
  9813. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9814. err = i40e_setup_misc_vector(pf);
  9815. if (err) {
  9816. dev_info(&pdev->dev,
  9817. "setup of misc vector failed: %d\n", err);
  9818. goto err_vsis;
  9819. }
  9820. }
  9821. #ifdef CONFIG_PCI_IOV
  9822. /* prep for VF support */
  9823. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9824. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9825. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9826. /* disable link interrupts for VFs */
  9827. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  9828. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  9829. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  9830. i40e_flush(hw);
  9831. if (pci_num_vf(pdev)) {
  9832. dev_info(&pdev->dev,
  9833. "Active VFs found, allocating resources.\n");
  9834. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  9835. if (err)
  9836. dev_info(&pdev->dev,
  9837. "Error %d allocating resources for existing VFs\n",
  9838. err);
  9839. }
  9840. }
  9841. #endif /* CONFIG_PCI_IOV */
  9842. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9843. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  9844. pf->num_iwarp_msix,
  9845. I40E_IWARP_IRQ_PILE_ID);
  9846. if (pf->iwarp_base_vector < 0) {
  9847. dev_info(&pdev->dev,
  9848. "failed to get tracking for %d vectors for IWARP err=%d\n",
  9849. pf->num_iwarp_msix, pf->iwarp_base_vector);
  9850. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9851. }
  9852. }
  9853. i40e_dbg_pf_init(pf);
  9854. /* tell the firmware that we're starting */
  9855. i40e_send_version(pf);
  9856. /* since everything's happy, start the service_task timer */
  9857. mod_timer(&pf->service_timer,
  9858. round_jiffies(jiffies + pf->service_timer_period));
  9859. /* add this PF to client device list and launch a client service task */
  9860. err = i40e_lan_add_device(pf);
  9861. if (err)
  9862. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  9863. err);
  9864. #define PCI_SPEED_SIZE 8
  9865. #define PCI_WIDTH_SIZE 8
  9866. /* Devices on the IOSF bus do not have this information
  9867. * and will report PCI Gen 1 x 1 by default so don't bother
  9868. * checking them.
  9869. */
  9870. if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
  9871. char speed[PCI_SPEED_SIZE] = "Unknown";
  9872. char width[PCI_WIDTH_SIZE] = "Unknown";
  9873. /* Get the negotiated link width and speed from PCI config
  9874. * space
  9875. */
  9876. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  9877. &link_status);
  9878. i40e_set_pci_config_data(hw, link_status);
  9879. switch (hw->bus.speed) {
  9880. case i40e_bus_speed_8000:
  9881. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  9882. case i40e_bus_speed_5000:
  9883. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  9884. case i40e_bus_speed_2500:
  9885. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  9886. default:
  9887. break;
  9888. }
  9889. switch (hw->bus.width) {
  9890. case i40e_bus_width_pcie_x8:
  9891. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  9892. case i40e_bus_width_pcie_x4:
  9893. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  9894. case i40e_bus_width_pcie_x2:
  9895. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  9896. case i40e_bus_width_pcie_x1:
  9897. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  9898. default:
  9899. break;
  9900. }
  9901. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  9902. speed, width);
  9903. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  9904. hw->bus.speed < i40e_bus_speed_8000) {
  9905. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  9906. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  9907. }
  9908. }
  9909. /* get the requested speeds from the fw */
  9910. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  9911. if (err)
  9912. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  9913. i40e_stat_str(&pf->hw, err),
  9914. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9915. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  9916. /* get the supported phy types from the fw */
  9917. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  9918. if (err)
  9919. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  9920. i40e_stat_str(&pf->hw, err),
  9921. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9922. /* Add a filter to drop all Flow control frames from any VSI from being
  9923. * transmitted. By doing so we stop a malicious VF from sending out
  9924. * PAUSE or PFC frames and potentially controlling traffic for other
  9925. * PF/VF VSIs.
  9926. * The FW can still send Flow control frames if enabled.
  9927. */
  9928. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  9929. pf->main_vsi_seid);
  9930. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  9931. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  9932. pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
  9933. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  9934. pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
  9935. /* print a string summarizing features */
  9936. i40e_print_features(pf);
  9937. return 0;
  9938. /* Unwind what we've done if something failed in the setup */
  9939. err_vsis:
  9940. set_bit(__I40E_DOWN, &pf->state);
  9941. i40e_clear_interrupt_scheme(pf);
  9942. kfree(pf->vsi);
  9943. err_switch_setup:
  9944. i40e_reset_interrupt_capability(pf);
  9945. del_timer_sync(&pf->service_timer);
  9946. err_mac_addr:
  9947. err_configure_lan_hmc:
  9948. (void)i40e_shutdown_lan_hmc(hw);
  9949. err_init_lan_hmc:
  9950. kfree(pf->qp_pile);
  9951. err_sw_init:
  9952. err_adminq_setup:
  9953. err_pf_reset:
  9954. iounmap(hw->hw_addr);
  9955. err_ioremap:
  9956. kfree(pf);
  9957. err_pf_alloc:
  9958. pci_disable_pcie_error_reporting(pdev);
  9959. pci_release_mem_regions(pdev);
  9960. err_pci_reg:
  9961. err_dma:
  9962. pci_disable_device(pdev);
  9963. return err;
  9964. }
  9965. /**
  9966. * i40e_remove - Device removal routine
  9967. * @pdev: PCI device information struct
  9968. *
  9969. * i40e_remove is called by the PCI subsystem to alert the driver
  9970. * that is should release a PCI device. This could be caused by a
  9971. * Hot-Plug event, or because the driver is going to be removed from
  9972. * memory.
  9973. **/
  9974. static void i40e_remove(struct pci_dev *pdev)
  9975. {
  9976. struct i40e_pf *pf = pci_get_drvdata(pdev);
  9977. struct i40e_hw *hw = &pf->hw;
  9978. i40e_status ret_code;
  9979. int i;
  9980. i40e_dbg_pf_exit(pf);
  9981. i40e_ptp_stop(pf);
  9982. /* Disable RSS in hw */
  9983. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  9984. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  9985. /* no more scheduling of any task */
  9986. set_bit(__I40E_SUSPENDED, &pf->state);
  9987. set_bit(__I40E_DOWN, &pf->state);
  9988. if (pf->service_timer.data)
  9989. del_timer_sync(&pf->service_timer);
  9990. if (pf->service_task.func)
  9991. cancel_work_sync(&pf->service_task);
  9992. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  9993. i40e_free_vfs(pf);
  9994. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  9995. }
  9996. i40e_fdir_teardown(pf);
  9997. /* If there is a switch structure or any orphans, remove them.
  9998. * This will leave only the PF's VSI remaining.
  9999. */
  10000. for (i = 0; i < I40E_MAX_VEB; i++) {
  10001. if (!pf->veb[i])
  10002. continue;
  10003. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  10004. pf->veb[i]->uplink_seid == 0)
  10005. i40e_switch_branch_release(pf->veb[i]);
  10006. }
  10007. /* Now we can shutdown the PF's VSI, just before we kill
  10008. * adminq and hmc.
  10009. */
  10010. if (pf->vsi[pf->lan_vsi])
  10011. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  10012. /* remove attached clients */
  10013. ret_code = i40e_lan_del_device(pf);
  10014. if (ret_code) {
  10015. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  10016. ret_code);
  10017. }
  10018. /* shutdown and destroy the HMC */
  10019. if (hw->hmc.hmc_obj) {
  10020. ret_code = i40e_shutdown_lan_hmc(hw);
  10021. if (ret_code)
  10022. dev_warn(&pdev->dev,
  10023. "Failed to destroy the HMC resources: %d\n",
  10024. ret_code);
  10025. }
  10026. /* shutdown the adminq */
  10027. i40e_shutdown_adminq(hw);
  10028. /* destroy the locks only once, here */
  10029. mutex_destroy(&hw->aq.arq_mutex);
  10030. mutex_destroy(&hw->aq.asq_mutex);
  10031. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  10032. i40e_clear_interrupt_scheme(pf);
  10033. for (i = 0; i < pf->num_alloc_vsi; i++) {
  10034. if (pf->vsi[i]) {
  10035. i40e_vsi_clear_rings(pf->vsi[i]);
  10036. i40e_vsi_clear(pf->vsi[i]);
  10037. pf->vsi[i] = NULL;
  10038. }
  10039. }
  10040. for (i = 0; i < I40E_MAX_VEB; i++) {
  10041. kfree(pf->veb[i]);
  10042. pf->veb[i] = NULL;
  10043. }
  10044. kfree(pf->qp_pile);
  10045. kfree(pf->vsi);
  10046. iounmap(hw->hw_addr);
  10047. kfree(pf);
  10048. pci_release_mem_regions(pdev);
  10049. pci_disable_pcie_error_reporting(pdev);
  10050. pci_disable_device(pdev);
  10051. }
  10052. /**
  10053. * i40e_pci_error_detected - warning that something funky happened in PCI land
  10054. * @pdev: PCI device information struct
  10055. *
  10056. * Called to warn that something happened and the error handling steps
  10057. * are in progress. Allows the driver to quiesce things, be ready for
  10058. * remediation.
  10059. **/
  10060. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  10061. enum pci_channel_state error)
  10062. {
  10063. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10064. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  10065. if (!pf) {
  10066. dev_info(&pdev->dev,
  10067. "Cannot recover - error happened during device probe\n");
  10068. return PCI_ERS_RESULT_DISCONNECT;
  10069. }
  10070. /* shutdown all operations */
  10071. if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
  10072. rtnl_lock();
  10073. i40e_prep_for_reset(pf);
  10074. rtnl_unlock();
  10075. }
  10076. /* Request a slot reset */
  10077. return PCI_ERS_RESULT_NEED_RESET;
  10078. }
  10079. /**
  10080. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  10081. * @pdev: PCI device information struct
  10082. *
  10083. * Called to find if the driver can work with the device now that
  10084. * the pci slot has been reset. If a basic connection seems good
  10085. * (registers are readable and have sane content) then return a
  10086. * happy little PCI_ERS_RESULT_xxx.
  10087. **/
  10088. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  10089. {
  10090. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10091. pci_ers_result_t result;
  10092. int err;
  10093. u32 reg;
  10094. dev_dbg(&pdev->dev, "%s\n", __func__);
  10095. if (pci_enable_device_mem(pdev)) {
  10096. dev_info(&pdev->dev,
  10097. "Cannot re-enable PCI device after reset.\n");
  10098. result = PCI_ERS_RESULT_DISCONNECT;
  10099. } else {
  10100. pci_set_master(pdev);
  10101. pci_restore_state(pdev);
  10102. pci_save_state(pdev);
  10103. pci_wake_from_d3(pdev, false);
  10104. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  10105. if (reg == 0)
  10106. result = PCI_ERS_RESULT_RECOVERED;
  10107. else
  10108. result = PCI_ERS_RESULT_DISCONNECT;
  10109. }
  10110. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  10111. if (err) {
  10112. dev_info(&pdev->dev,
  10113. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  10114. err);
  10115. /* non-fatal, continue */
  10116. }
  10117. return result;
  10118. }
  10119. /**
  10120. * i40e_pci_error_resume - restart operations after PCI error recovery
  10121. * @pdev: PCI device information struct
  10122. *
  10123. * Called to allow the driver to bring things back up after PCI error
  10124. * and/or reset recovery has finished.
  10125. **/
  10126. static void i40e_pci_error_resume(struct pci_dev *pdev)
  10127. {
  10128. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10129. dev_dbg(&pdev->dev, "%s\n", __func__);
  10130. if (test_bit(__I40E_SUSPENDED, &pf->state))
  10131. return;
  10132. rtnl_lock();
  10133. i40e_handle_reset_warning(pf);
  10134. rtnl_unlock();
  10135. }
  10136. /**
  10137. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  10138. * using the mac_address_write admin q function
  10139. * @pf: pointer to i40e_pf struct
  10140. **/
  10141. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  10142. {
  10143. struct i40e_hw *hw = &pf->hw;
  10144. i40e_status ret;
  10145. u8 mac_addr[6];
  10146. u16 flags = 0;
  10147. /* Get current MAC address in case it's an LAA */
  10148. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  10149. ether_addr_copy(mac_addr,
  10150. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  10151. } else {
  10152. dev_err(&pf->pdev->dev,
  10153. "Failed to retrieve MAC address; using default\n");
  10154. ether_addr_copy(mac_addr, hw->mac.addr);
  10155. }
  10156. /* The FW expects the mac address write cmd to first be called with
  10157. * one of these flags before calling it again with the multicast
  10158. * enable flags.
  10159. */
  10160. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  10161. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  10162. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  10163. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10164. if (ret) {
  10165. dev_err(&pf->pdev->dev,
  10166. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  10167. return;
  10168. }
  10169. flags = I40E_AQC_MC_MAG_EN
  10170. | I40E_AQC_WOL_PRESERVE_ON_PFR
  10171. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  10172. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10173. if (ret)
  10174. dev_err(&pf->pdev->dev,
  10175. "Failed to enable Multicast Magic Packet wake up\n");
  10176. }
  10177. /**
  10178. * i40e_shutdown - PCI callback for shutting down
  10179. * @pdev: PCI device information struct
  10180. **/
  10181. static void i40e_shutdown(struct pci_dev *pdev)
  10182. {
  10183. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10184. struct i40e_hw *hw = &pf->hw;
  10185. set_bit(__I40E_SUSPENDED, &pf->state);
  10186. set_bit(__I40E_DOWN, &pf->state);
  10187. rtnl_lock();
  10188. i40e_prep_for_reset(pf);
  10189. rtnl_unlock();
  10190. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10191. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10192. del_timer_sync(&pf->service_timer);
  10193. cancel_work_sync(&pf->service_task);
  10194. i40e_fdir_teardown(pf);
  10195. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10196. i40e_enable_mc_magic_wake(pf);
  10197. rtnl_lock();
  10198. i40e_prep_for_reset(pf);
  10199. rtnl_unlock();
  10200. wr32(hw, I40E_PFPM_APM,
  10201. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10202. wr32(hw, I40E_PFPM_WUFC,
  10203. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10204. i40e_clear_interrupt_scheme(pf);
  10205. if (system_state == SYSTEM_POWER_OFF) {
  10206. pci_wake_from_d3(pdev, pf->wol_en);
  10207. pci_set_power_state(pdev, PCI_D3hot);
  10208. }
  10209. }
  10210. #ifdef CONFIG_PM
  10211. /**
  10212. * i40e_suspend - PCI callback for moving to D3
  10213. * @pdev: PCI device information struct
  10214. **/
  10215. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  10216. {
  10217. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10218. struct i40e_hw *hw = &pf->hw;
  10219. int retval = 0;
  10220. set_bit(__I40E_SUSPENDED, &pf->state);
  10221. set_bit(__I40E_DOWN, &pf->state);
  10222. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10223. i40e_enable_mc_magic_wake(pf);
  10224. rtnl_lock();
  10225. i40e_prep_for_reset(pf);
  10226. rtnl_unlock();
  10227. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10228. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10229. i40e_stop_misc_vector(pf);
  10230. retval = pci_save_state(pdev);
  10231. if (retval)
  10232. return retval;
  10233. pci_wake_from_d3(pdev, pf->wol_en);
  10234. pci_set_power_state(pdev, PCI_D3hot);
  10235. return retval;
  10236. }
  10237. /**
  10238. * i40e_resume - PCI callback for waking up from D3
  10239. * @pdev: PCI device information struct
  10240. **/
  10241. static int i40e_resume(struct pci_dev *pdev)
  10242. {
  10243. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10244. u32 err;
  10245. pci_set_power_state(pdev, PCI_D0);
  10246. pci_restore_state(pdev);
  10247. /* pci_restore_state() clears dev->state_saves, so
  10248. * call pci_save_state() again to restore it.
  10249. */
  10250. pci_save_state(pdev);
  10251. err = pci_enable_device_mem(pdev);
  10252. if (err) {
  10253. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  10254. return err;
  10255. }
  10256. pci_set_master(pdev);
  10257. /* no wakeup events while running */
  10258. pci_wake_from_d3(pdev, false);
  10259. /* handling the reset will rebuild the device state */
  10260. if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
  10261. clear_bit(__I40E_DOWN, &pf->state);
  10262. rtnl_lock();
  10263. i40e_reset_and_rebuild(pf, false);
  10264. rtnl_unlock();
  10265. }
  10266. return 0;
  10267. }
  10268. #endif
  10269. static const struct pci_error_handlers i40e_err_handler = {
  10270. .error_detected = i40e_pci_error_detected,
  10271. .slot_reset = i40e_pci_error_slot_reset,
  10272. .resume = i40e_pci_error_resume,
  10273. };
  10274. static struct pci_driver i40e_driver = {
  10275. .name = i40e_driver_name,
  10276. .id_table = i40e_pci_tbl,
  10277. .probe = i40e_probe,
  10278. .remove = i40e_remove,
  10279. #ifdef CONFIG_PM
  10280. .suspend = i40e_suspend,
  10281. .resume = i40e_resume,
  10282. #endif
  10283. .shutdown = i40e_shutdown,
  10284. .err_handler = &i40e_err_handler,
  10285. .sriov_configure = i40e_pci_sriov_configure,
  10286. };
  10287. /**
  10288. * i40e_init_module - Driver registration routine
  10289. *
  10290. * i40e_init_module is the first routine called when the driver is
  10291. * loaded. All it does is register with the PCI subsystem.
  10292. **/
  10293. static int __init i40e_init_module(void)
  10294. {
  10295. pr_info("%s: %s - version %s\n", i40e_driver_name,
  10296. i40e_driver_string, i40e_driver_version_str);
  10297. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  10298. /* we will see if single thread per module is enough for now,
  10299. * it can't be any worse than using the system workqueue which
  10300. * was already single threaded
  10301. */
  10302. i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
  10303. i40e_driver_name);
  10304. if (!i40e_wq) {
  10305. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  10306. return -ENOMEM;
  10307. }
  10308. i40e_dbg_init();
  10309. return pci_register_driver(&i40e_driver);
  10310. }
  10311. module_init(i40e_init_module);
  10312. /**
  10313. * i40e_exit_module - Driver exit cleanup routine
  10314. *
  10315. * i40e_exit_module is called just before the driver is removed
  10316. * from memory.
  10317. **/
  10318. static void __exit i40e_exit_module(void)
  10319. {
  10320. pci_unregister_driver(&i40e_driver);
  10321. destroy_workqueue(i40e_wq);
  10322. i40e_dbg_exit();
  10323. }
  10324. module_exit(i40e_exit_module);