i40e_virtchnl_pf.c 63 KB

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