i40e_virtchnl_pf.c 63 KB

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