ice_virtchnl_pf.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. #include "ice.h"
  4. #include "ice_lib.h"
  5. /**
  6. * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
  7. * @pf: pointer to the PF structure
  8. * @v_opcode: operation code
  9. * @v_retval: return value
  10. * @msg: pointer to the msg buffer
  11. * @msglen: msg length
  12. */
  13. static void
  14. ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
  15. enum ice_status v_retval, u8 *msg, u16 msglen)
  16. {
  17. struct ice_hw *hw = &pf->hw;
  18. struct ice_vf *vf = pf->vf;
  19. int i;
  20. for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
  21. /* Not all vfs are enabled so skip the ones that are not */
  22. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
  23. !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
  24. continue;
  25. /* Ignore return value on purpose - a given VF may fail, but
  26. * we need to keep going and send to all of them
  27. */
  28. ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
  29. msglen, NULL);
  30. }
  31. }
  32. /**
  33. * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
  34. * @vf: pointer to the VF structure
  35. * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
  36. * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
  37. * @link_up: whether or not to set the link up/down
  38. */
  39. static void
  40. ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
  41. int ice_link_speed, bool link_up)
  42. {
  43. if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
  44. pfe->event_data.link_event_adv.link_status = link_up;
  45. /* Speed in Mbps */
  46. pfe->event_data.link_event_adv.link_speed =
  47. ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
  48. } else {
  49. pfe->event_data.link_event.link_status = link_up;
  50. /* Legacy method for virtchnl link speeds */
  51. pfe->event_data.link_event.link_speed =
  52. (enum virtchnl_link_speed)
  53. ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
  54. }
  55. }
  56. /**
  57. * ice_set_pfe_link_forced - Force the virtchnl_pf_event link speed/status
  58. * @vf: pointer to the VF structure
  59. * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
  60. * @link_up: whether or not to set the link up/down
  61. */
  62. static void
  63. ice_set_pfe_link_forced(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
  64. bool link_up)
  65. {
  66. u16 link_speed;
  67. if (link_up)
  68. link_speed = ICE_AQ_LINK_SPEED_40GB;
  69. else
  70. link_speed = ICE_AQ_LINK_SPEED_UNKNOWN;
  71. ice_set_pfe_link(vf, pfe, link_speed, link_up);
  72. }
  73. /**
  74. * ice_vc_notify_vf_link_state - Inform a VF of link status
  75. * @vf: pointer to the VF structure
  76. *
  77. * send a link status message to a single VF
  78. */
  79. static void ice_vc_notify_vf_link_state(struct ice_vf *vf)
  80. {
  81. struct virtchnl_pf_event pfe = { 0 };
  82. struct ice_link_status *ls;
  83. struct ice_pf *pf = vf->pf;
  84. struct ice_hw *hw;
  85. hw = &pf->hw;
  86. ls = &hw->port_info->phy.link_info;
  87. pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
  88. pfe.severity = PF_EVENT_SEVERITY_INFO;
  89. if (vf->link_forced)
  90. ice_set_pfe_link_forced(vf, &pfe, vf->link_up);
  91. else
  92. ice_set_pfe_link(vf, &pfe, ls->link_speed, ls->link_info &
  93. ICE_AQ_LINK_UP);
  94. ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT, 0, (u8 *)&pfe,
  95. sizeof(pfe), NULL);
  96. }
  97. /**
  98. * ice_get_vf_vector - get VF interrupt vector register offset
  99. * @vf_msix: number of MSIx vector per VF on a PF
  100. * @vf_id: VF identifier
  101. * @i: index of MSIx vector
  102. */
  103. static u32 ice_get_vf_vector(int vf_msix, int vf_id, int i)
  104. {
  105. return ((i == 0) ? VFINT_DYN_CTLN(vf_id) :
  106. VFINT_DYN_CTLN(((vf_msix - 1) * (vf_id)) + (i - 1)));
  107. }
  108. /**
  109. * ice_free_vf_res - Free a VF's resources
  110. * @vf: pointer to the VF info
  111. */
  112. static void ice_free_vf_res(struct ice_vf *vf)
  113. {
  114. struct ice_pf *pf = vf->pf;
  115. int i, pf_vf_msix;
  116. /* First, disable VF's configuration API to prevent OS from
  117. * accessing the VF's VSI after it's freed or invalidated.
  118. */
  119. clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
  120. /* free vsi & disconnect it from the parent uplink */
  121. if (vf->lan_vsi_idx) {
  122. ice_vsi_release(pf->vsi[vf->lan_vsi_idx]);
  123. vf->lan_vsi_idx = 0;
  124. vf->lan_vsi_num = 0;
  125. vf->num_mac = 0;
  126. }
  127. pf_vf_msix = pf->num_vf_msix;
  128. /* Disable interrupts so that VF starts in a known state */
  129. for (i = 0; i < pf_vf_msix; i++) {
  130. u32 reg_idx;
  131. reg_idx = ice_get_vf_vector(pf_vf_msix, vf->vf_id, i);
  132. wr32(&pf->hw, reg_idx, VFINT_DYN_CTLN_CLEARPBA_M);
  133. ice_flush(&pf->hw);
  134. }
  135. /* reset some of the state variables keeping track of the resources */
  136. clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
  137. clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
  138. }
  139. /***********************enable_vf routines*****************************/
  140. /**
  141. * ice_dis_vf_mappings
  142. * @vf: pointer to the VF structure
  143. */
  144. static void ice_dis_vf_mappings(struct ice_vf *vf)
  145. {
  146. struct ice_pf *pf = vf->pf;
  147. struct ice_vsi *vsi;
  148. int first, last, v;
  149. struct ice_hw *hw;
  150. hw = &pf->hw;
  151. vsi = pf->vsi[vf->lan_vsi_idx];
  152. wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
  153. first = vf->first_vector_idx;
  154. last = first + pf->num_vf_msix - 1;
  155. for (v = first; v <= last; v++) {
  156. u32 reg;
  157. reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
  158. GLINT_VECT2FUNC_IS_PF_M) |
  159. ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
  160. GLINT_VECT2FUNC_PF_NUM_M));
  161. wr32(hw, GLINT_VECT2FUNC(v), reg);
  162. }
  163. if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
  164. wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
  165. else
  166. dev_err(&pf->pdev->dev,
  167. "Scattered mode for VF Tx queues is not yet implemented\n");
  168. if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
  169. wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
  170. else
  171. dev_err(&pf->pdev->dev,
  172. "Scattered mode for VF Rx queues is not yet implemented\n");
  173. }
  174. /**
  175. * ice_free_vfs - Free all VFs
  176. * @pf: pointer to the PF structure
  177. */
  178. void ice_free_vfs(struct ice_pf *pf)
  179. {
  180. struct ice_hw *hw = &pf->hw;
  181. int tmp, i;
  182. if (!pf->vf)
  183. return;
  184. while (test_and_set_bit(__ICE_VF_DIS, pf->state))
  185. usleep_range(1000, 2000);
  186. /* Avoid wait time by stopping all VFs at the same time */
  187. for (i = 0; i < pf->num_alloc_vfs; i++) {
  188. if (!test_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states))
  189. continue;
  190. /* stop rings without wait time */
  191. ice_vsi_stop_tx_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
  192. ICE_NO_RESET, i);
  193. ice_vsi_stop_rx_rings(pf->vsi[pf->vf[i].lan_vsi_idx]);
  194. clear_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states);
  195. }
  196. /* Disable IOV before freeing resources. This lets any VF drivers
  197. * running in the host get themselves cleaned up before we yank
  198. * the carpet out from underneath their feet.
  199. */
  200. if (!pci_vfs_assigned(pf->pdev))
  201. pci_disable_sriov(pf->pdev);
  202. else
  203. dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
  204. tmp = pf->num_alloc_vfs;
  205. pf->num_vf_qps = 0;
  206. pf->num_alloc_vfs = 0;
  207. for (i = 0; i < tmp; i++) {
  208. if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
  209. /* disable VF qp mappings */
  210. ice_dis_vf_mappings(&pf->vf[i]);
  211. /* Set this state so that assigned VF vectors can be
  212. * reclaimed by PF for reuse in ice_vsi_release(). No
  213. * need to clear this bit since pf->vf array is being
  214. * freed anyways after this for loop
  215. */
  216. set_bit(ICE_VF_STATE_CFG_INTR, pf->vf[i].vf_states);
  217. ice_free_vf_res(&pf->vf[i]);
  218. }
  219. }
  220. devm_kfree(&pf->pdev->dev, pf->vf);
  221. pf->vf = NULL;
  222. /* This check is for when the driver is unloaded while VFs are
  223. * assigned. Setting the number of VFs to 0 through sysfs is caught
  224. * before this function ever gets called.
  225. */
  226. if (!pci_vfs_assigned(pf->pdev)) {
  227. int vf_id;
  228. /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
  229. * work correctly when SR-IOV gets re-enabled.
  230. */
  231. for (vf_id = 0; vf_id < tmp; vf_id++) {
  232. u32 reg_idx, bit_idx;
  233. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  234. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  235. wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  236. }
  237. }
  238. clear_bit(__ICE_VF_DIS, pf->state);
  239. clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
  240. }
  241. /**
  242. * ice_trigger_vf_reset - Reset a VF on HW
  243. * @vf: pointer to the VF structure
  244. * @is_vflr: true if VFLR was issued, false if not
  245. *
  246. * Trigger hardware to start a reset for a particular VF. Expects the caller
  247. * to wait the proper amount of time to allow hardware to reset the VF before
  248. * it cleans up and restores VF functionality.
  249. */
  250. static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr)
  251. {
  252. struct ice_pf *pf = vf->pf;
  253. u32 reg, reg_idx, bit_idx;
  254. struct ice_hw *hw;
  255. int vf_abs_id, i;
  256. hw = &pf->hw;
  257. vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
  258. /* Inform VF that it is no longer active, as a warning */
  259. clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
  260. /* Disable VF's configuration API during reset. The flag is re-enabled
  261. * in ice_alloc_vf_res(), when it's safe again to access VF's VSI.
  262. * It's normally disabled in ice_free_vf_res(), but it's safer
  263. * to do it earlier to give some time to finish to any VF config
  264. * functions that may still be running at this point.
  265. */
  266. clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
  267. /* In the case of a VFLR, the HW has already reset the VF and we
  268. * just need to clean up, so don't hit the VFRTRIG register.
  269. */
  270. if (!is_vflr) {
  271. /* reset VF using VPGEN_VFRTRIG reg */
  272. reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
  273. reg |= VPGEN_VFRTRIG_VFSWR_M;
  274. wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
  275. }
  276. /* clear the VFLR bit in GLGEN_VFLRSTAT */
  277. reg_idx = (vf_abs_id) / 32;
  278. bit_idx = (vf_abs_id) % 32;
  279. wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  280. ice_flush(hw);
  281. wr32(hw, PF_PCI_CIAA,
  282. VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
  283. for (i = 0; i < 100; i++) {
  284. reg = rd32(hw, PF_PCI_CIAD);
  285. if ((reg & VF_TRANS_PENDING_M) != 0)
  286. dev_err(&pf->pdev->dev,
  287. "VF %d PCI transactions stuck\n", vf->vf_id);
  288. udelay(1);
  289. }
  290. }
  291. /**
  292. * ice_vsi_set_pvid - Set port VLAN id for the VSI
  293. * @vsi: the VSI being changed
  294. * @vid: the VLAN id to set as a PVID
  295. */
  296. static int ice_vsi_set_pvid(struct ice_vsi *vsi, u16 vid)
  297. {
  298. struct device *dev = &vsi->back->pdev->dev;
  299. struct ice_hw *hw = &vsi->back->hw;
  300. struct ice_vsi_ctx ctxt = { 0 };
  301. enum ice_status status;
  302. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_TAGGED |
  303. ICE_AQ_VSI_PVLAN_INSERT_PVID |
  304. ICE_AQ_VSI_VLAN_EMOD_STR;
  305. ctxt.info.pvid = cpu_to_le16(vid);
  306. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  307. status = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
  308. if (status) {
  309. dev_info(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
  310. status, hw->adminq.sq_last_status);
  311. return -EIO;
  312. }
  313. vsi->info.pvid = ctxt.info.pvid;
  314. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  315. return 0;
  316. }
  317. /**
  318. * ice_vsi_kill_pvid - Remove port VLAN id from the VSI
  319. * @vsi: the VSI being changed
  320. */
  321. static int ice_vsi_kill_pvid(struct ice_vsi *vsi)
  322. {
  323. struct ice_pf *pf = vsi->back;
  324. if (ice_vsi_manage_vlan_stripping(vsi, false)) {
  325. dev_err(&pf->pdev->dev, "Error removing Port VLAN on VSI %i\n",
  326. vsi->vsi_num);
  327. return -ENODEV;
  328. }
  329. vsi->info.pvid = 0;
  330. return 0;
  331. }
  332. /**
  333. * ice_vf_vsi_setup - Set up a VF VSI
  334. * @pf: board private structure
  335. * @pi: pointer to the port_info instance
  336. * @vf_id: defines VF id to which this VSI connects.
  337. *
  338. * Returns pointer to the successfully allocated VSI struct on success,
  339. * otherwise returns NULL on failure.
  340. */
  341. static struct ice_vsi *
  342. ice_vf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi, u16 vf_id)
  343. {
  344. return ice_vsi_setup(pf, pi, ICE_VSI_VF, vf_id);
  345. }
  346. /**
  347. * ice_alloc_vsi_res - Setup VF VSI and its resources
  348. * @vf: pointer to the VF structure
  349. *
  350. * Returns 0 on success, negative value on failure
  351. */
  352. static int ice_alloc_vsi_res(struct ice_vf *vf)
  353. {
  354. struct ice_pf *pf = vf->pf;
  355. LIST_HEAD(tmp_add_list);
  356. u8 broadcast[ETH_ALEN];
  357. struct ice_vsi *vsi;
  358. int status = 0;
  359. vsi = ice_vf_vsi_setup(pf, pf->hw.port_info, vf->vf_id);
  360. if (!vsi) {
  361. dev_err(&pf->pdev->dev, "Failed to create VF VSI\n");
  362. return -ENOMEM;
  363. }
  364. vf->lan_vsi_idx = vsi->idx;
  365. vf->lan_vsi_num = vsi->vsi_num;
  366. /* first vector index is the VFs OICR index */
  367. vf->first_vector_idx = vsi->hw_base_vector;
  368. /* Since hw_base_vector holds the vector where data queue interrupts
  369. * starts, increment by 1 since VFs allocated vectors include OICR intr
  370. * as well.
  371. */
  372. vsi->hw_base_vector += 1;
  373. /* Check if port VLAN exist before, and restore it accordingly */
  374. if (vf->port_vlan_id)
  375. ice_vsi_set_pvid(vsi, vf->port_vlan_id);
  376. eth_broadcast_addr(broadcast);
  377. status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
  378. if (status)
  379. goto ice_alloc_vsi_res_exit;
  380. if (is_valid_ether_addr(vf->dflt_lan_addr.addr)) {
  381. status = ice_add_mac_to_list(vsi, &tmp_add_list,
  382. vf->dflt_lan_addr.addr);
  383. if (status)
  384. goto ice_alloc_vsi_res_exit;
  385. }
  386. status = ice_add_mac(&pf->hw, &tmp_add_list);
  387. if (status)
  388. dev_err(&pf->pdev->dev, "could not add mac filters\n");
  389. /* Clear this bit after VF initialization since we shouldn't reclaim
  390. * and reassign interrupts for synchronous or asynchronous VFR events.
  391. * We don't want to reconfigure interrupts since AVF driver doesn't
  392. * expect vector assignment to be changed unless there is a request for
  393. * more vectors.
  394. */
  395. clear_bit(ICE_VF_STATE_CFG_INTR, vf->vf_states);
  396. ice_alloc_vsi_res_exit:
  397. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  398. return status;
  399. }
  400. /**
  401. * ice_alloc_vf_res - Allocate VF resources
  402. * @vf: pointer to the VF structure
  403. */
  404. static int ice_alloc_vf_res(struct ice_vf *vf)
  405. {
  406. int status;
  407. /* setup VF VSI and necessary resources */
  408. status = ice_alloc_vsi_res(vf);
  409. if (status)
  410. goto ice_alloc_vf_res_exit;
  411. if (vf->trusted)
  412. set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  413. else
  414. clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  415. /* VF is now completely initialized */
  416. set_bit(ICE_VF_STATE_INIT, vf->vf_states);
  417. return status;
  418. ice_alloc_vf_res_exit:
  419. ice_free_vf_res(vf);
  420. return status;
  421. }
  422. /**
  423. * ice_ena_vf_mappings
  424. * @vf: pointer to the VF structure
  425. *
  426. * Enable VF vectors and queues allocation by writing the details into
  427. * respective registers.
  428. */
  429. static void ice_ena_vf_mappings(struct ice_vf *vf)
  430. {
  431. struct ice_pf *pf = vf->pf;
  432. struct ice_vsi *vsi;
  433. int first, last, v;
  434. struct ice_hw *hw;
  435. int abs_vf_id;
  436. u32 reg;
  437. hw = &pf->hw;
  438. vsi = pf->vsi[vf->lan_vsi_idx];
  439. first = vf->first_vector_idx;
  440. last = (first + pf->num_vf_msix) - 1;
  441. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  442. /* VF Vector allocation */
  443. reg = (((first << VPINT_ALLOC_FIRST_S) & VPINT_ALLOC_FIRST_M) |
  444. ((last << VPINT_ALLOC_LAST_S) & VPINT_ALLOC_LAST_M) |
  445. VPINT_ALLOC_VALID_M);
  446. wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
  447. /* map the interrupts to its functions */
  448. for (v = first; v <= last; v++) {
  449. reg = (((abs_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
  450. GLINT_VECT2FUNC_VF_NUM_M) |
  451. ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
  452. GLINT_VECT2FUNC_PF_NUM_M));
  453. wr32(hw, GLINT_VECT2FUNC(v), reg);
  454. }
  455. /* VF Tx queues allocation */
  456. if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
  457. wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id),
  458. VPLAN_TXQ_MAPENA_TX_ENA_M);
  459. /* set the VF PF Tx queue range
  460. * VFNUMQ value should be set to (number of queues - 1). A value
  461. * of 0 means 1 queue and a value of 255 means 256 queues
  462. */
  463. reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
  464. VPLAN_TX_QBASE_VFFIRSTQ_M) |
  465. (((vsi->alloc_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
  466. VPLAN_TX_QBASE_VFNUMQ_M));
  467. wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
  468. } else {
  469. dev_err(&pf->pdev->dev,
  470. "Scattered mode for VF Tx queues is not yet implemented\n");
  471. }
  472. /* VF Rx queues allocation */
  473. if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
  474. wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id),
  475. VPLAN_RXQ_MAPENA_RX_ENA_M);
  476. /* set the VF PF Rx queue range
  477. * VFNUMQ value should be set to (number of queues - 1). A value
  478. * of 0 means 1 queue and a value of 255 means 256 queues
  479. */
  480. reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
  481. VPLAN_RX_QBASE_VFFIRSTQ_M) |
  482. (((vsi->alloc_txq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
  483. VPLAN_RX_QBASE_VFNUMQ_M));
  484. wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
  485. } else {
  486. dev_err(&pf->pdev->dev,
  487. "Scattered mode for VF Rx queues is not yet implemented\n");
  488. }
  489. }
  490. /**
  491. * ice_determine_res
  492. * @pf: pointer to the PF structure
  493. * @avail_res: available resources in the PF structure
  494. * @max_res: maximum resources that can be given per VF
  495. * @min_res: minimum resources that can be given per VF
  496. *
  497. * Returns non-zero value if resources (queues/vectors) are available or
  498. * returns zero if PF cannot accommodate for all num_alloc_vfs.
  499. */
  500. static int
  501. ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
  502. {
  503. bool checked_min_res = false;
  504. int res;
  505. /* start by checking if PF can assign max number of resources for
  506. * all num_alloc_vfs.
  507. * if yes, return number per VF
  508. * If no, divide by 2 and roundup, check again
  509. * repeat the loop till we reach a point where even minimum resources
  510. * are not available, in that case return 0
  511. */
  512. res = max_res;
  513. while ((res >= min_res) && !checked_min_res) {
  514. int num_all_res;
  515. num_all_res = pf->num_alloc_vfs * res;
  516. if (num_all_res <= avail_res)
  517. return res;
  518. if (res == min_res)
  519. checked_min_res = true;
  520. res = DIV_ROUND_UP(res, 2);
  521. }
  522. return 0;
  523. }
  524. /**
  525. * ice_check_avail_res - check if vectors and queues are available
  526. * @pf: pointer to the PF structure
  527. *
  528. * This function is where we calculate actual number of resources for VF VSIs,
  529. * we don't reserve ahead of time during probe. Returns success if vectors and
  530. * queues resources are available, otherwise returns error code
  531. */
  532. static int ice_check_avail_res(struct ice_pf *pf)
  533. {
  534. u16 num_msix, num_txq, num_rxq;
  535. if (!pf->num_alloc_vfs)
  536. return -EINVAL;
  537. /* Grab from HW interrupts common pool
  538. * Note: By the time the user decides it needs more vectors in a VF
  539. * its already too late since one must decide this prior to creating the
  540. * VF interface. So the best we can do is take a guess as to what the
  541. * user might want.
  542. *
  543. * We have two policies for vector allocation:
  544. * 1. if num_alloc_vfs is from 1 to 16, then we consider this as small
  545. * number of NFV VFs used for NFV appliances, since this is a special
  546. * case, we try to assign maximum vectors per VF (65) as much as
  547. * possible, based on determine_resources algorithm.
  548. * 2. if num_alloc_vfs is from 17 to 256, then its large number of
  549. * regular VFs which are not used for any special purpose. Hence try to
  550. * grab default interrupt vectors (5 as supported by AVF driver).
  551. */
  552. if (pf->num_alloc_vfs <= 16) {
  553. num_msix = ice_determine_res(pf, pf->num_avail_hw_msix,
  554. ICE_MAX_INTR_PER_VF,
  555. ICE_MIN_INTR_PER_VF);
  556. } else if (pf->num_alloc_vfs <= ICE_MAX_VF_COUNT) {
  557. num_msix = ice_determine_res(pf, pf->num_avail_hw_msix,
  558. ICE_DFLT_INTR_PER_VF,
  559. ICE_MIN_INTR_PER_VF);
  560. } else {
  561. dev_err(&pf->pdev->dev,
  562. "Number of VFs %d exceeds max VF count %d\n",
  563. pf->num_alloc_vfs, ICE_MAX_VF_COUNT);
  564. return -EIO;
  565. }
  566. if (!num_msix)
  567. return -EIO;
  568. /* Grab from the common pool
  569. * start by requesting Default queues (4 as supported by AVF driver),
  570. * Note that, the main difference between queues and vectors is, latter
  571. * can only be reserved at init time but queues can be requested by VF
  572. * at runtime through Virtchnl, that is the reason we start by reserving
  573. * few queues.
  574. */
  575. num_txq = ice_determine_res(pf, pf->q_left_tx, ICE_DFLT_QS_PER_VF,
  576. ICE_MIN_QS_PER_VF);
  577. num_rxq = ice_determine_res(pf, pf->q_left_rx, ICE_DFLT_QS_PER_VF,
  578. ICE_MIN_QS_PER_VF);
  579. if (!num_txq || !num_rxq)
  580. return -EIO;
  581. /* since AVF driver works with only queue pairs which means, it expects
  582. * to have equal number of Rx and Tx queues, so take the minimum of
  583. * available Tx or Rx queues
  584. */
  585. pf->num_vf_qps = min_t(int, num_txq, num_rxq);
  586. pf->num_vf_msix = num_msix;
  587. return 0;
  588. }
  589. /**
  590. * ice_cleanup_and_realloc_vf - Clean up VF and reallocate resources after reset
  591. * @vf: pointer to the VF structure
  592. *
  593. * Cleanup a VF after the hardware reset is finished. Expects the caller to
  594. * have verified whether the reset is finished properly, and ensure the
  595. * minimum amount of wait time has passed. Reallocate VF resources back to make
  596. * VF state active
  597. */
  598. static void ice_cleanup_and_realloc_vf(struct ice_vf *vf)
  599. {
  600. struct ice_pf *pf = vf->pf;
  601. struct ice_hw *hw;
  602. u32 reg;
  603. hw = &pf->hw;
  604. /* PF software completes the flow by notifying VF that reset flow is
  605. * completed. This is done by enabling hardware by clearing the reset
  606. * bit in the VPGEN_VFRTRIG reg and setting VFR_STATE in the VFGEN_RSTAT
  607. * register to VFR completed (done at the end of this function)
  608. * By doing this we allow HW to access VF memory at any point. If we
  609. * did it any sooner, HW could access memory while it was being freed
  610. * in ice_free_vf_res(), causing an IOMMU fault.
  611. *
  612. * On the other hand, this needs to be done ASAP, because the VF driver
  613. * is waiting for this to happen and may report a timeout. It's
  614. * harmless, but it gets logged into Guest OS kernel log, so best avoid
  615. * it.
  616. */
  617. reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
  618. reg &= ~VPGEN_VFRTRIG_VFSWR_M;
  619. wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
  620. /* reallocate VF resources to finish resetting the VSI state */
  621. if (!ice_alloc_vf_res(vf)) {
  622. ice_ena_vf_mappings(vf);
  623. set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
  624. clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
  625. vf->num_vlan = 0;
  626. }
  627. /* Tell the VF driver the reset is done. This needs to be done only
  628. * after VF has been fully initialized, because the VF driver may
  629. * request resources immediately after setting this flag.
  630. */
  631. wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
  632. }
  633. /**
  634. * ice_reset_all_vfs - reset all allocated VFs in one go
  635. * @pf: pointer to the PF structure
  636. * @is_vflr: true if VFLR was issued, false if not
  637. *
  638. * First, tell the hardware to reset each VF, then do all the waiting in one
  639. * chunk, and finally finish restoring each VF after the wait. This is useful
  640. * during PF routines which need to reset all VFs, as otherwise it must perform
  641. * these resets in a serialized fashion.
  642. *
  643. * Returns true if any VFs were reset, and false otherwise.
  644. */
  645. bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
  646. {
  647. struct ice_hw *hw = &pf->hw;
  648. int v, i;
  649. /* If we don't have any VFs, then there is nothing to reset */
  650. if (!pf->num_alloc_vfs)
  651. return false;
  652. /* If VFs have been disabled, there is no need to reset */
  653. if (test_and_set_bit(__ICE_VF_DIS, pf->state))
  654. return false;
  655. /* Begin reset on all VFs at once */
  656. for (v = 0; v < pf->num_alloc_vfs; v++)
  657. ice_trigger_vf_reset(&pf->vf[v], is_vflr);
  658. /* Call Disable LAN Tx queue AQ call with VFR bit set and 0
  659. * queues to inform Firmware about VF reset.
  660. */
  661. for (v = 0; v < pf->num_alloc_vfs; v++)
  662. ice_dis_vsi_txq(pf->vsi[0]->port_info, 0, NULL, NULL,
  663. ICE_VF_RESET, v, NULL);
  664. /* HW requires some time to make sure it can flush the FIFO for a VF
  665. * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
  666. * sequence to make sure that it has completed. We'll keep track of
  667. * the VFs using a simple iterator that increments once that VF has
  668. * finished resetting.
  669. */
  670. for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
  671. usleep_range(10000, 20000);
  672. /* Check each VF in sequence */
  673. while (v < pf->num_alloc_vfs) {
  674. struct ice_vf *vf = &pf->vf[v];
  675. u32 reg;
  676. reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
  677. if (!(reg & VPGEN_VFRSTAT_VFRD_M))
  678. break;
  679. /* If the current VF has finished resetting, move on
  680. * to the next VF in sequence.
  681. */
  682. v++;
  683. }
  684. }
  685. /* Display a warning if at least one VF didn't manage to reset in
  686. * time, but continue on with the operation.
  687. */
  688. if (v < pf->num_alloc_vfs)
  689. dev_warn(&pf->pdev->dev, "VF reset check timeout\n");
  690. usleep_range(10000, 20000);
  691. /* free VF resources to begin resetting the VSI state */
  692. for (v = 0; v < pf->num_alloc_vfs; v++)
  693. ice_free_vf_res(&pf->vf[v]);
  694. if (ice_check_avail_res(pf)) {
  695. dev_err(&pf->pdev->dev,
  696. "Cannot allocate VF resources, try with fewer number of VFs\n");
  697. return false;
  698. }
  699. /* Finish the reset on each VF */
  700. for (v = 0; v < pf->num_alloc_vfs; v++)
  701. ice_cleanup_and_realloc_vf(&pf->vf[v]);
  702. ice_flush(hw);
  703. clear_bit(__ICE_VF_DIS, pf->state);
  704. return true;
  705. }
  706. /**
  707. * ice_reset_vf - Reset a particular VF
  708. * @vf: pointer to the VF structure
  709. * @is_vflr: true if VFLR was issued, false if not
  710. *
  711. * Returns true if the VF is reset, false otherwise.
  712. */
  713. static bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
  714. {
  715. struct ice_pf *pf = vf->pf;
  716. struct ice_hw *hw = &pf->hw;
  717. bool rsd = false;
  718. u32 reg;
  719. int i;
  720. /* If the VFs have been disabled, this means something else is
  721. * resetting the VF, so we shouldn't continue.
  722. */
  723. if (test_and_set_bit(__ICE_VF_DIS, pf->state))
  724. return false;
  725. ice_trigger_vf_reset(vf, is_vflr);
  726. if (test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
  727. ice_vsi_stop_tx_rings(pf->vsi[vf->lan_vsi_idx], ICE_VF_RESET,
  728. vf->vf_id);
  729. ice_vsi_stop_rx_rings(pf->vsi[vf->lan_vsi_idx]);
  730. clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
  731. } else {
  732. /* Call Disable LAN Tx queue AQ call even when queues are not
  733. * enabled. This is needed for successful completiom of VFR
  734. */
  735. ice_dis_vsi_txq(pf->vsi[vf->lan_vsi_idx]->port_info, 0,
  736. NULL, NULL, ICE_VF_RESET, vf->vf_id, NULL);
  737. }
  738. /* poll VPGEN_VFRSTAT reg to make sure
  739. * that reset is complete
  740. */
  741. for (i = 0; i < 10; i++) {
  742. /* VF reset requires driver to first reset the VF and then
  743. * poll the status register to make sure that the reset
  744. * completed successfully.
  745. */
  746. usleep_range(10000, 20000);
  747. reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
  748. if (reg & VPGEN_VFRSTAT_VFRD_M) {
  749. rsd = true;
  750. break;
  751. }
  752. }
  753. /* Display a warning if VF didn't manage to reset in time, but need to
  754. * continue on with the operation.
  755. */
  756. if (!rsd)
  757. dev_warn(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  758. vf->vf_id);
  759. usleep_range(10000, 20000);
  760. /* free VF resources to begin resetting the VSI state */
  761. ice_free_vf_res(vf);
  762. ice_cleanup_and_realloc_vf(vf);
  763. ice_flush(hw);
  764. clear_bit(__ICE_VF_DIS, pf->state);
  765. return true;
  766. }
  767. /**
  768. * ice_vc_notify_link_state - Inform all VFs on a PF of link status
  769. * @pf: pointer to the PF structure
  770. */
  771. void ice_vc_notify_link_state(struct ice_pf *pf)
  772. {
  773. int i;
  774. for (i = 0; i < pf->num_alloc_vfs; i++)
  775. ice_vc_notify_vf_link_state(&pf->vf[i]);
  776. }
  777. /**
  778. * ice_vc_notify_reset - Send pending reset message to all VFs
  779. * @pf: pointer to the PF structure
  780. *
  781. * indicate a pending reset to all VFs on a given PF
  782. */
  783. void ice_vc_notify_reset(struct ice_pf *pf)
  784. {
  785. struct virtchnl_pf_event pfe;
  786. if (!pf->num_alloc_vfs)
  787. return;
  788. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  789. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  790. ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, ICE_SUCCESS,
  791. (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
  792. }
  793. /**
  794. * ice_vc_notify_vf_reset - Notify VF of a reset event
  795. * @vf: pointer to the VF structure
  796. */
  797. static void ice_vc_notify_vf_reset(struct ice_vf *vf)
  798. {
  799. struct virtchnl_pf_event pfe;
  800. /* validate the request */
  801. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  802. return;
  803. /* verify if the VF is in either init or active before proceeding */
  804. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
  805. !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
  806. return;
  807. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  808. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  809. ice_aq_send_msg_to_vf(&vf->pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT, 0,
  810. (u8 *)&pfe, sizeof(pfe), NULL);
  811. }
  812. /**
  813. * ice_alloc_vfs - Allocate and set up VFs resources
  814. * @pf: pointer to the PF structure
  815. * @num_alloc_vfs: number of VFs to allocate
  816. */
  817. static int ice_alloc_vfs(struct ice_pf *pf, u16 num_alloc_vfs)
  818. {
  819. struct ice_hw *hw = &pf->hw;
  820. struct ice_vf *vfs;
  821. int i, ret;
  822. /* Disable global interrupt 0 so we don't try to handle the VFLR. */
  823. wr32(hw, GLINT_DYN_CTL(pf->hw_oicr_idx),
  824. ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
  825. ice_flush(hw);
  826. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  827. if (ret) {
  828. pf->num_alloc_vfs = 0;
  829. goto err_unroll_intr;
  830. }
  831. /* allocate memory */
  832. vfs = devm_kcalloc(&pf->pdev->dev, num_alloc_vfs, sizeof(*vfs),
  833. GFP_KERNEL);
  834. if (!vfs) {
  835. ret = -ENOMEM;
  836. goto err_unroll_sriov;
  837. }
  838. pf->vf = vfs;
  839. /* apply default profile */
  840. for (i = 0; i < num_alloc_vfs; i++) {
  841. vfs[i].pf = pf;
  842. vfs[i].vf_sw_id = pf->first_sw;
  843. vfs[i].vf_id = i;
  844. /* assign default capabilities */
  845. set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
  846. vfs[i].spoofchk = true;
  847. /* Set this state so that PF driver does VF vector assignment */
  848. set_bit(ICE_VF_STATE_CFG_INTR, vfs[i].vf_states);
  849. }
  850. pf->num_alloc_vfs = num_alloc_vfs;
  851. /* VF resources get allocated during reset */
  852. if (!ice_reset_all_vfs(pf, false))
  853. goto err_unroll_sriov;
  854. goto err_unroll_intr;
  855. err_unroll_sriov:
  856. pci_disable_sriov(pf->pdev);
  857. err_unroll_intr:
  858. /* rearm interrupts here */
  859. ice_irq_dynamic_ena(hw, NULL, NULL);
  860. return ret;
  861. }
  862. /**
  863. * ice_pf_state_is_nominal - checks the pf for nominal state
  864. * @pf: pointer to pf to check
  865. *
  866. * Check the PF's state for a collection of bits that would indicate
  867. * the PF is in a state that would inhibit normal operation for
  868. * driver functionality.
  869. *
  870. * Returns true if PF is in a nominal state.
  871. * Returns false otherwise
  872. */
  873. static bool ice_pf_state_is_nominal(struct ice_pf *pf)
  874. {
  875. DECLARE_BITMAP(check_bits, __ICE_STATE_NBITS) = { 0 };
  876. if (!pf)
  877. return false;
  878. bitmap_set(check_bits, 0, __ICE_STATE_NOMINAL_CHECK_BITS);
  879. if (bitmap_intersects(pf->state, check_bits, __ICE_STATE_NBITS))
  880. return false;
  881. return true;
  882. }
  883. /**
  884. * ice_pci_sriov_ena - Enable or change number of VFs
  885. * @pf: pointer to the PF structure
  886. * @num_vfs: number of VFs to allocate
  887. */
  888. static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
  889. {
  890. int pre_existing_vfs = pci_num_vf(pf->pdev);
  891. struct device *dev = &pf->pdev->dev;
  892. int err;
  893. if (!ice_pf_state_is_nominal(pf)) {
  894. dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
  895. return -EBUSY;
  896. }
  897. if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
  898. dev_err(dev, "This device is not capable of SR-IOV\n");
  899. return -ENODEV;
  900. }
  901. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  902. ice_free_vfs(pf);
  903. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  904. return num_vfs;
  905. if (num_vfs > pf->num_vfs_supported) {
  906. dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
  907. num_vfs, pf->num_vfs_supported);
  908. return -ENOTSUPP;
  909. }
  910. dev_info(dev, "Allocating %d VFs\n", num_vfs);
  911. err = ice_alloc_vfs(pf, num_vfs);
  912. if (err) {
  913. dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
  914. return err;
  915. }
  916. set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
  917. return num_vfs;
  918. }
  919. /**
  920. * ice_sriov_configure - Enable or change number of VFs via sysfs
  921. * @pdev: pointer to a pci_dev structure
  922. * @num_vfs: number of VFs to allocate
  923. *
  924. * This function is called when the user updates the number of VFs in sysfs.
  925. */
  926. int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
  927. {
  928. struct ice_pf *pf = pci_get_drvdata(pdev);
  929. if (num_vfs)
  930. return ice_pci_sriov_ena(pf, num_vfs);
  931. if (!pci_vfs_assigned(pdev)) {
  932. ice_free_vfs(pf);
  933. } else {
  934. dev_err(&pf->pdev->dev,
  935. "can't free VFs because some are assigned to VMs.\n");
  936. return -EBUSY;
  937. }
  938. return 0;
  939. }
  940. /**
  941. * ice_process_vflr_event - Free VF resources via IRQ calls
  942. * @pf: pointer to the PF structure
  943. *
  944. * called from the VLFR IRQ handler to
  945. * free up VF resources and state variables
  946. */
  947. void ice_process_vflr_event(struct ice_pf *pf)
  948. {
  949. struct ice_hw *hw = &pf->hw;
  950. int vf_id;
  951. u32 reg;
  952. if (!test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
  953. !pf->num_alloc_vfs)
  954. return;
  955. /* Re-enable the VFLR interrupt cause here, before looking for which
  956. * VF got reset. Otherwise, if another VF gets a reset while the
  957. * first one is being processed, that interrupt will be lost, and
  958. * that VF will be stuck in reset forever.
  959. */
  960. reg = rd32(hw, PFINT_OICR_ENA);
  961. reg |= PFINT_OICR_VFLR_M;
  962. wr32(hw, PFINT_OICR_ENA, reg);
  963. ice_flush(hw);
  964. clear_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
  965. for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
  966. struct ice_vf *vf = &pf->vf[vf_id];
  967. u32 reg_idx, bit_idx;
  968. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  969. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  970. /* read GLGEN_VFLRSTAT register to find out the flr VFs */
  971. reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
  972. if (reg & BIT(bit_idx))
  973. /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
  974. ice_reset_vf(vf, true);
  975. }
  976. }
  977. /**
  978. * ice_vc_dis_vf - Disable a given VF via SW reset
  979. * @vf: pointer to the VF info
  980. *
  981. * Disable the VF through a SW reset
  982. */
  983. static void ice_vc_dis_vf(struct ice_vf *vf)
  984. {
  985. ice_vc_notify_vf_reset(vf);
  986. ice_reset_vf(vf, false);
  987. }
  988. /**
  989. * ice_vc_send_msg_to_vf - Send message to VF
  990. * @vf: pointer to the VF info
  991. * @v_opcode: virtual channel opcode
  992. * @v_retval: virtual channel return value
  993. * @msg: pointer to the msg buffer
  994. * @msglen: msg length
  995. *
  996. * send msg to VF
  997. */
  998. static int ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
  999. enum ice_status v_retval, u8 *msg, u16 msglen)
  1000. {
  1001. enum ice_status aq_ret;
  1002. struct ice_pf *pf;
  1003. /* validate the request */
  1004. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  1005. return -EINVAL;
  1006. pf = vf->pf;
  1007. /* single place to detect unsuccessful return values */
  1008. if (v_retval) {
  1009. vf->num_inval_msgs++;
  1010. dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
  1011. vf->vf_id, v_opcode, v_retval);
  1012. if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
  1013. dev_err(&pf->pdev->dev,
  1014. "Number of invalid messages exceeded for VF %d\n",
  1015. vf->vf_id);
  1016. dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
  1017. set_bit(ICE_VF_STATE_DIS, vf->vf_states);
  1018. return -EIO;
  1019. }
  1020. } else {
  1021. vf->num_valid_msgs++;
  1022. /* reset the invalid counter, if a valid message is received. */
  1023. vf->num_inval_msgs = 0;
  1024. }
  1025. aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
  1026. msg, msglen, NULL);
  1027. if (aq_ret) {
  1028. dev_info(&pf->pdev->dev,
  1029. "Unable to send the message to VF %d aq_err %d\n",
  1030. vf->vf_id, pf->hw.mailboxq.sq_last_status);
  1031. return -EIO;
  1032. }
  1033. return 0;
  1034. }
  1035. /**
  1036. * ice_vc_get_ver_msg
  1037. * @vf: pointer to the VF info
  1038. * @msg: pointer to the msg buffer
  1039. *
  1040. * called from the VF to request the API version used by the PF
  1041. */
  1042. static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
  1043. {
  1044. struct virtchnl_version_info info = {
  1045. VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
  1046. };
  1047. vf->vf_ver = *(struct virtchnl_version_info *)msg;
  1048. /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
  1049. if (VF_IS_V10(&vf->vf_ver))
  1050. info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
  1051. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION, ICE_SUCCESS,
  1052. (u8 *)&info,
  1053. sizeof(struct virtchnl_version_info));
  1054. }
  1055. /**
  1056. * ice_vc_get_vf_res_msg
  1057. * @vf: pointer to the VF info
  1058. * @msg: pointer to the msg buffer
  1059. *
  1060. * called from the VF to request its resources
  1061. */
  1062. static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
  1063. {
  1064. struct virtchnl_vf_resource *vfres = NULL;
  1065. enum ice_status aq_ret = 0;
  1066. struct ice_pf *pf = vf->pf;
  1067. struct ice_vsi *vsi;
  1068. int len = 0;
  1069. int ret;
  1070. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  1071. aq_ret = ICE_ERR_PARAM;
  1072. goto err;
  1073. }
  1074. len = sizeof(struct virtchnl_vf_resource);
  1075. vfres = devm_kzalloc(&pf->pdev->dev, len, GFP_KERNEL);
  1076. if (!vfres) {
  1077. aq_ret = ICE_ERR_NO_MEMORY;
  1078. len = 0;
  1079. goto err;
  1080. }
  1081. if (VF_IS_V11(&vf->vf_ver))
  1082. vf->driver_caps = *(u32 *)msg;
  1083. else
  1084. vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
  1085. VIRTCHNL_VF_OFFLOAD_RSS_REG |
  1086. VIRTCHNL_VF_OFFLOAD_VLAN;
  1087. vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
  1088. vsi = pf->vsi[vf->lan_vsi_idx];
  1089. if (!vsi->info.pvid)
  1090. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
  1091. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
  1092. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
  1093. } else {
  1094. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
  1095. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
  1096. else
  1097. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
  1098. }
  1099. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
  1100. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
  1101. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
  1102. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
  1103. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
  1104. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
  1105. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
  1106. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
  1107. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
  1108. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
  1109. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
  1110. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
  1111. if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
  1112. vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
  1113. vfres->num_vsis = 1;
  1114. /* Tx and Rx queue are equal for VF */
  1115. vfres->num_queue_pairs = vsi->num_txq;
  1116. vfres->max_vectors = pf->num_vf_msix;
  1117. vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
  1118. vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
  1119. vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
  1120. vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
  1121. vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
  1122. ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
  1123. vf->dflt_lan_addr.addr);
  1124. set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
  1125. err:
  1126. /* send the response back to the VF */
  1127. ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, aq_ret,
  1128. (u8 *)vfres, len);
  1129. devm_kfree(&pf->pdev->dev, vfres);
  1130. return ret;
  1131. }
  1132. /**
  1133. * ice_vc_reset_vf_msg
  1134. * @vf: pointer to the VF info
  1135. *
  1136. * called from the VF to reset itself,
  1137. * unlike other virtchnl messages, PF driver
  1138. * doesn't send the response back to the VF
  1139. */
  1140. static void ice_vc_reset_vf_msg(struct ice_vf *vf)
  1141. {
  1142. if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
  1143. ice_reset_vf(vf, false);
  1144. }
  1145. /**
  1146. * ice_find_vsi_from_id
  1147. * @pf: the pf structure to search for the VSI
  1148. * @id: id of the VSI it is searching for
  1149. *
  1150. * searches for the VSI with the given id
  1151. */
  1152. static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
  1153. {
  1154. int i;
  1155. for (i = 0; i < pf->num_alloc_vsi; i++)
  1156. if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
  1157. return pf->vsi[i];
  1158. return NULL;
  1159. }
  1160. /**
  1161. * ice_vc_isvalid_vsi_id
  1162. * @vf: pointer to the VF info
  1163. * @vsi_id: VF relative VSI id
  1164. *
  1165. * check for the valid VSI id
  1166. */
  1167. static bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
  1168. {
  1169. struct ice_pf *pf = vf->pf;
  1170. struct ice_vsi *vsi;
  1171. vsi = ice_find_vsi_from_id(pf, vsi_id);
  1172. return (vsi && (vsi->vf_id == vf->vf_id));
  1173. }
  1174. /**
  1175. * ice_vc_isvalid_q_id
  1176. * @vf: pointer to the VF info
  1177. * @vsi_id: VSI id
  1178. * @qid: VSI relative queue id
  1179. *
  1180. * check for the valid queue id
  1181. */
  1182. static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
  1183. {
  1184. struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
  1185. /* allocated Tx and Rx queues should be always equal for VF VSI */
  1186. return (vsi && (qid < vsi->alloc_txq));
  1187. }
  1188. /**
  1189. * ice_vc_config_rss_key
  1190. * @vf: pointer to the VF info
  1191. * @msg: pointer to the msg buffer
  1192. *
  1193. * Configure the VF's RSS key
  1194. */
  1195. static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
  1196. {
  1197. struct virtchnl_rss_key *vrk =
  1198. (struct virtchnl_rss_key *)msg;
  1199. struct ice_vsi *vsi = NULL;
  1200. enum ice_status aq_ret;
  1201. int ret;
  1202. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1203. aq_ret = ICE_ERR_PARAM;
  1204. goto error_param;
  1205. }
  1206. if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
  1207. aq_ret = ICE_ERR_PARAM;
  1208. goto error_param;
  1209. }
  1210. vsi = ice_find_vsi_from_id(vf->pf, vrk->vsi_id);
  1211. if (!vsi) {
  1212. aq_ret = ICE_ERR_PARAM;
  1213. goto error_param;
  1214. }
  1215. if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
  1216. aq_ret = ICE_ERR_PARAM;
  1217. goto error_param;
  1218. }
  1219. if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
  1220. aq_ret = ICE_ERR_PARAM;
  1221. goto error_param;
  1222. }
  1223. ret = ice_set_rss(vsi, vrk->key, NULL, 0);
  1224. aq_ret = ret ? ICE_ERR_PARAM : ICE_SUCCESS;
  1225. error_param:
  1226. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, aq_ret,
  1227. NULL, 0);
  1228. }
  1229. /**
  1230. * ice_vc_config_rss_lut
  1231. * @vf: pointer to the VF info
  1232. * @msg: pointer to the msg buffer
  1233. *
  1234. * Configure the VF's RSS LUT
  1235. */
  1236. static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
  1237. {
  1238. struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
  1239. struct ice_vsi *vsi = NULL;
  1240. enum ice_status aq_ret;
  1241. int ret;
  1242. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1243. aq_ret = ICE_ERR_PARAM;
  1244. goto error_param;
  1245. }
  1246. if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
  1247. aq_ret = ICE_ERR_PARAM;
  1248. goto error_param;
  1249. }
  1250. vsi = ice_find_vsi_from_id(vf->pf, vrl->vsi_id);
  1251. if (!vsi) {
  1252. aq_ret = ICE_ERR_PARAM;
  1253. goto error_param;
  1254. }
  1255. if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
  1256. aq_ret = ICE_ERR_PARAM;
  1257. goto error_param;
  1258. }
  1259. if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
  1260. aq_ret = ICE_ERR_PARAM;
  1261. goto error_param;
  1262. }
  1263. ret = ice_set_rss(vsi, NULL, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE);
  1264. aq_ret = ret ? ICE_ERR_PARAM : ICE_SUCCESS;
  1265. error_param:
  1266. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, aq_ret,
  1267. NULL, 0);
  1268. }
  1269. /**
  1270. * ice_vc_get_stats_msg
  1271. * @vf: pointer to the VF info
  1272. * @msg: pointer to the msg buffer
  1273. *
  1274. * called from the VF to get VSI stats
  1275. */
  1276. static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
  1277. {
  1278. struct virtchnl_queue_select *vqs =
  1279. (struct virtchnl_queue_select *)msg;
  1280. enum ice_status aq_ret = 0;
  1281. struct ice_eth_stats stats;
  1282. struct ice_vsi *vsi;
  1283. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1284. aq_ret = ICE_ERR_PARAM;
  1285. goto error_param;
  1286. }
  1287. if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1288. aq_ret = ICE_ERR_PARAM;
  1289. goto error_param;
  1290. }
  1291. vsi = ice_find_vsi_from_id(vf->pf, vqs->vsi_id);
  1292. if (!vsi) {
  1293. aq_ret = ICE_ERR_PARAM;
  1294. goto error_param;
  1295. }
  1296. memset(&stats, 0, sizeof(struct ice_eth_stats));
  1297. ice_update_eth_stats(vsi);
  1298. stats = vsi->eth_stats;
  1299. error_param:
  1300. /* send the response to the VF */
  1301. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
  1302. (u8 *)&stats, sizeof(stats));
  1303. }
  1304. /**
  1305. * ice_vc_ena_qs_msg
  1306. * @vf: pointer to the VF info
  1307. * @msg: pointer to the msg buffer
  1308. *
  1309. * called from the VF to enable all or specific queue(s)
  1310. */
  1311. static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
  1312. {
  1313. struct virtchnl_queue_select *vqs =
  1314. (struct virtchnl_queue_select *)msg;
  1315. enum ice_status aq_ret = 0;
  1316. struct ice_vsi *vsi;
  1317. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1318. aq_ret = ICE_ERR_PARAM;
  1319. goto error_param;
  1320. }
  1321. if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1322. aq_ret = ICE_ERR_PARAM;
  1323. goto error_param;
  1324. }
  1325. if (!vqs->rx_queues && !vqs->tx_queues) {
  1326. aq_ret = ICE_ERR_PARAM;
  1327. goto error_param;
  1328. }
  1329. vsi = ice_find_vsi_from_id(vf->pf, vqs->vsi_id);
  1330. if (!vsi) {
  1331. aq_ret = ICE_ERR_PARAM;
  1332. goto error_param;
  1333. }
  1334. /* Enable only Rx rings, Tx rings were enabled by the FW when the
  1335. * Tx queue group list was configured and the context bits were
  1336. * programmed using ice_vsi_cfg_txqs
  1337. */
  1338. if (ice_vsi_start_rx_rings(vsi))
  1339. aq_ret = ICE_ERR_PARAM;
  1340. /* Set flag to indicate that queues are enabled */
  1341. if (!aq_ret)
  1342. set_bit(ICE_VF_STATE_ENA, vf->vf_states);
  1343. error_param:
  1344. /* send the response to the VF */
  1345. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, aq_ret,
  1346. NULL, 0);
  1347. }
  1348. /**
  1349. * ice_vc_dis_qs_msg
  1350. * @vf: pointer to the VF info
  1351. * @msg: pointer to the msg buffer
  1352. *
  1353. * called from the VF to disable all or specific
  1354. * queue(s)
  1355. */
  1356. static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
  1357. {
  1358. struct virtchnl_queue_select *vqs =
  1359. (struct virtchnl_queue_select *)msg;
  1360. enum ice_status aq_ret = 0;
  1361. struct ice_vsi *vsi;
  1362. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
  1363. !test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
  1364. aq_ret = ICE_ERR_PARAM;
  1365. goto error_param;
  1366. }
  1367. if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1368. aq_ret = ICE_ERR_PARAM;
  1369. goto error_param;
  1370. }
  1371. if (!vqs->rx_queues && !vqs->tx_queues) {
  1372. aq_ret = ICE_ERR_PARAM;
  1373. goto error_param;
  1374. }
  1375. vsi = ice_find_vsi_from_id(vf->pf, vqs->vsi_id);
  1376. if (!vsi) {
  1377. aq_ret = ICE_ERR_PARAM;
  1378. goto error_param;
  1379. }
  1380. if (ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, vf->vf_id)) {
  1381. dev_err(&vsi->back->pdev->dev,
  1382. "Failed to stop tx rings on VSI %d\n",
  1383. vsi->vsi_num);
  1384. aq_ret = ICE_ERR_PARAM;
  1385. }
  1386. if (ice_vsi_stop_rx_rings(vsi)) {
  1387. dev_err(&vsi->back->pdev->dev,
  1388. "Failed to stop rx rings on VSI %d\n",
  1389. vsi->vsi_num);
  1390. aq_ret = ICE_ERR_PARAM;
  1391. }
  1392. /* Clear enabled queues flag */
  1393. if (!aq_ret)
  1394. clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
  1395. error_param:
  1396. /* send the response to the VF */
  1397. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, aq_ret,
  1398. NULL, 0);
  1399. }
  1400. /**
  1401. * ice_vc_cfg_irq_map_msg
  1402. * @vf: pointer to the VF info
  1403. * @msg: pointer to the msg buffer
  1404. *
  1405. * called from the VF to configure the IRQ to queue map
  1406. */
  1407. static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
  1408. {
  1409. struct virtchnl_irq_map_info *irqmap_info =
  1410. (struct virtchnl_irq_map_info *)msg;
  1411. u16 vsi_id, vsi_q_id, vector_id;
  1412. struct virtchnl_vector_map *map;
  1413. struct ice_vsi *vsi = NULL;
  1414. struct ice_pf *pf = vf->pf;
  1415. enum ice_status aq_ret = 0;
  1416. unsigned long qmap;
  1417. int i;
  1418. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1419. aq_ret = ICE_ERR_PARAM;
  1420. goto error_param;
  1421. }
  1422. for (i = 0; i < irqmap_info->num_vectors; i++) {
  1423. map = &irqmap_info->vecmap[i];
  1424. vector_id = map->vector_id;
  1425. vsi_id = map->vsi_id;
  1426. /* validate msg params */
  1427. if (!(vector_id < pf->hw.func_caps.common_cap
  1428. .num_msix_vectors) || !ice_vc_isvalid_vsi_id(vf, vsi_id)) {
  1429. aq_ret = ICE_ERR_PARAM;
  1430. goto error_param;
  1431. }
  1432. vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
  1433. if (!vsi) {
  1434. aq_ret = ICE_ERR_PARAM;
  1435. goto error_param;
  1436. }
  1437. /* lookout for the invalid queue index */
  1438. qmap = map->rxq_map;
  1439. for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
  1440. struct ice_q_vector *q_vector;
  1441. if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
  1442. aq_ret = ICE_ERR_PARAM;
  1443. goto error_param;
  1444. }
  1445. q_vector = vsi->q_vectors[i];
  1446. q_vector->num_ring_rx++;
  1447. q_vector->rx.itr_idx = map->rxitr_idx;
  1448. vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
  1449. }
  1450. qmap = map->txq_map;
  1451. for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
  1452. struct ice_q_vector *q_vector;
  1453. if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
  1454. aq_ret = ICE_ERR_PARAM;
  1455. goto error_param;
  1456. }
  1457. q_vector = vsi->q_vectors[i];
  1458. q_vector->num_ring_tx++;
  1459. q_vector->tx.itr_idx = map->txitr_idx;
  1460. vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
  1461. }
  1462. }
  1463. if (vsi)
  1464. ice_vsi_cfg_msix(vsi);
  1465. error_param:
  1466. /* send the response to the VF */
  1467. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, aq_ret,
  1468. NULL, 0);
  1469. }
  1470. /**
  1471. * ice_vc_cfg_qs_msg
  1472. * @vf: pointer to the VF info
  1473. * @msg: pointer to the msg buffer
  1474. *
  1475. * called from the VF to configure the Rx/Tx queues
  1476. */
  1477. static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
  1478. {
  1479. struct virtchnl_vsi_queue_config_info *qci =
  1480. (struct virtchnl_vsi_queue_config_info *)msg;
  1481. struct virtchnl_queue_pair_info *qpi;
  1482. enum ice_status aq_ret = 0;
  1483. struct ice_vsi *vsi;
  1484. int i;
  1485. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1486. aq_ret = ICE_ERR_PARAM;
  1487. goto error_param;
  1488. }
  1489. if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
  1490. aq_ret = ICE_ERR_PARAM;
  1491. goto error_param;
  1492. }
  1493. vsi = ice_find_vsi_from_id(vf->pf, qci->vsi_id);
  1494. if (!vsi) {
  1495. aq_ret = ICE_ERR_PARAM;
  1496. goto error_param;
  1497. }
  1498. for (i = 0; i < qci->num_queue_pairs; i++) {
  1499. qpi = &qci->qpair[i];
  1500. if (qpi->txq.vsi_id != qci->vsi_id ||
  1501. qpi->rxq.vsi_id != qci->vsi_id ||
  1502. qpi->rxq.queue_id != qpi->txq.queue_id ||
  1503. !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
  1504. aq_ret = ICE_ERR_PARAM;
  1505. goto error_param;
  1506. }
  1507. /* copy Tx queue info from VF into VSI */
  1508. vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
  1509. vsi->tx_rings[i]->count = qpi->txq.ring_len;
  1510. /* copy Rx queue info from VF into vsi */
  1511. vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
  1512. vsi->rx_rings[i]->count = qpi->rxq.ring_len;
  1513. if (qpi->rxq.databuffer_size > ((16 * 1024) - 128)) {
  1514. aq_ret = ICE_ERR_PARAM;
  1515. goto error_param;
  1516. }
  1517. vsi->rx_buf_len = qpi->rxq.databuffer_size;
  1518. if (qpi->rxq.max_pkt_size >= (16 * 1024) ||
  1519. qpi->rxq.max_pkt_size < 64) {
  1520. aq_ret = ICE_ERR_PARAM;
  1521. goto error_param;
  1522. }
  1523. vsi->max_frame = qpi->rxq.max_pkt_size;
  1524. }
  1525. /* VF can request to configure less than allocated queues
  1526. * or default allocated queues. So update the VSI with new number
  1527. */
  1528. vsi->num_txq = qci->num_queue_pairs;
  1529. vsi->num_rxq = qci->num_queue_pairs;
  1530. if (!ice_vsi_cfg_txqs(vsi) && !ice_vsi_cfg_rxqs(vsi))
  1531. aq_ret = 0;
  1532. else
  1533. aq_ret = ICE_ERR_PARAM;
  1534. error_param:
  1535. /* send the response to the VF */
  1536. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, aq_ret,
  1537. NULL, 0);
  1538. }
  1539. /**
  1540. * ice_is_vf_trusted
  1541. * @vf: pointer to the VF info
  1542. */
  1543. static bool ice_is_vf_trusted(struct ice_vf *vf)
  1544. {
  1545. return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  1546. }
  1547. /**
  1548. * ice_can_vf_change_mac
  1549. * @vf: pointer to the VF info
  1550. *
  1551. * Return true if the VF is allowed to change its MAC filters, false otherwise
  1552. */
  1553. static bool ice_can_vf_change_mac(struct ice_vf *vf)
  1554. {
  1555. /* If the VF MAC address has been set administratively (via the
  1556. * ndo_set_vf_mac command), then deny permission to the VF to
  1557. * add/delete unicast MAC addresses, unless the VF is trusted
  1558. */
  1559. if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
  1560. return false;
  1561. return true;
  1562. }
  1563. /**
  1564. * ice_vc_handle_mac_addr_msg
  1565. * @vf: pointer to the VF info
  1566. * @msg: pointer to the msg buffer
  1567. * @set: true if mac filters are being set, false otherwise
  1568. *
  1569. * add guest mac address filter
  1570. */
  1571. static int
  1572. ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
  1573. {
  1574. struct virtchnl_ether_addr_list *al =
  1575. (struct virtchnl_ether_addr_list *)msg;
  1576. struct ice_pf *pf = vf->pf;
  1577. enum virtchnl_ops vc_op;
  1578. enum ice_status ret;
  1579. LIST_HEAD(mac_list);
  1580. struct ice_vsi *vsi;
  1581. int mac_count = 0;
  1582. int i;
  1583. if (set)
  1584. vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
  1585. else
  1586. vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
  1587. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
  1588. !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
  1589. ret = ICE_ERR_PARAM;
  1590. goto handle_mac_exit;
  1591. }
  1592. if (set && !ice_is_vf_trusted(vf) &&
  1593. (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
  1594. dev_err(&pf->pdev->dev,
  1595. "Can't add more MAC addresses, because VF is not trusted, switch the VF to trusted mode in order to add more functionalities\n");
  1596. ret = ICE_ERR_PARAM;
  1597. goto handle_mac_exit;
  1598. }
  1599. vsi = pf->vsi[vf->lan_vsi_idx];
  1600. for (i = 0; i < al->num_elements; i++) {
  1601. u8 *maddr = al->list[i].addr;
  1602. if (ether_addr_equal(maddr, vf->dflt_lan_addr.addr) ||
  1603. is_broadcast_ether_addr(maddr)) {
  1604. if (set) {
  1605. /* VF is trying to add filters that the PF
  1606. * already added. Just continue.
  1607. */
  1608. dev_info(&pf->pdev->dev,
  1609. "mac %pM already set for VF %d\n",
  1610. maddr, vf->vf_id);
  1611. continue;
  1612. } else {
  1613. /* VF can't remove dflt_lan_addr/bcast mac */
  1614. dev_err(&pf->pdev->dev,
  1615. "can't remove mac %pM for VF %d\n",
  1616. maddr, vf->vf_id);
  1617. ret = ICE_ERR_PARAM;
  1618. goto handle_mac_exit;
  1619. }
  1620. }
  1621. /* check for the invalid cases and bail if necessary */
  1622. if (is_zero_ether_addr(maddr)) {
  1623. dev_err(&pf->pdev->dev,
  1624. "invalid mac %pM provided for VF %d\n",
  1625. maddr, vf->vf_id);
  1626. ret = ICE_ERR_PARAM;
  1627. goto handle_mac_exit;
  1628. }
  1629. if (is_unicast_ether_addr(maddr) &&
  1630. !ice_can_vf_change_mac(vf)) {
  1631. dev_err(&pf->pdev->dev,
  1632. "can't change unicast mac for untrusted VF %d\n",
  1633. vf->vf_id);
  1634. ret = ICE_ERR_PARAM;
  1635. goto handle_mac_exit;
  1636. }
  1637. /* get here if maddr is multicast or if VF can change mac */
  1638. if (ice_add_mac_to_list(vsi, &mac_list, al->list[i].addr)) {
  1639. ret = ICE_ERR_NO_MEMORY;
  1640. goto handle_mac_exit;
  1641. }
  1642. mac_count++;
  1643. }
  1644. /* program the updated filter list */
  1645. if (set)
  1646. ret = ice_add_mac(&pf->hw, &mac_list);
  1647. else
  1648. ret = ice_remove_mac(&pf->hw, &mac_list);
  1649. if (ret) {
  1650. dev_err(&pf->pdev->dev,
  1651. "can't update mac filters for VF %d, error %d\n",
  1652. vf->vf_id, ret);
  1653. } else {
  1654. if (set)
  1655. vf->num_mac += mac_count;
  1656. else
  1657. vf->num_mac -= mac_count;
  1658. }
  1659. handle_mac_exit:
  1660. ice_free_fltr_list(&pf->pdev->dev, &mac_list);
  1661. /* send the response to the VF */
  1662. return ice_vc_send_msg_to_vf(vf, vc_op, ret, NULL, 0);
  1663. }
  1664. /**
  1665. * ice_vc_add_mac_addr_msg
  1666. * @vf: pointer to the VF info
  1667. * @msg: pointer to the msg buffer
  1668. *
  1669. * add guest MAC address filter
  1670. */
  1671. static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
  1672. {
  1673. return ice_vc_handle_mac_addr_msg(vf, msg, true);
  1674. }
  1675. /**
  1676. * ice_vc_del_mac_addr_msg
  1677. * @vf: pointer to the VF info
  1678. * @msg: pointer to the msg buffer
  1679. *
  1680. * remove guest MAC address filter
  1681. */
  1682. static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
  1683. {
  1684. return ice_vc_handle_mac_addr_msg(vf, msg, false);
  1685. }
  1686. /**
  1687. * ice_vc_request_qs_msg
  1688. * @vf: pointer to the VF info
  1689. * @msg: pointer to the msg buffer
  1690. *
  1691. * VFs get a default number of queues but can use this message to request a
  1692. * different number. If the request is successful, PF will reset the VF and
  1693. * return 0. If unsuccessful, PF will send message informing VF of number of
  1694. * available queue pairs via virtchnl message response to VF.
  1695. */
  1696. static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
  1697. {
  1698. struct virtchnl_vf_res_request *vfres =
  1699. (struct virtchnl_vf_res_request *)msg;
  1700. int req_queues = vfres->num_queue_pairs;
  1701. enum ice_status aq_ret = 0;
  1702. struct ice_pf *pf = vf->pf;
  1703. int tx_rx_queue_left;
  1704. int cur_queues;
  1705. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1706. aq_ret = ICE_ERR_PARAM;
  1707. goto error_param;
  1708. }
  1709. cur_queues = pf->num_vf_qps;
  1710. tx_rx_queue_left = min_t(int, pf->q_left_tx, pf->q_left_rx);
  1711. if (req_queues <= 0) {
  1712. dev_err(&pf->pdev->dev,
  1713. "VF %d tried to request %d queues. Ignoring.\n",
  1714. vf->vf_id, req_queues);
  1715. } else if (req_queues > ICE_MAX_QS_PER_VF) {
  1716. dev_err(&pf->pdev->dev,
  1717. "VF %d tried to request more than %d queues.\n",
  1718. vf->vf_id, ICE_MAX_QS_PER_VF);
  1719. vfres->num_queue_pairs = ICE_MAX_QS_PER_VF;
  1720. } else if (req_queues - cur_queues > tx_rx_queue_left) {
  1721. dev_warn(&pf->pdev->dev,
  1722. "VF %d requested %d more queues, but only %d left.\n",
  1723. vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
  1724. vfres->num_queue_pairs = tx_rx_queue_left + cur_queues;
  1725. } else {
  1726. /* request is successful, then reset VF */
  1727. vf->num_req_qs = req_queues;
  1728. ice_vc_dis_vf(vf);
  1729. dev_info(&pf->pdev->dev,
  1730. "VF %d granted request of %d queues.\n",
  1731. vf->vf_id, req_queues);
  1732. return 0;
  1733. }
  1734. error_param:
  1735. /* send the response to the VF */
  1736. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
  1737. aq_ret, (u8 *)vfres, sizeof(*vfres));
  1738. }
  1739. /**
  1740. * ice_set_vf_port_vlan
  1741. * @netdev: network interface device structure
  1742. * @vf_id: VF identifier
  1743. * @vlan_id: VLAN id being set
  1744. * @qos: priority setting
  1745. * @vlan_proto: VLAN protocol
  1746. *
  1747. * program VF Port VLAN id and/or qos
  1748. */
  1749. int
  1750. ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
  1751. __be16 vlan_proto)
  1752. {
  1753. u16 vlanprio = vlan_id | (qos << ICE_VLAN_PRIORITY_S);
  1754. struct ice_netdev_priv *np = netdev_priv(netdev);
  1755. struct ice_pf *pf = np->vsi->back;
  1756. struct ice_vsi *vsi;
  1757. struct ice_vf *vf;
  1758. int ret = 0;
  1759. /* validate the request */
  1760. if (vf_id >= pf->num_alloc_vfs) {
  1761. dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
  1762. return -EINVAL;
  1763. }
  1764. if (vlan_id > ICE_MAX_VLANID || qos > 7) {
  1765. dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
  1766. return -EINVAL;
  1767. }
  1768. if (vlan_proto != htons(ETH_P_8021Q)) {
  1769. dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
  1770. return -EPROTONOSUPPORT;
  1771. }
  1772. vf = &pf->vf[vf_id];
  1773. vsi = pf->vsi[vf->lan_vsi_idx];
  1774. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  1775. dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
  1776. return -EBUSY;
  1777. }
  1778. if (le16_to_cpu(vsi->info.pvid) == vlanprio) {
  1779. /* duplicate request, so just return success */
  1780. dev_info(&pf->pdev->dev,
  1781. "Duplicate pvid %d request\n", vlanprio);
  1782. return ret;
  1783. }
  1784. /* If pvid, then remove all filters on the old VLAN */
  1785. if (vsi->info.pvid)
  1786. ice_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
  1787. VLAN_VID_MASK));
  1788. if (vlan_id || qos) {
  1789. ret = ice_vsi_set_pvid(vsi, vlanprio);
  1790. if (ret)
  1791. goto error_set_pvid;
  1792. } else {
  1793. ice_vsi_kill_pvid(vsi);
  1794. }
  1795. if (vlan_id) {
  1796. dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
  1797. vlan_id, qos, vf_id);
  1798. /* add new VLAN filter for each MAC */
  1799. ret = ice_vsi_add_vlan(vsi, vlan_id);
  1800. if (ret)
  1801. goto error_set_pvid;
  1802. }
  1803. /* The Port VLAN needs to be saved across resets the same as the
  1804. * default LAN MAC address.
  1805. */
  1806. vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
  1807. error_set_pvid:
  1808. return ret;
  1809. }
  1810. /**
  1811. * ice_vc_process_vlan_msg
  1812. * @vf: pointer to the VF info
  1813. * @msg: pointer to the msg buffer
  1814. * @add_v: Add VLAN if true, otherwise delete VLAN
  1815. *
  1816. * Process virtchnl op to add or remove programmed guest VLAN id
  1817. */
  1818. static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
  1819. {
  1820. struct virtchnl_vlan_filter_list *vfl =
  1821. (struct virtchnl_vlan_filter_list *)msg;
  1822. enum ice_status aq_ret = 0;
  1823. struct ice_pf *pf = vf->pf;
  1824. struct ice_vsi *vsi;
  1825. int i;
  1826. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1827. aq_ret = ICE_ERR_PARAM;
  1828. goto error_param;
  1829. }
  1830. if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
  1831. aq_ret = ICE_ERR_PARAM;
  1832. goto error_param;
  1833. }
  1834. if (add_v && !ice_is_vf_trusted(vf) &&
  1835. vf->num_vlan >= ICE_MAX_VLAN_PER_VF) {
  1836. dev_info(&pf->pdev->dev,
  1837. "VF is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n");
  1838. aq_ret = ICE_ERR_PARAM;
  1839. goto error_param;
  1840. }
  1841. for (i = 0; i < vfl->num_elements; i++) {
  1842. if (vfl->vlan_id[i] > ICE_MAX_VLANID) {
  1843. aq_ret = ICE_ERR_PARAM;
  1844. dev_err(&pf->pdev->dev,
  1845. "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
  1846. goto error_param;
  1847. }
  1848. }
  1849. vsi = ice_find_vsi_from_id(vf->pf, vfl->vsi_id);
  1850. if (!vsi) {
  1851. aq_ret = ICE_ERR_PARAM;
  1852. goto error_param;
  1853. }
  1854. if (vsi->info.pvid) {
  1855. aq_ret = ICE_ERR_PARAM;
  1856. goto error_param;
  1857. }
  1858. if (ice_vsi_manage_vlan_stripping(vsi, add_v)) {
  1859. dev_err(&pf->pdev->dev,
  1860. "%sable VLAN stripping failed for VSI %i\n",
  1861. add_v ? "en" : "dis", vsi->vsi_num);
  1862. aq_ret = ICE_ERR_PARAM;
  1863. goto error_param;
  1864. }
  1865. if (add_v) {
  1866. for (i = 0; i < vfl->num_elements; i++) {
  1867. u16 vid = vfl->vlan_id[i];
  1868. if (!ice_vsi_add_vlan(vsi, vid)) {
  1869. vf->num_vlan++;
  1870. set_bit(vid, vsi->active_vlans);
  1871. /* Enable VLAN pruning when VLAN 0 is added */
  1872. if (unlikely(!vid))
  1873. if (ice_cfg_vlan_pruning(vsi, true))
  1874. aq_ret = ICE_ERR_PARAM;
  1875. } else {
  1876. aq_ret = ICE_ERR_PARAM;
  1877. }
  1878. }
  1879. } else {
  1880. for (i = 0; i < vfl->num_elements; i++) {
  1881. u16 vid = vfl->vlan_id[i];
  1882. /* Make sure ice_vsi_kill_vlan is successful before
  1883. * updating VLAN information
  1884. */
  1885. if (!ice_vsi_kill_vlan(vsi, vid)) {
  1886. vf->num_vlan--;
  1887. clear_bit(vid, vsi->active_vlans);
  1888. /* Disable VLAN pruning when removing VLAN 0 */
  1889. if (unlikely(!vid))
  1890. ice_cfg_vlan_pruning(vsi, false);
  1891. }
  1892. }
  1893. }
  1894. error_param:
  1895. /* send the response to the VF */
  1896. if (add_v)
  1897. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret,
  1898. NULL, 0);
  1899. else
  1900. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret,
  1901. NULL, 0);
  1902. }
  1903. /**
  1904. * ice_vc_add_vlan_msg
  1905. * @vf: pointer to the VF info
  1906. * @msg: pointer to the msg buffer
  1907. *
  1908. * Add and program guest VLAN id
  1909. */
  1910. static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
  1911. {
  1912. return ice_vc_process_vlan_msg(vf, msg, true);
  1913. }
  1914. /**
  1915. * ice_vc_remove_vlan_msg
  1916. * @vf: pointer to the VF info
  1917. * @msg: pointer to the msg buffer
  1918. *
  1919. * remove programmed guest VLAN id
  1920. */
  1921. static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
  1922. {
  1923. return ice_vc_process_vlan_msg(vf, msg, false);
  1924. }
  1925. /**
  1926. * ice_vc_ena_vlan_stripping
  1927. * @vf: pointer to the VF info
  1928. *
  1929. * Enable VLAN header stripping for a given VF
  1930. */
  1931. static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
  1932. {
  1933. enum ice_status aq_ret = 0;
  1934. struct ice_pf *pf = vf->pf;
  1935. struct ice_vsi *vsi;
  1936. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1937. aq_ret = ICE_ERR_PARAM;
  1938. goto error_param;
  1939. }
  1940. vsi = pf->vsi[vf->lan_vsi_idx];
  1941. if (ice_vsi_manage_vlan_stripping(vsi, true))
  1942. aq_ret = ICE_ERR_AQ_ERROR;
  1943. error_param:
  1944. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
  1945. aq_ret, NULL, 0);
  1946. }
  1947. /**
  1948. * ice_vc_dis_vlan_stripping
  1949. * @vf: pointer to the VF info
  1950. *
  1951. * Disable VLAN header stripping for a given VF
  1952. */
  1953. static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
  1954. {
  1955. enum ice_status aq_ret = 0;
  1956. struct ice_pf *pf = vf->pf;
  1957. struct ice_vsi *vsi;
  1958. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1959. aq_ret = ICE_ERR_PARAM;
  1960. goto error_param;
  1961. }
  1962. vsi = pf->vsi[vf->lan_vsi_idx];
  1963. if (ice_vsi_manage_vlan_stripping(vsi, false))
  1964. aq_ret = ICE_ERR_AQ_ERROR;
  1965. error_param:
  1966. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
  1967. aq_ret, NULL, 0);
  1968. }
  1969. /**
  1970. * ice_vc_process_vf_msg - Process request from VF
  1971. * @pf: pointer to the PF structure
  1972. * @event: pointer to the AQ event
  1973. *
  1974. * called from the common asq/arq handler to
  1975. * process request from VF
  1976. */
  1977. void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
  1978. {
  1979. u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
  1980. s16 vf_id = le16_to_cpu(event->desc.retval);
  1981. u16 msglen = event->msg_len;
  1982. u8 *msg = event->msg_buf;
  1983. struct ice_vf *vf = NULL;
  1984. int err = 0;
  1985. if (vf_id >= pf->num_alloc_vfs) {
  1986. err = -EINVAL;
  1987. goto error_handler;
  1988. }
  1989. vf = &pf->vf[vf_id];
  1990. /* Check if VF is disabled. */
  1991. if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
  1992. err = -EPERM;
  1993. goto error_handler;
  1994. }
  1995. /* Perform basic checks on the msg */
  1996. err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
  1997. if (err) {
  1998. if (err == VIRTCHNL_ERR_PARAM)
  1999. err = -EPERM;
  2000. else
  2001. err = -EINVAL;
  2002. goto error_handler;
  2003. }
  2004. /* Perform additional checks specific to RSS and Virtchnl */
  2005. if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
  2006. struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
  2007. if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE)
  2008. err = -EINVAL;
  2009. } else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
  2010. struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
  2011. if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE)
  2012. err = -EINVAL;
  2013. }
  2014. error_handler:
  2015. if (err) {
  2016. ice_vc_send_msg_to_vf(vf, v_opcode, ICE_ERR_PARAM, NULL, 0);
  2017. dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
  2018. vf_id, v_opcode, msglen, err);
  2019. return;
  2020. }
  2021. switch (v_opcode) {
  2022. case VIRTCHNL_OP_VERSION:
  2023. err = ice_vc_get_ver_msg(vf, msg);
  2024. break;
  2025. case VIRTCHNL_OP_GET_VF_RESOURCES:
  2026. err = ice_vc_get_vf_res_msg(vf, msg);
  2027. break;
  2028. case VIRTCHNL_OP_RESET_VF:
  2029. ice_vc_reset_vf_msg(vf);
  2030. break;
  2031. case VIRTCHNL_OP_ADD_ETH_ADDR:
  2032. err = ice_vc_add_mac_addr_msg(vf, msg);
  2033. break;
  2034. case VIRTCHNL_OP_DEL_ETH_ADDR:
  2035. err = ice_vc_del_mac_addr_msg(vf, msg);
  2036. break;
  2037. case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  2038. err = ice_vc_cfg_qs_msg(vf, msg);
  2039. break;
  2040. case VIRTCHNL_OP_ENABLE_QUEUES:
  2041. err = ice_vc_ena_qs_msg(vf, msg);
  2042. ice_vc_notify_vf_link_state(vf);
  2043. break;
  2044. case VIRTCHNL_OP_DISABLE_QUEUES:
  2045. err = ice_vc_dis_qs_msg(vf, msg);
  2046. break;
  2047. case VIRTCHNL_OP_REQUEST_QUEUES:
  2048. err = ice_vc_request_qs_msg(vf, msg);
  2049. break;
  2050. case VIRTCHNL_OP_CONFIG_IRQ_MAP:
  2051. err = ice_vc_cfg_irq_map_msg(vf, msg);
  2052. break;
  2053. case VIRTCHNL_OP_CONFIG_RSS_KEY:
  2054. err = ice_vc_config_rss_key(vf, msg);
  2055. break;
  2056. case VIRTCHNL_OP_CONFIG_RSS_LUT:
  2057. err = ice_vc_config_rss_lut(vf, msg);
  2058. break;
  2059. case VIRTCHNL_OP_GET_STATS:
  2060. err = ice_vc_get_stats_msg(vf, msg);
  2061. break;
  2062. case VIRTCHNL_OP_ADD_VLAN:
  2063. err = ice_vc_add_vlan_msg(vf, msg);
  2064. break;
  2065. case VIRTCHNL_OP_DEL_VLAN:
  2066. err = ice_vc_remove_vlan_msg(vf, msg);
  2067. break;
  2068. case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
  2069. err = ice_vc_ena_vlan_stripping(vf);
  2070. break;
  2071. case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
  2072. err = ice_vc_dis_vlan_stripping(vf);
  2073. break;
  2074. case VIRTCHNL_OP_UNKNOWN:
  2075. default:
  2076. dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
  2077. v_opcode, vf_id);
  2078. err = ice_vc_send_msg_to_vf(vf, v_opcode, ICE_ERR_NOT_IMPL,
  2079. NULL, 0);
  2080. break;
  2081. }
  2082. if (err) {
  2083. /* Helper function cares less about error return values here
  2084. * as it is busy with pending work.
  2085. */
  2086. dev_info(&pf->pdev->dev,
  2087. "PF failed to honor VF %d, opcode %d\n, error %d\n",
  2088. vf_id, v_opcode, err);
  2089. }
  2090. }
  2091. /**
  2092. * ice_get_vf_cfg
  2093. * @netdev: network interface device structure
  2094. * @vf_id: VF identifier
  2095. * @ivi: VF configuration structure
  2096. *
  2097. * return VF configuration
  2098. */
  2099. int ice_get_vf_cfg(struct net_device *netdev, int vf_id,
  2100. struct ifla_vf_info *ivi)
  2101. {
  2102. struct ice_netdev_priv *np = netdev_priv(netdev);
  2103. struct ice_vsi *vsi = np->vsi;
  2104. struct ice_pf *pf = vsi->back;
  2105. struct ice_vf *vf;
  2106. /* validate the request */
  2107. if (vf_id >= pf->num_alloc_vfs) {
  2108. netdev_err(netdev, "invalid VF id: %d\n", vf_id);
  2109. return -EINVAL;
  2110. }
  2111. vf = &pf->vf[vf_id];
  2112. vsi = pf->vsi[vf->lan_vsi_idx];
  2113. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2114. netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
  2115. return -EBUSY;
  2116. }
  2117. ivi->vf = vf_id;
  2118. ether_addr_copy(ivi->mac, vf->dflt_lan_addr.addr);
  2119. /* VF configuration for VLAN and applicable QoS */
  2120. ivi->vlan = le16_to_cpu(vsi->info.pvid) & ICE_VLAN_M;
  2121. ivi->qos = (le16_to_cpu(vsi->info.pvid) & ICE_PRIORITY_M) >>
  2122. ICE_VLAN_PRIORITY_S;
  2123. ivi->trusted = vf->trusted;
  2124. ivi->spoofchk = vf->spoofchk;
  2125. if (!vf->link_forced)
  2126. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  2127. else if (vf->link_up)
  2128. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  2129. else
  2130. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  2131. ivi->max_tx_rate = vf->tx_rate;
  2132. ivi->min_tx_rate = 0;
  2133. return 0;
  2134. }
  2135. /**
  2136. * ice_set_vf_spoofchk
  2137. * @netdev: network interface device structure
  2138. * @vf_id: VF identifier
  2139. * @ena: flag to enable or disable feature
  2140. *
  2141. * Enable or disable VF spoof checking
  2142. */
  2143. int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
  2144. {
  2145. struct ice_netdev_priv *np = netdev_priv(netdev);
  2146. struct ice_vsi_ctx ctx = { 0 };
  2147. struct ice_vsi *vsi = np->vsi;
  2148. struct ice_pf *pf = vsi->back;
  2149. struct ice_vf *vf;
  2150. int status;
  2151. /* validate the request */
  2152. if (vf_id >= pf->num_alloc_vfs) {
  2153. netdev_err(netdev, "invalid VF id: %d\n", vf_id);
  2154. return -EINVAL;
  2155. }
  2156. vf = &pf->vf[vf_id];
  2157. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2158. netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
  2159. return -EBUSY;
  2160. }
  2161. if (ena == vf->spoofchk) {
  2162. dev_dbg(&pf->pdev->dev, "VF spoofchk already %s\n",
  2163. ena ? "ON" : "OFF");
  2164. return 0;
  2165. }
  2166. ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
  2167. if (ena) {
  2168. ctx.info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
  2169. ctx.info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_PRUNE_EN_M;
  2170. }
  2171. status = ice_update_vsi(&pf->hw, vsi->idx, &ctx, NULL);
  2172. if (status) {
  2173. dev_dbg(&pf->pdev->dev,
  2174. "Error %d, failed to update VSI* parameters\n", status);
  2175. return -EIO;
  2176. }
  2177. vf->spoofchk = ena;
  2178. vsi->info.sec_flags = ctx.info.sec_flags;
  2179. vsi->info.sw_flags2 = ctx.info.sw_flags2;
  2180. return status;
  2181. }
  2182. /**
  2183. * ice_set_vf_mac
  2184. * @netdev: network interface device structure
  2185. * @vf_id: VF identifier
  2186. * @mac: mac address
  2187. *
  2188. * program VF mac address
  2189. */
  2190. int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
  2191. {
  2192. struct ice_netdev_priv *np = netdev_priv(netdev);
  2193. struct ice_vsi *vsi = np->vsi;
  2194. struct ice_pf *pf = vsi->back;
  2195. struct ice_vf *vf;
  2196. int ret = 0;
  2197. /* validate the request */
  2198. if (vf_id >= pf->num_alloc_vfs) {
  2199. netdev_err(netdev, "invalid VF id: %d\n", vf_id);
  2200. return -EINVAL;
  2201. }
  2202. vf = &pf->vf[vf_id];
  2203. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2204. netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
  2205. return -EBUSY;
  2206. }
  2207. if (is_zero_ether_addr(mac) || is_multicast_ether_addr(mac)) {
  2208. netdev_err(netdev, "%pM not a valid unicast address\n", mac);
  2209. return -EINVAL;
  2210. }
  2211. /* copy mac into dflt_lan_addr and trigger a VF reset. The reset
  2212. * flow will use the updated dflt_lan_addr and add a MAC filter
  2213. * using ice_add_mac. Also set pf_set_mac to indicate that the PF has
  2214. * set the MAC address for this VF.
  2215. */
  2216. ether_addr_copy(vf->dflt_lan_addr.addr, mac);
  2217. vf->pf_set_mac = true;
  2218. netdev_info(netdev,
  2219. "mac on VF %d set to %pM\n. VF driver will be reinitialized\n",
  2220. vf_id, mac);
  2221. ice_vc_dis_vf(vf);
  2222. return ret;
  2223. }
  2224. /**
  2225. * ice_set_vf_trust
  2226. * @netdev: network interface device structure
  2227. * @vf_id: VF identifier
  2228. * @trusted: Boolean value to enable/disable trusted VF
  2229. *
  2230. * Enable or disable a given VF as trusted
  2231. */
  2232. int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
  2233. {
  2234. struct ice_netdev_priv *np = netdev_priv(netdev);
  2235. struct ice_vsi *vsi = np->vsi;
  2236. struct ice_pf *pf = vsi->back;
  2237. struct ice_vf *vf;
  2238. /* validate the request */
  2239. if (vf_id >= pf->num_alloc_vfs) {
  2240. dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
  2241. return -EINVAL;
  2242. }
  2243. vf = &pf->vf[vf_id];
  2244. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2245. dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
  2246. return -EBUSY;
  2247. }
  2248. /* Check if already trusted */
  2249. if (trusted == vf->trusted)
  2250. return 0;
  2251. vf->trusted = trusted;
  2252. ice_vc_dis_vf(vf);
  2253. dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
  2254. vf_id, trusted ? "" : "un");
  2255. return 0;
  2256. }
  2257. /**
  2258. * ice_set_vf_link_state
  2259. * @netdev: network interface device structure
  2260. * @vf_id: VF identifier
  2261. * @link_state: required link state
  2262. *
  2263. * Set VF's link state, irrespective of physical link state status
  2264. */
  2265. int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
  2266. {
  2267. struct ice_netdev_priv *np = netdev_priv(netdev);
  2268. struct ice_pf *pf = np->vsi->back;
  2269. struct virtchnl_pf_event pfe = { 0 };
  2270. struct ice_link_status *ls;
  2271. struct ice_vf *vf;
  2272. struct ice_hw *hw;
  2273. if (vf_id >= pf->num_alloc_vfs) {
  2274. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2275. return -EINVAL;
  2276. }
  2277. vf = &pf->vf[vf_id];
  2278. hw = &pf->hw;
  2279. ls = &pf->hw.port_info->phy.link_info;
  2280. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2281. dev_err(&pf->pdev->dev, "vf %d in reset. Try again.\n", vf_id);
  2282. return -EBUSY;
  2283. }
  2284. pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
  2285. pfe.severity = PF_EVENT_SEVERITY_INFO;
  2286. switch (link_state) {
  2287. case IFLA_VF_LINK_STATE_AUTO:
  2288. vf->link_forced = false;
  2289. vf->link_up = ls->link_info & ICE_AQ_LINK_UP;
  2290. break;
  2291. case IFLA_VF_LINK_STATE_ENABLE:
  2292. vf->link_forced = true;
  2293. vf->link_up = true;
  2294. break;
  2295. case IFLA_VF_LINK_STATE_DISABLE:
  2296. vf->link_forced = true;
  2297. vf->link_up = false;
  2298. break;
  2299. default:
  2300. return -EINVAL;
  2301. }
  2302. if (vf->link_forced)
  2303. ice_set_pfe_link_forced(vf, &pfe, vf->link_up);
  2304. else
  2305. ice_set_pfe_link(vf, &pfe, ls->link_speed, vf->link_up);
  2306. /* Notify the VF of its new link state */
  2307. ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT, 0, (u8 *)&pfe,
  2308. sizeof(pfe), NULL);
  2309. return 0;
  2310. }