i40e_virtchnl_pf.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2015 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include "i40e.h"
  27. /*********************notification routines***********************/
  28. /**
  29. * i40e_vc_vf_broadcast
  30. * @pf: pointer to the PF structure
  31. * @opcode: operation code
  32. * @retval: return value
  33. * @msg: pointer to the msg buffer
  34. * @msglen: msg length
  35. *
  36. * send a message to all VFs on a given PF
  37. **/
  38. static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
  39. enum i40e_virtchnl_ops v_opcode,
  40. i40e_status v_retval, u8 *msg,
  41. u16 msglen)
  42. {
  43. struct i40e_hw *hw = &pf->hw;
  44. struct i40e_vf *vf = pf->vf;
  45. int i;
  46. for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
  47. int abs_vf_id = vf->vf_id + 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_STAT_INIT, &vf->vf_states) &&
  50. !test_bit(I40E_VF_STAT_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_link_state
  61. * @vf: pointer to the VF structure
  62. *
  63. * send a link status message to a single VF
  64. **/
  65. static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
  66. {
  67. struct i40e_virtchnl_pf_event pfe;
  68. struct i40e_pf *pf = vf->pf;
  69. struct i40e_hw *hw = &pf->hw;
  70. struct i40e_link_status *ls = &pf->hw.phy.link_info;
  71. int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  72. pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
  73. pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
  74. if (vf->link_forced) {
  75. pfe.event_data.link_event.link_status = vf->link_up;
  76. pfe.event_data.link_event.link_speed =
  77. (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
  78. } else {
  79. pfe.event_data.link_event.link_status =
  80. ls->link_info & I40E_AQ_LINK_UP;
  81. pfe.event_data.link_event.link_speed = ls->link_speed;
  82. }
  83. i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
  84. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  85. }
  86. /**
  87. * i40e_vc_notify_link_state
  88. * @pf: pointer to the PF structure
  89. *
  90. * send a link status message to all VFs on a given PF
  91. **/
  92. void i40e_vc_notify_link_state(struct i40e_pf *pf)
  93. {
  94. int i;
  95. for (i = 0; i < pf->num_alloc_vfs; i++)
  96. i40e_vc_notify_vf_link_state(&pf->vf[i]);
  97. }
  98. /**
  99. * i40e_vc_notify_reset
  100. * @pf: pointer to the PF structure
  101. *
  102. * indicate a pending reset to all VFs on a given PF
  103. **/
  104. void i40e_vc_notify_reset(struct i40e_pf *pf)
  105. {
  106. struct i40e_virtchnl_pf_event pfe;
  107. pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
  108. pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
  109. i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, 0,
  110. (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
  111. }
  112. /**
  113. * i40e_vc_notify_vf_reset
  114. * @vf: pointer to the VF structure
  115. *
  116. * indicate a pending reset to the given VF
  117. **/
  118. void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
  119. {
  120. struct i40e_virtchnl_pf_event pfe;
  121. int abs_vf_id;
  122. /* validate the request */
  123. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  124. return;
  125. /* verify if the VF is in either init or active before proceeding */
  126. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
  127. !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
  128. return;
  129. abs_vf_id = vf->vf_id + vf->pf->hw.func_caps.vf_base_id;
  130. pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
  131. pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
  132. i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
  133. 0, (u8 *)&pfe,
  134. sizeof(struct i40e_virtchnl_pf_event), NULL);
  135. }
  136. /***********************misc routines*****************************/
  137. /**
  138. * i40e_vc_disable_vf
  139. * @pf: pointer to the PF info
  140. * @vf: pointer to the VF info
  141. *
  142. * Disable the VF through a SW reset
  143. **/
  144. static inline void i40e_vc_disable_vf(struct i40e_pf *pf, struct i40e_vf *vf)
  145. {
  146. i40e_vc_notify_vf_reset(vf);
  147. i40e_reset_vf(vf, false);
  148. }
  149. /**
  150. * i40e_vc_isvalid_vsi_id
  151. * @vf: pointer to the VF info
  152. * @vsi_id: VF relative VSI id
  153. *
  154. * check for the valid VSI id
  155. **/
  156. static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
  157. {
  158. struct i40e_pf *pf = vf->pf;
  159. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  160. return (vsi && (vsi->vf_id == vf->vf_id));
  161. }
  162. /**
  163. * i40e_vc_isvalid_queue_id
  164. * @vf: pointer to the VF info
  165. * @vsi_id: vsi id
  166. * @qid: vsi relative queue id
  167. *
  168. * check for the valid queue id
  169. **/
  170. static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
  171. u8 qid)
  172. {
  173. struct i40e_pf *pf = vf->pf;
  174. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  175. return (vsi && (qid < vsi->alloc_queue_pairs));
  176. }
  177. /**
  178. * i40e_vc_isvalid_vector_id
  179. * @vf: pointer to the VF info
  180. * @vector_id: VF relative vector id
  181. *
  182. * check for the valid vector id
  183. **/
  184. static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
  185. {
  186. struct i40e_pf *pf = vf->pf;
  187. return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
  188. }
  189. /***********************vf resource mgmt routines*****************/
  190. /**
  191. * i40e_vc_get_pf_queue_id
  192. * @vf: pointer to the VF info
  193. * @vsi_id: id of VSI as provided by the FW
  194. * @vsi_queue_id: vsi relative queue id
  195. *
  196. * return PF relative queue id
  197. **/
  198. static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
  199. u8 vsi_queue_id)
  200. {
  201. struct i40e_pf *pf = vf->pf;
  202. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  203. u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
  204. if (!vsi)
  205. return pf_queue_id;
  206. if (le16_to_cpu(vsi->info.mapping_flags) &
  207. I40E_AQ_VSI_QUE_MAP_NONCONTIG)
  208. pf_queue_id =
  209. le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
  210. else
  211. pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
  212. vsi_queue_id;
  213. return pf_queue_id;
  214. }
  215. /**
  216. * i40e_config_irq_link_list
  217. * @vf: pointer to the VF info
  218. * @vsi_id: id of VSI as given by the FW
  219. * @vecmap: irq map info
  220. *
  221. * configure irq link list from the map
  222. **/
  223. static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
  224. struct i40e_virtchnl_vector_map *vecmap)
  225. {
  226. unsigned long linklistmap = 0, tempmap;
  227. struct i40e_pf *pf = vf->pf;
  228. struct i40e_hw *hw = &pf->hw;
  229. u16 vsi_queue_id, pf_queue_id;
  230. enum i40e_queue_type qtype;
  231. u16 next_q, vector_id;
  232. u32 reg, reg_idx;
  233. u16 itr_idx = 0;
  234. vector_id = vecmap->vector_id;
  235. /* setup the head */
  236. if (0 == vector_id)
  237. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  238. else
  239. reg_idx = I40E_VPINT_LNKLSTN(
  240. ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
  241. (vector_id - 1));
  242. if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
  243. /* Special case - No queues mapped on this vector */
  244. wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
  245. goto irq_list_done;
  246. }
  247. tempmap = vecmap->rxq_map;
  248. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  249. linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
  250. vsi_queue_id));
  251. }
  252. tempmap = vecmap->txq_map;
  253. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  254. linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
  255. vsi_queue_id + 1));
  256. }
  257. next_q = find_first_bit(&linklistmap,
  258. (I40E_MAX_VSI_QP *
  259. I40E_VIRTCHNL_SUPPORTED_QTYPES));
  260. vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
  261. qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
  262. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  263. reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
  264. wr32(hw, reg_idx, reg);
  265. while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
  266. switch (qtype) {
  267. case I40E_QUEUE_TYPE_RX:
  268. reg_idx = I40E_QINT_RQCTL(pf_queue_id);
  269. itr_idx = vecmap->rxitr_idx;
  270. break;
  271. case I40E_QUEUE_TYPE_TX:
  272. reg_idx = I40E_QINT_TQCTL(pf_queue_id);
  273. itr_idx = vecmap->txitr_idx;
  274. break;
  275. default:
  276. break;
  277. }
  278. next_q = find_next_bit(&linklistmap,
  279. (I40E_MAX_VSI_QP *
  280. I40E_VIRTCHNL_SUPPORTED_QTYPES),
  281. next_q + 1);
  282. if (next_q <
  283. (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
  284. vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
  285. qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
  286. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
  287. vsi_queue_id);
  288. } else {
  289. pf_queue_id = I40E_QUEUE_END_OF_LIST;
  290. qtype = 0;
  291. }
  292. /* format for the RQCTL & TQCTL regs is same */
  293. reg = (vector_id) |
  294. (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
  295. (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  296. BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
  297. (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
  298. wr32(hw, reg_idx, reg);
  299. }
  300. /* if the vf is running in polling mode and using interrupt zero,
  301. * need to disable auto-mask on enabling zero interrupt for VFs.
  302. */
  303. if ((vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
  304. (vector_id == 0)) {
  305. reg = rd32(hw, I40E_GLINT_CTL);
  306. if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
  307. reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
  308. wr32(hw, I40E_GLINT_CTL, reg);
  309. }
  310. }
  311. irq_list_done:
  312. i40e_flush(hw);
  313. }
  314. /**
  315. * i40e_config_vsi_tx_queue
  316. * @vf: pointer to the VF info
  317. * @vsi_id: id of VSI as provided by the FW
  318. * @vsi_queue_id: vsi relative queue index
  319. * @info: config. info
  320. *
  321. * configure tx queue
  322. **/
  323. static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
  324. u16 vsi_queue_id,
  325. struct i40e_virtchnl_txq_info *info)
  326. {
  327. struct i40e_pf *pf = vf->pf;
  328. struct i40e_hw *hw = &pf->hw;
  329. struct i40e_hmc_obj_txq tx_ctx;
  330. struct i40e_vsi *vsi;
  331. u16 pf_queue_id;
  332. u32 qtx_ctl;
  333. int ret = 0;
  334. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  335. vsi = i40e_find_vsi_from_id(pf, vsi_id);
  336. /* clear the context structure first */
  337. memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
  338. /* only set the required fields */
  339. tx_ctx.base = info->dma_ring_addr / 128;
  340. tx_ctx.qlen = info->ring_len;
  341. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
  342. tx_ctx.rdylist_act = 0;
  343. tx_ctx.head_wb_ena = info->headwb_enabled;
  344. tx_ctx.head_wb_addr = info->dma_headwb_addr;
  345. /* clear the context in the HMC */
  346. ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
  347. if (ret) {
  348. dev_err(&pf->pdev->dev,
  349. "Failed to clear VF LAN Tx queue context %d, error: %d\n",
  350. pf_queue_id, ret);
  351. ret = -ENOENT;
  352. goto error_context;
  353. }
  354. /* set the context in the HMC */
  355. ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
  356. if (ret) {
  357. dev_err(&pf->pdev->dev,
  358. "Failed to set VF LAN Tx queue context %d error: %d\n",
  359. pf_queue_id, ret);
  360. ret = -ENOENT;
  361. goto error_context;
  362. }
  363. /* associate this queue with the PCI VF function */
  364. qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
  365. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
  366. & I40E_QTX_CTL_PF_INDX_MASK);
  367. qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
  368. << I40E_QTX_CTL_VFVM_INDX_SHIFT)
  369. & I40E_QTX_CTL_VFVM_INDX_MASK);
  370. wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
  371. i40e_flush(hw);
  372. error_context:
  373. return ret;
  374. }
  375. /**
  376. * i40e_config_vsi_rx_queue
  377. * @vf: pointer to the VF info
  378. * @vsi_id: id of VSI as provided by the FW
  379. * @vsi_queue_id: vsi relative queue index
  380. * @info: config. info
  381. *
  382. * configure rx queue
  383. **/
  384. static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
  385. u16 vsi_queue_id,
  386. struct i40e_virtchnl_rxq_info *info)
  387. {
  388. struct i40e_pf *pf = vf->pf;
  389. struct i40e_hw *hw = &pf->hw;
  390. struct i40e_hmc_obj_rxq rx_ctx;
  391. u16 pf_queue_id;
  392. int ret = 0;
  393. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  394. /* clear the context structure first */
  395. memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
  396. /* only set the required fields */
  397. rx_ctx.base = info->dma_ring_addr / 128;
  398. rx_ctx.qlen = info->ring_len;
  399. if (info->splithdr_enabled) {
  400. rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
  401. I40E_RX_SPLIT_IP |
  402. I40E_RX_SPLIT_TCP_UDP |
  403. I40E_RX_SPLIT_SCTP;
  404. /* header length validation */
  405. if (info->hdr_size > ((2 * 1024) - 64)) {
  406. ret = -EINVAL;
  407. goto error_param;
  408. }
  409. rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
  410. /* set splitalways mode 10b */
  411. rx_ctx.dtype = 0x2;
  412. }
  413. /* databuffer length validation */
  414. if (info->databuffer_size > ((16 * 1024) - 128)) {
  415. ret = -EINVAL;
  416. goto error_param;
  417. }
  418. rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
  419. /* max pkt. length validation */
  420. if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
  421. ret = -EINVAL;
  422. goto error_param;
  423. }
  424. rx_ctx.rxmax = info->max_pkt_size;
  425. /* enable 32bytes desc always */
  426. rx_ctx.dsize = 1;
  427. /* default values */
  428. rx_ctx.lrxqthresh = 2;
  429. rx_ctx.crcstrip = 1;
  430. rx_ctx.prefena = 1;
  431. rx_ctx.l2tsel = 1;
  432. /* clear the context in the HMC */
  433. ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
  434. if (ret) {
  435. dev_err(&pf->pdev->dev,
  436. "Failed to clear VF LAN Rx queue context %d, error: %d\n",
  437. pf_queue_id, ret);
  438. ret = -ENOENT;
  439. goto error_param;
  440. }
  441. /* set the context in the HMC */
  442. ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
  443. if (ret) {
  444. dev_err(&pf->pdev->dev,
  445. "Failed to set VF LAN Rx queue context %d error: %d\n",
  446. pf_queue_id, ret);
  447. ret = -ENOENT;
  448. goto error_param;
  449. }
  450. error_param:
  451. return ret;
  452. }
  453. /**
  454. * i40e_alloc_vsi_res
  455. * @vf: pointer to the VF info
  456. * @type: type of VSI to allocate
  457. *
  458. * alloc VF vsi context & resources
  459. **/
  460. static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
  461. {
  462. struct i40e_mac_filter *f = NULL;
  463. struct i40e_pf *pf = vf->pf;
  464. struct i40e_vsi *vsi;
  465. int ret = 0;
  466. vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
  467. if (!vsi) {
  468. dev_err(&pf->pdev->dev,
  469. "add vsi failed for VF %d, aq_err %d\n",
  470. vf->vf_id, pf->hw.aq.asq_last_status);
  471. ret = -ENOENT;
  472. goto error_alloc_vsi_res;
  473. }
  474. if (type == I40E_VSI_SRIOV) {
  475. u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  476. vf->lan_vsi_idx = vsi->idx;
  477. vf->lan_vsi_id = vsi->id;
  478. /* If the port VLAN has been configured and then the
  479. * VF driver was removed then the VSI port VLAN
  480. * configuration was destroyed. Check if there is
  481. * a port VLAN and restore the VSI configuration if
  482. * needed.
  483. */
  484. if (vf->port_vlan_id)
  485. i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
  486. f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
  487. vf->port_vlan_id ? vf->port_vlan_id : -1,
  488. true, false);
  489. if (!f)
  490. dev_info(&pf->pdev->dev,
  491. "Could not allocate VF MAC addr\n");
  492. f = i40e_add_filter(vsi, brdcast,
  493. vf->port_vlan_id ? vf->port_vlan_id : -1,
  494. true, false);
  495. if (!f)
  496. dev_info(&pf->pdev->dev,
  497. "Could not allocate VF broadcast filter\n");
  498. }
  499. /* program mac filter */
  500. ret = i40e_sync_vsi_filters(vsi);
  501. if (ret)
  502. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  503. /* Set VF bandwidth if specified */
  504. if (vf->tx_rate) {
  505. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
  506. vf->tx_rate / 50, 0, NULL);
  507. if (ret)
  508. dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
  509. vf->vf_id, ret);
  510. }
  511. error_alloc_vsi_res:
  512. return ret;
  513. }
  514. /**
  515. * i40e_enable_vf_mappings
  516. * @vf: pointer to the VF info
  517. *
  518. * enable VF mappings
  519. **/
  520. static void i40e_enable_vf_mappings(struct i40e_vf *vf)
  521. {
  522. struct i40e_pf *pf = vf->pf;
  523. struct i40e_hw *hw = &pf->hw;
  524. u32 reg, total_queue_pairs = 0;
  525. int j;
  526. /* Tell the hardware we're using noncontiguous mapping. HW requires
  527. * that VF queues be mapped using this method, even when they are
  528. * contiguous in real life
  529. */
  530. wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
  531. I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
  532. /* enable VF vplan_qtable mappings */
  533. reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
  534. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
  535. /* map PF queues to VF queues */
  536. for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
  537. u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
  538. reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
  539. wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
  540. total_queue_pairs++;
  541. }
  542. /* map PF queues to VSI */
  543. for (j = 0; j < 7; j++) {
  544. if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
  545. reg = 0x07FF07FF; /* unused */
  546. } else {
  547. u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
  548. j * 2);
  549. reg = qid;
  550. qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
  551. (j * 2) + 1);
  552. reg |= qid << 16;
  553. }
  554. wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
  555. }
  556. i40e_flush(hw);
  557. }
  558. /**
  559. * i40e_disable_vf_mappings
  560. * @vf: pointer to the VF info
  561. *
  562. * disable VF mappings
  563. **/
  564. static void i40e_disable_vf_mappings(struct i40e_vf *vf)
  565. {
  566. struct i40e_pf *pf = vf->pf;
  567. struct i40e_hw *hw = &pf->hw;
  568. int i;
  569. /* disable qp mappings */
  570. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
  571. for (i = 0; i < I40E_MAX_VSI_QP; i++)
  572. wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
  573. I40E_QUEUE_END_OF_LIST);
  574. i40e_flush(hw);
  575. }
  576. /**
  577. * i40e_free_vf_res
  578. * @vf: pointer to the VF info
  579. *
  580. * free VF resources
  581. **/
  582. static void i40e_free_vf_res(struct i40e_vf *vf)
  583. {
  584. struct i40e_pf *pf = vf->pf;
  585. struct i40e_hw *hw = &pf->hw;
  586. u32 reg_idx, reg;
  587. int i, msix_vf;
  588. /* free vsi & disconnect it from the parent uplink */
  589. if (vf->lan_vsi_idx) {
  590. i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
  591. vf->lan_vsi_idx = 0;
  592. vf->lan_vsi_id = 0;
  593. }
  594. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  595. /* disable interrupts so the VF starts in a known state */
  596. for (i = 0; i < msix_vf; i++) {
  597. /* format is same for both registers */
  598. if (0 == i)
  599. reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
  600. else
  601. reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
  602. (vf->vf_id))
  603. + (i - 1));
  604. wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
  605. i40e_flush(hw);
  606. }
  607. /* clear the irq settings */
  608. for (i = 0; i < msix_vf; i++) {
  609. /* format is same for both registers */
  610. if (0 == i)
  611. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  612. else
  613. reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
  614. (vf->vf_id))
  615. + (i - 1));
  616. reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
  617. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
  618. wr32(hw, reg_idx, reg);
  619. i40e_flush(hw);
  620. }
  621. /* reset some of the state varibles keeping
  622. * track of the resources
  623. */
  624. vf->num_queue_pairs = 0;
  625. vf->vf_states = 0;
  626. }
  627. /**
  628. * i40e_alloc_vf_res
  629. * @vf: pointer to the VF info
  630. *
  631. * allocate VF resources
  632. **/
  633. static int i40e_alloc_vf_res(struct i40e_vf *vf)
  634. {
  635. struct i40e_pf *pf = vf->pf;
  636. int total_queue_pairs = 0;
  637. int ret;
  638. /* allocate hw vsi context & associated resources */
  639. ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
  640. if (ret)
  641. goto error_alloc;
  642. total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
  643. set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  644. /* store the total qps number for the runtime
  645. * VF req validation
  646. */
  647. vf->num_queue_pairs = total_queue_pairs;
  648. /* VF is now completely initialized */
  649. set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
  650. error_alloc:
  651. if (ret)
  652. i40e_free_vf_res(vf);
  653. return ret;
  654. }
  655. #define VF_DEVICE_STATUS 0xAA
  656. #define VF_TRANS_PENDING_MASK 0x20
  657. /**
  658. * i40e_quiesce_vf_pci
  659. * @vf: pointer to the VF structure
  660. *
  661. * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
  662. * if the transactions never clear.
  663. **/
  664. static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
  665. {
  666. struct i40e_pf *pf = vf->pf;
  667. struct i40e_hw *hw = &pf->hw;
  668. int vf_abs_id, i;
  669. u32 reg;
  670. vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
  671. wr32(hw, I40E_PF_PCI_CIAA,
  672. VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
  673. for (i = 0; i < 100; i++) {
  674. reg = rd32(hw, I40E_PF_PCI_CIAD);
  675. if ((reg & VF_TRANS_PENDING_MASK) == 0)
  676. return 0;
  677. udelay(1);
  678. }
  679. return -EIO;
  680. }
  681. /**
  682. * i40e_reset_vf
  683. * @vf: pointer to the VF structure
  684. * @flr: VFLR was issued or not
  685. *
  686. * reset the VF
  687. **/
  688. void i40e_reset_vf(struct i40e_vf *vf, bool flr)
  689. {
  690. struct i40e_pf *pf = vf->pf;
  691. struct i40e_hw *hw = &pf->hw;
  692. bool rsd = false;
  693. int i;
  694. u32 reg;
  695. if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
  696. return;
  697. /* warn the VF */
  698. clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
  699. /* In the case of a VFLR, the HW has already reset the VF and we
  700. * just need to clean up, so don't hit the VFRTRIG register.
  701. */
  702. if (!flr) {
  703. /* reset VF using VPGEN_VFRTRIG reg */
  704. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  705. reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  706. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  707. i40e_flush(hw);
  708. }
  709. if (i40e_quiesce_vf_pci(vf))
  710. dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
  711. vf->vf_id);
  712. /* poll VPGEN_VFRSTAT reg to make sure
  713. * that reset is complete
  714. */
  715. for (i = 0; i < 10; i++) {
  716. /* VF reset requires driver to first reset the VF and then
  717. * poll the status register to make sure that the reset
  718. * completed successfully. Due to internal HW FIFO flushes,
  719. * we must wait 10ms before the register will be valid.
  720. */
  721. usleep_range(10000, 20000);
  722. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  723. if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
  724. rsd = true;
  725. break;
  726. }
  727. }
  728. if (flr)
  729. usleep_range(10000, 20000);
  730. if (!rsd)
  731. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  732. vf->vf_id);
  733. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
  734. /* clear the reset bit in the VPGEN_VFRTRIG reg */
  735. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  736. reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  737. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  738. /* On initial reset, we won't have any queues */
  739. if (vf->lan_vsi_idx == 0)
  740. goto complete_reset;
  741. i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false);
  742. complete_reset:
  743. /* reallocate VF resources to reset the VSI state */
  744. i40e_free_vf_res(vf);
  745. i40e_alloc_vf_res(vf);
  746. i40e_enable_vf_mappings(vf);
  747. set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
  748. clear_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  749. /* tell the VF the reset is done */
  750. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
  751. i40e_flush(hw);
  752. clear_bit(__I40E_VF_DISABLE, &pf->state);
  753. }
  754. /**
  755. * i40e_free_vfs
  756. * @pf: pointer to the PF structure
  757. *
  758. * free VF resources
  759. **/
  760. void i40e_free_vfs(struct i40e_pf *pf)
  761. {
  762. struct i40e_hw *hw = &pf->hw;
  763. u32 reg_idx, bit_idx;
  764. int i, tmp, vf_id;
  765. if (!pf->vf)
  766. return;
  767. while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
  768. usleep_range(1000, 2000);
  769. for (i = 0; i < pf->num_alloc_vfs; i++)
  770. if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
  771. i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
  772. false);
  773. /* Disable IOV before freeing resources. This lets any VF drivers
  774. * running in the host get themselves cleaned up before we yank
  775. * the carpet out from underneath their feet.
  776. */
  777. if (!pci_vfs_assigned(pf->pdev))
  778. pci_disable_sriov(pf->pdev);
  779. else
  780. dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
  781. msleep(20); /* let any messages in transit get finished up */
  782. /* free up VF resources */
  783. tmp = pf->num_alloc_vfs;
  784. pf->num_alloc_vfs = 0;
  785. for (i = 0; i < tmp; i++) {
  786. if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
  787. i40e_free_vf_res(&pf->vf[i]);
  788. /* disable qp mappings */
  789. i40e_disable_vf_mappings(&pf->vf[i]);
  790. }
  791. kfree(pf->vf);
  792. pf->vf = NULL;
  793. /* This check is for when the driver is unloaded while VFs are
  794. * assigned. Setting the number of VFs to 0 through sysfs is caught
  795. * before this function ever gets called.
  796. */
  797. if (!pci_vfs_assigned(pf->pdev)) {
  798. /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
  799. * work correctly when SR-IOV gets re-enabled.
  800. */
  801. for (vf_id = 0; vf_id < tmp; vf_id++) {
  802. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  803. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  804. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  805. }
  806. }
  807. clear_bit(__I40E_VF_DISABLE, &pf->state);
  808. }
  809. #ifdef CONFIG_PCI_IOV
  810. /**
  811. * i40e_alloc_vfs
  812. * @pf: pointer to the PF structure
  813. * @num_alloc_vfs: number of VFs to allocate
  814. *
  815. * allocate VF resources
  816. **/
  817. int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
  818. {
  819. struct i40e_vf *vfs;
  820. int i, ret = 0;
  821. /* Disable interrupt 0 so we don't try to handle the VFLR. */
  822. i40e_irq_dynamic_disable_icr0(pf);
  823. /* Check to see if we're just allocating resources for extant VFs */
  824. if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
  825. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  826. if (ret) {
  827. pf->num_alloc_vfs = 0;
  828. goto err_iov;
  829. }
  830. }
  831. /* allocate memory */
  832. vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
  833. if (!vfs) {
  834. ret = -ENOMEM;
  835. goto err_alloc;
  836. }
  837. pf->vf = vfs;
  838. /* apply default profile */
  839. for (i = 0; i < num_alloc_vfs; i++) {
  840. vfs[i].pf = pf;
  841. vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
  842. vfs[i].vf_id = i;
  843. /* assign default capabilities */
  844. set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
  845. vfs[i].spoofchk = true;
  846. /* VF resources get allocated during reset */
  847. i40e_reset_vf(&vfs[i], false);
  848. /* enable VF vplan_qtable mappings */
  849. i40e_enable_vf_mappings(&vfs[i]);
  850. }
  851. pf->num_alloc_vfs = num_alloc_vfs;
  852. err_alloc:
  853. if (ret)
  854. i40e_free_vfs(pf);
  855. err_iov:
  856. /* Re-enable interrupt 0. */
  857. i40e_irq_dynamic_enable_icr0(pf);
  858. return ret;
  859. }
  860. #endif
  861. /**
  862. * i40e_pci_sriov_enable
  863. * @pdev: pointer to a pci_dev structure
  864. * @num_vfs: number of VFs to allocate
  865. *
  866. * Enable or change the number of VFs
  867. **/
  868. static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
  869. {
  870. #ifdef CONFIG_PCI_IOV
  871. struct i40e_pf *pf = pci_get_drvdata(pdev);
  872. int pre_existing_vfs = pci_num_vf(pdev);
  873. int err = 0;
  874. if (pf->state & __I40E_TESTING) {
  875. dev_warn(&pdev->dev,
  876. "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
  877. err = -EPERM;
  878. goto err_out;
  879. }
  880. dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
  881. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  882. i40e_free_vfs(pf);
  883. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  884. goto out;
  885. if (num_vfs > pf->num_req_vfs) {
  886. err = -EPERM;
  887. goto err_out;
  888. }
  889. err = i40e_alloc_vfs(pf, num_vfs);
  890. if (err) {
  891. dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
  892. goto err_out;
  893. }
  894. out:
  895. return num_vfs;
  896. err_out:
  897. return err;
  898. #endif
  899. return 0;
  900. }
  901. /**
  902. * i40e_pci_sriov_configure
  903. * @pdev: pointer to a pci_dev structure
  904. * @num_vfs: number of VFs to allocate
  905. *
  906. * Enable or change the number of VFs. Called when the user updates the number
  907. * of VFs in sysfs.
  908. **/
  909. int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
  910. {
  911. struct i40e_pf *pf = pci_get_drvdata(pdev);
  912. if (num_vfs) {
  913. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  914. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  915. i40e_do_reset_safe(pf,
  916. BIT_ULL(__I40E_PF_RESET_REQUESTED));
  917. }
  918. return i40e_pci_sriov_enable(pdev, num_vfs);
  919. }
  920. if (!pci_vfs_assigned(pf->pdev)) {
  921. i40e_free_vfs(pf);
  922. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  923. i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  924. } else {
  925. dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
  926. return -EINVAL;
  927. }
  928. return 0;
  929. }
  930. /***********************virtual channel routines******************/
  931. /**
  932. * i40e_vc_send_msg_to_vf
  933. * @vf: pointer to the VF info
  934. * @v_opcode: virtual channel opcode
  935. * @v_retval: virtual channel return value
  936. * @msg: pointer to the msg buffer
  937. * @msglen: msg length
  938. *
  939. * send msg to VF
  940. **/
  941. static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
  942. u32 v_retval, u8 *msg, u16 msglen)
  943. {
  944. struct i40e_pf *pf;
  945. struct i40e_hw *hw;
  946. int abs_vf_id;
  947. i40e_status aq_ret;
  948. /* validate the request */
  949. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  950. return -EINVAL;
  951. pf = vf->pf;
  952. hw = &pf->hw;
  953. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  954. /* single place to detect unsuccessful return values */
  955. if (v_retval) {
  956. vf->num_invalid_msgs++;
  957. dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
  958. v_opcode, v_retval);
  959. if (vf->num_invalid_msgs >
  960. I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
  961. dev_err(&pf->pdev->dev,
  962. "Number of invalid messages exceeded for VF %d\n",
  963. vf->vf_id);
  964. dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
  965. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  966. }
  967. } else {
  968. vf->num_valid_msgs++;
  969. }
  970. aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  971. msg, msglen, NULL);
  972. if (aq_ret) {
  973. dev_err(&pf->pdev->dev,
  974. "Unable to send the message to VF %d aq_err %d\n",
  975. vf->vf_id, pf->hw.aq.asq_last_status);
  976. return -EIO;
  977. }
  978. return 0;
  979. }
  980. /**
  981. * i40e_vc_send_resp_to_vf
  982. * @vf: pointer to the VF info
  983. * @opcode: operation code
  984. * @retval: return value
  985. *
  986. * send resp msg to VF
  987. **/
  988. static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
  989. enum i40e_virtchnl_ops opcode,
  990. i40e_status retval)
  991. {
  992. return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
  993. }
  994. /**
  995. * i40e_vc_get_version_msg
  996. * @vf: pointer to the VF info
  997. *
  998. * called from the VF to request the API version used by the PF
  999. **/
  1000. static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
  1001. {
  1002. struct i40e_virtchnl_version_info info = {
  1003. I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
  1004. };
  1005. vf->vf_ver = *(struct i40e_virtchnl_version_info *)msg;
  1006. /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
  1007. if (VF_IS_V10(vf))
  1008. info.minor = I40E_VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
  1009. return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
  1010. I40E_SUCCESS, (u8 *)&info,
  1011. sizeof(struct
  1012. i40e_virtchnl_version_info));
  1013. }
  1014. /**
  1015. * i40e_vc_get_vf_resources_msg
  1016. * @vf: pointer to the VF info
  1017. * @msg: pointer to the msg buffer
  1018. * @msglen: msg length
  1019. *
  1020. * called from the VF to request its resources
  1021. **/
  1022. static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
  1023. {
  1024. struct i40e_virtchnl_vf_resource *vfres = NULL;
  1025. struct i40e_pf *pf = vf->pf;
  1026. i40e_status aq_ret = 0;
  1027. struct i40e_vsi *vsi;
  1028. int i = 0, len = 0;
  1029. int num_vsis = 1;
  1030. int ret;
  1031. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  1032. aq_ret = I40E_ERR_PARAM;
  1033. goto err;
  1034. }
  1035. len = (sizeof(struct i40e_virtchnl_vf_resource) +
  1036. sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
  1037. vfres = kzalloc(len, GFP_KERNEL);
  1038. if (!vfres) {
  1039. aq_ret = I40E_ERR_NO_MEMORY;
  1040. len = 0;
  1041. goto err;
  1042. }
  1043. if (VF_IS_V11(vf))
  1044. vf->driver_caps = *(u32 *)msg;
  1045. else
  1046. vf->driver_caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
  1047. I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
  1048. I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
  1049. vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
  1050. vsi = pf->vsi[vf->lan_vsi_idx];
  1051. if (!vsi->info.pvid)
  1052. vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
  1053. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
  1054. if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ)
  1055. vfres->vf_offload_flags |=
  1056. I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ;
  1057. } else {
  1058. vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG;
  1059. }
  1060. vfres->num_vsis = num_vsis;
  1061. vfres->num_queue_pairs = vf->num_queue_pairs;
  1062. vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
  1063. if (vf->lan_vsi_idx) {
  1064. vfres->vsi_res[i].vsi_id = vf->lan_vsi_id;
  1065. vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
  1066. vfres->vsi_res[i].num_queue_pairs =
  1067. pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
  1068. memcpy(vfres->vsi_res[i].default_mac_addr,
  1069. vf->default_lan_addr.addr, ETH_ALEN);
  1070. i++;
  1071. }
  1072. set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
  1073. err:
  1074. /* send the response back to the VF */
  1075. ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
  1076. aq_ret, (u8 *)vfres, len);
  1077. kfree(vfres);
  1078. return ret;
  1079. }
  1080. /**
  1081. * i40e_vc_reset_vf_msg
  1082. * @vf: pointer to the VF info
  1083. * @msg: pointer to the msg buffer
  1084. * @msglen: msg length
  1085. *
  1086. * called from the VF to reset itself,
  1087. * unlike other virtchnl messages, PF driver
  1088. * doesn't send the response back to the VF
  1089. **/
  1090. static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
  1091. {
  1092. if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
  1093. i40e_reset_vf(vf, false);
  1094. }
  1095. /**
  1096. * i40e_vc_config_promiscuous_mode_msg
  1097. * @vf: pointer to the VF info
  1098. * @msg: pointer to the msg buffer
  1099. * @msglen: msg length
  1100. *
  1101. * called from the VF to configure the promiscuous mode of
  1102. * VF vsis
  1103. **/
  1104. static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
  1105. u8 *msg, u16 msglen)
  1106. {
  1107. struct i40e_virtchnl_promisc_info *info =
  1108. (struct i40e_virtchnl_promisc_info *)msg;
  1109. struct i40e_pf *pf = vf->pf;
  1110. struct i40e_hw *hw = &pf->hw;
  1111. struct i40e_vsi *vsi;
  1112. bool allmulti = false;
  1113. i40e_status aq_ret;
  1114. vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
  1115. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1116. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1117. !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
  1118. (vsi->type != I40E_VSI_FCOE)) {
  1119. aq_ret = I40E_ERR_PARAM;
  1120. goto error_param;
  1121. }
  1122. if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
  1123. allmulti = true;
  1124. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
  1125. allmulti, NULL);
  1126. error_param:
  1127. /* send the response to the VF */
  1128. return i40e_vc_send_resp_to_vf(vf,
  1129. I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
  1130. aq_ret);
  1131. }
  1132. /**
  1133. * i40e_vc_config_queues_msg
  1134. * @vf: pointer to the VF info
  1135. * @msg: pointer to the msg buffer
  1136. * @msglen: msg length
  1137. *
  1138. * called from the VF to configure the rx/tx
  1139. * queues
  1140. **/
  1141. static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1142. {
  1143. struct i40e_virtchnl_vsi_queue_config_info *qci =
  1144. (struct i40e_virtchnl_vsi_queue_config_info *)msg;
  1145. struct i40e_virtchnl_queue_pair_info *qpi;
  1146. struct i40e_pf *pf = vf->pf;
  1147. u16 vsi_id, vsi_queue_id;
  1148. i40e_status aq_ret = 0;
  1149. int i;
  1150. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1151. aq_ret = I40E_ERR_PARAM;
  1152. goto error_param;
  1153. }
  1154. vsi_id = qci->vsi_id;
  1155. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1156. aq_ret = I40E_ERR_PARAM;
  1157. goto error_param;
  1158. }
  1159. for (i = 0; i < qci->num_queue_pairs; i++) {
  1160. qpi = &qci->qpair[i];
  1161. vsi_queue_id = qpi->txq.queue_id;
  1162. if ((qpi->txq.vsi_id != vsi_id) ||
  1163. (qpi->rxq.vsi_id != vsi_id) ||
  1164. (qpi->rxq.queue_id != vsi_queue_id) ||
  1165. !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
  1166. aq_ret = I40E_ERR_PARAM;
  1167. goto error_param;
  1168. }
  1169. if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
  1170. &qpi->rxq) ||
  1171. i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
  1172. &qpi->txq)) {
  1173. aq_ret = I40E_ERR_PARAM;
  1174. goto error_param;
  1175. }
  1176. }
  1177. /* set vsi num_queue_pairs in use to num configured by VF */
  1178. pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
  1179. error_param:
  1180. /* send the response to the VF */
  1181. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
  1182. aq_ret);
  1183. }
  1184. /**
  1185. * i40e_vc_config_irq_map_msg
  1186. * @vf: pointer to the VF info
  1187. * @msg: pointer to the msg buffer
  1188. * @msglen: msg length
  1189. *
  1190. * called from the VF to configure the irq to
  1191. * queue map
  1192. **/
  1193. static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1194. {
  1195. struct i40e_virtchnl_irq_map_info *irqmap_info =
  1196. (struct i40e_virtchnl_irq_map_info *)msg;
  1197. struct i40e_virtchnl_vector_map *map;
  1198. u16 vsi_id, vsi_queue_id, vector_id;
  1199. i40e_status aq_ret = 0;
  1200. unsigned long tempmap;
  1201. int i;
  1202. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1203. aq_ret = I40E_ERR_PARAM;
  1204. goto error_param;
  1205. }
  1206. for (i = 0; i < irqmap_info->num_vectors; i++) {
  1207. map = &irqmap_info->vecmap[i];
  1208. vector_id = map->vector_id;
  1209. vsi_id = map->vsi_id;
  1210. /* validate msg params */
  1211. if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
  1212. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1213. aq_ret = I40E_ERR_PARAM;
  1214. goto error_param;
  1215. }
  1216. /* lookout for the invalid queue index */
  1217. tempmap = map->rxq_map;
  1218. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  1219. if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
  1220. vsi_queue_id)) {
  1221. aq_ret = I40E_ERR_PARAM;
  1222. goto error_param;
  1223. }
  1224. }
  1225. tempmap = map->txq_map;
  1226. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  1227. if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
  1228. vsi_queue_id)) {
  1229. aq_ret = I40E_ERR_PARAM;
  1230. goto error_param;
  1231. }
  1232. }
  1233. i40e_config_irq_link_list(vf, vsi_id, map);
  1234. }
  1235. error_param:
  1236. /* send the response to the VF */
  1237. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
  1238. aq_ret);
  1239. }
  1240. /**
  1241. * i40e_vc_enable_queues_msg
  1242. * @vf: pointer to the VF info
  1243. * @msg: pointer to the msg buffer
  1244. * @msglen: msg length
  1245. *
  1246. * called from the VF to enable all or specific queue(s)
  1247. **/
  1248. static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1249. {
  1250. struct i40e_virtchnl_queue_select *vqs =
  1251. (struct i40e_virtchnl_queue_select *)msg;
  1252. struct i40e_pf *pf = vf->pf;
  1253. u16 vsi_id = vqs->vsi_id;
  1254. i40e_status aq_ret = 0;
  1255. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1256. aq_ret = I40E_ERR_PARAM;
  1257. goto error_param;
  1258. }
  1259. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1260. aq_ret = I40E_ERR_PARAM;
  1261. goto error_param;
  1262. }
  1263. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1264. aq_ret = I40E_ERR_PARAM;
  1265. goto error_param;
  1266. }
  1267. if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], true))
  1268. aq_ret = I40E_ERR_TIMEOUT;
  1269. error_param:
  1270. /* send the response to the VF */
  1271. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
  1272. aq_ret);
  1273. }
  1274. /**
  1275. * i40e_vc_disable_queues_msg
  1276. * @vf: pointer to the VF info
  1277. * @msg: pointer to the msg buffer
  1278. * @msglen: msg length
  1279. *
  1280. * called from the VF to disable all or specific
  1281. * queue(s)
  1282. **/
  1283. static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1284. {
  1285. struct i40e_virtchnl_queue_select *vqs =
  1286. (struct i40e_virtchnl_queue_select *)msg;
  1287. struct i40e_pf *pf = vf->pf;
  1288. i40e_status aq_ret = 0;
  1289. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1290. aq_ret = I40E_ERR_PARAM;
  1291. goto error_param;
  1292. }
  1293. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1294. aq_ret = I40E_ERR_PARAM;
  1295. goto error_param;
  1296. }
  1297. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1298. aq_ret = I40E_ERR_PARAM;
  1299. goto error_param;
  1300. }
  1301. if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false))
  1302. aq_ret = I40E_ERR_TIMEOUT;
  1303. error_param:
  1304. /* send the response to the VF */
  1305. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
  1306. aq_ret);
  1307. }
  1308. /**
  1309. * i40e_vc_get_stats_msg
  1310. * @vf: pointer to the VF info
  1311. * @msg: pointer to the msg buffer
  1312. * @msglen: msg length
  1313. *
  1314. * called from the VF to get vsi stats
  1315. **/
  1316. static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1317. {
  1318. struct i40e_virtchnl_queue_select *vqs =
  1319. (struct i40e_virtchnl_queue_select *)msg;
  1320. struct i40e_pf *pf = vf->pf;
  1321. struct i40e_eth_stats stats;
  1322. i40e_status aq_ret = 0;
  1323. struct i40e_vsi *vsi;
  1324. memset(&stats, 0, sizeof(struct i40e_eth_stats));
  1325. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1326. aq_ret = I40E_ERR_PARAM;
  1327. goto error_param;
  1328. }
  1329. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1330. aq_ret = I40E_ERR_PARAM;
  1331. goto error_param;
  1332. }
  1333. vsi = pf->vsi[vf->lan_vsi_idx];
  1334. if (!vsi) {
  1335. aq_ret = I40E_ERR_PARAM;
  1336. goto error_param;
  1337. }
  1338. i40e_update_eth_stats(vsi);
  1339. stats = vsi->eth_stats;
  1340. error_param:
  1341. /* send the response back to the VF */
  1342. return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
  1343. (u8 *)&stats, sizeof(stats));
  1344. }
  1345. /**
  1346. * i40e_check_vf_permission
  1347. * @vf: pointer to the VF info
  1348. * @macaddr: pointer to the MAC Address being checked
  1349. *
  1350. * Check if the VF has permission to add or delete unicast MAC address
  1351. * filters and return error code -EPERM if not. Then check if the
  1352. * address filter requested is broadcast or zero and if so return
  1353. * an invalid MAC address error code.
  1354. **/
  1355. static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
  1356. {
  1357. struct i40e_pf *pf = vf->pf;
  1358. int ret = 0;
  1359. if (is_broadcast_ether_addr(macaddr) ||
  1360. is_zero_ether_addr(macaddr)) {
  1361. dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
  1362. ret = I40E_ERR_INVALID_MAC_ADDR;
  1363. } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
  1364. !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
  1365. /* If the host VMM administrator has set the VF MAC address
  1366. * administratively via the ndo_set_vf_mac command then deny
  1367. * permission to the VF to add or delete unicast MAC addresses.
  1368. * The VF may request to set the MAC address filter already
  1369. * assigned to it so do not return an error in that case.
  1370. */
  1371. dev_err(&pf->pdev->dev,
  1372. "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
  1373. ret = -EPERM;
  1374. }
  1375. return ret;
  1376. }
  1377. /**
  1378. * i40e_vc_add_mac_addr_msg
  1379. * @vf: pointer to the VF info
  1380. * @msg: pointer to the msg buffer
  1381. * @msglen: msg length
  1382. *
  1383. * add guest mac address filter
  1384. **/
  1385. static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1386. {
  1387. struct i40e_virtchnl_ether_addr_list *al =
  1388. (struct i40e_virtchnl_ether_addr_list *)msg;
  1389. struct i40e_pf *pf = vf->pf;
  1390. struct i40e_vsi *vsi = NULL;
  1391. u16 vsi_id = al->vsi_id;
  1392. i40e_status ret = 0;
  1393. int i;
  1394. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1395. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1396. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1397. ret = I40E_ERR_PARAM;
  1398. goto error_param;
  1399. }
  1400. for (i = 0; i < al->num_elements; i++) {
  1401. ret = i40e_check_vf_permission(vf, al->list[i].addr);
  1402. if (ret)
  1403. goto error_param;
  1404. }
  1405. vsi = pf->vsi[vf->lan_vsi_idx];
  1406. /* add new addresses to the list */
  1407. for (i = 0; i < al->num_elements; i++) {
  1408. struct i40e_mac_filter *f;
  1409. f = i40e_find_mac(vsi, al->list[i].addr, true, false);
  1410. if (!f) {
  1411. if (i40e_is_vsi_in_vlan(vsi))
  1412. f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
  1413. true, false);
  1414. else
  1415. f = i40e_add_filter(vsi, al->list[i].addr, -1,
  1416. true, false);
  1417. }
  1418. if (!f) {
  1419. dev_err(&pf->pdev->dev,
  1420. "Unable to add VF MAC filter\n");
  1421. ret = I40E_ERR_PARAM;
  1422. goto error_param;
  1423. }
  1424. }
  1425. /* program the updated filter list */
  1426. if (i40e_sync_vsi_filters(vsi))
  1427. dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
  1428. error_param:
  1429. /* send the response to the VF */
  1430. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
  1431. ret);
  1432. }
  1433. /**
  1434. * i40e_vc_del_mac_addr_msg
  1435. * @vf: pointer to the VF info
  1436. * @msg: pointer to the msg buffer
  1437. * @msglen: msg length
  1438. *
  1439. * remove guest mac address filter
  1440. **/
  1441. static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1442. {
  1443. struct i40e_virtchnl_ether_addr_list *al =
  1444. (struct i40e_virtchnl_ether_addr_list *)msg;
  1445. struct i40e_pf *pf = vf->pf;
  1446. struct i40e_vsi *vsi = NULL;
  1447. u16 vsi_id = al->vsi_id;
  1448. i40e_status ret = 0;
  1449. int i;
  1450. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1451. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1452. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1453. ret = I40E_ERR_PARAM;
  1454. goto error_param;
  1455. }
  1456. for (i = 0; i < al->num_elements; i++) {
  1457. if (is_broadcast_ether_addr(al->list[i].addr) ||
  1458. is_zero_ether_addr(al->list[i].addr)) {
  1459. dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
  1460. al->list[i].addr);
  1461. ret = I40E_ERR_INVALID_MAC_ADDR;
  1462. goto error_param;
  1463. }
  1464. }
  1465. vsi = pf->vsi[vf->lan_vsi_idx];
  1466. /* delete addresses from the list */
  1467. for (i = 0; i < al->num_elements; i++)
  1468. i40e_del_filter(vsi, al->list[i].addr,
  1469. I40E_VLAN_ANY, true, false);
  1470. /* program the updated filter list */
  1471. if (i40e_sync_vsi_filters(vsi))
  1472. dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
  1473. error_param:
  1474. /* send the response to the VF */
  1475. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
  1476. ret);
  1477. }
  1478. /**
  1479. * i40e_vc_add_vlan_msg
  1480. * @vf: pointer to the VF info
  1481. * @msg: pointer to the msg buffer
  1482. * @msglen: msg length
  1483. *
  1484. * program guest vlan id
  1485. **/
  1486. static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1487. {
  1488. struct i40e_virtchnl_vlan_filter_list *vfl =
  1489. (struct i40e_virtchnl_vlan_filter_list *)msg;
  1490. struct i40e_pf *pf = vf->pf;
  1491. struct i40e_vsi *vsi = NULL;
  1492. u16 vsi_id = vfl->vsi_id;
  1493. i40e_status aq_ret = 0;
  1494. int i;
  1495. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1496. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1497. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1498. aq_ret = I40E_ERR_PARAM;
  1499. goto error_param;
  1500. }
  1501. for (i = 0; i < vfl->num_elements; i++) {
  1502. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  1503. aq_ret = I40E_ERR_PARAM;
  1504. dev_err(&pf->pdev->dev,
  1505. "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
  1506. goto error_param;
  1507. }
  1508. }
  1509. vsi = pf->vsi[vf->lan_vsi_idx];
  1510. if (vsi->info.pvid) {
  1511. aq_ret = I40E_ERR_PARAM;
  1512. goto error_param;
  1513. }
  1514. i40e_vlan_stripping_enable(vsi);
  1515. for (i = 0; i < vfl->num_elements; i++) {
  1516. /* add new VLAN filter */
  1517. int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
  1518. if (ret)
  1519. dev_err(&pf->pdev->dev,
  1520. "Unable to add VF vlan filter %d, error %d\n",
  1521. vfl->vlan_id[i], ret);
  1522. }
  1523. error_param:
  1524. /* send the response to the VF */
  1525. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
  1526. }
  1527. /**
  1528. * i40e_vc_remove_vlan_msg
  1529. * @vf: pointer to the VF info
  1530. * @msg: pointer to the msg buffer
  1531. * @msglen: msg length
  1532. *
  1533. * remove programmed guest vlan id
  1534. **/
  1535. static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1536. {
  1537. struct i40e_virtchnl_vlan_filter_list *vfl =
  1538. (struct i40e_virtchnl_vlan_filter_list *)msg;
  1539. struct i40e_pf *pf = vf->pf;
  1540. struct i40e_vsi *vsi = NULL;
  1541. u16 vsi_id = vfl->vsi_id;
  1542. i40e_status aq_ret = 0;
  1543. int i;
  1544. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1545. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1546. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1547. aq_ret = I40E_ERR_PARAM;
  1548. goto error_param;
  1549. }
  1550. for (i = 0; i < vfl->num_elements; i++) {
  1551. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  1552. aq_ret = I40E_ERR_PARAM;
  1553. goto error_param;
  1554. }
  1555. }
  1556. vsi = pf->vsi[vf->lan_vsi_idx];
  1557. if (vsi->info.pvid) {
  1558. aq_ret = I40E_ERR_PARAM;
  1559. goto error_param;
  1560. }
  1561. for (i = 0; i < vfl->num_elements; i++) {
  1562. int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
  1563. if (ret)
  1564. dev_err(&pf->pdev->dev,
  1565. "Unable to delete VF vlan filter %d, error %d\n",
  1566. vfl->vlan_id[i], ret);
  1567. }
  1568. error_param:
  1569. /* send the response to the VF */
  1570. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
  1571. }
  1572. /**
  1573. * i40e_vc_validate_vf_msg
  1574. * @vf: pointer to the VF info
  1575. * @msg: pointer to the msg buffer
  1576. * @msglen: msg length
  1577. * @msghndl: msg handle
  1578. *
  1579. * validate msg
  1580. **/
  1581. static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
  1582. u32 v_retval, u8 *msg, u16 msglen)
  1583. {
  1584. bool err_msg_format = false;
  1585. int valid_len;
  1586. /* Check if VF is disabled. */
  1587. if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
  1588. return I40E_ERR_PARAM;
  1589. /* Validate message length. */
  1590. switch (v_opcode) {
  1591. case I40E_VIRTCHNL_OP_VERSION:
  1592. valid_len = sizeof(struct i40e_virtchnl_version_info);
  1593. break;
  1594. case I40E_VIRTCHNL_OP_RESET_VF:
  1595. valid_len = 0;
  1596. break;
  1597. case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
  1598. if (VF_IS_V11(vf))
  1599. valid_len = sizeof(u32);
  1600. else
  1601. valid_len = 0;
  1602. break;
  1603. case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
  1604. valid_len = sizeof(struct i40e_virtchnl_txq_info);
  1605. break;
  1606. case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
  1607. valid_len = sizeof(struct i40e_virtchnl_rxq_info);
  1608. break;
  1609. case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  1610. valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
  1611. if (msglen >= valid_len) {
  1612. struct i40e_virtchnl_vsi_queue_config_info *vqc =
  1613. (struct i40e_virtchnl_vsi_queue_config_info *)msg;
  1614. valid_len += (vqc->num_queue_pairs *
  1615. sizeof(struct
  1616. i40e_virtchnl_queue_pair_info));
  1617. if (vqc->num_queue_pairs == 0)
  1618. err_msg_format = true;
  1619. }
  1620. break;
  1621. case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
  1622. valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
  1623. if (msglen >= valid_len) {
  1624. struct i40e_virtchnl_irq_map_info *vimi =
  1625. (struct i40e_virtchnl_irq_map_info *)msg;
  1626. valid_len += (vimi->num_vectors *
  1627. sizeof(struct i40e_virtchnl_vector_map));
  1628. if (vimi->num_vectors == 0)
  1629. err_msg_format = true;
  1630. }
  1631. break;
  1632. case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
  1633. case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
  1634. valid_len = sizeof(struct i40e_virtchnl_queue_select);
  1635. break;
  1636. case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
  1637. case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
  1638. valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
  1639. if (msglen >= valid_len) {
  1640. struct i40e_virtchnl_ether_addr_list *veal =
  1641. (struct i40e_virtchnl_ether_addr_list *)msg;
  1642. valid_len += veal->num_elements *
  1643. sizeof(struct i40e_virtchnl_ether_addr);
  1644. if (veal->num_elements == 0)
  1645. err_msg_format = true;
  1646. }
  1647. break;
  1648. case I40E_VIRTCHNL_OP_ADD_VLAN:
  1649. case I40E_VIRTCHNL_OP_DEL_VLAN:
  1650. valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
  1651. if (msglen >= valid_len) {
  1652. struct i40e_virtchnl_vlan_filter_list *vfl =
  1653. (struct i40e_virtchnl_vlan_filter_list *)msg;
  1654. valid_len += vfl->num_elements * sizeof(u16);
  1655. if (vfl->num_elements == 0)
  1656. err_msg_format = true;
  1657. }
  1658. break;
  1659. case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
  1660. valid_len = sizeof(struct i40e_virtchnl_promisc_info);
  1661. break;
  1662. case I40E_VIRTCHNL_OP_GET_STATS:
  1663. valid_len = sizeof(struct i40e_virtchnl_queue_select);
  1664. break;
  1665. /* These are always errors coming from the VF. */
  1666. case I40E_VIRTCHNL_OP_EVENT:
  1667. case I40E_VIRTCHNL_OP_UNKNOWN:
  1668. default:
  1669. return -EPERM;
  1670. break;
  1671. }
  1672. /* few more checks */
  1673. if ((valid_len != msglen) || (err_msg_format)) {
  1674. i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
  1675. return -EINVAL;
  1676. } else {
  1677. return 0;
  1678. }
  1679. }
  1680. /**
  1681. * i40e_vc_process_vf_msg
  1682. * @pf: pointer to the PF structure
  1683. * @vf_id: source VF id
  1684. * @msg: pointer to the msg buffer
  1685. * @msglen: msg length
  1686. * @msghndl: msg handle
  1687. *
  1688. * called from the common aeq/arq handler to
  1689. * process request from VF
  1690. **/
  1691. int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
  1692. u32 v_retval, u8 *msg, u16 msglen)
  1693. {
  1694. struct i40e_hw *hw = &pf->hw;
  1695. unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
  1696. struct i40e_vf *vf;
  1697. int ret;
  1698. pf->vf_aq_requests++;
  1699. if (local_vf_id >= pf->num_alloc_vfs)
  1700. return -EINVAL;
  1701. vf = &(pf->vf[local_vf_id]);
  1702. /* perform basic checks on the msg */
  1703. ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
  1704. if (ret) {
  1705. dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
  1706. local_vf_id, v_opcode, msglen);
  1707. return ret;
  1708. }
  1709. switch (v_opcode) {
  1710. case I40E_VIRTCHNL_OP_VERSION:
  1711. ret = i40e_vc_get_version_msg(vf, msg);
  1712. break;
  1713. case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
  1714. ret = i40e_vc_get_vf_resources_msg(vf, msg);
  1715. break;
  1716. case I40E_VIRTCHNL_OP_RESET_VF:
  1717. i40e_vc_reset_vf_msg(vf);
  1718. ret = 0;
  1719. break;
  1720. case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
  1721. ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
  1722. break;
  1723. case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  1724. ret = i40e_vc_config_queues_msg(vf, msg, msglen);
  1725. break;
  1726. case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
  1727. ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
  1728. break;
  1729. case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
  1730. ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
  1731. i40e_vc_notify_vf_link_state(vf);
  1732. break;
  1733. case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
  1734. ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
  1735. break;
  1736. case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
  1737. ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
  1738. break;
  1739. case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
  1740. ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
  1741. break;
  1742. case I40E_VIRTCHNL_OP_ADD_VLAN:
  1743. ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
  1744. break;
  1745. case I40E_VIRTCHNL_OP_DEL_VLAN:
  1746. ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
  1747. break;
  1748. case I40E_VIRTCHNL_OP_GET_STATS:
  1749. ret = i40e_vc_get_stats_msg(vf, msg, msglen);
  1750. break;
  1751. case I40E_VIRTCHNL_OP_UNKNOWN:
  1752. default:
  1753. dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
  1754. v_opcode, local_vf_id);
  1755. ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
  1756. I40E_ERR_NOT_IMPLEMENTED);
  1757. break;
  1758. }
  1759. return ret;
  1760. }
  1761. /**
  1762. * i40e_vc_process_vflr_event
  1763. * @pf: pointer to the PF structure
  1764. *
  1765. * called from the vlfr irq handler to
  1766. * free up VF resources and state variables
  1767. **/
  1768. int i40e_vc_process_vflr_event(struct i40e_pf *pf)
  1769. {
  1770. u32 reg, reg_idx, bit_idx, vf_id;
  1771. struct i40e_hw *hw = &pf->hw;
  1772. struct i40e_vf *vf;
  1773. if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  1774. return 0;
  1775. /* re-enable vflr interrupt cause */
  1776. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  1777. reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
  1778. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  1779. i40e_flush(hw);
  1780. clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  1781. for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
  1782. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  1783. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  1784. /* read GLGEN_VFLRSTAT register to find out the flr VFs */
  1785. vf = &pf->vf[vf_id];
  1786. reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
  1787. if (reg & BIT(bit_idx)) {
  1788. /* clear the bit in GLGEN_VFLRSTAT */
  1789. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  1790. if (!test_bit(__I40E_DOWN, &pf->state))
  1791. i40e_reset_vf(vf, true);
  1792. }
  1793. }
  1794. return 0;
  1795. }
  1796. /**
  1797. * i40e_ndo_set_vf_mac
  1798. * @netdev: network interface device structure
  1799. * @vf_id: VF identifier
  1800. * @mac: mac address
  1801. *
  1802. * program VF mac address
  1803. **/
  1804. int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
  1805. {
  1806. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1807. struct i40e_vsi *vsi = np->vsi;
  1808. struct i40e_pf *pf = vsi->back;
  1809. struct i40e_mac_filter *f;
  1810. struct i40e_vf *vf;
  1811. int ret = 0;
  1812. /* validate the request */
  1813. if (vf_id >= pf->num_alloc_vfs) {
  1814. dev_err(&pf->pdev->dev,
  1815. "Invalid VF Identifier %d\n", vf_id);
  1816. ret = -EINVAL;
  1817. goto error_param;
  1818. }
  1819. vf = &(pf->vf[vf_id]);
  1820. vsi = pf->vsi[vf->lan_vsi_idx];
  1821. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  1822. dev_err(&pf->pdev->dev,
  1823. "Uninitialized VF %d\n", vf_id);
  1824. ret = -EINVAL;
  1825. goto error_param;
  1826. }
  1827. if (!is_valid_ether_addr(mac)) {
  1828. dev_err(&pf->pdev->dev,
  1829. "Invalid VF ethernet address\n");
  1830. ret = -EINVAL;
  1831. goto error_param;
  1832. }
  1833. /* delete the temporary mac address */
  1834. i40e_del_filter(vsi, vf->default_lan_addr.addr,
  1835. vf->port_vlan_id ? vf->port_vlan_id : -1,
  1836. true, false);
  1837. /* Delete all the filters for this VSI - we're going to kill it
  1838. * anyway.
  1839. */
  1840. list_for_each_entry(f, &vsi->mac_filter_list, list)
  1841. i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
  1842. dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
  1843. /* program mac filter */
  1844. if (i40e_sync_vsi_filters(vsi)) {
  1845. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  1846. ret = -EIO;
  1847. goto error_param;
  1848. }
  1849. ether_addr_copy(vf->default_lan_addr.addr, mac);
  1850. vf->pf_set_mac = true;
  1851. /* Force the VF driver stop so it has to reload with new MAC address */
  1852. i40e_vc_disable_vf(pf, vf);
  1853. dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
  1854. error_param:
  1855. return ret;
  1856. }
  1857. /**
  1858. * i40e_ndo_set_vf_port_vlan
  1859. * @netdev: network interface device structure
  1860. * @vf_id: VF identifier
  1861. * @vlan_id: mac address
  1862. * @qos: priority setting
  1863. *
  1864. * program VF vlan id and/or qos
  1865. **/
  1866. int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
  1867. int vf_id, u16 vlan_id, u8 qos)
  1868. {
  1869. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1870. struct i40e_pf *pf = np->vsi->back;
  1871. struct i40e_vsi *vsi;
  1872. struct i40e_vf *vf;
  1873. int ret = 0;
  1874. /* validate the request */
  1875. if (vf_id >= pf->num_alloc_vfs) {
  1876. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  1877. ret = -EINVAL;
  1878. goto error_pvid;
  1879. }
  1880. if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
  1881. dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
  1882. ret = -EINVAL;
  1883. goto error_pvid;
  1884. }
  1885. vf = &(pf->vf[vf_id]);
  1886. vsi = pf->vsi[vf->lan_vsi_idx];
  1887. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  1888. dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
  1889. ret = -EINVAL;
  1890. goto error_pvid;
  1891. }
  1892. if (le16_to_cpu(vsi->info.pvid) ==
  1893. (vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT)))
  1894. /* duplicate request, so just return success */
  1895. goto error_pvid;
  1896. if (le16_to_cpu(vsi->info.pvid) == 0 && i40e_is_vsi_in_vlan(vsi)) {
  1897. dev_err(&pf->pdev->dev,
  1898. "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",
  1899. vf_id);
  1900. /* Administrator Error - knock the VF offline until he does
  1901. * the right thing by reconfiguring his network correctly
  1902. * and then reloading the VF driver.
  1903. */
  1904. i40e_vc_disable_vf(pf, vf);
  1905. }
  1906. /* Check for condition where there was already a port VLAN ID
  1907. * filter set and now it is being deleted by setting it to zero.
  1908. * Additionally check for the condition where there was a port
  1909. * VLAN but now there is a new and different port VLAN being set.
  1910. * Before deleting all the old VLAN filters we must add new ones
  1911. * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
  1912. * MAC addresses deleted.
  1913. */
  1914. if ((!(vlan_id || qos) ||
  1915. (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
  1916. vsi->info.pvid)
  1917. ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
  1918. if (vsi->info.pvid) {
  1919. /* kill old VLAN */
  1920. ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
  1921. VLAN_VID_MASK));
  1922. if (ret) {
  1923. dev_info(&vsi->back->pdev->dev,
  1924. "remove VLAN failed, ret=%d, aq_err=%d\n",
  1925. ret, pf->hw.aq.asq_last_status);
  1926. }
  1927. }
  1928. if (vlan_id || qos)
  1929. ret = i40e_vsi_add_pvid(vsi,
  1930. vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
  1931. else
  1932. i40e_vsi_remove_pvid(vsi);
  1933. if (vlan_id) {
  1934. dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
  1935. vlan_id, qos, vf_id);
  1936. /* add new VLAN filter */
  1937. ret = i40e_vsi_add_vlan(vsi, vlan_id);
  1938. if (ret) {
  1939. dev_info(&vsi->back->pdev->dev,
  1940. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  1941. vsi->back->hw.aq.asq_last_status);
  1942. goto error_pvid;
  1943. }
  1944. /* Kill non-vlan MAC filters - ignore error return since
  1945. * there might not be any non-vlan MAC filters.
  1946. */
  1947. i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
  1948. }
  1949. if (ret) {
  1950. dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
  1951. goto error_pvid;
  1952. }
  1953. /* The Port VLAN needs to be saved across resets the same as the
  1954. * default LAN MAC address.
  1955. */
  1956. vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
  1957. ret = 0;
  1958. error_pvid:
  1959. return ret;
  1960. }
  1961. #define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
  1962. #define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
  1963. /**
  1964. * i40e_ndo_set_vf_bw
  1965. * @netdev: network interface device structure
  1966. * @vf_id: VF identifier
  1967. * @tx_rate: Tx rate
  1968. *
  1969. * configure VF Tx rate
  1970. **/
  1971. int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
  1972. int max_tx_rate)
  1973. {
  1974. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1975. struct i40e_pf *pf = np->vsi->back;
  1976. struct i40e_vsi *vsi;
  1977. struct i40e_vf *vf;
  1978. int speed = 0;
  1979. int ret = 0;
  1980. /* validate the request */
  1981. if (vf_id >= pf->num_alloc_vfs) {
  1982. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
  1983. ret = -EINVAL;
  1984. goto error;
  1985. }
  1986. if (min_tx_rate) {
  1987. dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
  1988. min_tx_rate, vf_id);
  1989. return -EINVAL;
  1990. }
  1991. vf = &(pf->vf[vf_id]);
  1992. vsi = pf->vsi[vf->lan_vsi_idx];
  1993. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  1994. dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
  1995. ret = -EINVAL;
  1996. goto error;
  1997. }
  1998. switch (pf->hw.phy.link_info.link_speed) {
  1999. case I40E_LINK_SPEED_40GB:
  2000. speed = 40000;
  2001. break;
  2002. case I40E_LINK_SPEED_10GB:
  2003. speed = 10000;
  2004. break;
  2005. case I40E_LINK_SPEED_1GB:
  2006. speed = 1000;
  2007. break;
  2008. default:
  2009. break;
  2010. }
  2011. if (max_tx_rate > speed) {
  2012. dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
  2013. max_tx_rate, vf->vf_id);
  2014. ret = -EINVAL;
  2015. goto error;
  2016. }
  2017. if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
  2018. dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
  2019. max_tx_rate = 50;
  2020. }
  2021. /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
  2022. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
  2023. max_tx_rate / I40E_BW_CREDIT_DIVISOR,
  2024. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  2025. if (ret) {
  2026. dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
  2027. ret);
  2028. ret = -EIO;
  2029. goto error;
  2030. }
  2031. vf->tx_rate = max_tx_rate;
  2032. error:
  2033. return ret;
  2034. }
  2035. /**
  2036. * i40e_ndo_get_vf_config
  2037. * @netdev: network interface device structure
  2038. * @vf_id: VF identifier
  2039. * @ivi: VF configuration structure
  2040. *
  2041. * return VF configuration
  2042. **/
  2043. int i40e_ndo_get_vf_config(struct net_device *netdev,
  2044. int vf_id, struct ifla_vf_info *ivi)
  2045. {
  2046. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2047. struct i40e_vsi *vsi = np->vsi;
  2048. struct i40e_pf *pf = vsi->back;
  2049. struct i40e_vf *vf;
  2050. int ret = 0;
  2051. /* validate the request */
  2052. if (vf_id >= pf->num_alloc_vfs) {
  2053. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2054. ret = -EINVAL;
  2055. goto error_param;
  2056. }
  2057. vf = &(pf->vf[vf_id]);
  2058. /* first vsi is always the LAN vsi */
  2059. vsi = pf->vsi[vf->lan_vsi_idx];
  2060. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  2061. dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
  2062. ret = -EINVAL;
  2063. goto error_param;
  2064. }
  2065. ivi->vf = vf_id;
  2066. memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
  2067. ivi->max_tx_rate = vf->tx_rate;
  2068. ivi->min_tx_rate = 0;
  2069. ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
  2070. ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
  2071. I40E_VLAN_PRIORITY_SHIFT;
  2072. if (vf->link_forced == false)
  2073. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  2074. else if (vf->link_up == true)
  2075. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  2076. else
  2077. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  2078. ivi->spoofchk = vf->spoofchk;
  2079. ret = 0;
  2080. error_param:
  2081. return ret;
  2082. }
  2083. /**
  2084. * i40e_ndo_set_vf_link_state
  2085. * @netdev: network interface device structure
  2086. * @vf_id: VF identifier
  2087. * @link: required link state
  2088. *
  2089. * Set the link state of a specified VF, regardless of physical link state
  2090. **/
  2091. int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
  2092. {
  2093. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2094. struct i40e_pf *pf = np->vsi->back;
  2095. struct i40e_virtchnl_pf_event pfe;
  2096. struct i40e_hw *hw = &pf->hw;
  2097. struct i40e_vf *vf;
  2098. int abs_vf_id;
  2099. int ret = 0;
  2100. /* validate the request */
  2101. if (vf_id >= pf->num_alloc_vfs) {
  2102. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2103. ret = -EINVAL;
  2104. goto error_out;
  2105. }
  2106. vf = &pf->vf[vf_id];
  2107. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  2108. pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
  2109. pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
  2110. switch (link) {
  2111. case IFLA_VF_LINK_STATE_AUTO:
  2112. vf->link_forced = false;
  2113. pfe.event_data.link_event.link_status =
  2114. pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
  2115. pfe.event_data.link_event.link_speed =
  2116. pf->hw.phy.link_info.link_speed;
  2117. break;
  2118. case IFLA_VF_LINK_STATE_ENABLE:
  2119. vf->link_forced = true;
  2120. vf->link_up = true;
  2121. pfe.event_data.link_event.link_status = true;
  2122. pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
  2123. break;
  2124. case IFLA_VF_LINK_STATE_DISABLE:
  2125. vf->link_forced = true;
  2126. vf->link_up = false;
  2127. pfe.event_data.link_event.link_status = false;
  2128. pfe.event_data.link_event.link_speed = 0;
  2129. break;
  2130. default:
  2131. ret = -EINVAL;
  2132. goto error_out;
  2133. }
  2134. /* Notify the VF of its new link state */
  2135. i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
  2136. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  2137. error_out:
  2138. return ret;
  2139. }
  2140. /**
  2141. * i40e_ndo_set_vf_spoofchk
  2142. * @netdev: network interface device structure
  2143. * @vf_id: VF identifier
  2144. * @enable: flag to enable or disable feature
  2145. *
  2146. * Enable or disable VF spoof checking
  2147. **/
  2148. int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
  2149. {
  2150. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2151. struct i40e_vsi *vsi = np->vsi;
  2152. struct i40e_pf *pf = vsi->back;
  2153. struct i40e_vsi_context ctxt;
  2154. struct i40e_hw *hw = &pf->hw;
  2155. struct i40e_vf *vf;
  2156. int ret = 0;
  2157. /* validate the request */
  2158. if (vf_id >= pf->num_alloc_vfs) {
  2159. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2160. ret = -EINVAL;
  2161. goto out;
  2162. }
  2163. vf = &(pf->vf[vf_id]);
  2164. if (enable == vf->spoofchk)
  2165. goto out;
  2166. vf->spoofchk = enable;
  2167. memset(&ctxt, 0, sizeof(ctxt));
  2168. ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
  2169. ctxt.pf_num = pf->hw.pf_id;
  2170. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  2171. if (enable)
  2172. ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  2173. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  2174. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  2175. if (ret) {
  2176. dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
  2177. ret);
  2178. ret = -EIO;
  2179. }
  2180. out:
  2181. return ret;
  2182. }