ice_main.c 139 KB

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