ice_vf_lib.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2022, Intel Corporation. */
  3. #include "ice_vf_lib_private.h"
  4. #include "ice.h"
  5. #include "ice_lib.h"
  6. #include "ice_fltr.h"
  7. #include "ice_virtchnl_allowlist.h"
  8. /* Public functions which may be accessed by all driver files */
  9. /**
  10. * ice_get_vf_by_id - Get pointer to VF by ID
  11. * @pf: the PF private structure
  12. * @vf_id: the VF ID to locate
  13. *
  14. * Locate and return a pointer to the VF structure associated with a given ID.
  15. * Returns NULL if the ID does not have a valid VF structure associated with
  16. * it.
  17. *
  18. * This function takes a reference to the VF, which must be released by
  19. * calling ice_put_vf() once the caller is finished accessing the VF structure
  20. * returned.
  21. */
  22. struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
  23. {
  24. struct ice_vf *vf;
  25. rcu_read_lock();
  26. hash_for_each_possible_rcu(pf->vfs.table, vf, entry, vf_id) {
  27. if (vf->vf_id == vf_id) {
  28. struct ice_vf *found;
  29. if (kref_get_unless_zero(&vf->refcnt))
  30. found = vf;
  31. else
  32. found = NULL;
  33. rcu_read_unlock();
  34. return found;
  35. }
  36. }
  37. rcu_read_unlock();
  38. return NULL;
  39. }
  40. /**
  41. * ice_release_vf - Release VF associated with a refcount
  42. * @ref: the kref decremented to zero
  43. *
  44. * Callback function for kref_put to release a VF once its reference count has
  45. * hit zero.
  46. */
  47. static void ice_release_vf(struct kref *ref)
  48. {
  49. struct ice_vf *vf = container_of(ref, struct ice_vf, refcnt);
  50. pci_dev_put(vf->vfdev);
  51. vf->vf_ops->free(vf);
  52. }
  53. /**
  54. * ice_put_vf - Release a reference to a VF
  55. * @vf: the VF structure to decrease reference count on
  56. *
  57. * Decrease the reference count for a VF, and free the entry if it is no
  58. * longer in use.
  59. *
  60. * This must be called after ice_get_vf_by_id() once the reference to the VF
  61. * structure is no longer used. Otherwise, the VF structure will never be
  62. * freed.
  63. */
  64. void ice_put_vf(struct ice_vf *vf)
  65. {
  66. kref_put(&vf->refcnt, ice_release_vf);
  67. }
  68. /**
  69. * ice_has_vfs - Return true if the PF has any associated VFs
  70. * @pf: the PF private structure
  71. *
  72. * Return whether or not the PF has any allocated VFs.
  73. *
  74. * Note that this function only guarantees that there are no VFs at the point
  75. * of calling it. It does not guarantee that no more VFs will be added.
  76. */
  77. bool ice_has_vfs(struct ice_pf *pf)
  78. {
  79. /* A simple check that the hash table is not empty does not require
  80. * the mutex or rcu_read_lock.
  81. */
  82. return !hash_empty(pf->vfs.table);
  83. }
  84. /**
  85. * ice_get_num_vfs - Get number of allocated VFs
  86. * @pf: the PF private structure
  87. *
  88. * Return the total number of allocated VFs. NOTE: VF IDs are not guaranteed
  89. * to be contiguous. Do not assume that a VF ID is guaranteed to be less than
  90. * the output of this function.
  91. */
  92. u16 ice_get_num_vfs(struct ice_pf *pf)
  93. {
  94. struct ice_vf *vf;
  95. unsigned int bkt;
  96. u16 num_vfs = 0;
  97. rcu_read_lock();
  98. ice_for_each_vf_rcu(pf, bkt, vf)
  99. num_vfs++;
  100. rcu_read_unlock();
  101. return num_vfs;
  102. }
  103. /**
  104. * ice_get_vf_vsi - get VF's VSI based on the stored index
  105. * @vf: VF used to get VSI
  106. */
  107. struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
  108. {
  109. if (vf->lan_vsi_idx == ICE_NO_VSI)
  110. return NULL;
  111. return vf->pf->vsi[vf->lan_vsi_idx];
  112. }
  113. /**
  114. * ice_is_vf_disabled
  115. * @vf: pointer to the VF info
  116. *
  117. * If the PF has been disabled, there is no need resetting VF until PF is
  118. * active again. Similarly, if the VF has been disabled, this means something
  119. * else is resetting the VF, so we shouldn't continue.
  120. *
  121. * Returns true if the caller should consider the VF as disabled whether
  122. * because that single VF is explicitly disabled or because the PF is
  123. * currently disabled.
  124. */
  125. bool ice_is_vf_disabled(struct ice_vf *vf)
  126. {
  127. struct ice_pf *pf = vf->pf;
  128. return (test_bit(ICE_VF_DIS, pf->state) ||
  129. test_bit(ICE_VF_STATE_DIS, vf->vf_states));
  130. }
  131. /**
  132. * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
  133. * @vf: The VF being resseting
  134. *
  135. * The max poll time is about ~800ms, which is about the maximum time it takes
  136. * for a VF to be reset and/or a VF driver to be removed.
  137. */
  138. static void ice_wait_on_vf_reset(struct ice_vf *vf)
  139. {
  140. int i;
  141. for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
  142. if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
  143. break;
  144. msleep(ICE_MAX_VF_RESET_SLEEP_MS);
  145. }
  146. }
  147. /**
  148. * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
  149. * @vf: VF to check if it's ready to be configured/queried
  150. *
  151. * The purpose of this function is to make sure the VF is not in reset, not
  152. * disabled, and initialized so it can be configured and/or queried by a host
  153. * administrator.
  154. */
  155. int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
  156. {
  157. ice_wait_on_vf_reset(vf);
  158. if (ice_is_vf_disabled(vf))
  159. return -EINVAL;
  160. if (ice_check_vf_init(vf))
  161. return -EBUSY;
  162. return 0;
  163. }
  164. /**
  165. * ice_trigger_vf_reset - Reset a VF on HW
  166. * @vf: pointer to the VF structure
  167. * @is_vflr: true if VFLR was issued, false if not
  168. * @is_pfr: true if the reset was triggered due to a previous PFR
  169. *
  170. * Trigger hardware to start a reset for a particular VF. Expects the caller
  171. * to wait the proper amount of time to allow hardware to reset the VF before
  172. * it cleans up and restores VF functionality.
  173. */
  174. static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
  175. {
  176. /* Inform VF that it is no longer active, as a warning */
  177. clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
  178. /* Disable VF's configuration API during reset. The flag is re-enabled
  179. * when it's safe again to access VF's VSI.
  180. */
  181. clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
  182. /* VF_MBX_ARQLEN and VF_MBX_ATQLEN are cleared by PFR, so the driver
  183. * needs to clear them in the case of VFR/VFLR. If this is done for
  184. * PFR, it can mess up VF resets because the VF driver may already
  185. * have started cleanup by the time we get here.
  186. */
  187. if (!is_pfr)
  188. vf->vf_ops->clear_mbx_register(vf);
  189. vf->vf_ops->trigger_reset_register(vf, is_vflr);
  190. }
  191. static void ice_vf_clear_counters(struct ice_vf *vf)
  192. {
  193. struct ice_vsi *vsi = ice_get_vf_vsi(vf);
  194. if (vsi)
  195. vsi->num_vlan = 0;
  196. vf->num_mac = 0;
  197. memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
  198. memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
  199. }
  200. /**
  201. * ice_vf_pre_vsi_rebuild - tasks to be done prior to VSI rebuild
  202. * @vf: VF to perform pre VSI rebuild tasks
  203. *
  204. * These tasks are items that don't need to be amortized since they are most
  205. * likely called in a for loop with all VF(s) in the reset_all_vfs() case.
  206. */
  207. static void ice_vf_pre_vsi_rebuild(struct ice_vf *vf)
  208. {
  209. /* Close any IRQ mapping now */
  210. if (vf->vf_ops->irq_close)
  211. vf->vf_ops->irq_close(vf);
  212. ice_vf_clear_counters(vf);
  213. vf->vf_ops->clear_reset_trigger(vf);
  214. }
  215. /**
  216. * ice_vf_reconfig_vsi - Reconfigure a VF VSI with the device
  217. * @vf: VF to reconfigure the VSI for
  218. *
  219. * This is called when a single VF is being reset (i.e. VVF, VFLR, host VF
  220. * configuration change, etc).
  221. *
  222. * It brings the VSI down and then reconfigures it with the hardware.
  223. */
  224. static int ice_vf_reconfig_vsi(struct ice_vf *vf)
  225. {
  226. struct ice_vsi *vsi = ice_get_vf_vsi(vf);
  227. struct ice_pf *pf = vf->pf;
  228. int err;
  229. if (WARN_ON(!vsi))
  230. return -EINVAL;
  231. vsi->flags = ICE_VSI_FLAG_NO_INIT;
  232. ice_vsi_decfg(vsi);
  233. ice_fltr_remove_all(vsi);
  234. err = ice_vsi_cfg(vsi);
  235. if (err) {
  236. dev_err(ice_pf_to_dev(pf),
  237. "Failed to reconfigure the VF%u's VSI, error %d\n",
  238. vf->vf_id, err);
  239. return err;
  240. }
  241. return 0;
  242. }
  243. /**
  244. * ice_vf_rebuild_vsi - rebuild the VF's VSI
  245. * @vf: VF to rebuild the VSI for
  246. *
  247. * This is only called when all VF(s) are being reset (i.e. PCIe Reset on the
  248. * host, PFR, CORER, etc.).
  249. *
  250. * It reprograms the VSI configuration back into hardware.
  251. */
  252. static int ice_vf_rebuild_vsi(struct ice_vf *vf)
  253. {
  254. struct ice_vsi *vsi = ice_get_vf_vsi(vf);
  255. struct ice_pf *pf = vf->pf;
  256. if (WARN_ON(!vsi))
  257. return -EINVAL;
  258. if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT)) {
  259. dev_err(ice_pf_to_dev(pf), "failed to rebuild VF %d VSI\n",
  260. vf->vf_id);
  261. return -EIO;
  262. }
  263. /* vsi->idx will remain the same in this case so don't update
  264. * vf->lan_vsi_idx
  265. */
  266. vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
  267. return 0;
  268. }
  269. /**
  270. * ice_vf_rebuild_host_vlan_cfg - add VLAN 0 filter or rebuild the Port VLAN
  271. * @vf: VF to add MAC filters for
  272. * @vsi: Pointer to VSI
  273. *
  274. * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
  275. * always re-adds either a VLAN 0 or port VLAN based filter after reset.
  276. */
  277. static int ice_vf_rebuild_host_vlan_cfg(struct ice_vf *vf, struct ice_vsi *vsi)
  278. {
  279. struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
  280. struct device *dev = ice_pf_to_dev(vf->pf);
  281. int err;
  282. if (ice_vf_is_port_vlan_ena(vf)) {
  283. err = vlan_ops->set_port_vlan(vsi, &vf->port_vlan_info);
  284. if (err) {
  285. dev_err(dev, "failed to configure port VLAN via VSI parameters for VF %u, error %d\n",
  286. vf->vf_id, err);
  287. return err;
  288. }
  289. err = vlan_ops->add_vlan(vsi, &vf->port_vlan_info);
  290. } else {
  291. /* clear possible previous port vlan config */
  292. err = ice_vsi_clear_port_vlan(vsi);
  293. if (err) {
  294. dev_err(dev, "failed to clear port VLAN via VSI parameters for VF %u, error %d\n",
  295. vf->vf_id, err);
  296. return err;
  297. }
  298. err = ice_vsi_add_vlan_zero(vsi);
  299. }
  300. if (err) {
  301. dev_err(dev, "failed to add VLAN %u filter for VF %u during VF rebuild, error %d\n",
  302. ice_vf_is_port_vlan_ena(vf) ?
  303. ice_vf_get_port_vlan_id(vf) : 0, vf->vf_id, err);
  304. return err;
  305. }
  306. err = vlan_ops->ena_rx_filtering(vsi);
  307. if (err)
  308. dev_warn(dev, "failed to enable Rx VLAN filtering for VF %d VSI %d during VF rebuild, error %d\n",
  309. vf->vf_id, vsi->idx, err);
  310. return 0;
  311. }
  312. /**
  313. * ice_vf_rebuild_host_tx_rate_cfg - re-apply the Tx rate limiting configuration
  314. * @vf: VF to re-apply the configuration for
  315. *
  316. * Called after a VF VSI has been re-added/rebuild during reset. The PF driver
  317. * needs to re-apply the host configured Tx rate limiting configuration.
  318. */
  319. static int ice_vf_rebuild_host_tx_rate_cfg(struct ice_vf *vf)
  320. {
  321. struct device *dev = ice_pf_to_dev(vf->pf);
  322. struct ice_vsi *vsi = ice_get_vf_vsi(vf);
  323. int err;
  324. if (WARN_ON(!vsi))
  325. return -EINVAL;
  326. if (vf->min_tx_rate) {
  327. err = ice_set_min_bw_limit(vsi, (u64)vf->min_tx_rate * 1000);
  328. if (err) {
  329. dev_err(dev, "failed to set min Tx rate to %d Mbps for VF %u, error %d\n",
  330. vf->min_tx_rate, vf->vf_id, err);
  331. return err;
  332. }
  333. }
  334. if (vf->max_tx_rate) {
  335. err = ice_set_max_bw_limit(vsi, (u64)vf->max_tx_rate * 1000);
  336. if (err) {
  337. dev_err(dev, "failed to set max Tx rate to %d Mbps for VF %u, error %d\n",
  338. vf->max_tx_rate, vf->vf_id, err);
  339. return err;
  340. }
  341. }
  342. return 0;
  343. }
  344. /**
  345. * ice_vf_set_host_trust_cfg - set trust setting based on pre-reset value
  346. * @vf: VF to configure trust setting for
  347. */
  348. static void ice_vf_set_host_trust_cfg(struct ice_vf *vf)
  349. {
  350. assign_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps, vf->trusted);
  351. }
  352. /**
  353. * ice_vf_rebuild_host_mac_cfg - add broadcast and the VF's perm_addr/LAA
  354. * @vf: VF to add MAC filters for
  355. *
  356. * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
  357. * always re-adds a broadcast filter and the VF's perm_addr/LAA after reset.
  358. */
  359. static int ice_vf_rebuild_host_mac_cfg(struct ice_vf *vf)
  360. {
  361. struct device *dev = ice_pf_to_dev(vf->pf);
  362. struct ice_vsi *vsi = ice_get_vf_vsi(vf);
  363. u8 broadcast[ETH_ALEN];
  364. int status;
  365. if (WARN_ON(!vsi))
  366. return -EINVAL;
  367. if (ice_is_eswitch_mode_switchdev(vf->pf))
  368. return 0;
  369. eth_broadcast_addr(broadcast);
  370. status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
  371. if (status) {
  372. dev_err(dev, "failed to add broadcast MAC filter for VF %u, error %d\n",
  373. vf->vf_id, status);
  374. return status;
  375. }
  376. vf->num_mac++;
  377. if (is_valid_ether_addr(vf->hw_lan_addr)) {
  378. status = ice_fltr_add_mac(vsi, vf->hw_lan_addr,
  379. ICE_FWD_TO_VSI);
  380. if (status) {
  381. dev_err(dev, "failed to add default unicast MAC filter %pM for VF %u, error %d\n",
  382. &vf->hw_lan_addr[0], vf->vf_id,
  383. status);
  384. return status;
  385. }
  386. vf->num_mac++;
  387. ether_addr_copy(vf->dev_lan_addr, vf->hw_lan_addr);
  388. }
  389. return 0;
  390. }
  391. /**
  392. * ice_vf_rebuild_aggregator_node_cfg - rebuild aggregator node config
  393. * @vsi: Pointer to VSI
  394. *
  395. * This function moves VSI into corresponding scheduler aggregator node
  396. * based on cached value of "aggregator node info" per VSI
  397. */
  398. static void ice_vf_rebuild_aggregator_node_cfg(struct ice_vsi *vsi)
  399. {
  400. struct ice_pf *pf = vsi->back;
  401. struct device *dev;
  402. int status;
  403. if (!vsi->agg_node)
  404. return;
  405. dev = ice_pf_to_dev(pf);
  406. if (vsi->agg_node->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
  407. dev_dbg(dev,
  408. "agg_id %u already has reached max_num_vsis %u\n",
  409. vsi->agg_node->agg_id, vsi->agg_node->num_vsis);
  410. return;
  411. }
  412. status = ice_move_vsi_to_agg(pf->hw.port_info, vsi->agg_node->agg_id,
  413. vsi->idx, vsi->tc_cfg.ena_tc);
  414. if (status)
  415. dev_dbg(dev, "unable to move VSI idx %u into aggregator %u node",
  416. vsi->idx, vsi->agg_node->agg_id);
  417. else
  418. vsi->agg_node->num_vsis++;
  419. }
  420. /**
  421. * ice_vf_rebuild_host_cfg - host admin configuration is persistent across reset
  422. * @vf: VF to rebuild host configuration on
  423. */
  424. static void ice_vf_rebuild_host_cfg(struct ice_vf *vf)
  425. {
  426. struct device *dev = ice_pf_to_dev(vf->pf);
  427. struct ice_vsi *vsi = ice_get_vf_vsi(vf);
  428. if (WARN_ON(!vsi))
  429. return;
  430. ice_vf_set_host_trust_cfg(vf);
  431. if (ice_vf_rebuild_host_mac_cfg(vf))
  432. dev_err(dev, "failed to rebuild default MAC configuration for VF %d\n",
  433. vf->vf_id);
  434. if (ice_vf_rebuild_host_vlan_cfg(vf, vsi))
  435. dev_err(dev, "failed to rebuild VLAN configuration for VF %u\n",
  436. vf->vf_id);
  437. if (ice_vf_rebuild_host_tx_rate_cfg(vf))
  438. dev_err(dev, "failed to rebuild Tx rate limiting configuration for VF %u\n",
  439. vf->vf_id);
  440. if (ice_vsi_apply_spoofchk(vsi, vf->spoofchk))
  441. dev_err(dev, "failed to rebuild spoofchk configuration for VF %d\n",
  442. vf->vf_id);
  443. /* rebuild aggregator node config for main VF VSI */
  444. ice_vf_rebuild_aggregator_node_cfg(vsi);
  445. }
  446. /**
  447. * ice_set_vf_state_qs_dis - Set VF queues state to disabled
  448. * @vf: pointer to the VF structure
  449. */
  450. static void ice_set_vf_state_qs_dis(struct ice_vf *vf)
  451. {
  452. /* Clear Rx/Tx enabled queues flag */
  453. bitmap_zero(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
  454. bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
  455. clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
  456. }
  457. /**
  458. * ice_vf_set_initialized - VF is ready for VIRTCHNL communication
  459. * @vf: VF to set in initialized state
  460. *
  461. * After this function the VF will be ready to receive/handle the
  462. * VIRTCHNL_OP_GET_VF_RESOURCES message
  463. */
  464. static void ice_vf_set_initialized(struct ice_vf *vf)
  465. {
  466. ice_set_vf_state_qs_dis(vf);
  467. clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
  468. clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
  469. clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
  470. set_bit(ICE_VF_STATE_INIT, vf->vf_states);
  471. memset(&vf->vlan_v2_caps, 0, sizeof(vf->vlan_v2_caps));
  472. }
  473. /**
  474. * ice_vf_post_vsi_rebuild - Reset tasks that occur after VSI rebuild
  475. * @vf: the VF being reset
  476. *
  477. * Perform reset tasks which must occur after the VSI has been re-created or
  478. * rebuilt during a VF reset.
  479. */
  480. static void ice_vf_post_vsi_rebuild(struct ice_vf *vf)
  481. {
  482. ice_vf_rebuild_host_cfg(vf);
  483. ice_vf_set_initialized(vf);
  484. vf->vf_ops->post_vsi_rebuild(vf);
  485. }
  486. /**
  487. * ice_is_any_vf_in_unicast_promisc - check if any VF(s)
  488. * are in unicast promiscuous mode
  489. * @pf: PF structure for accessing VF(s)
  490. *
  491. * Return false if no VF(s) are in unicast promiscuous mode,
  492. * else return true
  493. */
  494. bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf)
  495. {
  496. bool is_vf_promisc = false;
  497. struct ice_vf *vf;
  498. unsigned int bkt;
  499. rcu_read_lock();
  500. ice_for_each_vf_rcu(pf, bkt, vf) {
  501. /* found a VF that has promiscuous mode configured */
  502. if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states)) {
  503. is_vf_promisc = true;
  504. break;
  505. }
  506. }
  507. rcu_read_unlock();
  508. return is_vf_promisc;
  509. }
  510. /**
  511. * ice_vf_get_promisc_masks - Calculate masks for promiscuous modes
  512. * @vf: the VF pointer
  513. * @vsi: the VSI to configure
  514. * @ucast_m: promiscuous mask to apply to unicast
  515. * @mcast_m: promiscuous mask to apply to multicast
  516. *
  517. * Decide which mask should be used for unicast and multicast filter,
  518. * based on presence of VLANs
  519. */
  520. void
  521. ice_vf_get_promisc_masks(struct ice_vf *vf, struct ice_vsi *vsi,
  522. u8 *ucast_m, u8 *mcast_m)
  523. {
  524. if (ice_vf_is_port_vlan_ena(vf) ||
  525. ice_vsi_has_non_zero_vlans(vsi)) {
  526. *mcast_m = ICE_MCAST_VLAN_PROMISC_BITS;
  527. *ucast_m = ICE_UCAST_VLAN_PROMISC_BITS;
  528. } else {
  529. *mcast_m = ICE_MCAST_PROMISC_BITS;
  530. *ucast_m = ICE_UCAST_PROMISC_BITS;
  531. }
  532. }
  533. /**
  534. * ice_vf_clear_all_promisc_modes - Clear promisc/allmulticast on VF VSI
  535. * @vf: the VF pointer
  536. * @vsi: the VSI to configure
  537. *
  538. * Clear all promiscuous/allmulticast filters for a VF
  539. */
  540. static int
  541. ice_vf_clear_all_promisc_modes(struct ice_vf *vf, struct ice_vsi *vsi)
  542. {
  543. struct ice_pf *pf = vf->pf;
  544. u8 ucast_m, mcast_m;
  545. int ret = 0;
  546. ice_vf_get_promisc_masks(vf, vsi, &ucast_m, &mcast_m);
  547. if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states)) {
  548. if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
  549. if (ice_is_dflt_vsi_in_use(vsi->port_info))
  550. ret = ice_clear_dflt_vsi(vsi);
  551. } else {
  552. ret = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
  553. }
  554. if (ret) {
  555. dev_err(ice_pf_to_dev(vf->pf), "Disabling promiscuous mode failed\n");
  556. } else {
  557. clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
  558. dev_info(ice_pf_to_dev(vf->pf), "Disabling promiscuous mode succeeded\n");
  559. }
  560. }
  561. if (test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
  562. ret = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
  563. if (ret) {
  564. dev_err(ice_pf_to_dev(vf->pf), "Disabling allmulticast mode failed\n");
  565. } else {
  566. clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
  567. dev_info(ice_pf_to_dev(vf->pf), "Disabling allmulticast mode succeeded\n");
  568. }
  569. }
  570. return ret;
  571. }
  572. /**
  573. * ice_vf_set_vsi_promisc - Enable promiscuous mode for a VF VSI
  574. * @vf: the VF to configure
  575. * @vsi: the VF's VSI
  576. * @promisc_m: the promiscuous mode to enable
  577. */
  578. int
  579. ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
  580. {
  581. struct ice_hw *hw = &vsi->back->hw;
  582. int status;
  583. if (ice_vf_is_port_vlan_ena(vf))
  584. status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m,
  585. ice_vf_get_port_vlan_id(vf));
  586. else if (ice_vsi_has_non_zero_vlans(vsi))
  587. status = ice_fltr_set_vlan_vsi_promisc(hw, vsi, promisc_m);
  588. else
  589. status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m, 0);
  590. if (status && status != -EEXIST) {
  591. dev_err(ice_pf_to_dev(vsi->back), "enable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
  592. vf->vf_id, status);
  593. return status;
  594. }
  595. return 0;
  596. }
  597. /**
  598. * ice_vf_clear_vsi_promisc - Disable promiscuous mode for a VF VSI
  599. * @vf: the VF to configure
  600. * @vsi: the VF's VSI
  601. * @promisc_m: the promiscuous mode to disable
  602. */
  603. int
  604. ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
  605. {
  606. struct ice_hw *hw = &vsi->back->hw;
  607. int status;
  608. if (ice_vf_is_port_vlan_ena(vf))
  609. status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m,
  610. ice_vf_get_port_vlan_id(vf));
  611. else if (ice_vsi_has_non_zero_vlans(vsi))
  612. status = ice_fltr_clear_vlan_vsi_promisc(hw, vsi, promisc_m);
  613. else
  614. status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m, 0);
  615. if (status && status != -ENOENT) {
  616. dev_err(ice_pf_to_dev(vsi->back), "disable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
  617. vf->vf_id, status);
  618. return status;
  619. }
  620. return 0;
  621. }
  622. /**
  623. * ice_reset_vf_mbx_cnt - reset VF mailbox message count
  624. * @vf: pointer to the VF structure
  625. *
  626. * This function clears the VF mailbox message count, and should be called on
  627. * VF reset.
  628. */
  629. static void ice_reset_vf_mbx_cnt(struct ice_vf *vf)
  630. {
  631. struct ice_pf *pf = vf->pf;
  632. if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT))
  633. ice_mbx_vf_clear_cnt_e830(&pf->hw, vf->vf_id);
  634. else
  635. ice_mbx_clear_malvf(&vf->mbx_info);
  636. }
  637. /**
  638. * ice_reset_all_vfs - reset all allocated VFs in one go
  639. * @pf: pointer to the PF structure
  640. *
  641. * Reset all VFs at once, in response to a PF or other device reset.
  642. *
  643. * First, tell the hardware to reset each VF, then do all the waiting in one
  644. * chunk, and finally finish restoring each VF after the wait. This is useful
  645. * during PF routines which need to reset all VFs, as otherwise it must perform
  646. * these resets in a serialized fashion.
  647. */
  648. void ice_reset_all_vfs(struct ice_pf *pf)
  649. {
  650. struct device *dev = ice_pf_to_dev(pf);
  651. struct ice_hw *hw = &pf->hw;
  652. struct ice_vf *vf;
  653. unsigned int bkt;
  654. /* If we don't have any VFs, then there is nothing to reset */
  655. if (!ice_has_vfs(pf))
  656. return;
  657. mutex_lock(&pf->vfs.table_lock);
  658. /* clear all malicious info if the VFs are getting reset */
  659. ice_for_each_vf(pf, bkt, vf)
  660. ice_reset_vf_mbx_cnt(vf);
  661. /* If VFs have been disabled, there is no need to reset */
  662. if (test_and_set_bit(ICE_VF_DIS, pf->state)) {
  663. mutex_unlock(&pf->vfs.table_lock);
  664. return;
  665. }
  666. /* Begin reset on all VFs at once */
  667. ice_for_each_vf(pf, bkt, vf)
  668. ice_trigger_vf_reset(vf, true, true);
  669. /* HW requires some time to make sure it can flush the FIFO for a VF
  670. * when it resets it. Now that we've triggered all of the VFs, iterate
  671. * the table again and wait for each VF to complete.
  672. */
  673. ice_for_each_vf(pf, bkt, vf) {
  674. if (!vf->vf_ops->poll_reset_status(vf)) {
  675. /* Display a warning if at least one VF didn't manage
  676. * to reset in time, but continue on with the
  677. * operation.
  678. */
  679. dev_warn(dev, "VF %u reset check timeout\n", vf->vf_id);
  680. break;
  681. }
  682. }
  683. /* free VF resources to begin resetting the VSI state */
  684. ice_for_each_vf(pf, bkt, vf) {
  685. mutex_lock(&vf->cfg_lock);
  686. ice_eswitch_detach_vf(pf, vf);
  687. vf->driver_caps = 0;
  688. ice_vc_set_default_allowlist(vf);
  689. ice_vf_fdir_exit(vf);
  690. ice_vf_fdir_init(vf);
  691. /* clean VF control VSI when resetting VFs since it should be
  692. * setup only when VF creates its first FDIR rule.
  693. */
  694. if (vf->ctrl_vsi_idx != ICE_NO_VSI)
  695. ice_vf_ctrl_invalidate_vsi(vf);
  696. ice_vf_pre_vsi_rebuild(vf);
  697. ice_vf_rebuild_vsi(vf);
  698. ice_vf_post_vsi_rebuild(vf);
  699. ice_eswitch_attach_vf(pf, vf);
  700. mutex_unlock(&vf->cfg_lock);
  701. }
  702. ice_flush(hw);
  703. clear_bit(ICE_VF_DIS, pf->state);
  704. mutex_unlock(&pf->vfs.table_lock);
  705. }
  706. /**
  707. * ice_notify_vf_reset - Notify VF of a reset event
  708. * @vf: pointer to the VF structure
  709. */
  710. static void ice_notify_vf_reset(struct ice_vf *vf)
  711. {
  712. struct ice_hw *hw = &vf->pf->hw;
  713. struct virtchnl_pf_event pfe;
  714. /* Bail out if VF is in disabled state, neither initialized, nor active
  715. * state - otherwise proceed with notifications
  716. */
  717. if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
  718. !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
  719. test_bit(ICE_VF_STATE_DIS, vf->vf_states))
  720. return;
  721. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  722. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  723. ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
  724. VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
  725. NULL);
  726. }
  727. /**
  728. * ice_reset_vf - Reset a particular VF
  729. * @vf: pointer to the VF structure
  730. * @flags: flags controlling behavior of the reset
  731. *
  732. * Flags:
  733. * ICE_VF_RESET_VFLR - Indicates a reset is due to VFLR event
  734. * ICE_VF_RESET_NOTIFY - Send VF a notification prior to reset
  735. * ICE_VF_RESET_LOCK - Acquire VF cfg_lock before resetting
  736. *
  737. * Returns 0 if the VF is currently in reset, if resets are disabled, or if
  738. * the VF resets successfully. Returns an error code if the VF fails to
  739. * rebuild.
  740. */
  741. int ice_reset_vf(struct ice_vf *vf, u32 flags)
  742. {
  743. struct ice_pf *pf = vf->pf;
  744. struct ice_lag *lag;
  745. struct ice_vsi *vsi;
  746. u8 act_prt, pri_prt;
  747. struct device *dev;
  748. int err = 0;
  749. bool rsd;
  750. dev = ice_pf_to_dev(pf);
  751. act_prt = ICE_LAG_INVALID_PORT;
  752. pri_prt = pf->hw.port_info->lport;
  753. if (flags & ICE_VF_RESET_NOTIFY)
  754. ice_notify_vf_reset(vf);
  755. if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
  756. dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
  757. vf->vf_id);
  758. return 0;
  759. }
  760. if (flags & ICE_VF_RESET_LOCK)
  761. mutex_lock(&vf->cfg_lock);
  762. else
  763. lockdep_assert_held(&vf->cfg_lock);
  764. lag = pf->lag;
  765. mutex_lock(&pf->lag_mutex);
  766. if (lag && lag->bonded && lag->primary) {
  767. act_prt = lag->active_port;
  768. if (act_prt != pri_prt && act_prt != ICE_LAG_INVALID_PORT &&
  769. lag->upper_netdev)
  770. ice_lag_move_vf_nodes_cfg(lag, act_prt, pri_prt);
  771. else
  772. act_prt = ICE_LAG_INVALID_PORT;
  773. }
  774. if (ice_is_vf_disabled(vf)) {
  775. vsi = ice_get_vf_vsi(vf);
  776. if (!vsi) {
  777. dev_dbg(dev, "VF is already removed\n");
  778. err = -EINVAL;
  779. goto out_unlock;
  780. }
  781. ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
  782. if (ice_vsi_is_rx_queue_active(vsi))
  783. ice_vsi_stop_all_rx_rings(vsi);
  784. dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
  785. vf->vf_id);
  786. goto out_unlock;
  787. }
  788. /* Set VF disable bit state here, before triggering reset */
  789. set_bit(ICE_VF_STATE_DIS, vf->vf_states);
  790. ice_trigger_vf_reset(vf, flags & ICE_VF_RESET_VFLR, false);
  791. vsi = ice_get_vf_vsi(vf);
  792. if (WARN_ON(!vsi)) {
  793. err = -EIO;
  794. goto out_unlock;
  795. }
  796. ice_dis_vf_qs(vf);
  797. /* Call Disable LAN Tx queue AQ whether or not queues are
  798. * enabled. This is needed for successful completion of VFR.
  799. */
  800. ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
  801. NULL, vf->vf_ops->reset_type, vf->vf_id, NULL);
  802. /* poll VPGEN_VFRSTAT reg to make sure
  803. * that reset is complete
  804. */
  805. rsd = vf->vf_ops->poll_reset_status(vf);
  806. /* Display a warning if VF didn't manage to reset in time, but need to
  807. * continue on with the operation.
  808. */
  809. if (!rsd)
  810. dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
  811. vf->driver_caps = 0;
  812. ice_vc_set_default_allowlist(vf);
  813. /* disable promiscuous modes in case they were enabled
  814. * ignore any error if disabling process failed
  815. */
  816. ice_vf_clear_all_promisc_modes(vf, vsi);
  817. ice_vf_fdir_exit(vf);
  818. ice_vf_fdir_init(vf);
  819. /* clean VF control VSI when resetting VF since it should be setup
  820. * only when VF creates its first FDIR rule.
  821. */
  822. if (vf->ctrl_vsi_idx != ICE_NO_VSI)
  823. ice_vf_ctrl_vsi_release(vf);
  824. ice_vf_pre_vsi_rebuild(vf);
  825. if (ice_vf_reconfig_vsi(vf)) {
  826. dev_err(dev, "Failed to release and setup the VF%u's VSI\n",
  827. vf->vf_id);
  828. err = -EFAULT;
  829. goto out_unlock;
  830. }
  831. ice_vf_post_vsi_rebuild(vf);
  832. vsi = ice_get_vf_vsi(vf);
  833. if (WARN_ON(!vsi)) {
  834. err = -EINVAL;
  835. goto out_unlock;
  836. }
  837. ice_eswitch_update_repr(&vf->repr_id, vsi);
  838. /* if the VF has been reset allow it to come up again */
  839. ice_reset_vf_mbx_cnt(vf);
  840. out_unlock:
  841. if (lag && lag->bonded && lag->primary &&
  842. act_prt != ICE_LAG_INVALID_PORT)
  843. ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
  844. mutex_unlock(&pf->lag_mutex);
  845. if (flags & ICE_VF_RESET_LOCK)
  846. mutex_unlock(&vf->cfg_lock);
  847. return err;
  848. }
  849. /**
  850. * ice_set_vf_state_dis - Set VF state to disabled
  851. * @vf: pointer to the VF structure
  852. */
  853. void ice_set_vf_state_dis(struct ice_vf *vf)
  854. {
  855. ice_set_vf_state_qs_dis(vf);
  856. vf->vf_ops->clear_reset_state(vf);
  857. }
  858. /* Private functions only accessed from other virtualization files */
  859. /**
  860. * ice_initialize_vf_entry - Initialize a VF entry
  861. * @vf: pointer to the VF structure
  862. */
  863. void ice_initialize_vf_entry(struct ice_vf *vf)
  864. {
  865. struct ice_pf *pf = vf->pf;
  866. struct ice_vfs *vfs;
  867. vfs = &pf->vfs;
  868. /* assign default capabilities */
  869. vf->spoofchk = true;
  870. ice_vc_set_default_allowlist(vf);
  871. ice_virtchnl_set_dflt_ops(vf);
  872. /* set default number of MSI-X */
  873. vf->num_msix = vfs->num_msix_per;
  874. vf->num_vf_qs = vfs->num_qps_per;
  875. /* ctrl_vsi_idx will be set to a valid value only when iAVF
  876. * creates its first fdir rule.
  877. */
  878. ice_vf_ctrl_invalidate_vsi(vf);
  879. ice_vf_fdir_init(vf);
  880. /* Initialize mailbox info for this VF */
  881. if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT))
  882. ice_mbx_vf_clear_cnt_e830(&pf->hw, vf->vf_id);
  883. else
  884. ice_mbx_init_vf_info(&pf->hw, &vf->mbx_info);
  885. mutex_init(&vf->cfg_lock);
  886. }
  887. void ice_deinitialize_vf_entry(struct ice_vf *vf)
  888. {
  889. struct ice_pf *pf = vf->pf;
  890. if (!ice_is_feature_supported(pf, ICE_F_MBX_LIMIT))
  891. list_del(&vf->mbx_info.list_entry);
  892. }
  893. /**
  894. * ice_dis_vf_qs - Disable the VF queues
  895. * @vf: pointer to the VF structure
  896. */
  897. void ice_dis_vf_qs(struct ice_vf *vf)
  898. {
  899. struct ice_vsi *vsi = ice_get_vf_vsi(vf);
  900. if (WARN_ON(!vsi))
  901. return;
  902. ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
  903. ice_vsi_stop_all_rx_rings(vsi);
  904. ice_set_vf_state_qs_dis(vf);
  905. }
  906. /**
  907. * ice_err_to_virt_err - translate errors for VF return code
  908. * @err: error return code
  909. */
  910. enum virtchnl_status_code ice_err_to_virt_err(int err)
  911. {
  912. switch (err) {
  913. case 0:
  914. return VIRTCHNL_STATUS_SUCCESS;
  915. case -EINVAL:
  916. case -ENODEV:
  917. return VIRTCHNL_STATUS_ERR_PARAM;
  918. case -ENOMEM:
  919. return VIRTCHNL_STATUS_ERR_NO_MEMORY;
  920. case -EALREADY:
  921. case -EBUSY:
  922. case -EIO:
  923. case -ENOSPC:
  924. return VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
  925. default:
  926. return VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
  927. }
  928. }
  929. /**
  930. * ice_check_vf_init - helper to check if VF init complete
  931. * @vf: the pointer to the VF to check
  932. */
  933. int ice_check_vf_init(struct ice_vf *vf)
  934. {
  935. struct ice_pf *pf = vf->pf;
  936. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  937. dev_err(ice_pf_to_dev(pf), "VF ID: %u in reset. Try again.\n",
  938. vf->vf_id);
  939. return -EBUSY;
  940. }
  941. return 0;
  942. }
  943. /**
  944. * ice_vf_get_port_info - Get the VF's port info structure
  945. * @vf: VF used to get the port info structure for
  946. */
  947. struct ice_port_info *ice_vf_get_port_info(struct ice_vf *vf)
  948. {
  949. return vf->pf->hw.port_info;
  950. }
  951. /**
  952. * ice_cfg_mac_antispoof - Configure MAC antispoof checking behavior
  953. * @vsi: the VSI to configure
  954. * @enable: whether to enable or disable the spoof checking
  955. *
  956. * Configure a VSI to enable (or disable) spoof checking behavior.
  957. */
  958. static int ice_cfg_mac_antispoof(struct ice_vsi *vsi, bool enable)
  959. {
  960. struct ice_vsi_ctx *ctx;
  961. int err;
  962. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  963. if (!ctx)
  964. return -ENOMEM;
  965. ctx->info.sec_flags = vsi->info.sec_flags;
  966. ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
  967. if (enable)
  968. ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
  969. else
  970. ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
  971. err = ice_update_vsi(&vsi->back->hw, vsi->idx, ctx, NULL);
  972. if (err)
  973. dev_err(ice_pf_to_dev(vsi->back), "Failed to configure Tx MAC anti-spoof %s for VSI %d, error %d\n",
  974. enable ? "ON" : "OFF", vsi->vsi_num, err);
  975. else
  976. vsi->info.sec_flags = ctx->info.sec_flags;
  977. kfree(ctx);
  978. return err;
  979. }
  980. /**
  981. * ice_vsi_ena_spoofchk - enable Tx spoof checking for this VSI
  982. * @vsi: VSI to enable Tx spoof checking for
  983. */
  984. static int ice_vsi_ena_spoofchk(struct ice_vsi *vsi)
  985. {
  986. struct ice_vsi_vlan_ops *vlan_ops;
  987. int err = 0;
  988. vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
  989. /* Allow VF with VLAN 0 only to send all tagged traffic */
  990. if (vsi->type != ICE_VSI_VF || ice_vsi_has_non_zero_vlans(vsi)) {
  991. err = vlan_ops->ena_tx_filtering(vsi);
  992. if (err)
  993. return err;
  994. }
  995. return ice_cfg_mac_antispoof(vsi, true);
  996. }
  997. /**
  998. * ice_vsi_dis_spoofchk - disable Tx spoof checking for this VSI
  999. * @vsi: VSI to disable Tx spoof checking for
  1000. */
  1001. static int ice_vsi_dis_spoofchk(struct ice_vsi *vsi)
  1002. {
  1003. struct ice_vsi_vlan_ops *vlan_ops;
  1004. int err;
  1005. vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
  1006. err = vlan_ops->dis_tx_filtering(vsi);
  1007. if (err)
  1008. return err;
  1009. return ice_cfg_mac_antispoof(vsi, false);
  1010. }
  1011. /**
  1012. * ice_vsi_apply_spoofchk - Apply Tx spoof checking setting to a VSI
  1013. * @vsi: VSI associated to the VF
  1014. * @enable: whether to enable or disable the spoof checking
  1015. */
  1016. int ice_vsi_apply_spoofchk(struct ice_vsi *vsi, bool enable)
  1017. {
  1018. int err;
  1019. if (enable)
  1020. err = ice_vsi_ena_spoofchk(vsi);
  1021. else
  1022. err = ice_vsi_dis_spoofchk(vsi);
  1023. return err;
  1024. }
  1025. /**
  1026. * ice_is_vf_trusted
  1027. * @vf: pointer to the VF info
  1028. */
  1029. bool ice_is_vf_trusted(struct ice_vf *vf)
  1030. {
  1031. return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  1032. }
  1033. /**
  1034. * ice_vf_has_no_qs_ena - check if the VF has any Rx or Tx queues enabled
  1035. * @vf: the VF to check
  1036. *
  1037. * Returns true if the VF has no Rx and no Tx queues enabled and returns false
  1038. * otherwise
  1039. */
  1040. bool ice_vf_has_no_qs_ena(struct ice_vf *vf)
  1041. {
  1042. return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
  1043. !bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF));
  1044. }
  1045. /**
  1046. * ice_is_vf_link_up - check if the VF's link is up
  1047. * @vf: VF to check if link is up
  1048. */
  1049. bool ice_is_vf_link_up(struct ice_vf *vf)
  1050. {
  1051. struct ice_port_info *pi = ice_vf_get_port_info(vf);
  1052. if (ice_check_vf_init(vf))
  1053. return false;
  1054. if (ice_vf_has_no_qs_ena(vf))
  1055. return false;
  1056. else if (vf->link_forced)
  1057. return vf->link_up;
  1058. else
  1059. return pi->phy.link_info.link_info &
  1060. ICE_AQ_LINK_UP;
  1061. }
  1062. /**
  1063. * ice_vf_ctrl_invalidate_vsi - invalidate ctrl_vsi_idx to remove VSI access
  1064. * @vf: VF that control VSI is being invalidated on
  1065. */
  1066. void ice_vf_ctrl_invalidate_vsi(struct ice_vf *vf)
  1067. {
  1068. vf->ctrl_vsi_idx = ICE_NO_VSI;
  1069. }
  1070. /**
  1071. * ice_vf_ctrl_vsi_release - invalidate the VF's control VSI after freeing it
  1072. * @vf: VF that control VSI is being released on
  1073. */
  1074. void ice_vf_ctrl_vsi_release(struct ice_vf *vf)
  1075. {
  1076. ice_vsi_release(vf->pf->vsi[vf->ctrl_vsi_idx]);
  1077. ice_vf_ctrl_invalidate_vsi(vf);
  1078. }
  1079. /**
  1080. * ice_vf_ctrl_vsi_setup - Set up a VF control VSI
  1081. * @vf: VF to setup control VSI for
  1082. *
  1083. * Returns pointer to the successfully allocated VSI struct on success,
  1084. * otherwise returns NULL on failure.
  1085. */
  1086. struct ice_vsi *ice_vf_ctrl_vsi_setup(struct ice_vf *vf)
  1087. {
  1088. struct ice_vsi_cfg_params params = {};
  1089. struct ice_pf *pf = vf->pf;
  1090. struct ice_vsi *vsi;
  1091. params.type = ICE_VSI_CTRL;
  1092. params.port_info = ice_vf_get_port_info(vf);
  1093. params.vf = vf;
  1094. params.flags = ICE_VSI_FLAG_INIT;
  1095. vsi = ice_vsi_setup(pf, &params);
  1096. if (!vsi) {
  1097. dev_err(ice_pf_to_dev(pf), "Failed to create VF control VSI\n");
  1098. ice_vf_ctrl_invalidate_vsi(vf);
  1099. }
  1100. return vsi;
  1101. }
  1102. /**
  1103. * ice_vf_init_host_cfg - Initialize host admin configuration
  1104. * @vf: VF to initialize
  1105. * @vsi: the VSI created at initialization
  1106. *
  1107. * Initialize the VF host configuration. Called during VF creation to setup
  1108. * VLAN 0, add the VF VSI broadcast filter, and setup spoof checking. It
  1109. * should only be called during VF creation.
  1110. */
  1111. int ice_vf_init_host_cfg(struct ice_vf *vf, struct ice_vsi *vsi)
  1112. {
  1113. struct ice_vsi_vlan_ops *vlan_ops;
  1114. struct ice_pf *pf = vf->pf;
  1115. u8 broadcast[ETH_ALEN];
  1116. struct device *dev;
  1117. int err;
  1118. dev = ice_pf_to_dev(pf);
  1119. err = ice_vsi_add_vlan_zero(vsi);
  1120. if (err) {
  1121. dev_warn(dev, "Failed to add VLAN 0 filter for VF %d\n",
  1122. vf->vf_id);
  1123. return err;
  1124. }
  1125. vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
  1126. err = vlan_ops->ena_rx_filtering(vsi);
  1127. if (err) {
  1128. dev_warn(dev, "Failed to enable Rx VLAN filtering for VF %d\n",
  1129. vf->vf_id);
  1130. return err;
  1131. }
  1132. eth_broadcast_addr(broadcast);
  1133. err = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
  1134. if (err) {
  1135. dev_err(dev, "Failed to add broadcast MAC filter for VF %d, status %d\n",
  1136. vf->vf_id, err);
  1137. return err;
  1138. }
  1139. vf->num_mac = 1;
  1140. err = ice_vsi_apply_spoofchk(vsi, vf->spoofchk);
  1141. if (err) {
  1142. dev_warn(dev, "Failed to initialize spoofchk setting for VF %d\n",
  1143. vf->vf_id);
  1144. return err;
  1145. }
  1146. return 0;
  1147. }
  1148. /**
  1149. * ice_vf_invalidate_vsi - invalidate vsi_idx to remove VSI access
  1150. * @vf: VF to remove access to VSI for
  1151. */
  1152. void ice_vf_invalidate_vsi(struct ice_vf *vf)
  1153. {
  1154. vf->lan_vsi_idx = ICE_NO_VSI;
  1155. }
  1156. /**
  1157. * ice_vf_vsi_release - Release the VF VSI and invalidate indexes
  1158. * @vf: pointer to the VF structure
  1159. *
  1160. * Release the VF associated with this VSI and then invalidate the VSI
  1161. * indexes.
  1162. */
  1163. void ice_vf_vsi_release(struct ice_vf *vf)
  1164. {
  1165. struct ice_vsi *vsi = ice_get_vf_vsi(vf);
  1166. if (WARN_ON(!vsi))
  1167. return;
  1168. ice_vsi_release(vsi);
  1169. ice_vf_invalidate_vsi(vf);
  1170. }
  1171. /**
  1172. * ice_get_vf_ctrl_vsi - Get first VF control VSI pointer
  1173. * @pf: the PF private structure
  1174. * @vsi: pointer to the VSI
  1175. *
  1176. * Return first found VF control VSI other than the vsi
  1177. * passed by parameter. This function is used to determine
  1178. * whether new resources have to be allocated for control VSI
  1179. * or they can be shared with existing one.
  1180. *
  1181. * Return found VF control VSI pointer other itself. Return
  1182. * NULL Otherwise.
  1183. *
  1184. */
  1185. struct ice_vsi *ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi)
  1186. {
  1187. struct ice_vsi *ctrl_vsi = NULL;
  1188. struct ice_vf *vf;
  1189. unsigned int bkt;
  1190. rcu_read_lock();
  1191. ice_for_each_vf_rcu(pf, bkt, vf) {
  1192. if (vf != vsi->vf && vf->ctrl_vsi_idx != ICE_NO_VSI) {
  1193. ctrl_vsi = pf->vsi[vf->ctrl_vsi_idx];
  1194. break;
  1195. }
  1196. }
  1197. rcu_read_unlock();
  1198. return ctrl_vsi;
  1199. }