i40e_main.c 273 KB

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