i40e_main.c 218 KB

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