i40e_virtchnl_pf.c 62 KB

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