i40e_main.c 328 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882
  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. /* All i40e tracepoints are defined by the include below, which
  34. * must be included exactly once across the whole kernel with
  35. * CREATE_TRACE_POINTS defined
  36. */
  37. #define CREATE_TRACE_POINTS
  38. #include "i40e_trace.h"
  39. const char i40e_driver_name[] = "i40e";
  40. static const char i40e_driver_string[] =
  41. "Intel(R) Ethernet Connection XL710 Network Driver";
  42. #define DRV_KERN "-k"
  43. #define DRV_VERSION_MAJOR 2
  44. #define DRV_VERSION_MINOR 1
  45. #define DRV_VERSION_BUILD 7
  46. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  47. __stringify(DRV_VERSION_MINOR) "." \
  48. __stringify(DRV_VERSION_BUILD) DRV_KERN
  49. const char i40e_driver_version_str[] = DRV_VERSION;
  50. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  51. /* a bit of forward declarations */
  52. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  53. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
  54. static int i40e_add_vsi(struct i40e_vsi *vsi);
  55. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  56. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  57. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  58. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  59. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  60. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
  61. static int i40e_reset(struct i40e_pf *pf);
  62. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  63. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  64. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  65. /* i40e_pci_tbl - PCI Device ID Table
  66. *
  67. * Last entry must be all 0s
  68. *
  69. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  70. * Class, Class Mask, private data (not used) }
  71. */
  72. static const struct pci_device_id i40e_pci_tbl[] = {
  73. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  74. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  75. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  76. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  81. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  82. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  83. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  84. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  85. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  86. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  87. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  88. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  89. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  90. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  91. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  92. /* required last entry */
  93. {0, }
  94. };
  95. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  96. #define I40E_MAX_VF_COUNT 128
  97. static int debug = -1;
  98. module_param(debug, uint, 0);
  99. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  100. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  101. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  102. MODULE_LICENSE("GPL");
  103. MODULE_VERSION(DRV_VERSION);
  104. static struct workqueue_struct *i40e_wq;
  105. /**
  106. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  107. * @hw: pointer to the HW structure
  108. * @mem: ptr to mem struct to fill out
  109. * @size: size of memory requested
  110. * @alignment: what to align the allocation to
  111. **/
  112. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  113. u64 size, u32 alignment)
  114. {
  115. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  116. mem->size = ALIGN(size, alignment);
  117. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  118. &mem->pa, GFP_KERNEL);
  119. if (!mem->va)
  120. return -ENOMEM;
  121. return 0;
  122. }
  123. /**
  124. * i40e_free_dma_mem_d - OS specific memory free for shared code
  125. * @hw: pointer to the HW structure
  126. * @mem: ptr to mem struct to free
  127. **/
  128. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  129. {
  130. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  131. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  132. mem->va = NULL;
  133. mem->pa = 0;
  134. mem->size = 0;
  135. return 0;
  136. }
  137. /**
  138. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  139. * @hw: pointer to the HW structure
  140. * @mem: ptr to mem struct to fill out
  141. * @size: size of memory requested
  142. **/
  143. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  144. u32 size)
  145. {
  146. mem->size = size;
  147. mem->va = kzalloc(size, GFP_KERNEL);
  148. if (!mem->va)
  149. return -ENOMEM;
  150. return 0;
  151. }
  152. /**
  153. * i40e_free_virt_mem_d - OS specific memory free for shared code
  154. * @hw: pointer to the HW structure
  155. * @mem: ptr to mem struct to free
  156. **/
  157. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  158. {
  159. /* it's ok to kfree a NULL pointer */
  160. kfree(mem->va);
  161. mem->va = NULL;
  162. mem->size = 0;
  163. return 0;
  164. }
  165. /**
  166. * i40e_get_lump - find a lump of free generic resource
  167. * @pf: board private structure
  168. * @pile: the pile of resource to search
  169. * @needed: the number of items needed
  170. * @id: an owner id to stick on the items assigned
  171. *
  172. * Returns the base item index of the lump, or negative for error
  173. *
  174. * The search_hint trick and lack of advanced fit-finding only work
  175. * because we're highly likely to have all the same size lump requests.
  176. * Linear search time and any fragmentation should be minimal.
  177. **/
  178. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  179. u16 needed, u16 id)
  180. {
  181. int ret = -ENOMEM;
  182. int i, j;
  183. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  184. dev_info(&pf->pdev->dev,
  185. "param err: pile=%p needed=%d id=0x%04x\n",
  186. pile, needed, id);
  187. return -EINVAL;
  188. }
  189. /* start the linear search with an imperfect hint */
  190. i = pile->search_hint;
  191. while (i < pile->num_entries) {
  192. /* skip already allocated entries */
  193. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  194. i++;
  195. continue;
  196. }
  197. /* do we have enough in this lump? */
  198. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  199. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  200. break;
  201. }
  202. if (j == needed) {
  203. /* there was enough, so assign it to the requestor */
  204. for (j = 0; j < needed; j++)
  205. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  206. ret = i;
  207. pile->search_hint = i + j;
  208. break;
  209. }
  210. /* not enough, so skip over it and continue looking */
  211. i += j;
  212. }
  213. return ret;
  214. }
  215. /**
  216. * i40e_put_lump - return a lump of generic resource
  217. * @pile: the pile of resource to search
  218. * @index: the base item index
  219. * @id: the owner id of the items assigned
  220. *
  221. * Returns the count of items in the lump
  222. **/
  223. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  224. {
  225. int valid_id = (id | I40E_PILE_VALID_BIT);
  226. int count = 0;
  227. int i;
  228. if (!pile || index >= pile->num_entries)
  229. return -EINVAL;
  230. for (i = index;
  231. i < pile->num_entries && pile->list[i] == valid_id;
  232. i++) {
  233. pile->list[i] = 0;
  234. count++;
  235. }
  236. if (count && index < pile->search_hint)
  237. pile->search_hint = index;
  238. return count;
  239. }
  240. /**
  241. * i40e_find_vsi_from_id - searches for the vsi with the given id
  242. * @pf - the pf structure to search for the vsi
  243. * @id - id of the vsi it is searching for
  244. **/
  245. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  246. {
  247. int i;
  248. for (i = 0; i < pf->num_alloc_vsi; i++)
  249. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  250. return pf->vsi[i];
  251. return NULL;
  252. }
  253. /**
  254. * i40e_service_event_schedule - Schedule the service task to wake up
  255. * @pf: board private structure
  256. *
  257. * If not already scheduled, this puts the task into the work queue
  258. **/
  259. void i40e_service_event_schedule(struct i40e_pf *pf)
  260. {
  261. if (!test_bit(__I40E_DOWN, &pf->state) &&
  262. !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  263. queue_work(i40e_wq, &pf->service_task);
  264. }
  265. /**
  266. * i40e_tx_timeout - Respond to a Tx Hang
  267. * @netdev: network interface device structure
  268. *
  269. * If any port has noticed a Tx timeout, it is likely that the whole
  270. * device is munged, not just the one netdev port, so go for the full
  271. * reset.
  272. **/
  273. static void i40e_tx_timeout(struct net_device *netdev)
  274. {
  275. struct i40e_netdev_priv *np = netdev_priv(netdev);
  276. struct i40e_vsi *vsi = np->vsi;
  277. struct i40e_pf *pf = vsi->back;
  278. struct i40e_ring *tx_ring = NULL;
  279. unsigned int i, hung_queue = 0;
  280. u32 head, val;
  281. pf->tx_timeout_count++;
  282. /* find the stopped queue the same way the stack does */
  283. for (i = 0; i < netdev->num_tx_queues; i++) {
  284. struct netdev_queue *q;
  285. unsigned long trans_start;
  286. q = netdev_get_tx_queue(netdev, i);
  287. trans_start = q->trans_start;
  288. if (netif_xmit_stopped(q) &&
  289. time_after(jiffies,
  290. (trans_start + netdev->watchdog_timeo))) {
  291. hung_queue = i;
  292. break;
  293. }
  294. }
  295. if (i == netdev->num_tx_queues) {
  296. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  297. } else {
  298. /* now that we have an index, find the tx_ring struct */
  299. for (i = 0; i < vsi->num_queue_pairs; i++) {
  300. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  301. if (hung_queue ==
  302. vsi->tx_rings[i]->queue_index) {
  303. tx_ring = vsi->tx_rings[i];
  304. break;
  305. }
  306. }
  307. }
  308. }
  309. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  310. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  311. else if (time_before(jiffies,
  312. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  313. return; /* don't do any new action before the next timeout */
  314. if (tx_ring) {
  315. head = i40e_get_head(tx_ring);
  316. /* Read interrupt register */
  317. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  318. val = rd32(&pf->hw,
  319. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  320. tx_ring->vsi->base_vector - 1));
  321. else
  322. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  323. 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",
  324. vsi->seid, hung_queue, tx_ring->next_to_clean,
  325. head, tx_ring->next_to_use,
  326. readl(tx_ring->tail), val);
  327. }
  328. pf->tx_timeout_last_recovery = jiffies;
  329. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  330. pf->tx_timeout_recovery_level, hung_queue);
  331. switch (pf->tx_timeout_recovery_level) {
  332. case 1:
  333. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  334. break;
  335. case 2:
  336. set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  337. break;
  338. case 3:
  339. set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  340. break;
  341. default:
  342. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  343. break;
  344. }
  345. i40e_service_event_schedule(pf);
  346. pf->tx_timeout_recovery_level++;
  347. }
  348. /**
  349. * i40e_get_vsi_stats_struct - Get System Network Statistics
  350. * @vsi: the VSI we care about
  351. *
  352. * Returns the address of the device statistics structure.
  353. * The statistics are actually updated from the service task.
  354. **/
  355. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  356. {
  357. return &vsi->net_stats;
  358. }
  359. /**
  360. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  361. * @netdev: network interface device structure
  362. *
  363. * Returns the address of the device statistics structure.
  364. * The statistics are actually updated from the service task.
  365. **/
  366. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  367. struct rtnl_link_stats64 *stats)
  368. {
  369. struct i40e_netdev_priv *np = netdev_priv(netdev);
  370. struct i40e_ring *tx_ring, *rx_ring;
  371. struct i40e_vsi *vsi = np->vsi;
  372. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  373. int i;
  374. if (test_bit(__I40E_DOWN, &vsi->state))
  375. return;
  376. if (!vsi->tx_rings)
  377. return;
  378. rcu_read_lock();
  379. for (i = 0; i < vsi->num_queue_pairs; i++) {
  380. u64 bytes, packets;
  381. unsigned int start;
  382. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  383. if (!tx_ring)
  384. continue;
  385. do {
  386. start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
  387. packets = tx_ring->stats.packets;
  388. bytes = tx_ring->stats.bytes;
  389. } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
  390. stats->tx_packets += packets;
  391. stats->tx_bytes += bytes;
  392. rx_ring = &tx_ring[1];
  393. do {
  394. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  395. packets = rx_ring->stats.packets;
  396. bytes = rx_ring->stats.bytes;
  397. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  398. stats->rx_packets += packets;
  399. stats->rx_bytes += bytes;
  400. }
  401. rcu_read_unlock();
  402. /* following stats updated by i40e_watchdog_subtask() */
  403. stats->multicast = vsi_stats->multicast;
  404. stats->tx_errors = vsi_stats->tx_errors;
  405. stats->tx_dropped = vsi_stats->tx_dropped;
  406. stats->rx_errors = vsi_stats->rx_errors;
  407. stats->rx_dropped = vsi_stats->rx_dropped;
  408. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  409. stats->rx_length_errors = vsi_stats->rx_length_errors;
  410. }
  411. /**
  412. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  413. * @vsi: the VSI to have its stats reset
  414. **/
  415. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  416. {
  417. struct rtnl_link_stats64 *ns;
  418. int i;
  419. if (!vsi)
  420. return;
  421. ns = i40e_get_vsi_stats_struct(vsi);
  422. memset(ns, 0, sizeof(*ns));
  423. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  424. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  425. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  426. if (vsi->rx_rings && vsi->rx_rings[0]) {
  427. for (i = 0; i < vsi->num_queue_pairs; i++) {
  428. memset(&vsi->rx_rings[i]->stats, 0,
  429. sizeof(vsi->rx_rings[i]->stats));
  430. memset(&vsi->rx_rings[i]->rx_stats, 0,
  431. sizeof(vsi->rx_rings[i]->rx_stats));
  432. memset(&vsi->tx_rings[i]->stats, 0,
  433. sizeof(vsi->tx_rings[i]->stats));
  434. memset(&vsi->tx_rings[i]->tx_stats, 0,
  435. sizeof(vsi->tx_rings[i]->tx_stats));
  436. }
  437. }
  438. vsi->stat_offsets_loaded = false;
  439. }
  440. /**
  441. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  442. * @pf: the PF to be reset
  443. **/
  444. void i40e_pf_reset_stats(struct i40e_pf *pf)
  445. {
  446. int i;
  447. memset(&pf->stats, 0, sizeof(pf->stats));
  448. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  449. pf->stat_offsets_loaded = false;
  450. for (i = 0; i < I40E_MAX_VEB; i++) {
  451. if (pf->veb[i]) {
  452. memset(&pf->veb[i]->stats, 0,
  453. sizeof(pf->veb[i]->stats));
  454. memset(&pf->veb[i]->stats_offsets, 0,
  455. sizeof(pf->veb[i]->stats_offsets));
  456. pf->veb[i]->stat_offsets_loaded = false;
  457. }
  458. }
  459. pf->hw_csum_rx_error = 0;
  460. }
  461. /**
  462. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  463. * @hw: ptr to the hardware info
  464. * @hireg: the high 32 bit reg to read
  465. * @loreg: the low 32 bit reg to read
  466. * @offset_loaded: has the initial offset been loaded yet
  467. * @offset: ptr to current offset value
  468. * @stat: ptr to the stat
  469. *
  470. * Since the device stats are not reset at PFReset, they likely will not
  471. * be zeroed when the driver starts. We'll save the first values read
  472. * and use them as offsets to be subtracted from the raw values in order
  473. * to report stats that count from zero. In the process, we also manage
  474. * the potential roll-over.
  475. **/
  476. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  477. bool offset_loaded, u64 *offset, u64 *stat)
  478. {
  479. u64 new_data;
  480. if (hw->device_id == I40E_DEV_ID_QEMU) {
  481. new_data = rd32(hw, loreg);
  482. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  483. } else {
  484. new_data = rd64(hw, loreg);
  485. }
  486. if (!offset_loaded)
  487. *offset = new_data;
  488. if (likely(new_data >= *offset))
  489. *stat = new_data - *offset;
  490. else
  491. *stat = (new_data + BIT_ULL(48)) - *offset;
  492. *stat &= 0xFFFFFFFFFFFFULL;
  493. }
  494. /**
  495. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  496. * @hw: ptr to the hardware info
  497. * @reg: the hw reg to read
  498. * @offset_loaded: has the initial offset been loaded yet
  499. * @offset: ptr to current offset value
  500. * @stat: ptr to the stat
  501. **/
  502. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  503. bool offset_loaded, u64 *offset, u64 *stat)
  504. {
  505. u32 new_data;
  506. new_data = rd32(hw, reg);
  507. if (!offset_loaded)
  508. *offset = new_data;
  509. if (likely(new_data >= *offset))
  510. *stat = (u32)(new_data - *offset);
  511. else
  512. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  513. }
  514. /**
  515. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  516. * @vsi: the VSI to be updated
  517. **/
  518. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  519. {
  520. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  521. struct i40e_pf *pf = vsi->back;
  522. struct i40e_hw *hw = &pf->hw;
  523. struct i40e_eth_stats *oes;
  524. struct i40e_eth_stats *es; /* device's eth stats */
  525. es = &vsi->eth_stats;
  526. oes = &vsi->eth_stats_offsets;
  527. /* Gather up the stats that the hw collects */
  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_update32(hw, I40E_GLV_RDPC(stat_idx),
  532. vsi->stat_offsets_loaded,
  533. &oes->rx_discards, &es->rx_discards);
  534. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  535. vsi->stat_offsets_loaded,
  536. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  537. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  538. vsi->stat_offsets_loaded,
  539. &oes->tx_errors, &es->tx_errors);
  540. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  541. I40E_GLV_GORCL(stat_idx),
  542. vsi->stat_offsets_loaded,
  543. &oes->rx_bytes, &es->rx_bytes);
  544. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  545. I40E_GLV_UPRCL(stat_idx),
  546. vsi->stat_offsets_loaded,
  547. &oes->rx_unicast, &es->rx_unicast);
  548. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  549. I40E_GLV_MPRCL(stat_idx),
  550. vsi->stat_offsets_loaded,
  551. &oes->rx_multicast, &es->rx_multicast);
  552. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  553. I40E_GLV_BPRCL(stat_idx),
  554. vsi->stat_offsets_loaded,
  555. &oes->rx_broadcast, &es->rx_broadcast);
  556. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  557. I40E_GLV_GOTCL(stat_idx),
  558. vsi->stat_offsets_loaded,
  559. &oes->tx_bytes, &es->tx_bytes);
  560. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  561. I40E_GLV_UPTCL(stat_idx),
  562. vsi->stat_offsets_loaded,
  563. &oes->tx_unicast, &es->tx_unicast);
  564. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  565. I40E_GLV_MPTCL(stat_idx),
  566. vsi->stat_offsets_loaded,
  567. &oes->tx_multicast, &es->tx_multicast);
  568. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  569. I40E_GLV_BPTCL(stat_idx),
  570. vsi->stat_offsets_loaded,
  571. &oes->tx_broadcast, &es->tx_broadcast);
  572. vsi->stat_offsets_loaded = true;
  573. }
  574. /**
  575. * i40e_update_veb_stats - Update Switch component statistics
  576. * @veb: the VEB being updated
  577. **/
  578. static void i40e_update_veb_stats(struct i40e_veb *veb)
  579. {
  580. struct i40e_pf *pf = veb->pf;
  581. struct i40e_hw *hw = &pf->hw;
  582. struct i40e_eth_stats *oes;
  583. struct i40e_eth_stats *es; /* device's eth stats */
  584. struct i40e_veb_tc_stats *veb_oes;
  585. struct i40e_veb_tc_stats *veb_es;
  586. int i, idx = 0;
  587. idx = veb->stats_idx;
  588. es = &veb->stats;
  589. oes = &veb->stats_offsets;
  590. veb_es = &veb->tc_stats;
  591. veb_oes = &veb->tc_stats_offsets;
  592. /* Gather up the stats that the hw collects */
  593. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  594. veb->stat_offsets_loaded,
  595. &oes->tx_discards, &es->tx_discards);
  596. if (hw->revision_id > 0)
  597. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  598. veb->stat_offsets_loaded,
  599. &oes->rx_unknown_protocol,
  600. &es->rx_unknown_protocol);
  601. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  602. veb->stat_offsets_loaded,
  603. &oes->rx_bytes, &es->rx_bytes);
  604. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  605. veb->stat_offsets_loaded,
  606. &oes->rx_unicast, &es->rx_unicast);
  607. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  608. veb->stat_offsets_loaded,
  609. &oes->rx_multicast, &es->rx_multicast);
  610. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  611. veb->stat_offsets_loaded,
  612. &oes->rx_broadcast, &es->rx_broadcast);
  613. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  614. veb->stat_offsets_loaded,
  615. &oes->tx_bytes, &es->tx_bytes);
  616. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  617. veb->stat_offsets_loaded,
  618. &oes->tx_unicast, &es->tx_unicast);
  619. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  620. veb->stat_offsets_loaded,
  621. &oes->tx_multicast, &es->tx_multicast);
  622. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  623. veb->stat_offsets_loaded,
  624. &oes->tx_broadcast, &es->tx_broadcast);
  625. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  626. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  627. I40E_GLVEBTC_RPCL(i, idx),
  628. veb->stat_offsets_loaded,
  629. &veb_oes->tc_rx_packets[i],
  630. &veb_es->tc_rx_packets[i]);
  631. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  632. I40E_GLVEBTC_RBCL(i, idx),
  633. veb->stat_offsets_loaded,
  634. &veb_oes->tc_rx_bytes[i],
  635. &veb_es->tc_rx_bytes[i]);
  636. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  637. I40E_GLVEBTC_TPCL(i, idx),
  638. veb->stat_offsets_loaded,
  639. &veb_oes->tc_tx_packets[i],
  640. &veb_es->tc_tx_packets[i]);
  641. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  642. I40E_GLVEBTC_TBCL(i, idx),
  643. veb->stat_offsets_loaded,
  644. &veb_oes->tc_tx_bytes[i],
  645. &veb_es->tc_tx_bytes[i]);
  646. }
  647. veb->stat_offsets_loaded = true;
  648. }
  649. /**
  650. * i40e_update_vsi_stats - Update the vsi statistics counters.
  651. * @vsi: the VSI to be updated
  652. *
  653. * There are a few instances where we store the same stat in a
  654. * couple of different structs. This is partly because we have
  655. * the netdev stats that need to be filled out, which is slightly
  656. * different from the "eth_stats" defined by the chip and used in
  657. * VF communications. We sort it out here.
  658. **/
  659. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  660. {
  661. struct i40e_pf *pf = vsi->back;
  662. struct rtnl_link_stats64 *ons;
  663. struct rtnl_link_stats64 *ns; /* netdev stats */
  664. struct i40e_eth_stats *oes;
  665. struct i40e_eth_stats *es; /* device's eth stats */
  666. u32 tx_restart, tx_busy;
  667. struct i40e_ring *p;
  668. u32 rx_page, rx_buf;
  669. u64 bytes, packets;
  670. unsigned int start;
  671. u64 tx_linearize;
  672. u64 tx_force_wb;
  673. u64 rx_p, rx_b;
  674. u64 tx_p, tx_b;
  675. u16 q;
  676. if (test_bit(__I40E_DOWN, &vsi->state) ||
  677. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  678. return;
  679. ns = i40e_get_vsi_stats_struct(vsi);
  680. ons = &vsi->net_stats_offsets;
  681. es = &vsi->eth_stats;
  682. oes = &vsi->eth_stats_offsets;
  683. /* Gather up the netdev and vsi stats that the driver collects
  684. * on the fly during packet processing
  685. */
  686. rx_b = rx_p = 0;
  687. tx_b = tx_p = 0;
  688. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  689. rx_page = 0;
  690. rx_buf = 0;
  691. rcu_read_lock();
  692. for (q = 0; q < vsi->num_queue_pairs; q++) {
  693. /* locate Tx ring */
  694. p = ACCESS_ONCE(vsi->tx_rings[q]);
  695. do {
  696. start = u64_stats_fetch_begin_irq(&p->syncp);
  697. packets = p->stats.packets;
  698. bytes = p->stats.bytes;
  699. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  700. tx_b += bytes;
  701. tx_p += packets;
  702. tx_restart += p->tx_stats.restart_queue;
  703. tx_busy += p->tx_stats.tx_busy;
  704. tx_linearize += p->tx_stats.tx_linearize;
  705. tx_force_wb += p->tx_stats.tx_force_wb;
  706. /* Rx queue is part of the same block as Tx queue */
  707. p = &p[1];
  708. do {
  709. start = u64_stats_fetch_begin_irq(&p->syncp);
  710. packets = p->stats.packets;
  711. bytes = p->stats.bytes;
  712. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  713. rx_b += bytes;
  714. rx_p += packets;
  715. rx_buf += p->rx_stats.alloc_buff_failed;
  716. rx_page += p->rx_stats.alloc_page_failed;
  717. }
  718. rcu_read_unlock();
  719. vsi->tx_restart = tx_restart;
  720. vsi->tx_busy = tx_busy;
  721. vsi->tx_linearize = tx_linearize;
  722. vsi->tx_force_wb = tx_force_wb;
  723. vsi->rx_page_failed = rx_page;
  724. vsi->rx_buf_failed = rx_buf;
  725. ns->rx_packets = rx_p;
  726. ns->rx_bytes = rx_b;
  727. ns->tx_packets = tx_p;
  728. ns->tx_bytes = tx_b;
  729. /* update netdev stats from eth stats */
  730. i40e_update_eth_stats(vsi);
  731. ons->tx_errors = oes->tx_errors;
  732. ns->tx_errors = es->tx_errors;
  733. ons->multicast = oes->rx_multicast;
  734. ns->multicast = es->rx_multicast;
  735. ons->rx_dropped = oes->rx_discards;
  736. ns->rx_dropped = es->rx_discards;
  737. ons->tx_dropped = oes->tx_discards;
  738. ns->tx_dropped = es->tx_discards;
  739. /* pull in a couple PF stats if this is the main vsi */
  740. if (vsi == pf->vsi[pf->lan_vsi]) {
  741. ns->rx_crc_errors = pf->stats.crc_errors;
  742. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  743. ns->rx_length_errors = pf->stats.rx_length_errors;
  744. }
  745. }
  746. /**
  747. * i40e_update_pf_stats - Update the PF statistics counters.
  748. * @pf: the PF to be updated
  749. **/
  750. static void i40e_update_pf_stats(struct i40e_pf *pf)
  751. {
  752. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  753. struct i40e_hw_port_stats *nsd = &pf->stats;
  754. struct i40e_hw *hw = &pf->hw;
  755. u32 val;
  756. int i;
  757. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  758. I40E_GLPRT_GORCL(hw->port),
  759. pf->stat_offsets_loaded,
  760. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  761. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  762. I40E_GLPRT_GOTCL(hw->port),
  763. pf->stat_offsets_loaded,
  764. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  765. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  766. pf->stat_offsets_loaded,
  767. &osd->eth.rx_discards,
  768. &nsd->eth.rx_discards);
  769. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  770. I40E_GLPRT_UPRCL(hw->port),
  771. pf->stat_offsets_loaded,
  772. &osd->eth.rx_unicast,
  773. &nsd->eth.rx_unicast);
  774. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  775. I40E_GLPRT_MPRCL(hw->port),
  776. pf->stat_offsets_loaded,
  777. &osd->eth.rx_multicast,
  778. &nsd->eth.rx_multicast);
  779. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  780. I40E_GLPRT_BPRCL(hw->port),
  781. pf->stat_offsets_loaded,
  782. &osd->eth.rx_broadcast,
  783. &nsd->eth.rx_broadcast);
  784. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  785. I40E_GLPRT_UPTCL(hw->port),
  786. pf->stat_offsets_loaded,
  787. &osd->eth.tx_unicast,
  788. &nsd->eth.tx_unicast);
  789. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  790. I40E_GLPRT_MPTCL(hw->port),
  791. pf->stat_offsets_loaded,
  792. &osd->eth.tx_multicast,
  793. &nsd->eth.tx_multicast);
  794. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  795. I40E_GLPRT_BPTCL(hw->port),
  796. pf->stat_offsets_loaded,
  797. &osd->eth.tx_broadcast,
  798. &nsd->eth.tx_broadcast);
  799. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  800. pf->stat_offsets_loaded,
  801. &osd->tx_dropped_link_down,
  802. &nsd->tx_dropped_link_down);
  803. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  804. pf->stat_offsets_loaded,
  805. &osd->crc_errors, &nsd->crc_errors);
  806. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  807. pf->stat_offsets_loaded,
  808. &osd->illegal_bytes, &nsd->illegal_bytes);
  809. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  810. pf->stat_offsets_loaded,
  811. &osd->mac_local_faults,
  812. &nsd->mac_local_faults);
  813. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  814. pf->stat_offsets_loaded,
  815. &osd->mac_remote_faults,
  816. &nsd->mac_remote_faults);
  817. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  818. pf->stat_offsets_loaded,
  819. &osd->rx_length_errors,
  820. &nsd->rx_length_errors);
  821. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  822. pf->stat_offsets_loaded,
  823. &osd->link_xon_rx, &nsd->link_xon_rx);
  824. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  825. pf->stat_offsets_loaded,
  826. &osd->link_xon_tx, &nsd->link_xon_tx);
  827. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  828. pf->stat_offsets_loaded,
  829. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  830. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  831. pf->stat_offsets_loaded,
  832. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  833. for (i = 0; i < 8; i++) {
  834. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  835. pf->stat_offsets_loaded,
  836. &osd->priority_xoff_rx[i],
  837. &nsd->priority_xoff_rx[i]);
  838. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  839. pf->stat_offsets_loaded,
  840. &osd->priority_xon_rx[i],
  841. &nsd->priority_xon_rx[i]);
  842. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  843. pf->stat_offsets_loaded,
  844. &osd->priority_xon_tx[i],
  845. &nsd->priority_xon_tx[i]);
  846. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  847. pf->stat_offsets_loaded,
  848. &osd->priority_xoff_tx[i],
  849. &nsd->priority_xoff_tx[i]);
  850. i40e_stat_update32(hw,
  851. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  852. pf->stat_offsets_loaded,
  853. &osd->priority_xon_2_xoff[i],
  854. &nsd->priority_xon_2_xoff[i]);
  855. }
  856. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  857. I40E_GLPRT_PRC64L(hw->port),
  858. pf->stat_offsets_loaded,
  859. &osd->rx_size_64, &nsd->rx_size_64);
  860. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  861. I40E_GLPRT_PRC127L(hw->port),
  862. pf->stat_offsets_loaded,
  863. &osd->rx_size_127, &nsd->rx_size_127);
  864. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  865. I40E_GLPRT_PRC255L(hw->port),
  866. pf->stat_offsets_loaded,
  867. &osd->rx_size_255, &nsd->rx_size_255);
  868. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  869. I40E_GLPRT_PRC511L(hw->port),
  870. pf->stat_offsets_loaded,
  871. &osd->rx_size_511, &nsd->rx_size_511);
  872. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  873. I40E_GLPRT_PRC1023L(hw->port),
  874. pf->stat_offsets_loaded,
  875. &osd->rx_size_1023, &nsd->rx_size_1023);
  876. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  877. I40E_GLPRT_PRC1522L(hw->port),
  878. pf->stat_offsets_loaded,
  879. &osd->rx_size_1522, &nsd->rx_size_1522);
  880. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  881. I40E_GLPRT_PRC9522L(hw->port),
  882. pf->stat_offsets_loaded,
  883. &osd->rx_size_big, &nsd->rx_size_big);
  884. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  885. I40E_GLPRT_PTC64L(hw->port),
  886. pf->stat_offsets_loaded,
  887. &osd->tx_size_64, &nsd->tx_size_64);
  888. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  889. I40E_GLPRT_PTC127L(hw->port),
  890. pf->stat_offsets_loaded,
  891. &osd->tx_size_127, &nsd->tx_size_127);
  892. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  893. I40E_GLPRT_PTC255L(hw->port),
  894. pf->stat_offsets_loaded,
  895. &osd->tx_size_255, &nsd->tx_size_255);
  896. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  897. I40E_GLPRT_PTC511L(hw->port),
  898. pf->stat_offsets_loaded,
  899. &osd->tx_size_511, &nsd->tx_size_511);
  900. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  901. I40E_GLPRT_PTC1023L(hw->port),
  902. pf->stat_offsets_loaded,
  903. &osd->tx_size_1023, &nsd->tx_size_1023);
  904. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  905. I40E_GLPRT_PTC1522L(hw->port),
  906. pf->stat_offsets_loaded,
  907. &osd->tx_size_1522, &nsd->tx_size_1522);
  908. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  909. I40E_GLPRT_PTC9522L(hw->port),
  910. pf->stat_offsets_loaded,
  911. &osd->tx_size_big, &nsd->tx_size_big);
  912. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  913. pf->stat_offsets_loaded,
  914. &osd->rx_undersize, &nsd->rx_undersize);
  915. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  916. pf->stat_offsets_loaded,
  917. &osd->rx_fragments, &nsd->rx_fragments);
  918. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  919. pf->stat_offsets_loaded,
  920. &osd->rx_oversize, &nsd->rx_oversize);
  921. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  922. pf->stat_offsets_loaded,
  923. &osd->rx_jabber, &nsd->rx_jabber);
  924. /* FDIR stats */
  925. i40e_stat_update32(hw,
  926. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
  927. pf->stat_offsets_loaded,
  928. &osd->fd_atr_match, &nsd->fd_atr_match);
  929. i40e_stat_update32(hw,
  930. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
  931. pf->stat_offsets_loaded,
  932. &osd->fd_sb_match, &nsd->fd_sb_match);
  933. i40e_stat_update32(hw,
  934. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
  935. pf->stat_offsets_loaded,
  936. &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
  937. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  938. nsd->tx_lpi_status =
  939. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  940. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  941. nsd->rx_lpi_status =
  942. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  943. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  944. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  945. pf->stat_offsets_loaded,
  946. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  947. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  948. pf->stat_offsets_loaded,
  949. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  950. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  951. !(pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED))
  952. nsd->fd_sb_status = true;
  953. else
  954. nsd->fd_sb_status = false;
  955. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  956. !(pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED))
  957. nsd->fd_atr_status = true;
  958. else
  959. nsd->fd_atr_status = false;
  960. pf->stat_offsets_loaded = true;
  961. }
  962. /**
  963. * i40e_update_stats - Update the various statistics counters.
  964. * @vsi: the VSI to be updated
  965. *
  966. * Update the various stats for this VSI and its related entities.
  967. **/
  968. void i40e_update_stats(struct i40e_vsi *vsi)
  969. {
  970. struct i40e_pf *pf = vsi->back;
  971. if (vsi == pf->vsi[pf->lan_vsi])
  972. i40e_update_pf_stats(pf);
  973. i40e_update_vsi_stats(vsi);
  974. }
  975. /**
  976. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  977. * @vsi: the VSI to be searched
  978. * @macaddr: the MAC address
  979. * @vlan: the vlan
  980. *
  981. * Returns ptr to the filter object or NULL
  982. **/
  983. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  984. const u8 *macaddr, s16 vlan)
  985. {
  986. struct i40e_mac_filter *f;
  987. u64 key;
  988. if (!vsi || !macaddr)
  989. return NULL;
  990. key = i40e_addr_to_hkey(macaddr);
  991. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  992. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  993. (vlan == f->vlan))
  994. return f;
  995. }
  996. return NULL;
  997. }
  998. /**
  999. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1000. * @vsi: the VSI to be searched
  1001. * @macaddr: the MAC address we are searching for
  1002. *
  1003. * Returns the first filter with the provided MAC address or NULL if
  1004. * MAC address was not found
  1005. **/
  1006. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1007. {
  1008. struct i40e_mac_filter *f;
  1009. u64 key;
  1010. if (!vsi || !macaddr)
  1011. return NULL;
  1012. key = i40e_addr_to_hkey(macaddr);
  1013. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1014. if ((ether_addr_equal(macaddr, f->macaddr)))
  1015. return f;
  1016. }
  1017. return NULL;
  1018. }
  1019. /**
  1020. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1021. * @vsi: the VSI to be searched
  1022. *
  1023. * Returns true if VSI is in vlan mode or false otherwise
  1024. **/
  1025. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1026. {
  1027. /* If we have a PVID, always operate in VLAN mode */
  1028. if (vsi->info.pvid)
  1029. return true;
  1030. /* We need to operate in VLAN mode whenever we have any filters with
  1031. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1032. * time, incurring search cost repeatedly. However, we can notice two
  1033. * things:
  1034. *
  1035. * 1) the only place where we can gain a VLAN filter is in
  1036. * i40e_add_filter.
  1037. *
  1038. * 2) the only place where filters are actually removed is in
  1039. * i40e_sync_filters_subtask.
  1040. *
  1041. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1042. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1043. * we have to perform the full search after deleting filters in
  1044. * i40e_sync_filters_subtask, but we already have to search
  1045. * filters here and can perform the check at the same time. This
  1046. * results in avoiding embedding a loop for VLAN mode inside another
  1047. * loop over all the filters, and should maintain correctness as noted
  1048. * above.
  1049. */
  1050. return vsi->has_vlan_filter;
  1051. }
  1052. /**
  1053. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1054. * @vsi: the VSI to configure
  1055. * @tmp_add_list: list of filters ready to be added
  1056. * @tmp_del_list: list of filters ready to be deleted
  1057. * @vlan_filters: the number of active VLAN filters
  1058. *
  1059. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1060. * behave as expected. If we have any active VLAN filters remaining or about
  1061. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1062. * so that they only match against untagged traffic. If we no longer have any
  1063. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1064. * so that they match against both tagged and untagged traffic. In this way,
  1065. * we ensure that we correctly receive the desired traffic. This ensures that
  1066. * when we have an active VLAN we will receive only untagged traffic and
  1067. * traffic matching active VLANs. If we have no active VLANs then we will
  1068. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1069. *
  1070. * Finally, in a similar fashion, this function also corrects filters when
  1071. * there is an active PVID assigned to this VSI.
  1072. *
  1073. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1074. *
  1075. * This function is only expected to be called from within
  1076. * i40e_sync_vsi_filters.
  1077. *
  1078. * NOTE: This function expects to be called while under the
  1079. * mac_filter_hash_lock
  1080. */
  1081. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1082. struct hlist_head *tmp_add_list,
  1083. struct hlist_head *tmp_del_list,
  1084. int vlan_filters)
  1085. {
  1086. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1087. struct i40e_mac_filter *f, *add_head;
  1088. struct i40e_new_mac_filter *new;
  1089. struct hlist_node *h;
  1090. int bkt, new_vlan;
  1091. /* To determine if a particular filter needs to be replaced we
  1092. * have the three following conditions:
  1093. *
  1094. * a) if we have a PVID assigned, then all filters which are
  1095. * not marked as VLAN=PVID must be replaced with filters that
  1096. * are.
  1097. * b) otherwise, if we have any active VLANS, all filters
  1098. * which are marked as VLAN=-1 must be replaced with
  1099. * filters marked as VLAN=0
  1100. * c) finally, if we do not have any active VLANS, all filters
  1101. * which are marked as VLAN=0 must be replaced with filters
  1102. * marked as VLAN=-1
  1103. */
  1104. /* Update the filters about to be added in place */
  1105. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1106. if (pvid && new->f->vlan != pvid)
  1107. new->f->vlan = pvid;
  1108. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1109. new->f->vlan = 0;
  1110. else if (!vlan_filters && new->f->vlan == 0)
  1111. new->f->vlan = I40E_VLAN_ANY;
  1112. }
  1113. /* Update the remaining active filters */
  1114. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1115. /* Combine the checks for whether a filter needs to be changed
  1116. * and then determine the new VLAN inside the if block, in
  1117. * order to avoid duplicating code for adding the new filter
  1118. * then deleting the old filter.
  1119. */
  1120. if ((pvid && f->vlan != pvid) ||
  1121. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1122. (!vlan_filters && f->vlan == 0)) {
  1123. /* Determine the new vlan we will be adding */
  1124. if (pvid)
  1125. new_vlan = pvid;
  1126. else if (vlan_filters)
  1127. new_vlan = 0;
  1128. else
  1129. new_vlan = I40E_VLAN_ANY;
  1130. /* Create the new filter */
  1131. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1132. if (!add_head)
  1133. return -ENOMEM;
  1134. /* Create a temporary i40e_new_mac_filter */
  1135. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1136. if (!new)
  1137. return -ENOMEM;
  1138. new->f = add_head;
  1139. new->state = add_head->state;
  1140. /* Add the new filter to the tmp list */
  1141. hlist_add_head(&new->hlist, tmp_add_list);
  1142. /* Put the original filter into the delete list */
  1143. f->state = I40E_FILTER_REMOVE;
  1144. hash_del(&f->hlist);
  1145. hlist_add_head(&f->hlist, tmp_del_list);
  1146. }
  1147. }
  1148. vsi->has_vlan_filter = !!vlan_filters;
  1149. return 0;
  1150. }
  1151. /**
  1152. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1153. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1154. * @macaddr: the MAC address
  1155. *
  1156. * Remove whatever filter the firmware set up so the driver can manage
  1157. * its own filtering intelligently.
  1158. **/
  1159. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1160. {
  1161. struct i40e_aqc_remove_macvlan_element_data element;
  1162. struct i40e_pf *pf = vsi->back;
  1163. /* Only appropriate for the PF main VSI */
  1164. if (vsi->type != I40E_VSI_MAIN)
  1165. return;
  1166. memset(&element, 0, sizeof(element));
  1167. ether_addr_copy(element.mac_addr, macaddr);
  1168. element.vlan_tag = 0;
  1169. /* Ignore error returns, some firmware does it this way... */
  1170. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1171. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1172. memset(&element, 0, sizeof(element));
  1173. ether_addr_copy(element.mac_addr, macaddr);
  1174. element.vlan_tag = 0;
  1175. /* ...and some firmware does it this way. */
  1176. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1177. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1178. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1179. }
  1180. /**
  1181. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1182. * @vsi: the VSI to be searched
  1183. * @macaddr: the MAC address
  1184. * @vlan: the vlan
  1185. *
  1186. * Returns ptr to the filter object or NULL when no memory available.
  1187. *
  1188. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1189. * being held.
  1190. **/
  1191. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1192. const u8 *macaddr, s16 vlan)
  1193. {
  1194. struct i40e_mac_filter *f;
  1195. u64 key;
  1196. if (!vsi || !macaddr)
  1197. return NULL;
  1198. f = i40e_find_filter(vsi, macaddr, vlan);
  1199. if (!f) {
  1200. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1201. if (!f)
  1202. return NULL;
  1203. /* Update the boolean indicating if we need to function in
  1204. * VLAN mode.
  1205. */
  1206. if (vlan >= 0)
  1207. vsi->has_vlan_filter = true;
  1208. ether_addr_copy(f->macaddr, macaddr);
  1209. f->vlan = vlan;
  1210. /* If we're in overflow promisc mode, set the state directly
  1211. * to failed, so we don't bother to try sending the filter
  1212. * to the hardware.
  1213. */
  1214. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
  1215. f->state = I40E_FILTER_FAILED;
  1216. else
  1217. f->state = I40E_FILTER_NEW;
  1218. INIT_HLIST_NODE(&f->hlist);
  1219. key = i40e_addr_to_hkey(macaddr);
  1220. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1221. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1222. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1223. }
  1224. /* If we're asked to add a filter that has been marked for removal, it
  1225. * is safe to simply restore it to active state. __i40e_del_filter
  1226. * will have simply deleted any filters which were previously marked
  1227. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1228. * previously been ACTIVE. Since we haven't yet run the sync filters
  1229. * task, just restore this filter to the ACTIVE state so that the
  1230. * sync task leaves it in place
  1231. */
  1232. if (f->state == I40E_FILTER_REMOVE)
  1233. f->state = I40E_FILTER_ACTIVE;
  1234. return f;
  1235. }
  1236. /**
  1237. * __i40e_del_filter - Remove a specific filter from the VSI
  1238. * @vsi: VSI to remove from
  1239. * @f: the filter to remove from the list
  1240. *
  1241. * This function should be called instead of i40e_del_filter only if you know
  1242. * the exact filter you will remove already, such as via i40e_find_filter or
  1243. * i40e_find_mac.
  1244. *
  1245. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1246. * being held.
  1247. * ANOTHER NOTE: This function MUST be called from within the context of
  1248. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1249. * instead of list_for_each_entry().
  1250. **/
  1251. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1252. {
  1253. if (!f)
  1254. return;
  1255. /* If the filter was never added to firmware then we can just delete it
  1256. * directly and we don't want to set the status to remove or else an
  1257. * admin queue command will unnecessarily fire.
  1258. */
  1259. if ((f->state == I40E_FILTER_FAILED) ||
  1260. (f->state == I40E_FILTER_NEW)) {
  1261. hash_del(&f->hlist);
  1262. kfree(f);
  1263. } else {
  1264. f->state = I40E_FILTER_REMOVE;
  1265. }
  1266. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1267. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1268. }
  1269. /**
  1270. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1271. * @vsi: the VSI to be searched
  1272. * @macaddr: the MAC address
  1273. * @vlan: the VLAN
  1274. *
  1275. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1276. * being held.
  1277. * ANOTHER NOTE: This function MUST be called from within the context of
  1278. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1279. * instead of list_for_each_entry().
  1280. **/
  1281. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1282. {
  1283. struct i40e_mac_filter *f;
  1284. if (!vsi || !macaddr)
  1285. return;
  1286. f = i40e_find_filter(vsi, macaddr, vlan);
  1287. __i40e_del_filter(vsi, f);
  1288. }
  1289. /**
  1290. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1291. * @vsi: the VSI to be searched
  1292. * @macaddr: the mac address to be filtered
  1293. *
  1294. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1295. * go through all the macvlan filters and add a macvlan filter for each
  1296. * unique vlan that already exists. If a PVID has been assigned, instead only
  1297. * add the macaddr to that VLAN.
  1298. *
  1299. * Returns last filter added on success, else NULL
  1300. **/
  1301. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1302. const u8 *macaddr)
  1303. {
  1304. struct i40e_mac_filter *f, *add = NULL;
  1305. struct hlist_node *h;
  1306. int bkt;
  1307. if (vsi->info.pvid)
  1308. return i40e_add_filter(vsi, macaddr,
  1309. le16_to_cpu(vsi->info.pvid));
  1310. if (!i40e_is_vsi_in_vlan(vsi))
  1311. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1312. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1313. if (f->state == I40E_FILTER_REMOVE)
  1314. continue;
  1315. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1316. if (!add)
  1317. return NULL;
  1318. }
  1319. return add;
  1320. }
  1321. /**
  1322. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1323. * @vsi: the VSI to be searched
  1324. * @macaddr: the mac address to be removed
  1325. *
  1326. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1327. * associated with.
  1328. *
  1329. * Returns 0 for success, or error
  1330. **/
  1331. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1332. {
  1333. struct i40e_mac_filter *f;
  1334. struct hlist_node *h;
  1335. bool found = false;
  1336. int bkt;
  1337. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1338. "Missing mac_filter_hash_lock\n");
  1339. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1340. if (ether_addr_equal(macaddr, f->macaddr)) {
  1341. __i40e_del_filter(vsi, f);
  1342. found = true;
  1343. }
  1344. }
  1345. if (found)
  1346. return 0;
  1347. else
  1348. return -ENOENT;
  1349. }
  1350. /**
  1351. * i40e_set_mac - NDO callback to set mac address
  1352. * @netdev: network interface device structure
  1353. * @p: pointer to an address structure
  1354. *
  1355. * Returns 0 on success, negative on failure
  1356. **/
  1357. static int i40e_set_mac(struct net_device *netdev, void *p)
  1358. {
  1359. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1360. struct i40e_vsi *vsi = np->vsi;
  1361. struct i40e_pf *pf = vsi->back;
  1362. struct i40e_hw *hw = &pf->hw;
  1363. struct sockaddr *addr = p;
  1364. if (!is_valid_ether_addr(addr->sa_data))
  1365. return -EADDRNOTAVAIL;
  1366. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1367. netdev_info(netdev, "already using mac address %pM\n",
  1368. addr->sa_data);
  1369. return 0;
  1370. }
  1371. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1372. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1373. return -EADDRNOTAVAIL;
  1374. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1375. netdev_info(netdev, "returning to hw mac address %pM\n",
  1376. hw->mac.addr);
  1377. else
  1378. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1379. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1380. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1381. i40e_add_mac_filter(vsi, addr->sa_data);
  1382. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1383. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1384. if (vsi->type == I40E_VSI_MAIN) {
  1385. i40e_status ret;
  1386. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1387. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1388. addr->sa_data, NULL);
  1389. if (ret)
  1390. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1391. i40e_stat_str(hw, ret),
  1392. i40e_aq_str(hw, hw->aq.asq_last_status));
  1393. }
  1394. /* schedule our worker thread which will take care of
  1395. * applying the new filter changes
  1396. */
  1397. i40e_service_event_schedule(vsi->back);
  1398. return 0;
  1399. }
  1400. /**
  1401. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1402. * @vsi: the VSI being setup
  1403. * @ctxt: VSI context structure
  1404. * @enabled_tc: Enabled TCs bitmap
  1405. * @is_add: True if called before Add VSI
  1406. *
  1407. * Setup VSI queue mapping for enabled traffic classes.
  1408. **/
  1409. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1410. struct i40e_vsi_context *ctxt,
  1411. u8 enabled_tc,
  1412. bool is_add)
  1413. {
  1414. struct i40e_pf *pf = vsi->back;
  1415. u16 sections = 0;
  1416. u8 netdev_tc = 0;
  1417. u16 numtc = 0;
  1418. u16 qcount;
  1419. u8 offset;
  1420. u16 qmap;
  1421. int i;
  1422. u16 num_tc_qps = 0;
  1423. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1424. offset = 0;
  1425. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1426. /* Find numtc from enabled TC bitmap */
  1427. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1428. if (enabled_tc & BIT(i)) /* TC is enabled */
  1429. numtc++;
  1430. }
  1431. if (!numtc) {
  1432. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1433. numtc = 1;
  1434. }
  1435. } else {
  1436. /* At least TC0 is enabled in case of non-DCB case */
  1437. numtc = 1;
  1438. }
  1439. vsi->tc_config.numtc = numtc;
  1440. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1441. /* Number of queues per enabled TC */
  1442. qcount = vsi->alloc_queue_pairs;
  1443. num_tc_qps = qcount / numtc;
  1444. num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
  1445. /* Setup queue offset/count for all TCs for given VSI */
  1446. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1447. /* See if the given TC is enabled for the given VSI */
  1448. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1449. /* TC is enabled */
  1450. int pow, num_qps;
  1451. switch (vsi->type) {
  1452. case I40E_VSI_MAIN:
  1453. qcount = min_t(int, pf->alloc_rss_size,
  1454. num_tc_qps);
  1455. break;
  1456. case I40E_VSI_FDIR:
  1457. case I40E_VSI_SRIOV:
  1458. case I40E_VSI_VMDQ2:
  1459. default:
  1460. qcount = num_tc_qps;
  1461. WARN_ON(i != 0);
  1462. break;
  1463. }
  1464. vsi->tc_config.tc_info[i].qoffset = offset;
  1465. vsi->tc_config.tc_info[i].qcount = qcount;
  1466. /* find the next higher power-of-2 of num queue pairs */
  1467. num_qps = qcount;
  1468. pow = 0;
  1469. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1470. pow++;
  1471. num_qps >>= 1;
  1472. }
  1473. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1474. qmap =
  1475. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1476. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1477. offset += qcount;
  1478. } else {
  1479. /* TC is not enabled so set the offset to
  1480. * default queue and allocate one queue
  1481. * for the given TC.
  1482. */
  1483. vsi->tc_config.tc_info[i].qoffset = 0;
  1484. vsi->tc_config.tc_info[i].qcount = 1;
  1485. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1486. qmap = 0;
  1487. }
  1488. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1489. }
  1490. /* Set actual Tx/Rx queue pairs */
  1491. vsi->num_queue_pairs = offset;
  1492. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1493. if (vsi->req_queue_pairs > 0)
  1494. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1495. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1496. vsi->num_queue_pairs = pf->num_lan_msix;
  1497. }
  1498. /* Scheduler section valid can only be set for ADD VSI */
  1499. if (is_add) {
  1500. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1501. ctxt->info.up_enable_bits = enabled_tc;
  1502. }
  1503. if (vsi->type == I40E_VSI_SRIOV) {
  1504. ctxt->info.mapping_flags |=
  1505. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1506. for (i = 0; i < vsi->num_queue_pairs; i++)
  1507. ctxt->info.queue_mapping[i] =
  1508. cpu_to_le16(vsi->base_queue + i);
  1509. } else {
  1510. ctxt->info.mapping_flags |=
  1511. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1512. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1513. }
  1514. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1515. }
  1516. /**
  1517. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1518. * @netdev: the netdevice
  1519. * @addr: address to add
  1520. *
  1521. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1522. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1523. */
  1524. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1525. {
  1526. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1527. struct i40e_vsi *vsi = np->vsi;
  1528. if (i40e_add_mac_filter(vsi, addr))
  1529. return 0;
  1530. else
  1531. return -ENOMEM;
  1532. }
  1533. /**
  1534. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1535. * @netdev: the netdevice
  1536. * @addr: address to add
  1537. *
  1538. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1539. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1540. */
  1541. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1542. {
  1543. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1544. struct i40e_vsi *vsi = np->vsi;
  1545. i40e_del_mac_filter(vsi, addr);
  1546. return 0;
  1547. }
  1548. /**
  1549. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1550. * @netdev: network interface device structure
  1551. **/
  1552. static void i40e_set_rx_mode(struct net_device *netdev)
  1553. {
  1554. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1555. struct i40e_vsi *vsi = np->vsi;
  1556. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1557. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1558. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1559. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1560. /* check for other flag changes */
  1561. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1562. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1563. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1564. }
  1565. /* schedule our worker thread which will take care of
  1566. * applying the new filter changes
  1567. */
  1568. i40e_service_event_schedule(vsi->back);
  1569. }
  1570. /**
  1571. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1572. * @vsi: Pointer to VSI struct
  1573. * @from: Pointer to list which contains MAC filter entries - changes to
  1574. * those entries needs to be undone.
  1575. *
  1576. * MAC filter entries from this list were slated for deletion.
  1577. **/
  1578. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1579. struct hlist_head *from)
  1580. {
  1581. struct i40e_mac_filter *f;
  1582. struct hlist_node *h;
  1583. hlist_for_each_entry_safe(f, h, from, hlist) {
  1584. u64 key = i40e_addr_to_hkey(f->macaddr);
  1585. /* Move the element back into MAC filter list*/
  1586. hlist_del(&f->hlist);
  1587. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1588. }
  1589. }
  1590. /**
  1591. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1592. * @vsi: Pointer to vsi struct
  1593. * @from: Pointer to list which contains MAC filter entries - changes to
  1594. * those entries needs to be undone.
  1595. *
  1596. * MAC filter entries from this list were slated for addition.
  1597. **/
  1598. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1599. struct hlist_head *from)
  1600. {
  1601. struct i40e_new_mac_filter *new;
  1602. struct hlist_node *h;
  1603. hlist_for_each_entry_safe(new, h, from, hlist) {
  1604. /* We can simply free the wrapper structure */
  1605. hlist_del(&new->hlist);
  1606. kfree(new);
  1607. }
  1608. }
  1609. /**
  1610. * i40e_next_entry - Get the next non-broadcast filter from a list
  1611. * @next: pointer to filter in list
  1612. *
  1613. * Returns the next non-broadcast filter in the list. Required so that we
  1614. * ignore broadcast filters within the list, since these are not handled via
  1615. * the normal firmware update path.
  1616. */
  1617. static
  1618. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1619. {
  1620. hlist_for_each_entry_continue(next, hlist) {
  1621. if (!is_broadcast_ether_addr(next->f->macaddr))
  1622. return next;
  1623. }
  1624. return NULL;
  1625. }
  1626. /**
  1627. * i40e_update_filter_state - Update filter state based on return data
  1628. * from firmware
  1629. * @count: Number of filters added
  1630. * @add_list: return data from fw
  1631. * @head: pointer to first filter in current batch
  1632. *
  1633. * MAC filter entries from list were slated to be added to device. Returns
  1634. * number of successful filters. Note that 0 does NOT mean success!
  1635. **/
  1636. static int
  1637. i40e_update_filter_state(int count,
  1638. struct i40e_aqc_add_macvlan_element_data *add_list,
  1639. struct i40e_new_mac_filter *add_head)
  1640. {
  1641. int retval = 0;
  1642. int i;
  1643. for (i = 0; i < count; i++) {
  1644. /* Always check status of each filter. We don't need to check
  1645. * the firmware return status because we pre-set the filter
  1646. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1647. * request to the adminq. Thus, if it no longer matches then
  1648. * we know the filter is active.
  1649. */
  1650. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1651. add_head->state = I40E_FILTER_FAILED;
  1652. } else {
  1653. add_head->state = I40E_FILTER_ACTIVE;
  1654. retval++;
  1655. }
  1656. add_head = i40e_next_filter(add_head);
  1657. if (!add_head)
  1658. break;
  1659. }
  1660. return retval;
  1661. }
  1662. /**
  1663. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1664. * @vsi: ptr to the VSI
  1665. * @vsi_name: name to display in messages
  1666. * @list: the list of filters to send to firmware
  1667. * @num_del: the number of filters to delete
  1668. * @retval: Set to -EIO on failure to delete
  1669. *
  1670. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1671. * *retval instead of a return value so that success does not force ret_val to
  1672. * be set to 0. This ensures that a sequence of calls to this function
  1673. * preserve the previous value of *retval on successful delete.
  1674. */
  1675. static
  1676. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1677. struct i40e_aqc_remove_macvlan_element_data *list,
  1678. int num_del, int *retval)
  1679. {
  1680. struct i40e_hw *hw = &vsi->back->hw;
  1681. i40e_status aq_ret;
  1682. int aq_err;
  1683. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1684. aq_err = hw->aq.asq_last_status;
  1685. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1686. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1687. *retval = -EIO;
  1688. dev_info(&vsi->back->pdev->dev,
  1689. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1690. vsi_name, i40e_stat_str(hw, aq_ret),
  1691. i40e_aq_str(hw, aq_err));
  1692. }
  1693. }
  1694. /**
  1695. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1696. * @vsi: ptr to the VSI
  1697. * @vsi_name: name to display in messages
  1698. * @list: the list of filters to send to firmware
  1699. * @add_head: Position in the add hlist
  1700. * @num_add: the number of filters to add
  1701. * @promisc_change: set to true on exit if promiscuous mode was forced on
  1702. *
  1703. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1704. * promisc_changed to true if the firmware has run out of space for more
  1705. * filters.
  1706. */
  1707. static
  1708. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1709. struct i40e_aqc_add_macvlan_element_data *list,
  1710. struct i40e_new_mac_filter *add_head,
  1711. int num_add, bool *promisc_changed)
  1712. {
  1713. struct i40e_hw *hw = &vsi->back->hw;
  1714. int aq_err, fcnt;
  1715. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1716. aq_err = hw->aq.asq_last_status;
  1717. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1718. if (fcnt != num_add) {
  1719. *promisc_changed = true;
  1720. set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1721. dev_warn(&vsi->back->pdev->dev,
  1722. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1723. i40e_aq_str(hw, aq_err),
  1724. vsi_name);
  1725. }
  1726. }
  1727. /**
  1728. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1729. * @vsi: pointer to the VSI
  1730. * @f: filter data
  1731. *
  1732. * This function sets or clears the promiscuous broadcast flags for VLAN
  1733. * filters in order to properly receive broadcast frames. Assumes that only
  1734. * broadcast filters are passed.
  1735. *
  1736. * Returns status indicating success or failure;
  1737. **/
  1738. static i40e_status
  1739. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1740. struct i40e_mac_filter *f)
  1741. {
  1742. bool enable = f->state == I40E_FILTER_NEW;
  1743. struct i40e_hw *hw = &vsi->back->hw;
  1744. i40e_status aq_ret;
  1745. if (f->vlan == I40E_VLAN_ANY) {
  1746. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1747. vsi->seid,
  1748. enable,
  1749. NULL);
  1750. } else {
  1751. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1752. vsi->seid,
  1753. enable,
  1754. f->vlan,
  1755. NULL);
  1756. }
  1757. if (aq_ret)
  1758. dev_warn(&vsi->back->pdev->dev,
  1759. "Error %s setting broadcast promiscuous mode on %s\n",
  1760. i40e_aq_str(hw, hw->aq.asq_last_status),
  1761. vsi_name);
  1762. return aq_ret;
  1763. }
  1764. /**
  1765. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1766. * @vsi: ptr to the VSI
  1767. *
  1768. * Push any outstanding VSI filter changes through the AdminQ.
  1769. *
  1770. * Returns 0 or error value
  1771. **/
  1772. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1773. {
  1774. struct hlist_head tmp_add_list, tmp_del_list;
  1775. struct i40e_mac_filter *f;
  1776. struct i40e_new_mac_filter *new, *add_head = NULL;
  1777. struct i40e_hw *hw = &vsi->back->hw;
  1778. unsigned int failed_filters = 0;
  1779. unsigned int vlan_filters = 0;
  1780. bool promisc_changed = false;
  1781. char vsi_name[16] = "PF";
  1782. int filter_list_len = 0;
  1783. i40e_status aq_ret = 0;
  1784. u32 changed_flags = 0;
  1785. struct hlist_node *h;
  1786. struct i40e_pf *pf;
  1787. int num_add = 0;
  1788. int num_del = 0;
  1789. int retval = 0;
  1790. u16 cmd_flags;
  1791. int list_size;
  1792. int bkt;
  1793. /* empty array typed pointers, kcalloc later */
  1794. struct i40e_aqc_add_macvlan_element_data *add_list;
  1795. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1796. while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
  1797. usleep_range(1000, 2000);
  1798. pf = vsi->back;
  1799. if (vsi->netdev) {
  1800. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1801. vsi->current_netdev_flags = vsi->netdev->flags;
  1802. }
  1803. INIT_HLIST_HEAD(&tmp_add_list);
  1804. INIT_HLIST_HEAD(&tmp_del_list);
  1805. if (vsi->type == I40E_VSI_SRIOV)
  1806. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  1807. else if (vsi->type != I40E_VSI_MAIN)
  1808. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  1809. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1810. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1811. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1812. /* Create a list of filters to delete. */
  1813. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1814. if (f->state == I40E_FILTER_REMOVE) {
  1815. /* Move the element into temporary del_list */
  1816. hash_del(&f->hlist);
  1817. hlist_add_head(&f->hlist, &tmp_del_list);
  1818. /* Avoid counting removed filters */
  1819. continue;
  1820. }
  1821. if (f->state == I40E_FILTER_NEW) {
  1822. /* Create a temporary i40e_new_mac_filter */
  1823. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1824. if (!new)
  1825. goto err_no_memory_locked;
  1826. /* Store pointer to the real filter */
  1827. new->f = f;
  1828. new->state = f->state;
  1829. /* Add it to the hash list */
  1830. hlist_add_head(&new->hlist, &tmp_add_list);
  1831. }
  1832. /* Count the number of active (current and new) VLAN
  1833. * filters we have now. Does not count filters which
  1834. * are marked for deletion.
  1835. */
  1836. if (f->vlan > 0)
  1837. vlan_filters++;
  1838. }
  1839. retval = i40e_correct_mac_vlan_filters(vsi,
  1840. &tmp_add_list,
  1841. &tmp_del_list,
  1842. vlan_filters);
  1843. if (retval)
  1844. goto err_no_memory_locked;
  1845. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1846. }
  1847. /* Now process 'del_list' outside the lock */
  1848. if (!hlist_empty(&tmp_del_list)) {
  1849. filter_list_len = hw->aq.asq_buf_size /
  1850. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1851. list_size = filter_list_len *
  1852. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1853. del_list = kzalloc(list_size, GFP_ATOMIC);
  1854. if (!del_list)
  1855. goto err_no_memory;
  1856. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  1857. cmd_flags = 0;
  1858. /* handle broadcast filters by updating the broadcast
  1859. * promiscuous flag and release filter list.
  1860. */
  1861. if (is_broadcast_ether_addr(f->macaddr)) {
  1862. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  1863. hlist_del(&f->hlist);
  1864. kfree(f);
  1865. continue;
  1866. }
  1867. /* add to delete list */
  1868. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  1869. if (f->vlan == I40E_VLAN_ANY) {
  1870. del_list[num_del].vlan_tag = 0;
  1871. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1872. } else {
  1873. del_list[num_del].vlan_tag =
  1874. cpu_to_le16((u16)(f->vlan));
  1875. }
  1876. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1877. del_list[num_del].flags = cmd_flags;
  1878. num_del++;
  1879. /* flush a full buffer */
  1880. if (num_del == filter_list_len) {
  1881. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1882. num_del, &retval);
  1883. memset(del_list, 0, list_size);
  1884. num_del = 0;
  1885. }
  1886. /* Release memory for MAC filter entries which were
  1887. * synced up with HW.
  1888. */
  1889. hlist_del(&f->hlist);
  1890. kfree(f);
  1891. }
  1892. if (num_del) {
  1893. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1894. num_del, &retval);
  1895. }
  1896. kfree(del_list);
  1897. del_list = NULL;
  1898. }
  1899. if (!hlist_empty(&tmp_add_list)) {
  1900. /* Do all the adds now. */
  1901. filter_list_len = hw->aq.asq_buf_size /
  1902. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1903. list_size = filter_list_len *
  1904. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1905. add_list = kzalloc(list_size, GFP_ATOMIC);
  1906. if (!add_list)
  1907. goto err_no_memory;
  1908. num_add = 0;
  1909. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1910. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1911. &vsi->state)) {
  1912. new->state = I40E_FILTER_FAILED;
  1913. continue;
  1914. }
  1915. /* handle broadcast filters by updating the broadcast
  1916. * promiscuous flag instead of adding a MAC filter.
  1917. */
  1918. if (is_broadcast_ether_addr(new->f->macaddr)) {
  1919. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  1920. new->f))
  1921. new->state = I40E_FILTER_FAILED;
  1922. else
  1923. new->state = I40E_FILTER_ACTIVE;
  1924. continue;
  1925. }
  1926. /* add to add array */
  1927. if (num_add == 0)
  1928. add_head = new;
  1929. cmd_flags = 0;
  1930. ether_addr_copy(add_list[num_add].mac_addr,
  1931. new->f->macaddr);
  1932. if (new->f->vlan == I40E_VLAN_ANY) {
  1933. add_list[num_add].vlan_tag = 0;
  1934. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  1935. } else {
  1936. add_list[num_add].vlan_tag =
  1937. cpu_to_le16((u16)(new->f->vlan));
  1938. }
  1939. add_list[num_add].queue_number = 0;
  1940. /* set invalid match method for later detection */
  1941. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  1942. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  1943. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  1944. num_add++;
  1945. /* flush a full buffer */
  1946. if (num_add == filter_list_len) {
  1947. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  1948. add_head, num_add,
  1949. &promisc_changed);
  1950. memset(add_list, 0, list_size);
  1951. num_add = 0;
  1952. }
  1953. }
  1954. if (num_add) {
  1955. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  1956. num_add, &promisc_changed);
  1957. }
  1958. /* Now move all of the filters from the temp add list back to
  1959. * the VSI's list.
  1960. */
  1961. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1962. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1963. /* Only update the state if we're still NEW */
  1964. if (new->f->state == I40E_FILTER_NEW)
  1965. new->f->state = new->state;
  1966. hlist_del(&new->hlist);
  1967. kfree(new);
  1968. }
  1969. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1970. kfree(add_list);
  1971. add_list = NULL;
  1972. }
  1973. /* Determine the number of active and failed filters. */
  1974. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1975. vsi->active_filters = 0;
  1976. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1977. if (f->state == I40E_FILTER_ACTIVE)
  1978. vsi->active_filters++;
  1979. else if (f->state == I40E_FILTER_FAILED)
  1980. failed_filters++;
  1981. }
  1982. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1983. /* If promiscuous mode has changed, we need to calculate a new
  1984. * threshold for when we are safe to exit
  1985. */
  1986. if (promisc_changed)
  1987. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  1988. /* Check if we are able to exit overflow promiscuous mode. We can
  1989. * safely exit if we didn't just enter, we no longer have any failed
  1990. * filters, and we have reduced filters below the threshold value.
  1991. */
  1992. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
  1993. !promisc_changed && !failed_filters &&
  1994. (vsi->active_filters < vsi->promisc_threshold)) {
  1995. dev_info(&pf->pdev->dev,
  1996. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  1997. vsi_name);
  1998. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1999. promisc_changed = true;
  2000. vsi->promisc_threshold = 0;
  2001. }
  2002. /* if the VF is not trusted do not do promisc */
  2003. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2004. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  2005. goto out;
  2006. }
  2007. /* check for changes in promiscuous modes */
  2008. if (changed_flags & IFF_ALLMULTI) {
  2009. bool cur_multipromisc;
  2010. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2011. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2012. vsi->seid,
  2013. cur_multipromisc,
  2014. NULL);
  2015. if (aq_ret) {
  2016. retval = i40e_aq_rc_to_posix(aq_ret,
  2017. hw->aq.asq_last_status);
  2018. dev_info(&pf->pdev->dev,
  2019. "set multi promisc failed on %s, err %s aq_err %s\n",
  2020. vsi_name,
  2021. i40e_stat_str(hw, aq_ret),
  2022. i40e_aq_str(hw, hw->aq.asq_last_status));
  2023. }
  2024. }
  2025. if ((changed_flags & IFF_PROMISC) ||
  2026. (promisc_changed &&
  2027. test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
  2028. bool cur_promisc;
  2029. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2030. test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  2031. &vsi->state));
  2032. if ((vsi->type == I40E_VSI_MAIN) &&
  2033. (pf->lan_veb != I40E_NO_VEB) &&
  2034. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  2035. /* set defport ON for Main VSI instead of true promisc
  2036. * this way we will get all unicast/multicast and VLAN
  2037. * promisc behavior but will not get VF or VMDq traffic
  2038. * replicated on the Main VSI.
  2039. */
  2040. if (pf->cur_promisc != cur_promisc) {
  2041. pf->cur_promisc = cur_promisc;
  2042. if (cur_promisc)
  2043. aq_ret =
  2044. i40e_aq_set_default_vsi(hw,
  2045. vsi->seid,
  2046. NULL);
  2047. else
  2048. aq_ret =
  2049. i40e_aq_clear_default_vsi(hw,
  2050. vsi->seid,
  2051. NULL);
  2052. if (aq_ret) {
  2053. retval = i40e_aq_rc_to_posix(aq_ret,
  2054. hw->aq.asq_last_status);
  2055. dev_info(&pf->pdev->dev,
  2056. "Set default VSI failed on %s, err %s, aq_err %s\n",
  2057. vsi_name,
  2058. i40e_stat_str(hw, aq_ret),
  2059. i40e_aq_str(hw,
  2060. hw->aq.asq_last_status));
  2061. }
  2062. }
  2063. } else {
  2064. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  2065. hw,
  2066. vsi->seid,
  2067. cur_promisc, NULL,
  2068. true);
  2069. if (aq_ret) {
  2070. retval =
  2071. i40e_aq_rc_to_posix(aq_ret,
  2072. hw->aq.asq_last_status);
  2073. dev_info(&pf->pdev->dev,
  2074. "set unicast promisc failed on %s, err %s, aq_err %s\n",
  2075. vsi_name,
  2076. i40e_stat_str(hw, aq_ret),
  2077. i40e_aq_str(hw,
  2078. hw->aq.asq_last_status));
  2079. }
  2080. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  2081. hw,
  2082. vsi->seid,
  2083. cur_promisc, NULL);
  2084. if (aq_ret) {
  2085. retval =
  2086. i40e_aq_rc_to_posix(aq_ret,
  2087. hw->aq.asq_last_status);
  2088. dev_info(&pf->pdev->dev,
  2089. "set multicast promisc failed on %s, err %s, aq_err %s\n",
  2090. vsi_name,
  2091. i40e_stat_str(hw, aq_ret),
  2092. i40e_aq_str(hw,
  2093. hw->aq.asq_last_status));
  2094. }
  2095. }
  2096. aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
  2097. vsi->seid,
  2098. cur_promisc, NULL);
  2099. if (aq_ret) {
  2100. retval = i40e_aq_rc_to_posix(aq_ret,
  2101. pf->hw.aq.asq_last_status);
  2102. dev_info(&pf->pdev->dev,
  2103. "set brdcast promisc failed, err %s, aq_err %s\n",
  2104. i40e_stat_str(hw, aq_ret),
  2105. i40e_aq_str(hw,
  2106. hw->aq.asq_last_status));
  2107. }
  2108. }
  2109. out:
  2110. /* if something went wrong then set the changed flag so we try again */
  2111. if (retval)
  2112. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2113. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2114. return retval;
  2115. err_no_memory:
  2116. /* Restore elements on the temporary add and delete lists */
  2117. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2118. err_no_memory_locked:
  2119. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2120. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2121. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2122. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2123. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2124. return -ENOMEM;
  2125. }
  2126. /**
  2127. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2128. * @pf: board private structure
  2129. **/
  2130. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2131. {
  2132. int v;
  2133. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  2134. return;
  2135. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  2136. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2137. if (pf->vsi[v] &&
  2138. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2139. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2140. if (ret) {
  2141. /* come back and try again later */
  2142. pf->flags |= I40E_FLAG_FILTER_SYNC;
  2143. break;
  2144. }
  2145. }
  2146. }
  2147. }
  2148. /**
  2149. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2150. * @netdev: network interface device structure
  2151. * @new_mtu: new value for maximum frame size
  2152. *
  2153. * Returns 0 on success, negative on failure
  2154. **/
  2155. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2156. {
  2157. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2158. struct i40e_vsi *vsi = np->vsi;
  2159. struct i40e_pf *pf = vsi->back;
  2160. netdev_info(netdev, "changing MTU from %d to %d\n",
  2161. netdev->mtu, new_mtu);
  2162. netdev->mtu = new_mtu;
  2163. if (netif_running(netdev))
  2164. i40e_vsi_reinit_locked(vsi);
  2165. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  2166. I40E_FLAG_CLIENT_L2_CHANGE);
  2167. return 0;
  2168. }
  2169. /**
  2170. * i40e_ioctl - Access the hwtstamp interface
  2171. * @netdev: network interface device structure
  2172. * @ifr: interface request data
  2173. * @cmd: ioctl command
  2174. **/
  2175. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2176. {
  2177. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2178. struct i40e_pf *pf = np->vsi->back;
  2179. switch (cmd) {
  2180. case SIOCGHWTSTAMP:
  2181. return i40e_ptp_get_ts_config(pf, ifr);
  2182. case SIOCSHWTSTAMP:
  2183. return i40e_ptp_set_ts_config(pf, ifr);
  2184. default:
  2185. return -EOPNOTSUPP;
  2186. }
  2187. }
  2188. /**
  2189. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2190. * @vsi: the vsi being adjusted
  2191. **/
  2192. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2193. {
  2194. struct i40e_vsi_context ctxt;
  2195. i40e_status ret;
  2196. if ((vsi->info.valid_sections &
  2197. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2198. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2199. return; /* already enabled */
  2200. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2201. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2202. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2203. ctxt.seid = vsi->seid;
  2204. ctxt.info = vsi->info;
  2205. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2206. if (ret) {
  2207. dev_info(&vsi->back->pdev->dev,
  2208. "update vlan stripping failed, err %s aq_err %s\n",
  2209. i40e_stat_str(&vsi->back->hw, ret),
  2210. i40e_aq_str(&vsi->back->hw,
  2211. vsi->back->hw.aq.asq_last_status));
  2212. }
  2213. }
  2214. /**
  2215. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2216. * @vsi: the vsi being adjusted
  2217. **/
  2218. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2219. {
  2220. struct i40e_vsi_context ctxt;
  2221. i40e_status ret;
  2222. if ((vsi->info.valid_sections &
  2223. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2224. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2225. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2226. return; /* already disabled */
  2227. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2228. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2229. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2230. ctxt.seid = vsi->seid;
  2231. ctxt.info = vsi->info;
  2232. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2233. if (ret) {
  2234. dev_info(&vsi->back->pdev->dev,
  2235. "update vlan stripping failed, err %s aq_err %s\n",
  2236. i40e_stat_str(&vsi->back->hw, ret),
  2237. i40e_aq_str(&vsi->back->hw,
  2238. vsi->back->hw.aq.asq_last_status));
  2239. }
  2240. }
  2241. /**
  2242. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  2243. * @netdev: network interface to be adjusted
  2244. * @features: netdev features to test if VLAN offload is enabled or not
  2245. **/
  2246. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  2247. {
  2248. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2249. struct i40e_vsi *vsi = np->vsi;
  2250. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2251. i40e_vlan_stripping_enable(vsi);
  2252. else
  2253. i40e_vlan_stripping_disable(vsi);
  2254. }
  2255. /**
  2256. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2257. * @vsi: the vsi being configured
  2258. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2259. *
  2260. * This is a helper function for adding a new MAC/VLAN filter with the
  2261. * specified VLAN for each existing MAC address already in the hash table.
  2262. * This function does *not* perform any accounting to update filters based on
  2263. * VLAN mode.
  2264. *
  2265. * NOTE: this function expects to be called while under the
  2266. * mac_filter_hash_lock
  2267. **/
  2268. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2269. {
  2270. struct i40e_mac_filter *f, *add_f;
  2271. struct hlist_node *h;
  2272. int bkt;
  2273. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2274. if (f->state == I40E_FILTER_REMOVE)
  2275. continue;
  2276. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2277. if (!add_f) {
  2278. dev_info(&vsi->back->pdev->dev,
  2279. "Could not add vlan filter %d for %pM\n",
  2280. vid, f->macaddr);
  2281. return -ENOMEM;
  2282. }
  2283. }
  2284. return 0;
  2285. }
  2286. /**
  2287. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2288. * @vsi: the VSI being configured
  2289. * @vid: VLAN id to be added
  2290. **/
  2291. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2292. {
  2293. int err;
  2294. if (!vid || vsi->info.pvid)
  2295. return -EINVAL;
  2296. /* Locked once because all functions invoked below iterates list*/
  2297. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2298. err = i40e_add_vlan_all_mac(vsi, vid);
  2299. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2300. if (err)
  2301. return err;
  2302. /* schedule our worker thread which will take care of
  2303. * applying the new filter changes
  2304. */
  2305. i40e_service_event_schedule(vsi->back);
  2306. return 0;
  2307. }
  2308. /**
  2309. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2310. * @vsi: the vsi being configured
  2311. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2312. *
  2313. * This function should be used to remove all VLAN filters which match the
  2314. * given VID. It does not schedule the service event and does not take the
  2315. * mac_filter_hash_lock so it may be combined with other operations under
  2316. * a single invocation of the mac_filter_hash_lock.
  2317. *
  2318. * NOTE: this function expects to be called while under the
  2319. * mac_filter_hash_lock
  2320. */
  2321. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2322. {
  2323. struct i40e_mac_filter *f;
  2324. struct hlist_node *h;
  2325. int bkt;
  2326. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2327. if (f->vlan == vid)
  2328. __i40e_del_filter(vsi, f);
  2329. }
  2330. }
  2331. /**
  2332. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2333. * @vsi: the VSI being configured
  2334. * @vid: VLAN id to be removed
  2335. **/
  2336. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2337. {
  2338. if (!vid || vsi->info.pvid)
  2339. return;
  2340. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2341. i40e_rm_vlan_all_mac(vsi, vid);
  2342. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2343. /* schedule our worker thread which will take care of
  2344. * applying the new filter changes
  2345. */
  2346. i40e_service_event_schedule(vsi->back);
  2347. }
  2348. /**
  2349. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2350. * @netdev: network interface to be adjusted
  2351. * @vid: vlan id to be added
  2352. *
  2353. * net_device_ops implementation for adding vlan ids
  2354. **/
  2355. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2356. __always_unused __be16 proto, u16 vid)
  2357. {
  2358. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2359. struct i40e_vsi *vsi = np->vsi;
  2360. int ret = 0;
  2361. if (vid >= VLAN_N_VID)
  2362. return -EINVAL;
  2363. /* If the network stack called us with vid = 0 then
  2364. * it is asking to receive priority tagged packets with
  2365. * vlan id 0. Our HW receives them by default when configured
  2366. * to receive untagged packets so there is no need to add an
  2367. * extra filter for vlan 0 tagged packets.
  2368. */
  2369. if (vid)
  2370. ret = i40e_vsi_add_vlan(vsi, vid);
  2371. if (!ret)
  2372. set_bit(vid, vsi->active_vlans);
  2373. return ret;
  2374. }
  2375. /**
  2376. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2377. * @netdev: network interface to be adjusted
  2378. * @vid: vlan id to be removed
  2379. *
  2380. * net_device_ops implementation for removing vlan ids
  2381. **/
  2382. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2383. __always_unused __be16 proto, u16 vid)
  2384. {
  2385. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2386. struct i40e_vsi *vsi = np->vsi;
  2387. /* return code is ignored as there is nothing a user
  2388. * can do about failure to remove and a log message was
  2389. * already printed from the other function
  2390. */
  2391. i40e_vsi_kill_vlan(vsi, vid);
  2392. clear_bit(vid, vsi->active_vlans);
  2393. return 0;
  2394. }
  2395. /**
  2396. * i40e_macaddr_init - explicitly write the mac address filters
  2397. *
  2398. * @vsi: pointer to the vsi
  2399. * @macaddr: the MAC address
  2400. *
  2401. * This is needed when the macaddr has been obtained by other
  2402. * means than the default, e.g., from Open Firmware or IDPROM.
  2403. * Returns 0 on success, negative on failure
  2404. **/
  2405. static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
  2406. {
  2407. int ret;
  2408. struct i40e_aqc_add_macvlan_element_data element;
  2409. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  2410. I40E_AQC_WRITE_TYPE_LAA_WOL,
  2411. macaddr, NULL);
  2412. if (ret) {
  2413. dev_info(&vsi->back->pdev->dev,
  2414. "Addr change for VSI failed: %d\n", ret);
  2415. return -EADDRNOTAVAIL;
  2416. }
  2417. memset(&element, 0, sizeof(element));
  2418. ether_addr_copy(element.mac_addr, macaddr);
  2419. element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
  2420. ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
  2421. if (ret) {
  2422. dev_info(&vsi->back->pdev->dev,
  2423. "add filter failed err %s aq_err %s\n",
  2424. i40e_stat_str(&vsi->back->hw, ret),
  2425. i40e_aq_str(&vsi->back->hw,
  2426. vsi->back->hw.aq.asq_last_status));
  2427. }
  2428. return ret;
  2429. }
  2430. /**
  2431. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2432. * @vsi: the vsi being brought back up
  2433. **/
  2434. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2435. {
  2436. u16 vid;
  2437. if (!vsi->netdev)
  2438. return;
  2439. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  2440. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2441. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  2442. vid);
  2443. }
  2444. /**
  2445. * i40e_vsi_add_pvid - Add pvid for the VSI
  2446. * @vsi: the vsi being adjusted
  2447. * @vid: the vlan id to set as a PVID
  2448. **/
  2449. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2450. {
  2451. struct i40e_vsi_context ctxt;
  2452. i40e_status ret;
  2453. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2454. vsi->info.pvid = cpu_to_le16(vid);
  2455. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2456. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2457. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2458. ctxt.seid = vsi->seid;
  2459. ctxt.info = vsi->info;
  2460. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2461. if (ret) {
  2462. dev_info(&vsi->back->pdev->dev,
  2463. "add pvid failed, err %s aq_err %s\n",
  2464. i40e_stat_str(&vsi->back->hw, ret),
  2465. i40e_aq_str(&vsi->back->hw,
  2466. vsi->back->hw.aq.asq_last_status));
  2467. return -ENOENT;
  2468. }
  2469. return 0;
  2470. }
  2471. /**
  2472. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2473. * @vsi: the vsi being adjusted
  2474. *
  2475. * Just use the vlan_rx_register() service to put it back to normal
  2476. **/
  2477. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2478. {
  2479. i40e_vlan_stripping_disable(vsi);
  2480. vsi->info.pvid = 0;
  2481. }
  2482. /**
  2483. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2484. * @vsi: ptr to the VSI
  2485. *
  2486. * If this function returns with an error, then it's possible one or
  2487. * more of the rings is populated (while the rest are not). It is the
  2488. * callers duty to clean those orphaned rings.
  2489. *
  2490. * Return 0 on success, negative on failure
  2491. **/
  2492. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2493. {
  2494. int i, err = 0;
  2495. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2496. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2497. return err;
  2498. }
  2499. /**
  2500. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2501. * @vsi: ptr to the VSI
  2502. *
  2503. * Free VSI's transmit software resources
  2504. **/
  2505. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2506. {
  2507. int i;
  2508. if (!vsi->tx_rings)
  2509. return;
  2510. for (i = 0; i < vsi->num_queue_pairs; i++)
  2511. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2512. i40e_free_tx_resources(vsi->tx_rings[i]);
  2513. }
  2514. /**
  2515. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2516. * @vsi: ptr to the VSI
  2517. *
  2518. * If this function returns with an error, then it's possible one or
  2519. * more of the rings is populated (while the rest are not). It is the
  2520. * callers duty to clean those orphaned rings.
  2521. *
  2522. * Return 0 on success, negative on failure
  2523. **/
  2524. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2525. {
  2526. int i, err = 0;
  2527. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2528. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2529. return err;
  2530. }
  2531. /**
  2532. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2533. * @vsi: ptr to the VSI
  2534. *
  2535. * Free all receive software resources
  2536. **/
  2537. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2538. {
  2539. int i;
  2540. if (!vsi->rx_rings)
  2541. return;
  2542. for (i = 0; i < vsi->num_queue_pairs; i++)
  2543. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2544. i40e_free_rx_resources(vsi->rx_rings[i]);
  2545. }
  2546. /**
  2547. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2548. * @ring: The Tx ring to configure
  2549. *
  2550. * This enables/disables XPS for a given Tx descriptor ring
  2551. * based on the TCs enabled for the VSI that ring belongs to.
  2552. **/
  2553. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2554. {
  2555. struct i40e_vsi *vsi = ring->vsi;
  2556. cpumask_var_t mask;
  2557. if (!ring->q_vector || !ring->netdev)
  2558. return;
  2559. /* Single TC mode enable XPS */
  2560. if (vsi->tc_config.numtc <= 1) {
  2561. if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
  2562. netif_set_xps_queue(ring->netdev,
  2563. &ring->q_vector->affinity_mask,
  2564. ring->queue_index);
  2565. } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
  2566. /* Disable XPS to allow selection based on TC */
  2567. bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
  2568. netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
  2569. free_cpumask_var(mask);
  2570. }
  2571. /* schedule our worker thread which will take care of
  2572. * applying the new filter changes
  2573. */
  2574. i40e_service_event_schedule(vsi->back);
  2575. }
  2576. /**
  2577. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2578. * @ring: The Tx ring to configure
  2579. *
  2580. * Configure the Tx descriptor ring in the HMC context.
  2581. **/
  2582. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2583. {
  2584. struct i40e_vsi *vsi = ring->vsi;
  2585. u16 pf_q = vsi->base_queue + ring->queue_index;
  2586. struct i40e_hw *hw = &vsi->back->hw;
  2587. struct i40e_hmc_obj_txq tx_ctx;
  2588. i40e_status err = 0;
  2589. u32 qtx_ctl = 0;
  2590. /* some ATR related tx ring init */
  2591. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2592. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2593. ring->atr_count = 0;
  2594. } else {
  2595. ring->atr_sample_rate = 0;
  2596. }
  2597. /* configure XPS */
  2598. i40e_config_xps_tx_ring(ring);
  2599. /* clear the context structure first */
  2600. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2601. tx_ctx.new_context = 1;
  2602. tx_ctx.base = (ring->dma / 128);
  2603. tx_ctx.qlen = ring->count;
  2604. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2605. I40E_FLAG_FD_ATR_ENABLED));
  2606. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2607. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2608. if (vsi->type != I40E_VSI_FDIR)
  2609. tx_ctx.head_wb_ena = 1;
  2610. tx_ctx.head_wb_addr = ring->dma +
  2611. (ring->count * sizeof(struct i40e_tx_desc));
  2612. /* As part of VSI creation/update, FW allocates certain
  2613. * Tx arbitration queue sets for each TC enabled for
  2614. * the VSI. The FW returns the handles to these queue
  2615. * sets as part of the response buffer to Add VSI,
  2616. * Update VSI, etc. AQ commands. It is expected that
  2617. * these queue set handles be associated with the Tx
  2618. * queues by the driver as part of the TX queue context
  2619. * initialization. This has to be done regardless of
  2620. * DCB as by default everything is mapped to TC0.
  2621. */
  2622. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2623. tx_ctx.rdylist_act = 0;
  2624. /* clear the context in the HMC */
  2625. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2626. if (err) {
  2627. dev_info(&vsi->back->pdev->dev,
  2628. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2629. ring->queue_index, pf_q, err);
  2630. return -ENOMEM;
  2631. }
  2632. /* set the context in the HMC */
  2633. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2634. if (err) {
  2635. dev_info(&vsi->back->pdev->dev,
  2636. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2637. ring->queue_index, pf_q, err);
  2638. return -ENOMEM;
  2639. }
  2640. /* Now associate this queue with this PCI function */
  2641. if (vsi->type == I40E_VSI_VMDQ2) {
  2642. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2643. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2644. I40E_QTX_CTL_VFVM_INDX_MASK;
  2645. } else {
  2646. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2647. }
  2648. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2649. I40E_QTX_CTL_PF_INDX_MASK);
  2650. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2651. i40e_flush(hw);
  2652. /* cache tail off for easier writes later */
  2653. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2654. return 0;
  2655. }
  2656. /**
  2657. * i40e_configure_rx_ring - Configure a receive ring context
  2658. * @ring: The Rx ring to configure
  2659. *
  2660. * Configure the Rx descriptor ring in the HMC context.
  2661. **/
  2662. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2663. {
  2664. struct i40e_vsi *vsi = ring->vsi;
  2665. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2666. u16 pf_q = vsi->base_queue + ring->queue_index;
  2667. struct i40e_hw *hw = &vsi->back->hw;
  2668. struct i40e_hmc_obj_rxq rx_ctx;
  2669. i40e_status err = 0;
  2670. ring->state = 0;
  2671. /* clear the context structure first */
  2672. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2673. ring->rx_buf_len = vsi->rx_buf_len;
  2674. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2675. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2676. rx_ctx.base = (ring->dma / 128);
  2677. rx_ctx.qlen = ring->count;
  2678. /* use 32 byte descriptors */
  2679. rx_ctx.dsize = 1;
  2680. /* descriptor type is always zero
  2681. * rx_ctx.dtype = 0;
  2682. */
  2683. rx_ctx.hsplit_0 = 0;
  2684. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2685. if (hw->revision_id == 0)
  2686. rx_ctx.lrxqthresh = 0;
  2687. else
  2688. rx_ctx.lrxqthresh = 2;
  2689. rx_ctx.crcstrip = 1;
  2690. rx_ctx.l2tsel = 1;
  2691. /* this controls whether VLAN is stripped from inner headers */
  2692. rx_ctx.showiv = 0;
  2693. /* set the prefena field to 1 because the manual says to */
  2694. rx_ctx.prefena = 1;
  2695. /* clear the context in the HMC */
  2696. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2697. if (err) {
  2698. dev_info(&vsi->back->pdev->dev,
  2699. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2700. ring->queue_index, pf_q, err);
  2701. return -ENOMEM;
  2702. }
  2703. /* set the context in the HMC */
  2704. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2705. if (err) {
  2706. dev_info(&vsi->back->pdev->dev,
  2707. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2708. ring->queue_index, pf_q, err);
  2709. return -ENOMEM;
  2710. }
  2711. /* configure Rx buffer alignment */
  2712. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2713. clear_ring_build_skb_enabled(ring);
  2714. else
  2715. set_ring_build_skb_enabled(ring);
  2716. /* cache tail for quicker writes, and clear the reg before use */
  2717. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2718. writel(0, ring->tail);
  2719. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2720. return 0;
  2721. }
  2722. /**
  2723. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2724. * @vsi: VSI structure describing this set of rings and resources
  2725. *
  2726. * Configure the Tx VSI for operation.
  2727. **/
  2728. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2729. {
  2730. int err = 0;
  2731. u16 i;
  2732. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2733. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2734. return err;
  2735. }
  2736. /**
  2737. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2738. * @vsi: the VSI being configured
  2739. *
  2740. * Configure the Rx VSI for operation.
  2741. **/
  2742. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2743. {
  2744. int err = 0;
  2745. u16 i;
  2746. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2747. vsi->max_frame = I40E_MAX_RXBUFFER;
  2748. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2749. #if (PAGE_SIZE < 8192)
  2750. } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  2751. (vsi->netdev->mtu <= ETH_DATA_LEN)) {
  2752. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2753. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2754. #endif
  2755. } else {
  2756. vsi->max_frame = I40E_MAX_RXBUFFER;
  2757. vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
  2758. I40E_RXBUFFER_2048;
  2759. }
  2760. /* set up individual rings */
  2761. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2762. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2763. return err;
  2764. }
  2765. /**
  2766. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2767. * @vsi: ptr to the VSI
  2768. **/
  2769. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2770. {
  2771. struct i40e_ring *tx_ring, *rx_ring;
  2772. u16 qoffset, qcount;
  2773. int i, n;
  2774. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2775. /* Reset the TC information */
  2776. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2777. rx_ring = vsi->rx_rings[i];
  2778. tx_ring = vsi->tx_rings[i];
  2779. rx_ring->dcb_tc = 0;
  2780. tx_ring->dcb_tc = 0;
  2781. }
  2782. }
  2783. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2784. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2785. continue;
  2786. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2787. qcount = vsi->tc_config.tc_info[n].qcount;
  2788. for (i = qoffset; i < (qoffset + qcount); i++) {
  2789. rx_ring = vsi->rx_rings[i];
  2790. tx_ring = vsi->tx_rings[i];
  2791. rx_ring->dcb_tc = n;
  2792. tx_ring->dcb_tc = n;
  2793. }
  2794. }
  2795. }
  2796. /**
  2797. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2798. * @vsi: ptr to the VSI
  2799. **/
  2800. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2801. {
  2802. struct i40e_pf *pf = vsi->back;
  2803. int err;
  2804. if (vsi->netdev)
  2805. i40e_set_rx_mode(vsi->netdev);
  2806. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  2807. err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  2808. if (err) {
  2809. dev_warn(&pf->pdev->dev,
  2810. "could not set up macaddr; err %d\n", err);
  2811. }
  2812. }
  2813. }
  2814. /**
  2815. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2816. * @vsi: Pointer to the targeted VSI
  2817. *
  2818. * This function replays the hlist on the hw where all the SB Flow Director
  2819. * filters were saved.
  2820. **/
  2821. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2822. {
  2823. struct i40e_fdir_filter *filter;
  2824. struct i40e_pf *pf = vsi->back;
  2825. struct hlist_node *node;
  2826. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2827. return;
  2828. /* Reset FDir counters as we're replaying all existing filters */
  2829. pf->fd_tcp4_filter_cnt = 0;
  2830. pf->fd_udp4_filter_cnt = 0;
  2831. pf->fd_sctp4_filter_cnt = 0;
  2832. pf->fd_ip4_filter_cnt = 0;
  2833. hlist_for_each_entry_safe(filter, node,
  2834. &pf->fdir_filter_list, fdir_node) {
  2835. i40e_add_del_fdir(vsi, filter, true);
  2836. }
  2837. }
  2838. /**
  2839. * i40e_vsi_configure - Set up the VSI for action
  2840. * @vsi: the VSI being configured
  2841. **/
  2842. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2843. {
  2844. int err;
  2845. i40e_set_vsi_rx_mode(vsi);
  2846. i40e_restore_vlan(vsi);
  2847. i40e_vsi_config_dcb_rings(vsi);
  2848. err = i40e_vsi_configure_tx(vsi);
  2849. if (!err)
  2850. err = i40e_vsi_configure_rx(vsi);
  2851. return err;
  2852. }
  2853. /**
  2854. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2855. * @vsi: the VSI being configured
  2856. **/
  2857. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2858. {
  2859. struct i40e_pf *pf = vsi->back;
  2860. struct i40e_hw *hw = &pf->hw;
  2861. u16 vector;
  2862. int i, q;
  2863. u32 qp;
  2864. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2865. * and PFINT_LNKLSTn registers, e.g.:
  2866. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2867. */
  2868. qp = vsi->base_queue;
  2869. vector = vsi->base_vector;
  2870. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2871. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  2872. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2873. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
  2874. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2875. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2876. q_vector->rx.itr);
  2877. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
  2878. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2879. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2880. q_vector->tx.itr);
  2881. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  2882. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  2883. /* Linked list for the queuepairs assigned to this vector */
  2884. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2885. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2886. u32 val;
  2887. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2888. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2889. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2890. (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  2891. (I40E_QUEUE_TYPE_TX
  2892. << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2893. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2894. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2895. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2896. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2897. ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
  2898. (I40E_QUEUE_TYPE_RX
  2899. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2900. /* Terminate the linked list */
  2901. if (q == (q_vector->num_ringpairs - 1))
  2902. val |= (I40E_QUEUE_END_OF_LIST
  2903. << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2904. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2905. qp++;
  2906. }
  2907. }
  2908. i40e_flush(hw);
  2909. }
  2910. /**
  2911. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2912. * @hw: ptr to the hardware info
  2913. **/
  2914. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  2915. {
  2916. struct i40e_hw *hw = &pf->hw;
  2917. u32 val;
  2918. /* clear things first */
  2919. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  2920. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  2921. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  2922. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  2923. I40E_PFINT_ICR0_ENA_GRST_MASK |
  2924. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  2925. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  2926. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  2927. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  2928. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2929. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  2930. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  2931. if (pf->flags & I40E_FLAG_PTP)
  2932. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  2933. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  2934. /* SW_ITR_IDX = 0, but don't change INTENA */
  2935. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  2936. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  2937. /* OTHER_ITR_IDX = 0 */
  2938. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  2939. }
  2940. /**
  2941. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  2942. * @vsi: the VSI being configured
  2943. **/
  2944. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  2945. {
  2946. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  2947. struct i40e_pf *pf = vsi->back;
  2948. struct i40e_hw *hw = &pf->hw;
  2949. u32 val;
  2950. /* set the ITR configuration */
  2951. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2952. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
  2953. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2954. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  2955. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
  2956. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2957. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  2958. i40e_enable_misc_int_causes(pf);
  2959. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  2960. wr32(hw, I40E_PFINT_LNKLST0, 0);
  2961. /* Associate the queue pair to the vector and enable the queue int */
  2962. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2963. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2964. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2965. wr32(hw, I40E_QINT_RQCTL(0), val);
  2966. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2967. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2968. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2969. wr32(hw, I40E_QINT_TQCTL(0), val);
  2970. i40e_flush(hw);
  2971. }
  2972. /**
  2973. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  2974. * @pf: board private structure
  2975. **/
  2976. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  2977. {
  2978. struct i40e_hw *hw = &pf->hw;
  2979. wr32(hw, I40E_PFINT_DYN_CTL0,
  2980. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2981. i40e_flush(hw);
  2982. }
  2983. /**
  2984. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  2985. * @pf: board private structure
  2986. * @clearpba: true when all pending interrupt events should be cleared
  2987. **/
  2988. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
  2989. {
  2990. struct i40e_hw *hw = &pf->hw;
  2991. u32 val;
  2992. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  2993. (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
  2994. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  2995. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  2996. i40e_flush(hw);
  2997. }
  2998. /**
  2999. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3000. * @irq: interrupt number
  3001. * @data: pointer to a q_vector
  3002. **/
  3003. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3004. {
  3005. struct i40e_q_vector *q_vector = data;
  3006. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3007. return IRQ_HANDLED;
  3008. napi_schedule_irqoff(&q_vector->napi);
  3009. return IRQ_HANDLED;
  3010. }
  3011. /**
  3012. * i40e_irq_affinity_notify - Callback for affinity changes
  3013. * @notify: context as to what irq was changed
  3014. * @mask: the new affinity mask
  3015. *
  3016. * This is a callback function used by the irq_set_affinity_notifier function
  3017. * so that we may register to receive changes to the irq affinity masks.
  3018. **/
  3019. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3020. const cpumask_t *mask)
  3021. {
  3022. struct i40e_q_vector *q_vector =
  3023. container_of(notify, struct i40e_q_vector, affinity_notify);
  3024. q_vector->affinity_mask = *mask;
  3025. }
  3026. /**
  3027. * i40e_irq_affinity_release - Callback for affinity notifier release
  3028. * @ref: internal core kernel usage
  3029. *
  3030. * This is a callback function used by the irq_set_affinity_notifier function
  3031. * to inform the current notification subscriber that they will no longer
  3032. * receive notifications.
  3033. **/
  3034. static void i40e_irq_affinity_release(struct kref *ref) {}
  3035. /**
  3036. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3037. * @vsi: the VSI being configured
  3038. * @basename: name for the vector
  3039. *
  3040. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3041. **/
  3042. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3043. {
  3044. int q_vectors = vsi->num_q_vectors;
  3045. struct i40e_pf *pf = vsi->back;
  3046. int base = vsi->base_vector;
  3047. int rx_int_idx = 0;
  3048. int tx_int_idx = 0;
  3049. int vector, err;
  3050. int irq_num;
  3051. for (vector = 0; vector < q_vectors; vector++) {
  3052. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3053. irq_num = pf->msix_entries[base + vector].vector;
  3054. if (q_vector->tx.ring && q_vector->rx.ring) {
  3055. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3056. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3057. tx_int_idx++;
  3058. } else if (q_vector->rx.ring) {
  3059. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3060. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3061. } else if (q_vector->tx.ring) {
  3062. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3063. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3064. } else {
  3065. /* skip this unused q_vector */
  3066. continue;
  3067. }
  3068. err = request_irq(irq_num,
  3069. vsi->irq_handler,
  3070. 0,
  3071. q_vector->name,
  3072. q_vector);
  3073. if (err) {
  3074. dev_info(&pf->pdev->dev,
  3075. "MSIX request_irq failed, error: %d\n", err);
  3076. goto free_queue_irqs;
  3077. }
  3078. /* register for affinity change notifications */
  3079. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3080. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3081. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3082. /* assign the mask for this irq */
  3083. irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
  3084. }
  3085. vsi->irqs_ready = true;
  3086. return 0;
  3087. free_queue_irqs:
  3088. while (vector) {
  3089. vector--;
  3090. irq_num = pf->msix_entries[base + vector].vector;
  3091. irq_set_affinity_notifier(irq_num, NULL);
  3092. irq_set_affinity_hint(irq_num, NULL);
  3093. free_irq(irq_num, &vsi->q_vectors[vector]);
  3094. }
  3095. return err;
  3096. }
  3097. /**
  3098. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3099. * @vsi: the VSI being un-configured
  3100. **/
  3101. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3102. {
  3103. struct i40e_pf *pf = vsi->back;
  3104. struct i40e_hw *hw = &pf->hw;
  3105. int base = vsi->base_vector;
  3106. int i;
  3107. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3108. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
  3109. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
  3110. }
  3111. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3112. for (i = vsi->base_vector;
  3113. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3114. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3115. i40e_flush(hw);
  3116. for (i = 0; i < vsi->num_q_vectors; i++)
  3117. synchronize_irq(pf->msix_entries[i + base].vector);
  3118. } else {
  3119. /* Legacy and MSI mode - this stops all interrupt handling */
  3120. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3121. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3122. i40e_flush(hw);
  3123. synchronize_irq(pf->pdev->irq);
  3124. }
  3125. }
  3126. /**
  3127. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3128. * @vsi: the VSI being configured
  3129. **/
  3130. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3131. {
  3132. struct i40e_pf *pf = vsi->back;
  3133. int i;
  3134. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3135. for (i = 0; i < vsi->num_q_vectors; i++)
  3136. i40e_irq_dynamic_enable(vsi, i);
  3137. } else {
  3138. i40e_irq_dynamic_enable_icr0(pf, true);
  3139. }
  3140. i40e_flush(&pf->hw);
  3141. return 0;
  3142. }
  3143. /**
  3144. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  3145. * @pf: board private structure
  3146. **/
  3147. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  3148. {
  3149. /* Disable ICR 0 */
  3150. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3151. i40e_flush(&pf->hw);
  3152. }
  3153. /**
  3154. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3155. * @irq: interrupt number
  3156. * @data: pointer to a q_vector
  3157. *
  3158. * This is the handler used for all MSI/Legacy interrupts, and deals
  3159. * with both queue and non-queue interrupts. This is also used in
  3160. * MSIX mode to handle the non-queue interrupts.
  3161. **/
  3162. static irqreturn_t i40e_intr(int irq, void *data)
  3163. {
  3164. struct i40e_pf *pf = (struct i40e_pf *)data;
  3165. struct i40e_hw *hw = &pf->hw;
  3166. irqreturn_t ret = IRQ_NONE;
  3167. u32 icr0, icr0_remaining;
  3168. u32 val, ena_mask;
  3169. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3170. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3171. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3172. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3173. goto enable_intr;
  3174. /* if interrupt but no bits showing, must be SWINT */
  3175. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3176. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3177. pf->sw_int_count++;
  3178. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3179. (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3180. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3181. icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3182. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3183. }
  3184. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3185. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3186. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3187. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3188. /* We do not have a way to disarm Queue causes while leaving
  3189. * interrupt enabled for all other causes, ideally
  3190. * interrupt should be disabled while we are in NAPI but
  3191. * this is not a performance path and napi_schedule()
  3192. * can deal with rescheduling.
  3193. */
  3194. if (!test_bit(__I40E_DOWN, &pf->state))
  3195. napi_schedule_irqoff(&q_vector->napi);
  3196. }
  3197. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3198. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3199. set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  3200. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3201. }
  3202. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3203. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3204. set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  3205. }
  3206. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3207. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3208. set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  3209. }
  3210. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3211. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3212. set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  3213. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3214. val = rd32(hw, I40E_GLGEN_RSTAT);
  3215. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3216. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3217. if (val == I40E_RESET_CORER) {
  3218. pf->corer_count++;
  3219. } else if (val == I40E_RESET_GLOBR) {
  3220. pf->globr_count++;
  3221. } else if (val == I40E_RESET_EMPR) {
  3222. pf->empr_count++;
  3223. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
  3224. }
  3225. }
  3226. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3227. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3228. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3229. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3230. rd32(hw, I40E_PFHMC_ERRORINFO),
  3231. rd32(hw, I40E_PFHMC_ERRORDATA));
  3232. }
  3233. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3234. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3235. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3236. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3237. i40e_ptp_tx_hwtstamp(pf);
  3238. }
  3239. }
  3240. /* If a critical error is pending we have no choice but to reset the
  3241. * device.
  3242. * Report and mask out any remaining unexpected interrupts.
  3243. */
  3244. icr0_remaining = icr0 & ena_mask;
  3245. if (icr0_remaining) {
  3246. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3247. icr0_remaining);
  3248. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3249. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3250. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3251. dev_info(&pf->pdev->dev, "device will be reset\n");
  3252. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  3253. i40e_service_event_schedule(pf);
  3254. }
  3255. ena_mask &= ~icr0_remaining;
  3256. }
  3257. ret = IRQ_HANDLED;
  3258. enable_intr:
  3259. /* re-enable interrupt causes */
  3260. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3261. if (!test_bit(__I40E_DOWN, &pf->state)) {
  3262. i40e_service_event_schedule(pf);
  3263. i40e_irq_dynamic_enable_icr0(pf, false);
  3264. }
  3265. return ret;
  3266. }
  3267. /**
  3268. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3269. * @tx_ring: tx ring to clean
  3270. * @budget: how many cleans we're allowed
  3271. *
  3272. * Returns true if there's any budget left (e.g. the clean is finished)
  3273. **/
  3274. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3275. {
  3276. struct i40e_vsi *vsi = tx_ring->vsi;
  3277. u16 i = tx_ring->next_to_clean;
  3278. struct i40e_tx_buffer *tx_buf;
  3279. struct i40e_tx_desc *tx_desc;
  3280. tx_buf = &tx_ring->tx_bi[i];
  3281. tx_desc = I40E_TX_DESC(tx_ring, i);
  3282. i -= tx_ring->count;
  3283. do {
  3284. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3285. /* if next_to_watch is not set then there is no work pending */
  3286. if (!eop_desc)
  3287. break;
  3288. /* prevent any other reads prior to eop_desc */
  3289. read_barrier_depends();
  3290. /* if the descriptor isn't done, no work yet to do */
  3291. if (!(eop_desc->cmd_type_offset_bsz &
  3292. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3293. break;
  3294. /* clear next_to_watch to prevent false hangs */
  3295. tx_buf->next_to_watch = NULL;
  3296. tx_desc->buffer_addr = 0;
  3297. tx_desc->cmd_type_offset_bsz = 0;
  3298. /* move past filter desc */
  3299. tx_buf++;
  3300. tx_desc++;
  3301. i++;
  3302. if (unlikely(!i)) {
  3303. i -= tx_ring->count;
  3304. tx_buf = tx_ring->tx_bi;
  3305. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3306. }
  3307. /* unmap skb header data */
  3308. dma_unmap_single(tx_ring->dev,
  3309. dma_unmap_addr(tx_buf, dma),
  3310. dma_unmap_len(tx_buf, len),
  3311. DMA_TO_DEVICE);
  3312. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3313. kfree(tx_buf->raw_buf);
  3314. tx_buf->raw_buf = NULL;
  3315. tx_buf->tx_flags = 0;
  3316. tx_buf->next_to_watch = NULL;
  3317. dma_unmap_len_set(tx_buf, len, 0);
  3318. tx_desc->buffer_addr = 0;
  3319. tx_desc->cmd_type_offset_bsz = 0;
  3320. /* move us past the eop_desc for start of next FD desc */
  3321. tx_buf++;
  3322. tx_desc++;
  3323. i++;
  3324. if (unlikely(!i)) {
  3325. i -= tx_ring->count;
  3326. tx_buf = tx_ring->tx_bi;
  3327. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3328. }
  3329. /* update budget accounting */
  3330. budget--;
  3331. } while (likely(budget));
  3332. i += tx_ring->count;
  3333. tx_ring->next_to_clean = i;
  3334. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3335. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3336. return budget > 0;
  3337. }
  3338. /**
  3339. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3340. * @irq: interrupt number
  3341. * @data: pointer to a q_vector
  3342. **/
  3343. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3344. {
  3345. struct i40e_q_vector *q_vector = data;
  3346. struct i40e_vsi *vsi;
  3347. if (!q_vector->tx.ring)
  3348. return IRQ_HANDLED;
  3349. vsi = q_vector->tx.ring->vsi;
  3350. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3351. return IRQ_HANDLED;
  3352. }
  3353. /**
  3354. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3355. * @vsi: the VSI being configured
  3356. * @v_idx: vector index
  3357. * @qp_idx: queue pair index
  3358. **/
  3359. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3360. {
  3361. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3362. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3363. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3364. tx_ring->q_vector = q_vector;
  3365. tx_ring->next = q_vector->tx.ring;
  3366. q_vector->tx.ring = tx_ring;
  3367. q_vector->tx.count++;
  3368. rx_ring->q_vector = q_vector;
  3369. rx_ring->next = q_vector->rx.ring;
  3370. q_vector->rx.ring = rx_ring;
  3371. q_vector->rx.count++;
  3372. }
  3373. /**
  3374. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3375. * @vsi: the VSI being configured
  3376. *
  3377. * This function maps descriptor rings to the queue-specific vectors
  3378. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3379. * one vector per queue pair, but on a constrained vector budget, we
  3380. * group the queue pairs as "efficiently" as possible.
  3381. **/
  3382. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3383. {
  3384. int qp_remaining = vsi->num_queue_pairs;
  3385. int q_vectors = vsi->num_q_vectors;
  3386. int num_ringpairs;
  3387. int v_start = 0;
  3388. int qp_idx = 0;
  3389. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3390. * group them so there are multiple queues per vector.
  3391. * It is also important to go through all the vectors available to be
  3392. * sure that if we don't use all the vectors, that the remaining vectors
  3393. * are cleared. This is especially important when decreasing the
  3394. * number of queues in use.
  3395. */
  3396. for (; v_start < q_vectors; v_start++) {
  3397. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3398. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3399. q_vector->num_ringpairs = num_ringpairs;
  3400. q_vector->rx.count = 0;
  3401. q_vector->tx.count = 0;
  3402. q_vector->rx.ring = NULL;
  3403. q_vector->tx.ring = NULL;
  3404. while (num_ringpairs--) {
  3405. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3406. qp_idx++;
  3407. qp_remaining--;
  3408. }
  3409. }
  3410. }
  3411. /**
  3412. * i40e_vsi_request_irq - Request IRQ from the OS
  3413. * @vsi: the VSI being configured
  3414. * @basename: name for the vector
  3415. **/
  3416. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3417. {
  3418. struct i40e_pf *pf = vsi->back;
  3419. int err;
  3420. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3421. err = i40e_vsi_request_irq_msix(vsi, basename);
  3422. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3423. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3424. pf->int_name, pf);
  3425. else
  3426. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3427. pf->int_name, pf);
  3428. if (err)
  3429. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3430. return err;
  3431. }
  3432. #ifdef CONFIG_NET_POLL_CONTROLLER
  3433. /**
  3434. * i40e_netpoll - A Polling 'interrupt' handler
  3435. * @netdev: network interface device structure
  3436. *
  3437. * This is used by netconsole to send skbs without having to re-enable
  3438. * interrupts. It's not called while the normal interrupt routine is executing.
  3439. **/
  3440. static void i40e_netpoll(struct net_device *netdev)
  3441. {
  3442. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3443. struct i40e_vsi *vsi = np->vsi;
  3444. struct i40e_pf *pf = vsi->back;
  3445. int i;
  3446. /* if interface is down do nothing */
  3447. if (test_bit(__I40E_DOWN, &vsi->state))
  3448. return;
  3449. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3450. for (i = 0; i < vsi->num_q_vectors; i++)
  3451. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3452. } else {
  3453. i40e_intr(pf->pdev->irq, netdev);
  3454. }
  3455. }
  3456. #endif
  3457. #define I40E_QTX_ENA_WAIT_COUNT 50
  3458. /**
  3459. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3460. * @pf: the PF being configured
  3461. * @pf_q: the PF queue
  3462. * @enable: enable or disable state of the queue
  3463. *
  3464. * This routine will wait for the given Tx queue of the PF to reach the
  3465. * enabled or disabled state.
  3466. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3467. * multiple retries; else will return 0 in case of success.
  3468. **/
  3469. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3470. {
  3471. int i;
  3472. u32 tx_reg;
  3473. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3474. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3475. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3476. break;
  3477. usleep_range(10, 20);
  3478. }
  3479. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3480. return -ETIMEDOUT;
  3481. return 0;
  3482. }
  3483. /**
  3484. * i40e_control_tx_q - Start or stop a particular Tx queue
  3485. * @pf: the PF structure
  3486. * @pf_q: the PF queue to configure
  3487. * @enable: start or stop the queue
  3488. *
  3489. * This function enables or disables a single queue. Note that any delay
  3490. * required after the operation is expected to be handled by the caller of
  3491. * this function.
  3492. **/
  3493. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3494. {
  3495. struct i40e_hw *hw = &pf->hw;
  3496. u32 tx_reg;
  3497. int i;
  3498. /* warn the TX unit of coming changes */
  3499. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3500. if (!enable)
  3501. usleep_range(10, 20);
  3502. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3503. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3504. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3505. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3506. break;
  3507. usleep_range(1000, 2000);
  3508. }
  3509. /* Skip if the queue is already in the requested state */
  3510. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3511. return;
  3512. /* turn on/off the queue */
  3513. if (enable) {
  3514. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3515. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3516. } else {
  3517. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3518. }
  3519. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3520. }
  3521. /**
  3522. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3523. * @vsi: the VSI being configured
  3524. * @enable: start or stop the rings
  3525. **/
  3526. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3527. {
  3528. struct i40e_pf *pf = vsi->back;
  3529. int i, pf_q, ret = 0;
  3530. pf_q = vsi->base_queue;
  3531. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3532. i40e_control_tx_q(pf, pf_q, enable);
  3533. /* Don't wait to disable when port Tx is suspended */
  3534. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3535. continue;
  3536. /* wait for the change to finish */
  3537. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3538. if (ret) {
  3539. dev_info(&pf->pdev->dev,
  3540. "VSI seid %d Tx ring %d %sable timeout\n",
  3541. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3542. break;
  3543. }
  3544. }
  3545. return ret;
  3546. }
  3547. /**
  3548. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3549. * @pf: the PF being configured
  3550. * @pf_q: the PF queue
  3551. * @enable: enable or disable state of the queue
  3552. *
  3553. * This routine will wait for the given Rx queue of the PF to reach the
  3554. * enabled or disabled state.
  3555. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3556. * multiple retries; else will return 0 in case of success.
  3557. **/
  3558. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3559. {
  3560. int i;
  3561. u32 rx_reg;
  3562. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3563. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3564. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3565. break;
  3566. usleep_range(10, 20);
  3567. }
  3568. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3569. return -ETIMEDOUT;
  3570. return 0;
  3571. }
  3572. /**
  3573. * i40e_control_rx_q - Start or stop a particular Rx queue
  3574. * @pf: the PF structure
  3575. * @pf_q: the PF queue to configure
  3576. * @enable: start or stop the queue
  3577. *
  3578. * This function enables or disables a single queue. Note that any delay
  3579. * required after the operation is expected to be handled by the caller of
  3580. * this function.
  3581. **/
  3582. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3583. {
  3584. struct i40e_hw *hw = &pf->hw;
  3585. u32 rx_reg;
  3586. int i;
  3587. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3588. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3589. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3590. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3591. break;
  3592. usleep_range(1000, 2000);
  3593. }
  3594. /* Skip if the queue is already in the requested state */
  3595. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3596. return;
  3597. /* turn on/off the queue */
  3598. if (enable)
  3599. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3600. else
  3601. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3602. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3603. }
  3604. /**
  3605. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3606. * @vsi: the VSI being configured
  3607. * @enable: start or stop the rings
  3608. **/
  3609. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3610. {
  3611. struct i40e_pf *pf = vsi->back;
  3612. int i, pf_q, ret = 0;
  3613. pf_q = vsi->base_queue;
  3614. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3615. i40e_control_rx_q(pf, pf_q, enable);
  3616. /* Don't wait to disable when port Tx is suspended */
  3617. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3618. continue;
  3619. /* wait for the change to finish */
  3620. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3621. if (ret) {
  3622. dev_info(&pf->pdev->dev,
  3623. "VSI seid %d Rx ring %d %sable timeout\n",
  3624. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3625. break;
  3626. }
  3627. }
  3628. /* Due to HW errata, on Rx disable only, the register can indicate done
  3629. * before it really is. Needs 50ms to be sure
  3630. */
  3631. if (!enable)
  3632. mdelay(50);
  3633. return ret;
  3634. }
  3635. /**
  3636. * i40e_vsi_start_rings - Start a VSI's rings
  3637. * @vsi: the VSI being configured
  3638. **/
  3639. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3640. {
  3641. int ret = 0;
  3642. /* do rx first for enable and last for disable */
  3643. ret = i40e_vsi_control_rx(vsi, true);
  3644. if (ret)
  3645. return ret;
  3646. ret = i40e_vsi_control_tx(vsi, true);
  3647. return ret;
  3648. }
  3649. /**
  3650. * i40e_vsi_stop_rings - Stop a VSI's rings
  3651. * @vsi: the VSI being configured
  3652. **/
  3653. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3654. {
  3655. /* do rx first for enable and last for disable
  3656. * Ignore return value, we need to shutdown whatever we can
  3657. */
  3658. i40e_vsi_control_tx(vsi, false);
  3659. i40e_vsi_control_rx(vsi, false);
  3660. }
  3661. /**
  3662. * i40e_vsi_free_irq - Free the irq association with the OS
  3663. * @vsi: the VSI being configured
  3664. **/
  3665. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3666. {
  3667. struct i40e_pf *pf = vsi->back;
  3668. struct i40e_hw *hw = &pf->hw;
  3669. int base = vsi->base_vector;
  3670. u32 val, qp;
  3671. int i;
  3672. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3673. if (!vsi->q_vectors)
  3674. return;
  3675. if (!vsi->irqs_ready)
  3676. return;
  3677. vsi->irqs_ready = false;
  3678. for (i = 0; i < vsi->num_q_vectors; i++) {
  3679. int irq_num;
  3680. u16 vector;
  3681. vector = i + base;
  3682. irq_num = pf->msix_entries[vector].vector;
  3683. /* free only the irqs that were actually requested */
  3684. if (!vsi->q_vectors[i] ||
  3685. !vsi->q_vectors[i]->num_ringpairs)
  3686. continue;
  3687. /* clear the affinity notifier in the IRQ descriptor */
  3688. irq_set_affinity_notifier(irq_num, NULL);
  3689. /* clear the affinity_mask in the IRQ descriptor */
  3690. irq_set_affinity_hint(irq_num, NULL);
  3691. synchronize_irq(irq_num);
  3692. free_irq(irq_num, vsi->q_vectors[i]);
  3693. /* Tear down the interrupt queue link list
  3694. *
  3695. * We know that they come in pairs and always
  3696. * the Rx first, then the Tx. To clear the
  3697. * link list, stick the EOL value into the
  3698. * next_q field of the registers.
  3699. */
  3700. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3701. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3702. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3703. val |= I40E_QUEUE_END_OF_LIST
  3704. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3705. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3706. while (qp != I40E_QUEUE_END_OF_LIST) {
  3707. u32 next;
  3708. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3709. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3710. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3711. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3712. I40E_QINT_RQCTL_INTEVENT_MASK);
  3713. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3714. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3715. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3716. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3717. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3718. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3719. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3720. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3721. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3722. I40E_QINT_TQCTL_INTEVENT_MASK);
  3723. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3724. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3725. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3726. qp = next;
  3727. }
  3728. }
  3729. } else {
  3730. free_irq(pf->pdev->irq, pf);
  3731. val = rd32(hw, I40E_PFINT_LNKLST0);
  3732. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3733. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3734. val |= I40E_QUEUE_END_OF_LIST
  3735. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  3736. wr32(hw, I40E_PFINT_LNKLST0, val);
  3737. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3738. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3739. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3740. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3741. I40E_QINT_RQCTL_INTEVENT_MASK);
  3742. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3743. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3744. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3745. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3746. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3747. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3748. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3749. I40E_QINT_TQCTL_INTEVENT_MASK);
  3750. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3751. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3752. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3753. }
  3754. }
  3755. /**
  3756. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  3757. * @vsi: the VSI being configured
  3758. * @v_idx: Index of vector to be freed
  3759. *
  3760. * This function frees the memory allocated to the q_vector. In addition if
  3761. * NAPI is enabled it will delete any references to the NAPI struct prior
  3762. * to freeing the q_vector.
  3763. **/
  3764. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  3765. {
  3766. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3767. struct i40e_ring *ring;
  3768. if (!q_vector)
  3769. return;
  3770. /* disassociate q_vector from rings */
  3771. i40e_for_each_ring(ring, q_vector->tx)
  3772. ring->q_vector = NULL;
  3773. i40e_for_each_ring(ring, q_vector->rx)
  3774. ring->q_vector = NULL;
  3775. /* only VSI w/ an associated netdev is set up w/ NAPI */
  3776. if (vsi->netdev)
  3777. netif_napi_del(&q_vector->napi);
  3778. vsi->q_vectors[v_idx] = NULL;
  3779. kfree_rcu(q_vector, rcu);
  3780. }
  3781. /**
  3782. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  3783. * @vsi: the VSI being un-configured
  3784. *
  3785. * This frees the memory allocated to the q_vectors and
  3786. * deletes references to the NAPI struct.
  3787. **/
  3788. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  3789. {
  3790. int v_idx;
  3791. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  3792. i40e_free_q_vector(vsi, v_idx);
  3793. }
  3794. /**
  3795. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  3796. * @pf: board private structure
  3797. **/
  3798. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  3799. {
  3800. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  3801. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3802. pci_disable_msix(pf->pdev);
  3803. kfree(pf->msix_entries);
  3804. pf->msix_entries = NULL;
  3805. kfree(pf->irq_pile);
  3806. pf->irq_pile = NULL;
  3807. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  3808. pci_disable_msi(pf->pdev);
  3809. }
  3810. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  3811. }
  3812. /**
  3813. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  3814. * @pf: board private structure
  3815. *
  3816. * We go through and clear interrupt specific resources and reset the structure
  3817. * to pre-load conditions
  3818. **/
  3819. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  3820. {
  3821. int i;
  3822. i40e_stop_misc_vector(pf);
  3823. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3824. synchronize_irq(pf->msix_entries[0].vector);
  3825. free_irq(pf->msix_entries[0].vector, pf);
  3826. }
  3827. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  3828. I40E_IWARP_IRQ_PILE_ID);
  3829. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  3830. for (i = 0; i < pf->num_alloc_vsi; i++)
  3831. if (pf->vsi[i])
  3832. i40e_vsi_free_q_vectors(pf->vsi[i]);
  3833. i40e_reset_interrupt_capability(pf);
  3834. }
  3835. /**
  3836. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3837. * @vsi: the VSI being configured
  3838. **/
  3839. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  3840. {
  3841. int q_idx;
  3842. if (!vsi->netdev)
  3843. return;
  3844. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3845. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  3846. if (q_vector->rx.ring || q_vector->tx.ring)
  3847. napi_enable(&q_vector->napi);
  3848. }
  3849. }
  3850. /**
  3851. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  3852. * @vsi: the VSI being configured
  3853. **/
  3854. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  3855. {
  3856. int q_idx;
  3857. if (!vsi->netdev)
  3858. return;
  3859. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3860. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  3861. if (q_vector->rx.ring || q_vector->tx.ring)
  3862. napi_disable(&q_vector->napi);
  3863. }
  3864. }
  3865. /**
  3866. * i40e_vsi_close - Shut down a VSI
  3867. * @vsi: the vsi to be quelled
  3868. **/
  3869. static void i40e_vsi_close(struct i40e_vsi *vsi)
  3870. {
  3871. struct i40e_pf *pf = vsi->back;
  3872. if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
  3873. i40e_down(vsi);
  3874. i40e_vsi_free_irq(vsi);
  3875. i40e_vsi_free_tx_resources(vsi);
  3876. i40e_vsi_free_rx_resources(vsi);
  3877. vsi->current_netdev_flags = 0;
  3878. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  3879. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3880. pf->flags |= I40E_FLAG_CLIENT_RESET;
  3881. }
  3882. /**
  3883. * i40e_quiesce_vsi - Pause a given VSI
  3884. * @vsi: the VSI being paused
  3885. **/
  3886. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  3887. {
  3888. if (test_bit(__I40E_DOWN, &vsi->state))
  3889. return;
  3890. set_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3891. if (vsi->netdev && netif_running(vsi->netdev))
  3892. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  3893. else
  3894. i40e_vsi_close(vsi);
  3895. }
  3896. /**
  3897. * i40e_unquiesce_vsi - Resume a given VSI
  3898. * @vsi: the VSI being resumed
  3899. **/
  3900. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  3901. {
  3902. if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
  3903. return;
  3904. clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3905. if (vsi->netdev && netif_running(vsi->netdev))
  3906. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  3907. else
  3908. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  3909. }
  3910. /**
  3911. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  3912. * @pf: the PF
  3913. **/
  3914. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  3915. {
  3916. int v;
  3917. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3918. if (pf->vsi[v])
  3919. i40e_quiesce_vsi(pf->vsi[v]);
  3920. }
  3921. }
  3922. /**
  3923. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  3924. * @pf: the PF
  3925. **/
  3926. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  3927. {
  3928. int v;
  3929. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3930. if (pf->vsi[v])
  3931. i40e_unquiesce_vsi(pf->vsi[v]);
  3932. }
  3933. }
  3934. #ifdef CONFIG_I40E_DCB
  3935. /**
  3936. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  3937. * @vsi: the VSI being configured
  3938. *
  3939. * Wait until all queues on a given VSI have been disabled.
  3940. **/
  3941. static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  3942. {
  3943. struct i40e_pf *pf = vsi->back;
  3944. int i, pf_q, ret;
  3945. pf_q = vsi->base_queue;
  3946. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3947. /* Check and wait for the Tx queue */
  3948. ret = i40e_pf_txq_wait(pf, pf_q, false);
  3949. if (ret) {
  3950. dev_info(&pf->pdev->dev,
  3951. "VSI seid %d Tx ring %d disable timeout\n",
  3952. vsi->seid, pf_q);
  3953. return ret;
  3954. }
  3955. /* Check and wait for the Tx queue */
  3956. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  3957. if (ret) {
  3958. dev_info(&pf->pdev->dev,
  3959. "VSI seid %d Rx ring %d disable timeout\n",
  3960. vsi->seid, pf_q);
  3961. return ret;
  3962. }
  3963. }
  3964. return 0;
  3965. }
  3966. /**
  3967. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  3968. * @pf: the PF
  3969. *
  3970. * This function waits for the queues to be in disabled state for all the
  3971. * VSIs that are managed by this PF.
  3972. **/
  3973. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  3974. {
  3975. int v, ret = 0;
  3976. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3977. if (pf->vsi[v]) {
  3978. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  3979. if (ret)
  3980. break;
  3981. }
  3982. }
  3983. return ret;
  3984. }
  3985. #endif
  3986. /**
  3987. * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
  3988. * @q_idx: TX queue number
  3989. * @vsi: Pointer to VSI struct
  3990. *
  3991. * This function checks specified queue for given VSI. Detects hung condition.
  3992. * We proactively detect hung TX queues by checking if interrupts are disabled
  3993. * but there are pending descriptors. If it appears hung, attempt to recover
  3994. * by triggering a SW interrupt.
  3995. **/
  3996. static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
  3997. {
  3998. struct i40e_ring *tx_ring = NULL;
  3999. struct i40e_pf *pf;
  4000. u32 val, tx_pending;
  4001. int i;
  4002. pf = vsi->back;
  4003. /* now that we have an index, find the tx_ring struct */
  4004. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4005. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  4006. if (q_idx == vsi->tx_rings[i]->queue_index) {
  4007. tx_ring = vsi->tx_rings[i];
  4008. break;
  4009. }
  4010. }
  4011. }
  4012. if (!tx_ring)
  4013. return;
  4014. /* Read interrupt register */
  4015. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4016. val = rd32(&pf->hw,
  4017. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  4018. tx_ring->vsi->base_vector - 1));
  4019. else
  4020. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  4021. tx_pending = i40e_get_tx_pending(tx_ring);
  4022. /* Interrupts are disabled and TX pending is non-zero,
  4023. * trigger the SW interrupt (don't wait). Worst case
  4024. * there will be one extra interrupt which may result
  4025. * into not cleaning any queues because queues are cleaned.
  4026. */
  4027. if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
  4028. i40e_force_wb(vsi, tx_ring->q_vector);
  4029. }
  4030. /**
  4031. * i40e_detect_recover_hung - Function to detect and recover hung_queues
  4032. * @pf: pointer to PF struct
  4033. *
  4034. * LAN VSI has netdev and netdev has TX queues. This function is to check
  4035. * each of those TX queues if they are hung, trigger recovery by issuing
  4036. * SW interrupt.
  4037. **/
  4038. static void i40e_detect_recover_hung(struct i40e_pf *pf)
  4039. {
  4040. struct net_device *netdev;
  4041. struct i40e_vsi *vsi;
  4042. int i;
  4043. /* Only for LAN VSI */
  4044. vsi = pf->vsi[pf->lan_vsi];
  4045. if (!vsi)
  4046. return;
  4047. /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
  4048. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  4049. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  4050. return;
  4051. /* Make sure type is MAIN VSI */
  4052. if (vsi->type != I40E_VSI_MAIN)
  4053. return;
  4054. netdev = vsi->netdev;
  4055. if (!netdev)
  4056. return;
  4057. /* Bail out if netif_carrier is not OK */
  4058. if (!netif_carrier_ok(netdev))
  4059. return;
  4060. /* Go thru' TX queues for netdev */
  4061. for (i = 0; i < netdev->num_tx_queues; i++) {
  4062. struct netdev_queue *q;
  4063. q = netdev_get_tx_queue(netdev, i);
  4064. if (q)
  4065. i40e_detect_recover_hung_queue(i, vsi);
  4066. }
  4067. }
  4068. /**
  4069. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4070. * @pf: pointer to PF
  4071. *
  4072. * Get TC map for ISCSI PF type that will include iSCSI TC
  4073. * and LAN TC.
  4074. **/
  4075. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4076. {
  4077. struct i40e_dcb_app_priority_table app;
  4078. struct i40e_hw *hw = &pf->hw;
  4079. u8 enabled_tc = 1; /* TC0 is always enabled */
  4080. u8 tc, i;
  4081. /* Get the iSCSI APP TLV */
  4082. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4083. for (i = 0; i < dcbcfg->numapps; i++) {
  4084. app = dcbcfg->app[i];
  4085. if (app.selector == I40E_APP_SEL_TCPIP &&
  4086. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4087. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4088. enabled_tc |= BIT(tc);
  4089. break;
  4090. }
  4091. }
  4092. return enabled_tc;
  4093. }
  4094. /**
  4095. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4096. * @dcbcfg: the corresponding DCBx configuration structure
  4097. *
  4098. * Return the number of TCs from given DCBx configuration
  4099. **/
  4100. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4101. {
  4102. int i, tc_unused = 0;
  4103. u8 num_tc = 0;
  4104. u8 ret = 0;
  4105. /* Scan the ETS Config Priority Table to find
  4106. * traffic class enabled for a given priority
  4107. * and create a bitmask of enabled TCs
  4108. */
  4109. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4110. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4111. /* Now scan the bitmask to check for
  4112. * contiguous TCs starting with TC0
  4113. */
  4114. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4115. if (num_tc & BIT(i)) {
  4116. if (!tc_unused) {
  4117. ret++;
  4118. } else {
  4119. pr_err("Non-contiguous TC - Disabling DCB\n");
  4120. return 1;
  4121. }
  4122. } else {
  4123. tc_unused = 1;
  4124. }
  4125. }
  4126. /* There is always at least TC0 */
  4127. if (!ret)
  4128. ret = 1;
  4129. return ret;
  4130. }
  4131. /**
  4132. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4133. * @dcbcfg: the corresponding DCBx configuration structure
  4134. *
  4135. * Query the current DCB configuration and return the number of
  4136. * traffic classes enabled from the given DCBX config
  4137. **/
  4138. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4139. {
  4140. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4141. u8 enabled_tc = 1;
  4142. u8 i;
  4143. for (i = 0; i < num_tc; i++)
  4144. enabled_tc |= BIT(i);
  4145. return enabled_tc;
  4146. }
  4147. /**
  4148. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4149. * @pf: PF being queried
  4150. *
  4151. * Return number of traffic classes enabled for the given PF
  4152. **/
  4153. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4154. {
  4155. struct i40e_hw *hw = &pf->hw;
  4156. u8 i, enabled_tc = 1;
  4157. u8 num_tc = 0;
  4158. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4159. /* If DCB is not enabled then always in single TC */
  4160. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4161. return 1;
  4162. /* SFP mode will be enabled for all TCs on port */
  4163. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4164. return i40e_dcb_get_num_tc(dcbcfg);
  4165. /* MFP mode return count of enabled TCs for this PF */
  4166. if (pf->hw.func_caps.iscsi)
  4167. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4168. else
  4169. return 1; /* Only TC0 */
  4170. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4171. if (enabled_tc & BIT(i))
  4172. num_tc++;
  4173. }
  4174. return num_tc;
  4175. }
  4176. /**
  4177. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4178. * @pf: PF being queried
  4179. *
  4180. * Return a bitmap for enabled traffic classes for this PF.
  4181. **/
  4182. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4183. {
  4184. /* If DCB is not enabled for this PF then just return default TC */
  4185. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4186. return I40E_DEFAULT_TRAFFIC_CLASS;
  4187. /* SFP mode we want PF to be enabled for all TCs */
  4188. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4189. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4190. /* MFP enabled and iSCSI PF type */
  4191. if (pf->hw.func_caps.iscsi)
  4192. return i40e_get_iscsi_tc_map(pf);
  4193. else
  4194. return I40E_DEFAULT_TRAFFIC_CLASS;
  4195. }
  4196. /**
  4197. * i40e_vsi_get_bw_info - Query VSI BW Information
  4198. * @vsi: the VSI being queried
  4199. *
  4200. * Returns 0 on success, negative value on failure
  4201. **/
  4202. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4203. {
  4204. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4205. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4206. struct i40e_pf *pf = vsi->back;
  4207. struct i40e_hw *hw = &pf->hw;
  4208. i40e_status ret;
  4209. u32 tc_bw_max;
  4210. int i;
  4211. /* Get the VSI level BW configuration */
  4212. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4213. if (ret) {
  4214. dev_info(&pf->pdev->dev,
  4215. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4216. i40e_stat_str(&pf->hw, ret),
  4217. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4218. return -EINVAL;
  4219. }
  4220. /* Get the VSI level BW configuration per TC */
  4221. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4222. NULL);
  4223. if (ret) {
  4224. dev_info(&pf->pdev->dev,
  4225. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4226. i40e_stat_str(&pf->hw, ret),
  4227. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4228. return -EINVAL;
  4229. }
  4230. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4231. dev_info(&pf->pdev->dev,
  4232. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4233. bw_config.tc_valid_bits,
  4234. bw_ets_config.tc_valid_bits);
  4235. /* Still continuing */
  4236. }
  4237. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4238. vsi->bw_max_quanta = bw_config.max_bw;
  4239. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4240. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4241. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4242. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4243. vsi->bw_ets_limit_credits[i] =
  4244. le16_to_cpu(bw_ets_config.credits[i]);
  4245. /* 3 bits out of 4 for each TC */
  4246. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4247. }
  4248. return 0;
  4249. }
  4250. /**
  4251. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4252. * @vsi: the VSI being configured
  4253. * @enabled_tc: TC bitmap
  4254. * @bw_credits: BW shared credits per TC
  4255. *
  4256. * Returns 0 on success, negative value on failure
  4257. **/
  4258. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4259. u8 *bw_share)
  4260. {
  4261. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4262. i40e_status ret;
  4263. int i;
  4264. bw_data.tc_valid_bits = enabled_tc;
  4265. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4266. bw_data.tc_bw_credits[i] = bw_share[i];
  4267. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4268. NULL);
  4269. if (ret) {
  4270. dev_info(&vsi->back->pdev->dev,
  4271. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4272. vsi->back->hw.aq.asq_last_status);
  4273. return -EINVAL;
  4274. }
  4275. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4276. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4277. return 0;
  4278. }
  4279. /**
  4280. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4281. * @vsi: the VSI being configured
  4282. * @enabled_tc: TC map to be enabled
  4283. *
  4284. **/
  4285. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4286. {
  4287. struct net_device *netdev = vsi->netdev;
  4288. struct i40e_pf *pf = vsi->back;
  4289. struct i40e_hw *hw = &pf->hw;
  4290. u8 netdev_tc = 0;
  4291. int i;
  4292. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4293. if (!netdev)
  4294. return;
  4295. if (!enabled_tc) {
  4296. netdev_reset_tc(netdev);
  4297. return;
  4298. }
  4299. /* Set up actual enabled TCs on the VSI */
  4300. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4301. return;
  4302. /* set per TC queues for the VSI */
  4303. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4304. /* Only set TC queues for enabled tcs
  4305. *
  4306. * e.g. For a VSI that has TC0 and TC3 enabled the
  4307. * enabled_tc bitmap would be 0x00001001; the driver
  4308. * will set the numtc for netdev as 2 that will be
  4309. * referenced by the netdev layer as TC 0 and 1.
  4310. */
  4311. if (vsi->tc_config.enabled_tc & BIT(i))
  4312. netdev_set_tc_queue(netdev,
  4313. vsi->tc_config.tc_info[i].netdev_tc,
  4314. vsi->tc_config.tc_info[i].qcount,
  4315. vsi->tc_config.tc_info[i].qoffset);
  4316. }
  4317. /* Assign UP2TC map for the VSI */
  4318. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4319. /* Get the actual TC# for the UP */
  4320. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4321. /* Get the mapped netdev TC# for the UP */
  4322. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4323. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4324. }
  4325. }
  4326. /**
  4327. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4328. * @vsi: the VSI being configured
  4329. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4330. **/
  4331. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4332. struct i40e_vsi_context *ctxt)
  4333. {
  4334. /* copy just the sections touched not the entire info
  4335. * since not all sections are valid as returned by
  4336. * update vsi params
  4337. */
  4338. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4339. memcpy(&vsi->info.queue_mapping,
  4340. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4341. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4342. sizeof(vsi->info.tc_mapping));
  4343. }
  4344. /**
  4345. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4346. * @vsi: VSI to be configured
  4347. * @enabled_tc: TC bitmap
  4348. *
  4349. * This configures a particular VSI for TCs that are mapped to the
  4350. * given TC bitmap. It uses default bandwidth share for TCs across
  4351. * VSIs to configure TC for a particular VSI.
  4352. *
  4353. * NOTE:
  4354. * It is expected that the VSI queues have been quisced before calling
  4355. * this function.
  4356. **/
  4357. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4358. {
  4359. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4360. struct i40e_vsi_context ctxt;
  4361. int ret = 0;
  4362. int i;
  4363. /* Check if enabled_tc is same as existing or new TCs */
  4364. if (vsi->tc_config.enabled_tc == enabled_tc)
  4365. return ret;
  4366. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4367. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4368. if (enabled_tc & BIT(i))
  4369. bw_share[i] = 1;
  4370. }
  4371. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4372. if (ret) {
  4373. dev_info(&vsi->back->pdev->dev,
  4374. "Failed configuring TC map %d for VSI %d\n",
  4375. enabled_tc, vsi->seid);
  4376. goto out;
  4377. }
  4378. /* Update Queue Pairs Mapping for currently enabled UPs */
  4379. ctxt.seid = vsi->seid;
  4380. ctxt.pf_num = vsi->back->hw.pf_id;
  4381. ctxt.vf_num = 0;
  4382. ctxt.uplink_seid = vsi->uplink_seid;
  4383. ctxt.info = vsi->info;
  4384. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4385. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4386. ctxt.info.valid_sections |=
  4387. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4388. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4389. }
  4390. /* Update the VSI after updating the VSI queue-mapping information */
  4391. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  4392. if (ret) {
  4393. dev_info(&vsi->back->pdev->dev,
  4394. "Update vsi tc config failed, err %s aq_err %s\n",
  4395. i40e_stat_str(&vsi->back->hw, ret),
  4396. i40e_aq_str(&vsi->back->hw,
  4397. vsi->back->hw.aq.asq_last_status));
  4398. goto out;
  4399. }
  4400. /* update the local VSI info with updated queue map */
  4401. i40e_vsi_update_queue_map(vsi, &ctxt);
  4402. vsi->info.valid_sections = 0;
  4403. /* Update current VSI BW information */
  4404. ret = i40e_vsi_get_bw_info(vsi);
  4405. if (ret) {
  4406. dev_info(&vsi->back->pdev->dev,
  4407. "Failed updating vsi bw info, err %s aq_err %s\n",
  4408. i40e_stat_str(&vsi->back->hw, ret),
  4409. i40e_aq_str(&vsi->back->hw,
  4410. vsi->back->hw.aq.asq_last_status));
  4411. goto out;
  4412. }
  4413. /* Update the netdev TC setup */
  4414. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4415. out:
  4416. return ret;
  4417. }
  4418. /**
  4419. * i40e_veb_config_tc - Configure TCs for given VEB
  4420. * @veb: given VEB
  4421. * @enabled_tc: TC bitmap
  4422. *
  4423. * Configures given TC bitmap for VEB (switching) element
  4424. **/
  4425. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  4426. {
  4427. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  4428. struct i40e_pf *pf = veb->pf;
  4429. int ret = 0;
  4430. int i;
  4431. /* No TCs or already enabled TCs just return */
  4432. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  4433. return ret;
  4434. bw_data.tc_valid_bits = enabled_tc;
  4435. /* bw_data.absolute_credits is not set (relative) */
  4436. /* Enable ETS TCs with equal BW Share for now */
  4437. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4438. if (enabled_tc & BIT(i))
  4439. bw_data.tc_bw_share_credits[i] = 1;
  4440. }
  4441. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  4442. &bw_data, NULL);
  4443. if (ret) {
  4444. dev_info(&pf->pdev->dev,
  4445. "VEB bw config failed, 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. goto out;
  4449. }
  4450. /* Update the BW information */
  4451. ret = i40e_veb_get_bw_info(veb);
  4452. if (ret) {
  4453. dev_info(&pf->pdev->dev,
  4454. "Failed getting veb bw config, err %s aq_err %s\n",
  4455. i40e_stat_str(&pf->hw, ret),
  4456. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4457. }
  4458. out:
  4459. return ret;
  4460. }
  4461. #ifdef CONFIG_I40E_DCB
  4462. /**
  4463. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  4464. * @pf: PF struct
  4465. *
  4466. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  4467. * the caller would've quiesce all the VSIs before calling
  4468. * this function
  4469. **/
  4470. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  4471. {
  4472. u8 tc_map = 0;
  4473. int ret;
  4474. u8 v;
  4475. /* Enable the TCs available on PF to all VEBs */
  4476. tc_map = i40e_pf_get_tc_map(pf);
  4477. for (v = 0; v < I40E_MAX_VEB; v++) {
  4478. if (!pf->veb[v])
  4479. continue;
  4480. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  4481. if (ret) {
  4482. dev_info(&pf->pdev->dev,
  4483. "Failed configuring TC for VEB seid=%d\n",
  4484. pf->veb[v]->seid);
  4485. /* Will try to configure as many components */
  4486. }
  4487. }
  4488. /* Update each VSI */
  4489. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4490. if (!pf->vsi[v])
  4491. continue;
  4492. /* - Enable all TCs for the LAN VSI
  4493. * - For all others keep them at TC0 for now
  4494. */
  4495. if (v == pf->lan_vsi)
  4496. tc_map = i40e_pf_get_tc_map(pf);
  4497. else
  4498. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  4499. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  4500. if (ret) {
  4501. dev_info(&pf->pdev->dev,
  4502. "Failed configuring TC for VSI seid=%d\n",
  4503. pf->vsi[v]->seid);
  4504. /* Will try to configure as many components */
  4505. } else {
  4506. /* Re-configure VSI vectors based on updated TC map */
  4507. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  4508. if (pf->vsi[v]->netdev)
  4509. i40e_dcbnl_set_all(pf->vsi[v]);
  4510. }
  4511. }
  4512. }
  4513. /**
  4514. * i40e_resume_port_tx - Resume port Tx
  4515. * @pf: PF struct
  4516. *
  4517. * Resume a port's Tx and issue a PF reset in case of failure to
  4518. * resume.
  4519. **/
  4520. static int i40e_resume_port_tx(struct i40e_pf *pf)
  4521. {
  4522. struct i40e_hw *hw = &pf->hw;
  4523. int ret;
  4524. ret = i40e_aq_resume_port_tx(hw, NULL);
  4525. if (ret) {
  4526. dev_info(&pf->pdev->dev,
  4527. "Resume Port Tx failed, err %s aq_err %s\n",
  4528. i40e_stat_str(&pf->hw, ret),
  4529. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4530. /* Schedule PF reset to recover */
  4531. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  4532. i40e_service_event_schedule(pf);
  4533. }
  4534. return ret;
  4535. }
  4536. /**
  4537. * i40e_init_pf_dcb - Initialize DCB configuration
  4538. * @pf: PF being configured
  4539. *
  4540. * Query the current DCB configuration and cache it
  4541. * in the hardware structure
  4542. **/
  4543. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  4544. {
  4545. struct i40e_hw *hw = &pf->hw;
  4546. int err = 0;
  4547. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
  4548. if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
  4549. goto out;
  4550. /* Get the initial DCB configuration */
  4551. err = i40e_init_dcb(hw);
  4552. if (!err) {
  4553. /* Device/Function is not DCBX capable */
  4554. if ((!hw->func_caps.dcb) ||
  4555. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  4556. dev_info(&pf->pdev->dev,
  4557. "DCBX offload is not supported or is disabled for this PF.\n");
  4558. } else {
  4559. /* When status is not DISABLED then DCBX in FW */
  4560. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  4561. DCB_CAP_DCBX_VER_IEEE;
  4562. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  4563. /* Enable DCB tagging only when more than one TC
  4564. * or explicitly disable if only one TC
  4565. */
  4566. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  4567. pf->flags |= I40E_FLAG_DCB_ENABLED;
  4568. else
  4569. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  4570. dev_dbg(&pf->pdev->dev,
  4571. "DCBX offload is supported for this PF.\n");
  4572. }
  4573. } else {
  4574. dev_info(&pf->pdev->dev,
  4575. "Query for DCB configuration failed, err %s aq_err %s\n",
  4576. i40e_stat_str(&pf->hw, err),
  4577. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4578. }
  4579. out:
  4580. return err;
  4581. }
  4582. #endif /* CONFIG_I40E_DCB */
  4583. #define SPEED_SIZE 14
  4584. #define FC_SIZE 8
  4585. /**
  4586. * i40e_print_link_message - print link up or down
  4587. * @vsi: the VSI for which link needs a message
  4588. */
  4589. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  4590. {
  4591. enum i40e_aq_link_speed new_speed;
  4592. char *speed = "Unknown";
  4593. char *fc = "Unknown";
  4594. char *fec = "";
  4595. char *an = "";
  4596. new_speed = vsi->back->hw.phy.link_info.link_speed;
  4597. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  4598. return;
  4599. vsi->current_isup = isup;
  4600. vsi->current_speed = new_speed;
  4601. if (!isup) {
  4602. netdev_info(vsi->netdev, "NIC Link is Down\n");
  4603. return;
  4604. }
  4605. /* Warn user if link speed on NPAR enabled partition is not at
  4606. * least 10GB
  4607. */
  4608. if (vsi->back->hw.func_caps.npar_enable &&
  4609. (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  4610. vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  4611. netdev_warn(vsi->netdev,
  4612. "The partition detected link speed that is less than 10Gbps\n");
  4613. switch (vsi->back->hw.phy.link_info.link_speed) {
  4614. case I40E_LINK_SPEED_40GB:
  4615. speed = "40 G";
  4616. break;
  4617. case I40E_LINK_SPEED_20GB:
  4618. speed = "20 G";
  4619. break;
  4620. case I40E_LINK_SPEED_25GB:
  4621. speed = "25 G";
  4622. break;
  4623. case I40E_LINK_SPEED_10GB:
  4624. speed = "10 G";
  4625. break;
  4626. case I40E_LINK_SPEED_1GB:
  4627. speed = "1000 M";
  4628. break;
  4629. case I40E_LINK_SPEED_100MB:
  4630. speed = "100 M";
  4631. break;
  4632. default:
  4633. break;
  4634. }
  4635. switch (vsi->back->hw.fc.current_mode) {
  4636. case I40E_FC_FULL:
  4637. fc = "RX/TX";
  4638. break;
  4639. case I40E_FC_TX_PAUSE:
  4640. fc = "TX";
  4641. break;
  4642. case I40E_FC_RX_PAUSE:
  4643. fc = "RX";
  4644. break;
  4645. default:
  4646. fc = "None";
  4647. break;
  4648. }
  4649. if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  4650. fec = ", FEC: None";
  4651. an = ", Autoneg: False";
  4652. if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  4653. an = ", Autoneg: True";
  4654. if (vsi->back->hw.phy.link_info.fec_info &
  4655. I40E_AQ_CONFIG_FEC_KR_ENA)
  4656. fec = ", FEC: CL74 FC-FEC/BASE-R";
  4657. else if (vsi->back->hw.phy.link_info.fec_info &
  4658. I40E_AQ_CONFIG_FEC_RS_ENA)
  4659. fec = ", FEC: CL108 RS-FEC";
  4660. }
  4661. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
  4662. speed, fec, an, fc);
  4663. }
  4664. /**
  4665. * i40e_up_complete - Finish the last steps of bringing up a connection
  4666. * @vsi: the VSI being configured
  4667. **/
  4668. static int i40e_up_complete(struct i40e_vsi *vsi)
  4669. {
  4670. struct i40e_pf *pf = vsi->back;
  4671. int err;
  4672. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4673. i40e_vsi_configure_msix(vsi);
  4674. else
  4675. i40e_configure_msi_and_legacy(vsi);
  4676. /* start rings */
  4677. err = i40e_vsi_start_rings(vsi);
  4678. if (err)
  4679. return err;
  4680. clear_bit(__I40E_DOWN, &vsi->state);
  4681. i40e_napi_enable_all(vsi);
  4682. i40e_vsi_enable_irq(vsi);
  4683. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  4684. (vsi->netdev)) {
  4685. i40e_print_link_message(vsi, true);
  4686. netif_tx_start_all_queues(vsi->netdev);
  4687. netif_carrier_on(vsi->netdev);
  4688. } else if (vsi->netdev) {
  4689. i40e_print_link_message(vsi, false);
  4690. /* need to check for qualified module here*/
  4691. if ((pf->hw.phy.link_info.link_info &
  4692. I40E_AQ_MEDIA_AVAILABLE) &&
  4693. (!(pf->hw.phy.link_info.an_info &
  4694. I40E_AQ_QUALIFIED_MODULE)))
  4695. netdev_err(vsi->netdev,
  4696. "the driver failed to link because an unqualified module was detected.");
  4697. }
  4698. /* replay FDIR SB filters */
  4699. if (vsi->type == I40E_VSI_FDIR) {
  4700. /* reset fd counters */
  4701. pf->fd_add_err = 0;
  4702. pf->fd_atr_cnt = 0;
  4703. i40e_fdir_filter_restore(vsi);
  4704. }
  4705. /* On the next run of the service_task, notify any clients of the new
  4706. * opened netdev
  4707. */
  4708. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  4709. i40e_service_event_schedule(pf);
  4710. return 0;
  4711. }
  4712. /**
  4713. * i40e_vsi_reinit_locked - Reset the VSI
  4714. * @vsi: the VSI being configured
  4715. *
  4716. * Rebuild the ring structs after some configuration
  4717. * has changed, e.g. MTU size.
  4718. **/
  4719. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  4720. {
  4721. struct i40e_pf *pf = vsi->back;
  4722. WARN_ON(in_interrupt());
  4723. while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
  4724. usleep_range(1000, 2000);
  4725. i40e_down(vsi);
  4726. i40e_up(vsi);
  4727. clear_bit(__I40E_CONFIG_BUSY, &pf->state);
  4728. }
  4729. /**
  4730. * i40e_up - Bring the connection back up after being down
  4731. * @vsi: the VSI being configured
  4732. **/
  4733. int i40e_up(struct i40e_vsi *vsi)
  4734. {
  4735. int err;
  4736. err = i40e_vsi_configure(vsi);
  4737. if (!err)
  4738. err = i40e_up_complete(vsi);
  4739. return err;
  4740. }
  4741. /**
  4742. * i40e_down - Shutdown the connection processing
  4743. * @vsi: the VSI being stopped
  4744. **/
  4745. void i40e_down(struct i40e_vsi *vsi)
  4746. {
  4747. int i;
  4748. /* It is assumed that the caller of this function
  4749. * sets the vsi->state __I40E_DOWN bit.
  4750. */
  4751. if (vsi->netdev) {
  4752. netif_carrier_off(vsi->netdev);
  4753. netif_tx_disable(vsi->netdev);
  4754. }
  4755. i40e_vsi_disable_irq(vsi);
  4756. i40e_vsi_stop_rings(vsi);
  4757. i40e_napi_disable_all(vsi);
  4758. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4759. i40e_clean_tx_ring(vsi->tx_rings[i]);
  4760. i40e_clean_rx_ring(vsi->rx_rings[i]);
  4761. }
  4762. }
  4763. /**
  4764. * i40e_setup_tc - configure multiple traffic classes
  4765. * @netdev: net device to configure
  4766. * @tc: number of traffic classes to enable
  4767. **/
  4768. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  4769. {
  4770. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4771. struct i40e_vsi *vsi = np->vsi;
  4772. struct i40e_pf *pf = vsi->back;
  4773. u8 enabled_tc = 0;
  4774. int ret = -EINVAL;
  4775. int i;
  4776. /* Check if DCB enabled to continue */
  4777. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4778. netdev_info(netdev, "DCB is not enabled for adapter\n");
  4779. goto exit;
  4780. }
  4781. /* Check if MFP enabled */
  4782. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  4783. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  4784. goto exit;
  4785. }
  4786. /* Check whether tc count is within enabled limit */
  4787. if (tc > i40e_pf_get_num_tc(pf)) {
  4788. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  4789. goto exit;
  4790. }
  4791. /* Generate TC map for number of tc requested */
  4792. for (i = 0; i < tc; i++)
  4793. enabled_tc |= BIT(i);
  4794. /* Requesting same TC configuration as already enabled */
  4795. if (enabled_tc == vsi->tc_config.enabled_tc)
  4796. return 0;
  4797. /* Quiesce VSI queues */
  4798. i40e_quiesce_vsi(vsi);
  4799. /* Configure VSI for enabled TCs */
  4800. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  4801. if (ret) {
  4802. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  4803. vsi->seid);
  4804. goto exit;
  4805. }
  4806. /* Unquiesce VSI */
  4807. i40e_unquiesce_vsi(vsi);
  4808. exit:
  4809. return ret;
  4810. }
  4811. static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
  4812. struct tc_to_netdev *tc)
  4813. {
  4814. if (tc->type != TC_SETUP_MQPRIO)
  4815. return -EINVAL;
  4816. tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
  4817. return i40e_setup_tc(netdev, tc->mqprio->num_tc);
  4818. }
  4819. /**
  4820. * i40e_open - Called when a network interface is made active
  4821. * @netdev: network interface device structure
  4822. *
  4823. * The open entry point is called when a network interface is made
  4824. * active by the system (IFF_UP). At this point all resources needed
  4825. * for transmit and receive operations are allocated, the interrupt
  4826. * handler is registered with the OS, the netdev watchdog subtask is
  4827. * enabled, and the stack is notified that the interface is ready.
  4828. *
  4829. * Returns 0 on success, negative value on failure
  4830. **/
  4831. int i40e_open(struct net_device *netdev)
  4832. {
  4833. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4834. struct i40e_vsi *vsi = np->vsi;
  4835. struct i40e_pf *pf = vsi->back;
  4836. int err;
  4837. /* disallow open during test or if eeprom is broken */
  4838. if (test_bit(__I40E_TESTING, &pf->state) ||
  4839. test_bit(__I40E_BAD_EEPROM, &pf->state))
  4840. return -EBUSY;
  4841. netif_carrier_off(netdev);
  4842. err = i40e_vsi_open(vsi);
  4843. if (err)
  4844. return err;
  4845. /* configure global TSO hardware offload settings */
  4846. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  4847. TCP_FLAG_FIN) >> 16);
  4848. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  4849. TCP_FLAG_FIN |
  4850. TCP_FLAG_CWR) >> 16);
  4851. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  4852. udp_tunnel_get_rx_info(netdev);
  4853. return 0;
  4854. }
  4855. /**
  4856. * i40e_vsi_open -
  4857. * @vsi: the VSI to open
  4858. *
  4859. * Finish initialization of the VSI.
  4860. *
  4861. * Returns 0 on success, negative value on failure
  4862. *
  4863. * Note: expects to be called while under rtnl_lock()
  4864. **/
  4865. int i40e_vsi_open(struct i40e_vsi *vsi)
  4866. {
  4867. struct i40e_pf *pf = vsi->back;
  4868. char int_name[I40E_INT_NAME_STR_LEN];
  4869. int err;
  4870. /* allocate descriptors */
  4871. err = i40e_vsi_setup_tx_resources(vsi);
  4872. if (err)
  4873. goto err_setup_tx;
  4874. err = i40e_vsi_setup_rx_resources(vsi);
  4875. if (err)
  4876. goto err_setup_rx;
  4877. err = i40e_vsi_configure(vsi);
  4878. if (err)
  4879. goto err_setup_rx;
  4880. if (vsi->netdev) {
  4881. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  4882. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  4883. err = i40e_vsi_request_irq(vsi, int_name);
  4884. if (err)
  4885. goto err_setup_rx;
  4886. /* Notify the stack of the actual queue counts. */
  4887. err = netif_set_real_num_tx_queues(vsi->netdev,
  4888. vsi->num_queue_pairs);
  4889. if (err)
  4890. goto err_set_queues;
  4891. err = netif_set_real_num_rx_queues(vsi->netdev,
  4892. vsi->num_queue_pairs);
  4893. if (err)
  4894. goto err_set_queues;
  4895. } else if (vsi->type == I40E_VSI_FDIR) {
  4896. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  4897. dev_driver_string(&pf->pdev->dev),
  4898. dev_name(&pf->pdev->dev));
  4899. err = i40e_vsi_request_irq(vsi, int_name);
  4900. } else {
  4901. err = -EINVAL;
  4902. goto err_setup_rx;
  4903. }
  4904. err = i40e_up_complete(vsi);
  4905. if (err)
  4906. goto err_up_complete;
  4907. return 0;
  4908. err_up_complete:
  4909. i40e_down(vsi);
  4910. err_set_queues:
  4911. i40e_vsi_free_irq(vsi);
  4912. err_setup_rx:
  4913. i40e_vsi_free_rx_resources(vsi);
  4914. err_setup_tx:
  4915. i40e_vsi_free_tx_resources(vsi);
  4916. if (vsi == pf->vsi[pf->lan_vsi])
  4917. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
  4918. return err;
  4919. }
  4920. /**
  4921. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  4922. * @pf: Pointer to PF
  4923. *
  4924. * This function destroys the hlist where all the Flow Director
  4925. * filters were saved.
  4926. **/
  4927. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  4928. {
  4929. struct i40e_fdir_filter *filter;
  4930. struct i40e_flex_pit *pit_entry, *tmp;
  4931. struct hlist_node *node2;
  4932. hlist_for_each_entry_safe(filter, node2,
  4933. &pf->fdir_filter_list, fdir_node) {
  4934. hlist_del(&filter->fdir_node);
  4935. kfree(filter);
  4936. }
  4937. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  4938. list_del(&pit_entry->list);
  4939. kfree(pit_entry);
  4940. }
  4941. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  4942. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  4943. list_del(&pit_entry->list);
  4944. kfree(pit_entry);
  4945. }
  4946. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  4947. pf->fdir_pf_active_filters = 0;
  4948. pf->fd_tcp4_filter_cnt = 0;
  4949. pf->fd_udp4_filter_cnt = 0;
  4950. pf->fd_sctp4_filter_cnt = 0;
  4951. pf->fd_ip4_filter_cnt = 0;
  4952. /* Reprogram the default input set for TCP/IPv4 */
  4953. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  4954. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4955. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4956. /* Reprogram the default input set for UDP/IPv4 */
  4957. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  4958. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4959. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4960. /* Reprogram the default input set for SCTP/IPv4 */
  4961. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  4962. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4963. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4964. /* Reprogram the default input set for Other/IPv4 */
  4965. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  4966. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  4967. }
  4968. /**
  4969. * i40e_close - Disables a network interface
  4970. * @netdev: network interface device structure
  4971. *
  4972. * The close entry point is called when an interface is de-activated
  4973. * by the OS. The hardware is still under the driver's control, but
  4974. * this netdev interface is disabled.
  4975. *
  4976. * Returns 0, this is not allowed to fail
  4977. **/
  4978. int i40e_close(struct net_device *netdev)
  4979. {
  4980. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4981. struct i40e_vsi *vsi = np->vsi;
  4982. i40e_vsi_close(vsi);
  4983. return 0;
  4984. }
  4985. /**
  4986. * i40e_do_reset - Start a PF or Core Reset sequence
  4987. * @pf: board private structure
  4988. * @reset_flags: which reset is requested
  4989. * @lock_acquired: indicates whether or not the lock has been acquired
  4990. * before this function was called.
  4991. *
  4992. * The essential difference in resets is that the PF Reset
  4993. * doesn't clear the packet buffers, doesn't reset the PE
  4994. * firmware, and doesn't bother the other PFs on the chip.
  4995. **/
  4996. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  4997. {
  4998. u32 val;
  4999. WARN_ON(in_interrupt());
  5000. /* do the biggest reset indicated */
  5001. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  5002. /* Request a Global Reset
  5003. *
  5004. * This will start the chip's countdown to the actual full
  5005. * chip reset event, and a warning interrupt to be sent
  5006. * to all PFs, including the requestor. Our handler
  5007. * for the warning interrupt will deal with the shutdown
  5008. * and recovery of the switch setup.
  5009. */
  5010. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  5011. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  5012. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  5013. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5014. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  5015. /* Request a Core Reset
  5016. *
  5017. * Same as Global Reset, except does *not* include the MAC/PHY
  5018. */
  5019. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  5020. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  5021. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  5022. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5023. i40e_flush(&pf->hw);
  5024. } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
  5025. /* Request a PF Reset
  5026. *
  5027. * Resets only the PF-specific registers
  5028. *
  5029. * This goes directly to the tear-down and rebuild of
  5030. * the switch, since we need to do all the recovery as
  5031. * for the Core Reset.
  5032. */
  5033. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  5034. i40e_handle_reset_warning(pf, lock_acquired);
  5035. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  5036. int v;
  5037. /* Find the VSI(s) that requested a re-init */
  5038. dev_info(&pf->pdev->dev,
  5039. "VSI reinit requested\n");
  5040. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5041. struct i40e_vsi *vsi = pf->vsi[v];
  5042. if (vsi != NULL &&
  5043. test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
  5044. i40e_vsi_reinit_locked(pf->vsi[v]);
  5045. clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  5046. }
  5047. }
  5048. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  5049. int v;
  5050. /* Find the VSI(s) that needs to be brought down */
  5051. dev_info(&pf->pdev->dev, "VSI down requested\n");
  5052. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5053. struct i40e_vsi *vsi = pf->vsi[v];
  5054. if (vsi != NULL &&
  5055. test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
  5056. set_bit(__I40E_DOWN, &vsi->state);
  5057. i40e_down(vsi);
  5058. clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
  5059. }
  5060. }
  5061. } else {
  5062. dev_info(&pf->pdev->dev,
  5063. "bad reset request 0x%08x\n", reset_flags);
  5064. }
  5065. }
  5066. #ifdef CONFIG_I40E_DCB
  5067. /**
  5068. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  5069. * @pf: board private structure
  5070. * @old_cfg: current DCB config
  5071. * @new_cfg: new DCB config
  5072. **/
  5073. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  5074. struct i40e_dcbx_config *old_cfg,
  5075. struct i40e_dcbx_config *new_cfg)
  5076. {
  5077. bool need_reconfig = false;
  5078. /* Check if ETS configuration has changed */
  5079. if (memcmp(&new_cfg->etscfg,
  5080. &old_cfg->etscfg,
  5081. sizeof(new_cfg->etscfg))) {
  5082. /* If Priority Table has changed reconfig is needed */
  5083. if (memcmp(&new_cfg->etscfg.prioritytable,
  5084. &old_cfg->etscfg.prioritytable,
  5085. sizeof(new_cfg->etscfg.prioritytable))) {
  5086. need_reconfig = true;
  5087. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  5088. }
  5089. if (memcmp(&new_cfg->etscfg.tcbwtable,
  5090. &old_cfg->etscfg.tcbwtable,
  5091. sizeof(new_cfg->etscfg.tcbwtable)))
  5092. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  5093. if (memcmp(&new_cfg->etscfg.tsatable,
  5094. &old_cfg->etscfg.tsatable,
  5095. sizeof(new_cfg->etscfg.tsatable)))
  5096. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  5097. }
  5098. /* Check if PFC configuration has changed */
  5099. if (memcmp(&new_cfg->pfc,
  5100. &old_cfg->pfc,
  5101. sizeof(new_cfg->pfc))) {
  5102. need_reconfig = true;
  5103. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  5104. }
  5105. /* Check if APP Table has changed */
  5106. if (memcmp(&new_cfg->app,
  5107. &old_cfg->app,
  5108. sizeof(new_cfg->app))) {
  5109. need_reconfig = true;
  5110. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  5111. }
  5112. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  5113. return need_reconfig;
  5114. }
  5115. /**
  5116. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  5117. * @pf: board private structure
  5118. * @e: event info posted on ARQ
  5119. **/
  5120. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  5121. struct i40e_arq_event_info *e)
  5122. {
  5123. struct i40e_aqc_lldp_get_mib *mib =
  5124. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  5125. struct i40e_hw *hw = &pf->hw;
  5126. struct i40e_dcbx_config tmp_dcbx_cfg;
  5127. bool need_reconfig = false;
  5128. int ret = 0;
  5129. u8 type;
  5130. /* Not DCB capable or capability disabled */
  5131. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  5132. return ret;
  5133. /* Ignore if event is not for Nearest Bridge */
  5134. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  5135. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  5136. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  5137. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  5138. return ret;
  5139. /* Check MIB Type and return if event for Remote MIB update */
  5140. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  5141. dev_dbg(&pf->pdev->dev,
  5142. "LLDP event mib type %s\n", type ? "remote" : "local");
  5143. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  5144. /* Update the remote cached instance and return */
  5145. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  5146. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  5147. &hw->remote_dcbx_config);
  5148. goto exit;
  5149. }
  5150. /* Store the old configuration */
  5151. tmp_dcbx_cfg = hw->local_dcbx_config;
  5152. /* Reset the old DCBx configuration data */
  5153. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  5154. /* Get updated DCBX data from firmware */
  5155. ret = i40e_get_dcb_config(&pf->hw);
  5156. if (ret) {
  5157. dev_info(&pf->pdev->dev,
  5158. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  5159. i40e_stat_str(&pf->hw, ret),
  5160. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5161. goto exit;
  5162. }
  5163. /* No change detected in DCBX configs */
  5164. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  5165. sizeof(tmp_dcbx_cfg))) {
  5166. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  5167. goto exit;
  5168. }
  5169. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  5170. &hw->local_dcbx_config);
  5171. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  5172. if (!need_reconfig)
  5173. goto exit;
  5174. /* Enable DCB tagging only when more than one TC */
  5175. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5176. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5177. else
  5178. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5179. set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  5180. /* Reconfiguration needed quiesce all VSIs */
  5181. i40e_pf_quiesce_all_vsi(pf);
  5182. /* Changes in configuration update VEB/VSI */
  5183. i40e_dcb_reconfigure(pf);
  5184. ret = i40e_resume_port_tx(pf);
  5185. clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  5186. /* In case of error no point in resuming VSIs */
  5187. if (ret)
  5188. goto exit;
  5189. /* Wait for the PF's queues to be disabled */
  5190. ret = i40e_pf_wait_queues_disabled(pf);
  5191. if (ret) {
  5192. /* Schedule PF reset to recover */
  5193. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5194. i40e_service_event_schedule(pf);
  5195. } else {
  5196. i40e_pf_unquiesce_all_vsi(pf);
  5197. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  5198. I40E_FLAG_CLIENT_L2_CHANGE);
  5199. }
  5200. exit:
  5201. return ret;
  5202. }
  5203. #endif /* CONFIG_I40E_DCB */
  5204. /**
  5205. * i40e_do_reset_safe - Protected reset path for userland calls.
  5206. * @pf: board private structure
  5207. * @reset_flags: which reset is requested
  5208. *
  5209. **/
  5210. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  5211. {
  5212. rtnl_lock();
  5213. i40e_do_reset(pf, reset_flags, true);
  5214. rtnl_unlock();
  5215. }
  5216. /**
  5217. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  5218. * @pf: board private structure
  5219. * @e: event info posted on ARQ
  5220. *
  5221. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  5222. * and VF queues
  5223. **/
  5224. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  5225. struct i40e_arq_event_info *e)
  5226. {
  5227. struct i40e_aqc_lan_overflow *data =
  5228. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  5229. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  5230. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  5231. struct i40e_hw *hw = &pf->hw;
  5232. struct i40e_vf *vf;
  5233. u16 vf_id;
  5234. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  5235. queue, qtx_ctl);
  5236. /* Queue belongs to VF, find the VF and issue VF reset */
  5237. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  5238. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  5239. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  5240. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  5241. vf_id -= hw->func_caps.vf_base_id;
  5242. vf = &pf->vf[vf_id];
  5243. i40e_vc_notify_vf_reset(vf);
  5244. /* Allow VF to process pending reset notification */
  5245. msleep(20);
  5246. i40e_reset_vf(vf, false);
  5247. }
  5248. }
  5249. /**
  5250. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  5251. * @pf: board private structure
  5252. **/
  5253. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  5254. {
  5255. u32 val, fcnt_prog;
  5256. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5257. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  5258. return fcnt_prog;
  5259. }
  5260. /**
  5261. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  5262. * @pf: board private structure
  5263. **/
  5264. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  5265. {
  5266. u32 val, fcnt_prog;
  5267. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5268. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  5269. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  5270. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  5271. return fcnt_prog;
  5272. }
  5273. /**
  5274. * i40e_get_global_fd_count - Get total FD filters programmed on device
  5275. * @pf: board private structure
  5276. **/
  5277. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  5278. {
  5279. u32 val, fcnt_prog;
  5280. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  5281. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  5282. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  5283. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  5284. return fcnt_prog;
  5285. }
  5286. /**
  5287. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  5288. * @pf: board private structure
  5289. **/
  5290. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  5291. {
  5292. struct i40e_fdir_filter *filter;
  5293. u32 fcnt_prog, fcnt_avail;
  5294. struct hlist_node *node;
  5295. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5296. return;
  5297. /* Check if, FD SB or ATR was auto disabled and if there is enough room
  5298. * to re-enable
  5299. */
  5300. fcnt_prog = i40e_get_global_fd_count(pf);
  5301. fcnt_avail = pf->fdir_pf_filter_count;
  5302. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  5303. (pf->fd_add_err == 0) ||
  5304. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
  5305. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  5306. (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
  5307. pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5308. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5309. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  5310. }
  5311. }
  5312. /* Wait for some more space to be available to turn on ATR. We also
  5313. * must check that no existing ntuple rules for TCP are in effect
  5314. */
  5315. if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
  5316. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5317. (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5318. (pf->fd_tcp4_filter_cnt == 0)) {
  5319. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5320. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5321. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  5322. }
  5323. }
  5324. /* if hw had a problem adding a filter, delete it */
  5325. if (pf->fd_inv > 0) {
  5326. hlist_for_each_entry_safe(filter, node,
  5327. &pf->fdir_filter_list, fdir_node) {
  5328. if (filter->fd_id == pf->fd_inv) {
  5329. hlist_del(&filter->fdir_node);
  5330. kfree(filter);
  5331. pf->fdir_pf_active_filters--;
  5332. }
  5333. }
  5334. }
  5335. }
  5336. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  5337. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  5338. /**
  5339. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  5340. * @pf: board private structure
  5341. **/
  5342. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  5343. {
  5344. unsigned long min_flush_time;
  5345. int flush_wait_retry = 50;
  5346. bool disable_atr = false;
  5347. int fd_room;
  5348. int reg;
  5349. if (!time_after(jiffies, pf->fd_flush_timestamp +
  5350. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  5351. return;
  5352. /* If the flush is happening too quick and we have mostly SB rules we
  5353. * should not re-enable ATR for some time.
  5354. */
  5355. min_flush_time = pf->fd_flush_timestamp +
  5356. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  5357. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  5358. if (!(time_after(jiffies, min_flush_time)) &&
  5359. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  5360. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5361. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  5362. disable_atr = true;
  5363. }
  5364. pf->fd_flush_timestamp = jiffies;
  5365. pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
  5366. /* flush all filters */
  5367. wr32(&pf->hw, I40E_PFQF_CTL_1,
  5368. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  5369. i40e_flush(&pf->hw);
  5370. pf->fd_flush_cnt++;
  5371. pf->fd_add_err = 0;
  5372. do {
  5373. /* Check FD flush status every 5-6msec */
  5374. usleep_range(5000, 6000);
  5375. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  5376. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  5377. break;
  5378. } while (flush_wait_retry--);
  5379. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  5380. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  5381. } else {
  5382. /* replay sideband filters */
  5383. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  5384. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  5385. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5386. clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
  5387. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5388. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  5389. }
  5390. }
  5391. /**
  5392. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  5393. * @pf: board private structure
  5394. **/
  5395. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  5396. {
  5397. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  5398. }
  5399. /* We can see up to 256 filter programming desc in transit if the filters are
  5400. * being applied really fast; before we see the first
  5401. * filter miss error on Rx queue 0. Accumulating enough error messages before
  5402. * reacting will make sure we don't cause flush too often.
  5403. */
  5404. #define I40E_MAX_FD_PROGRAM_ERROR 256
  5405. /**
  5406. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  5407. * @pf: board private structure
  5408. **/
  5409. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  5410. {
  5411. /* if interface is down do nothing */
  5412. if (test_bit(__I40E_DOWN, &pf->state))
  5413. return;
  5414. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5415. i40e_fdir_flush_and_replay(pf);
  5416. i40e_fdir_check_and_reenable(pf);
  5417. }
  5418. /**
  5419. * i40e_vsi_link_event - notify VSI of a link event
  5420. * @vsi: vsi to be notified
  5421. * @link_up: link up or down
  5422. **/
  5423. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  5424. {
  5425. if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
  5426. return;
  5427. switch (vsi->type) {
  5428. case I40E_VSI_MAIN:
  5429. if (!vsi->netdev || !vsi->netdev_registered)
  5430. break;
  5431. if (link_up) {
  5432. netif_carrier_on(vsi->netdev);
  5433. netif_tx_wake_all_queues(vsi->netdev);
  5434. } else {
  5435. netif_carrier_off(vsi->netdev);
  5436. netif_tx_stop_all_queues(vsi->netdev);
  5437. }
  5438. break;
  5439. case I40E_VSI_SRIOV:
  5440. case I40E_VSI_VMDQ2:
  5441. case I40E_VSI_CTRL:
  5442. case I40E_VSI_IWARP:
  5443. case I40E_VSI_MIRROR:
  5444. default:
  5445. /* there is no notification for other VSIs */
  5446. break;
  5447. }
  5448. }
  5449. /**
  5450. * i40e_veb_link_event - notify elements on the veb of a link event
  5451. * @veb: veb to be notified
  5452. * @link_up: link up or down
  5453. **/
  5454. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  5455. {
  5456. struct i40e_pf *pf;
  5457. int i;
  5458. if (!veb || !veb->pf)
  5459. return;
  5460. pf = veb->pf;
  5461. /* depth first... */
  5462. for (i = 0; i < I40E_MAX_VEB; i++)
  5463. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  5464. i40e_veb_link_event(pf->veb[i], link_up);
  5465. /* ... now the local VSIs */
  5466. for (i = 0; i < pf->num_alloc_vsi; i++)
  5467. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  5468. i40e_vsi_link_event(pf->vsi[i], link_up);
  5469. }
  5470. /**
  5471. * i40e_link_event - Update netif_carrier status
  5472. * @pf: board private structure
  5473. **/
  5474. static void i40e_link_event(struct i40e_pf *pf)
  5475. {
  5476. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5477. u8 new_link_speed, old_link_speed;
  5478. i40e_status status;
  5479. bool new_link, old_link;
  5480. /* save off old link status information */
  5481. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  5482. /* set this to force the get_link_status call to refresh state */
  5483. pf->hw.phy.get_link_info = true;
  5484. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  5485. status = i40e_get_link_status(&pf->hw, &new_link);
  5486. /* On success, disable temp link polling */
  5487. if (status == I40E_SUCCESS) {
  5488. if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
  5489. pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
  5490. } else {
  5491. /* Enable link polling temporarily until i40e_get_link_status
  5492. * returns I40E_SUCCESS
  5493. */
  5494. pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
  5495. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  5496. status);
  5497. return;
  5498. }
  5499. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  5500. new_link_speed = pf->hw.phy.link_info.link_speed;
  5501. if (new_link == old_link &&
  5502. new_link_speed == old_link_speed &&
  5503. (test_bit(__I40E_DOWN, &vsi->state) ||
  5504. new_link == netif_carrier_ok(vsi->netdev)))
  5505. return;
  5506. if (!test_bit(__I40E_DOWN, &vsi->state))
  5507. i40e_print_link_message(vsi, new_link);
  5508. /* Notify the base of the switch tree connected to
  5509. * the link. Floating VEBs are not notified.
  5510. */
  5511. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  5512. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  5513. else
  5514. i40e_vsi_link_event(vsi, new_link);
  5515. if (pf->vf)
  5516. i40e_vc_notify_link_state(pf);
  5517. if (pf->flags & I40E_FLAG_PTP)
  5518. i40e_ptp_set_increment(pf);
  5519. }
  5520. /**
  5521. * i40e_watchdog_subtask - periodic checks not using event driven response
  5522. * @pf: board private structure
  5523. **/
  5524. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  5525. {
  5526. int i;
  5527. /* if interface is down do nothing */
  5528. if (test_bit(__I40E_DOWN, &pf->state) ||
  5529. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5530. return;
  5531. /* make sure we don't do these things too often */
  5532. if (time_before(jiffies, (pf->service_timer_previous +
  5533. pf->service_timer_period)))
  5534. return;
  5535. pf->service_timer_previous = jiffies;
  5536. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  5537. (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
  5538. i40e_link_event(pf);
  5539. /* Update the stats for active netdevs so the network stack
  5540. * can look at updated numbers whenever it cares to
  5541. */
  5542. for (i = 0; i < pf->num_alloc_vsi; i++)
  5543. if (pf->vsi[i] && pf->vsi[i]->netdev)
  5544. i40e_update_stats(pf->vsi[i]);
  5545. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  5546. /* Update the stats for the active switching components */
  5547. for (i = 0; i < I40E_MAX_VEB; i++)
  5548. if (pf->veb[i])
  5549. i40e_update_veb_stats(pf->veb[i]);
  5550. }
  5551. i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
  5552. }
  5553. /**
  5554. * i40e_reset_subtask - Set up for resetting the device and driver
  5555. * @pf: board private structure
  5556. **/
  5557. static void i40e_reset_subtask(struct i40e_pf *pf)
  5558. {
  5559. u32 reset_flags = 0;
  5560. if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
  5561. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  5562. clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
  5563. }
  5564. if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
  5565. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  5566. clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5567. }
  5568. if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
  5569. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  5570. clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  5571. }
  5572. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
  5573. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  5574. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  5575. }
  5576. if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
  5577. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  5578. clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
  5579. }
  5580. /* If there's a recovery already waiting, it takes
  5581. * precedence before starting a new reset sequence.
  5582. */
  5583. if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
  5584. i40e_prep_for_reset(pf, false);
  5585. i40e_reset(pf);
  5586. i40e_rebuild(pf, false, false);
  5587. }
  5588. /* If we're already down or resetting, just bail */
  5589. if (reset_flags &&
  5590. !test_bit(__I40E_DOWN, &pf->state) &&
  5591. !test_bit(__I40E_CONFIG_BUSY, &pf->state)) {
  5592. rtnl_lock();
  5593. i40e_do_reset(pf, reset_flags, true);
  5594. rtnl_unlock();
  5595. }
  5596. }
  5597. /**
  5598. * i40e_handle_link_event - Handle link event
  5599. * @pf: board private structure
  5600. * @e: event info posted on ARQ
  5601. **/
  5602. static void i40e_handle_link_event(struct i40e_pf *pf,
  5603. struct i40e_arq_event_info *e)
  5604. {
  5605. struct i40e_aqc_get_link_status *status =
  5606. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  5607. /* Do a new status request to re-enable LSE reporting
  5608. * and load new status information into the hw struct
  5609. * This completely ignores any state information
  5610. * in the ARQ event info, instead choosing to always
  5611. * issue the AQ update link status command.
  5612. */
  5613. i40e_link_event(pf);
  5614. /* check for unqualified module, if link is down */
  5615. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  5616. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  5617. (!(status->link_info & I40E_AQ_LINK_UP)))
  5618. dev_err(&pf->pdev->dev,
  5619. "The driver failed to link because an unqualified module was detected.\n");
  5620. }
  5621. /**
  5622. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  5623. * @pf: board private structure
  5624. **/
  5625. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  5626. {
  5627. struct i40e_arq_event_info event;
  5628. struct i40e_hw *hw = &pf->hw;
  5629. u16 pending, i = 0;
  5630. i40e_status ret;
  5631. u16 opcode;
  5632. u32 oldval;
  5633. u32 val;
  5634. /* Do not run clean AQ when PF reset fails */
  5635. if (test_bit(__I40E_RESET_FAILED, &pf->state))
  5636. return;
  5637. /* check for error indications */
  5638. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  5639. oldval = val;
  5640. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  5641. if (hw->debug_mask & I40E_DEBUG_AQ)
  5642. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  5643. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  5644. }
  5645. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  5646. if (hw->debug_mask & I40E_DEBUG_AQ)
  5647. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  5648. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  5649. pf->arq_overflows++;
  5650. }
  5651. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  5652. if (hw->debug_mask & I40E_DEBUG_AQ)
  5653. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  5654. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  5655. }
  5656. if (oldval != val)
  5657. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  5658. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  5659. oldval = val;
  5660. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  5661. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5662. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  5663. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  5664. }
  5665. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  5666. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5667. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  5668. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  5669. }
  5670. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  5671. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5672. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  5673. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  5674. }
  5675. if (oldval != val)
  5676. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  5677. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  5678. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  5679. if (!event.msg_buf)
  5680. return;
  5681. do {
  5682. ret = i40e_clean_arq_element(hw, &event, &pending);
  5683. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  5684. break;
  5685. else if (ret) {
  5686. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  5687. break;
  5688. }
  5689. opcode = le16_to_cpu(event.desc.opcode);
  5690. switch (opcode) {
  5691. case i40e_aqc_opc_get_link_status:
  5692. i40e_handle_link_event(pf, &event);
  5693. break;
  5694. case i40e_aqc_opc_send_msg_to_pf:
  5695. ret = i40e_vc_process_vf_msg(pf,
  5696. le16_to_cpu(event.desc.retval),
  5697. le32_to_cpu(event.desc.cookie_high),
  5698. le32_to_cpu(event.desc.cookie_low),
  5699. event.msg_buf,
  5700. event.msg_len);
  5701. break;
  5702. case i40e_aqc_opc_lldp_update_mib:
  5703. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  5704. #ifdef CONFIG_I40E_DCB
  5705. rtnl_lock();
  5706. ret = i40e_handle_lldp_event(pf, &event);
  5707. rtnl_unlock();
  5708. #endif /* CONFIG_I40E_DCB */
  5709. break;
  5710. case i40e_aqc_opc_event_lan_overflow:
  5711. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  5712. i40e_handle_lan_overflow_event(pf, &event);
  5713. break;
  5714. case i40e_aqc_opc_send_msg_to_peer:
  5715. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  5716. break;
  5717. case i40e_aqc_opc_nvm_erase:
  5718. case i40e_aqc_opc_nvm_update:
  5719. case i40e_aqc_opc_oem_post_update:
  5720. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  5721. "ARQ NVM operation 0x%04x completed\n",
  5722. opcode);
  5723. break;
  5724. default:
  5725. dev_info(&pf->pdev->dev,
  5726. "ARQ: Unknown event 0x%04x ignored\n",
  5727. opcode);
  5728. break;
  5729. }
  5730. } while (i++ < pf->adminq_work_limit);
  5731. if (i < pf->adminq_work_limit)
  5732. clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  5733. /* re-enable Admin queue interrupt cause */
  5734. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  5735. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  5736. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  5737. i40e_flush(hw);
  5738. kfree(event.msg_buf);
  5739. }
  5740. /**
  5741. * i40e_verify_eeprom - make sure eeprom is good to use
  5742. * @pf: board private structure
  5743. **/
  5744. static void i40e_verify_eeprom(struct i40e_pf *pf)
  5745. {
  5746. int err;
  5747. err = i40e_diag_eeprom_test(&pf->hw);
  5748. if (err) {
  5749. /* retry in case of garbage read */
  5750. err = i40e_diag_eeprom_test(&pf->hw);
  5751. if (err) {
  5752. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  5753. err);
  5754. set_bit(__I40E_BAD_EEPROM, &pf->state);
  5755. }
  5756. }
  5757. if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  5758. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  5759. clear_bit(__I40E_BAD_EEPROM, &pf->state);
  5760. }
  5761. }
  5762. /**
  5763. * i40e_enable_pf_switch_lb
  5764. * @pf: pointer to the PF structure
  5765. *
  5766. * enable switch loop back or die - no point in a return value
  5767. **/
  5768. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  5769. {
  5770. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5771. struct i40e_vsi_context ctxt;
  5772. int ret;
  5773. ctxt.seid = pf->main_vsi_seid;
  5774. ctxt.pf_num = pf->hw.pf_id;
  5775. ctxt.vf_num = 0;
  5776. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5777. if (ret) {
  5778. dev_info(&pf->pdev->dev,
  5779. "couldn't get PF vsi config, err %s aq_err %s\n",
  5780. i40e_stat_str(&pf->hw, ret),
  5781. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5782. return;
  5783. }
  5784. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5785. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5786. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5787. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5788. if (ret) {
  5789. dev_info(&pf->pdev->dev,
  5790. "update vsi switch failed, err %s aq_err %s\n",
  5791. i40e_stat_str(&pf->hw, ret),
  5792. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5793. }
  5794. }
  5795. /**
  5796. * i40e_disable_pf_switch_lb
  5797. * @pf: pointer to the PF structure
  5798. *
  5799. * disable switch loop back or die - no point in a return value
  5800. **/
  5801. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  5802. {
  5803. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5804. struct i40e_vsi_context ctxt;
  5805. int ret;
  5806. ctxt.seid = pf->main_vsi_seid;
  5807. ctxt.pf_num = pf->hw.pf_id;
  5808. ctxt.vf_num = 0;
  5809. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5810. if (ret) {
  5811. dev_info(&pf->pdev->dev,
  5812. "couldn't get PF vsi config, err %s aq_err %s\n",
  5813. i40e_stat_str(&pf->hw, ret),
  5814. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5815. return;
  5816. }
  5817. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5818. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5819. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5820. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5821. if (ret) {
  5822. dev_info(&pf->pdev->dev,
  5823. "update vsi switch failed, err %s aq_err %s\n",
  5824. i40e_stat_str(&pf->hw, ret),
  5825. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5826. }
  5827. }
  5828. /**
  5829. * i40e_config_bridge_mode - Configure the HW bridge mode
  5830. * @veb: pointer to the bridge instance
  5831. *
  5832. * Configure the loop back mode for the LAN VSI that is downlink to the
  5833. * specified HW bridge instance. It is expected this function is called
  5834. * when a new HW bridge is instantiated.
  5835. **/
  5836. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  5837. {
  5838. struct i40e_pf *pf = veb->pf;
  5839. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  5840. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  5841. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  5842. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  5843. i40e_disable_pf_switch_lb(pf);
  5844. else
  5845. i40e_enable_pf_switch_lb(pf);
  5846. }
  5847. /**
  5848. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  5849. * @veb: pointer to the VEB instance
  5850. *
  5851. * This is a recursive function that first builds the attached VSIs then
  5852. * recurses in to build the next layer of VEB. We track the connections
  5853. * through our own index numbers because the seid's from the HW could
  5854. * change across the reset.
  5855. **/
  5856. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  5857. {
  5858. struct i40e_vsi *ctl_vsi = NULL;
  5859. struct i40e_pf *pf = veb->pf;
  5860. int v, veb_idx;
  5861. int ret;
  5862. /* build VSI that owns this VEB, temporarily attached to base VEB */
  5863. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  5864. if (pf->vsi[v] &&
  5865. pf->vsi[v]->veb_idx == veb->idx &&
  5866. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  5867. ctl_vsi = pf->vsi[v];
  5868. break;
  5869. }
  5870. }
  5871. if (!ctl_vsi) {
  5872. dev_info(&pf->pdev->dev,
  5873. "missing owner VSI for veb_idx %d\n", veb->idx);
  5874. ret = -ENOENT;
  5875. goto end_reconstitute;
  5876. }
  5877. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  5878. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5879. ret = i40e_add_vsi(ctl_vsi);
  5880. if (ret) {
  5881. dev_info(&pf->pdev->dev,
  5882. "rebuild of veb_idx %d owner VSI failed: %d\n",
  5883. veb->idx, ret);
  5884. goto end_reconstitute;
  5885. }
  5886. i40e_vsi_reset_stats(ctl_vsi);
  5887. /* create the VEB in the switch and move the VSI onto the VEB */
  5888. ret = i40e_add_veb(veb, ctl_vsi);
  5889. if (ret)
  5890. goto end_reconstitute;
  5891. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  5892. veb->bridge_mode = BRIDGE_MODE_VEB;
  5893. else
  5894. veb->bridge_mode = BRIDGE_MODE_VEPA;
  5895. i40e_config_bridge_mode(veb);
  5896. /* create the remaining VSIs attached to this VEB */
  5897. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5898. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  5899. continue;
  5900. if (pf->vsi[v]->veb_idx == veb->idx) {
  5901. struct i40e_vsi *vsi = pf->vsi[v];
  5902. vsi->uplink_seid = veb->seid;
  5903. ret = i40e_add_vsi(vsi);
  5904. if (ret) {
  5905. dev_info(&pf->pdev->dev,
  5906. "rebuild of vsi_idx %d failed: %d\n",
  5907. v, ret);
  5908. goto end_reconstitute;
  5909. }
  5910. i40e_vsi_reset_stats(vsi);
  5911. }
  5912. }
  5913. /* create any VEBs attached to this VEB - RECURSION */
  5914. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  5915. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  5916. pf->veb[veb_idx]->uplink_seid = veb->seid;
  5917. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  5918. if (ret)
  5919. break;
  5920. }
  5921. }
  5922. end_reconstitute:
  5923. return ret;
  5924. }
  5925. /**
  5926. * i40e_get_capabilities - get info about the HW
  5927. * @pf: the PF struct
  5928. **/
  5929. static int i40e_get_capabilities(struct i40e_pf *pf)
  5930. {
  5931. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  5932. u16 data_size;
  5933. int buf_len;
  5934. int err;
  5935. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  5936. do {
  5937. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  5938. if (!cap_buf)
  5939. return -ENOMEM;
  5940. /* this loads the data into the hw struct for us */
  5941. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  5942. &data_size,
  5943. i40e_aqc_opc_list_func_capabilities,
  5944. NULL);
  5945. /* data loaded, buffer no longer needed */
  5946. kfree(cap_buf);
  5947. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  5948. /* retry with a larger buffer */
  5949. buf_len = data_size;
  5950. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  5951. dev_info(&pf->pdev->dev,
  5952. "capability discovery failed, err %s aq_err %s\n",
  5953. i40e_stat_str(&pf->hw, err),
  5954. i40e_aq_str(&pf->hw,
  5955. pf->hw.aq.asq_last_status));
  5956. return -ENODEV;
  5957. }
  5958. } while (err);
  5959. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  5960. dev_info(&pf->pdev->dev,
  5961. "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",
  5962. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  5963. pf->hw.func_caps.num_msix_vectors,
  5964. pf->hw.func_caps.num_msix_vectors_vf,
  5965. pf->hw.func_caps.fd_filters_guaranteed,
  5966. pf->hw.func_caps.fd_filters_best_effort,
  5967. pf->hw.func_caps.num_tx_qp,
  5968. pf->hw.func_caps.num_vsis);
  5969. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  5970. + pf->hw.func_caps.num_vfs)
  5971. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  5972. dev_info(&pf->pdev->dev,
  5973. "got num_vsis %d, setting num_vsis to %d\n",
  5974. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  5975. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  5976. }
  5977. return 0;
  5978. }
  5979. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  5980. /**
  5981. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  5982. * @pf: board private structure
  5983. **/
  5984. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  5985. {
  5986. struct i40e_vsi *vsi;
  5987. /* quick workaround for an NVM issue that leaves a critical register
  5988. * uninitialized
  5989. */
  5990. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  5991. static const u32 hkey[] = {
  5992. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  5993. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  5994. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  5995. 0x95b3a76d};
  5996. int i;
  5997. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  5998. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  5999. }
  6000. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  6001. return;
  6002. /* find existing VSI and see if it needs configuring */
  6003. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  6004. /* create a new VSI if none exists */
  6005. if (!vsi) {
  6006. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  6007. pf->vsi[pf->lan_vsi]->seid, 0);
  6008. if (!vsi) {
  6009. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  6010. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6011. return;
  6012. }
  6013. }
  6014. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  6015. }
  6016. /**
  6017. * i40e_fdir_teardown - release the Flow Director resources
  6018. * @pf: board private structure
  6019. **/
  6020. static void i40e_fdir_teardown(struct i40e_pf *pf)
  6021. {
  6022. struct i40e_vsi *vsi;
  6023. i40e_fdir_filter_exit(pf);
  6024. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  6025. if (vsi)
  6026. i40e_vsi_release(vsi);
  6027. }
  6028. /**
  6029. * i40e_prep_for_reset - prep for the core to reset
  6030. * @pf: board private structure
  6031. * @lock_acquired: indicates whether or not the lock has been acquired
  6032. * before this function was called.
  6033. *
  6034. * Close up the VFs and other things in prep for PF Reset.
  6035. **/
  6036. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  6037. {
  6038. struct i40e_hw *hw = &pf->hw;
  6039. i40e_status ret = 0;
  6040. u32 v;
  6041. clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  6042. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  6043. return;
  6044. if (i40e_check_asq_alive(&pf->hw))
  6045. i40e_vc_notify_reset(pf);
  6046. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  6047. /* quiesce the VSIs and their queues that are not already DOWN */
  6048. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  6049. if (!lock_acquired)
  6050. rtnl_lock();
  6051. i40e_pf_quiesce_all_vsi(pf);
  6052. if (!lock_acquired)
  6053. rtnl_unlock();
  6054. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6055. if (pf->vsi[v])
  6056. pf->vsi[v]->seid = 0;
  6057. }
  6058. i40e_shutdown_adminq(&pf->hw);
  6059. /* call shutdown HMC */
  6060. if (hw->hmc.hmc_obj) {
  6061. ret = i40e_shutdown_lan_hmc(hw);
  6062. if (ret)
  6063. dev_warn(&pf->pdev->dev,
  6064. "shutdown_lan_hmc failed: %d\n", ret);
  6065. }
  6066. }
  6067. /**
  6068. * i40e_send_version - update firmware with driver version
  6069. * @pf: PF struct
  6070. */
  6071. static void i40e_send_version(struct i40e_pf *pf)
  6072. {
  6073. struct i40e_driver_version dv;
  6074. dv.major_version = DRV_VERSION_MAJOR;
  6075. dv.minor_version = DRV_VERSION_MINOR;
  6076. dv.build_version = DRV_VERSION_BUILD;
  6077. dv.subbuild_version = 0;
  6078. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  6079. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  6080. }
  6081. /**
  6082. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  6083. * @pf: board private structure
  6084. **/
  6085. static int i40e_reset(struct i40e_pf *pf)
  6086. {
  6087. struct i40e_hw *hw = &pf->hw;
  6088. i40e_status ret;
  6089. ret = i40e_pf_reset(hw);
  6090. if (ret) {
  6091. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  6092. set_bit(__I40E_RESET_FAILED, &pf->state);
  6093. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  6094. } else {
  6095. pf->pfr_count++;
  6096. }
  6097. return ret;
  6098. }
  6099. /**
  6100. * i40e_rebuild - rebuild using a saved config
  6101. * @pf: board private structure
  6102. * @reinit: if the Main VSI needs to re-initialized.
  6103. * @lock_acquired: indicates whether or not the lock has been acquired
  6104. * before this function was called.
  6105. **/
  6106. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  6107. {
  6108. struct i40e_hw *hw = &pf->hw;
  6109. u8 set_fc_aq_fail = 0;
  6110. i40e_status ret;
  6111. u32 val;
  6112. int v;
  6113. if (test_bit(__I40E_DOWN, &pf->state))
  6114. goto clear_recovery;
  6115. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  6116. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  6117. ret = i40e_init_adminq(&pf->hw);
  6118. if (ret) {
  6119. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  6120. i40e_stat_str(&pf->hw, ret),
  6121. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6122. goto clear_recovery;
  6123. }
  6124. /* re-verify the eeprom if we just had an EMP reset */
  6125. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
  6126. i40e_verify_eeprom(pf);
  6127. i40e_clear_pxe_mode(hw);
  6128. ret = i40e_get_capabilities(pf);
  6129. if (ret)
  6130. goto end_core_reset;
  6131. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  6132. hw->func_caps.num_rx_qp, 0, 0);
  6133. if (ret) {
  6134. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  6135. goto end_core_reset;
  6136. }
  6137. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  6138. if (ret) {
  6139. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  6140. goto end_core_reset;
  6141. }
  6142. #ifdef CONFIG_I40E_DCB
  6143. ret = i40e_init_pf_dcb(pf);
  6144. if (ret) {
  6145. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  6146. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  6147. /* Continue without DCB enabled */
  6148. }
  6149. #endif /* CONFIG_I40E_DCB */
  6150. /* do basic switch setup */
  6151. if (!lock_acquired)
  6152. rtnl_lock();
  6153. ret = i40e_setup_pf_switch(pf, reinit);
  6154. if (ret)
  6155. goto end_unlock;
  6156. /* The driver only wants link up/down and module qualification
  6157. * reports from firmware. Note the negative logic.
  6158. */
  6159. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  6160. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  6161. I40E_AQ_EVENT_MEDIA_NA |
  6162. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  6163. if (ret)
  6164. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  6165. i40e_stat_str(&pf->hw, ret),
  6166. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6167. /* make sure our flow control settings are restored */
  6168. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  6169. if (ret)
  6170. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  6171. i40e_stat_str(&pf->hw, ret),
  6172. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6173. /* Rebuild the VSIs and VEBs that existed before reset.
  6174. * They are still in our local switch element arrays, so only
  6175. * need to rebuild the switch model in the HW.
  6176. *
  6177. * If there were VEBs but the reconstitution failed, we'll try
  6178. * try to recover minimal use by getting the basic PF VSI working.
  6179. */
  6180. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  6181. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  6182. /* find the one VEB connected to the MAC, and find orphans */
  6183. for (v = 0; v < I40E_MAX_VEB; v++) {
  6184. if (!pf->veb[v])
  6185. continue;
  6186. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  6187. pf->veb[v]->uplink_seid == 0) {
  6188. ret = i40e_reconstitute_veb(pf->veb[v]);
  6189. if (!ret)
  6190. continue;
  6191. /* If Main VEB failed, we're in deep doodoo,
  6192. * so give up rebuilding the switch and set up
  6193. * for minimal rebuild of PF VSI.
  6194. * If orphan failed, we'll report the error
  6195. * but try to keep going.
  6196. */
  6197. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  6198. dev_info(&pf->pdev->dev,
  6199. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  6200. ret);
  6201. pf->vsi[pf->lan_vsi]->uplink_seid
  6202. = pf->mac_seid;
  6203. break;
  6204. } else if (pf->veb[v]->uplink_seid == 0) {
  6205. dev_info(&pf->pdev->dev,
  6206. "rebuild of orphan VEB failed: %d\n",
  6207. ret);
  6208. }
  6209. }
  6210. }
  6211. }
  6212. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  6213. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  6214. /* no VEB, so rebuild only the Main VSI */
  6215. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  6216. if (ret) {
  6217. dev_info(&pf->pdev->dev,
  6218. "rebuild of Main VSI failed: %d\n", ret);
  6219. goto end_unlock;
  6220. }
  6221. }
  6222. /* Reconfigure hardware for allowing smaller MSS in the case
  6223. * of TSO, so that we avoid the MDD being fired and causing
  6224. * a reset in the case of small MSS+TSO.
  6225. */
  6226. #define I40E_REG_MSS 0x000E64DC
  6227. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  6228. #define I40E_64BYTE_MSS 0x400000
  6229. val = rd32(hw, I40E_REG_MSS);
  6230. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  6231. val &= ~I40E_REG_MSS_MIN_MASK;
  6232. val |= I40E_64BYTE_MSS;
  6233. wr32(hw, I40E_REG_MSS, val);
  6234. }
  6235. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  6236. msleep(75);
  6237. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  6238. if (ret)
  6239. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  6240. i40e_stat_str(&pf->hw, ret),
  6241. i40e_aq_str(&pf->hw,
  6242. pf->hw.aq.asq_last_status));
  6243. }
  6244. /* reinit the misc interrupt */
  6245. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6246. ret = i40e_setup_misc_vector(pf);
  6247. /* Add a filter to drop all Flow control frames from any VSI from being
  6248. * transmitted. By doing so we stop a malicious VF from sending out
  6249. * PAUSE or PFC frames and potentially controlling traffic for other
  6250. * PF/VF VSIs.
  6251. * The FW can still send Flow control frames if enabled.
  6252. */
  6253. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  6254. pf->main_vsi_seid);
  6255. /* restart the VSIs that were rebuilt and running before the reset */
  6256. i40e_pf_unquiesce_all_vsi(pf);
  6257. /* Release the RTNL lock before we start resetting VFs */
  6258. if (!lock_acquired)
  6259. rtnl_unlock();
  6260. if (pf->num_alloc_vfs) {
  6261. for (v = 0; v < pf->num_alloc_vfs; v++)
  6262. i40e_reset_vf(&pf->vf[v], true);
  6263. }
  6264. /* tell the firmware that we're starting */
  6265. i40e_send_version(pf);
  6266. /* We've already released the lock, so don't do it again */
  6267. goto end_core_reset;
  6268. end_unlock:
  6269. if (!lock_acquired)
  6270. rtnl_unlock();
  6271. end_core_reset:
  6272. clear_bit(__I40E_RESET_FAILED, &pf->state);
  6273. clear_recovery:
  6274. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  6275. }
  6276. /**
  6277. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  6278. * @pf: board private structure
  6279. * @reinit: if the Main VSI needs to re-initialized.
  6280. * @lock_acquired: indicates whether or not the lock has been acquired
  6281. * before this function was called.
  6282. **/
  6283. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  6284. bool lock_acquired)
  6285. {
  6286. int ret;
  6287. /* Now we wait for GRST to settle out.
  6288. * We don't have to delete the VEBs or VSIs from the hw switch
  6289. * because the reset will make them disappear.
  6290. */
  6291. ret = i40e_reset(pf);
  6292. if (!ret)
  6293. i40e_rebuild(pf, reinit, lock_acquired);
  6294. }
  6295. /**
  6296. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  6297. * @pf: board private structure
  6298. *
  6299. * Close up the VFs and other things in prep for a Core Reset,
  6300. * then get ready to rebuild the world.
  6301. * @lock_acquired: indicates whether or not the lock has been acquired
  6302. * before this function was called.
  6303. **/
  6304. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  6305. {
  6306. i40e_prep_for_reset(pf, lock_acquired);
  6307. i40e_reset_and_rebuild(pf, false, lock_acquired);
  6308. }
  6309. /**
  6310. * i40e_handle_mdd_event
  6311. * @pf: pointer to the PF structure
  6312. *
  6313. * Called from the MDD irq handler to identify possibly malicious vfs
  6314. **/
  6315. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  6316. {
  6317. struct i40e_hw *hw = &pf->hw;
  6318. bool mdd_detected = false;
  6319. bool pf_mdd_detected = false;
  6320. struct i40e_vf *vf;
  6321. u32 reg;
  6322. int i;
  6323. if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
  6324. return;
  6325. /* find what triggered the MDD event */
  6326. reg = rd32(hw, I40E_GL_MDET_TX);
  6327. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  6328. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  6329. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  6330. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  6331. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  6332. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  6333. I40E_GL_MDET_TX_EVENT_SHIFT;
  6334. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  6335. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  6336. pf->hw.func_caps.base_queue;
  6337. if (netif_msg_tx_err(pf))
  6338. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  6339. event, queue, pf_num, vf_num);
  6340. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  6341. mdd_detected = true;
  6342. }
  6343. reg = rd32(hw, I40E_GL_MDET_RX);
  6344. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  6345. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  6346. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  6347. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  6348. I40E_GL_MDET_RX_EVENT_SHIFT;
  6349. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  6350. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  6351. pf->hw.func_caps.base_queue;
  6352. if (netif_msg_rx_err(pf))
  6353. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  6354. event, queue, func);
  6355. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  6356. mdd_detected = true;
  6357. }
  6358. if (mdd_detected) {
  6359. reg = rd32(hw, I40E_PF_MDET_TX);
  6360. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  6361. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  6362. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  6363. pf_mdd_detected = true;
  6364. }
  6365. reg = rd32(hw, I40E_PF_MDET_RX);
  6366. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  6367. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  6368. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  6369. pf_mdd_detected = true;
  6370. }
  6371. /* Queue belongs to the PF, initiate a reset */
  6372. if (pf_mdd_detected) {
  6373. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  6374. i40e_service_event_schedule(pf);
  6375. }
  6376. }
  6377. /* see if one of the VFs needs its hand slapped */
  6378. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  6379. vf = &(pf->vf[i]);
  6380. reg = rd32(hw, I40E_VP_MDET_TX(i));
  6381. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  6382. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  6383. vf->num_mdd_events++;
  6384. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  6385. i);
  6386. }
  6387. reg = rd32(hw, I40E_VP_MDET_RX(i));
  6388. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  6389. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  6390. vf->num_mdd_events++;
  6391. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  6392. i);
  6393. }
  6394. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  6395. dev_info(&pf->pdev->dev,
  6396. "Too many MDD events on VF %d, disabled\n", i);
  6397. dev_info(&pf->pdev->dev,
  6398. "Use PF Control I/F to re-enable the VF\n");
  6399. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  6400. }
  6401. }
  6402. /* re-enable mdd interrupt cause */
  6403. clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  6404. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  6405. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  6406. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  6407. i40e_flush(hw);
  6408. }
  6409. /**
  6410. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  6411. * @pf: board private structure
  6412. **/
  6413. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  6414. {
  6415. struct i40e_hw *hw = &pf->hw;
  6416. i40e_status ret;
  6417. u16 port;
  6418. int i;
  6419. if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
  6420. return;
  6421. pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
  6422. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6423. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  6424. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  6425. port = pf->udp_ports[i].index;
  6426. if (port)
  6427. ret = i40e_aq_add_udp_tunnel(hw, port,
  6428. pf->udp_ports[i].type,
  6429. NULL, NULL);
  6430. else
  6431. ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
  6432. if (ret) {
  6433. dev_dbg(&pf->pdev->dev,
  6434. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  6435. pf->udp_ports[i].type ? "vxlan" : "geneve",
  6436. port ? "add" : "delete",
  6437. port, i,
  6438. i40e_stat_str(&pf->hw, ret),
  6439. i40e_aq_str(&pf->hw,
  6440. pf->hw.aq.asq_last_status));
  6441. pf->udp_ports[i].index = 0;
  6442. }
  6443. }
  6444. }
  6445. }
  6446. /**
  6447. * i40e_service_task - Run the driver's async subtasks
  6448. * @work: pointer to work_struct containing our data
  6449. **/
  6450. static void i40e_service_task(struct work_struct *work)
  6451. {
  6452. struct i40e_pf *pf = container_of(work,
  6453. struct i40e_pf,
  6454. service_task);
  6455. unsigned long start_time = jiffies;
  6456. /* don't bother with service tasks if a reset is in progress */
  6457. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  6458. return;
  6459. }
  6460. if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
  6461. return;
  6462. i40e_detect_recover_hung(pf);
  6463. i40e_sync_filters_subtask(pf);
  6464. i40e_reset_subtask(pf);
  6465. i40e_handle_mdd_event(pf);
  6466. i40e_vc_process_vflr_event(pf);
  6467. i40e_watchdog_subtask(pf);
  6468. i40e_fdir_reinit_subtask(pf);
  6469. if (pf->flags & I40E_FLAG_CLIENT_RESET) {
  6470. /* Client subtask will reopen next time through. */
  6471. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  6472. pf->flags &= ~I40E_FLAG_CLIENT_RESET;
  6473. } else {
  6474. i40e_client_subtask(pf);
  6475. if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
  6476. i40e_notify_client_of_l2_param_changes(
  6477. pf->vsi[pf->lan_vsi]);
  6478. pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
  6479. }
  6480. }
  6481. i40e_sync_filters_subtask(pf);
  6482. i40e_sync_udp_filters_subtask(pf);
  6483. i40e_clean_adminq_subtask(pf);
  6484. /* flush memory to make sure state is correct before next watchdog */
  6485. smp_mb__before_atomic();
  6486. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  6487. /* If the tasks have taken longer than one timer cycle or there
  6488. * is more work to be done, reschedule the service task now
  6489. * rather than wait for the timer to tick again.
  6490. */
  6491. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  6492. test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
  6493. test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
  6494. test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  6495. i40e_service_event_schedule(pf);
  6496. }
  6497. /**
  6498. * i40e_service_timer - timer callback
  6499. * @data: pointer to PF struct
  6500. **/
  6501. static void i40e_service_timer(unsigned long data)
  6502. {
  6503. struct i40e_pf *pf = (struct i40e_pf *)data;
  6504. mod_timer(&pf->service_timer,
  6505. round_jiffies(jiffies + pf->service_timer_period));
  6506. i40e_service_event_schedule(pf);
  6507. }
  6508. /**
  6509. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  6510. * @vsi: the VSI being configured
  6511. **/
  6512. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  6513. {
  6514. struct i40e_pf *pf = vsi->back;
  6515. switch (vsi->type) {
  6516. case I40E_VSI_MAIN:
  6517. vsi->alloc_queue_pairs = pf->num_lan_qps;
  6518. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6519. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6520. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6521. vsi->num_q_vectors = pf->num_lan_msix;
  6522. else
  6523. vsi->num_q_vectors = 1;
  6524. break;
  6525. case I40E_VSI_FDIR:
  6526. vsi->alloc_queue_pairs = 1;
  6527. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  6528. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6529. vsi->num_q_vectors = pf->num_fdsb_msix;
  6530. break;
  6531. case I40E_VSI_VMDQ2:
  6532. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  6533. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6534. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6535. vsi->num_q_vectors = pf->num_vmdq_msix;
  6536. break;
  6537. case I40E_VSI_SRIOV:
  6538. vsi->alloc_queue_pairs = pf->num_vf_qps;
  6539. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6540. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6541. break;
  6542. default:
  6543. WARN_ON(1);
  6544. return -ENODATA;
  6545. }
  6546. return 0;
  6547. }
  6548. /**
  6549. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  6550. * @type: VSI pointer
  6551. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  6552. *
  6553. * On error: returns error code (negative)
  6554. * On success: returns 0
  6555. **/
  6556. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  6557. {
  6558. int size;
  6559. int ret = 0;
  6560. /* allocate memory for both Tx and Rx ring pointers */
  6561. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
  6562. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  6563. if (!vsi->tx_rings)
  6564. return -ENOMEM;
  6565. vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
  6566. if (alloc_qvectors) {
  6567. /* allocate memory for q_vector pointers */
  6568. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  6569. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  6570. if (!vsi->q_vectors) {
  6571. ret = -ENOMEM;
  6572. goto err_vectors;
  6573. }
  6574. }
  6575. return ret;
  6576. err_vectors:
  6577. kfree(vsi->tx_rings);
  6578. return ret;
  6579. }
  6580. /**
  6581. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  6582. * @pf: board private structure
  6583. * @type: type of VSI
  6584. *
  6585. * On error: returns error code (negative)
  6586. * On success: returns vsi index in PF (positive)
  6587. **/
  6588. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  6589. {
  6590. int ret = -ENODEV;
  6591. struct i40e_vsi *vsi;
  6592. int vsi_idx;
  6593. int i;
  6594. /* Need to protect the allocation of the VSIs at the PF level */
  6595. mutex_lock(&pf->switch_mutex);
  6596. /* VSI list may be fragmented if VSI creation/destruction has
  6597. * been happening. We can afford to do a quick scan to look
  6598. * for any free VSIs in the list.
  6599. *
  6600. * find next empty vsi slot, looping back around if necessary
  6601. */
  6602. i = pf->next_vsi;
  6603. while (i < pf->num_alloc_vsi && pf->vsi[i])
  6604. i++;
  6605. if (i >= pf->num_alloc_vsi) {
  6606. i = 0;
  6607. while (i < pf->next_vsi && pf->vsi[i])
  6608. i++;
  6609. }
  6610. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  6611. vsi_idx = i; /* Found one! */
  6612. } else {
  6613. ret = -ENODEV;
  6614. goto unlock_pf; /* out of VSI slots! */
  6615. }
  6616. pf->next_vsi = ++i;
  6617. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  6618. if (!vsi) {
  6619. ret = -ENOMEM;
  6620. goto unlock_pf;
  6621. }
  6622. vsi->type = type;
  6623. vsi->back = pf;
  6624. set_bit(__I40E_DOWN, &vsi->state);
  6625. vsi->flags = 0;
  6626. vsi->idx = vsi_idx;
  6627. vsi->int_rate_limit = 0;
  6628. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  6629. pf->rss_table_size : 64;
  6630. vsi->netdev_registered = false;
  6631. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  6632. hash_init(vsi->mac_filter_hash);
  6633. vsi->irqs_ready = false;
  6634. ret = i40e_set_num_rings_in_vsi(vsi);
  6635. if (ret)
  6636. goto err_rings;
  6637. ret = i40e_vsi_alloc_arrays(vsi, true);
  6638. if (ret)
  6639. goto err_rings;
  6640. /* Setup default MSIX irq handler for VSI */
  6641. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  6642. /* Initialize VSI lock */
  6643. spin_lock_init(&vsi->mac_filter_hash_lock);
  6644. pf->vsi[vsi_idx] = vsi;
  6645. ret = vsi_idx;
  6646. goto unlock_pf;
  6647. err_rings:
  6648. pf->next_vsi = i - 1;
  6649. kfree(vsi);
  6650. unlock_pf:
  6651. mutex_unlock(&pf->switch_mutex);
  6652. return ret;
  6653. }
  6654. /**
  6655. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  6656. * @type: VSI pointer
  6657. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  6658. *
  6659. * On error: returns error code (negative)
  6660. * On success: returns 0
  6661. **/
  6662. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  6663. {
  6664. /* free the ring and vector containers */
  6665. if (free_qvectors) {
  6666. kfree(vsi->q_vectors);
  6667. vsi->q_vectors = NULL;
  6668. }
  6669. kfree(vsi->tx_rings);
  6670. vsi->tx_rings = NULL;
  6671. vsi->rx_rings = NULL;
  6672. }
  6673. /**
  6674. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  6675. * and lookup table
  6676. * @vsi: Pointer to VSI structure
  6677. */
  6678. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  6679. {
  6680. if (!vsi)
  6681. return;
  6682. kfree(vsi->rss_hkey_user);
  6683. vsi->rss_hkey_user = NULL;
  6684. kfree(vsi->rss_lut_user);
  6685. vsi->rss_lut_user = NULL;
  6686. }
  6687. /**
  6688. * i40e_vsi_clear - Deallocate the VSI provided
  6689. * @vsi: the VSI being un-configured
  6690. **/
  6691. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  6692. {
  6693. struct i40e_pf *pf;
  6694. if (!vsi)
  6695. return 0;
  6696. if (!vsi->back)
  6697. goto free_vsi;
  6698. pf = vsi->back;
  6699. mutex_lock(&pf->switch_mutex);
  6700. if (!pf->vsi[vsi->idx]) {
  6701. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  6702. vsi->idx, vsi->idx, vsi, vsi->type);
  6703. goto unlock_vsi;
  6704. }
  6705. if (pf->vsi[vsi->idx] != vsi) {
  6706. dev_err(&pf->pdev->dev,
  6707. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  6708. pf->vsi[vsi->idx]->idx,
  6709. pf->vsi[vsi->idx],
  6710. pf->vsi[vsi->idx]->type,
  6711. vsi->idx, vsi, vsi->type);
  6712. goto unlock_vsi;
  6713. }
  6714. /* updates the PF for this cleared vsi */
  6715. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  6716. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  6717. i40e_vsi_free_arrays(vsi, true);
  6718. i40e_clear_rss_config_user(vsi);
  6719. pf->vsi[vsi->idx] = NULL;
  6720. if (vsi->idx < pf->next_vsi)
  6721. pf->next_vsi = vsi->idx;
  6722. unlock_vsi:
  6723. mutex_unlock(&pf->switch_mutex);
  6724. free_vsi:
  6725. kfree(vsi);
  6726. return 0;
  6727. }
  6728. /**
  6729. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  6730. * @vsi: the VSI being cleaned
  6731. **/
  6732. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  6733. {
  6734. int i;
  6735. if (vsi->tx_rings && vsi->tx_rings[0]) {
  6736. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6737. kfree_rcu(vsi->tx_rings[i], rcu);
  6738. vsi->tx_rings[i] = NULL;
  6739. vsi->rx_rings[i] = NULL;
  6740. }
  6741. }
  6742. }
  6743. /**
  6744. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  6745. * @vsi: the VSI being configured
  6746. **/
  6747. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  6748. {
  6749. struct i40e_ring *tx_ring, *rx_ring;
  6750. struct i40e_pf *pf = vsi->back;
  6751. int i;
  6752. /* Set basic values in the rings to be used later during open() */
  6753. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6754. /* allocate space for both Tx and Rx in one shot */
  6755. tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
  6756. if (!tx_ring)
  6757. goto err_out;
  6758. tx_ring->queue_index = i;
  6759. tx_ring->reg_idx = vsi->base_queue + i;
  6760. tx_ring->ring_active = false;
  6761. tx_ring->vsi = vsi;
  6762. tx_ring->netdev = vsi->netdev;
  6763. tx_ring->dev = &pf->pdev->dev;
  6764. tx_ring->count = vsi->num_desc;
  6765. tx_ring->size = 0;
  6766. tx_ring->dcb_tc = 0;
  6767. if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
  6768. tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  6769. tx_ring->tx_itr_setting = pf->tx_itr_default;
  6770. vsi->tx_rings[i] = tx_ring;
  6771. rx_ring = &tx_ring[1];
  6772. rx_ring->queue_index = i;
  6773. rx_ring->reg_idx = vsi->base_queue + i;
  6774. rx_ring->ring_active = false;
  6775. rx_ring->vsi = vsi;
  6776. rx_ring->netdev = vsi->netdev;
  6777. rx_ring->dev = &pf->pdev->dev;
  6778. rx_ring->count = vsi->num_desc;
  6779. rx_ring->size = 0;
  6780. rx_ring->dcb_tc = 0;
  6781. rx_ring->rx_itr_setting = pf->rx_itr_default;
  6782. vsi->rx_rings[i] = rx_ring;
  6783. }
  6784. return 0;
  6785. err_out:
  6786. i40e_vsi_clear_rings(vsi);
  6787. return -ENOMEM;
  6788. }
  6789. /**
  6790. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  6791. * @pf: board private structure
  6792. * @vectors: the number of MSI-X vectors to request
  6793. *
  6794. * Returns the number of vectors reserved, or error
  6795. **/
  6796. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  6797. {
  6798. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  6799. I40E_MIN_MSIX, vectors);
  6800. if (vectors < 0) {
  6801. dev_info(&pf->pdev->dev,
  6802. "MSI-X vector reservation failed: %d\n", vectors);
  6803. vectors = 0;
  6804. }
  6805. return vectors;
  6806. }
  6807. /**
  6808. * i40e_init_msix - Setup the MSIX capability
  6809. * @pf: board private structure
  6810. *
  6811. * Work with the OS to set up the MSIX vectors needed.
  6812. *
  6813. * Returns the number of vectors reserved or negative on failure
  6814. **/
  6815. static int i40e_init_msix(struct i40e_pf *pf)
  6816. {
  6817. struct i40e_hw *hw = &pf->hw;
  6818. int cpus, extra_vectors;
  6819. int vectors_left;
  6820. int v_budget, i;
  6821. int v_actual;
  6822. int iwarp_requested = 0;
  6823. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  6824. return -ENODEV;
  6825. /* The number of vectors we'll request will be comprised of:
  6826. * - Add 1 for "other" cause for Admin Queue events, etc.
  6827. * - The number of LAN queue pairs
  6828. * - Queues being used for RSS.
  6829. * We don't need as many as max_rss_size vectors.
  6830. * use rss_size instead in the calculation since that
  6831. * is governed by number of cpus in the system.
  6832. * - assumes symmetric Tx/Rx pairing
  6833. * - The number of VMDq pairs
  6834. * - The CPU count within the NUMA node if iWARP is enabled
  6835. * Once we count this up, try the request.
  6836. *
  6837. * If we can't get what we want, we'll simplify to nearly nothing
  6838. * and try again. If that still fails, we punt.
  6839. */
  6840. vectors_left = hw->func_caps.num_msix_vectors;
  6841. v_budget = 0;
  6842. /* reserve one vector for miscellaneous handler */
  6843. if (vectors_left) {
  6844. v_budget++;
  6845. vectors_left--;
  6846. }
  6847. /* reserve some vectors for the main PF traffic queues. Initially we
  6848. * only reserve at most 50% of the available vectors, in the case that
  6849. * the number of online CPUs is large. This ensures that we can enable
  6850. * extra features as well. Once we've enabled the other features, we
  6851. * will use any remaining vectors to reach as close as we can to the
  6852. * number of online CPUs.
  6853. */
  6854. cpus = num_online_cpus();
  6855. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  6856. vectors_left -= pf->num_lan_msix;
  6857. /* reserve one vector for sideband flow director */
  6858. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6859. if (vectors_left) {
  6860. pf->num_fdsb_msix = 1;
  6861. v_budget++;
  6862. vectors_left--;
  6863. } else {
  6864. pf->num_fdsb_msix = 0;
  6865. }
  6866. }
  6867. /* can we reserve enough for iWARP? */
  6868. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6869. iwarp_requested = pf->num_iwarp_msix;
  6870. if (!vectors_left)
  6871. pf->num_iwarp_msix = 0;
  6872. else if (vectors_left < pf->num_iwarp_msix)
  6873. pf->num_iwarp_msix = 1;
  6874. v_budget += pf->num_iwarp_msix;
  6875. vectors_left -= pf->num_iwarp_msix;
  6876. }
  6877. /* any vectors left over go for VMDq support */
  6878. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  6879. int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
  6880. int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
  6881. if (!vectors_left) {
  6882. pf->num_vmdq_msix = 0;
  6883. pf->num_vmdq_qps = 0;
  6884. } else {
  6885. /* if we're short on vectors for what's desired, we limit
  6886. * the queues per vmdq. If this is still more than are
  6887. * available, the user will need to change the number of
  6888. * queues/vectors used by the PF later with the ethtool
  6889. * channels command
  6890. */
  6891. if (vmdq_vecs < vmdq_vecs_wanted)
  6892. pf->num_vmdq_qps = 1;
  6893. pf->num_vmdq_msix = pf->num_vmdq_qps;
  6894. v_budget += vmdq_vecs;
  6895. vectors_left -= vmdq_vecs;
  6896. }
  6897. }
  6898. /* On systems with a large number of SMP cores, we previously limited
  6899. * the number of vectors for num_lan_msix to be at most 50% of the
  6900. * available vectors, to allow for other features. Now, we add back
  6901. * the remaining vectors. However, we ensure that the total
  6902. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  6903. * calculate the number of vectors we can add without going over the
  6904. * cap of CPUs. For systems with a small number of CPUs this will be
  6905. * zero.
  6906. */
  6907. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  6908. pf->num_lan_msix += extra_vectors;
  6909. vectors_left -= extra_vectors;
  6910. WARN(vectors_left < 0,
  6911. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  6912. v_budget += pf->num_lan_msix;
  6913. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  6914. GFP_KERNEL);
  6915. if (!pf->msix_entries)
  6916. return -ENOMEM;
  6917. for (i = 0; i < v_budget; i++)
  6918. pf->msix_entries[i].entry = i;
  6919. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  6920. if (v_actual < I40E_MIN_MSIX) {
  6921. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  6922. kfree(pf->msix_entries);
  6923. pf->msix_entries = NULL;
  6924. pci_disable_msix(pf->pdev);
  6925. return -ENODEV;
  6926. } else if (v_actual == I40E_MIN_MSIX) {
  6927. /* Adjust for minimal MSIX use */
  6928. pf->num_vmdq_vsis = 0;
  6929. pf->num_vmdq_qps = 0;
  6930. pf->num_lan_qps = 1;
  6931. pf->num_lan_msix = 1;
  6932. } else if (!vectors_left) {
  6933. /* If we have limited resources, we will start with no vectors
  6934. * for the special features and then allocate vectors to some
  6935. * of these features based on the policy and at the end disable
  6936. * the features that did not get any vectors.
  6937. */
  6938. int vec;
  6939. dev_info(&pf->pdev->dev,
  6940. "MSI-X vector limit reached, attempting to redistribute vectors\n");
  6941. /* reserve the misc vector */
  6942. vec = v_actual - 1;
  6943. /* Scale vector usage down */
  6944. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  6945. pf->num_vmdq_vsis = 1;
  6946. pf->num_vmdq_qps = 1;
  6947. /* partition out the remaining vectors */
  6948. switch (vec) {
  6949. case 2:
  6950. pf->num_lan_msix = 1;
  6951. break;
  6952. case 3:
  6953. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6954. pf->num_lan_msix = 1;
  6955. pf->num_iwarp_msix = 1;
  6956. } else {
  6957. pf->num_lan_msix = 2;
  6958. }
  6959. break;
  6960. default:
  6961. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6962. pf->num_iwarp_msix = min_t(int, (vec / 3),
  6963. iwarp_requested);
  6964. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  6965. I40E_DEFAULT_NUM_VMDQ_VSI);
  6966. } else {
  6967. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  6968. I40E_DEFAULT_NUM_VMDQ_VSI);
  6969. }
  6970. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6971. pf->num_fdsb_msix = 1;
  6972. vec--;
  6973. }
  6974. pf->num_lan_msix = min_t(int,
  6975. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  6976. pf->num_lan_msix);
  6977. pf->num_lan_qps = pf->num_lan_msix;
  6978. break;
  6979. }
  6980. }
  6981. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  6982. (pf->num_fdsb_msix == 0)) {
  6983. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  6984. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6985. }
  6986. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  6987. (pf->num_vmdq_msix == 0)) {
  6988. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  6989. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  6990. }
  6991. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  6992. (pf->num_iwarp_msix == 0)) {
  6993. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  6994. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  6995. }
  6996. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  6997. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  6998. pf->num_lan_msix,
  6999. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  7000. pf->num_fdsb_msix,
  7001. pf->num_iwarp_msix);
  7002. return v_actual;
  7003. }
  7004. /**
  7005. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  7006. * @vsi: the VSI being configured
  7007. * @v_idx: index of the vector in the vsi struct
  7008. * @cpu: cpu to be used on affinity_mask
  7009. *
  7010. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  7011. **/
  7012. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  7013. {
  7014. struct i40e_q_vector *q_vector;
  7015. /* allocate q_vector */
  7016. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  7017. if (!q_vector)
  7018. return -ENOMEM;
  7019. q_vector->vsi = vsi;
  7020. q_vector->v_idx = v_idx;
  7021. cpumask_set_cpu(cpu, &q_vector->affinity_mask);
  7022. if (vsi->netdev)
  7023. netif_napi_add(vsi->netdev, &q_vector->napi,
  7024. i40e_napi_poll, NAPI_POLL_WEIGHT);
  7025. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  7026. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  7027. /* tie q_vector and vsi together */
  7028. vsi->q_vectors[v_idx] = q_vector;
  7029. return 0;
  7030. }
  7031. /**
  7032. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  7033. * @vsi: the VSI being configured
  7034. *
  7035. * We allocate one q_vector per queue interrupt. If allocation fails we
  7036. * return -ENOMEM.
  7037. **/
  7038. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  7039. {
  7040. struct i40e_pf *pf = vsi->back;
  7041. int err, v_idx, num_q_vectors, current_cpu;
  7042. /* if not MSIX, give the one vector only to the LAN VSI */
  7043. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  7044. num_q_vectors = vsi->num_q_vectors;
  7045. else if (vsi == pf->vsi[pf->lan_vsi])
  7046. num_q_vectors = 1;
  7047. else
  7048. return -EINVAL;
  7049. current_cpu = cpumask_first(cpu_online_mask);
  7050. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  7051. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  7052. if (err)
  7053. goto err_out;
  7054. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  7055. if (unlikely(current_cpu >= nr_cpu_ids))
  7056. current_cpu = cpumask_first(cpu_online_mask);
  7057. }
  7058. return 0;
  7059. err_out:
  7060. while (v_idx--)
  7061. i40e_free_q_vector(vsi, v_idx);
  7062. return err;
  7063. }
  7064. /**
  7065. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  7066. * @pf: board private structure to initialize
  7067. **/
  7068. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  7069. {
  7070. int vectors = 0;
  7071. ssize_t size;
  7072. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  7073. vectors = i40e_init_msix(pf);
  7074. if (vectors < 0) {
  7075. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  7076. I40E_FLAG_IWARP_ENABLED |
  7077. I40E_FLAG_RSS_ENABLED |
  7078. I40E_FLAG_DCB_CAPABLE |
  7079. I40E_FLAG_DCB_ENABLED |
  7080. I40E_FLAG_SRIOV_ENABLED |
  7081. I40E_FLAG_FD_SB_ENABLED |
  7082. I40E_FLAG_FD_ATR_ENABLED |
  7083. I40E_FLAG_VMDQ_ENABLED);
  7084. /* rework the queue expectations without MSIX */
  7085. i40e_determine_queue_usage(pf);
  7086. }
  7087. }
  7088. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  7089. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  7090. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  7091. vectors = pci_enable_msi(pf->pdev);
  7092. if (vectors < 0) {
  7093. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  7094. vectors);
  7095. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  7096. }
  7097. vectors = 1; /* one MSI or Legacy vector */
  7098. }
  7099. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  7100. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  7101. /* set up vector assignment tracking */
  7102. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  7103. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  7104. if (!pf->irq_pile) {
  7105. dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
  7106. return -ENOMEM;
  7107. }
  7108. pf->irq_pile->num_entries = vectors;
  7109. pf->irq_pile->search_hint = 0;
  7110. /* track first vector for misc interrupts, ignore return */
  7111. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  7112. return 0;
  7113. }
  7114. /**
  7115. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  7116. * @pf: board private structure
  7117. *
  7118. * This sets up the handler for MSIX 0, which is used to manage the
  7119. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  7120. * when in MSI or Legacy interrupt mode.
  7121. **/
  7122. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  7123. {
  7124. struct i40e_hw *hw = &pf->hw;
  7125. int err = 0;
  7126. /* Only request the irq if this is the first time through, and
  7127. * not when we're rebuilding after a Reset
  7128. */
  7129. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  7130. err = request_irq(pf->msix_entries[0].vector,
  7131. i40e_intr, 0, pf->int_name, pf);
  7132. if (err) {
  7133. dev_info(&pf->pdev->dev,
  7134. "request_irq for %s failed: %d\n",
  7135. pf->int_name, err);
  7136. return -EFAULT;
  7137. }
  7138. }
  7139. i40e_enable_misc_int_causes(pf);
  7140. /* associate no queues to the misc vector */
  7141. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  7142. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  7143. i40e_flush(hw);
  7144. i40e_irq_dynamic_enable_icr0(pf, true);
  7145. return err;
  7146. }
  7147. /**
  7148. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  7149. * @vsi: vsi structure
  7150. * @seed: RSS hash seed
  7151. **/
  7152. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7153. u8 *lut, u16 lut_size)
  7154. {
  7155. struct i40e_pf *pf = vsi->back;
  7156. struct i40e_hw *hw = &pf->hw;
  7157. int ret = 0;
  7158. if (seed) {
  7159. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  7160. (struct i40e_aqc_get_set_rss_key_data *)seed;
  7161. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  7162. if (ret) {
  7163. dev_info(&pf->pdev->dev,
  7164. "Cannot set RSS key, err %s aq_err %s\n",
  7165. i40e_stat_str(hw, ret),
  7166. i40e_aq_str(hw, hw->aq.asq_last_status));
  7167. return ret;
  7168. }
  7169. }
  7170. if (lut) {
  7171. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7172. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7173. if (ret) {
  7174. dev_info(&pf->pdev->dev,
  7175. "Cannot set RSS lut, err %s aq_err %s\n",
  7176. i40e_stat_str(hw, ret),
  7177. i40e_aq_str(hw, hw->aq.asq_last_status));
  7178. return ret;
  7179. }
  7180. }
  7181. return ret;
  7182. }
  7183. /**
  7184. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  7185. * @vsi: Pointer to vsi structure
  7186. * @seed: Buffter to store the hash keys
  7187. * @lut: Buffer to store the lookup table entries
  7188. * @lut_size: Size of buffer to store the lookup table entries
  7189. *
  7190. * Return 0 on success, negative on failure
  7191. */
  7192. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7193. u8 *lut, u16 lut_size)
  7194. {
  7195. struct i40e_pf *pf = vsi->back;
  7196. struct i40e_hw *hw = &pf->hw;
  7197. int ret = 0;
  7198. if (seed) {
  7199. ret = i40e_aq_get_rss_key(hw, vsi->id,
  7200. (struct i40e_aqc_get_set_rss_key_data *)seed);
  7201. if (ret) {
  7202. dev_info(&pf->pdev->dev,
  7203. "Cannot get RSS key, err %s aq_err %s\n",
  7204. i40e_stat_str(&pf->hw, ret),
  7205. i40e_aq_str(&pf->hw,
  7206. pf->hw.aq.asq_last_status));
  7207. return ret;
  7208. }
  7209. }
  7210. if (lut) {
  7211. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7212. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7213. if (ret) {
  7214. dev_info(&pf->pdev->dev,
  7215. "Cannot get RSS lut, err %s aq_err %s\n",
  7216. i40e_stat_str(&pf->hw, ret),
  7217. i40e_aq_str(&pf->hw,
  7218. pf->hw.aq.asq_last_status));
  7219. return ret;
  7220. }
  7221. }
  7222. return ret;
  7223. }
  7224. /**
  7225. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  7226. * @vsi: VSI structure
  7227. **/
  7228. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  7229. {
  7230. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7231. struct i40e_pf *pf = vsi->back;
  7232. u8 *lut;
  7233. int ret;
  7234. if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
  7235. return 0;
  7236. if (!vsi->rss_size)
  7237. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7238. vsi->num_queue_pairs);
  7239. if (!vsi->rss_size)
  7240. return -EINVAL;
  7241. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7242. if (!lut)
  7243. return -ENOMEM;
  7244. /* Use the user configured hash keys and lookup table if there is one,
  7245. * otherwise use default
  7246. */
  7247. if (vsi->rss_lut_user)
  7248. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7249. else
  7250. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7251. if (vsi->rss_hkey_user)
  7252. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7253. else
  7254. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7255. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  7256. kfree(lut);
  7257. return ret;
  7258. }
  7259. /**
  7260. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  7261. * @vsi: Pointer to vsi structure
  7262. * @seed: RSS hash seed
  7263. * @lut: Lookup table
  7264. * @lut_size: Lookup table size
  7265. *
  7266. * Returns 0 on success, negative on failure
  7267. **/
  7268. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  7269. const u8 *lut, u16 lut_size)
  7270. {
  7271. struct i40e_pf *pf = vsi->back;
  7272. struct i40e_hw *hw = &pf->hw;
  7273. u16 vf_id = vsi->vf_id;
  7274. u8 i;
  7275. /* Fill out hash function seed */
  7276. if (seed) {
  7277. u32 *seed_dw = (u32 *)seed;
  7278. if (vsi->type == I40E_VSI_MAIN) {
  7279. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7280. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  7281. } else if (vsi->type == I40E_VSI_SRIOV) {
  7282. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  7283. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  7284. } else {
  7285. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  7286. }
  7287. }
  7288. if (lut) {
  7289. u32 *lut_dw = (u32 *)lut;
  7290. if (vsi->type == I40E_VSI_MAIN) {
  7291. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7292. return -EINVAL;
  7293. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7294. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  7295. } else if (vsi->type == I40E_VSI_SRIOV) {
  7296. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  7297. return -EINVAL;
  7298. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7299. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  7300. } else {
  7301. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7302. }
  7303. }
  7304. i40e_flush(hw);
  7305. return 0;
  7306. }
  7307. /**
  7308. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  7309. * @vsi: Pointer to VSI structure
  7310. * @seed: Buffer to store the keys
  7311. * @lut: Buffer to store the lookup table entries
  7312. * @lut_size: Size of buffer to store the lookup table entries
  7313. *
  7314. * Returns 0 on success, negative on failure
  7315. */
  7316. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  7317. u8 *lut, u16 lut_size)
  7318. {
  7319. struct i40e_pf *pf = vsi->back;
  7320. struct i40e_hw *hw = &pf->hw;
  7321. u16 i;
  7322. if (seed) {
  7323. u32 *seed_dw = (u32 *)seed;
  7324. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7325. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  7326. }
  7327. if (lut) {
  7328. u32 *lut_dw = (u32 *)lut;
  7329. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7330. return -EINVAL;
  7331. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7332. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  7333. }
  7334. return 0;
  7335. }
  7336. /**
  7337. * i40e_config_rss - Configure RSS keys and lut
  7338. * @vsi: Pointer to VSI structure
  7339. * @seed: RSS hash seed
  7340. * @lut: Lookup table
  7341. * @lut_size: Lookup table size
  7342. *
  7343. * Returns 0 on success, negative on failure
  7344. */
  7345. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7346. {
  7347. struct i40e_pf *pf = vsi->back;
  7348. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7349. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  7350. else
  7351. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  7352. }
  7353. /**
  7354. * i40e_get_rss - Get RSS keys and lut
  7355. * @vsi: Pointer to VSI structure
  7356. * @seed: Buffer to store the keys
  7357. * @lut: Buffer to store the lookup table entries
  7358. * lut_size: Size of buffer to store the lookup table entries
  7359. *
  7360. * Returns 0 on success, negative on failure
  7361. */
  7362. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7363. {
  7364. struct i40e_pf *pf = vsi->back;
  7365. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7366. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  7367. else
  7368. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  7369. }
  7370. /**
  7371. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  7372. * @pf: Pointer to board private structure
  7373. * @lut: Lookup table
  7374. * @rss_table_size: Lookup table size
  7375. * @rss_size: Range of queue number for hashing
  7376. */
  7377. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  7378. u16 rss_table_size, u16 rss_size)
  7379. {
  7380. u16 i;
  7381. for (i = 0; i < rss_table_size; i++)
  7382. lut[i] = i % rss_size;
  7383. }
  7384. /**
  7385. * i40e_pf_config_rss - Prepare for RSS if used
  7386. * @pf: board private structure
  7387. **/
  7388. static int i40e_pf_config_rss(struct i40e_pf *pf)
  7389. {
  7390. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7391. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7392. u8 *lut;
  7393. struct i40e_hw *hw = &pf->hw;
  7394. u32 reg_val;
  7395. u64 hena;
  7396. int ret;
  7397. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  7398. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  7399. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  7400. hena |= i40e_pf_get_default_rss_hena(pf);
  7401. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  7402. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  7403. /* Determine the RSS table size based on the hardware capabilities */
  7404. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  7405. reg_val = (pf->rss_table_size == 512) ?
  7406. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  7407. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  7408. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  7409. /* Determine the RSS size of the VSI */
  7410. if (!vsi->rss_size) {
  7411. u16 qcount;
  7412. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7413. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7414. }
  7415. if (!vsi->rss_size)
  7416. return -EINVAL;
  7417. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7418. if (!lut)
  7419. return -ENOMEM;
  7420. /* Use user configured lut if there is one, otherwise use default */
  7421. if (vsi->rss_lut_user)
  7422. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7423. else
  7424. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7425. /* Use user configured hash key if there is one, otherwise
  7426. * use default.
  7427. */
  7428. if (vsi->rss_hkey_user)
  7429. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7430. else
  7431. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7432. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  7433. kfree(lut);
  7434. return ret;
  7435. }
  7436. /**
  7437. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  7438. * @pf: board private structure
  7439. * @queue_count: the requested queue count for rss.
  7440. *
  7441. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  7442. * count which may be different from the requested queue count.
  7443. * Note: expects to be called while under rtnl_lock()
  7444. **/
  7445. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  7446. {
  7447. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7448. int new_rss_size;
  7449. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  7450. return 0;
  7451. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  7452. if (queue_count != vsi->num_queue_pairs) {
  7453. u16 qcount;
  7454. vsi->req_queue_pairs = queue_count;
  7455. i40e_prep_for_reset(pf, true);
  7456. pf->alloc_rss_size = new_rss_size;
  7457. i40e_reset_and_rebuild(pf, true, true);
  7458. /* Discard the user configured hash keys and lut, if less
  7459. * queues are enabled.
  7460. */
  7461. if (queue_count < vsi->rss_size) {
  7462. i40e_clear_rss_config_user(vsi);
  7463. dev_dbg(&pf->pdev->dev,
  7464. "discard user configured hash keys and lut\n");
  7465. }
  7466. /* Reset vsi->rss_size, as number of enabled queues changed */
  7467. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7468. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7469. i40e_pf_config_rss(pf);
  7470. }
  7471. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  7472. vsi->req_queue_pairs, pf->rss_size_max);
  7473. return pf->alloc_rss_size;
  7474. }
  7475. /**
  7476. * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
  7477. * @pf: board private structure
  7478. **/
  7479. i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
  7480. {
  7481. i40e_status status;
  7482. bool min_valid, max_valid;
  7483. u32 max_bw, min_bw;
  7484. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  7485. &min_valid, &max_valid);
  7486. if (!status) {
  7487. if (min_valid)
  7488. pf->npar_min_bw = min_bw;
  7489. if (max_valid)
  7490. pf->npar_max_bw = max_bw;
  7491. }
  7492. return status;
  7493. }
  7494. /**
  7495. * i40e_set_npar_bw_setting - Set BW settings for this PF partition
  7496. * @pf: board private structure
  7497. **/
  7498. i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
  7499. {
  7500. struct i40e_aqc_configure_partition_bw_data bw_data;
  7501. i40e_status status;
  7502. /* Set the valid bit for this PF */
  7503. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  7504. bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
  7505. bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
  7506. /* Set the new bandwidths */
  7507. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  7508. return status;
  7509. }
  7510. /**
  7511. * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
  7512. * @pf: board private structure
  7513. **/
  7514. i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
  7515. {
  7516. /* Commit temporary BW setting to permanent NVM image */
  7517. enum i40e_admin_queue_err last_aq_status;
  7518. i40e_status ret;
  7519. u16 nvm_word;
  7520. if (pf->hw.partition_id != 1) {
  7521. dev_info(&pf->pdev->dev,
  7522. "Commit BW only works on partition 1! This is partition %d",
  7523. pf->hw.partition_id);
  7524. ret = I40E_NOT_SUPPORTED;
  7525. goto bw_commit_out;
  7526. }
  7527. /* Acquire NVM for read access */
  7528. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  7529. last_aq_status = pf->hw.aq.asq_last_status;
  7530. if (ret) {
  7531. dev_info(&pf->pdev->dev,
  7532. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  7533. i40e_stat_str(&pf->hw, ret),
  7534. i40e_aq_str(&pf->hw, last_aq_status));
  7535. goto bw_commit_out;
  7536. }
  7537. /* Read word 0x10 of NVM - SW compatibility word 1 */
  7538. ret = i40e_aq_read_nvm(&pf->hw,
  7539. I40E_SR_NVM_CONTROL_WORD,
  7540. 0x10, sizeof(nvm_word), &nvm_word,
  7541. false, NULL);
  7542. /* Save off last admin queue command status before releasing
  7543. * the NVM
  7544. */
  7545. last_aq_status = pf->hw.aq.asq_last_status;
  7546. i40e_release_nvm(&pf->hw);
  7547. if (ret) {
  7548. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  7549. i40e_stat_str(&pf->hw, ret),
  7550. i40e_aq_str(&pf->hw, last_aq_status));
  7551. goto bw_commit_out;
  7552. }
  7553. /* Wait a bit for NVM release to complete */
  7554. msleep(50);
  7555. /* Acquire NVM for write access */
  7556. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  7557. last_aq_status = pf->hw.aq.asq_last_status;
  7558. if (ret) {
  7559. dev_info(&pf->pdev->dev,
  7560. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  7561. i40e_stat_str(&pf->hw, ret),
  7562. i40e_aq_str(&pf->hw, last_aq_status));
  7563. goto bw_commit_out;
  7564. }
  7565. /* Write it back out unchanged to initiate update NVM,
  7566. * which will force a write of the shadow (alt) RAM to
  7567. * the NVM - thus storing the bandwidth values permanently.
  7568. */
  7569. ret = i40e_aq_update_nvm(&pf->hw,
  7570. I40E_SR_NVM_CONTROL_WORD,
  7571. 0x10, sizeof(nvm_word),
  7572. &nvm_word, true, NULL);
  7573. /* Save off last admin queue command status before releasing
  7574. * the NVM
  7575. */
  7576. last_aq_status = pf->hw.aq.asq_last_status;
  7577. i40e_release_nvm(&pf->hw);
  7578. if (ret)
  7579. dev_info(&pf->pdev->dev,
  7580. "BW settings NOT SAVED, err %s aq_err %s\n",
  7581. i40e_stat_str(&pf->hw, ret),
  7582. i40e_aq_str(&pf->hw, last_aq_status));
  7583. bw_commit_out:
  7584. return ret;
  7585. }
  7586. /**
  7587. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  7588. * @pf: board private structure to initialize
  7589. *
  7590. * i40e_sw_init initializes the Adapter private data structure.
  7591. * Fields are initialized based on PCI device information and
  7592. * OS network device settings (MTU size).
  7593. **/
  7594. static int i40e_sw_init(struct i40e_pf *pf)
  7595. {
  7596. int err = 0;
  7597. int size;
  7598. /* Set default capability flags */
  7599. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  7600. I40E_FLAG_MSI_ENABLED |
  7601. I40E_FLAG_MSIX_ENABLED;
  7602. /* Set default ITR */
  7603. pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
  7604. pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
  7605. /* Depending on PF configurations, it is possible that the RSS
  7606. * maximum might end up larger than the available queues
  7607. */
  7608. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  7609. pf->alloc_rss_size = 1;
  7610. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  7611. pf->rss_size_max = min_t(int, pf->rss_size_max,
  7612. pf->hw.func_caps.num_tx_qp);
  7613. if (pf->hw.func_caps.rss) {
  7614. pf->flags |= I40E_FLAG_RSS_ENABLED;
  7615. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  7616. num_online_cpus());
  7617. }
  7618. /* MFP mode enabled */
  7619. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  7620. pf->flags |= I40E_FLAG_MFP_ENABLED;
  7621. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  7622. if (i40e_get_npar_bw_setting(pf))
  7623. dev_warn(&pf->pdev->dev,
  7624. "Could not get NPAR bw settings\n");
  7625. else
  7626. dev_info(&pf->pdev->dev,
  7627. "Min BW = %8.8x, Max BW = %8.8x\n",
  7628. pf->npar_min_bw, pf->npar_max_bw);
  7629. }
  7630. /* FW/NVM is not yet fixed in this regard */
  7631. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  7632. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  7633. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  7634. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  7635. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  7636. pf->hw.num_partitions > 1)
  7637. dev_info(&pf->pdev->dev,
  7638. "Flow Director Sideband mode Disabled in MFP mode\n");
  7639. else
  7640. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7641. pf->fdir_pf_filter_count =
  7642. pf->hw.func_caps.fd_filters_guaranteed;
  7643. pf->hw.fdir_shared_filter_count =
  7644. pf->hw.func_caps.fd_filters_best_effort;
  7645. }
  7646. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7647. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  7648. (pf->hw.aq.fw_maj_ver < 4))) {
  7649. pf->flags |= I40E_FLAG_RESTART_AUTONEG;
  7650. /* No DCB support for FW < v4.33 */
  7651. pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
  7652. }
  7653. /* Disable FW LLDP if FW < v4.3 */
  7654. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7655. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  7656. (pf->hw.aq.fw_maj_ver < 4)))
  7657. pf->flags |= I40E_FLAG_STOP_FW_LLDP;
  7658. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  7659. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7660. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  7661. (pf->hw.aq.fw_maj_ver >= 5)))
  7662. pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
  7663. if (pf->hw.func_caps.vmdq) {
  7664. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  7665. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  7666. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  7667. }
  7668. if (pf->hw.func_caps.iwarp) {
  7669. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  7670. /* IWARP needs one extra vector for CQP just like MISC.*/
  7671. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  7672. }
  7673. #ifdef CONFIG_PCI_IOV
  7674. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  7675. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  7676. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  7677. pf->num_req_vfs = min_t(int,
  7678. pf->hw.func_caps.num_vfs,
  7679. I40E_MAX_VF_COUNT);
  7680. }
  7681. #endif /* CONFIG_PCI_IOV */
  7682. if (pf->hw.mac.type == I40E_MAC_X722) {
  7683. pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
  7684. | I40E_FLAG_128_QP_RSS_CAPABLE
  7685. | I40E_FLAG_HW_ATR_EVICT_CAPABLE
  7686. | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
  7687. | I40E_FLAG_WB_ON_ITR_CAPABLE
  7688. | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
  7689. | I40E_FLAG_NO_PCI_LINK_CHECK
  7690. | I40E_FLAG_USE_SET_LLDP_MIB
  7691. | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
  7692. | I40E_FLAG_PTP_L4_CAPABLE
  7693. | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
  7694. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  7695. ((pf->hw.aq.api_maj_ver == 1) &&
  7696. (pf->hw.aq.api_min_ver > 4))) {
  7697. /* Supported in FW API version higher than 1.4 */
  7698. pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
  7699. pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7700. } else {
  7701. pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7702. }
  7703. pf->eeprom_version = 0xDEAD;
  7704. pf->lan_veb = I40E_NO_VEB;
  7705. pf->lan_vsi = I40E_NO_VSI;
  7706. /* By default FW has this off for performance reasons */
  7707. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  7708. /* set up queue assignment tracking */
  7709. size = sizeof(struct i40e_lump_tracking)
  7710. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  7711. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  7712. if (!pf->qp_pile) {
  7713. err = -ENOMEM;
  7714. goto sw_init_done;
  7715. }
  7716. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  7717. pf->qp_pile->search_hint = 0;
  7718. pf->tx_timeout_recovery_level = 1;
  7719. mutex_init(&pf->switch_mutex);
  7720. /* If NPAR is enabled nudge the Tx scheduler */
  7721. if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
  7722. i40e_set_npar_bw_setting(pf);
  7723. sw_init_done:
  7724. return err;
  7725. }
  7726. /**
  7727. * i40e_set_ntuple - set the ntuple feature flag and take action
  7728. * @pf: board private structure to initialize
  7729. * @features: the feature set that the stack is suggesting
  7730. *
  7731. * returns a bool to indicate if reset needs to happen
  7732. **/
  7733. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  7734. {
  7735. bool need_reset = false;
  7736. /* Check if Flow Director n-tuple support was enabled or disabled. If
  7737. * the state changed, we need to reset.
  7738. */
  7739. if (features & NETIF_F_NTUPLE) {
  7740. /* Enable filters and mark for reset */
  7741. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7742. need_reset = true;
  7743. /* enable FD_SB only if there is MSI-X vector */
  7744. if (pf->num_fdsb_msix > 0)
  7745. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7746. } else {
  7747. /* turn off filters, mark for reset and clear SW filter list */
  7748. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7749. need_reset = true;
  7750. i40e_fdir_filter_exit(pf);
  7751. }
  7752. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7753. pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7754. /* reset fd counters */
  7755. pf->fd_add_err = 0;
  7756. pf->fd_atr_cnt = 0;
  7757. /* if ATR was auto disabled it can be re-enabled. */
  7758. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7759. (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
  7760. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  7761. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7762. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  7763. }
  7764. }
  7765. return need_reset;
  7766. }
  7767. /**
  7768. * i40e_clear_rss_lut - clear the rx hash lookup table
  7769. * @vsi: the VSI being configured
  7770. **/
  7771. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  7772. {
  7773. struct i40e_pf *pf = vsi->back;
  7774. struct i40e_hw *hw = &pf->hw;
  7775. u16 vf_id = vsi->vf_id;
  7776. u8 i;
  7777. if (vsi->type == I40E_VSI_MAIN) {
  7778. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7779. wr32(hw, I40E_PFQF_HLUT(i), 0);
  7780. } else if (vsi->type == I40E_VSI_SRIOV) {
  7781. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7782. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  7783. } else {
  7784. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7785. }
  7786. }
  7787. /**
  7788. * i40e_set_features - set the netdev feature flags
  7789. * @netdev: ptr to the netdev being adjusted
  7790. * @features: the feature set that the stack is suggesting
  7791. * Note: expects to be called while under rtnl_lock()
  7792. **/
  7793. static int i40e_set_features(struct net_device *netdev,
  7794. netdev_features_t features)
  7795. {
  7796. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7797. struct i40e_vsi *vsi = np->vsi;
  7798. struct i40e_pf *pf = vsi->back;
  7799. bool need_reset;
  7800. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  7801. i40e_pf_config_rss(pf);
  7802. else if (!(features & NETIF_F_RXHASH) &&
  7803. netdev->features & NETIF_F_RXHASH)
  7804. i40e_clear_rss_lut(vsi);
  7805. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  7806. i40e_vlan_stripping_enable(vsi);
  7807. else
  7808. i40e_vlan_stripping_disable(vsi);
  7809. need_reset = i40e_set_ntuple(pf, features);
  7810. if (need_reset)
  7811. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
  7812. return 0;
  7813. }
  7814. /**
  7815. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  7816. * @pf: board private structure
  7817. * @port: The UDP port to look up
  7818. *
  7819. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  7820. **/
  7821. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  7822. {
  7823. u8 i;
  7824. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  7825. if (pf->udp_ports[i].index == port)
  7826. return i;
  7827. }
  7828. return i;
  7829. }
  7830. /**
  7831. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  7832. * @netdev: This physical port's netdev
  7833. * @ti: Tunnel endpoint information
  7834. **/
  7835. static void i40e_udp_tunnel_add(struct net_device *netdev,
  7836. struct udp_tunnel_info *ti)
  7837. {
  7838. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7839. struct i40e_vsi *vsi = np->vsi;
  7840. struct i40e_pf *pf = vsi->back;
  7841. u16 port = ntohs(ti->port);
  7842. u8 next_idx;
  7843. u8 idx;
  7844. idx = i40e_get_udp_port_idx(pf, port);
  7845. /* Check if port already exists */
  7846. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7847. netdev_info(netdev, "port %d already offloaded\n", port);
  7848. return;
  7849. }
  7850. /* Now check if there is space to add the new port */
  7851. next_idx = i40e_get_udp_port_idx(pf, 0);
  7852. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7853. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  7854. port);
  7855. return;
  7856. }
  7857. switch (ti->type) {
  7858. case UDP_TUNNEL_TYPE_VXLAN:
  7859. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  7860. break;
  7861. case UDP_TUNNEL_TYPE_GENEVE:
  7862. if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
  7863. return;
  7864. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  7865. break;
  7866. default:
  7867. return;
  7868. }
  7869. /* New port: add it and mark its index in the bitmap */
  7870. pf->udp_ports[next_idx].index = port;
  7871. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  7872. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7873. }
  7874. /**
  7875. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  7876. * @netdev: This physical port's netdev
  7877. * @ti: Tunnel endpoint information
  7878. **/
  7879. static void i40e_udp_tunnel_del(struct net_device *netdev,
  7880. struct udp_tunnel_info *ti)
  7881. {
  7882. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7883. struct i40e_vsi *vsi = np->vsi;
  7884. struct i40e_pf *pf = vsi->back;
  7885. u16 port = ntohs(ti->port);
  7886. u8 idx;
  7887. idx = i40e_get_udp_port_idx(pf, port);
  7888. /* Check if port already exists */
  7889. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  7890. goto not_found;
  7891. switch (ti->type) {
  7892. case UDP_TUNNEL_TYPE_VXLAN:
  7893. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  7894. goto not_found;
  7895. break;
  7896. case UDP_TUNNEL_TYPE_GENEVE:
  7897. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  7898. goto not_found;
  7899. break;
  7900. default:
  7901. goto not_found;
  7902. }
  7903. /* if port exists, set it to 0 (mark for deletion)
  7904. * and make it pending
  7905. */
  7906. pf->udp_ports[idx].index = 0;
  7907. pf->pending_udp_bitmap |= BIT_ULL(idx);
  7908. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7909. return;
  7910. not_found:
  7911. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  7912. port);
  7913. }
  7914. static int i40e_get_phys_port_id(struct net_device *netdev,
  7915. struct netdev_phys_item_id *ppid)
  7916. {
  7917. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7918. struct i40e_pf *pf = np->vsi->back;
  7919. struct i40e_hw *hw = &pf->hw;
  7920. if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
  7921. return -EOPNOTSUPP;
  7922. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  7923. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  7924. return 0;
  7925. }
  7926. /**
  7927. * i40e_ndo_fdb_add - add an entry to the hardware database
  7928. * @ndm: the input from the stack
  7929. * @tb: pointer to array of nladdr (unused)
  7930. * @dev: the net device pointer
  7931. * @addr: the MAC address entry being added
  7932. * @flags: instructions from stack about fdb operation
  7933. */
  7934. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  7935. struct net_device *dev,
  7936. const unsigned char *addr, u16 vid,
  7937. u16 flags)
  7938. {
  7939. struct i40e_netdev_priv *np = netdev_priv(dev);
  7940. struct i40e_pf *pf = np->vsi->back;
  7941. int err = 0;
  7942. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  7943. return -EOPNOTSUPP;
  7944. if (vid) {
  7945. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  7946. return -EINVAL;
  7947. }
  7948. /* Hardware does not support aging addresses so if a
  7949. * ndm_state is given only allow permanent addresses
  7950. */
  7951. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  7952. netdev_info(dev, "FDB only supports static addresses\n");
  7953. return -EINVAL;
  7954. }
  7955. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  7956. err = dev_uc_add_excl(dev, addr);
  7957. else if (is_multicast_ether_addr(addr))
  7958. err = dev_mc_add_excl(dev, addr);
  7959. else
  7960. err = -EINVAL;
  7961. /* Only return duplicate errors if NLM_F_EXCL is set */
  7962. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  7963. err = 0;
  7964. return err;
  7965. }
  7966. /**
  7967. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  7968. * @dev: the netdev being configured
  7969. * @nlh: RTNL message
  7970. *
  7971. * Inserts a new hardware bridge if not already created and
  7972. * enables the bridging mode requested (VEB or VEPA). If the
  7973. * hardware bridge has already been inserted and the request
  7974. * is to change the mode then that requires a PF reset to
  7975. * allow rebuild of the components with required hardware
  7976. * bridge mode enabled.
  7977. *
  7978. * Note: expects to be called while under rtnl_lock()
  7979. **/
  7980. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  7981. struct nlmsghdr *nlh,
  7982. u16 flags)
  7983. {
  7984. struct i40e_netdev_priv *np = netdev_priv(dev);
  7985. struct i40e_vsi *vsi = np->vsi;
  7986. struct i40e_pf *pf = vsi->back;
  7987. struct i40e_veb *veb = NULL;
  7988. struct nlattr *attr, *br_spec;
  7989. int i, rem;
  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 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. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  7999. nla_for_each_nested(attr, br_spec, rem) {
  8000. __u16 mode;
  8001. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  8002. continue;
  8003. mode = nla_get_u16(attr);
  8004. if ((mode != BRIDGE_MODE_VEPA) &&
  8005. (mode != BRIDGE_MODE_VEB))
  8006. return -EINVAL;
  8007. /* Insert a new HW bridge */
  8008. if (!veb) {
  8009. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8010. vsi->tc_config.enabled_tc);
  8011. if (veb) {
  8012. veb->bridge_mode = mode;
  8013. i40e_config_bridge_mode(veb);
  8014. } else {
  8015. /* No Bridge HW offload available */
  8016. return -ENOENT;
  8017. }
  8018. break;
  8019. } else if (mode != veb->bridge_mode) {
  8020. /* Existing HW bridge but different mode needs reset */
  8021. veb->bridge_mode = mode;
  8022. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  8023. if (mode == BRIDGE_MODE_VEB)
  8024. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  8025. else
  8026. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8027. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
  8028. true);
  8029. break;
  8030. }
  8031. }
  8032. return 0;
  8033. }
  8034. /**
  8035. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  8036. * @skb: skb buff
  8037. * @pid: process id
  8038. * @seq: RTNL message seq #
  8039. * @dev: the netdev being configured
  8040. * @filter_mask: unused
  8041. * @nlflags: netlink flags passed in
  8042. *
  8043. * Return the mode in which the hardware bridge is operating in
  8044. * i.e VEB or VEPA.
  8045. **/
  8046. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  8047. struct net_device *dev,
  8048. u32 __always_unused filter_mask,
  8049. int nlflags)
  8050. {
  8051. struct i40e_netdev_priv *np = netdev_priv(dev);
  8052. struct i40e_vsi *vsi = np->vsi;
  8053. struct i40e_pf *pf = vsi->back;
  8054. struct i40e_veb *veb = NULL;
  8055. int i;
  8056. /* Only for PF VSI for now */
  8057. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  8058. return -EOPNOTSUPP;
  8059. /* Find the HW bridge for the PF VSI */
  8060. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8061. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8062. veb = pf->veb[i];
  8063. }
  8064. if (!veb)
  8065. return 0;
  8066. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  8067. 0, 0, nlflags, filter_mask, NULL);
  8068. }
  8069. /**
  8070. * i40e_features_check - Validate encapsulated packet conforms to limits
  8071. * @skb: skb buff
  8072. * @dev: This physical port's netdev
  8073. * @features: Offload features that the stack believes apply
  8074. **/
  8075. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  8076. struct net_device *dev,
  8077. netdev_features_t features)
  8078. {
  8079. size_t len;
  8080. /* No point in doing any of this if neither checksum nor GSO are
  8081. * being requested for this frame. We can rule out both by just
  8082. * checking for CHECKSUM_PARTIAL
  8083. */
  8084. if (skb->ip_summed != CHECKSUM_PARTIAL)
  8085. return features;
  8086. /* We cannot support GSO if the MSS is going to be less than
  8087. * 64 bytes. If it is then we need to drop support for GSO.
  8088. */
  8089. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  8090. features &= ~NETIF_F_GSO_MASK;
  8091. /* MACLEN can support at most 63 words */
  8092. len = skb_network_header(skb) - skb->data;
  8093. if (len & ~(63 * 2))
  8094. goto out_err;
  8095. /* IPLEN and EIPLEN can support at most 127 dwords */
  8096. len = skb_transport_header(skb) - skb_network_header(skb);
  8097. if (len & ~(127 * 4))
  8098. goto out_err;
  8099. if (skb->encapsulation) {
  8100. /* L4TUNLEN can support 127 words */
  8101. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  8102. if (len & ~(127 * 2))
  8103. goto out_err;
  8104. /* IPLEN can support at most 127 dwords */
  8105. len = skb_inner_transport_header(skb) -
  8106. skb_inner_network_header(skb);
  8107. if (len & ~(127 * 4))
  8108. goto out_err;
  8109. }
  8110. /* No need to validate L4LEN as TCP is the only protocol with a
  8111. * a flexible value and we support all possible values supported
  8112. * by TCP, which is at most 15 dwords
  8113. */
  8114. return features;
  8115. out_err:
  8116. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  8117. }
  8118. static const struct net_device_ops i40e_netdev_ops = {
  8119. .ndo_open = i40e_open,
  8120. .ndo_stop = i40e_close,
  8121. .ndo_start_xmit = i40e_lan_xmit_frame,
  8122. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  8123. .ndo_set_rx_mode = i40e_set_rx_mode,
  8124. .ndo_validate_addr = eth_validate_addr,
  8125. .ndo_set_mac_address = i40e_set_mac,
  8126. .ndo_change_mtu = i40e_change_mtu,
  8127. .ndo_do_ioctl = i40e_ioctl,
  8128. .ndo_tx_timeout = i40e_tx_timeout,
  8129. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  8130. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  8131. #ifdef CONFIG_NET_POLL_CONTROLLER
  8132. .ndo_poll_controller = i40e_netpoll,
  8133. #endif
  8134. .ndo_setup_tc = __i40e_setup_tc,
  8135. .ndo_set_features = i40e_set_features,
  8136. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  8137. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  8138. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  8139. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  8140. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  8141. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  8142. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  8143. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  8144. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  8145. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  8146. .ndo_fdb_add = i40e_ndo_fdb_add,
  8147. .ndo_features_check = i40e_features_check,
  8148. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  8149. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  8150. };
  8151. /**
  8152. * i40e_config_netdev - Setup the netdev flags
  8153. * @vsi: the VSI being configured
  8154. *
  8155. * Returns 0 on success, negative value on failure
  8156. **/
  8157. static int i40e_config_netdev(struct i40e_vsi *vsi)
  8158. {
  8159. struct i40e_pf *pf = vsi->back;
  8160. struct i40e_hw *hw = &pf->hw;
  8161. struct i40e_netdev_priv *np;
  8162. struct net_device *netdev;
  8163. u8 broadcast[ETH_ALEN];
  8164. u8 mac_addr[ETH_ALEN];
  8165. int etherdev_size;
  8166. netdev_features_t hw_enc_features;
  8167. netdev_features_t hw_features;
  8168. etherdev_size = sizeof(struct i40e_netdev_priv);
  8169. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  8170. if (!netdev)
  8171. return -ENOMEM;
  8172. vsi->netdev = netdev;
  8173. np = netdev_priv(netdev);
  8174. np->vsi = vsi;
  8175. hw_enc_features = NETIF_F_SG |
  8176. NETIF_F_IP_CSUM |
  8177. NETIF_F_IPV6_CSUM |
  8178. NETIF_F_HIGHDMA |
  8179. NETIF_F_SOFT_FEATURES |
  8180. NETIF_F_TSO |
  8181. NETIF_F_TSO_ECN |
  8182. NETIF_F_TSO6 |
  8183. NETIF_F_GSO_GRE |
  8184. NETIF_F_GSO_GRE_CSUM |
  8185. NETIF_F_GSO_PARTIAL |
  8186. NETIF_F_GSO_UDP_TUNNEL |
  8187. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  8188. NETIF_F_SCTP_CRC |
  8189. NETIF_F_RXHASH |
  8190. NETIF_F_RXCSUM |
  8191. 0;
  8192. if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
  8193. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  8194. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  8195. netdev->hw_enc_features |= hw_enc_features;
  8196. /* record features VLANs can make use of */
  8197. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  8198. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  8199. netdev->hw_features |= NETIF_F_NTUPLE;
  8200. hw_features = hw_enc_features |
  8201. NETIF_F_HW_VLAN_CTAG_TX |
  8202. NETIF_F_HW_VLAN_CTAG_RX;
  8203. netdev->hw_features |= hw_features;
  8204. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  8205. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  8206. if (vsi->type == I40E_VSI_MAIN) {
  8207. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  8208. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  8209. /* The following steps are necessary for two reasons. First,
  8210. * some older NVM configurations load a default MAC-VLAN
  8211. * filter that will accept any tagged packet, and we want to
  8212. * replace this with a normal filter. Additionally, it is
  8213. * possible our MAC address was provided by the platform using
  8214. * Open Firmware or similar.
  8215. *
  8216. * Thus, we need to remove the default filter and install one
  8217. * specific to the MAC address.
  8218. */
  8219. i40e_rm_default_mac_filter(vsi, mac_addr);
  8220. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8221. i40e_add_mac_filter(vsi, mac_addr);
  8222. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8223. } else {
  8224. /* relate the VSI_VMDQ name to the VSI_MAIN name */
  8225. snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
  8226. pf->vsi[pf->lan_vsi]->netdev->name);
  8227. random_ether_addr(mac_addr);
  8228. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8229. i40e_add_mac_filter(vsi, mac_addr);
  8230. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8231. }
  8232. /* Add the broadcast filter so that we initially will receive
  8233. * broadcast packets. Note that when a new VLAN is first added the
  8234. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  8235. * specific filters as part of transitioning into "vlan" operation.
  8236. * When more VLANs are added, the driver will copy each existing MAC
  8237. * filter and add it for the new VLAN.
  8238. *
  8239. * Broadcast filters are handled specially by
  8240. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  8241. * promiscuous bit instead of adding this directly as a MAC/VLAN
  8242. * filter. The subtask will update the correct broadcast promiscuous
  8243. * bits as VLANs become active or inactive.
  8244. */
  8245. eth_broadcast_addr(broadcast);
  8246. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8247. i40e_add_mac_filter(vsi, broadcast);
  8248. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8249. ether_addr_copy(netdev->dev_addr, mac_addr);
  8250. ether_addr_copy(netdev->perm_addr, mac_addr);
  8251. netdev->priv_flags |= IFF_UNICAST_FLT;
  8252. netdev->priv_flags |= IFF_SUPP_NOFCS;
  8253. /* Setup netdev TC information */
  8254. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  8255. netdev->netdev_ops = &i40e_netdev_ops;
  8256. netdev->watchdog_timeo = 5 * HZ;
  8257. i40e_set_ethtool_ops(netdev);
  8258. /* MTU range: 68 - 9706 */
  8259. netdev->min_mtu = ETH_MIN_MTU;
  8260. netdev->max_mtu = I40E_MAX_RXBUFFER -
  8261. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  8262. return 0;
  8263. }
  8264. /**
  8265. * i40e_vsi_delete - Delete a VSI from the switch
  8266. * @vsi: the VSI being removed
  8267. *
  8268. * Returns 0 on success, negative value on failure
  8269. **/
  8270. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  8271. {
  8272. /* remove default VSI is not allowed */
  8273. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  8274. return;
  8275. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  8276. }
  8277. /**
  8278. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  8279. * @vsi: the VSI being queried
  8280. *
  8281. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  8282. **/
  8283. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  8284. {
  8285. struct i40e_veb *veb;
  8286. struct i40e_pf *pf = vsi->back;
  8287. /* Uplink is not a bridge so default to VEB */
  8288. if (vsi->veb_idx == I40E_NO_VEB)
  8289. return 1;
  8290. veb = pf->veb[vsi->veb_idx];
  8291. if (!veb) {
  8292. dev_info(&pf->pdev->dev,
  8293. "There is no veb associated with the bridge\n");
  8294. return -ENOENT;
  8295. }
  8296. /* Uplink is a bridge in VEPA mode */
  8297. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  8298. return 0;
  8299. } else {
  8300. /* Uplink is a bridge in VEB mode */
  8301. return 1;
  8302. }
  8303. /* VEPA is now default bridge, so return 0 */
  8304. return 0;
  8305. }
  8306. /**
  8307. * i40e_add_vsi - Add a VSI to the switch
  8308. * @vsi: the VSI being configured
  8309. *
  8310. * This initializes a VSI context depending on the VSI type to be added and
  8311. * passes it down to the add_vsi aq command.
  8312. **/
  8313. static int i40e_add_vsi(struct i40e_vsi *vsi)
  8314. {
  8315. int ret = -ENODEV;
  8316. struct i40e_pf *pf = vsi->back;
  8317. struct i40e_hw *hw = &pf->hw;
  8318. struct i40e_vsi_context ctxt;
  8319. struct i40e_mac_filter *f;
  8320. struct hlist_node *h;
  8321. int bkt;
  8322. u8 enabled_tc = 0x1; /* TC0 enabled */
  8323. int f_count = 0;
  8324. memset(&ctxt, 0, sizeof(ctxt));
  8325. switch (vsi->type) {
  8326. case I40E_VSI_MAIN:
  8327. /* The PF's main VSI is already setup as part of the
  8328. * device initialization, so we'll not bother with
  8329. * the add_vsi call, but we will retrieve the current
  8330. * VSI context.
  8331. */
  8332. ctxt.seid = pf->main_vsi_seid;
  8333. ctxt.pf_num = pf->hw.pf_id;
  8334. ctxt.vf_num = 0;
  8335. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  8336. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8337. if (ret) {
  8338. dev_info(&pf->pdev->dev,
  8339. "couldn't get PF vsi config, err %s aq_err %s\n",
  8340. i40e_stat_str(&pf->hw, ret),
  8341. i40e_aq_str(&pf->hw,
  8342. pf->hw.aq.asq_last_status));
  8343. return -ENOENT;
  8344. }
  8345. vsi->info = ctxt.info;
  8346. vsi->info.valid_sections = 0;
  8347. vsi->seid = ctxt.seid;
  8348. vsi->id = ctxt.vsi_number;
  8349. enabled_tc = i40e_pf_get_tc_map(pf);
  8350. /* MFP mode setup queue map and update VSI */
  8351. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  8352. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  8353. memset(&ctxt, 0, sizeof(ctxt));
  8354. ctxt.seid = pf->main_vsi_seid;
  8355. ctxt.pf_num = pf->hw.pf_id;
  8356. ctxt.vf_num = 0;
  8357. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  8358. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  8359. if (ret) {
  8360. dev_info(&pf->pdev->dev,
  8361. "update vsi failed, err %s aq_err %s\n",
  8362. i40e_stat_str(&pf->hw, ret),
  8363. i40e_aq_str(&pf->hw,
  8364. pf->hw.aq.asq_last_status));
  8365. ret = -ENOENT;
  8366. goto err;
  8367. }
  8368. /* update the local VSI info queue map */
  8369. i40e_vsi_update_queue_map(vsi, &ctxt);
  8370. vsi->info.valid_sections = 0;
  8371. } else {
  8372. /* Default/Main VSI is only enabled for TC0
  8373. * reconfigure it to enable all TCs that are
  8374. * available on the port in SFP mode.
  8375. * For MFP case the iSCSI PF would use this
  8376. * flow to enable LAN+iSCSI TC.
  8377. */
  8378. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  8379. if (ret) {
  8380. dev_info(&pf->pdev->dev,
  8381. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  8382. enabled_tc,
  8383. i40e_stat_str(&pf->hw, ret),
  8384. i40e_aq_str(&pf->hw,
  8385. pf->hw.aq.asq_last_status));
  8386. ret = -ENOENT;
  8387. }
  8388. }
  8389. break;
  8390. case I40E_VSI_FDIR:
  8391. ctxt.pf_num = hw->pf_id;
  8392. ctxt.vf_num = 0;
  8393. ctxt.uplink_seid = vsi->uplink_seid;
  8394. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8395. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8396. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  8397. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  8398. ctxt.info.valid_sections |=
  8399. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8400. ctxt.info.switch_id =
  8401. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8402. }
  8403. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8404. break;
  8405. case I40E_VSI_VMDQ2:
  8406. ctxt.pf_num = hw->pf_id;
  8407. ctxt.vf_num = 0;
  8408. ctxt.uplink_seid = vsi->uplink_seid;
  8409. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8410. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  8411. /* This VSI is connected to VEB so the switch_id
  8412. * should be set to zero by default.
  8413. */
  8414. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8415. ctxt.info.valid_sections |=
  8416. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8417. ctxt.info.switch_id =
  8418. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8419. }
  8420. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8421. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8422. break;
  8423. case I40E_VSI_SRIOV:
  8424. ctxt.pf_num = hw->pf_id;
  8425. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  8426. ctxt.uplink_seid = vsi->uplink_seid;
  8427. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8428. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  8429. /* This VSI is connected to VEB so the switch_id
  8430. * should be set to zero by default.
  8431. */
  8432. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8433. ctxt.info.valid_sections |=
  8434. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8435. ctxt.info.switch_id =
  8436. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8437. }
  8438. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  8439. ctxt.info.valid_sections |=
  8440. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  8441. ctxt.info.queueing_opt_flags |=
  8442. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  8443. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  8444. }
  8445. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  8446. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  8447. if (pf->vf[vsi->vf_id].spoofchk) {
  8448. ctxt.info.valid_sections |=
  8449. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  8450. ctxt.info.sec_flags |=
  8451. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  8452. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  8453. }
  8454. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8455. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8456. break;
  8457. case I40E_VSI_IWARP:
  8458. /* send down message to iWARP */
  8459. break;
  8460. default:
  8461. return -ENODEV;
  8462. }
  8463. if (vsi->type != I40E_VSI_MAIN) {
  8464. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  8465. if (ret) {
  8466. dev_info(&vsi->back->pdev->dev,
  8467. "add vsi failed, err %s aq_err %s\n",
  8468. i40e_stat_str(&pf->hw, ret),
  8469. i40e_aq_str(&pf->hw,
  8470. pf->hw.aq.asq_last_status));
  8471. ret = -ENOENT;
  8472. goto err;
  8473. }
  8474. vsi->info = ctxt.info;
  8475. vsi->info.valid_sections = 0;
  8476. vsi->seid = ctxt.seid;
  8477. vsi->id = ctxt.vsi_number;
  8478. }
  8479. vsi->active_filters = 0;
  8480. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  8481. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8482. /* If macvlan filters already exist, force them to get loaded */
  8483. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  8484. f->state = I40E_FILTER_NEW;
  8485. f_count++;
  8486. }
  8487. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8488. if (f_count) {
  8489. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  8490. pf->flags |= I40E_FLAG_FILTER_SYNC;
  8491. }
  8492. /* Update VSI BW information */
  8493. ret = i40e_vsi_get_bw_info(vsi);
  8494. if (ret) {
  8495. dev_info(&pf->pdev->dev,
  8496. "couldn't get vsi bw info, err %s aq_err %s\n",
  8497. i40e_stat_str(&pf->hw, ret),
  8498. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8499. /* VSI is already added so not tearing that up */
  8500. ret = 0;
  8501. }
  8502. err:
  8503. return ret;
  8504. }
  8505. /**
  8506. * i40e_vsi_release - Delete a VSI and free its resources
  8507. * @vsi: the VSI being removed
  8508. *
  8509. * Returns 0 on success or < 0 on error
  8510. **/
  8511. int i40e_vsi_release(struct i40e_vsi *vsi)
  8512. {
  8513. struct i40e_mac_filter *f;
  8514. struct hlist_node *h;
  8515. struct i40e_veb *veb = NULL;
  8516. struct i40e_pf *pf;
  8517. u16 uplink_seid;
  8518. int i, n, bkt;
  8519. pf = vsi->back;
  8520. /* release of a VEB-owner or last VSI is not allowed */
  8521. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  8522. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  8523. vsi->seid, vsi->uplink_seid);
  8524. return -ENODEV;
  8525. }
  8526. if (vsi == pf->vsi[pf->lan_vsi] &&
  8527. !test_bit(__I40E_DOWN, &pf->state)) {
  8528. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  8529. return -ENODEV;
  8530. }
  8531. uplink_seid = vsi->uplink_seid;
  8532. if (vsi->type != I40E_VSI_SRIOV) {
  8533. if (vsi->netdev_registered) {
  8534. vsi->netdev_registered = false;
  8535. if (vsi->netdev) {
  8536. /* results in a call to i40e_close() */
  8537. unregister_netdev(vsi->netdev);
  8538. }
  8539. } else {
  8540. i40e_vsi_close(vsi);
  8541. }
  8542. i40e_vsi_disable_irq(vsi);
  8543. }
  8544. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8545. /* clear the sync flag on all filters */
  8546. if (vsi->netdev) {
  8547. __dev_uc_unsync(vsi->netdev, NULL);
  8548. __dev_mc_unsync(vsi->netdev, NULL);
  8549. }
  8550. /* make sure any remaining filters are marked for deletion */
  8551. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  8552. __i40e_del_filter(vsi, f);
  8553. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8554. i40e_sync_vsi_filters(vsi);
  8555. i40e_vsi_delete(vsi);
  8556. i40e_vsi_free_q_vectors(vsi);
  8557. if (vsi->netdev) {
  8558. free_netdev(vsi->netdev);
  8559. vsi->netdev = NULL;
  8560. }
  8561. i40e_vsi_clear_rings(vsi);
  8562. i40e_vsi_clear(vsi);
  8563. /* If this was the last thing on the VEB, except for the
  8564. * controlling VSI, remove the VEB, which puts the controlling
  8565. * VSI onto the next level down in the switch.
  8566. *
  8567. * Well, okay, there's one more exception here: don't remove
  8568. * the orphan VEBs yet. We'll wait for an explicit remove request
  8569. * from up the network stack.
  8570. */
  8571. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  8572. if (pf->vsi[i] &&
  8573. pf->vsi[i]->uplink_seid == uplink_seid &&
  8574. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8575. n++; /* count the VSIs */
  8576. }
  8577. }
  8578. for (i = 0; i < I40E_MAX_VEB; i++) {
  8579. if (!pf->veb[i])
  8580. continue;
  8581. if (pf->veb[i]->uplink_seid == uplink_seid)
  8582. n++; /* count the VEBs */
  8583. if (pf->veb[i]->seid == uplink_seid)
  8584. veb = pf->veb[i];
  8585. }
  8586. if (n == 0 && veb && veb->uplink_seid != 0)
  8587. i40e_veb_release(veb);
  8588. return 0;
  8589. }
  8590. /**
  8591. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  8592. * @vsi: ptr to the VSI
  8593. *
  8594. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  8595. * corresponding SW VSI structure and initializes num_queue_pairs for the
  8596. * newly allocated VSI.
  8597. *
  8598. * Returns 0 on success or negative on failure
  8599. **/
  8600. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  8601. {
  8602. int ret = -ENOENT;
  8603. struct i40e_pf *pf = vsi->back;
  8604. if (vsi->q_vectors[0]) {
  8605. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  8606. vsi->seid);
  8607. return -EEXIST;
  8608. }
  8609. if (vsi->base_vector) {
  8610. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  8611. vsi->seid, vsi->base_vector);
  8612. return -EEXIST;
  8613. }
  8614. ret = i40e_vsi_alloc_q_vectors(vsi);
  8615. if (ret) {
  8616. dev_info(&pf->pdev->dev,
  8617. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  8618. vsi->num_q_vectors, vsi->seid, ret);
  8619. vsi->num_q_vectors = 0;
  8620. goto vector_setup_out;
  8621. }
  8622. /* In Legacy mode, we do not have to get any other vector since we
  8623. * piggyback on the misc/ICR0 for queue interrupts.
  8624. */
  8625. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8626. return ret;
  8627. if (vsi->num_q_vectors)
  8628. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  8629. vsi->num_q_vectors, vsi->idx);
  8630. if (vsi->base_vector < 0) {
  8631. dev_info(&pf->pdev->dev,
  8632. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  8633. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  8634. i40e_vsi_free_q_vectors(vsi);
  8635. ret = -ENOENT;
  8636. goto vector_setup_out;
  8637. }
  8638. vector_setup_out:
  8639. return ret;
  8640. }
  8641. /**
  8642. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  8643. * @vsi: pointer to the vsi.
  8644. *
  8645. * This re-allocates a vsi's queue resources.
  8646. *
  8647. * Returns pointer to the successfully allocated and configured VSI sw struct
  8648. * on success, otherwise returns NULL on failure.
  8649. **/
  8650. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  8651. {
  8652. struct i40e_pf *pf;
  8653. u8 enabled_tc;
  8654. int ret;
  8655. if (!vsi)
  8656. return NULL;
  8657. pf = vsi->back;
  8658. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8659. i40e_vsi_clear_rings(vsi);
  8660. i40e_vsi_free_arrays(vsi, false);
  8661. i40e_set_num_rings_in_vsi(vsi);
  8662. ret = i40e_vsi_alloc_arrays(vsi, false);
  8663. if (ret)
  8664. goto err_vsi;
  8665. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
  8666. if (ret < 0) {
  8667. dev_info(&pf->pdev->dev,
  8668. "failed to get tracking for %d queues for VSI %d err %d\n",
  8669. vsi->alloc_queue_pairs, vsi->seid, ret);
  8670. goto err_vsi;
  8671. }
  8672. vsi->base_queue = ret;
  8673. /* Update the FW view of the VSI. Force a reset of TC and queue
  8674. * layout configurations.
  8675. */
  8676. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  8677. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  8678. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  8679. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  8680. if (vsi->type == I40E_VSI_MAIN)
  8681. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  8682. /* assign it some queues */
  8683. ret = i40e_alloc_rings(vsi);
  8684. if (ret)
  8685. goto err_rings;
  8686. /* map all of the rings to the q_vectors */
  8687. i40e_vsi_map_rings_to_vectors(vsi);
  8688. return vsi;
  8689. err_rings:
  8690. i40e_vsi_free_q_vectors(vsi);
  8691. if (vsi->netdev_registered) {
  8692. vsi->netdev_registered = false;
  8693. unregister_netdev(vsi->netdev);
  8694. free_netdev(vsi->netdev);
  8695. vsi->netdev = NULL;
  8696. }
  8697. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8698. err_vsi:
  8699. i40e_vsi_clear(vsi);
  8700. return NULL;
  8701. }
  8702. /**
  8703. * i40e_vsi_setup - Set up a VSI by a given type
  8704. * @pf: board private structure
  8705. * @type: VSI type
  8706. * @uplink_seid: the switch element to link to
  8707. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  8708. *
  8709. * This allocates the sw VSI structure and its queue resources, then add a VSI
  8710. * to the identified VEB.
  8711. *
  8712. * Returns pointer to the successfully allocated and configure VSI sw struct on
  8713. * success, otherwise returns NULL on failure.
  8714. **/
  8715. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  8716. u16 uplink_seid, u32 param1)
  8717. {
  8718. struct i40e_vsi *vsi = NULL;
  8719. struct i40e_veb *veb = NULL;
  8720. int ret, i;
  8721. int v_idx;
  8722. /* The requested uplink_seid must be either
  8723. * - the PF's port seid
  8724. * no VEB is needed because this is the PF
  8725. * or this is a Flow Director special case VSI
  8726. * - seid of an existing VEB
  8727. * - seid of a VSI that owns an existing VEB
  8728. * - seid of a VSI that doesn't own a VEB
  8729. * a new VEB is created and the VSI becomes the owner
  8730. * - seid of the PF VSI, which is what creates the first VEB
  8731. * this is a special case of the previous
  8732. *
  8733. * Find which uplink_seid we were given and create a new VEB if needed
  8734. */
  8735. for (i = 0; i < I40E_MAX_VEB; i++) {
  8736. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  8737. veb = pf->veb[i];
  8738. break;
  8739. }
  8740. }
  8741. if (!veb && uplink_seid != pf->mac_seid) {
  8742. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8743. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  8744. vsi = pf->vsi[i];
  8745. break;
  8746. }
  8747. }
  8748. if (!vsi) {
  8749. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  8750. uplink_seid);
  8751. return NULL;
  8752. }
  8753. if (vsi->uplink_seid == pf->mac_seid)
  8754. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  8755. vsi->tc_config.enabled_tc);
  8756. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  8757. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8758. vsi->tc_config.enabled_tc);
  8759. if (veb) {
  8760. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  8761. dev_info(&vsi->back->pdev->dev,
  8762. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  8763. return NULL;
  8764. }
  8765. /* We come up by default in VEPA mode if SRIOV is not
  8766. * already enabled, in which case we can't force VEPA
  8767. * mode.
  8768. */
  8769. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  8770. veb->bridge_mode = BRIDGE_MODE_VEPA;
  8771. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8772. }
  8773. i40e_config_bridge_mode(veb);
  8774. }
  8775. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8776. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8777. veb = pf->veb[i];
  8778. }
  8779. if (!veb) {
  8780. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  8781. return NULL;
  8782. }
  8783. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  8784. uplink_seid = veb->seid;
  8785. }
  8786. /* get vsi sw struct */
  8787. v_idx = i40e_vsi_mem_alloc(pf, type);
  8788. if (v_idx < 0)
  8789. goto err_alloc;
  8790. vsi = pf->vsi[v_idx];
  8791. if (!vsi)
  8792. goto err_alloc;
  8793. vsi->type = type;
  8794. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  8795. if (type == I40E_VSI_MAIN)
  8796. pf->lan_vsi = v_idx;
  8797. else if (type == I40E_VSI_SRIOV)
  8798. vsi->vf_id = param1;
  8799. /* assign it some queues */
  8800. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
  8801. vsi->idx);
  8802. if (ret < 0) {
  8803. dev_info(&pf->pdev->dev,
  8804. "failed to get tracking for %d queues for VSI %d err=%d\n",
  8805. vsi->alloc_queue_pairs, vsi->seid, ret);
  8806. goto err_vsi;
  8807. }
  8808. vsi->base_queue = ret;
  8809. /* get a VSI from the hardware */
  8810. vsi->uplink_seid = uplink_seid;
  8811. ret = i40e_add_vsi(vsi);
  8812. if (ret)
  8813. goto err_vsi;
  8814. switch (vsi->type) {
  8815. /* setup the netdev if needed */
  8816. case I40E_VSI_MAIN:
  8817. /* Apply relevant filters if a platform-specific mac
  8818. * address was selected.
  8819. */
  8820. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  8821. ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  8822. if (ret) {
  8823. dev_warn(&pf->pdev->dev,
  8824. "could not set up macaddr; err %d\n",
  8825. ret);
  8826. }
  8827. }
  8828. case I40E_VSI_VMDQ2:
  8829. ret = i40e_config_netdev(vsi);
  8830. if (ret)
  8831. goto err_netdev;
  8832. ret = register_netdev(vsi->netdev);
  8833. if (ret)
  8834. goto err_netdev;
  8835. vsi->netdev_registered = true;
  8836. netif_carrier_off(vsi->netdev);
  8837. #ifdef CONFIG_I40E_DCB
  8838. /* Setup DCB netlink interface */
  8839. i40e_dcbnl_setup(vsi);
  8840. #endif /* CONFIG_I40E_DCB */
  8841. /* fall through */
  8842. case I40E_VSI_FDIR:
  8843. /* set up vectors and rings if needed */
  8844. ret = i40e_vsi_setup_vectors(vsi);
  8845. if (ret)
  8846. goto err_msix;
  8847. ret = i40e_alloc_rings(vsi);
  8848. if (ret)
  8849. goto err_rings;
  8850. /* map all of the rings to the q_vectors */
  8851. i40e_vsi_map_rings_to_vectors(vsi);
  8852. i40e_vsi_reset_stats(vsi);
  8853. break;
  8854. default:
  8855. /* no netdev or rings for the other VSI types */
  8856. break;
  8857. }
  8858. if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
  8859. (vsi->type == I40E_VSI_VMDQ2)) {
  8860. ret = i40e_vsi_config_rss(vsi);
  8861. }
  8862. return vsi;
  8863. err_rings:
  8864. i40e_vsi_free_q_vectors(vsi);
  8865. err_msix:
  8866. if (vsi->netdev_registered) {
  8867. vsi->netdev_registered = false;
  8868. unregister_netdev(vsi->netdev);
  8869. free_netdev(vsi->netdev);
  8870. vsi->netdev = NULL;
  8871. }
  8872. err_netdev:
  8873. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8874. err_vsi:
  8875. i40e_vsi_clear(vsi);
  8876. err_alloc:
  8877. return NULL;
  8878. }
  8879. /**
  8880. * i40e_veb_get_bw_info - Query VEB BW information
  8881. * @veb: the veb to query
  8882. *
  8883. * Query the Tx scheduler BW configuration data for given VEB
  8884. **/
  8885. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  8886. {
  8887. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  8888. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  8889. struct i40e_pf *pf = veb->pf;
  8890. struct i40e_hw *hw = &pf->hw;
  8891. u32 tc_bw_max;
  8892. int ret = 0;
  8893. int i;
  8894. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  8895. &bw_data, NULL);
  8896. if (ret) {
  8897. dev_info(&pf->pdev->dev,
  8898. "query veb bw config failed, err %s aq_err %s\n",
  8899. i40e_stat_str(&pf->hw, ret),
  8900. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8901. goto out;
  8902. }
  8903. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  8904. &ets_data, NULL);
  8905. if (ret) {
  8906. dev_info(&pf->pdev->dev,
  8907. "query veb bw ets config failed, err %s aq_err %s\n",
  8908. i40e_stat_str(&pf->hw, ret),
  8909. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8910. goto out;
  8911. }
  8912. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  8913. veb->bw_max_quanta = ets_data.tc_bw_max;
  8914. veb->is_abs_credits = bw_data.absolute_credits_enable;
  8915. veb->enabled_tc = ets_data.tc_valid_bits;
  8916. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  8917. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  8918. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  8919. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  8920. veb->bw_tc_limit_credits[i] =
  8921. le16_to_cpu(bw_data.tc_bw_limits[i]);
  8922. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  8923. }
  8924. out:
  8925. return ret;
  8926. }
  8927. /**
  8928. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  8929. * @pf: board private structure
  8930. *
  8931. * On error: returns error code (negative)
  8932. * On success: returns vsi index in PF (positive)
  8933. **/
  8934. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  8935. {
  8936. int ret = -ENOENT;
  8937. struct i40e_veb *veb;
  8938. int i;
  8939. /* Need to protect the allocation of switch elements at the PF level */
  8940. mutex_lock(&pf->switch_mutex);
  8941. /* VEB list may be fragmented if VEB creation/destruction has
  8942. * been happening. We can afford to do a quick scan to look
  8943. * for any free slots in the list.
  8944. *
  8945. * find next empty veb slot, looping back around if necessary
  8946. */
  8947. i = 0;
  8948. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  8949. i++;
  8950. if (i >= I40E_MAX_VEB) {
  8951. ret = -ENOMEM;
  8952. goto err_alloc_veb; /* out of VEB slots! */
  8953. }
  8954. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  8955. if (!veb) {
  8956. ret = -ENOMEM;
  8957. goto err_alloc_veb;
  8958. }
  8959. veb->pf = pf;
  8960. veb->idx = i;
  8961. veb->enabled_tc = 1;
  8962. pf->veb[i] = veb;
  8963. ret = i;
  8964. err_alloc_veb:
  8965. mutex_unlock(&pf->switch_mutex);
  8966. return ret;
  8967. }
  8968. /**
  8969. * i40e_switch_branch_release - Delete a branch of the switch tree
  8970. * @branch: where to start deleting
  8971. *
  8972. * This uses recursion to find the tips of the branch to be
  8973. * removed, deleting until we get back to and can delete this VEB.
  8974. **/
  8975. static void i40e_switch_branch_release(struct i40e_veb *branch)
  8976. {
  8977. struct i40e_pf *pf = branch->pf;
  8978. u16 branch_seid = branch->seid;
  8979. u16 veb_idx = branch->idx;
  8980. int i;
  8981. /* release any VEBs on this VEB - RECURSION */
  8982. for (i = 0; i < I40E_MAX_VEB; i++) {
  8983. if (!pf->veb[i])
  8984. continue;
  8985. if (pf->veb[i]->uplink_seid == branch->seid)
  8986. i40e_switch_branch_release(pf->veb[i]);
  8987. }
  8988. /* Release the VSIs on this VEB, but not the owner VSI.
  8989. *
  8990. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  8991. * the VEB itself, so don't use (*branch) after this loop.
  8992. */
  8993. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8994. if (!pf->vsi[i])
  8995. continue;
  8996. if (pf->vsi[i]->uplink_seid == branch_seid &&
  8997. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8998. i40e_vsi_release(pf->vsi[i]);
  8999. }
  9000. }
  9001. /* There's one corner case where the VEB might not have been
  9002. * removed, so double check it here and remove it if needed.
  9003. * This case happens if the veb was created from the debugfs
  9004. * commands and no VSIs were added to it.
  9005. */
  9006. if (pf->veb[veb_idx])
  9007. i40e_veb_release(pf->veb[veb_idx]);
  9008. }
  9009. /**
  9010. * i40e_veb_clear - remove veb struct
  9011. * @veb: the veb to remove
  9012. **/
  9013. static void i40e_veb_clear(struct i40e_veb *veb)
  9014. {
  9015. if (!veb)
  9016. return;
  9017. if (veb->pf) {
  9018. struct i40e_pf *pf = veb->pf;
  9019. mutex_lock(&pf->switch_mutex);
  9020. if (pf->veb[veb->idx] == veb)
  9021. pf->veb[veb->idx] = NULL;
  9022. mutex_unlock(&pf->switch_mutex);
  9023. }
  9024. kfree(veb);
  9025. }
  9026. /**
  9027. * i40e_veb_release - Delete a VEB and free its resources
  9028. * @veb: the VEB being removed
  9029. **/
  9030. void i40e_veb_release(struct i40e_veb *veb)
  9031. {
  9032. struct i40e_vsi *vsi = NULL;
  9033. struct i40e_pf *pf;
  9034. int i, n = 0;
  9035. pf = veb->pf;
  9036. /* find the remaining VSI and check for extras */
  9037. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9038. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  9039. n++;
  9040. vsi = pf->vsi[i];
  9041. }
  9042. }
  9043. if (n != 1) {
  9044. dev_info(&pf->pdev->dev,
  9045. "can't remove VEB %d with %d VSIs left\n",
  9046. veb->seid, n);
  9047. return;
  9048. }
  9049. /* move the remaining VSI to uplink veb */
  9050. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  9051. if (veb->uplink_seid) {
  9052. vsi->uplink_seid = veb->uplink_seid;
  9053. if (veb->uplink_seid == pf->mac_seid)
  9054. vsi->veb_idx = I40E_NO_VEB;
  9055. else
  9056. vsi->veb_idx = veb->veb_idx;
  9057. } else {
  9058. /* floating VEB */
  9059. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  9060. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  9061. }
  9062. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9063. i40e_veb_clear(veb);
  9064. }
  9065. /**
  9066. * i40e_add_veb - create the VEB in the switch
  9067. * @veb: the VEB to be instantiated
  9068. * @vsi: the controlling VSI
  9069. **/
  9070. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  9071. {
  9072. struct i40e_pf *pf = veb->pf;
  9073. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  9074. int ret;
  9075. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  9076. veb->enabled_tc, false,
  9077. &veb->seid, enable_stats, NULL);
  9078. /* get a VEB from the hardware */
  9079. if (ret) {
  9080. dev_info(&pf->pdev->dev,
  9081. "couldn't add VEB, err %s aq_err %s\n",
  9082. i40e_stat_str(&pf->hw, ret),
  9083. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9084. return -EPERM;
  9085. }
  9086. /* get statistics counter */
  9087. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  9088. &veb->stats_idx, NULL, NULL, NULL);
  9089. if (ret) {
  9090. dev_info(&pf->pdev->dev,
  9091. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  9092. i40e_stat_str(&pf->hw, ret),
  9093. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9094. return -EPERM;
  9095. }
  9096. ret = i40e_veb_get_bw_info(veb);
  9097. if (ret) {
  9098. dev_info(&pf->pdev->dev,
  9099. "couldn't get VEB bw info, err %s aq_err %s\n",
  9100. i40e_stat_str(&pf->hw, ret),
  9101. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9102. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9103. return -ENOENT;
  9104. }
  9105. vsi->uplink_seid = veb->seid;
  9106. vsi->veb_idx = veb->idx;
  9107. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  9108. return 0;
  9109. }
  9110. /**
  9111. * i40e_veb_setup - Set up a VEB
  9112. * @pf: board private structure
  9113. * @flags: VEB setup flags
  9114. * @uplink_seid: the switch element to link to
  9115. * @vsi_seid: the initial VSI seid
  9116. * @enabled_tc: Enabled TC bit-map
  9117. *
  9118. * This allocates the sw VEB structure and links it into the switch
  9119. * It is possible and legal for this to be a duplicate of an already
  9120. * existing VEB. It is also possible for both uplink and vsi seids
  9121. * to be zero, in order to create a floating VEB.
  9122. *
  9123. * Returns pointer to the successfully allocated VEB sw struct on
  9124. * success, otherwise returns NULL on failure.
  9125. **/
  9126. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  9127. u16 uplink_seid, u16 vsi_seid,
  9128. u8 enabled_tc)
  9129. {
  9130. struct i40e_veb *veb, *uplink_veb = NULL;
  9131. int vsi_idx, veb_idx;
  9132. int ret;
  9133. /* if one seid is 0, the other must be 0 to create a floating relay */
  9134. if ((uplink_seid == 0 || vsi_seid == 0) &&
  9135. (uplink_seid + vsi_seid != 0)) {
  9136. dev_info(&pf->pdev->dev,
  9137. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  9138. uplink_seid, vsi_seid);
  9139. return NULL;
  9140. }
  9141. /* make sure there is such a vsi and uplink */
  9142. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  9143. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  9144. break;
  9145. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  9146. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  9147. vsi_seid);
  9148. return NULL;
  9149. }
  9150. if (uplink_seid && uplink_seid != pf->mac_seid) {
  9151. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  9152. if (pf->veb[veb_idx] &&
  9153. pf->veb[veb_idx]->seid == uplink_seid) {
  9154. uplink_veb = pf->veb[veb_idx];
  9155. break;
  9156. }
  9157. }
  9158. if (!uplink_veb) {
  9159. dev_info(&pf->pdev->dev,
  9160. "uplink seid %d not found\n", uplink_seid);
  9161. return NULL;
  9162. }
  9163. }
  9164. /* get veb sw struct */
  9165. veb_idx = i40e_veb_mem_alloc(pf);
  9166. if (veb_idx < 0)
  9167. goto err_alloc;
  9168. veb = pf->veb[veb_idx];
  9169. veb->flags = flags;
  9170. veb->uplink_seid = uplink_seid;
  9171. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  9172. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  9173. /* create the VEB in the switch */
  9174. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  9175. if (ret)
  9176. goto err_veb;
  9177. if (vsi_idx == pf->lan_vsi)
  9178. pf->lan_veb = veb->idx;
  9179. return veb;
  9180. err_veb:
  9181. i40e_veb_clear(veb);
  9182. err_alloc:
  9183. return NULL;
  9184. }
  9185. /**
  9186. * i40e_setup_pf_switch_element - set PF vars based on switch type
  9187. * @pf: board private structure
  9188. * @ele: element we are building info from
  9189. * @num_reported: total number of elements
  9190. * @printconfig: should we print the contents
  9191. *
  9192. * helper function to assist in extracting a few useful SEID values.
  9193. **/
  9194. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  9195. struct i40e_aqc_switch_config_element_resp *ele,
  9196. u16 num_reported, bool printconfig)
  9197. {
  9198. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  9199. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  9200. u8 element_type = ele->element_type;
  9201. u16 seid = le16_to_cpu(ele->seid);
  9202. if (printconfig)
  9203. dev_info(&pf->pdev->dev,
  9204. "type=%d seid=%d uplink=%d downlink=%d\n",
  9205. element_type, seid, uplink_seid, downlink_seid);
  9206. switch (element_type) {
  9207. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  9208. pf->mac_seid = seid;
  9209. break;
  9210. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  9211. /* Main VEB? */
  9212. if (uplink_seid != pf->mac_seid)
  9213. break;
  9214. if (pf->lan_veb == I40E_NO_VEB) {
  9215. int v;
  9216. /* find existing or else empty VEB */
  9217. for (v = 0; v < I40E_MAX_VEB; v++) {
  9218. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  9219. pf->lan_veb = v;
  9220. break;
  9221. }
  9222. }
  9223. if (pf->lan_veb == I40E_NO_VEB) {
  9224. v = i40e_veb_mem_alloc(pf);
  9225. if (v < 0)
  9226. break;
  9227. pf->lan_veb = v;
  9228. }
  9229. }
  9230. pf->veb[pf->lan_veb]->seid = seid;
  9231. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  9232. pf->veb[pf->lan_veb]->pf = pf;
  9233. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  9234. break;
  9235. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  9236. if (num_reported != 1)
  9237. break;
  9238. /* This is immediately after a reset so we can assume this is
  9239. * the PF's VSI
  9240. */
  9241. pf->mac_seid = uplink_seid;
  9242. pf->pf_seid = downlink_seid;
  9243. pf->main_vsi_seid = seid;
  9244. if (printconfig)
  9245. dev_info(&pf->pdev->dev,
  9246. "pf_seid=%d main_vsi_seid=%d\n",
  9247. pf->pf_seid, pf->main_vsi_seid);
  9248. break;
  9249. case I40E_SWITCH_ELEMENT_TYPE_PF:
  9250. case I40E_SWITCH_ELEMENT_TYPE_VF:
  9251. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  9252. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  9253. case I40E_SWITCH_ELEMENT_TYPE_PE:
  9254. case I40E_SWITCH_ELEMENT_TYPE_PA:
  9255. /* ignore these for now */
  9256. break;
  9257. default:
  9258. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  9259. element_type, seid);
  9260. break;
  9261. }
  9262. }
  9263. /**
  9264. * i40e_fetch_switch_configuration - Get switch config from firmware
  9265. * @pf: board private structure
  9266. * @printconfig: should we print the contents
  9267. *
  9268. * Get the current switch configuration from the device and
  9269. * extract a few useful SEID values.
  9270. **/
  9271. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  9272. {
  9273. struct i40e_aqc_get_switch_config_resp *sw_config;
  9274. u16 next_seid = 0;
  9275. int ret = 0;
  9276. u8 *aq_buf;
  9277. int i;
  9278. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  9279. if (!aq_buf)
  9280. return -ENOMEM;
  9281. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  9282. do {
  9283. u16 num_reported, num_total;
  9284. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  9285. I40E_AQ_LARGE_BUF,
  9286. &next_seid, NULL);
  9287. if (ret) {
  9288. dev_info(&pf->pdev->dev,
  9289. "get switch config failed err %s aq_err %s\n",
  9290. i40e_stat_str(&pf->hw, ret),
  9291. i40e_aq_str(&pf->hw,
  9292. pf->hw.aq.asq_last_status));
  9293. kfree(aq_buf);
  9294. return -ENOENT;
  9295. }
  9296. num_reported = le16_to_cpu(sw_config->header.num_reported);
  9297. num_total = le16_to_cpu(sw_config->header.num_total);
  9298. if (printconfig)
  9299. dev_info(&pf->pdev->dev,
  9300. "header: %d reported %d total\n",
  9301. num_reported, num_total);
  9302. for (i = 0; i < num_reported; i++) {
  9303. struct i40e_aqc_switch_config_element_resp *ele =
  9304. &sw_config->element[i];
  9305. i40e_setup_pf_switch_element(pf, ele, num_reported,
  9306. printconfig);
  9307. }
  9308. } while (next_seid != 0);
  9309. kfree(aq_buf);
  9310. return ret;
  9311. }
  9312. /**
  9313. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  9314. * @pf: board private structure
  9315. * @reinit: if the Main VSI needs to re-initialized.
  9316. *
  9317. * Returns 0 on success, negative value on failure
  9318. **/
  9319. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  9320. {
  9321. u16 flags = 0;
  9322. int ret;
  9323. /* find out what's out there already */
  9324. ret = i40e_fetch_switch_configuration(pf, false);
  9325. if (ret) {
  9326. dev_info(&pf->pdev->dev,
  9327. "couldn't fetch switch config, err %s aq_err %s\n",
  9328. i40e_stat_str(&pf->hw, ret),
  9329. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9330. return ret;
  9331. }
  9332. i40e_pf_reset_stats(pf);
  9333. /* set the switch config bit for the whole device to
  9334. * support limited promisc or true promisc
  9335. * when user requests promisc. The default is limited
  9336. * promisc.
  9337. */
  9338. if ((pf->hw.pf_id == 0) &&
  9339. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
  9340. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9341. if (pf->hw.pf_id == 0) {
  9342. u16 valid_flags;
  9343. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9344. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
  9345. NULL);
  9346. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  9347. dev_info(&pf->pdev->dev,
  9348. "couldn't set switch config bits, err %s aq_err %s\n",
  9349. i40e_stat_str(&pf->hw, ret),
  9350. i40e_aq_str(&pf->hw,
  9351. pf->hw.aq.asq_last_status));
  9352. /* not a fatal problem, just keep going */
  9353. }
  9354. }
  9355. /* first time setup */
  9356. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  9357. struct i40e_vsi *vsi = NULL;
  9358. u16 uplink_seid;
  9359. /* Set up the PF VSI associated with the PF's main VSI
  9360. * that is already in the HW switch
  9361. */
  9362. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  9363. uplink_seid = pf->veb[pf->lan_veb]->seid;
  9364. else
  9365. uplink_seid = pf->mac_seid;
  9366. if (pf->lan_vsi == I40E_NO_VSI)
  9367. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  9368. else if (reinit)
  9369. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  9370. if (!vsi) {
  9371. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  9372. i40e_fdir_teardown(pf);
  9373. return -EAGAIN;
  9374. }
  9375. } else {
  9376. /* force a reset of TC and queue layout configurations */
  9377. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  9378. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  9379. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  9380. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  9381. }
  9382. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  9383. i40e_fdir_sb_setup(pf);
  9384. /* Setup static PF queue filter control settings */
  9385. ret = i40e_setup_pf_filter_control(pf);
  9386. if (ret) {
  9387. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  9388. ret);
  9389. /* Failure here should not stop continuing other steps */
  9390. }
  9391. /* enable RSS in the HW, even for only one queue, as the stack can use
  9392. * the hash
  9393. */
  9394. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  9395. i40e_pf_config_rss(pf);
  9396. /* fill in link information and enable LSE reporting */
  9397. i40e_link_event(pf);
  9398. /* Initialize user-specific link properties */
  9399. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  9400. I40E_AQ_AN_COMPLETED) ? true : false);
  9401. i40e_ptp_init(pf);
  9402. return ret;
  9403. }
  9404. /**
  9405. * i40e_determine_queue_usage - Work out queue distribution
  9406. * @pf: board private structure
  9407. **/
  9408. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  9409. {
  9410. int queues_left;
  9411. pf->num_lan_qps = 0;
  9412. /* Find the max queues to be put into basic use. We'll always be
  9413. * using TC0, whether or not DCB is running, and TC0 will get the
  9414. * big RSS set.
  9415. */
  9416. queues_left = pf->hw.func_caps.num_tx_qp;
  9417. if ((queues_left == 1) ||
  9418. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  9419. /* one qp for PF, no queues for anything else */
  9420. queues_left = 0;
  9421. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9422. /* make sure all the fancies are disabled */
  9423. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9424. I40E_FLAG_IWARP_ENABLED |
  9425. I40E_FLAG_FD_SB_ENABLED |
  9426. I40E_FLAG_FD_ATR_ENABLED |
  9427. I40E_FLAG_DCB_CAPABLE |
  9428. I40E_FLAG_DCB_ENABLED |
  9429. I40E_FLAG_SRIOV_ENABLED |
  9430. I40E_FLAG_VMDQ_ENABLED);
  9431. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  9432. I40E_FLAG_FD_SB_ENABLED |
  9433. I40E_FLAG_FD_ATR_ENABLED |
  9434. I40E_FLAG_DCB_CAPABLE))) {
  9435. /* one qp for PF */
  9436. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9437. queues_left -= pf->num_lan_qps;
  9438. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9439. I40E_FLAG_IWARP_ENABLED |
  9440. I40E_FLAG_FD_SB_ENABLED |
  9441. I40E_FLAG_FD_ATR_ENABLED |
  9442. I40E_FLAG_DCB_ENABLED |
  9443. I40E_FLAG_VMDQ_ENABLED);
  9444. } else {
  9445. /* Not enough queues for all TCs */
  9446. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  9447. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  9448. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  9449. I40E_FLAG_DCB_ENABLED);
  9450. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  9451. }
  9452. pf->num_lan_qps = max_t(int, pf->rss_size_max,
  9453. num_online_cpus());
  9454. pf->num_lan_qps = min_t(int, pf->num_lan_qps,
  9455. pf->hw.func_caps.num_tx_qp);
  9456. queues_left -= pf->num_lan_qps;
  9457. }
  9458. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9459. if (queues_left > 1) {
  9460. queues_left -= 1; /* save 1 queue for FD */
  9461. } else {
  9462. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9463. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  9464. }
  9465. }
  9466. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9467. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  9468. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  9469. (queues_left / pf->num_vf_qps));
  9470. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  9471. }
  9472. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9473. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  9474. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  9475. (queues_left / pf->num_vmdq_qps));
  9476. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  9477. }
  9478. pf->queues_left = queues_left;
  9479. dev_dbg(&pf->pdev->dev,
  9480. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  9481. pf->hw.func_caps.num_tx_qp,
  9482. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  9483. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  9484. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  9485. queues_left);
  9486. }
  9487. /**
  9488. * i40e_setup_pf_filter_control - Setup PF static filter control
  9489. * @pf: PF to be setup
  9490. *
  9491. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  9492. * settings. If PE/FCoE are enabled then it will also set the per PF
  9493. * based filter sizes required for them. It also enables Flow director,
  9494. * ethertype and macvlan type filter settings for the pf.
  9495. *
  9496. * Returns 0 on success, negative on failure
  9497. **/
  9498. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  9499. {
  9500. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  9501. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  9502. /* Flow Director is enabled */
  9503. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  9504. settings->enable_fdir = true;
  9505. /* Ethtype and MACVLAN filters enabled for PF */
  9506. settings->enable_ethtype = true;
  9507. settings->enable_macvlan = true;
  9508. if (i40e_set_filter_control(&pf->hw, settings))
  9509. return -ENOENT;
  9510. return 0;
  9511. }
  9512. #define INFO_STRING_LEN 255
  9513. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  9514. static void i40e_print_features(struct i40e_pf *pf)
  9515. {
  9516. struct i40e_hw *hw = &pf->hw;
  9517. char *buf;
  9518. int i;
  9519. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  9520. if (!buf)
  9521. return;
  9522. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  9523. #ifdef CONFIG_PCI_IOV
  9524. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  9525. #endif
  9526. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  9527. pf->hw.func_caps.num_vsis,
  9528. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  9529. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  9530. i += snprintf(&buf[i], REMAIN(i), " RSS");
  9531. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  9532. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  9533. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9534. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  9535. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  9536. }
  9537. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  9538. i += snprintf(&buf[i], REMAIN(i), " DCB");
  9539. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  9540. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  9541. if (pf->flags & I40E_FLAG_PTP)
  9542. i += snprintf(&buf[i], REMAIN(i), " PTP");
  9543. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  9544. i += snprintf(&buf[i], REMAIN(i), " VEB");
  9545. else
  9546. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  9547. dev_info(&pf->pdev->dev, "%s\n", buf);
  9548. kfree(buf);
  9549. WARN_ON(i > INFO_STRING_LEN);
  9550. }
  9551. /**
  9552. * i40e_get_platform_mac_addr - get platform-specific MAC address
  9553. * @pdev: PCI device information struct
  9554. * @pf: board private structure
  9555. *
  9556. * Look up the MAC address for the device. First we'll try
  9557. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  9558. * specific fallback. Otherwise, we'll default to the stored value in
  9559. * firmware.
  9560. **/
  9561. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  9562. {
  9563. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  9564. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  9565. }
  9566. /**
  9567. * i40e_probe - Device initialization routine
  9568. * @pdev: PCI device information struct
  9569. * @ent: entry in i40e_pci_tbl
  9570. *
  9571. * i40e_probe initializes a PF identified by a pci_dev structure.
  9572. * The OS initialization, configuring of the PF private structure,
  9573. * and a hardware reset occur.
  9574. *
  9575. * Returns 0 on success, negative on failure
  9576. **/
  9577. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  9578. {
  9579. struct i40e_aq_get_phy_abilities_resp abilities;
  9580. struct i40e_pf *pf;
  9581. struct i40e_hw *hw;
  9582. static u16 pfs_found;
  9583. u16 wol_nvm_bits;
  9584. u16 link_status;
  9585. int err;
  9586. u32 val;
  9587. u32 i;
  9588. u8 set_fc_aq_fail;
  9589. err = pci_enable_device_mem(pdev);
  9590. if (err)
  9591. return err;
  9592. /* set up for high or low dma */
  9593. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  9594. if (err) {
  9595. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  9596. if (err) {
  9597. dev_err(&pdev->dev,
  9598. "DMA configuration failed: 0x%x\n", err);
  9599. goto err_dma;
  9600. }
  9601. }
  9602. /* set up pci connections */
  9603. err = pci_request_mem_regions(pdev, i40e_driver_name);
  9604. if (err) {
  9605. dev_info(&pdev->dev,
  9606. "pci_request_selected_regions failed %d\n", err);
  9607. goto err_pci_reg;
  9608. }
  9609. pci_enable_pcie_error_reporting(pdev);
  9610. pci_set_master(pdev);
  9611. /* Now that we have a PCI connection, we need to do the
  9612. * low level device setup. This is primarily setting up
  9613. * the Admin Queue structures and then querying for the
  9614. * device's current profile information.
  9615. */
  9616. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  9617. if (!pf) {
  9618. err = -ENOMEM;
  9619. goto err_pf_alloc;
  9620. }
  9621. pf->next_vsi = 0;
  9622. pf->pdev = pdev;
  9623. set_bit(__I40E_DOWN, &pf->state);
  9624. hw = &pf->hw;
  9625. hw->back = pf;
  9626. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  9627. I40E_MAX_CSR_SPACE);
  9628. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  9629. if (!hw->hw_addr) {
  9630. err = -EIO;
  9631. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  9632. (unsigned int)pci_resource_start(pdev, 0),
  9633. pf->ioremap_len, err);
  9634. goto err_ioremap;
  9635. }
  9636. hw->vendor_id = pdev->vendor;
  9637. hw->device_id = pdev->device;
  9638. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  9639. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  9640. hw->subsystem_device_id = pdev->subsystem_device;
  9641. hw->bus.device = PCI_SLOT(pdev->devfn);
  9642. hw->bus.func = PCI_FUNC(pdev->devfn);
  9643. hw->bus.bus_id = pdev->bus->number;
  9644. pf->instance = pfs_found;
  9645. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  9646. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  9647. /* set up the locks for the AQ, do this only once in probe
  9648. * and destroy them only once in remove
  9649. */
  9650. mutex_init(&hw->aq.asq_mutex);
  9651. mutex_init(&hw->aq.arq_mutex);
  9652. pf->msg_enable = netif_msg_init(debug,
  9653. NETIF_MSG_DRV |
  9654. NETIF_MSG_PROBE |
  9655. NETIF_MSG_LINK);
  9656. if (debug < -1)
  9657. pf->hw.debug_mask = debug;
  9658. /* do a special CORER for clearing PXE mode once at init */
  9659. if (hw->revision_id == 0 &&
  9660. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  9661. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  9662. i40e_flush(hw);
  9663. msleep(200);
  9664. pf->corer_count++;
  9665. i40e_clear_pxe_mode(hw);
  9666. }
  9667. /* Reset here to make sure all is clean and to define PF 'n' */
  9668. i40e_clear_hw(hw);
  9669. err = i40e_pf_reset(hw);
  9670. if (err) {
  9671. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  9672. goto err_pf_reset;
  9673. }
  9674. pf->pfr_count++;
  9675. hw->aq.num_arq_entries = I40E_AQ_LEN;
  9676. hw->aq.num_asq_entries = I40E_AQ_LEN;
  9677. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9678. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9679. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  9680. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  9681. "%s-%s:misc",
  9682. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  9683. err = i40e_init_shared_code(hw);
  9684. if (err) {
  9685. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  9686. err);
  9687. goto err_pf_reset;
  9688. }
  9689. /* set up a default setting for link flow control */
  9690. pf->hw.fc.requested_mode = I40E_FC_NONE;
  9691. err = i40e_init_adminq(hw);
  9692. if (err) {
  9693. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  9694. dev_info(&pdev->dev,
  9695. "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");
  9696. else
  9697. dev_info(&pdev->dev,
  9698. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  9699. goto err_pf_reset;
  9700. }
  9701. /* provide nvm, fw, api versions */
  9702. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  9703. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  9704. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  9705. i40e_nvm_version_str(hw));
  9706. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  9707. hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
  9708. dev_info(&pdev->dev,
  9709. "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");
  9710. else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
  9711. hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
  9712. dev_info(&pdev->dev,
  9713. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  9714. i40e_verify_eeprom(pf);
  9715. /* Rev 0 hardware was never productized */
  9716. if (hw->revision_id < 1)
  9717. 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");
  9718. i40e_clear_pxe_mode(hw);
  9719. err = i40e_get_capabilities(pf);
  9720. if (err)
  9721. goto err_adminq_setup;
  9722. err = i40e_sw_init(pf);
  9723. if (err) {
  9724. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  9725. goto err_sw_init;
  9726. }
  9727. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  9728. hw->func_caps.num_rx_qp, 0, 0);
  9729. if (err) {
  9730. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  9731. goto err_init_lan_hmc;
  9732. }
  9733. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  9734. if (err) {
  9735. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  9736. err = -ENOENT;
  9737. goto err_configure_lan_hmc;
  9738. }
  9739. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  9740. * Ignore error return codes because if it was already disabled via
  9741. * hardware settings this will fail
  9742. */
  9743. if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
  9744. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  9745. i40e_aq_stop_lldp(hw, true, NULL);
  9746. }
  9747. /* allow a platform config to override the HW addr */
  9748. i40e_get_platform_mac_addr(pdev, pf);
  9749. if (!is_valid_ether_addr(hw->mac.addr)) {
  9750. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  9751. err = -EIO;
  9752. goto err_mac_addr;
  9753. }
  9754. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  9755. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  9756. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  9757. if (is_valid_ether_addr(hw->mac.port_addr))
  9758. pf->flags |= I40E_FLAG_PORT_ID_VALID;
  9759. pci_set_drvdata(pdev, pf);
  9760. pci_save_state(pdev);
  9761. #ifdef CONFIG_I40E_DCB
  9762. err = i40e_init_pf_dcb(pf);
  9763. if (err) {
  9764. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  9765. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  9766. /* Continue without DCB enabled */
  9767. }
  9768. #endif /* CONFIG_I40E_DCB */
  9769. /* set up periodic task facility */
  9770. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  9771. pf->service_timer_period = HZ;
  9772. INIT_WORK(&pf->service_task, i40e_service_task);
  9773. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  9774. /* NVM bit on means WoL disabled for the port */
  9775. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  9776. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  9777. pf->wol_en = false;
  9778. else
  9779. pf->wol_en = true;
  9780. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  9781. /* set up the main switch operations */
  9782. i40e_determine_queue_usage(pf);
  9783. err = i40e_init_interrupt_scheme(pf);
  9784. if (err)
  9785. goto err_switch_setup;
  9786. /* The number of VSIs reported by the FW is the minimum guaranteed
  9787. * to us; HW supports far more and we share the remaining pool with
  9788. * the other PFs. We allocate space for more than the guarantee with
  9789. * the understanding that we might not get them all later.
  9790. */
  9791. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  9792. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  9793. else
  9794. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  9795. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  9796. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  9797. GFP_KERNEL);
  9798. if (!pf->vsi) {
  9799. err = -ENOMEM;
  9800. goto err_switch_setup;
  9801. }
  9802. #ifdef CONFIG_PCI_IOV
  9803. /* prep for VF support */
  9804. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9805. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9806. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9807. if (pci_num_vf(pdev))
  9808. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  9809. }
  9810. #endif
  9811. err = i40e_setup_pf_switch(pf, false);
  9812. if (err) {
  9813. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  9814. goto err_vsis;
  9815. }
  9816. /* Make sure flow control is set according to current settings */
  9817. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  9818. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  9819. dev_dbg(&pf->pdev->dev,
  9820. "Set fc with err %s aq_err %s on get_phy_cap\n",
  9821. i40e_stat_str(hw, err),
  9822. i40e_aq_str(hw, hw->aq.asq_last_status));
  9823. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  9824. dev_dbg(&pf->pdev->dev,
  9825. "Set fc with err %s aq_err %s on set_phy_config\n",
  9826. i40e_stat_str(hw, err),
  9827. i40e_aq_str(hw, hw->aq.asq_last_status));
  9828. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  9829. dev_dbg(&pf->pdev->dev,
  9830. "Set fc with err %s aq_err %s on get_link_info\n",
  9831. i40e_stat_str(hw, err),
  9832. i40e_aq_str(hw, hw->aq.asq_last_status));
  9833. /* if FDIR VSI was set up, start it now */
  9834. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9835. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  9836. i40e_vsi_open(pf->vsi[i]);
  9837. break;
  9838. }
  9839. }
  9840. /* The driver only wants link up/down and module qualification
  9841. * reports from firmware. Note the negative logic.
  9842. */
  9843. err = i40e_aq_set_phy_int_mask(&pf->hw,
  9844. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  9845. I40E_AQ_EVENT_MEDIA_NA |
  9846. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  9847. if (err)
  9848. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  9849. i40e_stat_str(&pf->hw, err),
  9850. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9851. /* Reconfigure hardware for allowing smaller MSS in the case
  9852. * of TSO, so that we avoid the MDD being fired and causing
  9853. * a reset in the case of small MSS+TSO.
  9854. */
  9855. val = rd32(hw, I40E_REG_MSS);
  9856. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  9857. val &= ~I40E_REG_MSS_MIN_MASK;
  9858. val |= I40E_64BYTE_MSS;
  9859. wr32(hw, I40E_REG_MSS, val);
  9860. }
  9861. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  9862. msleep(75);
  9863. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  9864. if (err)
  9865. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  9866. i40e_stat_str(&pf->hw, err),
  9867. i40e_aq_str(&pf->hw,
  9868. pf->hw.aq.asq_last_status));
  9869. }
  9870. /* The main driver is (mostly) up and happy. We need to set this state
  9871. * before setting up the misc vector or we get a race and the vector
  9872. * ends up disabled forever.
  9873. */
  9874. clear_bit(__I40E_DOWN, &pf->state);
  9875. /* In case of MSIX we are going to setup the misc vector right here
  9876. * to handle admin queue events etc. In case of legacy and MSI
  9877. * the misc functionality and queue processing is combined in
  9878. * the same vector and that gets setup at open.
  9879. */
  9880. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9881. err = i40e_setup_misc_vector(pf);
  9882. if (err) {
  9883. dev_info(&pdev->dev,
  9884. "setup of misc vector failed: %d\n", err);
  9885. goto err_vsis;
  9886. }
  9887. }
  9888. #ifdef CONFIG_PCI_IOV
  9889. /* prep for VF support */
  9890. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9891. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9892. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9893. /* disable link interrupts for VFs */
  9894. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  9895. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  9896. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  9897. i40e_flush(hw);
  9898. if (pci_num_vf(pdev)) {
  9899. dev_info(&pdev->dev,
  9900. "Active VFs found, allocating resources.\n");
  9901. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  9902. if (err)
  9903. dev_info(&pdev->dev,
  9904. "Error %d allocating resources for existing VFs\n",
  9905. err);
  9906. }
  9907. }
  9908. #endif /* CONFIG_PCI_IOV */
  9909. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9910. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  9911. pf->num_iwarp_msix,
  9912. I40E_IWARP_IRQ_PILE_ID);
  9913. if (pf->iwarp_base_vector < 0) {
  9914. dev_info(&pdev->dev,
  9915. "failed to get tracking for %d vectors for IWARP err=%d\n",
  9916. pf->num_iwarp_msix, pf->iwarp_base_vector);
  9917. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9918. }
  9919. }
  9920. i40e_dbg_pf_init(pf);
  9921. /* tell the firmware that we're starting */
  9922. i40e_send_version(pf);
  9923. /* since everything's happy, start the service_task timer */
  9924. mod_timer(&pf->service_timer,
  9925. round_jiffies(jiffies + pf->service_timer_period));
  9926. /* add this PF to client device list and launch a client service task */
  9927. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9928. err = i40e_lan_add_device(pf);
  9929. if (err)
  9930. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  9931. err);
  9932. }
  9933. #define PCI_SPEED_SIZE 8
  9934. #define PCI_WIDTH_SIZE 8
  9935. /* Devices on the IOSF bus do not have this information
  9936. * and will report PCI Gen 1 x 1 by default so don't bother
  9937. * checking them.
  9938. */
  9939. if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
  9940. char speed[PCI_SPEED_SIZE] = "Unknown";
  9941. char width[PCI_WIDTH_SIZE] = "Unknown";
  9942. /* Get the negotiated link width and speed from PCI config
  9943. * space
  9944. */
  9945. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  9946. &link_status);
  9947. i40e_set_pci_config_data(hw, link_status);
  9948. switch (hw->bus.speed) {
  9949. case i40e_bus_speed_8000:
  9950. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  9951. case i40e_bus_speed_5000:
  9952. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  9953. case i40e_bus_speed_2500:
  9954. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  9955. default:
  9956. break;
  9957. }
  9958. switch (hw->bus.width) {
  9959. case i40e_bus_width_pcie_x8:
  9960. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  9961. case i40e_bus_width_pcie_x4:
  9962. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  9963. case i40e_bus_width_pcie_x2:
  9964. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  9965. case i40e_bus_width_pcie_x1:
  9966. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  9967. default:
  9968. break;
  9969. }
  9970. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  9971. speed, width);
  9972. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  9973. hw->bus.speed < i40e_bus_speed_8000) {
  9974. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  9975. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  9976. }
  9977. }
  9978. /* get the requested speeds from the fw */
  9979. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  9980. if (err)
  9981. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  9982. i40e_stat_str(&pf->hw, err),
  9983. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9984. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  9985. /* get the supported phy types from the fw */
  9986. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  9987. if (err)
  9988. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  9989. i40e_stat_str(&pf->hw, err),
  9990. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9991. /* Add a filter to drop all Flow control frames from any VSI from being
  9992. * transmitted. By doing so we stop a malicious VF from sending out
  9993. * PAUSE or PFC frames and potentially controlling traffic for other
  9994. * PF/VF VSIs.
  9995. * The FW can still send Flow control frames if enabled.
  9996. */
  9997. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  9998. pf->main_vsi_seid);
  9999. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  10000. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  10001. pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
  10002. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  10003. pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
  10004. /* print a string summarizing features */
  10005. i40e_print_features(pf);
  10006. return 0;
  10007. /* Unwind what we've done if something failed in the setup */
  10008. err_vsis:
  10009. set_bit(__I40E_DOWN, &pf->state);
  10010. i40e_clear_interrupt_scheme(pf);
  10011. kfree(pf->vsi);
  10012. err_switch_setup:
  10013. i40e_reset_interrupt_capability(pf);
  10014. del_timer_sync(&pf->service_timer);
  10015. err_mac_addr:
  10016. err_configure_lan_hmc:
  10017. (void)i40e_shutdown_lan_hmc(hw);
  10018. err_init_lan_hmc:
  10019. kfree(pf->qp_pile);
  10020. err_sw_init:
  10021. err_adminq_setup:
  10022. err_pf_reset:
  10023. iounmap(hw->hw_addr);
  10024. err_ioremap:
  10025. kfree(pf);
  10026. err_pf_alloc:
  10027. pci_disable_pcie_error_reporting(pdev);
  10028. pci_release_mem_regions(pdev);
  10029. err_pci_reg:
  10030. err_dma:
  10031. pci_disable_device(pdev);
  10032. return err;
  10033. }
  10034. /**
  10035. * i40e_remove - Device removal routine
  10036. * @pdev: PCI device information struct
  10037. *
  10038. * i40e_remove is called by the PCI subsystem to alert the driver
  10039. * that is should release a PCI device. This could be caused by a
  10040. * Hot-Plug event, or because the driver is going to be removed from
  10041. * memory.
  10042. **/
  10043. static void i40e_remove(struct pci_dev *pdev)
  10044. {
  10045. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10046. struct i40e_hw *hw = &pf->hw;
  10047. i40e_status ret_code;
  10048. int i;
  10049. i40e_dbg_pf_exit(pf);
  10050. i40e_ptp_stop(pf);
  10051. /* Disable RSS in hw */
  10052. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  10053. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  10054. /* no more scheduling of any task */
  10055. set_bit(__I40E_SUSPENDED, &pf->state);
  10056. set_bit(__I40E_DOWN, &pf->state);
  10057. if (pf->service_timer.data)
  10058. del_timer_sync(&pf->service_timer);
  10059. if (pf->service_task.func)
  10060. cancel_work_sync(&pf->service_task);
  10061. /* Client close must be called explicitly here because the timer
  10062. * has been stopped.
  10063. */
  10064. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  10065. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  10066. i40e_free_vfs(pf);
  10067. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  10068. }
  10069. i40e_fdir_teardown(pf);
  10070. /* If there is a switch structure or any orphans, remove them.
  10071. * This will leave only the PF's VSI remaining.
  10072. */
  10073. for (i = 0; i < I40E_MAX_VEB; i++) {
  10074. if (!pf->veb[i])
  10075. continue;
  10076. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  10077. pf->veb[i]->uplink_seid == 0)
  10078. i40e_switch_branch_release(pf->veb[i]);
  10079. }
  10080. /* Now we can shutdown the PF's VSI, just before we kill
  10081. * adminq and hmc.
  10082. */
  10083. if (pf->vsi[pf->lan_vsi])
  10084. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  10085. /* remove attached clients */
  10086. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  10087. ret_code = i40e_lan_del_device(pf);
  10088. if (ret_code)
  10089. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  10090. ret_code);
  10091. }
  10092. /* shutdown and destroy the HMC */
  10093. if (hw->hmc.hmc_obj) {
  10094. ret_code = i40e_shutdown_lan_hmc(hw);
  10095. if (ret_code)
  10096. dev_warn(&pdev->dev,
  10097. "Failed to destroy the HMC resources: %d\n",
  10098. ret_code);
  10099. }
  10100. /* shutdown the adminq */
  10101. i40e_shutdown_adminq(hw);
  10102. /* destroy the locks only once, here */
  10103. mutex_destroy(&hw->aq.arq_mutex);
  10104. mutex_destroy(&hw->aq.asq_mutex);
  10105. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  10106. i40e_clear_interrupt_scheme(pf);
  10107. for (i = 0; i < pf->num_alloc_vsi; i++) {
  10108. if (pf->vsi[i]) {
  10109. i40e_vsi_clear_rings(pf->vsi[i]);
  10110. i40e_vsi_clear(pf->vsi[i]);
  10111. pf->vsi[i] = NULL;
  10112. }
  10113. }
  10114. for (i = 0; i < I40E_MAX_VEB; i++) {
  10115. kfree(pf->veb[i]);
  10116. pf->veb[i] = NULL;
  10117. }
  10118. kfree(pf->qp_pile);
  10119. kfree(pf->vsi);
  10120. iounmap(hw->hw_addr);
  10121. kfree(pf);
  10122. pci_release_mem_regions(pdev);
  10123. pci_disable_pcie_error_reporting(pdev);
  10124. pci_disable_device(pdev);
  10125. }
  10126. /**
  10127. * i40e_pci_error_detected - warning that something funky happened in PCI land
  10128. * @pdev: PCI device information struct
  10129. *
  10130. * Called to warn that something happened and the error handling steps
  10131. * are in progress. Allows the driver to quiesce things, be ready for
  10132. * remediation.
  10133. **/
  10134. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  10135. enum pci_channel_state error)
  10136. {
  10137. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10138. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  10139. if (!pf) {
  10140. dev_info(&pdev->dev,
  10141. "Cannot recover - error happened during device probe\n");
  10142. return PCI_ERS_RESULT_DISCONNECT;
  10143. }
  10144. /* shutdown all operations */
  10145. if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
  10146. rtnl_lock();
  10147. i40e_prep_for_reset(pf, true);
  10148. rtnl_unlock();
  10149. }
  10150. /* Request a slot reset */
  10151. return PCI_ERS_RESULT_NEED_RESET;
  10152. }
  10153. /**
  10154. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  10155. * @pdev: PCI device information struct
  10156. *
  10157. * Called to find if the driver can work with the device now that
  10158. * the pci slot has been reset. If a basic connection seems good
  10159. * (registers are readable and have sane content) then return a
  10160. * happy little PCI_ERS_RESULT_xxx.
  10161. **/
  10162. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  10163. {
  10164. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10165. pci_ers_result_t result;
  10166. int err;
  10167. u32 reg;
  10168. dev_dbg(&pdev->dev, "%s\n", __func__);
  10169. if (pci_enable_device_mem(pdev)) {
  10170. dev_info(&pdev->dev,
  10171. "Cannot re-enable PCI device after reset.\n");
  10172. result = PCI_ERS_RESULT_DISCONNECT;
  10173. } else {
  10174. pci_set_master(pdev);
  10175. pci_restore_state(pdev);
  10176. pci_save_state(pdev);
  10177. pci_wake_from_d3(pdev, false);
  10178. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  10179. if (reg == 0)
  10180. result = PCI_ERS_RESULT_RECOVERED;
  10181. else
  10182. result = PCI_ERS_RESULT_DISCONNECT;
  10183. }
  10184. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  10185. if (err) {
  10186. dev_info(&pdev->dev,
  10187. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  10188. err);
  10189. /* non-fatal, continue */
  10190. }
  10191. return result;
  10192. }
  10193. /**
  10194. * i40e_pci_error_resume - restart operations after PCI error recovery
  10195. * @pdev: PCI device information struct
  10196. *
  10197. * Called to allow the driver to bring things back up after PCI error
  10198. * and/or reset recovery has finished.
  10199. **/
  10200. static void i40e_pci_error_resume(struct pci_dev *pdev)
  10201. {
  10202. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10203. dev_dbg(&pdev->dev, "%s\n", __func__);
  10204. if (test_bit(__I40E_SUSPENDED, &pf->state))
  10205. return;
  10206. rtnl_lock();
  10207. i40e_handle_reset_warning(pf, true);
  10208. rtnl_unlock();
  10209. }
  10210. /**
  10211. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  10212. * using the mac_address_write admin q function
  10213. * @pf: pointer to i40e_pf struct
  10214. **/
  10215. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  10216. {
  10217. struct i40e_hw *hw = &pf->hw;
  10218. i40e_status ret;
  10219. u8 mac_addr[6];
  10220. u16 flags = 0;
  10221. /* Get current MAC address in case it's an LAA */
  10222. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  10223. ether_addr_copy(mac_addr,
  10224. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  10225. } else {
  10226. dev_err(&pf->pdev->dev,
  10227. "Failed to retrieve MAC address; using default\n");
  10228. ether_addr_copy(mac_addr, hw->mac.addr);
  10229. }
  10230. /* The FW expects the mac address write cmd to first be called with
  10231. * one of these flags before calling it again with the multicast
  10232. * enable flags.
  10233. */
  10234. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  10235. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  10236. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  10237. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10238. if (ret) {
  10239. dev_err(&pf->pdev->dev,
  10240. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  10241. return;
  10242. }
  10243. flags = I40E_AQC_MC_MAG_EN
  10244. | I40E_AQC_WOL_PRESERVE_ON_PFR
  10245. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  10246. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10247. if (ret)
  10248. dev_err(&pf->pdev->dev,
  10249. "Failed to enable Multicast Magic Packet wake up\n");
  10250. }
  10251. /**
  10252. * i40e_shutdown - PCI callback for shutting down
  10253. * @pdev: PCI device information struct
  10254. **/
  10255. static void i40e_shutdown(struct pci_dev *pdev)
  10256. {
  10257. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10258. struct i40e_hw *hw = &pf->hw;
  10259. set_bit(__I40E_SUSPENDED, &pf->state);
  10260. set_bit(__I40E_DOWN, &pf->state);
  10261. rtnl_lock();
  10262. i40e_prep_for_reset(pf, true);
  10263. rtnl_unlock();
  10264. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10265. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10266. del_timer_sync(&pf->service_timer);
  10267. cancel_work_sync(&pf->service_task);
  10268. i40e_fdir_teardown(pf);
  10269. /* Client close must be called explicitly here because the timer
  10270. * has been stopped.
  10271. */
  10272. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  10273. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10274. i40e_enable_mc_magic_wake(pf);
  10275. rtnl_lock();
  10276. i40e_prep_for_reset(pf, true);
  10277. rtnl_unlock();
  10278. wr32(hw, I40E_PFPM_APM,
  10279. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10280. wr32(hw, I40E_PFPM_WUFC,
  10281. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10282. i40e_clear_interrupt_scheme(pf);
  10283. if (system_state == SYSTEM_POWER_OFF) {
  10284. pci_wake_from_d3(pdev, pf->wol_en);
  10285. pci_set_power_state(pdev, PCI_D3hot);
  10286. }
  10287. }
  10288. #ifdef CONFIG_PM
  10289. /**
  10290. * i40e_suspend - PCI callback for moving to D3
  10291. * @pdev: PCI device information struct
  10292. **/
  10293. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  10294. {
  10295. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10296. struct i40e_hw *hw = &pf->hw;
  10297. int retval = 0;
  10298. set_bit(__I40E_SUSPENDED, &pf->state);
  10299. set_bit(__I40E_DOWN, &pf->state);
  10300. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10301. i40e_enable_mc_magic_wake(pf);
  10302. rtnl_lock();
  10303. i40e_prep_for_reset(pf, true);
  10304. rtnl_unlock();
  10305. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10306. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10307. i40e_stop_misc_vector(pf);
  10308. retval = pci_save_state(pdev);
  10309. if (retval)
  10310. return retval;
  10311. pci_wake_from_d3(pdev, pf->wol_en);
  10312. pci_set_power_state(pdev, PCI_D3hot);
  10313. return retval;
  10314. }
  10315. /**
  10316. * i40e_resume - PCI callback for waking up from D3
  10317. * @pdev: PCI device information struct
  10318. **/
  10319. static int i40e_resume(struct pci_dev *pdev)
  10320. {
  10321. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10322. u32 err;
  10323. pci_set_power_state(pdev, PCI_D0);
  10324. pci_restore_state(pdev);
  10325. /* pci_restore_state() clears dev->state_saves, so
  10326. * call pci_save_state() again to restore it.
  10327. */
  10328. pci_save_state(pdev);
  10329. err = pci_enable_device_mem(pdev);
  10330. if (err) {
  10331. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  10332. return err;
  10333. }
  10334. pci_set_master(pdev);
  10335. /* no wakeup events while running */
  10336. pci_wake_from_d3(pdev, false);
  10337. /* handling the reset will rebuild the device state */
  10338. if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
  10339. clear_bit(__I40E_DOWN, &pf->state);
  10340. rtnl_lock();
  10341. i40e_reset_and_rebuild(pf, false, true);
  10342. rtnl_unlock();
  10343. }
  10344. return 0;
  10345. }
  10346. #endif
  10347. static const struct pci_error_handlers i40e_err_handler = {
  10348. .error_detected = i40e_pci_error_detected,
  10349. .slot_reset = i40e_pci_error_slot_reset,
  10350. .resume = i40e_pci_error_resume,
  10351. };
  10352. static struct pci_driver i40e_driver = {
  10353. .name = i40e_driver_name,
  10354. .id_table = i40e_pci_tbl,
  10355. .probe = i40e_probe,
  10356. .remove = i40e_remove,
  10357. #ifdef CONFIG_PM
  10358. .suspend = i40e_suspend,
  10359. .resume = i40e_resume,
  10360. #endif
  10361. .shutdown = i40e_shutdown,
  10362. .err_handler = &i40e_err_handler,
  10363. .sriov_configure = i40e_pci_sriov_configure,
  10364. };
  10365. /**
  10366. * i40e_init_module - Driver registration routine
  10367. *
  10368. * i40e_init_module is the first routine called when the driver is
  10369. * loaded. All it does is register with the PCI subsystem.
  10370. **/
  10371. static int __init i40e_init_module(void)
  10372. {
  10373. pr_info("%s: %s - version %s\n", i40e_driver_name,
  10374. i40e_driver_string, i40e_driver_version_str);
  10375. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  10376. /* we will see if single thread per module is enough for now,
  10377. * it can't be any worse than using the system workqueue which
  10378. * was already single threaded
  10379. */
  10380. i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
  10381. i40e_driver_name);
  10382. if (!i40e_wq) {
  10383. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  10384. return -ENOMEM;
  10385. }
  10386. i40e_dbg_init();
  10387. return pci_register_driver(&i40e_driver);
  10388. }
  10389. module_init(i40e_init_module);
  10390. /**
  10391. * i40e_exit_module - Driver exit cleanup routine
  10392. *
  10393. * i40e_exit_module is called just before the driver is removed
  10394. * from memory.
  10395. **/
  10396. static void __exit i40e_exit_module(void)
  10397. {
  10398. pci_unregister_driver(&i40e_driver);
  10399. destroy_workqueue(i40e_wq);
  10400. i40e_dbg_exit();
  10401. }
  10402. module_exit(i40e_exit_module);