i40e_virtchnl_pf.c 64 KB

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