i40e_main.c 217 KB

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