i40e_virtchnl_pf.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2014 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. /* warn the VF */
  577. clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
  578. /* In the case of a VFLR, the HW has already reset the VF and we
  579. * just need to clean up, so don't hit the VFRTRIG register.
  580. */
  581. if (!flr) {
  582. /* reset vf using VPGEN_VFRTRIG reg */
  583. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  584. reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  585. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  586. i40e_flush(hw);
  587. }
  588. if (i40e_quiesce_vf_pci(vf))
  589. dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
  590. vf->vf_id);
  591. /* poll VPGEN_VFRSTAT reg to make sure
  592. * that reset is complete
  593. */
  594. for (i = 0; i < 100; i++) {
  595. /* vf reset requires driver to first reset the
  596. * vf and then poll the status register to make sure
  597. * that the requested op was completed
  598. * successfully
  599. */
  600. udelay(10);
  601. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  602. if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
  603. rsd = true;
  604. break;
  605. }
  606. }
  607. if (!rsd)
  608. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  609. vf->vf_id);
  610. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
  611. /* clear the reset bit in the VPGEN_VFRTRIG reg */
  612. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  613. reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  614. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  615. /* On initial reset, we won't have any queues */
  616. if (vf->lan_vsi_index == 0)
  617. goto complete_reset;
  618. i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_index], false);
  619. complete_reset:
  620. /* reallocate vf resources to reset the VSI state */
  621. i40e_free_vf_res(vf);
  622. i40e_alloc_vf_res(vf);
  623. i40e_enable_vf_mappings(vf);
  624. set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
  625. /* tell the VF the reset is done */
  626. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
  627. i40e_flush(hw);
  628. }
  629. #ifdef CONFIG_PCI_IOV
  630. /**
  631. * i40e_enable_pf_switch_lb
  632. * @pf: pointer to the pf structure
  633. *
  634. * enable switch loop back or die - no point in a return value
  635. **/
  636. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  637. {
  638. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  639. struct i40e_vsi_context ctxt;
  640. int aq_ret;
  641. ctxt.seid = pf->main_vsi_seid;
  642. ctxt.pf_num = pf->hw.pf_id;
  643. ctxt.vf_num = 0;
  644. aq_ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  645. if (aq_ret) {
  646. dev_info(&pf->pdev->dev,
  647. "%s couldn't get pf vsi config, err %d, aq_err %d\n",
  648. __func__, aq_ret, pf->hw.aq.asq_last_status);
  649. return;
  650. }
  651. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  652. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  653. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  654. aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  655. if (aq_ret) {
  656. dev_info(&pf->pdev->dev,
  657. "%s: update vsi switch failed, aq_err=%d\n",
  658. __func__, vsi->back->hw.aq.asq_last_status);
  659. }
  660. }
  661. #endif
  662. /**
  663. * i40e_disable_pf_switch_lb
  664. * @pf: pointer to the pf structure
  665. *
  666. * disable switch loop back or die - no point in a return value
  667. **/
  668. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  669. {
  670. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  671. struct i40e_vsi_context ctxt;
  672. int aq_ret;
  673. ctxt.seid = pf->main_vsi_seid;
  674. ctxt.pf_num = pf->hw.pf_id;
  675. ctxt.vf_num = 0;
  676. aq_ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  677. if (aq_ret) {
  678. dev_info(&pf->pdev->dev,
  679. "%s couldn't get pf vsi config, err %d, aq_err %d\n",
  680. __func__, aq_ret, pf->hw.aq.asq_last_status);
  681. return;
  682. }
  683. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  684. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  685. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  686. aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  687. if (aq_ret) {
  688. dev_info(&pf->pdev->dev,
  689. "%s: update vsi switch failed, aq_err=%d\n",
  690. __func__, vsi->back->hw.aq.asq_last_status);
  691. }
  692. }
  693. /**
  694. * i40e_free_vfs
  695. * @pf: pointer to the pf structure
  696. *
  697. * free vf resources
  698. **/
  699. void i40e_free_vfs(struct i40e_pf *pf)
  700. {
  701. struct i40e_hw *hw = &pf->hw;
  702. u32 reg_idx, bit_idx;
  703. int i, tmp, vf_id;
  704. if (!pf->vf)
  705. return;
  706. /* Disable interrupt 0 so we don't try to handle the VFLR. */
  707. i40e_irq_dynamic_disable_icr0(pf);
  708. mdelay(10); /* let any messages in transit get finished up */
  709. /* free up vf resources */
  710. tmp = pf->num_alloc_vfs;
  711. pf->num_alloc_vfs = 0;
  712. for (i = 0; i < tmp; i++) {
  713. if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
  714. i40e_free_vf_res(&pf->vf[i]);
  715. /* disable qp mappings */
  716. i40e_disable_vf_mappings(&pf->vf[i]);
  717. }
  718. kfree(pf->vf);
  719. pf->vf = NULL;
  720. /* This check is for when the driver is unloaded while VFs are
  721. * assigned. Setting the number of VFs to 0 through sysfs is caught
  722. * before this function ever gets called.
  723. */
  724. if (!pci_vfs_assigned(pf->pdev)) {
  725. pci_disable_sriov(pf->pdev);
  726. /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
  727. * work correctly when SR-IOV gets re-enabled.
  728. */
  729. for (vf_id = 0; vf_id < tmp; vf_id++) {
  730. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  731. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  732. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
  733. }
  734. i40e_disable_pf_switch_lb(pf);
  735. } else {
  736. dev_warn(&pf->pdev->dev,
  737. "unable to disable SR-IOV because VFs are assigned.\n");
  738. }
  739. /* Re-enable interrupt 0. */
  740. i40e_irq_dynamic_enable_icr0(pf);
  741. }
  742. #ifdef CONFIG_PCI_IOV
  743. /**
  744. * i40e_alloc_vfs
  745. * @pf: pointer to the pf structure
  746. * @num_alloc_vfs: number of vfs to allocate
  747. *
  748. * allocate vf resources
  749. **/
  750. int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
  751. {
  752. struct i40e_vf *vfs;
  753. int i, ret = 0;
  754. /* Disable interrupt 0 so we don't try to handle the VFLR. */
  755. i40e_irq_dynamic_disable_icr0(pf);
  756. /* Check to see if we're just allocating resources for extant VFs */
  757. if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
  758. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  759. if (ret) {
  760. dev_err(&pf->pdev->dev,
  761. "Failed to enable SR-IOV, error %d.\n", ret);
  762. pf->num_alloc_vfs = 0;
  763. goto err_iov;
  764. }
  765. }
  766. /* allocate memory */
  767. vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
  768. if (!vfs) {
  769. ret = -ENOMEM;
  770. goto err_alloc;
  771. }
  772. pf->vf = vfs;
  773. /* apply default profile */
  774. for (i = 0; i < num_alloc_vfs; i++) {
  775. vfs[i].pf = pf;
  776. vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
  777. vfs[i].vf_id = i;
  778. /* assign default capabilities */
  779. set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
  780. vfs[i].spoofchk = true;
  781. /* vf resources get allocated during reset */
  782. i40e_reset_vf(&vfs[i], false);
  783. /* enable vf vplan_qtable mappings */
  784. i40e_enable_vf_mappings(&vfs[i]);
  785. }
  786. pf->num_alloc_vfs = num_alloc_vfs;
  787. i40e_enable_pf_switch_lb(pf);
  788. err_alloc:
  789. if (ret)
  790. i40e_free_vfs(pf);
  791. err_iov:
  792. /* Re-enable interrupt 0. */
  793. i40e_irq_dynamic_enable_icr0(pf);
  794. return ret;
  795. }
  796. #endif
  797. /**
  798. * i40e_pci_sriov_enable
  799. * @pdev: pointer to a pci_dev structure
  800. * @num_vfs: number of vfs to allocate
  801. *
  802. * Enable or change the number of VFs
  803. **/
  804. static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
  805. {
  806. #ifdef CONFIG_PCI_IOV
  807. struct i40e_pf *pf = pci_get_drvdata(pdev);
  808. int pre_existing_vfs = pci_num_vf(pdev);
  809. int err = 0;
  810. dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
  811. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  812. i40e_free_vfs(pf);
  813. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  814. goto out;
  815. if (num_vfs > pf->num_req_vfs) {
  816. err = -EPERM;
  817. goto err_out;
  818. }
  819. err = i40e_alloc_vfs(pf, num_vfs);
  820. if (err) {
  821. dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
  822. goto err_out;
  823. }
  824. out:
  825. return num_vfs;
  826. err_out:
  827. return err;
  828. #endif
  829. return 0;
  830. }
  831. /**
  832. * i40e_pci_sriov_configure
  833. * @pdev: pointer to a pci_dev structure
  834. * @num_vfs: number of vfs to allocate
  835. *
  836. * Enable or change the number of VFs. Called when the user updates the number
  837. * of VFs in sysfs.
  838. **/
  839. int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
  840. {
  841. struct i40e_pf *pf = pci_get_drvdata(pdev);
  842. if (num_vfs)
  843. return i40e_pci_sriov_enable(pdev, num_vfs);
  844. if (!pci_vfs_assigned(pf->pdev)) {
  845. i40e_free_vfs(pf);
  846. } else {
  847. dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
  848. return -EINVAL;
  849. }
  850. return 0;
  851. }
  852. /***********************virtual channel routines******************/
  853. /**
  854. * i40e_vc_send_msg_to_vf
  855. * @vf: pointer to the vf info
  856. * @v_opcode: virtual channel opcode
  857. * @v_retval: virtual channel return value
  858. * @msg: pointer to the msg buffer
  859. * @msglen: msg length
  860. *
  861. * send msg to vf
  862. **/
  863. static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
  864. u32 v_retval, u8 *msg, u16 msglen)
  865. {
  866. struct i40e_pf *pf;
  867. struct i40e_hw *hw;
  868. int abs_vf_id;
  869. i40e_status aq_ret;
  870. /* validate the request */
  871. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  872. return -EINVAL;
  873. pf = vf->pf;
  874. hw = &pf->hw;
  875. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  876. /* single place to detect unsuccessful return values */
  877. if (v_retval) {
  878. vf->num_invalid_msgs++;
  879. dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
  880. v_opcode, v_retval);
  881. if (vf->num_invalid_msgs >
  882. I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
  883. dev_err(&pf->pdev->dev,
  884. "Number of invalid messages exceeded for VF %d\n",
  885. vf->vf_id);
  886. dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
  887. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  888. }
  889. } else {
  890. vf->num_valid_msgs++;
  891. }
  892. aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  893. msg, msglen, NULL);
  894. if (aq_ret) {
  895. dev_err(&pf->pdev->dev,
  896. "Unable to send the message to VF %d aq_err %d\n",
  897. vf->vf_id, pf->hw.aq.asq_last_status);
  898. return -EIO;
  899. }
  900. return 0;
  901. }
  902. /**
  903. * i40e_vc_send_resp_to_vf
  904. * @vf: pointer to the vf info
  905. * @opcode: operation code
  906. * @retval: return value
  907. *
  908. * send resp msg to vf
  909. **/
  910. static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
  911. enum i40e_virtchnl_ops opcode,
  912. i40e_status retval)
  913. {
  914. return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
  915. }
  916. /**
  917. * i40e_vc_get_version_msg
  918. * @vf: pointer to the vf info
  919. *
  920. * called from the vf to request the API version used by the PF
  921. **/
  922. static int i40e_vc_get_version_msg(struct i40e_vf *vf)
  923. {
  924. struct i40e_virtchnl_version_info info = {
  925. I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
  926. };
  927. return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
  928. I40E_SUCCESS, (u8 *)&info,
  929. sizeof(struct
  930. i40e_virtchnl_version_info));
  931. }
  932. /**
  933. * i40e_vc_get_vf_resources_msg
  934. * @vf: pointer to the vf info
  935. * @msg: pointer to the msg buffer
  936. * @msglen: msg length
  937. *
  938. * called from the vf to request its resources
  939. **/
  940. static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf)
  941. {
  942. struct i40e_virtchnl_vf_resource *vfres = NULL;
  943. struct i40e_pf *pf = vf->pf;
  944. i40e_status aq_ret = 0;
  945. struct i40e_vsi *vsi;
  946. int i = 0, len = 0;
  947. int num_vsis = 1;
  948. int ret;
  949. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  950. aq_ret = I40E_ERR_PARAM;
  951. goto err;
  952. }
  953. len = (sizeof(struct i40e_virtchnl_vf_resource) +
  954. sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
  955. vfres = kzalloc(len, GFP_KERNEL);
  956. if (!vfres) {
  957. aq_ret = I40E_ERR_NO_MEMORY;
  958. len = 0;
  959. goto err;
  960. }
  961. vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
  962. vsi = pf->vsi[vf->lan_vsi_index];
  963. if (!vsi->info.pvid)
  964. vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
  965. vfres->num_vsis = num_vsis;
  966. vfres->num_queue_pairs = vf->num_queue_pairs;
  967. vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
  968. if (vf->lan_vsi_index) {
  969. vfres->vsi_res[i].vsi_id = vf->lan_vsi_index;
  970. vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
  971. vfres->vsi_res[i].num_queue_pairs =
  972. pf->vsi[vf->lan_vsi_index]->alloc_queue_pairs;
  973. memcpy(vfres->vsi_res[i].default_mac_addr,
  974. vf->default_lan_addr.addr, ETH_ALEN);
  975. i++;
  976. }
  977. set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
  978. err:
  979. /* send the response back to the vf */
  980. ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
  981. aq_ret, (u8 *)vfres, len);
  982. kfree(vfres);
  983. return ret;
  984. }
  985. /**
  986. * i40e_vc_reset_vf_msg
  987. * @vf: pointer to the vf info
  988. * @msg: pointer to the msg buffer
  989. * @msglen: msg length
  990. *
  991. * called from the vf to reset itself,
  992. * unlike other virtchnl messages, pf driver
  993. * doesn't send the response back to the vf
  994. **/
  995. static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
  996. {
  997. if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
  998. i40e_reset_vf(vf, false);
  999. }
  1000. /**
  1001. * i40e_vc_config_promiscuous_mode_msg
  1002. * @vf: pointer to the vf info
  1003. * @msg: pointer to the msg buffer
  1004. * @msglen: msg length
  1005. *
  1006. * called from the vf to configure the promiscuous mode of
  1007. * vf vsis
  1008. **/
  1009. static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
  1010. u8 *msg, u16 msglen)
  1011. {
  1012. struct i40e_virtchnl_promisc_info *info =
  1013. (struct i40e_virtchnl_promisc_info *)msg;
  1014. struct i40e_pf *pf = vf->pf;
  1015. struct i40e_hw *hw = &pf->hw;
  1016. struct i40e_vsi *vsi;
  1017. bool allmulti = false;
  1018. i40e_status aq_ret;
  1019. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1020. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1021. !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
  1022. (pf->vsi[info->vsi_id]->type != I40E_VSI_FCOE)) {
  1023. aq_ret = I40E_ERR_PARAM;
  1024. goto error_param;
  1025. }
  1026. vsi = pf->vsi[info->vsi_id];
  1027. if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
  1028. allmulti = true;
  1029. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
  1030. allmulti, NULL);
  1031. error_param:
  1032. /* send the response to the vf */
  1033. return i40e_vc_send_resp_to_vf(vf,
  1034. I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
  1035. aq_ret);
  1036. }
  1037. /**
  1038. * i40e_vc_config_queues_msg
  1039. * @vf: pointer to the vf info
  1040. * @msg: pointer to the msg buffer
  1041. * @msglen: msg length
  1042. *
  1043. * called from the vf to configure the rx/tx
  1044. * queues
  1045. **/
  1046. static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1047. {
  1048. struct i40e_virtchnl_vsi_queue_config_info *qci =
  1049. (struct i40e_virtchnl_vsi_queue_config_info *)msg;
  1050. struct i40e_virtchnl_queue_pair_info *qpi;
  1051. struct i40e_pf *pf = vf->pf;
  1052. u16 vsi_id, vsi_queue_id;
  1053. i40e_status aq_ret = 0;
  1054. int i;
  1055. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1056. aq_ret = I40E_ERR_PARAM;
  1057. goto error_param;
  1058. }
  1059. vsi_id = qci->vsi_id;
  1060. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1061. aq_ret = I40E_ERR_PARAM;
  1062. goto error_param;
  1063. }
  1064. for (i = 0; i < qci->num_queue_pairs; i++) {
  1065. qpi = &qci->qpair[i];
  1066. vsi_queue_id = qpi->txq.queue_id;
  1067. if ((qpi->txq.vsi_id != vsi_id) ||
  1068. (qpi->rxq.vsi_id != vsi_id) ||
  1069. (qpi->rxq.queue_id != vsi_queue_id) ||
  1070. !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
  1071. aq_ret = I40E_ERR_PARAM;
  1072. goto error_param;
  1073. }
  1074. if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
  1075. &qpi->rxq) ||
  1076. i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
  1077. &qpi->txq)) {
  1078. aq_ret = I40E_ERR_PARAM;
  1079. goto error_param;
  1080. }
  1081. }
  1082. /* set vsi num_queue_pairs in use to num configured by vf */
  1083. pf->vsi[vf->lan_vsi_index]->num_queue_pairs = qci->num_queue_pairs;
  1084. error_param:
  1085. /* send the response to the vf */
  1086. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
  1087. aq_ret);
  1088. }
  1089. /**
  1090. * i40e_vc_config_irq_map_msg
  1091. * @vf: pointer to the vf info
  1092. * @msg: pointer to the msg buffer
  1093. * @msglen: msg length
  1094. *
  1095. * called from the vf to configure the irq to
  1096. * queue map
  1097. **/
  1098. static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1099. {
  1100. struct i40e_virtchnl_irq_map_info *irqmap_info =
  1101. (struct i40e_virtchnl_irq_map_info *)msg;
  1102. struct i40e_virtchnl_vector_map *map;
  1103. u16 vsi_id, vsi_queue_id, vector_id;
  1104. i40e_status aq_ret = 0;
  1105. unsigned long tempmap;
  1106. int i;
  1107. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1108. aq_ret = I40E_ERR_PARAM;
  1109. goto error_param;
  1110. }
  1111. for (i = 0; i < irqmap_info->num_vectors; i++) {
  1112. map = &irqmap_info->vecmap[i];
  1113. vector_id = map->vector_id;
  1114. vsi_id = map->vsi_id;
  1115. /* validate msg params */
  1116. if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
  1117. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1118. aq_ret = I40E_ERR_PARAM;
  1119. goto error_param;
  1120. }
  1121. /* lookout for the invalid queue index */
  1122. tempmap = map->rxq_map;
  1123. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  1124. if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
  1125. vsi_queue_id)) {
  1126. aq_ret = I40E_ERR_PARAM;
  1127. goto error_param;
  1128. }
  1129. }
  1130. tempmap = map->txq_map;
  1131. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  1132. if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
  1133. vsi_queue_id)) {
  1134. aq_ret = I40E_ERR_PARAM;
  1135. goto error_param;
  1136. }
  1137. }
  1138. i40e_config_irq_link_list(vf, vsi_id, map);
  1139. }
  1140. error_param:
  1141. /* send the response to the vf */
  1142. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
  1143. aq_ret);
  1144. }
  1145. /**
  1146. * i40e_vc_enable_queues_msg
  1147. * @vf: pointer to the vf info
  1148. * @msg: pointer to the msg buffer
  1149. * @msglen: msg length
  1150. *
  1151. * called from the vf to enable all or specific queue(s)
  1152. **/
  1153. static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1154. {
  1155. struct i40e_virtchnl_queue_select *vqs =
  1156. (struct i40e_virtchnl_queue_select *)msg;
  1157. struct i40e_pf *pf = vf->pf;
  1158. u16 vsi_id = vqs->vsi_id;
  1159. i40e_status aq_ret = 0;
  1160. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1161. aq_ret = I40E_ERR_PARAM;
  1162. goto error_param;
  1163. }
  1164. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1165. aq_ret = I40E_ERR_PARAM;
  1166. goto error_param;
  1167. }
  1168. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1169. aq_ret = I40E_ERR_PARAM;
  1170. goto error_param;
  1171. }
  1172. if (i40e_vsi_control_rings(pf->vsi[vsi_id], true))
  1173. aq_ret = I40E_ERR_TIMEOUT;
  1174. error_param:
  1175. /* send the response to the vf */
  1176. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
  1177. aq_ret);
  1178. }
  1179. /**
  1180. * i40e_vc_disable_queues_msg
  1181. * @vf: pointer to the vf info
  1182. * @msg: pointer to the msg buffer
  1183. * @msglen: msg length
  1184. *
  1185. * called from the vf to disable all or specific
  1186. * queue(s)
  1187. **/
  1188. static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1189. {
  1190. struct i40e_virtchnl_queue_select *vqs =
  1191. (struct i40e_virtchnl_queue_select *)msg;
  1192. struct i40e_pf *pf = vf->pf;
  1193. u16 vsi_id = vqs->vsi_id;
  1194. i40e_status aq_ret = 0;
  1195. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1196. aq_ret = I40E_ERR_PARAM;
  1197. goto error_param;
  1198. }
  1199. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1200. aq_ret = I40E_ERR_PARAM;
  1201. goto error_param;
  1202. }
  1203. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1204. aq_ret = I40E_ERR_PARAM;
  1205. goto error_param;
  1206. }
  1207. if (i40e_vsi_control_rings(pf->vsi[vsi_id], false))
  1208. aq_ret = I40E_ERR_TIMEOUT;
  1209. error_param:
  1210. /* send the response to the vf */
  1211. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
  1212. aq_ret);
  1213. }
  1214. /**
  1215. * i40e_vc_get_stats_msg
  1216. * @vf: pointer to the vf info
  1217. * @msg: pointer to the msg buffer
  1218. * @msglen: msg length
  1219. *
  1220. * called from the vf to get vsi stats
  1221. **/
  1222. static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1223. {
  1224. struct i40e_virtchnl_queue_select *vqs =
  1225. (struct i40e_virtchnl_queue_select *)msg;
  1226. struct i40e_pf *pf = vf->pf;
  1227. struct i40e_eth_stats stats;
  1228. i40e_status aq_ret = 0;
  1229. struct i40e_vsi *vsi;
  1230. memset(&stats, 0, sizeof(struct i40e_eth_stats));
  1231. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1232. aq_ret = I40E_ERR_PARAM;
  1233. goto error_param;
  1234. }
  1235. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1236. aq_ret = I40E_ERR_PARAM;
  1237. goto error_param;
  1238. }
  1239. vsi = pf->vsi[vqs->vsi_id];
  1240. if (!vsi) {
  1241. aq_ret = I40E_ERR_PARAM;
  1242. goto error_param;
  1243. }
  1244. i40e_update_eth_stats(vsi);
  1245. stats = vsi->eth_stats;
  1246. error_param:
  1247. /* send the response back to the vf */
  1248. return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
  1249. (u8 *)&stats, sizeof(stats));
  1250. }
  1251. /**
  1252. * i40e_check_vf_permission
  1253. * @vf: pointer to the vf info
  1254. * @macaddr: pointer to the MAC Address being checked
  1255. *
  1256. * Check if the VF has permission to add or delete unicast MAC address
  1257. * filters and return error code -EPERM if not. Then check if the
  1258. * address filter requested is broadcast or zero and if so return
  1259. * an invalid MAC address error code.
  1260. **/
  1261. static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
  1262. {
  1263. struct i40e_pf *pf = vf->pf;
  1264. int ret = 0;
  1265. if (is_broadcast_ether_addr(macaddr) ||
  1266. is_zero_ether_addr(macaddr)) {
  1267. dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
  1268. ret = I40E_ERR_INVALID_MAC_ADDR;
  1269. } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
  1270. !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
  1271. /* If the host VMM administrator has set the VF MAC address
  1272. * administratively via the ndo_set_vf_mac command then deny
  1273. * permission to the VF to add or delete unicast MAC addresses.
  1274. * The VF may request to set the MAC address filter already
  1275. * assigned to it so do not return an error in that case.
  1276. */
  1277. dev_err(&pf->pdev->dev,
  1278. "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
  1279. ret = -EPERM;
  1280. }
  1281. return ret;
  1282. }
  1283. /**
  1284. * i40e_vc_add_mac_addr_msg
  1285. * @vf: pointer to the vf info
  1286. * @msg: pointer to the msg buffer
  1287. * @msglen: msg length
  1288. *
  1289. * add guest mac address filter
  1290. **/
  1291. static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1292. {
  1293. struct i40e_virtchnl_ether_addr_list *al =
  1294. (struct i40e_virtchnl_ether_addr_list *)msg;
  1295. struct i40e_pf *pf = vf->pf;
  1296. struct i40e_vsi *vsi = NULL;
  1297. u16 vsi_id = al->vsi_id;
  1298. i40e_status ret = 0;
  1299. int i;
  1300. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1301. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1302. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1303. ret = I40E_ERR_PARAM;
  1304. goto error_param;
  1305. }
  1306. for (i = 0; i < al->num_elements; i++) {
  1307. ret = i40e_check_vf_permission(vf, al->list[i].addr);
  1308. if (ret)
  1309. goto error_param;
  1310. }
  1311. vsi = pf->vsi[vsi_id];
  1312. /* add new addresses to the list */
  1313. for (i = 0; i < al->num_elements; i++) {
  1314. struct i40e_mac_filter *f;
  1315. f = i40e_find_mac(vsi, al->list[i].addr, true, false);
  1316. if (!f) {
  1317. if (i40e_is_vsi_in_vlan(vsi))
  1318. f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
  1319. true, false);
  1320. else
  1321. f = i40e_add_filter(vsi, al->list[i].addr, -1,
  1322. true, false);
  1323. }
  1324. if (!f) {
  1325. dev_err(&pf->pdev->dev,
  1326. "Unable to add VF MAC filter\n");
  1327. ret = I40E_ERR_PARAM;
  1328. goto error_param;
  1329. }
  1330. }
  1331. /* program the updated filter list */
  1332. if (i40e_sync_vsi_filters(vsi))
  1333. dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
  1334. error_param:
  1335. /* send the response to the vf */
  1336. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
  1337. ret);
  1338. }
  1339. /**
  1340. * i40e_vc_del_mac_addr_msg
  1341. * @vf: pointer to the vf info
  1342. * @msg: pointer to the msg buffer
  1343. * @msglen: msg length
  1344. *
  1345. * remove guest mac address filter
  1346. **/
  1347. static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1348. {
  1349. struct i40e_virtchnl_ether_addr_list *al =
  1350. (struct i40e_virtchnl_ether_addr_list *)msg;
  1351. struct i40e_pf *pf = vf->pf;
  1352. struct i40e_vsi *vsi = NULL;
  1353. u16 vsi_id = al->vsi_id;
  1354. i40e_status ret = 0;
  1355. int i;
  1356. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1357. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1358. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1359. ret = I40E_ERR_PARAM;
  1360. goto error_param;
  1361. }
  1362. for (i = 0; i < al->num_elements; i++) {
  1363. if (is_broadcast_ether_addr(al->list[i].addr) ||
  1364. is_zero_ether_addr(al->list[i].addr)) {
  1365. dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
  1366. al->list[i].addr);
  1367. ret = I40E_ERR_INVALID_MAC_ADDR;
  1368. goto error_param;
  1369. }
  1370. }
  1371. vsi = pf->vsi[vsi_id];
  1372. /* delete addresses from the list */
  1373. for (i = 0; i < al->num_elements; i++)
  1374. i40e_del_filter(vsi, al->list[i].addr,
  1375. I40E_VLAN_ANY, true, false);
  1376. /* program the updated filter list */
  1377. if (i40e_sync_vsi_filters(vsi))
  1378. dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
  1379. error_param:
  1380. /* send the response to the vf */
  1381. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
  1382. ret);
  1383. }
  1384. /**
  1385. * i40e_vc_add_vlan_msg
  1386. * @vf: pointer to the vf info
  1387. * @msg: pointer to the msg buffer
  1388. * @msglen: msg length
  1389. *
  1390. * program guest vlan id
  1391. **/
  1392. static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1393. {
  1394. struct i40e_virtchnl_vlan_filter_list *vfl =
  1395. (struct i40e_virtchnl_vlan_filter_list *)msg;
  1396. struct i40e_pf *pf = vf->pf;
  1397. struct i40e_vsi *vsi = NULL;
  1398. u16 vsi_id = vfl->vsi_id;
  1399. i40e_status aq_ret = 0;
  1400. int i;
  1401. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1402. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1403. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1404. aq_ret = I40E_ERR_PARAM;
  1405. goto error_param;
  1406. }
  1407. for (i = 0; i < vfl->num_elements; i++) {
  1408. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  1409. aq_ret = I40E_ERR_PARAM;
  1410. dev_err(&pf->pdev->dev,
  1411. "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
  1412. goto error_param;
  1413. }
  1414. }
  1415. vsi = pf->vsi[vsi_id];
  1416. if (vsi->info.pvid) {
  1417. aq_ret = I40E_ERR_PARAM;
  1418. goto error_param;
  1419. }
  1420. i40e_vlan_stripping_enable(vsi);
  1421. for (i = 0; i < vfl->num_elements; i++) {
  1422. /* add new VLAN filter */
  1423. int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
  1424. if (ret)
  1425. dev_err(&pf->pdev->dev,
  1426. "Unable to add VF vlan filter %d, error %d\n",
  1427. vfl->vlan_id[i], ret);
  1428. }
  1429. error_param:
  1430. /* send the response to the vf */
  1431. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
  1432. }
  1433. /**
  1434. * i40e_vc_remove_vlan_msg
  1435. * @vf: pointer to the vf info
  1436. * @msg: pointer to the msg buffer
  1437. * @msglen: msg length
  1438. *
  1439. * remove programmed guest vlan id
  1440. **/
  1441. static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1442. {
  1443. struct i40e_virtchnl_vlan_filter_list *vfl =
  1444. (struct i40e_virtchnl_vlan_filter_list *)msg;
  1445. struct i40e_pf *pf = vf->pf;
  1446. struct i40e_vsi *vsi = NULL;
  1447. u16 vsi_id = vfl->vsi_id;
  1448. i40e_status aq_ret = 0;
  1449. int i;
  1450. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1451. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1452. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1453. aq_ret = I40E_ERR_PARAM;
  1454. goto error_param;
  1455. }
  1456. for (i = 0; i < vfl->num_elements; i++) {
  1457. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  1458. aq_ret = I40E_ERR_PARAM;
  1459. goto error_param;
  1460. }
  1461. }
  1462. vsi = pf->vsi[vsi_id];
  1463. if (vsi->info.pvid) {
  1464. aq_ret = I40E_ERR_PARAM;
  1465. goto error_param;
  1466. }
  1467. for (i = 0; i < vfl->num_elements; i++) {
  1468. int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
  1469. if (ret)
  1470. dev_err(&pf->pdev->dev,
  1471. "Unable to delete VF vlan filter %d, error %d\n",
  1472. vfl->vlan_id[i], ret);
  1473. }
  1474. error_param:
  1475. /* send the response to the vf */
  1476. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
  1477. }
  1478. /**
  1479. * i40e_vc_validate_vf_msg
  1480. * @vf: pointer to the vf info
  1481. * @msg: pointer to the msg buffer
  1482. * @msglen: msg length
  1483. * @msghndl: msg handle
  1484. *
  1485. * validate msg
  1486. **/
  1487. static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
  1488. u32 v_retval, u8 *msg, u16 msglen)
  1489. {
  1490. bool err_msg_format = false;
  1491. int valid_len;
  1492. /* Check if VF is disabled. */
  1493. if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
  1494. return I40E_ERR_PARAM;
  1495. /* Validate message length. */
  1496. switch (v_opcode) {
  1497. case I40E_VIRTCHNL_OP_VERSION:
  1498. valid_len = sizeof(struct i40e_virtchnl_version_info);
  1499. break;
  1500. case I40E_VIRTCHNL_OP_RESET_VF:
  1501. case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
  1502. valid_len = 0;
  1503. break;
  1504. case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
  1505. valid_len = sizeof(struct i40e_virtchnl_txq_info);
  1506. break;
  1507. case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
  1508. valid_len = sizeof(struct i40e_virtchnl_rxq_info);
  1509. break;
  1510. case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  1511. valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
  1512. if (msglen >= valid_len) {
  1513. struct i40e_virtchnl_vsi_queue_config_info *vqc =
  1514. (struct i40e_virtchnl_vsi_queue_config_info *)msg;
  1515. valid_len += (vqc->num_queue_pairs *
  1516. sizeof(struct
  1517. i40e_virtchnl_queue_pair_info));
  1518. if (vqc->num_queue_pairs == 0)
  1519. err_msg_format = true;
  1520. }
  1521. break;
  1522. case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
  1523. valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
  1524. if (msglen >= valid_len) {
  1525. struct i40e_virtchnl_irq_map_info *vimi =
  1526. (struct i40e_virtchnl_irq_map_info *)msg;
  1527. valid_len += (vimi->num_vectors *
  1528. sizeof(struct i40e_virtchnl_vector_map));
  1529. if (vimi->num_vectors == 0)
  1530. err_msg_format = true;
  1531. }
  1532. break;
  1533. case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
  1534. case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
  1535. valid_len = sizeof(struct i40e_virtchnl_queue_select);
  1536. break;
  1537. case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
  1538. case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
  1539. valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
  1540. if (msglen >= valid_len) {
  1541. struct i40e_virtchnl_ether_addr_list *veal =
  1542. (struct i40e_virtchnl_ether_addr_list *)msg;
  1543. valid_len += veal->num_elements *
  1544. sizeof(struct i40e_virtchnl_ether_addr);
  1545. if (veal->num_elements == 0)
  1546. err_msg_format = true;
  1547. }
  1548. break;
  1549. case I40E_VIRTCHNL_OP_ADD_VLAN:
  1550. case I40E_VIRTCHNL_OP_DEL_VLAN:
  1551. valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
  1552. if (msglen >= valid_len) {
  1553. struct i40e_virtchnl_vlan_filter_list *vfl =
  1554. (struct i40e_virtchnl_vlan_filter_list *)msg;
  1555. valid_len += vfl->num_elements * sizeof(u16);
  1556. if (vfl->num_elements == 0)
  1557. err_msg_format = true;
  1558. }
  1559. break;
  1560. case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
  1561. valid_len = sizeof(struct i40e_virtchnl_promisc_info);
  1562. break;
  1563. case I40E_VIRTCHNL_OP_GET_STATS:
  1564. valid_len = sizeof(struct i40e_virtchnl_queue_select);
  1565. break;
  1566. /* These are always errors coming from the VF. */
  1567. case I40E_VIRTCHNL_OP_EVENT:
  1568. case I40E_VIRTCHNL_OP_UNKNOWN:
  1569. default:
  1570. return -EPERM;
  1571. break;
  1572. }
  1573. /* few more checks */
  1574. if ((valid_len != msglen) || (err_msg_format)) {
  1575. i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
  1576. return -EINVAL;
  1577. } else {
  1578. return 0;
  1579. }
  1580. }
  1581. /**
  1582. * i40e_vc_process_vf_msg
  1583. * @pf: pointer to the pf structure
  1584. * @vf_id: source vf id
  1585. * @msg: pointer to the msg buffer
  1586. * @msglen: msg length
  1587. * @msghndl: msg handle
  1588. *
  1589. * called from the common aeq/arq handler to
  1590. * process request from vf
  1591. **/
  1592. int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
  1593. u32 v_retval, u8 *msg, u16 msglen)
  1594. {
  1595. struct i40e_hw *hw = &pf->hw;
  1596. unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
  1597. struct i40e_vf *vf;
  1598. int ret;
  1599. pf->vf_aq_requests++;
  1600. if (local_vf_id >= pf->num_alloc_vfs)
  1601. return -EINVAL;
  1602. vf = &(pf->vf[local_vf_id]);
  1603. /* perform basic checks on the msg */
  1604. ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
  1605. if (ret) {
  1606. dev_err(&pf->pdev->dev, "Invalid message from vf %d, opcode %d, len %d\n",
  1607. local_vf_id, v_opcode, msglen);
  1608. return ret;
  1609. }
  1610. switch (v_opcode) {
  1611. case I40E_VIRTCHNL_OP_VERSION:
  1612. ret = i40e_vc_get_version_msg(vf);
  1613. break;
  1614. case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
  1615. ret = i40e_vc_get_vf_resources_msg(vf);
  1616. break;
  1617. case I40E_VIRTCHNL_OP_RESET_VF:
  1618. i40e_vc_reset_vf_msg(vf);
  1619. ret = 0;
  1620. break;
  1621. case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
  1622. ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
  1623. break;
  1624. case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  1625. ret = i40e_vc_config_queues_msg(vf, msg, msglen);
  1626. break;
  1627. case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
  1628. ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
  1629. break;
  1630. case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
  1631. ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
  1632. break;
  1633. case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
  1634. ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
  1635. break;
  1636. case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
  1637. ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
  1638. break;
  1639. case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
  1640. ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
  1641. break;
  1642. case I40E_VIRTCHNL_OP_ADD_VLAN:
  1643. ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
  1644. break;
  1645. case I40E_VIRTCHNL_OP_DEL_VLAN:
  1646. ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
  1647. break;
  1648. case I40E_VIRTCHNL_OP_GET_STATS:
  1649. ret = i40e_vc_get_stats_msg(vf, msg, msglen);
  1650. break;
  1651. case I40E_VIRTCHNL_OP_UNKNOWN:
  1652. default:
  1653. dev_err(&pf->pdev->dev, "Unsupported opcode %d from vf %d\n",
  1654. v_opcode, local_vf_id);
  1655. ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
  1656. I40E_ERR_NOT_IMPLEMENTED);
  1657. break;
  1658. }
  1659. return ret;
  1660. }
  1661. /**
  1662. * i40e_vc_process_vflr_event
  1663. * @pf: pointer to the pf structure
  1664. *
  1665. * called from the vlfr irq handler to
  1666. * free up vf resources and state variables
  1667. **/
  1668. int i40e_vc_process_vflr_event(struct i40e_pf *pf)
  1669. {
  1670. u32 reg, reg_idx, bit_idx, vf_id;
  1671. struct i40e_hw *hw = &pf->hw;
  1672. struct i40e_vf *vf;
  1673. if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  1674. return 0;
  1675. clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  1676. for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
  1677. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  1678. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  1679. /* read GLGEN_VFLRSTAT register to find out the flr vfs */
  1680. vf = &pf->vf[vf_id];
  1681. reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
  1682. if (reg & (1 << bit_idx)) {
  1683. /* clear the bit in GLGEN_VFLRSTAT */
  1684. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
  1685. if (!test_bit(__I40E_DOWN, &pf->state))
  1686. i40e_reset_vf(vf, true);
  1687. }
  1688. }
  1689. /* re-enable vflr interrupt cause */
  1690. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  1691. reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
  1692. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  1693. i40e_flush(hw);
  1694. return 0;
  1695. }
  1696. /**
  1697. * i40e_vc_vf_broadcast
  1698. * @pf: pointer to the pf structure
  1699. * @opcode: operation code
  1700. * @retval: return value
  1701. * @msg: pointer to the msg buffer
  1702. * @msglen: msg length
  1703. *
  1704. * send a message to all VFs on a given PF
  1705. **/
  1706. static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
  1707. enum i40e_virtchnl_ops v_opcode,
  1708. i40e_status v_retval, u8 *msg,
  1709. u16 msglen)
  1710. {
  1711. struct i40e_hw *hw = &pf->hw;
  1712. struct i40e_vf *vf = pf->vf;
  1713. int i;
  1714. for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
  1715. int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  1716. /* Not all vfs are enabled so skip the ones that are not */
  1717. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
  1718. !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
  1719. continue;
  1720. /* Ignore return value on purpose - a given VF may fail, but
  1721. * we need to keep going and send to all of them
  1722. */
  1723. i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  1724. msg, msglen, NULL);
  1725. }
  1726. }
  1727. /**
  1728. * i40e_vc_notify_link_state
  1729. * @pf: pointer to the pf structure
  1730. *
  1731. * send a link status message to all VFs on a given PF
  1732. **/
  1733. void i40e_vc_notify_link_state(struct i40e_pf *pf)
  1734. {
  1735. struct i40e_virtchnl_pf_event pfe;
  1736. struct i40e_hw *hw = &pf->hw;
  1737. struct i40e_vf *vf = pf->vf;
  1738. struct i40e_link_status *ls = &pf->hw.phy.link_info;
  1739. int i;
  1740. pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
  1741. pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
  1742. for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
  1743. int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  1744. if (vf->link_forced) {
  1745. pfe.event_data.link_event.link_status = vf->link_up;
  1746. pfe.event_data.link_event.link_speed =
  1747. (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
  1748. } else {
  1749. pfe.event_data.link_event.link_status =
  1750. ls->link_info & I40E_AQ_LINK_UP;
  1751. pfe.event_data.link_event.link_speed = ls->link_speed;
  1752. }
  1753. i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
  1754. 0, (u8 *)&pfe, sizeof(pfe),
  1755. NULL);
  1756. }
  1757. }
  1758. /**
  1759. * i40e_vc_notify_reset
  1760. * @pf: pointer to the pf structure
  1761. *
  1762. * indicate a pending reset to all VFs on a given PF
  1763. **/
  1764. void i40e_vc_notify_reset(struct i40e_pf *pf)
  1765. {
  1766. struct i40e_virtchnl_pf_event pfe;
  1767. pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
  1768. pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
  1769. i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, I40E_SUCCESS,
  1770. (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
  1771. }
  1772. /**
  1773. * i40e_vc_notify_vf_reset
  1774. * @vf: pointer to the vf structure
  1775. *
  1776. * indicate a pending reset to the given VF
  1777. **/
  1778. void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
  1779. {
  1780. struct i40e_virtchnl_pf_event pfe;
  1781. int abs_vf_id;
  1782. /* validate the request */
  1783. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  1784. return;
  1785. /* verify if the VF is in either init or active before proceeding */
  1786. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
  1787. !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
  1788. return;
  1789. abs_vf_id = vf->vf_id + vf->pf->hw.func_caps.vf_base_id;
  1790. pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
  1791. pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
  1792. i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
  1793. I40E_SUCCESS, (u8 *)&pfe,
  1794. sizeof(struct i40e_virtchnl_pf_event), NULL);
  1795. }
  1796. /**
  1797. * i40e_ndo_set_vf_mac
  1798. * @netdev: network interface device structure
  1799. * @vf_id: vf identifier
  1800. * @mac: mac address
  1801. *
  1802. * program vf mac address
  1803. **/
  1804. int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
  1805. {
  1806. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1807. struct i40e_vsi *vsi = np->vsi;
  1808. struct i40e_pf *pf = vsi->back;
  1809. struct i40e_mac_filter *f;
  1810. struct i40e_vf *vf;
  1811. int ret = 0;
  1812. /* validate the request */
  1813. if (vf_id >= pf->num_alloc_vfs) {
  1814. dev_err(&pf->pdev->dev,
  1815. "Invalid VF Identifier %d\n", vf_id);
  1816. ret = -EINVAL;
  1817. goto error_param;
  1818. }
  1819. vf = &(pf->vf[vf_id]);
  1820. vsi = pf->vsi[vf->lan_vsi_index];
  1821. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  1822. dev_err(&pf->pdev->dev,
  1823. "Uninitialized VF %d\n", vf_id);
  1824. ret = -EINVAL;
  1825. goto error_param;
  1826. }
  1827. if (!is_valid_ether_addr(mac)) {
  1828. dev_err(&pf->pdev->dev,
  1829. "Invalid VF ethernet address\n");
  1830. ret = -EINVAL;
  1831. goto error_param;
  1832. }
  1833. /* delete the temporary mac address */
  1834. i40e_del_filter(vsi, vf->default_lan_addr.addr, vf->port_vlan_id,
  1835. true, false);
  1836. /* Delete all the filters for this VSI - we're going to kill it
  1837. * anyway.
  1838. */
  1839. list_for_each_entry(f, &vsi->mac_filter_list, list)
  1840. i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
  1841. dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
  1842. /* program mac filter */
  1843. if (i40e_sync_vsi_filters(vsi)) {
  1844. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  1845. ret = -EIO;
  1846. goto error_param;
  1847. }
  1848. ether_addr_copy(vf->default_lan_addr.addr, mac);
  1849. vf->pf_set_mac = true;
  1850. /* Force the VF driver stop so it has to reload with new MAC address */
  1851. i40e_vc_disable_vf(pf, vf);
  1852. dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
  1853. error_param:
  1854. return ret;
  1855. }
  1856. /**
  1857. * i40e_ndo_set_vf_port_vlan
  1858. * @netdev: network interface device structure
  1859. * @vf_id: vf identifier
  1860. * @vlan_id: mac address
  1861. * @qos: priority setting
  1862. *
  1863. * program vf vlan id and/or qos
  1864. **/
  1865. int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
  1866. int vf_id, u16 vlan_id, u8 qos)
  1867. {
  1868. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1869. struct i40e_pf *pf = np->vsi->back;
  1870. struct i40e_vsi *vsi;
  1871. struct i40e_vf *vf;
  1872. int ret = 0;
  1873. /* validate the request */
  1874. if (vf_id >= pf->num_alloc_vfs) {
  1875. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  1876. ret = -EINVAL;
  1877. goto error_pvid;
  1878. }
  1879. if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
  1880. dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
  1881. ret = -EINVAL;
  1882. goto error_pvid;
  1883. }
  1884. vf = &(pf->vf[vf_id]);
  1885. vsi = pf->vsi[vf->lan_vsi_index];
  1886. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  1887. dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
  1888. ret = -EINVAL;
  1889. goto error_pvid;
  1890. }
  1891. if (vsi->info.pvid == 0 && i40e_is_vsi_in_vlan(vsi)) {
  1892. dev_err(&pf->pdev->dev,
  1893. "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",
  1894. vf_id);
  1895. /* Administrator Error - knock the VF offline until he does
  1896. * the right thing by reconfiguring his network correctly
  1897. * and then reloading the VF driver.
  1898. */
  1899. i40e_vc_disable_vf(pf, vf);
  1900. }
  1901. /* Check for condition where there was already a port VLAN ID
  1902. * filter set and now it is being deleted by setting it to zero.
  1903. * Additionally check for the condition where there was a port
  1904. * VLAN but now there is a new and different port VLAN being set.
  1905. * Before deleting all the old VLAN filters we must add new ones
  1906. * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
  1907. * MAC addresses deleted.
  1908. */
  1909. if ((!(vlan_id || qos) ||
  1910. (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
  1911. vsi->info.pvid)
  1912. ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
  1913. if (vsi->info.pvid) {
  1914. /* kill old VLAN */
  1915. ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
  1916. VLAN_VID_MASK));
  1917. if (ret) {
  1918. dev_info(&vsi->back->pdev->dev,
  1919. "remove VLAN failed, ret=%d, aq_err=%d\n",
  1920. ret, pf->hw.aq.asq_last_status);
  1921. }
  1922. }
  1923. if (vlan_id || qos)
  1924. ret = i40e_vsi_add_pvid(vsi,
  1925. vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
  1926. else
  1927. i40e_vsi_remove_pvid(vsi);
  1928. if (vlan_id) {
  1929. dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
  1930. vlan_id, qos, vf_id);
  1931. /* add new VLAN filter */
  1932. ret = i40e_vsi_add_vlan(vsi, vlan_id);
  1933. if (ret) {
  1934. dev_info(&vsi->back->pdev->dev,
  1935. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  1936. vsi->back->hw.aq.asq_last_status);
  1937. goto error_pvid;
  1938. }
  1939. /* Kill non-vlan MAC filters - ignore error return since
  1940. * there might not be any non-vlan MAC filters.
  1941. */
  1942. i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
  1943. }
  1944. if (ret) {
  1945. dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
  1946. goto error_pvid;
  1947. }
  1948. /* The Port VLAN needs to be saved across resets the same as the
  1949. * default LAN MAC address.
  1950. */
  1951. vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
  1952. ret = 0;
  1953. error_pvid:
  1954. return ret;
  1955. }
  1956. #define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
  1957. #define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
  1958. /**
  1959. * i40e_ndo_set_vf_bw
  1960. * @netdev: network interface device structure
  1961. * @vf_id: vf identifier
  1962. * @tx_rate: tx rate
  1963. *
  1964. * configure vf tx rate
  1965. **/
  1966. int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
  1967. int max_tx_rate)
  1968. {
  1969. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1970. struct i40e_pf *pf = np->vsi->back;
  1971. struct i40e_vsi *vsi;
  1972. struct i40e_vf *vf;
  1973. int speed = 0;
  1974. int ret = 0;
  1975. /* validate the request */
  1976. if (vf_id >= pf->num_alloc_vfs) {
  1977. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
  1978. ret = -EINVAL;
  1979. goto error;
  1980. }
  1981. if (min_tx_rate) {
  1982. dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for vf %d.\n",
  1983. min_tx_rate, vf_id);
  1984. return -EINVAL;
  1985. }
  1986. vf = &(pf->vf[vf_id]);
  1987. vsi = pf->vsi[vf->lan_vsi_index];
  1988. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  1989. dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
  1990. ret = -EINVAL;
  1991. goto error;
  1992. }
  1993. switch (pf->hw.phy.link_info.link_speed) {
  1994. case I40E_LINK_SPEED_40GB:
  1995. speed = 40000;
  1996. break;
  1997. case I40E_LINK_SPEED_10GB:
  1998. speed = 10000;
  1999. break;
  2000. case I40E_LINK_SPEED_1GB:
  2001. speed = 1000;
  2002. break;
  2003. default:
  2004. break;
  2005. }
  2006. if (max_tx_rate > speed) {
  2007. dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for vf %d.",
  2008. max_tx_rate, vf->vf_id);
  2009. ret = -EINVAL;
  2010. goto error;
  2011. }
  2012. if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
  2013. dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
  2014. max_tx_rate = 50;
  2015. }
  2016. /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
  2017. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
  2018. max_tx_rate / I40E_BW_CREDIT_DIVISOR,
  2019. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  2020. if (ret) {
  2021. dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
  2022. ret);
  2023. ret = -EIO;
  2024. goto error;
  2025. }
  2026. vf->tx_rate = max_tx_rate;
  2027. error:
  2028. return ret;
  2029. }
  2030. /**
  2031. * i40e_ndo_get_vf_config
  2032. * @netdev: network interface device structure
  2033. * @vf_id: vf identifier
  2034. * @ivi: vf configuration structure
  2035. *
  2036. * return vf configuration
  2037. **/
  2038. int i40e_ndo_get_vf_config(struct net_device *netdev,
  2039. int vf_id, struct ifla_vf_info *ivi)
  2040. {
  2041. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2042. struct i40e_vsi *vsi = np->vsi;
  2043. struct i40e_pf *pf = vsi->back;
  2044. struct i40e_vf *vf;
  2045. int ret = 0;
  2046. /* validate the request */
  2047. if (vf_id >= pf->num_alloc_vfs) {
  2048. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2049. ret = -EINVAL;
  2050. goto error_param;
  2051. }
  2052. vf = &(pf->vf[vf_id]);
  2053. /* first vsi is always the LAN vsi */
  2054. vsi = pf->vsi[vf->lan_vsi_index];
  2055. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  2056. dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
  2057. ret = -EINVAL;
  2058. goto error_param;
  2059. }
  2060. ivi->vf = vf_id;
  2061. memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
  2062. ivi->max_tx_rate = vf->tx_rate;
  2063. ivi->min_tx_rate = 0;
  2064. ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
  2065. ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
  2066. I40E_VLAN_PRIORITY_SHIFT;
  2067. if (vf->link_forced == false)
  2068. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  2069. else if (vf->link_up == true)
  2070. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  2071. else
  2072. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  2073. ivi->spoofchk = vf->spoofchk;
  2074. ret = 0;
  2075. error_param:
  2076. return ret;
  2077. }
  2078. /**
  2079. * i40e_ndo_set_vf_link_state
  2080. * @netdev: network interface device structure
  2081. * @vf_id: vf identifier
  2082. * @link: required link state
  2083. *
  2084. * Set the link state of a specified VF, regardless of physical link state
  2085. **/
  2086. int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
  2087. {
  2088. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2089. struct i40e_pf *pf = np->vsi->back;
  2090. struct i40e_virtchnl_pf_event pfe;
  2091. struct i40e_hw *hw = &pf->hw;
  2092. struct i40e_vf *vf;
  2093. int abs_vf_id;
  2094. int ret = 0;
  2095. /* validate the request */
  2096. if (vf_id >= pf->num_alloc_vfs) {
  2097. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2098. ret = -EINVAL;
  2099. goto error_out;
  2100. }
  2101. vf = &pf->vf[vf_id];
  2102. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  2103. pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
  2104. pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
  2105. switch (link) {
  2106. case IFLA_VF_LINK_STATE_AUTO:
  2107. vf->link_forced = false;
  2108. pfe.event_data.link_event.link_status =
  2109. pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
  2110. pfe.event_data.link_event.link_speed =
  2111. pf->hw.phy.link_info.link_speed;
  2112. break;
  2113. case IFLA_VF_LINK_STATE_ENABLE:
  2114. vf->link_forced = true;
  2115. vf->link_up = true;
  2116. pfe.event_data.link_event.link_status = true;
  2117. pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
  2118. break;
  2119. case IFLA_VF_LINK_STATE_DISABLE:
  2120. vf->link_forced = true;
  2121. vf->link_up = false;
  2122. pfe.event_data.link_event.link_status = false;
  2123. pfe.event_data.link_event.link_speed = 0;
  2124. break;
  2125. default:
  2126. ret = -EINVAL;
  2127. goto error_out;
  2128. }
  2129. /* Notify the VF of its new link state */
  2130. i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
  2131. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  2132. error_out:
  2133. return ret;
  2134. }
  2135. /**
  2136. * i40e_ndo_set_vf_spoofchk
  2137. * @netdev: network interface device structure
  2138. * @vf_id: vf identifier
  2139. * @enable: flag to enable or disable feature
  2140. *
  2141. * Enable or disable VF spoof checking
  2142. **/
  2143. int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
  2144. {
  2145. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2146. struct i40e_vsi *vsi = np->vsi;
  2147. struct i40e_pf *pf = vsi->back;
  2148. struct i40e_vsi_context ctxt;
  2149. struct i40e_hw *hw = &pf->hw;
  2150. struct i40e_vf *vf;
  2151. int ret = 0;
  2152. /* validate the request */
  2153. if (vf_id >= pf->num_alloc_vfs) {
  2154. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2155. ret = -EINVAL;
  2156. goto out;
  2157. }
  2158. vf = &(pf->vf[vf_id]);
  2159. if (enable == vf->spoofchk)
  2160. goto out;
  2161. vf->spoofchk = enable;
  2162. memset(&ctxt, 0, sizeof(ctxt));
  2163. ctxt.seid = pf->vsi[vf->lan_vsi_index]->seid;
  2164. ctxt.pf_num = pf->hw.pf_id;
  2165. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  2166. if (enable)
  2167. ctxt.info.sec_flags |= I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK;
  2168. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  2169. if (ret) {
  2170. dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
  2171. ret);
  2172. ret = -EIO;
  2173. }
  2174. out:
  2175. return ret;
  2176. }