i40e_virtchnl_pf.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
  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 = 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.tphrdesc_ena = 1;
  309. rx_ctx.tphwdesc_ena = 1;
  310. rx_ctx.tphdata_ena = 1;
  311. rx_ctx.tphhead_ena = 1;
  312. rx_ctx.lrxqthresh = 2;
  313. rx_ctx.crcstrip = 1;
  314. rx_ctx.prefena = 1;
  315. /* clear the context in the HMC */
  316. ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
  317. if (ret) {
  318. dev_err(&pf->pdev->dev,
  319. "Failed to clear VF LAN Rx queue context %d, error: %d\n",
  320. pf_queue_id, ret);
  321. ret = -ENOENT;
  322. goto error_param;
  323. }
  324. /* set the context in the HMC */
  325. ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
  326. if (ret) {
  327. dev_err(&pf->pdev->dev,
  328. "Failed to set VF LAN Rx queue context %d error: %d\n",
  329. pf_queue_id, ret);
  330. ret = -ENOENT;
  331. goto error_param;
  332. }
  333. error_param:
  334. return ret;
  335. }
  336. /**
  337. * i40e_alloc_vsi_res
  338. * @vf: pointer to the vf info
  339. * @type: type of VSI to allocate
  340. *
  341. * alloc vf vsi context & resources
  342. **/
  343. static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
  344. {
  345. struct i40e_mac_filter *f = NULL;
  346. struct i40e_pf *pf = vf->pf;
  347. struct i40e_vsi *vsi;
  348. int ret = 0;
  349. vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
  350. if (!vsi) {
  351. dev_err(&pf->pdev->dev,
  352. "add vsi failed for vf %d, aq_err %d\n",
  353. vf->vf_id, pf->hw.aq.asq_last_status);
  354. ret = -ENOENT;
  355. goto error_alloc_vsi_res;
  356. }
  357. if (type == I40E_VSI_SRIOV) {
  358. u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  359. vf->lan_vsi_index = vsi->idx;
  360. vf->lan_vsi_id = vsi->id;
  361. dev_info(&pf->pdev->dev,
  362. "VF %d assigned LAN VSI index %d, VSI id %d\n",
  363. vf->vf_id, vsi->idx, vsi->id);
  364. /* If the port VLAN has been configured and then the
  365. * VF driver was removed then the VSI port VLAN
  366. * configuration was destroyed. Check if there is
  367. * a port VLAN and restore the VSI configuration if
  368. * needed.
  369. */
  370. if (vf->port_vlan_id)
  371. i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
  372. f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
  373. vf->port_vlan_id, true, false);
  374. if (!f)
  375. dev_info(&pf->pdev->dev,
  376. "Could not allocate VF MAC addr\n");
  377. f = i40e_add_filter(vsi, brdcast, vf->port_vlan_id,
  378. true, false);
  379. if (!f)
  380. dev_info(&pf->pdev->dev,
  381. "Could not allocate VF broadcast filter\n");
  382. }
  383. /* program mac filter */
  384. ret = i40e_sync_vsi_filters(vsi);
  385. if (ret)
  386. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  387. /* Set VF bandwidth if specified */
  388. if (vf->tx_rate) {
  389. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
  390. vf->tx_rate / 50, 0, NULL);
  391. if (ret)
  392. dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
  393. vf->vf_id, ret);
  394. }
  395. error_alloc_vsi_res:
  396. return ret;
  397. }
  398. /**
  399. * i40e_enable_vf_mappings
  400. * @vf: pointer to the vf info
  401. *
  402. * enable vf mappings
  403. **/
  404. static void i40e_enable_vf_mappings(struct i40e_vf *vf)
  405. {
  406. struct i40e_pf *pf = vf->pf;
  407. struct i40e_hw *hw = &pf->hw;
  408. u32 reg, total_queue_pairs = 0;
  409. int j;
  410. /* Tell the hardware we're using noncontiguous mapping. HW requires
  411. * that VF queues be mapped using this method, even when they are
  412. * contiguous in real life
  413. */
  414. wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
  415. I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
  416. /* enable VF vplan_qtable mappings */
  417. reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
  418. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
  419. /* map PF queues to VF queues */
  420. for (j = 0; j < pf->vsi[vf->lan_vsi_index]->num_queue_pairs; j++) {
  421. u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index, j);
  422. reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
  423. wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
  424. total_queue_pairs++;
  425. }
  426. /* map PF queues to VSI */
  427. for (j = 0; j < 7; j++) {
  428. if (j * 2 >= pf->vsi[vf->lan_vsi_index]->num_queue_pairs) {
  429. reg = 0x07FF07FF; /* unused */
  430. } else {
  431. u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index,
  432. j * 2);
  433. reg = qid;
  434. qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_index,
  435. (j * 2) + 1);
  436. reg |= qid << 16;
  437. }
  438. wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
  439. }
  440. i40e_flush(hw);
  441. }
  442. /**
  443. * i40e_disable_vf_mappings
  444. * @vf: pointer to the vf info
  445. *
  446. * disable vf mappings
  447. **/
  448. static void i40e_disable_vf_mappings(struct i40e_vf *vf)
  449. {
  450. struct i40e_pf *pf = vf->pf;
  451. struct i40e_hw *hw = &pf->hw;
  452. int i;
  453. /* disable qp mappings */
  454. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
  455. for (i = 0; i < I40E_MAX_VSI_QP; i++)
  456. wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
  457. I40E_QUEUE_END_OF_LIST);
  458. i40e_flush(hw);
  459. }
  460. /**
  461. * i40e_free_vf_res
  462. * @vf: pointer to the vf info
  463. *
  464. * free vf resources
  465. **/
  466. static void i40e_free_vf_res(struct i40e_vf *vf)
  467. {
  468. struct i40e_pf *pf = vf->pf;
  469. struct i40e_hw *hw = &pf->hw;
  470. u32 reg_idx, reg;
  471. int i, msix_vf;
  472. /* free vsi & disconnect it from the parent uplink */
  473. if (vf->lan_vsi_index) {
  474. i40e_vsi_release(pf->vsi[vf->lan_vsi_index]);
  475. vf->lan_vsi_index = 0;
  476. vf->lan_vsi_id = 0;
  477. }
  478. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  479. /* disable interrupts so the VF starts in a known state */
  480. for (i = 0; i < msix_vf; i++) {
  481. /* format is same for both registers */
  482. if (0 == i)
  483. reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
  484. else
  485. reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
  486. (vf->vf_id))
  487. + (i - 1));
  488. wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
  489. i40e_flush(hw);
  490. }
  491. /* clear the irq settings */
  492. for (i = 0; i < msix_vf; i++) {
  493. /* format is same for both registers */
  494. if (0 == i)
  495. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  496. else
  497. reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
  498. (vf->vf_id))
  499. + (i - 1));
  500. reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
  501. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
  502. wr32(hw, reg_idx, reg);
  503. i40e_flush(hw);
  504. }
  505. /* reset some of the state varibles keeping
  506. * track of the resources
  507. */
  508. vf->num_queue_pairs = 0;
  509. vf->vf_states = 0;
  510. }
  511. /**
  512. * i40e_alloc_vf_res
  513. * @vf: pointer to the vf info
  514. *
  515. * allocate vf resources
  516. **/
  517. static int i40e_alloc_vf_res(struct i40e_vf *vf)
  518. {
  519. struct i40e_pf *pf = vf->pf;
  520. int total_queue_pairs = 0;
  521. int ret;
  522. /* allocate hw vsi context & associated resources */
  523. ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
  524. if (ret)
  525. goto error_alloc;
  526. total_queue_pairs += pf->vsi[vf->lan_vsi_index]->num_queue_pairs;
  527. set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  528. /* store the total qps number for the runtime
  529. * vf req validation
  530. */
  531. vf->num_queue_pairs = total_queue_pairs;
  532. /* vf is now completely initialized */
  533. set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
  534. error_alloc:
  535. if (ret)
  536. i40e_free_vf_res(vf);
  537. return ret;
  538. }
  539. #define VF_DEVICE_STATUS 0xAA
  540. #define VF_TRANS_PENDING_MASK 0x20
  541. /**
  542. * i40e_quiesce_vf_pci
  543. * @vf: pointer to the vf structure
  544. *
  545. * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
  546. * if the transactions never clear.
  547. **/
  548. static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
  549. {
  550. struct i40e_pf *pf = vf->pf;
  551. struct i40e_hw *hw = &pf->hw;
  552. int vf_abs_id, i;
  553. u32 reg;
  554. vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
  555. wr32(hw, I40E_PF_PCI_CIAA,
  556. VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
  557. for (i = 0; i < 100; i++) {
  558. reg = rd32(hw, I40E_PF_PCI_CIAD);
  559. if ((reg & VF_TRANS_PENDING_MASK) == 0)
  560. return 0;
  561. udelay(1);
  562. }
  563. return -EIO;
  564. }
  565. /**
  566. * i40e_reset_vf
  567. * @vf: pointer to the vf structure
  568. * @flr: VFLR was issued or not
  569. *
  570. * reset the vf
  571. **/
  572. void i40e_reset_vf(struct i40e_vf *vf, bool flr)
  573. {
  574. struct i40e_pf *pf = vf->pf;
  575. struct i40e_hw *hw = &pf->hw;
  576. bool rsd = false;
  577. int i;
  578. u32 reg;
  579. /* warn the VF */
  580. clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
  581. /* In the case of a VFLR, the HW has already reset the VF and we
  582. * just need to clean up, so don't hit the VFRTRIG register.
  583. */
  584. if (!flr) {
  585. /* reset vf using VPGEN_VFRTRIG reg */
  586. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  587. reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  588. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  589. i40e_flush(hw);
  590. }
  591. if (i40e_quiesce_vf_pci(vf))
  592. dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
  593. vf->vf_id);
  594. /* poll VPGEN_VFRSTAT reg to make sure
  595. * that reset is complete
  596. */
  597. for (i = 0; i < 100; i++) {
  598. /* vf reset requires driver to first reset the
  599. * vf & than poll the status register to make sure
  600. * that the requested op was completed
  601. * successfully
  602. */
  603. udelay(10);
  604. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  605. if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
  606. rsd = true;
  607. break;
  608. }
  609. }
  610. if (!rsd)
  611. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  612. vf->vf_id);
  613. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
  614. /* clear the reset bit in the VPGEN_VFRTRIG reg */
  615. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  616. reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  617. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  618. /* On initial reset, we won't have any queues */
  619. if (vf->lan_vsi_index == 0)
  620. goto complete_reset;
  621. i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_index], false);
  622. complete_reset:
  623. /* reallocate vf resources to reset the VSI state */
  624. i40e_free_vf_res(vf);
  625. i40e_alloc_vf_res(vf);
  626. i40e_enable_vf_mappings(vf);
  627. set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
  628. /* tell the VF the reset is done */
  629. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
  630. i40e_flush(hw);
  631. }
  632. /**
  633. * i40e_vfs_are_assigned
  634. * @pf: pointer to the pf structure
  635. *
  636. * Determine if any VFs are assigned to VMs
  637. **/
  638. static bool i40e_vfs_are_assigned(struct i40e_pf *pf)
  639. {
  640. struct pci_dev *pdev = pf->pdev;
  641. struct pci_dev *vfdev;
  642. /* loop through all the VFs to see if we own any that are assigned */
  643. vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, I40E_DEV_ID_VF , NULL);
  644. while (vfdev) {
  645. /* if we don't own it we don't care */
  646. if (vfdev->is_virtfn && pci_physfn(vfdev) == pdev) {
  647. /* if it is assigned we cannot release it */
  648. if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
  649. return true;
  650. }
  651. vfdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  652. I40E_DEV_ID_VF,
  653. vfdev);
  654. }
  655. return false;
  656. }
  657. #ifdef CONFIG_PCI_IOV
  658. /**
  659. * i40e_enable_pf_switch_lb
  660. * @pf: pointer to the pf structure
  661. *
  662. * enable switch loop back or die - no point in a return value
  663. **/
  664. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  665. {
  666. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  667. struct i40e_vsi_context ctxt;
  668. int aq_ret;
  669. ctxt.seid = pf->main_vsi_seid;
  670. ctxt.pf_num = pf->hw.pf_id;
  671. ctxt.vf_num = 0;
  672. aq_ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  673. if (aq_ret) {
  674. dev_info(&pf->pdev->dev,
  675. "%s couldn't get pf vsi config, err %d, aq_err %d\n",
  676. __func__, aq_ret, pf->hw.aq.asq_last_status);
  677. return;
  678. }
  679. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  680. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  681. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  682. aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  683. if (aq_ret) {
  684. dev_info(&pf->pdev->dev,
  685. "%s: update vsi switch failed, aq_err=%d\n",
  686. __func__, vsi->back->hw.aq.asq_last_status);
  687. }
  688. }
  689. #endif
  690. /**
  691. * i40e_disable_pf_switch_lb
  692. * @pf: pointer to the pf structure
  693. *
  694. * disable switch loop back or die - no point in a return value
  695. **/
  696. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  697. {
  698. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  699. struct i40e_vsi_context ctxt;
  700. int aq_ret;
  701. ctxt.seid = pf->main_vsi_seid;
  702. ctxt.pf_num = pf->hw.pf_id;
  703. ctxt.vf_num = 0;
  704. aq_ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  705. if (aq_ret) {
  706. dev_info(&pf->pdev->dev,
  707. "%s couldn't get pf vsi config, err %d, aq_err %d\n",
  708. __func__, aq_ret, pf->hw.aq.asq_last_status);
  709. return;
  710. }
  711. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  712. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  713. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  714. aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  715. if (aq_ret) {
  716. dev_info(&pf->pdev->dev,
  717. "%s: update vsi switch failed, aq_err=%d\n",
  718. __func__, vsi->back->hw.aq.asq_last_status);
  719. }
  720. }
  721. /**
  722. * i40e_free_vfs
  723. * @pf: pointer to the pf structure
  724. *
  725. * free vf resources
  726. **/
  727. void i40e_free_vfs(struct i40e_pf *pf)
  728. {
  729. struct i40e_hw *hw = &pf->hw;
  730. u32 reg_idx, bit_idx;
  731. int i, tmp, vf_id;
  732. if (!pf->vf)
  733. return;
  734. /* Disable interrupt 0 so we don't try to handle the VFLR. */
  735. i40e_irq_dynamic_disable_icr0(pf);
  736. mdelay(10); /* let any messages in transit get finished up */
  737. /* free up vf resources */
  738. tmp = pf->num_alloc_vfs;
  739. pf->num_alloc_vfs = 0;
  740. for (i = 0; i < tmp; i++) {
  741. if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
  742. i40e_free_vf_res(&pf->vf[i]);
  743. /* disable qp mappings */
  744. i40e_disable_vf_mappings(&pf->vf[i]);
  745. }
  746. kfree(pf->vf);
  747. pf->vf = NULL;
  748. /* This check is for when the driver is unloaded while VFs are
  749. * assigned. Setting the number of VFs to 0 through sysfs is caught
  750. * before this function ever gets called.
  751. */
  752. if (!i40e_vfs_are_assigned(pf)) {
  753. pci_disable_sriov(pf->pdev);
  754. /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
  755. * work correctly when SR-IOV gets re-enabled.
  756. */
  757. for (vf_id = 0; vf_id < tmp; vf_id++) {
  758. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  759. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  760. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), (1 << bit_idx));
  761. }
  762. i40e_disable_pf_switch_lb(pf);
  763. } else {
  764. dev_warn(&pf->pdev->dev,
  765. "unable to disable SR-IOV because VFs are assigned.\n");
  766. }
  767. /* Re-enable interrupt 0. */
  768. i40e_irq_dynamic_enable_icr0(pf);
  769. }
  770. #ifdef CONFIG_PCI_IOV
  771. /**
  772. * i40e_alloc_vfs
  773. * @pf: pointer to the pf structure
  774. * @num_alloc_vfs: number of vfs to allocate
  775. *
  776. * allocate vf resources
  777. **/
  778. int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
  779. {
  780. struct i40e_vf *vfs;
  781. int i, ret = 0;
  782. /* Disable interrupt 0 so we don't try to handle the VFLR. */
  783. i40e_irq_dynamic_disable_icr0(pf);
  784. /* Check to see if we're just allocating resources for extant VFs */
  785. if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
  786. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  787. if (ret) {
  788. dev_err(&pf->pdev->dev,
  789. "Failed to enable SR-IOV, error %d.\n", ret);
  790. pf->num_alloc_vfs = 0;
  791. goto err_iov;
  792. }
  793. }
  794. /* allocate memory */
  795. vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
  796. if (!vfs) {
  797. ret = -ENOMEM;
  798. goto err_alloc;
  799. }
  800. pf->vf = vfs;
  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. vfs[i].spoofchk = true;
  809. /* vf resources get allocated during reset */
  810. i40e_reset_vf(&vfs[i], false);
  811. /* enable vf vplan_qtable mappings */
  812. i40e_enable_vf_mappings(&vfs[i]);
  813. }
  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. /* Delete all the filters for this VSI - we're going to kill it
  1848. * anyway.
  1849. */
  1850. list_for_each_entry(f, &vsi->mac_filter_list, list)
  1851. i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
  1852. dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
  1853. /* program mac filter */
  1854. if (i40e_sync_vsi_filters(vsi)) {
  1855. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  1856. ret = -EIO;
  1857. goto error_param;
  1858. }
  1859. ether_addr_copy(vf->default_lan_addr.addr, mac);
  1860. vf->pf_set_mac = true;
  1861. /* Force the VF driver stop so it has to reload with new MAC address */
  1862. i40e_vc_disable_vf(pf, vf);
  1863. dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
  1864. ret = 0;
  1865. error_param:
  1866. return ret;
  1867. }
  1868. /**
  1869. * i40e_ndo_set_vf_port_vlan
  1870. * @netdev: network interface device structure
  1871. * @vf_id: vf identifier
  1872. * @vlan_id: mac address
  1873. * @qos: priority setting
  1874. *
  1875. * program vf vlan id and/or qos
  1876. **/
  1877. int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
  1878. int vf_id, u16 vlan_id, u8 qos)
  1879. {
  1880. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1881. struct i40e_pf *pf = np->vsi->back;
  1882. struct i40e_vsi *vsi;
  1883. struct i40e_vf *vf;
  1884. int ret = 0;
  1885. /* validate the request */
  1886. if (vf_id >= pf->num_alloc_vfs) {
  1887. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  1888. ret = -EINVAL;
  1889. goto error_pvid;
  1890. }
  1891. if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
  1892. dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
  1893. ret = -EINVAL;
  1894. goto error_pvid;
  1895. }
  1896. vf = &(pf->vf[vf_id]);
  1897. vsi = pf->vsi[vf->lan_vsi_index];
  1898. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  1899. dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
  1900. ret = -EINVAL;
  1901. goto error_pvid;
  1902. }
  1903. if (vsi->info.pvid == 0 && i40e_is_vsi_in_vlan(vsi)) {
  1904. dev_err(&pf->pdev->dev,
  1905. "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",
  1906. vf_id);
  1907. /* Administrator Error - knock the VF offline until he does
  1908. * the right thing by reconfiguring his network correctly
  1909. * and then reloading the VF driver.
  1910. */
  1911. i40e_vc_disable_vf(pf, vf);
  1912. }
  1913. /* Check for condition where there was already a port VLAN ID
  1914. * filter set and now it is being deleted by setting it to zero.
  1915. * Additionally check for the condition where there was a port
  1916. * VLAN but now there is a new and different port VLAN being set.
  1917. * Before deleting all the old VLAN filters we must add new ones
  1918. * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
  1919. * MAC addresses deleted.
  1920. */
  1921. if ((!(vlan_id || qos) ||
  1922. (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
  1923. vsi->info.pvid)
  1924. ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
  1925. if (vsi->info.pvid) {
  1926. /* kill old VLAN */
  1927. ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
  1928. VLAN_VID_MASK));
  1929. if (ret) {
  1930. dev_info(&vsi->back->pdev->dev,
  1931. "remove VLAN failed, ret=%d, aq_err=%d\n",
  1932. ret, pf->hw.aq.asq_last_status);
  1933. }
  1934. }
  1935. if (vlan_id || qos)
  1936. ret = i40e_vsi_add_pvid(vsi,
  1937. vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
  1938. else
  1939. i40e_vsi_remove_pvid(vsi);
  1940. if (vlan_id) {
  1941. dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
  1942. vlan_id, qos, vf_id);
  1943. /* add new VLAN filter */
  1944. ret = i40e_vsi_add_vlan(vsi, vlan_id);
  1945. if (ret) {
  1946. dev_info(&vsi->back->pdev->dev,
  1947. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  1948. vsi->back->hw.aq.asq_last_status);
  1949. goto error_pvid;
  1950. }
  1951. /* Kill non-vlan MAC filters - ignore error return since
  1952. * there might not be any non-vlan MAC filters.
  1953. */
  1954. i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
  1955. }
  1956. if (ret) {
  1957. dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
  1958. goto error_pvid;
  1959. }
  1960. /* The Port VLAN needs to be saved across resets the same as the
  1961. * default LAN MAC address.
  1962. */
  1963. vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
  1964. ret = 0;
  1965. error_pvid:
  1966. return ret;
  1967. }
  1968. #define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
  1969. #define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
  1970. /**
  1971. * i40e_ndo_set_vf_bw
  1972. * @netdev: network interface device structure
  1973. * @vf_id: vf identifier
  1974. * @tx_rate: tx rate
  1975. *
  1976. * configure vf tx rate
  1977. **/
  1978. int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
  1979. int max_tx_rate)
  1980. {
  1981. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1982. struct i40e_pf *pf = np->vsi->back;
  1983. struct i40e_vsi *vsi;
  1984. struct i40e_vf *vf;
  1985. int speed = 0;
  1986. int ret = 0;
  1987. /* validate the request */
  1988. if (vf_id >= pf->num_alloc_vfs) {
  1989. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
  1990. ret = -EINVAL;
  1991. goto error;
  1992. }
  1993. if (min_tx_rate) {
  1994. dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for vf %d.\n",
  1995. min_tx_rate, vf_id);
  1996. return -EINVAL;
  1997. }
  1998. vf = &(pf->vf[vf_id]);
  1999. vsi = pf->vsi[vf->lan_vsi_index];
  2000. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  2001. dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
  2002. ret = -EINVAL;
  2003. goto error;
  2004. }
  2005. switch (pf->hw.phy.link_info.link_speed) {
  2006. case I40E_LINK_SPEED_40GB:
  2007. speed = 40000;
  2008. break;
  2009. case I40E_LINK_SPEED_10GB:
  2010. speed = 10000;
  2011. break;
  2012. case I40E_LINK_SPEED_1GB:
  2013. speed = 1000;
  2014. break;
  2015. default:
  2016. break;
  2017. }
  2018. if (max_tx_rate > speed) {
  2019. dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for vf %d.",
  2020. max_tx_rate, vf->vf_id);
  2021. ret = -EINVAL;
  2022. goto error;
  2023. }
  2024. if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
  2025. dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
  2026. max_tx_rate = 50;
  2027. }
  2028. /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
  2029. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
  2030. max_tx_rate / I40E_BW_CREDIT_DIVISOR,
  2031. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  2032. if (ret) {
  2033. dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
  2034. ret);
  2035. ret = -EIO;
  2036. goto error;
  2037. }
  2038. vf->tx_rate = max_tx_rate;
  2039. error:
  2040. return ret;
  2041. }
  2042. /**
  2043. * i40e_ndo_get_vf_config
  2044. * @netdev: network interface device structure
  2045. * @vf_id: vf identifier
  2046. * @ivi: vf configuration structure
  2047. *
  2048. * return vf configuration
  2049. **/
  2050. int i40e_ndo_get_vf_config(struct net_device *netdev,
  2051. int vf_id, struct ifla_vf_info *ivi)
  2052. {
  2053. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2054. struct i40e_vsi *vsi = np->vsi;
  2055. struct i40e_pf *pf = vsi->back;
  2056. struct i40e_vf *vf;
  2057. int ret = 0;
  2058. /* validate the request */
  2059. if (vf_id >= pf->num_alloc_vfs) {
  2060. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2061. ret = -EINVAL;
  2062. goto error_param;
  2063. }
  2064. vf = &(pf->vf[vf_id]);
  2065. /* first vsi is always the LAN vsi */
  2066. vsi = pf->vsi[vf->lan_vsi_index];
  2067. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  2068. dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
  2069. ret = -EINVAL;
  2070. goto error_param;
  2071. }
  2072. ivi->vf = vf_id;
  2073. memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
  2074. ivi->max_tx_rate = vf->tx_rate;
  2075. ivi->min_tx_rate = 0;
  2076. ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
  2077. ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
  2078. I40E_VLAN_PRIORITY_SHIFT;
  2079. if (vf->link_forced == false)
  2080. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  2081. else if (vf->link_up == true)
  2082. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  2083. else
  2084. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  2085. ivi->spoofchk = vf->spoofchk;
  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. }
  2145. /**
  2146. * i40e_ndo_set_vf_spoofchk
  2147. * @netdev: network interface device structure
  2148. * @vf_id: vf identifier
  2149. * @enable: flag to enable or disable feature
  2150. *
  2151. * Enable or disable VF spoof checking
  2152. **/
  2153. int i40e_ndo_set_vf_spoofck(struct net_device *netdev, int vf_id, bool enable)
  2154. {
  2155. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2156. struct i40e_vsi *vsi = np->vsi;
  2157. struct i40e_pf *pf = vsi->back;
  2158. struct i40e_vsi_context ctxt;
  2159. struct i40e_hw *hw = &pf->hw;
  2160. struct i40e_vf *vf;
  2161. int ret = 0;
  2162. /* validate the request */
  2163. if (vf_id >= pf->num_alloc_vfs) {
  2164. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2165. ret = -EINVAL;
  2166. goto out;
  2167. }
  2168. vf = &(pf->vf[vf_id]);
  2169. if (enable == vf->spoofchk)
  2170. goto out;
  2171. vf->spoofchk = enable;
  2172. memset(&ctxt, 0, sizeof(ctxt));
  2173. ctxt.seid = pf->vsi[vf->lan_vsi_index]->seid;
  2174. ctxt.pf_num = pf->hw.pf_id;
  2175. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  2176. if (enable)
  2177. ctxt.info.sec_flags |= I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK;
  2178. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  2179. if (ret) {
  2180. dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
  2181. ret);
  2182. ret = -EIO;
  2183. }
  2184. out:
  2185. return ret;
  2186. }