i40e_main.c 245 KB

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