ice_virtchnl_pf.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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_get_vf_vector - get VF interrupt vector register offset
  7. * @vf_msix: number of MSIx vector per VF on a PF
  8. * @vf_id: VF identifier
  9. * @i: index of MSIx vector
  10. */
  11. static u32 ice_get_vf_vector(int vf_msix, int vf_id, int i)
  12. {
  13. return ((i == 0) ? VFINT_DYN_CTLN(vf_id) :
  14. VFINT_DYN_CTLN(((vf_msix - 1) * (vf_id)) + (i - 1)));
  15. }
  16. /**
  17. * ice_free_vf_res - Free a VF's resources
  18. * @vf: pointer to the VF info
  19. */
  20. static void ice_free_vf_res(struct ice_vf *vf)
  21. {
  22. struct ice_pf *pf = vf->pf;
  23. int i, pf_vf_msix;
  24. /* First, disable VF's configuration API to prevent OS from
  25. * accessing the VF's VSI after it's freed or invalidated.
  26. */
  27. clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
  28. /* free vsi & disconnect it from the parent uplink */
  29. if (vf->lan_vsi_idx) {
  30. ice_vsi_release(pf->vsi[vf->lan_vsi_idx]);
  31. vf->lan_vsi_idx = 0;
  32. vf->lan_vsi_num = 0;
  33. vf->num_mac = 0;
  34. }
  35. pf_vf_msix = pf->num_vf_msix;
  36. /* Disable interrupts so that VF starts in a known state */
  37. for (i = 0; i < pf_vf_msix; i++) {
  38. u32 reg_idx;
  39. reg_idx = ice_get_vf_vector(pf_vf_msix, vf->vf_id, i);
  40. wr32(&pf->hw, reg_idx, VFINT_DYN_CTLN_CLEARPBA_M);
  41. ice_flush(&pf->hw);
  42. }
  43. /* reset some of the state variables keeping track of the resources */
  44. clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
  45. clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
  46. }
  47. /***********************enable_vf routines*****************************/
  48. /**
  49. * ice_dis_vf_mappings
  50. * @vf: pointer to the VF structure
  51. */
  52. static void ice_dis_vf_mappings(struct ice_vf *vf)
  53. {
  54. struct ice_pf *pf = vf->pf;
  55. struct ice_vsi *vsi;
  56. int first, last, v;
  57. struct ice_hw *hw;
  58. hw = &pf->hw;
  59. vsi = pf->vsi[vf->lan_vsi_idx];
  60. wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
  61. first = vf->first_vector_idx;
  62. last = first + pf->num_vf_msix - 1;
  63. for (v = first; v <= last; v++) {
  64. u32 reg;
  65. reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
  66. GLINT_VECT2FUNC_IS_PF_M) |
  67. ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
  68. GLINT_VECT2FUNC_PF_NUM_M));
  69. wr32(hw, GLINT_VECT2FUNC(v), reg);
  70. }
  71. if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
  72. wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
  73. else
  74. dev_err(&pf->pdev->dev,
  75. "Scattered mode for VF Tx queues is not yet implemented\n");
  76. if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
  77. wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
  78. else
  79. dev_err(&pf->pdev->dev,
  80. "Scattered mode for VF Rx queues is not yet implemented\n");
  81. }
  82. /**
  83. * ice_free_vfs - Free all VFs
  84. * @pf: pointer to the PF structure
  85. */
  86. void ice_free_vfs(struct ice_pf *pf)
  87. {
  88. struct ice_hw *hw = &pf->hw;
  89. int tmp, i;
  90. if (!pf->vf)
  91. return;
  92. while (test_and_set_bit(__ICE_VF_DIS, pf->state))
  93. usleep_range(1000, 2000);
  94. /* Avoid wait time by stopping all VFs at the same time */
  95. for (i = 0; i < pf->num_alloc_vfs; i++) {
  96. if (!test_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states))
  97. continue;
  98. /* stop rings without wait time */
  99. ice_vsi_stop_tx_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
  100. ICE_NO_RESET, i);
  101. ice_vsi_stop_rx_rings(pf->vsi[pf->vf[i].lan_vsi_idx]);
  102. clear_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states);
  103. }
  104. /* Disable IOV before freeing resources. This lets any VF drivers
  105. * running in the host get themselves cleaned up before we yank
  106. * the carpet out from underneath their feet.
  107. */
  108. if (!pci_vfs_assigned(pf->pdev))
  109. pci_disable_sriov(pf->pdev);
  110. else
  111. dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
  112. tmp = pf->num_alloc_vfs;
  113. pf->num_vf_qps = 0;
  114. pf->num_alloc_vfs = 0;
  115. for (i = 0; i < tmp; i++) {
  116. if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
  117. /* disable VF qp mappings */
  118. ice_dis_vf_mappings(&pf->vf[i]);
  119. /* Set this state so that assigned VF vectors can be
  120. * reclaimed by PF for reuse in ice_vsi_release(). No
  121. * need to clear this bit since pf->vf array is being
  122. * freed anyways after this for loop
  123. */
  124. set_bit(ICE_VF_STATE_CFG_INTR, pf->vf[i].vf_states);
  125. ice_free_vf_res(&pf->vf[i]);
  126. }
  127. }
  128. devm_kfree(&pf->pdev->dev, pf->vf);
  129. pf->vf = NULL;
  130. /* This check is for when the driver is unloaded while VFs are
  131. * assigned. Setting the number of VFs to 0 through sysfs is caught
  132. * before this function ever gets called.
  133. */
  134. if (!pci_vfs_assigned(pf->pdev)) {
  135. int vf_id;
  136. /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
  137. * work correctly when SR-IOV gets re-enabled.
  138. */
  139. for (vf_id = 0; vf_id < tmp; vf_id++) {
  140. u32 reg_idx, bit_idx;
  141. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  142. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  143. wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  144. }
  145. }
  146. clear_bit(__ICE_VF_DIS, pf->state);
  147. clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
  148. }
  149. /**
  150. * ice_trigger_vf_reset - Reset a VF on HW
  151. * @vf: pointer to the VF structure
  152. * @is_vflr: true if VFLR was issued, false if not
  153. *
  154. * Trigger hardware to start a reset for a particular VF. Expects the caller
  155. * to wait the proper amount of time to allow hardware to reset the VF before
  156. * it cleans up and restores VF functionality.
  157. */
  158. static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr)
  159. {
  160. struct ice_pf *pf = vf->pf;
  161. u32 reg, reg_idx, bit_idx;
  162. struct ice_hw *hw;
  163. int vf_abs_id, i;
  164. hw = &pf->hw;
  165. vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
  166. /* Inform VF that it is no longer active, as a warning */
  167. clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
  168. /* Disable VF's configuration API during reset. The flag is re-enabled
  169. * in ice_alloc_vf_res(), when it's safe again to access VF's VSI.
  170. * It's normally disabled in ice_free_vf_res(), but it's safer
  171. * to do it earlier to give some time to finish to any VF config
  172. * functions that may still be running at this point.
  173. */
  174. clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
  175. /* In the case of a VFLR, the HW has already reset the VF and we
  176. * just need to clean up, so don't hit the VFRTRIG register.
  177. */
  178. if (!is_vflr) {
  179. /* reset VF using VPGEN_VFRTRIG reg */
  180. reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
  181. reg |= VPGEN_VFRTRIG_VFSWR_M;
  182. wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
  183. }
  184. /* clear the VFLR bit in GLGEN_VFLRSTAT */
  185. reg_idx = (vf_abs_id) / 32;
  186. bit_idx = (vf_abs_id) % 32;
  187. wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  188. ice_flush(hw);
  189. wr32(hw, PF_PCI_CIAA,
  190. VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
  191. for (i = 0; i < 100; i++) {
  192. reg = rd32(hw, PF_PCI_CIAD);
  193. if ((reg & VF_TRANS_PENDING_M) != 0)
  194. dev_err(&pf->pdev->dev,
  195. "VF %d PCI transactions stuck\n", vf->vf_id);
  196. udelay(1);
  197. }
  198. }
  199. /**
  200. * ice_vsi_set_pvid - Set port VLAN id for the VSI
  201. * @vsi: the VSI being changed
  202. * @vid: the VLAN id to set as a PVID
  203. */
  204. static int ice_vsi_set_pvid(struct ice_vsi *vsi, u16 vid)
  205. {
  206. struct device *dev = &vsi->back->pdev->dev;
  207. struct ice_hw *hw = &vsi->back->hw;
  208. struct ice_vsi_ctx ctxt = { 0 };
  209. enum ice_status status;
  210. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_TAGGED |
  211. ICE_AQ_VSI_PVLAN_INSERT_PVID |
  212. ICE_AQ_VSI_VLAN_EMOD_STR;
  213. ctxt.info.pvid = cpu_to_le16(vid);
  214. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  215. status = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
  216. if (status) {
  217. dev_info(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
  218. status, hw->adminq.sq_last_status);
  219. return -EIO;
  220. }
  221. vsi->info.pvid = ctxt.info.pvid;
  222. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  223. return 0;
  224. }
  225. /**
  226. * ice_vf_vsi_setup - Set up a VF VSI
  227. * @pf: board private structure
  228. * @pi: pointer to the port_info instance
  229. * @vf_id: defines VF id to which this VSI connects.
  230. *
  231. * Returns pointer to the successfully allocated VSI struct on success,
  232. * otherwise returns NULL on failure.
  233. */
  234. static struct ice_vsi *
  235. ice_vf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi, u16 vf_id)
  236. {
  237. return ice_vsi_setup(pf, pi, ICE_VSI_VF, vf_id);
  238. }
  239. /**
  240. * ice_alloc_vsi_res - Setup VF VSI and its resources
  241. * @vf: pointer to the VF structure
  242. *
  243. * Returns 0 on success, negative value on failure
  244. */
  245. static int ice_alloc_vsi_res(struct ice_vf *vf)
  246. {
  247. struct ice_pf *pf = vf->pf;
  248. LIST_HEAD(tmp_add_list);
  249. u8 broadcast[ETH_ALEN];
  250. struct ice_vsi *vsi;
  251. int status = 0;
  252. vsi = ice_vf_vsi_setup(pf, pf->hw.port_info, vf->vf_id);
  253. if (!vsi) {
  254. dev_err(&pf->pdev->dev, "Failed to create VF VSI\n");
  255. return -ENOMEM;
  256. }
  257. vf->lan_vsi_idx = vsi->idx;
  258. vf->lan_vsi_num = vsi->vsi_num;
  259. /* first vector index is the VFs OICR index */
  260. vf->first_vector_idx = vsi->hw_base_vector;
  261. /* Since hw_base_vector holds the vector where data queue interrupts
  262. * starts, increment by 1 since VFs allocated vectors include OICR intr
  263. * as well.
  264. */
  265. vsi->hw_base_vector += 1;
  266. /* Check if port VLAN exist before, and restore it accordingly */
  267. if (vf->port_vlan_id)
  268. ice_vsi_set_pvid(vsi, vf->port_vlan_id);
  269. eth_broadcast_addr(broadcast);
  270. status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
  271. if (status)
  272. goto ice_alloc_vsi_res_exit;
  273. if (is_valid_ether_addr(vf->dflt_lan_addr.addr)) {
  274. status = ice_add_mac_to_list(vsi, &tmp_add_list,
  275. vf->dflt_lan_addr.addr);
  276. if (status)
  277. goto ice_alloc_vsi_res_exit;
  278. }
  279. status = ice_add_mac(&pf->hw, &tmp_add_list);
  280. if (status)
  281. dev_err(&pf->pdev->dev, "could not add mac filters\n");
  282. /* Clear this bit after VF initialization since we shouldn't reclaim
  283. * and reassign interrupts for synchronous or asynchronous VFR events.
  284. * We don't want to reconfigure interrupts since AVF driver doesn't
  285. * expect vector assignment to be changed unless there is a request for
  286. * more vectors.
  287. */
  288. clear_bit(ICE_VF_STATE_CFG_INTR, vf->vf_states);
  289. ice_alloc_vsi_res_exit:
  290. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  291. return status;
  292. }
  293. /**
  294. * ice_alloc_vf_res - Allocate VF resources
  295. * @vf: pointer to the VF structure
  296. */
  297. static int ice_alloc_vf_res(struct ice_vf *vf)
  298. {
  299. int status;
  300. /* setup VF VSI and necessary resources */
  301. status = ice_alloc_vsi_res(vf);
  302. if (status)
  303. goto ice_alloc_vf_res_exit;
  304. if (vf->trusted)
  305. set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  306. else
  307. clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  308. /* VF is now completely initialized */
  309. set_bit(ICE_VF_STATE_INIT, vf->vf_states);
  310. return status;
  311. ice_alloc_vf_res_exit:
  312. ice_free_vf_res(vf);
  313. return status;
  314. }
  315. /**
  316. * ice_ena_vf_mappings
  317. * @vf: pointer to the VF structure
  318. *
  319. * Enable VF vectors and queues allocation by writing the details into
  320. * respective registers.
  321. */
  322. static void ice_ena_vf_mappings(struct ice_vf *vf)
  323. {
  324. struct ice_pf *pf = vf->pf;
  325. struct ice_vsi *vsi;
  326. int first, last, v;
  327. struct ice_hw *hw;
  328. int abs_vf_id;
  329. u32 reg;
  330. hw = &pf->hw;
  331. vsi = pf->vsi[vf->lan_vsi_idx];
  332. first = vf->first_vector_idx;
  333. last = (first + pf->num_vf_msix) - 1;
  334. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  335. /* VF Vector allocation */
  336. reg = (((first << VPINT_ALLOC_FIRST_S) & VPINT_ALLOC_FIRST_M) |
  337. ((last << VPINT_ALLOC_LAST_S) & VPINT_ALLOC_LAST_M) |
  338. VPINT_ALLOC_VALID_M);
  339. wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
  340. /* map the interrupts to its functions */
  341. for (v = first; v <= last; v++) {
  342. reg = (((abs_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
  343. GLINT_VECT2FUNC_VF_NUM_M) |
  344. ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
  345. GLINT_VECT2FUNC_PF_NUM_M));
  346. wr32(hw, GLINT_VECT2FUNC(v), reg);
  347. }
  348. /* VF Tx queues allocation */
  349. if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
  350. wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id),
  351. VPLAN_TXQ_MAPENA_TX_ENA_M);
  352. /* set the VF PF Tx queue range
  353. * VFNUMQ value should be set to (number of queues - 1). A value
  354. * of 0 means 1 queue and a value of 255 means 256 queues
  355. */
  356. reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
  357. VPLAN_TX_QBASE_VFFIRSTQ_M) |
  358. (((vsi->alloc_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
  359. VPLAN_TX_QBASE_VFNUMQ_M));
  360. wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
  361. } else {
  362. dev_err(&pf->pdev->dev,
  363. "Scattered mode for VF Tx queues is not yet implemented\n");
  364. }
  365. /* VF Rx queues allocation */
  366. if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
  367. wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id),
  368. VPLAN_RXQ_MAPENA_RX_ENA_M);
  369. /* set the VF PF Rx queue range
  370. * VFNUMQ value should be set to (number of queues - 1). A value
  371. * of 0 means 1 queue and a value of 255 means 256 queues
  372. */
  373. reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
  374. VPLAN_RX_QBASE_VFFIRSTQ_M) |
  375. (((vsi->alloc_txq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
  376. VPLAN_RX_QBASE_VFNUMQ_M));
  377. wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
  378. } else {
  379. dev_err(&pf->pdev->dev,
  380. "Scattered mode for VF Rx queues is not yet implemented\n");
  381. }
  382. }
  383. /**
  384. * ice_determine_res
  385. * @pf: pointer to the PF structure
  386. * @avail_res: available resources in the PF structure
  387. * @max_res: maximum resources that can be given per VF
  388. * @min_res: minimum resources that can be given per VF
  389. *
  390. * Returns non-zero value if resources (queues/vectors) are available or
  391. * returns zero if PF cannot accommodate for all num_alloc_vfs.
  392. */
  393. static int
  394. ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
  395. {
  396. bool checked_min_res = false;
  397. int res;
  398. /* start by checking if PF can assign max number of resources for
  399. * all num_alloc_vfs.
  400. * if yes, return number per VF
  401. * If no, divide by 2 and roundup, check again
  402. * repeat the loop till we reach a point where even minimum resources
  403. * are not available, in that case return 0
  404. */
  405. res = max_res;
  406. while ((res >= min_res) && !checked_min_res) {
  407. int num_all_res;
  408. num_all_res = pf->num_alloc_vfs * res;
  409. if (num_all_res <= avail_res)
  410. return res;
  411. if (res == min_res)
  412. checked_min_res = true;
  413. res = DIV_ROUND_UP(res, 2);
  414. }
  415. return 0;
  416. }
  417. /**
  418. * ice_check_avail_res - check if vectors and queues are available
  419. * @pf: pointer to the PF structure
  420. *
  421. * This function is where we calculate actual number of resources for VF VSIs,
  422. * we don't reserve ahead of time during probe. Returns success if vectors and
  423. * queues resources are available, otherwise returns error code
  424. */
  425. static int ice_check_avail_res(struct ice_pf *pf)
  426. {
  427. u16 num_msix, num_txq, num_rxq;
  428. if (!pf->num_alloc_vfs)
  429. return -EINVAL;
  430. /* Grab from HW interrupts common pool
  431. * Note: By the time the user decides it needs more vectors in a VF
  432. * its already too late since one must decide this prior to creating the
  433. * VF interface. So the best we can do is take a guess as to what the
  434. * user might want.
  435. *
  436. * We have two policies for vector allocation:
  437. * 1. if num_alloc_vfs is from 1 to 16, then we consider this as small
  438. * number of NFV VFs used for NFV appliances, since this is a special
  439. * case, we try to assign maximum vectors per VF (65) as much as
  440. * possible, based on determine_resources algorithm.
  441. * 2. if num_alloc_vfs is from 17 to 256, then its large number of
  442. * regular VFs which are not used for any special purpose. Hence try to
  443. * grab default interrupt vectors (5 as supported by AVF driver).
  444. */
  445. if (pf->num_alloc_vfs <= 16) {
  446. num_msix = ice_determine_res(pf, pf->num_avail_hw_msix,
  447. ICE_MAX_INTR_PER_VF,
  448. ICE_MIN_INTR_PER_VF);
  449. } else if (pf->num_alloc_vfs <= ICE_MAX_VF_COUNT) {
  450. num_msix = ice_determine_res(pf, pf->num_avail_hw_msix,
  451. ICE_DFLT_INTR_PER_VF,
  452. ICE_MIN_INTR_PER_VF);
  453. } else {
  454. dev_err(&pf->pdev->dev,
  455. "Number of VFs %d exceeds max VF count %d\n",
  456. pf->num_alloc_vfs, ICE_MAX_VF_COUNT);
  457. return -EIO;
  458. }
  459. if (!num_msix)
  460. return -EIO;
  461. /* Grab from the common pool
  462. * start by requesting Default queues (4 as supported by AVF driver),
  463. * Note that, the main difference between queues and vectors is, latter
  464. * can only be reserved at init time but queues can be requested by VF
  465. * at runtime through Virtchnl, that is the reason we start by reserving
  466. * few queues.
  467. */
  468. num_txq = ice_determine_res(pf, pf->q_left_tx, ICE_DFLT_QS_PER_VF,
  469. ICE_MIN_QS_PER_VF);
  470. num_rxq = ice_determine_res(pf, pf->q_left_rx, ICE_DFLT_QS_PER_VF,
  471. ICE_MIN_QS_PER_VF);
  472. if (!num_txq || !num_rxq)
  473. return -EIO;
  474. /* since AVF driver works with only queue pairs which means, it expects
  475. * to have equal number of Rx and Tx queues, so take the minimum of
  476. * available Tx or Rx queues
  477. */
  478. pf->num_vf_qps = min_t(int, num_txq, num_rxq);
  479. pf->num_vf_msix = num_msix;
  480. return 0;
  481. }
  482. /**
  483. * ice_cleanup_and_realloc_vf - Clean up VF and reallocate resources after reset
  484. * @vf: pointer to the VF structure
  485. *
  486. * Cleanup a VF after the hardware reset is finished. Expects the caller to
  487. * have verified whether the reset is finished properly, and ensure the
  488. * minimum amount of wait time has passed. Reallocate VF resources back to make
  489. * VF state active
  490. */
  491. static void ice_cleanup_and_realloc_vf(struct ice_vf *vf)
  492. {
  493. struct ice_pf *pf = vf->pf;
  494. struct ice_hw *hw;
  495. u32 reg;
  496. hw = &pf->hw;
  497. /* PF software completes the flow by notifying VF that reset flow is
  498. * completed. This is done by enabling hardware by clearing the reset
  499. * bit in the VPGEN_VFRTRIG reg and setting VFR_STATE in the VFGEN_RSTAT
  500. * register to VFR completed (done at the end of this function)
  501. * By doing this we allow HW to access VF memory at any point. If we
  502. * did it any sooner, HW could access memory while it was being freed
  503. * in ice_free_vf_res(), causing an IOMMU fault.
  504. *
  505. * On the other hand, this needs to be done ASAP, because the VF driver
  506. * is waiting for this to happen and may report a timeout. It's
  507. * harmless, but it gets logged into Guest OS kernel log, so best avoid
  508. * it.
  509. */
  510. reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
  511. reg &= ~VPGEN_VFRTRIG_VFSWR_M;
  512. wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
  513. /* reallocate VF resources to finish resetting the VSI state */
  514. if (!ice_alloc_vf_res(vf)) {
  515. ice_ena_vf_mappings(vf);
  516. set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
  517. clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
  518. vf->num_vlan = 0;
  519. }
  520. /* Tell the VF driver the reset is done. This needs to be done only
  521. * after VF has been fully initialized, because the VF driver may
  522. * request resources immediately after setting this flag.
  523. */
  524. wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
  525. }
  526. /**
  527. * ice_reset_all_vfs - reset all allocated VFs in one go
  528. * @pf: pointer to the PF structure
  529. * @is_vflr: true if VFLR was issued, false if not
  530. *
  531. * First, tell the hardware to reset each VF, then do all the waiting in one
  532. * chunk, and finally finish restoring each VF after the wait. This is useful
  533. * during PF routines which need to reset all VFs, as otherwise it must perform
  534. * these resets in a serialized fashion.
  535. *
  536. * Returns true if any VFs were reset, and false otherwise.
  537. */
  538. bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
  539. {
  540. struct ice_hw *hw = &pf->hw;
  541. int v, i;
  542. /* If we don't have any VFs, then there is nothing to reset */
  543. if (!pf->num_alloc_vfs)
  544. return false;
  545. /* If VFs have been disabled, there is no need to reset */
  546. if (test_and_set_bit(__ICE_VF_DIS, pf->state))
  547. return false;
  548. /* Begin reset on all VFs at once */
  549. for (v = 0; v < pf->num_alloc_vfs; v++)
  550. ice_trigger_vf_reset(&pf->vf[v], is_vflr);
  551. /* Call Disable LAN Tx queue AQ call with VFR bit set and 0
  552. * queues to inform Firmware about VF reset.
  553. */
  554. for (v = 0; v < pf->num_alloc_vfs; v++)
  555. ice_dis_vsi_txq(pf->vsi[0]->port_info, 0, NULL, NULL,
  556. ICE_VF_RESET, v, NULL);
  557. /* HW requires some time to make sure it can flush the FIFO for a VF
  558. * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
  559. * sequence to make sure that it has completed. We'll keep track of
  560. * the VFs using a simple iterator that increments once that VF has
  561. * finished resetting.
  562. */
  563. for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
  564. usleep_range(10000, 20000);
  565. /* Check each VF in sequence */
  566. while (v < pf->num_alloc_vfs) {
  567. struct ice_vf *vf = &pf->vf[v];
  568. u32 reg;
  569. reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
  570. if (!(reg & VPGEN_VFRSTAT_VFRD_M))
  571. break;
  572. /* If the current VF has finished resetting, move on
  573. * to the next VF in sequence.
  574. */
  575. v++;
  576. }
  577. }
  578. /* Display a warning if at least one VF didn't manage to reset in
  579. * time, but continue on with the operation.
  580. */
  581. if (v < pf->num_alloc_vfs)
  582. dev_warn(&pf->pdev->dev, "VF reset check timeout\n");
  583. usleep_range(10000, 20000);
  584. /* free VF resources to begin resetting the VSI state */
  585. for (v = 0; v < pf->num_alloc_vfs; v++)
  586. ice_free_vf_res(&pf->vf[v]);
  587. if (ice_check_avail_res(pf)) {
  588. dev_err(&pf->pdev->dev,
  589. "Cannot allocate VF resources, try with fewer number of VFs\n");
  590. return false;
  591. }
  592. /* Finish the reset on each VF */
  593. for (v = 0; v < pf->num_alloc_vfs; v++)
  594. ice_cleanup_and_realloc_vf(&pf->vf[v]);
  595. ice_flush(hw);
  596. clear_bit(__ICE_VF_DIS, pf->state);
  597. return true;
  598. }
  599. /**
  600. * ice_alloc_vfs - Allocate and set up VFs resources
  601. * @pf: pointer to the PF structure
  602. * @num_alloc_vfs: number of VFs to allocate
  603. */
  604. static int ice_alloc_vfs(struct ice_pf *pf, u16 num_alloc_vfs)
  605. {
  606. struct ice_hw *hw = &pf->hw;
  607. struct ice_vf *vfs;
  608. int i, ret;
  609. /* Disable global interrupt 0 so we don't try to handle the VFLR. */
  610. wr32(hw, GLINT_DYN_CTL(pf->hw_oicr_idx),
  611. ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
  612. ice_flush(hw);
  613. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  614. if (ret) {
  615. pf->num_alloc_vfs = 0;
  616. goto err_unroll_intr;
  617. }
  618. /* allocate memory */
  619. vfs = devm_kcalloc(&pf->pdev->dev, num_alloc_vfs, sizeof(*vfs),
  620. GFP_KERNEL);
  621. if (!vfs) {
  622. ret = -ENOMEM;
  623. goto err_unroll_sriov;
  624. }
  625. pf->vf = vfs;
  626. /* apply default profile */
  627. for (i = 0; i < num_alloc_vfs; i++) {
  628. vfs[i].pf = pf;
  629. vfs[i].vf_sw_id = pf->first_sw;
  630. vfs[i].vf_id = i;
  631. /* assign default capabilities */
  632. set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
  633. vfs[i].spoofchk = true;
  634. /* Set this state so that PF driver does VF vector assignment */
  635. set_bit(ICE_VF_STATE_CFG_INTR, vfs[i].vf_states);
  636. }
  637. pf->num_alloc_vfs = num_alloc_vfs;
  638. /* VF resources get allocated during reset */
  639. if (!ice_reset_all_vfs(pf, false))
  640. goto err_unroll_sriov;
  641. goto err_unroll_intr;
  642. err_unroll_sriov:
  643. pci_disable_sriov(pf->pdev);
  644. err_unroll_intr:
  645. /* rearm interrupts here */
  646. ice_irq_dynamic_ena(hw, NULL, NULL);
  647. return ret;
  648. }
  649. /**
  650. * ice_pf_state_is_nominal - checks the pf for nominal state
  651. * @pf: pointer to pf to check
  652. *
  653. * Check the PF's state for a collection of bits that would indicate
  654. * the PF is in a state that would inhibit normal operation for
  655. * driver functionality.
  656. *
  657. * Returns true if PF is in a nominal state.
  658. * Returns false otherwise
  659. */
  660. static bool ice_pf_state_is_nominal(struct ice_pf *pf)
  661. {
  662. DECLARE_BITMAP(check_bits, __ICE_STATE_NBITS) = { 0 };
  663. if (!pf)
  664. return false;
  665. bitmap_set(check_bits, 0, __ICE_STATE_NOMINAL_CHECK_BITS);
  666. if (bitmap_intersects(pf->state, check_bits, __ICE_STATE_NBITS))
  667. return false;
  668. return true;
  669. }
  670. /**
  671. * ice_pci_sriov_ena - Enable or change number of VFs
  672. * @pf: pointer to the PF structure
  673. * @num_vfs: number of VFs to allocate
  674. */
  675. static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
  676. {
  677. int pre_existing_vfs = pci_num_vf(pf->pdev);
  678. struct device *dev = &pf->pdev->dev;
  679. int err;
  680. if (!ice_pf_state_is_nominal(pf)) {
  681. dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
  682. return -EBUSY;
  683. }
  684. if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
  685. dev_err(dev, "This device is not capable of SR-IOV\n");
  686. return -ENODEV;
  687. }
  688. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  689. ice_free_vfs(pf);
  690. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  691. return num_vfs;
  692. if (num_vfs > pf->num_vfs_supported) {
  693. dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
  694. num_vfs, pf->num_vfs_supported);
  695. return -ENOTSUPP;
  696. }
  697. dev_info(dev, "Allocating %d VFs\n", num_vfs);
  698. err = ice_alloc_vfs(pf, num_vfs);
  699. if (err) {
  700. dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
  701. return err;
  702. }
  703. set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
  704. return num_vfs;
  705. }
  706. /**
  707. * ice_sriov_configure - Enable or change number of VFs via sysfs
  708. * @pdev: pointer to a pci_dev structure
  709. * @num_vfs: number of VFs to allocate
  710. *
  711. * This function is called when the user updates the number of VFs in sysfs.
  712. */
  713. int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
  714. {
  715. struct ice_pf *pf = pci_get_drvdata(pdev);
  716. if (num_vfs)
  717. return ice_pci_sriov_ena(pf, num_vfs);
  718. if (!pci_vfs_assigned(pdev)) {
  719. ice_free_vfs(pf);
  720. } else {
  721. dev_err(&pf->pdev->dev,
  722. "can't free VFs because some are assigned to VMs.\n");
  723. return -EBUSY;
  724. }
  725. return 0;
  726. }