i40e_virtchnl_pf.c 62 KB

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