ice_main.c 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. /* Intel(R) Ethernet Connection E800 Series Linux Driver */
  4. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  5. #include "ice.h"
  6. #define DRV_VERSION "0.7.1-k"
  7. #define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
  8. const char ice_drv_ver[] = DRV_VERSION;
  9. static const char ice_driver_string[] = DRV_SUMMARY;
  10. static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
  11. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  12. MODULE_DESCRIPTION(DRV_SUMMARY);
  13. MODULE_LICENSE("GPL v2");
  14. MODULE_VERSION(DRV_VERSION);
  15. static int debug = -1;
  16. module_param(debug, int, 0644);
  17. #ifndef CONFIG_DYNAMIC_DEBUG
  18. MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
  19. #else
  20. MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
  21. #endif /* !CONFIG_DYNAMIC_DEBUG */
  22. static struct workqueue_struct *ice_wq;
  23. static const struct net_device_ops ice_netdev_ops;
  24. static void ice_pf_dis_all_vsi(struct ice_pf *pf);
  25. static void ice_rebuild(struct ice_pf *pf);
  26. static int ice_vsi_release(struct ice_vsi *vsi);
  27. static void ice_vsi_release_all(struct ice_pf *pf);
  28. static void ice_update_vsi_stats(struct ice_vsi *vsi);
  29. static void ice_update_pf_stats(struct ice_pf *pf);
  30. /**
  31. * ice_get_tx_pending - returns number of Tx descriptors not processed
  32. * @ring: the ring of descriptors
  33. */
  34. static u32 ice_get_tx_pending(struct ice_ring *ring)
  35. {
  36. u32 head, tail;
  37. head = ring->next_to_clean;
  38. tail = readl(ring->tail);
  39. if (head != tail)
  40. return (head < tail) ?
  41. tail - head : (tail + ring->count - head);
  42. return 0;
  43. }
  44. /**
  45. * ice_check_for_hang_subtask - check for and recover hung queues
  46. * @pf: pointer to PF struct
  47. */
  48. static void ice_check_for_hang_subtask(struct ice_pf *pf)
  49. {
  50. struct ice_vsi *vsi = NULL;
  51. unsigned int i;
  52. u32 v, v_idx;
  53. int packets;
  54. ice_for_each_vsi(pf, v)
  55. if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
  56. vsi = pf->vsi[v];
  57. break;
  58. }
  59. if (!vsi || test_bit(__ICE_DOWN, vsi->state))
  60. return;
  61. if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
  62. return;
  63. for (i = 0; i < vsi->num_txq; i++) {
  64. struct ice_ring *tx_ring = vsi->tx_rings[i];
  65. if (tx_ring && tx_ring->desc) {
  66. int itr = ICE_ITR_NONE;
  67. /* If packet counter has not changed the queue is
  68. * likely stalled, so force an interrupt for this
  69. * queue.
  70. *
  71. * prev_pkt would be negative if there was no
  72. * pending work.
  73. */
  74. packets = tx_ring->stats.pkts & INT_MAX;
  75. if (tx_ring->tx_stats.prev_pkt == packets) {
  76. /* Trigger sw interrupt to revive the queue */
  77. v_idx = tx_ring->q_vector->v_idx;
  78. wr32(&vsi->back->hw,
  79. GLINT_DYN_CTL(vsi->base_vector + v_idx),
  80. (itr << GLINT_DYN_CTL_ITR_INDX_S) |
  81. GLINT_DYN_CTL_SWINT_TRIG_M |
  82. GLINT_DYN_CTL_INTENA_MSK_M);
  83. continue;
  84. }
  85. /* Memory barrier between read of packet count and call
  86. * to ice_get_tx_pending()
  87. */
  88. smp_rmb();
  89. tx_ring->tx_stats.prev_pkt =
  90. ice_get_tx_pending(tx_ring) ? packets : -1;
  91. }
  92. }
  93. }
  94. /**
  95. * ice_get_free_slot - get the next non-NULL location index in array
  96. * @array: array to search
  97. * @size: size of the array
  98. * @curr: last known occupied index to be used as a search hint
  99. *
  100. * void * is being used to keep the functionality generic. This lets us use this
  101. * function on any array of pointers.
  102. */
  103. static int ice_get_free_slot(void *array, int size, int curr)
  104. {
  105. int **tmp_array = (int **)array;
  106. int next;
  107. if (curr < (size - 1) && !tmp_array[curr + 1]) {
  108. next = curr + 1;
  109. } else {
  110. int i = 0;
  111. while ((i < size) && (tmp_array[i]))
  112. i++;
  113. if (i == size)
  114. next = ICE_NO_VSI;
  115. else
  116. next = i;
  117. }
  118. return next;
  119. }
  120. /**
  121. * ice_search_res - Search the tracker for a block of resources
  122. * @res: pointer to the resource
  123. * @needed: size of the block needed
  124. * @id: identifier to track owner
  125. * Returns the base item index of the block, or -ENOMEM for error
  126. */
  127. static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
  128. {
  129. int start = res->search_hint;
  130. int end = start;
  131. id |= ICE_RES_VALID_BIT;
  132. do {
  133. /* skip already allocated entries */
  134. if (res->list[end++] & ICE_RES_VALID_BIT) {
  135. start = end;
  136. if ((start + needed) > res->num_entries)
  137. break;
  138. }
  139. if (end == (start + needed)) {
  140. int i = start;
  141. /* there was enough, so assign it to the requestor */
  142. while (i != end)
  143. res->list[i++] = id;
  144. if (end == res->num_entries)
  145. end = 0;
  146. res->search_hint = end;
  147. return start;
  148. }
  149. } while (1);
  150. return -ENOMEM;
  151. }
  152. /**
  153. * ice_get_res - get a block of resources
  154. * @pf: board private structure
  155. * @res: pointer to the resource
  156. * @needed: size of the block needed
  157. * @id: identifier to track owner
  158. *
  159. * Returns the base item index of the block, or -ENOMEM for error
  160. * The search_hint trick and lack of advanced fit-finding only works
  161. * because we're highly likely to have all the same sized requests.
  162. * Linear search time and any fragmentation should be minimal.
  163. */
  164. static int
  165. ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
  166. {
  167. int ret;
  168. if (!res || !pf)
  169. return -EINVAL;
  170. if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
  171. dev_err(&pf->pdev->dev,
  172. "param err: needed=%d, num_entries = %d id=0x%04x\n",
  173. needed, res->num_entries, id);
  174. return -EINVAL;
  175. }
  176. /* search based on search_hint */
  177. ret = ice_search_res(res, needed, id);
  178. if (ret < 0) {
  179. /* previous search failed. Reset search hint and try again */
  180. res->search_hint = 0;
  181. ret = ice_search_res(res, needed, id);
  182. }
  183. return ret;
  184. }
  185. /**
  186. * ice_free_res - free a block of resources
  187. * @res: pointer to the resource
  188. * @index: starting index previously returned by ice_get_res
  189. * @id: identifier to track owner
  190. * Returns number of resources freed
  191. */
  192. static int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
  193. {
  194. int count = 0;
  195. int i;
  196. if (!res || index >= res->num_entries)
  197. return -EINVAL;
  198. id |= ICE_RES_VALID_BIT;
  199. for (i = index; i < res->num_entries && res->list[i] == id; i++) {
  200. res->list[i] = 0;
  201. count++;
  202. }
  203. return count;
  204. }
  205. /**
  206. * ice_add_mac_to_list - Add a mac address filter entry to the list
  207. * @vsi: the VSI to be forwarded to
  208. * @add_list: pointer to the list which contains MAC filter entries
  209. * @macaddr: the MAC address to be added.
  210. *
  211. * Adds mac address filter entry to the temp list
  212. *
  213. * Returns 0 on success or ENOMEM on failure.
  214. */
  215. static int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
  216. const u8 *macaddr)
  217. {
  218. struct ice_fltr_list_entry *tmp;
  219. struct ice_pf *pf = vsi->back;
  220. tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
  221. if (!tmp)
  222. return -ENOMEM;
  223. tmp->fltr_info.flag = ICE_FLTR_TX;
  224. tmp->fltr_info.src = vsi->vsi_num;
  225. tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
  226. tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  227. tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
  228. ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
  229. INIT_LIST_HEAD(&tmp->list_entry);
  230. list_add(&tmp->list_entry, add_list);
  231. return 0;
  232. }
  233. /**
  234. * ice_add_mac_to_sync_list - creates list of mac addresses to be synced
  235. * @netdev: the net device on which the sync is happening
  236. * @addr: mac address to sync
  237. *
  238. * This is a callback function which is called by the in kernel device sync
  239. * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
  240. * populates the tmp_sync_list, which is later used by ice_add_mac to add the
  241. * mac filters from the hardware.
  242. */
  243. static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
  244. {
  245. struct ice_netdev_priv *np = netdev_priv(netdev);
  246. struct ice_vsi *vsi = np->vsi;
  247. if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
  248. return -EINVAL;
  249. return 0;
  250. }
  251. /**
  252. * ice_add_mac_to_unsync_list - creates list of mac addresses to be unsynced
  253. * @netdev: the net device on which the unsync is happening
  254. * @addr: mac address to unsync
  255. *
  256. * This is a callback function which is called by the in kernel device unsync
  257. * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
  258. * populates the tmp_unsync_list, which is later used by ice_remove_mac to
  259. * delete the mac filters from the hardware.
  260. */
  261. static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
  262. {
  263. struct ice_netdev_priv *np = netdev_priv(netdev);
  264. struct ice_vsi *vsi = np->vsi;
  265. if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
  266. return -EINVAL;
  267. return 0;
  268. }
  269. /**
  270. * ice_free_fltr_list - free filter lists helper
  271. * @dev: pointer to the device struct
  272. * @h: pointer to the list head to be freed
  273. *
  274. * Helper function to free filter lists previously created using
  275. * ice_add_mac_to_list
  276. */
  277. static void ice_free_fltr_list(struct device *dev, struct list_head *h)
  278. {
  279. struct ice_fltr_list_entry *e, *tmp;
  280. list_for_each_entry_safe(e, tmp, h, list_entry) {
  281. list_del(&e->list_entry);
  282. devm_kfree(dev, e);
  283. }
  284. }
  285. /**
  286. * ice_vsi_fltr_changed - check if filter state changed
  287. * @vsi: VSI to be checked
  288. *
  289. * returns true if filter state has changed, false otherwise.
  290. */
  291. static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
  292. {
  293. return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
  294. test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
  295. test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
  296. }
  297. /**
  298. * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
  299. * @vsi: VSI to enable or disable VLAN pruning on
  300. * @ena: set to true to enable VLAN pruning and false to disable it
  301. *
  302. * returns 0 if VSI is updated, negative otherwise
  303. */
  304. static int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena)
  305. {
  306. struct ice_vsi_ctx *ctxt;
  307. struct device *dev;
  308. int status;
  309. if (!vsi)
  310. return -EINVAL;
  311. dev = &vsi->back->pdev->dev;
  312. ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
  313. if (!ctxt)
  314. return -ENOMEM;
  315. ctxt->info = vsi->info;
  316. if (ena) {
  317. ctxt->info.sec_flags |=
  318. ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
  319. ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
  320. ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
  321. } else {
  322. ctxt->info.sec_flags &=
  323. ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
  324. ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
  325. ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
  326. }
  327. ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID |
  328. ICE_AQ_VSI_PROP_SW_VALID);
  329. ctxt->vsi_num = vsi->vsi_num;
  330. status = ice_aq_update_vsi(&vsi->back->hw, ctxt, NULL);
  331. if (status) {
  332. netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI %d failed, err = %d, aq_err = %d\n",
  333. ena ? "Ena" : "Dis", vsi->vsi_num, status,
  334. vsi->back->hw.adminq.sq_last_status);
  335. goto err_out;
  336. }
  337. vsi->info.sec_flags = ctxt->info.sec_flags;
  338. vsi->info.sw_flags2 = ctxt->info.sw_flags2;
  339. devm_kfree(dev, ctxt);
  340. return 0;
  341. err_out:
  342. devm_kfree(dev, ctxt);
  343. return -EIO;
  344. }
  345. /**
  346. * ice_vsi_sync_fltr - Update the VSI filter list to the HW
  347. * @vsi: ptr to the VSI
  348. *
  349. * Push any outstanding VSI filter changes through the AdminQ.
  350. */
  351. static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
  352. {
  353. struct device *dev = &vsi->back->pdev->dev;
  354. struct net_device *netdev = vsi->netdev;
  355. bool promisc_forced_on = false;
  356. struct ice_pf *pf = vsi->back;
  357. struct ice_hw *hw = &pf->hw;
  358. enum ice_status status = 0;
  359. u32 changed_flags = 0;
  360. int err = 0;
  361. if (!vsi->netdev)
  362. return -EINVAL;
  363. while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
  364. usleep_range(1000, 2000);
  365. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  366. vsi->current_netdev_flags = vsi->netdev->flags;
  367. INIT_LIST_HEAD(&vsi->tmp_sync_list);
  368. INIT_LIST_HEAD(&vsi->tmp_unsync_list);
  369. if (ice_vsi_fltr_changed(vsi)) {
  370. clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
  371. clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
  372. clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
  373. /* grab the netdev's addr_list_lock */
  374. netif_addr_lock_bh(netdev);
  375. __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
  376. ice_add_mac_to_unsync_list);
  377. __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
  378. ice_add_mac_to_unsync_list);
  379. /* our temp lists are populated. release lock */
  380. netif_addr_unlock_bh(netdev);
  381. }
  382. /* Remove mac addresses in the unsync list */
  383. status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
  384. ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
  385. if (status) {
  386. netdev_err(netdev, "Failed to delete MAC filters\n");
  387. /* if we failed because of alloc failures, just bail */
  388. if (status == ICE_ERR_NO_MEMORY) {
  389. err = -ENOMEM;
  390. goto out;
  391. }
  392. }
  393. /* Add mac addresses in the sync list */
  394. status = ice_add_mac(hw, &vsi->tmp_sync_list);
  395. ice_free_fltr_list(dev, &vsi->tmp_sync_list);
  396. if (status) {
  397. netdev_err(netdev, "Failed to add MAC filters\n");
  398. /* If there is no more space for new umac filters, vsi
  399. * should go into promiscuous mode. There should be some
  400. * space reserved for promiscuous filters.
  401. */
  402. if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
  403. !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
  404. vsi->state)) {
  405. promisc_forced_on = true;
  406. netdev_warn(netdev,
  407. "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
  408. vsi->vsi_num);
  409. } else {
  410. err = -EIO;
  411. goto out;
  412. }
  413. }
  414. /* check for changes in promiscuous modes */
  415. if (changed_flags & IFF_ALLMULTI)
  416. netdev_warn(netdev, "Unsupported configuration\n");
  417. if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
  418. test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
  419. clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
  420. if (vsi->current_netdev_flags & IFF_PROMISC) {
  421. /* Apply TX filter rule to get traffic from VMs */
  422. status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
  423. ICE_FLTR_TX);
  424. if (status) {
  425. netdev_err(netdev, "Error setting default VSI %i tx rule\n",
  426. vsi->vsi_num);
  427. vsi->current_netdev_flags &= ~IFF_PROMISC;
  428. err = -EIO;
  429. goto out_promisc;
  430. }
  431. /* Apply RX filter rule to get traffic from wire */
  432. status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
  433. ICE_FLTR_RX);
  434. if (status) {
  435. netdev_err(netdev, "Error setting default VSI %i rx rule\n",
  436. vsi->vsi_num);
  437. vsi->current_netdev_flags &= ~IFF_PROMISC;
  438. err = -EIO;
  439. goto out_promisc;
  440. }
  441. } else {
  442. /* Clear TX filter rule to stop traffic from VMs */
  443. status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
  444. ICE_FLTR_TX);
  445. if (status) {
  446. netdev_err(netdev, "Error clearing default VSI %i tx rule\n",
  447. vsi->vsi_num);
  448. vsi->current_netdev_flags |= IFF_PROMISC;
  449. err = -EIO;
  450. goto out_promisc;
  451. }
  452. /* Clear filter RX to remove traffic from wire */
  453. status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
  454. ICE_FLTR_RX);
  455. if (status) {
  456. netdev_err(netdev, "Error clearing default VSI %i rx rule\n",
  457. vsi->vsi_num);
  458. vsi->current_netdev_flags |= IFF_PROMISC;
  459. err = -EIO;
  460. goto out_promisc;
  461. }
  462. }
  463. }
  464. goto exit;
  465. out_promisc:
  466. set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
  467. goto exit;
  468. out:
  469. /* if something went wrong then set the changed flag so we try again */
  470. set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
  471. set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
  472. exit:
  473. clear_bit(__ICE_CFG_BUSY, vsi->state);
  474. return err;
  475. }
  476. /**
  477. * ice_sync_fltr_subtask - Sync the VSI filter list with HW
  478. * @pf: board private structure
  479. */
  480. static void ice_sync_fltr_subtask(struct ice_pf *pf)
  481. {
  482. int v;
  483. if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
  484. return;
  485. clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
  486. for (v = 0; v < pf->num_alloc_vsi; v++)
  487. if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
  488. ice_vsi_sync_fltr(pf->vsi[v])) {
  489. /* come back and try again later */
  490. set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
  491. break;
  492. }
  493. }
  494. /**
  495. * ice_is_reset_recovery_pending - schedule a reset
  496. * @state: pf state field
  497. */
  498. static bool ice_is_reset_recovery_pending(unsigned long int *state)
  499. {
  500. return test_bit(__ICE_RESET_RECOVERY_PENDING, state);
  501. }
  502. /**
  503. * ice_prepare_for_reset - prep for the core to reset
  504. * @pf: board private structure
  505. *
  506. * Inform or close all dependent features in prep for reset.
  507. */
  508. static void
  509. ice_prepare_for_reset(struct ice_pf *pf)
  510. {
  511. struct ice_hw *hw = &pf->hw;
  512. /* disable the VSIs and their queues that are not already DOWN */
  513. ice_pf_dis_all_vsi(pf);
  514. ice_shutdown_all_ctrlq(hw);
  515. set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
  516. }
  517. /**
  518. * ice_do_reset - Initiate one of many types of resets
  519. * @pf: board private structure
  520. * @reset_type: reset type requested
  521. * before this function was called.
  522. */
  523. static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
  524. {
  525. struct device *dev = &pf->pdev->dev;
  526. struct ice_hw *hw = &pf->hw;
  527. dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
  528. WARN_ON(in_interrupt());
  529. /* PFR is a bit of a special case because it doesn't result in an OICR
  530. * interrupt. Set pending bit here which otherwise gets set in the
  531. * OICR handler.
  532. */
  533. if (reset_type == ICE_RESET_PFR)
  534. set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  535. ice_prepare_for_reset(pf);
  536. /* trigger the reset */
  537. if (ice_reset(hw, reset_type)) {
  538. dev_err(dev, "reset %d failed\n", reset_type);
  539. set_bit(__ICE_RESET_FAILED, pf->state);
  540. clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  541. clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
  542. return;
  543. }
  544. /* PFR is a bit of a special case because it doesn't result in an OICR
  545. * interrupt. So for PFR, rebuild after the reset and clear the reset-
  546. * associated state bits.
  547. */
  548. if (reset_type == ICE_RESET_PFR) {
  549. pf->pfr_count++;
  550. ice_rebuild(pf);
  551. clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  552. clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
  553. }
  554. }
  555. /**
  556. * ice_reset_subtask - Set up for resetting the device and driver
  557. * @pf: board private structure
  558. */
  559. static void ice_reset_subtask(struct ice_pf *pf)
  560. {
  561. enum ice_reset_req reset_type = ICE_RESET_INVAL;
  562. /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
  563. * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
  564. * of reset is pending and sets bits in pf->state indicating the reset
  565. * type and __ICE_RESET_RECOVERY_PENDING. So, if the latter bit is set
  566. * prepare for pending reset if not already (for PF software-initiated
  567. * global resets the software should already be prepared for it as
  568. * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
  569. * by firmware or software on other PFs, that bit is not set so prepare
  570. * for the reset now), poll for reset done, rebuild and return.
  571. */
  572. if (ice_is_reset_recovery_pending(pf->state)) {
  573. clear_bit(__ICE_GLOBR_RECV, pf->state);
  574. clear_bit(__ICE_CORER_RECV, pf->state);
  575. if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
  576. ice_prepare_for_reset(pf);
  577. /* make sure we are ready to rebuild */
  578. if (ice_check_reset(&pf->hw)) {
  579. set_bit(__ICE_RESET_FAILED, pf->state);
  580. } else {
  581. /* done with reset. start rebuild */
  582. pf->hw.reset_ongoing = false;
  583. ice_rebuild(pf);
  584. /* clear bit to resume normal operations, but
  585. * ICE_NEEDS_RESTART bit is set incase rebuild failed
  586. */
  587. clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  588. clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
  589. }
  590. return;
  591. }
  592. /* No pending resets to finish processing. Check for new resets */
  593. if (test_and_clear_bit(__ICE_PFR_REQ, pf->state))
  594. reset_type = ICE_RESET_PFR;
  595. if (test_and_clear_bit(__ICE_CORER_REQ, pf->state))
  596. reset_type = ICE_RESET_CORER;
  597. if (test_and_clear_bit(__ICE_GLOBR_REQ, pf->state))
  598. reset_type = ICE_RESET_GLOBR;
  599. /* If no valid reset type requested just return */
  600. if (reset_type == ICE_RESET_INVAL)
  601. return;
  602. /* reset if not already down or busy */
  603. if (!test_bit(__ICE_DOWN, pf->state) &&
  604. !test_bit(__ICE_CFG_BUSY, pf->state)) {
  605. ice_do_reset(pf, reset_type);
  606. }
  607. }
  608. /**
  609. * ice_watchdog_subtask - periodic tasks not using event driven scheduling
  610. * @pf: board private structure
  611. */
  612. static void ice_watchdog_subtask(struct ice_pf *pf)
  613. {
  614. int i;
  615. /* if interface is down do nothing */
  616. if (test_bit(__ICE_DOWN, pf->state) ||
  617. test_bit(__ICE_CFG_BUSY, pf->state))
  618. return;
  619. /* make sure we don't do these things too often */
  620. if (time_before(jiffies,
  621. pf->serv_tmr_prev + pf->serv_tmr_period))
  622. return;
  623. pf->serv_tmr_prev = jiffies;
  624. /* Update the stats for active netdevs so the network stack
  625. * can look at updated numbers whenever it cares to
  626. */
  627. ice_update_pf_stats(pf);
  628. for (i = 0; i < pf->num_alloc_vsi; i++)
  629. if (pf->vsi[i] && pf->vsi[i]->netdev)
  630. ice_update_vsi_stats(pf->vsi[i]);
  631. }
  632. /**
  633. * ice_print_link_msg - print link up or down message
  634. * @vsi: the VSI whose link status is being queried
  635. * @isup: boolean for if the link is now up or down
  636. */
  637. void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
  638. {
  639. const char *speed;
  640. const char *fc;
  641. if (vsi->current_isup == isup)
  642. return;
  643. vsi->current_isup = isup;
  644. if (!isup) {
  645. netdev_info(vsi->netdev, "NIC Link is Down\n");
  646. return;
  647. }
  648. switch (vsi->port_info->phy.link_info.link_speed) {
  649. case ICE_AQ_LINK_SPEED_40GB:
  650. speed = "40 G";
  651. break;
  652. case ICE_AQ_LINK_SPEED_25GB:
  653. speed = "25 G";
  654. break;
  655. case ICE_AQ_LINK_SPEED_20GB:
  656. speed = "20 G";
  657. break;
  658. case ICE_AQ_LINK_SPEED_10GB:
  659. speed = "10 G";
  660. break;
  661. case ICE_AQ_LINK_SPEED_5GB:
  662. speed = "5 G";
  663. break;
  664. case ICE_AQ_LINK_SPEED_2500MB:
  665. speed = "2.5 G";
  666. break;
  667. case ICE_AQ_LINK_SPEED_1000MB:
  668. speed = "1 G";
  669. break;
  670. case ICE_AQ_LINK_SPEED_100MB:
  671. speed = "100 M";
  672. break;
  673. default:
  674. speed = "Unknown";
  675. break;
  676. }
  677. switch (vsi->port_info->fc.current_mode) {
  678. case ICE_FC_FULL:
  679. fc = "RX/TX";
  680. break;
  681. case ICE_FC_TX_PAUSE:
  682. fc = "TX";
  683. break;
  684. case ICE_FC_RX_PAUSE:
  685. fc = "RX";
  686. break;
  687. default:
  688. fc = "Unknown";
  689. break;
  690. }
  691. netdev_info(vsi->netdev, "NIC Link is up %sbps, Flow Control: %s\n",
  692. speed, fc);
  693. }
  694. /**
  695. * ice_init_link_events - enable/initialize link events
  696. * @pi: pointer to the port_info instance
  697. *
  698. * Returns -EIO on failure, 0 on success
  699. */
  700. static int ice_init_link_events(struct ice_port_info *pi)
  701. {
  702. u16 mask;
  703. mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
  704. ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
  705. if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
  706. dev_dbg(ice_hw_to_dev(pi->hw),
  707. "Failed to set link event mask for port %d\n",
  708. pi->lport);
  709. return -EIO;
  710. }
  711. if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
  712. dev_dbg(ice_hw_to_dev(pi->hw),
  713. "Failed to enable link events for port %d\n",
  714. pi->lport);
  715. return -EIO;
  716. }
  717. return 0;
  718. }
  719. /**
  720. * ice_vsi_link_event - update the vsi's netdev
  721. * @vsi: the vsi on which the link event occurred
  722. * @link_up: whether or not the vsi needs to be set up or down
  723. */
  724. static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
  725. {
  726. if (!vsi || test_bit(__ICE_DOWN, vsi->state))
  727. return;
  728. if (vsi->type == ICE_VSI_PF) {
  729. if (!vsi->netdev) {
  730. dev_dbg(&vsi->back->pdev->dev,
  731. "vsi->netdev is not initialized!\n");
  732. return;
  733. }
  734. if (link_up) {
  735. netif_carrier_on(vsi->netdev);
  736. netif_tx_wake_all_queues(vsi->netdev);
  737. } else {
  738. netif_carrier_off(vsi->netdev);
  739. netif_tx_stop_all_queues(vsi->netdev);
  740. }
  741. }
  742. }
  743. /**
  744. * ice_link_event - process the link event
  745. * @pf: pf that the link event is associated with
  746. * @pi: port_info for the port that the link event is associated with
  747. *
  748. * Returns -EIO if ice_get_link_status() fails
  749. * Returns 0 on success
  750. */
  751. static int
  752. ice_link_event(struct ice_pf *pf, struct ice_port_info *pi)
  753. {
  754. u8 new_link_speed, old_link_speed;
  755. struct ice_phy_info *phy_info;
  756. bool new_link_same_as_old;
  757. bool new_link, old_link;
  758. u8 lport;
  759. u16 v;
  760. phy_info = &pi->phy;
  761. phy_info->link_info_old = phy_info->link_info;
  762. /* Force ice_get_link_status() to update link info */
  763. phy_info->get_link_info = true;
  764. old_link = (phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
  765. old_link_speed = phy_info->link_info_old.link_speed;
  766. lport = pi->lport;
  767. if (ice_get_link_status(pi, &new_link)) {
  768. dev_dbg(&pf->pdev->dev,
  769. "Could not get link status for port %d\n", lport);
  770. return -EIO;
  771. }
  772. new_link_speed = phy_info->link_info.link_speed;
  773. new_link_same_as_old = (new_link == old_link &&
  774. new_link_speed == old_link_speed);
  775. ice_for_each_vsi(pf, v) {
  776. struct ice_vsi *vsi = pf->vsi[v];
  777. if (!vsi || !vsi->port_info)
  778. continue;
  779. if (new_link_same_as_old &&
  780. (test_bit(__ICE_DOWN, vsi->state) ||
  781. new_link == netif_carrier_ok(vsi->netdev)))
  782. continue;
  783. if (vsi->port_info->lport == lport) {
  784. ice_print_link_msg(vsi, new_link);
  785. ice_vsi_link_event(vsi, new_link);
  786. }
  787. }
  788. return 0;
  789. }
  790. /**
  791. * ice_handle_link_event - handle link event via ARQ
  792. * @pf: pf that the link event is associated with
  793. *
  794. * Return -EINVAL if port_info is null
  795. * Return status on succes
  796. */
  797. static int ice_handle_link_event(struct ice_pf *pf)
  798. {
  799. struct ice_port_info *port_info;
  800. int status;
  801. port_info = pf->hw.port_info;
  802. if (!port_info)
  803. return -EINVAL;
  804. status = ice_link_event(pf, port_info);
  805. if (status)
  806. dev_dbg(&pf->pdev->dev,
  807. "Could not process link event, error %d\n", status);
  808. return status;
  809. }
  810. /**
  811. * __ice_clean_ctrlq - helper function to clean controlq rings
  812. * @pf: ptr to struct ice_pf
  813. * @q_type: specific Control queue type
  814. */
  815. static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
  816. {
  817. struct ice_rq_event_info event;
  818. struct ice_hw *hw = &pf->hw;
  819. struct ice_ctl_q_info *cq;
  820. u16 pending, i = 0;
  821. const char *qtype;
  822. u32 oldval, val;
  823. /* Do not clean control queue if/when PF reset fails */
  824. if (test_bit(__ICE_RESET_FAILED, pf->state))
  825. return 0;
  826. switch (q_type) {
  827. case ICE_CTL_Q_ADMIN:
  828. cq = &hw->adminq;
  829. qtype = "Admin";
  830. break;
  831. default:
  832. dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
  833. q_type);
  834. return 0;
  835. }
  836. /* check for error indications - PF_xx_AxQLEN register layout for
  837. * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
  838. */
  839. val = rd32(hw, cq->rq.len);
  840. if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
  841. PF_FW_ARQLEN_ARQCRIT_M)) {
  842. oldval = val;
  843. if (val & PF_FW_ARQLEN_ARQVFE_M)
  844. dev_dbg(&pf->pdev->dev,
  845. "%s Receive Queue VF Error detected\n", qtype);
  846. if (val & PF_FW_ARQLEN_ARQOVFL_M) {
  847. dev_dbg(&pf->pdev->dev,
  848. "%s Receive Queue Overflow Error detected\n",
  849. qtype);
  850. }
  851. if (val & PF_FW_ARQLEN_ARQCRIT_M)
  852. dev_dbg(&pf->pdev->dev,
  853. "%s Receive Queue Critical Error detected\n",
  854. qtype);
  855. val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
  856. PF_FW_ARQLEN_ARQCRIT_M);
  857. if (oldval != val)
  858. wr32(hw, cq->rq.len, val);
  859. }
  860. val = rd32(hw, cq->sq.len);
  861. if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
  862. PF_FW_ATQLEN_ATQCRIT_M)) {
  863. oldval = val;
  864. if (val & PF_FW_ATQLEN_ATQVFE_M)
  865. dev_dbg(&pf->pdev->dev,
  866. "%s Send Queue VF Error detected\n", qtype);
  867. if (val & PF_FW_ATQLEN_ATQOVFL_M) {
  868. dev_dbg(&pf->pdev->dev,
  869. "%s Send Queue Overflow Error detected\n",
  870. qtype);
  871. }
  872. if (val & PF_FW_ATQLEN_ATQCRIT_M)
  873. dev_dbg(&pf->pdev->dev,
  874. "%s Send Queue Critical Error detected\n",
  875. qtype);
  876. val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
  877. PF_FW_ATQLEN_ATQCRIT_M);
  878. if (oldval != val)
  879. wr32(hw, cq->sq.len, val);
  880. }
  881. event.buf_len = cq->rq_buf_size;
  882. event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
  883. GFP_KERNEL);
  884. if (!event.msg_buf)
  885. return 0;
  886. do {
  887. enum ice_status ret;
  888. u16 opcode;
  889. ret = ice_clean_rq_elem(hw, cq, &event, &pending);
  890. if (ret == ICE_ERR_AQ_NO_WORK)
  891. break;
  892. if (ret) {
  893. dev_err(&pf->pdev->dev,
  894. "%s Receive Queue event error %d\n", qtype,
  895. ret);
  896. break;
  897. }
  898. opcode = le16_to_cpu(event.desc.opcode);
  899. switch (opcode) {
  900. case ice_aqc_opc_get_link_status:
  901. if (ice_handle_link_event(pf))
  902. dev_err(&pf->pdev->dev,
  903. "Could not handle link event\n");
  904. break;
  905. case ice_aqc_opc_fw_logging:
  906. ice_output_fw_log(hw, &event.desc, event.msg_buf);
  907. break;
  908. default:
  909. dev_dbg(&pf->pdev->dev,
  910. "%s Receive Queue unknown event 0x%04x ignored\n",
  911. qtype, opcode);
  912. break;
  913. }
  914. } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
  915. devm_kfree(&pf->pdev->dev, event.msg_buf);
  916. return pending && (i == ICE_DFLT_IRQ_WORK);
  917. }
  918. /**
  919. * ice_ctrlq_pending - check if there is a difference between ntc and ntu
  920. * @hw: pointer to hardware info
  921. * @cq: control queue information
  922. *
  923. * returns true if there are pending messages in a queue, false if there aren't
  924. */
  925. static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  926. {
  927. u16 ntu;
  928. ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
  929. return cq->rq.next_to_clean != ntu;
  930. }
  931. /**
  932. * ice_clean_adminq_subtask - clean the AdminQ rings
  933. * @pf: board private structure
  934. */
  935. static void ice_clean_adminq_subtask(struct ice_pf *pf)
  936. {
  937. struct ice_hw *hw = &pf->hw;
  938. if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
  939. return;
  940. if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
  941. return;
  942. clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
  943. /* There might be a situation where new messages arrive to a control
  944. * queue between processing the last message and clearing the
  945. * EVENT_PENDING bit. So before exiting, check queue head again (using
  946. * ice_ctrlq_pending) and process new messages if any.
  947. */
  948. if (ice_ctrlq_pending(hw, &hw->adminq))
  949. __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
  950. ice_flush(hw);
  951. }
  952. /**
  953. * ice_service_task_schedule - schedule the service task to wake up
  954. * @pf: board private structure
  955. *
  956. * If not already scheduled, this puts the task into the work queue.
  957. */
  958. static void ice_service_task_schedule(struct ice_pf *pf)
  959. {
  960. if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
  961. !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
  962. !test_bit(__ICE_NEEDS_RESTART, pf->state))
  963. queue_work(ice_wq, &pf->serv_task);
  964. }
  965. /**
  966. * ice_service_task_complete - finish up the service task
  967. * @pf: board private structure
  968. */
  969. static void ice_service_task_complete(struct ice_pf *pf)
  970. {
  971. WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
  972. /* force memory (pf->state) to sync before next service task */
  973. smp_mb__before_atomic();
  974. clear_bit(__ICE_SERVICE_SCHED, pf->state);
  975. }
  976. /**
  977. * ice_service_task_stop - stop service task and cancel works
  978. * @pf: board private structure
  979. */
  980. static void ice_service_task_stop(struct ice_pf *pf)
  981. {
  982. set_bit(__ICE_SERVICE_DIS, pf->state);
  983. if (pf->serv_tmr.function)
  984. del_timer_sync(&pf->serv_tmr);
  985. if (pf->serv_task.func)
  986. cancel_work_sync(&pf->serv_task);
  987. clear_bit(__ICE_SERVICE_SCHED, pf->state);
  988. }
  989. /**
  990. * ice_service_timer - timer callback to schedule service task
  991. * @t: pointer to timer_list
  992. */
  993. static void ice_service_timer(struct timer_list *t)
  994. {
  995. struct ice_pf *pf = from_timer(pf, t, serv_tmr);
  996. mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
  997. ice_service_task_schedule(pf);
  998. }
  999. /**
  1000. * ice_handle_mdd_event - handle malicious driver detect event
  1001. * @pf: pointer to the PF structure
  1002. *
  1003. * Called from service task. OICR interrupt handler indicates MDD event
  1004. */
  1005. static void ice_handle_mdd_event(struct ice_pf *pf)
  1006. {
  1007. struct ice_hw *hw = &pf->hw;
  1008. bool mdd_detected = false;
  1009. u32 reg;
  1010. if (!test_bit(__ICE_MDD_EVENT_PENDING, pf->state))
  1011. return;
  1012. /* find what triggered the MDD event */
  1013. reg = rd32(hw, GL_MDET_TX_PQM);
  1014. if (reg & GL_MDET_TX_PQM_VALID_M) {
  1015. u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
  1016. GL_MDET_TX_PQM_PF_NUM_S;
  1017. u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
  1018. GL_MDET_TX_PQM_VF_NUM_S;
  1019. u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
  1020. GL_MDET_TX_PQM_MAL_TYPE_S;
  1021. u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
  1022. GL_MDET_TX_PQM_QNUM_S);
  1023. if (netif_msg_tx_err(pf))
  1024. dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
  1025. event, queue, pf_num, vf_num);
  1026. wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
  1027. mdd_detected = true;
  1028. }
  1029. reg = rd32(hw, GL_MDET_TX_TCLAN);
  1030. if (reg & GL_MDET_TX_TCLAN_VALID_M) {
  1031. u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
  1032. GL_MDET_TX_TCLAN_PF_NUM_S;
  1033. u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
  1034. GL_MDET_TX_TCLAN_VF_NUM_S;
  1035. u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
  1036. GL_MDET_TX_TCLAN_MAL_TYPE_S;
  1037. u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
  1038. GL_MDET_TX_TCLAN_QNUM_S);
  1039. if (netif_msg_rx_err(pf))
  1040. dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
  1041. event, queue, pf_num, vf_num);
  1042. wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
  1043. mdd_detected = true;
  1044. }
  1045. reg = rd32(hw, GL_MDET_RX);
  1046. if (reg & GL_MDET_RX_VALID_M) {
  1047. u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
  1048. GL_MDET_RX_PF_NUM_S;
  1049. u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
  1050. GL_MDET_RX_VF_NUM_S;
  1051. u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
  1052. GL_MDET_RX_MAL_TYPE_S;
  1053. u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
  1054. GL_MDET_RX_QNUM_S);
  1055. if (netif_msg_rx_err(pf))
  1056. dev_info(&pf->pdev->dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
  1057. event, queue, pf_num, vf_num);
  1058. wr32(hw, GL_MDET_RX, 0xffffffff);
  1059. mdd_detected = true;
  1060. }
  1061. if (mdd_detected) {
  1062. bool pf_mdd_detected = false;
  1063. reg = rd32(hw, PF_MDET_TX_PQM);
  1064. if (reg & PF_MDET_TX_PQM_VALID_M) {
  1065. wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
  1066. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  1067. pf_mdd_detected = true;
  1068. }
  1069. reg = rd32(hw, PF_MDET_TX_TCLAN);
  1070. if (reg & PF_MDET_TX_TCLAN_VALID_M) {
  1071. wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
  1072. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  1073. pf_mdd_detected = true;
  1074. }
  1075. reg = rd32(hw, PF_MDET_RX);
  1076. if (reg & PF_MDET_RX_VALID_M) {
  1077. wr32(hw, PF_MDET_RX, 0xFFFF);
  1078. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  1079. pf_mdd_detected = true;
  1080. }
  1081. /* Queue belongs to the PF initiate a reset */
  1082. if (pf_mdd_detected) {
  1083. set_bit(__ICE_NEEDS_RESTART, pf->state);
  1084. ice_service_task_schedule(pf);
  1085. }
  1086. }
  1087. /* re-enable MDD interrupt cause */
  1088. clear_bit(__ICE_MDD_EVENT_PENDING, pf->state);
  1089. reg = rd32(hw, PFINT_OICR_ENA);
  1090. reg |= PFINT_OICR_MAL_DETECT_M;
  1091. wr32(hw, PFINT_OICR_ENA, reg);
  1092. ice_flush(hw);
  1093. }
  1094. /**
  1095. * ice_service_task - manage and run subtasks
  1096. * @work: pointer to work_struct contained by the PF struct
  1097. */
  1098. static void ice_service_task(struct work_struct *work)
  1099. {
  1100. struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
  1101. unsigned long start_time = jiffies;
  1102. /* subtasks */
  1103. /* process reset requests first */
  1104. ice_reset_subtask(pf);
  1105. /* bail if a reset/recovery cycle is pending or rebuild failed */
  1106. if (ice_is_reset_recovery_pending(pf->state) ||
  1107. test_bit(__ICE_SUSPENDED, pf->state) ||
  1108. test_bit(__ICE_NEEDS_RESTART, pf->state)) {
  1109. ice_service_task_complete(pf);
  1110. return;
  1111. }
  1112. ice_check_for_hang_subtask(pf);
  1113. ice_sync_fltr_subtask(pf);
  1114. ice_handle_mdd_event(pf);
  1115. ice_watchdog_subtask(pf);
  1116. ice_clean_adminq_subtask(pf);
  1117. /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
  1118. ice_service_task_complete(pf);
  1119. /* If the tasks have taken longer than one service timer period
  1120. * or there is more work to be done, reset the service timer to
  1121. * schedule the service task now.
  1122. */
  1123. if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
  1124. test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
  1125. test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
  1126. mod_timer(&pf->serv_tmr, jiffies);
  1127. }
  1128. /**
  1129. * ice_set_ctrlq_len - helper function to set controlq length
  1130. * @hw: pointer to the hw instance
  1131. */
  1132. static void ice_set_ctrlq_len(struct ice_hw *hw)
  1133. {
  1134. hw->adminq.num_rq_entries = ICE_AQ_LEN;
  1135. hw->adminq.num_sq_entries = ICE_AQ_LEN;
  1136. hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
  1137. hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
  1138. }
  1139. /**
  1140. * ice_irq_affinity_notify - Callback for affinity changes
  1141. * @notify: context as to what irq was changed
  1142. * @mask: the new affinity mask
  1143. *
  1144. * This is a callback function used by the irq_set_affinity_notifier function
  1145. * so that we may register to receive changes to the irq affinity masks.
  1146. */
  1147. static void ice_irq_affinity_notify(struct irq_affinity_notify *notify,
  1148. const cpumask_t *mask)
  1149. {
  1150. struct ice_q_vector *q_vector =
  1151. container_of(notify, struct ice_q_vector, affinity_notify);
  1152. cpumask_copy(&q_vector->affinity_mask, mask);
  1153. }
  1154. /**
  1155. * ice_irq_affinity_release - Callback for affinity notifier release
  1156. * @ref: internal core kernel usage
  1157. *
  1158. * This is a callback function used by the irq_set_affinity_notifier function
  1159. * to inform the current notification subscriber that they will no longer
  1160. * receive notifications.
  1161. */
  1162. static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
  1163. /**
  1164. * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
  1165. * @vsi: the VSI being un-configured
  1166. */
  1167. static void ice_vsi_dis_irq(struct ice_vsi *vsi)
  1168. {
  1169. struct ice_pf *pf = vsi->back;
  1170. struct ice_hw *hw = &pf->hw;
  1171. int base = vsi->base_vector;
  1172. u32 val;
  1173. int i;
  1174. /* disable interrupt causation from each queue */
  1175. if (vsi->tx_rings) {
  1176. ice_for_each_txq(vsi, i) {
  1177. if (vsi->tx_rings[i]) {
  1178. u16 reg;
  1179. reg = vsi->tx_rings[i]->reg_idx;
  1180. val = rd32(hw, QINT_TQCTL(reg));
  1181. val &= ~QINT_TQCTL_CAUSE_ENA_M;
  1182. wr32(hw, QINT_TQCTL(reg), val);
  1183. }
  1184. }
  1185. }
  1186. if (vsi->rx_rings) {
  1187. ice_for_each_rxq(vsi, i) {
  1188. if (vsi->rx_rings[i]) {
  1189. u16 reg;
  1190. reg = vsi->rx_rings[i]->reg_idx;
  1191. val = rd32(hw, QINT_RQCTL(reg));
  1192. val &= ~QINT_RQCTL_CAUSE_ENA_M;
  1193. wr32(hw, QINT_RQCTL(reg), val);
  1194. }
  1195. }
  1196. }
  1197. /* disable each interrupt */
  1198. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  1199. for (i = vsi->base_vector;
  1200. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  1201. wr32(hw, GLINT_DYN_CTL(i), 0);
  1202. ice_flush(hw);
  1203. for (i = 0; i < vsi->num_q_vectors; i++)
  1204. synchronize_irq(pf->msix_entries[i + base].vector);
  1205. }
  1206. }
  1207. /**
  1208. * ice_vsi_ena_irq - Enable IRQ for the given VSI
  1209. * @vsi: the VSI being configured
  1210. */
  1211. static int ice_vsi_ena_irq(struct ice_vsi *vsi)
  1212. {
  1213. struct ice_pf *pf = vsi->back;
  1214. struct ice_hw *hw = &pf->hw;
  1215. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  1216. int i;
  1217. for (i = 0; i < vsi->num_q_vectors; i++)
  1218. ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
  1219. }
  1220. ice_flush(hw);
  1221. return 0;
  1222. }
  1223. /**
  1224. * ice_vsi_delete - delete a VSI from the switch
  1225. * @vsi: pointer to VSI being removed
  1226. */
  1227. static void ice_vsi_delete(struct ice_vsi *vsi)
  1228. {
  1229. struct ice_pf *pf = vsi->back;
  1230. struct ice_vsi_ctx ctxt;
  1231. enum ice_status status;
  1232. ctxt.vsi_num = vsi->vsi_num;
  1233. memcpy(&ctxt.info, &vsi->info, sizeof(struct ice_aqc_vsi_props));
  1234. status = ice_free_vsi(&pf->hw, vsi->idx, &ctxt, false, NULL);
  1235. if (status)
  1236. dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
  1237. vsi->vsi_num);
  1238. }
  1239. /**
  1240. * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
  1241. * @vsi: the VSI being configured
  1242. * @basename: name for the vector
  1243. */
  1244. static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
  1245. {
  1246. int q_vectors = vsi->num_q_vectors;
  1247. struct ice_pf *pf = vsi->back;
  1248. int base = vsi->base_vector;
  1249. int rx_int_idx = 0;
  1250. int tx_int_idx = 0;
  1251. int vector, err;
  1252. int irq_num;
  1253. for (vector = 0; vector < q_vectors; vector++) {
  1254. struct ice_q_vector *q_vector = vsi->q_vectors[vector];
  1255. irq_num = pf->msix_entries[base + vector].vector;
  1256. if (q_vector->tx.ring && q_vector->rx.ring) {
  1257. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  1258. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  1259. tx_int_idx++;
  1260. } else if (q_vector->rx.ring) {
  1261. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  1262. "%s-%s-%d", basename, "rx", rx_int_idx++);
  1263. } else if (q_vector->tx.ring) {
  1264. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  1265. "%s-%s-%d", basename, "tx", tx_int_idx++);
  1266. } else {
  1267. /* skip this unused q_vector */
  1268. continue;
  1269. }
  1270. err = devm_request_irq(&pf->pdev->dev,
  1271. pf->msix_entries[base + vector].vector,
  1272. vsi->irq_handler, 0, q_vector->name,
  1273. q_vector);
  1274. if (err) {
  1275. netdev_err(vsi->netdev,
  1276. "MSIX request_irq failed, error: %d\n", err);
  1277. goto free_q_irqs;
  1278. }
  1279. /* register for affinity change notifications */
  1280. q_vector->affinity_notify.notify = ice_irq_affinity_notify;
  1281. q_vector->affinity_notify.release = ice_irq_affinity_release;
  1282. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  1283. /* assign the mask for this irq */
  1284. irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
  1285. }
  1286. vsi->irqs_ready = true;
  1287. return 0;
  1288. free_q_irqs:
  1289. while (vector) {
  1290. vector--;
  1291. irq_num = pf->msix_entries[base + vector].vector,
  1292. irq_set_affinity_notifier(irq_num, NULL);
  1293. irq_set_affinity_hint(irq_num, NULL);
  1294. devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
  1295. }
  1296. return err;
  1297. }
  1298. /**
  1299. * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
  1300. * @vsi: the VSI being configured
  1301. */
  1302. static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
  1303. {
  1304. struct ice_hw_common_caps *cap;
  1305. struct ice_pf *pf = vsi->back;
  1306. if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
  1307. vsi->rss_size = 1;
  1308. return;
  1309. }
  1310. cap = &pf->hw.func_caps.common_cap;
  1311. switch (vsi->type) {
  1312. case ICE_VSI_PF:
  1313. /* PF VSI will inherit RSS instance of PF */
  1314. vsi->rss_table_size = cap->rss_table_size;
  1315. vsi->rss_size = min_t(int, num_online_cpus(),
  1316. BIT(cap->rss_table_entry_width));
  1317. vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
  1318. break;
  1319. default:
  1320. dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
  1321. break;
  1322. }
  1323. }
  1324. /**
  1325. * ice_vsi_setup_q_map - Setup a VSI queue map
  1326. * @vsi: the VSI being configured
  1327. * @ctxt: VSI context structure
  1328. */
  1329. static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
  1330. {
  1331. u16 offset = 0, qmap = 0, numq_tc;
  1332. u16 pow = 0, max_rss = 0, qcount;
  1333. u16 qcount_tx = vsi->alloc_txq;
  1334. u16 qcount_rx = vsi->alloc_rxq;
  1335. bool ena_tc0 = false;
  1336. int i;
  1337. /* at least TC0 should be enabled by default */
  1338. if (vsi->tc_cfg.numtc) {
  1339. if (!(vsi->tc_cfg.ena_tc & BIT(0)))
  1340. ena_tc0 = true;
  1341. } else {
  1342. ena_tc0 = true;
  1343. }
  1344. if (ena_tc0) {
  1345. vsi->tc_cfg.numtc++;
  1346. vsi->tc_cfg.ena_tc |= 1;
  1347. }
  1348. numq_tc = qcount_rx / vsi->tc_cfg.numtc;
  1349. /* TC mapping is a function of the number of Rx queues assigned to the
  1350. * VSI for each traffic class and the offset of these queues.
  1351. * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
  1352. * queues allocated to TC0. No:of queues is a power-of-2.
  1353. *
  1354. * If TC is not enabled, the queue offset is set to 0, and allocate one
  1355. * queue, this way, traffic for the given TC will be sent to the default
  1356. * queue.
  1357. *
  1358. * Setup number and offset of Rx queues for all TCs for the VSI
  1359. */
  1360. /* qcount will change if RSS is enabled */
  1361. if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
  1362. if (vsi->type == ICE_VSI_PF)
  1363. max_rss = ICE_MAX_LG_RSS_QS;
  1364. else
  1365. max_rss = ICE_MAX_SMALL_RSS_QS;
  1366. qcount = min_t(int, numq_tc, max_rss);
  1367. qcount = min_t(int, qcount, vsi->rss_size);
  1368. } else {
  1369. qcount = numq_tc;
  1370. }
  1371. /* find the (rounded up) power-of-2 of qcount */
  1372. pow = order_base_2(qcount);
  1373. for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
  1374. if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
  1375. /* TC is not enabled */
  1376. vsi->tc_cfg.tc_info[i].qoffset = 0;
  1377. vsi->tc_cfg.tc_info[i].qcount = 1;
  1378. ctxt->info.tc_mapping[i] = 0;
  1379. continue;
  1380. }
  1381. /* TC is enabled */
  1382. vsi->tc_cfg.tc_info[i].qoffset = offset;
  1383. vsi->tc_cfg.tc_info[i].qcount = qcount;
  1384. qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
  1385. ICE_AQ_VSI_TC_Q_OFFSET_M) |
  1386. ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
  1387. ICE_AQ_VSI_TC_Q_NUM_M);
  1388. offset += qcount;
  1389. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1390. }
  1391. vsi->num_txq = qcount_tx;
  1392. vsi->num_rxq = offset;
  1393. /* Rx queue mapping */
  1394. ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
  1395. /* q_mapping buffer holds the info for the first queue allocated for
  1396. * this VSI in the PF space and also the number of queues associated
  1397. * with this VSI.
  1398. */
  1399. ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
  1400. ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
  1401. }
  1402. /**
  1403. * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
  1404. * @ctxt: the VSI context being set
  1405. *
  1406. * This initializes a default VSI context for all sections except the Queues.
  1407. */
  1408. static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
  1409. {
  1410. u32 table = 0;
  1411. memset(&ctxt->info, 0, sizeof(ctxt->info));
  1412. /* VSI's should be allocated from shared pool */
  1413. ctxt->alloc_from_pool = true;
  1414. /* Src pruning enabled by default */
  1415. ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
  1416. /* Traffic from VSI can be sent to LAN */
  1417. ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
  1418. /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
  1419. * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
  1420. * packets untagged/tagged.
  1421. */
  1422. ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
  1423. ICE_AQ_VSI_VLAN_MODE_M) >>
  1424. ICE_AQ_VSI_VLAN_MODE_S);
  1425. /* Have 1:1 UP mapping for both ingress/egress tables */
  1426. table |= ICE_UP_TABLE_TRANSLATE(0, 0);
  1427. table |= ICE_UP_TABLE_TRANSLATE(1, 1);
  1428. table |= ICE_UP_TABLE_TRANSLATE(2, 2);
  1429. table |= ICE_UP_TABLE_TRANSLATE(3, 3);
  1430. table |= ICE_UP_TABLE_TRANSLATE(4, 4);
  1431. table |= ICE_UP_TABLE_TRANSLATE(5, 5);
  1432. table |= ICE_UP_TABLE_TRANSLATE(6, 6);
  1433. table |= ICE_UP_TABLE_TRANSLATE(7, 7);
  1434. ctxt->info.ingress_table = cpu_to_le32(table);
  1435. ctxt->info.egress_table = cpu_to_le32(table);
  1436. /* Have 1:1 UP mapping for outer to inner UP table */
  1437. ctxt->info.outer_up_table = cpu_to_le32(table);
  1438. /* No Outer tag support outer_tag_flags remains to zero */
  1439. }
  1440. /**
  1441. * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
  1442. * @ctxt: the VSI context being set
  1443. * @vsi: the VSI being configured
  1444. */
  1445. static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
  1446. {
  1447. u8 lut_type, hash_type;
  1448. switch (vsi->type) {
  1449. case ICE_VSI_PF:
  1450. /* PF VSI will inherit RSS instance of PF */
  1451. lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
  1452. hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
  1453. break;
  1454. default:
  1455. dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
  1456. vsi->type);
  1457. return;
  1458. }
  1459. ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
  1460. ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
  1461. ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
  1462. ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
  1463. }
  1464. /**
  1465. * ice_vsi_init - Create and initialize a VSI
  1466. * @vsi: the VSI being configured
  1467. *
  1468. * This initializes a VSI context depending on the VSI type to be added and
  1469. * passes it down to the add_vsi aq command to create a new VSI.
  1470. */
  1471. static int ice_vsi_init(struct ice_vsi *vsi)
  1472. {
  1473. struct ice_vsi_ctx ctxt = { 0 };
  1474. struct ice_pf *pf = vsi->back;
  1475. struct ice_hw *hw = &pf->hw;
  1476. int ret = 0;
  1477. switch (vsi->type) {
  1478. case ICE_VSI_PF:
  1479. ctxt.flags = ICE_AQ_VSI_TYPE_PF;
  1480. break;
  1481. default:
  1482. return -ENODEV;
  1483. }
  1484. ice_set_dflt_vsi_ctx(&ctxt);
  1485. /* if the switch is in VEB mode, allow VSI loopback */
  1486. if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
  1487. ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
  1488. /* Set LUT type and HASH type if RSS is enabled */
  1489. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  1490. ice_set_rss_vsi_ctx(&ctxt, vsi);
  1491. ctxt.info.sw_id = vsi->port_info->sw_id;
  1492. ice_vsi_setup_q_map(vsi, &ctxt);
  1493. ret = ice_add_vsi(hw, vsi->idx, &ctxt, NULL);
  1494. if (ret) {
  1495. dev_err(&pf->pdev->dev,
  1496. "Add VSI failed, err %d\n", ret);
  1497. return -EIO;
  1498. }
  1499. /* keep context for update VSI operations */
  1500. vsi->info = ctxt.info;
  1501. /* record VSI number returned */
  1502. vsi->vsi_num = ctxt.vsi_num;
  1503. return ret;
  1504. }
  1505. /**
  1506. * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
  1507. * @vsi: the VSI being cleaned up
  1508. */
  1509. static void ice_vsi_release_msix(struct ice_vsi *vsi)
  1510. {
  1511. struct ice_pf *pf = vsi->back;
  1512. u16 vector = vsi->base_vector;
  1513. struct ice_hw *hw = &pf->hw;
  1514. u32 txq = 0;
  1515. u32 rxq = 0;
  1516. int i, q;
  1517. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  1518. struct ice_q_vector *q_vector = vsi->q_vectors[i];
  1519. wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
  1520. wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
  1521. for (q = 0; q < q_vector->num_ring_tx; q++) {
  1522. wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
  1523. txq++;
  1524. }
  1525. for (q = 0; q < q_vector->num_ring_rx; q++) {
  1526. wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
  1527. rxq++;
  1528. }
  1529. }
  1530. ice_flush(hw);
  1531. }
  1532. /**
  1533. * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
  1534. * @vsi: the VSI having rings deallocated
  1535. */
  1536. static void ice_vsi_clear_rings(struct ice_vsi *vsi)
  1537. {
  1538. int i;
  1539. if (vsi->tx_rings) {
  1540. for (i = 0; i < vsi->alloc_txq; i++) {
  1541. if (vsi->tx_rings[i]) {
  1542. kfree_rcu(vsi->tx_rings[i], rcu);
  1543. vsi->tx_rings[i] = NULL;
  1544. }
  1545. }
  1546. }
  1547. if (vsi->rx_rings) {
  1548. for (i = 0; i < vsi->alloc_rxq; i++) {
  1549. if (vsi->rx_rings[i]) {
  1550. kfree_rcu(vsi->rx_rings[i], rcu);
  1551. vsi->rx_rings[i] = NULL;
  1552. }
  1553. }
  1554. }
  1555. }
  1556. /**
  1557. * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
  1558. * @vsi: VSI which is having rings allocated
  1559. */
  1560. static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
  1561. {
  1562. struct ice_pf *pf = vsi->back;
  1563. int i;
  1564. /* Allocate tx_rings */
  1565. for (i = 0; i < vsi->alloc_txq; i++) {
  1566. struct ice_ring *ring;
  1567. /* allocate with kzalloc(), free with kfree_rcu() */
  1568. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  1569. if (!ring)
  1570. goto err_out;
  1571. ring->q_index = i;
  1572. ring->reg_idx = vsi->txq_map[i];
  1573. ring->ring_active = false;
  1574. ring->vsi = vsi;
  1575. ring->netdev = vsi->netdev;
  1576. ring->dev = &pf->pdev->dev;
  1577. ring->count = vsi->num_desc;
  1578. vsi->tx_rings[i] = ring;
  1579. }
  1580. /* Allocate rx_rings */
  1581. for (i = 0; i < vsi->alloc_rxq; i++) {
  1582. struct ice_ring *ring;
  1583. /* allocate with kzalloc(), free with kfree_rcu() */
  1584. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  1585. if (!ring)
  1586. goto err_out;
  1587. ring->q_index = i;
  1588. ring->reg_idx = vsi->rxq_map[i];
  1589. ring->ring_active = false;
  1590. ring->vsi = vsi;
  1591. ring->netdev = vsi->netdev;
  1592. ring->dev = &pf->pdev->dev;
  1593. ring->count = vsi->num_desc;
  1594. vsi->rx_rings[i] = ring;
  1595. }
  1596. return 0;
  1597. err_out:
  1598. ice_vsi_clear_rings(vsi);
  1599. return -ENOMEM;
  1600. }
  1601. /**
  1602. * ice_vsi_free_irq - Free the irq association with the OS
  1603. * @vsi: the VSI being configured
  1604. */
  1605. static void ice_vsi_free_irq(struct ice_vsi *vsi)
  1606. {
  1607. struct ice_pf *pf = vsi->back;
  1608. int base = vsi->base_vector;
  1609. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  1610. int i;
  1611. if (!vsi->q_vectors || !vsi->irqs_ready)
  1612. return;
  1613. vsi->irqs_ready = false;
  1614. for (i = 0; i < vsi->num_q_vectors; i++) {
  1615. u16 vector = i + base;
  1616. int irq_num;
  1617. irq_num = pf->msix_entries[vector].vector;
  1618. /* free only the irqs that were actually requested */
  1619. if (!vsi->q_vectors[i] ||
  1620. !(vsi->q_vectors[i]->num_ring_tx ||
  1621. vsi->q_vectors[i]->num_ring_rx))
  1622. continue;
  1623. /* clear the affinity notifier in the IRQ descriptor */
  1624. irq_set_affinity_notifier(irq_num, NULL);
  1625. /* clear the affinity_mask in the IRQ descriptor */
  1626. irq_set_affinity_hint(irq_num, NULL);
  1627. synchronize_irq(irq_num);
  1628. devm_free_irq(&pf->pdev->dev, irq_num,
  1629. vsi->q_vectors[i]);
  1630. }
  1631. ice_vsi_release_msix(vsi);
  1632. }
  1633. }
  1634. /**
  1635. * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
  1636. * @vsi: the VSI being configured
  1637. */
  1638. static void ice_vsi_cfg_msix(struct ice_vsi *vsi)
  1639. {
  1640. struct ice_pf *pf = vsi->back;
  1641. u16 vector = vsi->base_vector;
  1642. struct ice_hw *hw = &pf->hw;
  1643. u32 txq = 0, rxq = 0;
  1644. int i, q, itr;
  1645. u8 itr_gran;
  1646. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  1647. struct ice_q_vector *q_vector = vsi->q_vectors[i];
  1648. itr_gran = hw->itr_gran_200;
  1649. if (q_vector->num_ring_rx) {
  1650. q_vector->rx.itr =
  1651. ITR_TO_REG(vsi->rx_rings[rxq]->rx_itr_setting,
  1652. itr_gran);
  1653. q_vector->rx.latency_range = ICE_LOW_LATENCY;
  1654. }
  1655. if (q_vector->num_ring_tx) {
  1656. q_vector->tx.itr =
  1657. ITR_TO_REG(vsi->tx_rings[txq]->tx_itr_setting,
  1658. itr_gran);
  1659. q_vector->tx.latency_range = ICE_LOW_LATENCY;
  1660. }
  1661. wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), q_vector->rx.itr);
  1662. wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), q_vector->tx.itr);
  1663. /* Both Transmit Queue Interrupt Cause Control register
  1664. * and Receive Queue Interrupt Cause control register
  1665. * expects MSIX_INDX field to be the vector index
  1666. * within the function space and not the absolute
  1667. * vector index across PF or across device.
  1668. * For SR-IOV VF VSIs queue vector index always starts
  1669. * with 1 since first vector index(0) is used for OICR
  1670. * in VF space. Since VMDq and other PF VSIs are withtin
  1671. * the PF function space, use the vector index thats
  1672. * tracked for this PF.
  1673. */
  1674. for (q = 0; q < q_vector->num_ring_tx; q++) {
  1675. u32 val;
  1676. itr = ICE_TX_ITR;
  1677. val = QINT_TQCTL_CAUSE_ENA_M |
  1678. (itr << QINT_TQCTL_ITR_INDX_S) |
  1679. (vector << QINT_TQCTL_MSIX_INDX_S);
  1680. wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
  1681. txq++;
  1682. }
  1683. for (q = 0; q < q_vector->num_ring_rx; q++) {
  1684. u32 val;
  1685. itr = ICE_RX_ITR;
  1686. val = QINT_RQCTL_CAUSE_ENA_M |
  1687. (itr << QINT_RQCTL_ITR_INDX_S) |
  1688. (vector << QINT_RQCTL_MSIX_INDX_S);
  1689. wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
  1690. rxq++;
  1691. }
  1692. }
  1693. ice_flush(hw);
  1694. }
  1695. /**
  1696. * ice_ena_misc_vector - enable the non-queue interrupts
  1697. * @pf: board private structure
  1698. */
  1699. static void ice_ena_misc_vector(struct ice_pf *pf)
  1700. {
  1701. struct ice_hw *hw = &pf->hw;
  1702. u32 val;
  1703. /* clear things first */
  1704. wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
  1705. rd32(hw, PFINT_OICR); /* read to clear */
  1706. val = (PFINT_OICR_ECC_ERR_M |
  1707. PFINT_OICR_MAL_DETECT_M |
  1708. PFINT_OICR_GRST_M |
  1709. PFINT_OICR_PCI_EXCEPTION_M |
  1710. PFINT_OICR_HMC_ERR_M |
  1711. PFINT_OICR_PE_CRITERR_M);
  1712. wr32(hw, PFINT_OICR_ENA, val);
  1713. /* SW_ITR_IDX = 0, but don't change INTENA */
  1714. wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
  1715. GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
  1716. }
  1717. /**
  1718. * ice_misc_intr - misc interrupt handler
  1719. * @irq: interrupt number
  1720. * @data: pointer to a q_vector
  1721. */
  1722. static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
  1723. {
  1724. struct ice_pf *pf = (struct ice_pf *)data;
  1725. struct ice_hw *hw = &pf->hw;
  1726. irqreturn_t ret = IRQ_NONE;
  1727. u32 oicr, ena_mask;
  1728. set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
  1729. oicr = rd32(hw, PFINT_OICR);
  1730. ena_mask = rd32(hw, PFINT_OICR_ENA);
  1731. if (oicr & PFINT_OICR_MAL_DETECT_M) {
  1732. ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
  1733. set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
  1734. }
  1735. if (oicr & PFINT_OICR_GRST_M) {
  1736. u32 reset;
  1737. /* we have a reset warning */
  1738. ena_mask &= ~PFINT_OICR_GRST_M;
  1739. reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
  1740. GLGEN_RSTAT_RESET_TYPE_S;
  1741. if (reset == ICE_RESET_CORER)
  1742. pf->corer_count++;
  1743. else if (reset == ICE_RESET_GLOBR)
  1744. pf->globr_count++;
  1745. else
  1746. pf->empr_count++;
  1747. /* If a reset cycle isn't already in progress, we set a bit in
  1748. * pf->state so that the service task can start a reset/rebuild.
  1749. * We also make note of which reset happened so that peer
  1750. * devices/drivers can be informed.
  1751. */
  1752. if (!test_and_set_bit(__ICE_RESET_RECOVERY_PENDING,
  1753. pf->state)) {
  1754. if (reset == ICE_RESET_CORER)
  1755. set_bit(__ICE_CORER_RECV, pf->state);
  1756. else if (reset == ICE_RESET_GLOBR)
  1757. set_bit(__ICE_GLOBR_RECV, pf->state);
  1758. else
  1759. set_bit(__ICE_EMPR_RECV, pf->state);
  1760. /* There are couple of different bits at play here.
  1761. * hw->reset_ongoing indicates whether the hardware is
  1762. * in reset. This is set to true when a reset interrupt
  1763. * is received and set back to false after the driver
  1764. * has determined that the hardware is out of reset.
  1765. *
  1766. * __ICE_RESET_RECOVERY_PENDING in pf->state indicates
  1767. * that a post reset rebuild is required before the
  1768. * driver is operational again. This is set above.
  1769. *
  1770. * As this is the start of the reset/rebuild cycle, set
  1771. * both to indicate that.
  1772. */
  1773. hw->reset_ongoing = true;
  1774. }
  1775. }
  1776. if (oicr & PFINT_OICR_HMC_ERR_M) {
  1777. ena_mask &= ~PFINT_OICR_HMC_ERR_M;
  1778. dev_dbg(&pf->pdev->dev,
  1779. "HMC Error interrupt - info 0x%x, data 0x%x\n",
  1780. rd32(hw, PFHMC_ERRORINFO),
  1781. rd32(hw, PFHMC_ERRORDATA));
  1782. }
  1783. /* Report and mask off any remaining unexpected interrupts */
  1784. oicr &= ena_mask;
  1785. if (oicr) {
  1786. dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
  1787. oicr);
  1788. /* If a critical error is pending there is no choice but to
  1789. * reset the device.
  1790. */
  1791. if (oicr & (PFINT_OICR_PE_CRITERR_M |
  1792. PFINT_OICR_PCI_EXCEPTION_M |
  1793. PFINT_OICR_ECC_ERR_M)) {
  1794. set_bit(__ICE_PFR_REQ, pf->state);
  1795. ice_service_task_schedule(pf);
  1796. }
  1797. ena_mask &= ~oicr;
  1798. }
  1799. ret = IRQ_HANDLED;
  1800. /* re-enable interrupt causes that are not handled during this pass */
  1801. wr32(hw, PFINT_OICR_ENA, ena_mask);
  1802. if (!test_bit(__ICE_DOWN, pf->state)) {
  1803. ice_service_task_schedule(pf);
  1804. ice_irq_dynamic_ena(hw, NULL, NULL);
  1805. }
  1806. return ret;
  1807. }
  1808. /**
  1809. * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
  1810. * @vsi: the VSI being configured
  1811. *
  1812. * This function maps descriptor rings to the queue-specific vectors allotted
  1813. * through the MSI-X enabling code. On a constrained vector budget, we map Tx
  1814. * and Rx rings to the vector as "efficiently" as possible.
  1815. */
  1816. static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
  1817. {
  1818. int q_vectors = vsi->num_q_vectors;
  1819. int tx_rings_rem, rx_rings_rem;
  1820. int v_id;
  1821. /* initially assigning remaining rings count to VSIs num queue value */
  1822. tx_rings_rem = vsi->num_txq;
  1823. rx_rings_rem = vsi->num_rxq;
  1824. for (v_id = 0; v_id < q_vectors; v_id++) {
  1825. struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
  1826. int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
  1827. /* Tx rings mapping to vector */
  1828. tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
  1829. q_vector->num_ring_tx = tx_rings_per_v;
  1830. q_vector->tx.ring = NULL;
  1831. q_base = vsi->num_txq - tx_rings_rem;
  1832. for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
  1833. struct ice_ring *tx_ring = vsi->tx_rings[q_id];
  1834. tx_ring->q_vector = q_vector;
  1835. tx_ring->next = q_vector->tx.ring;
  1836. q_vector->tx.ring = tx_ring;
  1837. }
  1838. tx_rings_rem -= tx_rings_per_v;
  1839. /* Rx rings mapping to vector */
  1840. rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
  1841. q_vector->num_ring_rx = rx_rings_per_v;
  1842. q_vector->rx.ring = NULL;
  1843. q_base = vsi->num_rxq - rx_rings_rem;
  1844. for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
  1845. struct ice_ring *rx_ring = vsi->rx_rings[q_id];
  1846. rx_ring->q_vector = q_vector;
  1847. rx_ring->next = q_vector->rx.ring;
  1848. q_vector->rx.ring = rx_ring;
  1849. }
  1850. rx_rings_rem -= rx_rings_per_v;
  1851. }
  1852. }
  1853. /**
  1854. * ice_vsi_set_num_qs - Set num queues, descriptors and vectors for a VSI
  1855. * @vsi: the VSI being configured
  1856. *
  1857. * Return 0 on success and a negative value on error
  1858. */
  1859. static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
  1860. {
  1861. struct ice_pf *pf = vsi->back;
  1862. switch (vsi->type) {
  1863. case ICE_VSI_PF:
  1864. vsi->alloc_txq = pf->num_lan_tx;
  1865. vsi->alloc_rxq = pf->num_lan_rx;
  1866. vsi->num_desc = ALIGN(ICE_DFLT_NUM_DESC, ICE_REQ_DESC_MULTIPLE);
  1867. vsi->num_q_vectors = max_t(int, pf->num_lan_rx, pf->num_lan_tx);
  1868. break;
  1869. default:
  1870. dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
  1871. vsi->type);
  1872. break;
  1873. }
  1874. }
  1875. /**
  1876. * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  1877. * @vsi: VSI pointer
  1878. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  1879. *
  1880. * On error: returns error code (negative)
  1881. * On success: returns 0
  1882. */
  1883. static int ice_vsi_alloc_arrays(struct ice_vsi *vsi, bool alloc_qvectors)
  1884. {
  1885. struct ice_pf *pf = vsi->back;
  1886. /* allocate memory for both Tx and Rx ring pointers */
  1887. vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
  1888. sizeof(struct ice_ring *), GFP_KERNEL);
  1889. if (!vsi->tx_rings)
  1890. goto err_txrings;
  1891. vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
  1892. sizeof(struct ice_ring *), GFP_KERNEL);
  1893. if (!vsi->rx_rings)
  1894. goto err_rxrings;
  1895. if (alloc_qvectors) {
  1896. /* allocate memory for q_vector pointers */
  1897. vsi->q_vectors = devm_kcalloc(&pf->pdev->dev,
  1898. vsi->num_q_vectors,
  1899. sizeof(struct ice_q_vector *),
  1900. GFP_KERNEL);
  1901. if (!vsi->q_vectors)
  1902. goto err_vectors;
  1903. }
  1904. return 0;
  1905. err_vectors:
  1906. devm_kfree(&pf->pdev->dev, vsi->rx_rings);
  1907. err_rxrings:
  1908. devm_kfree(&pf->pdev->dev, vsi->tx_rings);
  1909. err_txrings:
  1910. return -ENOMEM;
  1911. }
  1912. /**
  1913. * ice_msix_clean_rings - MSIX mode Interrupt Handler
  1914. * @irq: interrupt number
  1915. * @data: pointer to a q_vector
  1916. */
  1917. static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
  1918. {
  1919. struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
  1920. if (!q_vector->tx.ring && !q_vector->rx.ring)
  1921. return IRQ_HANDLED;
  1922. napi_schedule(&q_vector->napi);
  1923. return IRQ_HANDLED;
  1924. }
  1925. /**
  1926. * ice_vsi_alloc - Allocates the next available struct vsi in the PF
  1927. * @pf: board private structure
  1928. * @type: type of VSI
  1929. *
  1930. * returns a pointer to a VSI on success, NULL on failure.
  1931. */
  1932. static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type)
  1933. {
  1934. struct ice_vsi *vsi = NULL;
  1935. /* Need to protect the allocation of the VSIs at the PF level */
  1936. mutex_lock(&pf->sw_mutex);
  1937. /* If we have already allocated our maximum number of VSIs,
  1938. * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
  1939. * is available to be populated
  1940. */
  1941. if (pf->next_vsi == ICE_NO_VSI) {
  1942. dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
  1943. goto unlock_pf;
  1944. }
  1945. vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
  1946. if (!vsi)
  1947. goto unlock_pf;
  1948. vsi->type = type;
  1949. vsi->back = pf;
  1950. set_bit(__ICE_DOWN, vsi->state);
  1951. vsi->idx = pf->next_vsi;
  1952. vsi->work_lmt = ICE_DFLT_IRQ_WORK;
  1953. ice_vsi_set_num_qs(vsi);
  1954. switch (vsi->type) {
  1955. case ICE_VSI_PF:
  1956. if (ice_vsi_alloc_arrays(vsi, true))
  1957. goto err_rings;
  1958. /* Setup default MSIX irq handler for VSI */
  1959. vsi->irq_handler = ice_msix_clean_rings;
  1960. break;
  1961. default:
  1962. dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
  1963. goto unlock_pf;
  1964. }
  1965. /* fill VSI slot in the PF struct */
  1966. pf->vsi[pf->next_vsi] = vsi;
  1967. /* prepare pf->next_vsi for next use */
  1968. pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
  1969. pf->next_vsi);
  1970. goto unlock_pf;
  1971. err_rings:
  1972. devm_kfree(&pf->pdev->dev, vsi);
  1973. vsi = NULL;
  1974. unlock_pf:
  1975. mutex_unlock(&pf->sw_mutex);
  1976. return vsi;
  1977. }
  1978. /**
  1979. * ice_free_irq_msix_misc - Unroll misc vector setup
  1980. * @pf: board private structure
  1981. */
  1982. static void ice_free_irq_msix_misc(struct ice_pf *pf)
  1983. {
  1984. /* disable OICR interrupt */
  1985. wr32(&pf->hw, PFINT_OICR_ENA, 0);
  1986. ice_flush(&pf->hw);
  1987. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
  1988. synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
  1989. devm_free_irq(&pf->pdev->dev,
  1990. pf->msix_entries[pf->oicr_idx].vector, pf);
  1991. }
  1992. ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
  1993. }
  1994. /**
  1995. * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
  1996. * @pf: board private structure
  1997. *
  1998. * This sets up the handler for MSIX 0, which is used to manage the
  1999. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  2000. * when in MSI or Legacy interrupt mode.
  2001. */
  2002. static int ice_req_irq_msix_misc(struct ice_pf *pf)
  2003. {
  2004. struct ice_hw *hw = &pf->hw;
  2005. int oicr_idx, err = 0;
  2006. u8 itr_gran;
  2007. u32 val;
  2008. if (!pf->int_name[0])
  2009. snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
  2010. dev_driver_string(&pf->pdev->dev),
  2011. dev_name(&pf->pdev->dev));
  2012. /* Do not request IRQ but do enable OICR interrupt since settings are
  2013. * lost during reset. Note that this function is called only during
  2014. * rebuild path and not while reset is in progress.
  2015. */
  2016. if (ice_is_reset_recovery_pending(pf->state))
  2017. goto skip_req_irq;
  2018. /* reserve one vector in irq_tracker for misc interrupts */
  2019. oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
  2020. if (oicr_idx < 0)
  2021. return oicr_idx;
  2022. pf->oicr_idx = oicr_idx;
  2023. err = devm_request_irq(&pf->pdev->dev,
  2024. pf->msix_entries[pf->oicr_idx].vector,
  2025. ice_misc_intr, 0, pf->int_name, pf);
  2026. if (err) {
  2027. dev_err(&pf->pdev->dev,
  2028. "devm_request_irq for %s failed: %d\n",
  2029. pf->int_name, err);
  2030. ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
  2031. return err;
  2032. }
  2033. skip_req_irq:
  2034. ice_ena_misc_vector(pf);
  2035. val = ((pf->oicr_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
  2036. PFINT_OICR_CTL_CAUSE_ENA_M);
  2037. wr32(hw, PFINT_OICR_CTL, val);
  2038. /* This enables Admin queue Interrupt causes */
  2039. val = ((pf->oicr_idx & PFINT_FW_CTL_MSIX_INDX_M) |
  2040. PFINT_FW_CTL_CAUSE_ENA_M);
  2041. wr32(hw, PFINT_FW_CTL, val);
  2042. itr_gran = hw->itr_gran_200;
  2043. wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
  2044. ITR_TO_REG(ICE_ITR_8K, itr_gran));
  2045. ice_flush(hw);
  2046. ice_irq_dynamic_ena(hw, NULL, NULL);
  2047. return 0;
  2048. }
  2049. /**
  2050. * ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
  2051. * @vsi: the VSI getting queues
  2052. *
  2053. * Return 0 on success and a negative value on error
  2054. */
  2055. static int ice_vsi_get_qs_contig(struct ice_vsi *vsi)
  2056. {
  2057. struct ice_pf *pf = vsi->back;
  2058. int offset, ret = 0;
  2059. mutex_lock(&pf->avail_q_mutex);
  2060. /* look for contiguous block of queues for tx */
  2061. offset = bitmap_find_next_zero_area(pf->avail_txqs, ICE_MAX_TXQS,
  2062. 0, vsi->alloc_txq, 0);
  2063. if (offset < ICE_MAX_TXQS) {
  2064. int i;
  2065. bitmap_set(pf->avail_txqs, offset, vsi->alloc_txq);
  2066. for (i = 0; i < vsi->alloc_txq; i++)
  2067. vsi->txq_map[i] = i + offset;
  2068. } else {
  2069. ret = -ENOMEM;
  2070. vsi->tx_mapping_mode = ICE_VSI_MAP_SCATTER;
  2071. }
  2072. /* look for contiguous block of queues for rx */
  2073. offset = bitmap_find_next_zero_area(pf->avail_rxqs, ICE_MAX_RXQS,
  2074. 0, vsi->alloc_rxq, 0);
  2075. if (offset < ICE_MAX_RXQS) {
  2076. int i;
  2077. bitmap_set(pf->avail_rxqs, offset, vsi->alloc_rxq);
  2078. for (i = 0; i < vsi->alloc_rxq; i++)
  2079. vsi->rxq_map[i] = i + offset;
  2080. } else {
  2081. ret = -ENOMEM;
  2082. vsi->rx_mapping_mode = ICE_VSI_MAP_SCATTER;
  2083. }
  2084. mutex_unlock(&pf->avail_q_mutex);
  2085. return ret;
  2086. }
  2087. /**
  2088. * ice_vsi_get_qs_scatter - Assign a scattered queues to VSI
  2089. * @vsi: the VSI getting queues
  2090. *
  2091. * Return 0 on success and a negative value on error
  2092. */
  2093. static int ice_vsi_get_qs_scatter(struct ice_vsi *vsi)
  2094. {
  2095. struct ice_pf *pf = vsi->back;
  2096. int i, index = 0;
  2097. mutex_lock(&pf->avail_q_mutex);
  2098. if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER) {
  2099. for (i = 0; i < vsi->alloc_txq; i++) {
  2100. index = find_next_zero_bit(pf->avail_txqs,
  2101. ICE_MAX_TXQS, index);
  2102. if (index < ICE_MAX_TXQS) {
  2103. set_bit(index, pf->avail_txqs);
  2104. vsi->txq_map[i] = index;
  2105. } else {
  2106. goto err_scatter_tx;
  2107. }
  2108. }
  2109. }
  2110. if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER) {
  2111. for (i = 0; i < vsi->alloc_rxq; i++) {
  2112. index = find_next_zero_bit(pf->avail_rxqs,
  2113. ICE_MAX_RXQS, index);
  2114. if (index < ICE_MAX_RXQS) {
  2115. set_bit(index, pf->avail_rxqs);
  2116. vsi->rxq_map[i] = index;
  2117. } else {
  2118. goto err_scatter_rx;
  2119. }
  2120. }
  2121. }
  2122. mutex_unlock(&pf->avail_q_mutex);
  2123. return 0;
  2124. err_scatter_rx:
  2125. /* unflag any queues we have grabbed (i is failed position) */
  2126. for (index = 0; index < i; index++) {
  2127. clear_bit(vsi->rxq_map[index], pf->avail_rxqs);
  2128. vsi->rxq_map[index] = 0;
  2129. }
  2130. i = vsi->alloc_txq;
  2131. err_scatter_tx:
  2132. /* i is either position of failed attempt or vsi->alloc_txq */
  2133. for (index = 0; index < i; index++) {
  2134. clear_bit(vsi->txq_map[index], pf->avail_txqs);
  2135. vsi->txq_map[index] = 0;
  2136. }
  2137. mutex_unlock(&pf->avail_q_mutex);
  2138. return -ENOMEM;
  2139. }
  2140. /**
  2141. * ice_vsi_get_qs - Assign queues from PF to VSI
  2142. * @vsi: the VSI to assign queues to
  2143. *
  2144. * Returns 0 on success and a negative value on error
  2145. */
  2146. static int ice_vsi_get_qs(struct ice_vsi *vsi)
  2147. {
  2148. int ret = 0;
  2149. vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
  2150. vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
  2151. /* NOTE: ice_vsi_get_qs_contig() will set the rx/tx mapping
  2152. * modes individually to scatter if assigning contiguous queues
  2153. * to rx or tx fails
  2154. */
  2155. ret = ice_vsi_get_qs_contig(vsi);
  2156. if (ret < 0) {
  2157. if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER)
  2158. vsi->alloc_txq = max_t(u16, vsi->alloc_txq,
  2159. ICE_MAX_SCATTER_TXQS);
  2160. if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER)
  2161. vsi->alloc_rxq = max_t(u16, vsi->alloc_rxq,
  2162. ICE_MAX_SCATTER_RXQS);
  2163. ret = ice_vsi_get_qs_scatter(vsi);
  2164. }
  2165. return ret;
  2166. }
  2167. /**
  2168. * ice_vsi_put_qs - Release queues from VSI to PF
  2169. * @vsi: the VSI thats going to release queues
  2170. */
  2171. static void ice_vsi_put_qs(struct ice_vsi *vsi)
  2172. {
  2173. struct ice_pf *pf = vsi->back;
  2174. int i;
  2175. mutex_lock(&pf->avail_q_mutex);
  2176. for (i = 0; i < vsi->alloc_txq; i++) {
  2177. clear_bit(vsi->txq_map[i], pf->avail_txqs);
  2178. vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
  2179. }
  2180. for (i = 0; i < vsi->alloc_rxq; i++) {
  2181. clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
  2182. vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
  2183. }
  2184. mutex_unlock(&pf->avail_q_mutex);
  2185. }
  2186. /**
  2187. * ice_free_q_vector - Free memory allocated for a specific interrupt vector
  2188. * @vsi: VSI having the memory freed
  2189. * @v_idx: index of the vector to be freed
  2190. */
  2191. static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
  2192. {
  2193. struct ice_q_vector *q_vector;
  2194. struct ice_ring *ring;
  2195. if (!vsi->q_vectors[v_idx]) {
  2196. dev_dbg(&vsi->back->pdev->dev, "Queue vector at index %d not found\n",
  2197. v_idx);
  2198. return;
  2199. }
  2200. q_vector = vsi->q_vectors[v_idx];
  2201. ice_for_each_ring(ring, q_vector->tx)
  2202. ring->q_vector = NULL;
  2203. ice_for_each_ring(ring, q_vector->rx)
  2204. ring->q_vector = NULL;
  2205. /* only VSI with an associated netdev is set up with NAPI */
  2206. if (vsi->netdev)
  2207. netif_napi_del(&q_vector->napi);
  2208. devm_kfree(&vsi->back->pdev->dev, q_vector);
  2209. vsi->q_vectors[v_idx] = NULL;
  2210. }
  2211. /**
  2212. * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  2213. * @vsi: the VSI having memory freed
  2214. */
  2215. static void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
  2216. {
  2217. int v_idx;
  2218. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  2219. ice_free_q_vector(vsi, v_idx);
  2220. }
  2221. /**
  2222. * ice_cfg_netdev - Setup the netdev flags
  2223. * @vsi: the VSI being configured
  2224. *
  2225. * Returns 0 on success, negative value on failure
  2226. */
  2227. static int ice_cfg_netdev(struct ice_vsi *vsi)
  2228. {
  2229. netdev_features_t csumo_features;
  2230. netdev_features_t vlano_features;
  2231. netdev_features_t dflt_features;
  2232. netdev_features_t tso_features;
  2233. struct ice_netdev_priv *np;
  2234. struct net_device *netdev;
  2235. u8 mac_addr[ETH_ALEN];
  2236. netdev = alloc_etherdev_mqs(sizeof(struct ice_netdev_priv),
  2237. vsi->alloc_txq, vsi->alloc_rxq);
  2238. if (!netdev)
  2239. return -ENOMEM;
  2240. vsi->netdev = netdev;
  2241. np = netdev_priv(netdev);
  2242. np->vsi = vsi;
  2243. dflt_features = NETIF_F_SG |
  2244. NETIF_F_HIGHDMA |
  2245. NETIF_F_RXHASH;
  2246. csumo_features = NETIF_F_RXCSUM |
  2247. NETIF_F_IP_CSUM |
  2248. NETIF_F_IPV6_CSUM;
  2249. vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
  2250. NETIF_F_HW_VLAN_CTAG_TX |
  2251. NETIF_F_HW_VLAN_CTAG_RX;
  2252. tso_features = NETIF_F_TSO;
  2253. /* set features that user can change */
  2254. netdev->hw_features = dflt_features | csumo_features |
  2255. vlano_features | tso_features;
  2256. /* enable features */
  2257. netdev->features |= netdev->hw_features;
  2258. /* encap and VLAN devices inherit default, csumo and tso features */
  2259. netdev->hw_enc_features |= dflt_features | csumo_features |
  2260. tso_features;
  2261. netdev->vlan_features |= dflt_features | csumo_features |
  2262. tso_features;
  2263. if (vsi->type == ICE_VSI_PF) {
  2264. SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
  2265. ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
  2266. ether_addr_copy(netdev->dev_addr, mac_addr);
  2267. ether_addr_copy(netdev->perm_addr, mac_addr);
  2268. }
  2269. netdev->priv_flags |= IFF_UNICAST_FLT;
  2270. /* assign netdev_ops */
  2271. netdev->netdev_ops = &ice_netdev_ops;
  2272. /* setup watchdog timeout value to be 5 second */
  2273. netdev->watchdog_timeo = 5 * HZ;
  2274. ice_set_ethtool_ops(netdev);
  2275. netdev->min_mtu = ETH_MIN_MTU;
  2276. netdev->max_mtu = ICE_MAX_MTU;
  2277. return 0;
  2278. }
  2279. /**
  2280. * ice_vsi_free_arrays - clean up vsi resources
  2281. * @vsi: pointer to VSI being cleared
  2282. * @free_qvectors: bool to specify if q_vectors should be deallocated
  2283. */
  2284. static void ice_vsi_free_arrays(struct ice_vsi *vsi, bool free_qvectors)
  2285. {
  2286. struct ice_pf *pf = vsi->back;
  2287. /* free the ring and vector containers */
  2288. if (free_qvectors && vsi->q_vectors) {
  2289. devm_kfree(&pf->pdev->dev, vsi->q_vectors);
  2290. vsi->q_vectors = NULL;
  2291. }
  2292. if (vsi->tx_rings) {
  2293. devm_kfree(&pf->pdev->dev, vsi->tx_rings);
  2294. vsi->tx_rings = NULL;
  2295. }
  2296. if (vsi->rx_rings) {
  2297. devm_kfree(&pf->pdev->dev, vsi->rx_rings);
  2298. vsi->rx_rings = NULL;
  2299. }
  2300. }
  2301. /**
  2302. * ice_vsi_clear - clean up and deallocate the provided vsi
  2303. * @vsi: pointer to VSI being cleared
  2304. *
  2305. * This deallocates the vsi's queue resources, removes it from the PF's
  2306. * VSI array if necessary, and deallocates the VSI
  2307. *
  2308. * Returns 0 on success, negative on failure
  2309. */
  2310. static int ice_vsi_clear(struct ice_vsi *vsi)
  2311. {
  2312. struct ice_pf *pf = NULL;
  2313. if (!vsi)
  2314. return 0;
  2315. if (!vsi->back)
  2316. return -EINVAL;
  2317. pf = vsi->back;
  2318. if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
  2319. dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
  2320. vsi->idx);
  2321. return -EINVAL;
  2322. }
  2323. mutex_lock(&pf->sw_mutex);
  2324. /* updates the PF for this cleared vsi */
  2325. pf->vsi[vsi->idx] = NULL;
  2326. if (vsi->idx < pf->next_vsi)
  2327. pf->next_vsi = vsi->idx;
  2328. ice_vsi_free_arrays(vsi, true);
  2329. mutex_unlock(&pf->sw_mutex);
  2330. devm_kfree(&pf->pdev->dev, vsi);
  2331. return 0;
  2332. }
  2333. /**
  2334. * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  2335. * @vsi: the VSI being configured
  2336. * @v_idx: index of the vector in the vsi struct
  2337. *
  2338. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  2339. */
  2340. static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, int v_idx)
  2341. {
  2342. struct ice_pf *pf = vsi->back;
  2343. struct ice_q_vector *q_vector;
  2344. /* allocate q_vector */
  2345. q_vector = devm_kzalloc(&pf->pdev->dev, sizeof(*q_vector), GFP_KERNEL);
  2346. if (!q_vector)
  2347. return -ENOMEM;
  2348. q_vector->vsi = vsi;
  2349. q_vector->v_idx = v_idx;
  2350. /* only set affinity_mask if the CPU is online */
  2351. if (cpu_online(v_idx))
  2352. cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
  2353. if (vsi->netdev)
  2354. netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
  2355. NAPI_POLL_WEIGHT);
  2356. /* tie q_vector and vsi together */
  2357. vsi->q_vectors[v_idx] = q_vector;
  2358. return 0;
  2359. }
  2360. /**
  2361. * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  2362. * @vsi: the VSI being configured
  2363. *
  2364. * We allocate one q_vector per queue interrupt. If allocation fails we
  2365. * return -ENOMEM.
  2366. */
  2367. static int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
  2368. {
  2369. struct ice_pf *pf = vsi->back;
  2370. int v_idx = 0, num_q_vectors;
  2371. int err;
  2372. if (vsi->q_vectors[0]) {
  2373. dev_dbg(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  2374. vsi->vsi_num);
  2375. return -EEXIST;
  2376. }
  2377. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  2378. num_q_vectors = vsi->num_q_vectors;
  2379. } else {
  2380. err = -EINVAL;
  2381. goto err_out;
  2382. }
  2383. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  2384. err = ice_vsi_alloc_q_vector(vsi, v_idx);
  2385. if (err)
  2386. goto err_out;
  2387. }
  2388. return 0;
  2389. err_out:
  2390. while (v_idx--)
  2391. ice_free_q_vector(vsi, v_idx);
  2392. dev_err(&pf->pdev->dev,
  2393. "Failed to allocate %d q_vector for VSI %d, ret=%d\n",
  2394. vsi->num_q_vectors, vsi->vsi_num, err);
  2395. vsi->num_q_vectors = 0;
  2396. return err;
  2397. }
  2398. /**
  2399. * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
  2400. * @vsi: ptr to the VSI
  2401. *
  2402. * This should only be called after ice_vsi_alloc() which allocates the
  2403. * corresponding SW VSI structure and initializes num_queue_pairs for the
  2404. * newly allocated VSI.
  2405. *
  2406. * Returns 0 on success or negative on failure
  2407. */
  2408. static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
  2409. {
  2410. struct ice_pf *pf = vsi->back;
  2411. int num_q_vectors = 0;
  2412. if (vsi->base_vector) {
  2413. dev_dbg(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  2414. vsi->vsi_num, vsi->base_vector);
  2415. return -EEXIST;
  2416. }
  2417. if (!test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  2418. return -ENOENT;
  2419. switch (vsi->type) {
  2420. case ICE_VSI_PF:
  2421. num_q_vectors = vsi->num_q_vectors;
  2422. break;
  2423. default:
  2424. dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
  2425. vsi->type);
  2426. break;
  2427. }
  2428. if (num_q_vectors)
  2429. vsi->base_vector = ice_get_res(pf, pf->irq_tracker,
  2430. num_q_vectors, vsi->idx);
  2431. if (vsi->base_vector < 0) {
  2432. dev_err(&pf->pdev->dev,
  2433. "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
  2434. num_q_vectors, vsi->vsi_num, vsi->base_vector);
  2435. return -ENOENT;
  2436. }
  2437. return 0;
  2438. }
  2439. /**
  2440. * ice_fill_rss_lut - Fill the RSS lookup table with default values
  2441. * @lut: Lookup table
  2442. * @rss_table_size: Lookup table size
  2443. * @rss_size: Range of queue number for hashing
  2444. */
  2445. void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
  2446. {
  2447. u16 i;
  2448. for (i = 0; i < rss_table_size; i++)
  2449. lut[i] = i % rss_size;
  2450. }
  2451. /**
  2452. * ice_vsi_cfg_rss - Configure RSS params for a VSI
  2453. * @vsi: VSI to be configured
  2454. */
  2455. static int ice_vsi_cfg_rss(struct ice_vsi *vsi)
  2456. {
  2457. u8 seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE];
  2458. struct ice_aqc_get_set_rss_keys *key;
  2459. struct ice_pf *pf = vsi->back;
  2460. enum ice_status status;
  2461. int err = 0;
  2462. u8 *lut;
  2463. vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
  2464. lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
  2465. if (!lut)
  2466. return -ENOMEM;
  2467. if (vsi->rss_lut_user)
  2468. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  2469. else
  2470. ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
  2471. status = ice_aq_set_rss_lut(&pf->hw, vsi->vsi_num, vsi->rss_lut_type,
  2472. lut, vsi->rss_table_size);
  2473. if (status) {
  2474. dev_err(&vsi->back->pdev->dev,
  2475. "set_rss_lut failed, error %d\n", status);
  2476. err = -EIO;
  2477. goto ice_vsi_cfg_rss_exit;
  2478. }
  2479. key = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*key), GFP_KERNEL);
  2480. if (!key) {
  2481. err = -ENOMEM;
  2482. goto ice_vsi_cfg_rss_exit;
  2483. }
  2484. if (vsi->rss_hkey_user)
  2485. memcpy(seed, vsi->rss_hkey_user,
  2486. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  2487. else
  2488. netdev_rss_key_fill((void *)seed,
  2489. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  2490. memcpy(&key->standard_rss_key, seed,
  2491. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  2492. status = ice_aq_set_rss_key(&pf->hw, vsi->vsi_num, key);
  2493. if (status) {
  2494. dev_err(&vsi->back->pdev->dev, "set_rss_key failed, error %d\n",
  2495. status);
  2496. err = -EIO;
  2497. }
  2498. devm_kfree(&pf->pdev->dev, key);
  2499. ice_vsi_cfg_rss_exit:
  2500. devm_kfree(&pf->pdev->dev, lut);
  2501. return err;
  2502. }
  2503. /**
  2504. * ice_vsi_rebuild - Rebuild VSI after reset
  2505. * @vsi: vsi to be rebuild
  2506. *
  2507. * Returns 0 on success and negative value on failure
  2508. */
  2509. static int ice_vsi_rebuild(struct ice_vsi *vsi)
  2510. {
  2511. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  2512. int ret, i;
  2513. if (!vsi)
  2514. return -EINVAL;
  2515. ice_vsi_free_q_vectors(vsi);
  2516. ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
  2517. vsi->base_vector = 0;
  2518. ice_vsi_clear_rings(vsi);
  2519. ice_vsi_free_arrays(vsi, false);
  2520. ice_vsi_set_num_qs(vsi);
  2521. /* Initialize VSI struct elements and create VSI in FW */
  2522. ret = ice_vsi_init(vsi);
  2523. if (ret < 0)
  2524. goto err_vsi;
  2525. ret = ice_vsi_alloc_arrays(vsi, false);
  2526. if (ret < 0)
  2527. goto err_vsi;
  2528. switch (vsi->type) {
  2529. case ICE_VSI_PF:
  2530. /* fall through */
  2531. ret = ice_vsi_alloc_q_vectors(vsi);
  2532. if (ret)
  2533. goto err_rings;
  2534. ret = ice_vsi_setup_vector_base(vsi);
  2535. if (ret)
  2536. goto err_vectors;
  2537. ret = ice_vsi_alloc_rings(vsi);
  2538. if (ret)
  2539. goto err_vectors;
  2540. ice_vsi_map_rings_to_vectors(vsi);
  2541. break;
  2542. default:
  2543. break;
  2544. }
  2545. ice_vsi_set_tc_cfg(vsi);
  2546. /* configure VSI nodes based on number of queues and TC's */
  2547. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  2548. max_txqs[i] = vsi->num_txq;
  2549. ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
  2550. vsi->tc_cfg.ena_tc, max_txqs);
  2551. if (ret) {
  2552. dev_info(&vsi->back->pdev->dev,
  2553. "Failed VSI lan queue config\n");
  2554. goto err_vectors;
  2555. }
  2556. return 0;
  2557. err_vectors:
  2558. ice_vsi_free_q_vectors(vsi);
  2559. err_rings:
  2560. if (vsi->netdev) {
  2561. vsi->current_netdev_flags = 0;
  2562. unregister_netdev(vsi->netdev);
  2563. free_netdev(vsi->netdev);
  2564. vsi->netdev = NULL;
  2565. }
  2566. err_vsi:
  2567. ice_vsi_clear(vsi);
  2568. set_bit(__ICE_RESET_FAILED, vsi->back->state);
  2569. return ret;
  2570. }
  2571. /**
  2572. * ice_vsi_setup - Set up a VSI by a given type
  2573. * @pf: board private structure
  2574. * @pi: pointer to the port_info instance
  2575. * @type: VSI type
  2576. * @vf_id: defines VF id to which this VSI connects. This field is meant to be
  2577. * used only for ICE_VSI_VF VSI type. For other VSI types, should
  2578. * fill-in ICE_INVAL_VFID as input.
  2579. *
  2580. * This allocates the sw VSI structure and its queue resources.
  2581. *
  2582. * Returns pointer to the successfully allocated and configured VSI sw struct on
  2583. * success, NULL on failure.
  2584. */
  2585. static struct ice_vsi *
  2586. ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
  2587. enum ice_vsi_type type, u16 __always_unused vf_id)
  2588. {
  2589. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  2590. struct device *dev = &pf->pdev->dev;
  2591. struct ice_vsi *vsi;
  2592. int ret, i;
  2593. vsi = ice_vsi_alloc(pf, type);
  2594. if (!vsi) {
  2595. dev_err(dev, "could not allocate VSI\n");
  2596. return NULL;
  2597. }
  2598. vsi->port_info = pi;
  2599. vsi->vsw = pf->first_sw;
  2600. if (ice_vsi_get_qs(vsi)) {
  2601. dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
  2602. vsi->idx);
  2603. goto err_get_qs;
  2604. }
  2605. /* set RSS capabilities */
  2606. ice_vsi_set_rss_params(vsi);
  2607. /* create the VSI */
  2608. ret = ice_vsi_init(vsi);
  2609. if (ret)
  2610. goto err_vsi;
  2611. switch (vsi->type) {
  2612. case ICE_VSI_PF:
  2613. ret = ice_cfg_netdev(vsi);
  2614. if (ret)
  2615. goto err_cfg_netdev;
  2616. ret = register_netdev(vsi->netdev);
  2617. if (ret)
  2618. goto err_register_netdev;
  2619. netif_carrier_off(vsi->netdev);
  2620. /* make sure transmit queues start off as stopped */
  2621. netif_tx_stop_all_queues(vsi->netdev);
  2622. ret = ice_vsi_alloc_q_vectors(vsi);
  2623. if (ret)
  2624. goto err_msix;
  2625. ret = ice_vsi_setup_vector_base(vsi);
  2626. if (ret)
  2627. goto err_rings;
  2628. ret = ice_vsi_alloc_rings(vsi);
  2629. if (ret)
  2630. goto err_rings;
  2631. ice_vsi_map_rings_to_vectors(vsi);
  2632. /* Do not exit if configuring RSS had an issue, at least
  2633. * receive traffic on first queue. Hence no need to capture
  2634. * return value
  2635. */
  2636. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  2637. ice_vsi_cfg_rss(vsi);
  2638. break;
  2639. default:
  2640. /* if vsi type is not recognized, clean up the resources and
  2641. * exit
  2642. */
  2643. goto err_rings;
  2644. }
  2645. ice_vsi_set_tc_cfg(vsi);
  2646. /* configure VSI nodes based on number of queues and TC's */
  2647. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  2648. max_txqs[i] = vsi->num_txq;
  2649. ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
  2650. vsi->tc_cfg.ena_tc, max_txqs);
  2651. if (ret) {
  2652. dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
  2653. goto err_rings;
  2654. }
  2655. return vsi;
  2656. err_rings:
  2657. ice_vsi_free_q_vectors(vsi);
  2658. err_msix:
  2659. if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
  2660. unregister_netdev(vsi->netdev);
  2661. err_register_netdev:
  2662. if (vsi->netdev) {
  2663. free_netdev(vsi->netdev);
  2664. vsi->netdev = NULL;
  2665. }
  2666. err_cfg_netdev:
  2667. ice_vsi_delete(vsi);
  2668. err_vsi:
  2669. ice_vsi_put_qs(vsi);
  2670. err_get_qs:
  2671. pf->q_left_tx += vsi->alloc_txq;
  2672. pf->q_left_rx += vsi->alloc_rxq;
  2673. ice_vsi_clear(vsi);
  2674. return NULL;
  2675. }
  2676. /**
  2677. * ice_pf_vsi_setup - Set up a PF VSI
  2678. * @pf: board private structure
  2679. * @pi: pointer to the port_info instance
  2680. *
  2681. * Returns pointer to the successfully allocated VSI sw struct on success,
  2682. * otherwise returns NULL on failure.
  2683. */
  2684. static struct ice_vsi *
  2685. ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
  2686. {
  2687. return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
  2688. }
  2689. /**
  2690. * ice_vsi_add_vlan - Add vsi membership for given vlan
  2691. * @vsi: the vsi being configured
  2692. * @vid: vlan id to be added
  2693. */
  2694. static int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
  2695. {
  2696. struct ice_fltr_list_entry *tmp;
  2697. struct ice_pf *pf = vsi->back;
  2698. LIST_HEAD(tmp_add_list);
  2699. enum ice_status status;
  2700. int err = 0;
  2701. tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
  2702. if (!tmp)
  2703. return -ENOMEM;
  2704. tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
  2705. tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  2706. tmp->fltr_info.flag = ICE_FLTR_TX;
  2707. tmp->fltr_info.src = vsi->vsi_num;
  2708. tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
  2709. tmp->fltr_info.l_data.vlan.vlan_id = vid;
  2710. INIT_LIST_HEAD(&tmp->list_entry);
  2711. list_add(&tmp->list_entry, &tmp_add_list);
  2712. status = ice_add_vlan(&pf->hw, &tmp_add_list);
  2713. if (status) {
  2714. err = -ENODEV;
  2715. dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
  2716. vid, vsi->vsi_num);
  2717. }
  2718. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  2719. return err;
  2720. }
  2721. /**
  2722. * ice_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2723. * @netdev: network interface to be adjusted
  2724. * @proto: unused protocol
  2725. * @vid: vlan id to be added
  2726. *
  2727. * net_device_ops implementation for adding vlan ids
  2728. */
  2729. static int ice_vlan_rx_add_vid(struct net_device *netdev,
  2730. __always_unused __be16 proto, u16 vid)
  2731. {
  2732. struct ice_netdev_priv *np = netdev_priv(netdev);
  2733. struct ice_vsi *vsi = np->vsi;
  2734. int ret;
  2735. if (vid >= VLAN_N_VID) {
  2736. netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
  2737. vid, VLAN_N_VID);
  2738. return -EINVAL;
  2739. }
  2740. if (vsi->info.pvid)
  2741. return -EINVAL;
  2742. /* Enable VLAN pruning when VLAN 0 is added */
  2743. if (unlikely(!vid)) {
  2744. ret = ice_cfg_vlan_pruning(vsi, true);
  2745. if (ret)
  2746. return ret;
  2747. }
  2748. /* Add all VLAN ids including 0 to the switch filter. VLAN id 0 is
  2749. * needed to continue allowing all untagged packets since VLAN prune
  2750. * list is applied to all packets by the switch
  2751. */
  2752. ret = ice_vsi_add_vlan(vsi, vid);
  2753. if (!ret)
  2754. set_bit(vid, vsi->active_vlans);
  2755. return ret;
  2756. }
  2757. /**
  2758. * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
  2759. * @vsi: the VSI being configured
  2760. * @vid: VLAN id to be removed
  2761. *
  2762. * Returns 0 on success and negative on failure
  2763. */
  2764. static int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
  2765. {
  2766. struct ice_fltr_list_entry *list;
  2767. struct ice_pf *pf = vsi->back;
  2768. LIST_HEAD(tmp_add_list);
  2769. int status = 0;
  2770. list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
  2771. if (!list)
  2772. return -ENOMEM;
  2773. list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
  2774. list->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
  2775. list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  2776. list->fltr_info.l_data.vlan.vlan_id = vid;
  2777. list->fltr_info.flag = ICE_FLTR_TX;
  2778. list->fltr_info.src = vsi->vsi_num;
  2779. INIT_LIST_HEAD(&list->list_entry);
  2780. list_add(&list->list_entry, &tmp_add_list);
  2781. if (ice_remove_vlan(&pf->hw, &tmp_add_list)) {
  2782. dev_err(&pf->pdev->dev, "Error removing VLAN %d on vsi %i\n",
  2783. vid, vsi->vsi_num);
  2784. status = -EIO;
  2785. }
  2786. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  2787. return status;
  2788. }
  2789. /**
  2790. * ice_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2791. * @netdev: network interface to be adjusted
  2792. * @proto: unused protocol
  2793. * @vid: vlan id to be removed
  2794. *
  2795. * net_device_ops implementation for removing vlan ids
  2796. */
  2797. static int ice_vlan_rx_kill_vid(struct net_device *netdev,
  2798. __always_unused __be16 proto, u16 vid)
  2799. {
  2800. struct ice_netdev_priv *np = netdev_priv(netdev);
  2801. struct ice_vsi *vsi = np->vsi;
  2802. int status;
  2803. if (vsi->info.pvid)
  2804. return -EINVAL;
  2805. /* Make sure ice_vsi_kill_vlan is successful before updating VLAN
  2806. * information
  2807. */
  2808. status = ice_vsi_kill_vlan(vsi, vid);
  2809. if (status)
  2810. return status;
  2811. clear_bit(vid, vsi->active_vlans);
  2812. /* Disable VLAN pruning when VLAN 0 is removed */
  2813. if (unlikely(!vid))
  2814. status = ice_cfg_vlan_pruning(vsi, false);
  2815. return status;
  2816. }
  2817. /**
  2818. * ice_setup_pf_sw - Setup the HW switch on startup or after reset
  2819. * @pf: board private structure
  2820. *
  2821. * Returns 0 on success, negative value on failure
  2822. */
  2823. static int ice_setup_pf_sw(struct ice_pf *pf)
  2824. {
  2825. LIST_HEAD(tmp_add_list);
  2826. u8 broadcast[ETH_ALEN];
  2827. struct ice_vsi *vsi;
  2828. int status = 0;
  2829. if (ice_is_reset_recovery_pending(pf->state))
  2830. return -EBUSY;
  2831. vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
  2832. if (!vsi) {
  2833. status = -ENOMEM;
  2834. goto unroll_vsi_setup;
  2835. }
  2836. /* To add a MAC filter, first add the MAC to a list and then
  2837. * pass the list to ice_add_mac.
  2838. */
  2839. /* Add a unicast MAC filter so the VSI can get its packets */
  2840. status = ice_add_mac_to_list(vsi, &tmp_add_list,
  2841. vsi->port_info->mac.perm_addr);
  2842. if (status)
  2843. goto unroll_vsi_setup;
  2844. /* VSI needs to receive broadcast traffic, so add the broadcast
  2845. * MAC address to the list as well.
  2846. */
  2847. eth_broadcast_addr(broadcast);
  2848. status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
  2849. if (status)
  2850. goto free_mac_list;
  2851. /* program MAC filters for entries in tmp_add_list */
  2852. status = ice_add_mac(&pf->hw, &tmp_add_list);
  2853. if (status) {
  2854. dev_err(&pf->pdev->dev, "Could not add MAC filters\n");
  2855. status = -ENOMEM;
  2856. goto free_mac_list;
  2857. }
  2858. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  2859. return status;
  2860. free_mac_list:
  2861. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  2862. unroll_vsi_setup:
  2863. if (vsi) {
  2864. ice_vsi_free_q_vectors(vsi);
  2865. if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
  2866. unregister_netdev(vsi->netdev);
  2867. if (vsi->netdev) {
  2868. free_netdev(vsi->netdev);
  2869. vsi->netdev = NULL;
  2870. }
  2871. ice_vsi_delete(vsi);
  2872. ice_vsi_put_qs(vsi);
  2873. pf->q_left_tx += vsi->alloc_txq;
  2874. pf->q_left_rx += vsi->alloc_rxq;
  2875. ice_vsi_clear(vsi);
  2876. }
  2877. return status;
  2878. }
  2879. /**
  2880. * ice_determine_q_usage - Calculate queue distribution
  2881. * @pf: board private structure
  2882. *
  2883. * Return -ENOMEM if we don't get enough queues for all ports
  2884. */
  2885. static void ice_determine_q_usage(struct ice_pf *pf)
  2886. {
  2887. u16 q_left_tx, q_left_rx;
  2888. q_left_tx = pf->hw.func_caps.common_cap.num_txq;
  2889. q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
  2890. pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
  2891. /* only 1 rx queue unless RSS is enabled */
  2892. if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  2893. pf->num_lan_rx = 1;
  2894. else
  2895. pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
  2896. pf->q_left_tx = q_left_tx - pf->num_lan_tx;
  2897. pf->q_left_rx = q_left_rx - pf->num_lan_rx;
  2898. }
  2899. /**
  2900. * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
  2901. * @pf: board private structure to initialize
  2902. */
  2903. static void ice_deinit_pf(struct ice_pf *pf)
  2904. {
  2905. ice_service_task_stop(pf);
  2906. mutex_destroy(&pf->sw_mutex);
  2907. mutex_destroy(&pf->avail_q_mutex);
  2908. }
  2909. /**
  2910. * ice_init_pf - Initialize general software structures (struct ice_pf)
  2911. * @pf: board private structure to initialize
  2912. */
  2913. static void ice_init_pf(struct ice_pf *pf)
  2914. {
  2915. bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
  2916. set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
  2917. mutex_init(&pf->sw_mutex);
  2918. mutex_init(&pf->avail_q_mutex);
  2919. /* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
  2920. mutex_lock(&pf->avail_q_mutex);
  2921. bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
  2922. bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
  2923. mutex_unlock(&pf->avail_q_mutex);
  2924. if (pf->hw.func_caps.common_cap.rss_table_size)
  2925. set_bit(ICE_FLAG_RSS_ENA, pf->flags);
  2926. /* setup service timer and periodic service task */
  2927. timer_setup(&pf->serv_tmr, ice_service_timer, 0);
  2928. pf->serv_tmr_period = HZ;
  2929. INIT_WORK(&pf->serv_task, ice_service_task);
  2930. clear_bit(__ICE_SERVICE_SCHED, pf->state);
  2931. }
  2932. /**
  2933. * ice_ena_msix_range - Request a range of MSIX vectors from the OS
  2934. * @pf: board private structure
  2935. *
  2936. * compute the number of MSIX vectors required (v_budget) and request from
  2937. * the OS. Return the number of vectors reserved or negative on failure
  2938. */
  2939. static int ice_ena_msix_range(struct ice_pf *pf)
  2940. {
  2941. int v_left, v_actual, v_budget = 0;
  2942. int needed, err, i;
  2943. v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
  2944. /* reserve one vector for miscellaneous handler */
  2945. needed = 1;
  2946. v_budget += needed;
  2947. v_left -= needed;
  2948. /* reserve vectors for LAN traffic */
  2949. pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
  2950. v_budget += pf->num_lan_msix;
  2951. pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
  2952. sizeof(struct msix_entry), GFP_KERNEL);
  2953. if (!pf->msix_entries) {
  2954. err = -ENOMEM;
  2955. goto exit_err;
  2956. }
  2957. for (i = 0; i < v_budget; i++)
  2958. pf->msix_entries[i].entry = i;
  2959. /* actually reserve the vectors */
  2960. v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  2961. ICE_MIN_MSIX, v_budget);
  2962. if (v_actual < 0) {
  2963. dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
  2964. err = v_actual;
  2965. goto msix_err;
  2966. }
  2967. if (v_actual < v_budget) {
  2968. dev_warn(&pf->pdev->dev,
  2969. "not enough vectors. requested = %d, obtained = %d\n",
  2970. v_budget, v_actual);
  2971. if (v_actual >= (pf->num_lan_msix + 1)) {
  2972. pf->num_avail_msix = v_actual - (pf->num_lan_msix + 1);
  2973. } else if (v_actual >= 2) {
  2974. pf->num_lan_msix = 1;
  2975. pf->num_avail_msix = v_actual - 2;
  2976. } else {
  2977. pci_disable_msix(pf->pdev);
  2978. err = -ERANGE;
  2979. goto msix_err;
  2980. }
  2981. }
  2982. return v_actual;
  2983. msix_err:
  2984. devm_kfree(&pf->pdev->dev, pf->msix_entries);
  2985. goto exit_err;
  2986. exit_err:
  2987. pf->num_lan_msix = 0;
  2988. clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
  2989. return err;
  2990. }
  2991. /**
  2992. * ice_dis_msix - Disable MSI-X interrupt setup in OS
  2993. * @pf: board private structure
  2994. */
  2995. static void ice_dis_msix(struct ice_pf *pf)
  2996. {
  2997. pci_disable_msix(pf->pdev);
  2998. devm_kfree(&pf->pdev->dev, pf->msix_entries);
  2999. pf->msix_entries = NULL;
  3000. clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
  3001. }
  3002. /**
  3003. * ice_init_interrupt_scheme - Determine proper interrupt scheme
  3004. * @pf: board private structure to initialize
  3005. */
  3006. static int ice_init_interrupt_scheme(struct ice_pf *pf)
  3007. {
  3008. int vectors = 0;
  3009. ssize_t size;
  3010. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  3011. vectors = ice_ena_msix_range(pf);
  3012. else
  3013. return -ENODEV;
  3014. if (vectors < 0)
  3015. return vectors;
  3016. /* set up vector assignment tracking */
  3017. size = sizeof(struct ice_res_tracker) + (sizeof(u16) * vectors);
  3018. pf->irq_tracker = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
  3019. if (!pf->irq_tracker) {
  3020. ice_dis_msix(pf);
  3021. return -ENOMEM;
  3022. }
  3023. pf->irq_tracker->num_entries = vectors;
  3024. return 0;
  3025. }
  3026. /**
  3027. * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
  3028. * @pf: board private structure
  3029. */
  3030. static void ice_clear_interrupt_scheme(struct ice_pf *pf)
  3031. {
  3032. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  3033. ice_dis_msix(pf);
  3034. if (pf->irq_tracker) {
  3035. devm_kfree(&pf->pdev->dev, pf->irq_tracker);
  3036. pf->irq_tracker = NULL;
  3037. }
  3038. }
  3039. /**
  3040. * ice_probe - Device initialization routine
  3041. * @pdev: PCI device information struct
  3042. * @ent: entry in ice_pci_tbl
  3043. *
  3044. * Returns 0 on success, negative on failure
  3045. */
  3046. static int ice_probe(struct pci_dev *pdev,
  3047. const struct pci_device_id __always_unused *ent)
  3048. {
  3049. struct ice_pf *pf;
  3050. struct ice_hw *hw;
  3051. int err;
  3052. /* this driver uses devres, see Documentation/driver-model/devres.txt */
  3053. err = pcim_enable_device(pdev);
  3054. if (err)
  3055. return err;
  3056. err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
  3057. if (err) {
  3058. dev_err(&pdev->dev, "BAR0 I/O map error %d\n", err);
  3059. return err;
  3060. }
  3061. pf = devm_kzalloc(&pdev->dev, sizeof(*pf), GFP_KERNEL);
  3062. if (!pf)
  3063. return -ENOMEM;
  3064. /* set up for high or low dma */
  3065. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  3066. if (err)
  3067. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  3068. if (err) {
  3069. dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err);
  3070. return err;
  3071. }
  3072. pci_enable_pcie_error_reporting(pdev);
  3073. pci_set_master(pdev);
  3074. pf->pdev = pdev;
  3075. pci_set_drvdata(pdev, pf);
  3076. set_bit(__ICE_DOWN, pf->state);
  3077. /* Disable service task until DOWN bit is cleared */
  3078. set_bit(__ICE_SERVICE_DIS, pf->state);
  3079. hw = &pf->hw;
  3080. hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
  3081. hw->back = pf;
  3082. hw->vendor_id = pdev->vendor;
  3083. hw->device_id = pdev->device;
  3084. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  3085. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  3086. hw->subsystem_device_id = pdev->subsystem_device;
  3087. hw->bus.device = PCI_SLOT(pdev->devfn);
  3088. hw->bus.func = PCI_FUNC(pdev->devfn);
  3089. ice_set_ctrlq_len(hw);
  3090. pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
  3091. #ifndef CONFIG_DYNAMIC_DEBUG
  3092. if (debug < -1)
  3093. hw->debug_mask = debug;
  3094. #endif
  3095. err = ice_init_hw(hw);
  3096. if (err) {
  3097. dev_err(&pdev->dev, "ice_init_hw failed: %d\n", err);
  3098. err = -EIO;
  3099. goto err_exit_unroll;
  3100. }
  3101. dev_info(&pdev->dev, "firmware %d.%d.%05d api %d.%d\n",
  3102. hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
  3103. hw->api_maj_ver, hw->api_min_ver);
  3104. ice_init_pf(pf);
  3105. ice_determine_q_usage(pf);
  3106. pf->num_alloc_vsi = min_t(u16, ICE_MAX_VSI_ALLOC,
  3107. hw->func_caps.guaranteed_num_vsi);
  3108. if (!pf->num_alloc_vsi) {
  3109. err = -EIO;
  3110. goto err_init_pf_unroll;
  3111. }
  3112. pf->vsi = devm_kcalloc(&pdev->dev, pf->num_alloc_vsi,
  3113. sizeof(struct ice_vsi *), GFP_KERNEL);
  3114. if (!pf->vsi) {
  3115. err = -ENOMEM;
  3116. goto err_init_pf_unroll;
  3117. }
  3118. err = ice_init_interrupt_scheme(pf);
  3119. if (err) {
  3120. dev_err(&pdev->dev,
  3121. "ice_init_interrupt_scheme failed: %d\n", err);
  3122. err = -EIO;
  3123. goto err_init_interrupt_unroll;
  3124. }
  3125. /* Driver is mostly up */
  3126. clear_bit(__ICE_DOWN, pf->state);
  3127. /* In case of MSIX we are going to setup the misc vector right here
  3128. * to handle admin queue events etc. In case of legacy and MSI
  3129. * the misc functionality and queue processing is combined in
  3130. * the same vector and that gets setup at open.
  3131. */
  3132. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  3133. err = ice_req_irq_msix_misc(pf);
  3134. if (err) {
  3135. dev_err(&pdev->dev,
  3136. "setup of misc vector failed: %d\n", err);
  3137. goto err_init_interrupt_unroll;
  3138. }
  3139. }
  3140. /* create switch struct for the switch element created by FW on boot */
  3141. pf->first_sw = devm_kzalloc(&pdev->dev, sizeof(struct ice_sw),
  3142. GFP_KERNEL);
  3143. if (!pf->first_sw) {
  3144. err = -ENOMEM;
  3145. goto err_msix_misc_unroll;
  3146. }
  3147. if (hw->evb_veb)
  3148. pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
  3149. else
  3150. pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
  3151. pf->first_sw->pf = pf;
  3152. /* record the sw_id available for later use */
  3153. pf->first_sw->sw_id = hw->port_info->sw_id;
  3154. err = ice_setup_pf_sw(pf);
  3155. if (err) {
  3156. dev_err(&pdev->dev,
  3157. "probe failed due to setup pf switch:%d\n", err);
  3158. goto err_alloc_sw_unroll;
  3159. }
  3160. clear_bit(__ICE_SERVICE_DIS, pf->state);
  3161. /* since everything is good, start the service timer */
  3162. mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
  3163. err = ice_init_link_events(pf->hw.port_info);
  3164. if (err) {
  3165. dev_err(&pdev->dev, "ice_init_link_events failed: %d\n", err);
  3166. goto err_alloc_sw_unroll;
  3167. }
  3168. return 0;
  3169. err_alloc_sw_unroll:
  3170. set_bit(__ICE_SERVICE_DIS, pf->state);
  3171. set_bit(__ICE_DOWN, pf->state);
  3172. devm_kfree(&pf->pdev->dev, pf->first_sw);
  3173. err_msix_misc_unroll:
  3174. ice_free_irq_msix_misc(pf);
  3175. err_init_interrupt_unroll:
  3176. ice_clear_interrupt_scheme(pf);
  3177. devm_kfree(&pdev->dev, pf->vsi);
  3178. err_init_pf_unroll:
  3179. ice_deinit_pf(pf);
  3180. ice_deinit_hw(hw);
  3181. err_exit_unroll:
  3182. pci_disable_pcie_error_reporting(pdev);
  3183. return err;
  3184. }
  3185. /**
  3186. * ice_remove - Device removal routine
  3187. * @pdev: PCI device information struct
  3188. */
  3189. static void ice_remove(struct pci_dev *pdev)
  3190. {
  3191. struct ice_pf *pf = pci_get_drvdata(pdev);
  3192. if (!pf)
  3193. return;
  3194. set_bit(__ICE_DOWN, pf->state);
  3195. ice_service_task_stop(pf);
  3196. ice_vsi_release_all(pf);
  3197. ice_free_irq_msix_misc(pf);
  3198. ice_clear_interrupt_scheme(pf);
  3199. ice_deinit_pf(pf);
  3200. ice_deinit_hw(&pf->hw);
  3201. pci_disable_pcie_error_reporting(pdev);
  3202. }
  3203. /* ice_pci_tbl - PCI Device ID Table
  3204. *
  3205. * Wildcard entries (PCI_ANY_ID) should come last
  3206. * Last entry must be all 0s
  3207. *
  3208. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  3209. * Class, Class Mask, private data (not used) }
  3210. */
  3211. static const struct pci_device_id ice_pci_tbl[] = {
  3212. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_BACKPLANE), 0 },
  3213. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_QSFP), 0 },
  3214. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SFP), 0 },
  3215. /* required last entry */
  3216. { 0, }
  3217. };
  3218. MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
  3219. static struct pci_driver ice_driver = {
  3220. .name = KBUILD_MODNAME,
  3221. .id_table = ice_pci_tbl,
  3222. .probe = ice_probe,
  3223. .remove = ice_remove,
  3224. };
  3225. /**
  3226. * ice_module_init - Driver registration routine
  3227. *
  3228. * ice_module_init is the first routine called when the driver is
  3229. * loaded. All it does is register with the PCI subsystem.
  3230. */
  3231. static int __init ice_module_init(void)
  3232. {
  3233. int status;
  3234. pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
  3235. pr_info("%s\n", ice_copyright);
  3236. ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
  3237. if (!ice_wq) {
  3238. pr_err("Failed to create workqueue\n");
  3239. return -ENOMEM;
  3240. }
  3241. status = pci_register_driver(&ice_driver);
  3242. if (status) {
  3243. pr_err("failed to register pci driver, err %d\n", status);
  3244. destroy_workqueue(ice_wq);
  3245. }
  3246. return status;
  3247. }
  3248. module_init(ice_module_init);
  3249. /**
  3250. * ice_module_exit - Driver exit cleanup routine
  3251. *
  3252. * ice_module_exit is called just before the driver is removed
  3253. * from memory.
  3254. */
  3255. static void __exit ice_module_exit(void)
  3256. {
  3257. pci_unregister_driver(&ice_driver);
  3258. destroy_workqueue(ice_wq);
  3259. pr_info("module unloaded\n");
  3260. }
  3261. module_exit(ice_module_exit);
  3262. /**
  3263. * ice_set_mac_address - NDO callback to set mac address
  3264. * @netdev: network interface device structure
  3265. * @pi: pointer to an address structure
  3266. *
  3267. * Returns 0 on success, negative on failure
  3268. */
  3269. static int ice_set_mac_address(struct net_device *netdev, void *pi)
  3270. {
  3271. struct ice_netdev_priv *np = netdev_priv(netdev);
  3272. struct ice_vsi *vsi = np->vsi;
  3273. struct ice_pf *pf = vsi->back;
  3274. struct ice_hw *hw = &pf->hw;
  3275. struct sockaddr *addr = pi;
  3276. enum ice_status status;
  3277. LIST_HEAD(a_mac_list);
  3278. LIST_HEAD(r_mac_list);
  3279. u8 flags = 0;
  3280. int err;
  3281. u8 *mac;
  3282. mac = (u8 *)addr->sa_data;
  3283. if (!is_valid_ether_addr(mac))
  3284. return -EADDRNOTAVAIL;
  3285. if (ether_addr_equal(netdev->dev_addr, mac)) {
  3286. netdev_warn(netdev, "already using mac %pM\n", mac);
  3287. return 0;
  3288. }
  3289. if (test_bit(__ICE_DOWN, pf->state) ||
  3290. ice_is_reset_recovery_pending(pf->state)) {
  3291. netdev_err(netdev, "can't set mac %pM. device not ready\n",
  3292. mac);
  3293. return -EBUSY;
  3294. }
  3295. /* When we change the mac address we also have to change the mac address
  3296. * based filter rules that were created previously for the old mac
  3297. * address. So first, we remove the old filter rule using ice_remove_mac
  3298. * and then create a new filter rule using ice_add_mac. Note that for
  3299. * both these operations, we first need to form a "list" of mac
  3300. * addresses (even though in this case, we have only 1 mac address to be
  3301. * added/removed) and this done using ice_add_mac_to_list. Depending on
  3302. * the ensuing operation this "list" of mac addresses is either to be
  3303. * added or removed from the filter.
  3304. */
  3305. err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
  3306. if (err) {
  3307. err = -EADDRNOTAVAIL;
  3308. goto free_lists;
  3309. }
  3310. status = ice_remove_mac(hw, &r_mac_list);
  3311. if (status) {
  3312. err = -EADDRNOTAVAIL;
  3313. goto free_lists;
  3314. }
  3315. err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
  3316. if (err) {
  3317. err = -EADDRNOTAVAIL;
  3318. goto free_lists;
  3319. }
  3320. status = ice_add_mac(hw, &a_mac_list);
  3321. if (status) {
  3322. err = -EADDRNOTAVAIL;
  3323. goto free_lists;
  3324. }
  3325. free_lists:
  3326. /* free list entries */
  3327. ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
  3328. ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
  3329. if (err) {
  3330. netdev_err(netdev, "can't set mac %pM. filter update failed\n",
  3331. mac);
  3332. return err;
  3333. }
  3334. /* change the netdev's mac address */
  3335. memcpy(netdev->dev_addr, mac, netdev->addr_len);
  3336. netdev_dbg(vsi->netdev, "updated mac address to %pM\n",
  3337. netdev->dev_addr);
  3338. /* write new mac address to the firmware */
  3339. flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
  3340. status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
  3341. if (status) {
  3342. netdev_err(netdev, "can't set mac %pM. write to firmware failed.\n",
  3343. mac);
  3344. }
  3345. return 0;
  3346. }
  3347. /**
  3348. * ice_set_rx_mode - NDO callback to set the netdev filters
  3349. * @netdev: network interface device structure
  3350. */
  3351. static void ice_set_rx_mode(struct net_device *netdev)
  3352. {
  3353. struct ice_netdev_priv *np = netdev_priv(netdev);
  3354. struct ice_vsi *vsi = np->vsi;
  3355. if (!vsi)
  3356. return;
  3357. /* Set the flags to synchronize filters
  3358. * ndo_set_rx_mode may be triggered even without a change in netdev
  3359. * flags
  3360. */
  3361. set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
  3362. set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
  3363. set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
  3364. /* schedule our worker thread which will take care of
  3365. * applying the new filter changes
  3366. */
  3367. ice_service_task_schedule(vsi->back);
  3368. }
  3369. /**
  3370. * ice_fdb_add - add an entry to the hardware database
  3371. * @ndm: the input from the stack
  3372. * @tb: pointer to array of nladdr (unused)
  3373. * @dev: the net device pointer
  3374. * @addr: the MAC address entry being added
  3375. * @vid: VLAN id
  3376. * @flags: instructions from stack about fdb operation
  3377. */
  3378. static int ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
  3379. struct net_device *dev, const unsigned char *addr,
  3380. u16 vid, u16 flags)
  3381. {
  3382. int err;
  3383. if (vid) {
  3384. netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
  3385. return -EINVAL;
  3386. }
  3387. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  3388. netdev_err(dev, "FDB only supports static addresses\n");
  3389. return -EINVAL;
  3390. }
  3391. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  3392. err = dev_uc_add_excl(dev, addr);
  3393. else if (is_multicast_ether_addr(addr))
  3394. err = dev_mc_add_excl(dev, addr);
  3395. else
  3396. err = -EINVAL;
  3397. /* Only return duplicate errors if NLM_F_EXCL is set */
  3398. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  3399. err = 0;
  3400. return err;
  3401. }
  3402. /**
  3403. * ice_fdb_del - delete an entry from the hardware database
  3404. * @ndm: the input from the stack
  3405. * @tb: pointer to array of nladdr (unused)
  3406. * @dev: the net device pointer
  3407. * @addr: the MAC address entry being added
  3408. * @vid: VLAN id
  3409. */
  3410. static int ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
  3411. struct net_device *dev, const unsigned char *addr,
  3412. __always_unused u16 vid)
  3413. {
  3414. int err;
  3415. if (ndm->ndm_state & NUD_PERMANENT) {
  3416. netdev_err(dev, "FDB only supports static addresses\n");
  3417. return -EINVAL;
  3418. }
  3419. if (is_unicast_ether_addr(addr))
  3420. err = dev_uc_del(dev, addr);
  3421. else if (is_multicast_ether_addr(addr))
  3422. err = dev_mc_del(dev, addr);
  3423. else
  3424. err = -EINVAL;
  3425. return err;
  3426. }
  3427. /**
  3428. * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
  3429. * @vsi: the vsi being changed
  3430. */
  3431. static int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
  3432. {
  3433. struct device *dev = &vsi->back->pdev->dev;
  3434. struct ice_hw *hw = &vsi->back->hw;
  3435. struct ice_vsi_ctx ctxt = { 0 };
  3436. enum ice_status status;
  3437. /* Here we are configuring the VSI to let the driver add VLAN tags by
  3438. * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
  3439. * insertion happens in the Tx hot path, in ice_tx_map.
  3440. */
  3441. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
  3442. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  3443. ctxt.vsi_num = vsi->vsi_num;
  3444. status = ice_aq_update_vsi(hw, &ctxt, NULL);
  3445. if (status) {
  3446. dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
  3447. status, hw->adminq.sq_last_status);
  3448. return -EIO;
  3449. }
  3450. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  3451. return 0;
  3452. }
  3453. /**
  3454. * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
  3455. * @vsi: the vsi being changed
  3456. * @ena: boolean value indicating if this is a enable or disable request
  3457. */
  3458. static int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
  3459. {
  3460. struct device *dev = &vsi->back->pdev->dev;
  3461. struct ice_hw *hw = &vsi->back->hw;
  3462. struct ice_vsi_ctx ctxt = { 0 };
  3463. enum ice_status status;
  3464. /* Here we are configuring what the VSI should do with the VLAN tag in
  3465. * the Rx packet. We can either leave the tag in the packet or put it in
  3466. * the Rx descriptor.
  3467. */
  3468. if (ena) {
  3469. /* Strip VLAN tag from Rx packet and put it in the desc */
  3470. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
  3471. } else {
  3472. /* Disable stripping. Leave tag in packet */
  3473. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
  3474. }
  3475. /* Allow all packets untagged/tagged */
  3476. ctxt.info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
  3477. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  3478. ctxt.vsi_num = vsi->vsi_num;
  3479. status = ice_aq_update_vsi(hw, &ctxt, NULL);
  3480. if (status) {
  3481. dev_err(dev, "update VSI for VALN strip failed, ena = %d err %d aq_err %d\n",
  3482. ena, status, hw->adminq.sq_last_status);
  3483. return -EIO;
  3484. }
  3485. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  3486. return 0;
  3487. }
  3488. /**
  3489. * ice_set_features - set the netdev feature flags
  3490. * @netdev: ptr to the netdev being adjusted
  3491. * @features: the feature set that the stack is suggesting
  3492. */
  3493. static int ice_set_features(struct net_device *netdev,
  3494. netdev_features_t features)
  3495. {
  3496. struct ice_netdev_priv *np = netdev_priv(netdev);
  3497. struct ice_vsi *vsi = np->vsi;
  3498. int ret = 0;
  3499. if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
  3500. !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
  3501. ret = ice_vsi_manage_vlan_stripping(vsi, true);
  3502. else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
  3503. (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
  3504. ret = ice_vsi_manage_vlan_stripping(vsi, false);
  3505. else if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
  3506. !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
  3507. ret = ice_vsi_manage_vlan_insertion(vsi);
  3508. else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
  3509. (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
  3510. ret = ice_vsi_manage_vlan_insertion(vsi);
  3511. return ret;
  3512. }
  3513. /**
  3514. * ice_vsi_vlan_setup - Setup vlan offload properties on a VSI
  3515. * @vsi: VSI to setup vlan properties for
  3516. */
  3517. static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
  3518. {
  3519. int ret = 0;
  3520. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  3521. ret = ice_vsi_manage_vlan_stripping(vsi, true);
  3522. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
  3523. ret = ice_vsi_manage_vlan_insertion(vsi);
  3524. return ret;
  3525. }
  3526. /**
  3527. * ice_restore_vlan - Reinstate VLANs when vsi/netdev comes back up
  3528. * @vsi: the VSI being brought back up
  3529. */
  3530. static int ice_restore_vlan(struct ice_vsi *vsi)
  3531. {
  3532. int err;
  3533. u16 vid;
  3534. if (!vsi->netdev)
  3535. return -EINVAL;
  3536. err = ice_vsi_vlan_setup(vsi);
  3537. if (err)
  3538. return err;
  3539. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID) {
  3540. err = ice_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q), vid);
  3541. if (err)
  3542. break;
  3543. }
  3544. return err;
  3545. }
  3546. /**
  3547. * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
  3548. * @ring: The Tx ring to configure
  3549. * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
  3550. * @pf_q: queue index in the PF space
  3551. *
  3552. * Configure the Tx descriptor ring in TLAN context.
  3553. */
  3554. static void
  3555. ice_setup_tx_ctx(struct ice_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
  3556. {
  3557. struct ice_vsi *vsi = ring->vsi;
  3558. struct ice_hw *hw = &vsi->back->hw;
  3559. tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
  3560. tlan_ctx->port_num = vsi->port_info->lport;
  3561. /* Transmit Queue Length */
  3562. tlan_ctx->qlen = ring->count;
  3563. /* PF number */
  3564. tlan_ctx->pf_num = hw->pf_id;
  3565. /* queue belongs to a specific VSI type
  3566. * VF / VM index should be programmed per vmvf_type setting:
  3567. * for vmvf_type = VF, it is VF number between 0-256
  3568. * for vmvf_type = VM, it is VM number between 0-767
  3569. * for PF or EMP this field should be set to zero
  3570. */
  3571. switch (vsi->type) {
  3572. case ICE_VSI_PF:
  3573. tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
  3574. break;
  3575. default:
  3576. return;
  3577. }
  3578. /* make sure the context is associated with the right VSI */
  3579. tlan_ctx->src_vsi = vsi->vsi_num;
  3580. tlan_ctx->tso_ena = ICE_TX_LEGACY;
  3581. tlan_ctx->tso_qnum = pf_q;
  3582. /* Legacy or Advanced Host Interface:
  3583. * 0: Advanced Host Interface
  3584. * 1: Legacy Host Interface
  3585. */
  3586. tlan_ctx->legacy_int = ICE_TX_LEGACY;
  3587. }
  3588. /**
  3589. * ice_vsi_cfg_txqs - Configure the VSI for Tx
  3590. * @vsi: the VSI being configured
  3591. *
  3592. * Return 0 on success and a negative value on error
  3593. * Configure the Tx VSI for operation.
  3594. */
  3595. static int ice_vsi_cfg_txqs(struct ice_vsi *vsi)
  3596. {
  3597. struct ice_aqc_add_tx_qgrp *qg_buf;
  3598. struct ice_aqc_add_txqs_perq *txq;
  3599. struct ice_pf *pf = vsi->back;
  3600. enum ice_status status;
  3601. u16 buf_len, i, pf_q;
  3602. int err = 0, tc = 0;
  3603. u8 num_q_grps;
  3604. buf_len = sizeof(struct ice_aqc_add_tx_qgrp);
  3605. qg_buf = devm_kzalloc(&pf->pdev->dev, buf_len, GFP_KERNEL);
  3606. if (!qg_buf)
  3607. return -ENOMEM;
  3608. if (vsi->num_txq > ICE_MAX_TXQ_PER_TXQG) {
  3609. err = -EINVAL;
  3610. goto err_cfg_txqs;
  3611. }
  3612. qg_buf->num_txqs = 1;
  3613. num_q_grps = 1;
  3614. /* set up and configure the tx queues */
  3615. ice_for_each_txq(vsi, i) {
  3616. struct ice_tlan_ctx tlan_ctx = { 0 };
  3617. pf_q = vsi->txq_map[i];
  3618. ice_setup_tx_ctx(vsi->tx_rings[i], &tlan_ctx, pf_q);
  3619. /* copy context contents into the qg_buf */
  3620. qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
  3621. ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
  3622. ice_tlan_ctx_info);
  3623. /* init queue specific tail reg. It is referred as transmit
  3624. * comm scheduler queue doorbell.
  3625. */
  3626. vsi->tx_rings[i]->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
  3627. status = ice_ena_vsi_txq(vsi->port_info, vsi->vsi_num, tc,
  3628. num_q_grps, qg_buf, buf_len, NULL);
  3629. if (status) {
  3630. dev_err(&vsi->back->pdev->dev,
  3631. "Failed to set LAN Tx queue context, error: %d\n",
  3632. status);
  3633. err = -ENODEV;
  3634. goto err_cfg_txqs;
  3635. }
  3636. /* Add Tx Queue TEID into the VSI tx ring from the response
  3637. * This will complete configuring and enabling the queue.
  3638. */
  3639. txq = &qg_buf->txqs[0];
  3640. if (pf_q == le16_to_cpu(txq->txq_id))
  3641. vsi->tx_rings[i]->txq_teid =
  3642. le32_to_cpu(txq->q_teid);
  3643. }
  3644. err_cfg_txqs:
  3645. devm_kfree(&pf->pdev->dev, qg_buf);
  3646. return err;
  3647. }
  3648. /**
  3649. * ice_setup_rx_ctx - Configure a receive ring context
  3650. * @ring: The Rx ring to configure
  3651. *
  3652. * Configure the Rx descriptor ring in RLAN context.
  3653. */
  3654. static int ice_setup_rx_ctx(struct ice_ring *ring)
  3655. {
  3656. struct ice_vsi *vsi = ring->vsi;
  3657. struct ice_hw *hw = &vsi->back->hw;
  3658. u32 rxdid = ICE_RXDID_FLEX_NIC;
  3659. struct ice_rlan_ctx rlan_ctx;
  3660. u32 regval;
  3661. u16 pf_q;
  3662. int err;
  3663. /* what is RX queue number in global space of 2K rx queues */
  3664. pf_q = vsi->rxq_map[ring->q_index];
  3665. /* clear the context structure first */
  3666. memset(&rlan_ctx, 0, sizeof(rlan_ctx));
  3667. rlan_ctx.base = ring->dma >> ICE_RLAN_BASE_S;
  3668. rlan_ctx.qlen = ring->count;
  3669. /* Receive Packet Data Buffer Size.
  3670. * The Packet Data Buffer Size is defined in 128 byte units.
  3671. */
  3672. rlan_ctx.dbuf = vsi->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
  3673. /* use 32 byte descriptors */
  3674. rlan_ctx.dsize = 1;
  3675. /* Strip the Ethernet CRC bytes before the packet is posted to host
  3676. * memory.
  3677. */
  3678. rlan_ctx.crcstrip = 1;
  3679. /* L2TSEL flag defines the reported L2 Tags in the receive descriptor */
  3680. rlan_ctx.l2tsel = 1;
  3681. rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
  3682. rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
  3683. rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
  3684. /* This controls whether VLAN is stripped from inner headers
  3685. * The VLAN in the inner L2 header is stripped to the receive
  3686. * descriptor if enabled by this flag.
  3687. */
  3688. rlan_ctx.showiv = 0;
  3689. /* Max packet size for this queue - must not be set to a larger value
  3690. * than 5 x DBUF
  3691. */
  3692. rlan_ctx.rxmax = min_t(u16, vsi->max_frame,
  3693. ICE_MAX_CHAINED_RX_BUFS * vsi->rx_buf_len);
  3694. /* Rx queue threshold in units of 64 */
  3695. rlan_ctx.lrxqthresh = 1;
  3696. /* Enable Flexible Descriptors in the queue context which
  3697. * allows this driver to select a specific receive descriptor format
  3698. */
  3699. regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
  3700. regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
  3701. QRXFLXP_CNTXT_RXDID_IDX_M;
  3702. /* increasing context priority to pick up profile id;
  3703. * default is 0x01; setting to 0x03 to ensure profile
  3704. * is programming if prev context is of same priority
  3705. */
  3706. regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
  3707. QRXFLXP_CNTXT_RXDID_PRIO_M;
  3708. wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
  3709. /* Absolute queue number out of 2K needs to be passed */
  3710. err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
  3711. if (err) {
  3712. dev_err(&vsi->back->pdev->dev,
  3713. "Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
  3714. pf_q, err);
  3715. return -EIO;
  3716. }
  3717. /* init queue specific tail register */
  3718. ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
  3719. writel(0, ring->tail);
  3720. ice_alloc_rx_bufs(ring, ICE_DESC_UNUSED(ring));
  3721. return 0;
  3722. }
  3723. /**
  3724. * ice_vsi_cfg_rxqs - Configure the VSI for Rx
  3725. * @vsi: the VSI being configured
  3726. *
  3727. * Return 0 on success and a negative value on error
  3728. * Configure the Rx VSI for operation.
  3729. */
  3730. static int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
  3731. {
  3732. int err = 0;
  3733. u16 i;
  3734. if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
  3735. vsi->max_frame = vsi->netdev->mtu +
  3736. ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  3737. else
  3738. vsi->max_frame = ICE_RXBUF_2048;
  3739. vsi->rx_buf_len = ICE_RXBUF_2048;
  3740. /* set up individual rings */
  3741. for (i = 0; i < vsi->num_rxq && !err; i++)
  3742. err = ice_setup_rx_ctx(vsi->rx_rings[i]);
  3743. if (err) {
  3744. dev_err(&vsi->back->pdev->dev, "ice_setup_rx_ctx failed\n");
  3745. return -EIO;
  3746. }
  3747. return err;
  3748. }
  3749. /**
  3750. * ice_vsi_cfg - Setup the VSI
  3751. * @vsi: the VSI being configured
  3752. *
  3753. * Return 0 on success and negative value on error
  3754. */
  3755. static int ice_vsi_cfg(struct ice_vsi *vsi)
  3756. {
  3757. int err;
  3758. if (vsi->netdev) {
  3759. ice_set_rx_mode(vsi->netdev);
  3760. err = ice_restore_vlan(vsi);
  3761. if (err)
  3762. return err;
  3763. }
  3764. err = ice_vsi_cfg_txqs(vsi);
  3765. if (!err)
  3766. err = ice_vsi_cfg_rxqs(vsi);
  3767. return err;
  3768. }
  3769. /**
  3770. * ice_vsi_stop_tx_rings - Disable Tx rings
  3771. * @vsi: the VSI being configured
  3772. */
  3773. static int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
  3774. {
  3775. struct ice_pf *pf = vsi->back;
  3776. struct ice_hw *hw = &pf->hw;
  3777. enum ice_status status;
  3778. u32 *q_teids, val;
  3779. u16 *q_ids, i;
  3780. int err = 0;
  3781. if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
  3782. return -EINVAL;
  3783. q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
  3784. GFP_KERNEL);
  3785. if (!q_teids)
  3786. return -ENOMEM;
  3787. q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
  3788. GFP_KERNEL);
  3789. if (!q_ids) {
  3790. err = -ENOMEM;
  3791. goto err_alloc_q_ids;
  3792. }
  3793. /* set up the tx queue list to be disabled */
  3794. ice_for_each_txq(vsi, i) {
  3795. u16 v_idx;
  3796. if (!vsi->tx_rings || !vsi->tx_rings[i]) {
  3797. err = -EINVAL;
  3798. goto err_out;
  3799. }
  3800. q_ids[i] = vsi->txq_map[i];
  3801. q_teids[i] = vsi->tx_rings[i]->txq_teid;
  3802. /* clear cause_ena bit for disabled queues */
  3803. val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3804. val &= ~QINT_TQCTL_CAUSE_ENA_M;
  3805. wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3806. /* software is expected to wait for 100 ns */
  3807. ndelay(100);
  3808. /* trigger a software interrupt for the vector associated to
  3809. * the queue to schedule napi handler
  3810. */
  3811. v_idx = vsi->tx_rings[i]->q_vector->v_idx;
  3812. wr32(hw, GLINT_DYN_CTL(vsi->base_vector + v_idx),
  3813. GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
  3814. }
  3815. status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
  3816. NULL);
  3817. /* if the disable queue command was exercised during an active reset
  3818. * flow, ICE_ERR_RESET_ONGOING is returned. This is not an error as
  3819. * the reset operation disables queues at the hardware level anyway.
  3820. */
  3821. if (status == ICE_ERR_RESET_ONGOING) {
  3822. dev_dbg(&pf->pdev->dev,
  3823. "Reset in progress. LAN Tx queues already disabled\n");
  3824. } else if (status) {
  3825. dev_err(&pf->pdev->dev,
  3826. "Failed to disable LAN Tx queues, error: %d\n",
  3827. status);
  3828. err = -ENODEV;
  3829. }
  3830. err_out:
  3831. devm_kfree(&pf->pdev->dev, q_ids);
  3832. err_alloc_q_ids:
  3833. devm_kfree(&pf->pdev->dev, q_teids);
  3834. return err;
  3835. }
  3836. /**
  3837. * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3838. * @pf: the PF being configured
  3839. * @pf_q: the PF queue
  3840. * @ena: enable or disable state of the queue
  3841. *
  3842. * This routine will wait for the given Rx queue of the PF to reach the
  3843. * enabled or disabled state.
  3844. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3845. * multiple retries; else will return 0 in case of success.
  3846. */
  3847. static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
  3848. {
  3849. int i;
  3850. for (i = 0; i < ICE_Q_WAIT_RETRY_LIMIT; i++) {
  3851. u32 rx_reg = rd32(&pf->hw, QRX_CTRL(pf_q));
  3852. if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
  3853. break;
  3854. usleep_range(10, 20);
  3855. }
  3856. if (i >= ICE_Q_WAIT_RETRY_LIMIT)
  3857. return -ETIMEDOUT;
  3858. return 0;
  3859. }
  3860. /**
  3861. * ice_vsi_ctrl_rx_rings - Start or stop a VSI's rx rings
  3862. * @vsi: the VSI being configured
  3863. * @ena: start or stop the rx rings
  3864. */
  3865. static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
  3866. {
  3867. struct ice_pf *pf = vsi->back;
  3868. struct ice_hw *hw = &pf->hw;
  3869. int i, j, ret = 0;
  3870. for (i = 0; i < vsi->num_rxq; i++) {
  3871. int pf_q = vsi->rxq_map[i];
  3872. u32 rx_reg;
  3873. for (j = 0; j < ICE_Q_WAIT_MAX_RETRY; j++) {
  3874. rx_reg = rd32(hw, QRX_CTRL(pf_q));
  3875. if (((rx_reg >> QRX_CTRL_QENA_REQ_S) & 1) ==
  3876. ((rx_reg >> QRX_CTRL_QENA_STAT_S) & 1))
  3877. break;
  3878. usleep_range(1000, 2000);
  3879. }
  3880. /* Skip if the queue is already in the requested state */
  3881. if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
  3882. continue;
  3883. /* turn on/off the queue */
  3884. if (ena)
  3885. rx_reg |= QRX_CTRL_QENA_REQ_M;
  3886. else
  3887. rx_reg &= ~QRX_CTRL_QENA_REQ_M;
  3888. wr32(hw, QRX_CTRL(pf_q), rx_reg);
  3889. /* wait for the change to finish */
  3890. ret = ice_pf_rxq_wait(pf, pf_q, ena);
  3891. if (ret) {
  3892. dev_err(&pf->pdev->dev,
  3893. "VSI idx %d Rx ring %d %sable timeout\n",
  3894. vsi->idx, pf_q, (ena ? "en" : "dis"));
  3895. break;
  3896. }
  3897. }
  3898. return ret;
  3899. }
  3900. /**
  3901. * ice_vsi_start_rx_rings - start VSI's rx rings
  3902. * @vsi: the VSI whose rings are to be started
  3903. *
  3904. * Returns 0 on success and a negative value on error
  3905. */
  3906. static int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
  3907. {
  3908. return ice_vsi_ctrl_rx_rings(vsi, true);
  3909. }
  3910. /**
  3911. * ice_vsi_stop_rx_rings - stop VSI's rx rings
  3912. * @vsi: the VSI
  3913. *
  3914. * Returns 0 on success and a negative value on error
  3915. */
  3916. static int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
  3917. {
  3918. return ice_vsi_ctrl_rx_rings(vsi, false);
  3919. }
  3920. /**
  3921. * ice_vsi_stop_tx_rx_rings - stop VSI's tx and rx rings
  3922. * @vsi: the VSI
  3923. * Returns 0 on success and a negative value on error
  3924. */
  3925. static int ice_vsi_stop_tx_rx_rings(struct ice_vsi *vsi)
  3926. {
  3927. int err_tx, err_rx;
  3928. err_tx = ice_vsi_stop_tx_rings(vsi);
  3929. if (err_tx)
  3930. dev_dbg(&vsi->back->pdev->dev, "Failed to disable Tx rings\n");
  3931. err_rx = ice_vsi_stop_rx_rings(vsi);
  3932. if (err_rx)
  3933. dev_dbg(&vsi->back->pdev->dev, "Failed to disable Rx rings\n");
  3934. if (err_tx || err_rx)
  3935. return -EIO;
  3936. return 0;
  3937. }
  3938. /**
  3939. * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3940. * @vsi: the VSI being configured
  3941. */
  3942. static void ice_napi_enable_all(struct ice_vsi *vsi)
  3943. {
  3944. int q_idx;
  3945. if (!vsi->netdev)
  3946. return;
  3947. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3948. napi_enable(&vsi->q_vectors[q_idx]->napi);
  3949. }
  3950. /**
  3951. * ice_up_complete - Finish the last steps of bringing up a connection
  3952. * @vsi: The VSI being configured
  3953. *
  3954. * Return 0 on success and negative value on error
  3955. */
  3956. static int ice_up_complete(struct ice_vsi *vsi)
  3957. {
  3958. struct ice_pf *pf = vsi->back;
  3959. int err;
  3960. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  3961. ice_vsi_cfg_msix(vsi);
  3962. else
  3963. return -ENOTSUPP;
  3964. /* Enable only Rx rings, Tx rings were enabled by the FW when the
  3965. * Tx queue group list was configured and the context bits were
  3966. * programmed using ice_vsi_cfg_txqs
  3967. */
  3968. err = ice_vsi_start_rx_rings(vsi);
  3969. if (err)
  3970. return err;
  3971. clear_bit(__ICE_DOWN, vsi->state);
  3972. ice_napi_enable_all(vsi);
  3973. ice_vsi_ena_irq(vsi);
  3974. if (vsi->port_info &&
  3975. (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
  3976. vsi->netdev) {
  3977. ice_print_link_msg(vsi, true);
  3978. netif_tx_start_all_queues(vsi->netdev);
  3979. netif_carrier_on(vsi->netdev);
  3980. }
  3981. ice_service_task_schedule(pf);
  3982. return err;
  3983. }
  3984. /**
  3985. * ice_up - Bring the connection back up after being down
  3986. * @vsi: VSI being configured
  3987. */
  3988. int ice_up(struct ice_vsi *vsi)
  3989. {
  3990. int err;
  3991. err = ice_vsi_cfg(vsi);
  3992. if (!err)
  3993. err = ice_up_complete(vsi);
  3994. return err;
  3995. }
  3996. /**
  3997. * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
  3998. * @ring: Tx or Rx ring to read stats from
  3999. * @pkts: packets stats counter
  4000. * @bytes: bytes stats counter
  4001. *
  4002. * This function fetches stats from the ring considering the atomic operations
  4003. * that needs to be performed to read u64 values in 32 bit machine.
  4004. */
  4005. static void ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts,
  4006. u64 *bytes)
  4007. {
  4008. unsigned int start;
  4009. *pkts = 0;
  4010. *bytes = 0;
  4011. if (!ring)
  4012. return;
  4013. do {
  4014. start = u64_stats_fetch_begin_irq(&ring->syncp);
  4015. *pkts = ring->stats.pkts;
  4016. *bytes = ring->stats.bytes;
  4017. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  4018. }
  4019. /**
  4020. * ice_stat_update40 - read 40 bit stat from the chip and update stat values
  4021. * @hw: ptr to the hardware info
  4022. * @hireg: high 32 bit HW register to read from
  4023. * @loreg: low 32 bit HW register to read from
  4024. * @prev_stat_loaded: bool to specify if previous stats are loaded
  4025. * @prev_stat: ptr to previous loaded stat value
  4026. * @cur_stat: ptr to current stat value
  4027. */
  4028. static void ice_stat_update40(struct ice_hw *hw, u32 hireg, u32 loreg,
  4029. bool prev_stat_loaded, u64 *prev_stat,
  4030. u64 *cur_stat)
  4031. {
  4032. u64 new_data;
  4033. new_data = rd32(hw, loreg);
  4034. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  4035. /* device stats are not reset at PFR, they likely will not be zeroed
  4036. * when the driver starts. So save the first values read and use them as
  4037. * offsets to be subtracted from the raw values in order to report stats
  4038. * that count from zero.
  4039. */
  4040. if (!prev_stat_loaded)
  4041. *prev_stat = new_data;
  4042. if (likely(new_data >= *prev_stat))
  4043. *cur_stat = new_data - *prev_stat;
  4044. else
  4045. /* to manage the potential roll-over */
  4046. *cur_stat = (new_data + BIT_ULL(40)) - *prev_stat;
  4047. *cur_stat &= 0xFFFFFFFFFFULL;
  4048. }
  4049. /**
  4050. * ice_stat_update32 - read 32 bit stat from the chip and update stat values
  4051. * @hw: ptr to the hardware info
  4052. * @reg: HW register to read from
  4053. * @prev_stat_loaded: bool to specify if previous stats are loaded
  4054. * @prev_stat: ptr to previous loaded stat value
  4055. * @cur_stat: ptr to current stat value
  4056. */
  4057. static void ice_stat_update32(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
  4058. u64 *prev_stat, u64 *cur_stat)
  4059. {
  4060. u32 new_data;
  4061. new_data = rd32(hw, reg);
  4062. /* device stats are not reset at PFR, they likely will not be zeroed
  4063. * when the driver starts. So save the first values read and use them as
  4064. * offsets to be subtracted from the raw values in order to report stats
  4065. * that count from zero.
  4066. */
  4067. if (!prev_stat_loaded)
  4068. *prev_stat = new_data;
  4069. if (likely(new_data >= *prev_stat))
  4070. *cur_stat = new_data - *prev_stat;
  4071. else
  4072. /* to manage the potential roll-over */
  4073. *cur_stat = (new_data + BIT_ULL(32)) - *prev_stat;
  4074. }
  4075. /**
  4076. * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
  4077. * @vsi: the VSI to be updated
  4078. */
  4079. static void ice_update_eth_stats(struct ice_vsi *vsi)
  4080. {
  4081. struct ice_eth_stats *prev_es, *cur_es;
  4082. struct ice_hw *hw = &vsi->back->hw;
  4083. u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
  4084. prev_es = &vsi->eth_stats_prev;
  4085. cur_es = &vsi->eth_stats;
  4086. ice_stat_update40(hw, GLV_GORCH(vsi_num), GLV_GORCL(vsi_num),
  4087. vsi->stat_offsets_loaded, &prev_es->rx_bytes,
  4088. &cur_es->rx_bytes);
  4089. ice_stat_update40(hw, GLV_UPRCH(vsi_num), GLV_UPRCL(vsi_num),
  4090. vsi->stat_offsets_loaded, &prev_es->rx_unicast,
  4091. &cur_es->rx_unicast);
  4092. ice_stat_update40(hw, GLV_MPRCH(vsi_num), GLV_MPRCL(vsi_num),
  4093. vsi->stat_offsets_loaded, &prev_es->rx_multicast,
  4094. &cur_es->rx_multicast);
  4095. ice_stat_update40(hw, GLV_BPRCH(vsi_num), GLV_BPRCL(vsi_num),
  4096. vsi->stat_offsets_loaded, &prev_es->rx_broadcast,
  4097. &cur_es->rx_broadcast);
  4098. ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
  4099. &prev_es->rx_discards, &cur_es->rx_discards);
  4100. ice_stat_update40(hw, GLV_GOTCH(vsi_num), GLV_GOTCL(vsi_num),
  4101. vsi->stat_offsets_loaded, &prev_es->tx_bytes,
  4102. &cur_es->tx_bytes);
  4103. ice_stat_update40(hw, GLV_UPTCH(vsi_num), GLV_UPTCL(vsi_num),
  4104. vsi->stat_offsets_loaded, &prev_es->tx_unicast,
  4105. &cur_es->tx_unicast);
  4106. ice_stat_update40(hw, GLV_MPTCH(vsi_num), GLV_MPTCL(vsi_num),
  4107. vsi->stat_offsets_loaded, &prev_es->tx_multicast,
  4108. &cur_es->tx_multicast);
  4109. ice_stat_update40(hw, GLV_BPTCH(vsi_num), GLV_BPTCL(vsi_num),
  4110. vsi->stat_offsets_loaded, &prev_es->tx_broadcast,
  4111. &cur_es->tx_broadcast);
  4112. ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
  4113. &prev_es->tx_errors, &cur_es->tx_errors);
  4114. vsi->stat_offsets_loaded = true;
  4115. }
  4116. /**
  4117. * ice_update_vsi_ring_stats - Update VSI stats counters
  4118. * @vsi: the VSI to be updated
  4119. */
  4120. static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
  4121. {
  4122. struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
  4123. struct ice_ring *ring;
  4124. u64 pkts, bytes;
  4125. int i;
  4126. /* reset netdev stats */
  4127. vsi_stats->tx_packets = 0;
  4128. vsi_stats->tx_bytes = 0;
  4129. vsi_stats->rx_packets = 0;
  4130. vsi_stats->rx_bytes = 0;
  4131. /* reset non-netdev (extended) stats */
  4132. vsi->tx_restart = 0;
  4133. vsi->tx_busy = 0;
  4134. vsi->tx_linearize = 0;
  4135. vsi->rx_buf_failed = 0;
  4136. vsi->rx_page_failed = 0;
  4137. rcu_read_lock();
  4138. /* update Tx rings counters */
  4139. ice_for_each_txq(vsi, i) {
  4140. ring = READ_ONCE(vsi->tx_rings[i]);
  4141. ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
  4142. vsi_stats->tx_packets += pkts;
  4143. vsi_stats->tx_bytes += bytes;
  4144. vsi->tx_restart += ring->tx_stats.restart_q;
  4145. vsi->tx_busy += ring->tx_stats.tx_busy;
  4146. vsi->tx_linearize += ring->tx_stats.tx_linearize;
  4147. }
  4148. /* update Rx rings counters */
  4149. ice_for_each_rxq(vsi, i) {
  4150. ring = READ_ONCE(vsi->rx_rings[i]);
  4151. ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
  4152. vsi_stats->rx_packets += pkts;
  4153. vsi_stats->rx_bytes += bytes;
  4154. vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
  4155. vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
  4156. }
  4157. rcu_read_unlock();
  4158. }
  4159. /**
  4160. * ice_update_vsi_stats - Update VSI stats counters
  4161. * @vsi: the VSI to be updated
  4162. */
  4163. static void ice_update_vsi_stats(struct ice_vsi *vsi)
  4164. {
  4165. struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
  4166. struct ice_eth_stats *cur_es = &vsi->eth_stats;
  4167. struct ice_pf *pf = vsi->back;
  4168. if (test_bit(__ICE_DOWN, vsi->state) ||
  4169. test_bit(__ICE_CFG_BUSY, pf->state))
  4170. return;
  4171. /* get stats as recorded by Tx/Rx rings */
  4172. ice_update_vsi_ring_stats(vsi);
  4173. /* get VSI stats as recorded by the hardware */
  4174. ice_update_eth_stats(vsi);
  4175. cur_ns->tx_errors = cur_es->tx_errors;
  4176. cur_ns->rx_dropped = cur_es->rx_discards;
  4177. cur_ns->tx_dropped = cur_es->tx_discards;
  4178. cur_ns->multicast = cur_es->rx_multicast;
  4179. /* update some more netdev stats if this is main VSI */
  4180. if (vsi->type == ICE_VSI_PF) {
  4181. cur_ns->rx_crc_errors = pf->stats.crc_errors;
  4182. cur_ns->rx_errors = pf->stats.crc_errors +
  4183. pf->stats.illegal_bytes;
  4184. cur_ns->rx_length_errors = pf->stats.rx_len_errors;
  4185. }
  4186. }
  4187. /**
  4188. * ice_update_pf_stats - Update PF port stats counters
  4189. * @pf: PF whose stats needs to be updated
  4190. */
  4191. static void ice_update_pf_stats(struct ice_pf *pf)
  4192. {
  4193. struct ice_hw_port_stats *prev_ps, *cur_ps;
  4194. struct ice_hw *hw = &pf->hw;
  4195. u8 pf_id;
  4196. prev_ps = &pf->stats_prev;
  4197. cur_ps = &pf->stats;
  4198. pf_id = hw->pf_id;
  4199. ice_stat_update40(hw, GLPRT_GORCH(pf_id), GLPRT_GORCL(pf_id),
  4200. pf->stat_prev_loaded, &prev_ps->eth.rx_bytes,
  4201. &cur_ps->eth.rx_bytes);
  4202. ice_stat_update40(hw, GLPRT_UPRCH(pf_id), GLPRT_UPRCL(pf_id),
  4203. pf->stat_prev_loaded, &prev_ps->eth.rx_unicast,
  4204. &cur_ps->eth.rx_unicast);
  4205. ice_stat_update40(hw, GLPRT_MPRCH(pf_id), GLPRT_MPRCL(pf_id),
  4206. pf->stat_prev_loaded, &prev_ps->eth.rx_multicast,
  4207. &cur_ps->eth.rx_multicast);
  4208. ice_stat_update40(hw, GLPRT_BPRCH(pf_id), GLPRT_BPRCL(pf_id),
  4209. pf->stat_prev_loaded, &prev_ps->eth.rx_broadcast,
  4210. &cur_ps->eth.rx_broadcast);
  4211. ice_stat_update40(hw, GLPRT_GOTCH(pf_id), GLPRT_GOTCL(pf_id),
  4212. pf->stat_prev_loaded, &prev_ps->eth.tx_bytes,
  4213. &cur_ps->eth.tx_bytes);
  4214. ice_stat_update40(hw, GLPRT_UPTCH(pf_id), GLPRT_UPTCL(pf_id),
  4215. pf->stat_prev_loaded, &prev_ps->eth.tx_unicast,
  4216. &cur_ps->eth.tx_unicast);
  4217. ice_stat_update40(hw, GLPRT_MPTCH(pf_id), GLPRT_MPTCL(pf_id),
  4218. pf->stat_prev_loaded, &prev_ps->eth.tx_multicast,
  4219. &cur_ps->eth.tx_multicast);
  4220. ice_stat_update40(hw, GLPRT_BPTCH(pf_id), GLPRT_BPTCL(pf_id),
  4221. pf->stat_prev_loaded, &prev_ps->eth.tx_broadcast,
  4222. &cur_ps->eth.tx_broadcast);
  4223. ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
  4224. &prev_ps->tx_dropped_link_down,
  4225. &cur_ps->tx_dropped_link_down);
  4226. ice_stat_update40(hw, GLPRT_PRC64H(pf_id), GLPRT_PRC64L(pf_id),
  4227. pf->stat_prev_loaded, &prev_ps->rx_size_64,
  4228. &cur_ps->rx_size_64);
  4229. ice_stat_update40(hw, GLPRT_PRC127H(pf_id), GLPRT_PRC127L(pf_id),
  4230. pf->stat_prev_loaded, &prev_ps->rx_size_127,
  4231. &cur_ps->rx_size_127);
  4232. ice_stat_update40(hw, GLPRT_PRC255H(pf_id), GLPRT_PRC255L(pf_id),
  4233. pf->stat_prev_loaded, &prev_ps->rx_size_255,
  4234. &cur_ps->rx_size_255);
  4235. ice_stat_update40(hw, GLPRT_PRC511H(pf_id), GLPRT_PRC511L(pf_id),
  4236. pf->stat_prev_loaded, &prev_ps->rx_size_511,
  4237. &cur_ps->rx_size_511);
  4238. ice_stat_update40(hw, GLPRT_PRC1023H(pf_id),
  4239. GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
  4240. &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
  4241. ice_stat_update40(hw, GLPRT_PRC1522H(pf_id),
  4242. GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
  4243. &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
  4244. ice_stat_update40(hw, GLPRT_PRC9522H(pf_id),
  4245. GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
  4246. &prev_ps->rx_size_big, &cur_ps->rx_size_big);
  4247. ice_stat_update40(hw, GLPRT_PTC64H(pf_id), GLPRT_PTC64L(pf_id),
  4248. pf->stat_prev_loaded, &prev_ps->tx_size_64,
  4249. &cur_ps->tx_size_64);
  4250. ice_stat_update40(hw, GLPRT_PTC127H(pf_id), GLPRT_PTC127L(pf_id),
  4251. pf->stat_prev_loaded, &prev_ps->tx_size_127,
  4252. &cur_ps->tx_size_127);
  4253. ice_stat_update40(hw, GLPRT_PTC255H(pf_id), GLPRT_PTC255L(pf_id),
  4254. pf->stat_prev_loaded, &prev_ps->tx_size_255,
  4255. &cur_ps->tx_size_255);
  4256. ice_stat_update40(hw, GLPRT_PTC511H(pf_id), GLPRT_PTC511L(pf_id),
  4257. pf->stat_prev_loaded, &prev_ps->tx_size_511,
  4258. &cur_ps->tx_size_511);
  4259. ice_stat_update40(hw, GLPRT_PTC1023H(pf_id),
  4260. GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
  4261. &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
  4262. ice_stat_update40(hw, GLPRT_PTC1522H(pf_id),
  4263. GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
  4264. &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
  4265. ice_stat_update40(hw, GLPRT_PTC9522H(pf_id),
  4266. GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
  4267. &prev_ps->tx_size_big, &cur_ps->tx_size_big);
  4268. ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
  4269. &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
  4270. ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
  4271. &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
  4272. ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
  4273. &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
  4274. ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
  4275. &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
  4276. ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
  4277. &prev_ps->crc_errors, &cur_ps->crc_errors);
  4278. ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
  4279. &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
  4280. ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
  4281. &prev_ps->mac_local_faults,
  4282. &cur_ps->mac_local_faults);
  4283. ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
  4284. &prev_ps->mac_remote_faults,
  4285. &cur_ps->mac_remote_faults);
  4286. ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
  4287. &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
  4288. ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
  4289. &prev_ps->rx_undersize, &cur_ps->rx_undersize);
  4290. ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
  4291. &prev_ps->rx_fragments, &cur_ps->rx_fragments);
  4292. ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
  4293. &prev_ps->rx_oversize, &cur_ps->rx_oversize);
  4294. ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
  4295. &prev_ps->rx_jabber, &cur_ps->rx_jabber);
  4296. pf->stat_prev_loaded = true;
  4297. }
  4298. /**
  4299. * ice_get_stats64 - get statistics for network device structure
  4300. * @netdev: network interface device structure
  4301. * @stats: main device statistics structure
  4302. */
  4303. static
  4304. void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
  4305. {
  4306. struct ice_netdev_priv *np = netdev_priv(netdev);
  4307. struct rtnl_link_stats64 *vsi_stats;
  4308. struct ice_vsi *vsi = np->vsi;
  4309. vsi_stats = &vsi->net_stats;
  4310. if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
  4311. return;
  4312. /* netdev packet/byte stats come from ring counter. These are obtained
  4313. * by summing up ring counters (done by ice_update_vsi_ring_stats).
  4314. */
  4315. ice_update_vsi_ring_stats(vsi);
  4316. stats->tx_packets = vsi_stats->tx_packets;
  4317. stats->tx_bytes = vsi_stats->tx_bytes;
  4318. stats->rx_packets = vsi_stats->rx_packets;
  4319. stats->rx_bytes = vsi_stats->rx_bytes;
  4320. /* The rest of the stats can be read from the hardware but instead we
  4321. * just return values that the watchdog task has already obtained from
  4322. * the hardware.
  4323. */
  4324. stats->multicast = vsi_stats->multicast;
  4325. stats->tx_errors = vsi_stats->tx_errors;
  4326. stats->tx_dropped = vsi_stats->tx_dropped;
  4327. stats->rx_errors = vsi_stats->rx_errors;
  4328. stats->rx_dropped = vsi_stats->rx_dropped;
  4329. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  4330. stats->rx_length_errors = vsi_stats->rx_length_errors;
  4331. }
  4332. /**
  4333. * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  4334. * @vsi: VSI having NAPI disabled
  4335. */
  4336. static void ice_napi_disable_all(struct ice_vsi *vsi)
  4337. {
  4338. int q_idx;
  4339. if (!vsi->netdev)
  4340. return;
  4341. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  4342. napi_disable(&vsi->q_vectors[q_idx]->napi);
  4343. }
  4344. /**
  4345. * ice_down - Shutdown the connection
  4346. * @vsi: The VSI being stopped
  4347. */
  4348. int ice_down(struct ice_vsi *vsi)
  4349. {
  4350. int i, err;
  4351. /* Caller of this function is expected to set the
  4352. * vsi->state __ICE_DOWN bit
  4353. */
  4354. if (vsi->netdev) {
  4355. netif_carrier_off(vsi->netdev);
  4356. netif_tx_disable(vsi->netdev);
  4357. }
  4358. ice_vsi_dis_irq(vsi);
  4359. err = ice_vsi_stop_tx_rx_rings(vsi);
  4360. ice_napi_disable_all(vsi);
  4361. ice_for_each_txq(vsi, i)
  4362. ice_clean_tx_ring(vsi->tx_rings[i]);
  4363. ice_for_each_rxq(vsi, i)
  4364. ice_clean_rx_ring(vsi->rx_rings[i]);
  4365. if (err)
  4366. netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
  4367. vsi->vsi_num, vsi->vsw->sw_id);
  4368. return err;
  4369. }
  4370. /**
  4371. * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
  4372. * @vsi: VSI having resources allocated
  4373. *
  4374. * Return 0 on success, negative on failure
  4375. */
  4376. static int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
  4377. {
  4378. int i, err = 0;
  4379. if (!vsi->num_txq) {
  4380. dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
  4381. vsi->vsi_num);
  4382. return -EINVAL;
  4383. }
  4384. ice_for_each_txq(vsi, i) {
  4385. err = ice_setup_tx_ring(vsi->tx_rings[i]);
  4386. if (err)
  4387. break;
  4388. }
  4389. return err;
  4390. }
  4391. /**
  4392. * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
  4393. * @vsi: VSI having resources allocated
  4394. *
  4395. * Return 0 on success, negative on failure
  4396. */
  4397. static int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
  4398. {
  4399. int i, err = 0;
  4400. if (!vsi->num_rxq) {
  4401. dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
  4402. vsi->vsi_num);
  4403. return -EINVAL;
  4404. }
  4405. ice_for_each_rxq(vsi, i) {
  4406. err = ice_setup_rx_ring(vsi->rx_rings[i]);
  4407. if (err)
  4408. break;
  4409. }
  4410. return err;
  4411. }
  4412. /**
  4413. * ice_vsi_req_irq - Request IRQ from the OS
  4414. * @vsi: The VSI IRQ is being requested for
  4415. * @basename: name for the vector
  4416. *
  4417. * Return 0 on success and a negative value on error
  4418. */
  4419. static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
  4420. {
  4421. struct ice_pf *pf = vsi->back;
  4422. int err = -EINVAL;
  4423. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  4424. err = ice_vsi_req_irq_msix(vsi, basename);
  4425. return err;
  4426. }
  4427. /**
  4428. * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
  4429. * @vsi: the VSI having resources freed
  4430. */
  4431. static void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
  4432. {
  4433. int i;
  4434. if (!vsi->tx_rings)
  4435. return;
  4436. ice_for_each_txq(vsi, i)
  4437. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  4438. ice_free_tx_ring(vsi->tx_rings[i]);
  4439. }
  4440. /**
  4441. * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
  4442. * @vsi: the VSI having resources freed
  4443. */
  4444. static void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
  4445. {
  4446. int i;
  4447. if (!vsi->rx_rings)
  4448. return;
  4449. ice_for_each_rxq(vsi, i)
  4450. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  4451. ice_free_rx_ring(vsi->rx_rings[i]);
  4452. }
  4453. /**
  4454. * ice_vsi_open - Called when a network interface is made active
  4455. * @vsi: the VSI to open
  4456. *
  4457. * Initialization of the VSI
  4458. *
  4459. * Returns 0 on success, negative value on error
  4460. */
  4461. static int ice_vsi_open(struct ice_vsi *vsi)
  4462. {
  4463. char int_name[ICE_INT_NAME_STR_LEN];
  4464. struct ice_pf *pf = vsi->back;
  4465. int err;
  4466. /* allocate descriptors */
  4467. err = ice_vsi_setup_tx_rings(vsi);
  4468. if (err)
  4469. goto err_setup_tx;
  4470. err = ice_vsi_setup_rx_rings(vsi);
  4471. if (err)
  4472. goto err_setup_rx;
  4473. err = ice_vsi_cfg(vsi);
  4474. if (err)
  4475. goto err_setup_rx;
  4476. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  4477. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  4478. err = ice_vsi_req_irq(vsi, int_name);
  4479. if (err)
  4480. goto err_setup_rx;
  4481. /* Notify the stack of the actual queue counts. */
  4482. err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
  4483. if (err)
  4484. goto err_set_qs;
  4485. err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
  4486. if (err)
  4487. goto err_set_qs;
  4488. err = ice_up_complete(vsi);
  4489. if (err)
  4490. goto err_up_complete;
  4491. return 0;
  4492. err_up_complete:
  4493. ice_down(vsi);
  4494. err_set_qs:
  4495. ice_vsi_free_irq(vsi);
  4496. err_setup_rx:
  4497. ice_vsi_free_rx_rings(vsi);
  4498. err_setup_tx:
  4499. ice_vsi_free_tx_rings(vsi);
  4500. return err;
  4501. }
  4502. /**
  4503. * ice_vsi_close - Shut down a VSI
  4504. * @vsi: the VSI being shut down
  4505. */
  4506. static void ice_vsi_close(struct ice_vsi *vsi)
  4507. {
  4508. if (!test_and_set_bit(__ICE_DOWN, vsi->state))
  4509. ice_down(vsi);
  4510. ice_vsi_free_irq(vsi);
  4511. ice_vsi_free_tx_rings(vsi);
  4512. ice_vsi_free_rx_rings(vsi);
  4513. }
  4514. /**
  4515. * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
  4516. * @vsi: the VSI being removed
  4517. */
  4518. static void ice_rss_clean(struct ice_vsi *vsi)
  4519. {
  4520. struct ice_pf *pf;
  4521. pf = vsi->back;
  4522. if (vsi->rss_hkey_user)
  4523. devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
  4524. if (vsi->rss_lut_user)
  4525. devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
  4526. }
  4527. /**
  4528. * ice_vsi_release - Delete a VSI and free its resources
  4529. * @vsi: the VSI being removed
  4530. *
  4531. * Returns 0 on success or < 0 on error
  4532. */
  4533. static int ice_vsi_release(struct ice_vsi *vsi)
  4534. {
  4535. struct ice_pf *pf;
  4536. if (!vsi->back)
  4537. return -ENODEV;
  4538. pf = vsi->back;
  4539. /* do not unregister and free netdevs while driver is in the reset
  4540. * recovery pending state. Since reset/rebuild happens through PF
  4541. * service task workqueue, its not a good idea to unregister netdev
  4542. * that is associated to the PF that is running the work queue items
  4543. * currently. This is done to avoid check_flush_dependency() warning
  4544. * on this wq
  4545. */
  4546. if (vsi->netdev && !ice_is_reset_recovery_pending(pf->state)) {
  4547. unregister_netdev(vsi->netdev);
  4548. free_netdev(vsi->netdev);
  4549. vsi->netdev = NULL;
  4550. }
  4551. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  4552. ice_rss_clean(vsi);
  4553. /* Disable VSI and free resources */
  4554. ice_vsi_dis_irq(vsi);
  4555. ice_vsi_close(vsi);
  4556. /* reclaim interrupt vectors back to PF */
  4557. ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
  4558. pf->num_avail_msix += vsi->num_q_vectors;
  4559. ice_remove_vsi_fltr(&pf->hw, vsi->vsi_num);
  4560. ice_vsi_delete(vsi);
  4561. ice_vsi_free_q_vectors(vsi);
  4562. ice_vsi_clear_rings(vsi);
  4563. ice_vsi_put_qs(vsi);
  4564. pf->q_left_tx += vsi->alloc_txq;
  4565. pf->q_left_rx += vsi->alloc_rxq;
  4566. /* retain SW VSI data structure since it is needed to unregister and
  4567. * free VSI netdev when PF is not in reset recovery pending state,\
  4568. * for ex: during rmmod.
  4569. */
  4570. if (!ice_is_reset_recovery_pending(pf->state))
  4571. ice_vsi_clear(vsi);
  4572. return 0;
  4573. }
  4574. /**
  4575. * ice_vsi_release_all - Delete all VSIs
  4576. * @pf: PF from which all VSIs are being removed
  4577. */
  4578. static void ice_vsi_release_all(struct ice_pf *pf)
  4579. {
  4580. int err, i;
  4581. if (!pf->vsi)
  4582. return;
  4583. for (i = 0; i < pf->num_alloc_vsi; i++) {
  4584. if (!pf->vsi[i])
  4585. continue;
  4586. err = ice_vsi_release(pf->vsi[i]);
  4587. if (err)
  4588. dev_dbg(&pf->pdev->dev,
  4589. "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
  4590. i, err, pf->vsi[i]->vsi_num);
  4591. }
  4592. }
  4593. /**
  4594. * ice_dis_vsi - pause a VSI
  4595. * @vsi: the VSI being paused
  4596. */
  4597. static void ice_dis_vsi(struct ice_vsi *vsi)
  4598. {
  4599. if (test_bit(__ICE_DOWN, vsi->state))
  4600. return;
  4601. set_bit(__ICE_NEEDS_RESTART, vsi->state);
  4602. if (vsi->netdev && netif_running(vsi->netdev) &&
  4603. vsi->type == ICE_VSI_PF) {
  4604. rtnl_lock();
  4605. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4606. rtnl_unlock();
  4607. } else {
  4608. ice_vsi_close(vsi);
  4609. }
  4610. }
  4611. /**
  4612. * ice_ena_vsi - resume a VSI
  4613. * @vsi: the VSI being resume
  4614. */
  4615. static int ice_ena_vsi(struct ice_vsi *vsi)
  4616. {
  4617. int err = 0;
  4618. if (test_and_clear_bit(__ICE_NEEDS_RESTART, vsi->state))
  4619. if (vsi->netdev && netif_running(vsi->netdev)) {
  4620. rtnl_lock();
  4621. err = vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4622. rtnl_unlock();
  4623. }
  4624. return err;
  4625. }
  4626. /**
  4627. * ice_pf_dis_all_vsi - Pause all VSIs on a PF
  4628. * @pf: the PF
  4629. */
  4630. static void ice_pf_dis_all_vsi(struct ice_pf *pf)
  4631. {
  4632. int v;
  4633. ice_for_each_vsi(pf, v)
  4634. if (pf->vsi[v])
  4635. ice_dis_vsi(pf->vsi[v]);
  4636. }
  4637. /**
  4638. * ice_pf_ena_all_vsi - Resume all VSIs on a PF
  4639. * @pf: the PF
  4640. */
  4641. static int ice_pf_ena_all_vsi(struct ice_pf *pf)
  4642. {
  4643. int v;
  4644. ice_for_each_vsi(pf, v)
  4645. if (pf->vsi[v])
  4646. if (ice_ena_vsi(pf->vsi[v]))
  4647. return -EIO;
  4648. return 0;
  4649. }
  4650. /**
  4651. * ice_vsi_rebuild_all - rebuild all VSIs in pf
  4652. * @pf: the PF
  4653. */
  4654. static int ice_vsi_rebuild_all(struct ice_pf *pf)
  4655. {
  4656. int i;
  4657. /* loop through pf->vsi array and reinit the VSI if found */
  4658. for (i = 0; i < pf->num_alloc_vsi; i++) {
  4659. int err;
  4660. if (!pf->vsi[i])
  4661. continue;
  4662. err = ice_vsi_rebuild(pf->vsi[i]);
  4663. if (err) {
  4664. dev_err(&pf->pdev->dev,
  4665. "VSI at index %d rebuild failed\n",
  4666. pf->vsi[i]->idx);
  4667. return err;
  4668. }
  4669. dev_info(&pf->pdev->dev,
  4670. "VSI at index %d rebuilt. vsi_num = 0x%x\n",
  4671. pf->vsi[i]->idx, pf->vsi[i]->vsi_num);
  4672. }
  4673. return 0;
  4674. }
  4675. /**
  4676. * ice_rebuild - rebuild after reset
  4677. * @pf: pf to rebuild
  4678. */
  4679. static void ice_rebuild(struct ice_pf *pf)
  4680. {
  4681. struct device *dev = &pf->pdev->dev;
  4682. struct ice_hw *hw = &pf->hw;
  4683. enum ice_status ret;
  4684. int err;
  4685. if (test_bit(__ICE_DOWN, pf->state))
  4686. goto clear_recovery;
  4687. dev_dbg(dev, "rebuilding pf\n");
  4688. ret = ice_init_all_ctrlq(hw);
  4689. if (ret) {
  4690. dev_err(dev, "control queues init failed %d\n", ret);
  4691. goto err_init_ctrlq;
  4692. }
  4693. ret = ice_clear_pf_cfg(hw);
  4694. if (ret) {
  4695. dev_err(dev, "clear PF configuration failed %d\n", ret);
  4696. goto err_init_ctrlq;
  4697. }
  4698. ice_clear_pxe_mode(hw);
  4699. ret = ice_get_caps(hw);
  4700. if (ret) {
  4701. dev_err(dev, "ice_get_caps failed %d\n", ret);
  4702. goto err_init_ctrlq;
  4703. }
  4704. err = ice_sched_init_port(hw->port_info);
  4705. if (err)
  4706. goto err_sched_init_port;
  4707. err = ice_vsi_rebuild_all(pf);
  4708. if (err) {
  4709. dev_err(dev, "ice_vsi_rebuild_all failed\n");
  4710. goto err_vsi_rebuild;
  4711. }
  4712. ret = ice_replay_all_fltr(&pf->hw);
  4713. if (ret) {
  4714. dev_err(&pf->pdev->dev,
  4715. "error replaying switch filter rules\n");
  4716. goto err_vsi_rebuild;
  4717. }
  4718. /* start misc vector */
  4719. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  4720. err = ice_req_irq_msix_misc(pf);
  4721. if (err) {
  4722. dev_err(dev, "misc vector setup failed: %d\n", err);
  4723. goto err_vsi_rebuild;
  4724. }
  4725. }
  4726. /* restart the VSIs that were rebuilt and running before the reset */
  4727. err = ice_pf_ena_all_vsi(pf);
  4728. if (err) {
  4729. dev_err(&pf->pdev->dev, "error enabling VSIs\n");
  4730. /* no need to disable VSIs in tear down path in ice_rebuild()
  4731. * since its already taken care in ice_vsi_open()
  4732. */
  4733. goto err_vsi_rebuild;
  4734. }
  4735. /* if we get here, reset flow is successful */
  4736. clear_bit(__ICE_RESET_FAILED, pf->state);
  4737. return;
  4738. err_vsi_rebuild:
  4739. ice_vsi_release_all(pf);
  4740. err_sched_init_port:
  4741. ice_sched_cleanup_all(hw);
  4742. err_init_ctrlq:
  4743. ice_shutdown_all_ctrlq(hw);
  4744. set_bit(__ICE_RESET_FAILED, pf->state);
  4745. clear_recovery:
  4746. /* set this bit in PF state to control service task scheduling */
  4747. set_bit(__ICE_NEEDS_RESTART, pf->state);
  4748. dev_err(dev, "Rebuild failed, unload and reload driver\n");
  4749. }
  4750. /**
  4751. * ice_change_mtu - NDO callback to change the MTU
  4752. * @netdev: network interface device structure
  4753. * @new_mtu: new value for maximum frame size
  4754. *
  4755. * Returns 0 on success, negative on failure
  4756. */
  4757. static int ice_change_mtu(struct net_device *netdev, int new_mtu)
  4758. {
  4759. struct ice_netdev_priv *np = netdev_priv(netdev);
  4760. struct ice_vsi *vsi = np->vsi;
  4761. struct ice_pf *pf = vsi->back;
  4762. u8 count = 0;
  4763. if (new_mtu == netdev->mtu) {
  4764. netdev_warn(netdev, "mtu is already %u\n", netdev->mtu);
  4765. return 0;
  4766. }
  4767. if (new_mtu < netdev->min_mtu) {
  4768. netdev_err(netdev, "new mtu invalid. min_mtu is %d\n",
  4769. netdev->min_mtu);
  4770. return -EINVAL;
  4771. } else if (new_mtu > netdev->max_mtu) {
  4772. netdev_err(netdev, "new mtu invalid. max_mtu is %d\n",
  4773. netdev->min_mtu);
  4774. return -EINVAL;
  4775. }
  4776. /* if a reset is in progress, wait for some time for it to complete */
  4777. do {
  4778. if (ice_is_reset_recovery_pending(pf->state)) {
  4779. count++;
  4780. usleep_range(1000, 2000);
  4781. } else {
  4782. break;
  4783. }
  4784. } while (count < 100);
  4785. if (count == 100) {
  4786. netdev_err(netdev, "can't change mtu. Device is busy\n");
  4787. return -EBUSY;
  4788. }
  4789. netdev->mtu = new_mtu;
  4790. /* if VSI is up, bring it down and then back up */
  4791. if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
  4792. int err;
  4793. err = ice_down(vsi);
  4794. if (err) {
  4795. netdev_err(netdev, "change mtu if_up err %d\n", err);
  4796. return err;
  4797. }
  4798. err = ice_up(vsi);
  4799. if (err) {
  4800. netdev_err(netdev, "change mtu if_up err %d\n", err);
  4801. return err;
  4802. }
  4803. }
  4804. netdev_dbg(netdev, "changed mtu to %d\n", new_mtu);
  4805. return 0;
  4806. }
  4807. /**
  4808. * ice_set_rss - Set RSS keys and lut
  4809. * @vsi: Pointer to VSI structure
  4810. * @seed: RSS hash seed
  4811. * @lut: Lookup table
  4812. * @lut_size: Lookup table size
  4813. *
  4814. * Returns 0 on success, negative on failure
  4815. */
  4816. int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  4817. {
  4818. struct ice_pf *pf = vsi->back;
  4819. struct ice_hw *hw = &pf->hw;
  4820. enum ice_status status;
  4821. if (seed) {
  4822. struct ice_aqc_get_set_rss_keys *buf =
  4823. (struct ice_aqc_get_set_rss_keys *)seed;
  4824. status = ice_aq_set_rss_key(hw, vsi->vsi_num, buf);
  4825. if (status) {
  4826. dev_err(&pf->pdev->dev,
  4827. "Cannot set RSS key, err %d aq_err %d\n",
  4828. status, hw->adminq.rq_last_status);
  4829. return -EIO;
  4830. }
  4831. }
  4832. if (lut) {
  4833. status = ice_aq_set_rss_lut(hw, vsi->vsi_num,
  4834. vsi->rss_lut_type, lut, lut_size);
  4835. if (status) {
  4836. dev_err(&pf->pdev->dev,
  4837. "Cannot set RSS lut, err %d aq_err %d\n",
  4838. status, hw->adminq.rq_last_status);
  4839. return -EIO;
  4840. }
  4841. }
  4842. return 0;
  4843. }
  4844. /**
  4845. * ice_get_rss - Get RSS keys and lut
  4846. * @vsi: Pointer to VSI structure
  4847. * @seed: Buffer to store the keys
  4848. * @lut: Buffer to store the lookup table entries
  4849. * @lut_size: Size of buffer to store the lookup table entries
  4850. *
  4851. * Returns 0 on success, negative on failure
  4852. */
  4853. int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  4854. {
  4855. struct ice_pf *pf = vsi->back;
  4856. struct ice_hw *hw = &pf->hw;
  4857. enum ice_status status;
  4858. if (seed) {
  4859. struct ice_aqc_get_set_rss_keys *buf =
  4860. (struct ice_aqc_get_set_rss_keys *)seed;
  4861. status = ice_aq_get_rss_key(hw, vsi->vsi_num, buf);
  4862. if (status) {
  4863. dev_err(&pf->pdev->dev,
  4864. "Cannot get RSS key, err %d aq_err %d\n",
  4865. status, hw->adminq.rq_last_status);
  4866. return -EIO;
  4867. }
  4868. }
  4869. if (lut) {
  4870. status = ice_aq_get_rss_lut(hw, vsi->vsi_num,
  4871. vsi->rss_lut_type, lut, lut_size);
  4872. if (status) {
  4873. dev_err(&pf->pdev->dev,
  4874. "Cannot get RSS lut, err %d aq_err %d\n",
  4875. status, hw->adminq.rq_last_status);
  4876. return -EIO;
  4877. }
  4878. }
  4879. return 0;
  4880. }
  4881. /**
  4882. * ice_bridge_getlink - Get the hardware bridge mode
  4883. * @skb: skb buff
  4884. * @pid: process id
  4885. * @seq: RTNL message seq
  4886. * @dev: the netdev being configured
  4887. * @filter_mask: filter mask passed in
  4888. * @nlflags: netlink flags passed in
  4889. *
  4890. * Return the bridge mode (VEB/VEPA)
  4891. */
  4892. static int
  4893. ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  4894. struct net_device *dev, u32 filter_mask, int nlflags)
  4895. {
  4896. struct ice_netdev_priv *np = netdev_priv(dev);
  4897. struct ice_vsi *vsi = np->vsi;
  4898. struct ice_pf *pf = vsi->back;
  4899. u16 bmode;
  4900. bmode = pf->first_sw->bridge_mode;
  4901. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
  4902. filter_mask, NULL);
  4903. }
  4904. /**
  4905. * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
  4906. * @vsi: Pointer to VSI structure
  4907. * @bmode: Hardware bridge mode (VEB/VEPA)
  4908. *
  4909. * Returns 0 on success, negative on failure
  4910. */
  4911. static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
  4912. {
  4913. struct device *dev = &vsi->back->pdev->dev;
  4914. struct ice_aqc_vsi_props *vsi_props;
  4915. struct ice_hw *hw = &vsi->back->hw;
  4916. struct ice_vsi_ctx ctxt = { 0 };
  4917. enum ice_status status;
  4918. vsi_props = &vsi->info;
  4919. ctxt.info = vsi->info;
  4920. if (bmode == BRIDGE_MODE_VEB)
  4921. /* change from VEPA to VEB mode */
  4922. ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
  4923. else
  4924. /* change from VEB to VEPA mode */
  4925. ctxt.info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
  4926. ctxt.vsi_num = vsi->vsi_num;
  4927. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
  4928. status = ice_aq_update_vsi(hw, &ctxt, NULL);
  4929. if (status) {
  4930. dev_err(dev, "update VSI for bridge mode failed, bmode = %d err %d aq_err %d\n",
  4931. bmode, status, hw->adminq.sq_last_status);
  4932. return -EIO;
  4933. }
  4934. /* Update sw flags for book keeping */
  4935. vsi_props->sw_flags = ctxt.info.sw_flags;
  4936. return 0;
  4937. }
  4938. /**
  4939. * ice_bridge_setlink - Set the hardware bridge mode
  4940. * @dev: the netdev being configured
  4941. * @nlh: RTNL message
  4942. * @flags: bridge setlink flags
  4943. *
  4944. * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
  4945. * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
  4946. * not already set for all VSIs connected to this switch. And also update the
  4947. * unicast switch filter rules for the corresponding switch of the netdev.
  4948. */
  4949. static int
  4950. ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
  4951. u16 __always_unused flags)
  4952. {
  4953. struct ice_netdev_priv *np = netdev_priv(dev);
  4954. struct ice_pf *pf = np->vsi->back;
  4955. struct nlattr *attr, *br_spec;
  4956. struct ice_hw *hw = &pf->hw;
  4957. enum ice_status status;
  4958. struct ice_sw *pf_sw;
  4959. int rem, v, err = 0;
  4960. pf_sw = pf->first_sw;
  4961. /* find the attribute in the netlink message */
  4962. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  4963. nla_for_each_nested(attr, br_spec, rem) {
  4964. __u16 mode;
  4965. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  4966. continue;
  4967. mode = nla_get_u16(attr);
  4968. if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
  4969. return -EINVAL;
  4970. /* Continue if bridge mode is not being flipped */
  4971. if (mode == pf_sw->bridge_mode)
  4972. continue;
  4973. /* Iterates through the PF VSI list and update the loopback
  4974. * mode of the VSI
  4975. */
  4976. ice_for_each_vsi(pf, v) {
  4977. if (!pf->vsi[v])
  4978. continue;
  4979. err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
  4980. if (err)
  4981. return err;
  4982. }
  4983. hw->evb_veb = (mode == BRIDGE_MODE_VEB);
  4984. /* Update the unicast switch filter rules for the corresponding
  4985. * switch of the netdev
  4986. */
  4987. status = ice_update_sw_rule_bridge_mode(hw);
  4988. if (status) {
  4989. netdev_err(dev, "update SW_RULE for bridge mode failed, = %d err %d aq_err %d\n",
  4990. mode, status, hw->adminq.sq_last_status);
  4991. /* revert hw->evb_veb */
  4992. hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
  4993. return -EIO;
  4994. }
  4995. pf_sw->bridge_mode = mode;
  4996. }
  4997. return 0;
  4998. }
  4999. /**
  5000. * ice_tx_timeout - Respond to a Tx Hang
  5001. * @netdev: network interface device structure
  5002. */
  5003. static void ice_tx_timeout(struct net_device *netdev)
  5004. {
  5005. struct ice_netdev_priv *np = netdev_priv(netdev);
  5006. struct ice_ring *tx_ring = NULL;
  5007. struct ice_vsi *vsi = np->vsi;
  5008. struct ice_pf *pf = vsi->back;
  5009. u32 head, val = 0, i;
  5010. int hung_queue = -1;
  5011. pf->tx_timeout_count++;
  5012. /* find the stopped queue the same way the stack does */
  5013. for (i = 0; i < netdev->num_tx_queues; i++) {
  5014. struct netdev_queue *q;
  5015. unsigned long trans_start;
  5016. q = netdev_get_tx_queue(netdev, i);
  5017. trans_start = q->trans_start;
  5018. if (netif_xmit_stopped(q) &&
  5019. time_after(jiffies,
  5020. (trans_start + netdev->watchdog_timeo))) {
  5021. hung_queue = i;
  5022. break;
  5023. }
  5024. }
  5025. if (i == netdev->num_tx_queues) {
  5026. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  5027. } else {
  5028. /* now that we have an index, find the tx_ring struct */
  5029. for (i = 0; i < vsi->num_txq; i++) {
  5030. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  5031. if (hung_queue ==
  5032. vsi->tx_rings[i]->q_index) {
  5033. tx_ring = vsi->tx_rings[i];
  5034. break;
  5035. }
  5036. }
  5037. }
  5038. }
  5039. /* Reset recovery level if enough time has elapsed after last timeout.
  5040. * Also ensure no new reset action happens before next timeout period.
  5041. */
  5042. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
  5043. pf->tx_timeout_recovery_level = 1;
  5044. else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
  5045. netdev->watchdog_timeo)))
  5046. return;
  5047. if (tx_ring) {
  5048. head = tx_ring->next_to_clean;
  5049. /* Read interrupt register */
  5050. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  5051. val = rd32(&pf->hw,
  5052. GLINT_DYN_CTL(tx_ring->q_vector->v_idx +
  5053. tx_ring->vsi->base_vector - 1));
  5054. netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
  5055. vsi->vsi_num, hung_queue, tx_ring->next_to_clean,
  5056. head, tx_ring->next_to_use,
  5057. readl(tx_ring->tail), val);
  5058. }
  5059. pf->tx_timeout_last_recovery = jiffies;
  5060. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  5061. pf->tx_timeout_recovery_level, hung_queue);
  5062. switch (pf->tx_timeout_recovery_level) {
  5063. case 1:
  5064. set_bit(__ICE_PFR_REQ, pf->state);
  5065. break;
  5066. case 2:
  5067. set_bit(__ICE_CORER_REQ, pf->state);
  5068. break;
  5069. case 3:
  5070. set_bit(__ICE_GLOBR_REQ, pf->state);
  5071. break;
  5072. default:
  5073. netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
  5074. set_bit(__ICE_DOWN, pf->state);
  5075. set_bit(__ICE_NEEDS_RESTART, vsi->state);
  5076. set_bit(__ICE_SERVICE_DIS, pf->state);
  5077. break;
  5078. }
  5079. ice_service_task_schedule(pf);
  5080. pf->tx_timeout_recovery_level++;
  5081. }
  5082. /**
  5083. * ice_open - Called when a network interface becomes active
  5084. * @netdev: network interface device structure
  5085. *
  5086. * The open entry point is called when a network interface is made
  5087. * active by the system (IFF_UP). At this point all resources needed
  5088. * for transmit and receive operations are allocated, the interrupt
  5089. * handler is registered with the OS, the netdev watchdog is enabled,
  5090. * and the stack is notified that the interface is ready.
  5091. *
  5092. * Returns 0 on success, negative value on failure
  5093. */
  5094. static int ice_open(struct net_device *netdev)
  5095. {
  5096. struct ice_netdev_priv *np = netdev_priv(netdev);
  5097. struct ice_vsi *vsi = np->vsi;
  5098. int err;
  5099. if (test_bit(__ICE_NEEDS_RESTART, vsi->back->state)) {
  5100. netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
  5101. return -EIO;
  5102. }
  5103. netif_carrier_off(netdev);
  5104. err = ice_vsi_open(vsi);
  5105. if (err)
  5106. netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
  5107. vsi->vsi_num, vsi->vsw->sw_id);
  5108. return err;
  5109. }
  5110. /**
  5111. * ice_stop - Disables a network interface
  5112. * @netdev: network interface device structure
  5113. *
  5114. * The stop entry point is called when an interface is de-activated by the OS,
  5115. * and the netdevice enters the DOWN state. The hardware is still under the
  5116. * driver's control, but the netdev interface is disabled.
  5117. *
  5118. * Returns success only - not allowed to fail
  5119. */
  5120. static int ice_stop(struct net_device *netdev)
  5121. {
  5122. struct ice_netdev_priv *np = netdev_priv(netdev);
  5123. struct ice_vsi *vsi = np->vsi;
  5124. ice_vsi_close(vsi);
  5125. return 0;
  5126. }
  5127. /**
  5128. * ice_features_check - Validate encapsulated packet conforms to limits
  5129. * @skb: skb buffer
  5130. * @netdev: This port's netdev
  5131. * @features: Offload features that the stack believes apply
  5132. */
  5133. static netdev_features_t
  5134. ice_features_check(struct sk_buff *skb,
  5135. struct net_device __always_unused *netdev,
  5136. netdev_features_t features)
  5137. {
  5138. size_t len;
  5139. /* No point in doing any of this if neither checksum nor GSO are
  5140. * being requested for this frame. We can rule out both by just
  5141. * checking for CHECKSUM_PARTIAL
  5142. */
  5143. if (skb->ip_summed != CHECKSUM_PARTIAL)
  5144. return features;
  5145. /* We cannot support GSO if the MSS is going to be less than
  5146. * 64 bytes. If it is then we need to drop support for GSO.
  5147. */
  5148. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  5149. features &= ~NETIF_F_GSO_MASK;
  5150. len = skb_network_header(skb) - skb->data;
  5151. if (len & ~(ICE_TXD_MACLEN_MAX))
  5152. goto out_rm_features;
  5153. len = skb_transport_header(skb) - skb_network_header(skb);
  5154. if (len & ~(ICE_TXD_IPLEN_MAX))
  5155. goto out_rm_features;
  5156. if (skb->encapsulation) {
  5157. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  5158. if (len & ~(ICE_TXD_L4LEN_MAX))
  5159. goto out_rm_features;
  5160. len = skb_inner_transport_header(skb) -
  5161. skb_inner_network_header(skb);
  5162. if (len & ~(ICE_TXD_IPLEN_MAX))
  5163. goto out_rm_features;
  5164. }
  5165. return features;
  5166. out_rm_features:
  5167. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  5168. }
  5169. static const struct net_device_ops ice_netdev_ops = {
  5170. .ndo_open = ice_open,
  5171. .ndo_stop = ice_stop,
  5172. .ndo_start_xmit = ice_start_xmit,
  5173. .ndo_features_check = ice_features_check,
  5174. .ndo_set_rx_mode = ice_set_rx_mode,
  5175. .ndo_set_mac_address = ice_set_mac_address,
  5176. .ndo_validate_addr = eth_validate_addr,
  5177. .ndo_change_mtu = ice_change_mtu,
  5178. .ndo_get_stats64 = ice_get_stats64,
  5179. .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
  5180. .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
  5181. .ndo_set_features = ice_set_features,
  5182. .ndo_bridge_getlink = ice_bridge_getlink,
  5183. .ndo_bridge_setlink = ice_bridge_setlink,
  5184. .ndo_fdb_add = ice_fdb_add,
  5185. .ndo_fdb_del = ice_fdb_del,
  5186. .ndo_tx_timeout = ice_tx_timeout,
  5187. };