i40e_virtchnl_pf.c 87 KB

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