i40e_virtchnl_pf.c 61 KB

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