i40e_main.c 198 KB

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