ice_main.c 139 KB

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