i40e_virtchnl_pf.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include "i40e.h"
  27. /*********************notification routines***********************/
  28. /**
  29. * i40e_vc_vf_broadcast
  30. * @pf: pointer to the PF structure
  31. * @opcode: operation code
  32. * @retval: return value
  33. * @msg: pointer to the msg buffer
  34. * @msglen: msg length
  35. *
  36. * send a message to all VFs on a given PF
  37. **/
  38. static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
  39. enum i40e_virtchnl_ops v_opcode,
  40. i40e_status v_retval, u8 *msg,
  41. u16 msglen)
  42. {
  43. struct i40e_hw *hw = &pf->hw;
  44. struct i40e_vf *vf = pf->vf;
  45. int i;
  46. for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
  47. int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
  48. /* Not all vfs are enabled so skip the ones that are not */
  49. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
  50. !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  51. continue;
  52. /* Ignore return value on purpose - a given VF may fail, but
  53. * we need to keep going and send to all of them
  54. */
  55. i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  56. msg, msglen, NULL);
  57. }
  58. }
  59. /**
  60. * i40e_vc_notify_vf_link_state
  61. * @vf: pointer to the VF structure
  62. *
  63. * send a link status message to a single VF
  64. **/
  65. static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
  66. {
  67. struct i40e_virtchnl_pf_event pfe;
  68. struct i40e_pf *pf = vf->pf;
  69. struct i40e_hw *hw = &pf->hw;
  70. struct i40e_link_status *ls = &pf->hw.phy.link_info;
  71. int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
  72. pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
  73. pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
  74. if (vf->link_forced) {
  75. pfe.event_data.link_event.link_status = vf->link_up;
  76. pfe.event_data.link_event.link_speed =
  77. (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
  78. } else {
  79. pfe.event_data.link_event.link_status =
  80. ls->link_info & I40E_AQ_LINK_UP;
  81. pfe.event_data.link_event.link_speed = ls->link_speed;
  82. }
  83. i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
  84. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  85. }
  86. /**
  87. * i40e_vc_notify_link_state
  88. * @pf: pointer to the PF structure
  89. *
  90. * send a link status message to all VFs on a given PF
  91. **/
  92. void i40e_vc_notify_link_state(struct i40e_pf *pf)
  93. {
  94. int i;
  95. for (i = 0; i < pf->num_alloc_vfs; i++)
  96. i40e_vc_notify_vf_link_state(&pf->vf[i]);
  97. }
  98. /**
  99. * i40e_vc_notify_reset
  100. * @pf: pointer to the PF structure
  101. *
  102. * indicate a pending reset to all VFs on a given PF
  103. **/
  104. void i40e_vc_notify_reset(struct i40e_pf *pf)
  105. {
  106. struct i40e_virtchnl_pf_event pfe;
  107. pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
  108. pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
  109. i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, 0,
  110. (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
  111. }
  112. /**
  113. * i40e_vc_notify_vf_reset
  114. * @vf: pointer to the VF structure
  115. *
  116. * indicate a pending reset to the given VF
  117. **/
  118. void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
  119. {
  120. struct i40e_virtchnl_pf_event pfe;
  121. int abs_vf_id;
  122. /* validate the request */
  123. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  124. return;
  125. /* verify if the VF is in either init or active before proceeding */
  126. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
  127. !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  128. return;
  129. abs_vf_id = vf->vf_id + (int)vf->pf->hw.func_caps.vf_base_id;
  130. pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
  131. pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
  132. i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
  133. 0, (u8 *)&pfe,
  134. sizeof(struct i40e_virtchnl_pf_event), NULL);
  135. }
  136. /***********************misc routines*****************************/
  137. /**
  138. * i40e_vc_disable_vf
  139. * @pf: pointer to the PF info
  140. * @vf: pointer to the VF info
  141. *
  142. * Disable the VF through a SW reset
  143. **/
  144. static inline void i40e_vc_disable_vf(struct i40e_pf *pf, struct i40e_vf *vf)
  145. {
  146. i40e_vc_notify_vf_reset(vf);
  147. i40e_reset_vf(vf, false);
  148. }
  149. /**
  150. * i40e_vc_isvalid_vsi_id
  151. * @vf: pointer to the VF info
  152. * @vsi_id: VF relative VSI id
  153. *
  154. * check for the valid VSI id
  155. **/
  156. static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
  157. {
  158. struct i40e_pf *pf = vf->pf;
  159. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  160. return (vsi && (vsi->vf_id == vf->vf_id));
  161. }
  162. /**
  163. * i40e_vc_isvalid_queue_id
  164. * @vf: pointer to the VF info
  165. * @vsi_id: vsi id
  166. * @qid: vsi relative queue id
  167. *
  168. * check for the valid queue id
  169. **/
  170. static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
  171. u8 qid)
  172. {
  173. struct i40e_pf *pf = vf->pf;
  174. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  175. return (vsi && (qid < vsi->alloc_queue_pairs));
  176. }
  177. /**
  178. * i40e_vc_isvalid_vector_id
  179. * @vf: pointer to the VF info
  180. * @vector_id: VF relative vector id
  181. *
  182. * check for the valid vector id
  183. **/
  184. static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
  185. {
  186. struct i40e_pf *pf = vf->pf;
  187. return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
  188. }
  189. /***********************vf resource mgmt routines*****************/
  190. /**
  191. * i40e_vc_get_pf_queue_id
  192. * @vf: pointer to the VF info
  193. * @vsi_id: id of VSI as provided by the FW
  194. * @vsi_queue_id: vsi relative queue id
  195. *
  196. * return PF relative queue id
  197. **/
  198. static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
  199. u8 vsi_queue_id)
  200. {
  201. struct i40e_pf *pf = vf->pf;
  202. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  203. u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
  204. if (!vsi)
  205. return pf_queue_id;
  206. if (le16_to_cpu(vsi->info.mapping_flags) &
  207. I40E_AQ_VSI_QUE_MAP_NONCONTIG)
  208. pf_queue_id =
  209. le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
  210. else
  211. pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
  212. vsi_queue_id;
  213. return pf_queue_id;
  214. }
  215. /**
  216. * i40e_config_irq_link_list
  217. * @vf: pointer to the VF info
  218. * @vsi_id: id of VSI as given by the FW
  219. * @vecmap: irq map info
  220. *
  221. * configure irq link list from the map
  222. **/
  223. static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
  224. struct i40e_virtchnl_vector_map *vecmap)
  225. {
  226. unsigned long linklistmap = 0, tempmap;
  227. struct i40e_pf *pf = vf->pf;
  228. struct i40e_hw *hw = &pf->hw;
  229. u16 vsi_queue_id, pf_queue_id;
  230. enum i40e_queue_type qtype;
  231. u16 next_q, vector_id;
  232. u32 reg, reg_idx;
  233. u16 itr_idx = 0;
  234. vector_id = vecmap->vector_id;
  235. /* setup the head */
  236. if (0 == vector_id)
  237. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  238. else
  239. reg_idx = I40E_VPINT_LNKLSTN(
  240. ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
  241. (vector_id - 1));
  242. if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
  243. /* Special case - No queues mapped on this vector */
  244. wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
  245. goto irq_list_done;
  246. }
  247. tempmap = vecmap->rxq_map;
  248. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  249. linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
  250. vsi_queue_id));
  251. }
  252. tempmap = vecmap->txq_map;
  253. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  254. linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
  255. vsi_queue_id + 1));
  256. }
  257. next_q = find_first_bit(&linklistmap,
  258. (I40E_MAX_VSI_QP *
  259. I40E_VIRTCHNL_SUPPORTED_QTYPES));
  260. vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
  261. qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
  262. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  263. reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
  264. wr32(hw, reg_idx, reg);
  265. while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
  266. switch (qtype) {
  267. case I40E_QUEUE_TYPE_RX:
  268. reg_idx = I40E_QINT_RQCTL(pf_queue_id);
  269. itr_idx = vecmap->rxitr_idx;
  270. break;
  271. case I40E_QUEUE_TYPE_TX:
  272. reg_idx = I40E_QINT_TQCTL(pf_queue_id);
  273. itr_idx = vecmap->txitr_idx;
  274. break;
  275. default:
  276. break;
  277. }
  278. next_q = find_next_bit(&linklistmap,
  279. (I40E_MAX_VSI_QP *
  280. I40E_VIRTCHNL_SUPPORTED_QTYPES),
  281. next_q + 1);
  282. if (next_q <
  283. (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
  284. vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
  285. qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
  286. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
  287. vsi_queue_id);
  288. } else {
  289. pf_queue_id = I40E_QUEUE_END_OF_LIST;
  290. qtype = 0;
  291. }
  292. /* format for the RQCTL & TQCTL regs is same */
  293. reg = (vector_id) |
  294. (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
  295. (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  296. BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
  297. (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
  298. wr32(hw, reg_idx, reg);
  299. }
  300. /* if the vf is running in polling mode and using interrupt zero,
  301. * need to disable auto-mask on enabling zero interrupt for VFs.
  302. */
  303. if ((vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
  304. (vector_id == 0)) {
  305. reg = rd32(hw, I40E_GLINT_CTL);
  306. if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
  307. reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
  308. wr32(hw, I40E_GLINT_CTL, reg);
  309. }
  310. }
  311. irq_list_done:
  312. i40e_flush(hw);
  313. }
  314. /**
  315. * i40e_release_iwarp_qvlist
  316. * @vf: pointer to the VF.
  317. *
  318. **/
  319. static void i40e_release_iwarp_qvlist(struct i40e_vf *vf)
  320. {
  321. struct i40e_pf *pf = vf->pf;
  322. struct i40e_virtchnl_iwarp_qvlist_info *qvlist_info = vf->qvlist_info;
  323. u32 msix_vf;
  324. u32 i;
  325. if (!vf->qvlist_info)
  326. return;
  327. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  328. for (i = 0; i < qvlist_info->num_vectors; i++) {
  329. struct i40e_virtchnl_iwarp_qv_info *qv_info;
  330. u32 next_q_index, next_q_type;
  331. struct i40e_hw *hw = &pf->hw;
  332. u32 v_idx, reg_idx, reg;
  333. qv_info = &qvlist_info->qv_info[i];
  334. if (!qv_info)
  335. continue;
  336. v_idx = qv_info->v_idx;
  337. if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
  338. /* Figure out the queue after CEQ and make that the
  339. * first queue.
  340. */
  341. reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
  342. reg = rd32(hw, I40E_VPINT_CEQCTL(reg_idx));
  343. next_q_index = (reg & I40E_VPINT_CEQCTL_NEXTQ_INDX_MASK)
  344. >> I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT;
  345. next_q_type = (reg & I40E_VPINT_CEQCTL_NEXTQ_TYPE_MASK)
  346. >> I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT;
  347. reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
  348. reg = (next_q_index &
  349. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
  350. (next_q_type <<
  351. I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  352. wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
  353. }
  354. }
  355. kfree(vf->qvlist_info);
  356. vf->qvlist_info = NULL;
  357. }
  358. /**
  359. * i40e_config_iwarp_qvlist
  360. * @vf: pointer to the VF info
  361. * @qvlist_info: queue and vector list
  362. *
  363. * Return 0 on success or < 0 on error
  364. **/
  365. static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
  366. struct i40e_virtchnl_iwarp_qvlist_info *qvlist_info)
  367. {
  368. struct i40e_pf *pf = vf->pf;
  369. struct i40e_hw *hw = &pf->hw;
  370. struct i40e_virtchnl_iwarp_qv_info *qv_info;
  371. u32 v_idx, i, reg_idx, reg;
  372. u32 next_q_idx, next_q_type;
  373. u32 msix_vf, size;
  374. size = sizeof(struct i40e_virtchnl_iwarp_qvlist_info) +
  375. (sizeof(struct i40e_virtchnl_iwarp_qv_info) *
  376. (qvlist_info->num_vectors - 1));
  377. vf->qvlist_info = kzalloc(size, GFP_KERNEL);
  378. vf->qvlist_info->num_vectors = qvlist_info->num_vectors;
  379. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  380. for (i = 0; i < qvlist_info->num_vectors; i++) {
  381. qv_info = &qvlist_info->qv_info[i];
  382. if (!qv_info)
  383. continue;
  384. v_idx = qv_info->v_idx;
  385. /* Validate vector id belongs to this vf */
  386. if (!i40e_vc_isvalid_vector_id(vf, v_idx))
  387. goto err;
  388. vf->qvlist_info->qv_info[i] = *qv_info;
  389. reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
  390. /* We might be sharing the interrupt, so get the first queue
  391. * index and type, push it down the list by adding the new
  392. * queue on top. Also link it with the new queue in CEQCTL.
  393. */
  394. reg = rd32(hw, I40E_VPINT_LNKLSTN(reg_idx));
  395. next_q_idx = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) >>
  396. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_SHIFT);
  397. next_q_type = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK) >>
  398. I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  399. if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
  400. reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
  401. reg = (I40E_VPINT_CEQCTL_CAUSE_ENA_MASK |
  402. (v_idx << I40E_VPINT_CEQCTL_MSIX_INDX_SHIFT) |
  403. (qv_info->itr_idx << I40E_VPINT_CEQCTL_ITR_INDX_SHIFT) |
  404. (next_q_type << I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT) |
  405. (next_q_idx << I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT));
  406. wr32(hw, I40E_VPINT_CEQCTL(reg_idx), reg);
  407. reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
  408. reg = (qv_info->ceq_idx &
  409. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
  410. (I40E_QUEUE_TYPE_PE_CEQ <<
  411. I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  412. wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
  413. }
  414. if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
  415. reg = (I40E_VPINT_AEQCTL_CAUSE_ENA_MASK |
  416. (v_idx << I40E_VPINT_AEQCTL_MSIX_INDX_SHIFT) |
  417. (qv_info->itr_idx << I40E_VPINT_AEQCTL_ITR_INDX_SHIFT));
  418. wr32(hw, I40E_VPINT_AEQCTL(vf->vf_id), reg);
  419. }
  420. }
  421. return 0;
  422. err:
  423. kfree(vf->qvlist_info);
  424. vf->qvlist_info = NULL;
  425. return -EINVAL;
  426. }
  427. /**
  428. * i40e_config_vsi_tx_queue
  429. * @vf: pointer to the VF info
  430. * @vsi_id: id of VSI as provided by the FW
  431. * @vsi_queue_id: vsi relative queue index
  432. * @info: config. info
  433. *
  434. * configure tx queue
  435. **/
  436. static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
  437. u16 vsi_queue_id,
  438. struct i40e_virtchnl_txq_info *info)
  439. {
  440. struct i40e_pf *pf = vf->pf;
  441. struct i40e_hw *hw = &pf->hw;
  442. struct i40e_hmc_obj_txq tx_ctx;
  443. struct i40e_vsi *vsi;
  444. u16 pf_queue_id;
  445. u32 qtx_ctl;
  446. int ret = 0;
  447. if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
  448. ret = -ENOENT;
  449. goto error_context;
  450. }
  451. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  452. vsi = i40e_find_vsi_from_id(pf, vsi_id);
  453. if (!vsi) {
  454. ret = -ENOENT;
  455. goto error_context;
  456. }
  457. /* clear the context structure first */
  458. memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
  459. /* only set the required fields */
  460. tx_ctx.base = info->dma_ring_addr / 128;
  461. tx_ctx.qlen = info->ring_len;
  462. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
  463. tx_ctx.rdylist_act = 0;
  464. tx_ctx.head_wb_ena = info->headwb_enabled;
  465. tx_ctx.head_wb_addr = info->dma_headwb_addr;
  466. /* clear the context in the HMC */
  467. ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
  468. if (ret) {
  469. dev_err(&pf->pdev->dev,
  470. "Failed to clear VF LAN Tx queue context %d, error: %d\n",
  471. pf_queue_id, ret);
  472. ret = -ENOENT;
  473. goto error_context;
  474. }
  475. /* set the context in the HMC */
  476. ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
  477. if (ret) {
  478. dev_err(&pf->pdev->dev,
  479. "Failed to set VF LAN Tx queue context %d error: %d\n",
  480. pf_queue_id, ret);
  481. ret = -ENOENT;
  482. goto error_context;
  483. }
  484. /* associate this queue with the PCI VF function */
  485. qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
  486. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
  487. & I40E_QTX_CTL_PF_INDX_MASK);
  488. qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
  489. << I40E_QTX_CTL_VFVM_INDX_SHIFT)
  490. & I40E_QTX_CTL_VFVM_INDX_MASK);
  491. wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
  492. i40e_flush(hw);
  493. error_context:
  494. return ret;
  495. }
  496. /**
  497. * i40e_config_vsi_rx_queue
  498. * @vf: pointer to the VF info
  499. * @vsi_id: id of VSI as provided by the FW
  500. * @vsi_queue_id: vsi relative queue index
  501. * @info: config. info
  502. *
  503. * configure rx queue
  504. **/
  505. static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
  506. u16 vsi_queue_id,
  507. struct i40e_virtchnl_rxq_info *info)
  508. {
  509. struct i40e_pf *pf = vf->pf;
  510. struct i40e_hw *hw = &pf->hw;
  511. struct i40e_hmc_obj_rxq rx_ctx;
  512. u16 pf_queue_id;
  513. int ret = 0;
  514. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  515. /* clear the context structure first */
  516. memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
  517. /* only set the required fields */
  518. rx_ctx.base = info->dma_ring_addr / 128;
  519. rx_ctx.qlen = info->ring_len;
  520. if (info->splithdr_enabled) {
  521. rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
  522. I40E_RX_SPLIT_IP |
  523. I40E_RX_SPLIT_TCP_UDP |
  524. I40E_RX_SPLIT_SCTP;
  525. /* header length validation */
  526. if (info->hdr_size > ((2 * 1024) - 64)) {
  527. ret = -EINVAL;
  528. goto error_param;
  529. }
  530. rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
  531. /* set split mode 10b */
  532. rx_ctx.dtype = I40E_RX_DTYPE_HEADER_SPLIT;
  533. }
  534. /* databuffer length validation */
  535. if (info->databuffer_size > ((16 * 1024) - 128)) {
  536. ret = -EINVAL;
  537. goto error_param;
  538. }
  539. rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
  540. /* max pkt. length validation */
  541. if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
  542. ret = -EINVAL;
  543. goto error_param;
  544. }
  545. rx_ctx.rxmax = info->max_pkt_size;
  546. /* enable 32bytes desc always */
  547. rx_ctx.dsize = 1;
  548. /* default values */
  549. rx_ctx.lrxqthresh = 2;
  550. rx_ctx.crcstrip = 1;
  551. rx_ctx.prefena = 1;
  552. rx_ctx.l2tsel = 1;
  553. /* clear the context in the HMC */
  554. ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
  555. if (ret) {
  556. dev_err(&pf->pdev->dev,
  557. "Failed to clear VF LAN Rx queue context %d, error: %d\n",
  558. pf_queue_id, ret);
  559. ret = -ENOENT;
  560. goto error_param;
  561. }
  562. /* set the context in the HMC */
  563. ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
  564. if (ret) {
  565. dev_err(&pf->pdev->dev,
  566. "Failed to set VF LAN Rx queue context %d error: %d\n",
  567. pf_queue_id, ret);
  568. ret = -ENOENT;
  569. goto error_param;
  570. }
  571. error_param:
  572. return ret;
  573. }
  574. /**
  575. * i40e_alloc_vsi_res
  576. * @vf: pointer to the VF info
  577. * @type: type of VSI to allocate
  578. *
  579. * alloc VF vsi context & resources
  580. **/
  581. static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
  582. {
  583. struct i40e_mac_filter *f = NULL;
  584. struct i40e_pf *pf = vf->pf;
  585. struct i40e_vsi *vsi;
  586. int ret = 0;
  587. vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
  588. if (!vsi) {
  589. dev_err(&pf->pdev->dev,
  590. "add vsi failed for VF %d, aq_err %d\n",
  591. vf->vf_id, pf->hw.aq.asq_last_status);
  592. ret = -ENOENT;
  593. goto error_alloc_vsi_res;
  594. }
  595. if (type == I40E_VSI_SRIOV) {
  596. u64 hena = i40e_pf_get_default_rss_hena(pf);
  597. u8 broadcast[ETH_ALEN];
  598. vf->lan_vsi_idx = vsi->idx;
  599. vf->lan_vsi_id = vsi->id;
  600. /* If the port VLAN has been configured and then the
  601. * VF driver was removed then the VSI port VLAN
  602. * configuration was destroyed. Check if there is
  603. * a port VLAN and restore the VSI configuration if
  604. * needed.
  605. */
  606. if (vf->port_vlan_id)
  607. i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
  608. spin_lock_bh(&vsi->mac_filter_hash_lock);
  609. if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
  610. f = i40e_add_mac_filter(vsi,
  611. vf->default_lan_addr.addr);
  612. if (!f)
  613. dev_info(&pf->pdev->dev,
  614. "Could not add MAC filter %pM for VF %d\n",
  615. vf->default_lan_addr.addr, vf->vf_id);
  616. }
  617. eth_broadcast_addr(broadcast);
  618. f = i40e_add_mac_filter(vsi, broadcast);
  619. if (!f)
  620. dev_info(&pf->pdev->dev,
  621. "Could not allocate VF broadcast filter\n");
  622. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  623. wr32(&pf->hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)hena);
  624. wr32(&pf->hw, I40E_VFQF_HENA1(1, vf->vf_id), (u32)(hena >> 32));
  625. }
  626. /* program mac filter */
  627. ret = i40e_sync_vsi_filters(vsi);
  628. if (ret)
  629. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  630. /* Set VF bandwidth if specified */
  631. if (vf->tx_rate) {
  632. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
  633. vf->tx_rate / 50, 0, NULL);
  634. if (ret)
  635. dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
  636. vf->vf_id, ret);
  637. }
  638. error_alloc_vsi_res:
  639. return ret;
  640. }
  641. /**
  642. * i40e_enable_vf_mappings
  643. * @vf: pointer to the VF info
  644. *
  645. * enable VF mappings
  646. **/
  647. static void i40e_enable_vf_mappings(struct i40e_vf *vf)
  648. {
  649. struct i40e_pf *pf = vf->pf;
  650. struct i40e_hw *hw = &pf->hw;
  651. u32 reg, total_queue_pairs = 0;
  652. int j;
  653. /* Tell the hardware we're using noncontiguous mapping. HW requires
  654. * that VF queues be mapped using this method, even when they are
  655. * contiguous in real life
  656. */
  657. i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
  658. I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
  659. /* enable VF vplan_qtable mappings */
  660. reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
  661. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
  662. /* map PF queues to VF queues */
  663. for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
  664. u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
  665. reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
  666. wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
  667. total_queue_pairs++;
  668. }
  669. /* map PF queues to VSI */
  670. for (j = 0; j < 7; j++) {
  671. if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
  672. reg = 0x07FF07FF; /* unused */
  673. } else {
  674. u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
  675. j * 2);
  676. reg = qid;
  677. qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
  678. (j * 2) + 1);
  679. reg |= qid << 16;
  680. }
  681. i40e_write_rx_ctl(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id),
  682. reg);
  683. }
  684. i40e_flush(hw);
  685. }
  686. /**
  687. * i40e_disable_vf_mappings
  688. * @vf: pointer to the VF info
  689. *
  690. * disable VF mappings
  691. **/
  692. static void i40e_disable_vf_mappings(struct i40e_vf *vf)
  693. {
  694. struct i40e_pf *pf = vf->pf;
  695. struct i40e_hw *hw = &pf->hw;
  696. int i;
  697. /* disable qp mappings */
  698. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
  699. for (i = 0; i < I40E_MAX_VSI_QP; i++)
  700. wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
  701. I40E_QUEUE_END_OF_LIST);
  702. i40e_flush(hw);
  703. }
  704. /**
  705. * i40e_free_vf_res
  706. * @vf: pointer to the VF info
  707. *
  708. * free VF resources
  709. **/
  710. static void i40e_free_vf_res(struct i40e_vf *vf)
  711. {
  712. struct i40e_pf *pf = vf->pf;
  713. struct i40e_hw *hw = &pf->hw;
  714. u32 reg_idx, reg;
  715. int i, msix_vf;
  716. /* Start by disabling VF's configuration API to prevent the OS from
  717. * accessing the VF's VSI after it's freed / invalidated.
  718. */
  719. clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  720. /* free vsi & disconnect it from the parent uplink */
  721. if (vf->lan_vsi_idx) {
  722. i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
  723. vf->lan_vsi_idx = 0;
  724. vf->lan_vsi_id = 0;
  725. vf->num_mac = 0;
  726. }
  727. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  728. /* disable interrupts so the VF starts in a known state */
  729. for (i = 0; i < msix_vf; i++) {
  730. /* format is same for both registers */
  731. if (0 == i)
  732. reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
  733. else
  734. reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
  735. (vf->vf_id))
  736. + (i - 1));
  737. wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
  738. i40e_flush(hw);
  739. }
  740. /* clear the irq settings */
  741. for (i = 0; i < msix_vf; i++) {
  742. /* format is same for both registers */
  743. if (0 == i)
  744. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  745. else
  746. reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
  747. (vf->vf_id))
  748. + (i - 1));
  749. reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
  750. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
  751. wr32(hw, reg_idx, reg);
  752. i40e_flush(hw);
  753. }
  754. /* reset some of the state variables keeping track of the resources */
  755. vf->num_queue_pairs = 0;
  756. vf->vf_states = 0;
  757. }
  758. /**
  759. * i40e_alloc_vf_res
  760. * @vf: pointer to the VF info
  761. *
  762. * allocate VF resources
  763. **/
  764. static int i40e_alloc_vf_res(struct i40e_vf *vf)
  765. {
  766. struct i40e_pf *pf = vf->pf;
  767. int total_queue_pairs = 0;
  768. int ret;
  769. /* allocate hw vsi context & associated resources */
  770. ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
  771. if (ret)
  772. goto error_alloc;
  773. total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
  774. if (vf->trusted)
  775. set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  776. else
  777. clear_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  778. /* store the total qps number for the runtime
  779. * VF req validation
  780. */
  781. vf->num_queue_pairs = total_queue_pairs;
  782. /* VF is now completely initialized */
  783. set_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  784. error_alloc:
  785. if (ret)
  786. i40e_free_vf_res(vf);
  787. return ret;
  788. }
  789. #define VF_DEVICE_STATUS 0xAA
  790. #define VF_TRANS_PENDING_MASK 0x20
  791. /**
  792. * i40e_quiesce_vf_pci
  793. * @vf: pointer to the VF structure
  794. *
  795. * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
  796. * if the transactions never clear.
  797. **/
  798. static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
  799. {
  800. struct i40e_pf *pf = vf->pf;
  801. struct i40e_hw *hw = &pf->hw;
  802. int vf_abs_id, i;
  803. u32 reg;
  804. vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
  805. wr32(hw, I40E_PF_PCI_CIAA,
  806. VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
  807. for (i = 0; i < 100; i++) {
  808. reg = rd32(hw, I40E_PF_PCI_CIAD);
  809. if ((reg & VF_TRANS_PENDING_MASK) == 0)
  810. return 0;
  811. udelay(1);
  812. }
  813. return -EIO;
  814. }
  815. /**
  816. * i40e_trigger_vf_reset
  817. * @vf: pointer to the VF structure
  818. * @flr: VFLR was issued or not
  819. *
  820. * Trigger hardware to start a reset for a particular VF. Expects the caller
  821. * to wait the proper amount of time to allow hardware to reset the VF before
  822. * it cleans up and restores VF functionality.
  823. **/
  824. static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
  825. {
  826. struct i40e_pf *pf = vf->pf;
  827. struct i40e_hw *hw = &pf->hw;
  828. u32 reg, reg_idx, bit_idx;
  829. /* warn the VF */
  830. clear_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  831. /* Disable VF's configuration API during reset. The flag is re-enabled
  832. * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
  833. * It's normally disabled in i40e_free_vf_res(), but it's safer
  834. * to do it earlier to give some time to finish to any VF config
  835. * functions that may still be running at this point.
  836. */
  837. clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  838. /* In the case of a VFLR, the HW has already reset the VF and we
  839. * just need to clean up, so don't hit the VFRTRIG register.
  840. */
  841. if (!flr) {
  842. /* reset VF using VPGEN_VFRTRIG reg */
  843. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  844. reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  845. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  846. i40e_flush(hw);
  847. }
  848. /* clear the VFLR bit in GLGEN_VFLRSTAT */
  849. reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
  850. bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
  851. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  852. i40e_flush(hw);
  853. if (i40e_quiesce_vf_pci(vf))
  854. dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
  855. vf->vf_id);
  856. }
  857. /**
  858. * i40e_cleanup_reset_vf
  859. * @vf: pointer to the VF structure
  860. *
  861. * Cleanup a VF after the hardware reset is finished. Expects the caller to
  862. * have verified whether the reset is finished properly, and ensure the
  863. * minimum amount of wait time has passed.
  864. **/
  865. static void i40e_cleanup_reset_vf(struct i40e_vf *vf)
  866. {
  867. struct i40e_pf *pf = vf->pf;
  868. struct i40e_hw *hw = &pf->hw;
  869. u32 reg;
  870. /* free VF resources to begin resetting the VSI state */
  871. i40e_free_vf_res(vf);
  872. /* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
  873. * By doing this we allow HW to access VF memory at any point. If we
  874. * did it any sooner, HW could access memory while it was being freed
  875. * in i40e_free_vf_res(), causing an IOMMU fault.
  876. *
  877. * On the other hand, this needs to be done ASAP, because the VF driver
  878. * is waiting for this to happen and may report a timeout. It's
  879. * harmless, but it gets logged into Guest OS kernel log, so best avoid
  880. * it.
  881. */
  882. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  883. reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  884. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  885. /* reallocate VF resources to finish resetting the VSI state */
  886. if (!i40e_alloc_vf_res(vf)) {
  887. int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  888. i40e_enable_vf_mappings(vf);
  889. set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  890. clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  891. /* Do not notify the client during VF init */
  892. if (test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
  893. &vf->vf_states))
  894. i40e_notify_client_of_vf_reset(pf, abs_vf_id);
  895. vf->num_vlan = 0;
  896. }
  897. /* Tell the VF driver the reset is done. This needs to be done only
  898. * after VF has been fully initialized, because the VF driver may
  899. * request resources immediately after setting this flag.
  900. */
  901. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
  902. }
  903. /**
  904. * i40e_reset_vf
  905. * @vf: pointer to the VF structure
  906. * @flr: VFLR was issued or not
  907. *
  908. * reset the VF
  909. **/
  910. void i40e_reset_vf(struct i40e_vf *vf, bool flr)
  911. {
  912. struct i40e_pf *pf = vf->pf;
  913. struct i40e_hw *hw = &pf->hw;
  914. bool rsd = false;
  915. u32 reg;
  916. int i;
  917. /* If VFs have been disabled, there is no need to reset */
  918. if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  919. return;
  920. i40e_trigger_vf_reset(vf, flr);
  921. /* poll VPGEN_VFRSTAT reg to make sure
  922. * that reset is complete
  923. */
  924. for (i = 0; i < 10; i++) {
  925. /* VF reset requires driver to first reset the VF and then
  926. * poll the status register to make sure that the reset
  927. * completed successfully. Due to internal HW FIFO flushes,
  928. * we must wait 10ms before the register will be valid.
  929. */
  930. usleep_range(10000, 20000);
  931. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  932. if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
  933. rsd = true;
  934. break;
  935. }
  936. }
  937. if (flr)
  938. usleep_range(10000, 20000);
  939. if (!rsd)
  940. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  941. vf->vf_id);
  942. usleep_range(10000, 20000);
  943. /* On initial reset, we don't have any queues to disable */
  944. if (vf->lan_vsi_idx != 0)
  945. i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
  946. i40e_cleanup_reset_vf(vf);
  947. i40e_flush(hw);
  948. clear_bit(__I40E_VF_DISABLE, pf->state);
  949. }
  950. /**
  951. * i40e_reset_all_vfs
  952. * @pf: pointer to the PF structure
  953. * @flr: VFLR was issued or not
  954. *
  955. * Reset all allocated VFs in one go. First, tell the hardware to reset each
  956. * VF, then do all the waiting in one chunk, and finally finish restoring each
  957. * VF after the wait. This is useful during PF routines which need to reset
  958. * all VFs, as otherwise it must perform these resets in a serialized fashion.
  959. **/
  960. void i40e_reset_all_vfs(struct i40e_pf *pf, bool flr)
  961. {
  962. struct i40e_hw *hw = &pf->hw;
  963. struct i40e_vf *vf;
  964. int i, v;
  965. u32 reg;
  966. /* If we don't have any VFs, then there is nothing to reset */
  967. if (!pf->num_alloc_vfs)
  968. return;
  969. /* If VFs have been disabled, there is no need to reset */
  970. if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  971. return;
  972. /* Begin reset on all VFs at once */
  973. for (v = 0; v < pf->num_alloc_vfs; v++)
  974. i40e_trigger_vf_reset(&pf->vf[v], flr);
  975. /* HW requires some time to make sure it can flush the FIFO for a VF
  976. * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
  977. * sequence to make sure that it has completed. We'll keep track of
  978. * the VFs using a simple iterator that increments once that VF has
  979. * finished resetting.
  980. */
  981. for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
  982. usleep_range(10000, 20000);
  983. /* Check each VF in sequence, beginning with the VF to fail
  984. * the previous check.
  985. */
  986. while (v < pf->num_alloc_vfs) {
  987. vf = &pf->vf[v];
  988. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  989. if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
  990. break;
  991. /* If the current VF has finished resetting, move on
  992. * to the next VF in sequence.
  993. */
  994. v++;
  995. }
  996. }
  997. if (flr)
  998. usleep_range(10000, 20000);
  999. /* Display a warning if at least one VF didn't manage to reset in
  1000. * time, but continue on with the operation.
  1001. */
  1002. if (v < pf->num_alloc_vfs)
  1003. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  1004. pf->vf[v].vf_id);
  1005. usleep_range(10000, 20000);
  1006. /* Begin disabling all the rings associated with VFs, but do not wait
  1007. * between each VF.
  1008. */
  1009. for (v = 0; v < pf->num_alloc_vfs; v++) {
  1010. /* On initial reset, we don't have any queues to disable */
  1011. if (pf->vf[v].lan_vsi_idx == 0)
  1012. continue;
  1013. i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
  1014. }
  1015. /* Now that we've notified HW to disable all of the VF rings, wait
  1016. * until they finish.
  1017. */
  1018. for (v = 0; v < pf->num_alloc_vfs; v++) {
  1019. /* On initial reset, we don't have any queues to disable */
  1020. if (pf->vf[v].lan_vsi_idx == 0)
  1021. continue;
  1022. i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
  1023. }
  1024. /* Hw may need up to 50ms to finish disabling the RX queues. We
  1025. * minimize the wait by delaying only once for all VFs.
  1026. */
  1027. mdelay(50);
  1028. /* Finish the reset on each VF */
  1029. for (v = 0; v < pf->num_alloc_vfs; v++)
  1030. i40e_cleanup_reset_vf(&pf->vf[v]);
  1031. i40e_flush(hw);
  1032. clear_bit(__I40E_VF_DISABLE, pf->state);
  1033. }
  1034. /**
  1035. * i40e_free_vfs
  1036. * @pf: pointer to the PF structure
  1037. *
  1038. * free VF resources
  1039. **/
  1040. void i40e_free_vfs(struct i40e_pf *pf)
  1041. {
  1042. struct i40e_hw *hw = &pf->hw;
  1043. u32 reg_idx, bit_idx;
  1044. int i, tmp, vf_id;
  1045. if (!pf->vf)
  1046. return;
  1047. while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  1048. usleep_range(1000, 2000);
  1049. i40e_notify_client_of_vf_enable(pf, 0);
  1050. /* Amortize wait time by stopping all VFs at the same time */
  1051. for (i = 0; i < pf->num_alloc_vfs; i++) {
  1052. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1053. continue;
  1054. i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
  1055. }
  1056. for (i = 0; i < pf->num_alloc_vfs; i++) {
  1057. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1058. continue;
  1059. i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
  1060. }
  1061. /* Disable IOV before freeing resources. This lets any VF drivers
  1062. * running in the host get themselves cleaned up before we yank
  1063. * the carpet out from underneath their feet.
  1064. */
  1065. if (!pci_vfs_assigned(pf->pdev))
  1066. pci_disable_sriov(pf->pdev);
  1067. else
  1068. dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
  1069. /* free up VF resources */
  1070. tmp = pf->num_alloc_vfs;
  1071. pf->num_alloc_vfs = 0;
  1072. for (i = 0; i < tmp; i++) {
  1073. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1074. i40e_free_vf_res(&pf->vf[i]);
  1075. /* disable qp mappings */
  1076. i40e_disable_vf_mappings(&pf->vf[i]);
  1077. }
  1078. kfree(pf->vf);
  1079. pf->vf = NULL;
  1080. /* This check is for when the driver is unloaded while VFs are
  1081. * assigned. Setting the number of VFs to 0 through sysfs is caught
  1082. * before this function ever gets called.
  1083. */
  1084. if (!pci_vfs_assigned(pf->pdev)) {
  1085. /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
  1086. * work correctly when SR-IOV gets re-enabled.
  1087. */
  1088. for (vf_id = 0; vf_id < tmp; vf_id++) {
  1089. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  1090. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  1091. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  1092. }
  1093. }
  1094. clear_bit(__I40E_VF_DISABLE, pf->state);
  1095. }
  1096. #ifdef CONFIG_PCI_IOV
  1097. /**
  1098. * i40e_alloc_vfs
  1099. * @pf: pointer to the PF structure
  1100. * @num_alloc_vfs: number of VFs to allocate
  1101. *
  1102. * allocate VF resources
  1103. **/
  1104. int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
  1105. {
  1106. struct i40e_vf *vfs;
  1107. int i, ret = 0;
  1108. /* Disable interrupt 0 so we don't try to handle the VFLR. */
  1109. i40e_irq_dynamic_disable_icr0(pf);
  1110. /* Check to see if we're just allocating resources for extant VFs */
  1111. if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
  1112. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  1113. if (ret) {
  1114. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  1115. pf->num_alloc_vfs = 0;
  1116. goto err_iov;
  1117. }
  1118. }
  1119. /* allocate memory */
  1120. vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
  1121. if (!vfs) {
  1122. ret = -ENOMEM;
  1123. goto err_alloc;
  1124. }
  1125. pf->vf = vfs;
  1126. /* apply default profile */
  1127. for (i = 0; i < num_alloc_vfs; i++) {
  1128. vfs[i].pf = pf;
  1129. vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
  1130. vfs[i].vf_id = i;
  1131. /* assign default capabilities */
  1132. set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
  1133. vfs[i].spoofchk = true;
  1134. set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
  1135. }
  1136. pf->num_alloc_vfs = num_alloc_vfs;
  1137. /* VF resources get allocated during reset */
  1138. i40e_reset_all_vfs(pf, false);
  1139. i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
  1140. err_alloc:
  1141. if (ret)
  1142. i40e_free_vfs(pf);
  1143. err_iov:
  1144. /* Re-enable interrupt 0. */
  1145. i40e_irq_dynamic_enable_icr0(pf, false);
  1146. return ret;
  1147. }
  1148. #endif
  1149. /**
  1150. * i40e_pci_sriov_enable
  1151. * @pdev: pointer to a pci_dev structure
  1152. * @num_vfs: number of VFs to allocate
  1153. *
  1154. * Enable or change the number of VFs
  1155. **/
  1156. static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
  1157. {
  1158. #ifdef CONFIG_PCI_IOV
  1159. struct i40e_pf *pf = pci_get_drvdata(pdev);
  1160. int pre_existing_vfs = pci_num_vf(pdev);
  1161. int err = 0;
  1162. if (test_bit(__I40E_TESTING, pf->state)) {
  1163. dev_warn(&pdev->dev,
  1164. "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
  1165. err = -EPERM;
  1166. goto err_out;
  1167. }
  1168. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  1169. i40e_free_vfs(pf);
  1170. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  1171. goto out;
  1172. if (num_vfs > pf->num_req_vfs) {
  1173. dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
  1174. num_vfs, pf->num_req_vfs);
  1175. err = -EPERM;
  1176. goto err_out;
  1177. }
  1178. dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
  1179. err = i40e_alloc_vfs(pf, num_vfs);
  1180. if (err) {
  1181. dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
  1182. goto err_out;
  1183. }
  1184. out:
  1185. return num_vfs;
  1186. err_out:
  1187. return err;
  1188. #endif
  1189. return 0;
  1190. }
  1191. /**
  1192. * i40e_pci_sriov_configure
  1193. * @pdev: pointer to a pci_dev structure
  1194. * @num_vfs: number of VFs to allocate
  1195. *
  1196. * Enable or change the number of VFs. Called when the user updates the number
  1197. * of VFs in sysfs.
  1198. **/
  1199. int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
  1200. {
  1201. struct i40e_pf *pf = pci_get_drvdata(pdev);
  1202. if (num_vfs) {
  1203. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  1204. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  1205. i40e_do_reset_safe(pf,
  1206. BIT_ULL(__I40E_PF_RESET_REQUESTED));
  1207. }
  1208. return i40e_pci_sriov_enable(pdev, num_vfs);
  1209. }
  1210. if (!pci_vfs_assigned(pf->pdev)) {
  1211. i40e_free_vfs(pf);
  1212. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  1213. i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  1214. } else {
  1215. dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
  1216. return -EINVAL;
  1217. }
  1218. return 0;
  1219. }
  1220. /***********************virtual channel routines******************/
  1221. /**
  1222. * i40e_vc_send_msg_to_vf
  1223. * @vf: pointer to the VF info
  1224. * @v_opcode: virtual channel opcode
  1225. * @v_retval: virtual channel return value
  1226. * @msg: pointer to the msg buffer
  1227. * @msglen: msg length
  1228. *
  1229. * send msg to VF
  1230. **/
  1231. static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
  1232. u32 v_retval, u8 *msg, u16 msglen)
  1233. {
  1234. struct i40e_pf *pf;
  1235. struct i40e_hw *hw;
  1236. int abs_vf_id;
  1237. i40e_status aq_ret;
  1238. /* validate the request */
  1239. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  1240. return -EINVAL;
  1241. pf = vf->pf;
  1242. hw = &pf->hw;
  1243. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  1244. /* single place to detect unsuccessful return values */
  1245. if (v_retval) {
  1246. vf->num_invalid_msgs++;
  1247. dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
  1248. vf->vf_id, v_opcode, v_retval);
  1249. if (vf->num_invalid_msgs >
  1250. I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
  1251. dev_err(&pf->pdev->dev,
  1252. "Number of invalid messages exceeded for VF %d\n",
  1253. vf->vf_id);
  1254. dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
  1255. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  1256. }
  1257. } else {
  1258. vf->num_valid_msgs++;
  1259. /* reset the invalid counter, if a valid message is received. */
  1260. vf->num_invalid_msgs = 0;
  1261. }
  1262. aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  1263. msg, msglen, NULL);
  1264. if (aq_ret) {
  1265. dev_info(&pf->pdev->dev,
  1266. "Unable to send the message to VF %d aq_err %d\n",
  1267. vf->vf_id, pf->hw.aq.asq_last_status);
  1268. return -EIO;
  1269. }
  1270. return 0;
  1271. }
  1272. /**
  1273. * i40e_vc_send_resp_to_vf
  1274. * @vf: pointer to the VF info
  1275. * @opcode: operation code
  1276. * @retval: return value
  1277. *
  1278. * send resp msg to VF
  1279. **/
  1280. static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
  1281. enum i40e_virtchnl_ops opcode,
  1282. i40e_status retval)
  1283. {
  1284. return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
  1285. }
  1286. /**
  1287. * i40e_vc_get_version_msg
  1288. * @vf: pointer to the VF info
  1289. *
  1290. * called from the VF to request the API version used by the PF
  1291. **/
  1292. static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
  1293. {
  1294. struct i40e_virtchnl_version_info info = {
  1295. I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
  1296. };
  1297. vf->vf_ver = *(struct i40e_virtchnl_version_info *)msg;
  1298. /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
  1299. if (VF_IS_V10(vf))
  1300. info.minor = I40E_VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
  1301. return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
  1302. I40E_SUCCESS, (u8 *)&info,
  1303. sizeof(struct
  1304. i40e_virtchnl_version_info));
  1305. }
  1306. /**
  1307. * i40e_vc_get_vf_resources_msg
  1308. * @vf: pointer to the VF info
  1309. * @msg: pointer to the msg buffer
  1310. * @msglen: msg length
  1311. *
  1312. * called from the VF to request its resources
  1313. **/
  1314. static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
  1315. {
  1316. struct i40e_virtchnl_vf_resource *vfres = NULL;
  1317. struct i40e_pf *pf = vf->pf;
  1318. i40e_status aq_ret = 0;
  1319. struct i40e_vsi *vsi;
  1320. int num_vsis = 1;
  1321. int len = 0;
  1322. int ret;
  1323. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  1324. aq_ret = I40E_ERR_PARAM;
  1325. goto err;
  1326. }
  1327. len = (sizeof(struct i40e_virtchnl_vf_resource) +
  1328. sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
  1329. vfres = kzalloc(len, GFP_KERNEL);
  1330. if (!vfres) {
  1331. aq_ret = I40E_ERR_NO_MEMORY;
  1332. len = 0;
  1333. goto err;
  1334. }
  1335. if (VF_IS_V11(vf))
  1336. vf->driver_caps = *(u32 *)msg;
  1337. else
  1338. vf->driver_caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
  1339. I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
  1340. I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
  1341. vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
  1342. vsi = pf->vsi[vf->lan_vsi_idx];
  1343. if (!vsi->info.pvid)
  1344. vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
  1345. if (i40e_vf_client_capable(pf, vf->vf_id) &&
  1346. (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_IWARP)) {
  1347. vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_IWARP;
  1348. set_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
  1349. }
  1350. if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PF) {
  1351. vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RSS_PF;
  1352. } else {
  1353. if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
  1354. (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ))
  1355. vfres->vf_offload_flags |=
  1356. I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ;
  1357. else
  1358. vfres->vf_offload_flags |=
  1359. I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG;
  1360. }
  1361. if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
  1362. if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
  1363. vfres->vf_offload_flags |=
  1364. I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
  1365. }
  1366. if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_ENCAP)
  1367. vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_ENCAP;
  1368. if ((pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE) &&
  1369. (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
  1370. vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
  1371. if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
  1372. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  1373. dev_err(&pf->pdev->dev,
  1374. "VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
  1375. vf->vf_id);
  1376. ret = I40E_ERR_PARAM;
  1377. goto err;
  1378. }
  1379. vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING;
  1380. }
  1381. if (pf->flags & I40E_FLAG_WB_ON_ITR_CAPABLE) {
  1382. if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
  1383. vfres->vf_offload_flags |=
  1384. I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
  1385. }
  1386. vfres->num_vsis = num_vsis;
  1387. vfres->num_queue_pairs = vf->num_queue_pairs;
  1388. vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
  1389. vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
  1390. vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
  1391. if (vf->lan_vsi_idx) {
  1392. vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
  1393. vfres->vsi_res[0].vsi_type = I40E_VSI_SRIOV;
  1394. vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
  1395. /* VFs only use TC 0 */
  1396. vfres->vsi_res[0].qset_handle
  1397. = le16_to_cpu(vsi->info.qs_handle[0]);
  1398. ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
  1399. vf->default_lan_addr.addr);
  1400. }
  1401. set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  1402. err:
  1403. /* send the response back to the VF */
  1404. ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
  1405. aq_ret, (u8 *)vfres, len);
  1406. kfree(vfres);
  1407. return ret;
  1408. }
  1409. /**
  1410. * i40e_vc_reset_vf_msg
  1411. * @vf: pointer to the VF info
  1412. * @msg: pointer to the msg buffer
  1413. * @msglen: msg length
  1414. *
  1415. * called from the VF to reset itself,
  1416. * unlike other virtchnl messages, PF driver
  1417. * doesn't send the response back to the VF
  1418. **/
  1419. static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
  1420. {
  1421. if (test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  1422. i40e_reset_vf(vf, false);
  1423. }
  1424. /**
  1425. * i40e_getnum_vf_vsi_vlan_filters
  1426. * @vsi: pointer to the vsi
  1427. *
  1428. * called to get the number of VLANs offloaded on this VF
  1429. **/
  1430. static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
  1431. {
  1432. struct i40e_mac_filter *f;
  1433. int num_vlans = 0, bkt;
  1434. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1435. if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
  1436. num_vlans++;
  1437. }
  1438. return num_vlans;
  1439. }
  1440. /**
  1441. * i40e_vc_config_promiscuous_mode_msg
  1442. * @vf: pointer to the VF info
  1443. * @msg: pointer to the msg buffer
  1444. * @msglen: msg length
  1445. *
  1446. * called from the VF to configure the promiscuous mode of
  1447. * VF vsis
  1448. **/
  1449. static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
  1450. u8 *msg, u16 msglen)
  1451. {
  1452. struct i40e_virtchnl_promisc_info *info =
  1453. (struct i40e_virtchnl_promisc_info *)msg;
  1454. struct i40e_pf *pf = vf->pf;
  1455. struct i40e_hw *hw = &pf->hw;
  1456. struct i40e_mac_filter *f;
  1457. i40e_status aq_ret = 0;
  1458. bool allmulti = false;
  1459. struct i40e_vsi *vsi;
  1460. bool alluni = false;
  1461. int aq_err = 0;
  1462. int bkt;
  1463. vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
  1464. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  1465. !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
  1466. !vsi) {
  1467. aq_ret = I40E_ERR_PARAM;
  1468. goto error_param;
  1469. }
  1470. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  1471. dev_err(&pf->pdev->dev,
  1472. "Unprivileged VF %d is attempting to configure promiscuous mode\n",
  1473. vf->vf_id);
  1474. /* Lie to the VF on purpose. */
  1475. aq_ret = 0;
  1476. goto error_param;
  1477. }
  1478. /* Multicast promiscuous handling*/
  1479. if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
  1480. allmulti = true;
  1481. if (vf->port_vlan_id) {
  1482. aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
  1483. allmulti,
  1484. vf->port_vlan_id,
  1485. NULL);
  1486. } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
  1487. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1488. if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
  1489. continue;
  1490. aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw,
  1491. vsi->seid,
  1492. allmulti,
  1493. f->vlan,
  1494. NULL);
  1495. aq_err = pf->hw.aq.asq_last_status;
  1496. if (aq_ret) {
  1497. dev_err(&pf->pdev->dev,
  1498. "Could not add VLAN %d to multicast promiscuous domain err %s aq_err %s\n",
  1499. f->vlan,
  1500. i40e_stat_str(&pf->hw, aq_ret),
  1501. i40e_aq_str(&pf->hw, aq_err));
  1502. break;
  1503. }
  1504. }
  1505. } else {
  1506. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
  1507. allmulti, NULL);
  1508. aq_err = pf->hw.aq.asq_last_status;
  1509. if (aq_ret) {
  1510. dev_err(&pf->pdev->dev,
  1511. "VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
  1512. vf->vf_id,
  1513. i40e_stat_str(&pf->hw, aq_ret),
  1514. i40e_aq_str(&pf->hw, aq_err));
  1515. goto error_param;
  1516. }
  1517. }
  1518. if (!aq_ret) {
  1519. dev_info(&pf->pdev->dev,
  1520. "VF %d successfully set multicast promiscuous mode\n",
  1521. vf->vf_id);
  1522. if (allmulti)
  1523. set_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  1524. else
  1525. clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  1526. }
  1527. if (info->flags & I40E_FLAG_VF_UNICAST_PROMISC)
  1528. alluni = true;
  1529. if (vf->port_vlan_id) {
  1530. aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
  1531. alluni,
  1532. vf->port_vlan_id,
  1533. NULL);
  1534. } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
  1535. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1536. aq_ret = 0;
  1537. if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID) {
  1538. aq_ret =
  1539. i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
  1540. vsi->seid,
  1541. alluni,
  1542. f->vlan,
  1543. NULL);
  1544. aq_err = pf->hw.aq.asq_last_status;
  1545. }
  1546. if (aq_ret)
  1547. dev_err(&pf->pdev->dev,
  1548. "Could not add VLAN %d to Unicast promiscuous domain err %s aq_err %s\n",
  1549. f->vlan,
  1550. i40e_stat_str(&pf->hw, aq_ret),
  1551. i40e_aq_str(&pf->hw, aq_err));
  1552. }
  1553. } else {
  1554. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid,
  1555. allmulti, NULL,
  1556. true);
  1557. aq_err = pf->hw.aq.asq_last_status;
  1558. if (aq_ret) {
  1559. dev_err(&pf->pdev->dev,
  1560. "VF %d failed to set unicast promiscuous mode %8.8x err %s aq_err %s\n",
  1561. vf->vf_id, info->flags,
  1562. i40e_stat_str(&pf->hw, aq_ret),
  1563. i40e_aq_str(&pf->hw, aq_err));
  1564. goto error_param;
  1565. }
  1566. }
  1567. if (!aq_ret) {
  1568. dev_info(&pf->pdev->dev,
  1569. "VF %d successfully set unicast promiscuous mode\n",
  1570. vf->vf_id);
  1571. if (alluni)
  1572. set_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  1573. else
  1574. clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  1575. }
  1576. error_param:
  1577. /* send the response to the VF */
  1578. return i40e_vc_send_resp_to_vf(vf,
  1579. I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
  1580. aq_ret);
  1581. }
  1582. /**
  1583. * i40e_vc_config_queues_msg
  1584. * @vf: pointer to the VF info
  1585. * @msg: pointer to the msg buffer
  1586. * @msglen: msg length
  1587. *
  1588. * called from the VF to configure the rx/tx
  1589. * queues
  1590. **/
  1591. static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1592. {
  1593. struct i40e_virtchnl_vsi_queue_config_info *qci =
  1594. (struct i40e_virtchnl_vsi_queue_config_info *)msg;
  1595. struct i40e_virtchnl_queue_pair_info *qpi;
  1596. struct i40e_pf *pf = vf->pf;
  1597. u16 vsi_id, vsi_queue_id;
  1598. i40e_status aq_ret = 0;
  1599. int i;
  1600. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1601. aq_ret = I40E_ERR_PARAM;
  1602. goto error_param;
  1603. }
  1604. vsi_id = qci->vsi_id;
  1605. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1606. aq_ret = I40E_ERR_PARAM;
  1607. goto error_param;
  1608. }
  1609. for (i = 0; i < qci->num_queue_pairs; i++) {
  1610. qpi = &qci->qpair[i];
  1611. vsi_queue_id = qpi->txq.queue_id;
  1612. if ((qpi->txq.vsi_id != vsi_id) ||
  1613. (qpi->rxq.vsi_id != vsi_id) ||
  1614. (qpi->rxq.queue_id != vsi_queue_id) ||
  1615. !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
  1616. aq_ret = I40E_ERR_PARAM;
  1617. goto error_param;
  1618. }
  1619. if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
  1620. &qpi->rxq) ||
  1621. i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
  1622. &qpi->txq)) {
  1623. aq_ret = I40E_ERR_PARAM;
  1624. goto error_param;
  1625. }
  1626. }
  1627. /* set vsi num_queue_pairs in use to num configured by VF */
  1628. pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
  1629. error_param:
  1630. /* send the response to the VF */
  1631. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
  1632. aq_ret);
  1633. }
  1634. /**
  1635. * i40e_vc_config_irq_map_msg
  1636. * @vf: pointer to the VF info
  1637. * @msg: pointer to the msg buffer
  1638. * @msglen: msg length
  1639. *
  1640. * called from the VF to configure the irq to
  1641. * queue map
  1642. **/
  1643. static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1644. {
  1645. struct i40e_virtchnl_irq_map_info *irqmap_info =
  1646. (struct i40e_virtchnl_irq_map_info *)msg;
  1647. struct i40e_virtchnl_vector_map *map;
  1648. u16 vsi_id, vsi_queue_id, vector_id;
  1649. i40e_status aq_ret = 0;
  1650. unsigned long tempmap;
  1651. int i;
  1652. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1653. aq_ret = I40E_ERR_PARAM;
  1654. goto error_param;
  1655. }
  1656. for (i = 0; i < irqmap_info->num_vectors; i++) {
  1657. map = &irqmap_info->vecmap[i];
  1658. vector_id = map->vector_id;
  1659. vsi_id = map->vsi_id;
  1660. /* validate msg params */
  1661. if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
  1662. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1663. aq_ret = I40E_ERR_PARAM;
  1664. goto error_param;
  1665. }
  1666. /* lookout for the invalid queue index */
  1667. tempmap = map->rxq_map;
  1668. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  1669. if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
  1670. vsi_queue_id)) {
  1671. aq_ret = I40E_ERR_PARAM;
  1672. goto error_param;
  1673. }
  1674. }
  1675. tempmap = map->txq_map;
  1676. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  1677. if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
  1678. vsi_queue_id)) {
  1679. aq_ret = I40E_ERR_PARAM;
  1680. goto error_param;
  1681. }
  1682. }
  1683. i40e_config_irq_link_list(vf, vsi_id, map);
  1684. }
  1685. error_param:
  1686. /* send the response to the VF */
  1687. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
  1688. aq_ret);
  1689. }
  1690. /**
  1691. * i40e_vc_enable_queues_msg
  1692. * @vf: pointer to the VF info
  1693. * @msg: pointer to the msg buffer
  1694. * @msglen: msg length
  1695. *
  1696. * called from the VF to enable all or specific queue(s)
  1697. **/
  1698. static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1699. {
  1700. struct i40e_virtchnl_queue_select *vqs =
  1701. (struct i40e_virtchnl_queue_select *)msg;
  1702. struct i40e_pf *pf = vf->pf;
  1703. u16 vsi_id = vqs->vsi_id;
  1704. i40e_status aq_ret = 0;
  1705. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1706. aq_ret = I40E_ERR_PARAM;
  1707. goto error_param;
  1708. }
  1709. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1710. aq_ret = I40E_ERR_PARAM;
  1711. goto error_param;
  1712. }
  1713. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1714. aq_ret = I40E_ERR_PARAM;
  1715. goto error_param;
  1716. }
  1717. if (i40e_vsi_start_rings(pf->vsi[vf->lan_vsi_idx]))
  1718. aq_ret = I40E_ERR_TIMEOUT;
  1719. error_param:
  1720. /* send the response to the VF */
  1721. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
  1722. aq_ret);
  1723. }
  1724. /**
  1725. * i40e_vc_disable_queues_msg
  1726. * @vf: pointer to the VF info
  1727. * @msg: pointer to the msg buffer
  1728. * @msglen: msg length
  1729. *
  1730. * called from the VF to disable all or specific
  1731. * queue(s)
  1732. **/
  1733. static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1734. {
  1735. struct i40e_virtchnl_queue_select *vqs =
  1736. (struct i40e_virtchnl_queue_select *)msg;
  1737. struct i40e_pf *pf = vf->pf;
  1738. i40e_status aq_ret = 0;
  1739. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1740. aq_ret = I40E_ERR_PARAM;
  1741. goto error_param;
  1742. }
  1743. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1744. aq_ret = I40E_ERR_PARAM;
  1745. goto error_param;
  1746. }
  1747. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1748. aq_ret = I40E_ERR_PARAM;
  1749. goto error_param;
  1750. }
  1751. i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
  1752. error_param:
  1753. /* send the response to the VF */
  1754. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
  1755. aq_ret);
  1756. }
  1757. /**
  1758. * i40e_vc_get_stats_msg
  1759. * @vf: pointer to the VF info
  1760. * @msg: pointer to the msg buffer
  1761. * @msglen: msg length
  1762. *
  1763. * called from the VF to get vsi stats
  1764. **/
  1765. static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1766. {
  1767. struct i40e_virtchnl_queue_select *vqs =
  1768. (struct i40e_virtchnl_queue_select *)msg;
  1769. struct i40e_pf *pf = vf->pf;
  1770. struct i40e_eth_stats stats;
  1771. i40e_status aq_ret = 0;
  1772. struct i40e_vsi *vsi;
  1773. memset(&stats, 0, sizeof(struct i40e_eth_stats));
  1774. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1775. aq_ret = I40E_ERR_PARAM;
  1776. goto error_param;
  1777. }
  1778. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1779. aq_ret = I40E_ERR_PARAM;
  1780. goto error_param;
  1781. }
  1782. vsi = pf->vsi[vf->lan_vsi_idx];
  1783. if (!vsi) {
  1784. aq_ret = I40E_ERR_PARAM;
  1785. goto error_param;
  1786. }
  1787. i40e_update_eth_stats(vsi);
  1788. stats = vsi->eth_stats;
  1789. error_param:
  1790. /* send the response back to the VF */
  1791. return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
  1792. (u8 *)&stats, sizeof(stats));
  1793. }
  1794. /* If the VF is not trusted restrict the number of MAC/VLAN it can program */
  1795. #define I40E_VC_MAX_MAC_ADDR_PER_VF 12
  1796. #define I40E_VC_MAX_VLAN_PER_VF 8
  1797. /**
  1798. * i40e_check_vf_permission
  1799. * @vf: pointer to the VF info
  1800. * @macaddr: pointer to the MAC Address being checked
  1801. *
  1802. * Check if the VF has permission to add or delete unicast MAC address
  1803. * filters and return error code -EPERM if not. Then check if the
  1804. * address filter requested is broadcast or zero and if so return
  1805. * an invalid MAC address error code.
  1806. **/
  1807. static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
  1808. {
  1809. struct i40e_pf *pf = vf->pf;
  1810. int ret = 0;
  1811. if (is_broadcast_ether_addr(macaddr) ||
  1812. is_zero_ether_addr(macaddr)) {
  1813. dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
  1814. ret = I40E_ERR_INVALID_MAC_ADDR;
  1815. } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
  1816. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
  1817. !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
  1818. /* If the host VMM administrator has set the VF MAC address
  1819. * administratively via the ndo_set_vf_mac command then deny
  1820. * permission to the VF to add or delete unicast MAC addresses.
  1821. * Unless the VF is privileged and then it can do whatever.
  1822. * The VF may request to set the MAC address filter already
  1823. * assigned to it so do not return an error in that case.
  1824. */
  1825. dev_err(&pf->pdev->dev,
  1826. "VF attempting to override administratively set MAC address, reload the VF driver to resume normal operation\n");
  1827. ret = -EPERM;
  1828. } else if ((vf->num_mac >= I40E_VC_MAX_MAC_ADDR_PER_VF) &&
  1829. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  1830. dev_err(&pf->pdev->dev,
  1831. "VF is not trusted, switch the VF to trusted to add more functionality\n");
  1832. ret = -EPERM;
  1833. }
  1834. return ret;
  1835. }
  1836. /**
  1837. * i40e_vc_add_mac_addr_msg
  1838. * @vf: pointer to the VF info
  1839. * @msg: pointer to the msg buffer
  1840. * @msglen: msg length
  1841. *
  1842. * add guest mac address filter
  1843. **/
  1844. static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1845. {
  1846. struct i40e_virtchnl_ether_addr_list *al =
  1847. (struct i40e_virtchnl_ether_addr_list *)msg;
  1848. struct i40e_pf *pf = vf->pf;
  1849. struct i40e_vsi *vsi = NULL;
  1850. u16 vsi_id = al->vsi_id;
  1851. i40e_status ret = 0;
  1852. int i;
  1853. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  1854. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1855. ret = I40E_ERR_PARAM;
  1856. goto error_param;
  1857. }
  1858. for (i = 0; i < al->num_elements; i++) {
  1859. ret = i40e_check_vf_permission(vf, al->list[i].addr);
  1860. if (ret)
  1861. goto error_param;
  1862. }
  1863. vsi = pf->vsi[vf->lan_vsi_idx];
  1864. /* Lock once, because all function inside for loop accesses VSI's
  1865. * MAC filter list which needs to be protected using same lock.
  1866. */
  1867. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1868. /* add new addresses to the list */
  1869. for (i = 0; i < al->num_elements; i++) {
  1870. struct i40e_mac_filter *f;
  1871. f = i40e_find_mac(vsi, al->list[i].addr);
  1872. if (!f)
  1873. f = i40e_add_mac_filter(vsi, al->list[i].addr);
  1874. if (!f) {
  1875. dev_err(&pf->pdev->dev,
  1876. "Unable to add MAC filter %pM for VF %d\n",
  1877. al->list[i].addr, vf->vf_id);
  1878. ret = I40E_ERR_PARAM;
  1879. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1880. goto error_param;
  1881. } else {
  1882. vf->num_mac++;
  1883. }
  1884. }
  1885. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1886. /* program the updated filter list */
  1887. ret = i40e_sync_vsi_filters(vsi);
  1888. if (ret)
  1889. dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
  1890. vf->vf_id, ret);
  1891. error_param:
  1892. /* send the response to the VF */
  1893. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
  1894. ret);
  1895. }
  1896. /**
  1897. * i40e_vc_del_mac_addr_msg
  1898. * @vf: pointer to the VF info
  1899. * @msg: pointer to the msg buffer
  1900. * @msglen: msg length
  1901. *
  1902. * remove guest mac address filter
  1903. **/
  1904. static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1905. {
  1906. struct i40e_virtchnl_ether_addr_list *al =
  1907. (struct i40e_virtchnl_ether_addr_list *)msg;
  1908. struct i40e_pf *pf = vf->pf;
  1909. struct i40e_vsi *vsi = NULL;
  1910. u16 vsi_id = al->vsi_id;
  1911. i40e_status ret = 0;
  1912. int i;
  1913. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  1914. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1915. ret = I40E_ERR_PARAM;
  1916. goto error_param;
  1917. }
  1918. for (i = 0; i < al->num_elements; i++) {
  1919. if (is_broadcast_ether_addr(al->list[i].addr) ||
  1920. is_zero_ether_addr(al->list[i].addr)) {
  1921. dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
  1922. al->list[i].addr, vf->vf_id);
  1923. ret = I40E_ERR_INVALID_MAC_ADDR;
  1924. goto error_param;
  1925. }
  1926. }
  1927. vsi = pf->vsi[vf->lan_vsi_idx];
  1928. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1929. /* delete addresses from the list */
  1930. for (i = 0; i < al->num_elements; i++)
  1931. if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
  1932. ret = I40E_ERR_INVALID_MAC_ADDR;
  1933. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1934. goto error_param;
  1935. } else {
  1936. vf->num_mac--;
  1937. }
  1938. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1939. /* program the updated filter list */
  1940. ret = i40e_sync_vsi_filters(vsi);
  1941. if (ret)
  1942. dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
  1943. vf->vf_id, ret);
  1944. error_param:
  1945. /* send the response to the VF */
  1946. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
  1947. ret);
  1948. }
  1949. /**
  1950. * i40e_vc_add_vlan_msg
  1951. * @vf: pointer to the VF info
  1952. * @msg: pointer to the msg buffer
  1953. * @msglen: msg length
  1954. *
  1955. * program guest vlan id
  1956. **/
  1957. static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1958. {
  1959. struct i40e_virtchnl_vlan_filter_list *vfl =
  1960. (struct i40e_virtchnl_vlan_filter_list *)msg;
  1961. struct i40e_pf *pf = vf->pf;
  1962. struct i40e_vsi *vsi = NULL;
  1963. u16 vsi_id = vfl->vsi_id;
  1964. i40e_status aq_ret = 0;
  1965. int i;
  1966. if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
  1967. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  1968. dev_err(&pf->pdev->dev,
  1969. "VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
  1970. goto error_param;
  1971. }
  1972. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  1973. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1974. aq_ret = I40E_ERR_PARAM;
  1975. goto error_param;
  1976. }
  1977. for (i = 0; i < vfl->num_elements; i++) {
  1978. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  1979. aq_ret = I40E_ERR_PARAM;
  1980. dev_err(&pf->pdev->dev,
  1981. "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
  1982. goto error_param;
  1983. }
  1984. }
  1985. vsi = pf->vsi[vf->lan_vsi_idx];
  1986. if (vsi->info.pvid) {
  1987. aq_ret = I40E_ERR_PARAM;
  1988. goto error_param;
  1989. }
  1990. i40e_vlan_stripping_enable(vsi);
  1991. for (i = 0; i < vfl->num_elements; i++) {
  1992. /* add new VLAN filter */
  1993. int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
  1994. if (!ret)
  1995. vf->num_vlan++;
  1996. if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
  1997. i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
  1998. true,
  1999. vfl->vlan_id[i],
  2000. NULL);
  2001. if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
  2002. i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
  2003. true,
  2004. vfl->vlan_id[i],
  2005. NULL);
  2006. if (ret)
  2007. dev_err(&pf->pdev->dev,
  2008. "Unable to add VLAN filter %d for VF %d, error %d\n",
  2009. vfl->vlan_id[i], vf->vf_id, ret);
  2010. }
  2011. error_param:
  2012. /* send the response to the VF */
  2013. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
  2014. }
  2015. /**
  2016. * i40e_vc_remove_vlan_msg
  2017. * @vf: pointer to the VF info
  2018. * @msg: pointer to the msg buffer
  2019. * @msglen: msg length
  2020. *
  2021. * remove programmed guest vlan id
  2022. **/
  2023. static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2024. {
  2025. struct i40e_virtchnl_vlan_filter_list *vfl =
  2026. (struct i40e_virtchnl_vlan_filter_list *)msg;
  2027. struct i40e_pf *pf = vf->pf;
  2028. struct i40e_vsi *vsi = NULL;
  2029. u16 vsi_id = vfl->vsi_id;
  2030. i40e_status aq_ret = 0;
  2031. int i;
  2032. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2033. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2034. aq_ret = I40E_ERR_PARAM;
  2035. goto error_param;
  2036. }
  2037. for (i = 0; i < vfl->num_elements; i++) {
  2038. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  2039. aq_ret = I40E_ERR_PARAM;
  2040. goto error_param;
  2041. }
  2042. }
  2043. vsi = pf->vsi[vf->lan_vsi_idx];
  2044. if (vsi->info.pvid) {
  2045. aq_ret = I40E_ERR_PARAM;
  2046. goto error_param;
  2047. }
  2048. for (i = 0; i < vfl->num_elements; i++) {
  2049. i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
  2050. vf->num_vlan--;
  2051. if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
  2052. i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
  2053. false,
  2054. vfl->vlan_id[i],
  2055. NULL);
  2056. if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
  2057. i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
  2058. false,
  2059. vfl->vlan_id[i],
  2060. NULL);
  2061. }
  2062. error_param:
  2063. /* send the response to the VF */
  2064. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
  2065. }
  2066. /**
  2067. * i40e_vc_iwarp_msg
  2068. * @vf: pointer to the VF info
  2069. * @msg: pointer to the msg buffer
  2070. * @msglen: msg length
  2071. *
  2072. * called from the VF for the iwarp msgs
  2073. **/
  2074. static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2075. {
  2076. struct i40e_pf *pf = vf->pf;
  2077. int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
  2078. i40e_status aq_ret = 0;
  2079. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2080. !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
  2081. aq_ret = I40E_ERR_PARAM;
  2082. goto error_param;
  2083. }
  2084. i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
  2085. msg, msglen);
  2086. error_param:
  2087. /* send the response to the VF */
  2088. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_IWARP,
  2089. aq_ret);
  2090. }
  2091. /**
  2092. * i40e_vc_iwarp_qvmap_msg
  2093. * @vf: pointer to the VF info
  2094. * @msg: pointer to the msg buffer
  2095. * @msglen: msg length
  2096. * @config: config qvmap or release it
  2097. *
  2098. * called from the VF for the iwarp msgs
  2099. **/
  2100. static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, u16 msglen,
  2101. bool config)
  2102. {
  2103. struct i40e_virtchnl_iwarp_qvlist_info *qvlist_info =
  2104. (struct i40e_virtchnl_iwarp_qvlist_info *)msg;
  2105. i40e_status aq_ret = 0;
  2106. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2107. !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
  2108. aq_ret = I40E_ERR_PARAM;
  2109. goto error_param;
  2110. }
  2111. if (config) {
  2112. if (i40e_config_iwarp_qvlist(vf, qvlist_info))
  2113. aq_ret = I40E_ERR_PARAM;
  2114. } else {
  2115. i40e_release_iwarp_qvlist(vf);
  2116. }
  2117. error_param:
  2118. /* send the response to the VF */
  2119. return i40e_vc_send_resp_to_vf(vf,
  2120. config ? I40E_VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
  2121. I40E_VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
  2122. aq_ret);
  2123. }
  2124. /**
  2125. * i40e_vc_config_rss_key
  2126. * @vf: pointer to the VF info
  2127. * @msg: pointer to the msg buffer
  2128. * @msglen: msg length
  2129. *
  2130. * Configure the VF's RSS key
  2131. **/
  2132. static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2133. {
  2134. struct i40e_virtchnl_rss_key *vrk =
  2135. (struct i40e_virtchnl_rss_key *)msg;
  2136. struct i40e_pf *pf = vf->pf;
  2137. struct i40e_vsi *vsi = NULL;
  2138. u16 vsi_id = vrk->vsi_id;
  2139. i40e_status aq_ret = 0;
  2140. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2141. !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
  2142. (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
  2143. aq_ret = I40E_ERR_PARAM;
  2144. goto err;
  2145. }
  2146. vsi = pf->vsi[vf->lan_vsi_idx];
  2147. aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
  2148. err:
  2149. /* send the response to the VF */
  2150. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_RSS_KEY,
  2151. aq_ret);
  2152. }
  2153. /**
  2154. * i40e_vc_config_rss_lut
  2155. * @vf: pointer to the VF info
  2156. * @msg: pointer to the msg buffer
  2157. * @msglen: msg length
  2158. *
  2159. * Configure the VF's RSS LUT
  2160. **/
  2161. static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2162. {
  2163. struct i40e_virtchnl_rss_lut *vrl =
  2164. (struct i40e_virtchnl_rss_lut *)msg;
  2165. struct i40e_pf *pf = vf->pf;
  2166. struct i40e_vsi *vsi = NULL;
  2167. u16 vsi_id = vrl->vsi_id;
  2168. i40e_status aq_ret = 0;
  2169. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2170. !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
  2171. (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
  2172. aq_ret = I40E_ERR_PARAM;
  2173. goto err;
  2174. }
  2175. vsi = pf->vsi[vf->lan_vsi_idx];
  2176. aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
  2177. /* send the response to the VF */
  2178. err:
  2179. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_RSS_LUT,
  2180. aq_ret);
  2181. }
  2182. /**
  2183. * i40e_vc_get_rss_hena
  2184. * @vf: pointer to the VF info
  2185. * @msg: pointer to the msg buffer
  2186. * @msglen: msg length
  2187. *
  2188. * Return the RSS HENA bits allowed by the hardware
  2189. **/
  2190. static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2191. {
  2192. struct i40e_virtchnl_rss_hena *vrh = NULL;
  2193. struct i40e_pf *pf = vf->pf;
  2194. i40e_status aq_ret = 0;
  2195. int len = 0;
  2196. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2197. aq_ret = I40E_ERR_PARAM;
  2198. goto err;
  2199. }
  2200. len = sizeof(struct i40e_virtchnl_rss_hena);
  2201. vrh = kzalloc(len, GFP_KERNEL);
  2202. if (!vrh) {
  2203. aq_ret = I40E_ERR_NO_MEMORY;
  2204. len = 0;
  2205. goto err;
  2206. }
  2207. vrh->hena = i40e_pf_get_default_rss_hena(pf);
  2208. err:
  2209. /* send the response back to the VF */
  2210. aq_ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_RSS_HENA_CAPS,
  2211. aq_ret, (u8 *)vrh, len);
  2212. kfree(vrh);
  2213. return aq_ret;
  2214. }
  2215. /**
  2216. * i40e_vc_set_rss_hena
  2217. * @vf: pointer to the VF info
  2218. * @msg: pointer to the msg buffer
  2219. * @msglen: msg length
  2220. *
  2221. * Set the RSS HENA bits for the VF
  2222. **/
  2223. static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2224. {
  2225. struct i40e_virtchnl_rss_hena *vrh =
  2226. (struct i40e_virtchnl_rss_hena *)msg;
  2227. struct i40e_pf *pf = vf->pf;
  2228. struct i40e_hw *hw = &pf->hw;
  2229. i40e_status aq_ret = 0;
  2230. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2231. aq_ret = I40E_ERR_PARAM;
  2232. goto err;
  2233. }
  2234. i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
  2235. i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
  2236. (u32)(vrh->hena >> 32));
  2237. /* send the response to the VF */
  2238. err:
  2239. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_SET_RSS_HENA,
  2240. aq_ret);
  2241. }
  2242. /**
  2243. * i40e_vc_validate_vf_msg
  2244. * @vf: pointer to the VF info
  2245. * @msg: pointer to the msg buffer
  2246. * @msglen: msg length
  2247. * @msghndl: msg handle
  2248. *
  2249. * validate msg
  2250. **/
  2251. static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
  2252. u32 v_retval, u8 *msg, u16 msglen)
  2253. {
  2254. bool err_msg_format = false;
  2255. int valid_len = 0;
  2256. /* Check if VF is disabled. */
  2257. if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
  2258. return I40E_ERR_PARAM;
  2259. /* Validate message length. */
  2260. switch (v_opcode) {
  2261. case I40E_VIRTCHNL_OP_VERSION:
  2262. valid_len = sizeof(struct i40e_virtchnl_version_info);
  2263. break;
  2264. case I40E_VIRTCHNL_OP_RESET_VF:
  2265. break;
  2266. case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
  2267. if (VF_IS_V11(vf))
  2268. valid_len = sizeof(u32);
  2269. break;
  2270. case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
  2271. valid_len = sizeof(struct i40e_virtchnl_txq_info);
  2272. break;
  2273. case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
  2274. valid_len = sizeof(struct i40e_virtchnl_rxq_info);
  2275. break;
  2276. case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  2277. valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
  2278. if (msglen >= valid_len) {
  2279. struct i40e_virtchnl_vsi_queue_config_info *vqc =
  2280. (struct i40e_virtchnl_vsi_queue_config_info *)msg;
  2281. valid_len += (vqc->num_queue_pairs *
  2282. sizeof(struct
  2283. i40e_virtchnl_queue_pair_info));
  2284. if (vqc->num_queue_pairs == 0)
  2285. err_msg_format = true;
  2286. }
  2287. break;
  2288. case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
  2289. valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
  2290. if (msglen >= valid_len) {
  2291. struct i40e_virtchnl_irq_map_info *vimi =
  2292. (struct i40e_virtchnl_irq_map_info *)msg;
  2293. valid_len += (vimi->num_vectors *
  2294. sizeof(struct i40e_virtchnl_vector_map));
  2295. if (vimi->num_vectors == 0)
  2296. err_msg_format = true;
  2297. }
  2298. break;
  2299. case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
  2300. case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
  2301. valid_len = sizeof(struct i40e_virtchnl_queue_select);
  2302. break;
  2303. case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
  2304. case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
  2305. valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
  2306. if (msglen >= valid_len) {
  2307. struct i40e_virtchnl_ether_addr_list *veal =
  2308. (struct i40e_virtchnl_ether_addr_list *)msg;
  2309. valid_len += veal->num_elements *
  2310. sizeof(struct i40e_virtchnl_ether_addr);
  2311. if (veal->num_elements == 0)
  2312. err_msg_format = true;
  2313. }
  2314. break;
  2315. case I40E_VIRTCHNL_OP_ADD_VLAN:
  2316. case I40E_VIRTCHNL_OP_DEL_VLAN:
  2317. valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
  2318. if (msglen >= valid_len) {
  2319. struct i40e_virtchnl_vlan_filter_list *vfl =
  2320. (struct i40e_virtchnl_vlan_filter_list *)msg;
  2321. valid_len += vfl->num_elements * sizeof(u16);
  2322. if (vfl->num_elements == 0)
  2323. err_msg_format = true;
  2324. }
  2325. break;
  2326. case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
  2327. valid_len = sizeof(struct i40e_virtchnl_promisc_info);
  2328. break;
  2329. case I40E_VIRTCHNL_OP_GET_STATS:
  2330. valid_len = sizeof(struct i40e_virtchnl_queue_select);
  2331. break;
  2332. case I40E_VIRTCHNL_OP_IWARP:
  2333. /* These messages are opaque to us and will be validated in
  2334. * the RDMA client code. We just need to check for nonzero
  2335. * length. The firmware will enforce max length restrictions.
  2336. */
  2337. if (msglen)
  2338. valid_len = msglen;
  2339. else
  2340. err_msg_format = true;
  2341. break;
  2342. case I40E_VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
  2343. valid_len = 0;
  2344. break;
  2345. case I40E_VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
  2346. valid_len = sizeof(struct i40e_virtchnl_iwarp_qvlist_info);
  2347. if (msglen >= valid_len) {
  2348. struct i40e_virtchnl_iwarp_qvlist_info *qv =
  2349. (struct i40e_virtchnl_iwarp_qvlist_info *)msg;
  2350. if (qv->num_vectors == 0) {
  2351. err_msg_format = true;
  2352. break;
  2353. }
  2354. valid_len += ((qv->num_vectors - 1) *
  2355. sizeof(struct i40e_virtchnl_iwarp_qv_info));
  2356. }
  2357. break;
  2358. case I40E_VIRTCHNL_OP_CONFIG_RSS_KEY:
  2359. valid_len = sizeof(struct i40e_virtchnl_rss_key);
  2360. if (msglen >= valid_len) {
  2361. struct i40e_virtchnl_rss_key *vrk =
  2362. (struct i40e_virtchnl_rss_key *)msg;
  2363. if (vrk->key_len != I40E_HKEY_ARRAY_SIZE) {
  2364. err_msg_format = true;
  2365. break;
  2366. }
  2367. valid_len += vrk->key_len - 1;
  2368. }
  2369. break;
  2370. case I40E_VIRTCHNL_OP_CONFIG_RSS_LUT:
  2371. valid_len = sizeof(struct i40e_virtchnl_rss_lut);
  2372. if (msglen >= valid_len) {
  2373. struct i40e_virtchnl_rss_lut *vrl =
  2374. (struct i40e_virtchnl_rss_lut *)msg;
  2375. if (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE) {
  2376. err_msg_format = true;
  2377. break;
  2378. }
  2379. valid_len += vrl->lut_entries - 1;
  2380. }
  2381. break;
  2382. case I40E_VIRTCHNL_OP_GET_RSS_HENA_CAPS:
  2383. break;
  2384. case I40E_VIRTCHNL_OP_SET_RSS_HENA:
  2385. valid_len = sizeof(struct i40e_virtchnl_rss_hena);
  2386. break;
  2387. /* These are always errors coming from the VF. */
  2388. case I40E_VIRTCHNL_OP_EVENT:
  2389. case I40E_VIRTCHNL_OP_UNKNOWN:
  2390. default:
  2391. return -EPERM;
  2392. }
  2393. /* few more checks */
  2394. if ((valid_len != msglen) || (err_msg_format)) {
  2395. i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
  2396. return -EINVAL;
  2397. } else {
  2398. return 0;
  2399. }
  2400. }
  2401. /**
  2402. * i40e_vc_process_vf_msg
  2403. * @pf: pointer to the PF structure
  2404. * @vf_id: source VF id
  2405. * @msg: pointer to the msg buffer
  2406. * @msglen: msg length
  2407. * @msghndl: msg handle
  2408. *
  2409. * called from the common aeq/arq handler to
  2410. * process request from VF
  2411. **/
  2412. int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
  2413. u32 v_retval, u8 *msg, u16 msglen)
  2414. {
  2415. struct i40e_hw *hw = &pf->hw;
  2416. int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
  2417. struct i40e_vf *vf;
  2418. int ret;
  2419. pf->vf_aq_requests++;
  2420. if (local_vf_id >= pf->num_alloc_vfs)
  2421. return -EINVAL;
  2422. vf = &(pf->vf[local_vf_id]);
  2423. /* perform basic checks on the msg */
  2424. ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
  2425. if (ret) {
  2426. dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
  2427. local_vf_id, v_opcode, msglen);
  2428. return ret;
  2429. }
  2430. switch (v_opcode) {
  2431. case I40E_VIRTCHNL_OP_VERSION:
  2432. ret = i40e_vc_get_version_msg(vf, msg);
  2433. break;
  2434. case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
  2435. ret = i40e_vc_get_vf_resources_msg(vf, msg);
  2436. break;
  2437. case I40E_VIRTCHNL_OP_RESET_VF:
  2438. i40e_vc_reset_vf_msg(vf);
  2439. ret = 0;
  2440. break;
  2441. case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
  2442. ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
  2443. break;
  2444. case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  2445. ret = i40e_vc_config_queues_msg(vf, msg, msglen);
  2446. break;
  2447. case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
  2448. ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
  2449. break;
  2450. case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
  2451. ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
  2452. i40e_vc_notify_vf_link_state(vf);
  2453. break;
  2454. case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
  2455. ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
  2456. break;
  2457. case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
  2458. ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
  2459. break;
  2460. case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
  2461. ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
  2462. break;
  2463. case I40E_VIRTCHNL_OP_ADD_VLAN:
  2464. ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
  2465. break;
  2466. case I40E_VIRTCHNL_OP_DEL_VLAN:
  2467. ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
  2468. break;
  2469. case I40E_VIRTCHNL_OP_GET_STATS:
  2470. ret = i40e_vc_get_stats_msg(vf, msg, msglen);
  2471. break;
  2472. case I40E_VIRTCHNL_OP_IWARP:
  2473. ret = i40e_vc_iwarp_msg(vf, msg, msglen);
  2474. break;
  2475. case I40E_VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
  2476. ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, true);
  2477. break;
  2478. case I40E_VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
  2479. ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, false);
  2480. break;
  2481. case I40E_VIRTCHNL_OP_CONFIG_RSS_KEY:
  2482. ret = i40e_vc_config_rss_key(vf, msg, msglen);
  2483. break;
  2484. case I40E_VIRTCHNL_OP_CONFIG_RSS_LUT:
  2485. ret = i40e_vc_config_rss_lut(vf, msg, msglen);
  2486. break;
  2487. case I40E_VIRTCHNL_OP_GET_RSS_HENA_CAPS:
  2488. ret = i40e_vc_get_rss_hena(vf, msg, msglen);
  2489. break;
  2490. case I40E_VIRTCHNL_OP_SET_RSS_HENA:
  2491. ret = i40e_vc_set_rss_hena(vf, msg, msglen);
  2492. break;
  2493. case I40E_VIRTCHNL_OP_UNKNOWN:
  2494. default:
  2495. dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
  2496. v_opcode, local_vf_id);
  2497. ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
  2498. I40E_ERR_NOT_IMPLEMENTED);
  2499. break;
  2500. }
  2501. return ret;
  2502. }
  2503. /**
  2504. * i40e_vc_process_vflr_event
  2505. * @pf: pointer to the PF structure
  2506. *
  2507. * called from the vlfr irq handler to
  2508. * free up VF resources and state variables
  2509. **/
  2510. int i40e_vc_process_vflr_event(struct i40e_pf *pf)
  2511. {
  2512. struct i40e_hw *hw = &pf->hw;
  2513. u32 reg, reg_idx, bit_idx;
  2514. struct i40e_vf *vf;
  2515. int vf_id;
  2516. if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  2517. return 0;
  2518. /* Re-enable the VFLR interrupt cause here, before looking for which
  2519. * VF got reset. Otherwise, if another VF gets a reset while the
  2520. * first one is being processed, that interrupt will be lost, and
  2521. * that VF will be stuck in reset forever.
  2522. */
  2523. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  2524. reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
  2525. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  2526. i40e_flush(hw);
  2527. clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  2528. for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
  2529. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  2530. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  2531. /* read GLGEN_VFLRSTAT register to find out the flr VFs */
  2532. vf = &pf->vf[vf_id];
  2533. reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
  2534. if (reg & BIT(bit_idx))
  2535. /* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
  2536. i40e_reset_vf(vf, true);
  2537. }
  2538. return 0;
  2539. }
  2540. /**
  2541. * i40e_ndo_set_vf_mac
  2542. * @netdev: network interface device structure
  2543. * @vf_id: VF identifier
  2544. * @mac: mac address
  2545. *
  2546. * program VF mac address
  2547. **/
  2548. int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
  2549. {
  2550. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2551. struct i40e_vsi *vsi = np->vsi;
  2552. struct i40e_pf *pf = vsi->back;
  2553. struct i40e_mac_filter *f;
  2554. struct i40e_vf *vf;
  2555. int ret = 0;
  2556. int bkt;
  2557. /* validate the request */
  2558. if (vf_id >= pf->num_alloc_vfs) {
  2559. dev_err(&pf->pdev->dev,
  2560. "Invalid VF Identifier %d\n", vf_id);
  2561. ret = -EINVAL;
  2562. goto error_param;
  2563. }
  2564. vf = &(pf->vf[vf_id]);
  2565. vsi = pf->vsi[vf->lan_vsi_idx];
  2566. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  2567. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  2568. vf_id);
  2569. ret = -EAGAIN;
  2570. goto error_param;
  2571. }
  2572. if (is_multicast_ether_addr(mac)) {
  2573. dev_err(&pf->pdev->dev,
  2574. "Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
  2575. ret = -EINVAL;
  2576. goto error_param;
  2577. }
  2578. /* Lock once because below invoked function add/del_filter requires
  2579. * mac_filter_hash_lock to be held
  2580. */
  2581. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2582. /* delete the temporary mac address */
  2583. if (!is_zero_ether_addr(vf->default_lan_addr.addr))
  2584. i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
  2585. /* Delete all the filters for this VSI - we're going to kill it
  2586. * anyway.
  2587. */
  2588. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist)
  2589. __i40e_del_filter(vsi, f);
  2590. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2591. dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
  2592. /* program mac filter */
  2593. if (i40e_sync_vsi_filters(vsi)) {
  2594. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  2595. ret = -EIO;
  2596. goto error_param;
  2597. }
  2598. ether_addr_copy(vf->default_lan_addr.addr, mac);
  2599. vf->pf_set_mac = true;
  2600. /* Force the VF driver stop so it has to reload with new MAC address */
  2601. i40e_vc_disable_vf(pf, vf);
  2602. dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
  2603. error_param:
  2604. return ret;
  2605. }
  2606. /**
  2607. * i40e_ndo_set_vf_port_vlan
  2608. * @netdev: network interface device structure
  2609. * @vf_id: VF identifier
  2610. * @vlan_id: mac address
  2611. * @qos: priority setting
  2612. * @vlan_proto: vlan protocol
  2613. *
  2614. * program VF vlan id and/or qos
  2615. **/
  2616. int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
  2617. u16 vlan_id, u8 qos, __be16 vlan_proto)
  2618. {
  2619. u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
  2620. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2621. struct i40e_pf *pf = np->vsi->back;
  2622. struct i40e_vsi *vsi;
  2623. struct i40e_vf *vf;
  2624. int ret = 0;
  2625. /* validate the request */
  2626. if (vf_id >= pf->num_alloc_vfs) {
  2627. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2628. ret = -EINVAL;
  2629. goto error_pvid;
  2630. }
  2631. if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
  2632. dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
  2633. ret = -EINVAL;
  2634. goto error_pvid;
  2635. }
  2636. if (vlan_proto != htons(ETH_P_8021Q)) {
  2637. dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
  2638. ret = -EPROTONOSUPPORT;
  2639. goto error_pvid;
  2640. }
  2641. vf = &(pf->vf[vf_id]);
  2642. vsi = pf->vsi[vf->lan_vsi_idx];
  2643. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  2644. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  2645. vf_id);
  2646. ret = -EAGAIN;
  2647. goto error_pvid;
  2648. }
  2649. if (le16_to_cpu(vsi->info.pvid) == vlanprio)
  2650. /* duplicate request, so just return success */
  2651. goto error_pvid;
  2652. /* Locked once because multiple functions below iterate list */
  2653. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2654. if (le16_to_cpu(vsi->info.pvid) == 0 && i40e_is_vsi_in_vlan(vsi)) {
  2655. dev_err(&pf->pdev->dev,
  2656. "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",
  2657. vf_id);
  2658. /* Administrator Error - knock the VF offline until he does
  2659. * the right thing by reconfiguring his network correctly
  2660. * and then reloading the VF driver.
  2661. */
  2662. i40e_vc_disable_vf(pf, vf);
  2663. /* During reset the VF got a new VSI, so refresh the pointer. */
  2664. vsi = pf->vsi[vf->lan_vsi_idx];
  2665. }
  2666. /* Check for condition where there was already a port VLAN ID
  2667. * filter set and now it is being deleted by setting it to zero.
  2668. * Additionally check for the condition where there was a port
  2669. * VLAN but now there is a new and different port VLAN being set.
  2670. * Before deleting all the old VLAN filters we must add new ones
  2671. * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
  2672. * MAC addresses deleted.
  2673. */
  2674. if ((!(vlan_id || qos) ||
  2675. vlanprio != le16_to_cpu(vsi->info.pvid)) &&
  2676. vsi->info.pvid) {
  2677. ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
  2678. if (ret) {
  2679. dev_info(&vsi->back->pdev->dev,
  2680. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  2681. vsi->back->hw.aq.asq_last_status);
  2682. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2683. goto error_pvid;
  2684. }
  2685. }
  2686. if (vsi->info.pvid) {
  2687. /* remove all filters on the old VLAN */
  2688. i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
  2689. VLAN_VID_MASK));
  2690. }
  2691. if (vlan_id || qos)
  2692. ret = i40e_vsi_add_pvid(vsi, vlanprio);
  2693. else
  2694. i40e_vsi_remove_pvid(vsi);
  2695. if (vlan_id) {
  2696. dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
  2697. vlan_id, qos, vf_id);
  2698. /* add new VLAN filter for each MAC */
  2699. ret = i40e_add_vlan_all_mac(vsi, vlan_id);
  2700. if (ret) {
  2701. dev_info(&vsi->back->pdev->dev,
  2702. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  2703. vsi->back->hw.aq.asq_last_status);
  2704. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2705. goto error_pvid;
  2706. }
  2707. /* remove the previously added non-VLAN MAC filters */
  2708. i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
  2709. }
  2710. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2711. /* Schedule the worker thread to take care of applying changes */
  2712. i40e_service_event_schedule(vsi->back);
  2713. if (ret) {
  2714. dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
  2715. goto error_pvid;
  2716. }
  2717. /* The Port VLAN needs to be saved across resets the same as the
  2718. * default LAN MAC address.
  2719. */
  2720. vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
  2721. ret = 0;
  2722. error_pvid:
  2723. return ret;
  2724. }
  2725. #define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
  2726. #define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
  2727. /**
  2728. * i40e_ndo_set_vf_bw
  2729. * @netdev: network interface device structure
  2730. * @vf_id: VF identifier
  2731. * @tx_rate: Tx rate
  2732. *
  2733. * configure VF Tx rate
  2734. **/
  2735. int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
  2736. int max_tx_rate)
  2737. {
  2738. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2739. struct i40e_pf *pf = np->vsi->back;
  2740. struct i40e_vsi *vsi;
  2741. struct i40e_vf *vf;
  2742. int speed = 0;
  2743. int ret = 0;
  2744. /* validate the request */
  2745. if (vf_id >= pf->num_alloc_vfs) {
  2746. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
  2747. ret = -EINVAL;
  2748. goto error;
  2749. }
  2750. if (min_tx_rate) {
  2751. dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
  2752. min_tx_rate, vf_id);
  2753. return -EINVAL;
  2754. }
  2755. vf = &(pf->vf[vf_id]);
  2756. vsi = pf->vsi[vf->lan_vsi_idx];
  2757. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  2758. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  2759. vf_id);
  2760. ret = -EAGAIN;
  2761. goto error;
  2762. }
  2763. switch (pf->hw.phy.link_info.link_speed) {
  2764. case I40E_LINK_SPEED_40GB:
  2765. speed = 40000;
  2766. break;
  2767. case I40E_LINK_SPEED_25GB:
  2768. speed = 25000;
  2769. break;
  2770. case I40E_LINK_SPEED_20GB:
  2771. speed = 20000;
  2772. break;
  2773. case I40E_LINK_SPEED_10GB:
  2774. speed = 10000;
  2775. break;
  2776. case I40E_LINK_SPEED_1GB:
  2777. speed = 1000;
  2778. break;
  2779. default:
  2780. break;
  2781. }
  2782. if (max_tx_rate > speed) {
  2783. dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.\n",
  2784. max_tx_rate, vf->vf_id);
  2785. ret = -EINVAL;
  2786. goto error;
  2787. }
  2788. if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
  2789. dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
  2790. max_tx_rate = 50;
  2791. }
  2792. /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
  2793. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
  2794. max_tx_rate / I40E_BW_CREDIT_DIVISOR,
  2795. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  2796. if (ret) {
  2797. dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
  2798. ret);
  2799. ret = -EIO;
  2800. goto error;
  2801. }
  2802. vf->tx_rate = max_tx_rate;
  2803. error:
  2804. return ret;
  2805. }
  2806. /**
  2807. * i40e_ndo_get_vf_config
  2808. * @netdev: network interface device structure
  2809. * @vf_id: VF identifier
  2810. * @ivi: VF configuration structure
  2811. *
  2812. * return VF configuration
  2813. **/
  2814. int i40e_ndo_get_vf_config(struct net_device *netdev,
  2815. int vf_id, struct ifla_vf_info *ivi)
  2816. {
  2817. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2818. struct i40e_vsi *vsi = np->vsi;
  2819. struct i40e_pf *pf = vsi->back;
  2820. struct i40e_vf *vf;
  2821. int ret = 0;
  2822. /* validate the request */
  2823. if (vf_id >= pf->num_alloc_vfs) {
  2824. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2825. ret = -EINVAL;
  2826. goto error_param;
  2827. }
  2828. vf = &(pf->vf[vf_id]);
  2829. /* first vsi is always the LAN vsi */
  2830. vsi = pf->vsi[vf->lan_vsi_idx];
  2831. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  2832. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  2833. vf_id);
  2834. ret = -EAGAIN;
  2835. goto error_param;
  2836. }
  2837. ivi->vf = vf_id;
  2838. ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
  2839. ivi->max_tx_rate = vf->tx_rate;
  2840. ivi->min_tx_rate = 0;
  2841. ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
  2842. ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
  2843. I40E_VLAN_PRIORITY_SHIFT;
  2844. if (vf->link_forced == false)
  2845. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  2846. else if (vf->link_up == true)
  2847. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  2848. else
  2849. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  2850. ivi->spoofchk = vf->spoofchk;
  2851. ivi->trusted = vf->trusted;
  2852. ret = 0;
  2853. error_param:
  2854. return ret;
  2855. }
  2856. /**
  2857. * i40e_ndo_set_vf_link_state
  2858. * @netdev: network interface device structure
  2859. * @vf_id: VF identifier
  2860. * @link: required link state
  2861. *
  2862. * Set the link state of a specified VF, regardless of physical link state
  2863. **/
  2864. int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
  2865. {
  2866. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2867. struct i40e_pf *pf = np->vsi->back;
  2868. struct i40e_virtchnl_pf_event pfe;
  2869. struct i40e_hw *hw = &pf->hw;
  2870. struct i40e_vf *vf;
  2871. int abs_vf_id;
  2872. int ret = 0;
  2873. /* validate the request */
  2874. if (vf_id >= pf->num_alloc_vfs) {
  2875. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2876. ret = -EINVAL;
  2877. goto error_out;
  2878. }
  2879. vf = &pf->vf[vf_id];
  2880. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  2881. pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
  2882. pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
  2883. switch (link) {
  2884. case IFLA_VF_LINK_STATE_AUTO:
  2885. vf->link_forced = false;
  2886. pfe.event_data.link_event.link_status =
  2887. pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
  2888. pfe.event_data.link_event.link_speed =
  2889. pf->hw.phy.link_info.link_speed;
  2890. break;
  2891. case IFLA_VF_LINK_STATE_ENABLE:
  2892. vf->link_forced = true;
  2893. vf->link_up = true;
  2894. pfe.event_data.link_event.link_status = true;
  2895. pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
  2896. break;
  2897. case IFLA_VF_LINK_STATE_DISABLE:
  2898. vf->link_forced = true;
  2899. vf->link_up = false;
  2900. pfe.event_data.link_event.link_status = false;
  2901. pfe.event_data.link_event.link_speed = 0;
  2902. break;
  2903. default:
  2904. ret = -EINVAL;
  2905. goto error_out;
  2906. }
  2907. /* Notify the VF of its new link state */
  2908. i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
  2909. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  2910. error_out:
  2911. return ret;
  2912. }
  2913. /**
  2914. * i40e_ndo_set_vf_spoofchk
  2915. * @netdev: network interface device structure
  2916. * @vf_id: VF identifier
  2917. * @enable: flag to enable or disable feature
  2918. *
  2919. * Enable or disable VF spoof checking
  2920. **/
  2921. int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
  2922. {
  2923. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2924. struct i40e_vsi *vsi = np->vsi;
  2925. struct i40e_pf *pf = vsi->back;
  2926. struct i40e_vsi_context ctxt;
  2927. struct i40e_hw *hw = &pf->hw;
  2928. struct i40e_vf *vf;
  2929. int ret = 0;
  2930. /* validate the request */
  2931. if (vf_id >= pf->num_alloc_vfs) {
  2932. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2933. ret = -EINVAL;
  2934. goto out;
  2935. }
  2936. vf = &(pf->vf[vf_id]);
  2937. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  2938. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  2939. vf_id);
  2940. ret = -EAGAIN;
  2941. goto out;
  2942. }
  2943. if (enable == vf->spoofchk)
  2944. goto out;
  2945. vf->spoofchk = enable;
  2946. memset(&ctxt, 0, sizeof(ctxt));
  2947. ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
  2948. ctxt.pf_num = pf->hw.pf_id;
  2949. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  2950. if (enable)
  2951. ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  2952. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  2953. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  2954. if (ret) {
  2955. dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
  2956. ret);
  2957. ret = -EIO;
  2958. }
  2959. out:
  2960. return ret;
  2961. }
  2962. /**
  2963. * i40e_ndo_set_vf_trust
  2964. * @netdev: network interface device structure of the pf
  2965. * @vf_id: VF identifier
  2966. * @setting: trust setting
  2967. *
  2968. * Enable or disable VF trust setting
  2969. **/
  2970. int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
  2971. {
  2972. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2973. struct i40e_pf *pf = np->vsi->back;
  2974. struct i40e_vf *vf;
  2975. int ret = 0;
  2976. /* validate the request */
  2977. if (vf_id >= pf->num_alloc_vfs) {
  2978. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2979. return -EINVAL;
  2980. }
  2981. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  2982. dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
  2983. return -EINVAL;
  2984. }
  2985. vf = &pf->vf[vf_id];
  2986. if (!vf)
  2987. return -EINVAL;
  2988. if (setting == vf->trusted)
  2989. goto out;
  2990. vf->trusted = setting;
  2991. i40e_vc_notify_vf_reset(vf);
  2992. i40e_reset_vf(vf, false);
  2993. dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
  2994. vf_id, setting ? "" : "un");
  2995. out:
  2996. return ret;
  2997. }