i40e_main.c 227 KB

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