i40e_virtchnl_pf.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include "i40e.h"
  4. /*********************notification routines***********************/
  5. /**
  6. * i40e_vc_vf_broadcast
  7. * @pf: pointer to the PF structure
  8. * @v_opcode: operation code
  9. * @v_retval: return value
  10. * @msg: pointer to the msg buffer
  11. * @msglen: msg length
  12. *
  13. * send a message to all VFs on a given PF
  14. **/
  15. static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
  16. enum virtchnl_ops v_opcode,
  17. i40e_status v_retval, u8 *msg,
  18. u16 msglen)
  19. {
  20. struct i40e_hw *hw = &pf->hw;
  21. struct i40e_vf *vf = pf->vf;
  22. int i;
  23. for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
  24. int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
  25. /* Not all vfs are enabled so skip the ones that are not */
  26. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
  27. !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  28. continue;
  29. /* Ignore return value on purpose - a given VF may fail, but
  30. * we need to keep going and send to all of them
  31. */
  32. i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  33. msg, msglen, NULL);
  34. }
  35. }
  36. /**
  37. * i40e_vc_notify_vf_link_state
  38. * @vf: pointer to the VF structure
  39. *
  40. * send a link status message to a single VF
  41. **/
  42. static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
  43. {
  44. struct virtchnl_pf_event pfe;
  45. struct i40e_pf *pf = vf->pf;
  46. struct i40e_hw *hw = &pf->hw;
  47. struct i40e_link_status *ls = &pf->hw.phy.link_info;
  48. int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
  49. pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
  50. pfe.severity = PF_EVENT_SEVERITY_INFO;
  51. if (vf->link_forced) {
  52. pfe.event_data.link_event.link_status = vf->link_up;
  53. pfe.event_data.link_event.link_speed =
  54. (vf->link_up ? VIRTCHNL_LINK_SPEED_40GB : 0);
  55. } else {
  56. pfe.event_data.link_event.link_status =
  57. ls->link_info & I40E_AQ_LINK_UP;
  58. pfe.event_data.link_event.link_speed =
  59. i40e_virtchnl_link_speed(ls->link_speed);
  60. }
  61. i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
  62. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  63. }
  64. /**
  65. * i40e_vc_notify_link_state
  66. * @pf: pointer to the PF structure
  67. *
  68. * send a link status message to all VFs on a given PF
  69. **/
  70. void i40e_vc_notify_link_state(struct i40e_pf *pf)
  71. {
  72. int i;
  73. for (i = 0; i < pf->num_alloc_vfs; i++)
  74. i40e_vc_notify_vf_link_state(&pf->vf[i]);
  75. }
  76. /**
  77. * i40e_vc_notify_reset
  78. * @pf: pointer to the PF structure
  79. *
  80. * indicate a pending reset to all VFs on a given PF
  81. **/
  82. void i40e_vc_notify_reset(struct i40e_pf *pf)
  83. {
  84. struct virtchnl_pf_event pfe;
  85. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  86. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  87. i40e_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, 0,
  88. (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
  89. }
  90. /**
  91. * i40e_vc_notify_vf_reset
  92. * @vf: pointer to the VF structure
  93. *
  94. * indicate a pending reset to the given VF
  95. **/
  96. void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
  97. {
  98. struct virtchnl_pf_event pfe;
  99. int abs_vf_id;
  100. /* validate the request */
  101. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  102. return;
  103. /* verify if the VF is in either init or active before proceeding */
  104. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
  105. !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  106. return;
  107. abs_vf_id = vf->vf_id + (int)vf->pf->hw.func_caps.vf_base_id;
  108. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  109. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  110. i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, VIRTCHNL_OP_EVENT,
  111. 0, (u8 *)&pfe,
  112. sizeof(struct virtchnl_pf_event), NULL);
  113. }
  114. /***********************misc routines*****************************/
  115. /**
  116. * i40e_vc_disable_vf
  117. * @vf: pointer to the VF info
  118. *
  119. * Disable the VF through a SW reset.
  120. **/
  121. static inline void i40e_vc_disable_vf(struct i40e_vf *vf)
  122. {
  123. int i;
  124. i40e_vc_notify_vf_reset(vf);
  125. /* We want to ensure that an actual reset occurs initiated after this
  126. * function was called. However, we do not want to wait forever, so
  127. * we'll give a reasonable time and print a message if we failed to
  128. * ensure a reset.
  129. */
  130. for (i = 0; i < 20; i++) {
  131. if (i40e_reset_vf(vf, false))
  132. return;
  133. usleep_range(10000, 20000);
  134. }
  135. dev_warn(&vf->pf->pdev->dev,
  136. "Failed to initiate reset for VF %d after 200 milliseconds\n",
  137. vf->vf_id);
  138. }
  139. /**
  140. * i40e_vc_isvalid_vsi_id
  141. * @vf: pointer to the VF info
  142. * @vsi_id: VF relative VSI id
  143. *
  144. * check for the valid VSI id
  145. **/
  146. static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
  147. {
  148. struct i40e_pf *pf = vf->pf;
  149. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  150. return (vsi && (vsi->vf_id == vf->vf_id));
  151. }
  152. /**
  153. * i40e_vc_isvalid_queue_id
  154. * @vf: pointer to the VF info
  155. * @vsi_id: vsi id
  156. * @qid: vsi relative queue id
  157. *
  158. * check for the valid queue id
  159. **/
  160. static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
  161. u8 qid)
  162. {
  163. struct i40e_pf *pf = vf->pf;
  164. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  165. return (vsi && (qid < vsi->alloc_queue_pairs));
  166. }
  167. /**
  168. * i40e_vc_isvalid_vector_id
  169. * @vf: pointer to the VF info
  170. * @vector_id: VF relative vector id
  171. *
  172. * check for the valid vector id
  173. **/
  174. static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
  175. {
  176. struct i40e_pf *pf = vf->pf;
  177. return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
  178. }
  179. /***********************vf resource mgmt routines*****************/
  180. /**
  181. * i40e_vc_get_pf_queue_id
  182. * @vf: pointer to the VF info
  183. * @vsi_id: id of VSI as provided by the FW
  184. * @vsi_queue_id: vsi relative queue id
  185. *
  186. * return PF relative queue id
  187. **/
  188. static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
  189. u8 vsi_queue_id)
  190. {
  191. struct i40e_pf *pf = vf->pf;
  192. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  193. u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
  194. if (!vsi)
  195. return pf_queue_id;
  196. if (le16_to_cpu(vsi->info.mapping_flags) &
  197. I40E_AQ_VSI_QUE_MAP_NONCONTIG)
  198. pf_queue_id =
  199. le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
  200. else
  201. pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
  202. vsi_queue_id;
  203. return pf_queue_id;
  204. }
  205. /**
  206. * i40e_get_real_pf_qid
  207. * @vf: pointer to the VF info
  208. * @vsi_id: vsi id
  209. * @queue_id: queue number
  210. *
  211. * wrapper function to get pf_queue_id handling ADq code as well
  212. **/
  213. static u16 i40e_get_real_pf_qid(struct i40e_vf *vf, u16 vsi_id, u16 queue_id)
  214. {
  215. int i;
  216. if (vf->adq_enabled) {
  217. /* Although VF considers all the queues(can be 1 to 16) as its
  218. * own but they may actually belong to different VSIs(up to 4).
  219. * We need to find which queues belongs to which VSI.
  220. */
  221. for (i = 0; i < vf->num_tc; i++) {
  222. if (queue_id < vf->ch[i].num_qps) {
  223. vsi_id = vf->ch[i].vsi_id;
  224. break;
  225. }
  226. /* find right queue id which is relative to a
  227. * given VSI.
  228. */
  229. queue_id -= vf->ch[i].num_qps;
  230. }
  231. }
  232. return i40e_vc_get_pf_queue_id(vf, vsi_id, queue_id);
  233. }
  234. /**
  235. * i40e_config_irq_link_list
  236. * @vf: pointer to the VF info
  237. * @vsi_id: id of VSI as given by the FW
  238. * @vecmap: irq map info
  239. *
  240. * configure irq link list from the map
  241. **/
  242. static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
  243. struct virtchnl_vector_map *vecmap)
  244. {
  245. unsigned long linklistmap = 0, tempmap;
  246. struct i40e_pf *pf = vf->pf;
  247. struct i40e_hw *hw = &pf->hw;
  248. u16 vsi_queue_id, pf_queue_id;
  249. enum i40e_queue_type qtype;
  250. u16 next_q, vector_id, size;
  251. u32 reg, reg_idx;
  252. u16 itr_idx = 0;
  253. vector_id = vecmap->vector_id;
  254. /* setup the head */
  255. if (0 == vector_id)
  256. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  257. else
  258. reg_idx = I40E_VPINT_LNKLSTN(
  259. ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
  260. (vector_id - 1));
  261. if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
  262. /* Special case - No queues mapped on this vector */
  263. wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
  264. goto irq_list_done;
  265. }
  266. tempmap = vecmap->rxq_map;
  267. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  268. linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
  269. vsi_queue_id));
  270. }
  271. tempmap = vecmap->txq_map;
  272. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  273. linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
  274. vsi_queue_id + 1));
  275. }
  276. size = I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES;
  277. next_q = find_first_bit(&linklistmap, size);
  278. if (unlikely(next_q == size))
  279. goto irq_list_done;
  280. vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
  281. qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
  282. pf_queue_id = i40e_get_real_pf_qid(vf, vsi_id, vsi_queue_id);
  283. reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
  284. wr32(hw, reg_idx, reg);
  285. while (next_q < size) {
  286. switch (qtype) {
  287. case I40E_QUEUE_TYPE_RX:
  288. reg_idx = I40E_QINT_RQCTL(pf_queue_id);
  289. itr_idx = vecmap->rxitr_idx;
  290. break;
  291. case I40E_QUEUE_TYPE_TX:
  292. reg_idx = I40E_QINT_TQCTL(pf_queue_id);
  293. itr_idx = vecmap->txitr_idx;
  294. break;
  295. default:
  296. break;
  297. }
  298. next_q = find_next_bit(&linklistmap, size, next_q + 1);
  299. if (next_q < size) {
  300. vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
  301. qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
  302. pf_queue_id = i40e_get_real_pf_qid(vf,
  303. vsi_id,
  304. vsi_queue_id);
  305. } else {
  306. pf_queue_id = I40E_QUEUE_END_OF_LIST;
  307. qtype = 0;
  308. }
  309. /* format for the RQCTL & TQCTL regs is same */
  310. reg = (vector_id) |
  311. (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
  312. (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  313. BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
  314. (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
  315. wr32(hw, reg_idx, reg);
  316. }
  317. /* if the vf is running in polling mode and using interrupt zero,
  318. * need to disable auto-mask on enabling zero interrupt for VFs.
  319. */
  320. if ((vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
  321. (vector_id == 0)) {
  322. reg = rd32(hw, I40E_GLINT_CTL);
  323. if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
  324. reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
  325. wr32(hw, I40E_GLINT_CTL, reg);
  326. }
  327. }
  328. irq_list_done:
  329. i40e_flush(hw);
  330. }
  331. /**
  332. * i40e_release_iwarp_qvlist
  333. * @vf: pointer to the VF.
  334. *
  335. **/
  336. static void i40e_release_iwarp_qvlist(struct i40e_vf *vf)
  337. {
  338. struct i40e_pf *pf = vf->pf;
  339. struct virtchnl_iwarp_qvlist_info *qvlist_info = vf->qvlist_info;
  340. u32 msix_vf;
  341. u32 i;
  342. if (!vf->qvlist_info)
  343. return;
  344. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  345. for (i = 0; i < qvlist_info->num_vectors; i++) {
  346. struct virtchnl_iwarp_qv_info *qv_info;
  347. u32 next_q_index, next_q_type;
  348. struct i40e_hw *hw = &pf->hw;
  349. u32 v_idx, reg_idx, reg;
  350. qv_info = &qvlist_info->qv_info[i];
  351. if (!qv_info)
  352. continue;
  353. v_idx = qv_info->v_idx;
  354. if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
  355. /* Figure out the queue after CEQ and make that the
  356. * first queue.
  357. */
  358. reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
  359. reg = rd32(hw, I40E_VPINT_CEQCTL(reg_idx));
  360. next_q_index = (reg & I40E_VPINT_CEQCTL_NEXTQ_INDX_MASK)
  361. >> I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT;
  362. next_q_type = (reg & I40E_VPINT_CEQCTL_NEXTQ_TYPE_MASK)
  363. >> I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT;
  364. reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
  365. reg = (next_q_index &
  366. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
  367. (next_q_type <<
  368. I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  369. wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
  370. }
  371. }
  372. kfree(vf->qvlist_info);
  373. vf->qvlist_info = NULL;
  374. }
  375. /**
  376. * i40e_config_iwarp_qvlist
  377. * @vf: pointer to the VF info
  378. * @qvlist_info: queue and vector list
  379. *
  380. * Return 0 on success or < 0 on error
  381. **/
  382. static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
  383. struct virtchnl_iwarp_qvlist_info *qvlist_info)
  384. {
  385. struct i40e_pf *pf = vf->pf;
  386. struct i40e_hw *hw = &pf->hw;
  387. struct virtchnl_iwarp_qv_info *qv_info;
  388. u32 v_idx, i, reg_idx, reg;
  389. u32 next_q_idx, next_q_type;
  390. u32 msix_vf, size;
  391. size = sizeof(struct virtchnl_iwarp_qvlist_info) +
  392. (sizeof(struct virtchnl_iwarp_qv_info) *
  393. (qvlist_info->num_vectors - 1));
  394. vf->qvlist_info = kzalloc(size, GFP_KERNEL);
  395. if (!vf->qvlist_info)
  396. return -ENOMEM;
  397. vf->qvlist_info->num_vectors = qvlist_info->num_vectors;
  398. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  399. for (i = 0; i < qvlist_info->num_vectors; i++) {
  400. qv_info = &qvlist_info->qv_info[i];
  401. if (!qv_info)
  402. continue;
  403. v_idx = qv_info->v_idx;
  404. /* Validate vector id belongs to this vf */
  405. if (!i40e_vc_isvalid_vector_id(vf, v_idx))
  406. goto err;
  407. vf->qvlist_info->qv_info[i] = *qv_info;
  408. reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
  409. /* We might be sharing the interrupt, so get the first queue
  410. * index and type, push it down the list by adding the new
  411. * queue on top. Also link it with the new queue in CEQCTL.
  412. */
  413. reg = rd32(hw, I40E_VPINT_LNKLSTN(reg_idx));
  414. next_q_idx = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) >>
  415. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_SHIFT);
  416. next_q_type = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK) >>
  417. I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  418. if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
  419. reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
  420. reg = (I40E_VPINT_CEQCTL_CAUSE_ENA_MASK |
  421. (v_idx << I40E_VPINT_CEQCTL_MSIX_INDX_SHIFT) |
  422. (qv_info->itr_idx << I40E_VPINT_CEQCTL_ITR_INDX_SHIFT) |
  423. (next_q_type << I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT) |
  424. (next_q_idx << I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT));
  425. wr32(hw, I40E_VPINT_CEQCTL(reg_idx), reg);
  426. reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
  427. reg = (qv_info->ceq_idx &
  428. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
  429. (I40E_QUEUE_TYPE_PE_CEQ <<
  430. I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  431. wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
  432. }
  433. if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
  434. reg = (I40E_VPINT_AEQCTL_CAUSE_ENA_MASK |
  435. (v_idx << I40E_VPINT_AEQCTL_MSIX_INDX_SHIFT) |
  436. (qv_info->itr_idx << I40E_VPINT_AEQCTL_ITR_INDX_SHIFT));
  437. wr32(hw, I40E_VPINT_AEQCTL(vf->vf_id), reg);
  438. }
  439. }
  440. return 0;
  441. err:
  442. kfree(vf->qvlist_info);
  443. vf->qvlist_info = NULL;
  444. return -EINVAL;
  445. }
  446. /**
  447. * i40e_config_vsi_tx_queue
  448. * @vf: pointer to the VF info
  449. * @vsi_id: id of VSI as provided by the FW
  450. * @vsi_queue_id: vsi relative queue index
  451. * @info: config. info
  452. *
  453. * configure tx queue
  454. **/
  455. static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
  456. u16 vsi_queue_id,
  457. struct virtchnl_txq_info *info)
  458. {
  459. struct i40e_pf *pf = vf->pf;
  460. struct i40e_hw *hw = &pf->hw;
  461. struct i40e_hmc_obj_txq tx_ctx;
  462. struct i40e_vsi *vsi;
  463. u16 pf_queue_id;
  464. u32 qtx_ctl;
  465. int ret = 0;
  466. if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
  467. ret = -ENOENT;
  468. goto error_context;
  469. }
  470. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  471. vsi = i40e_find_vsi_from_id(pf, vsi_id);
  472. if (!vsi) {
  473. ret = -ENOENT;
  474. goto error_context;
  475. }
  476. /* clear the context structure first */
  477. memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
  478. /* only set the required fields */
  479. tx_ctx.base = info->dma_ring_addr / 128;
  480. tx_ctx.qlen = info->ring_len;
  481. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
  482. tx_ctx.rdylist_act = 0;
  483. tx_ctx.head_wb_ena = info->headwb_enabled;
  484. tx_ctx.head_wb_addr = info->dma_headwb_addr;
  485. /* clear the context in the HMC */
  486. ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
  487. if (ret) {
  488. dev_err(&pf->pdev->dev,
  489. "Failed to clear VF LAN Tx queue context %d, error: %d\n",
  490. pf_queue_id, ret);
  491. ret = -ENOENT;
  492. goto error_context;
  493. }
  494. /* set the context in the HMC */
  495. ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
  496. if (ret) {
  497. dev_err(&pf->pdev->dev,
  498. "Failed to set VF LAN Tx queue context %d error: %d\n",
  499. pf_queue_id, ret);
  500. ret = -ENOENT;
  501. goto error_context;
  502. }
  503. /* associate this queue with the PCI VF function */
  504. qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
  505. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
  506. & I40E_QTX_CTL_PF_INDX_MASK);
  507. qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
  508. << I40E_QTX_CTL_VFVM_INDX_SHIFT)
  509. & I40E_QTX_CTL_VFVM_INDX_MASK);
  510. wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
  511. i40e_flush(hw);
  512. error_context:
  513. return ret;
  514. }
  515. /**
  516. * i40e_config_vsi_rx_queue
  517. * @vf: pointer to the VF info
  518. * @vsi_id: id of VSI as provided by the FW
  519. * @vsi_queue_id: vsi relative queue index
  520. * @info: config. info
  521. *
  522. * configure rx queue
  523. **/
  524. static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
  525. u16 vsi_queue_id,
  526. struct virtchnl_rxq_info *info)
  527. {
  528. struct i40e_pf *pf = vf->pf;
  529. struct i40e_hw *hw = &pf->hw;
  530. struct i40e_hmc_obj_rxq rx_ctx;
  531. u16 pf_queue_id;
  532. int ret = 0;
  533. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  534. /* clear the context structure first */
  535. memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
  536. /* only set the required fields */
  537. rx_ctx.base = info->dma_ring_addr / 128;
  538. rx_ctx.qlen = info->ring_len;
  539. if (info->splithdr_enabled) {
  540. rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
  541. I40E_RX_SPLIT_IP |
  542. I40E_RX_SPLIT_TCP_UDP |
  543. I40E_RX_SPLIT_SCTP;
  544. /* header length validation */
  545. if (info->hdr_size > ((2 * 1024) - 64)) {
  546. ret = -EINVAL;
  547. goto error_param;
  548. }
  549. rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
  550. /* set split mode 10b */
  551. rx_ctx.dtype = I40E_RX_DTYPE_HEADER_SPLIT;
  552. }
  553. /* databuffer length validation */
  554. if (info->databuffer_size > ((16 * 1024) - 128)) {
  555. ret = -EINVAL;
  556. goto error_param;
  557. }
  558. rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
  559. /* max pkt. length validation */
  560. if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
  561. ret = -EINVAL;
  562. goto error_param;
  563. }
  564. rx_ctx.rxmax = info->max_pkt_size;
  565. /* enable 32bytes desc always */
  566. rx_ctx.dsize = 1;
  567. /* default values */
  568. rx_ctx.lrxqthresh = 1;
  569. rx_ctx.crcstrip = 1;
  570. rx_ctx.prefena = 1;
  571. rx_ctx.l2tsel = 1;
  572. /* clear the context in the HMC */
  573. ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
  574. if (ret) {
  575. dev_err(&pf->pdev->dev,
  576. "Failed to clear VF LAN Rx queue context %d, error: %d\n",
  577. pf_queue_id, ret);
  578. ret = -ENOENT;
  579. goto error_param;
  580. }
  581. /* set the context in the HMC */
  582. ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
  583. if (ret) {
  584. dev_err(&pf->pdev->dev,
  585. "Failed to set VF LAN Rx queue context %d error: %d\n",
  586. pf_queue_id, ret);
  587. ret = -ENOENT;
  588. goto error_param;
  589. }
  590. error_param:
  591. return ret;
  592. }
  593. /**
  594. * i40e_alloc_vsi_res
  595. * @vf: pointer to the VF info
  596. * @idx: VSI index, applies only for ADq mode, zero otherwise
  597. *
  598. * alloc VF vsi context & resources
  599. **/
  600. static int i40e_alloc_vsi_res(struct i40e_vf *vf, u8 idx)
  601. {
  602. struct i40e_mac_filter *f = NULL;
  603. struct i40e_pf *pf = vf->pf;
  604. struct i40e_vsi *vsi;
  605. u64 max_tx_rate = 0;
  606. int ret = 0;
  607. vsi = i40e_vsi_setup(pf, I40E_VSI_SRIOV, pf->vsi[pf->lan_vsi]->seid,
  608. vf->vf_id);
  609. if (!vsi) {
  610. dev_err(&pf->pdev->dev,
  611. "add vsi failed for VF %d, aq_err %d\n",
  612. vf->vf_id, pf->hw.aq.asq_last_status);
  613. ret = -ENOENT;
  614. goto error_alloc_vsi_res;
  615. }
  616. if (!idx) {
  617. u64 hena = i40e_pf_get_default_rss_hena(pf);
  618. u8 broadcast[ETH_ALEN];
  619. vf->lan_vsi_idx = vsi->idx;
  620. vf->lan_vsi_id = vsi->id;
  621. /* If the port VLAN has been configured and then the
  622. * VF driver was removed then the VSI port VLAN
  623. * configuration was destroyed. Check if there is
  624. * a port VLAN and restore the VSI configuration if
  625. * needed.
  626. */
  627. if (vf->port_vlan_id)
  628. i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
  629. spin_lock_bh(&vsi->mac_filter_hash_lock);
  630. if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
  631. f = i40e_add_mac_filter(vsi,
  632. vf->default_lan_addr.addr);
  633. if (!f)
  634. dev_info(&pf->pdev->dev,
  635. "Could not add MAC filter %pM for VF %d\n",
  636. vf->default_lan_addr.addr, vf->vf_id);
  637. }
  638. eth_broadcast_addr(broadcast);
  639. f = i40e_add_mac_filter(vsi, broadcast);
  640. if (!f)
  641. dev_info(&pf->pdev->dev,
  642. "Could not allocate VF broadcast filter\n");
  643. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  644. wr32(&pf->hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)hena);
  645. wr32(&pf->hw, I40E_VFQF_HENA1(1, vf->vf_id), (u32)(hena >> 32));
  646. /* program mac filter only for VF VSI */
  647. ret = i40e_sync_vsi_filters(vsi);
  648. if (ret)
  649. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  650. }
  651. /* storing VSI index and id for ADq and don't apply the mac filter */
  652. if (vf->adq_enabled) {
  653. vf->ch[idx].vsi_idx = vsi->idx;
  654. vf->ch[idx].vsi_id = vsi->id;
  655. }
  656. /* Set VF bandwidth if specified */
  657. if (vf->tx_rate) {
  658. max_tx_rate = vf->tx_rate;
  659. } else if (vf->ch[idx].max_tx_rate) {
  660. max_tx_rate = vf->ch[idx].max_tx_rate;
  661. }
  662. if (max_tx_rate) {
  663. max_tx_rate = div_u64(max_tx_rate, I40E_BW_CREDIT_DIVISOR);
  664. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
  665. max_tx_rate, 0, NULL);
  666. if (ret)
  667. dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
  668. vf->vf_id, ret);
  669. }
  670. error_alloc_vsi_res:
  671. return ret;
  672. }
  673. /**
  674. * i40e_map_pf_queues_to_vsi
  675. * @vf: pointer to the VF info
  676. *
  677. * PF maps LQPs to a VF by programming VSILAN_QTABLE & VPLAN_QTABLE. This
  678. * function takes care of first part VSILAN_QTABLE, mapping pf queues to VSI.
  679. **/
  680. static void i40e_map_pf_queues_to_vsi(struct i40e_vf *vf)
  681. {
  682. struct i40e_pf *pf = vf->pf;
  683. struct i40e_hw *hw = &pf->hw;
  684. u32 reg, num_tc = 1; /* VF has at least one traffic class */
  685. u16 vsi_id, qps;
  686. int i, j;
  687. if (vf->adq_enabled)
  688. num_tc = vf->num_tc;
  689. for (i = 0; i < num_tc; i++) {
  690. if (vf->adq_enabled) {
  691. qps = vf->ch[i].num_qps;
  692. vsi_id = vf->ch[i].vsi_id;
  693. } else {
  694. qps = pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
  695. vsi_id = vf->lan_vsi_id;
  696. }
  697. for (j = 0; j < 7; j++) {
  698. if (j * 2 >= qps) {
  699. /* end of list */
  700. reg = 0x07FF07FF;
  701. } else {
  702. u16 qid = i40e_vc_get_pf_queue_id(vf,
  703. vsi_id,
  704. j * 2);
  705. reg = qid;
  706. qid = i40e_vc_get_pf_queue_id(vf, vsi_id,
  707. (j * 2) + 1);
  708. reg |= qid << 16;
  709. }
  710. i40e_write_rx_ctl(hw,
  711. I40E_VSILAN_QTABLE(j, vsi_id),
  712. reg);
  713. }
  714. }
  715. }
  716. /**
  717. * i40e_map_pf_to_vf_queues
  718. * @vf: pointer to the VF info
  719. *
  720. * PF maps LQPs to a VF by programming VSILAN_QTABLE & VPLAN_QTABLE. This
  721. * function takes care of the second part VPLAN_QTABLE & completes VF mappings.
  722. **/
  723. static void i40e_map_pf_to_vf_queues(struct i40e_vf *vf)
  724. {
  725. struct i40e_pf *pf = vf->pf;
  726. struct i40e_hw *hw = &pf->hw;
  727. u32 reg, total_qps = 0;
  728. u32 qps, num_tc = 1; /* VF has at least one traffic class */
  729. u16 vsi_id, qid;
  730. int i, j;
  731. if (vf->adq_enabled)
  732. num_tc = vf->num_tc;
  733. for (i = 0; i < num_tc; i++) {
  734. if (vf->adq_enabled) {
  735. qps = vf->ch[i].num_qps;
  736. vsi_id = vf->ch[i].vsi_id;
  737. } else {
  738. qps = pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
  739. vsi_id = vf->lan_vsi_id;
  740. }
  741. for (j = 0; j < qps; j++) {
  742. qid = i40e_vc_get_pf_queue_id(vf, vsi_id, j);
  743. reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
  744. wr32(hw, I40E_VPLAN_QTABLE(total_qps, vf->vf_id),
  745. reg);
  746. total_qps++;
  747. }
  748. }
  749. }
  750. /**
  751. * i40e_enable_vf_mappings
  752. * @vf: pointer to the VF info
  753. *
  754. * enable VF mappings
  755. **/
  756. static void i40e_enable_vf_mappings(struct i40e_vf *vf)
  757. {
  758. struct i40e_pf *pf = vf->pf;
  759. struct i40e_hw *hw = &pf->hw;
  760. u32 reg;
  761. /* Tell the hardware we're using noncontiguous mapping. HW requires
  762. * that VF queues be mapped using this method, even when they are
  763. * contiguous in real life
  764. */
  765. i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
  766. I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
  767. /* enable VF vplan_qtable mappings */
  768. reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
  769. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
  770. i40e_map_pf_to_vf_queues(vf);
  771. i40e_map_pf_queues_to_vsi(vf);
  772. i40e_flush(hw);
  773. }
  774. /**
  775. * i40e_disable_vf_mappings
  776. * @vf: pointer to the VF info
  777. *
  778. * disable VF mappings
  779. **/
  780. static void i40e_disable_vf_mappings(struct i40e_vf *vf)
  781. {
  782. struct i40e_pf *pf = vf->pf;
  783. struct i40e_hw *hw = &pf->hw;
  784. int i;
  785. /* disable qp mappings */
  786. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
  787. for (i = 0; i < I40E_MAX_VSI_QP; i++)
  788. wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
  789. I40E_QUEUE_END_OF_LIST);
  790. i40e_flush(hw);
  791. }
  792. /**
  793. * i40e_free_vf_res
  794. * @vf: pointer to the VF info
  795. *
  796. * free VF resources
  797. **/
  798. static void i40e_free_vf_res(struct i40e_vf *vf)
  799. {
  800. struct i40e_pf *pf = vf->pf;
  801. struct i40e_hw *hw = &pf->hw;
  802. u32 reg_idx, reg;
  803. int i, j, msix_vf;
  804. /* Start by disabling VF's configuration API to prevent the OS from
  805. * accessing the VF's VSI after it's freed / invalidated.
  806. */
  807. clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  808. /* It's possible the VF had requeuested more queues than the default so
  809. * do the accounting here when we're about to free them.
  810. */
  811. if (vf->num_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF) {
  812. pf->queues_left += vf->num_queue_pairs -
  813. I40E_DEFAULT_QUEUES_PER_VF;
  814. }
  815. /* free vsi & disconnect it from the parent uplink */
  816. if (vf->lan_vsi_idx) {
  817. i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
  818. vf->lan_vsi_idx = 0;
  819. vf->lan_vsi_id = 0;
  820. vf->num_mac = 0;
  821. }
  822. /* do the accounting and remove additional ADq VSI's */
  823. if (vf->adq_enabled && vf->ch[0].vsi_idx) {
  824. for (j = 0; j < vf->num_tc; j++) {
  825. /* At this point VSI0 is already released so don't
  826. * release it again and only clear their values in
  827. * structure variables
  828. */
  829. if (j)
  830. i40e_vsi_release(pf->vsi[vf->ch[j].vsi_idx]);
  831. vf->ch[j].vsi_idx = 0;
  832. vf->ch[j].vsi_id = 0;
  833. }
  834. }
  835. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  836. /* disable interrupts so the VF starts in a known state */
  837. for (i = 0; i < msix_vf; i++) {
  838. /* format is same for both registers */
  839. if (0 == i)
  840. reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
  841. else
  842. reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
  843. (vf->vf_id))
  844. + (i - 1));
  845. wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
  846. i40e_flush(hw);
  847. }
  848. /* clear the irq settings */
  849. for (i = 0; i < msix_vf; i++) {
  850. /* format is same for both registers */
  851. if (0 == i)
  852. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  853. else
  854. reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
  855. (vf->vf_id))
  856. + (i - 1));
  857. reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
  858. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
  859. wr32(hw, reg_idx, reg);
  860. i40e_flush(hw);
  861. }
  862. /* reset some of the state variables keeping track of the resources */
  863. vf->num_queue_pairs = 0;
  864. clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  865. clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  866. }
  867. /**
  868. * i40e_alloc_vf_res
  869. * @vf: pointer to the VF info
  870. *
  871. * allocate VF resources
  872. **/
  873. static int i40e_alloc_vf_res(struct i40e_vf *vf)
  874. {
  875. struct i40e_pf *pf = vf->pf;
  876. int total_queue_pairs = 0;
  877. int ret, idx;
  878. if (vf->num_req_queues &&
  879. vf->num_req_queues <= pf->queues_left + I40E_DEFAULT_QUEUES_PER_VF)
  880. pf->num_vf_qps = vf->num_req_queues;
  881. else
  882. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  883. /* allocate hw vsi context & associated resources */
  884. ret = i40e_alloc_vsi_res(vf, 0);
  885. if (ret)
  886. goto error_alloc;
  887. total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
  888. /* allocate additional VSIs based on tc information for ADq */
  889. if (vf->adq_enabled) {
  890. if (pf->queues_left >=
  891. (I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF)) {
  892. /* TC 0 always belongs to VF VSI */
  893. for (idx = 1; idx < vf->num_tc; idx++) {
  894. ret = i40e_alloc_vsi_res(vf, idx);
  895. if (ret)
  896. goto error_alloc;
  897. }
  898. /* send correct number of queues */
  899. total_queue_pairs = I40E_MAX_VF_QUEUES;
  900. } else {
  901. dev_info(&pf->pdev->dev, "VF %d: Not enough queues to allocate, disabling ADq\n",
  902. vf->vf_id);
  903. vf->adq_enabled = false;
  904. }
  905. }
  906. /* We account for each VF to get a default number of queue pairs. If
  907. * the VF has now requested more, we need to account for that to make
  908. * certain we never request more queues than we actually have left in
  909. * HW.
  910. */
  911. if (total_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF)
  912. pf->queues_left -=
  913. total_queue_pairs - I40E_DEFAULT_QUEUES_PER_VF;
  914. if (vf->trusted)
  915. set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  916. else
  917. clear_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  918. /* store the total qps number for the runtime
  919. * VF req validation
  920. */
  921. vf->num_queue_pairs = total_queue_pairs;
  922. /* VF is now completely initialized */
  923. set_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  924. error_alloc:
  925. if (ret)
  926. i40e_free_vf_res(vf);
  927. return ret;
  928. }
  929. #define VF_DEVICE_STATUS 0xAA
  930. #define VF_TRANS_PENDING_MASK 0x20
  931. /**
  932. * i40e_quiesce_vf_pci
  933. * @vf: pointer to the VF structure
  934. *
  935. * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
  936. * if the transactions never clear.
  937. **/
  938. static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
  939. {
  940. struct i40e_pf *pf = vf->pf;
  941. struct i40e_hw *hw = &pf->hw;
  942. int vf_abs_id, i;
  943. u32 reg;
  944. vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
  945. wr32(hw, I40E_PF_PCI_CIAA,
  946. VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
  947. for (i = 0; i < 100; i++) {
  948. reg = rd32(hw, I40E_PF_PCI_CIAD);
  949. if ((reg & VF_TRANS_PENDING_MASK) == 0)
  950. return 0;
  951. udelay(1);
  952. }
  953. return -EIO;
  954. }
  955. /**
  956. * i40e_trigger_vf_reset
  957. * @vf: pointer to the VF structure
  958. * @flr: VFLR was issued or not
  959. *
  960. * Trigger hardware to start a reset for a particular VF. Expects the caller
  961. * to wait the proper amount of time to allow hardware to reset the VF before
  962. * it cleans up and restores VF functionality.
  963. **/
  964. static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
  965. {
  966. struct i40e_pf *pf = vf->pf;
  967. struct i40e_hw *hw = &pf->hw;
  968. u32 reg, reg_idx, bit_idx;
  969. /* warn the VF */
  970. clear_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  971. /* Disable VF's configuration API during reset. The flag is re-enabled
  972. * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
  973. * It's normally disabled in i40e_free_vf_res(), but it's safer
  974. * to do it earlier to give some time to finish to any VF config
  975. * functions that may still be running at this point.
  976. */
  977. clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  978. /* In the case of a VFLR, the HW has already reset the VF and we
  979. * just need to clean up, so don't hit the VFRTRIG register.
  980. */
  981. if (!flr) {
  982. /* reset VF using VPGEN_VFRTRIG reg */
  983. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  984. reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  985. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  986. i40e_flush(hw);
  987. }
  988. /* clear the VFLR bit in GLGEN_VFLRSTAT */
  989. reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
  990. bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
  991. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  992. i40e_flush(hw);
  993. if (i40e_quiesce_vf_pci(vf))
  994. dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
  995. vf->vf_id);
  996. }
  997. /**
  998. * i40e_cleanup_reset_vf
  999. * @vf: pointer to the VF structure
  1000. *
  1001. * Cleanup a VF after the hardware reset is finished. Expects the caller to
  1002. * have verified whether the reset is finished properly, and ensure the
  1003. * minimum amount of wait time has passed.
  1004. **/
  1005. static void i40e_cleanup_reset_vf(struct i40e_vf *vf)
  1006. {
  1007. struct i40e_pf *pf = vf->pf;
  1008. struct i40e_hw *hw = &pf->hw;
  1009. u32 reg;
  1010. /* free VF resources to begin resetting the VSI state */
  1011. i40e_free_vf_res(vf);
  1012. /* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
  1013. * By doing this we allow HW to access VF memory at any point. If we
  1014. * did it any sooner, HW could access memory while it was being freed
  1015. * in i40e_free_vf_res(), causing an IOMMU fault.
  1016. *
  1017. * On the other hand, this needs to be done ASAP, because the VF driver
  1018. * is waiting for this to happen and may report a timeout. It's
  1019. * harmless, but it gets logged into Guest OS kernel log, so best avoid
  1020. * it.
  1021. */
  1022. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  1023. reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  1024. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  1025. /* reallocate VF resources to finish resetting the VSI state */
  1026. if (!i40e_alloc_vf_res(vf)) {
  1027. int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  1028. i40e_enable_vf_mappings(vf);
  1029. set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  1030. clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  1031. /* Do not notify the client during VF init */
  1032. if (!test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
  1033. &vf->vf_states))
  1034. i40e_notify_client_of_vf_reset(pf, abs_vf_id);
  1035. vf->num_vlan = 0;
  1036. }
  1037. /* Tell the VF driver the reset is done. This needs to be done only
  1038. * after VF has been fully initialized, because the VF driver may
  1039. * request resources immediately after setting this flag.
  1040. */
  1041. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
  1042. }
  1043. /**
  1044. * i40e_reset_vf
  1045. * @vf: pointer to the VF structure
  1046. * @flr: VFLR was issued or not
  1047. *
  1048. * Returns true if the VF is reset, false otherwise.
  1049. **/
  1050. bool i40e_reset_vf(struct i40e_vf *vf, bool flr)
  1051. {
  1052. struct i40e_pf *pf = vf->pf;
  1053. struct i40e_hw *hw = &pf->hw;
  1054. bool rsd = false;
  1055. u32 reg;
  1056. int i;
  1057. /* If the VFs have been disabled, this means something else is
  1058. * resetting the VF, so we shouldn't continue.
  1059. */
  1060. if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  1061. return false;
  1062. i40e_trigger_vf_reset(vf, flr);
  1063. /* poll VPGEN_VFRSTAT reg to make sure
  1064. * that reset is complete
  1065. */
  1066. for (i = 0; i < 10; i++) {
  1067. /* VF reset requires driver to first reset the VF and then
  1068. * poll the status register to make sure that the reset
  1069. * completed successfully. Due to internal HW FIFO flushes,
  1070. * we must wait 10ms before the register will be valid.
  1071. */
  1072. usleep_range(10000, 20000);
  1073. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  1074. if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
  1075. rsd = true;
  1076. break;
  1077. }
  1078. }
  1079. if (flr)
  1080. usleep_range(10000, 20000);
  1081. if (!rsd)
  1082. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  1083. vf->vf_id);
  1084. usleep_range(10000, 20000);
  1085. /* On initial reset, we don't have any queues to disable */
  1086. if (vf->lan_vsi_idx != 0)
  1087. i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
  1088. i40e_cleanup_reset_vf(vf);
  1089. i40e_flush(hw);
  1090. clear_bit(__I40E_VF_DISABLE, pf->state);
  1091. return true;
  1092. }
  1093. /**
  1094. * i40e_reset_all_vfs
  1095. * @pf: pointer to the PF structure
  1096. * @flr: VFLR was issued or not
  1097. *
  1098. * Reset all allocated VFs in one go. First, tell the hardware to reset each
  1099. * VF, then do all the waiting in one chunk, and finally finish restoring each
  1100. * VF after the wait. This is useful during PF routines which need to reset
  1101. * all VFs, as otherwise it must perform these resets in a serialized fashion.
  1102. *
  1103. * Returns true if any VFs were reset, and false otherwise.
  1104. **/
  1105. bool i40e_reset_all_vfs(struct i40e_pf *pf, bool flr)
  1106. {
  1107. struct i40e_hw *hw = &pf->hw;
  1108. struct i40e_vf *vf;
  1109. int i, v;
  1110. u32 reg;
  1111. /* If we don't have any VFs, then there is nothing to reset */
  1112. if (!pf->num_alloc_vfs)
  1113. return false;
  1114. /* If VFs have been disabled, there is no need to reset */
  1115. if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  1116. return false;
  1117. /* Begin reset on all VFs at once */
  1118. for (v = 0; v < pf->num_alloc_vfs; v++)
  1119. i40e_trigger_vf_reset(&pf->vf[v], flr);
  1120. /* HW requires some time to make sure it can flush the FIFO for a VF
  1121. * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
  1122. * sequence to make sure that it has completed. We'll keep track of
  1123. * the VFs using a simple iterator that increments once that VF has
  1124. * finished resetting.
  1125. */
  1126. for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
  1127. usleep_range(10000, 20000);
  1128. /* Check each VF in sequence, beginning with the VF to fail
  1129. * the previous check.
  1130. */
  1131. while (v < pf->num_alloc_vfs) {
  1132. vf = &pf->vf[v];
  1133. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  1134. if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
  1135. break;
  1136. /* If the current VF has finished resetting, move on
  1137. * to the next VF in sequence.
  1138. */
  1139. v++;
  1140. }
  1141. }
  1142. if (flr)
  1143. usleep_range(10000, 20000);
  1144. /* Display a warning if at least one VF didn't manage to reset in
  1145. * time, but continue on with the operation.
  1146. */
  1147. if (v < pf->num_alloc_vfs)
  1148. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  1149. pf->vf[v].vf_id);
  1150. usleep_range(10000, 20000);
  1151. /* Begin disabling all the rings associated with VFs, but do not wait
  1152. * between each VF.
  1153. */
  1154. for (v = 0; v < pf->num_alloc_vfs; v++) {
  1155. /* On initial reset, we don't have any queues to disable */
  1156. if (pf->vf[v].lan_vsi_idx == 0)
  1157. continue;
  1158. i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
  1159. }
  1160. /* Now that we've notified HW to disable all of the VF rings, wait
  1161. * until they finish.
  1162. */
  1163. for (v = 0; v < pf->num_alloc_vfs; v++) {
  1164. /* On initial reset, we don't have any queues to disable */
  1165. if (pf->vf[v].lan_vsi_idx == 0)
  1166. continue;
  1167. i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
  1168. }
  1169. /* Hw may need up to 50ms to finish disabling the RX queues. We
  1170. * minimize the wait by delaying only once for all VFs.
  1171. */
  1172. mdelay(50);
  1173. /* Finish the reset on each VF */
  1174. for (v = 0; v < pf->num_alloc_vfs; v++)
  1175. i40e_cleanup_reset_vf(&pf->vf[v]);
  1176. i40e_flush(hw);
  1177. clear_bit(__I40E_VF_DISABLE, pf->state);
  1178. return true;
  1179. }
  1180. /**
  1181. * i40e_free_vfs
  1182. * @pf: pointer to the PF structure
  1183. *
  1184. * free VF resources
  1185. **/
  1186. void i40e_free_vfs(struct i40e_pf *pf)
  1187. {
  1188. struct i40e_hw *hw = &pf->hw;
  1189. u32 reg_idx, bit_idx;
  1190. int i, tmp, vf_id;
  1191. if (!pf->vf)
  1192. return;
  1193. while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  1194. usleep_range(1000, 2000);
  1195. i40e_notify_client_of_vf_enable(pf, 0);
  1196. /* Amortize wait time by stopping all VFs at the same time */
  1197. for (i = 0; i < pf->num_alloc_vfs; i++) {
  1198. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1199. continue;
  1200. i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
  1201. }
  1202. for (i = 0; i < pf->num_alloc_vfs; i++) {
  1203. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1204. continue;
  1205. i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
  1206. }
  1207. /* Disable IOV before freeing resources. This lets any VF drivers
  1208. * running in the host get themselves cleaned up before we yank
  1209. * the carpet out from underneath their feet.
  1210. */
  1211. if (!pci_vfs_assigned(pf->pdev))
  1212. pci_disable_sriov(pf->pdev);
  1213. else
  1214. dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
  1215. /* free up VF resources */
  1216. tmp = pf->num_alloc_vfs;
  1217. pf->num_alloc_vfs = 0;
  1218. for (i = 0; i < tmp; i++) {
  1219. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1220. i40e_free_vf_res(&pf->vf[i]);
  1221. /* disable qp mappings */
  1222. i40e_disable_vf_mappings(&pf->vf[i]);
  1223. }
  1224. kfree(pf->vf);
  1225. pf->vf = NULL;
  1226. /* This check is for when the driver is unloaded while VFs are
  1227. * assigned. Setting the number of VFs to 0 through sysfs is caught
  1228. * before this function ever gets called.
  1229. */
  1230. if (!pci_vfs_assigned(pf->pdev)) {
  1231. /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
  1232. * work correctly when SR-IOV gets re-enabled.
  1233. */
  1234. for (vf_id = 0; vf_id < tmp; vf_id++) {
  1235. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  1236. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  1237. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  1238. }
  1239. }
  1240. clear_bit(__I40E_VF_DISABLE, pf->state);
  1241. }
  1242. #ifdef CONFIG_PCI_IOV
  1243. /**
  1244. * i40e_alloc_vfs
  1245. * @pf: pointer to the PF structure
  1246. * @num_alloc_vfs: number of VFs to allocate
  1247. *
  1248. * allocate VF resources
  1249. **/
  1250. int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
  1251. {
  1252. struct i40e_vf *vfs;
  1253. int i, ret = 0;
  1254. /* Disable interrupt 0 so we don't try to handle the VFLR. */
  1255. i40e_irq_dynamic_disable_icr0(pf);
  1256. /* Check to see if we're just allocating resources for extant VFs */
  1257. if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
  1258. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  1259. if (ret) {
  1260. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  1261. pf->num_alloc_vfs = 0;
  1262. goto err_iov;
  1263. }
  1264. }
  1265. /* allocate memory */
  1266. vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
  1267. if (!vfs) {
  1268. ret = -ENOMEM;
  1269. goto err_alloc;
  1270. }
  1271. pf->vf = vfs;
  1272. /* apply default profile */
  1273. for (i = 0; i < num_alloc_vfs; i++) {
  1274. vfs[i].pf = pf;
  1275. vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
  1276. vfs[i].vf_id = i;
  1277. /* assign default capabilities */
  1278. set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
  1279. vfs[i].spoofchk = true;
  1280. set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
  1281. }
  1282. pf->num_alloc_vfs = num_alloc_vfs;
  1283. /* VF resources get allocated during reset */
  1284. i40e_reset_all_vfs(pf, false);
  1285. i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
  1286. err_alloc:
  1287. if (ret)
  1288. i40e_free_vfs(pf);
  1289. err_iov:
  1290. /* Re-enable interrupt 0. */
  1291. i40e_irq_dynamic_enable_icr0(pf);
  1292. return ret;
  1293. }
  1294. #endif
  1295. /**
  1296. * i40e_pci_sriov_enable
  1297. * @pdev: pointer to a pci_dev structure
  1298. * @num_vfs: number of VFs to allocate
  1299. *
  1300. * Enable or change the number of VFs
  1301. **/
  1302. static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
  1303. {
  1304. #ifdef CONFIG_PCI_IOV
  1305. struct i40e_pf *pf = pci_get_drvdata(pdev);
  1306. int pre_existing_vfs = pci_num_vf(pdev);
  1307. int err = 0;
  1308. if (test_bit(__I40E_TESTING, pf->state)) {
  1309. dev_warn(&pdev->dev,
  1310. "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
  1311. err = -EPERM;
  1312. goto err_out;
  1313. }
  1314. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  1315. i40e_free_vfs(pf);
  1316. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  1317. goto out;
  1318. if (num_vfs > pf->num_req_vfs) {
  1319. dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
  1320. num_vfs, pf->num_req_vfs);
  1321. err = -EPERM;
  1322. goto err_out;
  1323. }
  1324. dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
  1325. err = i40e_alloc_vfs(pf, num_vfs);
  1326. if (err) {
  1327. dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
  1328. goto err_out;
  1329. }
  1330. out:
  1331. return num_vfs;
  1332. err_out:
  1333. return err;
  1334. #endif
  1335. return 0;
  1336. }
  1337. /**
  1338. * i40e_pci_sriov_configure
  1339. * @pdev: pointer to a pci_dev structure
  1340. * @num_vfs: number of VFs to allocate
  1341. *
  1342. * Enable or change the number of VFs. Called when the user updates the number
  1343. * of VFs in sysfs.
  1344. **/
  1345. int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
  1346. {
  1347. struct i40e_pf *pf = pci_get_drvdata(pdev);
  1348. if (num_vfs) {
  1349. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  1350. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  1351. i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
  1352. }
  1353. return i40e_pci_sriov_enable(pdev, num_vfs);
  1354. }
  1355. if (!pci_vfs_assigned(pf->pdev)) {
  1356. i40e_free_vfs(pf);
  1357. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  1358. i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
  1359. } else {
  1360. dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
  1361. return -EINVAL;
  1362. }
  1363. return 0;
  1364. }
  1365. /***********************virtual channel routines******************/
  1366. /**
  1367. * i40e_vc_send_msg_to_vf
  1368. * @vf: pointer to the VF info
  1369. * @v_opcode: virtual channel opcode
  1370. * @v_retval: virtual channel return value
  1371. * @msg: pointer to the msg buffer
  1372. * @msglen: msg length
  1373. *
  1374. * send msg to VF
  1375. **/
  1376. static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
  1377. u32 v_retval, u8 *msg, u16 msglen)
  1378. {
  1379. struct i40e_pf *pf;
  1380. struct i40e_hw *hw;
  1381. int abs_vf_id;
  1382. i40e_status aq_ret;
  1383. /* validate the request */
  1384. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  1385. return -EINVAL;
  1386. pf = vf->pf;
  1387. hw = &pf->hw;
  1388. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  1389. /* single place to detect unsuccessful return values */
  1390. if (v_retval) {
  1391. vf->num_invalid_msgs++;
  1392. dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
  1393. vf->vf_id, v_opcode, v_retval);
  1394. if (vf->num_invalid_msgs >
  1395. I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
  1396. dev_err(&pf->pdev->dev,
  1397. "Number of invalid messages exceeded for VF %d\n",
  1398. vf->vf_id);
  1399. dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
  1400. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  1401. }
  1402. } else {
  1403. vf->num_valid_msgs++;
  1404. /* reset the invalid counter, if a valid message is received. */
  1405. vf->num_invalid_msgs = 0;
  1406. }
  1407. aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  1408. msg, msglen, NULL);
  1409. if (aq_ret) {
  1410. dev_info(&pf->pdev->dev,
  1411. "Unable to send the message to VF %d aq_err %d\n",
  1412. vf->vf_id, pf->hw.aq.asq_last_status);
  1413. return -EIO;
  1414. }
  1415. return 0;
  1416. }
  1417. /**
  1418. * i40e_vc_send_resp_to_vf
  1419. * @vf: pointer to the VF info
  1420. * @opcode: operation code
  1421. * @retval: return value
  1422. *
  1423. * send resp msg to VF
  1424. **/
  1425. static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
  1426. enum virtchnl_ops opcode,
  1427. i40e_status retval)
  1428. {
  1429. return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
  1430. }
  1431. /**
  1432. * i40e_vc_get_version_msg
  1433. * @vf: pointer to the VF info
  1434. * @msg: pointer to the msg buffer
  1435. *
  1436. * called from the VF to request the API version used by the PF
  1437. **/
  1438. static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
  1439. {
  1440. struct virtchnl_version_info info = {
  1441. VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
  1442. };
  1443. vf->vf_ver = *(struct virtchnl_version_info *)msg;
  1444. /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
  1445. if (VF_IS_V10(&vf->vf_ver))
  1446. info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
  1447. return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
  1448. I40E_SUCCESS, (u8 *)&info,
  1449. sizeof(struct virtchnl_version_info));
  1450. }
  1451. /**
  1452. * i40e_del_qch - delete all the additional VSIs created as a part of ADq
  1453. * @vf: pointer to VF structure
  1454. **/
  1455. static void i40e_del_qch(struct i40e_vf *vf)
  1456. {
  1457. struct i40e_pf *pf = vf->pf;
  1458. int i;
  1459. /* first element in the array belongs to primary VF VSI and we shouldn't
  1460. * delete it. We should however delete the rest of the VSIs created
  1461. */
  1462. for (i = 1; i < vf->num_tc; i++) {
  1463. if (vf->ch[i].vsi_idx) {
  1464. i40e_vsi_release(pf->vsi[vf->ch[i].vsi_idx]);
  1465. vf->ch[i].vsi_idx = 0;
  1466. vf->ch[i].vsi_id = 0;
  1467. }
  1468. }
  1469. }
  1470. /**
  1471. * i40e_vc_get_vf_resources_msg
  1472. * @vf: pointer to the VF info
  1473. * @msg: pointer to the msg buffer
  1474. *
  1475. * called from the VF to request its resources
  1476. **/
  1477. static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
  1478. {
  1479. struct virtchnl_vf_resource *vfres = NULL;
  1480. struct i40e_pf *pf = vf->pf;
  1481. i40e_status aq_ret = 0;
  1482. struct i40e_vsi *vsi;
  1483. int num_vsis = 1;
  1484. int len = 0;
  1485. int ret;
  1486. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  1487. aq_ret = I40E_ERR_PARAM;
  1488. goto err;
  1489. }
  1490. len = (sizeof(struct virtchnl_vf_resource) +
  1491. sizeof(struct virtchnl_vsi_resource) * num_vsis);
  1492. vfres = kzalloc(len, GFP_KERNEL);
  1493. if (!vfres) {
  1494. aq_ret = I40E_ERR_NO_MEMORY;
  1495. len = 0;
  1496. goto err;
  1497. }
  1498. if (VF_IS_V11(&vf->vf_ver))
  1499. vf->driver_caps = *(u32 *)msg;
  1500. else
  1501. vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
  1502. VIRTCHNL_VF_OFFLOAD_RSS_REG |
  1503. VIRTCHNL_VF_OFFLOAD_VLAN;
  1504. vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
  1505. vsi = pf->vsi[vf->lan_vsi_idx];
  1506. if (!vsi->info.pvid)
  1507. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
  1508. if (i40e_vf_client_capable(pf, vf->vf_id) &&
  1509. (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_IWARP)) {
  1510. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_IWARP;
  1511. set_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
  1512. } else {
  1513. clear_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
  1514. }
  1515. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
  1516. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
  1517. } else {
  1518. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  1519. (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ))
  1520. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
  1521. else
  1522. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
  1523. }
  1524. if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
  1525. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
  1526. vfres->vf_cap_flags |=
  1527. VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
  1528. }
  1529. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
  1530. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
  1531. if ((pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE) &&
  1532. (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
  1533. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
  1534. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
  1535. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  1536. dev_err(&pf->pdev->dev,
  1537. "VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
  1538. vf->vf_id);
  1539. aq_ret = I40E_ERR_PARAM;
  1540. goto err;
  1541. }
  1542. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
  1543. }
  1544. if (pf->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) {
  1545. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
  1546. vfres->vf_cap_flags |=
  1547. VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
  1548. }
  1549. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
  1550. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
  1551. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)
  1552. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADQ;
  1553. vfres->num_vsis = num_vsis;
  1554. vfres->num_queue_pairs = vf->num_queue_pairs;
  1555. vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
  1556. vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
  1557. vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
  1558. if (vf->lan_vsi_idx) {
  1559. vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
  1560. vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
  1561. vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
  1562. /* VFs only use TC 0 */
  1563. vfres->vsi_res[0].qset_handle
  1564. = le16_to_cpu(vsi->info.qs_handle[0]);
  1565. ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
  1566. vf->default_lan_addr.addr);
  1567. }
  1568. set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  1569. err:
  1570. /* send the response back to the VF */
  1571. ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
  1572. aq_ret, (u8 *)vfres, len);
  1573. kfree(vfres);
  1574. return ret;
  1575. }
  1576. /**
  1577. * i40e_vc_reset_vf_msg
  1578. * @vf: pointer to the VF info
  1579. *
  1580. * called from the VF to reset itself,
  1581. * unlike other virtchnl messages, PF driver
  1582. * doesn't send the response back to the VF
  1583. **/
  1584. static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
  1585. {
  1586. if (test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  1587. i40e_reset_vf(vf, false);
  1588. }
  1589. /**
  1590. * i40e_getnum_vf_vsi_vlan_filters
  1591. * @vsi: pointer to the vsi
  1592. *
  1593. * called to get the number of VLANs offloaded on this VF
  1594. **/
  1595. static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
  1596. {
  1597. struct i40e_mac_filter *f;
  1598. int num_vlans = 0, bkt;
  1599. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1600. if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
  1601. num_vlans++;
  1602. }
  1603. return num_vlans;
  1604. }
  1605. /**
  1606. * i40e_vc_config_promiscuous_mode_msg
  1607. * @vf: pointer to the VF info
  1608. * @msg: pointer to the msg buffer
  1609. * @msglen: msg length
  1610. *
  1611. * called from the VF to configure the promiscuous mode of
  1612. * VF vsis
  1613. **/
  1614. static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
  1615. u8 *msg, u16 msglen)
  1616. {
  1617. struct virtchnl_promisc_info *info =
  1618. (struct virtchnl_promisc_info *)msg;
  1619. struct i40e_pf *pf = vf->pf;
  1620. struct i40e_hw *hw = &pf->hw;
  1621. struct i40e_mac_filter *f;
  1622. i40e_status aq_ret = 0;
  1623. bool allmulti = false;
  1624. struct i40e_vsi *vsi;
  1625. bool alluni = false;
  1626. int aq_err = 0;
  1627. int bkt;
  1628. vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
  1629. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  1630. !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
  1631. !vsi) {
  1632. aq_ret = I40E_ERR_PARAM;
  1633. goto error_param;
  1634. }
  1635. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  1636. dev_err(&pf->pdev->dev,
  1637. "Unprivileged VF %d is attempting to configure promiscuous mode\n",
  1638. vf->vf_id);
  1639. /* Lie to the VF on purpose. */
  1640. aq_ret = 0;
  1641. goto error_param;
  1642. }
  1643. /* Multicast promiscuous handling*/
  1644. if (info->flags & FLAG_VF_MULTICAST_PROMISC)
  1645. allmulti = true;
  1646. if (vf->port_vlan_id) {
  1647. aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
  1648. allmulti,
  1649. vf->port_vlan_id,
  1650. NULL);
  1651. } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
  1652. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1653. if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
  1654. continue;
  1655. aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw,
  1656. vsi->seid,
  1657. allmulti,
  1658. f->vlan,
  1659. NULL);
  1660. aq_err = pf->hw.aq.asq_last_status;
  1661. if (aq_ret) {
  1662. dev_err(&pf->pdev->dev,
  1663. "Could not add VLAN %d to multicast promiscuous domain err %s aq_err %s\n",
  1664. f->vlan,
  1665. i40e_stat_str(&pf->hw, aq_ret),
  1666. i40e_aq_str(&pf->hw, aq_err));
  1667. break;
  1668. }
  1669. }
  1670. } else {
  1671. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
  1672. allmulti, NULL);
  1673. aq_err = pf->hw.aq.asq_last_status;
  1674. if (aq_ret) {
  1675. dev_err(&pf->pdev->dev,
  1676. "VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
  1677. vf->vf_id,
  1678. i40e_stat_str(&pf->hw, aq_ret),
  1679. i40e_aq_str(&pf->hw, aq_err));
  1680. goto error_param;
  1681. }
  1682. }
  1683. if (!aq_ret) {
  1684. dev_info(&pf->pdev->dev,
  1685. "VF %d successfully set multicast promiscuous mode\n",
  1686. vf->vf_id);
  1687. if (allmulti)
  1688. set_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  1689. else
  1690. clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  1691. }
  1692. if (info->flags & FLAG_VF_UNICAST_PROMISC)
  1693. alluni = true;
  1694. if (vf->port_vlan_id) {
  1695. aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
  1696. alluni,
  1697. vf->port_vlan_id,
  1698. NULL);
  1699. } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
  1700. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1701. if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
  1702. continue;
  1703. aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
  1704. vsi->seid,
  1705. alluni,
  1706. f->vlan,
  1707. NULL);
  1708. aq_err = pf->hw.aq.asq_last_status;
  1709. if (aq_ret)
  1710. dev_err(&pf->pdev->dev,
  1711. "Could not add VLAN %d to Unicast promiscuous domain err %s aq_err %s\n",
  1712. f->vlan,
  1713. i40e_stat_str(&pf->hw, aq_ret),
  1714. i40e_aq_str(&pf->hw, aq_err));
  1715. }
  1716. } else {
  1717. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid,
  1718. alluni, NULL,
  1719. true);
  1720. aq_err = pf->hw.aq.asq_last_status;
  1721. if (aq_ret) {
  1722. dev_err(&pf->pdev->dev,
  1723. "VF %d failed to set unicast promiscuous mode %8.8x err %s aq_err %s\n",
  1724. vf->vf_id, info->flags,
  1725. i40e_stat_str(&pf->hw, aq_ret),
  1726. i40e_aq_str(&pf->hw, aq_err));
  1727. goto error_param;
  1728. }
  1729. }
  1730. if (!aq_ret) {
  1731. dev_info(&pf->pdev->dev,
  1732. "VF %d successfully set unicast promiscuous mode\n",
  1733. vf->vf_id);
  1734. if (alluni)
  1735. set_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  1736. else
  1737. clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  1738. }
  1739. error_param:
  1740. /* send the response to the VF */
  1741. return i40e_vc_send_resp_to_vf(vf,
  1742. VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
  1743. aq_ret);
  1744. }
  1745. /**
  1746. * i40e_vc_config_queues_msg
  1747. * @vf: pointer to the VF info
  1748. * @msg: pointer to the msg buffer
  1749. * @msglen: msg length
  1750. *
  1751. * called from the VF to configure the rx/tx
  1752. * queues
  1753. **/
  1754. static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1755. {
  1756. struct virtchnl_vsi_queue_config_info *qci =
  1757. (struct virtchnl_vsi_queue_config_info *)msg;
  1758. struct virtchnl_queue_pair_info *qpi;
  1759. struct i40e_pf *pf = vf->pf;
  1760. u16 vsi_id, vsi_queue_id = 0;
  1761. i40e_status aq_ret = 0;
  1762. int i, j = 0, idx = 0;
  1763. vsi_id = qci->vsi_id;
  1764. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1765. aq_ret = I40E_ERR_PARAM;
  1766. goto error_param;
  1767. }
  1768. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1769. aq_ret = I40E_ERR_PARAM;
  1770. goto error_param;
  1771. }
  1772. for (i = 0; i < qci->num_queue_pairs; i++) {
  1773. qpi = &qci->qpair[i];
  1774. if (!vf->adq_enabled) {
  1775. vsi_queue_id = qpi->txq.queue_id;
  1776. if (qpi->txq.vsi_id != qci->vsi_id ||
  1777. qpi->rxq.vsi_id != qci->vsi_id ||
  1778. qpi->rxq.queue_id != vsi_queue_id) {
  1779. aq_ret = I40E_ERR_PARAM;
  1780. goto error_param;
  1781. }
  1782. }
  1783. if (!i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
  1784. aq_ret = I40E_ERR_PARAM;
  1785. goto error_param;
  1786. }
  1787. if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
  1788. &qpi->rxq) ||
  1789. i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
  1790. &qpi->txq)) {
  1791. aq_ret = I40E_ERR_PARAM;
  1792. goto error_param;
  1793. }
  1794. /* For ADq there can be up to 4 VSIs with max 4 queues each.
  1795. * VF does not know about these additional VSIs and all
  1796. * it cares is about its own queues. PF configures these queues
  1797. * to its appropriate VSIs based on TC mapping
  1798. **/
  1799. if (vf->adq_enabled) {
  1800. if (j == (vf->ch[idx].num_qps - 1)) {
  1801. idx++;
  1802. j = 0; /* resetting the queue count */
  1803. vsi_queue_id = 0;
  1804. } else {
  1805. j++;
  1806. vsi_queue_id++;
  1807. }
  1808. vsi_id = vf->ch[idx].vsi_id;
  1809. }
  1810. }
  1811. /* set vsi num_queue_pairs in use to num configured by VF */
  1812. if (!vf->adq_enabled) {
  1813. pf->vsi[vf->lan_vsi_idx]->num_queue_pairs =
  1814. qci->num_queue_pairs;
  1815. } else {
  1816. for (i = 0; i < vf->num_tc; i++)
  1817. pf->vsi[vf->ch[i].vsi_idx]->num_queue_pairs =
  1818. vf->ch[i].num_qps;
  1819. }
  1820. error_param:
  1821. /* send the response to the VF */
  1822. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
  1823. aq_ret);
  1824. }
  1825. /**
  1826. * i40e_validate_queue_map
  1827. * @vsi_id: vsi id
  1828. * @queuemap: Tx or Rx queue map
  1829. *
  1830. * check if Tx or Rx queue map is valid
  1831. **/
  1832. static int i40e_validate_queue_map(struct i40e_vf *vf, u16 vsi_id,
  1833. unsigned long queuemap)
  1834. {
  1835. u16 vsi_queue_id, queue_id;
  1836. for_each_set_bit(vsi_queue_id, &queuemap, I40E_MAX_VSI_QP) {
  1837. if (vf->adq_enabled) {
  1838. vsi_id = vf->ch[vsi_queue_id / I40E_MAX_VF_VSI].vsi_id;
  1839. queue_id = (vsi_queue_id % I40E_DEFAULT_QUEUES_PER_VF);
  1840. } else {
  1841. queue_id = vsi_queue_id;
  1842. }
  1843. if (!i40e_vc_isvalid_queue_id(vf, vsi_id, queue_id))
  1844. return -EINVAL;
  1845. }
  1846. return 0;
  1847. }
  1848. /**
  1849. * i40e_vc_config_irq_map_msg
  1850. * @vf: pointer to the VF info
  1851. * @msg: pointer to the msg buffer
  1852. * @msglen: msg length
  1853. *
  1854. * called from the VF to configure the irq to
  1855. * queue map
  1856. **/
  1857. static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1858. {
  1859. struct virtchnl_irq_map_info *irqmap_info =
  1860. (struct virtchnl_irq_map_info *)msg;
  1861. struct virtchnl_vector_map *map;
  1862. u16 vsi_id, vector_id;
  1863. i40e_status aq_ret = 0;
  1864. int i;
  1865. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1866. aq_ret = I40E_ERR_PARAM;
  1867. goto error_param;
  1868. }
  1869. for (i = 0; i < irqmap_info->num_vectors; i++) {
  1870. map = &irqmap_info->vecmap[i];
  1871. vector_id = map->vector_id;
  1872. vsi_id = map->vsi_id;
  1873. /* validate msg params */
  1874. if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
  1875. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1876. aq_ret = I40E_ERR_PARAM;
  1877. goto error_param;
  1878. }
  1879. if (i40e_validate_queue_map(vf, vsi_id, map->rxq_map)) {
  1880. aq_ret = I40E_ERR_PARAM;
  1881. goto error_param;
  1882. }
  1883. if (i40e_validate_queue_map(vf, vsi_id, map->txq_map)) {
  1884. aq_ret = I40E_ERR_PARAM;
  1885. goto error_param;
  1886. }
  1887. i40e_config_irq_link_list(vf, vsi_id, map);
  1888. }
  1889. error_param:
  1890. /* send the response to the VF */
  1891. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
  1892. aq_ret);
  1893. }
  1894. /**
  1895. * i40e_ctrl_vf_tx_rings
  1896. * @vsi: the SRIOV VSI being configured
  1897. * @q_map: bit map of the queues to be enabled
  1898. * @enable: start or stop the queue
  1899. **/
  1900. static int i40e_ctrl_vf_tx_rings(struct i40e_vsi *vsi, unsigned long q_map,
  1901. bool enable)
  1902. {
  1903. struct i40e_pf *pf = vsi->back;
  1904. int ret = 0;
  1905. u16 q_id;
  1906. for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
  1907. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  1908. vsi->base_queue + q_id,
  1909. false /*is xdp*/, enable);
  1910. if (ret)
  1911. break;
  1912. }
  1913. return ret;
  1914. }
  1915. /**
  1916. * i40e_ctrl_vf_rx_rings
  1917. * @vsi: the SRIOV VSI being configured
  1918. * @q_map: bit map of the queues to be enabled
  1919. * @enable: start or stop the queue
  1920. **/
  1921. static int i40e_ctrl_vf_rx_rings(struct i40e_vsi *vsi, unsigned long q_map,
  1922. bool enable)
  1923. {
  1924. struct i40e_pf *pf = vsi->back;
  1925. int ret = 0;
  1926. u16 q_id;
  1927. for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
  1928. ret = i40e_control_wait_rx_q(pf, vsi->base_queue + q_id,
  1929. enable);
  1930. if (ret)
  1931. break;
  1932. }
  1933. return ret;
  1934. }
  1935. /**
  1936. * i40e_vc_enable_queues_msg
  1937. * @vf: pointer to the VF info
  1938. * @msg: pointer to the msg buffer
  1939. * @msglen: msg length
  1940. *
  1941. * called from the VF to enable all or specific queue(s)
  1942. **/
  1943. static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1944. {
  1945. struct virtchnl_queue_select *vqs =
  1946. (struct virtchnl_queue_select *)msg;
  1947. struct i40e_pf *pf = vf->pf;
  1948. u16 vsi_id = vqs->vsi_id;
  1949. i40e_status aq_ret = 0;
  1950. int i;
  1951. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1952. aq_ret = I40E_ERR_PARAM;
  1953. goto error_param;
  1954. }
  1955. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1956. aq_ret = I40E_ERR_PARAM;
  1957. goto error_param;
  1958. }
  1959. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1960. aq_ret = I40E_ERR_PARAM;
  1961. goto error_param;
  1962. }
  1963. /* Use the queue bit map sent by the VF */
  1964. if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
  1965. true)) {
  1966. aq_ret = I40E_ERR_TIMEOUT;
  1967. goto error_param;
  1968. }
  1969. if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
  1970. true)) {
  1971. aq_ret = I40E_ERR_TIMEOUT;
  1972. goto error_param;
  1973. }
  1974. /* need to start the rings for additional ADq VSI's as well */
  1975. if (vf->adq_enabled) {
  1976. /* zero belongs to LAN VSI */
  1977. for (i = 1; i < vf->num_tc; i++) {
  1978. if (i40e_vsi_start_rings(pf->vsi[vf->ch[i].vsi_idx]))
  1979. aq_ret = I40E_ERR_TIMEOUT;
  1980. }
  1981. }
  1982. error_param:
  1983. /* send the response to the VF */
  1984. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
  1985. aq_ret);
  1986. }
  1987. /**
  1988. * i40e_vc_disable_queues_msg
  1989. * @vf: pointer to the VF info
  1990. * @msg: pointer to the msg buffer
  1991. * @msglen: msg length
  1992. *
  1993. * called from the VF to disable all or specific
  1994. * queue(s)
  1995. **/
  1996. static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1997. {
  1998. struct virtchnl_queue_select *vqs =
  1999. (struct virtchnl_queue_select *)msg;
  2000. struct i40e_pf *pf = vf->pf;
  2001. i40e_status aq_ret = 0;
  2002. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2003. aq_ret = I40E_ERR_PARAM;
  2004. goto error_param;
  2005. }
  2006. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  2007. aq_ret = I40E_ERR_PARAM;
  2008. goto error_param;
  2009. }
  2010. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  2011. aq_ret = I40E_ERR_PARAM;
  2012. goto error_param;
  2013. }
  2014. /* Use the queue bit map sent by the VF */
  2015. if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
  2016. false)) {
  2017. aq_ret = I40E_ERR_TIMEOUT;
  2018. goto error_param;
  2019. }
  2020. if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
  2021. false)) {
  2022. aq_ret = I40E_ERR_TIMEOUT;
  2023. goto error_param;
  2024. }
  2025. error_param:
  2026. /* send the response to the VF */
  2027. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
  2028. aq_ret);
  2029. }
  2030. /**
  2031. * i40e_vc_request_queues_msg
  2032. * @vf: pointer to the VF info
  2033. * @msg: pointer to the msg buffer
  2034. * @msglen: msg length
  2035. *
  2036. * VFs get a default number of queues but can use this message to request a
  2037. * different number. If the request is successful, PF will reset the VF and
  2038. * return 0. If unsuccessful, PF will send message informing VF of number of
  2039. * available queues and return result of sending VF a message.
  2040. **/
  2041. static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg, int msglen)
  2042. {
  2043. struct virtchnl_vf_res_request *vfres =
  2044. (struct virtchnl_vf_res_request *)msg;
  2045. int req_pairs = vfres->num_queue_pairs;
  2046. int cur_pairs = vf->num_queue_pairs;
  2047. struct i40e_pf *pf = vf->pf;
  2048. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  2049. return -EINVAL;
  2050. if (req_pairs <= 0) {
  2051. dev_err(&pf->pdev->dev,
  2052. "VF %d tried to request %d queues. Ignoring.\n",
  2053. vf->vf_id, req_pairs);
  2054. } else if (req_pairs > I40E_MAX_VF_QUEUES) {
  2055. dev_err(&pf->pdev->dev,
  2056. "VF %d tried to request more than %d queues.\n",
  2057. vf->vf_id,
  2058. I40E_MAX_VF_QUEUES);
  2059. vfres->num_queue_pairs = I40E_MAX_VF_QUEUES;
  2060. } else if (req_pairs - cur_pairs > pf->queues_left) {
  2061. dev_warn(&pf->pdev->dev,
  2062. "VF %d requested %d more queues, but only %d left.\n",
  2063. vf->vf_id,
  2064. req_pairs - cur_pairs,
  2065. pf->queues_left);
  2066. vfres->num_queue_pairs = pf->queues_left + cur_pairs;
  2067. } else {
  2068. /* successful request */
  2069. vf->num_req_queues = req_pairs;
  2070. i40e_vc_notify_vf_reset(vf);
  2071. i40e_reset_vf(vf, false);
  2072. return 0;
  2073. }
  2074. return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES, 0,
  2075. (u8 *)vfres, sizeof(*vfres));
  2076. }
  2077. /**
  2078. * i40e_vc_get_stats_msg
  2079. * @vf: pointer to the VF info
  2080. * @msg: pointer to the msg buffer
  2081. * @msglen: msg length
  2082. *
  2083. * called from the VF to get vsi stats
  2084. **/
  2085. static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2086. {
  2087. struct virtchnl_queue_select *vqs =
  2088. (struct virtchnl_queue_select *)msg;
  2089. struct i40e_pf *pf = vf->pf;
  2090. struct i40e_eth_stats stats;
  2091. i40e_status aq_ret = 0;
  2092. struct i40e_vsi *vsi;
  2093. memset(&stats, 0, sizeof(struct i40e_eth_stats));
  2094. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2095. aq_ret = I40E_ERR_PARAM;
  2096. goto error_param;
  2097. }
  2098. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  2099. aq_ret = I40E_ERR_PARAM;
  2100. goto error_param;
  2101. }
  2102. vsi = pf->vsi[vf->lan_vsi_idx];
  2103. if (!vsi) {
  2104. aq_ret = I40E_ERR_PARAM;
  2105. goto error_param;
  2106. }
  2107. i40e_update_eth_stats(vsi);
  2108. stats = vsi->eth_stats;
  2109. error_param:
  2110. /* send the response back to the VF */
  2111. return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
  2112. (u8 *)&stats, sizeof(stats));
  2113. }
  2114. /* If the VF is not trusted restrict the number of MAC/VLAN it can program */
  2115. #define I40E_VC_MAX_MAC_ADDR_PER_VF 12
  2116. #define I40E_VC_MAX_VLAN_PER_VF 8
  2117. /**
  2118. * i40e_check_vf_permission
  2119. * @vf: pointer to the VF info
  2120. * @al: MAC address list from virtchnl
  2121. *
  2122. * Check that the given list of MAC addresses is allowed. Will return -EPERM
  2123. * if any address in the list is not valid. Checks the following conditions:
  2124. *
  2125. * 1) broadcast and zero addresses are never valid
  2126. * 2) unicast addresses are not allowed if the VMM has administratively set
  2127. * the VF MAC address, unless the VF is marked as privileged.
  2128. * 3) There is enough space to add all the addresses.
  2129. *
  2130. * Note that to guarantee consistency, it is expected this function be called
  2131. * while holding the mac_filter_hash_lock, as otherwise the current number of
  2132. * addresses might not be accurate.
  2133. **/
  2134. static inline int i40e_check_vf_permission(struct i40e_vf *vf,
  2135. struct virtchnl_ether_addr_list *al)
  2136. {
  2137. struct i40e_pf *pf = vf->pf;
  2138. int i;
  2139. /* If this VF is not privileged, then we can't add more than a limited
  2140. * number of addresses. Check to make sure that the additions do not
  2141. * push us over the limit.
  2142. */
  2143. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
  2144. (vf->num_mac + al->num_elements) > I40E_VC_MAX_MAC_ADDR_PER_VF) {
  2145. dev_err(&pf->pdev->dev,
  2146. "Cannot add more MAC addresses, VF is not trusted, switch the VF to trusted to add more functionality\n");
  2147. return -EPERM;
  2148. }
  2149. for (i = 0; i < al->num_elements; i++) {
  2150. u8 *addr = al->list[i].addr;
  2151. if (is_broadcast_ether_addr(addr) ||
  2152. is_zero_ether_addr(addr)) {
  2153. dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
  2154. addr);
  2155. return I40E_ERR_INVALID_MAC_ADDR;
  2156. }
  2157. /* If the host VMM administrator has set the VF MAC address
  2158. * administratively via the ndo_set_vf_mac command then deny
  2159. * permission to the VF to add or delete unicast MAC addresses.
  2160. * Unless the VF is privileged and then it can do whatever.
  2161. * The VF may request to set the MAC address filter already
  2162. * assigned to it so do not return an error in that case.
  2163. */
  2164. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
  2165. !is_multicast_ether_addr(addr) && vf->pf_set_mac &&
  2166. !ether_addr_equal(addr, vf->default_lan_addr.addr)) {
  2167. dev_err(&pf->pdev->dev,
  2168. "VF attempting to override administratively set MAC address, reload the VF driver to resume normal operation\n");
  2169. return -EPERM;
  2170. }
  2171. }
  2172. return 0;
  2173. }
  2174. /**
  2175. * i40e_vc_add_mac_addr_msg
  2176. * @vf: pointer to the VF info
  2177. * @msg: pointer to the msg buffer
  2178. * @msglen: msg length
  2179. *
  2180. * add guest mac address filter
  2181. **/
  2182. static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2183. {
  2184. struct virtchnl_ether_addr_list *al =
  2185. (struct virtchnl_ether_addr_list *)msg;
  2186. struct i40e_pf *pf = vf->pf;
  2187. struct i40e_vsi *vsi = NULL;
  2188. u16 vsi_id = al->vsi_id;
  2189. i40e_status ret = 0;
  2190. int i;
  2191. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2192. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2193. ret = I40E_ERR_PARAM;
  2194. goto error_param;
  2195. }
  2196. vsi = pf->vsi[vf->lan_vsi_idx];
  2197. /* Lock once, because all function inside for loop accesses VSI's
  2198. * MAC filter list which needs to be protected using same lock.
  2199. */
  2200. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2201. ret = i40e_check_vf_permission(vf, al);
  2202. if (ret) {
  2203. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2204. goto error_param;
  2205. }
  2206. /* add new addresses to the list */
  2207. for (i = 0; i < al->num_elements; i++) {
  2208. struct i40e_mac_filter *f;
  2209. f = i40e_find_mac(vsi, al->list[i].addr);
  2210. if (!f) {
  2211. f = i40e_add_mac_filter(vsi, al->list[i].addr);
  2212. if (!f) {
  2213. dev_err(&pf->pdev->dev,
  2214. "Unable to add MAC filter %pM for VF %d\n",
  2215. al->list[i].addr, vf->vf_id);
  2216. ret = I40E_ERR_PARAM;
  2217. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2218. goto error_param;
  2219. } else {
  2220. vf->num_mac++;
  2221. }
  2222. }
  2223. }
  2224. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2225. /* program the updated filter list */
  2226. ret = i40e_sync_vsi_filters(vsi);
  2227. if (ret)
  2228. dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
  2229. vf->vf_id, ret);
  2230. error_param:
  2231. /* send the response to the VF */
  2232. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
  2233. ret);
  2234. }
  2235. /**
  2236. * i40e_vc_del_mac_addr_msg
  2237. * @vf: pointer to the VF info
  2238. * @msg: pointer to the msg buffer
  2239. * @msglen: msg length
  2240. *
  2241. * remove guest mac address filter
  2242. **/
  2243. static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2244. {
  2245. struct virtchnl_ether_addr_list *al =
  2246. (struct virtchnl_ether_addr_list *)msg;
  2247. struct i40e_pf *pf = vf->pf;
  2248. struct i40e_vsi *vsi = NULL;
  2249. u16 vsi_id = al->vsi_id;
  2250. i40e_status ret = 0;
  2251. int i;
  2252. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2253. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2254. ret = I40E_ERR_PARAM;
  2255. goto error_param;
  2256. }
  2257. for (i = 0; i < al->num_elements; i++) {
  2258. if (is_broadcast_ether_addr(al->list[i].addr) ||
  2259. is_zero_ether_addr(al->list[i].addr)) {
  2260. dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
  2261. al->list[i].addr, vf->vf_id);
  2262. ret = I40E_ERR_INVALID_MAC_ADDR;
  2263. goto error_param;
  2264. }
  2265. }
  2266. vsi = pf->vsi[vf->lan_vsi_idx];
  2267. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2268. /* delete addresses from the list */
  2269. for (i = 0; i < al->num_elements; i++)
  2270. if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
  2271. ret = I40E_ERR_INVALID_MAC_ADDR;
  2272. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2273. goto error_param;
  2274. } else {
  2275. vf->num_mac--;
  2276. }
  2277. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2278. /* program the updated filter list */
  2279. ret = i40e_sync_vsi_filters(vsi);
  2280. if (ret)
  2281. dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
  2282. vf->vf_id, ret);
  2283. error_param:
  2284. /* send the response to the VF */
  2285. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
  2286. ret);
  2287. }
  2288. /**
  2289. * i40e_vc_add_vlan_msg
  2290. * @vf: pointer to the VF info
  2291. * @msg: pointer to the msg buffer
  2292. * @msglen: msg length
  2293. *
  2294. * program guest vlan id
  2295. **/
  2296. static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2297. {
  2298. struct virtchnl_vlan_filter_list *vfl =
  2299. (struct virtchnl_vlan_filter_list *)msg;
  2300. struct i40e_pf *pf = vf->pf;
  2301. struct i40e_vsi *vsi = NULL;
  2302. u16 vsi_id = vfl->vsi_id;
  2303. i40e_status aq_ret = 0;
  2304. int i;
  2305. if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
  2306. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  2307. dev_err(&pf->pdev->dev,
  2308. "VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
  2309. goto error_param;
  2310. }
  2311. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2312. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2313. aq_ret = I40E_ERR_PARAM;
  2314. goto error_param;
  2315. }
  2316. for (i = 0; i < vfl->num_elements; i++) {
  2317. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  2318. aq_ret = I40E_ERR_PARAM;
  2319. dev_err(&pf->pdev->dev,
  2320. "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
  2321. goto error_param;
  2322. }
  2323. }
  2324. vsi = pf->vsi[vf->lan_vsi_idx];
  2325. if (vsi->info.pvid) {
  2326. aq_ret = I40E_ERR_PARAM;
  2327. goto error_param;
  2328. }
  2329. i40e_vlan_stripping_enable(vsi);
  2330. for (i = 0; i < vfl->num_elements; i++) {
  2331. /* add new VLAN filter */
  2332. int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
  2333. if (!ret)
  2334. vf->num_vlan++;
  2335. if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
  2336. i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
  2337. true,
  2338. vfl->vlan_id[i],
  2339. NULL);
  2340. if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
  2341. i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
  2342. true,
  2343. vfl->vlan_id[i],
  2344. NULL);
  2345. if (ret)
  2346. dev_err(&pf->pdev->dev,
  2347. "Unable to add VLAN filter %d for VF %d, error %d\n",
  2348. vfl->vlan_id[i], vf->vf_id, ret);
  2349. }
  2350. error_param:
  2351. /* send the response to the VF */
  2352. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
  2353. }
  2354. /**
  2355. * i40e_vc_remove_vlan_msg
  2356. * @vf: pointer to the VF info
  2357. * @msg: pointer to the msg buffer
  2358. * @msglen: msg length
  2359. *
  2360. * remove programmed guest vlan id
  2361. **/
  2362. static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2363. {
  2364. struct virtchnl_vlan_filter_list *vfl =
  2365. (struct virtchnl_vlan_filter_list *)msg;
  2366. struct i40e_pf *pf = vf->pf;
  2367. struct i40e_vsi *vsi = NULL;
  2368. u16 vsi_id = vfl->vsi_id;
  2369. i40e_status aq_ret = 0;
  2370. int i;
  2371. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2372. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2373. aq_ret = I40E_ERR_PARAM;
  2374. goto error_param;
  2375. }
  2376. for (i = 0; i < vfl->num_elements; i++) {
  2377. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  2378. aq_ret = I40E_ERR_PARAM;
  2379. goto error_param;
  2380. }
  2381. }
  2382. vsi = pf->vsi[vf->lan_vsi_idx];
  2383. if (vsi->info.pvid) {
  2384. aq_ret = I40E_ERR_PARAM;
  2385. goto error_param;
  2386. }
  2387. for (i = 0; i < vfl->num_elements; i++) {
  2388. i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
  2389. vf->num_vlan--;
  2390. if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
  2391. i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
  2392. false,
  2393. vfl->vlan_id[i],
  2394. NULL);
  2395. if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
  2396. i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
  2397. false,
  2398. vfl->vlan_id[i],
  2399. NULL);
  2400. }
  2401. error_param:
  2402. /* send the response to the VF */
  2403. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
  2404. }
  2405. /**
  2406. * i40e_vc_iwarp_msg
  2407. * @vf: pointer to the VF info
  2408. * @msg: pointer to the msg buffer
  2409. * @msglen: msg length
  2410. *
  2411. * called from the VF for the iwarp msgs
  2412. **/
  2413. static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2414. {
  2415. struct i40e_pf *pf = vf->pf;
  2416. int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
  2417. i40e_status aq_ret = 0;
  2418. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2419. !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
  2420. aq_ret = I40E_ERR_PARAM;
  2421. goto error_param;
  2422. }
  2423. i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
  2424. msg, msglen);
  2425. error_param:
  2426. /* send the response to the VF */
  2427. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
  2428. aq_ret);
  2429. }
  2430. /**
  2431. * i40e_vc_iwarp_qvmap_msg
  2432. * @vf: pointer to the VF info
  2433. * @msg: pointer to the msg buffer
  2434. * @msglen: msg length
  2435. * @config: config qvmap or release it
  2436. *
  2437. * called from the VF for the iwarp msgs
  2438. **/
  2439. static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, u16 msglen,
  2440. bool config)
  2441. {
  2442. struct virtchnl_iwarp_qvlist_info *qvlist_info =
  2443. (struct virtchnl_iwarp_qvlist_info *)msg;
  2444. i40e_status aq_ret = 0;
  2445. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2446. !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
  2447. aq_ret = I40E_ERR_PARAM;
  2448. goto error_param;
  2449. }
  2450. if (config) {
  2451. if (i40e_config_iwarp_qvlist(vf, qvlist_info))
  2452. aq_ret = I40E_ERR_PARAM;
  2453. } else {
  2454. i40e_release_iwarp_qvlist(vf);
  2455. }
  2456. error_param:
  2457. /* send the response to the VF */
  2458. return i40e_vc_send_resp_to_vf(vf,
  2459. config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
  2460. VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
  2461. aq_ret);
  2462. }
  2463. /**
  2464. * i40e_vc_config_rss_key
  2465. * @vf: pointer to the VF info
  2466. * @msg: pointer to the msg buffer
  2467. * @msglen: msg length
  2468. *
  2469. * Configure the VF's RSS key
  2470. **/
  2471. static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2472. {
  2473. struct virtchnl_rss_key *vrk =
  2474. (struct virtchnl_rss_key *)msg;
  2475. struct i40e_pf *pf = vf->pf;
  2476. struct i40e_vsi *vsi = NULL;
  2477. u16 vsi_id = vrk->vsi_id;
  2478. i40e_status aq_ret = 0;
  2479. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2480. !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
  2481. (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
  2482. aq_ret = I40E_ERR_PARAM;
  2483. goto err;
  2484. }
  2485. vsi = pf->vsi[vf->lan_vsi_idx];
  2486. aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
  2487. err:
  2488. /* send the response to the VF */
  2489. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
  2490. aq_ret);
  2491. }
  2492. /**
  2493. * i40e_vc_config_rss_lut
  2494. * @vf: pointer to the VF info
  2495. * @msg: pointer to the msg buffer
  2496. * @msglen: msg length
  2497. *
  2498. * Configure the VF's RSS LUT
  2499. **/
  2500. static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2501. {
  2502. struct virtchnl_rss_lut *vrl =
  2503. (struct virtchnl_rss_lut *)msg;
  2504. struct i40e_pf *pf = vf->pf;
  2505. struct i40e_vsi *vsi = NULL;
  2506. u16 vsi_id = vrl->vsi_id;
  2507. i40e_status aq_ret = 0;
  2508. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2509. !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
  2510. (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
  2511. aq_ret = I40E_ERR_PARAM;
  2512. goto err;
  2513. }
  2514. vsi = pf->vsi[vf->lan_vsi_idx];
  2515. aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
  2516. /* send the response to the VF */
  2517. err:
  2518. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
  2519. aq_ret);
  2520. }
  2521. /**
  2522. * i40e_vc_get_rss_hena
  2523. * @vf: pointer to the VF info
  2524. * @msg: pointer to the msg buffer
  2525. * @msglen: msg length
  2526. *
  2527. * Return the RSS HENA bits allowed by the hardware
  2528. **/
  2529. static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2530. {
  2531. struct virtchnl_rss_hena *vrh = NULL;
  2532. struct i40e_pf *pf = vf->pf;
  2533. i40e_status aq_ret = 0;
  2534. int len = 0;
  2535. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2536. aq_ret = I40E_ERR_PARAM;
  2537. goto err;
  2538. }
  2539. len = sizeof(struct virtchnl_rss_hena);
  2540. vrh = kzalloc(len, GFP_KERNEL);
  2541. if (!vrh) {
  2542. aq_ret = I40E_ERR_NO_MEMORY;
  2543. len = 0;
  2544. goto err;
  2545. }
  2546. vrh->hena = i40e_pf_get_default_rss_hena(pf);
  2547. err:
  2548. /* send the response back to the VF */
  2549. aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
  2550. aq_ret, (u8 *)vrh, len);
  2551. kfree(vrh);
  2552. return aq_ret;
  2553. }
  2554. /**
  2555. * i40e_vc_set_rss_hena
  2556. * @vf: pointer to the VF info
  2557. * @msg: pointer to the msg buffer
  2558. * @msglen: msg length
  2559. *
  2560. * Set the RSS HENA bits for the VF
  2561. **/
  2562. static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2563. {
  2564. struct virtchnl_rss_hena *vrh =
  2565. (struct virtchnl_rss_hena *)msg;
  2566. struct i40e_pf *pf = vf->pf;
  2567. struct i40e_hw *hw = &pf->hw;
  2568. i40e_status aq_ret = 0;
  2569. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2570. aq_ret = I40E_ERR_PARAM;
  2571. goto err;
  2572. }
  2573. i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
  2574. i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
  2575. (u32)(vrh->hena >> 32));
  2576. /* send the response to the VF */
  2577. err:
  2578. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
  2579. }
  2580. /**
  2581. * i40e_vc_enable_vlan_stripping
  2582. * @vf: pointer to the VF info
  2583. * @msg: pointer to the msg buffer
  2584. * @msglen: msg length
  2585. *
  2586. * Enable vlan header stripping for the VF
  2587. **/
  2588. static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
  2589. u16 msglen)
  2590. {
  2591. struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
  2592. i40e_status aq_ret = 0;
  2593. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2594. aq_ret = I40E_ERR_PARAM;
  2595. goto err;
  2596. }
  2597. i40e_vlan_stripping_enable(vsi);
  2598. /* send the response to the VF */
  2599. err:
  2600. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
  2601. aq_ret);
  2602. }
  2603. /**
  2604. * i40e_vc_disable_vlan_stripping
  2605. * @vf: pointer to the VF info
  2606. * @msg: pointer to the msg buffer
  2607. * @msglen: msg length
  2608. *
  2609. * Disable vlan header stripping for the VF
  2610. **/
  2611. static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
  2612. u16 msglen)
  2613. {
  2614. struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
  2615. i40e_status aq_ret = 0;
  2616. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2617. aq_ret = I40E_ERR_PARAM;
  2618. goto err;
  2619. }
  2620. i40e_vlan_stripping_disable(vsi);
  2621. /* send the response to the VF */
  2622. err:
  2623. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
  2624. aq_ret);
  2625. }
  2626. /**
  2627. * i40e_validate_cloud_filter
  2628. * @mask: mask for TC filter
  2629. * @data: data for TC filter
  2630. *
  2631. * This function validates cloud filter programmed as TC filter for ADq
  2632. **/
  2633. static int i40e_validate_cloud_filter(struct i40e_vf *vf,
  2634. struct virtchnl_filter *tc_filter)
  2635. {
  2636. struct virtchnl_l4_spec mask = tc_filter->mask.tcp_spec;
  2637. struct virtchnl_l4_spec data = tc_filter->data.tcp_spec;
  2638. struct i40e_pf *pf = vf->pf;
  2639. struct i40e_vsi *vsi = NULL;
  2640. struct i40e_mac_filter *f;
  2641. struct hlist_node *h;
  2642. bool found = false;
  2643. int bkt;
  2644. if (!tc_filter->action) {
  2645. dev_info(&pf->pdev->dev,
  2646. "VF %d: Currently ADq doesn't support Drop Action\n",
  2647. vf->vf_id);
  2648. goto err;
  2649. }
  2650. /* action_meta is TC number here to which the filter is applied */
  2651. if (!tc_filter->action_meta ||
  2652. tc_filter->action_meta > I40E_MAX_VF_VSI) {
  2653. dev_info(&pf->pdev->dev, "VF %d: Invalid TC number %u\n",
  2654. vf->vf_id, tc_filter->action_meta);
  2655. goto err;
  2656. }
  2657. /* Check filter if it's programmed for advanced mode or basic mode.
  2658. * There are two ADq modes (for VF only),
  2659. * 1. Basic mode: intended to allow as many filter options as possible
  2660. * to be added to a VF in Non-trusted mode. Main goal is
  2661. * to add filters to its own MAC and VLAN id.
  2662. * 2. Advanced mode: is for allowing filters to be applied other than
  2663. * its own MAC or VLAN. This mode requires the VF to be
  2664. * Trusted.
  2665. */
  2666. if (mask.dst_mac[0] && !mask.dst_ip[0]) {
  2667. vsi = pf->vsi[vf->lan_vsi_idx];
  2668. f = i40e_find_mac(vsi, data.dst_mac);
  2669. if (!f) {
  2670. dev_info(&pf->pdev->dev,
  2671. "Destination MAC %pM doesn't belong to VF %d\n",
  2672. data.dst_mac, vf->vf_id);
  2673. goto err;
  2674. }
  2675. if (mask.vlan_id) {
  2676. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
  2677. hlist) {
  2678. if (f->vlan == ntohs(data.vlan_id)) {
  2679. found = true;
  2680. break;
  2681. }
  2682. }
  2683. if (!found) {
  2684. dev_info(&pf->pdev->dev,
  2685. "VF %d doesn't have any VLAN id %u\n",
  2686. vf->vf_id, ntohs(data.vlan_id));
  2687. goto err;
  2688. }
  2689. }
  2690. } else {
  2691. /* Check if VF is trusted */
  2692. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  2693. dev_err(&pf->pdev->dev,
  2694. "VF %d not trusted, make VF trusted to add advanced mode ADq cloud filters\n",
  2695. vf->vf_id);
  2696. return I40E_ERR_CONFIG;
  2697. }
  2698. }
  2699. if (mask.dst_mac[0] & data.dst_mac[0]) {
  2700. if (is_broadcast_ether_addr(data.dst_mac) ||
  2701. is_zero_ether_addr(data.dst_mac)) {
  2702. dev_info(&pf->pdev->dev, "VF %d: Invalid Dest MAC addr %pM\n",
  2703. vf->vf_id, data.dst_mac);
  2704. goto err;
  2705. }
  2706. }
  2707. if (mask.src_mac[0] & data.src_mac[0]) {
  2708. if (is_broadcast_ether_addr(data.src_mac) ||
  2709. is_zero_ether_addr(data.src_mac)) {
  2710. dev_info(&pf->pdev->dev, "VF %d: Invalid Source MAC addr %pM\n",
  2711. vf->vf_id, data.src_mac);
  2712. goto err;
  2713. }
  2714. }
  2715. if (mask.dst_port & data.dst_port) {
  2716. if (!data.dst_port || be16_to_cpu(data.dst_port) > 0xFFFF) {
  2717. dev_info(&pf->pdev->dev, "VF %d: Invalid Dest port\n",
  2718. vf->vf_id);
  2719. goto err;
  2720. }
  2721. }
  2722. if (mask.src_port & data.src_port) {
  2723. if (!data.src_port || be16_to_cpu(data.src_port) > 0xFFFF) {
  2724. dev_info(&pf->pdev->dev, "VF %d: Invalid Source port\n",
  2725. vf->vf_id);
  2726. goto err;
  2727. }
  2728. }
  2729. if (tc_filter->flow_type != VIRTCHNL_TCP_V6_FLOW &&
  2730. tc_filter->flow_type != VIRTCHNL_TCP_V4_FLOW) {
  2731. dev_info(&pf->pdev->dev, "VF %d: Invalid Flow type\n",
  2732. vf->vf_id);
  2733. goto err;
  2734. }
  2735. if (mask.vlan_id & data.vlan_id) {
  2736. if (ntohs(data.vlan_id) > I40E_MAX_VLANID) {
  2737. dev_info(&pf->pdev->dev, "VF %d: invalid VLAN ID\n",
  2738. vf->vf_id);
  2739. goto err;
  2740. }
  2741. }
  2742. return I40E_SUCCESS;
  2743. err:
  2744. return I40E_ERR_CONFIG;
  2745. }
  2746. /**
  2747. * i40e_find_vsi_from_seid - searches for the vsi with the given seid
  2748. * @vf: pointer to the VF info
  2749. * @seid - seid of the vsi it is searching for
  2750. **/
  2751. static struct i40e_vsi *i40e_find_vsi_from_seid(struct i40e_vf *vf, u16 seid)
  2752. {
  2753. struct i40e_pf *pf = vf->pf;
  2754. struct i40e_vsi *vsi = NULL;
  2755. int i;
  2756. for (i = 0; i < vf->num_tc ; i++) {
  2757. vsi = i40e_find_vsi_from_id(pf, vf->ch[i].vsi_id);
  2758. if (vsi && vsi->seid == seid)
  2759. return vsi;
  2760. }
  2761. return NULL;
  2762. }
  2763. /**
  2764. * i40e_del_all_cloud_filters
  2765. * @vf: pointer to the VF info
  2766. *
  2767. * This function deletes all cloud filters
  2768. **/
  2769. static void i40e_del_all_cloud_filters(struct i40e_vf *vf)
  2770. {
  2771. struct i40e_cloud_filter *cfilter = NULL;
  2772. struct i40e_pf *pf = vf->pf;
  2773. struct i40e_vsi *vsi = NULL;
  2774. struct hlist_node *node;
  2775. int ret;
  2776. hlist_for_each_entry_safe(cfilter, node,
  2777. &vf->cloud_filter_list, cloud_node) {
  2778. vsi = i40e_find_vsi_from_seid(vf, cfilter->seid);
  2779. if (!vsi) {
  2780. dev_err(&pf->pdev->dev, "VF %d: no VSI found for matching %u seid, can't delete cloud filter\n",
  2781. vf->vf_id, cfilter->seid);
  2782. continue;
  2783. }
  2784. if (cfilter->dst_port)
  2785. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
  2786. false);
  2787. else
  2788. ret = i40e_add_del_cloud_filter(vsi, cfilter, false);
  2789. if (ret)
  2790. dev_err(&pf->pdev->dev,
  2791. "VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
  2792. vf->vf_id, i40e_stat_str(&pf->hw, ret),
  2793. i40e_aq_str(&pf->hw,
  2794. pf->hw.aq.asq_last_status));
  2795. hlist_del(&cfilter->cloud_node);
  2796. kfree(cfilter);
  2797. vf->num_cloud_filters--;
  2798. }
  2799. }
  2800. /**
  2801. * i40e_vc_del_cloud_filter
  2802. * @vf: pointer to the VF info
  2803. * @msg: pointer to the msg buffer
  2804. *
  2805. * This function deletes a cloud filter programmed as TC filter for ADq
  2806. **/
  2807. static int i40e_vc_del_cloud_filter(struct i40e_vf *vf, u8 *msg)
  2808. {
  2809. struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
  2810. struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
  2811. struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
  2812. struct i40e_cloud_filter cfilter, *cf = NULL;
  2813. struct i40e_pf *pf = vf->pf;
  2814. struct i40e_vsi *vsi = NULL;
  2815. struct hlist_node *node;
  2816. i40e_status aq_ret = 0;
  2817. int i, ret;
  2818. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2819. aq_ret = I40E_ERR_PARAM;
  2820. goto err;
  2821. }
  2822. if (!vf->adq_enabled) {
  2823. dev_info(&pf->pdev->dev,
  2824. "VF %d: ADq not enabled, can't apply cloud filter\n",
  2825. vf->vf_id);
  2826. aq_ret = I40E_ERR_PARAM;
  2827. goto err;
  2828. }
  2829. if (i40e_validate_cloud_filter(vf, vcf)) {
  2830. dev_info(&pf->pdev->dev,
  2831. "VF %d: Invalid input, can't apply cloud filter\n",
  2832. vf->vf_id);
  2833. aq_ret = I40E_ERR_PARAM;
  2834. goto err;
  2835. }
  2836. memset(&cfilter, 0, sizeof(cfilter));
  2837. /* parse destination mac address */
  2838. for (i = 0; i < ETH_ALEN; i++)
  2839. cfilter.dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
  2840. /* parse source mac address */
  2841. for (i = 0; i < ETH_ALEN; i++)
  2842. cfilter.src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
  2843. cfilter.vlan_id = mask.vlan_id & tcf.vlan_id;
  2844. cfilter.dst_port = mask.dst_port & tcf.dst_port;
  2845. cfilter.src_port = mask.src_port & tcf.src_port;
  2846. switch (vcf->flow_type) {
  2847. case VIRTCHNL_TCP_V4_FLOW:
  2848. cfilter.n_proto = ETH_P_IP;
  2849. if (mask.dst_ip[0] & tcf.dst_ip[0])
  2850. memcpy(&cfilter.ip.v4.dst_ip, tcf.dst_ip,
  2851. ARRAY_SIZE(tcf.dst_ip));
  2852. else if (mask.src_ip[0] & tcf.dst_ip[0])
  2853. memcpy(&cfilter.ip.v4.src_ip, tcf.src_ip,
  2854. ARRAY_SIZE(tcf.dst_ip));
  2855. break;
  2856. case VIRTCHNL_TCP_V6_FLOW:
  2857. cfilter.n_proto = ETH_P_IPV6;
  2858. if (mask.dst_ip[3] & tcf.dst_ip[3])
  2859. memcpy(&cfilter.ip.v6.dst_ip6, tcf.dst_ip,
  2860. sizeof(cfilter.ip.v6.dst_ip6));
  2861. if (mask.src_ip[3] & tcf.src_ip[3])
  2862. memcpy(&cfilter.ip.v6.src_ip6, tcf.src_ip,
  2863. sizeof(cfilter.ip.v6.src_ip6));
  2864. break;
  2865. default:
  2866. /* TC filter can be configured based on different combinations
  2867. * and in this case IP is not a part of filter config
  2868. */
  2869. dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
  2870. vf->vf_id);
  2871. }
  2872. /* get the vsi to which the tc belongs to */
  2873. vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
  2874. cfilter.seid = vsi->seid;
  2875. cfilter.flags = vcf->field_flags;
  2876. /* Deleting TC filter */
  2877. if (tcf.dst_port)
  2878. ret = i40e_add_del_cloud_filter_big_buf(vsi, &cfilter, false);
  2879. else
  2880. ret = i40e_add_del_cloud_filter(vsi, &cfilter, false);
  2881. if (ret) {
  2882. dev_err(&pf->pdev->dev,
  2883. "VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
  2884. vf->vf_id, i40e_stat_str(&pf->hw, ret),
  2885. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  2886. goto err;
  2887. }
  2888. hlist_for_each_entry_safe(cf, node,
  2889. &vf->cloud_filter_list, cloud_node) {
  2890. if (cf->seid != cfilter.seid)
  2891. continue;
  2892. if (mask.dst_port)
  2893. if (cfilter.dst_port != cf->dst_port)
  2894. continue;
  2895. if (mask.dst_mac[0])
  2896. if (!ether_addr_equal(cf->src_mac, cfilter.src_mac))
  2897. continue;
  2898. /* for ipv4 data to be valid, only first byte of mask is set */
  2899. if (cfilter.n_proto == ETH_P_IP && mask.dst_ip[0])
  2900. if (memcmp(&cfilter.ip.v4.dst_ip, &cf->ip.v4.dst_ip,
  2901. ARRAY_SIZE(tcf.dst_ip)))
  2902. continue;
  2903. /* for ipv6, mask is set for all sixteen bytes (4 words) */
  2904. if (cfilter.n_proto == ETH_P_IPV6 && mask.dst_ip[3])
  2905. if (memcmp(&cfilter.ip.v6.dst_ip6, &cf->ip.v6.dst_ip6,
  2906. sizeof(cfilter.ip.v6.src_ip6)))
  2907. continue;
  2908. if (mask.vlan_id)
  2909. if (cfilter.vlan_id != cf->vlan_id)
  2910. continue;
  2911. hlist_del(&cf->cloud_node);
  2912. kfree(cf);
  2913. vf->num_cloud_filters--;
  2914. }
  2915. err:
  2916. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_CLOUD_FILTER,
  2917. aq_ret);
  2918. }
  2919. /**
  2920. * i40e_vc_add_cloud_filter
  2921. * @vf: pointer to the VF info
  2922. * @msg: pointer to the msg buffer
  2923. *
  2924. * This function adds a cloud filter programmed as TC filter for ADq
  2925. **/
  2926. static int i40e_vc_add_cloud_filter(struct i40e_vf *vf, u8 *msg)
  2927. {
  2928. struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
  2929. struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
  2930. struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
  2931. struct i40e_cloud_filter *cfilter = NULL;
  2932. struct i40e_pf *pf = vf->pf;
  2933. struct i40e_vsi *vsi = NULL;
  2934. i40e_status aq_ret = 0;
  2935. int i, ret;
  2936. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2937. aq_ret = I40E_ERR_PARAM;
  2938. goto err;
  2939. }
  2940. if (!vf->adq_enabled) {
  2941. dev_info(&pf->pdev->dev,
  2942. "VF %d: ADq is not enabled, can't apply cloud filter\n",
  2943. vf->vf_id);
  2944. aq_ret = I40E_ERR_PARAM;
  2945. goto err;
  2946. }
  2947. if (i40e_validate_cloud_filter(vf, vcf)) {
  2948. dev_info(&pf->pdev->dev,
  2949. "VF %d: Invalid input/s, can't apply cloud filter\n",
  2950. vf->vf_id);
  2951. aq_ret = I40E_ERR_PARAM;
  2952. goto err;
  2953. }
  2954. cfilter = kzalloc(sizeof(*cfilter), GFP_KERNEL);
  2955. if (!cfilter)
  2956. return -ENOMEM;
  2957. /* parse destination mac address */
  2958. for (i = 0; i < ETH_ALEN; i++)
  2959. cfilter->dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
  2960. /* parse source mac address */
  2961. for (i = 0; i < ETH_ALEN; i++)
  2962. cfilter->src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
  2963. cfilter->vlan_id = mask.vlan_id & tcf.vlan_id;
  2964. cfilter->dst_port = mask.dst_port & tcf.dst_port;
  2965. cfilter->src_port = mask.src_port & tcf.src_port;
  2966. switch (vcf->flow_type) {
  2967. case VIRTCHNL_TCP_V4_FLOW:
  2968. cfilter->n_proto = ETH_P_IP;
  2969. if (mask.dst_ip[0] & tcf.dst_ip[0])
  2970. memcpy(&cfilter->ip.v4.dst_ip, tcf.dst_ip,
  2971. ARRAY_SIZE(tcf.dst_ip));
  2972. else if (mask.src_ip[0] & tcf.dst_ip[0])
  2973. memcpy(&cfilter->ip.v4.src_ip, tcf.src_ip,
  2974. ARRAY_SIZE(tcf.dst_ip));
  2975. break;
  2976. case VIRTCHNL_TCP_V6_FLOW:
  2977. cfilter->n_proto = ETH_P_IPV6;
  2978. if (mask.dst_ip[3] & tcf.dst_ip[3])
  2979. memcpy(&cfilter->ip.v6.dst_ip6, tcf.dst_ip,
  2980. sizeof(cfilter->ip.v6.dst_ip6));
  2981. if (mask.src_ip[3] & tcf.src_ip[3])
  2982. memcpy(&cfilter->ip.v6.src_ip6, tcf.src_ip,
  2983. sizeof(cfilter->ip.v6.src_ip6));
  2984. break;
  2985. default:
  2986. /* TC filter can be configured based on different combinations
  2987. * and in this case IP is not a part of filter config
  2988. */
  2989. dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
  2990. vf->vf_id);
  2991. }
  2992. /* get the VSI to which the TC belongs to */
  2993. vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
  2994. cfilter->seid = vsi->seid;
  2995. cfilter->flags = vcf->field_flags;
  2996. /* Adding cloud filter programmed as TC filter */
  2997. if (tcf.dst_port)
  2998. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter, true);
  2999. else
  3000. ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
  3001. if (ret) {
  3002. dev_err(&pf->pdev->dev,
  3003. "VF %d: Failed to add cloud filter, err %s aq_err %s\n",
  3004. vf->vf_id, i40e_stat_str(&pf->hw, ret),
  3005. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  3006. goto err;
  3007. }
  3008. INIT_HLIST_NODE(&cfilter->cloud_node);
  3009. hlist_add_head(&cfilter->cloud_node, &vf->cloud_filter_list);
  3010. vf->num_cloud_filters++;
  3011. err:
  3012. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_CLOUD_FILTER,
  3013. aq_ret);
  3014. }
  3015. /**
  3016. * i40e_vc_add_qch_msg: Add queue channel and enable ADq
  3017. * @vf: pointer to the VF info
  3018. * @msg: pointer to the msg buffer
  3019. **/
  3020. static int i40e_vc_add_qch_msg(struct i40e_vf *vf, u8 *msg)
  3021. {
  3022. struct virtchnl_tc_info *tci =
  3023. (struct virtchnl_tc_info *)msg;
  3024. struct i40e_pf *pf = vf->pf;
  3025. struct i40e_link_status *ls = &pf->hw.phy.link_info;
  3026. int i, adq_request_qps = 0, speed = 0;
  3027. i40e_status aq_ret = 0;
  3028. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  3029. aq_ret = I40E_ERR_PARAM;
  3030. goto err;
  3031. }
  3032. /* ADq cannot be applied if spoof check is ON */
  3033. if (vf->spoofchk) {
  3034. dev_err(&pf->pdev->dev,
  3035. "Spoof check is ON, turn it OFF to enable ADq\n");
  3036. aq_ret = I40E_ERR_PARAM;
  3037. goto err;
  3038. }
  3039. if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)) {
  3040. dev_err(&pf->pdev->dev,
  3041. "VF %d attempting to enable ADq, but hasn't properly negotiated that capability\n",
  3042. vf->vf_id);
  3043. aq_ret = I40E_ERR_PARAM;
  3044. goto err;
  3045. }
  3046. /* max number of traffic classes for VF currently capped at 4 */
  3047. if (!tci->num_tc || tci->num_tc > I40E_MAX_VF_VSI) {
  3048. dev_err(&pf->pdev->dev,
  3049. "VF %d trying to set %u TCs, valid range 1-4 TCs per VF\n",
  3050. vf->vf_id, tci->num_tc);
  3051. aq_ret = I40E_ERR_PARAM;
  3052. goto err;
  3053. }
  3054. /* validate queues for each TC */
  3055. for (i = 0; i < tci->num_tc; i++)
  3056. if (!tci->list[i].count ||
  3057. tci->list[i].count > I40E_DEFAULT_QUEUES_PER_VF) {
  3058. dev_err(&pf->pdev->dev,
  3059. "VF %d: TC %d trying to set %u queues, valid range 1-4 queues per TC\n",
  3060. vf->vf_id, i, tci->list[i].count);
  3061. aq_ret = I40E_ERR_PARAM;
  3062. goto err;
  3063. }
  3064. /* need Max VF queues but already have default number of queues */
  3065. adq_request_qps = I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF;
  3066. if (pf->queues_left < adq_request_qps) {
  3067. dev_err(&pf->pdev->dev,
  3068. "No queues left to allocate to VF %d\n",
  3069. vf->vf_id);
  3070. aq_ret = I40E_ERR_PARAM;
  3071. goto err;
  3072. } else {
  3073. /* we need to allocate max VF queues to enable ADq so as to
  3074. * make sure ADq enabled VF always gets back queues when it
  3075. * goes through a reset.
  3076. */
  3077. vf->num_queue_pairs = I40E_MAX_VF_QUEUES;
  3078. }
  3079. /* get link speed in MB to validate rate limit */
  3080. switch (ls->link_speed) {
  3081. case VIRTCHNL_LINK_SPEED_100MB:
  3082. speed = SPEED_100;
  3083. break;
  3084. case VIRTCHNL_LINK_SPEED_1GB:
  3085. speed = SPEED_1000;
  3086. break;
  3087. case VIRTCHNL_LINK_SPEED_10GB:
  3088. speed = SPEED_10000;
  3089. break;
  3090. case VIRTCHNL_LINK_SPEED_20GB:
  3091. speed = SPEED_20000;
  3092. break;
  3093. case VIRTCHNL_LINK_SPEED_25GB:
  3094. speed = SPEED_25000;
  3095. break;
  3096. case VIRTCHNL_LINK_SPEED_40GB:
  3097. speed = SPEED_40000;
  3098. break;
  3099. default:
  3100. dev_err(&pf->pdev->dev,
  3101. "Cannot detect link speed\n");
  3102. aq_ret = I40E_ERR_PARAM;
  3103. goto err;
  3104. }
  3105. /* parse data from the queue channel info */
  3106. vf->num_tc = tci->num_tc;
  3107. for (i = 0; i < vf->num_tc; i++) {
  3108. if (tci->list[i].max_tx_rate) {
  3109. if (tci->list[i].max_tx_rate > speed) {
  3110. dev_err(&pf->pdev->dev,
  3111. "Invalid max tx rate %llu specified for VF %d.",
  3112. tci->list[i].max_tx_rate,
  3113. vf->vf_id);
  3114. aq_ret = I40E_ERR_PARAM;
  3115. goto err;
  3116. } else {
  3117. vf->ch[i].max_tx_rate =
  3118. tci->list[i].max_tx_rate;
  3119. }
  3120. }
  3121. vf->ch[i].num_qps = tci->list[i].count;
  3122. }
  3123. /* set this flag only after making sure all inputs are sane */
  3124. vf->adq_enabled = true;
  3125. /* num_req_queues is set when user changes number of queues via ethtool
  3126. * and this causes issue for default VSI(which depends on this variable)
  3127. * when ADq is enabled, hence reset it.
  3128. */
  3129. vf->num_req_queues = 0;
  3130. /* reset the VF in order to allocate resources */
  3131. i40e_vc_notify_vf_reset(vf);
  3132. i40e_reset_vf(vf, false);
  3133. return I40E_SUCCESS;
  3134. /* send the response to the VF */
  3135. err:
  3136. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_CHANNELS,
  3137. aq_ret);
  3138. }
  3139. /**
  3140. * i40e_vc_del_qch_msg
  3141. * @vf: pointer to the VF info
  3142. * @msg: pointer to the msg buffer
  3143. **/
  3144. static int i40e_vc_del_qch_msg(struct i40e_vf *vf, u8 *msg)
  3145. {
  3146. struct i40e_pf *pf = vf->pf;
  3147. i40e_status aq_ret = 0;
  3148. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  3149. aq_ret = I40E_ERR_PARAM;
  3150. goto err;
  3151. }
  3152. if (vf->adq_enabled) {
  3153. i40e_del_all_cloud_filters(vf);
  3154. i40e_del_qch(vf);
  3155. vf->adq_enabled = false;
  3156. vf->num_tc = 0;
  3157. dev_info(&pf->pdev->dev,
  3158. "Deleting Queue Channels and cloud filters for ADq on VF %d\n",
  3159. vf->vf_id);
  3160. } else {
  3161. dev_info(&pf->pdev->dev, "VF %d trying to delete queue channels but ADq isn't enabled\n",
  3162. vf->vf_id);
  3163. aq_ret = I40E_ERR_PARAM;
  3164. }
  3165. /* reset the VF in order to allocate resources */
  3166. i40e_vc_notify_vf_reset(vf);
  3167. i40e_reset_vf(vf, false);
  3168. return I40E_SUCCESS;
  3169. err:
  3170. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_CHANNELS,
  3171. aq_ret);
  3172. }
  3173. /**
  3174. * i40e_vc_process_vf_msg
  3175. * @pf: pointer to the PF structure
  3176. * @vf_id: source VF id
  3177. * @v_opcode: operation code
  3178. * @v_retval: unused return value code
  3179. * @msg: pointer to the msg buffer
  3180. * @msglen: msg length
  3181. *
  3182. * called from the common aeq/arq handler to
  3183. * process request from VF
  3184. **/
  3185. int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
  3186. u32 __always_unused v_retval, u8 *msg, u16 msglen)
  3187. {
  3188. struct i40e_hw *hw = &pf->hw;
  3189. int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
  3190. struct i40e_vf *vf;
  3191. int ret;
  3192. pf->vf_aq_requests++;
  3193. if (local_vf_id >= pf->num_alloc_vfs)
  3194. return -EINVAL;
  3195. vf = &(pf->vf[local_vf_id]);
  3196. /* Check if VF is disabled. */
  3197. if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
  3198. return I40E_ERR_PARAM;
  3199. /* perform basic checks on the msg */
  3200. ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
  3201. /* perform additional checks specific to this driver */
  3202. if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
  3203. struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
  3204. if (vrk->key_len != I40E_HKEY_ARRAY_SIZE)
  3205. ret = -EINVAL;
  3206. } else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
  3207. struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
  3208. if (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)
  3209. ret = -EINVAL;
  3210. }
  3211. if (ret) {
  3212. i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
  3213. dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
  3214. local_vf_id, v_opcode, msglen);
  3215. switch (ret) {
  3216. case VIRTCHNL_ERR_PARAM:
  3217. return -EPERM;
  3218. default:
  3219. return -EINVAL;
  3220. }
  3221. }
  3222. switch (v_opcode) {
  3223. case VIRTCHNL_OP_VERSION:
  3224. ret = i40e_vc_get_version_msg(vf, msg);
  3225. break;
  3226. case VIRTCHNL_OP_GET_VF_RESOURCES:
  3227. ret = i40e_vc_get_vf_resources_msg(vf, msg);
  3228. i40e_vc_notify_vf_link_state(vf);
  3229. break;
  3230. case VIRTCHNL_OP_RESET_VF:
  3231. i40e_vc_reset_vf_msg(vf);
  3232. ret = 0;
  3233. break;
  3234. case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
  3235. ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
  3236. break;
  3237. case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  3238. ret = i40e_vc_config_queues_msg(vf, msg, msglen);
  3239. break;
  3240. case VIRTCHNL_OP_CONFIG_IRQ_MAP:
  3241. ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
  3242. break;
  3243. case VIRTCHNL_OP_ENABLE_QUEUES:
  3244. ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
  3245. i40e_vc_notify_vf_link_state(vf);
  3246. break;
  3247. case VIRTCHNL_OP_DISABLE_QUEUES:
  3248. ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
  3249. break;
  3250. case VIRTCHNL_OP_ADD_ETH_ADDR:
  3251. ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
  3252. break;
  3253. case VIRTCHNL_OP_DEL_ETH_ADDR:
  3254. ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
  3255. break;
  3256. case VIRTCHNL_OP_ADD_VLAN:
  3257. ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
  3258. break;
  3259. case VIRTCHNL_OP_DEL_VLAN:
  3260. ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
  3261. break;
  3262. case VIRTCHNL_OP_GET_STATS:
  3263. ret = i40e_vc_get_stats_msg(vf, msg, msglen);
  3264. break;
  3265. case VIRTCHNL_OP_IWARP:
  3266. ret = i40e_vc_iwarp_msg(vf, msg, msglen);
  3267. break;
  3268. case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
  3269. ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, true);
  3270. break;
  3271. case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
  3272. ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, false);
  3273. break;
  3274. case VIRTCHNL_OP_CONFIG_RSS_KEY:
  3275. ret = i40e_vc_config_rss_key(vf, msg, msglen);
  3276. break;
  3277. case VIRTCHNL_OP_CONFIG_RSS_LUT:
  3278. ret = i40e_vc_config_rss_lut(vf, msg, msglen);
  3279. break;
  3280. case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
  3281. ret = i40e_vc_get_rss_hena(vf, msg, msglen);
  3282. break;
  3283. case VIRTCHNL_OP_SET_RSS_HENA:
  3284. ret = i40e_vc_set_rss_hena(vf, msg, msglen);
  3285. break;
  3286. case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
  3287. ret = i40e_vc_enable_vlan_stripping(vf, msg, msglen);
  3288. break;
  3289. case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
  3290. ret = i40e_vc_disable_vlan_stripping(vf, msg, msglen);
  3291. break;
  3292. case VIRTCHNL_OP_REQUEST_QUEUES:
  3293. ret = i40e_vc_request_queues_msg(vf, msg, msglen);
  3294. break;
  3295. case VIRTCHNL_OP_ENABLE_CHANNELS:
  3296. ret = i40e_vc_add_qch_msg(vf, msg);
  3297. break;
  3298. case VIRTCHNL_OP_DISABLE_CHANNELS:
  3299. ret = i40e_vc_del_qch_msg(vf, msg);
  3300. break;
  3301. case VIRTCHNL_OP_ADD_CLOUD_FILTER:
  3302. ret = i40e_vc_add_cloud_filter(vf, msg);
  3303. break;
  3304. case VIRTCHNL_OP_DEL_CLOUD_FILTER:
  3305. ret = i40e_vc_del_cloud_filter(vf, msg);
  3306. break;
  3307. case VIRTCHNL_OP_UNKNOWN:
  3308. default:
  3309. dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
  3310. v_opcode, local_vf_id);
  3311. ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
  3312. I40E_ERR_NOT_IMPLEMENTED);
  3313. break;
  3314. }
  3315. return ret;
  3316. }
  3317. /**
  3318. * i40e_vc_process_vflr_event
  3319. * @pf: pointer to the PF structure
  3320. *
  3321. * called from the vlfr irq handler to
  3322. * free up VF resources and state variables
  3323. **/
  3324. int i40e_vc_process_vflr_event(struct i40e_pf *pf)
  3325. {
  3326. struct i40e_hw *hw = &pf->hw;
  3327. u32 reg, reg_idx, bit_idx;
  3328. struct i40e_vf *vf;
  3329. int vf_id;
  3330. if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  3331. return 0;
  3332. /* Re-enable the VFLR interrupt cause here, before looking for which
  3333. * VF got reset. Otherwise, if another VF gets a reset while the
  3334. * first one is being processed, that interrupt will be lost, and
  3335. * that VF will be stuck in reset forever.
  3336. */
  3337. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  3338. reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3339. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  3340. i40e_flush(hw);
  3341. clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3342. for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
  3343. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  3344. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  3345. /* read GLGEN_VFLRSTAT register to find out the flr VFs */
  3346. vf = &pf->vf[vf_id];
  3347. reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
  3348. if (reg & BIT(bit_idx))
  3349. /* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
  3350. i40e_reset_vf(vf, true);
  3351. }
  3352. return 0;
  3353. }
  3354. /**
  3355. * i40e_ndo_set_vf_mac
  3356. * @netdev: network interface device structure
  3357. * @vf_id: VF identifier
  3358. * @mac: mac address
  3359. *
  3360. * program VF mac address
  3361. **/
  3362. int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
  3363. {
  3364. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3365. struct i40e_vsi *vsi = np->vsi;
  3366. struct i40e_pf *pf = vsi->back;
  3367. struct i40e_mac_filter *f;
  3368. struct i40e_vf *vf;
  3369. int ret = 0;
  3370. struct hlist_node *h;
  3371. int bkt;
  3372. u8 i;
  3373. /* validate the request */
  3374. if (vf_id >= pf->num_alloc_vfs) {
  3375. dev_err(&pf->pdev->dev,
  3376. "Invalid VF Identifier %d\n", vf_id);
  3377. ret = -EINVAL;
  3378. goto error_param;
  3379. }
  3380. vf = &(pf->vf[vf_id]);
  3381. vsi = pf->vsi[vf->lan_vsi_idx];
  3382. /* When the VF is resetting wait until it is done.
  3383. * It can take up to 200 milliseconds,
  3384. * but wait for up to 300 milliseconds to be safe.
  3385. */
  3386. for (i = 0; i < 15; i++) {
  3387. if (test_bit(I40E_VF_STATE_INIT, &vf->vf_states))
  3388. break;
  3389. msleep(20);
  3390. }
  3391. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3392. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3393. vf_id);
  3394. ret = -EAGAIN;
  3395. goto error_param;
  3396. }
  3397. if (is_multicast_ether_addr(mac)) {
  3398. dev_err(&pf->pdev->dev,
  3399. "Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
  3400. ret = -EINVAL;
  3401. goto error_param;
  3402. }
  3403. /* Lock once because below invoked function add/del_filter requires
  3404. * mac_filter_hash_lock to be held
  3405. */
  3406. spin_lock_bh(&vsi->mac_filter_hash_lock);
  3407. /* delete the temporary mac address */
  3408. if (!is_zero_ether_addr(vf->default_lan_addr.addr))
  3409. i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
  3410. /* Delete all the filters for this VSI - we're going to kill it
  3411. * anyway.
  3412. */
  3413. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  3414. __i40e_del_filter(vsi, f);
  3415. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3416. /* program mac filter */
  3417. if (i40e_sync_vsi_filters(vsi)) {
  3418. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  3419. ret = -EIO;
  3420. goto error_param;
  3421. }
  3422. ether_addr_copy(vf->default_lan_addr.addr, mac);
  3423. if (is_zero_ether_addr(mac)) {
  3424. vf->pf_set_mac = false;
  3425. dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
  3426. } else {
  3427. vf->pf_set_mac = true;
  3428. dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
  3429. mac, vf_id);
  3430. }
  3431. /* Force the VF driver stop so it has to reload with new MAC address */
  3432. i40e_vc_disable_vf(vf);
  3433. dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
  3434. error_param:
  3435. return ret;
  3436. }
  3437. /**
  3438. * i40e_vsi_has_vlans - True if VSI has configured VLANs
  3439. * @vsi: pointer to the vsi
  3440. *
  3441. * Check if a VSI has configured any VLANs. False if we have a port VLAN or if
  3442. * we have no configured VLANs. Do not call while holding the
  3443. * mac_filter_hash_lock.
  3444. */
  3445. static bool i40e_vsi_has_vlans(struct i40e_vsi *vsi)
  3446. {
  3447. bool have_vlans;
  3448. /* If we have a port VLAN, then the VSI cannot have any VLANs
  3449. * configured, as all MAC/VLAN filters will be assigned to the PVID.
  3450. */
  3451. if (vsi->info.pvid)
  3452. return false;
  3453. /* Since we don't have a PVID, we know that if the device is in VLAN
  3454. * mode it must be because of a VLAN filter configured on this VSI.
  3455. */
  3456. spin_lock_bh(&vsi->mac_filter_hash_lock);
  3457. have_vlans = i40e_is_vsi_in_vlan(vsi);
  3458. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3459. return have_vlans;
  3460. }
  3461. /**
  3462. * i40e_ndo_set_vf_port_vlan
  3463. * @netdev: network interface device structure
  3464. * @vf_id: VF identifier
  3465. * @vlan_id: mac address
  3466. * @qos: priority setting
  3467. * @vlan_proto: vlan protocol
  3468. *
  3469. * program VF vlan id and/or qos
  3470. **/
  3471. int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
  3472. u16 vlan_id, u8 qos, __be16 vlan_proto)
  3473. {
  3474. u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
  3475. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3476. struct i40e_pf *pf = np->vsi->back;
  3477. struct i40e_vsi *vsi;
  3478. struct i40e_vf *vf;
  3479. int ret = 0;
  3480. /* validate the request */
  3481. if (vf_id >= pf->num_alloc_vfs) {
  3482. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3483. ret = -EINVAL;
  3484. goto error_pvid;
  3485. }
  3486. if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
  3487. dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
  3488. ret = -EINVAL;
  3489. goto error_pvid;
  3490. }
  3491. if (vlan_proto != htons(ETH_P_8021Q)) {
  3492. dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
  3493. ret = -EPROTONOSUPPORT;
  3494. goto error_pvid;
  3495. }
  3496. vf = &(pf->vf[vf_id]);
  3497. vsi = pf->vsi[vf->lan_vsi_idx];
  3498. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3499. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3500. vf_id);
  3501. ret = -EAGAIN;
  3502. goto error_pvid;
  3503. }
  3504. if (le16_to_cpu(vsi->info.pvid) == vlanprio)
  3505. /* duplicate request, so just return success */
  3506. goto error_pvid;
  3507. if (i40e_vsi_has_vlans(vsi)) {
  3508. dev_err(&pf->pdev->dev,
  3509. "VF %d has already configured VLAN filters and the administrator is requesting a port VLAN override.\nPlease unload and reload the VF driver for this change to take effect.\n",
  3510. vf_id);
  3511. /* Administrator Error - knock the VF offline until he does
  3512. * the right thing by reconfiguring his network correctly
  3513. * and then reloading the VF driver.
  3514. */
  3515. i40e_vc_disable_vf(vf);
  3516. /* During reset the VF got a new VSI, so refresh the pointer. */
  3517. vsi = pf->vsi[vf->lan_vsi_idx];
  3518. }
  3519. /* Locked once because multiple functions below iterate list */
  3520. spin_lock_bh(&vsi->mac_filter_hash_lock);
  3521. /* Check for condition where there was already a port VLAN ID
  3522. * filter set and now it is being deleted by setting it to zero.
  3523. * Additionally check for the condition where there was a port
  3524. * VLAN but now there is a new and different port VLAN being set.
  3525. * Before deleting all the old VLAN filters we must add new ones
  3526. * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
  3527. * MAC addresses deleted.
  3528. */
  3529. if ((!(vlan_id || qos) ||
  3530. vlanprio != le16_to_cpu(vsi->info.pvid)) &&
  3531. vsi->info.pvid) {
  3532. ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
  3533. if (ret) {
  3534. dev_info(&vsi->back->pdev->dev,
  3535. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  3536. vsi->back->hw.aq.asq_last_status);
  3537. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3538. goto error_pvid;
  3539. }
  3540. }
  3541. if (vsi->info.pvid) {
  3542. /* remove all filters on the old VLAN */
  3543. i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
  3544. VLAN_VID_MASK));
  3545. }
  3546. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3547. if (vlan_id || qos)
  3548. ret = i40e_vsi_add_pvid(vsi, vlanprio);
  3549. else
  3550. i40e_vsi_remove_pvid(vsi);
  3551. spin_lock_bh(&vsi->mac_filter_hash_lock);
  3552. if (vlan_id) {
  3553. dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
  3554. vlan_id, qos, vf_id);
  3555. /* add new VLAN filter for each MAC */
  3556. ret = i40e_add_vlan_all_mac(vsi, vlan_id);
  3557. if (ret) {
  3558. dev_info(&vsi->back->pdev->dev,
  3559. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  3560. vsi->back->hw.aq.asq_last_status);
  3561. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3562. goto error_pvid;
  3563. }
  3564. /* remove the previously added non-VLAN MAC filters */
  3565. i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
  3566. }
  3567. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3568. /* Schedule the worker thread to take care of applying changes */
  3569. i40e_service_event_schedule(vsi->back);
  3570. if (ret) {
  3571. dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
  3572. goto error_pvid;
  3573. }
  3574. /* The Port VLAN needs to be saved across resets the same as the
  3575. * default LAN MAC address.
  3576. */
  3577. vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
  3578. ret = 0;
  3579. error_pvid:
  3580. return ret;
  3581. }
  3582. /**
  3583. * i40e_ndo_set_vf_bw
  3584. * @netdev: network interface device structure
  3585. * @vf_id: VF identifier
  3586. * @min_tx_rate: Minimum Tx rate
  3587. * @max_tx_rate: Maximum Tx rate
  3588. *
  3589. * configure VF Tx rate
  3590. **/
  3591. int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
  3592. int max_tx_rate)
  3593. {
  3594. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3595. struct i40e_pf *pf = np->vsi->back;
  3596. struct i40e_vsi *vsi;
  3597. struct i40e_vf *vf;
  3598. int ret = 0;
  3599. /* validate the request */
  3600. if (vf_id >= pf->num_alloc_vfs) {
  3601. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
  3602. ret = -EINVAL;
  3603. goto error;
  3604. }
  3605. if (min_tx_rate) {
  3606. dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
  3607. min_tx_rate, vf_id);
  3608. return -EINVAL;
  3609. }
  3610. vf = &(pf->vf[vf_id]);
  3611. vsi = pf->vsi[vf->lan_vsi_idx];
  3612. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3613. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3614. vf_id);
  3615. ret = -EAGAIN;
  3616. goto error;
  3617. }
  3618. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  3619. if (ret)
  3620. goto error;
  3621. vf->tx_rate = max_tx_rate;
  3622. error:
  3623. return ret;
  3624. }
  3625. /**
  3626. * i40e_ndo_get_vf_config
  3627. * @netdev: network interface device structure
  3628. * @vf_id: VF identifier
  3629. * @ivi: VF configuration structure
  3630. *
  3631. * return VF configuration
  3632. **/
  3633. int i40e_ndo_get_vf_config(struct net_device *netdev,
  3634. int vf_id, struct ifla_vf_info *ivi)
  3635. {
  3636. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3637. struct i40e_vsi *vsi = np->vsi;
  3638. struct i40e_pf *pf = vsi->back;
  3639. struct i40e_vf *vf;
  3640. int ret = 0;
  3641. /* validate the request */
  3642. if (vf_id >= pf->num_alloc_vfs) {
  3643. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3644. ret = -EINVAL;
  3645. goto error_param;
  3646. }
  3647. vf = &(pf->vf[vf_id]);
  3648. /* first vsi is always the LAN vsi */
  3649. vsi = pf->vsi[vf->lan_vsi_idx];
  3650. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3651. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3652. vf_id);
  3653. ret = -EAGAIN;
  3654. goto error_param;
  3655. }
  3656. ivi->vf = vf_id;
  3657. ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
  3658. ivi->max_tx_rate = vf->tx_rate;
  3659. ivi->min_tx_rate = 0;
  3660. ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
  3661. ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
  3662. I40E_VLAN_PRIORITY_SHIFT;
  3663. if (vf->link_forced == false)
  3664. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  3665. else if (vf->link_up == true)
  3666. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  3667. else
  3668. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  3669. ivi->spoofchk = vf->spoofchk;
  3670. ivi->trusted = vf->trusted;
  3671. ret = 0;
  3672. error_param:
  3673. return ret;
  3674. }
  3675. /**
  3676. * i40e_ndo_set_vf_link_state
  3677. * @netdev: network interface device structure
  3678. * @vf_id: VF identifier
  3679. * @link: required link state
  3680. *
  3681. * Set the link state of a specified VF, regardless of physical link state
  3682. **/
  3683. int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
  3684. {
  3685. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3686. struct i40e_pf *pf = np->vsi->back;
  3687. struct virtchnl_pf_event pfe;
  3688. struct i40e_hw *hw = &pf->hw;
  3689. struct i40e_vf *vf;
  3690. int abs_vf_id;
  3691. int ret = 0;
  3692. /* validate the request */
  3693. if (vf_id >= pf->num_alloc_vfs) {
  3694. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3695. ret = -EINVAL;
  3696. goto error_out;
  3697. }
  3698. vf = &pf->vf[vf_id];
  3699. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  3700. pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
  3701. pfe.severity = PF_EVENT_SEVERITY_INFO;
  3702. switch (link) {
  3703. case IFLA_VF_LINK_STATE_AUTO:
  3704. vf->link_forced = false;
  3705. pfe.event_data.link_event.link_status =
  3706. pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
  3707. pfe.event_data.link_event.link_speed =
  3708. (enum virtchnl_link_speed)
  3709. pf->hw.phy.link_info.link_speed;
  3710. break;
  3711. case IFLA_VF_LINK_STATE_ENABLE:
  3712. vf->link_forced = true;
  3713. vf->link_up = true;
  3714. pfe.event_data.link_event.link_status = true;
  3715. pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
  3716. break;
  3717. case IFLA_VF_LINK_STATE_DISABLE:
  3718. vf->link_forced = true;
  3719. vf->link_up = false;
  3720. pfe.event_data.link_event.link_status = false;
  3721. pfe.event_data.link_event.link_speed = 0;
  3722. break;
  3723. default:
  3724. ret = -EINVAL;
  3725. goto error_out;
  3726. }
  3727. /* Notify the VF of its new link state */
  3728. i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
  3729. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  3730. error_out:
  3731. return ret;
  3732. }
  3733. /**
  3734. * i40e_ndo_set_vf_spoofchk
  3735. * @netdev: network interface device structure
  3736. * @vf_id: VF identifier
  3737. * @enable: flag to enable or disable feature
  3738. *
  3739. * Enable or disable VF spoof checking
  3740. **/
  3741. int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
  3742. {
  3743. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3744. struct i40e_vsi *vsi = np->vsi;
  3745. struct i40e_pf *pf = vsi->back;
  3746. struct i40e_vsi_context ctxt;
  3747. struct i40e_hw *hw = &pf->hw;
  3748. struct i40e_vf *vf;
  3749. int ret = 0;
  3750. /* validate the request */
  3751. if (vf_id >= pf->num_alloc_vfs) {
  3752. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3753. ret = -EINVAL;
  3754. goto out;
  3755. }
  3756. vf = &(pf->vf[vf_id]);
  3757. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3758. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3759. vf_id);
  3760. ret = -EAGAIN;
  3761. goto out;
  3762. }
  3763. if (enable == vf->spoofchk)
  3764. goto out;
  3765. vf->spoofchk = enable;
  3766. memset(&ctxt, 0, sizeof(ctxt));
  3767. ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
  3768. ctxt.pf_num = pf->hw.pf_id;
  3769. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  3770. if (enable)
  3771. ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  3772. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  3773. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  3774. if (ret) {
  3775. dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
  3776. ret);
  3777. ret = -EIO;
  3778. }
  3779. out:
  3780. return ret;
  3781. }
  3782. /**
  3783. * i40e_ndo_set_vf_trust
  3784. * @netdev: network interface device structure of the pf
  3785. * @vf_id: VF identifier
  3786. * @setting: trust setting
  3787. *
  3788. * Enable or disable VF trust setting
  3789. **/
  3790. int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
  3791. {
  3792. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3793. struct i40e_pf *pf = np->vsi->back;
  3794. struct i40e_vf *vf;
  3795. int ret = 0;
  3796. /* validate the request */
  3797. if (vf_id >= pf->num_alloc_vfs) {
  3798. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3799. return -EINVAL;
  3800. }
  3801. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  3802. dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
  3803. return -EINVAL;
  3804. }
  3805. vf = &pf->vf[vf_id];
  3806. if (setting == vf->trusted)
  3807. goto out;
  3808. vf->trusted = setting;
  3809. i40e_vc_disable_vf(vf);
  3810. dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
  3811. vf_id, setting ? "" : "un");
  3812. if (vf->adq_enabled) {
  3813. if (!vf->trusted) {
  3814. dev_info(&pf->pdev->dev,
  3815. "VF %u no longer Trusted, deleting all cloud filters\n",
  3816. vf_id);
  3817. i40e_del_all_cloud_filters(vf);
  3818. }
  3819. }
  3820. out:
  3821. return ret;
  3822. }