i40e_main.c 230 KB

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