i40e_virtchnl_pf.c 63 KB

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