i40e_virtchnl_pf.c 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380
  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. static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi);
  956. /**
  957. * i40e_config_vf_promiscuous_mode
  958. * @vf: pointer to the VF info
  959. * @vsi_id: VSI id
  960. * @allmulti: set MAC L2 layer multicast promiscuous enable/disable
  961. * @alluni: set MAC L2 layer unicast promiscuous enable/disable
  962. *
  963. * Called from the VF to configure the promiscuous mode of
  964. * VF vsis and from the VF reset path to reset promiscuous mode.
  965. **/
  966. static i40e_status i40e_config_vf_promiscuous_mode(struct i40e_vf *vf,
  967. u16 vsi_id,
  968. bool allmulti,
  969. bool alluni)
  970. {
  971. struct i40e_pf *pf = vf->pf;
  972. struct i40e_hw *hw = &pf->hw;
  973. struct i40e_mac_filter *f;
  974. i40e_status aq_ret = 0;
  975. struct i40e_vsi *vsi;
  976. int bkt;
  977. vsi = i40e_find_vsi_from_id(pf, vsi_id);
  978. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id) || !vsi)
  979. return I40E_ERR_PARAM;
  980. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  981. dev_err(&pf->pdev->dev,
  982. "Unprivileged VF %d is attempting to configure promiscuous mode\n",
  983. vf->vf_id);
  984. /* Lie to the VF on purpose. */
  985. return 0;
  986. }
  987. if (vf->port_vlan_id) {
  988. aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
  989. allmulti,
  990. vf->port_vlan_id,
  991. NULL);
  992. if (aq_ret) {
  993. int aq_err = pf->hw.aq.asq_last_status;
  994. dev_err(&pf->pdev->dev,
  995. "VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
  996. vf->vf_id,
  997. i40e_stat_str(&pf->hw, aq_ret),
  998. i40e_aq_str(&pf->hw, aq_err));
  999. return aq_ret;
  1000. }
  1001. aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
  1002. alluni,
  1003. vf->port_vlan_id,
  1004. NULL);
  1005. if (aq_ret) {
  1006. int aq_err = pf->hw.aq.asq_last_status;
  1007. dev_err(&pf->pdev->dev,
  1008. "VF %d failed to set unicast promiscuous mode err %s aq_err %s\n",
  1009. vf->vf_id,
  1010. i40e_stat_str(&pf->hw, aq_ret),
  1011. i40e_aq_str(&pf->hw, aq_err));
  1012. }
  1013. return aq_ret;
  1014. } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
  1015. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1016. if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
  1017. continue;
  1018. aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw,
  1019. vsi->seid,
  1020. allmulti,
  1021. f->vlan,
  1022. NULL);
  1023. if (aq_ret) {
  1024. int aq_err = pf->hw.aq.asq_last_status;
  1025. dev_err(&pf->pdev->dev,
  1026. "Could not add VLAN %d to multicast promiscuous domain err %s aq_err %s\n",
  1027. f->vlan,
  1028. i40e_stat_str(&pf->hw, aq_ret),
  1029. i40e_aq_str(&pf->hw, aq_err));
  1030. }
  1031. aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
  1032. vsi->seid,
  1033. alluni,
  1034. f->vlan,
  1035. NULL);
  1036. if (aq_ret) {
  1037. int aq_err = pf->hw.aq.asq_last_status;
  1038. dev_err(&pf->pdev->dev,
  1039. "Could not add VLAN %d to Unicast promiscuous domain err %s aq_err %s\n",
  1040. f->vlan,
  1041. i40e_stat_str(&pf->hw, aq_ret),
  1042. i40e_aq_str(&pf->hw, aq_err));
  1043. }
  1044. }
  1045. return aq_ret;
  1046. }
  1047. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid, allmulti,
  1048. NULL);
  1049. if (aq_ret) {
  1050. int aq_err = pf->hw.aq.asq_last_status;
  1051. dev_err(&pf->pdev->dev,
  1052. "VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
  1053. vf->vf_id,
  1054. i40e_stat_str(&pf->hw, aq_ret),
  1055. i40e_aq_str(&pf->hw, aq_err));
  1056. return aq_ret;
  1057. }
  1058. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid, alluni,
  1059. NULL, true);
  1060. if (aq_ret) {
  1061. int aq_err = pf->hw.aq.asq_last_status;
  1062. dev_err(&pf->pdev->dev,
  1063. "VF %d failed to set unicast promiscuous mode err %s aq_err %s\n",
  1064. vf->vf_id,
  1065. i40e_stat_str(&pf->hw, aq_ret),
  1066. i40e_aq_str(&pf->hw, aq_err));
  1067. }
  1068. return aq_ret;
  1069. }
  1070. /**
  1071. * i40e_trigger_vf_reset
  1072. * @vf: pointer to the VF structure
  1073. * @flr: VFLR was issued or not
  1074. *
  1075. * Trigger hardware to start a reset for a particular VF. Expects the caller
  1076. * to wait the proper amount of time to allow hardware to reset the VF before
  1077. * it cleans up and restores VF functionality.
  1078. **/
  1079. static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
  1080. {
  1081. struct i40e_pf *pf = vf->pf;
  1082. struct i40e_hw *hw = &pf->hw;
  1083. u32 reg, reg_idx, bit_idx;
  1084. /* warn the VF */
  1085. clear_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  1086. /* Disable VF's configuration API during reset. The flag is re-enabled
  1087. * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
  1088. * It's normally disabled in i40e_free_vf_res(), but it's safer
  1089. * to do it earlier to give some time to finish to any VF config
  1090. * functions that may still be running at this point.
  1091. */
  1092. clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  1093. /* In the case of a VFLR, the HW has already reset the VF and we
  1094. * just need to clean up, so don't hit the VFRTRIG register.
  1095. */
  1096. if (!flr) {
  1097. /* reset VF using VPGEN_VFRTRIG reg */
  1098. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  1099. reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  1100. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  1101. i40e_flush(hw);
  1102. }
  1103. /* clear the VFLR bit in GLGEN_VFLRSTAT */
  1104. reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
  1105. bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
  1106. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  1107. i40e_flush(hw);
  1108. if (i40e_quiesce_vf_pci(vf))
  1109. dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
  1110. vf->vf_id);
  1111. }
  1112. /**
  1113. * i40e_cleanup_reset_vf
  1114. * @vf: pointer to the VF structure
  1115. *
  1116. * Cleanup a VF after the hardware reset is finished. Expects the caller to
  1117. * have verified whether the reset is finished properly, and ensure the
  1118. * minimum amount of wait time has passed.
  1119. **/
  1120. static void i40e_cleanup_reset_vf(struct i40e_vf *vf)
  1121. {
  1122. struct i40e_pf *pf = vf->pf;
  1123. struct i40e_hw *hw = &pf->hw;
  1124. u32 reg;
  1125. /* disable promisc modes in case they were enabled */
  1126. i40e_config_vf_promiscuous_mode(vf, vf->lan_vsi_id, false, false);
  1127. /* free VF resources to begin resetting the VSI state */
  1128. i40e_free_vf_res(vf);
  1129. /* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
  1130. * By doing this we allow HW to access VF memory at any point. If we
  1131. * did it any sooner, HW could access memory while it was being freed
  1132. * in i40e_free_vf_res(), causing an IOMMU fault.
  1133. *
  1134. * On the other hand, this needs to be done ASAP, because the VF driver
  1135. * is waiting for this to happen and may report a timeout. It's
  1136. * harmless, but it gets logged into Guest OS kernel log, so best avoid
  1137. * it.
  1138. */
  1139. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  1140. reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  1141. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  1142. /* reallocate VF resources to finish resetting the VSI state */
  1143. if (!i40e_alloc_vf_res(vf)) {
  1144. int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  1145. i40e_enable_vf_mappings(vf);
  1146. set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  1147. clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  1148. /* Do not notify the client during VF init */
  1149. if (!test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
  1150. &vf->vf_states))
  1151. i40e_notify_client_of_vf_reset(pf, abs_vf_id);
  1152. vf->num_vlan = 0;
  1153. }
  1154. /* Tell the VF driver the reset is done. This needs to be done only
  1155. * after VF has been fully initialized, because the VF driver may
  1156. * request resources immediately after setting this flag.
  1157. */
  1158. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
  1159. }
  1160. /**
  1161. * i40e_reset_vf
  1162. * @vf: pointer to the VF structure
  1163. * @flr: VFLR was issued or not
  1164. *
  1165. * Returns true if the VF is reset, false otherwise.
  1166. **/
  1167. bool i40e_reset_vf(struct i40e_vf *vf, bool flr)
  1168. {
  1169. struct i40e_pf *pf = vf->pf;
  1170. struct i40e_hw *hw = &pf->hw;
  1171. bool rsd = false;
  1172. u32 reg;
  1173. int i;
  1174. /* If the VFs have been disabled, this means something else is
  1175. * resetting the VF, so we shouldn't continue.
  1176. */
  1177. if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  1178. return false;
  1179. i40e_trigger_vf_reset(vf, flr);
  1180. /* poll VPGEN_VFRSTAT reg to make sure
  1181. * that reset is complete
  1182. */
  1183. for (i = 0; i < 10; i++) {
  1184. /* VF reset requires driver to first reset the VF and then
  1185. * poll the status register to make sure that the reset
  1186. * completed successfully. Due to internal HW FIFO flushes,
  1187. * we must wait 10ms before the register will be valid.
  1188. */
  1189. usleep_range(10000, 20000);
  1190. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  1191. if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
  1192. rsd = true;
  1193. break;
  1194. }
  1195. }
  1196. if (flr)
  1197. usleep_range(10000, 20000);
  1198. if (!rsd)
  1199. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  1200. vf->vf_id);
  1201. usleep_range(10000, 20000);
  1202. /* On initial reset, we don't have any queues to disable */
  1203. if (vf->lan_vsi_idx != 0)
  1204. i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
  1205. i40e_cleanup_reset_vf(vf);
  1206. i40e_flush(hw);
  1207. clear_bit(__I40E_VF_DISABLE, pf->state);
  1208. return true;
  1209. }
  1210. /**
  1211. * i40e_reset_all_vfs
  1212. * @pf: pointer to the PF structure
  1213. * @flr: VFLR was issued or not
  1214. *
  1215. * Reset all allocated VFs in one go. First, tell the hardware to reset each
  1216. * VF, then do all the waiting in one chunk, and finally finish restoring each
  1217. * VF after the wait. This is useful during PF routines which need to reset
  1218. * all VFs, as otherwise it must perform these resets in a serialized fashion.
  1219. *
  1220. * Returns true if any VFs were reset, and false otherwise.
  1221. **/
  1222. bool i40e_reset_all_vfs(struct i40e_pf *pf, bool flr)
  1223. {
  1224. struct i40e_hw *hw = &pf->hw;
  1225. struct i40e_vf *vf;
  1226. int i, v;
  1227. u32 reg;
  1228. /* If we don't have any VFs, then there is nothing to reset */
  1229. if (!pf->num_alloc_vfs)
  1230. return false;
  1231. /* If VFs have been disabled, there is no need to reset */
  1232. if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  1233. return false;
  1234. /* Begin reset on all VFs at once */
  1235. for (v = 0; v < pf->num_alloc_vfs; v++)
  1236. i40e_trigger_vf_reset(&pf->vf[v], flr);
  1237. /* HW requires some time to make sure it can flush the FIFO for a VF
  1238. * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
  1239. * sequence to make sure that it has completed. We'll keep track of
  1240. * the VFs using a simple iterator that increments once that VF has
  1241. * finished resetting.
  1242. */
  1243. for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
  1244. usleep_range(10000, 20000);
  1245. /* Check each VF in sequence, beginning with the VF to fail
  1246. * the previous check.
  1247. */
  1248. while (v < pf->num_alloc_vfs) {
  1249. vf = &pf->vf[v];
  1250. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  1251. if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
  1252. break;
  1253. /* If the current VF has finished resetting, move on
  1254. * to the next VF in sequence.
  1255. */
  1256. v++;
  1257. }
  1258. }
  1259. if (flr)
  1260. usleep_range(10000, 20000);
  1261. /* Display a warning if at least one VF didn't manage to reset in
  1262. * time, but continue on with the operation.
  1263. */
  1264. if (v < pf->num_alloc_vfs)
  1265. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  1266. pf->vf[v].vf_id);
  1267. usleep_range(10000, 20000);
  1268. /* Begin disabling all the rings associated with VFs, but do not wait
  1269. * between each VF.
  1270. */
  1271. for (v = 0; v < pf->num_alloc_vfs; v++) {
  1272. /* On initial reset, we don't have any queues to disable */
  1273. if (pf->vf[v].lan_vsi_idx == 0)
  1274. continue;
  1275. i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
  1276. }
  1277. /* Now that we've notified HW to disable all of the VF rings, wait
  1278. * until they finish.
  1279. */
  1280. for (v = 0; v < pf->num_alloc_vfs; v++) {
  1281. /* On initial reset, we don't have any queues to disable */
  1282. if (pf->vf[v].lan_vsi_idx == 0)
  1283. continue;
  1284. i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
  1285. }
  1286. /* Hw may need up to 50ms to finish disabling the RX queues. We
  1287. * minimize the wait by delaying only once for all VFs.
  1288. */
  1289. mdelay(50);
  1290. /* Finish the reset on each VF */
  1291. for (v = 0; v < pf->num_alloc_vfs; v++)
  1292. i40e_cleanup_reset_vf(&pf->vf[v]);
  1293. i40e_flush(hw);
  1294. clear_bit(__I40E_VF_DISABLE, pf->state);
  1295. return true;
  1296. }
  1297. /**
  1298. * i40e_free_vfs
  1299. * @pf: pointer to the PF structure
  1300. *
  1301. * free VF resources
  1302. **/
  1303. void i40e_free_vfs(struct i40e_pf *pf)
  1304. {
  1305. struct i40e_hw *hw = &pf->hw;
  1306. u32 reg_idx, bit_idx;
  1307. int i, tmp, vf_id;
  1308. if (!pf->vf)
  1309. return;
  1310. while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  1311. usleep_range(1000, 2000);
  1312. i40e_notify_client_of_vf_enable(pf, 0);
  1313. /* Amortize wait time by stopping all VFs at the same time */
  1314. for (i = 0; i < pf->num_alloc_vfs; i++) {
  1315. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1316. continue;
  1317. i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
  1318. }
  1319. for (i = 0; i < pf->num_alloc_vfs; i++) {
  1320. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1321. continue;
  1322. i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
  1323. }
  1324. /* Disable IOV before freeing resources. This lets any VF drivers
  1325. * running in the host get themselves cleaned up before we yank
  1326. * the carpet out from underneath their feet.
  1327. */
  1328. if (!pci_vfs_assigned(pf->pdev))
  1329. pci_disable_sriov(pf->pdev);
  1330. else
  1331. dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
  1332. /* free up VF resources */
  1333. tmp = pf->num_alloc_vfs;
  1334. pf->num_alloc_vfs = 0;
  1335. for (i = 0; i < tmp; i++) {
  1336. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1337. i40e_free_vf_res(&pf->vf[i]);
  1338. /* disable qp mappings */
  1339. i40e_disable_vf_mappings(&pf->vf[i]);
  1340. }
  1341. kfree(pf->vf);
  1342. pf->vf = NULL;
  1343. /* This check is for when the driver is unloaded while VFs are
  1344. * assigned. Setting the number of VFs to 0 through sysfs is caught
  1345. * before this function ever gets called.
  1346. */
  1347. if (!pci_vfs_assigned(pf->pdev)) {
  1348. /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
  1349. * work correctly when SR-IOV gets re-enabled.
  1350. */
  1351. for (vf_id = 0; vf_id < tmp; vf_id++) {
  1352. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  1353. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  1354. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  1355. }
  1356. }
  1357. clear_bit(__I40E_VF_DISABLE, pf->state);
  1358. }
  1359. #ifdef CONFIG_PCI_IOV
  1360. /**
  1361. * i40e_alloc_vfs
  1362. * @pf: pointer to the PF structure
  1363. * @num_alloc_vfs: number of VFs to allocate
  1364. *
  1365. * allocate VF resources
  1366. **/
  1367. int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
  1368. {
  1369. struct i40e_vf *vfs;
  1370. int i, ret = 0;
  1371. /* Disable interrupt 0 so we don't try to handle the VFLR. */
  1372. i40e_irq_dynamic_disable_icr0(pf);
  1373. /* Check to see if we're just allocating resources for extant VFs */
  1374. if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
  1375. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  1376. if (ret) {
  1377. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  1378. pf->num_alloc_vfs = 0;
  1379. goto err_iov;
  1380. }
  1381. }
  1382. /* allocate memory */
  1383. vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
  1384. if (!vfs) {
  1385. ret = -ENOMEM;
  1386. goto err_alloc;
  1387. }
  1388. pf->vf = vfs;
  1389. /* apply default profile */
  1390. for (i = 0; i < num_alloc_vfs; i++) {
  1391. vfs[i].pf = pf;
  1392. vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
  1393. vfs[i].vf_id = i;
  1394. /* assign default capabilities */
  1395. set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
  1396. vfs[i].spoofchk = true;
  1397. set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
  1398. }
  1399. pf->num_alloc_vfs = num_alloc_vfs;
  1400. /* VF resources get allocated during reset */
  1401. i40e_reset_all_vfs(pf, false);
  1402. i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
  1403. err_alloc:
  1404. if (ret)
  1405. i40e_free_vfs(pf);
  1406. err_iov:
  1407. /* Re-enable interrupt 0. */
  1408. i40e_irq_dynamic_enable_icr0(pf);
  1409. return ret;
  1410. }
  1411. #endif
  1412. /**
  1413. * i40e_pci_sriov_enable
  1414. * @pdev: pointer to a pci_dev structure
  1415. * @num_vfs: number of VFs to allocate
  1416. *
  1417. * Enable or change the number of VFs
  1418. **/
  1419. static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
  1420. {
  1421. #ifdef CONFIG_PCI_IOV
  1422. struct i40e_pf *pf = pci_get_drvdata(pdev);
  1423. int pre_existing_vfs = pci_num_vf(pdev);
  1424. int err = 0;
  1425. if (test_bit(__I40E_TESTING, pf->state)) {
  1426. dev_warn(&pdev->dev,
  1427. "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
  1428. err = -EPERM;
  1429. goto err_out;
  1430. }
  1431. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  1432. i40e_free_vfs(pf);
  1433. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  1434. goto out;
  1435. if (num_vfs > pf->num_req_vfs) {
  1436. dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
  1437. num_vfs, pf->num_req_vfs);
  1438. err = -EPERM;
  1439. goto err_out;
  1440. }
  1441. dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
  1442. err = i40e_alloc_vfs(pf, num_vfs);
  1443. if (err) {
  1444. dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
  1445. goto err_out;
  1446. }
  1447. out:
  1448. return num_vfs;
  1449. err_out:
  1450. return err;
  1451. #endif
  1452. return 0;
  1453. }
  1454. /**
  1455. * i40e_pci_sriov_configure
  1456. * @pdev: pointer to a pci_dev structure
  1457. * @num_vfs: number of VFs to allocate
  1458. *
  1459. * Enable or change the number of VFs. Called when the user updates the number
  1460. * of VFs in sysfs.
  1461. **/
  1462. int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
  1463. {
  1464. struct i40e_pf *pf = pci_get_drvdata(pdev);
  1465. if (num_vfs) {
  1466. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  1467. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  1468. i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
  1469. }
  1470. return i40e_pci_sriov_enable(pdev, num_vfs);
  1471. }
  1472. if (!pci_vfs_assigned(pf->pdev)) {
  1473. i40e_free_vfs(pf);
  1474. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  1475. i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
  1476. } else {
  1477. dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
  1478. return -EINVAL;
  1479. }
  1480. return 0;
  1481. }
  1482. /***********************virtual channel routines******************/
  1483. /**
  1484. * i40e_vc_send_msg_to_vf
  1485. * @vf: pointer to the VF info
  1486. * @v_opcode: virtual channel opcode
  1487. * @v_retval: virtual channel return value
  1488. * @msg: pointer to the msg buffer
  1489. * @msglen: msg length
  1490. *
  1491. * send msg to VF
  1492. **/
  1493. static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
  1494. u32 v_retval, u8 *msg, u16 msglen)
  1495. {
  1496. struct i40e_pf *pf;
  1497. struct i40e_hw *hw;
  1498. int abs_vf_id;
  1499. i40e_status aq_ret;
  1500. /* validate the request */
  1501. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  1502. return -EINVAL;
  1503. pf = vf->pf;
  1504. hw = &pf->hw;
  1505. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  1506. /* single place to detect unsuccessful return values */
  1507. if (v_retval) {
  1508. vf->num_invalid_msgs++;
  1509. dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
  1510. vf->vf_id, v_opcode, v_retval);
  1511. if (vf->num_invalid_msgs >
  1512. I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
  1513. dev_err(&pf->pdev->dev,
  1514. "Number of invalid messages exceeded for VF %d\n",
  1515. vf->vf_id);
  1516. dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
  1517. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  1518. }
  1519. } else {
  1520. vf->num_valid_msgs++;
  1521. /* reset the invalid counter, if a valid message is received. */
  1522. vf->num_invalid_msgs = 0;
  1523. }
  1524. aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  1525. msg, msglen, NULL);
  1526. if (aq_ret) {
  1527. dev_info(&pf->pdev->dev,
  1528. "Unable to send the message to VF %d aq_err %d\n",
  1529. vf->vf_id, pf->hw.aq.asq_last_status);
  1530. return -EIO;
  1531. }
  1532. return 0;
  1533. }
  1534. /**
  1535. * i40e_vc_send_resp_to_vf
  1536. * @vf: pointer to the VF info
  1537. * @opcode: operation code
  1538. * @retval: return value
  1539. *
  1540. * send resp msg to VF
  1541. **/
  1542. static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
  1543. enum virtchnl_ops opcode,
  1544. i40e_status retval)
  1545. {
  1546. return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
  1547. }
  1548. /**
  1549. * i40e_vc_get_version_msg
  1550. * @vf: pointer to the VF info
  1551. * @msg: pointer to the msg buffer
  1552. *
  1553. * called from the VF to request the API version used by the PF
  1554. **/
  1555. static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
  1556. {
  1557. struct virtchnl_version_info info = {
  1558. VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
  1559. };
  1560. vf->vf_ver = *(struct virtchnl_version_info *)msg;
  1561. /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
  1562. if (VF_IS_V10(&vf->vf_ver))
  1563. info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
  1564. return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
  1565. I40E_SUCCESS, (u8 *)&info,
  1566. sizeof(struct virtchnl_version_info));
  1567. }
  1568. /**
  1569. * i40e_del_qch - delete all the additional VSIs created as a part of ADq
  1570. * @vf: pointer to VF structure
  1571. **/
  1572. static void i40e_del_qch(struct i40e_vf *vf)
  1573. {
  1574. struct i40e_pf *pf = vf->pf;
  1575. int i;
  1576. /* first element in the array belongs to primary VF VSI and we shouldn't
  1577. * delete it. We should however delete the rest of the VSIs created
  1578. */
  1579. for (i = 1; i < vf->num_tc; i++) {
  1580. if (vf->ch[i].vsi_idx) {
  1581. i40e_vsi_release(pf->vsi[vf->ch[i].vsi_idx]);
  1582. vf->ch[i].vsi_idx = 0;
  1583. vf->ch[i].vsi_id = 0;
  1584. }
  1585. }
  1586. }
  1587. /**
  1588. * i40e_vc_get_vf_resources_msg
  1589. * @vf: pointer to the VF info
  1590. * @msg: pointer to the msg buffer
  1591. *
  1592. * called from the VF to request its resources
  1593. **/
  1594. static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
  1595. {
  1596. struct virtchnl_vf_resource *vfres = NULL;
  1597. struct i40e_pf *pf = vf->pf;
  1598. i40e_status aq_ret = 0;
  1599. struct i40e_vsi *vsi;
  1600. int num_vsis = 1;
  1601. int len = 0;
  1602. int ret;
  1603. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  1604. aq_ret = I40E_ERR_PARAM;
  1605. goto err;
  1606. }
  1607. len = (sizeof(struct virtchnl_vf_resource) +
  1608. sizeof(struct virtchnl_vsi_resource) * num_vsis);
  1609. vfres = kzalloc(len, GFP_KERNEL);
  1610. if (!vfres) {
  1611. aq_ret = I40E_ERR_NO_MEMORY;
  1612. len = 0;
  1613. goto err;
  1614. }
  1615. if (VF_IS_V11(&vf->vf_ver))
  1616. vf->driver_caps = *(u32 *)msg;
  1617. else
  1618. vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
  1619. VIRTCHNL_VF_OFFLOAD_RSS_REG |
  1620. VIRTCHNL_VF_OFFLOAD_VLAN;
  1621. vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
  1622. vsi = pf->vsi[vf->lan_vsi_idx];
  1623. if (!vsi->info.pvid)
  1624. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
  1625. if (i40e_vf_client_capable(pf, vf->vf_id) &&
  1626. (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_IWARP)) {
  1627. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_IWARP;
  1628. set_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
  1629. } else {
  1630. clear_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
  1631. }
  1632. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
  1633. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
  1634. } else {
  1635. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  1636. (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ))
  1637. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
  1638. else
  1639. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
  1640. }
  1641. if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
  1642. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
  1643. vfres->vf_cap_flags |=
  1644. VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
  1645. }
  1646. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
  1647. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
  1648. if ((pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE) &&
  1649. (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
  1650. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
  1651. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
  1652. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  1653. dev_err(&pf->pdev->dev,
  1654. "VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
  1655. vf->vf_id);
  1656. aq_ret = I40E_ERR_PARAM;
  1657. goto err;
  1658. }
  1659. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
  1660. }
  1661. if (pf->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) {
  1662. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
  1663. vfres->vf_cap_flags |=
  1664. VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
  1665. }
  1666. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
  1667. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
  1668. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)
  1669. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADQ;
  1670. vfres->num_vsis = num_vsis;
  1671. vfres->num_queue_pairs = vf->num_queue_pairs;
  1672. vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
  1673. vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
  1674. vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
  1675. if (vf->lan_vsi_idx) {
  1676. vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
  1677. vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
  1678. vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
  1679. /* VFs only use TC 0 */
  1680. vfres->vsi_res[0].qset_handle
  1681. = le16_to_cpu(vsi->info.qs_handle[0]);
  1682. ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
  1683. vf->default_lan_addr.addr);
  1684. }
  1685. set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  1686. err:
  1687. /* send the response back to the VF */
  1688. ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
  1689. aq_ret, (u8 *)vfres, len);
  1690. kfree(vfres);
  1691. return ret;
  1692. }
  1693. /**
  1694. * i40e_vc_reset_vf_msg
  1695. * @vf: pointer to the VF info
  1696. *
  1697. * called from the VF to reset itself,
  1698. * unlike other virtchnl messages, PF driver
  1699. * doesn't send the response back to the VF
  1700. **/
  1701. static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
  1702. {
  1703. if (test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  1704. i40e_reset_vf(vf, false);
  1705. }
  1706. /**
  1707. * i40e_getnum_vf_vsi_vlan_filters
  1708. * @vsi: pointer to the vsi
  1709. *
  1710. * called to get the number of VLANs offloaded on this VF
  1711. **/
  1712. static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
  1713. {
  1714. struct i40e_mac_filter *f;
  1715. int num_vlans = 0, bkt;
  1716. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1717. if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
  1718. num_vlans++;
  1719. }
  1720. return num_vlans;
  1721. }
  1722. /**
  1723. * i40e_vc_config_promiscuous_mode_msg
  1724. * @vf: pointer to the VF info
  1725. * @msg: pointer to the msg buffer
  1726. *
  1727. * called from the VF to configure the promiscuous mode of
  1728. * VF vsis
  1729. **/
  1730. static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf, u8 *msg)
  1731. {
  1732. struct virtchnl_promisc_info *info =
  1733. (struct virtchnl_promisc_info *)msg;
  1734. struct i40e_pf *pf = vf->pf;
  1735. i40e_status aq_ret = 0;
  1736. bool allmulti = false;
  1737. bool alluni = false;
  1738. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  1739. return I40E_ERR_PARAM;
  1740. /* Multicast promiscuous handling*/
  1741. if (info->flags & FLAG_VF_MULTICAST_PROMISC)
  1742. allmulti = true;
  1743. if (info->flags & FLAG_VF_UNICAST_PROMISC)
  1744. alluni = true;
  1745. aq_ret = i40e_config_vf_promiscuous_mode(vf, info->vsi_id, allmulti,
  1746. alluni);
  1747. if (!aq_ret) {
  1748. if (allmulti) {
  1749. dev_info(&pf->pdev->dev,
  1750. "VF %d successfully set multicast promiscuous mode\n",
  1751. vf->vf_id);
  1752. set_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  1753. } else {
  1754. dev_info(&pf->pdev->dev,
  1755. "VF %d successfully unset multicast promiscuous mode\n",
  1756. vf->vf_id);
  1757. clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  1758. }
  1759. if (alluni) {
  1760. dev_info(&pf->pdev->dev,
  1761. "VF %d successfully set unicast promiscuous mode\n",
  1762. vf->vf_id);
  1763. set_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  1764. } else {
  1765. dev_info(&pf->pdev->dev,
  1766. "VF %d successfully unset unicast promiscuous mode\n",
  1767. vf->vf_id);
  1768. clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  1769. }
  1770. }
  1771. /* send the response to the VF */
  1772. return i40e_vc_send_resp_to_vf(vf,
  1773. VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
  1774. aq_ret);
  1775. }
  1776. /**
  1777. * i40e_vc_config_queues_msg
  1778. * @vf: pointer to the VF info
  1779. * @msg: pointer to the msg buffer
  1780. *
  1781. * called from the VF to configure the rx/tx
  1782. * queues
  1783. **/
  1784. static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg)
  1785. {
  1786. struct virtchnl_vsi_queue_config_info *qci =
  1787. (struct virtchnl_vsi_queue_config_info *)msg;
  1788. struct virtchnl_queue_pair_info *qpi;
  1789. struct i40e_pf *pf = vf->pf;
  1790. u16 vsi_id, vsi_queue_id = 0;
  1791. i40e_status aq_ret = 0;
  1792. int i, j = 0, idx = 0;
  1793. vsi_id = qci->vsi_id;
  1794. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1795. aq_ret = I40E_ERR_PARAM;
  1796. goto error_param;
  1797. }
  1798. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1799. aq_ret = I40E_ERR_PARAM;
  1800. goto error_param;
  1801. }
  1802. for (i = 0; i < qci->num_queue_pairs; i++) {
  1803. qpi = &qci->qpair[i];
  1804. if (!vf->adq_enabled) {
  1805. vsi_queue_id = qpi->txq.queue_id;
  1806. if (qpi->txq.vsi_id != qci->vsi_id ||
  1807. qpi->rxq.vsi_id != qci->vsi_id ||
  1808. qpi->rxq.queue_id != vsi_queue_id) {
  1809. aq_ret = I40E_ERR_PARAM;
  1810. goto error_param;
  1811. }
  1812. }
  1813. if (!i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
  1814. aq_ret = I40E_ERR_PARAM;
  1815. goto error_param;
  1816. }
  1817. if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
  1818. &qpi->rxq) ||
  1819. i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
  1820. &qpi->txq)) {
  1821. aq_ret = I40E_ERR_PARAM;
  1822. goto error_param;
  1823. }
  1824. /* For ADq there can be up to 4 VSIs with max 4 queues each.
  1825. * VF does not know about these additional VSIs and all
  1826. * it cares is about its own queues. PF configures these queues
  1827. * to its appropriate VSIs based on TC mapping
  1828. **/
  1829. if (vf->adq_enabled) {
  1830. if (j == (vf->ch[idx].num_qps - 1)) {
  1831. idx++;
  1832. j = 0; /* resetting the queue count */
  1833. vsi_queue_id = 0;
  1834. } else {
  1835. j++;
  1836. vsi_queue_id++;
  1837. }
  1838. vsi_id = vf->ch[idx].vsi_id;
  1839. }
  1840. }
  1841. /* set vsi num_queue_pairs in use to num configured by VF */
  1842. if (!vf->adq_enabled) {
  1843. pf->vsi[vf->lan_vsi_idx]->num_queue_pairs =
  1844. qci->num_queue_pairs;
  1845. } else {
  1846. for (i = 0; i < vf->num_tc; i++)
  1847. pf->vsi[vf->ch[i].vsi_idx]->num_queue_pairs =
  1848. vf->ch[i].num_qps;
  1849. }
  1850. error_param:
  1851. /* send the response to the VF */
  1852. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
  1853. aq_ret);
  1854. }
  1855. /**
  1856. * i40e_validate_queue_map
  1857. * @vsi_id: vsi id
  1858. * @queuemap: Tx or Rx queue map
  1859. *
  1860. * check if Tx or Rx queue map is valid
  1861. **/
  1862. static int i40e_validate_queue_map(struct i40e_vf *vf, u16 vsi_id,
  1863. unsigned long queuemap)
  1864. {
  1865. u16 vsi_queue_id, queue_id;
  1866. for_each_set_bit(vsi_queue_id, &queuemap, I40E_MAX_VSI_QP) {
  1867. if (vf->adq_enabled) {
  1868. vsi_id = vf->ch[vsi_queue_id / I40E_MAX_VF_VSI].vsi_id;
  1869. queue_id = (vsi_queue_id % I40E_DEFAULT_QUEUES_PER_VF);
  1870. } else {
  1871. queue_id = vsi_queue_id;
  1872. }
  1873. if (!i40e_vc_isvalid_queue_id(vf, vsi_id, queue_id))
  1874. return -EINVAL;
  1875. }
  1876. return 0;
  1877. }
  1878. /**
  1879. * i40e_vc_config_irq_map_msg
  1880. * @vf: pointer to the VF info
  1881. * @msg: pointer to the msg buffer
  1882. *
  1883. * called from the VF to configure the irq to
  1884. * queue map
  1885. **/
  1886. static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg)
  1887. {
  1888. struct virtchnl_irq_map_info *irqmap_info =
  1889. (struct virtchnl_irq_map_info *)msg;
  1890. struct virtchnl_vector_map *map;
  1891. u16 vsi_id, vector_id;
  1892. i40e_status aq_ret = 0;
  1893. int i;
  1894. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1895. aq_ret = I40E_ERR_PARAM;
  1896. goto error_param;
  1897. }
  1898. for (i = 0; i < irqmap_info->num_vectors; i++) {
  1899. map = &irqmap_info->vecmap[i];
  1900. vector_id = map->vector_id;
  1901. vsi_id = map->vsi_id;
  1902. /* validate msg params */
  1903. if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
  1904. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1905. aq_ret = I40E_ERR_PARAM;
  1906. goto error_param;
  1907. }
  1908. if (i40e_validate_queue_map(vf, vsi_id, map->rxq_map)) {
  1909. aq_ret = I40E_ERR_PARAM;
  1910. goto error_param;
  1911. }
  1912. if (i40e_validate_queue_map(vf, vsi_id, map->txq_map)) {
  1913. aq_ret = I40E_ERR_PARAM;
  1914. goto error_param;
  1915. }
  1916. i40e_config_irq_link_list(vf, vsi_id, map);
  1917. }
  1918. error_param:
  1919. /* send the response to the VF */
  1920. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
  1921. aq_ret);
  1922. }
  1923. /**
  1924. * i40e_ctrl_vf_tx_rings
  1925. * @vsi: the SRIOV VSI being configured
  1926. * @q_map: bit map of the queues to be enabled
  1927. * @enable: start or stop the queue
  1928. **/
  1929. static int i40e_ctrl_vf_tx_rings(struct i40e_vsi *vsi, unsigned long q_map,
  1930. bool enable)
  1931. {
  1932. struct i40e_pf *pf = vsi->back;
  1933. int ret = 0;
  1934. u16 q_id;
  1935. for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
  1936. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  1937. vsi->base_queue + q_id,
  1938. false /*is xdp*/, enable);
  1939. if (ret)
  1940. break;
  1941. }
  1942. return ret;
  1943. }
  1944. /**
  1945. * i40e_ctrl_vf_rx_rings
  1946. * @vsi: the SRIOV VSI being configured
  1947. * @q_map: bit map of the queues to be enabled
  1948. * @enable: start or stop the queue
  1949. **/
  1950. static int i40e_ctrl_vf_rx_rings(struct i40e_vsi *vsi, unsigned long q_map,
  1951. bool enable)
  1952. {
  1953. struct i40e_pf *pf = vsi->back;
  1954. int ret = 0;
  1955. u16 q_id;
  1956. for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
  1957. ret = i40e_control_wait_rx_q(pf, vsi->base_queue + q_id,
  1958. enable);
  1959. if (ret)
  1960. break;
  1961. }
  1962. return ret;
  1963. }
  1964. /**
  1965. * i40e_vc_enable_queues_msg
  1966. * @vf: pointer to the VF info
  1967. * @msg: pointer to the msg buffer
  1968. *
  1969. * called from the VF to enable all or specific queue(s)
  1970. **/
  1971. static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg)
  1972. {
  1973. struct virtchnl_queue_select *vqs =
  1974. (struct virtchnl_queue_select *)msg;
  1975. struct i40e_pf *pf = vf->pf;
  1976. u16 vsi_id = vqs->vsi_id;
  1977. i40e_status aq_ret = 0;
  1978. int i;
  1979. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1980. aq_ret = I40E_ERR_PARAM;
  1981. goto error_param;
  1982. }
  1983. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1984. aq_ret = I40E_ERR_PARAM;
  1985. goto error_param;
  1986. }
  1987. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1988. aq_ret = I40E_ERR_PARAM;
  1989. goto error_param;
  1990. }
  1991. /* Use the queue bit map sent by the VF */
  1992. if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
  1993. true)) {
  1994. aq_ret = I40E_ERR_TIMEOUT;
  1995. goto error_param;
  1996. }
  1997. if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
  1998. true)) {
  1999. aq_ret = I40E_ERR_TIMEOUT;
  2000. goto error_param;
  2001. }
  2002. /* need to start the rings for additional ADq VSI's as well */
  2003. if (vf->adq_enabled) {
  2004. /* zero belongs to LAN VSI */
  2005. for (i = 1; i < vf->num_tc; i++) {
  2006. if (i40e_vsi_start_rings(pf->vsi[vf->ch[i].vsi_idx]))
  2007. aq_ret = I40E_ERR_TIMEOUT;
  2008. }
  2009. }
  2010. error_param:
  2011. /* send the response to the VF */
  2012. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
  2013. aq_ret);
  2014. }
  2015. /**
  2016. * i40e_vc_disable_queues_msg
  2017. * @vf: pointer to the VF info
  2018. * @msg: pointer to the msg buffer
  2019. *
  2020. * called from the VF to disable all or specific
  2021. * queue(s)
  2022. **/
  2023. static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg)
  2024. {
  2025. struct virtchnl_queue_select *vqs =
  2026. (struct virtchnl_queue_select *)msg;
  2027. struct i40e_pf *pf = vf->pf;
  2028. i40e_status aq_ret = 0;
  2029. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2030. aq_ret = I40E_ERR_PARAM;
  2031. goto error_param;
  2032. }
  2033. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  2034. aq_ret = I40E_ERR_PARAM;
  2035. goto error_param;
  2036. }
  2037. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  2038. aq_ret = I40E_ERR_PARAM;
  2039. goto error_param;
  2040. }
  2041. /* Use the queue bit map sent by the VF */
  2042. if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
  2043. false)) {
  2044. aq_ret = I40E_ERR_TIMEOUT;
  2045. goto error_param;
  2046. }
  2047. if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
  2048. false)) {
  2049. aq_ret = I40E_ERR_TIMEOUT;
  2050. goto error_param;
  2051. }
  2052. error_param:
  2053. /* send the response to the VF */
  2054. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
  2055. aq_ret);
  2056. }
  2057. /**
  2058. * i40e_vc_request_queues_msg
  2059. * @vf: pointer to the VF info
  2060. * @msg: pointer to the msg buffer
  2061. *
  2062. * VFs get a default number of queues but can use this message to request a
  2063. * different number. If the request is successful, PF will reset the VF and
  2064. * return 0. If unsuccessful, PF will send message informing VF of number of
  2065. * available queues and return result of sending VF a message.
  2066. **/
  2067. static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg)
  2068. {
  2069. struct virtchnl_vf_res_request *vfres =
  2070. (struct virtchnl_vf_res_request *)msg;
  2071. int req_pairs = vfres->num_queue_pairs;
  2072. int cur_pairs = vf->num_queue_pairs;
  2073. struct i40e_pf *pf = vf->pf;
  2074. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  2075. return -EINVAL;
  2076. if (req_pairs <= 0) {
  2077. dev_err(&pf->pdev->dev,
  2078. "VF %d tried to request %d queues. Ignoring.\n",
  2079. vf->vf_id, req_pairs);
  2080. } else if (req_pairs > I40E_MAX_VF_QUEUES) {
  2081. dev_err(&pf->pdev->dev,
  2082. "VF %d tried to request more than %d queues.\n",
  2083. vf->vf_id,
  2084. I40E_MAX_VF_QUEUES);
  2085. vfres->num_queue_pairs = I40E_MAX_VF_QUEUES;
  2086. } else if (req_pairs - cur_pairs > pf->queues_left) {
  2087. dev_warn(&pf->pdev->dev,
  2088. "VF %d requested %d more queues, but only %d left.\n",
  2089. vf->vf_id,
  2090. req_pairs - cur_pairs,
  2091. pf->queues_left);
  2092. vfres->num_queue_pairs = pf->queues_left + cur_pairs;
  2093. } else {
  2094. /* successful request */
  2095. vf->num_req_queues = req_pairs;
  2096. i40e_vc_notify_vf_reset(vf);
  2097. i40e_reset_vf(vf, false);
  2098. return 0;
  2099. }
  2100. return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES, 0,
  2101. (u8 *)vfres, sizeof(*vfres));
  2102. }
  2103. /**
  2104. * i40e_vc_get_stats_msg
  2105. * @vf: pointer to the VF info
  2106. * @msg: pointer to the msg buffer
  2107. *
  2108. * called from the VF to get vsi stats
  2109. **/
  2110. static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg)
  2111. {
  2112. struct virtchnl_queue_select *vqs =
  2113. (struct virtchnl_queue_select *)msg;
  2114. struct i40e_pf *pf = vf->pf;
  2115. struct i40e_eth_stats stats;
  2116. i40e_status aq_ret = 0;
  2117. struct i40e_vsi *vsi;
  2118. memset(&stats, 0, sizeof(struct i40e_eth_stats));
  2119. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2120. aq_ret = I40E_ERR_PARAM;
  2121. goto error_param;
  2122. }
  2123. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  2124. aq_ret = I40E_ERR_PARAM;
  2125. goto error_param;
  2126. }
  2127. vsi = pf->vsi[vf->lan_vsi_idx];
  2128. if (!vsi) {
  2129. aq_ret = I40E_ERR_PARAM;
  2130. goto error_param;
  2131. }
  2132. i40e_update_eth_stats(vsi);
  2133. stats = vsi->eth_stats;
  2134. error_param:
  2135. /* send the response back to the VF */
  2136. return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
  2137. (u8 *)&stats, sizeof(stats));
  2138. }
  2139. /* If the VF is not trusted restrict the number of MAC/VLAN it can program */
  2140. #define I40E_VC_MAX_MAC_ADDR_PER_VF 12
  2141. #define I40E_VC_MAX_VLAN_PER_VF 8
  2142. /**
  2143. * i40e_check_vf_permission
  2144. * @vf: pointer to the VF info
  2145. * @al: MAC address list from virtchnl
  2146. *
  2147. * Check that the given list of MAC addresses is allowed. Will return -EPERM
  2148. * if any address in the list is not valid. Checks the following conditions:
  2149. *
  2150. * 1) broadcast and zero addresses are never valid
  2151. * 2) unicast addresses are not allowed if the VMM has administratively set
  2152. * the VF MAC address, unless the VF is marked as privileged.
  2153. * 3) There is enough space to add all the addresses.
  2154. *
  2155. * Note that to guarantee consistency, it is expected this function be called
  2156. * while holding the mac_filter_hash_lock, as otherwise the current number of
  2157. * addresses might not be accurate.
  2158. **/
  2159. static inline int i40e_check_vf_permission(struct i40e_vf *vf,
  2160. struct virtchnl_ether_addr_list *al)
  2161. {
  2162. struct i40e_pf *pf = vf->pf;
  2163. int i;
  2164. /* If this VF is not privileged, then we can't add more than a limited
  2165. * number of addresses. Check to make sure that the additions do not
  2166. * push us over the limit.
  2167. */
  2168. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
  2169. (vf->num_mac + al->num_elements) > I40E_VC_MAX_MAC_ADDR_PER_VF) {
  2170. dev_err(&pf->pdev->dev,
  2171. "Cannot add more MAC addresses, VF is not trusted, switch the VF to trusted to add more functionality\n");
  2172. return -EPERM;
  2173. }
  2174. for (i = 0; i < al->num_elements; i++) {
  2175. u8 *addr = al->list[i].addr;
  2176. if (is_broadcast_ether_addr(addr) ||
  2177. is_zero_ether_addr(addr)) {
  2178. dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
  2179. addr);
  2180. return I40E_ERR_INVALID_MAC_ADDR;
  2181. }
  2182. /* If the host VMM administrator has set the VF MAC address
  2183. * administratively via the ndo_set_vf_mac command then deny
  2184. * permission to the VF to add or delete unicast MAC addresses.
  2185. * Unless the VF is privileged and then it can do whatever.
  2186. * The VF may request to set the MAC address filter already
  2187. * assigned to it so do not return an error in that case.
  2188. */
  2189. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
  2190. !is_multicast_ether_addr(addr) && vf->pf_set_mac &&
  2191. !ether_addr_equal(addr, vf->default_lan_addr.addr)) {
  2192. dev_err(&pf->pdev->dev,
  2193. "VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
  2194. return -EPERM;
  2195. }
  2196. }
  2197. return 0;
  2198. }
  2199. /**
  2200. * i40e_vc_add_mac_addr_msg
  2201. * @vf: pointer to the VF info
  2202. * @msg: pointer to the msg buffer
  2203. *
  2204. * add guest mac address filter
  2205. **/
  2206. static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
  2207. {
  2208. struct virtchnl_ether_addr_list *al =
  2209. (struct virtchnl_ether_addr_list *)msg;
  2210. struct i40e_pf *pf = vf->pf;
  2211. struct i40e_vsi *vsi = NULL;
  2212. u16 vsi_id = al->vsi_id;
  2213. i40e_status ret = 0;
  2214. int i;
  2215. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2216. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2217. ret = I40E_ERR_PARAM;
  2218. goto error_param;
  2219. }
  2220. vsi = pf->vsi[vf->lan_vsi_idx];
  2221. /* Lock once, because all function inside for loop accesses VSI's
  2222. * MAC filter list which needs to be protected using same lock.
  2223. */
  2224. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2225. ret = i40e_check_vf_permission(vf, al);
  2226. if (ret) {
  2227. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2228. goto error_param;
  2229. }
  2230. /* add new addresses to the list */
  2231. for (i = 0; i < al->num_elements; i++) {
  2232. struct i40e_mac_filter *f;
  2233. f = i40e_find_mac(vsi, al->list[i].addr);
  2234. if (!f) {
  2235. f = i40e_add_mac_filter(vsi, al->list[i].addr);
  2236. if (!f) {
  2237. dev_err(&pf->pdev->dev,
  2238. "Unable to add MAC filter %pM for VF %d\n",
  2239. al->list[i].addr, vf->vf_id);
  2240. ret = I40E_ERR_PARAM;
  2241. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2242. goto error_param;
  2243. } else {
  2244. vf->num_mac++;
  2245. }
  2246. }
  2247. }
  2248. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2249. /* program the updated filter list */
  2250. ret = i40e_sync_vsi_filters(vsi);
  2251. if (ret)
  2252. dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
  2253. vf->vf_id, ret);
  2254. error_param:
  2255. /* send the response to the VF */
  2256. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
  2257. ret);
  2258. }
  2259. /**
  2260. * i40e_vc_del_mac_addr_msg
  2261. * @vf: pointer to the VF info
  2262. * @msg: pointer to the msg buffer
  2263. *
  2264. * remove guest mac address filter
  2265. **/
  2266. static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
  2267. {
  2268. struct virtchnl_ether_addr_list *al =
  2269. (struct virtchnl_ether_addr_list *)msg;
  2270. struct i40e_pf *pf = vf->pf;
  2271. struct i40e_vsi *vsi = NULL;
  2272. u16 vsi_id = al->vsi_id;
  2273. i40e_status ret = 0;
  2274. int i;
  2275. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2276. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2277. ret = I40E_ERR_PARAM;
  2278. goto error_param;
  2279. }
  2280. for (i = 0; i < al->num_elements; i++) {
  2281. if (is_broadcast_ether_addr(al->list[i].addr) ||
  2282. is_zero_ether_addr(al->list[i].addr)) {
  2283. dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
  2284. al->list[i].addr, vf->vf_id);
  2285. ret = I40E_ERR_INVALID_MAC_ADDR;
  2286. goto error_param;
  2287. }
  2288. if (vf->pf_set_mac &&
  2289. ether_addr_equal(al->list[i].addr,
  2290. vf->default_lan_addr.addr)) {
  2291. dev_err(&pf->pdev->dev,
  2292. "MAC addr %pM has been set by PF, cannot delete it for VF %d, reset VF to change MAC addr\n",
  2293. vf->default_lan_addr.addr, vf->vf_id);
  2294. ret = I40E_ERR_PARAM;
  2295. goto error_param;
  2296. }
  2297. }
  2298. vsi = pf->vsi[vf->lan_vsi_idx];
  2299. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2300. /* delete addresses from the list */
  2301. for (i = 0; i < al->num_elements; i++)
  2302. if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
  2303. ret = I40E_ERR_INVALID_MAC_ADDR;
  2304. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2305. goto error_param;
  2306. } else {
  2307. vf->num_mac--;
  2308. }
  2309. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2310. /* program the updated filter list */
  2311. ret = i40e_sync_vsi_filters(vsi);
  2312. if (ret)
  2313. dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
  2314. vf->vf_id, ret);
  2315. error_param:
  2316. /* send the response to the VF */
  2317. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
  2318. ret);
  2319. }
  2320. /**
  2321. * i40e_vc_add_vlan_msg
  2322. * @vf: pointer to the VF info
  2323. * @msg: pointer to the msg buffer
  2324. *
  2325. * program guest vlan id
  2326. **/
  2327. static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg)
  2328. {
  2329. struct virtchnl_vlan_filter_list *vfl =
  2330. (struct virtchnl_vlan_filter_list *)msg;
  2331. struct i40e_pf *pf = vf->pf;
  2332. struct i40e_vsi *vsi = NULL;
  2333. u16 vsi_id = vfl->vsi_id;
  2334. i40e_status aq_ret = 0;
  2335. int i;
  2336. if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
  2337. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  2338. dev_err(&pf->pdev->dev,
  2339. "VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
  2340. goto error_param;
  2341. }
  2342. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2343. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2344. aq_ret = I40E_ERR_PARAM;
  2345. goto error_param;
  2346. }
  2347. for (i = 0; i < vfl->num_elements; i++) {
  2348. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  2349. aq_ret = I40E_ERR_PARAM;
  2350. dev_err(&pf->pdev->dev,
  2351. "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
  2352. goto error_param;
  2353. }
  2354. }
  2355. vsi = pf->vsi[vf->lan_vsi_idx];
  2356. if (vsi->info.pvid) {
  2357. aq_ret = I40E_ERR_PARAM;
  2358. goto error_param;
  2359. }
  2360. i40e_vlan_stripping_enable(vsi);
  2361. for (i = 0; i < vfl->num_elements; i++) {
  2362. /* add new VLAN filter */
  2363. int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
  2364. if (!ret)
  2365. vf->num_vlan++;
  2366. if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
  2367. i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
  2368. true,
  2369. vfl->vlan_id[i],
  2370. NULL);
  2371. if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
  2372. i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
  2373. true,
  2374. vfl->vlan_id[i],
  2375. NULL);
  2376. if (ret)
  2377. dev_err(&pf->pdev->dev,
  2378. "Unable to add VLAN filter %d for VF %d, error %d\n",
  2379. vfl->vlan_id[i], vf->vf_id, ret);
  2380. }
  2381. error_param:
  2382. /* send the response to the VF */
  2383. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
  2384. }
  2385. /**
  2386. * i40e_vc_remove_vlan_msg
  2387. * @vf: pointer to the VF info
  2388. * @msg: pointer to the msg buffer
  2389. *
  2390. * remove programmed guest vlan id
  2391. **/
  2392. static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg)
  2393. {
  2394. struct virtchnl_vlan_filter_list *vfl =
  2395. (struct virtchnl_vlan_filter_list *)msg;
  2396. struct i40e_pf *pf = vf->pf;
  2397. struct i40e_vsi *vsi = NULL;
  2398. u16 vsi_id = vfl->vsi_id;
  2399. i40e_status aq_ret = 0;
  2400. int i;
  2401. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2402. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2403. aq_ret = I40E_ERR_PARAM;
  2404. goto error_param;
  2405. }
  2406. for (i = 0; i < vfl->num_elements; i++) {
  2407. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  2408. aq_ret = I40E_ERR_PARAM;
  2409. goto error_param;
  2410. }
  2411. }
  2412. vsi = pf->vsi[vf->lan_vsi_idx];
  2413. if (vsi->info.pvid) {
  2414. aq_ret = I40E_ERR_PARAM;
  2415. goto error_param;
  2416. }
  2417. for (i = 0; i < vfl->num_elements; i++) {
  2418. i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
  2419. vf->num_vlan--;
  2420. if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
  2421. i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
  2422. false,
  2423. vfl->vlan_id[i],
  2424. NULL);
  2425. if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
  2426. i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
  2427. false,
  2428. vfl->vlan_id[i],
  2429. NULL);
  2430. }
  2431. error_param:
  2432. /* send the response to the VF */
  2433. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
  2434. }
  2435. /**
  2436. * i40e_vc_iwarp_msg
  2437. * @vf: pointer to the VF info
  2438. * @msg: pointer to the msg buffer
  2439. * @msglen: msg length
  2440. *
  2441. * called from the VF for the iwarp msgs
  2442. **/
  2443. static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2444. {
  2445. struct i40e_pf *pf = vf->pf;
  2446. int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
  2447. i40e_status aq_ret = 0;
  2448. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2449. !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
  2450. aq_ret = I40E_ERR_PARAM;
  2451. goto error_param;
  2452. }
  2453. i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
  2454. msg, msglen);
  2455. error_param:
  2456. /* send the response to the VF */
  2457. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
  2458. aq_ret);
  2459. }
  2460. /**
  2461. * i40e_vc_iwarp_qvmap_msg
  2462. * @vf: pointer to the VF info
  2463. * @msg: pointer to the msg buffer
  2464. * @config: config qvmap or release it
  2465. *
  2466. * called from the VF for the iwarp msgs
  2467. **/
  2468. static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, bool config)
  2469. {
  2470. struct virtchnl_iwarp_qvlist_info *qvlist_info =
  2471. (struct virtchnl_iwarp_qvlist_info *)msg;
  2472. i40e_status aq_ret = 0;
  2473. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2474. !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
  2475. aq_ret = I40E_ERR_PARAM;
  2476. goto error_param;
  2477. }
  2478. if (config) {
  2479. if (i40e_config_iwarp_qvlist(vf, qvlist_info))
  2480. aq_ret = I40E_ERR_PARAM;
  2481. } else {
  2482. i40e_release_iwarp_qvlist(vf);
  2483. }
  2484. error_param:
  2485. /* send the response to the VF */
  2486. return i40e_vc_send_resp_to_vf(vf,
  2487. config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
  2488. VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
  2489. aq_ret);
  2490. }
  2491. /**
  2492. * i40e_vc_config_rss_key
  2493. * @vf: pointer to the VF info
  2494. * @msg: pointer to the msg buffer
  2495. *
  2496. * Configure the VF's RSS key
  2497. **/
  2498. static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg)
  2499. {
  2500. struct virtchnl_rss_key *vrk =
  2501. (struct virtchnl_rss_key *)msg;
  2502. struct i40e_pf *pf = vf->pf;
  2503. struct i40e_vsi *vsi = NULL;
  2504. u16 vsi_id = vrk->vsi_id;
  2505. i40e_status aq_ret = 0;
  2506. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2507. !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
  2508. (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
  2509. aq_ret = I40E_ERR_PARAM;
  2510. goto err;
  2511. }
  2512. vsi = pf->vsi[vf->lan_vsi_idx];
  2513. aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
  2514. err:
  2515. /* send the response to the VF */
  2516. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
  2517. aq_ret);
  2518. }
  2519. /**
  2520. * i40e_vc_config_rss_lut
  2521. * @vf: pointer to the VF info
  2522. * @msg: pointer to the msg buffer
  2523. *
  2524. * Configure the VF's RSS LUT
  2525. **/
  2526. static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg)
  2527. {
  2528. struct virtchnl_rss_lut *vrl =
  2529. (struct virtchnl_rss_lut *)msg;
  2530. struct i40e_pf *pf = vf->pf;
  2531. struct i40e_vsi *vsi = NULL;
  2532. u16 vsi_id = vrl->vsi_id;
  2533. i40e_status aq_ret = 0;
  2534. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2535. !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
  2536. (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
  2537. aq_ret = I40E_ERR_PARAM;
  2538. goto err;
  2539. }
  2540. vsi = pf->vsi[vf->lan_vsi_idx];
  2541. aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
  2542. /* send the response to the VF */
  2543. err:
  2544. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
  2545. aq_ret);
  2546. }
  2547. /**
  2548. * i40e_vc_get_rss_hena
  2549. * @vf: pointer to the VF info
  2550. * @msg: pointer to the msg buffer
  2551. *
  2552. * Return the RSS HENA bits allowed by the hardware
  2553. **/
  2554. static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg)
  2555. {
  2556. struct virtchnl_rss_hena *vrh = NULL;
  2557. struct i40e_pf *pf = vf->pf;
  2558. i40e_status aq_ret = 0;
  2559. int len = 0;
  2560. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2561. aq_ret = I40E_ERR_PARAM;
  2562. goto err;
  2563. }
  2564. len = sizeof(struct virtchnl_rss_hena);
  2565. vrh = kzalloc(len, GFP_KERNEL);
  2566. if (!vrh) {
  2567. aq_ret = I40E_ERR_NO_MEMORY;
  2568. len = 0;
  2569. goto err;
  2570. }
  2571. vrh->hena = i40e_pf_get_default_rss_hena(pf);
  2572. err:
  2573. /* send the response back to the VF */
  2574. aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
  2575. aq_ret, (u8 *)vrh, len);
  2576. kfree(vrh);
  2577. return aq_ret;
  2578. }
  2579. /**
  2580. * i40e_vc_set_rss_hena
  2581. * @vf: pointer to the VF info
  2582. * @msg: pointer to the msg buffer
  2583. *
  2584. * Set the RSS HENA bits for the VF
  2585. **/
  2586. static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg)
  2587. {
  2588. struct virtchnl_rss_hena *vrh =
  2589. (struct virtchnl_rss_hena *)msg;
  2590. struct i40e_pf *pf = vf->pf;
  2591. struct i40e_hw *hw = &pf->hw;
  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_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
  2598. i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
  2599. (u32)(vrh->hena >> 32));
  2600. /* send the response to the VF */
  2601. err:
  2602. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
  2603. }
  2604. /**
  2605. * i40e_vc_enable_vlan_stripping
  2606. * @vf: pointer to the VF info
  2607. * @msg: pointer to the msg buffer
  2608. *
  2609. * Enable vlan header stripping for the VF
  2610. **/
  2611. static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
  2612. {
  2613. struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
  2614. i40e_status aq_ret = 0;
  2615. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2616. aq_ret = I40E_ERR_PARAM;
  2617. goto err;
  2618. }
  2619. i40e_vlan_stripping_enable(vsi);
  2620. /* send the response to the VF */
  2621. err:
  2622. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
  2623. aq_ret);
  2624. }
  2625. /**
  2626. * i40e_vc_disable_vlan_stripping
  2627. * @vf: pointer to the VF info
  2628. * @msg: pointer to the msg buffer
  2629. *
  2630. * Disable vlan header stripping for the VF
  2631. **/
  2632. static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
  2633. {
  2634. struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
  2635. i40e_status aq_ret = 0;
  2636. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2637. aq_ret = I40E_ERR_PARAM;
  2638. goto err;
  2639. }
  2640. i40e_vlan_stripping_disable(vsi);
  2641. /* send the response to the VF */
  2642. err:
  2643. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
  2644. aq_ret);
  2645. }
  2646. /**
  2647. * i40e_validate_cloud_filter
  2648. * @mask: mask for TC filter
  2649. * @data: data for TC filter
  2650. *
  2651. * This function validates cloud filter programmed as TC filter for ADq
  2652. **/
  2653. static int i40e_validate_cloud_filter(struct i40e_vf *vf,
  2654. struct virtchnl_filter *tc_filter)
  2655. {
  2656. struct virtchnl_l4_spec mask = tc_filter->mask.tcp_spec;
  2657. struct virtchnl_l4_spec data = tc_filter->data.tcp_spec;
  2658. struct i40e_pf *pf = vf->pf;
  2659. struct i40e_vsi *vsi = NULL;
  2660. struct i40e_mac_filter *f;
  2661. struct hlist_node *h;
  2662. bool found = false;
  2663. int bkt;
  2664. if (!tc_filter->action) {
  2665. dev_info(&pf->pdev->dev,
  2666. "VF %d: Currently ADq doesn't support Drop Action\n",
  2667. vf->vf_id);
  2668. goto err;
  2669. }
  2670. /* action_meta is TC number here to which the filter is applied */
  2671. if (!tc_filter->action_meta ||
  2672. tc_filter->action_meta > I40E_MAX_VF_VSI) {
  2673. dev_info(&pf->pdev->dev, "VF %d: Invalid TC number %u\n",
  2674. vf->vf_id, tc_filter->action_meta);
  2675. goto err;
  2676. }
  2677. /* Check filter if it's programmed for advanced mode or basic mode.
  2678. * There are two ADq modes (for VF only),
  2679. * 1. Basic mode: intended to allow as many filter options as possible
  2680. * to be added to a VF in Non-trusted mode. Main goal is
  2681. * to add filters to its own MAC and VLAN id.
  2682. * 2. Advanced mode: is for allowing filters to be applied other than
  2683. * its own MAC or VLAN. This mode requires the VF to be
  2684. * Trusted.
  2685. */
  2686. if (mask.dst_mac[0] && !mask.dst_ip[0]) {
  2687. vsi = pf->vsi[vf->lan_vsi_idx];
  2688. f = i40e_find_mac(vsi, data.dst_mac);
  2689. if (!f) {
  2690. dev_info(&pf->pdev->dev,
  2691. "Destination MAC %pM doesn't belong to VF %d\n",
  2692. data.dst_mac, vf->vf_id);
  2693. goto err;
  2694. }
  2695. if (mask.vlan_id) {
  2696. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
  2697. hlist) {
  2698. if (f->vlan == ntohs(data.vlan_id)) {
  2699. found = true;
  2700. break;
  2701. }
  2702. }
  2703. if (!found) {
  2704. dev_info(&pf->pdev->dev,
  2705. "VF %d doesn't have any VLAN id %u\n",
  2706. vf->vf_id, ntohs(data.vlan_id));
  2707. goto err;
  2708. }
  2709. }
  2710. } else {
  2711. /* Check if VF is trusted */
  2712. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  2713. dev_err(&pf->pdev->dev,
  2714. "VF %d not trusted, make VF trusted to add advanced mode ADq cloud filters\n",
  2715. vf->vf_id);
  2716. return I40E_ERR_CONFIG;
  2717. }
  2718. }
  2719. if (mask.dst_mac[0] & data.dst_mac[0]) {
  2720. if (is_broadcast_ether_addr(data.dst_mac) ||
  2721. is_zero_ether_addr(data.dst_mac)) {
  2722. dev_info(&pf->pdev->dev, "VF %d: Invalid Dest MAC addr %pM\n",
  2723. vf->vf_id, data.dst_mac);
  2724. goto err;
  2725. }
  2726. }
  2727. if (mask.src_mac[0] & data.src_mac[0]) {
  2728. if (is_broadcast_ether_addr(data.src_mac) ||
  2729. is_zero_ether_addr(data.src_mac)) {
  2730. dev_info(&pf->pdev->dev, "VF %d: Invalid Source MAC addr %pM\n",
  2731. vf->vf_id, data.src_mac);
  2732. goto err;
  2733. }
  2734. }
  2735. if (mask.dst_port & data.dst_port) {
  2736. if (!data.dst_port || be16_to_cpu(data.dst_port) > 0xFFFF) {
  2737. dev_info(&pf->pdev->dev, "VF %d: Invalid Dest port\n",
  2738. vf->vf_id);
  2739. goto err;
  2740. }
  2741. }
  2742. if (mask.src_port & data.src_port) {
  2743. if (!data.src_port || be16_to_cpu(data.src_port) > 0xFFFF) {
  2744. dev_info(&pf->pdev->dev, "VF %d: Invalid Source port\n",
  2745. vf->vf_id);
  2746. goto err;
  2747. }
  2748. }
  2749. if (tc_filter->flow_type != VIRTCHNL_TCP_V6_FLOW &&
  2750. tc_filter->flow_type != VIRTCHNL_TCP_V4_FLOW) {
  2751. dev_info(&pf->pdev->dev, "VF %d: Invalid Flow type\n",
  2752. vf->vf_id);
  2753. goto err;
  2754. }
  2755. if (mask.vlan_id & data.vlan_id) {
  2756. if (ntohs(data.vlan_id) > I40E_MAX_VLANID) {
  2757. dev_info(&pf->pdev->dev, "VF %d: invalid VLAN ID\n",
  2758. vf->vf_id);
  2759. goto err;
  2760. }
  2761. }
  2762. return I40E_SUCCESS;
  2763. err:
  2764. return I40E_ERR_CONFIG;
  2765. }
  2766. /**
  2767. * i40e_find_vsi_from_seid - searches for the vsi with the given seid
  2768. * @vf: pointer to the VF info
  2769. * @seid - seid of the vsi it is searching for
  2770. **/
  2771. static struct i40e_vsi *i40e_find_vsi_from_seid(struct i40e_vf *vf, u16 seid)
  2772. {
  2773. struct i40e_pf *pf = vf->pf;
  2774. struct i40e_vsi *vsi = NULL;
  2775. int i;
  2776. for (i = 0; i < vf->num_tc ; i++) {
  2777. vsi = i40e_find_vsi_from_id(pf, vf->ch[i].vsi_id);
  2778. if (vsi && vsi->seid == seid)
  2779. return vsi;
  2780. }
  2781. return NULL;
  2782. }
  2783. /**
  2784. * i40e_del_all_cloud_filters
  2785. * @vf: pointer to the VF info
  2786. *
  2787. * This function deletes all cloud filters
  2788. **/
  2789. static void i40e_del_all_cloud_filters(struct i40e_vf *vf)
  2790. {
  2791. struct i40e_cloud_filter *cfilter = NULL;
  2792. struct i40e_pf *pf = vf->pf;
  2793. struct i40e_vsi *vsi = NULL;
  2794. struct hlist_node *node;
  2795. int ret;
  2796. hlist_for_each_entry_safe(cfilter, node,
  2797. &vf->cloud_filter_list, cloud_node) {
  2798. vsi = i40e_find_vsi_from_seid(vf, cfilter->seid);
  2799. if (!vsi) {
  2800. dev_err(&pf->pdev->dev, "VF %d: no VSI found for matching %u seid, can't delete cloud filter\n",
  2801. vf->vf_id, cfilter->seid);
  2802. continue;
  2803. }
  2804. if (cfilter->dst_port)
  2805. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
  2806. false);
  2807. else
  2808. ret = i40e_add_del_cloud_filter(vsi, cfilter, false);
  2809. if (ret)
  2810. dev_err(&pf->pdev->dev,
  2811. "VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
  2812. vf->vf_id, i40e_stat_str(&pf->hw, ret),
  2813. i40e_aq_str(&pf->hw,
  2814. pf->hw.aq.asq_last_status));
  2815. hlist_del(&cfilter->cloud_node);
  2816. kfree(cfilter);
  2817. vf->num_cloud_filters--;
  2818. }
  2819. }
  2820. /**
  2821. * i40e_vc_del_cloud_filter
  2822. * @vf: pointer to the VF info
  2823. * @msg: pointer to the msg buffer
  2824. *
  2825. * This function deletes a cloud filter programmed as TC filter for ADq
  2826. **/
  2827. static int i40e_vc_del_cloud_filter(struct i40e_vf *vf, u8 *msg)
  2828. {
  2829. struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
  2830. struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
  2831. struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
  2832. struct i40e_cloud_filter cfilter, *cf = NULL;
  2833. struct i40e_pf *pf = vf->pf;
  2834. struct i40e_vsi *vsi = NULL;
  2835. struct hlist_node *node;
  2836. i40e_status aq_ret = 0;
  2837. int i, ret;
  2838. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2839. aq_ret = I40E_ERR_PARAM;
  2840. goto err;
  2841. }
  2842. if (!vf->adq_enabled) {
  2843. dev_info(&pf->pdev->dev,
  2844. "VF %d: ADq not enabled, can't apply cloud filter\n",
  2845. vf->vf_id);
  2846. aq_ret = I40E_ERR_PARAM;
  2847. goto err;
  2848. }
  2849. if (i40e_validate_cloud_filter(vf, vcf)) {
  2850. dev_info(&pf->pdev->dev,
  2851. "VF %d: Invalid input, can't apply cloud filter\n",
  2852. vf->vf_id);
  2853. aq_ret = I40E_ERR_PARAM;
  2854. goto err;
  2855. }
  2856. memset(&cfilter, 0, sizeof(cfilter));
  2857. /* parse destination mac address */
  2858. for (i = 0; i < ETH_ALEN; i++)
  2859. cfilter.dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
  2860. /* parse source mac address */
  2861. for (i = 0; i < ETH_ALEN; i++)
  2862. cfilter.src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
  2863. cfilter.vlan_id = mask.vlan_id & tcf.vlan_id;
  2864. cfilter.dst_port = mask.dst_port & tcf.dst_port;
  2865. cfilter.src_port = mask.src_port & tcf.src_port;
  2866. switch (vcf->flow_type) {
  2867. case VIRTCHNL_TCP_V4_FLOW:
  2868. cfilter.n_proto = ETH_P_IP;
  2869. if (mask.dst_ip[0] & tcf.dst_ip[0])
  2870. memcpy(&cfilter.ip.v4.dst_ip, tcf.dst_ip,
  2871. ARRAY_SIZE(tcf.dst_ip));
  2872. else if (mask.src_ip[0] & tcf.dst_ip[0])
  2873. memcpy(&cfilter.ip.v4.src_ip, tcf.src_ip,
  2874. ARRAY_SIZE(tcf.dst_ip));
  2875. break;
  2876. case VIRTCHNL_TCP_V6_FLOW:
  2877. cfilter.n_proto = ETH_P_IPV6;
  2878. if (mask.dst_ip[3] & tcf.dst_ip[3])
  2879. memcpy(&cfilter.ip.v6.dst_ip6, tcf.dst_ip,
  2880. sizeof(cfilter.ip.v6.dst_ip6));
  2881. if (mask.src_ip[3] & tcf.src_ip[3])
  2882. memcpy(&cfilter.ip.v6.src_ip6, tcf.src_ip,
  2883. sizeof(cfilter.ip.v6.src_ip6));
  2884. break;
  2885. default:
  2886. /* TC filter can be configured based on different combinations
  2887. * and in this case IP is not a part of filter config
  2888. */
  2889. dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
  2890. vf->vf_id);
  2891. }
  2892. /* get the vsi to which the tc belongs to */
  2893. vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
  2894. cfilter.seid = vsi->seid;
  2895. cfilter.flags = vcf->field_flags;
  2896. /* Deleting TC filter */
  2897. if (tcf.dst_port)
  2898. ret = i40e_add_del_cloud_filter_big_buf(vsi, &cfilter, false);
  2899. else
  2900. ret = i40e_add_del_cloud_filter(vsi, &cfilter, false);
  2901. if (ret) {
  2902. dev_err(&pf->pdev->dev,
  2903. "VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
  2904. vf->vf_id, i40e_stat_str(&pf->hw, ret),
  2905. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  2906. goto err;
  2907. }
  2908. hlist_for_each_entry_safe(cf, node,
  2909. &vf->cloud_filter_list, cloud_node) {
  2910. if (cf->seid != cfilter.seid)
  2911. continue;
  2912. if (mask.dst_port)
  2913. if (cfilter.dst_port != cf->dst_port)
  2914. continue;
  2915. if (mask.dst_mac[0])
  2916. if (!ether_addr_equal(cf->src_mac, cfilter.src_mac))
  2917. continue;
  2918. /* for ipv4 data to be valid, only first byte of mask is set */
  2919. if (cfilter.n_proto == ETH_P_IP && mask.dst_ip[0])
  2920. if (memcmp(&cfilter.ip.v4.dst_ip, &cf->ip.v4.dst_ip,
  2921. ARRAY_SIZE(tcf.dst_ip)))
  2922. continue;
  2923. /* for ipv6, mask is set for all sixteen bytes (4 words) */
  2924. if (cfilter.n_proto == ETH_P_IPV6 && mask.dst_ip[3])
  2925. if (memcmp(&cfilter.ip.v6.dst_ip6, &cf->ip.v6.dst_ip6,
  2926. sizeof(cfilter.ip.v6.src_ip6)))
  2927. continue;
  2928. if (mask.vlan_id)
  2929. if (cfilter.vlan_id != cf->vlan_id)
  2930. continue;
  2931. hlist_del(&cf->cloud_node);
  2932. kfree(cf);
  2933. vf->num_cloud_filters--;
  2934. }
  2935. err:
  2936. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_CLOUD_FILTER,
  2937. aq_ret);
  2938. }
  2939. /**
  2940. * i40e_vc_add_cloud_filter
  2941. * @vf: pointer to the VF info
  2942. * @msg: pointer to the msg buffer
  2943. *
  2944. * This function adds a cloud filter programmed as TC filter for ADq
  2945. **/
  2946. static int i40e_vc_add_cloud_filter(struct i40e_vf *vf, u8 *msg)
  2947. {
  2948. struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
  2949. struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
  2950. struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
  2951. struct i40e_cloud_filter *cfilter = NULL;
  2952. struct i40e_pf *pf = vf->pf;
  2953. struct i40e_vsi *vsi = NULL;
  2954. i40e_status aq_ret = 0;
  2955. int i, ret;
  2956. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2957. aq_ret = I40E_ERR_PARAM;
  2958. goto err;
  2959. }
  2960. if (!vf->adq_enabled) {
  2961. dev_info(&pf->pdev->dev,
  2962. "VF %d: ADq is not enabled, can't apply cloud filter\n",
  2963. vf->vf_id);
  2964. aq_ret = I40E_ERR_PARAM;
  2965. goto err;
  2966. }
  2967. if (i40e_validate_cloud_filter(vf, vcf)) {
  2968. dev_info(&pf->pdev->dev,
  2969. "VF %d: Invalid input/s, can't apply cloud filter\n",
  2970. vf->vf_id);
  2971. aq_ret = I40E_ERR_PARAM;
  2972. goto err;
  2973. }
  2974. cfilter = kzalloc(sizeof(*cfilter), GFP_KERNEL);
  2975. if (!cfilter)
  2976. return -ENOMEM;
  2977. /* parse destination mac address */
  2978. for (i = 0; i < ETH_ALEN; i++)
  2979. cfilter->dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
  2980. /* parse source mac address */
  2981. for (i = 0; i < ETH_ALEN; i++)
  2982. cfilter->src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
  2983. cfilter->vlan_id = mask.vlan_id & tcf.vlan_id;
  2984. cfilter->dst_port = mask.dst_port & tcf.dst_port;
  2985. cfilter->src_port = mask.src_port & tcf.src_port;
  2986. switch (vcf->flow_type) {
  2987. case VIRTCHNL_TCP_V4_FLOW:
  2988. cfilter->n_proto = ETH_P_IP;
  2989. if (mask.dst_ip[0] & tcf.dst_ip[0])
  2990. memcpy(&cfilter->ip.v4.dst_ip, tcf.dst_ip,
  2991. ARRAY_SIZE(tcf.dst_ip));
  2992. else if (mask.src_ip[0] & tcf.dst_ip[0])
  2993. memcpy(&cfilter->ip.v4.src_ip, tcf.src_ip,
  2994. ARRAY_SIZE(tcf.dst_ip));
  2995. break;
  2996. case VIRTCHNL_TCP_V6_FLOW:
  2997. cfilter->n_proto = ETH_P_IPV6;
  2998. if (mask.dst_ip[3] & tcf.dst_ip[3])
  2999. memcpy(&cfilter->ip.v6.dst_ip6, tcf.dst_ip,
  3000. sizeof(cfilter->ip.v6.dst_ip6));
  3001. if (mask.src_ip[3] & tcf.src_ip[3])
  3002. memcpy(&cfilter->ip.v6.src_ip6, tcf.src_ip,
  3003. sizeof(cfilter->ip.v6.src_ip6));
  3004. break;
  3005. default:
  3006. /* TC filter can be configured based on different combinations
  3007. * and in this case IP is not a part of filter config
  3008. */
  3009. dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
  3010. vf->vf_id);
  3011. }
  3012. /* get the VSI to which the TC belongs to */
  3013. vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
  3014. cfilter->seid = vsi->seid;
  3015. cfilter->flags = vcf->field_flags;
  3016. /* Adding cloud filter programmed as TC filter */
  3017. if (tcf.dst_port)
  3018. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter, true);
  3019. else
  3020. ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
  3021. if (ret) {
  3022. dev_err(&pf->pdev->dev,
  3023. "VF %d: Failed to add cloud filter, err %s aq_err %s\n",
  3024. vf->vf_id, i40e_stat_str(&pf->hw, ret),
  3025. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  3026. goto err;
  3027. }
  3028. INIT_HLIST_NODE(&cfilter->cloud_node);
  3029. hlist_add_head(&cfilter->cloud_node, &vf->cloud_filter_list);
  3030. vf->num_cloud_filters++;
  3031. err:
  3032. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_CLOUD_FILTER,
  3033. aq_ret);
  3034. }
  3035. /**
  3036. * i40e_vc_add_qch_msg: Add queue channel and enable ADq
  3037. * @vf: pointer to the VF info
  3038. * @msg: pointer to the msg buffer
  3039. **/
  3040. static int i40e_vc_add_qch_msg(struct i40e_vf *vf, u8 *msg)
  3041. {
  3042. struct virtchnl_tc_info *tci =
  3043. (struct virtchnl_tc_info *)msg;
  3044. struct i40e_pf *pf = vf->pf;
  3045. struct i40e_link_status *ls = &pf->hw.phy.link_info;
  3046. int i, adq_request_qps = 0, speed = 0;
  3047. i40e_status aq_ret = 0;
  3048. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  3049. aq_ret = I40E_ERR_PARAM;
  3050. goto err;
  3051. }
  3052. /* ADq cannot be applied if spoof check is ON */
  3053. if (vf->spoofchk) {
  3054. dev_err(&pf->pdev->dev,
  3055. "Spoof check is ON, turn it OFF to enable ADq\n");
  3056. aq_ret = I40E_ERR_PARAM;
  3057. goto err;
  3058. }
  3059. if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)) {
  3060. dev_err(&pf->pdev->dev,
  3061. "VF %d attempting to enable ADq, but hasn't properly negotiated that capability\n",
  3062. vf->vf_id);
  3063. aq_ret = I40E_ERR_PARAM;
  3064. goto err;
  3065. }
  3066. /* max number of traffic classes for VF currently capped at 4 */
  3067. if (!tci->num_tc || tci->num_tc > I40E_MAX_VF_VSI) {
  3068. dev_err(&pf->pdev->dev,
  3069. "VF %d trying to set %u TCs, valid range 1-4 TCs per VF\n",
  3070. vf->vf_id, tci->num_tc);
  3071. aq_ret = I40E_ERR_PARAM;
  3072. goto err;
  3073. }
  3074. /* validate queues for each TC */
  3075. for (i = 0; i < tci->num_tc; i++)
  3076. if (!tci->list[i].count ||
  3077. tci->list[i].count > I40E_DEFAULT_QUEUES_PER_VF) {
  3078. dev_err(&pf->pdev->dev,
  3079. "VF %d: TC %d trying to set %u queues, valid range 1-4 queues per TC\n",
  3080. vf->vf_id, i, tci->list[i].count);
  3081. aq_ret = I40E_ERR_PARAM;
  3082. goto err;
  3083. }
  3084. /* need Max VF queues but already have default number of queues */
  3085. adq_request_qps = I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF;
  3086. if (pf->queues_left < adq_request_qps) {
  3087. dev_err(&pf->pdev->dev,
  3088. "No queues left to allocate to VF %d\n",
  3089. vf->vf_id);
  3090. aq_ret = I40E_ERR_PARAM;
  3091. goto err;
  3092. } else {
  3093. /* we need to allocate max VF queues to enable ADq so as to
  3094. * make sure ADq enabled VF always gets back queues when it
  3095. * goes through a reset.
  3096. */
  3097. vf->num_queue_pairs = I40E_MAX_VF_QUEUES;
  3098. }
  3099. /* get link speed in MB to validate rate limit */
  3100. switch (ls->link_speed) {
  3101. case VIRTCHNL_LINK_SPEED_100MB:
  3102. speed = SPEED_100;
  3103. break;
  3104. case VIRTCHNL_LINK_SPEED_1GB:
  3105. speed = SPEED_1000;
  3106. break;
  3107. case VIRTCHNL_LINK_SPEED_10GB:
  3108. speed = SPEED_10000;
  3109. break;
  3110. case VIRTCHNL_LINK_SPEED_20GB:
  3111. speed = SPEED_20000;
  3112. break;
  3113. case VIRTCHNL_LINK_SPEED_25GB:
  3114. speed = SPEED_25000;
  3115. break;
  3116. case VIRTCHNL_LINK_SPEED_40GB:
  3117. speed = SPEED_40000;
  3118. break;
  3119. default:
  3120. dev_err(&pf->pdev->dev,
  3121. "Cannot detect link speed\n");
  3122. aq_ret = I40E_ERR_PARAM;
  3123. goto err;
  3124. }
  3125. /* parse data from the queue channel info */
  3126. vf->num_tc = tci->num_tc;
  3127. for (i = 0; i < vf->num_tc; i++) {
  3128. if (tci->list[i].max_tx_rate) {
  3129. if (tci->list[i].max_tx_rate > speed) {
  3130. dev_err(&pf->pdev->dev,
  3131. "Invalid max tx rate %llu specified for VF %d.",
  3132. tci->list[i].max_tx_rate,
  3133. vf->vf_id);
  3134. aq_ret = I40E_ERR_PARAM;
  3135. goto err;
  3136. } else {
  3137. vf->ch[i].max_tx_rate =
  3138. tci->list[i].max_tx_rate;
  3139. }
  3140. }
  3141. vf->ch[i].num_qps = tci->list[i].count;
  3142. }
  3143. /* set this flag only after making sure all inputs are sane */
  3144. vf->adq_enabled = true;
  3145. /* num_req_queues is set when user changes number of queues via ethtool
  3146. * and this causes issue for default VSI(which depends on this variable)
  3147. * when ADq is enabled, hence reset it.
  3148. */
  3149. vf->num_req_queues = 0;
  3150. /* reset the VF in order to allocate resources */
  3151. i40e_vc_notify_vf_reset(vf);
  3152. i40e_reset_vf(vf, false);
  3153. return I40E_SUCCESS;
  3154. /* send the response to the VF */
  3155. err:
  3156. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_CHANNELS,
  3157. aq_ret);
  3158. }
  3159. /**
  3160. * i40e_vc_del_qch_msg
  3161. * @vf: pointer to the VF info
  3162. * @msg: pointer to the msg buffer
  3163. **/
  3164. static int i40e_vc_del_qch_msg(struct i40e_vf *vf, u8 *msg)
  3165. {
  3166. struct i40e_pf *pf = vf->pf;
  3167. i40e_status aq_ret = 0;
  3168. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  3169. aq_ret = I40E_ERR_PARAM;
  3170. goto err;
  3171. }
  3172. if (vf->adq_enabled) {
  3173. i40e_del_all_cloud_filters(vf);
  3174. i40e_del_qch(vf);
  3175. vf->adq_enabled = false;
  3176. vf->num_tc = 0;
  3177. dev_info(&pf->pdev->dev,
  3178. "Deleting Queue Channels and cloud filters for ADq on VF %d\n",
  3179. vf->vf_id);
  3180. } else {
  3181. dev_info(&pf->pdev->dev, "VF %d trying to delete queue channels but ADq isn't enabled\n",
  3182. vf->vf_id);
  3183. aq_ret = I40E_ERR_PARAM;
  3184. }
  3185. /* reset the VF in order to allocate resources */
  3186. i40e_vc_notify_vf_reset(vf);
  3187. i40e_reset_vf(vf, false);
  3188. return I40E_SUCCESS;
  3189. err:
  3190. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_CHANNELS,
  3191. aq_ret);
  3192. }
  3193. /**
  3194. * i40e_vc_process_vf_msg
  3195. * @pf: pointer to the PF structure
  3196. * @vf_id: source VF id
  3197. * @v_opcode: operation code
  3198. * @v_retval: unused return value code
  3199. * @msg: pointer to the msg buffer
  3200. * @msglen: msg length
  3201. *
  3202. * called from the common aeq/arq handler to
  3203. * process request from VF
  3204. **/
  3205. int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
  3206. u32 __always_unused v_retval, u8 *msg, u16 msglen)
  3207. {
  3208. struct i40e_hw *hw = &pf->hw;
  3209. int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
  3210. struct i40e_vf *vf;
  3211. int ret;
  3212. pf->vf_aq_requests++;
  3213. if (local_vf_id >= pf->num_alloc_vfs)
  3214. return -EINVAL;
  3215. vf = &(pf->vf[local_vf_id]);
  3216. /* Check if VF is disabled. */
  3217. if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
  3218. return I40E_ERR_PARAM;
  3219. /* perform basic checks on the msg */
  3220. ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
  3221. /* perform additional checks specific to this driver */
  3222. if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
  3223. struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
  3224. if (vrk->key_len != I40E_HKEY_ARRAY_SIZE)
  3225. ret = -EINVAL;
  3226. } else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
  3227. struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
  3228. if (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)
  3229. ret = -EINVAL;
  3230. }
  3231. if (ret) {
  3232. i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
  3233. dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
  3234. local_vf_id, v_opcode, msglen);
  3235. switch (ret) {
  3236. case VIRTCHNL_ERR_PARAM:
  3237. return -EPERM;
  3238. default:
  3239. return -EINVAL;
  3240. }
  3241. }
  3242. switch (v_opcode) {
  3243. case VIRTCHNL_OP_VERSION:
  3244. ret = i40e_vc_get_version_msg(vf, msg);
  3245. break;
  3246. case VIRTCHNL_OP_GET_VF_RESOURCES:
  3247. ret = i40e_vc_get_vf_resources_msg(vf, msg);
  3248. i40e_vc_notify_vf_link_state(vf);
  3249. break;
  3250. case VIRTCHNL_OP_RESET_VF:
  3251. i40e_vc_reset_vf_msg(vf);
  3252. ret = 0;
  3253. break;
  3254. case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
  3255. ret = i40e_vc_config_promiscuous_mode_msg(vf, msg);
  3256. break;
  3257. case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  3258. ret = i40e_vc_config_queues_msg(vf, msg);
  3259. break;
  3260. case VIRTCHNL_OP_CONFIG_IRQ_MAP:
  3261. ret = i40e_vc_config_irq_map_msg(vf, msg);
  3262. break;
  3263. case VIRTCHNL_OP_ENABLE_QUEUES:
  3264. ret = i40e_vc_enable_queues_msg(vf, msg);
  3265. i40e_vc_notify_vf_link_state(vf);
  3266. break;
  3267. case VIRTCHNL_OP_DISABLE_QUEUES:
  3268. ret = i40e_vc_disable_queues_msg(vf, msg);
  3269. break;
  3270. case VIRTCHNL_OP_ADD_ETH_ADDR:
  3271. ret = i40e_vc_add_mac_addr_msg(vf, msg);
  3272. break;
  3273. case VIRTCHNL_OP_DEL_ETH_ADDR:
  3274. ret = i40e_vc_del_mac_addr_msg(vf, msg);
  3275. break;
  3276. case VIRTCHNL_OP_ADD_VLAN:
  3277. ret = i40e_vc_add_vlan_msg(vf, msg);
  3278. break;
  3279. case VIRTCHNL_OP_DEL_VLAN:
  3280. ret = i40e_vc_remove_vlan_msg(vf, msg);
  3281. break;
  3282. case VIRTCHNL_OP_GET_STATS:
  3283. ret = i40e_vc_get_stats_msg(vf, msg);
  3284. break;
  3285. case VIRTCHNL_OP_IWARP:
  3286. ret = i40e_vc_iwarp_msg(vf, msg, msglen);
  3287. break;
  3288. case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
  3289. ret = i40e_vc_iwarp_qvmap_msg(vf, msg, true);
  3290. break;
  3291. case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
  3292. ret = i40e_vc_iwarp_qvmap_msg(vf, msg, false);
  3293. break;
  3294. case VIRTCHNL_OP_CONFIG_RSS_KEY:
  3295. ret = i40e_vc_config_rss_key(vf, msg);
  3296. break;
  3297. case VIRTCHNL_OP_CONFIG_RSS_LUT:
  3298. ret = i40e_vc_config_rss_lut(vf, msg);
  3299. break;
  3300. case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
  3301. ret = i40e_vc_get_rss_hena(vf, msg);
  3302. break;
  3303. case VIRTCHNL_OP_SET_RSS_HENA:
  3304. ret = i40e_vc_set_rss_hena(vf, msg);
  3305. break;
  3306. case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
  3307. ret = i40e_vc_enable_vlan_stripping(vf, msg);
  3308. break;
  3309. case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
  3310. ret = i40e_vc_disable_vlan_stripping(vf, msg);
  3311. break;
  3312. case VIRTCHNL_OP_REQUEST_QUEUES:
  3313. ret = i40e_vc_request_queues_msg(vf, msg);
  3314. break;
  3315. case VIRTCHNL_OP_ENABLE_CHANNELS:
  3316. ret = i40e_vc_add_qch_msg(vf, msg);
  3317. break;
  3318. case VIRTCHNL_OP_DISABLE_CHANNELS:
  3319. ret = i40e_vc_del_qch_msg(vf, msg);
  3320. break;
  3321. case VIRTCHNL_OP_ADD_CLOUD_FILTER:
  3322. ret = i40e_vc_add_cloud_filter(vf, msg);
  3323. break;
  3324. case VIRTCHNL_OP_DEL_CLOUD_FILTER:
  3325. ret = i40e_vc_del_cloud_filter(vf, msg);
  3326. break;
  3327. case VIRTCHNL_OP_UNKNOWN:
  3328. default:
  3329. dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
  3330. v_opcode, local_vf_id);
  3331. ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
  3332. I40E_ERR_NOT_IMPLEMENTED);
  3333. break;
  3334. }
  3335. return ret;
  3336. }
  3337. /**
  3338. * i40e_vc_process_vflr_event
  3339. * @pf: pointer to the PF structure
  3340. *
  3341. * called from the vlfr irq handler to
  3342. * free up VF resources and state variables
  3343. **/
  3344. int i40e_vc_process_vflr_event(struct i40e_pf *pf)
  3345. {
  3346. struct i40e_hw *hw = &pf->hw;
  3347. u32 reg, reg_idx, bit_idx;
  3348. struct i40e_vf *vf;
  3349. int vf_id;
  3350. if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  3351. return 0;
  3352. /* Re-enable the VFLR interrupt cause here, before looking for which
  3353. * VF got reset. Otherwise, if another VF gets a reset while the
  3354. * first one is being processed, that interrupt will be lost, and
  3355. * that VF will be stuck in reset forever.
  3356. */
  3357. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  3358. reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3359. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  3360. i40e_flush(hw);
  3361. clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3362. for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
  3363. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  3364. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  3365. /* read GLGEN_VFLRSTAT register to find out the flr VFs */
  3366. vf = &pf->vf[vf_id];
  3367. reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
  3368. if (reg & BIT(bit_idx))
  3369. /* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
  3370. i40e_reset_vf(vf, true);
  3371. }
  3372. return 0;
  3373. }
  3374. /**
  3375. * i40e_validate_vf
  3376. * @pf: the physical function
  3377. * @vf_id: VF identifier
  3378. *
  3379. * Check that the VF is enabled and the VSI exists.
  3380. *
  3381. * Returns 0 on success, negative on failure
  3382. **/
  3383. static int i40e_validate_vf(struct i40e_pf *pf, int vf_id)
  3384. {
  3385. struct i40e_vsi *vsi;
  3386. struct i40e_vf *vf;
  3387. int ret = 0;
  3388. if (vf_id >= pf->num_alloc_vfs) {
  3389. dev_err(&pf->pdev->dev,
  3390. "Invalid VF Identifier %d\n", vf_id);
  3391. ret = -EINVAL;
  3392. goto err_out;
  3393. }
  3394. vf = &pf->vf[vf_id];
  3395. vsi = i40e_find_vsi_from_id(pf, vf->lan_vsi_id);
  3396. if (!vsi)
  3397. ret = -EINVAL;
  3398. err_out:
  3399. return ret;
  3400. }
  3401. /**
  3402. * i40e_ndo_set_vf_mac
  3403. * @netdev: network interface device structure
  3404. * @vf_id: VF identifier
  3405. * @mac: mac address
  3406. *
  3407. * program VF mac address
  3408. **/
  3409. int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
  3410. {
  3411. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3412. struct i40e_vsi *vsi = np->vsi;
  3413. struct i40e_pf *pf = vsi->back;
  3414. struct i40e_mac_filter *f;
  3415. struct i40e_vf *vf;
  3416. int ret = 0;
  3417. struct hlist_node *h;
  3418. int bkt;
  3419. u8 i;
  3420. /* validate the request */
  3421. ret = i40e_validate_vf(pf, vf_id);
  3422. if (ret)
  3423. goto error_param;
  3424. vf = &pf->vf[vf_id];
  3425. vsi = pf->vsi[vf->lan_vsi_idx];
  3426. /* When the VF is resetting wait until it is done.
  3427. * It can take up to 200 milliseconds,
  3428. * but wait for up to 300 milliseconds to be safe.
  3429. */
  3430. for (i = 0; i < 15; i++) {
  3431. if (test_bit(I40E_VF_STATE_INIT, &vf->vf_states))
  3432. break;
  3433. msleep(20);
  3434. }
  3435. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3436. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3437. vf_id);
  3438. ret = -EAGAIN;
  3439. goto error_param;
  3440. }
  3441. if (is_multicast_ether_addr(mac)) {
  3442. dev_err(&pf->pdev->dev,
  3443. "Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
  3444. ret = -EINVAL;
  3445. goto error_param;
  3446. }
  3447. /* Lock once because below invoked function add/del_filter requires
  3448. * mac_filter_hash_lock to be held
  3449. */
  3450. spin_lock_bh(&vsi->mac_filter_hash_lock);
  3451. /* delete the temporary mac address */
  3452. if (!is_zero_ether_addr(vf->default_lan_addr.addr))
  3453. i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
  3454. /* Delete all the filters for this VSI - we're going to kill it
  3455. * anyway.
  3456. */
  3457. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  3458. __i40e_del_filter(vsi, f);
  3459. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3460. /* program mac filter */
  3461. if (i40e_sync_vsi_filters(vsi)) {
  3462. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  3463. ret = -EIO;
  3464. goto error_param;
  3465. }
  3466. ether_addr_copy(vf->default_lan_addr.addr, mac);
  3467. if (is_zero_ether_addr(mac)) {
  3468. vf->pf_set_mac = false;
  3469. dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
  3470. } else {
  3471. vf->pf_set_mac = true;
  3472. dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
  3473. mac, vf_id);
  3474. }
  3475. /* Force the VF interface down so it has to bring up with new MAC
  3476. * address
  3477. */
  3478. i40e_vc_disable_vf(vf);
  3479. dev_info(&pf->pdev->dev, "Bring down and up the VF interface to make this change effective.\n");
  3480. error_param:
  3481. return ret;
  3482. }
  3483. /**
  3484. * i40e_vsi_has_vlans - True if VSI has configured VLANs
  3485. * @vsi: pointer to the vsi
  3486. *
  3487. * Check if a VSI has configured any VLANs. False if we have a port VLAN or if
  3488. * we have no configured VLANs. Do not call while holding the
  3489. * mac_filter_hash_lock.
  3490. */
  3491. static bool i40e_vsi_has_vlans(struct i40e_vsi *vsi)
  3492. {
  3493. bool have_vlans;
  3494. /* If we have a port VLAN, then the VSI cannot have any VLANs
  3495. * configured, as all MAC/VLAN filters will be assigned to the PVID.
  3496. */
  3497. if (vsi->info.pvid)
  3498. return false;
  3499. /* Since we don't have a PVID, we know that if the device is in VLAN
  3500. * mode it must be because of a VLAN filter configured on this VSI.
  3501. */
  3502. spin_lock_bh(&vsi->mac_filter_hash_lock);
  3503. have_vlans = i40e_is_vsi_in_vlan(vsi);
  3504. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3505. return have_vlans;
  3506. }
  3507. /**
  3508. * i40e_ndo_set_vf_port_vlan
  3509. * @netdev: network interface device structure
  3510. * @vf_id: VF identifier
  3511. * @vlan_id: mac address
  3512. * @qos: priority setting
  3513. * @vlan_proto: vlan protocol
  3514. *
  3515. * program VF vlan id and/or qos
  3516. **/
  3517. int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
  3518. u16 vlan_id, u8 qos, __be16 vlan_proto)
  3519. {
  3520. u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
  3521. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3522. struct i40e_pf *pf = np->vsi->back;
  3523. struct i40e_vsi *vsi;
  3524. struct i40e_vf *vf;
  3525. int ret = 0;
  3526. /* validate the request */
  3527. ret = i40e_validate_vf(pf, vf_id);
  3528. if (ret)
  3529. goto error_pvid;
  3530. if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
  3531. dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
  3532. ret = -EINVAL;
  3533. goto error_pvid;
  3534. }
  3535. if (vlan_proto != htons(ETH_P_8021Q)) {
  3536. dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
  3537. ret = -EPROTONOSUPPORT;
  3538. goto error_pvid;
  3539. }
  3540. vf = &pf->vf[vf_id];
  3541. vsi = pf->vsi[vf->lan_vsi_idx];
  3542. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3543. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3544. vf_id);
  3545. ret = -EAGAIN;
  3546. goto error_pvid;
  3547. }
  3548. if (le16_to_cpu(vsi->info.pvid) == vlanprio)
  3549. /* duplicate request, so just return success */
  3550. goto error_pvid;
  3551. if (i40e_vsi_has_vlans(vsi)) {
  3552. dev_err(&pf->pdev->dev,
  3553. "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",
  3554. vf_id);
  3555. /* Administrator Error - knock the VF offline until he does
  3556. * the right thing by reconfiguring his network correctly
  3557. * and then reloading the VF driver.
  3558. */
  3559. i40e_vc_disable_vf(vf);
  3560. /* During reset the VF got a new VSI, so refresh the pointer. */
  3561. vsi = pf->vsi[vf->lan_vsi_idx];
  3562. }
  3563. /* Locked once because multiple functions below iterate list */
  3564. spin_lock_bh(&vsi->mac_filter_hash_lock);
  3565. /* Check for condition where there was already a port VLAN ID
  3566. * filter set and now it is being deleted by setting it to zero.
  3567. * Additionally check for the condition where there was a port
  3568. * VLAN but now there is a new and different port VLAN being set.
  3569. * Before deleting all the old VLAN filters we must add new ones
  3570. * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
  3571. * MAC addresses deleted.
  3572. */
  3573. if ((!(vlan_id || qos) ||
  3574. vlanprio != le16_to_cpu(vsi->info.pvid)) &&
  3575. vsi->info.pvid) {
  3576. ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
  3577. if (ret) {
  3578. dev_info(&vsi->back->pdev->dev,
  3579. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  3580. vsi->back->hw.aq.asq_last_status);
  3581. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3582. goto error_pvid;
  3583. }
  3584. }
  3585. if (vsi->info.pvid) {
  3586. /* remove all filters on the old VLAN */
  3587. i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
  3588. VLAN_VID_MASK));
  3589. }
  3590. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3591. if (vlan_id || qos)
  3592. ret = i40e_vsi_add_pvid(vsi, vlanprio);
  3593. else
  3594. i40e_vsi_remove_pvid(vsi);
  3595. spin_lock_bh(&vsi->mac_filter_hash_lock);
  3596. if (vlan_id) {
  3597. dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
  3598. vlan_id, qos, vf_id);
  3599. /* add new VLAN filter for each MAC */
  3600. ret = i40e_add_vlan_all_mac(vsi, vlan_id);
  3601. if (ret) {
  3602. dev_info(&vsi->back->pdev->dev,
  3603. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  3604. vsi->back->hw.aq.asq_last_status);
  3605. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3606. goto error_pvid;
  3607. }
  3608. /* remove the previously added non-VLAN MAC filters */
  3609. i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
  3610. }
  3611. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3612. /* Schedule the worker thread to take care of applying changes */
  3613. i40e_service_event_schedule(vsi->back);
  3614. if (ret) {
  3615. dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
  3616. goto error_pvid;
  3617. }
  3618. /* The Port VLAN needs to be saved across resets the same as the
  3619. * default LAN MAC address.
  3620. */
  3621. vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
  3622. ret = 0;
  3623. error_pvid:
  3624. return ret;
  3625. }
  3626. /**
  3627. * i40e_ndo_set_vf_bw
  3628. * @netdev: network interface device structure
  3629. * @vf_id: VF identifier
  3630. * @min_tx_rate: Minimum Tx rate
  3631. * @max_tx_rate: Maximum Tx rate
  3632. *
  3633. * configure VF Tx rate
  3634. **/
  3635. int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
  3636. int max_tx_rate)
  3637. {
  3638. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3639. struct i40e_pf *pf = np->vsi->back;
  3640. struct i40e_vsi *vsi;
  3641. struct i40e_vf *vf;
  3642. int ret = 0;
  3643. /* validate the request */
  3644. ret = i40e_validate_vf(pf, vf_id);
  3645. if (ret)
  3646. goto error;
  3647. if (min_tx_rate) {
  3648. dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
  3649. min_tx_rate, vf_id);
  3650. return -EINVAL;
  3651. }
  3652. vf = &pf->vf[vf_id];
  3653. vsi = pf->vsi[vf->lan_vsi_idx];
  3654. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3655. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3656. vf_id);
  3657. ret = -EAGAIN;
  3658. goto error;
  3659. }
  3660. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  3661. if (ret)
  3662. goto error;
  3663. vf->tx_rate = max_tx_rate;
  3664. error:
  3665. return ret;
  3666. }
  3667. /**
  3668. * i40e_ndo_get_vf_config
  3669. * @netdev: network interface device structure
  3670. * @vf_id: VF identifier
  3671. * @ivi: VF configuration structure
  3672. *
  3673. * return VF configuration
  3674. **/
  3675. int i40e_ndo_get_vf_config(struct net_device *netdev,
  3676. int vf_id, struct ifla_vf_info *ivi)
  3677. {
  3678. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3679. struct i40e_vsi *vsi = np->vsi;
  3680. struct i40e_pf *pf = vsi->back;
  3681. struct i40e_vf *vf;
  3682. int ret = 0;
  3683. /* validate the request */
  3684. ret = i40e_validate_vf(pf, vf_id);
  3685. if (ret)
  3686. goto error_param;
  3687. vf = &pf->vf[vf_id];
  3688. /* first vsi is always the LAN vsi */
  3689. vsi = pf->vsi[vf->lan_vsi_idx];
  3690. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3691. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3692. vf_id);
  3693. ret = -EAGAIN;
  3694. goto error_param;
  3695. }
  3696. ivi->vf = vf_id;
  3697. ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
  3698. ivi->max_tx_rate = vf->tx_rate;
  3699. ivi->min_tx_rate = 0;
  3700. ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
  3701. ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
  3702. I40E_VLAN_PRIORITY_SHIFT;
  3703. if (vf->link_forced == false)
  3704. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  3705. else if (vf->link_up == true)
  3706. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  3707. else
  3708. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  3709. ivi->spoofchk = vf->spoofchk;
  3710. ivi->trusted = vf->trusted;
  3711. ret = 0;
  3712. error_param:
  3713. return ret;
  3714. }
  3715. /**
  3716. * i40e_ndo_set_vf_link_state
  3717. * @netdev: network interface device structure
  3718. * @vf_id: VF identifier
  3719. * @link: required link state
  3720. *
  3721. * Set the link state of a specified VF, regardless of physical link state
  3722. **/
  3723. int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
  3724. {
  3725. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3726. struct i40e_pf *pf = np->vsi->back;
  3727. struct virtchnl_pf_event pfe;
  3728. struct i40e_hw *hw = &pf->hw;
  3729. struct i40e_vf *vf;
  3730. int abs_vf_id;
  3731. int ret = 0;
  3732. /* validate the request */
  3733. if (vf_id >= pf->num_alloc_vfs) {
  3734. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3735. ret = -EINVAL;
  3736. goto error_out;
  3737. }
  3738. vf = &pf->vf[vf_id];
  3739. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  3740. pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
  3741. pfe.severity = PF_EVENT_SEVERITY_INFO;
  3742. switch (link) {
  3743. case IFLA_VF_LINK_STATE_AUTO:
  3744. vf->link_forced = false;
  3745. pfe.event_data.link_event.link_status =
  3746. pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
  3747. pfe.event_data.link_event.link_speed =
  3748. (enum virtchnl_link_speed)
  3749. pf->hw.phy.link_info.link_speed;
  3750. break;
  3751. case IFLA_VF_LINK_STATE_ENABLE:
  3752. vf->link_forced = true;
  3753. vf->link_up = true;
  3754. pfe.event_data.link_event.link_status = true;
  3755. pfe.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_40GB;
  3756. break;
  3757. case IFLA_VF_LINK_STATE_DISABLE:
  3758. vf->link_forced = true;
  3759. vf->link_up = false;
  3760. pfe.event_data.link_event.link_status = false;
  3761. pfe.event_data.link_event.link_speed = 0;
  3762. break;
  3763. default:
  3764. ret = -EINVAL;
  3765. goto error_out;
  3766. }
  3767. /* Notify the VF of its new link state */
  3768. i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
  3769. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  3770. error_out:
  3771. return ret;
  3772. }
  3773. /**
  3774. * i40e_ndo_set_vf_spoofchk
  3775. * @netdev: network interface device structure
  3776. * @vf_id: VF identifier
  3777. * @enable: flag to enable or disable feature
  3778. *
  3779. * Enable or disable VF spoof checking
  3780. **/
  3781. int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
  3782. {
  3783. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3784. struct i40e_vsi *vsi = np->vsi;
  3785. struct i40e_pf *pf = vsi->back;
  3786. struct i40e_vsi_context ctxt;
  3787. struct i40e_hw *hw = &pf->hw;
  3788. struct i40e_vf *vf;
  3789. int ret = 0;
  3790. /* validate the request */
  3791. if (vf_id >= pf->num_alloc_vfs) {
  3792. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3793. ret = -EINVAL;
  3794. goto out;
  3795. }
  3796. vf = &(pf->vf[vf_id]);
  3797. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3798. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3799. vf_id);
  3800. ret = -EAGAIN;
  3801. goto out;
  3802. }
  3803. if (enable == vf->spoofchk)
  3804. goto out;
  3805. vf->spoofchk = enable;
  3806. memset(&ctxt, 0, sizeof(ctxt));
  3807. ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
  3808. ctxt.pf_num = pf->hw.pf_id;
  3809. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  3810. if (enable)
  3811. ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  3812. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  3813. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  3814. if (ret) {
  3815. dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
  3816. ret);
  3817. ret = -EIO;
  3818. }
  3819. out:
  3820. return ret;
  3821. }
  3822. /**
  3823. * i40e_ndo_set_vf_trust
  3824. * @netdev: network interface device structure of the pf
  3825. * @vf_id: VF identifier
  3826. * @setting: trust setting
  3827. *
  3828. * Enable or disable VF trust setting
  3829. **/
  3830. int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
  3831. {
  3832. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3833. struct i40e_pf *pf = np->vsi->back;
  3834. struct i40e_vf *vf;
  3835. int ret = 0;
  3836. /* validate the request */
  3837. if (vf_id >= pf->num_alloc_vfs) {
  3838. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3839. return -EINVAL;
  3840. }
  3841. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  3842. dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
  3843. return -EINVAL;
  3844. }
  3845. vf = &pf->vf[vf_id];
  3846. if (setting == vf->trusted)
  3847. goto out;
  3848. vf->trusted = setting;
  3849. i40e_vc_disable_vf(vf);
  3850. dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
  3851. vf_id, setting ? "" : "un");
  3852. if (vf->adq_enabled) {
  3853. if (!vf->trusted) {
  3854. dev_info(&pf->pdev->dev,
  3855. "VF %u no longer Trusted, deleting all cloud filters\n",
  3856. vf_id);
  3857. i40e_del_all_cloud_filters(vf);
  3858. }
  3859. }
  3860. out:
  3861. return ret;
  3862. }