blob: 6f97ed471fe9a190cf82f10dfc0ed0f1b3942f3b [file] [log] [blame]
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07001// SPDX-License-Identifier: GPL-2.0
Paul Greenwaltba1124f2023-10-25 14:41:52 -07002/* Copyright (c) 2018-2023, Intel Corporation. */
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07003
4/* Intel(R) Ethernet Connection E800 Series Linux Driver */
5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
Jeff Kirsher34a2a3b82020-05-29 00:18:33 -07008#include <generated/utsrelease.h>
Jesse Brandeburg0288c3e2023-10-11 16:33:33 -07009#include <linux/crash_dump.h>
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -070010#include "ice.h"
Anirudh Venkataramananeff380a2019-10-24 01:11:17 -070011#include "ice_base.h"
Anirudh Venkataramanan45d3d422018-09-19 17:23:04 -070012#include "ice_lib.h"
Michal Swiatkowski1b8f15b2020-05-07 17:41:08 -070013#include "ice_fltr.h"
Anirudh Venkataramanan37b6f642019-02-28 15:24:22 -080014#include "ice_dcb_lib.h"
Dave Ertmanb94b0132019-11-06 02:05:29 -080015#include "ice_dcb_nl.h"
Michal Swiatkowski0545cc82024-03-25 22:34:31 +010016#include "devlink/devlink.h"
Piotr Raczynski4ebc5f22024-03-25 22:34:32 +010017#include "devlink/devlink_port.h"
Konrad Knitter4da71a72023-12-01 10:08:39 -080018#include "ice_hwmon.h"
Jesse Brandeburg3089cf62021-06-08 16:35:17 -070019/* Including ice_trace.h with CREATE_TRACE_POINTS defined will generate the
20 * ice tracepoint functions. This must be done exactly once across the
21 * ice driver.
22 */
23#define CREATE_TRACE_POINTS
24#include "ice_trace.h"
Grzegorz Nitkab3be9182021-08-19 17:08:57 -070025#include "ice_eswitch.h"
Kiran Patil0d08a442021-08-06 10:49:05 +020026#include "ice_tc_lib.h"
Brett Creeleyc31af682021-12-02 08:38:46 -080027#include "ice_vsi_vlan_ops.h"
Marek Majtyka66c0e132023-02-01 11:24:18 +010028#include <net/xdp_sock_drv.h>
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -070029
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -070030#define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -070031static const char ice_driver_string[] = DRV_SUMMARY;
32static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
33
Tony Nguyen462acf6a2019-09-09 06:47:46 -070034/* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
35#define ICE_DDP_PKG_PATH "intel/ice/ddp/"
36#define ICE_DDP_PKG_FILE ICE_DDP_PKG_PATH "ice.pkg"
37
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -070038MODULE_DESCRIPTION(DRV_SUMMARY);
Alexander Lobakin306ec722024-04-18 13:36:07 +020039MODULE_IMPORT_NS(LIBIE);
Jesse Brandeburg98674eb2018-09-14 17:37:57 -070040MODULE_LICENSE("GPL v2");
Tony Nguyen462acf6a2019-09-09 06:47:46 -070041MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -070042
43static int debug = -1;
44module_param(debug, int, 0644);
Anirudh Venkataramanan7ec59ee2018-03-20 07:58:06 -070045#ifndef CONFIG_DYNAMIC_DEBUG
46MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
47#else
48MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
49#endif /* !CONFIG_DYNAMIC_DEBUG */
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -070050
Maciej Fijalkowski22bf8772021-08-19 14:00:03 +020051DEFINE_STATIC_KEY_FALSE(ice_xdp_locking_key);
52EXPORT_SYMBOL(ice_xdp_locking_key);
Dave Ertmand25a0fc2021-05-20 09:37:49 -050053
Jacob Keller649c87c2022-02-22 16:26:50 -080054/**
55 * ice_hw_to_dev - Get device pointer from the hardware structure
56 * @hw: pointer to the device HW structure
57 *
58 * Used to access the device pointer from compilation units which can't easily
59 * include the definition of struct ice_pf without leading to circular header
60 * dependencies.
61 */
62struct device *ice_hw_to_dev(struct ice_hw *hw)
63{
64 struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
65
66 return &pf->pdev->dev;
67}
68
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -070069static struct workqueue_struct *ice_wq;
Dave Ertmanbb52f422023-06-20 15:18:46 -070070struct workqueue_struct *ice_lag_wq;
Tony Nguyen462acf6a2019-09-09 06:47:46 -070071static const struct net_device_ops ice_netdev_safe_mode_ops;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -070072static const struct net_device_ops ice_netdev_ops;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -070073
Tony Nguyen462acf6a2019-09-09 06:47:46 -070074static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
Anirudh Venkataramanan28c2a642018-09-19 17:23:07 -070075
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -070076static void ice_vsi_release_all(struct ice_pf *pf);
Anirudh Venkataramanan3a858ba2018-03-20 07:58:11 -070077
Kiran Patilfbc7b272021-10-15 16:35:16 -070078static int ice_rebuild_channels(struct ice_pf *pf);
79static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_adv_fltr);
80
Michal Swiatkowski195bb482021-10-12 11:31:03 -070081static int
82ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
83 void *cb_priv, enum tc_setup_type type, void *type_data,
84 void *data,
85 void (*cleanup)(struct flow_block_cb *block_cb));
86
Wojciech Drewekf6e8fb52023-07-12 13:03:31 +020087bool netif_is_ice(const struct net_device *dev)
Dave Ertmandf006dd2020-11-20 16:39:26 -080088{
89 return dev && (dev->netdev_ops == &ice_netdev_ops);
90}
91
Anirudh Venkataramanan3a858ba2018-03-20 07:58:11 -070092/**
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -070093 * ice_get_tx_pending - returns number of Tx descriptors not processed
94 * @ring: the ring of descriptors
95 */
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +020096static u16 ice_get_tx_pending(struct ice_tx_ring *ring)
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -070097{
Brett Creeleyc1ddf1f2019-07-25 01:55:28 -070098 u16 head, tail;
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -070099
100 head = ring->next_to_clean;
Brett Creeleyc1ddf1f2019-07-25 01:55:28 -0700101 tail = ring->next_to_use;
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -0700102
103 if (head != tail)
104 return (head < tail) ?
105 tail - head : (tail + ring->count - head);
106 return 0;
107}
108
109/**
110 * ice_check_for_hang_subtask - check for and recover hung queues
111 * @pf: pointer to PF struct
112 */
113static void ice_check_for_hang_subtask(struct ice_pf *pf)
114{
115 struct ice_vsi *vsi = NULL;
Brett Creeleye89e8992019-04-16 10:30:51 -0700116 struct ice_hw *hw;
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -0700117 unsigned int i;
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -0700118 int packets;
Brett Creeleye89e8992019-04-16 10:30:51 -0700119 u32 v;
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -0700120
121 ice_for_each_vsi(pf, v)
122 if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
123 vsi = pf->vsi[v];
124 break;
125 }
126
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -0800127 if (!vsi || test_bit(ICE_VSI_DOWN, vsi->state))
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -0700128 return;
129
130 if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
131 return;
132
Brett Creeleye89e8992019-04-16 10:30:51 -0700133 hw = &vsi->back->hw;
134
Maciej Fijalkowski2faf63b2021-08-19 14:00:04 +0200135 ice_for_each_txq(vsi, i) {
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +0200136 struct ice_tx_ring *tx_ring = vsi->tx_rings[i];
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -0500137 struct ice_ring_stats *ring_stats;
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -0700138
Kiran Patilfbc7b272021-10-15 16:35:16 -0700139 if (!tx_ring)
140 continue;
141 if (ice_ring_ch_enabled(tx_ring))
142 continue;
143
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -0500144 ring_stats = tx_ring->ring_stats;
145 if (!ring_stats)
146 continue;
147
Kiran Patilfbc7b272021-10-15 16:35:16 -0700148 if (tx_ring->desc) {
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -0700149 /* If packet counter has not changed the queue is
150 * likely stalled, so force an interrupt for this
151 * queue.
152 *
153 * prev_pkt would be negative if there was no
154 * pending work.
155 */
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -0500156 packets = ring_stats->stats.pkts & INT_MAX;
157 if (ring_stats->tx_stats.prev_pkt == packets) {
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -0700158 /* Trigger sw interrupt to revive the queue */
Brett Creeleye89e8992019-04-16 10:30:51 -0700159 ice_trigger_sw_intr(hw, tx_ring->q_vector);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -0700160 continue;
161 }
162
163 /* Memory barrier between read of packet count and call
164 * to ice_get_tx_pending()
165 */
166 smp_rmb();
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -0500167 ring_stats->tx_stats.prev_pkt =
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -0700168 ice_get_tx_pending(tx_ring) ? packets : -1;
169 }
170 }
171}
172
173/**
Tony Nguyen561f4372019-04-16 10:34:50 -0700174 * ice_init_mac_fltr - Set initial MAC filters
175 * @pf: board private structure
176 *
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -0700177 * Set initial set of MAC filters for PF VSI; configure filters for permanent
Tony Nguyen561f4372019-04-16 10:34:50 -0700178 * address and broadcast address. If an error is encountered, netdevice will be
179 * unregistered.
180 */
181static int ice_init_mac_fltr(struct ice_pf *pf)
182{
Tony Nguyen561f4372019-04-16 10:34:50 -0700183 struct ice_vsi *vsi;
Michal Swiatkowski1b8f15b2020-05-07 17:41:08 -0700184 u8 *perm_addr;
Tony Nguyen561f4372019-04-16 10:34:50 -0700185
Anirudh Venkataramanan208ff752019-08-08 07:39:33 -0700186 vsi = ice_get_main_vsi(pf);
Tony Nguyen561f4372019-04-16 10:34:50 -0700187 if (!vsi)
188 return -EINVAL;
189
Michal Swiatkowski1b8f15b2020-05-07 17:41:08 -0700190 perm_addr = vsi->port_info->mac.perm_addr;
Tony Nguyenc1484692021-10-07 16:01:58 -0700191 return ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
Tony Nguyen561f4372019-04-16 10:34:50 -0700192}
193
194/**
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -0800195 * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700196 * @netdev: the net device on which the sync is happening
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -0800197 * @addr: MAC address to sync
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700198 *
199 * This is a callback function which is called by the in kernel device sync
200 * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
201 * populates the tmp_sync_list, which is later used by ice_add_mac to add the
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -0800202 * MAC filters from the hardware.
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700203 */
204static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
205{
206 struct ice_netdev_priv *np = netdev_priv(netdev);
207 struct ice_vsi *vsi = np->vsi;
208
Michal Swiatkowski1b8f15b2020-05-07 17:41:08 -0700209 if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
210 ICE_FWD_TO_VSI))
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700211 return -EINVAL;
212
213 return 0;
214}
215
216/**
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -0800217 * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700218 * @netdev: the net device on which the unsync is happening
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -0800219 * @addr: MAC address to unsync
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700220 *
221 * This is a callback function which is called by the in kernel device unsync
222 * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
223 * populates the tmp_unsync_list, which is later used by ice_remove_mac to
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -0800224 * delete the MAC filters from the hardware.
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700225 */
226static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
227{
228 struct ice_netdev_priv *np = netdev_priv(netdev);
229 struct ice_vsi *vsi = np->vsi;
230
Brett Creeley3ba7f532021-08-06 09:51:27 -0700231 /* Under some circumstances, we might receive a request to delete our
232 * own device address from our uc list. Because we store the device
233 * address in the VSI's MAC filter list, we need to ignore such
234 * requests and not delete our device address from this list.
235 */
236 if (ether_addr_equal(addr, netdev->dev_addr))
237 return 0;
238
Michal Swiatkowski1b8f15b2020-05-07 17:41:08 -0700239 if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
240 ICE_FWD_TO_VSI))
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700241 return -EINVAL;
242
243 return 0;
244}
245
246/**
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700247 * ice_vsi_fltr_changed - check if filter state changed
248 * @vsi: VSI to be checked
249 *
250 * returns true if filter state has changed, false otherwise.
251 */
252static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
253{
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -0800254 return test_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state) ||
Ivan Vecera1273f892022-03-31 09:20:08 -0700255 test_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700256}
257
258/**
Brett Creeleyfabf4802021-03-02 10:15:34 -0800259 * ice_set_promisc - Enable promiscuous mode for a given PF
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -0800260 * @vsi: the VSI being configured
261 * @promisc_m: mask of promiscuous config bits
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -0800262 *
263 */
Brett Creeleyfabf4802021-03-02 10:15:34 -0800264static int ice_set_promisc(struct ice_vsi *vsi, u8 promisc_m)
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -0800265{
Tony Nguyen5e24d592021-10-07 15:56:57 -0700266 int status;
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -0800267
268 if (vsi->type != ICE_VSI_PF)
269 return 0;
270
Ivan Vecera1273f892022-03-31 09:20:08 -0700271 if (ice_vsi_has_non_zero_vlans(vsi)) {
272 promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
273 status = ice_fltr_set_vlan_vsi_promisc(&vsi->back->hw, vsi,
274 promisc_m);
275 } else {
276 status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
277 promisc_m, 0);
278 }
Grzegorz Siwikabddafd2022-08-12 15:25:49 +0200279 if (status && status != -EEXIST)
280 return status;
Ivan Vecera1273f892022-03-31 09:20:08 -0700281
Jesse Brandeburg43fbca02023-02-06 15:54:36 -0800282 netdev_dbg(vsi->netdev, "set promisc filter bits for VSI %i: 0x%x\n",
283 vsi->vsi_num, promisc_m);
Grzegorz Siwikabddafd2022-08-12 15:25:49 +0200284 return 0;
Brett Creeleyfabf4802021-03-02 10:15:34 -0800285}
286
287/**
288 * ice_clear_promisc - Disable promiscuous mode for a given PF
289 * @vsi: the VSI being configured
290 * @promisc_m: mask of promiscuous config bits
291 *
292 */
293static int ice_clear_promisc(struct ice_vsi *vsi, u8 promisc_m)
294{
Tony Nguyen5e24d592021-10-07 15:56:57 -0700295 int status;
Brett Creeleyfabf4802021-03-02 10:15:34 -0800296
297 if (vsi->type != ICE_VSI_PF)
298 return 0;
299
Ivan Vecera1273f892022-03-31 09:20:08 -0700300 if (ice_vsi_has_non_zero_vlans(vsi)) {
301 promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
302 status = ice_fltr_clear_vlan_vsi_promisc(&vsi->back->hw, vsi,
303 promisc_m);
304 } else {
305 status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
306 promisc_m, 0);
307 }
308
Jesse Brandeburg43fbca02023-02-06 15:54:36 -0800309 netdev_dbg(vsi->netdev, "clear promisc filter bits for VSI %i: 0x%x\n",
310 vsi->vsi_num, promisc_m);
Tony Nguyenc1484692021-10-07 16:01:58 -0700311 return status;
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -0800312}
313
314/**
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700315 * ice_vsi_sync_fltr - Update the VSI filter list to the HW
316 * @vsi: ptr to the VSI
317 *
318 * Push any outstanding VSI filter changes through the AdminQ.
319 */
320static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
321{
Brett Creeleyc31af682021-12-02 08:38:46 -0800322 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
Anirudh Venkataramanan9a946842020-02-06 01:20:09 -0800323 struct device *dev = ice_pf_to_dev(vsi->back);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700324 struct net_device *netdev = vsi->netdev;
325 bool promisc_forced_on = false;
326 struct ice_pf *pf = vsi->back;
327 struct ice_hw *hw = &pf->hw;
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700328 u32 changed_flags = 0;
Tony Nguyen2ccc1c12021-10-07 16:00:23 -0700329 int err;
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700330
331 if (!vsi->netdev)
332 return -EINVAL;
333
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800334 while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700335 usleep_range(1000, 2000);
336
337 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
338 vsi->current_netdev_flags = vsi->netdev->flags;
339
340 INIT_LIST_HEAD(&vsi->tmp_sync_list);
341 INIT_LIST_HEAD(&vsi->tmp_unsync_list);
342
343 if (ice_vsi_fltr_changed(vsi)) {
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -0800344 clear_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
345 clear_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700346
347 /* grab the netdev's addr_list_lock */
348 netif_addr_lock_bh(netdev);
349 __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
350 ice_add_mac_to_unsync_list);
351 __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
352 ice_add_mac_to_unsync_list);
353 /* our temp lists are populated. release lock */
354 netif_addr_unlock_bh(netdev);
355 }
356
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -0800357 /* Remove MAC addresses in the unsync list */
Tony Nguyen2ccc1c12021-10-07 16:00:23 -0700358 err = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
Michal Swiatkowski1b8f15b2020-05-07 17:41:08 -0700359 ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
Tony Nguyen2ccc1c12021-10-07 16:00:23 -0700360 if (err) {
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700361 netdev_err(netdev, "Failed to delete MAC filters\n");
362 /* if we failed because of alloc failures, just bail */
Tony Nguyen2ccc1c12021-10-07 16:00:23 -0700363 if (err == -ENOMEM)
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700364 goto out;
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700365 }
366
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -0800367 /* Add MAC addresses in the sync list */
Tony Nguyen2ccc1c12021-10-07 16:00:23 -0700368 err = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
Michal Swiatkowski1b8f15b2020-05-07 17:41:08 -0700369 ice_fltr_free_list(dev, &vsi->tmp_sync_list);
Preethi Banala89f3e4a2019-02-19 15:04:04 -0800370 /* If filter is added successfully or already exists, do not go into
371 * 'if' condition and report it as error. Instead continue processing
372 * rest of the function.
373 */
Tony Nguyen2ccc1c12021-10-07 16:00:23 -0700374 if (err && err != -EEXIST) {
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700375 netdev_err(netdev, "Failed to add MAC filters\n");
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -0800376 /* If there is no more space for new umac filters, VSI
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700377 * should go into promiscuous mode. There should be some
378 * space reserved for promiscuous filters.
379 */
380 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800381 !test_and_set_bit(ICE_FLTR_OVERFLOW_PROMISC,
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700382 vsi->state)) {
383 promisc_forced_on = true;
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -0800384 netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700385 vsi->vsi_num);
386 } else {
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700387 goto out;
388 }
389 }
Tony Nguyen2ccc1c12021-10-07 16:00:23 -0700390 err = 0;
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700391 /* check for changes in promiscuous modes */
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -0800392 if (changed_flags & IFF_ALLMULTI) {
393 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
Ivan Vecera1273f892022-03-31 09:20:08 -0700394 err = ice_set_promisc(vsi, ICE_MCAST_PROMISC_BITS);
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -0800395 if (err) {
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -0800396 vsi->current_netdev_flags &= ~IFF_ALLMULTI;
397 goto out_promisc;
398 }
Bruce Allan92ace482020-05-07 17:41:10 -0700399 } else {
400 /* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
Ivan Vecera1273f892022-03-31 09:20:08 -0700401 err = ice_clear_promisc(vsi, ICE_MCAST_PROMISC_BITS);
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -0800402 if (err) {
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -0800403 vsi->current_netdev_flags |= IFF_ALLMULTI;
404 goto out_promisc;
405 }
406 }
407 }
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700408
409 if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -0800410 test_bit(ICE_VSI_PROMISC_CHANGED, vsi->state)) {
411 clear_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700412 if (vsi->current_netdev_flags & IFF_PROMISC) {
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -0800413 /* Apply Rx filter rule to get traffic from wire */
Michal Wilczynskid7393422022-07-04 15:12:26 +0200414 if (!ice_is_dflt_vsi_in_use(vsi->port_info)) {
415 err = ice_set_dflt_vsi(vsi);
Brett Creeleyfc0f39b2019-12-12 03:12:55 -0800416 if (err && err != -EEXIST) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -0800417 netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
Brett Creeleyfc0f39b2019-12-12 03:12:55 -0800418 err, vsi->vsi_num);
419 vsi->current_netdev_flags &=
420 ~IFF_PROMISC;
421 goto out_promisc;
422 }
Tony Nguyen2ccc1c12021-10-07 16:00:23 -0700423 err = 0;
Brett Creeleyc31af682021-12-02 08:38:46 -0800424 vlan_ops->dis_rx_filtering(vsi);
Jesse Brandeburg43fbca02023-02-06 15:54:36 -0800425
426 /* promiscuous mode implies allmulticast so
427 * that VSIs that are in promiscuous mode are
428 * subscribed to multicast packets coming to
429 * the port
430 */
431 err = ice_set_promisc(vsi,
432 ICE_MCAST_PROMISC_BITS);
433 if (err)
434 goto out_promisc;
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700435 }
436 } else {
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -0800437 /* Clear Rx filter to remove traffic from wire */
Michal Wilczynskid7393422022-07-04 15:12:26 +0200438 if (ice_is_vsi_dflt_vsi(vsi)) {
439 err = ice_clear_dflt_vsi(vsi);
Brett Creeleyfc0f39b2019-12-12 03:12:55 -0800440 if (err) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -0800441 netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
Brett Creeleyfc0f39b2019-12-12 03:12:55 -0800442 err, vsi->vsi_num);
443 vsi->current_netdev_flags |=
444 IFF_PROMISC;
445 goto out_promisc;
446 }
Jian Shen7dc839f2022-07-29 18:17:54 +0800447 if (vsi->netdev->features &
Brett Creeley1babaf72021-12-02 08:38:50 -0800448 NETIF_F_HW_VLAN_CTAG_FILTER)
Brett Creeleyc31af682021-12-02 08:38:46 -0800449 vlan_ops->ena_rx_filtering(vsi);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700450 }
Jesse Brandeburg43fbca02023-02-06 15:54:36 -0800451
452 /* disable allmulti here, but only if allmulti is not
453 * still enabled for the netdev
454 */
455 if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) {
456 err = ice_clear_promisc(vsi,
457 ICE_MCAST_PROMISC_BITS);
458 if (err) {
459 netdev_err(netdev, "Error %d clearing multicast promiscuous on VSI %i\n",
460 err, vsi->vsi_num);
461 }
462 }
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700463 }
464 }
465 goto exit;
466
467out_promisc:
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -0800468 set_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700469 goto exit;
470out:
471 /* if something went wrong then set the changed flag so we try again */
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -0800472 set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
473 set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700474exit:
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800475 clear_bit(ICE_CFG_BUSY, vsi->state);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700476 return err;
477}
478
479/**
480 * ice_sync_fltr_subtask - Sync the VSI filter list with HW
481 * @pf: board private structure
482 */
483static void ice_sync_fltr_subtask(struct ice_pf *pf)
484{
485 int v;
486
487 if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
488 return;
489
490 clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
491
Brett Creeley80ed404a2019-02-08 12:50:54 -0800492 ice_for_each_vsi(pf, v)
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -0700493 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
494 ice_vsi_sync_fltr(pf->vsi[v])) {
495 /* come back and try again later */
496 set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
497 break;
498 }
499}
500
501/**
Anirudh Venkataramanan7b9ffc72019-02-28 15:24:24 -0800502 * ice_pf_dis_all_vsi - Pause all VSIs on a PF
503 * @pf: the PF
504 * @locked: is the rtnl_lock already held
505 */
Anirudh Venkataramanan7b9ffc72019-02-28 15:24:24 -0800506static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
Anirudh Venkataramanan7b9ffc72019-02-28 15:24:24 -0800507{
Kiran Patilb126bd62020-11-20 16:39:27 -0800508 int node;
Anirudh Venkataramanan7b9ffc72019-02-28 15:24:24 -0800509 int v;
510
511 ice_for_each_vsi(pf, v)
512 if (pf->vsi[v])
513 ice_dis_vsi(pf->vsi[v], locked);
Kiran Patilb126bd62020-11-20 16:39:27 -0800514
515 for (node = 0; node < ICE_MAX_PF_AGG_NODES; node++)
516 pf->pf_agg_node[node].num_vsis = 0;
517
518 for (node = 0; node < ICE_MAX_VF_AGG_NODES; node++)
519 pf->vf_agg_node[node].num_vsis = 0;
Anirudh Venkataramanan7b9ffc72019-02-28 15:24:24 -0800520}
521
522/**
Wojciech Drewekc1e5da52021-10-26 12:38:40 +0200523 * ice_clear_sw_switch_recipes - clear switch recipes
524 * @pf: board private structure
525 *
526 * Mark switch recipes as not created in sw structures. There are cases where
527 * rules (especially advanced rules) need to be restored, either re-read from
528 * hardware or added again. For example after the reset. 'recp_created' flag
529 * prevents from doing that and need to be cleared upfront.
530 */
531static void ice_clear_sw_switch_recipes(struct ice_pf *pf)
532{
533 struct ice_sw_recipe *recp;
534 u8 i;
535
536 recp = pf->hw.switch_info->recp_list;
537 for (i = 0; i < ICE_MAX_NUM_RECIPES; i++)
538 recp[i].recp_created = false;
539}
540
541/**
Kiran Patilfbc7b272021-10-15 16:35:16 -0700542 * ice_prepare_for_reset - prep for reset
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700543 * @pf: board private structure
Kiran Patilfbc7b272021-10-15 16:35:16 -0700544 * @reset_type: reset type requested
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700545 *
546 * Inform or close all dependent features in prep for reset.
547 */
548static void
Kiran Patilfbc7b272021-10-15 16:35:16 -0700549ice_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700550{
551 struct ice_hw *hw = &pf->hw;
Kiran Patilfbc7b272021-10-15 16:35:16 -0700552 struct ice_vsi *vsi;
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -0800553 struct ice_vf *vf;
554 unsigned int bkt;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700555
Kiran Patilfbc7b272021-10-15 16:35:16 -0700556 dev_dbg(ice_pf_to_dev(pf), "reset_type=%d\n", reset_type);
557
Brett Creeley5abac9d2019-02-13 10:51:14 -0800558 /* already prepared for reset */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800559 if (test_bit(ICE_PREPARED_FOR_RESET, pf->state))
Brett Creeley5abac9d2019-02-13 10:51:14 -0800560 return;
561
Grzegorz Nitka25a71232024-07-15 17:39:10 +0200562 synchronize_irq(pf->oicr_irq.virq);
563
Dave Ertmanf9f53012021-05-20 09:37:51 -0500564 ice_unplug_aux_dev(pf);
565
Anirudh Venkataramanan007676b2018-09-19 17:42:57 -0700566 /* Notify VFs of impending reset */
567 if (ice_check_sq_alive(hw, &hw->mailboxq))
568 ice_vc_notify_reset(pf);
569
Akeem G Abodunrinc7aeb4d2019-06-26 02:20:18 -0700570 /* Disable VFs until reset is completed */
Jacob Keller3d5985a2022-02-16 13:37:38 -0800571 mutex_lock(&pf->vfs.table_lock);
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -0800572 ice_for_each_vf(pf, bkt, vf)
Jacob Kellerfa4a15c2023-01-18 17:16:51 -0800573 ice_set_vf_state_dis(vf);
Jacob Keller3d5985a2022-02-16 13:37:38 -0800574 mutex_unlock(&pf->vfs.table_lock);
Akeem G Abodunrinc7aeb4d2019-06-26 02:20:18 -0700575
Wojciech Drewekc1e5da52021-10-26 12:38:40 +0200576 if (ice_is_eswitch_mode_switchdev(pf)) {
577 if (reset_type != ICE_RESET_PFR)
578 ice_clear_sw_switch_recipes(pf);
579 }
580
Kiran Patilfbc7b272021-10-15 16:35:16 -0700581 /* release ADQ specific HW and SW resources */
582 vsi = ice_get_main_vsi(pf);
583 if (!vsi)
584 goto skip;
585
586 /* to be on safe side, reset orig_rss_size so that normal flow
587 * of deciding rss_size can take precedence
588 */
589 vsi->orig_rss_size = 0;
590
591 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
592 if (reset_type == ICE_RESET_PFR) {
593 vsi->old_ena_tc = vsi->all_enatc;
594 vsi->old_numtc = vsi->all_numtc;
595 } else {
596 ice_remove_q_channels(vsi, true);
597
598 /* for other reset type, do not support channel rebuild
599 * hence reset needed info
600 */
601 vsi->old_ena_tc = 0;
602 vsi->all_enatc = 0;
603 vsi->old_numtc = 0;
604 vsi->all_numtc = 0;
605 vsi->req_txq = 0;
606 vsi->req_rxq = 0;
607 clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
608 memset(&vsi->mqprio_qopt, 0, sizeof(vsi->mqprio_qopt));
609 }
610 }
611skip:
612
Tony Nguyen462acf6a2019-09-09 06:47:46 -0700613 /* clear SW filtering DB */
614 ice_clear_hw_tbls(hw);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700615 /* disable the VSIs and their queues that are not already DOWN */
Anirudh Venkataramanan7b9ffc72019-02-28 15:24:24 -0800616 ice_pf_dis_all_vsi(pf, false);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700617
Jacob Keller06c16d82021-06-09 09:39:50 -0700618 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
Jacob Kellerc75d5e62024-01-25 13:57:50 -0800619 ice_ptp_prepare_for_reset(pf, reset_type);
Jacob Keller06c16d82021-06-09 09:39:50 -0700620
Karol Kolacinski43113ff2022-03-01 10:38:03 -0800621 if (ice_is_feature_supported(pf, ICE_F_GNSS))
622 ice_gnss_exit(pf);
623
Usha Ketinenic5a2a4a2018-10-26 11:44:35 -0700624 if (hw->port_info)
625 ice_sched_clear_port(hw->port_info);
626
Piotr Gardockifdd288e2024-06-14 12:38:11 +0200627 ice_shutdown_all_ctrlq(hw, false);
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700628
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800629 set_bit(ICE_PREPARED_FOR_RESET, pf->state);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700630}
631
632/**
633 * ice_do_reset - Initiate one of many types of resets
634 * @pf: board private structure
Kiran Patilfbc7b272021-10-15 16:35:16 -0700635 * @reset_type: reset type requested before this function was called.
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700636 */
637static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
638{
Brett Creeley4015d112019-11-08 06:23:26 -0800639 struct device *dev = ice_pf_to_dev(pf);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700640 struct ice_hw *hw = &pf->hw;
641
642 dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700643
Dave Ertman3579aa82023-06-20 15:18:54 -0700644 if (pf->lag && pf->lag->bonded && reset_type == ICE_RESET_PFR) {
645 dev_dbg(dev, "PFR on a bonded interface, promoting to CORER\n");
646 reset_type = ICE_RESET_CORER;
647 }
648
Kiran Patilfbc7b272021-10-15 16:35:16 -0700649 ice_prepare_for_reset(pf, reset_type);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700650
651 /* trigger the reset */
652 if (ice_reset(hw, reset_type)) {
653 dev_err(dev, "reset %d failed\n", reset_type);
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800654 set_bit(ICE_RESET_FAILED, pf->state);
655 clear_bit(ICE_RESET_OICR_RECV, pf->state);
656 clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
657 clear_bit(ICE_PFR_REQ, pf->state);
658 clear_bit(ICE_CORER_REQ, pf->state);
659 clear_bit(ICE_GLOBR_REQ, pf->state);
Jacob Keller1c080522021-05-06 08:39:59 -0700660 wake_up(&pf->reset_wait_queue);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700661 return;
662 }
663
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700664 /* PFR is a bit of a special case because it doesn't result in an OICR
665 * interrupt. So for PFR, rebuild after the reset and clear the reset-
666 * associated state bits.
667 */
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700668 if (reset_type == ICE_RESET_PFR) {
669 pf->pfr_count++;
Tony Nguyen462acf6a2019-09-09 06:47:46 -0700670 ice_rebuild(pf, reset_type);
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800671 clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
672 clear_bit(ICE_PFR_REQ, pf->state);
Jacob Keller1c080522021-05-06 08:39:59 -0700673 wake_up(&pf->reset_wait_queue);
Jacob Kellerdac57282022-02-22 16:27:04 -0800674 ice_reset_all_vfs(pf);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700675 }
676}
677
678/**
679 * ice_reset_subtask - Set up for resetting the device and driver
680 * @pf: board private structure
681 */
682static void ice_reset_subtask(struct ice_pf *pf)
683{
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700684 enum ice_reset_req reset_type = ICE_RESET_INVAL;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700685
686 /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700687 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
688 * of reset is pending and sets bits in pf->state indicating the reset
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800689 * type and ICE_RESET_OICR_RECV. So, if the latter bit is set
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700690 * prepare for pending reset if not already (for PF software-initiated
691 * global resets the software should already be prepared for it as
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800692 * indicated by ICE_PREPARED_FOR_RESET; for global resets initiated
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700693 * by firmware or software on other PFs, that bit is not set so prepare
694 * for the reset now), poll for reset done, rebuild and return.
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700695 */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800696 if (test_bit(ICE_RESET_OICR_RECV, pf->state)) {
Dave Ertman2ebd4422019-02-13 10:51:08 -0800697 /* Perform the largest reset requested */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800698 if (test_and_clear_bit(ICE_CORER_RECV, pf->state))
Dave Ertman2ebd4422019-02-13 10:51:08 -0800699 reset_type = ICE_RESET_CORER;
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800700 if (test_and_clear_bit(ICE_GLOBR_RECV, pf->state))
Dave Ertman2ebd4422019-02-13 10:51:08 -0800701 reset_type = ICE_RESET_GLOBR;
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800702 if (test_and_clear_bit(ICE_EMPR_RECV, pf->state))
Anirudh Venkataramanan03af8402019-08-02 01:25:25 -0700703 reset_type = ICE_RESET_EMPR;
Dave Ertman2ebd4422019-02-13 10:51:08 -0800704 /* return if no valid reset type requested */
705 if (reset_type == ICE_RESET_INVAL)
706 return;
Kiran Patilfbc7b272021-10-15 16:35:16 -0700707 ice_prepare_for_reset(pf, reset_type);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700708
709 /* make sure we are ready to rebuild */
Anirudh Venkataramananfd2a9812018-08-09 06:29:47 -0700710 if (ice_check_reset(&pf->hw)) {
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800711 set_bit(ICE_RESET_FAILED, pf->state);
Anirudh Venkataramananfd2a9812018-08-09 06:29:47 -0700712 } else {
713 /* done with reset. start rebuild */
714 pf->hw.reset_ongoing = false;
Tony Nguyen462acf6a2019-09-09 06:47:46 -0700715 ice_rebuild(pf, reset_type);
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700716 /* clear bit to resume normal operations, but
Anirudh Venkataramanan94c44412019-02-19 15:04:12 -0800717 * ICE_NEEDS_RESTART bit is set in case rebuild failed
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700718 */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800719 clear_bit(ICE_RESET_OICR_RECV, pf->state);
720 clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
721 clear_bit(ICE_PFR_REQ, pf->state);
722 clear_bit(ICE_CORER_REQ, pf->state);
723 clear_bit(ICE_GLOBR_REQ, pf->state);
Jacob Keller1c080522021-05-06 08:39:59 -0700724 wake_up(&pf->reset_wait_queue);
Jacob Kellerdac57282022-02-22 16:27:04 -0800725 ice_reset_all_vfs(pf);
Anirudh Venkataramananfd2a9812018-08-09 06:29:47 -0700726 }
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700727
728 return;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700729 }
730
731 /* No pending resets to finish processing. Check for new resets */
Dave Ertman3579aa82023-06-20 15:18:54 -0700732 if (test_bit(ICE_PFR_REQ, pf->state)) {
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700733 reset_type = ICE_RESET_PFR;
Dave Ertman3579aa82023-06-20 15:18:54 -0700734 if (pf->lag && pf->lag->bonded) {
735 dev_dbg(ice_pf_to_dev(pf), "PFR on a bonded interface, promoting to CORER\n");
736 reset_type = ICE_RESET_CORER;
737 }
738 }
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800739 if (test_bit(ICE_CORER_REQ, pf->state))
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700740 reset_type = ICE_RESET_CORER;
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800741 if (test_bit(ICE_GLOBR_REQ, pf->state))
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700742 reset_type = ICE_RESET_GLOBR;
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700743 /* If no valid reset type requested just return */
744 if (reset_type == ICE_RESET_INVAL)
745 return;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700746
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -0700747 /* reset if not already down or busy */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -0800748 if (!test_bit(ICE_DOWN, pf->state) &&
749 !test_bit(ICE_CFG_BUSY, pf->state)) {
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700750 ice_do_reset(pf, reset_type);
751 }
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700752}
753
754/**
Jesse Brandeburg2e0ab372019-08-02 01:25:32 -0700755 * ice_print_topo_conflict - print topology conflict message
756 * @vsi: the VSI whose topology status is being checked
757 */
758static void ice_print_topo_conflict(struct ice_vsi *vsi)
759{
760 switch (vsi->port_info->phy.link_info.topo_media_conflict) {
761 case ICE_AQ_LINK_TOPO_CONFLICT:
762 case ICE_AQ_LINK_MEDIA_CONFLICT:
Paul Greenwalt58785892019-10-09 07:09:49 -0700763 case ICE_AQ_LINK_TOPO_UNREACH_PRT:
764 case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
765 case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
Paul Greenwalt5c571452021-03-02 10:12:07 -0800766 netdev_info(vsi->netdev, "Potential misconfiguration of the Ethernet port detected. If it was not intended, please use the Intel (R) Ethernet Port Configuration Tool to address the issue.\n");
Jesse Brandeburg2e0ab372019-08-02 01:25:32 -0700767 break;
Paul Greenwalt58785892019-10-09 07:09:49 -0700768 case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
Anirudh Venkataramanan4fc5fbe2021-07-16 15:16:39 -0700769 if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, vsi->back->flags))
770 netdev_warn(vsi->netdev, "An unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules\n");
771 else
772 netdev_err(vsi->netdev, "Rx/Tx is disabled on this device because an unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
Paul Greenwalt58785892019-10-09 07:09:49 -0700773 break;
Jesse Brandeburg2e0ab372019-08-02 01:25:32 -0700774 default:
775 break;
776 }
777}
778
779/**
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700780 * ice_print_link_msg - print link up or down message
781 * @vsi: the VSI whose link status is being queried
782 * @isup: boolean for if the link is now up or down
783 */
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -0700784void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700785{
Paul Greenwaltf776b3a2019-04-16 10:34:52 -0700786 struct ice_aqc_get_phy_caps_data *caps;
Paul Greenwalt5ee30562020-07-09 09:16:10 -0700787 const char *an_advertised;
Paul Greenwaltf776b3a2019-04-16 10:34:52 -0700788 const char *fec_req;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700789 const char *speed;
Paul Greenwaltf776b3a2019-04-16 10:34:52 -0700790 const char *fec;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700791 const char *fc;
Jesse Brandeburg43260982019-08-02 01:25:31 -0700792 const char *an;
Tony Nguyen5518ac22021-10-07 15:59:03 -0700793 int status;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700794
Brett Creeleyc2a23e02019-02-28 15:26:01 -0800795 if (!vsi)
796 return;
797
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700798 if (vsi->current_isup == isup)
799 return;
800
801 vsi->current_isup = isup;
802
803 if (!isup) {
804 netdev_info(vsi->netdev, "NIC Link is Down\n");
805 return;
806 }
807
808 switch (vsi->port_info->phy.link_info.link_speed) {
Paul Greenwaltaeccadb2024-05-30 13:06:17 -0400809 case ICE_AQ_LINK_SPEED_200GB:
810 speed = "200 G";
811 break;
Anirudh Venkataramanan072efdf2019-04-16 10:35:02 -0700812 case ICE_AQ_LINK_SPEED_100GB:
813 speed = "100 G";
814 break;
815 case ICE_AQ_LINK_SPEED_50GB:
816 speed = "50 G";
817 break;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700818 case ICE_AQ_LINK_SPEED_40GB:
819 speed = "40 G";
820 break;
821 case ICE_AQ_LINK_SPEED_25GB:
822 speed = "25 G";
823 break;
824 case ICE_AQ_LINK_SPEED_20GB:
825 speed = "20 G";
826 break;
827 case ICE_AQ_LINK_SPEED_10GB:
828 speed = "10 G";
829 break;
830 case ICE_AQ_LINK_SPEED_5GB:
831 speed = "5 G";
832 break;
833 case ICE_AQ_LINK_SPEED_2500MB:
834 speed = "2.5 G";
835 break;
836 case ICE_AQ_LINK_SPEED_1000MB:
837 speed = "1 G";
838 break;
839 case ICE_AQ_LINK_SPEED_100MB:
840 speed = "100 M";
841 break;
842 default:
Simon Perron Caissy5b138862020-09-17 13:13:47 -0700843 speed = "Unknown ";
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700844 break;
845 }
846
847 switch (vsi->port_info->fc.current_mode) {
848 case ICE_FC_FULL:
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -0700849 fc = "Rx/Tx";
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700850 break;
851 case ICE_FC_TX_PAUSE:
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -0700852 fc = "Tx";
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700853 break;
854 case ICE_FC_RX_PAUSE:
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -0700855 fc = "Rx";
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700856 break;
Brett Creeley203a0682019-02-19 15:04:06 -0800857 case ICE_FC_NONE:
858 fc = "None";
859 break;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700860 default:
861 fc = "Unknown";
862 break;
863 }
864
Paul Greenwaltf776b3a2019-04-16 10:34:52 -0700865 /* Get FEC mode based on negotiated link info */
866 switch (vsi->port_info->phy.link_info.fec_info) {
867 case ICE_AQ_LINK_25G_RS_528_FEC_EN:
Paul Greenwaltf776b3a2019-04-16 10:34:52 -0700868 case ICE_AQ_LINK_25G_RS_544_FEC_EN:
869 fec = "RS-FEC";
870 break;
871 case ICE_AQ_LINK_25G_KR_FEC_EN:
872 fec = "FC-FEC/BASE-R";
873 break;
874 default:
875 fec = "NONE";
876 break;
877 }
878
Jesse Brandeburg43260982019-08-02 01:25:31 -0700879 /* check if autoneg completed, might be false due to not supported */
880 if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
881 an = "True";
882 else
883 an = "False";
884
Paul Greenwaltf776b3a2019-04-16 10:34:52 -0700885 /* Get FEC mode requested based on PHY caps last SW configuration */
Tony Nguyen9efe35d02019-11-08 06:23:25 -0800886 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
Paul Greenwaltf776b3a2019-04-16 10:34:52 -0700887 if (!caps) {
888 fec_req = "Unknown";
Paul Greenwalt5ee30562020-07-09 09:16:10 -0700889 an_advertised = "Unknown";
Paul Greenwaltf776b3a2019-04-16 10:34:52 -0700890 goto done;
891 }
892
893 status = ice_aq_get_phy_caps(vsi->port_info, false,
Anirudh Venkataramanand6730a82021-03-25 15:35:06 -0700894 ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
Paul Greenwaltf776b3a2019-04-16 10:34:52 -0700895 if (status)
896 netdev_info(vsi->netdev, "Get phy capability failed.\n");
897
Paul Greenwalt5ee30562020-07-09 09:16:10 -0700898 an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
899
Paul Greenwaltf776b3a2019-04-16 10:34:52 -0700900 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
901 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
902 fec_req = "RS-FEC";
903 else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
904 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
905 fec_req = "FC-FEC/BASE-R";
906 else
907 fec_req = "NONE";
908
Tony Nguyen9efe35d02019-11-08 06:23:25 -0800909 kfree(caps);
Paul Greenwaltf776b3a2019-04-16 10:34:52 -0700910
911done:
Paul Greenwalt5ee30562020-07-09 09:16:10 -0700912 netdev_info(vsi->netdev, "NIC Link is up %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg Advertised: %s, Autoneg Negotiated: %s, Flow Control: %s\n",
913 speed, fec_req, fec, an_advertised, an, fc);
Jesse Brandeburg2e0ab372019-08-02 01:25:32 -0700914 ice_print_topo_conflict(vsi);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -0700915}
916
917/**
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -0800918 * ice_vsi_link_event - update the VSI's netdev
919 * @vsi: the VSI on which the link event occurred
920 * @link_up: whether or not the VSI needs to be set up or down
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700921 */
922static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
923{
Brett Creeleyc2a23e02019-02-28 15:26:01 -0800924 if (!vsi)
925 return;
926
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -0800927 if (test_bit(ICE_VSI_DOWN, vsi->state) || !vsi->netdev)
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700928 return;
929
930 if (vsi->type == ICE_VSI_PF) {
Brett Creeleyc2a23e02019-02-28 15:26:01 -0800931 if (link_up == netif_carrier_ok(vsi->netdev))
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700932 return;
Brett Creeleyc2a23e02019-02-28 15:26:01 -0800933
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -0700934 if (link_up) {
935 netif_carrier_on(vsi->netdev);
936 netif_tx_wake_all_queues(vsi->netdev);
937 } else {
938 netif_carrier_off(vsi->netdev);
939 netif_tx_stop_all_queues(vsi->netdev);
940 }
941 }
942}
943
944/**
Dave Ertman7d9c9b72020-07-13 13:53:04 -0700945 * ice_set_dflt_mib - send a default config MIB to the FW
946 * @pf: private PF struct
947 *
948 * This function sends a default configuration MIB to the FW.
949 *
950 * If this function errors out at any point, the driver is still able to
951 * function. The main impact is that LFC may not operate as expected.
952 * Therefore an error state in this function should be treated with a DBG
953 * message and continue on with driver rebuild/reenable.
954 */
955static void ice_set_dflt_mib(struct ice_pf *pf)
956{
957 struct device *dev = ice_pf_to_dev(pf);
958 u8 mib_type, *buf, *lldpmib = NULL;
959 u16 len, typelen, offset = 0;
960 struct ice_lldp_org_tlv *tlv;
Bruce Allan12aae8f2020-10-12 15:53:26 -0700961 struct ice_hw *hw = &pf->hw;
Dave Ertman7d9c9b72020-07-13 13:53:04 -0700962 u32 ouisubtype;
963
Dave Ertman7d9c9b72020-07-13 13:53:04 -0700964 mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
965 lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
966 if (!lldpmib) {
967 dev_dbg(dev, "%s Failed to allocate MIB memory\n",
968 __func__);
969 return;
970 }
971
972 /* Add ETS CFG TLV */
973 tlv = (struct ice_lldp_org_tlv *)lldpmib;
974 typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
975 ICE_IEEE_ETS_TLV_LEN);
976 tlv->typelen = htons(typelen);
977 ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
978 ICE_IEEE_SUBTYPE_ETS_CFG);
979 tlv->ouisubtype = htonl(ouisubtype);
980
981 buf = tlv->tlvinfo;
982 buf[0] = 0;
983
984 /* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
985 * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
986 * Octets 13 - 20 are TSA values - leave as zeros
987 */
988 buf[5] = 0x64;
Jesse Brandeburg5a259f82023-12-05 17:01:12 -0800989 len = FIELD_GET(ICE_LLDP_TLV_LEN_M, typelen);
Dave Ertman7d9c9b72020-07-13 13:53:04 -0700990 offset += len + 2;
991 tlv = (struct ice_lldp_org_tlv *)
992 ((char *)tlv + sizeof(tlv->typelen) + len);
993
994 /* Add ETS REC TLV */
995 buf = tlv->tlvinfo;
996 tlv->typelen = htons(typelen);
997
998 ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
999 ICE_IEEE_SUBTYPE_ETS_REC);
1000 tlv->ouisubtype = htonl(ouisubtype);
1001
1002 /* First octet of buf is reserved
1003 * Octets 1 - 4 map UP to TC - all UPs map to zero
1004 * Octets 5 - 12 are BW values - set TC 0 to 100%.
1005 * Octets 13 - 20 are TSA value - leave as zeros
1006 */
1007 buf[5] = 0x64;
1008 offset += len + 2;
1009 tlv = (struct ice_lldp_org_tlv *)
1010 ((char *)tlv + sizeof(tlv->typelen) + len);
1011
1012 /* Add PFC CFG TLV */
1013 typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
1014 ICE_IEEE_PFC_TLV_LEN);
1015 tlv->typelen = htons(typelen);
1016
1017 ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
1018 ICE_IEEE_SUBTYPE_PFC_CFG);
1019 tlv->ouisubtype = htonl(ouisubtype);
1020
1021 /* Octet 1 left as all zeros - PFC disabled */
1022 buf[0] = 0x08;
Jesse Brandeburg5a259f82023-12-05 17:01:12 -08001023 len = FIELD_GET(ICE_LLDP_TLV_LEN_M, typelen);
Dave Ertman7d9c9b72020-07-13 13:53:04 -07001024 offset += len + 2;
1025
1026 if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
1027 dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
1028
1029 kfree(lldpmib);
1030}
1031
1032/**
Brett Creeley99d40752021-10-13 09:02:19 -07001033 * ice_check_phy_fw_load - check if PHY FW load failed
1034 * @pf: pointer to PF struct
1035 * @link_cfg_err: bitmap from the link info structure
1036 *
1037 * check if external PHY FW load failed and print an error message if it did
1038 */
1039static void ice_check_phy_fw_load(struct ice_pf *pf, u8 link_cfg_err)
1040{
1041 if (!(link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE)) {
1042 clear_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1043 return;
1044 }
1045
1046 if (test_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags))
1047 return;
1048
1049 if (link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE) {
1050 dev_err(ice_pf_to_dev(pf), "Device failed to load the FW for the external PHY. Please download and install the latest NVM for your device and try again\n");
1051 set_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1052 }
1053}
1054
1055/**
Anirudh Venkataramananc77849f52021-05-06 08:40:01 -07001056 * ice_check_module_power
1057 * @pf: pointer to PF struct
1058 * @link_cfg_err: bitmap from the link info structure
1059 *
1060 * check module power level returned by a previous call to aq_get_link_info
1061 * and print error messages if module power level is not supported
1062 */
1063static void ice_check_module_power(struct ice_pf *pf, u8 link_cfg_err)
1064{
1065 /* if module power level is supported, clear the flag */
1066 if (!(link_cfg_err & (ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT |
1067 ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED))) {
1068 clear_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1069 return;
1070 }
1071
1072 /* if ICE_FLAG_MOD_POWER_UNSUPPORTED was previously set and the
1073 * above block didn't clear this bit, there's nothing to do
1074 */
1075 if (test_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags))
1076 return;
1077
1078 if (link_cfg_err & ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT) {
1079 dev_err(ice_pf_to_dev(pf), "The installed module is incompatible with the device's NVM image. Cannot start link\n");
1080 set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1081 } else if (link_cfg_err & ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED) {
1082 dev_err(ice_pf_to_dev(pf), "The module's power requirements exceed the device's power supply. Cannot start link\n");
1083 set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1084 }
1085}
1086
1087/**
Brett Creeley99d40752021-10-13 09:02:19 -07001088 * ice_check_link_cfg_err - check if link configuration failed
1089 * @pf: pointer to the PF struct
1090 * @link_cfg_err: bitmap from the link info structure
1091 *
1092 * print if any link configuration failure happens due to the value in the
1093 * link_cfg_err parameter in the link info structure
1094 */
1095static void ice_check_link_cfg_err(struct ice_pf *pf, u8 link_cfg_err)
1096{
1097 ice_check_module_power(pf, link_cfg_err);
1098 ice_check_phy_fw_load(pf, link_cfg_err);
1099}
1100
1101/**
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001102 * ice_link_event - process the link event
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -07001103 * @pf: PF that the link event is associated with
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001104 * @pi: port_info for the port that the link event is associated with
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001105 * @link_up: true if the physical link is up and false if it is down
1106 * @link_speed: current link speed received from the link event
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001107 *
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001108 * Returns 0 on success and negative on failure
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001109 */
1110static int
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001111ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
1112 u16 link_speed)
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001113{
Brett Creeley4015d112019-11-08 06:23:26 -08001114 struct device *dev = ice_pf_to_dev(pf);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001115 struct ice_phy_info *phy_info;
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001116 struct ice_vsi *vsi;
1117 u16 old_link_speed;
1118 bool old_link;
Tony Nguyen5518ac22021-10-07 15:59:03 -07001119 int status;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001120
1121 phy_info = &pi->phy;
1122 phy_info->link_info_old = phy_info->link_info;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001123
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001124 old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001125 old_link_speed = phy_info->link_info_old.link_speed;
1126
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001127 /* update the link info structures and re-enable link events,
1128 * don't bail on failure due to other book keeping needed
1129 */
Anirudh Venkataramanand348d512021-03-25 15:35:07 -07001130 status = ice_update_link_info(pi);
1131 if (status)
Tony Nguyen5f87ec42021-10-07 15:56:02 -07001132 dev_dbg(dev, "Failed to update link status on port %d, err %d aq_err %s\n",
1133 pi->lport, status,
Anirudh Venkataramanand348d512021-03-25 15:35:07 -07001134 ice_aq_str(pi->hw->adminq.sq_last_status));
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001135
Brett Creeley99d40752021-10-13 09:02:19 -07001136 ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
Anirudh Venkataramananc77849f52021-05-06 08:40:01 -07001137
Dave Ertman0ce6c342020-07-13 13:53:06 -07001138 /* Check if the link state is up after updating link info, and treat
1139 * this event as an UP event since the link is actually UP now.
1140 */
1141 if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
1142 link_up = true;
1143
Anirudh Venkataramanan208ff752019-08-08 07:39:33 -07001144 vsi = ice_get_main_vsi(pf);
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001145 if (!vsi || !vsi->port_info)
1146 return -EINVAL;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001147
Tony Nguyen6d599942019-06-26 02:20:17 -07001148 /* turn off PHY if media was removed */
1149 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
1150 !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
1151 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
Anirudh Venkataramanand348d512021-03-25 15:35:07 -07001152 ice_set_link(vsi, false);
Tony Nguyen6d599942019-06-26 02:20:17 -07001153 }
1154
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07001155 /* if the old link up/down and speed is the same as the new */
1156 if (link_up == old_link && link_speed == old_link_speed)
Anirudh Venkataramanand348d512021-03-25 15:35:07 -07001157 return 0;
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07001158
Jacob Keller6b1ff5d2022-12-05 11:52:43 -08001159 ice_ptp_link_change(pf, pf->hw.pf_id, link_up);
Jacob Keller3a749622021-10-13 08:54:51 -07001160
Dave Ertman7d9c9b72020-07-13 13:53:04 -07001161 if (ice_is_dcb_active(pf)) {
1162 if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
1163 ice_dcb_rebuild(pf);
1164 } else {
1165 if (link_up)
1166 ice_set_dflt_mib(pf);
1167 }
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001168 ice_vsi_link_event(vsi, link_up);
1169 ice_print_link_msg(vsi, link_up);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001170
Brett Creeley26a91522019-12-12 03:13:01 -08001171 ice_vc_notify_link_state(pf);
Anirudh Venkataramanan53b8dec2018-09-19 17:43:00 -07001172
Anirudh Venkataramanand348d512021-03-25 15:35:07 -07001173 return 0;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001174}
1175
1176/**
Anirudh Venkataramanan4f4be032018-10-18 08:37:09 -07001177 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
1178 * @pf: board private structure
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001179 */
Anirudh Venkataramanan4f4be032018-10-18 08:37:09 -07001180static void ice_watchdog_subtask(struct ice_pf *pf)
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001181{
Anirudh Venkataramanan4f4be032018-10-18 08:37:09 -07001182 int i;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001183
Anirudh Venkataramanan4f4be032018-10-18 08:37:09 -07001184 /* if interface is down do nothing */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001185 if (test_bit(ICE_DOWN, pf->state) ||
1186 test_bit(ICE_CFG_BUSY, pf->state))
Anirudh Venkataramanan4f4be032018-10-18 08:37:09 -07001187 return;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001188
Anirudh Venkataramanan4f4be032018-10-18 08:37:09 -07001189 /* make sure we don't do these things too often */
1190 if (time_before(jiffies,
1191 pf->serv_tmr_prev + pf->serv_tmr_period))
1192 return;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001193
Anirudh Venkataramanan4f4be032018-10-18 08:37:09 -07001194 pf->serv_tmr_prev = jiffies;
1195
Anirudh Venkataramanan4f4be032018-10-18 08:37:09 -07001196 /* Update the stats for active netdevs so the network stack
1197 * can look at updated numbers whenever it cares to
1198 */
1199 ice_update_pf_stats(pf);
Brett Creeley80ed404a2019-02-08 12:50:54 -08001200 ice_for_each_vsi(pf, i)
Anirudh Venkataramanan4f4be032018-10-18 08:37:09 -07001201 if (pf->vsi[i] && pf->vsi[i]->netdev)
1202 ice_update_vsi_stats(pf->vsi[i]);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001203}
1204
1205/**
Brett Creeley250c3b32019-02-26 16:35:23 -08001206 * ice_init_link_events - enable/initialize link events
1207 * @pi: pointer to the port_info instance
1208 *
1209 * Returns -EIO on failure, 0 on success
1210 */
1211static int ice_init_link_events(struct ice_port_info *pi)
1212{
1213 u16 mask;
1214
1215 mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
Brett Creeley99d40752021-10-13 09:02:19 -07001216 ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL |
1217 ICE_AQ_LINK_EVENT_PHY_FW_LOAD_FAIL));
Brett Creeley250c3b32019-02-26 16:35:23 -08001218
1219 if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08001220 dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
Brett Creeley250c3b32019-02-26 16:35:23 -08001221 pi->lport);
1222 return -EIO;
1223 }
1224
1225 if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08001226 dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
Brett Creeley250c3b32019-02-26 16:35:23 -08001227 pi->lport);
1228 return -EIO;
1229 }
1230
1231 return 0;
1232}
1233
1234/**
1235 * ice_handle_link_event - handle link event via ARQ
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -07001236 * @pf: PF that the link event is associated with
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001237 * @event: event structure containing link status info
Brett Creeley250c3b32019-02-26 16:35:23 -08001238 */
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001239static int
1240ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
Brett Creeley250c3b32019-02-26 16:35:23 -08001241{
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001242 struct ice_aqc_get_link_status_data *link_data;
Brett Creeley250c3b32019-02-26 16:35:23 -08001243 struct ice_port_info *port_info;
1244 int status;
1245
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001246 link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
Brett Creeley250c3b32019-02-26 16:35:23 -08001247 port_info = pf->hw.port_info;
1248 if (!port_info)
1249 return -EINVAL;
1250
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001251 status = ice_link_event(pf, port_info,
1252 !!(link_data->link_info & ICE_AQ_LINK_UP),
1253 le16_to_cpu(link_data->link_speed));
Brett Creeley250c3b32019-02-26 16:35:23 -08001254 if (status)
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08001255 dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
1256 status);
Brett Creeley250c3b32019-02-26 16:35:23 -08001257
1258 return status;
1259}
1260
Jacob Kellerd69ea412020-07-23 17:22:03 -07001261/**
Paul M Stillwell Jr9d3535e2023-12-12 21:07:14 -08001262 * ice_get_fwlog_data - copy the FW log data from ARQ event
1263 * @pf: PF that the FW log event is associated with
1264 * @event: event structure containing FW log data
1265 */
1266static void
1267ice_get_fwlog_data(struct ice_pf *pf, struct ice_rq_event_info *event)
1268{
1269 struct ice_fwlog_data *fwlog;
1270 struct ice_hw *hw = &pf->hw;
1271
1272 fwlog = &hw->fwlog_ring.rings[hw->fwlog_ring.tail];
1273
1274 memset(fwlog->data, 0, PAGE_SIZE);
1275 fwlog->data_size = le16_to_cpu(event->desc.datalen);
1276
1277 memcpy(fwlog->data, event->msg_buf, fwlog->data_size);
1278 ice_fwlog_ring_increment(&hw->fwlog_ring.tail, hw->fwlog_ring.size);
1279
1280 if (ice_fwlog_ring_full(&hw->fwlog_ring)) {
1281 /* the rings are full so bump the head to create room */
1282 ice_fwlog_ring_increment(&hw->fwlog_ring.head,
1283 hw->fwlog_ring.size);
1284 }
1285}
1286
1287/**
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001288 * ice_aq_prep_for_event - Prepare to wait for an AdminQ event from firmware
Jacob Kellerd69ea412020-07-23 17:22:03 -07001289 * @pf: pointer to the PF private structure
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001290 * @task: intermediate helper storage and identifier for waiting
Jacob Kellerd69ea412020-07-23 17:22:03 -07001291 * @opcode: the opcode to wait for
Jacob Kellerd69ea412020-07-23 17:22:03 -07001292 *
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001293 * Prepares to wait for a specific AdminQ completion event on the ARQ for
1294 * a given PF. Actual wait would be done by a call to ice_aq_wait_for_event().
Jacob Kellerd69ea412020-07-23 17:22:03 -07001295 *
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001296 * Calls are separated to allow caller registering for event before sending
1297 * the command, which mitigates a race between registering and FW responding.
1298 *
1299 * To obtain only the descriptor contents, pass an task->event with null
Jacob Kellerd69ea412020-07-23 17:22:03 -07001300 * msg_buf. If the complete data buffer is desired, allocate the
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001301 * task->event.msg_buf with enough space ahead of time.
Jacob Kellerd69ea412020-07-23 17:22:03 -07001302 */
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001303void ice_aq_prep_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1304 u16 opcode)
Jacob Kellerd69ea412020-07-23 17:22:03 -07001305{
Jacob Kellerd69ea412020-07-23 17:22:03 -07001306 INIT_HLIST_NODE(&task->entry);
1307 task->opcode = opcode;
Jacob Kellerd69ea412020-07-23 17:22:03 -07001308 task->state = ICE_AQ_TASK_WAITING;
1309
1310 spin_lock_bh(&pf->aq_wait_lock);
1311 hlist_add_head(&task->entry, &pf->aq_wait_list);
1312 spin_unlock_bh(&pf->aq_wait_lock);
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001313}
Jacob Kellerd69ea412020-07-23 17:22:03 -07001314
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001315/**
1316 * ice_aq_wait_for_event - Wait for an AdminQ event from firmware
1317 * @pf: pointer to the PF private structure
1318 * @task: ptr prepared by ice_aq_prep_for_event()
1319 * @timeout: how long to wait, in jiffies
1320 *
1321 * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1322 * current thread will be put to sleep until the specified event occurs or
1323 * until the given timeout is reached.
1324 *
1325 * Returns: zero on success, or a negative error code on failure.
1326 */
1327int ice_aq_wait_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1328 unsigned long timeout)
1329{
1330 enum ice_aq_task_state *state = &task->state;
1331 struct device *dev = ice_pf_to_dev(pf);
1332 unsigned long start = jiffies;
1333 long ret;
1334 int err;
Jacob Keller1e8249c2020-10-07 10:54:45 -07001335
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001336 ret = wait_event_interruptible_timeout(pf->aq_wait_queue,
1337 *state != ICE_AQ_TASK_WAITING,
Jacob Kellerd69ea412020-07-23 17:22:03 -07001338 timeout);
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001339 switch (*state) {
1340 case ICE_AQ_TASK_NOT_PREPARED:
1341 WARN(1, "call to %s without ice_aq_prep_for_event()", __func__);
1342 err = -EINVAL;
1343 break;
Jacob Kellerd69ea412020-07-23 17:22:03 -07001344 case ICE_AQ_TASK_WAITING:
1345 err = ret < 0 ? ret : -ETIMEDOUT;
1346 break;
1347 case ICE_AQ_TASK_CANCELED:
1348 err = ret < 0 ? ret : -ECANCELED;
1349 break;
1350 case ICE_AQ_TASK_COMPLETE:
1351 err = ret < 0 ? ret : 0;
1352 break;
1353 default:
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001354 WARN(1, "Unexpected AdminQ wait task state %u", *state);
Jacob Kellerd69ea412020-07-23 17:22:03 -07001355 err = -EINVAL;
1356 break;
1357 }
1358
Jacob Keller1e8249c2020-10-07 10:54:45 -07001359 dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
1360 jiffies_to_msecs(jiffies - start),
1361 jiffies_to_msecs(timeout),
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001362 task->opcode);
Jacob Keller1e8249c2020-10-07 10:54:45 -07001363
Jacob Kellerd69ea412020-07-23 17:22:03 -07001364 spin_lock_bh(&pf->aq_wait_lock);
1365 hlist_del(&task->entry);
1366 spin_unlock_bh(&pf->aq_wait_lock);
Jacob Kellerd69ea412020-07-23 17:22:03 -07001367
1368 return err;
1369}
1370
1371/**
1372 * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
1373 * @pf: pointer to the PF private structure
1374 * @opcode: the opcode of the event
1375 * @event: the event to check
1376 *
1377 * Loops over the current list of pending threads waiting for an AdminQ event.
1378 * For each matching task, copy the contents of the event into the task
1379 * structure and wake up the thread.
1380 *
1381 * If multiple threads wait for the same opcode, they will all be woken up.
1382 *
1383 * Note that event->msg_buf will only be duplicated if the event has a buffer
1384 * with enough space already allocated. Otherwise, only the descriptor and
1385 * message length will be copied.
1386 *
1387 * Returns: true if an event was found, false otherwise
1388 */
1389static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
1390 struct ice_rq_event_info *event)
1391{
Przemek Kitszele1e8a142023-08-08 17:54:15 -04001392 struct ice_rq_event_info *task_ev;
Jacob Kellerd69ea412020-07-23 17:22:03 -07001393 struct ice_aq_task *task;
1394 bool found = false;
1395
1396 spin_lock_bh(&pf->aq_wait_lock);
1397 hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
Przemek Kitszelfb9840c2023-08-08 17:54:17 -04001398 if (task->state != ICE_AQ_TASK_WAITING)
1399 continue;
1400 if (task->opcode != opcode)
Jacob Kellerd69ea412020-07-23 17:22:03 -07001401 continue;
1402
Przemek Kitszelb214b982023-08-08 17:54:16 -04001403 task_ev = &task->event;
Przemek Kitszele1e8a142023-08-08 17:54:15 -04001404 memcpy(&task_ev->desc, &event->desc, sizeof(event->desc));
1405 task_ev->msg_len = event->msg_len;
Jacob Kellerd69ea412020-07-23 17:22:03 -07001406
1407 /* Only copy the data buffer if a destination was set */
Przemek Kitszele1e8a142023-08-08 17:54:15 -04001408 if (task_ev->msg_buf && task_ev->buf_len >= event->buf_len) {
1409 memcpy(task_ev->msg_buf, event->msg_buf,
Jacob Kellerd69ea412020-07-23 17:22:03 -07001410 event->buf_len);
Przemek Kitszele1e8a142023-08-08 17:54:15 -04001411 task_ev->buf_len = event->buf_len;
Jacob Kellerd69ea412020-07-23 17:22:03 -07001412 }
1413
1414 task->state = ICE_AQ_TASK_COMPLETE;
1415 found = true;
1416 }
1417 spin_unlock_bh(&pf->aq_wait_lock);
1418
1419 if (found)
1420 wake_up(&pf->aq_wait_queue);
1421}
1422
1423/**
1424 * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
1425 * @pf: the PF private structure
1426 *
1427 * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
1428 * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
1429 */
1430static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
1431{
1432 struct ice_aq_task *task;
1433
1434 spin_lock_bh(&pf->aq_wait_lock);
1435 hlist_for_each_entry(task, &pf->aq_wait_list, entry)
1436 task->state = ICE_AQ_TASK_CANCELED;
1437 spin_unlock_bh(&pf->aq_wait_lock);
1438
1439 wake_up(&pf->aq_wait_queue);
1440}
1441
Jacob Kellerafc24d62023-02-22 09:09:17 -08001442#define ICE_MBX_OVERFLOW_WATERMARK 64
1443
Brett Creeley250c3b32019-02-26 16:35:23 -08001444/**
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001445 * __ice_clean_ctrlq - helper function to clean controlq rings
1446 * @pf: ptr to struct ice_pf
1447 * @q_type: specific Control queue type
1448 */
1449static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
1450{
Brett Creeley4015d112019-11-08 06:23:26 -08001451 struct device *dev = ice_pf_to_dev(pf);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001452 struct ice_rq_event_info event;
1453 struct ice_hw *hw = &pf->hw;
1454 struct ice_ctl_q_info *cq;
1455 u16 pending, i = 0;
1456 const char *qtype;
1457 u32 oldval, val;
1458
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001459 /* Do not clean control queue if/when PF reset fails */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001460 if (test_bit(ICE_RESET_FAILED, pf->state))
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001461 return 0;
1462
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001463 switch (q_type) {
1464 case ICE_CTL_Q_ADMIN:
1465 cq = &hw->adminq;
1466 qtype = "Admin";
1467 break;
Jacob Keller8f5ee3c2021-06-09 09:39:46 -07001468 case ICE_CTL_Q_SB:
1469 cq = &hw->sbq;
1470 qtype = "Sideband";
1471 break;
Anirudh Venkataramanan75d2b252018-09-19 17:42:54 -07001472 case ICE_CTL_Q_MAILBOX:
1473 cq = &hw->mailboxq;
1474 qtype = "Mailbox";
Vignesh Sridhar0891c892021-03-02 10:12:00 -08001475 /* we are going to try to detect a malicious VF, so set the
1476 * state to begin detection
1477 */
1478 hw->mbx_snapshot.mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
Anirudh Venkataramanan75d2b252018-09-19 17:42:54 -07001479 break;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001480 default:
Brett Creeley4015d112019-11-08 06:23:26 -08001481 dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001482 return 0;
1483 }
1484
1485 /* check for error indications - PF_xx_AxQLEN register layout for
1486 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
1487 */
1488 val = rd32(hw, cq->rq.len);
1489 if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1490 PF_FW_ARQLEN_ARQCRIT_M)) {
1491 oldval = val;
1492 if (val & PF_FW_ARQLEN_ARQVFE_M)
Brett Creeley4015d112019-11-08 06:23:26 -08001493 dev_dbg(dev, "%s Receive Queue VF Error detected\n",
1494 qtype);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001495 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08001496 dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001497 qtype);
1498 }
1499 if (val & PF_FW_ARQLEN_ARQCRIT_M)
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08001500 dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001501 qtype);
1502 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1503 PF_FW_ARQLEN_ARQCRIT_M);
1504 if (oldval != val)
1505 wr32(hw, cq->rq.len, val);
1506 }
1507
1508 val = rd32(hw, cq->sq.len);
1509 if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1510 PF_FW_ATQLEN_ATQCRIT_M)) {
1511 oldval = val;
1512 if (val & PF_FW_ATQLEN_ATQVFE_M)
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08001513 dev_dbg(dev, "%s Send Queue VF Error detected\n",
1514 qtype);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001515 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
Brett Creeley4015d112019-11-08 06:23:26 -08001516 dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001517 qtype);
1518 }
1519 if (val & PF_FW_ATQLEN_ATQCRIT_M)
Brett Creeley4015d112019-11-08 06:23:26 -08001520 dev_dbg(dev, "%s Send Queue Critical Error detected\n",
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001521 qtype);
1522 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1523 PF_FW_ATQLEN_ATQCRIT_M);
1524 if (oldval != val)
1525 wr32(hw, cq->sq.len, val);
1526 }
1527
1528 event.buf_len = cq->rq_buf_size;
Tony Nguyen9efe35d02019-11-08 06:23:25 -08001529 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001530 if (!event.msg_buf)
1531 return 0;
1532
1533 do {
Jacob Kellerafc24d62023-02-22 09:09:17 -08001534 struct ice_mbx_data data = {};
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001535 u16 opcode;
Tony Nguyen5518ac22021-10-07 15:59:03 -07001536 int ret;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001537
1538 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
Tony Nguyend54699e2021-10-07 15:58:01 -07001539 if (ret == -EALREADY)
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001540 break;
1541 if (ret) {
Tony Nguyen5f87ec42021-10-07 15:56:02 -07001542 dev_err(dev, "%s Receive Queue event error %d\n", qtype,
1543 ret);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001544 break;
1545 }
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001546
1547 opcode = le16_to_cpu(event.desc.opcode);
1548
Jacob Kellerd69ea412020-07-23 17:22:03 -07001549 /* Notify any thread that might be waiting for this event */
1550 ice_aq_check_events(pf, opcode, &event);
1551
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001552 switch (opcode) {
Brett Creeley250c3b32019-02-26 16:35:23 -08001553 case ice_aqc_opc_get_link_status:
Brett Creeleyc2a23e02019-02-28 15:26:01 -08001554 if (ice_handle_link_event(pf, &event))
Brett Creeley4015d112019-11-08 06:23:26 -08001555 dev_err(dev, "Could not handle link event\n");
Brett Creeley250c3b32019-02-26 16:35:23 -08001556 break;
Brett Creeley2309ae32020-01-22 07:21:31 -08001557 case ice_aqc_opc_event_lan_overflow:
1558 ice_vf_lan_overflow_event(pf, &event);
1559 break;
Anirudh Venkataramanan1071a832018-09-19 17:42:59 -07001560 case ice_mbx_opc_send_msg_to_pf:
Jacob Kellerafc24d62023-02-22 09:09:17 -08001561 data.num_msg_proc = i;
1562 data.num_pending_arq = pending;
1563 data.max_num_msgs_mbx = hw->mailboxq.num_rq_entries;
1564 data.async_watermark_val = ICE_MBX_OVERFLOW_WATERMARK;
1565
Jacob Kellerbe968152023-02-22 09:09:20 -08001566 ice_vc_process_vf_msg(pf, &event, &data);
Anirudh Venkataramanan1071a832018-09-19 17:42:59 -07001567 break;
Paul M Stillwell Jr9d3535e2023-12-12 21:07:14 -08001568 case ice_aqc_opc_fw_logs_event:
1569 ice_get_fwlog_data(pf, &event);
1570 break;
Anirudh Venkataramanan00cc3f12019-02-28 15:24:26 -08001571 case ice_aqc_opc_lldp_set_mib_change:
1572 ice_dcb_process_lldp_set_mib_change(pf, &event);
1573 break;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001574 default:
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08001575 dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07001576 qtype, opcode);
1577 break;
1578 }
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001579 } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1580
Tony Nguyen9efe35d02019-11-08 06:23:25 -08001581 kfree(event.msg_buf);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001582
1583 return pending && (i == ICE_DFLT_IRQ_WORK);
1584}
1585
1586/**
Anirudh Venkataramanan3d6b6402018-08-09 06:28:56 -07001587 * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1588 * @hw: pointer to hardware info
1589 * @cq: control queue information
1590 *
1591 * returns true if there are pending messages in a queue, false if there aren't
1592 */
1593static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1594{
1595 u16 ntu;
1596
1597 ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1598 return cq->rq.next_to_clean != ntu;
1599}
1600
1601/**
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001602 * ice_clean_adminq_subtask - clean the AdminQ rings
1603 * @pf: board private structure
1604 */
1605static void ice_clean_adminq_subtask(struct ice_pf *pf)
1606{
1607 struct ice_hw *hw = &pf->hw;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001608
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001609 if (!test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001610 return;
1611
1612 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1613 return;
1614
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001615 clear_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001616
Anirudh Venkataramanan3d6b6402018-08-09 06:28:56 -07001617 /* There might be a situation where new messages arrive to a control
1618 * queue between processing the last message and clearing the
1619 * EVENT_PENDING bit. So before exiting, check queue head again (using
1620 * ice_ctrlq_pending) and process new messages if any.
1621 */
1622 if (ice_ctrlq_pending(hw, &hw->adminq))
1623 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001624
1625 ice_flush(hw);
1626}
1627
1628/**
Anirudh Venkataramanan75d2b252018-09-19 17:42:54 -07001629 * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1630 * @pf: board private structure
1631 */
1632static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1633{
1634 struct ice_hw *hw = &pf->hw;
1635
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001636 if (!test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state))
Anirudh Venkataramanan75d2b252018-09-19 17:42:54 -07001637 return;
1638
1639 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1640 return;
1641
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001642 clear_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
Anirudh Venkataramanan75d2b252018-09-19 17:42:54 -07001643
1644 if (ice_ctrlq_pending(hw, &hw->mailboxq))
1645 __ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1646
1647 ice_flush(hw);
1648}
1649
1650/**
Jacob Keller8f5ee3c2021-06-09 09:39:46 -07001651 * ice_clean_sbq_subtask - clean the Sideband Queue rings
1652 * @pf: board private structure
1653 */
1654static void ice_clean_sbq_subtask(struct ice_pf *pf)
1655{
1656 struct ice_hw *hw = &pf->hw;
1657
Grzegorz Nitka7a156682023-12-06 20:29:18 +01001658 /* if mac_type is not generic, sideband is not supported
1659 * and there's nothing to do here
1660 */
1661 if (!ice_is_generic_mac(hw)) {
Jacob Keller8f5ee3c2021-06-09 09:39:46 -07001662 clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1663 return;
1664 }
1665
1666 if (!test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state))
1667 return;
1668
1669 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_SB))
1670 return;
1671
1672 clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1673
1674 if (ice_ctrlq_pending(hw, &hw->sbq))
1675 __ice_clean_ctrlq(pf, ICE_CTL_Q_SB);
1676
1677 ice_flush(hw);
1678}
1679
1680/**
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001681 * ice_service_task_schedule - schedule the service task to wake up
1682 * @pf: board private structure
1683 *
1684 * If not already scheduled, this puts the task into the work queue.
1685 */
Brett Creeley28bf2672020-05-11 18:01:46 -07001686void ice_service_task_schedule(struct ice_pf *pf)
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001687{
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001688 if (!test_bit(ICE_SERVICE_DIS, pf->state) &&
1689 !test_and_set_bit(ICE_SERVICE_SCHED, pf->state) &&
1690 !test_bit(ICE_NEEDS_RESTART, pf->state))
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001691 queue_work(ice_wq, &pf->serv_task);
1692}
1693
1694/**
1695 * ice_service_task_complete - finish up the service task
1696 * @pf: board private structure
1697 */
1698static void ice_service_task_complete(struct ice_pf *pf)
1699{
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001700 WARN_ON(!test_bit(ICE_SERVICE_SCHED, pf->state));
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001701
1702 /* force memory (pf->state) to sync before next service task */
1703 smp_mb__before_atomic();
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001704 clear_bit(ICE_SERVICE_SCHED, pf->state);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001705}
1706
1707/**
Akeem G Abodunrin8d81fa52018-08-09 06:29:57 -07001708 * ice_service_task_stop - stop service task and cancel works
1709 * @pf: board private structure
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07001710 *
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001711 * Return 0 if the ICE_SERVICE_DIS bit was not already set,
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07001712 * 1 otherwise.
Akeem G Abodunrin8d81fa52018-08-09 06:29:57 -07001713 */
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07001714static int ice_service_task_stop(struct ice_pf *pf)
Akeem G Abodunrin8d81fa52018-08-09 06:29:57 -07001715{
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07001716 int ret;
1717
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001718 ret = test_and_set_bit(ICE_SERVICE_DIS, pf->state);
Akeem G Abodunrin8d81fa52018-08-09 06:29:57 -07001719
1720 if (pf->serv_tmr.function)
1721 del_timer_sync(&pf->serv_tmr);
1722 if (pf->serv_task.func)
1723 cancel_work_sync(&pf->serv_task);
1724
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001725 clear_bit(ICE_SERVICE_SCHED, pf->state);
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07001726 return ret;
Akeem G Abodunrin8d81fa52018-08-09 06:29:57 -07001727}
1728
1729/**
Brett Creeley5995b6d2019-02-13 10:51:15 -08001730 * ice_service_task_restart - restart service task and schedule works
1731 * @pf: board private structure
1732 *
1733 * This function is needed for suspend and resume works (e.g WoL scenario)
1734 */
1735static void ice_service_task_restart(struct ice_pf *pf)
1736{
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001737 clear_bit(ICE_SERVICE_DIS, pf->state);
Brett Creeley5995b6d2019-02-13 10:51:15 -08001738 ice_service_task_schedule(pf);
1739}
1740
1741/**
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07001742 * ice_service_timer - timer callback to schedule service task
1743 * @t: pointer to timer_list
1744 */
1745static void ice_service_timer(struct timer_list *t)
1746{
1747 struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1748
1749 mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1750 ice_service_task_schedule(pf);
1751}
1752
1753/**
Marcin Szycikcc2a9d62024-04-04 16:04:51 +02001754 * ice_mdd_maybe_reset_vf - reset VF after MDD event
1755 * @pf: pointer to the PF structure
1756 * @vf: pointer to the VF structure
1757 * @reset_vf_tx: whether Tx MDD has occurred
1758 * @reset_vf_rx: whether Rx MDD has occurred
1759 *
1760 * Since the queue can get stuck on VF MDD events, the PF can be configured to
1761 * automatically reset the VF by enabling the private ethtool flag
1762 * mdd-auto-reset-vf.
1763 */
1764static void ice_mdd_maybe_reset_vf(struct ice_pf *pf, struct ice_vf *vf,
1765 bool reset_vf_tx, bool reset_vf_rx)
1766{
1767 struct device *dev = ice_pf_to_dev(pf);
1768
1769 if (!test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags))
1770 return;
1771
1772 /* VF MDD event counters will be cleared by reset, so print the event
1773 * prior to reset.
1774 */
1775 if (reset_vf_tx)
1776 ice_print_vf_tx_mdd_event(vf);
1777
1778 if (reset_vf_rx)
1779 ice_print_vf_rx_mdd_event(vf);
1780
1781 dev_info(dev, "PF-to-VF reset on PF %d VF %d due to MDD event\n",
1782 pf->hw.pf_id, vf->vf_id);
1783 ice_reset_vf(vf, ICE_VF_RESET_NOTIFY | ICE_VF_RESET_LOCK);
1784}
1785
1786/**
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001787 * ice_handle_mdd_event - handle malicious driver detect event
1788 * @pf: pointer to the PF structure
1789 *
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001790 * Called from service task. OICR interrupt handler indicates MDD event.
1791 * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
1792 * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
1793 * disable the queue, the PF can be configured to reset the VF using ethtool
1794 * private flag mdd-auto-reset-vf.
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001795 */
1796static void ice_handle_mdd_event(struct ice_pf *pf)
1797{
Brett Creeley4015d112019-11-08 06:23:26 -08001798 struct device *dev = ice_pf_to_dev(pf);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001799 struct ice_hw *hw = &pf->hw;
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001800 struct ice_vf *vf;
1801 unsigned int bkt;
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001802 u32 reg;
1803
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001804 if (!test_and_clear_bit(ICE_MDD_EVENT_PENDING, pf->state)) {
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001805 /* Since the VF MDD event logging is rate limited, check if
1806 * there are pending MDD events.
1807 */
1808 ice_print_vfs_mdd_events(pf);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001809 return;
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001810 }
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001811
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001812 /* find what triggered an MDD event */
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001813 reg = rd32(hw, GL_MDET_TX_PQM);
1814 if (reg & GL_MDET_TX_PQM_VALID_M) {
Jesse Brandeburg5a259f82023-12-05 17:01:12 -08001815 u8 pf_num = FIELD_GET(GL_MDET_TX_PQM_PF_NUM_M, reg);
1816 u16 vf_num = FIELD_GET(GL_MDET_TX_PQM_VF_NUM_M, reg);
1817 u8 event = FIELD_GET(GL_MDET_TX_PQM_MAL_TYPE_M, reg);
1818 u16 queue = FIELD_GET(GL_MDET_TX_PQM_QNUM_M, reg);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001819
1820 if (netif_msg_tx_err(pf))
Brett Creeley4015d112019-11-08 06:23:26 -08001821 dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001822 event, queue, pf_num, vf_num);
1823 wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001824 }
1825
Paul Greenwaltba1124f2023-10-25 14:41:52 -07001826 reg = rd32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw));
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001827 if (reg & GL_MDET_TX_TCLAN_VALID_M) {
Jesse Brandeburg5a259f82023-12-05 17:01:12 -08001828 u8 pf_num = FIELD_GET(GL_MDET_TX_TCLAN_PF_NUM_M, reg);
1829 u16 vf_num = FIELD_GET(GL_MDET_TX_TCLAN_VF_NUM_M, reg);
1830 u8 event = FIELD_GET(GL_MDET_TX_TCLAN_MAL_TYPE_M, reg);
1831 u16 queue = FIELD_GET(GL_MDET_TX_TCLAN_QNUM_M, reg);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001832
Ben Shelton1d8bd992020-02-06 01:20:12 -08001833 if (netif_msg_tx_err(pf))
Brett Creeley4015d112019-11-08 06:23:26 -08001834 dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001835 event, queue, pf_num, vf_num);
Paul Greenwaltba1124f2023-10-25 14:41:52 -07001836 wr32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw), U32_MAX);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001837 }
1838
1839 reg = rd32(hw, GL_MDET_RX);
1840 if (reg & GL_MDET_RX_VALID_M) {
Jesse Brandeburg5a259f82023-12-05 17:01:12 -08001841 u8 pf_num = FIELD_GET(GL_MDET_RX_PF_NUM_M, reg);
1842 u16 vf_num = FIELD_GET(GL_MDET_RX_VF_NUM_M, reg);
1843 u8 event = FIELD_GET(GL_MDET_RX_MAL_TYPE_M, reg);
1844 u16 queue = FIELD_GET(GL_MDET_RX_QNUM_M, reg);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001845
1846 if (netif_msg_rx_err(pf))
Brett Creeley4015d112019-11-08 06:23:26 -08001847 dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001848 event, queue, pf_num, vf_num);
1849 wr32(hw, GL_MDET_RX, 0xffffffff);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001850 }
1851
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001852 /* check to see if this PF caused an MDD event */
1853 reg = rd32(hw, PF_MDET_TX_PQM);
1854 if (reg & PF_MDET_TX_PQM_VALID_M) {
1855 wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1856 if (netif_msg_tx_err(pf))
1857 dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001858 }
1859
Paul Greenwaltba1124f2023-10-25 14:41:52 -07001860 reg = rd32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw));
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001861 if (reg & PF_MDET_TX_TCLAN_VALID_M) {
Paul Greenwaltba1124f2023-10-25 14:41:52 -07001862 wr32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw), 0xffff);
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001863 if (netif_msg_tx_err(pf))
1864 dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
1865 }
1866
1867 reg = rd32(hw, PF_MDET_RX);
1868 if (reg & PF_MDET_RX_VALID_M) {
1869 wr32(hw, PF_MDET_RX, 0xFFFF);
1870 if (netif_msg_rx_err(pf))
1871 dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
1872 }
1873
1874 /* Check to see if one of the VFs caused an MDD event, and then
1875 * increment counters and set print pending
1876 */
Jacob Keller3d5985a2022-02-16 13:37:38 -08001877 mutex_lock(&pf->vfs.table_lock);
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001878 ice_for_each_vf(pf, bkt, vf) {
Marcin Szycikcc2a9d62024-04-04 16:04:51 +02001879 bool reset_vf_tx = false, reset_vf_rx = false;
1880
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001881 reg = rd32(hw, VP_MDET_TX_PQM(vf->vf_id));
Anirudh Venkataramanan7c4bc1f2018-09-19 17:43:01 -07001882 if (reg & VP_MDET_TX_PQM_VALID_M) {
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001883 wr32(hw, VP_MDET_TX_PQM(vf->vf_id), 0xFFFF);
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001884 vf->mdd_tx_events.count++;
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001885 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001886 if (netif_msg_tx_err(pf))
1887 dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001888 vf->vf_id);
Marcin Szycikcc2a9d62024-04-04 16:04:51 +02001889
1890 reset_vf_tx = true;
Anirudh Venkataramanan7c4bc1f2018-09-19 17:43:01 -07001891 }
1892
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001893 reg = rd32(hw, VP_MDET_TX_TCLAN(vf->vf_id));
Anirudh Venkataramanan7c4bc1f2018-09-19 17:43:01 -07001894 if (reg & VP_MDET_TX_TCLAN_VALID_M) {
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001895 wr32(hw, VP_MDET_TX_TCLAN(vf->vf_id), 0xFFFF);
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001896 vf->mdd_tx_events.count++;
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001897 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001898 if (netif_msg_tx_err(pf))
1899 dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001900 vf->vf_id);
Marcin Szycikcc2a9d62024-04-04 16:04:51 +02001901
1902 reset_vf_tx = true;
Anirudh Venkataramanan7c4bc1f2018-09-19 17:43:01 -07001903 }
1904
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001905 reg = rd32(hw, VP_MDET_TX_TDPU(vf->vf_id));
Anirudh Venkataramanan7c4bc1f2018-09-19 17:43:01 -07001906 if (reg & VP_MDET_TX_TDPU_VALID_M) {
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001907 wr32(hw, VP_MDET_TX_TDPU(vf->vf_id), 0xFFFF);
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001908 vf->mdd_tx_events.count++;
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001909 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001910 if (netif_msg_tx_err(pf))
1911 dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001912 vf->vf_id);
Marcin Szycikcc2a9d62024-04-04 16:04:51 +02001913
1914 reset_vf_tx = true;
Anirudh Venkataramanan7c4bc1f2018-09-19 17:43:01 -07001915 }
1916
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001917 reg = rd32(hw, VP_MDET_RX(vf->vf_id));
Anirudh Venkataramanan7c4bc1f2018-09-19 17:43:01 -07001918 if (reg & VP_MDET_RX_VALID_M) {
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001919 wr32(hw, VP_MDET_RX(vf->vf_id), 0xFFFF);
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001920 vf->mdd_rx_events.count++;
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08001921 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001922 if (netif_msg_rx_err(pf))
1923 dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
Jacob Kellerc4c2c7d2022-02-16 13:37:35 -08001924 vf->vf_id);
Anirudh Venkataramanan7c4bc1f2018-09-19 17:43:01 -07001925
Marcin Szycikcc2a9d62024-04-04 16:04:51 +02001926 reset_vf_rx = true;
Anirudh Venkataramanan7c4bc1f2018-09-19 17:43:01 -07001927 }
Marcin Szycikcc2a9d62024-04-04 16:04:51 +02001928
1929 if (reset_vf_tx || reset_vf_rx)
1930 ice_mdd_maybe_reset_vf(pf, vf, reset_vf_tx,
1931 reset_vf_rx);
Anirudh Venkataramanan7c4bc1f2018-09-19 17:43:01 -07001932 }
Jacob Keller3d5985a2022-02-16 13:37:38 -08001933 mutex_unlock(&pf->vfs.table_lock);
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08001934
1935 ice_print_vfs_mdd_events(pf);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07001936}
1937
1938/**
Tony Nguyen6d599942019-06-26 02:20:17 -07001939 * ice_force_phys_link_state - Force the physical link state
1940 * @vsi: VSI to force the physical link state to up/down
1941 * @link_up: true/false indicates to set the physical link to up/down
1942 *
1943 * Force the physical link state by getting the current PHY capabilities from
1944 * hardware and setting the PHY config based on the determined capabilities. If
1945 * link changes a link event will be triggered because both the Enable Automatic
1946 * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1947 *
1948 * Returns 0 on success, negative on failure
1949 */
1950static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1951{
1952 struct ice_aqc_get_phy_caps_data *pcaps;
1953 struct ice_aqc_set_phy_cfg_data *cfg;
1954 struct ice_port_info *pi;
1955 struct device *dev;
1956 int retcode;
1957
1958 if (!vsi || !vsi->port_info || !vsi->back)
1959 return -EINVAL;
1960 if (vsi->type != ICE_VSI_PF)
1961 return 0;
1962
Anirudh Venkataramanan9a946842020-02-06 01:20:09 -08001963 dev = ice_pf_to_dev(vsi->back);
Tony Nguyen6d599942019-06-26 02:20:17 -07001964
1965 pi = vsi->port_info;
1966
Tony Nguyen9efe35d02019-11-08 06:23:25 -08001967 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
Tony Nguyen6d599942019-06-26 02:20:17 -07001968 if (!pcaps)
1969 return -ENOMEM;
1970
Anirudh Venkataramanand6730a82021-03-25 15:35:06 -07001971 retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
Tony Nguyen6d599942019-06-26 02:20:17 -07001972 NULL);
1973 if (retcode) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08001974 dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
Tony Nguyen6d599942019-06-26 02:20:17 -07001975 vsi->vsi_num, retcode);
1976 retcode = -EIO;
1977 goto out;
1978 }
1979
1980 /* No change in link */
1981 if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1982 link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1983 goto out;
1984
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07001985 /* Use the current user PHY configuration. The current user PHY
1986 * configuration is initialized during probe from PHY capabilities
1987 * software mode, and updated on set PHY configuration.
1988 */
1989 cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
Tony Nguyen6d599942019-06-26 02:20:17 -07001990 if (!cfg) {
1991 retcode = -ENOMEM;
1992 goto out;
1993 }
1994
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07001995 cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
Tony Nguyen6d599942019-06-26 02:20:17 -07001996 if (link_up)
1997 cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1998 else
1999 cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
2000
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002001 retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
Tony Nguyen6d599942019-06-26 02:20:17 -07002002 if (retcode) {
2003 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
2004 vsi->vsi_num, retcode);
2005 retcode = -EIO;
2006 }
2007
Tony Nguyen9efe35d02019-11-08 06:23:25 -08002008 kfree(cfg);
Tony Nguyen6d599942019-06-26 02:20:17 -07002009out:
Tony Nguyen9efe35d02019-11-08 06:23:25 -08002010 kfree(pcaps);
Tony Nguyen6d599942019-06-26 02:20:17 -07002011 return retcode;
2012}
2013
2014/**
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002015 * ice_init_nvm_phy_type - Initialize the NVM PHY type
2016 * @pi: port info structure
2017 *
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002018 * Initialize nvm_phy_type_[low|high] for link lenient mode support
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002019 */
2020static int ice_init_nvm_phy_type(struct ice_port_info *pi)
2021{
2022 struct ice_aqc_get_phy_caps_data *pcaps;
2023 struct ice_pf *pf = pi->hw->back;
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002024 int err;
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002025
2026 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2027 if (!pcaps)
2028 return -ENOMEM;
2029
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002030 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA,
2031 pcaps, NULL);
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002032
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002033 if (err) {
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002034 dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002035 goto out;
2036 }
2037
2038 pf->nvm_phy_type_hi = pcaps->phy_type_high;
2039 pf->nvm_phy_type_lo = pcaps->phy_type_low;
2040
2041out:
2042 kfree(pcaps);
2043 return err;
2044}
2045
2046/**
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002047 * ice_init_link_dflt_override - Initialize link default override
2048 * @pi: port info structure
Bruce Allanb4e813d2020-07-09 09:16:08 -07002049 *
2050 * Initialize link default override and PHY total port shutdown during probe
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002051 */
2052static void ice_init_link_dflt_override(struct ice_port_info *pi)
2053{
2054 struct ice_link_default_override_tlv *ldo;
2055 struct ice_pf *pf = pi->hw->back;
2056
2057 ldo = &pf->link_dflt_override;
Bruce Allanb4e813d2020-07-09 09:16:08 -07002058 if (ice_get_link_default_override(ldo, pi))
2059 return;
2060
2061 if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
2062 return;
2063
2064 /* Enable Total Port Shutdown (override/replace link-down-on-close
2065 * ethtool private flag) for ports with Port Disable bit set.
2066 */
2067 set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
2068 set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002069}
2070
2071/**
2072 * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
2073 * @pi: port info structure
2074 *
Anirudh Venkataramanan0a029442021-03-25 15:35:12 -07002075 * If default override is enabled, initialize the user PHY cfg speed and FEC
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002076 * settings using the default override mask from the NVM.
2077 *
2078 * The PHY should only be configured with the default override settings the
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002079 * first time media is available. The ICE_LINK_DEFAULT_OVERRIDE_PENDING state
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002080 * is used to indicate that the user PHY cfg default override is initialized
2081 * and the PHY has not been configured with the default override settings. The
2082 * state is set here, and cleared in ice_configure_phy the first time the PHY is
2083 * configured.
Anirudh Venkataramanan0a029442021-03-25 15:35:12 -07002084 *
2085 * This function should be called only if the FW doesn't support default
2086 * configuration mode, as reported by ice_fw_supports_report_dflt_cfg.
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002087 */
2088static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
2089{
2090 struct ice_link_default_override_tlv *ldo;
2091 struct ice_aqc_set_phy_cfg_data *cfg;
2092 struct ice_phy_info *phy = &pi->phy;
2093 struct ice_pf *pf = pi->hw->back;
2094
2095 ldo = &pf->link_dflt_override;
2096
2097 /* If link default override is enabled, use to mask NVM PHY capabilities
2098 * for speed and FEC default configuration.
2099 */
2100 cfg = &phy->curr_user_phy_cfg;
2101
2102 if (ldo->phy_type_low || ldo->phy_type_high) {
2103 cfg->phy_type_low = pf->nvm_phy_type_lo &
2104 cpu_to_le64(ldo->phy_type_low);
2105 cfg->phy_type_high = pf->nvm_phy_type_hi &
2106 cpu_to_le64(ldo->phy_type_high);
2107 }
2108 cfg->link_fec_opt = ldo->fec_options;
2109 phy->curr_user_fec_req = ICE_FEC_AUTO;
2110
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002111 set_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002112}
2113
2114/**
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002115 * ice_init_phy_user_cfg - Initialize the PHY user configuration
2116 * @pi: port info structure
2117 *
2118 * Initialize the current user PHY configuration, speed, FEC, and FC requested
2119 * mode to default. The PHY defaults are from get PHY capabilities topology
2120 * with media so call when media is first available. An error is returned if
2121 * called when media is not available. The PHY initialization completed state is
2122 * set here.
2123 *
2124 * These configurations are used when setting PHY
2125 * configuration. The user PHY configuration is updated on set PHY
2126 * configuration. Returns 0 on success, negative on failure
2127 */
2128static int ice_init_phy_user_cfg(struct ice_port_info *pi)
2129{
2130 struct ice_aqc_get_phy_caps_data *pcaps;
2131 struct ice_phy_info *phy = &pi->phy;
2132 struct ice_pf *pf = pi->hw->back;
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002133 int err;
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002134
2135 if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2136 return -EIO;
2137
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002138 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2139 if (!pcaps)
2140 return -ENOMEM;
2141
Anirudh Venkataramanan0a029442021-03-25 15:35:12 -07002142 if (ice_fw_supports_report_dflt_cfg(pi->hw))
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002143 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2144 pcaps, NULL);
Anirudh Venkataramanan0a029442021-03-25 15:35:12 -07002145 else
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002146 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2147 pcaps, NULL);
2148 if (err) {
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002149 dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002150 goto err_out;
2151 }
2152
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002153 ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
2154
2155 /* check if lenient mode is supported and enabled */
Anirudh Venkataramanandc6aaa12021-03-25 15:35:14 -07002156 if (ice_fw_supports_link_override(pi->hw) &&
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002157 !(pcaps->module_compliance_enforcement &
2158 ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
2159 set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
2160
Anirudh Venkataramanan0a029442021-03-25 15:35:12 -07002161 /* if the FW supports default PHY configuration mode, then the driver
2162 * does not have to apply link override settings. If not,
2163 * initialize user PHY configuration with link override values
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002164 */
Anirudh Venkataramanan0a029442021-03-25 15:35:12 -07002165 if (!ice_fw_supports_report_dflt_cfg(pi->hw) &&
2166 (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN)) {
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002167 ice_init_phy_cfg_dflt_override(pi);
2168 goto out;
2169 }
2170 }
2171
Anirudh Venkataramanan0a029442021-03-25 15:35:12 -07002172 /* if link default override is not enabled, set user flow control and
2173 * FEC settings based on what get_phy_caps returned
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002174 */
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002175 phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
2176 pcaps->link_fec_options);
2177 phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
2178
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002179out:
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002180 phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002181 set_bit(ICE_PHY_INIT_COMPLETE, pf->state);
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002182err_out:
2183 kfree(pcaps);
2184 return err;
2185}
2186
2187/**
2188 * ice_configure_phy - configure PHY
2189 * @vsi: VSI of PHY
2190 *
2191 * Set the PHY configuration. If the current PHY configuration is the same as
2192 * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
2193 * configure the based get PHY capabilities for topology with media.
2194 */
2195static int ice_configure_phy(struct ice_vsi *vsi)
2196{
2197 struct device *dev = ice_pf_to_dev(vsi->back);
Anirudh Venkataramananefc1edd2021-03-25 15:35:15 -07002198 struct ice_port_info *pi = vsi->port_info;
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002199 struct ice_aqc_get_phy_caps_data *pcaps;
2200 struct ice_aqc_set_phy_cfg_data *cfg;
Anirudh Venkataramananefc1edd2021-03-25 15:35:15 -07002201 struct ice_phy_info *phy = &pi->phy;
2202 struct ice_pf *pf = vsi->back;
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002203 int err;
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002204
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002205 /* Ensure we have media as we cannot configure a medialess port */
Anirudh Venkataramananefc1edd2021-03-25 15:35:15 -07002206 if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
Katarzyna Wieczerzycka6a8d8bb52023-12-15 12:01:56 +01002207 return -ENOMEDIUM;
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002208
2209 ice_print_topo_conflict(vsi);
2210
Anirudh Venkataramanan4fc5fbe2021-07-16 15:16:39 -07002211 if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags) &&
2212 phy->link_info.topo_media_conflict == ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002213 return -EPERM;
2214
Anirudh Venkataramananefc1edd2021-03-25 15:35:15 -07002215 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002216 return ice_force_phys_link_state(vsi, true);
2217
2218 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2219 if (!pcaps)
2220 return -ENOMEM;
2221
2222 /* Get current PHY config */
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002223 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
2224 NULL);
2225 if (err) {
Tony Nguyen5f87ec42021-10-07 15:56:02 -07002226 dev_err(dev, "Failed to get PHY configuration, VSI %d error %d\n",
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002227 vsi->vsi_num, err);
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002228 goto done;
2229 }
2230
2231 /* If PHY enable link is configured and configuration has not changed,
2232 * there's nothing to do
2233 */
2234 if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
Anirudh Venkataramananefc1edd2021-03-25 15:35:15 -07002235 ice_phy_caps_equals_cfg(pcaps, &phy->curr_user_phy_cfg))
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002236 goto done;
2237
2238 /* Use PHY topology as baseline for configuration */
2239 memset(pcaps, 0, sizeof(*pcaps));
Anirudh Venkataramanan0a029442021-03-25 15:35:12 -07002240 if (ice_fw_supports_report_dflt_cfg(pi->hw))
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002241 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2242 pcaps, NULL);
Anirudh Venkataramanan0a029442021-03-25 15:35:12 -07002243 else
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002244 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2245 pcaps, NULL);
2246 if (err) {
Tony Nguyen5f87ec42021-10-07 15:56:02 -07002247 dev_err(dev, "Failed to get PHY caps, VSI %d error %d\n",
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002248 vsi->vsi_num, err);
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002249 goto done;
2250 }
2251
2252 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2253 if (!cfg) {
2254 err = -ENOMEM;
2255 goto done;
2256 }
2257
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002258 ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002259
2260 /* Speed - If default override pending, use curr_user_phy_cfg set in
2261 * ice_init_phy_user_cfg_ldo.
2262 */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002263 if (test_and_clear_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING,
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002264 vsi->back->state)) {
Anirudh Venkataramananefc1edd2021-03-25 15:35:15 -07002265 cfg->phy_type_low = phy->curr_user_phy_cfg.phy_type_low;
2266 cfg->phy_type_high = phy->curr_user_phy_cfg.phy_type_high;
Paul Greenwaltea78ce42020-07-09 09:16:07 -07002267 } else {
2268 u64 phy_low = 0, phy_high = 0;
2269
2270 ice_update_phy_type(&phy_low, &phy_high,
2271 pi->phy.curr_user_speed_req);
2272 cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
2273 cfg->phy_type_high = pcaps->phy_type_high &
2274 cpu_to_le64(phy_high);
2275 }
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002276
2277 /* Can't provide what was requested; use PHY capabilities */
2278 if (!cfg->phy_type_low && !cfg->phy_type_high) {
2279 cfg->phy_type_low = pcaps->phy_type_low;
2280 cfg->phy_type_high = pcaps->phy_type_high;
2281 }
2282
2283 /* FEC */
Anirudh Venkataramananefc1edd2021-03-25 15:35:15 -07002284 ice_cfg_phy_fec(pi, cfg, phy->curr_user_fec_req);
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002285
2286 /* Can't provide what was requested; use PHY capabilities */
2287 if (cfg->link_fec_opt !=
2288 (cfg->link_fec_opt & pcaps->link_fec_options)) {
2289 cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
2290 cfg->link_fec_opt = pcaps->link_fec_options;
2291 }
2292
2293 /* Flow Control - always supported; no need to check against
2294 * capabilities
2295 */
Anirudh Venkataramananefc1edd2021-03-25 15:35:15 -07002296 ice_cfg_phy_fc(pi, cfg, phy->curr_user_fc_req);
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002297
2298 /* Enable link and link update */
2299 cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
2300
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002301 err = ice_aq_set_phy_cfg(&pf->hw, pi, cfg, NULL);
Tony Nguyenc1484692021-10-07 16:01:58 -07002302 if (err)
Tony Nguyen5f87ec42021-10-07 15:56:02 -07002303 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07002304 vsi->vsi_num, err);
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002305
2306 kfree(cfg);
2307done:
2308 kfree(pcaps);
2309 return err;
2310}
2311
2312/**
2313 * ice_check_media_subtask - Check for media
Tony Nguyen6d599942019-06-26 02:20:17 -07002314 * @pf: pointer to PF struct
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002315 *
2316 * If media is available, then initialize PHY user configuration if it is not
2317 * been, and configure the PHY if the interface is up.
Tony Nguyen6d599942019-06-26 02:20:17 -07002318 */
2319static void ice_check_media_subtask(struct ice_pf *pf)
2320{
2321 struct ice_port_info *pi;
2322 struct ice_vsi *vsi;
2323 int err;
2324
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002325 /* No need to check for media if it's already present */
2326 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
Tony Nguyen6d599942019-06-26 02:20:17 -07002327 return;
2328
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002329 vsi = ice_get_main_vsi(pf);
2330 if (!vsi)
Tony Nguyen6d599942019-06-26 02:20:17 -07002331 return;
2332
2333 /* Refresh link info and check if media is present */
2334 pi = vsi->port_info;
2335 err = ice_update_link_info(pi);
2336 if (err)
2337 return;
2338
Brett Creeley99d40752021-10-13 09:02:19 -07002339 ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
Anirudh Venkataramananc77849f52021-05-06 08:40:01 -07002340
Tony Nguyen6d599942019-06-26 02:20:17 -07002341 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002342 if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state))
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002343 ice_init_phy_user_cfg(pi);
2344
2345 /* PHY settings are reset on media insertion, reconfigure
2346 * PHY to preserve settings.
2347 */
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -08002348 if (test_bit(ICE_VSI_DOWN, vsi->state) &&
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002349 test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
Tony Nguyen6d599942019-06-26 02:20:17 -07002350 return;
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07002351
2352 err = ice_configure_phy(vsi);
2353 if (!err)
2354 clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
Tony Nguyen6d599942019-06-26 02:20:17 -07002355
2356 /* A Link Status Event will be generated; the event handler
2357 * will complete bringing the interface up
2358 */
2359 }
2360}
2361
2362/**
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07002363 * ice_service_task - manage and run subtasks
2364 * @work: pointer to work_struct contained by the PF struct
2365 */
2366static void ice_service_task(struct work_struct *work)
2367{
2368 struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
2369 unsigned long start_time = jiffies;
2370
2371 /* subtasks */
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07002372
2373 /* process reset requests first */
2374 ice_reset_subtask(pf);
2375
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07002376 /* bail if a reset/recovery cycle is pending or rebuild failed */
Dave Ertman5df7e452018-09-19 17:23:11 -07002377 if (ice_is_reset_in_progress(pf->state) ||
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002378 test_bit(ICE_SUSPENDED, pf->state) ||
2379 test_bit(ICE_NEEDS_RESTART, pf->state)) {
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07002380 ice_service_task_complete(pf);
2381 return;
2382 }
2383
Alexander Lobakin32d53c02022-03-23 13:43:52 +01002384 if (test_and_clear_bit(ICE_AUX_ERR_PENDING, pf->state)) {
2385 struct iidc_event *event;
2386
2387 event = kzalloc(sizeof(*event), GFP_KERNEL);
2388 if (event) {
2389 set_bit(IIDC_EVENT_CRIT_ERR, event->type);
2390 /* report the entire OICR value to AUX driver */
2391 swap(event->reg, pf->oicr_err_reg);
2392 ice_send_event_to_aux(pf, event);
2393 kfree(event);
2394 }
2395 }
2396
Dave Ertman248401cb2023-03-10 11:48:33 -08002397 /* unplug aux dev per request, if an unplug request came in
2398 * while processing a plug request, this will handle it
2399 */
2400 if (test_and_clear_bit(ICE_FLAG_UNPLUG_AUX_DEV, pf->flags))
2401 ice_unplug_aux_dev(pf);
Dave Ertman5dbbbd02022-01-20 16:27:56 -08002402
Dave Ertman248401cb2023-03-10 11:48:33 -08002403 /* Plug aux device per request */
2404 if (test_and_clear_bit(ICE_FLAG_PLUG_AUX_DEV, pf->flags))
2405 ice_plug_aux_dev(pf);
Ivan Vecera5cb1ebd2022-03-10 18:16:41 +01002406
Dave Ertman97b01292022-02-18 12:39:25 -08002407 if (test_and_clear_bit(ICE_FLAG_MTU_CHANGED, pf->flags)) {
2408 struct iidc_event *event;
2409
2410 event = kzalloc(sizeof(*event), GFP_KERNEL);
2411 if (event) {
2412 set_bit(IIDC_EVENT_AFTER_MTU_CHANGE, event->type);
2413 ice_send_event_to_aux(pf, event);
2414 kfree(event);
2415 }
2416 }
2417
Tony Nguyen462acf6a2019-09-09 06:47:46 -07002418 ice_clean_adminq_subtask(pf);
Tony Nguyen6d599942019-06-26 02:20:17 -07002419 ice_check_media_subtask(pf);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07002420 ice_check_for_hang_subtask(pf);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07002421 ice_sync_fltr_subtask(pf);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07002422 ice_handle_mdd_event(pf);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07002423 ice_watchdog_subtask(pf);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07002424
2425 if (ice_is_safe_mode(pf)) {
2426 ice_service_task_complete(pf);
2427 return;
2428 }
2429
2430 ice_process_vflr_event(pf);
Anirudh Venkataramanan75d2b252018-09-19 17:42:54 -07002431 ice_clean_mailboxq_subtask(pf);
Jacob Keller8f5ee3c2021-06-09 09:39:46 -07002432 ice_clean_sbq_subtask(pf);
Brett Creeley28bf2672020-05-11 18:01:46 -07002433 ice_sync_arfs_fltrs(pf);
Qi Zhangd6218312021-03-09 11:08:10 +08002434 ice_flush_fdir_ctx(pf);
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002435
2436 /* Clear ICE_SERVICE_SCHED flag to allow scheduling next event */
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07002437 ice_service_task_complete(pf);
2438
2439 /* If the tasks have taken longer than one service timer period
2440 * or there is more work to be done, reset the service timer to
2441 * schedule the service task now.
2442 */
2443 if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002444 test_bit(ICE_MDD_EVENT_PENDING, pf->state) ||
2445 test_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2446 test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
2447 test_bit(ICE_FD_VF_FLUSH_CTX, pf->state) ||
Jacob Keller8f5ee3c2021-06-09 09:39:46 -07002448 test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state) ||
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002449 test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07002450 mod_timer(&pf->serv_tmr, jiffies);
2451}
2452
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07002453/**
Anirudh Venkataramananf31e4b62018-03-20 07:58:07 -07002454 * ice_set_ctrlq_len - helper function to set controlq length
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -08002455 * @hw: pointer to the HW instance
Anirudh Venkataramananf31e4b62018-03-20 07:58:07 -07002456 */
2457static void ice_set_ctrlq_len(struct ice_hw *hw)
2458{
2459 hw->adminq.num_rq_entries = ICE_AQ_LEN;
2460 hw->adminq.num_sq_entries = ICE_AQ_LEN;
2461 hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
2462 hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
Lukasz Czapnikc8a10712020-02-27 10:15:00 -08002463 hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
Brett Creeley11836212019-07-25 01:55:38 -07002464 hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
Anirudh Venkataramanan75d2b252018-09-19 17:42:54 -07002465 hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2466 hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
Jacob Keller8f5ee3c2021-06-09 09:39:46 -07002467 hw->sbq.num_rq_entries = ICE_SBQ_LEN;
2468 hw->sbq.num_sq_entries = ICE_SBQ_LEN;
2469 hw->sbq.rq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2470 hw->sbq.sq_buf_size = ICE_SBQ_MAX_BUF_LEN;
Anirudh Venkataramananf31e4b62018-03-20 07:58:07 -07002471}
2472
2473/**
Henry Tieman87324e72019-11-08 06:23:29 -08002474 * ice_schedule_reset - schedule a reset
2475 * @pf: board private structure
2476 * @reset: reset being requested
2477 */
2478int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
2479{
2480 struct device *dev = ice_pf_to_dev(pf);
2481
2482 /* bail out if earlier reset has failed */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002483 if (test_bit(ICE_RESET_FAILED, pf->state)) {
Henry Tieman87324e72019-11-08 06:23:29 -08002484 dev_dbg(dev, "earlier reset has failed\n");
2485 return -EIO;
2486 }
2487 /* bail if reset/recovery already in progress */
2488 if (ice_is_reset_in_progress(pf->state)) {
2489 dev_dbg(dev, "Reset already in progress\n");
2490 return -EBUSY;
2491 }
2492
2493 switch (reset) {
2494 case ICE_RESET_PFR:
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002495 set_bit(ICE_PFR_REQ, pf->state);
Henry Tieman87324e72019-11-08 06:23:29 -08002496 break;
2497 case ICE_RESET_CORER:
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002498 set_bit(ICE_CORER_REQ, pf->state);
Henry Tieman87324e72019-11-08 06:23:29 -08002499 break;
2500 case ICE_RESET_GLOBR:
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08002501 set_bit(ICE_GLOBR_REQ, pf->state);
Henry Tieman87324e72019-11-08 06:23:29 -08002502 break;
2503 default:
2504 return -EINVAL;
2505 }
2506
2507 ice_service_task_schedule(pf);
2508 return 0;
2509}
2510
2511/**
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002512 * ice_irq_affinity_notify - Callback for affinity changes
2513 * @notify: context as to what irq was changed
2514 * @mask: the new affinity mask
2515 *
2516 * This is a callback function used by the irq_set_affinity_notifier function
2517 * so that we may register to receive changes to the irq affinity masks.
2518 */
Bruce Allanc8b7abd2019-02-26 16:35:11 -08002519static void
2520ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2521 const cpumask_t *mask)
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002522{
2523 struct ice_q_vector *q_vector =
2524 container_of(notify, struct ice_q_vector, affinity_notify);
2525
2526 cpumask_copy(&q_vector->affinity_mask, mask);
2527}
2528
2529/**
2530 * ice_irq_affinity_release - Callback for affinity notifier release
2531 * @ref: internal core kernel usage
2532 *
2533 * This is a callback function used by the irq_set_affinity_notifier function
2534 * to inform the current notification subscriber that they will no longer
2535 * receive notifications.
2536 */
2537static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2538
2539/**
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002540 * ice_vsi_ena_irq - Enable IRQ for the given VSI
2541 * @vsi: the VSI being configured
2542 */
2543static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2544{
Brett Creeleyba880732019-06-26 02:20:25 -07002545 struct ice_hw *hw = &vsi->back->hw;
2546 int i;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002547
Brett Creeleyba880732019-06-26 02:20:25 -07002548 ice_for_each_q_vector(vsi, i)
2549 ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002550
2551 ice_flush(hw);
2552 return 0;
2553}
2554
2555/**
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002556 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2557 * @vsi: the VSI being configured
2558 * @basename: name for the vector
2559 */
2560static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2561{
2562 int q_vectors = vsi->num_q_vectors;
2563 struct ice_pf *pf = vsi->back;
Brett Creeley4015d112019-11-08 06:23:26 -08002564 struct device *dev;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002565 int rx_int_idx = 0;
2566 int tx_int_idx = 0;
2567 int vector, err;
2568 int irq_num;
2569
Brett Creeley4015d112019-11-08 06:23:26 -08002570 dev = ice_pf_to_dev(pf);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002571 for (vector = 0; vector < q_vectors; vector++) {
2572 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2573
Piotr Raczynski4aad5332023-05-15 21:03:17 +02002574 irq_num = q_vector->irq.virq;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002575
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02002576 if (q_vector->tx.tx_ring && q_vector->rx.rx_ring) {
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002577 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2578 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2579 tx_int_idx++;
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02002580 } else if (q_vector->rx.rx_ring) {
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002581 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2582 "%s-%s-%d", basename, "rx", rx_int_idx++);
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02002583 } else if (q_vector->tx.tx_ring) {
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002584 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2585 "%s-%s-%d", basename, "tx", tx_int_idx++);
2586 } else {
2587 /* skip this unused q_vector */
2588 continue;
2589 }
Jacob Kellerb03d5192022-02-16 13:37:29 -08002590 if (vsi->type == ICE_VSI_CTRL && vsi->vf)
Qi Zhangda62c5f2021-03-09 11:08:03 +08002591 err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2592 IRQF_SHARED, q_vector->name,
2593 q_vector);
2594 else
2595 err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2596 0, q_vector->name, q_vector);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002597 if (err) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08002598 netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2599 err);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002600 goto free_q_irqs;
2601 }
2602
2603 /* register for affinity change notifications */
Brett Creeley28bf2672020-05-11 18:01:46 -07002604 if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2605 struct irq_affinity_notify *affinity_notify;
2606
2607 affinity_notify = &q_vector->affinity_notify;
2608 affinity_notify->notify = ice_irq_affinity_notify;
2609 affinity_notify->release = ice_irq_affinity_release;
2610 irq_set_affinity_notifier(irq_num, affinity_notify);
2611 }
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002612
2613 /* assign the mask for this irq */
Michal Schmidtdee55762024-06-07 14:22:34 -07002614 irq_update_affinity_hint(irq_num, &q_vector->affinity_mask);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002615 }
2616
Alexander Lobakind7442f52022-04-04 18:15:09 +02002617 err = ice_set_cpu_rx_rmap(vsi);
2618 if (err) {
2619 netdev_err(vsi->netdev, "Failed to setup CPU RMAP on VSI %u: %pe\n",
2620 vsi->vsi_num, ERR_PTR(err));
2621 goto free_q_irqs;
2622 }
2623
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002624 vsi->irqs_ready = true;
2625 return 0;
2626
2627free_q_irqs:
Piotr Raczynski4aad5332023-05-15 21:03:17 +02002628 while (vector--) {
2629 irq_num = vsi->q_vectors[vector]->irq.virq;
Brett Creeley28bf2672020-05-11 18:01:46 -07002630 if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2631 irq_set_affinity_notifier(irq_num, NULL);
Michal Schmidtdee55762024-06-07 14:22:34 -07002632 irq_update_affinity_hint(irq_num, NULL);
Brett Creeley4015d112019-11-08 06:23:26 -08002633 devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07002634 }
2635 return err;
2636}
2637
2638/**
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002639 * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2640 * @vsi: VSI to setup Tx rings used by XDP
2641 *
2642 * Return 0 on success and negative value on error
2643 */
2644static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2645{
Anirudh Venkataramanan9a946842020-02-06 01:20:09 -08002646 struct device *dev = ice_pf_to_dev(vsi->back);
Maciej Fijalkowski9610bd92021-08-19 14:00:02 +02002647 struct ice_tx_desc *tx_desc;
2648 int i, j;
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002649
Maciej Fijalkowski2faf63b2021-08-19 14:00:04 +02002650 ice_for_each_xdp_txq(vsi, i) {
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002651 u16 xdp_q_idx = vsi->alloc_txq + i;
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -05002652 struct ice_ring_stats *ring_stats;
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02002653 struct ice_tx_ring *xdp_ring;
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002654
2655 xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002656 if (!xdp_ring)
2657 goto free_xdp_rings;
2658
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -05002659 ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
2660 if (!ring_stats) {
2661 ice_free_tx_ring(xdp_ring);
2662 goto free_xdp_rings;
2663 }
2664
2665 xdp_ring->ring_stats = ring_stats;
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002666 xdp_ring->q_index = xdp_q_idx;
2667 xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002668 xdp_ring->vsi = vsi;
2669 xdp_ring->netdev = NULL;
2670 xdp_ring->dev = dev;
2671 xdp_ring->count = vsi->num_tx_desc;
Ciara Loftusb1d95cc2020-06-09 13:19:45 +00002672 WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002673 if (ice_setup_tx_ring(xdp_ring))
2674 goto free_xdp_rings;
2675 ice_set_ring_xdp(xdp_ring);
Maciej Fijalkowski22bf8772021-08-19 14:00:03 +02002676 spin_lock_init(&xdp_ring->tx_lock);
Maciej Fijalkowski9610bd92021-08-19 14:00:02 +02002677 for (j = 0; j < xdp_ring->count; j++) {
2678 tx_desc = ICE_TX_DESC(xdp_ring, j);
Maciej Fijalkowskie19778e2022-03-17 19:36:29 +01002679 tx_desc->cmd_type_offset_bsz = 0;
Maciej Fijalkowski9610bd92021-08-19 14:00:02 +02002680 }
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002681 }
2682
2683 return 0;
2684
2685free_xdp_rings:
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -05002686 for (; i >= 0; i--) {
2687 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc) {
2688 kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2689 vsi->xdp_rings[i]->ring_stats = NULL;
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002690 ice_free_tx_ring(vsi->xdp_rings[i]);
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -05002691 }
2692 }
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002693 return -ENOMEM;
2694}
2695
2696/**
2697 * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2698 * @vsi: VSI to set the bpf prog on
2699 * @prog: the bpf prog pointer
2700 */
2701static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2702{
2703 struct bpf_prog *old_prog;
2704 int i;
2705
2706 old_prog = xchg(&vsi->xdp_prog, prog);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002707 ice_for_each_rxq(vsi, i)
2708 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
Maciej Fijalkowski469748422023-06-15 13:33:26 +02002709
2710 if (old_prog)
2711 bpf_prog_put(old_prog);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002712}
2713
Larysa Zarembaf3df4042024-06-03 14:42:34 -07002714static struct ice_tx_ring *ice_xdp_ring_from_qid(struct ice_vsi *vsi, int qid)
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002715{
Larysa Zarembaf3df4042024-06-03 14:42:34 -07002716 struct ice_q_vector *q_vector;
2717 struct ice_tx_ring *ring;
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002718
Maciej Fijalkowski22bf8772021-08-19 14:00:03 +02002719 if (static_key_enabled(&ice_xdp_locking_key))
Larysa Zarembaf3df4042024-06-03 14:42:34 -07002720 return vsi->xdp_rings[qid % vsi->num_xdp_txq];
Maciej Fijalkowski22bf8772021-08-19 14:00:03 +02002721
Larysa Zarembaf3df4042024-06-03 14:42:34 -07002722 q_vector = vsi->rx_rings[qid]->q_vector;
2723 ice_for_each_tx_ring(ring, q_vector->tx)
2724 if (ice_ring_is_xdp(ring))
2725 return ring;
2726
2727 return NULL;
2728}
2729
2730/**
2731 * ice_map_xdp_rings - Map XDP rings to interrupt vectors
2732 * @vsi: the VSI with XDP rings being configured
2733 *
2734 * Map XDP rings to interrupt vectors and perform the configuration steps
2735 * dependent on the mapping.
2736 */
2737void ice_map_xdp_rings(struct ice_vsi *vsi)
2738{
2739 int xdp_rings_rem = vsi->num_xdp_txq;
2740 int v_idx, q_idx;
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002741
2742 /* follow the logic from ice_vsi_map_rings_to_vectors */
2743 ice_for_each_q_vector(vsi, v_idx) {
2744 struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2745 int xdp_rings_per_v, q_id, q_base;
2746
2747 xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2748 vsi->num_q_vectors - v_idx);
2749 q_base = vsi->num_xdp_txq - xdp_rings_rem;
2750
2751 for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02002752 struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_id];
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002753
2754 xdp_ring->q_vector = q_vector;
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02002755 xdp_ring->next = q_vector->tx.tx_ring;
2756 q_vector->tx.tx_ring = xdp_ring;
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002757 }
2758 xdp_rings_rem -= xdp_rings_per_v;
2759 }
2760
Larysa Zarembaf3df4042024-06-03 14:42:34 -07002761 ice_for_each_rxq(vsi, q_idx) {
2762 vsi->rx_rings[q_idx]->xdp_ring = ice_xdp_ring_from_qid(vsi,
2763 q_idx);
2764 ice_tx_xsk_pool(vsi, q_idx);
Maciej Fijalkowski9ead7e72022-08-11 20:21:49 +02002765 }
Larysa Zarembaf3df4042024-06-03 14:42:34 -07002766}
2767
2768/**
2769 * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2770 * @vsi: VSI to bring up Tx rings used by XDP
2771 * @prog: bpf program that will be assigned to VSI
2772 * @cfg_type: create from scratch or restore the existing configuration
2773 *
2774 * Return 0 on success and negative value on error
2775 */
2776int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog,
2777 enum ice_xdp_cfg cfg_type)
2778{
2779 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2780 struct ice_pf *pf = vsi->back;
2781 struct ice_qs_cfg xdp_qs_cfg = {
2782 .qs_mutex = &pf->avail_q_mutex,
2783 .pf_map = pf->avail_txqs,
2784 .pf_map_size = pf->max_pf_txqs,
2785 .q_count = vsi->num_xdp_txq,
2786 .scatter_count = ICE_MAX_SCATTER_TXQS,
2787 .vsi_map = vsi->txq_map,
2788 .vsi_map_offset = vsi->alloc_txq,
2789 .mapping_mode = ICE_VSI_MAP_CONTIG
2790 };
2791 struct device *dev;
2792 int status, i;
2793
2794 dev = ice_pf_to_dev(pf);
2795 vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2796 sizeof(*vsi->xdp_rings), GFP_KERNEL);
2797 if (!vsi->xdp_rings)
2798 return -ENOMEM;
2799
2800 vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2801 if (__ice_vsi_get_qs(&xdp_qs_cfg))
2802 goto err_map_xdp;
2803
2804 if (static_key_enabled(&ice_xdp_locking_key))
2805 netdev_warn(vsi->netdev,
2806 "Could not allocate one XDP Tx ring per CPU, XDP_TX/XDP_REDIRECT actions will be slower\n");
2807
2808 if (ice_xdp_alloc_setup_rings(vsi))
2809 goto clear_xdp_rings;
Maciej Fijalkowski9ead7e72022-08-11 20:21:49 +02002810
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002811 /* omit the scheduler update if in reset path; XDP queues will be
2812 * taken into account at the end of ice_vsi_rebuild, where
2813 * ice_cfg_vsi_lan is being called
2814 */
Larysa Zaremba744d1972024-06-03 14:42:33 -07002815 if (cfg_type == ICE_XDP_CFG_PART)
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002816 return 0;
2817
Larysa Zarembaf3df4042024-06-03 14:42:34 -07002818 ice_map_xdp_rings(vsi);
2819
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002820 /* tell the Tx scheduler that right now we have
2821 * additional queues
2822 */
2823 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2824 max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2825
2826 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2827 max_txqs);
2828 if (status) {
Tony Nguyen5f87ec42021-10-07 15:56:02 -07002829 dev_err(dev, "Failed VSI LAN queue config for XDP, error: %d\n",
2830 status);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002831 goto clear_xdp_rings;
2832 }
Marta Plantykowf65ee532021-10-26 18:47:19 +02002833
2834 /* assign the prog only when it's not already present on VSI;
2835 * this flow is a subject of both ethtool -L and ndo_bpf flows;
2836 * VSI rebuild that happens under ethtool -L can expose us to
2837 * the bpf_prog refcount issues as we would be swapping same
2838 * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
2839 * on it as it would be treated as an 'old_prog'; for ndo_bpf
2840 * this is not harmful as dev_xdp_install bumps the refcount
2841 * before calling the op exposed by the driver;
2842 */
2843 if (!ice_is_xdp_ena_vsi(vsi))
2844 ice_vsi_assign_bpf_prog(vsi, prog);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002845
2846 return 0;
2847clear_xdp_rings:
Maciej Fijalkowski2faf63b2021-08-19 14:00:04 +02002848 ice_for_each_xdp_txq(vsi, i)
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002849 if (vsi->xdp_rings[i]) {
2850 kfree_rcu(vsi->xdp_rings[i], rcu);
2851 vsi->xdp_rings[i] = NULL;
2852 }
2853
2854err_map_xdp:
2855 mutex_lock(&pf->avail_q_mutex);
Maciej Fijalkowski2faf63b2021-08-19 14:00:04 +02002856 ice_for_each_xdp_txq(vsi, i) {
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002857 clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2858 vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2859 }
2860 mutex_unlock(&pf->avail_q_mutex);
2861
Brett Creeley4015d112019-11-08 06:23:26 -08002862 devm_kfree(dev, vsi->xdp_rings);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002863 return -ENOMEM;
2864}
2865
2866/**
2867 * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2868 * @vsi: VSI to remove XDP rings
Larysa Zaremba744d1972024-06-03 14:42:33 -07002869 * @cfg_type: disable XDP permanently or allow it to be restored later
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002870 *
2871 * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2872 * resources
2873 */
Larysa Zaremba744d1972024-06-03 14:42:33 -07002874int ice_destroy_xdp_rings(struct ice_vsi *vsi, enum ice_xdp_cfg cfg_type)
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002875{
2876 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2877 struct ice_pf *pf = vsi->back;
2878 int i, v_idx;
2879
2880 /* q_vectors are freed in reset path so there's no point in detaching
Larysa Zaremba744d1972024-06-03 14:42:33 -07002881 * rings
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002882 */
Larysa Zaremba744d1972024-06-03 14:42:33 -07002883 if (cfg_type == ICE_XDP_CFG_PART)
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002884 goto free_qmap;
2885
2886 ice_for_each_q_vector(vsi, v_idx) {
2887 struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02002888 struct ice_tx_ring *ring;
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002889
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02002890 ice_for_each_tx_ring(ring, q_vector->tx)
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002891 if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2892 break;
2893
2894 /* restore the value of last node prior to XDP setup */
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02002895 q_vector->tx.tx_ring = ring;
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002896 }
2897
2898free_qmap:
2899 mutex_lock(&pf->avail_q_mutex);
Maciej Fijalkowski2faf63b2021-08-19 14:00:04 +02002900 ice_for_each_xdp_txq(vsi, i) {
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002901 clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2902 vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2903 }
2904 mutex_unlock(&pf->avail_q_mutex);
2905
Maciej Fijalkowski2faf63b2021-08-19 14:00:04 +02002906 ice_for_each_xdp_txq(vsi, i)
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002907 if (vsi->xdp_rings[i]) {
Maciej Fijalkowskif9124c62022-03-17 19:36:27 +01002908 if (vsi->xdp_rings[i]->desc) {
2909 synchronize_rcu();
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002910 ice_free_tx_ring(vsi->xdp_rings[i]);
Maciej Fijalkowskif9124c62022-03-17 19:36:27 +01002911 }
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -05002912 kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2913 vsi->xdp_rings[i]->ring_stats = NULL;
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002914 kfree_rcu(vsi->xdp_rings[i], rcu);
2915 vsi->xdp_rings[i] = NULL;
2916 }
2917
Brett Creeley4015d112019-11-08 06:23:26 -08002918 devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002919 vsi->xdp_rings = NULL;
2920
Maciej Fijalkowski22bf8772021-08-19 14:00:03 +02002921 if (static_key_enabled(&ice_xdp_locking_key))
2922 static_branch_dec(&ice_xdp_locking_key);
2923
Larysa Zaremba744d1972024-06-03 14:42:33 -07002924 if (cfg_type == ICE_XDP_CFG_PART)
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002925 return 0;
2926
2927 ice_vsi_assign_bpf_prog(vsi, NULL);
2928
2929 /* notify Tx scheduler that we destroyed XDP queues and bring
2930 * back the old number of child nodes
2931 */
2932 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2933 max_txqs[i] = vsi->num_txq;
2934
Marta Plantykowc8f135c2020-05-15 17:42:15 -07002935 /* change number of XDP Tx queues to 0 */
2936 vsi->num_xdp_txq = 0;
2937
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002938 return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2939 max_txqs);
2940}
2941
2942/**
Michal Swiatkowskic7a21902020-11-02 04:37:27 -05002943 * ice_vsi_rx_napi_schedule - Schedule napi on RX queues from VSI
2944 * @vsi: VSI to schedule napi on
2945 */
2946static void ice_vsi_rx_napi_schedule(struct ice_vsi *vsi)
2947{
2948 int i;
2949
2950 ice_for_each_rxq(vsi, i) {
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02002951 struct ice_rx_ring *rx_ring = vsi->rx_rings[i];
Michal Swiatkowskic7a21902020-11-02 04:37:27 -05002952
Maciej Fijalkowskiebc33a32024-07-26 20:17:14 +02002953 if (READ_ONCE(rx_ring->xsk_pool))
Michal Swiatkowskic7a21902020-11-02 04:37:27 -05002954 napi_schedule(&rx_ring->q_vector->napi);
2955 }
2956}
2957
2958/**
Maciej Fijalkowski22bf8772021-08-19 14:00:03 +02002959 * ice_vsi_determine_xdp_res - figure out how many Tx qs can XDP have
2960 * @vsi: VSI to determine the count of XDP Tx qs
2961 *
2962 * returns 0 if Tx qs count is higher than at least half of CPU count,
2963 * -ENOMEM otherwise
2964 */
2965int ice_vsi_determine_xdp_res(struct ice_vsi *vsi)
2966{
2967 u16 avail = ice_get_avail_txq_count(vsi->back);
2968 u16 cpus = num_possible_cpus();
2969
2970 if (avail < cpus / 2)
2971 return -ENOMEM;
2972
2973 vsi->num_xdp_txq = min_t(u16, avail, cpus);
2974
2975 if (vsi->num_xdp_txq < cpus)
2976 static_branch_inc(&ice_xdp_locking_key);
2977
2978 return 0;
2979}
2980
2981/**
Maciej Fijalkowski60bc72b2023-01-31 21:45:00 +01002982 * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2983 * @vsi: Pointer to VSI structure
2984 */
2985static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
2986{
2987 if (test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
2988 return ICE_RXBUF_1664;
2989 else
2990 return ICE_RXBUF_3072;
2991}
2992
2993/**
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08002994 * ice_xdp_setup_prog - Add or remove XDP eBPF program
2995 * @vsi: VSI to setup XDP for
2996 * @prog: XDP program
2997 * @extack: netlink extended ack
2998 */
2999static int
3000ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
3001 struct netlink_ext_ack *extack)
3002{
Maciej Fijalkowski60bc72b2023-01-31 21:45:00 +01003003 unsigned int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08003004 bool if_running = netif_running(vsi->netdev);
3005 int ret = 0, xdp_ring_err = 0;
3006
Maciej Fijalkowski2fba7dc2023-01-31 21:45:03 +01003007 if (prog && !prog->aux->xdp_has_frags) {
3008 if (frame_size > ice_max_xdp_frame_size(vsi)) {
3009 NL_SET_ERR_MSG_MOD(extack,
3010 "MTU is too large for linear frames and XDP prog does not support frags");
3011 return -EOPNOTSUPP;
3012 }
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08003013 }
3014
Maciej Fijalkowski469748422023-06-15 13:33:26 +02003015 /* hot swap progs and avoid toggling link */
3016 if (ice_is_xdp_ena_vsi(vsi) == !!prog) {
3017 ice_vsi_assign_bpf_prog(vsi, prog);
3018 return 0;
3019 }
3020
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08003021 /* need to stop netdev while setting up the program for Rx rings */
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -08003022 if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08003023 ret = ice_down(vsi);
3024 if (ret) {
Jesse Brandeburgaf236352020-02-13 13:31:26 -08003025 NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08003026 return ret;
3027 }
3028 }
3029
3030 if (!ice_is_xdp_ena_vsi(vsi) && prog) {
Maciej Fijalkowski22bf8772021-08-19 14:00:03 +02003031 xdp_ring_err = ice_vsi_determine_xdp_res(vsi);
3032 if (xdp_ring_err) {
3033 NL_SET_ERR_MSG_MOD(extack, "Not enough Tx resources for XDP");
3034 } else {
Larysa Zaremba744d1972024-06-03 14:42:33 -07003035 xdp_ring_err = ice_prepare_xdp_rings(vsi, prog,
3036 ICE_XDP_CFG_FULL);
Maciej Fijalkowski22bf8772021-08-19 14:00:03 +02003037 if (xdp_ring_err)
3038 NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
3039 }
Lorenzo Bianconib6a41032023-02-14 15:39:27 +01003040 xdp_features_set_redirect_target(vsi->netdev, true);
Przemyslaw Patynowski7e753eb2022-08-11 12:09:22 +02003041 /* reallocate Rx queues that are used for zero-copy */
3042 xdp_ring_err = ice_realloc_zc_buf(vsi, true);
3043 if (xdp_ring_err)
3044 NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Rx resources failed");
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08003045 } else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
Marek Majtyka66c0e132023-02-01 11:24:18 +01003046 xdp_features_clear_redirect_target(vsi->netdev);
Larysa Zaremba744d1972024-06-03 14:42:33 -07003047 xdp_ring_err = ice_destroy_xdp_rings(vsi, ICE_XDP_CFG_FULL);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08003048 if (xdp_ring_err)
Jesse Brandeburgaf236352020-02-13 13:31:26 -08003049 NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
Przemyslaw Patynowski7e753eb2022-08-11 12:09:22 +02003050 /* reallocate Rx queues that were used for zero-copy */
3051 xdp_ring_err = ice_realloc_zc_buf(vsi, false);
3052 if (xdp_ring_err)
3053 NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Rx resources failed");
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08003054 }
3055
3056 if (if_running)
3057 ret = ice_up(vsi);
3058
Michal Swiatkowskic7a21902020-11-02 04:37:27 -05003059 if (!ret && prog)
3060 ice_vsi_rx_napi_schedule(vsi);
Krzysztof Kazimierczak2d4238f2019-11-04 09:38:56 -08003061
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08003062 return (ret || xdp_ring_err) ? -ENOMEM : 0;
3063}
3064
3065/**
Maciej Fijalkowskiebc53992021-05-20 08:34:59 +02003066 * ice_xdp_safe_mode - XDP handler for safe mode
3067 * @dev: netdevice
3068 * @xdp: XDP command
3069 */
3070static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
3071 struct netdev_bpf *xdp)
3072{
3073 NL_SET_ERR_MSG_MOD(xdp->extack,
3074 "Please provide working DDP firmware package in order to use XDP\n"
3075 "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
3076 return -EOPNOTSUPP;
3077}
3078
3079/**
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08003080 * ice_xdp - implements XDP handler
3081 * @dev: netdevice
3082 * @xdp: XDP command
3083 */
3084static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3085{
3086 struct ice_netdev_priv *np = netdev_priv(dev);
3087 struct ice_vsi *vsi = np->vsi;
3088
3089 if (vsi->type != ICE_VSI_PF) {
Jesse Brandeburgaf236352020-02-13 13:31:26 -08003090 NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08003091 return -EINVAL;
3092 }
3093
3094 switch (xdp->command) {
3095 case XDP_SETUP_PROG:
3096 return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
Magnus Karlsson1742b3d2020-08-28 10:26:15 +02003097 case XDP_SETUP_XSK_POOL:
3098 return ice_xsk_pool_setup(vsi, xdp->xsk.pool,
Krzysztof Kazimierczak2d4238f2019-11-04 09:38:56 -08003099 xdp->xsk.queue_id);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08003100 default:
3101 return -EINVAL;
3102 }
3103}
3104
3105/**
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003106 * ice_ena_misc_vector - enable the non-queue interrupts
3107 * @pf: board private structure
3108 */
3109static void ice_ena_misc_vector(struct ice_pf *pf)
3110{
3111 struct ice_hw *hw = &pf->hw;
Karol Kolacinski82e71b22023-11-29 13:40:23 +01003112 u32 pf_intr_start_offset;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003113 u32 val;
3114
Paul Greenwalt9d5c5a52020-02-13 13:31:16 -08003115 /* Disable anti-spoof detection interrupt to prevent spurious event
3116 * interrupts during a function reset. Anti-spoof functionally is
3117 * still supported.
3118 */
3119 val = rd32(hw, GL_MDCK_TX_TDPU);
3120 val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
3121 wr32(hw, GL_MDCK_TX_TDPU, val);
3122
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003123 /* clear things first */
3124 wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
3125 rd32(hw, PFINT_OICR); /* read to clear */
3126
Bruce Allan3bcd7fa2018-08-09 06:28:59 -07003127 val = (PFINT_OICR_ECC_ERR_M |
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003128 PFINT_OICR_MAL_DETECT_M |
3129 PFINT_OICR_GRST_M |
3130 PFINT_OICR_PCI_EXCEPTION_M |
Anirudh Venkataramanan007676b2018-09-19 17:42:57 -07003131 PFINT_OICR_VFLR_M |
Bruce Allan3bcd7fa2018-08-09 06:28:59 -07003132 PFINT_OICR_HMC_ERR_M |
Dave Ertman348048e2021-05-20 09:37:50 -05003133 PFINT_OICR_PE_PUSH_M |
Bruce Allan3bcd7fa2018-08-09 06:28:59 -07003134 PFINT_OICR_PE_CRITERR_M);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003135
3136 wr32(hw, PFINT_OICR_ENA, val);
3137
3138 /* SW_ITR_IDX = 0, but don't change INTENA */
Piotr Raczynski4aad5332023-05-15 21:03:17 +02003139 wr32(hw, GLINT_DYN_CTL(pf->oicr_irq.index),
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003140 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
Karol Kolacinski82e71b22023-11-29 13:40:23 +01003141
3142 if (!pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3143 return;
3144 pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3145 wr32(hw, GLINT_DYN_CTL(pf->ll_ts_irq.index + pf_intr_start_offset),
3146 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
3147}
3148
3149/**
3150 * ice_ll_ts_intr - ll_ts interrupt handler
3151 * @irq: interrupt number
3152 * @data: pointer to a q_vector
3153 */
3154static irqreturn_t ice_ll_ts_intr(int __always_unused irq, void *data)
3155{
3156 struct ice_pf *pf = data;
3157 u32 pf_intr_start_offset;
3158 struct ice_ptp_tx *tx;
3159 unsigned long flags;
3160 struct ice_hw *hw;
3161 u32 val;
3162 u8 idx;
3163
3164 hw = &pf->hw;
3165 tx = &pf->ptp.port.tx;
3166 spin_lock_irqsave(&tx->lock, flags);
3167 ice_ptp_complete_tx_single_tstamp(tx);
3168
3169 idx = find_next_bit_wrap(tx->in_use, tx->len,
3170 tx->last_ll_ts_idx_read + 1);
3171 if (idx != tx->len)
3172 ice_ptp_req_tx_single_tstamp(tx, idx);
3173 spin_unlock_irqrestore(&tx->lock, flags);
3174
3175 val = GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M |
3176 (ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
3177 pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3178 wr32(hw, GLINT_DYN_CTL(pf->ll_ts_irq.index + pf_intr_start_offset),
3179 val);
3180
3181 return IRQ_HANDLED;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003182}
3183
3184/**
3185 * ice_misc_intr - misc interrupt handler
3186 * @irq: interrupt number
3187 * @data: pointer to a q_vector
3188 */
3189static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
3190{
3191 struct ice_pf *pf = (struct ice_pf *)data;
Karol Kolacinski00d50002023-11-29 13:40:22 +01003192 irqreturn_t ret = IRQ_HANDLED;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003193 struct ice_hw *hw = &pf->hw;
Brett Creeley4015d112019-11-08 06:23:26 -08003194 struct device *dev;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003195 u32 oicr, ena_mask;
3196
Brett Creeley4015d112019-11-08 06:23:26 -08003197 dev = ice_pf_to_dev(pf);
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08003198 set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
3199 set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
Jacob Keller8f5ee3c2021-06-09 09:39:46 -07003200 set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003201
3202 oicr = rd32(hw, PFINT_OICR);
3203 ena_mask = rd32(hw, PFINT_OICR_ENA);
3204
Anirudh Venkataramanan0e674ae2019-04-16 10:30:43 -07003205 if (oicr & PFINT_OICR_SWINT_M) {
3206 ena_mask &= ~PFINT_OICR_SWINT_M;
3207 pf->sw_int_count++;
3208 }
3209
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07003210 if (oicr & PFINT_OICR_MAL_DETECT_M) {
3211 ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08003212 set_bit(ICE_MDD_EVENT_PENDING, pf->state);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07003213 }
Anirudh Venkataramanan007676b2018-09-19 17:42:57 -07003214 if (oicr & PFINT_OICR_VFLR_M) {
Brett Creeleyf844d522020-02-27 10:14:55 -08003215 /* disable any further VFLR event notifications */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08003216 if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
Brett Creeleyf844d522020-02-27 10:14:55 -08003217 u32 reg = rd32(hw, PFINT_OICR_ENA);
3218
3219 reg &= ~PFINT_OICR_VFLR_M;
3220 wr32(hw, PFINT_OICR_ENA, reg);
3221 } else {
3222 ena_mask &= ~PFINT_OICR_VFLR_M;
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08003223 set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
Brett Creeleyf844d522020-02-27 10:14:55 -08003224 }
Anirudh Venkataramanan007676b2018-09-19 17:42:57 -07003225 }
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07003226
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003227 if (oicr & PFINT_OICR_GRST_M) {
3228 u32 reset;
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07003229
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003230 /* we have a reset warning */
3231 ena_mask &= ~PFINT_OICR_GRST_M;
Jesse Brandeburg5a259f82023-12-05 17:01:12 -08003232 reset = FIELD_GET(GLGEN_RSTAT_RESET_TYPE_M,
3233 rd32(hw, GLGEN_RSTAT));
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003234
3235 if (reset == ICE_RESET_CORER)
3236 pf->corer_count++;
3237 else if (reset == ICE_RESET_GLOBR)
3238 pf->globr_count++;
Brett Creeleyca4929b2018-09-19 17:23:18 -07003239 else if (reset == ICE_RESET_EMPR)
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003240 pf->empr_count++;
Brett Creeleyca4929b2018-09-19 17:23:18 -07003241 else
Brett Creeley4015d112019-11-08 06:23:26 -08003242 dev_dbg(dev, "Invalid reset type %d\n", reset);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003243
3244 /* If a reset cycle isn't already in progress, we set a bit in
3245 * pf->state so that the service task can start a reset/rebuild.
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003246 */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08003247 if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003248 if (reset == ICE_RESET_CORER)
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08003249 set_bit(ICE_CORER_RECV, pf->state);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003250 else if (reset == ICE_RESET_GLOBR)
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08003251 set_bit(ICE_GLOBR_RECV, pf->state);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003252 else
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08003253 set_bit(ICE_EMPR_RECV, pf->state);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003254
Anirudh Venkataramananfd2a9812018-08-09 06:29:47 -07003255 /* There are couple of different bits at play here.
3256 * hw->reset_ongoing indicates whether the hardware is
3257 * in reset. This is set to true when a reset interrupt
3258 * is received and set back to false after the driver
3259 * has determined that the hardware is out of reset.
3260 *
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08003261 * ICE_RESET_OICR_RECV in pf->state indicates
Anirudh Venkataramananfd2a9812018-08-09 06:29:47 -07003262 * that a post reset rebuild is required before the
3263 * driver is operational again. This is set above.
3264 *
3265 * As this is the start of the reset/rebuild cycle, set
3266 * both to indicate that.
3267 */
3268 hw->reset_ongoing = true;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003269 }
3270 }
3271
Jacob Kellerea9b8472021-06-09 09:39:53 -07003272 if (oicr & PFINT_OICR_TSYN_TX_M) {
3273 ena_mask &= ~PFINT_OICR_TSYN_TX_M;
Karol Kolacinski82e71b22023-11-29 13:40:23 +01003274 if (ice_pf_state_is_nominal(pf) &&
3275 pf->hw.dev_caps.ts_dev_info.ts_ll_int_read) {
3276 struct ice_ptp_tx *tx = &pf->ptp.port.tx;
3277 unsigned long flags;
3278 u8 idx;
3279
3280 spin_lock_irqsave(&tx->lock, flags);
3281 idx = find_next_bit_wrap(tx->in_use, tx->len,
3282 tx->last_ll_ts_idx_read + 1);
3283 if (idx != tx->len)
3284 ice_ptp_req_tx_single_tstamp(tx, idx);
3285 spin_unlock_irqrestore(&tx->lock, flags);
3286 } else if (ice_ptp_pf_handles_tx_interrupt(pf)) {
Karol Kolacinski6e8b2c82023-06-01 14:15:03 -07003287 set_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread);
Karol Kolacinski00d50002023-11-29 13:40:22 +01003288 ret = IRQ_WAKE_THREAD;
3289 }
Jacob Kellerea9b8472021-06-09 09:39:53 -07003290 }
3291
Maciej Machnikowski172db5f2021-06-16 09:35:22 -07003292 if (oicr & PFINT_OICR_TSYN_EVNT_M) {
3293 u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3294 u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
3295
Maciej Machnikowski172db5f2021-06-16 09:35:22 -07003296 ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
Karol Kolacinski6e8b2c82023-06-01 14:15:03 -07003297
Jacob Keller42d40bb2023-09-08 14:37:14 -07003298 if (ice_pf_src_tmr_owned(pf)) {
Karol Kolacinski6e8b2c82023-06-01 14:15:03 -07003299 /* Save EVENTs from GLTSYN register */
3300 pf->ptp.ext_ts_irq |= gltsyn_stat &
3301 (GLTSYN_STAT_EVENT0_M |
3302 GLTSYN_STAT_EVENT1_M |
3303 GLTSYN_STAT_EVENT2_M);
3304
Karol Kolacinski00d50002023-11-29 13:40:22 +01003305 ice_ptp_extts_event(pf);
Karol Kolacinski6e8b2c82023-06-01 14:15:03 -07003306 }
Maciej Machnikowski172db5f2021-06-16 09:35:22 -07003307 }
3308
Dave Ertman348048e2021-05-20 09:37:50 -05003309#define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
3310 if (oicr & ICE_AUX_CRIT_ERR) {
Alexander Lobakin32d53c02022-03-23 13:43:52 +01003311 pf->oicr_err_reg |= oicr;
3312 set_bit(ICE_AUX_ERR_PENDING, pf->state);
Dave Ertman348048e2021-05-20 09:37:50 -05003313 ena_mask &= ~ICE_AUX_CRIT_ERR;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003314 }
3315
Md Fahad Iqbal Polash8d7189d2019-02-28 15:25:58 -08003316 /* Report any remaining unexpected interrupts */
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003317 oicr &= ena_mask;
3318 if (oicr) {
Brett Creeley4015d112019-11-08 06:23:26 -08003319 dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003320 /* If a critical error is pending there is no choice but to
3321 * reset the device.
3322 */
Dave Ertman348048e2021-05-20 09:37:50 -05003323 if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003324 PFINT_OICR_ECC_ERR_M)) {
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08003325 set_bit(ICE_PFR_REQ, pf->state);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003326 }
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003327 }
Karol Kolacinski00d50002023-11-29 13:40:22 +01003328 ice_service_task_schedule(pf);
3329 if (ret == IRQ_HANDLED)
3330 ice_irq_dynamic_ena(hw, NULL, NULL);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003331
Karol Kolacinski00d50002023-11-29 13:40:22 +01003332 return ret;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003333}
3334
3335/**
Karol Kolacinski1229b332022-09-16 13:17:28 -07003336 * ice_misc_intr_thread_fn - misc interrupt thread function
3337 * @irq: interrupt number
3338 * @data: pointer to a q_vector
3339 */
3340static irqreturn_t ice_misc_intr_thread_fn(int __always_unused irq, void *data)
3341{
Karol Kolacinski1229b332022-09-16 13:17:28 -07003342 struct ice_pf *pf = data;
Jacob Keller0ec38df2023-06-01 14:15:07 -07003343 struct ice_hw *hw;
3344
3345 hw = &pf->hw;
Karol Kolacinski1229b332022-09-16 13:17:28 -07003346
Jacob Keller30f15872022-11-18 14:27:29 -08003347 if (ice_is_reset_in_progress(pf->state))
Karol Kolacinski00d50002023-11-29 13:40:22 +01003348 goto skip_irq;
Karol Kolacinski6e8b2c82023-06-01 14:15:03 -07003349
3350 if (test_and_clear_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread)) {
Jacob Keller9a8648c2023-06-01 14:15:06 -07003351 /* Process outstanding Tx timestamps. If there is more work,
3352 * re-arm the interrupt to trigger again.
3353 */
3354 if (ice_ptp_process_ts(pf) == ICE_TX_TSTAMP_WORK_PENDING) {
3355 wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
3356 ice_flush(hw);
3357 }
Karol Kolacinski6e8b2c82023-06-01 14:15:03 -07003358 }
Jacob Keller30f15872022-11-18 14:27:29 -08003359
Karol Kolacinski00d50002023-11-29 13:40:22 +01003360skip_irq:
Jacob Keller0ec38df2023-06-01 14:15:07 -07003361 ice_irq_dynamic_ena(hw, NULL, NULL);
3362
Jacob Keller30f15872022-11-18 14:27:29 -08003363 return IRQ_HANDLED;
Karol Kolacinski1229b332022-09-16 13:17:28 -07003364}
3365
3366/**
Brett Creeley0e04e8e2019-02-08 12:50:34 -08003367 * ice_dis_ctrlq_interrupts - disable control queue interrupts
3368 * @hw: pointer to HW structure
3369 */
3370static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
3371{
3372 /* disable Admin queue Interrupt causes */
3373 wr32(hw, PFINT_FW_CTL,
3374 rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
3375
3376 /* disable Mailbox queue Interrupt causes */
3377 wr32(hw, PFINT_MBX_CTL,
3378 rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
3379
Jacob Keller8f5ee3c2021-06-09 09:39:46 -07003380 wr32(hw, PFINT_SB_CTL,
3381 rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
3382
Brett Creeley0e04e8e2019-02-08 12:50:34 -08003383 /* disable Control queue Interrupt causes */
3384 wr32(hw, PFINT_OICR_CTL,
3385 rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
3386
3387 ice_flush(hw);
3388}
3389
3390/**
Karol Kolacinski82e71b22023-11-29 13:40:23 +01003391 * ice_free_irq_msix_ll_ts- Unroll ll_ts vector setup
3392 * @pf: board private structure
3393 */
3394static void ice_free_irq_msix_ll_ts(struct ice_pf *pf)
3395{
3396 int irq_num = pf->ll_ts_irq.virq;
3397
3398 synchronize_irq(irq_num);
3399 devm_free_irq(ice_pf_to_dev(pf), irq_num, pf);
3400
3401 ice_free_irq(pf, pf->ll_ts_irq);
3402}
3403
3404/**
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003405 * ice_free_irq_msix_misc - Unroll misc vector setup
3406 * @pf: board private structure
3407 */
3408static void ice_free_irq_msix_misc(struct ice_pf *pf)
3409{
Piotr Raczynski4aad5332023-05-15 21:03:17 +02003410 int misc_irq_num = pf->oicr_irq.virq;
Brett Creeley0e04e8e2019-02-08 12:50:34 -08003411 struct ice_hw *hw = &pf->hw;
3412
3413 ice_dis_ctrlq_interrupts(hw);
3414
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003415 /* disable OICR interrupt */
Brett Creeley0e04e8e2019-02-08 12:50:34 -08003416 wr32(hw, PFINT_OICR_ENA, 0);
3417 ice_flush(hw);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003418
Piotr Raczynski05018932023-05-15 21:03:14 +02003419 synchronize_irq(misc_irq_num);
3420 devm_free_irq(ice_pf_to_dev(pf), misc_irq_num, pf);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003421
Piotr Raczynski4aad5332023-05-15 21:03:17 +02003422 ice_free_irq(pf, pf->oicr_irq);
Karol Kolacinski82e71b22023-11-29 13:40:23 +01003423 if (pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3424 ice_free_irq_msix_ll_ts(pf);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003425}
3426
3427/**
Brett Creeley0e04e8e2019-02-08 12:50:34 -08003428 * ice_ena_ctrlq_interrupts - enable control queue interrupts
3429 * @hw: pointer to HW structure
Brett Creeleyb07833a2019-02-28 15:25:59 -08003430 * @reg_idx: HW vector index to associate the control queue interrupts with
Brett Creeley0e04e8e2019-02-08 12:50:34 -08003431 */
Brett Creeleyb07833a2019-02-28 15:25:59 -08003432static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
Brett Creeley0e04e8e2019-02-08 12:50:34 -08003433{
3434 u32 val;
3435
Brett Creeleyb07833a2019-02-28 15:25:59 -08003436 val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
Brett Creeley0e04e8e2019-02-08 12:50:34 -08003437 PFINT_OICR_CTL_CAUSE_ENA_M);
3438 wr32(hw, PFINT_OICR_CTL, val);
3439
3440 /* enable Admin queue Interrupt causes */
Brett Creeleyb07833a2019-02-28 15:25:59 -08003441 val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
Brett Creeley0e04e8e2019-02-08 12:50:34 -08003442 PFINT_FW_CTL_CAUSE_ENA_M);
3443 wr32(hw, PFINT_FW_CTL, val);
3444
3445 /* enable Mailbox queue Interrupt causes */
Brett Creeleyb07833a2019-02-28 15:25:59 -08003446 val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
Brett Creeley0e04e8e2019-02-08 12:50:34 -08003447 PFINT_MBX_CTL_CAUSE_ENA_M);
3448 wr32(hw, PFINT_MBX_CTL, val);
3449
Karol Kolacinski82e71b22023-11-29 13:40:23 +01003450 if (!hw->dev_caps.ts_dev_info.ts_ll_int_read) {
3451 /* enable Sideband queue Interrupt causes */
3452 val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
3453 PFINT_SB_CTL_CAUSE_ENA_M);
3454 wr32(hw, PFINT_SB_CTL, val);
3455 }
Jacob Keller8f5ee3c2021-06-09 09:39:46 -07003456
Brett Creeley0e04e8e2019-02-08 12:50:34 -08003457 ice_flush(hw);
3458}
3459
3460/**
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003461 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
3462 * @pf: board private structure
3463 *
3464 * This sets up the handler for MSIX 0, which is used to manage the
Anirudh Venkataramanandf17b7e2018-10-26 11:44:46 -07003465 * non-queue interrupts, e.g. AdminQ and errors. This is not used
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003466 * when in MSI or Legacy interrupt mode.
3467 */
3468static int ice_req_irq_msix_misc(struct ice_pf *pf)
3469{
Brett Creeley4015d112019-11-08 06:23:26 -08003470 struct device *dev = ice_pf_to_dev(pf);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003471 struct ice_hw *hw = &pf->hw;
Karol Kolacinski82e71b22023-11-29 13:40:23 +01003472 u32 pf_intr_start_offset;
3473 struct msi_map irq;
Piotr Raczynski4aad5332023-05-15 21:03:17 +02003474 int err = 0;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003475
3476 if (!pf->int_name[0])
3477 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
Brett Creeley4015d112019-11-08 06:23:26 -08003478 dev_driver_string(dev), dev_name(dev));
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003479
Karol Kolacinski82e71b22023-11-29 13:40:23 +01003480 if (!pf->int_name_ll_ts[0])
3481 snprintf(pf->int_name_ll_ts, sizeof(pf->int_name_ll_ts) - 1,
3482 "%s-%s:ll_ts", dev_driver_string(dev), dev_name(dev));
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003483 /* Do not request IRQ but do enable OICR interrupt since settings are
3484 * lost during reset. Note that this function is called only during
3485 * rebuild path and not while reset is in progress.
3486 */
Dave Ertman5df7e452018-09-19 17:23:11 -07003487 if (ice_is_reset_in_progress(pf->state))
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003488 goto skip_req_irq;
3489
Brett Creeleycbe66bf2019-04-16 10:30:44 -07003490 /* reserve one vector in irq_tracker for misc interrupts */
Karol Kolacinski82e71b22023-11-29 13:40:23 +01003491 irq = ice_alloc_irq(pf, false);
3492 if (irq.index < 0)
3493 return irq.index;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003494
Karol Kolacinski82e71b22023-11-29 13:40:23 +01003495 pf->oicr_irq = irq;
Piotr Raczynski4aad5332023-05-15 21:03:17 +02003496 err = devm_request_threaded_irq(dev, pf->oicr_irq.virq, ice_misc_intr,
3497 ice_misc_intr_thread_fn, 0,
3498 pf->int_name, pf);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003499 if (err) {
Karol Kolacinski1229b332022-09-16 13:17:28 -07003500 dev_err(dev, "devm_request_threaded_irq for %s failed: %d\n",
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003501 pf->int_name, err);
Piotr Raczynski4aad5332023-05-15 21:03:17 +02003502 ice_free_irq(pf, pf->oicr_irq);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003503 return err;
3504 }
3505
Karol Kolacinski82e71b22023-11-29 13:40:23 +01003506 /* reserve one vector in irq_tracker for ll_ts interrupt */
3507 if (!pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3508 goto skip_req_irq;
3509
3510 irq = ice_alloc_irq(pf, false);
3511 if (irq.index < 0)
3512 return irq.index;
3513
3514 pf->ll_ts_irq = irq;
3515 err = devm_request_irq(dev, pf->ll_ts_irq.virq, ice_ll_ts_intr, 0,
3516 pf->int_name_ll_ts, pf);
3517 if (err) {
3518 dev_err(dev, "devm_request_irq for %s failed: %d\n",
3519 pf->int_name_ll_ts, err);
3520 ice_free_irq(pf, pf->ll_ts_irq);
3521 return err;
3522 }
3523
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07003524skip_req_irq:
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003525 ice_ena_misc_vector(pf);
3526
Piotr Raczynski4aad5332023-05-15 21:03:17 +02003527 ice_ena_ctrlq_interrupts(hw, pf->oicr_irq.index);
Karol Kolacinski82e71b22023-11-29 13:40:23 +01003528 /* This enables LL TS interrupt */
3529 pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3530 if (pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3531 wr32(hw, PFINT_SB_CTL,
3532 ((pf->ll_ts_irq.index + pf_intr_start_offset) &
3533 PFINT_SB_CTL_MSIX_INDX_M) | PFINT_SB_CTL_CAUSE_ENA_M);
Piotr Raczynski4aad5332023-05-15 21:03:17 +02003534 wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_irq.index),
Brett Creeley63f545e2018-12-19 10:03:29 -08003535 ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003536
3537 ice_flush(hw);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07003538 ice_irq_dynamic_ena(hw, NULL, NULL);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003539
3540 return 0;
3541}
3542
3543/**
Anirudh Venkataramanandf0f8472018-09-19 17:23:09 -07003544 * ice_napi_add - register NAPI handler for the VSI
3545 * @vsi: VSI for which NAPI handler is to be registered
3546 *
3547 * This function is only called in the driver's load path. Registering the NAPI
3548 * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
3549 * reset/rebuild, etc.)
3550 */
3551static void ice_napi_add(struct ice_vsi *vsi)
3552{
3553 int v_idx;
3554
3555 if (!vsi->netdev)
3556 return;
3557
Amritha Nambiar91fdbce2023-12-01 15:28:40 -08003558 ice_for_each_q_vector(vsi, v_idx) {
Anirudh Venkataramanandf0f8472018-09-19 17:23:09 -07003559 netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
Jakub Kicinskib48b89f2022-09-27 06:27:53 -07003560 ice_napi_poll);
Amritha Nambiar080b0c82024-02-13 11:48:50 -08003561 __ice_q_vector_set_napi_queues(vsi->q_vectors[v_idx], false);
Amritha Nambiar91fdbce2023-12-01 15:28:40 -08003562 }
Anirudh Venkataramanandf0f8472018-09-19 17:23:09 -07003563}
3564
3565/**
Tony Nguyen462acf6a2019-09-09 06:47:46 -07003566 * ice_set_ops - set netdev and ethtools ops for the given netdev
Lorenzo Bianconib6a41032023-02-14 15:39:27 +01003567 * @vsi: the VSI associated with the new netdev
Anirudh Venkataramanan3a858ba2018-03-20 07:58:11 -07003568 */
Lorenzo Bianconib6a41032023-02-14 15:39:27 +01003569static void ice_set_ops(struct ice_vsi *vsi)
Anirudh Venkataramanan3a858ba2018-03-20 07:58:11 -07003570{
Lorenzo Bianconib6a41032023-02-14 15:39:27 +01003571 struct net_device *netdev = vsi->netdev;
Tony Nguyen462acf6a2019-09-09 06:47:46 -07003572 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3573
3574 if (ice_is_safe_mode(pf)) {
3575 netdev->netdev_ops = &ice_netdev_safe_mode_ops;
3576 ice_set_ethtool_safe_mode_ops(netdev);
3577 return;
3578 }
3579
3580 netdev->netdev_ops = &ice_netdev_ops;
Jakub Kicinskib20e6c12020-09-25 17:56:46 -07003581 netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
Larysa Zaremba9031d5f2023-12-05 22:08:34 +01003582 netdev->xdp_metadata_ops = &ice_xdp_md_ops;
Tony Nguyen462acf6a2019-09-09 06:47:46 -07003583 ice_set_ethtool_ops(netdev);
Lorenzo Bianconib6a41032023-02-14 15:39:27 +01003584
3585 if (vsi->type != ICE_VSI_PF)
3586 return;
3587
3588 netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
3589 NETDEV_XDP_ACT_XSK_ZEROCOPY |
3590 NETDEV_XDP_ACT_RX_SG;
Maciej Fijalkowskieeb2b532023-07-19 15:24:12 +02003591 netdev->xdp_zc_max_segs = ICE_MAX_BUF_TXD;
Tony Nguyen462acf6a2019-09-09 06:47:46 -07003592}
3593
3594/**
3595 * ice_set_netdev_features - set features for the given netdev
3596 * @netdev: netdev instance
3597 */
3598static void ice_set_netdev_features(struct net_device *netdev)
3599{
3600 struct ice_pf *pf = ice_netdev_to_pf(netdev);
Brett Creeley1babaf72021-12-02 08:38:50 -08003601 bool is_dvm_ena = ice_is_dvm_ena(&pf->hw);
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003602 netdev_features_t csumo_features;
3603 netdev_features_t vlano_features;
3604 netdev_features_t dflt_features;
3605 netdev_features_t tso_features;
Anirudh Venkataramanan3a858ba2018-03-20 07:58:11 -07003606
Tony Nguyen462acf6a2019-09-09 06:47:46 -07003607 if (ice_is_safe_mode(pf)) {
3608 /* safe mode */
3609 netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
3610 netdev->hw_features = netdev->features;
3611 return;
3612 }
Anirudh Venkataramanan3a858ba2018-03-20 07:58:11 -07003613
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003614 dflt_features = NETIF_F_SG |
3615 NETIF_F_HIGHDMA |
Henry Tieman148beb62020-05-11 18:01:40 -07003616 NETIF_F_NTUPLE |
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003617 NETIF_F_RXHASH;
3618
3619 csumo_features = NETIF_F_RXCSUM |
3620 NETIF_F_IP_CSUM |
Anirudh Venkataramanancf909e12018-12-19 10:03:32 -08003621 NETIF_F_SCTP_CRC |
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003622 NETIF_F_IPV6_CSUM;
3623
3624 vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
3625 NETIF_F_HW_VLAN_CTAG_TX |
3626 NETIF_F_HW_VLAN_CTAG_RX;
3627
Brett Creeley1babaf72021-12-02 08:38:50 -08003628 /* Enable CTAG/STAG filtering by default in Double VLAN Mode (DVM) */
3629 if (is_dvm_ena)
3630 vlano_features |= NETIF_F_HW_VLAN_STAG_FILTER;
3631
Tony Nguyena4e82a82020-05-06 09:32:30 -07003632 tso_features = NETIF_F_TSO |
3633 NETIF_F_TSO_ECN |
3634 NETIF_F_TSO6 |
3635 NETIF_F_GSO_GRE |
3636 NETIF_F_GSO_UDP_TUNNEL |
3637 NETIF_F_GSO_GRE_CSUM |
3638 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3639 NETIF_F_GSO_PARTIAL |
3640 NETIF_F_GSO_IPXIP4 |
3641 NETIF_F_GSO_IPXIP6 |
Brett Creeleya54e3b82019-12-12 03:12:53 -08003642 NETIF_F_GSO_UDP_L4;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003643
Tony Nguyena4e82a82020-05-06 09:32:30 -07003644 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3645 NETIF_F_GSO_GRE_CSUM;
Anirudh Venkataramanan3a858ba2018-03-20 07:58:11 -07003646 /* set features that user can change */
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003647 netdev->hw_features = dflt_features | csumo_features |
3648 vlano_features | tso_features;
Anirudh Venkataramanan3a858ba2018-03-20 07:58:11 -07003649
Tony Nguyena4e82a82020-05-06 09:32:30 -07003650 /* add support for HW_CSUM on packets with MPLS header */
Joe Damato69e66c02022-03-17 21:12:12 -07003651 netdev->mpls_features = NETIF_F_HW_CSUM |
3652 NETIF_F_TSO |
3653 NETIF_F_TSO6;
Tony Nguyena4e82a82020-05-06 09:32:30 -07003654
Anirudh Venkataramanan3a858ba2018-03-20 07:58:11 -07003655 /* enable features */
3656 netdev->features |= netdev->hw_features;
Kiran Patil0d08a442021-08-06 10:49:05 +02003657
3658 netdev->hw_features |= NETIF_F_HW_TC;
Maciej Fijalkowski44ece4e2022-07-07 12:16:51 +02003659 netdev->hw_features |= NETIF_F_LOOPBACK;
Kiran Patil0d08a442021-08-06 10:49:05 +02003660
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003661 /* encap and VLAN devices inherit default, csumo and tso features */
3662 netdev->hw_enc_features |= dflt_features | csumo_features |
3663 tso_features;
3664 netdev->vlan_features |= dflt_features | csumo_features |
3665 tso_features;
Brett Creeley1babaf72021-12-02 08:38:50 -08003666
3667 /* advertise support but don't enable by default since only one type of
3668 * VLAN offload can be enabled at a time (i.e. CTAG or STAG). When one
3669 * type turns on the other has to be turned off. This is enforced by the
3670 * ice_fix_features() ndo callback.
3671 */
3672 if (is_dvm_ena)
3673 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX |
3674 NETIF_F_HW_VLAN_STAG_TX;
Jesse Brandeburgdddd4062022-07-27 09:24:05 +02003675
3676 /* Leave CRC / FCS stripping enabled by default, but allow the value to
3677 * be changed at runtime
3678 */
3679 netdev->hw_features |= NETIF_F_RXFCS;
Pawel Chmielewskifce92db2023-02-07 17:23:03 +01003680
3681 netif_set_tso_max_size(netdev, ICE_MAX_TSO_SIZE);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07003682}
3683
3684/**
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003685 * ice_fill_rss_lut - Fill the RSS lookup table with default values
3686 * @lut: Lookup table
3687 * @rss_table_size: Lookup table size
3688 * @rss_size: Range of queue number for hashing
3689 */
3690void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3691{
3692 u16 i;
3693
3694 for (i = 0; i < rss_table_size; i++)
3695 lut[i] = i % rss_size;
3696}
3697
3698/**
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07003699 * ice_pf_vsi_setup - Set up a PF VSI
3700 * @pf: board private structure
3701 * @pi: pointer to the port_info instance
3702 *
Anirudh Venkataramanan0e674ae2019-04-16 10:30:43 -07003703 * Returns pointer to the successfully allocated VSI software struct
3704 * on success, otherwise returns NULL on failure.
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07003705 */
3706static struct ice_vsi *
3707ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3708{
Jacob Keller5e509ab2023-01-18 17:16:43 -08003709 struct ice_vsi_cfg_params params = {};
3710
3711 params.type = ICE_VSI_PF;
Mateusz Polchlopekdeea4272024-04-19 05:11:04 -04003712 params.port_info = pi;
Jacob Keller5e509ab2023-01-18 17:16:43 -08003713 params.flags = ICE_VSI_FLAG_INIT;
3714
3715 return ice_vsi_setup(pf, &params);
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07003716}
3717
Kiran Patilfbc7b272021-10-15 16:35:16 -07003718static struct ice_vsi *
3719ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3720 struct ice_channel *ch)
3721{
Jacob Keller5e509ab2023-01-18 17:16:43 -08003722 struct ice_vsi_cfg_params params = {};
3723
3724 params.type = ICE_VSI_CHNL;
Mateusz Polchlopekdeea4272024-04-19 05:11:04 -04003725 params.port_info = pi;
Jacob Keller5e509ab2023-01-18 17:16:43 -08003726 params.ch = ch;
3727 params.flags = ICE_VSI_FLAG_INIT;
3728
3729 return ice_vsi_setup(pf, &params);
Kiran Patilfbc7b272021-10-15 16:35:16 -07003730}
3731
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07003732/**
Henry Tieman148beb62020-05-11 18:01:40 -07003733 * ice_ctrl_vsi_setup - Set up a control VSI
3734 * @pf: board private structure
3735 * @pi: pointer to the port_info instance
3736 *
3737 * Returns pointer to the successfully allocated VSI software struct
3738 * on success, otherwise returns NULL on failure.
3739 */
3740static struct ice_vsi *
3741ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3742{
Jacob Keller5e509ab2023-01-18 17:16:43 -08003743 struct ice_vsi_cfg_params params = {};
3744
3745 params.type = ICE_VSI_CTRL;
Mateusz Polchlopekdeea4272024-04-19 05:11:04 -04003746 params.port_info = pi;
Jacob Keller5e509ab2023-01-18 17:16:43 -08003747 params.flags = ICE_VSI_FLAG_INIT;
3748
3749 return ice_vsi_setup(pf, &params);
Henry Tieman148beb62020-05-11 18:01:40 -07003750}
3751
3752/**
Anirudh Venkataramanan0e674ae2019-04-16 10:30:43 -07003753 * ice_lb_vsi_setup - Set up a loopback VSI
3754 * @pf: board private structure
3755 * @pi: pointer to the port_info instance
3756 *
3757 * Returns pointer to the successfully allocated VSI software struct
3758 * on success, otherwise returns NULL on failure.
3759 */
3760struct ice_vsi *
3761ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3762{
Jacob Keller5e509ab2023-01-18 17:16:43 -08003763 struct ice_vsi_cfg_params params = {};
3764
3765 params.type = ICE_VSI_LB;
Mateusz Polchlopekdeea4272024-04-19 05:11:04 -04003766 params.port_info = pi;
Jacob Keller5e509ab2023-01-18 17:16:43 -08003767 params.flags = ICE_VSI_FLAG_INIT;
3768
3769 return ice_vsi_setup(pf, &params);
Anirudh Venkataramanan0e674ae2019-04-16 10:30:43 -07003770}
3771
3772/**
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -08003773 * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003774 * @netdev: network interface to be adjusted
Brett Creeley2bfefa22021-12-02 08:38:44 -08003775 * @proto: VLAN TPID
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -08003776 * @vid: VLAN ID to be added
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003777 *
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -08003778 * net_device_ops implementation for adding VLAN IDs
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003779 */
Bruce Allanc8b7abd2019-02-26 16:35:11 -08003780static int
Brett Creeley2bfefa22021-12-02 08:38:44 -08003781ice_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003782{
3783 struct ice_netdev_priv *np = netdev_priv(netdev);
Brett Creeleyc31af682021-12-02 08:38:46 -08003784 struct ice_vsi_vlan_ops *vlan_ops;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003785 struct ice_vsi *vsi = np->vsi;
Brett Creeleyfb05ba12021-12-02 08:38:42 -08003786 struct ice_vlan vlan;
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -08003787 int ret;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003788
Brett Creeley42f3efe2020-01-22 07:21:24 -08003789 /* VLAN 0 is added by default during load/reset */
3790 if (!vid)
3791 return 0;
3792
Ivan Vecera1273f892022-03-31 09:20:08 -07003793 while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3794 usleep_range(1000, 2000);
3795
3796 /* Add multicast promisc rule for the VLAN ID to be added if
3797 * all-multicast is currently enabled.
3798 */
3799 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3800 ret = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3801 ICE_MCAST_VLAN_PROMISC_BITS,
3802 vid);
3803 if (ret)
3804 goto finish;
3805 }
3806
Brett Creeleyc31af682021-12-02 08:38:46 -08003807 vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
Brett Creeley4f74dcc2018-08-09 06:29:56 -07003808
Brett Creeley42f3efe2020-01-22 07:21:24 -08003809 /* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3810 * packets aren't pruned by the device's internal switch on Rx
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003811 */
Brett Creeley2bfefa22021-12-02 08:38:44 -08003812 vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
Brett Creeleyc31af682021-12-02 08:38:46 -08003813 ret = vlan_ops->add_vlan(vsi, &vlan);
Ivan Vecera1273f892022-03-31 09:20:08 -07003814 if (ret)
3815 goto finish;
3816
3817 /* If all-multicast is currently enabled and this VLAN ID is only one
3818 * besides VLAN-0 we have to update look-up type of multicast promisc
3819 * rule for VLAN-0 from ICE_SW_LKUP_PROMISC to ICE_SW_LKUP_PROMISC_VLAN.
3820 */
3821 if ((vsi->current_netdev_flags & IFF_ALLMULTI) &&
3822 ice_vsi_num_non_zero_vlans(vsi) == 1) {
3823 ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3824 ICE_MCAST_PROMISC_BITS, 0);
3825 ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3826 ICE_MCAST_VLAN_PROMISC_BITS, 0);
3827 }
3828
3829finish:
3830 clear_bit(ICE_CFG_BUSY, vsi->state);
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -08003831
3832 return ret;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003833}
3834
3835/**
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -08003836 * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003837 * @netdev: network interface to be adjusted
Brett Creeley2bfefa22021-12-02 08:38:44 -08003838 * @proto: VLAN TPID
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -08003839 * @vid: VLAN ID to be removed
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003840 *
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -08003841 * net_device_ops implementation for removing VLAN IDs
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003842 */
Bruce Allanc8b7abd2019-02-26 16:35:11 -08003843static int
Brett Creeley2bfefa22021-12-02 08:38:44 -08003844ice_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003845{
3846 struct ice_netdev_priv *np = netdev_priv(netdev);
Brett Creeleyc31af682021-12-02 08:38:46 -08003847 struct ice_vsi_vlan_ops *vlan_ops;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003848 struct ice_vsi *vsi = np->vsi;
Brett Creeleyfb05ba12021-12-02 08:38:42 -08003849 struct ice_vlan vlan;
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -08003850 int ret;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003851
Brett Creeley42f3efe2020-01-22 07:21:24 -08003852 /* don't allow removal of VLAN 0 */
3853 if (!vid)
3854 return 0;
3855
Ivan Vecera1273f892022-03-31 09:20:08 -07003856 while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3857 usleep_range(1000, 2000);
3858
Grzegorz Siwikabddafd2022-08-12 15:25:49 +02003859 ret = ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3860 ICE_MCAST_VLAN_PROMISC_BITS, vid);
3861 if (ret) {
3862 netdev_err(netdev, "Error clearing multicast promiscuous mode on VSI %i\n",
3863 vsi->vsi_num);
3864 vsi->current_netdev_flags |= IFF_ALLMULTI;
3865 }
3866
Brett Creeleyc31af682021-12-02 08:38:46 -08003867 vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3868
Brett Creeleybc42afa2021-12-02 08:38:41 -08003869 /* Make sure VLAN delete is successful before updating VLAN
Brett Creeley4f74dcc2018-08-09 06:29:56 -07003870 * information
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003871 */
Brett Creeley2bfefa22021-12-02 08:38:44 -08003872 vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
Brett Creeleyc31af682021-12-02 08:38:46 -08003873 ret = vlan_ops->del_vlan(vsi, &vlan);
Akeem G Abodunrin5eda8af2019-02-26 16:35:14 -08003874 if (ret)
Ivan Vecera1273f892022-03-31 09:20:08 -07003875 goto finish;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003876
Ivan Vecera1273f892022-03-31 09:20:08 -07003877 /* Remove multicast promisc rule for the removed VLAN ID if
3878 * all-multicast is enabled.
3879 */
3880 if (vsi->current_netdev_flags & IFF_ALLMULTI)
3881 ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3882 ICE_MCAST_VLAN_PROMISC_BITS, vid);
3883
3884 if (!ice_vsi_has_non_zero_vlans(vsi)) {
3885 /* Update look-up type of multicast promisc rule for VLAN 0
3886 * from ICE_SW_LKUP_PROMISC_VLAN to ICE_SW_LKUP_PROMISC when
3887 * all-multicast is enabled and VLAN 0 is the only VLAN rule.
3888 */
3889 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3890 ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3891 ICE_MCAST_VLAN_PROMISC_BITS,
3892 0);
3893 ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3894 ICE_MCAST_PROMISC_BITS, 0);
3895 }
3896 }
3897
3898finish:
3899 clear_bit(ICE_CFG_BUSY, vsi->state);
3900
3901 return ret;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003902}
3903
3904/**
Michal Swiatkowski195bb482021-10-12 11:31:03 -07003905 * ice_rep_indr_tc_block_unbind
3906 * @cb_priv: indirection block private data
3907 */
3908static void ice_rep_indr_tc_block_unbind(void *cb_priv)
3909{
3910 struct ice_indr_block_priv *indr_priv = cb_priv;
3911
3912 list_del(&indr_priv->list);
3913 kfree(indr_priv);
3914}
3915
3916/**
3917 * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
3918 * @vsi: VSI struct which has the netdev
3919 */
3920static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
3921{
3922 struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
3923
3924 flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
3925 ice_rep_indr_tc_block_unbind);
3926}
3927
3928/**
Michal Swiatkowski195bb482021-10-12 11:31:03 -07003929 * ice_tc_indir_block_register - Register TC indirect block notifications
3930 * @vsi: VSI struct which has the netdev
3931 *
3932 * Returns 0 on success, negative value on failure
3933 */
3934static int ice_tc_indir_block_register(struct ice_vsi *vsi)
3935{
3936 struct ice_netdev_priv *np;
3937
3938 if (!vsi || !vsi->netdev)
3939 return -EINVAL;
3940
3941 np = netdev_priv(vsi->netdev);
3942
3943 INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
3944 return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
3945}
3946
3947/**
Anirudh Venkataramanan8c243702019-09-03 01:31:06 -07003948 * ice_get_avail_q_count - Get count of queues in use
3949 * @pf_qmap: bitmap to get queue use count from
3950 * @lock: pointer to a mutex that protects access to pf_qmap
3951 * @size: size of the bitmap
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003952 */
Anirudh Venkataramanan8c243702019-09-03 01:31:06 -07003953static u16
3954ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003955{
Karol Kolacinski88865fc2020-05-07 17:41:05 -07003956 unsigned long bit;
3957 u16 count = 0;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003958
Anirudh Venkataramanan8c243702019-09-03 01:31:06 -07003959 mutex_lock(lock);
3960 for_each_clear_bit(bit, pf_qmap, size)
3961 count++;
3962 mutex_unlock(lock);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003963
Anirudh Venkataramanan8c243702019-09-03 01:31:06 -07003964 return count;
3965}
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07003966
Anirudh Venkataramanan8c243702019-09-03 01:31:06 -07003967/**
3968 * ice_get_avail_txq_count - Get count of Tx queues in use
3969 * @pf: pointer to an ice_pf instance
3970 */
3971u16 ice_get_avail_txq_count(struct ice_pf *pf)
3972{
3973 return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3974 pf->max_pf_txqs);
3975}
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003976
Anirudh Venkataramanan8c243702019-09-03 01:31:06 -07003977/**
3978 * ice_get_avail_rxq_count - Get count of Rx queues in use
3979 * @pf: pointer to an ice_pf instance
3980 */
3981u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3982{
3983 return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3984 pf->max_pf_rxqs);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003985}
3986
3987/**
3988 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3989 * @pf: board private structure to initialize
3990 */
3991static void ice_deinit_pf(struct ice_pf *pf)
3992{
Akeem G Abodunrin8d81fa52018-08-09 06:29:57 -07003993 ice_service_task_stop(pf);
Dave Ertmanbb52f422023-06-20 15:18:46 -07003994 mutex_destroy(&pf->lag_mutex);
Ivan Vecera486b9ee2022-04-23 12:20:21 +02003995 mutex_destroy(&pf->adev_mutex);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003996 mutex_destroy(&pf->sw_mutex);
Dave Ertmanb94b0132019-11-06 02:05:29 -08003997 mutex_destroy(&pf->tc_mutex);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07003998 mutex_destroy(&pf->avail_q_mutex);
Jacob Keller3d5985a2022-02-16 13:37:38 -08003999 mutex_destroy(&pf->vfs.table_lock);
Anirudh Venkataramanan78b57132019-08-02 01:25:21 -07004000
4001 if (pf->avail_txqs) {
4002 bitmap_free(pf->avail_txqs);
4003 pf->avail_txqs = NULL;
4004 }
4005
4006 if (pf->avail_rxqs) {
4007 bitmap_free(pf->avail_rxqs);
4008 pf->avail_rxqs = NULL;
4009 }
Jacob Keller06c16d82021-06-09 09:39:50 -07004010
4011 if (pf->ptp.clock)
4012 ptp_clock_unregister(pf->ptp.clock);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07004013}
4014
4015/**
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004016 * ice_set_pf_caps - set PFs capability flags
4017 * @pf: pointer to the PF instance
4018 */
4019static void ice_set_pf_caps(struct ice_pf *pf)
4020{
4021 struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
4022
Dave Ertmand25a0fc2021-05-20 09:37:49 -05004023 clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
Dave Ertman88f62ae2022-02-11 10:26:03 -08004024 if (func_caps->common_cap.rdma)
Dave Ertmand25a0fc2021-05-20 09:37:49 -05004025 set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004026 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4027 if (func_caps->common_cap.dcb)
4028 set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004029 clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
4030 if (func_caps->common_cap.sr_iov_1_1) {
4031 set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
Jacob Keller000773c2022-02-16 13:37:36 -08004032 pf->vfs.num_supported = min_t(int, func_caps->num_allocd_vfs,
Jacob Kellerdc367962022-02-22 16:26:53 -08004033 ICE_MAX_SRIOV_VFS);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004034 }
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004035 clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
4036 if (func_caps->common_cap.rss_table_size)
4037 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
4038
Henry Tieman148beb62020-05-11 18:01:40 -07004039 clear_bit(ICE_FLAG_FD_ENA, pf->flags);
4040 if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
4041 u16 unused;
4042
4043 /* ctrl_vsi_idx will be set to a valid value when flow director
4044 * is setup by ice_init_fdir
4045 */
4046 pf->ctrl_vsi_idx = ICE_NO_VSI;
4047 set_bit(ICE_FLAG_FD_ENA, pf->flags);
4048 /* force guaranteed filter pool for PF */
4049 ice_alloc_fd_guar_item(&pf->hw, &unused,
4050 func_caps->fd_fltr_guar);
4051 /* force shared filter pool for PF */
4052 ice_alloc_fd_shrd_item(&pf->hw, &unused,
4053 func_caps->fd_fltr_best_effort);
4054 }
4055
Jacob Keller06c16d82021-06-09 09:39:50 -07004056 clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
Paul Greenwaltba1124f2023-10-25 14:41:52 -07004057 if (func_caps->common_cap.ieee_1588 &&
4058 !(pf->hw.mac_type == ICE_MAC_E830))
Jacob Keller06c16d82021-06-09 09:39:50 -07004059 set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
4060
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004061 pf->max_pf_txqs = func_caps->common_cap.num_txq;
4062 pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
4063}
4064
4065/**
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07004066 * ice_init_pf - Initialize general software structures (struct ice_pf)
4067 * @pf: board private structure to initialize
4068 */
Anirudh Venkataramanan78b57132019-08-02 01:25:21 -07004069static int ice_init_pf(struct ice_pf *pf)
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07004070{
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004071 ice_set_pf_caps(pf);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07004072
4073 mutex_init(&pf->sw_mutex);
Dave Ertmanb94b0132019-11-06 02:05:29 -08004074 mutex_init(&pf->tc_mutex);
Ivan Vecera486b9ee2022-04-23 12:20:21 +02004075 mutex_init(&pf->adev_mutex);
Dave Ertmanbb52f422023-06-20 15:18:46 -07004076 mutex_init(&pf->lag_mutex);
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07004077
Jacob Kellerd69ea412020-07-23 17:22:03 -07004078 INIT_HLIST_HEAD(&pf->aq_wait_list);
4079 spin_lock_init(&pf->aq_wait_lock);
4080 init_waitqueue_head(&pf->aq_wait_queue);
4081
Jacob Keller1c080522021-05-06 08:39:59 -07004082 init_waitqueue_head(&pf->reset_wait_queue);
4083
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07004084 /* setup service timer and periodic service task */
4085 timer_setup(&pf->serv_tmr, ice_service_timer, 0);
4086 pf->serv_tmr_period = HZ;
4087 INIT_WORK(&pf->serv_task, ice_service_task);
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08004088 clear_bit(ICE_SERVICE_SCHED, pf->state);
Anirudh Venkataramanan78b57132019-08-02 01:25:21 -07004089
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004090 mutex_init(&pf->avail_q_mutex);
Anirudh Venkataramanan78b57132019-08-02 01:25:21 -07004091 pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
4092 if (!pf->avail_txqs)
4093 return -ENOMEM;
4094
4095 pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
4096 if (!pf->avail_rxqs) {
Michal Swiatkowski59ac3252022-08-17 10:53:20 +02004097 bitmap_free(pf->avail_txqs);
Anirudh Venkataramanan78b57132019-08-02 01:25:21 -07004098 pf->avail_txqs = NULL;
4099 return -ENOMEM;
4100 }
4101
Jacob Keller3d5985a2022-02-16 13:37:38 -08004102 mutex_init(&pf->vfs.table_lock);
4103 hash_init(pf->vfs.table);
Jacob Kellerdde7db62023-02-22 09:09:13 -08004104 ice_mbx_init_snapshot(&pf->hw);
Jacob Keller3d5985a2022-02-16 13:37:38 -08004105
Anirudh Venkataramanan78b57132019-08-02 01:25:21 -07004106 return 0;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07004107}
4108
4109/**
Anirudh Venkataramanan31765512021-02-26 13:19:30 -08004110 * ice_is_wol_supported - check if WoL is supported
4111 * @hw: pointer to hardware info
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07004112 *
4113 * Check if WoL is supported based on the HW configuration.
4114 * Returns true if NVM supports and enables WoL for this port, false otherwise
4115 */
Anirudh Venkataramanan31765512021-02-26 13:19:30 -08004116bool ice_is_wol_supported(struct ice_hw *hw)
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07004117{
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07004118 u16 wol_ctrl;
4119
4120 /* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
4121 * word) indicates WoL is not supported on the corresponding PF ID.
4122 */
4123 if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
4124 return false;
4125
Anirudh Venkataramanan31765512021-02-26 13:19:30 -08004126 return !(BIT(hw->port_info->lport) & wol_ctrl);
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07004127}
4128
4129/**
Henry Tieman87324e72019-11-08 06:23:29 -08004130 * ice_vsi_recfg_qs - Change the number of queues on a VSI
4131 * @vsi: VSI being changed
4132 * @new_rx: new number of Rx queues
4133 * @new_tx: new number of Tx queues
Dave Ertmana6a09742023-01-24 09:19:43 -08004134 * @locked: is adev device_lock held
Henry Tieman87324e72019-11-08 06:23:29 -08004135 *
4136 * Only change the number of queues if new_tx, or new_rx is non-0.
4137 *
4138 * Returns 0 on success.
4139 */
Dave Ertmana6a09742023-01-24 09:19:43 -08004140int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx, bool locked)
Henry Tieman87324e72019-11-08 06:23:29 -08004141{
4142 struct ice_pf *pf = vsi->back;
Jan Sokolowskif4b91c12024-06-21 10:54:19 -07004143 int i, err = 0, timeout = 50;
Henry Tieman87324e72019-11-08 06:23:29 -08004144
4145 if (!new_rx && !new_tx)
4146 return -EINVAL;
4147
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08004148 while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
Henry Tieman87324e72019-11-08 06:23:29 -08004149 timeout--;
4150 if (!timeout)
4151 return -EBUSY;
4152 usleep_range(1000, 2000);
4153 }
4154
4155 if (new_tx)
Karol Kolacinski88865fc2020-05-07 17:41:05 -07004156 vsi->req_txq = (u16)new_tx;
Henry Tieman87324e72019-11-08 06:23:29 -08004157 if (new_rx)
Karol Kolacinski88865fc2020-05-07 17:41:05 -07004158 vsi->req_rxq = (u16)new_rx;
Henry Tieman87324e72019-11-08 06:23:29 -08004159
4160 /* set for the next time the netdev is started */
4161 if (!netif_running(vsi->netdev)) {
Eric Joynerd47bf9a2024-06-17 14:46:25 +02004162 err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4163 if (err)
4164 goto rebuild_err;
Henry Tieman87324e72019-11-08 06:23:29 -08004165 dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
4166 goto done;
4167 }
4168
4169 ice_vsi_close(vsi);
Eric Joynerd47bf9a2024-06-17 14:46:25 +02004170 err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4171 if (err)
4172 goto rebuild_err;
Jan Sokolowskif4b91c12024-06-21 10:54:19 -07004173
4174 ice_for_each_traffic_class(i) {
4175 if (vsi->tc_cfg.ena_tc & BIT(i))
4176 netdev_set_tc_queue(vsi->netdev,
4177 vsi->tc_cfg.tc_info[i].netdev_tc,
4178 vsi->tc_cfg.tc_info[i].qcount_tx,
4179 vsi->tc_cfg.tc_info[i].qoffset);
4180 }
Dave Ertmana6a09742023-01-24 09:19:43 -08004181 ice_pf_dcb_recfg(pf, locked);
Henry Tieman87324e72019-11-08 06:23:29 -08004182 ice_vsi_open(vsi);
Eric Joynerd47bf9a2024-06-17 14:46:25 +02004183 goto done;
4184
4185rebuild_err:
4186 dev_err(ice_pf_to_dev(pf), "Error during VSI rebuild: %d. Unload and reload the driver.\n",
4187 err);
Henry Tieman87324e72019-11-08 06:23:29 -08004188done:
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08004189 clear_bit(ICE_CFG_BUSY, pf->state);
Henry Tieman87324e72019-11-08 06:23:29 -08004190 return err;
4191}
4192
4193/**
Brett Creeleycd1f56f2020-07-13 13:53:14 -07004194 * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
4195 * @pf: PF to configure
4196 *
4197 * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
4198 * VSI can still Tx/Rx VLAN tagged packets.
4199 */
4200static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
4201{
4202 struct ice_vsi *vsi = ice_get_main_vsi(pf);
4203 struct ice_vsi_ctx *ctxt;
Brett Creeleycd1f56f2020-07-13 13:53:14 -07004204 struct ice_hw *hw;
Tony Nguyen5518ac22021-10-07 15:59:03 -07004205 int status;
Brett Creeleycd1f56f2020-07-13 13:53:14 -07004206
4207 if (!vsi)
4208 return;
4209
4210 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
4211 if (!ctxt)
4212 return;
4213
4214 hw = &pf->hw;
4215 ctxt->info = vsi->info;
4216
4217 ctxt->info.valid_sections =
4218 cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
4219 ICE_AQ_VSI_PROP_SECURITY_VALID |
4220 ICE_AQ_VSI_PROP_SW_VALID);
4221
4222 /* disable VLAN anti-spoof */
4223 ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4224 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4225
4226 /* disable VLAN pruning and keep all other settings */
4227 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
4228
4229 /* allow all VLANs on Tx and don't strip on Rx */
Brett Creeley7bd527a2021-12-02 08:38:45 -08004230 ctxt->info.inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL |
4231 ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
Brett Creeleycd1f56f2020-07-13 13:53:14 -07004232
4233 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4234 if (status) {
Tony Nguyen5f87ec42021-10-07 15:56:02 -07004235 dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %d aq_err %s\n",
Tony Nguyen5518ac22021-10-07 15:59:03 -07004236 status, ice_aq_str(hw->adminq.sq_last_status));
Brett Creeleycd1f56f2020-07-13 13:53:14 -07004237 } else {
4238 vsi->info.sec_flags = ctxt->info.sec_flags;
4239 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
Brett Creeley7bd527a2021-12-02 08:38:45 -08004240 vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
Brett Creeleycd1f56f2020-07-13 13:53:14 -07004241 }
4242
4243 kfree(ctxt);
4244}
4245
4246/**
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004247 * ice_log_pkg_init - log result of DDP package load
4248 * @hw: pointer to hardware info
Wojciech Drewek247dd972021-10-07 15:54:37 -07004249 * @state: state of package load
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004250 */
Wojciech Drewek247dd972021-10-07 15:54:37 -07004251static void ice_log_pkg_init(struct ice_hw *hw, enum ice_ddp_state state)
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004252{
Wojciech Drewek247dd972021-10-07 15:54:37 -07004253 struct ice_pf *pf = hw->back;
4254 struct device *dev;
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004255
Wojciech Drewek247dd972021-10-07 15:54:37 -07004256 dev = ice_pf_to_dev(pf);
4257
4258 switch (state) {
4259 case ICE_DDP_PKG_SUCCESS:
4260 dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
4261 hw->active_pkg_name,
4262 hw->active_pkg_ver.major,
4263 hw->active_pkg_ver.minor,
4264 hw->active_pkg_ver.update,
4265 hw->active_pkg_ver.draft);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004266 break;
Wojciech Drewek247dd972021-10-07 15:54:37 -07004267 case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
4268 dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
4269 hw->active_pkg_name,
4270 hw->active_pkg_ver.major,
4271 hw->active_pkg_ver.minor,
4272 hw->active_pkg_ver.update,
4273 hw->active_pkg_ver.draft);
4274 break;
4275 case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
4276 dev_err(dev, "The device has a DDP package that is not supported by the driver. The device has package '%s' version %d.%d.x.x. The driver requires version %d.%d.x.x. Entering Safe Mode.\n",
4277 hw->active_pkg_name,
4278 hw->active_pkg_ver.major,
4279 hw->active_pkg_ver.minor,
4280 ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4281 break;
4282 case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
4283 dev_info(dev, "The driver could not load the DDP package file because a compatible DDP package is already present on the device. The device has package '%s' version %d.%d.%d.%d. The package file found by the driver: '%s' version %d.%d.%d.%d.\n",
4284 hw->active_pkg_name,
4285 hw->active_pkg_ver.major,
4286 hw->active_pkg_ver.minor,
4287 hw->active_pkg_ver.update,
4288 hw->active_pkg_ver.draft,
4289 hw->pkg_name,
4290 hw->pkg_ver.major,
4291 hw->pkg_ver.minor,
4292 hw->pkg_ver.update,
4293 hw->pkg_ver.draft);
4294 break;
4295 case ICE_DDP_PKG_FW_MISMATCH:
Victor Rajb8272912020-05-15 17:36:34 -07004296 dev_err(dev, "The firmware loaded on the device is not compatible with the DDP package. Please update the device's NVM. Entering safe mode.\n");
4297 break;
Wojciech Drewek247dd972021-10-07 15:54:37 -07004298 case ICE_DDP_PKG_INVALID_FILE:
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08004299 dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004300 break;
Wojciech Drewek247dd972021-10-07 15:54:37 -07004301 case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
4302 dev_err(dev, "The DDP package file version is higher than the driver supports. Please use an updated driver. Entering Safe Mode.\n");
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004303 break;
Wojciech Drewek247dd972021-10-07 15:54:37 -07004304 case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
4305 dev_err(dev, "The DDP package file version is lower than the driver supports. The driver requires version %d.%d.x.x. Please use an updated DDP Package file. Entering Safe Mode.\n",
4306 ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4307 break;
4308 case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
4309 dev_err(dev, "The DDP package could not be loaded because its signature is not valid. Please use a valid DDP Package. Entering Safe Mode.\n");
4310 break;
4311 case ICE_DDP_PKG_FILE_REVISION_TOO_LOW:
4312 dev_err(dev, "The DDP Package could not be loaded because its security revision is too low. Please use an updated DDP Package. Entering Safe Mode.\n");
4313 break;
4314 case ICE_DDP_PKG_LOAD_ERROR:
4315 dev_err(dev, "An error occurred on the device while loading the DDP package. The device will be reset.\n");
Jesse Brandeburg0092db52021-12-21 15:05:38 -08004316 /* poll for reset to complete */
4317 if (ice_check_reset(hw))
4318 dev_err(dev, "Error resetting device. Please reload the driver\n");
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004319 break;
Wojciech Drewek247dd972021-10-07 15:54:37 -07004320 case ICE_DDP_PKG_ERR:
4321 default:
4322 dev_err(dev, "An unknown error occurred when loading the DDP package. Entering Safe Mode.\n");
Jesse Brandeburg0092db52021-12-21 15:05:38 -08004323 break;
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004324 }
4325}
4326
4327/**
4328 * ice_load_pkg - load/reload the DDP Package file
4329 * @firmware: firmware structure when firmware requested or NULL for reload
4330 * @pf: pointer to the PF instance
4331 *
4332 * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
4333 * initialize HW tables.
4334 */
4335static void
4336ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
4337{
Wojciech Drewek247dd972021-10-07 15:54:37 -07004338 enum ice_ddp_state state = ICE_DDP_PKG_ERR;
Brett Creeley4015d112019-11-08 06:23:26 -08004339 struct device *dev = ice_pf_to_dev(pf);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004340 struct ice_hw *hw = &pf->hw;
4341
4342 /* Load DDP Package */
4343 if (firmware && !hw->pkg_copy) {
Wojciech Drewek247dd972021-10-07 15:54:37 -07004344 state = ice_copy_and_init_pkg(hw, firmware->data,
4345 firmware->size);
4346 ice_log_pkg_init(hw, state);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004347 } else if (!firmware && hw->pkg_copy) {
4348 /* Reload package during rebuild after CORER/GLOBR reset */
Wojciech Drewek247dd972021-10-07 15:54:37 -07004349 state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
4350 ice_log_pkg_init(hw, state);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004351 } else {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08004352 dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004353 }
4354
Wojciech Drewek247dd972021-10-07 15:54:37 -07004355 if (!ice_is_init_pkg_successful(state)) {
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004356 /* Safe Mode */
4357 clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4358 return;
4359 }
4360
4361 /* Successful download package is the precondition for advanced
4362 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
4363 */
4364 set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4365}
4366
4367/**
Brett Creeleyc585ea42018-10-26 10:40:58 -07004368 * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
4369 * @pf: pointer to the PF structure
4370 *
4371 * There is no error returned here because the driver should be able to handle
4372 * 128 Byte cache lines, so we only print a warning in case issues are seen,
4373 * specifically with Tx.
4374 */
4375static void ice_verify_cacheline_size(struct ice_pf *pf)
4376{
4377 if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08004378 dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
Brett Creeleyc585ea42018-10-26 10:40:58 -07004379 ICE_CACHE_LINE_BYTES);
4380}
4381
4382/**
Paul M Stillwell Jre3710a02019-09-09 06:47:42 -07004383 * ice_send_version - update firmware with driver version
4384 * @pf: PF struct
4385 *
Tony Nguyend54699e2021-10-07 15:58:01 -07004386 * Returns 0 on success, else error code
Paul M Stillwell Jre3710a02019-09-09 06:47:42 -07004387 */
Tony Nguyen5e24d592021-10-07 15:56:57 -07004388static int ice_send_version(struct ice_pf *pf)
Paul M Stillwell Jre3710a02019-09-09 06:47:42 -07004389{
4390 struct ice_driver_ver dv;
4391
Jeff Kirsher34a2a3b82020-05-29 00:18:33 -07004392 dv.major_ver = 0xff;
4393 dv.minor_ver = 0xff;
4394 dv.build_ver = 0xff;
Paul M Stillwell Jre3710a02019-09-09 06:47:42 -07004395 dv.subbuild_ver = 0;
Jeff Kirsher34a2a3b82020-05-29 00:18:33 -07004396 strscpy((char *)dv.driver_string, UTS_RELEASE,
Paul M Stillwell Jre3710a02019-09-09 06:47:42 -07004397 sizeof(dv.driver_string));
4398 return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
4399}
4400
4401/**
Henry Tieman148beb62020-05-11 18:01:40 -07004402 * ice_init_fdir - Initialize flow director VSI and configuration
4403 * @pf: pointer to the PF instance
4404 *
4405 * returns 0 on success, negative on error
4406 */
4407static int ice_init_fdir(struct ice_pf *pf)
4408{
4409 struct device *dev = ice_pf_to_dev(pf);
4410 struct ice_vsi *ctrl_vsi;
4411 int err;
4412
4413 /* Side Band Flow Director needs to have a control VSI.
4414 * Allocate it and store it in the PF.
4415 */
4416 ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
4417 if (!ctrl_vsi) {
4418 dev_dbg(dev, "could not create control VSI\n");
4419 return -ENOMEM;
4420 }
4421
4422 err = ice_vsi_open_ctrl(ctrl_vsi);
4423 if (err) {
4424 dev_dbg(dev, "could not open control VSI\n");
4425 goto err_vsi_open;
4426 }
4427
4428 mutex_init(&pf->hw.fdir_fltr_lock);
4429
4430 err = ice_fdir_create_dflt_rules(pf);
4431 if (err)
4432 goto err_fdir_rule;
4433
4434 return 0;
4435
4436err_fdir_rule:
4437 ice_fdir_release_flows(&pf->hw);
4438 ice_vsi_close(ctrl_vsi);
4439err_vsi_open:
4440 ice_vsi_release(ctrl_vsi);
4441 if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4442 pf->vsi[pf->ctrl_vsi_idx] = NULL;
4443 pf->ctrl_vsi_idx = ICE_NO_VSI;
4444 }
4445 return err;
4446}
4447
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004448static void ice_deinit_fdir(struct ice_pf *pf)
4449{
4450 struct ice_vsi *vsi = ice_get_ctrl_vsi(pf);
4451
4452 if (!vsi)
4453 return;
4454
4455 ice_vsi_manage_fdir(vsi, false);
4456 ice_vsi_release(vsi);
4457 if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4458 pf->vsi[pf->ctrl_vsi_idx] = NULL;
4459 pf->ctrl_vsi_idx = ICE_NO_VSI;
4460 }
4461
4462 mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4463}
4464
Henry Tieman148beb62020-05-11 18:01:40 -07004465/**
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004466 * ice_get_opt_fw_name - return optional firmware file name or NULL
4467 * @pf: pointer to the PF instance
4468 */
4469static char *ice_get_opt_fw_name(struct ice_pf *pf)
4470{
4471 /* Optional firmware name same as default with additional dash
4472 * followed by a EUI-64 identifier (PCIe Device Serial Number)
4473 */
4474 struct pci_dev *pdev = pf->pdev;
Jacob Kellerceb2f002020-03-02 18:25:03 -08004475 char *opt_fw_filename;
4476 u64 dsn;
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004477
4478 /* Determine the name of the optional file using the DSN (two
4479 * dwords following the start of the DSN Capability).
4480 */
Jacob Kellerceb2f002020-03-02 18:25:03 -08004481 dsn = pci_get_dsn(pdev);
4482 if (!dsn)
4483 return NULL;
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004484
Jacob Kellerceb2f002020-03-02 18:25:03 -08004485 opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
4486 if (!opt_fw_filename)
4487 return NULL;
4488
Paul M Stillwell Jr1a9c5612020-05-15 17:55:03 -07004489 snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
Jacob Kellerceb2f002020-03-02 18:25:03 -08004490 ICE_DDP_PKG_PATH, dsn);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004491
4492 return opt_fw_filename;
4493}
4494
4495/**
4496 * ice_request_fw - Device initialization routine
4497 * @pf: pointer to the PF instance
Michal Wilczynskicc5776f2024-04-19 04:08:52 -04004498 * @firmware: double pointer to firmware struct
4499 *
4500 * Return: zero when successful, negative values otherwise.
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004501 */
Michal Wilczynskicc5776f2024-04-19 04:08:52 -04004502static int ice_request_fw(struct ice_pf *pf, const struct firmware **firmware)
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004503{
4504 char *opt_fw_filename = ice_get_opt_fw_name(pf);
Brett Creeley4015d112019-11-08 06:23:26 -08004505 struct device *dev = ice_pf_to_dev(pf);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004506 int err = 0;
4507
4508 /* optional device-specific DDP (if present) overrides the default DDP
4509 * package file. kernel logs a debug message if the file doesn't exist,
4510 * and warning messages for other errors.
4511 */
4512 if (opt_fw_filename) {
Michal Wilczynskicc5776f2024-04-19 04:08:52 -04004513 err = firmware_request_nowarn(firmware, opt_fw_filename, dev);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004514 kfree(opt_fw_filename);
Michal Wilczynskicc5776f2024-04-19 04:08:52 -04004515 if (!err)
4516 return err;
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004517 }
Michal Wilczynskicc5776f2024-04-19 04:08:52 -04004518 err = request_firmware(firmware, ICE_DDP_PKG_FILE, dev);
4519 if (err)
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08004520 dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
Michal Wilczynskicc5776f2024-04-19 04:08:52 -04004521
4522 return err;
4523}
4524
4525/**
4526 * ice_init_tx_topology - performs Tx topology initialization
4527 * @hw: pointer to the hardware structure
4528 * @firmware: pointer to firmware structure
4529 *
4530 * Return: zero when init was successful, negative values otherwise.
4531 */
4532static int
4533ice_init_tx_topology(struct ice_hw *hw, const struct firmware *firmware)
4534{
4535 u8 num_tx_sched_layers = hw->num_tx_sched_layers;
4536 struct ice_pf *pf = hw->back;
4537 struct device *dev;
4538 u8 *buf_copy;
4539 int err;
4540
4541 dev = ice_pf_to_dev(pf);
4542 /* ice_cfg_tx_topo buf argument is not a constant,
4543 * so we have to make a copy
4544 */
4545 buf_copy = kmemdup(firmware->data, firmware->size, GFP_KERNEL);
4546
4547 err = ice_cfg_tx_topo(hw, buf_copy, firmware->size);
4548 if (!err) {
4549 if (hw->num_tx_sched_layers > num_tx_sched_layers)
4550 dev_info(dev, "Tx scheduling layers switching feature disabled\n");
4551 else
4552 dev_info(dev, "Tx scheduling layers switching feature enabled\n");
4553 /* if there was a change in topology ice_cfg_tx_topo triggered
4554 * a CORER and we need to re-init hw
4555 */
4556 ice_deinit_hw(hw);
4557 err = ice_init_hw(hw);
4558
4559 return err;
4560 } else if (err == -EIO) {
4561 dev_info(dev, "DDP package does not support Tx scheduling layers switching feature - please update to the latest DDP package and try again\n");
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004562 }
4563
Michal Wilczynskicc5776f2024-04-19 04:08:52 -04004564 return 0;
4565}
4566
4567/**
4568 * ice_init_ddp_config - DDP related configuration
4569 * @hw: pointer to the hardware structure
4570 * @pf: pointer to pf structure
4571 *
4572 * This function loads DDP file from the disk, then initializes Tx
4573 * topology. At the end DDP package is loaded on the card.
4574 *
4575 * Return: zero when init was successful, negative values otherwise.
4576 */
4577static int ice_init_ddp_config(struct ice_hw *hw, struct ice_pf *pf)
4578{
4579 struct device *dev = ice_pf_to_dev(pf);
4580 const struct firmware *firmware = NULL;
4581 int err;
4582
4583 err = ice_request_fw(pf, &firmware);
4584 if (err) {
4585 dev_err(dev, "Fail during requesting FW: %d\n", err);
4586 return err;
4587 }
4588
4589 err = ice_init_tx_topology(hw, firmware);
4590 if (err) {
4591 dev_err(dev, "Fail during initialization of Tx topology: %d\n",
4592 err);
4593 release_firmware(firmware);
4594 return err;
4595 }
4596
4597 /* Download firmware to device */
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004598 ice_load_pkg(firmware, pf);
4599 release_firmware(firmware);
Michal Wilczynskicc5776f2024-04-19 04:08:52 -04004600
4601 return 0;
Tony Nguyen462acf6a2019-09-09 06:47:46 -07004602}
4603
4604/**
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07004605 * ice_print_wake_reason - show the wake up cause in the log
4606 * @pf: pointer to the PF struct
4607 */
4608static void ice_print_wake_reason(struct ice_pf *pf)
4609{
4610 u32 wus = pf->wakeup_reason;
4611 const char *wake_str;
4612
4613 /* if no wake event, nothing to print */
4614 if (!wus)
4615 return;
4616
4617 if (wus & PFPM_WUS_LNKC_M)
4618 wake_str = "Link\n";
4619 else if (wus & PFPM_WUS_MAG_M)
4620 wake_str = "Magic Packet\n";
4621 else if (wus & PFPM_WUS_MNG_M)
4622 wake_str = "Management\n";
4623 else if (wus & PFPM_WUS_FW_RST_WK_M)
4624 wake_str = "Firmware Reset\n";
4625 else
4626 wake_str = "Unknown\n";
4627
4628 dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4629}
4630
4631/**
Paul M Stillwell Jr96a9a932023-12-12 21:07:12 -08004632 * ice_pf_fwlog_update_module - update 1 module
4633 * @pf: pointer to the PF struct
4634 * @log_level: log_level to use for the @module
4635 * @module: module to update
4636 */
4637void ice_pf_fwlog_update_module(struct ice_pf *pf, int log_level, int module)
4638{
4639 struct ice_hw *hw = &pf->hw;
4640
4641 hw->fwlog_cfg.module_entries[module].log_level = log_level;
4642}
4643
4644/**
Paul M Stillwell Jr418e5342023-01-23 16:57:14 -08004645 * ice_register_netdev - register netdev
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004646 * @vsi: pointer to the VSI struct
Anirudh Venkataramanan1e23f072021-03-02 10:12:03 -08004647 */
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004648static int ice_register_netdev(struct ice_vsi *vsi)
Anirudh Venkataramanan1e23f072021-03-02 10:12:03 -08004649{
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004650 int err;
Anirudh Venkataramanan1e23f072021-03-02 10:12:03 -08004651
Anirudh Venkataramanan1e23f072021-03-02 10:12:03 -08004652 if (!vsi || !vsi->netdev)
4653 return -EIO;
4654
4655 err = register_netdev(vsi->netdev);
4656 if (err)
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004657 return err;
Anirudh Venkataramanan1e23f072021-03-02 10:12:03 -08004658
Anirudh Venkataramanana476d722021-03-02 10:15:41 -08004659 set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
Anirudh Venkataramanan1e23f072021-03-02 10:12:03 -08004660 netif_carrier_off(vsi->netdev);
4661 netif_tx_stop_all_queues(vsi->netdev);
Anirudh Venkataramanan1e23f072021-03-02 10:12:03 -08004662
Anirudh Venkataramanan1e23f072021-03-02 10:12:03 -08004663 return 0;
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004664}
4665
4666static void ice_unregister_netdev(struct ice_vsi *vsi)
4667{
4668 if (!vsi || !vsi->netdev)
4669 return;
4670
4671 unregister_netdev(vsi->netdev);
4672 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4673}
4674
4675/**
4676 * ice_cfg_netdev - Allocate, configure and register a netdev
4677 * @vsi: the VSI associated with the new netdev
4678 *
4679 * Returns 0 on success, negative value on failure
4680 */
4681static int ice_cfg_netdev(struct ice_vsi *vsi)
4682{
4683 struct ice_netdev_priv *np;
4684 struct net_device *netdev;
4685 u8 mac_addr[ETH_ALEN];
4686
4687 netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
4688 vsi->alloc_rxq);
4689 if (!netdev)
4690 return -ENOMEM;
4691
4692 set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4693 vsi->netdev = netdev;
4694 np = netdev_priv(netdev);
4695 np->vsi = vsi;
4696
4697 ice_set_netdev_features(netdev);
Lorenzo Bianconib6a41032023-02-14 15:39:27 +01004698 ice_set_ops(vsi);
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004699
4700 if (vsi->type == ICE_VSI_PF) {
4701 SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
4702 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4703 eth_hw_addr_set(netdev, mac_addr);
4704 }
4705
4706 netdev->priv_flags |= IFF_UNICAST_FLT;
4707
4708 /* Setup netdev TC information */
4709 ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
4710
4711 netdev->max_mtu = ICE_MAX_MTU;
4712
4713 return 0;
4714}
4715
4716static void ice_decfg_netdev(struct ice_vsi *vsi)
4717{
4718 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
Anirudh Venkataramanan1e23f072021-03-02 10:12:03 -08004719 free_netdev(vsi->netdev);
4720 vsi->netdev = NULL;
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004721}
4722
Jan Sokolowski57081552023-07-13 15:21:24 +02004723/**
4724 * ice_wait_for_fw - wait for full FW readiness
4725 * @hw: pointer to the hardware structure
4726 * @timeout: milliseconds that can elapse before timing out
4727 */
4728static int ice_wait_for_fw(struct ice_hw *hw, u32 timeout)
4729{
4730 int fw_loading;
4731 u32 elapsed = 0;
4732
4733 while (elapsed <= timeout) {
4734 fw_loading = rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M;
4735
4736 /* firmware was not yet loaded, we have to wait more */
4737 if (fw_loading) {
4738 elapsed += 100;
4739 msleep(100);
4740 continue;
4741 }
4742 return 0;
4743 }
4744
4745 return -ETIMEDOUT;
4746}
4747
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01004748int ice_init_dev(struct ice_pf *pf)
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004749{
4750 struct device *dev = ice_pf_to_dev(pf);
4751 struct ice_hw *hw = &pf->hw;
4752 int err;
4753
4754 err = ice_init_hw(hw);
4755 if (err) {
4756 dev_err(dev, "ice_init_hw failed: %d\n", err);
4757 return err;
4758 }
4759
Jan Sokolowski57081552023-07-13 15:21:24 +02004760 /* Some cards require longer initialization times
4761 * due to necessity of loading FW from an external source.
4762 * This can take even half a minute.
4763 */
4764 if (ice_is_pf_c827(hw)) {
4765 err = ice_wait_for_fw(hw, 30000);
4766 if (err) {
4767 dev_err(dev, "ice_wait_for_fw timed out");
4768 return err;
4769 }
4770 }
4771
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004772 ice_init_feature_support(pf);
4773
Michal Wilczynskicc5776f2024-04-19 04:08:52 -04004774 err = ice_init_ddp_config(hw, pf);
4775 if (err)
4776 return err;
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004777
Michal Wilczynskicc5776f2024-04-19 04:08:52 -04004778 /* if ice_init_ddp_config fails, ICE_FLAG_ADV_FEATURES bit won't be
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004779 * set in pf->state, which will cause ice_is_safe_mode to return
4780 * true
4781 */
4782 if (ice_is_safe_mode(pf)) {
4783 /* we already got function/device capabilities but these don't
4784 * reflect what the driver needs to do in safe mode. Instead of
4785 * adding conditional logic everywhere to ignore these
4786 * device/function capabilities, override them.
4787 */
4788 ice_set_safe_mode_caps(hw);
4789 }
4790
4791 err = ice_init_pf(pf);
4792 if (err) {
4793 dev_err(dev, "ice_init_pf failed: %d\n", err);
4794 goto err_init_pf;
4795 }
4796
4797 pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4798 pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4799 pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4800 pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4801 if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4802 pf->hw.udp_tunnel_nic.tables[0].n_entries =
4803 pf->hw.tnl.valid_count[TNL_VXLAN];
4804 pf->hw.udp_tunnel_nic.tables[0].tunnel_types =
4805 UDP_TUNNEL_TYPE_VXLAN;
4806 }
4807 if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4808 pf->hw.udp_tunnel_nic.tables[1].n_entries =
4809 pf->hw.tnl.valid_count[TNL_GENEVE];
4810 pf->hw.udp_tunnel_nic.tables[1].tunnel_types =
4811 UDP_TUNNEL_TYPE_GENEVE;
4812 }
4813
4814 err = ice_init_interrupt_scheme(pf);
4815 if (err) {
4816 dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4817 err = -EIO;
4818 goto err_init_interrupt_scheme;
4819 }
4820
4821 /* In case of MSIX we are going to setup the misc vector right here
4822 * to handle admin queue events etc. In case of legacy and MSI
4823 * the misc functionality and queue processing is combined in
4824 * the same vector and that gets setup at open.
4825 */
4826 err = ice_req_irq_msix_misc(pf);
4827 if (err) {
4828 dev_err(dev, "setup of misc vector failed: %d\n", err);
4829 goto err_req_irq_msix_misc;
4830 }
4831
4832 return 0;
4833
4834err_req_irq_msix_misc:
4835 ice_clear_interrupt_scheme(pf);
4836err_init_interrupt_scheme:
4837 ice_deinit_pf(pf);
4838err_init_pf:
4839 ice_deinit_hw(hw);
4840 return err;
4841}
4842
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01004843void ice_deinit_dev(struct ice_pf *pf)
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004844{
4845 ice_free_irq_msix_misc(pf);
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004846 ice_deinit_pf(pf);
4847 ice_deinit_hw(&pf->hw);
Jakub Buchocki24b454b2023-06-12 10:14:21 -07004848
4849 /* Service task is already stopped, so call reset directly. */
4850 ice_reset(&pf->hw, ICE_RESET_PFR);
4851 pci_wait_for_pending_transaction(pf->pdev);
4852 ice_clear_interrupt_scheme(pf);
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004853}
4854
4855static void ice_init_features(struct ice_pf *pf)
4856{
4857 struct device *dev = ice_pf_to_dev(pf);
4858
4859 if (ice_is_safe_mode(pf))
4860 return;
4861
4862 /* initialize DDP driven features */
4863 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4864 ice_ptp_init(pf);
4865
4866 if (ice_is_feature_supported(pf, ICE_F_GNSS))
4867 ice_gnss_init(pf);
4868
Arkadiusz Kubalewskid7999f52023-09-13 21:49:41 +01004869 if (ice_is_feature_supported(pf, ICE_F_CGU) ||
4870 ice_is_feature_supported(pf, ICE_F_PHY_RCLK))
4871 ice_dpll_init(pf);
4872
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004873 /* Note: Flow director init failure is non-fatal to load */
4874 if (ice_init_fdir(pf))
4875 dev_err(dev, "could not initialize flow director\n");
4876
4877 /* Note: DCB init failure is non-fatal to load */
4878 if (ice_init_pf_dcb(pf, false)) {
4879 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4880 clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4881 } else {
4882 ice_cfg_lldp_mib_change(&pf->hw, true);
4883 }
4884
4885 if (ice_init_lag(pf))
4886 dev_warn(dev, "Failed to init link aggregation support\n");
Konrad Knitter4da71a72023-12-01 10:08:39 -08004887
4888 ice_hwmon_init(pf);
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004889}
4890
4891static void ice_deinit_features(struct ice_pf *pf)
4892{
Mateusz Pacuszka42066c4d2023-10-11 16:33:34 -07004893 if (ice_is_safe_mode(pf))
4894 return;
4895
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004896 ice_deinit_lag(pf);
4897 if (test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags))
4898 ice_cfg_lldp_mib_change(&pf->hw, false);
4899 ice_deinit_fdir(pf);
4900 if (ice_is_feature_supported(pf, ICE_F_GNSS))
4901 ice_gnss_exit(pf);
4902 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4903 ice_ptp_release(pf);
Arkadiusz Kubalewskid7999f52023-09-13 21:49:41 +01004904 if (test_bit(ICE_FLAG_DPLL, pf->flags))
4905 ice_dpll_deinit(pf);
Michal Swiatkowskiaf41b182023-10-24 13:09:20 +02004906 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
4907 xa_destroy(&pf->eswitch.reprs);
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01004908}
4909
4910static void ice_init_wakeup(struct ice_pf *pf)
4911{
4912 /* Save wakeup reason register for later use */
4913 pf->wakeup_reason = rd32(&pf->hw, PFPM_WUS);
4914
4915 /* check for a power management event */
4916 ice_print_wake_reason(pf);
4917
4918 /* clear wake status, all bits */
4919 wr32(&pf->hw, PFPM_WUS, U32_MAX);
4920
4921 /* Disable WoL at init, wait for user to enable */
4922 device_set_wakeup_enable(ice_pf_to_dev(pf), false);
4923}
4924
4925static int ice_init_link(struct ice_pf *pf)
4926{
4927 struct device *dev = ice_pf_to_dev(pf);
4928 int err;
4929
4930 err = ice_init_link_events(pf->hw.port_info);
4931 if (err) {
4932 dev_err(dev, "ice_init_link_events failed: %d\n", err);
4933 return err;
4934 }
4935
4936 /* not a fatal error if this fails */
4937 err = ice_init_nvm_phy_type(pf->hw.port_info);
4938 if (err)
4939 dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4940
4941 /* not a fatal error if this fails */
4942 err = ice_update_link_info(pf->hw.port_info);
4943 if (err)
4944 dev_err(dev, "ice_update_link_info failed: %d\n", err);
4945
4946 ice_init_link_dflt_override(pf->hw.port_info);
4947
4948 ice_check_link_cfg_err(pf,
4949 pf->hw.port_info->phy.link_info.link_cfg_err);
4950
4951 /* if media available, initialize PHY settings */
4952 if (pf->hw.port_info->phy.link_info.link_info &
4953 ICE_AQ_MEDIA_AVAILABLE) {
4954 /* not a fatal error if this fails */
4955 err = ice_init_phy_user_cfg(pf->hw.port_info);
4956 if (err)
4957 dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4958
4959 if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4960 struct ice_vsi *vsi = ice_get_main_vsi(pf);
4961
4962 if (vsi)
4963 ice_configure_phy(vsi);
4964 }
4965 } else {
4966 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4967 }
4968
4969 return err;
4970}
4971
4972static int ice_init_pf_sw(struct ice_pf *pf)
4973{
4974 bool dvm = ice_is_dvm_ena(&pf->hw);
4975 struct ice_vsi *vsi;
4976 int err;
4977
4978 /* create switch struct for the switch element created by FW on boot */
4979 pf->first_sw = kzalloc(sizeof(*pf->first_sw), GFP_KERNEL);
4980 if (!pf->first_sw)
4981 return -ENOMEM;
4982
4983 if (pf->hw.evb_veb)
4984 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4985 else
4986 pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4987
4988 pf->first_sw->pf = pf;
4989
4990 /* record the sw_id available for later use */
4991 pf->first_sw->sw_id = pf->hw.port_info->sw_id;
4992
4993 err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
4994 if (err)
4995 goto err_aq_set_port_params;
4996
4997 vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
4998 if (!vsi) {
4999 err = -ENOMEM;
5000 goto err_pf_vsi_setup;
5001 }
5002
5003 return 0;
5004
5005err_pf_vsi_setup:
5006err_aq_set_port_params:
5007 kfree(pf->first_sw);
5008 return err;
5009}
5010
5011static void ice_deinit_pf_sw(struct ice_pf *pf)
5012{
5013 struct ice_vsi *vsi = ice_get_main_vsi(pf);
5014
5015 if (!vsi)
5016 return;
5017
5018 ice_vsi_release(vsi);
5019 kfree(pf->first_sw);
5020}
5021
5022static int ice_alloc_vsis(struct ice_pf *pf)
5023{
5024 struct device *dev = ice_pf_to_dev(pf);
5025
5026 pf->num_alloc_vsi = pf->hw.func_caps.guar_num_vsi;
5027 if (!pf->num_alloc_vsi)
5028 return -EIO;
5029
5030 if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
5031 dev_warn(dev,
5032 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
5033 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
5034 pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
5035 }
5036
5037 pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
5038 GFP_KERNEL);
5039 if (!pf->vsi)
5040 return -ENOMEM;
5041
5042 pf->vsi_stats = devm_kcalloc(dev, pf->num_alloc_vsi,
5043 sizeof(*pf->vsi_stats), GFP_KERNEL);
5044 if (!pf->vsi_stats) {
5045 devm_kfree(dev, pf->vsi);
5046 return -ENOMEM;
5047 }
5048
5049 return 0;
5050}
5051
5052static void ice_dealloc_vsis(struct ice_pf *pf)
5053{
5054 devm_kfree(ice_pf_to_dev(pf), pf->vsi_stats);
5055 pf->vsi_stats = NULL;
5056
5057 pf->num_alloc_vsi = 0;
5058 devm_kfree(ice_pf_to_dev(pf), pf->vsi);
5059 pf->vsi = NULL;
5060}
5061
5062static int ice_init_devlink(struct ice_pf *pf)
5063{
5064 int err;
5065
5066 err = ice_devlink_register_params(pf);
5067 if (err)
5068 return err;
5069
5070 ice_devlink_init_regions(pf);
5071 ice_devlink_register(pf);
5072
5073 return 0;
5074}
5075
5076static void ice_deinit_devlink(struct ice_pf *pf)
5077{
5078 ice_devlink_unregister(pf);
5079 ice_devlink_destroy_regions(pf);
5080 ice_devlink_unregister_params(pf);
5081}
5082
5083static int ice_init(struct ice_pf *pf)
5084{
5085 int err;
5086
5087 err = ice_init_dev(pf);
5088 if (err)
5089 return err;
5090
5091 err = ice_alloc_vsis(pf);
5092 if (err)
5093 goto err_alloc_vsis;
5094
5095 err = ice_init_pf_sw(pf);
5096 if (err)
5097 goto err_init_pf_sw;
5098
5099 ice_init_wakeup(pf);
5100
5101 err = ice_init_link(pf);
5102 if (err)
5103 goto err_init_link;
5104
5105 err = ice_send_version(pf);
5106 if (err)
5107 goto err_init_link;
5108
5109 ice_verify_cacheline_size(pf);
5110
5111 if (ice_is_safe_mode(pf))
5112 ice_set_safe_mode_vlan_cfg(pf);
5113 else
5114 /* print PCI link speed and width */
5115 pcie_print_link_status(pf->pdev);
5116
5117 /* ready to go, so clear down state bit */
5118 clear_bit(ICE_DOWN, pf->state);
5119 clear_bit(ICE_SERVICE_DIS, pf->state);
5120
5121 /* since everything is good, start the service timer */
5122 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5123
5124 return 0;
5125
5126err_init_link:
5127 ice_deinit_pf_sw(pf);
5128err_init_pf_sw:
5129 ice_dealloc_vsis(pf);
5130err_alloc_vsis:
5131 ice_deinit_dev(pf);
5132 return err;
5133}
5134
5135static void ice_deinit(struct ice_pf *pf)
5136{
5137 set_bit(ICE_SERVICE_DIS, pf->state);
5138 set_bit(ICE_DOWN, pf->state);
5139
5140 ice_deinit_pf_sw(pf);
5141 ice_dealloc_vsis(pf);
5142 ice_deinit_dev(pf);
5143}
5144
5145/**
5146 * ice_load - load pf by init hw and starting VSI
5147 * @pf: pointer to the pf instance
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005148 *
5149 * This function has to be called under devl_lock.
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005150 */
5151int ice_load(struct ice_pf *pf)
5152{
5153 struct ice_vsi *vsi;
5154 int err;
5155
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005156 devl_assert_locked(priv_to_devlink(pf));
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005157
5158 vsi = ice_get_main_vsi(pf);
Jacob Keller5e509ab2023-01-18 17:16:43 -08005159
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005160 /* init channel list */
5161 INIT_LIST_HEAD(&vsi->ch_list);
Jacob Keller5e509ab2023-01-18 17:16:43 -08005162
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005163 err = ice_cfg_netdev(vsi);
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005164 if (err)
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005165 return err;
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005166
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005167 /* Setup DCB netlink interface */
5168 ice_dcbnl_setup(vsi);
5169
5170 err = ice_init_mac_fltr(pf);
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005171 if (err)
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005172 goto err_init_mac_fltr;
5173
5174 err = ice_devlink_create_pf_port(pf);
5175 if (err)
5176 goto err_devlink_create_pf_port;
5177
5178 SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
5179
5180 err = ice_register_netdev(vsi);
5181 if (err)
5182 goto err_register_netdev;
5183
5184 err = ice_tc_indir_block_register(vsi);
5185 if (err)
5186 goto err_tc_indir_block_register;
5187
5188 ice_napi_add(vsi);
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005189
5190 err = ice_init_rdma(pf);
5191 if (err)
5192 goto err_init_rdma;
5193
5194 ice_init_features(pf);
5195 ice_service_task_restart(pf);
5196
5197 clear_bit(ICE_DOWN, pf->state);
5198
5199 return 0;
5200
5201err_init_rdma:
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005202 ice_tc_indir_block_unregister(vsi);
5203err_tc_indir_block_register:
5204 ice_unregister_netdev(vsi);
5205err_register_netdev:
5206 ice_devlink_destroy_pf_port(pf);
5207err_devlink_create_pf_port:
5208err_init_mac_fltr:
5209 ice_decfg_netdev(vsi);
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005210 return err;
5211}
5212
5213/**
5214 * ice_unload - unload pf by stopping VSI and deinit hw
5215 * @pf: pointer to the pf instance
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005216 *
5217 * This function has to be called under devl_lock.
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005218 */
5219void ice_unload(struct ice_pf *pf)
5220{
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005221 struct ice_vsi *vsi = ice_get_main_vsi(pf);
5222
5223 devl_assert_locked(priv_to_devlink(pf));
5224
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005225 ice_deinit_features(pf);
5226 ice_deinit_rdma(pf);
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005227 ice_tc_indir_block_unregister(vsi);
5228 ice_unregister_netdev(vsi);
5229 ice_devlink_destroy_pf_port(pf);
5230 ice_decfg_netdev(vsi);
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005231}
5232
Anirudh Venkataramanan1e23f072021-03-02 10:12:03 -08005233/**
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005234 * ice_probe - Device initialization routine
5235 * @pdev: PCI device information struct
5236 * @ent: entry in ice_pci_tbl
5237 *
5238 * Returns 0 on success, negative on failure
5239 */
Bruce Allanc8b7abd2019-02-26 16:35:11 -08005240static int
5241ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005242{
Bruce Allan77ed84f2019-02-08 12:50:50 -08005243 struct device *dev = &pdev->dev;
Michal Schmidt0e2bddf2024-03-26 00:20:37 +01005244 struct ice_adapter *adapter;
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005245 struct ice_pf *pf;
5246 struct ice_hw *hw;
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005247 int err;
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005248
Anirudh Venkataramanan50ac7472021-07-28 12:39:10 -07005249 if (pdev->is_virtfn) {
5250 dev_err(dev, "can't probe a virtual function\n");
5251 return -EINVAL;
5252 }
5253
Jesse Brandeburg0288c3e2023-10-11 16:33:33 -07005254 /* when under a kdump kernel initiate a reset before enabling the
5255 * device in order to clear out any pending DMA transactions. These
5256 * transactions can cause some systems to machine check when doing
5257 * the pcim_enable_device() below.
5258 */
5259 if (is_kdump_kernel()) {
5260 pci_save_state(pdev);
5261 pci_clear_master(pdev);
5262 err = pcie_flr(pdev);
5263 if (err)
5264 return err;
5265 pci_restore_state(pdev);
5266 }
5267
Tony Nguyen4ee656b2020-02-06 01:20:13 -08005268 /* this driver uses devres, see
5269 * Documentation/driver-api/driver-model/devres.rst
5270 */
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005271 err = pcim_enable_device(pdev);
5272 if (err)
5273 return err;
5274
Jesse Brandeburg80ad6dd2021-03-31 14:17:01 -07005275 err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005276 if (err) {
Bruce Allan77ed84f2019-02-08 12:50:50 -08005277 dev_err(dev, "BAR0 I/O map error %d\n", err);
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005278 return err;
5279 }
5280
Jacob Keller1adf7ea2020-03-11 18:58:15 -07005281 pf = ice_allocate_pf(dev);
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005282 if (!pf)
5283 return -ENOMEM;
5284
Dave Ertman73e30a62021-10-04 05:15:25 -07005285 /* initialize Auxiliary index to invalid value */
5286 pf->aux_idx = -1;
5287
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -07005288 /* set up for high or low DMA */
Bruce Allan77ed84f2019-02-08 12:50:50 -08005289 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005290 if (err) {
Bruce Allan77ed84f2019-02-08 12:50:50 -08005291 dev_err(dev, "DMA configuration failed: 0x%x\n", err);
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005292 return err;
5293 }
5294
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005295 pci_set_master(pdev);
5296
Michal Schmidt0e2bddf2024-03-26 00:20:37 +01005297 adapter = ice_adapter_get(pdev);
5298 if (IS_ERR(adapter))
5299 return PTR_ERR(adapter);
5300
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005301 pf->pdev = pdev;
Michal Schmidt0e2bddf2024-03-26 00:20:37 +01005302 pf->adapter = adapter;
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005303 pci_set_drvdata(pdev, pf);
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005304 set_bit(ICE_DOWN, pf->state);
Akeem G Abodunrin8d81fa52018-08-09 06:29:57 -07005305 /* Disable service task until DOWN bit is cleared */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005306 set_bit(ICE_SERVICE_DIS, pf->state);
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005307
5308 hw = &pf->hw;
5309 hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
Michal Swiatkowski4e568022019-10-09 07:09:46 -07005310 pci_save_state(pdev);
5311
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005312 hw->back = pf;
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005313 hw->port_info = NULL;
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005314 hw->vendor_id = pdev->vendor;
5315 hw->device_id = pdev->device;
5316 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
5317 hw->subsystem_vendor_id = pdev->subsystem_vendor;
5318 hw->subsystem_device_id = pdev->subsystem_device;
5319 hw->bus.device = PCI_SLOT(pdev->devfn);
5320 hw->bus.func = PCI_FUNC(pdev->devfn);
Anirudh Venkataramananf31e4b62018-03-20 07:58:07 -07005321 ice_set_ctrlq_len(hw);
5322
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005323 pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
5324
Anirudh Venkataramanan7ec59ee2018-03-20 07:58:06 -07005325#ifndef CONFIG_DYNAMIC_DEBUG
5326 if (debug < -1)
5327 hw->debug_mask = debug;
5328#endif
5329
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005330 err = ice_init(pf);
Anirudh Venkataramanan08771bc2021-02-26 13:19:22 -08005331 if (err)
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005332 goto err_init;
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07005333
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005334 devl_lock(priv_to_devlink(pf));
5335 err = ice_load(pf);
Anirudh Venkataramanan08771bc2021-02-26 13:19:22 -08005336 if (err)
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005337 goto err_load;
Dave Ertmand25a0fc2021-05-20 09:37:49 -05005338
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005339 err = ice_init_devlink(pf);
5340 if (err)
5341 goto err_init_devlink;
Michal Swiatkowski118c6bd2024-03-25 22:34:33 +01005342 devl_unlock(priv_to_devlink(pf));
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005343
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005344 return 0;
Anirudh Venkataramananf31e4b62018-03-20 07:58:07 -07005345
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005346err_init_devlink:
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005347 ice_unload(pf);
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005348err_load:
Michal Swiatkowski118c6bd2024-03-25 22:34:33 +01005349 devl_unlock(priv_to_devlink(pf));
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005350 ice_deinit(pf);
5351err_init:
Michal Schmidt0e2bddf2024-03-26 00:20:37 +01005352 ice_adapter_put(pdev);
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005353 pci_disable_device(pdev);
Anirudh Venkataramananf31e4b62018-03-20 07:58:07 -07005354 return err;
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005355}
5356
5357/**
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005358 * ice_set_wake - enable or disable Wake on LAN
5359 * @pf: pointer to the PF struct
5360 *
5361 * Simple helper for WoL control
5362 */
5363static void ice_set_wake(struct ice_pf *pf)
5364{
5365 struct ice_hw *hw = &pf->hw;
5366 bool wol = pf->wol_ena;
5367
5368 /* clear wake state, otherwise new wake events won't fire */
5369 wr32(hw, PFPM_WUS, U32_MAX);
5370
5371 /* enable / disable APM wake up, no RMW needed */
5372 wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
5373
5374 /* set magic packet filter enabled */
5375 wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
5376}
5377
5378/**
Tony Nguyenef860482021-03-02 10:15:45 -08005379 * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005380 * @pf: pointer to the PF struct
5381 *
5382 * Issue firmware command to enable multicast magic wake, making
5383 * sure that any locally administered address (LAA) is used for
5384 * wake, and that PF reset doesn't undo the LAA.
5385 */
5386static void ice_setup_mc_magic_wake(struct ice_pf *pf)
5387{
5388 struct device *dev = ice_pf_to_dev(pf);
5389 struct ice_hw *hw = &pf->hw;
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005390 u8 mac_addr[ETH_ALEN];
5391 struct ice_vsi *vsi;
Tony Nguyen5518ac22021-10-07 15:59:03 -07005392 int status;
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005393 u8 flags;
5394
5395 if (!pf->wol_ena)
5396 return;
5397
5398 vsi = ice_get_main_vsi(pf);
5399 if (!vsi)
5400 return;
5401
5402 /* Get current MAC address in case it's an LAA */
5403 if (vsi->netdev)
5404 ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
5405 else
5406 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
5407
5408 flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
5409 ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
5410 ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
5411
5412 status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
5413 if (status)
Tony Nguyen5f87ec42021-10-07 15:56:02 -07005414 dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
Tony Nguyen5518ac22021-10-07 15:59:03 -07005415 status, ice_aq_str(hw->adminq.sq_last_status));
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005416}
5417
5418/**
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005419 * ice_remove - Device removal routine
5420 * @pdev: PCI device information struct
5421 */
5422static void ice_remove(struct pci_dev *pdev)
5423{
5424 struct ice_pf *pf = pci_get_drvdata(pdev);
Dave Ertman81b23582018-09-19 17:43:07 -07005425 int i;
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005426
Anirudh Venkataramananafd9d4a2018-10-26 10:40:51 -07005427 for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
5428 if (!ice_is_reset_in_progress(pf->state))
5429 break;
5430 msleep(100);
5431 }
5432
Brett Creeleyf844d522020-02-27 10:14:55 -08005433 if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005434 set_bit(ICE_VF_RESETS_DISABLED, pf->state);
Brett Creeleyf844d522020-02-27 10:14:55 -08005435 ice_free_vfs(pf);
5436 }
5437
Konrad Knitter4da71a72023-12-01 10:08:39 -08005438 ice_hwmon_exit(pf);
5439
Akeem G Abodunrin8d81fa52018-08-09 06:29:57 -07005440 ice_service_task_stop(pf);
Jacob Kellerd69ea412020-07-23 17:22:03 -07005441 ice_aq_cancel_waiting_tasks(pf);
Dave Ertmanf9f53012021-05-20 09:37:51 -05005442 set_bit(ICE_DOWN, pf->state);
Jacob Kellerd69ea412020-07-23 17:22:03 -07005443
Brett Creeley28bf2672020-05-11 18:01:46 -07005444 if (!ice_is_safe_mode(pf))
5445 ice_remove_arfs(pf);
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005446
Michal Swiatkowski118c6bd2024-03-25 22:34:33 +01005447 devl_lock(priv_to_devlink(pf));
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005448 ice_deinit_devlink(pf);
5449
Wojciech Drewek41cc4e52024-02-05 14:03:56 +01005450 ice_unload(pf);
5451 devl_unlock(priv_to_devlink(pf));
5452
5453 ice_deinit(pf);
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07005454 ice_vsi_release_all(pf);
Michal Swiatkowski5b246e52022-12-21 12:38:18 +01005455
5456 ice_setup_mc_magic_wake(pf);
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005457 ice_set_wake(pf);
Jacob Keller1adf7ea2020-03-11 18:58:15 -07005458
Michal Schmidt0e2bddf2024-03-26 00:20:37 +01005459 ice_adapter_put(pdev);
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005460 pci_disable_device(pdev);
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005461}
5462
Brett Creeley5995b6d2019-02-13 10:51:15 -08005463/**
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005464 * ice_shutdown - PCI callback for shutting down device
5465 * @pdev: PCI device information struct
5466 */
5467static void ice_shutdown(struct pci_dev *pdev)
5468{
5469 struct ice_pf *pf = pci_get_drvdata(pdev);
5470
5471 ice_remove(pdev);
5472
5473 if (system_state == SYSTEM_POWER_OFF) {
5474 pci_wake_from_d3(pdev, pf->wol_ena);
5475 pci_set_power_state(pdev, PCI_D3hot);
5476 }
5477}
5478
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005479/**
5480 * ice_prepare_for_shutdown - prep for PCI shutdown
5481 * @pf: board private structure
5482 *
5483 * Inform or close all dependent features in prep for PCI device shutdown
5484 */
5485static void ice_prepare_for_shutdown(struct ice_pf *pf)
5486{
5487 struct ice_hw *hw = &pf->hw;
5488 u32 v;
5489
5490 /* Notify VFs of impending reset */
5491 if (ice_check_sq_alive(hw, &hw->mailboxq))
5492 ice_vc_notify_reset(pf);
5493
5494 dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
5495
5496 /* disable the VSIs and their queues that are not already DOWN */
5497 ice_pf_dis_all_vsi(pf, false);
5498
5499 ice_for_each_vsi(pf, v)
5500 if (pf->vsi[v])
5501 pf->vsi[v]->vsi_num = 0;
5502
Piotr Gardockifdd288e2024-06-14 12:38:11 +02005503 ice_shutdown_all_ctrlq(hw, true);
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005504}
5505
5506/**
5507 * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
5508 * @pf: board private structure to reinitialize
5509 *
5510 * This routine reinitialize interrupt scheme that was cleared during
5511 * power management suspend callback.
5512 *
5513 * This should be called during resume routine to re-allocate the q_vectors
5514 * and reacquire interrupts.
5515 */
5516static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
5517{
5518 struct device *dev = ice_pf_to_dev(pf);
5519 int ret, v;
5520
5521 /* Since we clear MSIX flag during suspend, we need to
5522 * set it back during resume...
5523 */
5524
5525 ret = ice_init_interrupt_scheme(pf);
5526 if (ret) {
5527 dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
5528 return ret;
5529 }
5530
5531 /* Remap vectors and rings, after successful re-init interrupts */
5532 ice_for_each_vsi(pf, v) {
5533 if (!pf->vsi[v])
5534 continue;
5535
5536 ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
5537 if (ret)
5538 goto err_reinit;
5539 ice_vsi_map_rings_to_vectors(pf->vsi[v]);
Amritha Nambiar080b0c82024-02-13 11:48:50 -08005540 ice_vsi_set_napi_queues(pf->vsi[v]);
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005541 }
5542
5543 ret = ice_req_irq_msix_misc(pf);
5544 if (ret) {
5545 dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
5546 ret);
5547 goto err_reinit;
5548 }
5549
5550 return 0;
5551
5552err_reinit:
5553 while (v--)
5554 if (pf->vsi[v])
5555 ice_vsi_free_q_vectors(pf->vsi[v]);
5556
5557 return ret;
5558}
5559
5560/**
5561 * ice_suspend
5562 * @dev: generic device information structure
5563 *
5564 * Power Management callback to quiesce the device and prepare
5565 * for D3 transition.
5566 */
Jesse Brandeburg75a3f932024-03-05 18:50:22 -08005567static int ice_suspend(struct device *dev)
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005568{
5569 struct pci_dev *pdev = to_pci_dev(dev);
5570 struct ice_pf *pf;
5571 int disabled, v;
5572
5573 pf = pci_get_drvdata(pdev);
5574
5575 if (!ice_pf_state_is_nominal(pf)) {
5576 dev_err(dev, "Device is not ready, no need to suspend it\n");
5577 return -EBUSY;
5578 }
5579
5580 /* Stop watchdog tasks until resume completion.
5581 * Even though it is most likely that the service task is
5582 * disabled if the device is suspended or down, the service task's
5583 * state is controlled by a different state bit, and we should
5584 * store and honor whatever state that bit is in at this point.
5585 */
5586 disabled = ice_service_task_stop(pf);
5587
En-Wei Wubc69ad72024-05-30 22:21:31 +08005588 ice_deinit_rdma(pf);
Dave Ertmanf9f53012021-05-20 09:37:51 -05005589
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005590 /* Already suspended?, then there is nothing to do */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005591 if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005592 if (!disabled)
5593 ice_service_task_restart(pf);
5594 return 0;
5595 }
5596
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005597 if (test_bit(ICE_DOWN, pf->state) ||
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005598 ice_is_reset_in_progress(pf->state)) {
5599 dev_err(dev, "can't suspend device in reset or already down\n");
5600 if (!disabled)
5601 ice_service_task_restart(pf);
5602 return 0;
5603 }
5604
5605 ice_setup_mc_magic_wake(pf);
5606
5607 ice_prepare_for_shutdown(pf);
5608
5609 ice_set_wake(pf);
5610
5611 /* Free vectors, clear the interrupt scheme and release IRQs
5612 * for proper hibernation, especially with large number of CPUs.
5613 * Otherwise hibernation might fail when mapping all the vectors back
5614 * to CPU0.
5615 */
5616 ice_free_irq_msix_misc(pf);
5617 ice_for_each_vsi(pf, v) {
5618 if (!pf->vsi[v])
5619 continue;
5620 ice_vsi_free_q_vectors(pf->vsi[v]);
5621 }
5622 ice_clear_interrupt_scheme(pf);
5623
Anirudh Venkataramanan466e4392020-09-02 08:53:45 -07005624 pci_save_state(pdev);
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005625 pci_wake_from_d3(pdev, pf->wol_ena);
5626 pci_set_power_state(pdev, PCI_D3hot);
5627 return 0;
5628}
5629
5630/**
5631 * ice_resume - PM callback for waking up from D3
5632 * @dev: generic device information structure
5633 */
Jesse Brandeburg75a3f932024-03-05 18:50:22 -08005634static int ice_resume(struct device *dev)
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005635{
5636 struct pci_dev *pdev = to_pci_dev(dev);
5637 enum ice_reset_req reset_type;
5638 struct ice_pf *pf;
5639 struct ice_hw *hw;
5640 int ret;
5641
5642 pci_set_power_state(pdev, PCI_D0);
5643 pci_restore_state(pdev);
5644 pci_save_state(pdev);
5645
5646 if (!pci_device_is_present(pdev))
5647 return -ENODEV;
5648
5649 ret = pci_enable_device_mem(pdev);
5650 if (ret) {
5651 dev_err(dev, "Cannot enable device after suspend\n");
5652 return ret;
5653 }
5654
5655 pf = pci_get_drvdata(pdev);
5656 hw = &pf->hw;
5657
5658 pf->wakeup_reason = rd32(hw, PFPM_WUS);
5659 ice_print_wake_reason(pf);
5660
5661 /* We cleared the interrupt scheme when we suspended, so we need to
5662 * restore it now to resume device functionality.
5663 */
5664 ret = ice_reinit_interrupt_scheme(pf);
5665 if (ret)
5666 dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5667
En-Wei Wubc69ad72024-05-30 22:21:31 +08005668 ret = ice_init_rdma(pf);
5669 if (ret)
5670 dev_err(dev, "Reinitialize RDMA during resume failed: %d\n",
5671 ret);
5672
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005673 clear_bit(ICE_DOWN, pf->state);
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005674 /* Now perform PF reset and rebuild */
5675 reset_type = ICE_RESET_PFR;
5676 /* re-enable service task for reset, but allow reset to schedule it */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005677 clear_bit(ICE_SERVICE_DIS, pf->state);
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005678
5679 if (ice_schedule_reset(pf, reset_type))
5680 dev_err(dev, "Reset during resume failed.\n");
5681
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005682 clear_bit(ICE_SUSPENDED, pf->state);
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005683 ice_service_task_restart(pf);
5684
5685 /* Restart the service task */
5686 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5687
5688 return 0;
5689}
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005690
5691/**
Brett Creeley5995b6d2019-02-13 10:51:15 -08005692 * ice_pci_err_detected - warning that PCI error has been detected
5693 * @pdev: PCI device information struct
5694 * @err: the type of PCI error
5695 *
5696 * Called to warn that something happened on the PCI bus and the error handling
5697 * is in progress. Allows the driver to gracefully prepare/handle PCI errors.
5698 */
5699static pci_ers_result_t
Luc Van Oostenryck16d79cd2020-07-02 18:26:49 +02005700ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
Brett Creeley5995b6d2019-02-13 10:51:15 -08005701{
5702 struct ice_pf *pf = pci_get_drvdata(pdev);
5703
5704 if (!pf) {
5705 dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5706 __func__, err);
5707 return PCI_ERS_RESULT_DISCONNECT;
5708 }
5709
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005710 if (!test_bit(ICE_SUSPENDED, pf->state)) {
Brett Creeley5995b6d2019-02-13 10:51:15 -08005711 ice_service_task_stop(pf);
5712
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005713 if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5714 set_bit(ICE_PFR_REQ, pf->state);
Kiran Patilfbc7b272021-10-15 16:35:16 -07005715 ice_prepare_for_reset(pf, ICE_RESET_PFR);
Brett Creeley5995b6d2019-02-13 10:51:15 -08005716 }
5717 }
5718
5719 return PCI_ERS_RESULT_NEED_RESET;
5720}
5721
5722/**
5723 * ice_pci_err_slot_reset - a PCI slot reset has just happened
5724 * @pdev: PCI device information struct
5725 *
5726 * Called to determine if the driver can recover from the PCI slot reset by
5727 * using a register read to determine if the device is recoverable.
5728 */
5729static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5730{
5731 struct ice_pf *pf = pci_get_drvdata(pdev);
5732 pci_ers_result_t result;
5733 int err;
5734 u32 reg;
5735
5736 err = pci_enable_device_mem(pdev);
5737 if (err) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08005738 dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
Brett Creeley5995b6d2019-02-13 10:51:15 -08005739 err);
5740 result = PCI_ERS_RESULT_DISCONNECT;
5741 } else {
5742 pci_set_master(pdev);
5743 pci_restore_state(pdev);
5744 pci_save_state(pdev);
5745 pci_wake_from_d3(pdev, false);
5746
5747 /* Check for life */
5748 reg = rd32(&pf->hw, GLGEN_RTRIG);
5749 if (!reg)
5750 result = PCI_ERS_RESULT_RECOVERED;
5751 else
5752 result = PCI_ERS_RESULT_DISCONNECT;
5753 }
5754
Brett Creeley5995b6d2019-02-13 10:51:15 -08005755 return result;
5756}
5757
5758/**
5759 * ice_pci_err_resume - restart operations after PCI error recovery
5760 * @pdev: PCI device information struct
5761 *
5762 * Called to allow the driver to bring things back up after PCI error and/or
5763 * reset recovery have finished
5764 */
5765static void ice_pci_err_resume(struct pci_dev *pdev)
5766{
5767 struct ice_pf *pf = pci_get_drvdata(pdev);
5768
5769 if (!pf) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08005770 dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5771 __func__);
Brett Creeley5995b6d2019-02-13 10:51:15 -08005772 return;
5773 }
5774
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005775 if (test_bit(ICE_SUSPENDED, pf->state)) {
Brett Creeley5995b6d2019-02-13 10:51:15 -08005776 dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5777 __func__);
5778 return;
5779 }
5780
Przemek Kitszel31642d22023-10-19 10:32:19 -07005781 ice_restore_all_vfs_msi_state(pf);
Nick Nunleya54a0b22020-07-13 13:53:07 -07005782
Brett Creeley5995b6d2019-02-13 10:51:15 -08005783 ice_do_reset(pf, ICE_RESET_PFR);
5784 ice_service_task_restart(pf);
5785 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5786}
5787
5788/**
5789 * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5790 * @pdev: PCI device information struct
5791 */
5792static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5793{
5794 struct ice_pf *pf = pci_get_drvdata(pdev);
5795
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005796 if (!test_bit(ICE_SUSPENDED, pf->state)) {
Brett Creeley5995b6d2019-02-13 10:51:15 -08005797 ice_service_task_stop(pf);
5798
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005799 if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5800 set_bit(ICE_PFR_REQ, pf->state);
Kiran Patilfbc7b272021-10-15 16:35:16 -07005801 ice_prepare_for_reset(pf, ICE_RESET_PFR);
Brett Creeley5995b6d2019-02-13 10:51:15 -08005802 }
5803 }
5804}
5805
5806/**
5807 * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5808 * @pdev: PCI device information struct
5809 */
5810static void ice_pci_err_reset_done(struct pci_dev *pdev)
5811{
5812 ice_pci_err_resume(pdev);
5813}
5814
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005815/* ice_pci_tbl - PCI Device ID Table
5816 *
5817 * Wildcard entries (PCI_ANY_ID) should come last
5818 * Last entry must be all 0s
5819 *
5820 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5821 * Class, Class Mask, private data (not used) }
5822 */
5823static const struct pci_device_id ice_pci_tbl[] = {
Pawel Chmielewskif8ab08c2023-10-25 14:41:56 -07005824 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE) },
5825 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP) },
5826 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP) },
5827 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE) },
5828 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP) },
5829 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP) },
5830 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE) },
5831 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP) },
5832 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP) },
5833 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T) },
5834 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII) },
5835 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE) },
5836 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP) },
5837 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP) },
5838 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T) },
5839 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII) },
5840 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE) },
5841 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP) },
5842 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T) },
5843 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII) },
5844 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE) },
5845 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP) },
5846 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T) },
5847 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE) },
5848 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP) },
5849 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822_SI_DFLT) },
Grzegorz Nitkaf64e1892023-12-06 20:29:17 +01005850 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_BACKPLANE), },
5851 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_QSFP), },
5852 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_SFP), },
5853 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_SGMII), },
Paul Greenwalta8e682f2024-03-28 21:07:08 -04005854 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_BACKPLANE) },
5855 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_QSFP56) },
5856 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_SFP) },
5857 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_SFP_DD) },
Paul Greenwalt4fd10402024-03-28 21:07:07 -04005858 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_BACKPLANE), },
5859 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_BACKPLANE), },
5860 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_QSFP), },
5861 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_QSFP), },
5862 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_SFP), },
5863 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_SFP), },
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005864 /* required last entry */
Pawel Chmielewskif8ab08c2023-10-25 14:41:56 -07005865 {}
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005866};
5867MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5868
Jesse Brandeburg75a3f932024-03-05 18:50:22 -08005869static DEFINE_SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005870
Brett Creeley5995b6d2019-02-13 10:51:15 -08005871static const struct pci_error_handlers ice_pci_err_handler = {
5872 .error_detected = ice_pci_err_detected,
5873 .slot_reset = ice_pci_err_slot_reset,
5874 .reset_prepare = ice_pci_err_reset_prepare,
5875 .reset_done = ice_pci_err_reset_done,
5876 .resume = ice_pci_err_resume
5877};
5878
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005879static struct pci_driver ice_driver = {
5880 .name = KBUILD_MODNAME,
5881 .id_table = ice_pci_tbl,
5882 .probe = ice_probe,
5883 .remove = ice_remove,
Jesse Brandeburg75a3f932024-03-05 18:50:22 -08005884 .driver.pm = pm_sleep_ptr(&ice_pm_ops),
Akeem G Abodunrin769c5002020-07-09 09:16:03 -07005885 .shutdown = ice_shutdown,
Anirudh Venkataramananddf30f72018-09-19 17:42:55 -07005886 .sriov_configure = ice_sriov_configure,
Michal Swiatkowski05c166872023-10-19 10:32:22 -07005887 .sriov_get_vf_total_msix = ice_sriov_get_vf_total_msix,
5888 .sriov_set_msix_vec_count = ice_sriov_set_msix_vec_count,
Brett Creeley5995b6d2019-02-13 10:51:15 -08005889 .err_handler = &ice_pci_err_handler
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005890};
5891
5892/**
5893 * ice_module_init - Driver registration routine
5894 *
5895 * ice_module_init is the first routine called when the driver is
5896 * loaded. All it does is register with the PCI subsystem.
5897 */
5898static int __init ice_module_init(void)
5899{
Dave Ertmanbb52f422023-06-20 15:18:46 -07005900 int status = -ENOMEM;
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005901
Jeff Kirsher34a2a3b82020-05-29 00:18:33 -07005902 pr_info("%s\n", ice_driver_string);
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005903 pr_info("%s\n", ice_copyright);
5904
Pawel Chmielewski982b0192023-10-15 19:43:04 -04005905 ice_adv_lnk_speed_maps_init();
5906
Anirudh Venkataramanan4d159f72023-01-30 14:06:40 -08005907 ice_wq = alloc_workqueue("%s", 0, 0, KBUILD_MODNAME);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07005908 if (!ice_wq) {
5909 pr_err("Failed to create workqueue\n");
Dave Ertmanbb52f422023-06-20 15:18:46 -07005910 return status;
5911 }
5912
5913 ice_lag_wq = alloc_ordered_workqueue("ice_lag_wq", 0);
5914 if (!ice_lag_wq) {
5915 pr_err("Failed to create LAG workqueue\n");
5916 goto err_dest_wq;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07005917 }
5918
Paul M Stillwell Jr96a9a932023-12-12 21:07:12 -08005919 ice_debugfs_init();
5920
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005921 status = pci_register_driver(&ice_driver);
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07005922 if (status) {
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -07005923 pr_err("failed to register PCI driver, err %d\n", status);
Dave Ertmanbb52f422023-06-20 15:18:46 -07005924 goto err_dest_lag_wq;
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07005925 }
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005926
Dave Ertmanbb52f422023-06-20 15:18:46 -07005927 return 0;
5928
5929err_dest_lag_wq:
5930 destroy_workqueue(ice_lag_wq);
Paul M Stillwell Jr96a9a932023-12-12 21:07:12 -08005931 ice_debugfs_exit();
Dave Ertmanbb52f422023-06-20 15:18:46 -07005932err_dest_wq:
5933 destroy_workqueue(ice_wq);
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005934 return status;
5935}
5936module_init(ice_module_init);
5937
5938/**
5939 * ice_module_exit - Driver exit cleanup routine
5940 *
5941 * ice_module_exit is called just before the driver is removed
5942 * from memory.
5943 */
5944static void __exit ice_module_exit(void)
5945{
5946 pci_unregister_driver(&ice_driver);
Wojciech Drewek500d0df2024-02-05 14:03:57 +01005947 ice_debugfs_exit();
Anirudh Venkataramanan940b61a2018-03-20 07:58:10 -07005948 destroy_workqueue(ice_wq);
Dave Ertmanbb52f422023-06-20 15:18:46 -07005949 destroy_workqueue(ice_lag_wq);
Anirudh Venkataramanan837f08f2018-03-20 07:58:05 -07005950 pr_info("module unloaded\n");
5951}
5952module_exit(ice_module_exit);
Anirudh Venkataramanan3a858ba2018-03-20 07:58:11 -07005953
5954/**
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -08005955 * ice_set_mac_address - NDO callback to set MAC address
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07005956 * @netdev: network interface device structure
5957 * @pi: pointer to an address structure
5958 *
5959 * Returns 0 on success, negative on failure
5960 */
5961static int ice_set_mac_address(struct net_device *netdev, void *pi)
5962{
5963 struct ice_netdev_priv *np = netdev_priv(netdev);
5964 struct ice_vsi *vsi = np->vsi;
5965 struct ice_pf *pf = vsi->back;
5966 struct ice_hw *hw = &pf->hw;
5967 struct sockaddr *addr = pi;
Brett Creeleyb357d9712021-08-24 12:27:53 -07005968 u8 old_mac[ETH_ALEN];
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07005969 u8 flags = 0;
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07005970 u8 *mac;
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07005971 int err;
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07005972
5973 mac = (u8 *)addr->sa_data;
5974
5975 if (!is_valid_ether_addr(mac))
5976 return -EADDRNOTAVAIL;
5977
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08005978 if (test_bit(ICE_DOWN, pf->state) ||
Dave Ertman5df7e452018-09-19 17:23:11 -07005979 ice_is_reset_in_progress(pf->state)) {
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07005980 netdev_err(netdev, "can't set mac %pM. device not ready\n",
5981 mac);
5982 return -EBUSY;
5983 }
5984
Kiran Patil9fea7492021-10-15 16:35:17 -07005985 if (ice_chnl_dmac_fltr_cnt(pf)) {
5986 netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
5987 mac);
5988 return -EAGAIN;
5989 }
5990
Brett Creeley3ba7f532021-08-06 09:51:27 -07005991 netif_addr_lock_bh(netdev);
Brett Creeleyb357d9712021-08-24 12:27:53 -07005992 ether_addr_copy(old_mac, netdev->dev_addr);
5993 /* change the netdev's MAC address */
Jakub Kicinskia05e4c02021-10-04 09:05:21 -07005994 eth_hw_addr_set(netdev, mac);
Brett Creeleyb357d9712021-08-24 12:27:53 -07005995 netif_addr_unlock_bh(netdev);
5996
Lihong Yang757976a2020-05-07 17:41:09 -07005997 /* Clean up old MAC filter. Not an error if old filter doesn't exist */
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07005998 err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
5999 if (err && err != -ENOENT) {
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006000 err = -EADDRNOTAVAIL;
Akeem G Abodunrinbbb968e2019-07-25 02:53:51 -07006001 goto err_update_filters;
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006002 }
6003
Nick Nunley13ed5e82020-11-20 16:38:33 -08006004 /* Add filter for new MAC. If filter exists, return success */
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07006005 err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
Ivan Vecera2c0069f2022-03-31 09:20:07 -07006006 if (err == -EEXIST) {
Nick Nunley13ed5e82020-11-20 16:38:33 -08006007 /* Although this MAC filter is already present in hardware it's
6008 * possible in some cases (e.g. bonding) that dev_addr was
6009 * modified outside of the driver and needs to be restored back
6010 * to this value.
6011 */
Lihong Yang757976a2020-05-07 17:41:09 -07006012 netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
Ivan Vecera2c0069f2022-03-31 09:20:07 -07006013
6014 return 0;
6015 } else if (err) {
Brett Creeley3ba7f532021-08-06 09:51:27 -07006016 /* error if the new filter addition failed */
Lihong Yang757976a2020-05-07 17:41:09 -07006017 err = -EADDRNOTAVAIL;
Ivan Vecera2c0069f2022-03-31 09:20:07 -07006018 }
Lihong Yang757976a2020-05-07 17:41:09 -07006019
Akeem G Abodunrinbbb968e2019-07-25 02:53:51 -07006020err_update_filters:
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006021 if (err) {
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -07006022 netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006023 mac);
Brett Creeleyb357d9712021-08-24 12:27:53 -07006024 netif_addr_lock_bh(netdev);
Jakub Kicinskif3956eb2021-10-01 14:32:23 -07006025 eth_hw_addr_set(netdev, old_mac);
Brett Creeley3ba7f532021-08-06 09:51:27 -07006026 netif_addr_unlock_bh(netdev);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006027 return err;
6028 }
6029
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -07006030 netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006031 netdev->dev_addr);
6032
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -08006033 /* write new MAC address to the firmware */
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006034 flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07006035 err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
6036 if (err) {
Tony Nguyen5f87ec42021-10-07 15:56:02 -07006037 netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07006038 mac, err);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006039 }
6040 return 0;
6041}
6042
6043/**
6044 * ice_set_rx_mode - NDO callback to set the netdev filters
6045 * @netdev: network interface device structure
6046 */
6047static void ice_set_rx_mode(struct net_device *netdev)
6048{
6049 struct ice_netdev_priv *np = netdev_priv(netdev);
6050 struct ice_vsi *vsi = np->vsi;
6051
Wojciech Drewek2571a3f2023-07-12 13:03:27 +02006052 if (!vsi || ice_is_switchdev_running(vsi->back))
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006053 return;
6054
6055 /* Set the flags to synchronize filters
6056 * ndo_set_rx_mode may be triggered even without a change in netdev
6057 * flags
6058 */
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -08006059 set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
6060 set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006061 set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
6062
6063 /* schedule our worker thread which will take care of
6064 * applying the new filter changes
6065 */
6066 ice_service_task_schedule(vsi->back);
6067}
6068
6069/**
Usha Ketineni1ddef452019-11-06 02:05:28 -08006070 * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
6071 * @netdev: network interface device structure
6072 * @queue_index: Queue ID
6073 * @maxrate: maximum bandwidth in Mbps
6074 */
6075static int
6076ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
6077{
6078 struct ice_netdev_priv *np = netdev_priv(netdev);
6079 struct ice_vsi *vsi = np->vsi;
Usha Ketineni1ddef452019-11-06 02:05:28 -08006080 u16 q_handle;
Tony Nguyen5518ac22021-10-07 15:59:03 -07006081 int status;
Usha Ketineni1ddef452019-11-06 02:05:28 -08006082 u8 tc;
6083
6084 /* Validate maxrate requested is within permitted range */
6085 if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08006086 netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
Usha Ketineni1ddef452019-11-06 02:05:28 -08006087 maxrate, queue_index);
6088 return -EINVAL;
6089 }
6090
6091 q_handle = vsi->tx_rings[queue_index]->q_handle;
6092 tc = ice_dcb_get_tc(vsi, queue_index);
6093
Sridhar Samudrala479cdfe2023-06-09 17:40:24 -07006094 vsi = ice_locate_vsi_using_queue(vsi, queue_index);
6095 if (!vsi) {
6096 netdev_err(netdev, "Invalid VSI for given queue %d\n",
6097 queue_index);
6098 return -EINVAL;
6099 }
6100
Usha Ketineni1ddef452019-11-06 02:05:28 -08006101 /* Set BW back to default, when user set maxrate to 0 */
6102 if (!maxrate)
6103 status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
6104 q_handle, ICE_MAX_BW);
6105 else
6106 status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
6107 q_handle, ICE_MAX_BW, maxrate * 1000);
Tony Nguyenc1484692021-10-07 16:01:58 -07006108 if (status)
Tony Nguyen5f87ec42021-10-07 15:56:02 -07006109 netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
6110 status);
Usha Ketineni1ddef452019-11-06 02:05:28 -08006111
Tony Nguyenc1484692021-10-07 16:01:58 -07006112 return status;
Usha Ketineni1ddef452019-11-06 02:05:28 -08006113}
6114
6115/**
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006116 * ice_fdb_add - add an entry to the hardware database
6117 * @ndm: the input from the stack
6118 * @tb: pointer to array of nladdr (unused)
6119 * @dev: the net device pointer
6120 * @addr: the MAC address entry being added
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -08006121 * @vid: VLAN ID
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006122 * @flags: instructions from stack about fdb operation
Bruce Allan99be37e2019-02-08 12:50:28 -08006123 * @extack: netlink extended ack
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006124 */
Bruce Allan99be37e2019-02-08 12:50:28 -08006125static int
6126ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
6127 struct net_device *dev, const unsigned char *addr, u16 vid,
6128 u16 flags, struct netlink_ext_ack __always_unused *extack)
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006129{
6130 int err;
6131
6132 if (vid) {
6133 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
6134 return -EINVAL;
6135 }
6136 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6137 netdev_err(dev, "FDB only supports static addresses\n");
6138 return -EINVAL;
6139 }
6140
6141 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6142 err = dev_uc_add_excl(dev, addr);
6143 else if (is_multicast_ether_addr(addr))
6144 err = dev_mc_add_excl(dev, addr);
6145 else
6146 err = -EINVAL;
6147
6148 /* Only return duplicate errors if NLM_F_EXCL is set */
6149 if (err == -EEXIST && !(flags & NLM_F_EXCL))
6150 err = 0;
6151
6152 return err;
6153}
6154
6155/**
6156 * ice_fdb_del - delete an entry from the hardware database
6157 * @ndm: the input from the stack
6158 * @tb: pointer to array of nladdr (unused)
6159 * @dev: the net device pointer
6160 * @addr: the MAC address entry being added
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -08006161 * @vid: VLAN ID
Alaa Mohamedca4567f2022-05-05 17:09:57 +02006162 * @extack: netlink extended ack
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006163 */
Bruce Allanc8b7abd2019-02-26 16:35:11 -08006164static int
6165ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
6166 struct net_device *dev, const unsigned char *addr,
Alaa Mohamedca4567f2022-05-05 17:09:57 +02006167 __always_unused u16 vid, struct netlink_ext_ack *extack)
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006168{
6169 int err;
6170
6171 if (ndm->ndm_state & NUD_PERMANENT) {
6172 netdev_err(dev, "FDB only supports static addresses\n");
6173 return -EINVAL;
6174 }
6175
6176 if (is_unicast_ether_addr(addr))
6177 err = dev_uc_del(dev, addr);
6178 else if (is_multicast_ether_addr(addr))
6179 err = dev_mc_del(dev, addr);
6180 else
6181 err = -EINVAL;
6182
6183 return err;
6184}
6185
Brett Creeley1babaf72021-12-02 08:38:50 -08006186#define NETIF_VLAN_OFFLOAD_FEATURES (NETIF_F_HW_VLAN_CTAG_RX | \
6187 NETIF_F_HW_VLAN_CTAG_TX | \
6188 NETIF_F_HW_VLAN_STAG_RX | \
6189 NETIF_F_HW_VLAN_STAG_TX)
6190
Anatolii Gerasymenkoaffa1022022-07-27 09:24:06 +02006191#define NETIF_VLAN_STRIPPING_FEATURES (NETIF_F_HW_VLAN_CTAG_RX | \
6192 NETIF_F_HW_VLAN_STAG_RX)
6193
Brett Creeley1babaf72021-12-02 08:38:50 -08006194#define NETIF_VLAN_FILTERING_FEATURES (NETIF_F_HW_VLAN_CTAG_FILTER | \
6195 NETIF_F_HW_VLAN_STAG_FILTER)
6196
6197/**
6198 * ice_fix_features - fix the netdev features flags based on device limitations
6199 * @netdev: ptr to the netdev that flags are being fixed on
6200 * @features: features that need to be checked and possibly fixed
6201 *
6202 * Make sure any fixups are made to features in this callback. This enables the
6203 * driver to not have to check unsupported configurations throughout the driver
6204 * because that's the responsiblity of this callback.
6205 *
6206 * Single VLAN Mode (SVM) Supported Features:
6207 * NETIF_F_HW_VLAN_CTAG_FILTER
6208 * NETIF_F_HW_VLAN_CTAG_RX
6209 * NETIF_F_HW_VLAN_CTAG_TX
6210 *
6211 * Double VLAN Mode (DVM) Supported Features:
6212 * NETIF_F_HW_VLAN_CTAG_FILTER
6213 * NETIF_F_HW_VLAN_CTAG_RX
6214 * NETIF_F_HW_VLAN_CTAG_TX
6215 *
6216 * NETIF_F_HW_VLAN_STAG_FILTER
6217 * NETIF_HW_VLAN_STAG_RX
6218 * NETIF_HW_VLAN_STAG_TX
6219 *
6220 * Features that need fixing:
6221 * Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
6222 * These are mutually exlusive as the VSI context cannot support multiple
6223 * VLAN ethertypes simultaneously for stripping and/or insertion. If this
6224 * is not done, then default to clearing the requested STAG offload
6225 * settings.
6226 *
6227 * All supported filtering has to be enabled or disabled together. For
6228 * example, in DVM, CTAG and STAG filtering have to be enabled and disabled
6229 * together. If this is not done, then default to VLAN filtering disabled.
6230 * These are mutually exclusive as there is currently no way to
6231 * enable/disable VLAN filtering based on VLAN ethertype when using VLAN
6232 * prune rules.
6233 */
6234static netdev_features_t
6235ice_fix_features(struct net_device *netdev, netdev_features_t features)
6236{
6237 struct ice_netdev_priv *np = netdev_priv(netdev);
Roman Storozhenko9542ef42022-06-07 08:54:57 +02006238 netdev_features_t req_vlan_fltr, cur_vlan_fltr;
6239 bool cur_ctag, cur_stag, req_ctag, req_stag;
Brett Creeley1babaf72021-12-02 08:38:50 -08006240
Roman Storozhenko9542ef42022-06-07 08:54:57 +02006241 cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
6242 cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6243 cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
Brett Creeley1babaf72021-12-02 08:38:50 -08006244
Roman Storozhenko9542ef42022-06-07 08:54:57 +02006245 req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
6246 req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6247 req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
Brett Creeley1babaf72021-12-02 08:38:50 -08006248
Roman Storozhenko9542ef42022-06-07 08:54:57 +02006249 if (req_vlan_fltr != cur_vlan_fltr) {
6250 if (ice_is_dvm_ena(&np->vsi->back->hw)) {
6251 if (req_ctag && req_stag) {
6252 features |= NETIF_VLAN_FILTERING_FEATURES;
6253 } else if (!req_ctag && !req_stag) {
6254 features &= ~NETIF_VLAN_FILTERING_FEATURES;
6255 } else if ((!cur_ctag && req_ctag && !cur_stag) ||
6256 (!cur_stag && req_stag && !cur_ctag)) {
6257 features |= NETIF_VLAN_FILTERING_FEATURES;
6258 netdev_warn(netdev, "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been enabled for both types.\n");
6259 } else if ((cur_ctag && !req_ctag && cur_stag) ||
6260 (cur_stag && !req_stag && cur_ctag)) {
6261 features &= ~NETIF_VLAN_FILTERING_FEATURES;
6262 netdev_warn(netdev, "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been disabled for both types.\n");
6263 }
Brett Creeley1babaf72021-12-02 08:38:50 -08006264 } else {
Roman Storozhenko9542ef42022-06-07 08:54:57 +02006265 if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
6266 netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
6267
6268 if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
6269 features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Brett Creeley1babaf72021-12-02 08:38:50 -08006270 }
6271 }
6272
6273 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
6274 (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
6275 netdev_warn(netdev, "cannot support CTAG and STAG VLAN stripping and/or insertion simultaneously since CTAG and STAG offloads are mutually exclusive, clearing STAG offload settings\n");
6276 features &= ~(NETIF_F_HW_VLAN_STAG_RX |
6277 NETIF_F_HW_VLAN_STAG_TX);
6278 }
6279
Anatolii Gerasymenkoaffa1022022-07-27 09:24:06 +02006280 if (!(netdev->features & NETIF_F_RXFCS) &&
6281 (features & NETIF_F_RXFCS) &&
6282 (features & NETIF_VLAN_STRIPPING_FEATURES) &&
6283 !ice_vsi_has_non_zero_vlans(np->vsi)) {
6284 netdev_warn(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
6285 features &= ~NETIF_VLAN_STRIPPING_FEATURES;
6286 }
6287
Brett Creeley1babaf72021-12-02 08:38:50 -08006288 return features;
6289}
6290
6291/**
Larysa Zaremba714ed942023-12-05 22:08:39 +01006292 * ice_set_rx_rings_vlan_proto - update rings with new stripped VLAN proto
6293 * @vsi: PF's VSI
6294 * @vlan_ethertype: VLAN ethertype (802.1Q or 802.1ad) in network byte order
6295 *
6296 * Store current stripped VLAN proto in ring packet context,
6297 * so it can be accessed more efficiently by packet processing code.
6298 */
6299static void
6300ice_set_rx_rings_vlan_proto(struct ice_vsi *vsi, __be16 vlan_ethertype)
6301{
6302 u16 i;
6303
6304 ice_for_each_alloc_rxq(vsi, i)
6305 vsi->rx_rings[i]->pkt_ctx.vlan_proto = vlan_ethertype;
6306}
6307
6308/**
Brett Creeley1babaf72021-12-02 08:38:50 -08006309 * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
6310 * @vsi: PF's VSI
6311 * @features: features used to determine VLAN offload settings
6312 *
6313 * First, determine the vlan_ethertype based on the VLAN offload bits in
6314 * features. Then determine if stripping and insertion should be enabled or
6315 * disabled. Finally enable or disable VLAN stripping and insertion.
6316 */
6317static int
6318ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
6319{
6320 bool enable_stripping = true, enable_insertion = true;
6321 struct ice_vsi_vlan_ops *vlan_ops;
6322 int strip_err = 0, insert_err = 0;
6323 u16 vlan_ethertype = 0;
6324
6325 vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6326
6327 if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
6328 vlan_ethertype = ETH_P_8021AD;
6329 else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
6330 vlan_ethertype = ETH_P_8021Q;
6331
6332 if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
6333 enable_stripping = false;
6334 if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
6335 enable_insertion = false;
6336
6337 if (enable_stripping)
6338 strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
6339 else
6340 strip_err = vlan_ops->dis_stripping(vsi);
6341
6342 if (enable_insertion)
6343 insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
6344 else
6345 insert_err = vlan_ops->dis_insertion(vsi);
6346
6347 if (strip_err || insert_err)
6348 return -EIO;
6349
Larysa Zaremba714ed942023-12-05 22:08:39 +01006350 ice_set_rx_rings_vlan_proto(vsi, enable_stripping ?
6351 htons(vlan_ethertype) : 0);
6352
Brett Creeley1babaf72021-12-02 08:38:50 -08006353 return 0;
6354}
6355
6356/**
6357 * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
6358 * @vsi: PF's VSI
6359 * @features: features used to determine VLAN filtering settings
6360 *
6361 * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
6362 * features.
6363 */
6364static int
6365ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
6366{
6367 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6368 int err = 0;
6369
6370 /* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
6371 * if either bit is set
6372 */
6373 if (features &
6374 (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER))
6375 err = vlan_ops->ena_rx_filtering(vsi);
6376 else
6377 err = vlan_ops->dis_rx_filtering(vsi);
6378
6379 return err;
6380}
6381
6382/**
6383 * ice_set_vlan_features - set VLAN settings based on suggested feature set
6384 * @netdev: ptr to the netdev being adjusted
6385 * @features: the feature set that the stack is suggesting
6386 *
6387 * Only update VLAN settings if the requested_vlan_features are different than
6388 * the current_vlan_features.
6389 */
6390static int
6391ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
6392{
6393 netdev_features_t current_vlan_features, requested_vlan_features;
6394 struct ice_netdev_priv *np = netdev_priv(netdev);
6395 struct ice_vsi *vsi = np->vsi;
6396 int err;
6397
6398 current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
6399 requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
6400 if (current_vlan_features ^ requested_vlan_features) {
Anatolii Gerasymenkoaffa1022022-07-27 09:24:06 +02006401 if ((features & NETIF_F_RXFCS) &&
6402 (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6403 dev_err(ice_pf_to_dev(vsi->back),
6404 "To enable VLAN stripping, you must first enable FCS/CRC stripping\n");
6405 return -EIO;
6406 }
6407
Brett Creeley1babaf72021-12-02 08:38:50 -08006408 err = ice_set_vlan_offload_features(vsi, features);
6409 if (err)
6410 return err;
6411 }
6412
6413 current_vlan_features = netdev->features &
6414 NETIF_VLAN_FILTERING_FEATURES;
6415 requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
6416 if (current_vlan_features ^ requested_vlan_features) {
6417 err = ice_set_vlan_filtering_features(vsi, features);
6418 if (err)
6419 return err;
6420 }
6421
6422 return 0;
6423}
6424
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07006425/**
Maciej Fijalkowski44ece4e2022-07-07 12:16:51 +02006426 * ice_set_loopback - turn on/off loopback mode on underlying PF
6427 * @vsi: ptr to VSI
6428 * @ena: flag to indicate the on/off setting
6429 */
6430static int ice_set_loopback(struct ice_vsi *vsi, bool ena)
6431{
6432 bool if_running = netif_running(vsi->netdev);
6433 int ret;
6434
6435 if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
6436 ret = ice_down(vsi);
6437 if (ret) {
6438 netdev_err(vsi->netdev, "Preparing device to toggle loopback failed\n");
6439 return ret;
6440 }
6441 }
6442 ret = ice_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
6443 if (ret)
6444 netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
6445 if (if_running)
6446 ret = ice_up(vsi);
6447
6448 return ret;
6449}
6450
6451/**
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07006452 * ice_set_features - set the netdev feature flags
6453 * @netdev: ptr to the netdev being adjusted
6454 * @features: the feature set that the stack is suggesting
6455 */
Bruce Allanc8b7abd2019-02-26 16:35:11 -08006456static int
6457ice_set_features(struct net_device *netdev, netdev_features_t features)
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07006458{
Maciej Fijalkowskic67672f2022-07-07 12:16:50 +02006459 netdev_features_t changed = netdev->features ^ features;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07006460 struct ice_netdev_priv *np = netdev_priv(netdev);
6461 struct ice_vsi *vsi = np->vsi;
Henry Tieman5f8cc352019-11-06 02:05:30 -08006462 struct ice_pf *pf = vsi->back;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07006463 int ret = 0;
6464
Tony Nguyen462acf6a2019-09-09 06:47:46 -07006465 /* Don't set any netdev advanced features with device in Safe Mode */
Maciej Fijalkowskic67672f2022-07-07 12:16:50 +02006466 if (ice_is_safe_mode(pf)) {
6467 dev_err(ice_pf_to_dev(pf),
6468 "Device is in Safe Mode - not enabling advanced netdev features\n");
Tony Nguyen462acf6a2019-09-09 06:47:46 -07006469 return ret;
6470 }
6471
Henry Tieman5f8cc352019-11-06 02:05:30 -08006472 /* Do not change setting during reset */
6473 if (ice_is_reset_in_progress(pf->state)) {
Maciej Fijalkowskic67672f2022-07-07 12:16:50 +02006474 dev_err(ice_pf_to_dev(pf),
6475 "Device is resetting, changing advanced netdev features temporarily unavailable.\n");
Henry Tieman5f8cc352019-11-06 02:05:30 -08006476 return -EBUSY;
6477 }
6478
Tony Nguyen8f529ff2019-04-16 10:21:23 -07006479 /* Multiple features can be changed in one call so keep features in
6480 * separate if/else statements to guarantee each feature is checked
6481 */
Maciej Fijalkowskic67672f2022-07-07 12:16:50 +02006482 if (changed & NETIF_F_RXHASH)
6483 ice_vsi_manage_rss_lut(vsi, !!(features & NETIF_F_RXHASH));
Md Fahad Iqbal Polash492af0a2018-09-19 17:23:17 -07006484
Brett Creeley1babaf72021-12-02 08:38:50 -08006485 ret = ice_set_vlan_features(netdev, features);
6486 if (ret)
6487 return ret;
Tony Nguyen31719482019-04-16 10:30:39 -07006488
Jesse Brandeburgdddd4062022-07-27 09:24:05 +02006489 /* Turn on receive of FCS aka CRC, and after setting this
6490 * flag the packet data will have the 4 byte CRC appended
6491 */
6492 if (changed & NETIF_F_RXFCS) {
Anatolii Gerasymenkoaffa1022022-07-27 09:24:06 +02006493 if ((features & NETIF_F_RXFCS) &&
6494 (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6495 dev_err(ice_pf_to_dev(vsi->back),
6496 "To disable FCS/CRC stripping, you must first disable VLAN stripping\n");
6497 return -EIO;
6498 }
6499
Jesse Brandeburgdddd4062022-07-27 09:24:05 +02006500 ice_vsi_cfg_crc_strip(vsi, !!(features & NETIF_F_RXFCS));
6501 ret = ice_down_up(vsi);
6502 if (ret)
6503 return ret;
6504 }
6505
Maciej Fijalkowskic67672f2022-07-07 12:16:50 +02006506 if (changed & NETIF_F_NTUPLE) {
6507 bool ena = !!(features & NETIF_F_NTUPLE);
6508
6509 ice_vsi_manage_fdir(vsi, ena);
6510 ena ? ice_init_arfs(vsi) : ice_clear_arfs(vsi);
Brett Creeley28bf2672020-05-11 18:01:46 -07006511 }
Henry Tieman148beb62020-05-11 18:01:40 -07006512
Kiran Patilfbc7b272021-10-15 16:35:16 -07006513 /* don't turn off hw_tc_offload when ADQ is already enabled */
6514 if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
6515 dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
6516 return -EACCES;
6517 }
Kiran Patil9fea7492021-10-15 16:35:17 -07006518
Maciej Fijalkowskic67672f2022-07-07 12:16:50 +02006519 if (changed & NETIF_F_HW_TC) {
6520 bool ena = !!(features & NETIF_F_HW_TC);
Kiran Patil9fea7492021-10-15 16:35:17 -07006521
Maciej Fijalkowskic67672f2022-07-07 12:16:50 +02006522 ena ? set_bit(ICE_FLAG_CLS_FLOWER, pf->flags) :
6523 clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
6524 }
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07006525
Maciej Fijalkowski44ece4e2022-07-07 12:16:51 +02006526 if (changed & NETIF_F_LOOPBACK)
6527 ret = ice_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
6528
6529 return ret;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07006530}
6531
6532/**
Brett Creeleyc31af682021-12-02 08:38:46 -08006533 * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07006534 * @vsi: VSI to setup VLAN properties for
6535 */
6536static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
6537{
Brett Creeley1babaf72021-12-02 08:38:50 -08006538 int err;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07006539
Brett Creeley1babaf72021-12-02 08:38:50 -08006540 err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
6541 if (err)
6542 return err;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07006543
Brett Creeley1babaf72021-12-02 08:38:50 -08006544 err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
6545 if (err)
6546 return err;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07006547
Brett Creeleyc31af682021-12-02 08:38:46 -08006548 return ice_vsi_add_vlan_zero(vsi);
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07006549}
6550
6551/**
Michal Swiatkowski0db66d22022-12-21 12:38:15 +01006552 * ice_vsi_cfg_lan - Setup the VSI lan related config
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07006553 * @vsi: the VSI being configured
6554 *
6555 * Return 0 on success and negative value on error
6556 */
Michal Swiatkowski0db66d22022-12-21 12:38:15 +01006557int ice_vsi_cfg_lan(struct ice_vsi *vsi)
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07006558{
6559 int err;
6560
Jesse Brandeburg6a8d0132022-12-13 16:01:31 -08006561 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
Anirudh Venkataramananc7f2c422018-08-09 06:29:00 -07006562 ice_set_rx_mode(vsi->netdev);
Anirudh Venkataramanan9ecd25c2018-10-26 10:40:54 -07006563
Jesse Brandeburg6a8d0132022-12-13 16:01:31 -08006564 err = ice_vsi_vlan_setup(vsi);
6565 if (err)
6566 return err;
Anirudh Venkataramananc7f2c422018-08-09 06:29:00 -07006567 }
Anirudh Venkataramanana629cf02019-02-28 15:24:27 -08006568 ice_vsi_cfg_dcb_rings(vsi);
Anirudh Venkataramanan03f7a982018-12-19 10:03:27 -08006569
6570 err = ice_vsi_cfg_lan_txqs(vsi);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08006571 if (!err && ice_is_xdp_ena_vsi(vsi))
6572 err = ice_vsi_cfg_xdp_txqs(vsi);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07006573 if (!err)
6574 err = ice_vsi_cfg_rxqs(vsi);
6575
6576 return err;
6577}
6578
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006579/* THEORY OF MODERATION:
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006580 * The ice driver hardware works differently than the hardware that DIMLIB was
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006581 * originally made for. ice hardware doesn't have packet count limits that
6582 * can trigger an interrupt, but it *does* have interrupt rate limit support,
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006583 * which is hard-coded to a limit of 250,000 ints/second.
6584 * If not using dynamic moderation, the INTRL value can be modified
6585 * by ethtool rx-usecs-high.
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006586 */
6587struct ice_dim {
6588 /* the throttle rate for interrupts, basically worst case delay before
6589 * an initial interrupt fires, value is stored in microseconds.
6590 */
6591 u16 itr;
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006592};
6593
6594/* Make a different profile for Rx that doesn't allow quite so aggressive
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006595 * moderation at the high end (it maxes out at 126us or about 8k interrupts a
6596 * second.
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006597 */
6598static const struct ice_dim rx_profile[] = {
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006599 {2}, /* 500,000 ints/s, capped at 250K by INTRL */
6600 {8}, /* 125,000 ints/s */
6601 {16}, /* 62,500 ints/s */
6602 {62}, /* 16,129 ints/s */
6603 {126} /* 7,936 ints/s */
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006604};
6605
6606/* The transmit profile, which has the same sorts of values
6607 * as the previous struct
6608 */
6609static const struct ice_dim tx_profile[] = {
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006610 {2}, /* 500,000 ints/s, capped at 250K by INTRL */
6611 {8}, /* 125,000 ints/s */
6612 {40}, /* 16,125 ints/s */
6613 {128}, /* 7,812 ints/s */
6614 {256} /* 3,906 ints/s */
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006615};
6616
6617static void ice_tx_dim_work(struct work_struct *work)
6618{
6619 struct ice_ring_container *rc;
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006620 struct dim *dim;
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006621 u16 itr;
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006622
6623 dim = container_of(work, struct dim, work);
Wu Yunchuanc59cc262023-07-17 11:11:54 +08006624 rc = dim->priv;
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006625
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006626 WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006627
6628 /* look up the values in our local table */
6629 itr = tx_profile[dim->profile_ix].itr;
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006630
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006631 ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006632 ice_write_itr(rc, itr);
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006633
6634 dim->state = DIM_START_MEASURE;
6635}
6636
6637static void ice_rx_dim_work(struct work_struct *work)
6638{
6639 struct ice_ring_container *rc;
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006640 struct dim *dim;
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006641 u16 itr;
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006642
6643 dim = container_of(work, struct dim, work);
Wu Yunchuanc59cc262023-07-17 11:11:54 +08006644 rc = dim->priv;
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006645
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006646 WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006647
6648 /* look up the values in our local table */
6649 itr = rx_profile[dim->profile_ix].itr;
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006650
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006651 ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006652 ice_write_itr(rc, itr);
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006653
6654 dim->state = DIM_START_MEASURE;
6655}
6656
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006657#define ICE_DIM_DEFAULT_PROFILE_IX 1
6658
6659/**
6660 * ice_init_moderation - set up interrupt moderation
6661 * @q_vector: the vector containing rings to be configured
6662 *
6663 * Set up interrupt moderation registers, with the intent to do the right thing
6664 * when called from reset or from probe, and whether or not dynamic moderation
6665 * is enabled or not. Take special care to write all the registers in both
6666 * dynamic moderation mode or not in order to make sure hardware is in a known
6667 * state.
6668 */
6669static void ice_init_moderation(struct ice_q_vector *q_vector)
6670{
6671 struct ice_ring_container *rc;
6672 bool tx_dynamic, rx_dynamic;
6673
6674 rc = &q_vector->tx;
6675 INIT_WORK(&rc->dim.work, ice_tx_dim_work);
6676 rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6677 rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6678 rc->dim.priv = rc;
6679 tx_dynamic = ITR_IS_DYNAMIC(rc);
6680
6681 /* set the initial TX ITR to match the above */
6682 ice_write_itr(rc, tx_dynamic ?
6683 tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
6684
6685 rc = &q_vector->rx;
6686 INIT_WORK(&rc->dim.work, ice_rx_dim_work);
6687 rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6688 rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6689 rc->dim.priv = rc;
6690 rx_dynamic = ITR_IS_DYNAMIC(rc);
6691
6692 /* set the initial RX ITR to match the above */
6693 ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
6694 rc->itr_setting);
6695
6696 ice_set_q_vector_intrl(q_vector);
6697}
6698
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07006699/**
Anirudh Venkataramanan2b245cb2018-03-20 07:58:14 -07006700 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
6701 * @vsi: the VSI being configured
6702 */
6703static void ice_napi_enable_all(struct ice_vsi *vsi)
6704{
6705 int q_idx;
6706
6707 if (!vsi->netdev)
6708 return;
6709
Anirudh Venkataramananb4603db2019-04-16 10:21:28 -07006710 ice_for_each_q_vector(vsi, q_idx) {
Young Xiaoeec90372018-11-29 01:54:10 +00006711 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6712
Jesse Brandeburgd8eb7ad2021-09-20 12:30:12 -07006713 ice_init_moderation(q_vector);
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07006714
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02006715 if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
Young Xiaoeec90372018-11-29 01:54:10 +00006716 napi_enable(&q_vector->napi);
6717 }
Anirudh Venkataramanan2b245cb2018-03-20 07:58:14 -07006718}
6719
6720/**
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07006721 * ice_up_complete - Finish the last steps of bringing up a connection
6722 * @vsi: The VSI being configured
6723 *
6724 * Return 0 on success and negative value on error
6725 */
6726static int ice_up_complete(struct ice_vsi *vsi)
6727{
6728 struct ice_pf *pf = vsi->back;
6729 int err;
6730
Brett Creeleyba880732019-06-26 02:20:25 -07006731 ice_vsi_cfg_msix(vsi);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07006732
6733 /* Enable only Rx rings, Tx rings were enabled by the FW when the
6734 * Tx queue group list was configured and the context bits were
6735 * programmed using ice_vsi_cfg_txqs
6736 */
Brett Creeley13a62332020-01-22 07:21:29 -08006737 err = ice_vsi_start_all_rx_rings(vsi);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07006738 if (err)
6739 return err;
6740
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -08006741 clear_bit(ICE_VSI_DOWN, vsi->state);
Anirudh Venkataramanan2b245cb2018-03-20 07:58:14 -07006742 ice_napi_enable_all(vsi);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07006743 ice_vsi_ena_irq(vsi);
6744
6745 if (vsi->port_info &&
6746 (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
Jesse Brandeburg6a8d0132022-12-13 16:01:31 -08006747 vsi->netdev && vsi->type == ICE_VSI_PF) {
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07006748 ice_print_link_msg(vsi, true);
6749 netif_tx_start_all_queues(vsi->netdev);
6750 netif_carrier_on(vsi->netdev);
Jacob Keller6b1ff5d2022-12-05 11:52:43 -08006751 ice_ptp_link_change(pf, pf->hw.pf_id, true);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07006752 }
6753
Paul Greenwalt31b6298f2022-04-28 14:11:42 -07006754 /* Perform an initial read of the statistics registers now to
6755 * set the baseline so counters are ready when interface is up
6756 */
6757 ice_update_eth_stats(vsi);
Jesse Brandeburg6a8d0132022-12-13 16:01:31 -08006758
6759 if (vsi->type == ICE_VSI_PF)
6760 ice_service_task_schedule(pf);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07006761
Bruce Allan1b5c19c2019-02-26 16:35:07 -08006762 return 0;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07006763}
6764
6765/**
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006766 * ice_up - Bring the connection back up after being down
6767 * @vsi: VSI being configured
6768 */
6769int ice_up(struct ice_vsi *vsi)
6770{
6771 int err;
6772
Michal Swiatkowski0db66d22022-12-21 12:38:15 +01006773 err = ice_vsi_cfg_lan(vsi);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006774 if (!err)
6775 err = ice_up_complete(vsi);
6776
6777 return err;
6778}
6779
6780/**
6781 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02006782 * @syncp: pointer to u64_stats_sync
6783 * @stats: stats that pkts and bytes count will be taken from
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006784 * @pkts: packets stats counter
6785 * @bytes: bytes stats counter
6786 *
6787 * This function fetches stats from the ring considering the atomic operations
6788 * that needs to be performed to read u64 values in 32 bit machine.
6789 */
Marcin Szycikc8ff29b2022-01-27 16:04:26 +01006790void
6791ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
6792 struct ice_q_stats stats, u64 *pkts, u64 *bytes)
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006793{
6794 unsigned int start;
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006795
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006796 do {
Thomas Gleixner068c38a2022-10-26 15:22:14 +02006797 start = u64_stats_fetch_begin(syncp);
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02006798 *pkts = stats.pkts;
6799 *bytes = stats.bytes;
Thomas Gleixner068c38a2022-10-26 15:22:14 +02006800 } while (u64_stats_fetch_retry(syncp, start));
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006801}
6802
6803/**
Marta Plantykow49d358e2020-05-15 17:42:16 -07006804 * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
6805 * @vsi: the VSI to be updated
Jesse Brandeburg1a0f25a2021-11-12 17:06:02 -08006806 * @vsi_stats: the stats struct to be updated
Marta Plantykow49d358e2020-05-15 17:42:16 -07006807 * @rings: rings to work on
6808 * @count: number of rings
6809 */
6810static void
Jesse Brandeburg1a0f25a2021-11-12 17:06:02 -08006811ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
6812 struct rtnl_link_stats64 *vsi_stats,
6813 struct ice_tx_ring **rings, u16 count)
Marta Plantykow49d358e2020-05-15 17:42:16 -07006814{
Marta Plantykow49d358e2020-05-15 17:42:16 -07006815 u16 i;
6816
6817 for (i = 0; i < count; i++) {
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02006818 struct ice_tx_ring *ring;
6819 u64 pkts = 0, bytes = 0;
Marta Plantykow49d358e2020-05-15 17:42:16 -07006820
6821 ring = READ_ONCE(rings[i]);
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -05006822 if (!ring || !ring->ring_stats)
Maciej Fijalkowskif15354692022-03-07 18:47:39 +01006823 continue;
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -05006824 ice_fetch_u64_stats_per_ring(&ring->ring_stats->syncp,
6825 ring->ring_stats->stats, &pkts,
6826 &bytes);
Marta Plantykow49d358e2020-05-15 17:42:16 -07006827 vsi_stats->tx_packets += pkts;
6828 vsi_stats->tx_bytes += bytes;
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -05006829 vsi->tx_restart += ring->ring_stats->tx_stats.restart_q;
6830 vsi->tx_busy += ring->ring_stats->tx_stats.tx_busy;
6831 vsi->tx_linearize += ring->ring_stats->tx_stats.tx_linearize;
Marta Plantykow49d358e2020-05-15 17:42:16 -07006832 }
6833}
6834
6835/**
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006836 * ice_update_vsi_ring_stats - Update VSI stats counters
6837 * @vsi: the VSI to be updated
6838 */
6839static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
6840{
Benjamin Mikailenko2fd5e432022-11-18 16:20:01 -05006841 struct rtnl_link_stats64 *net_stats, *stats_prev;
Jesse Brandeburg1a0f25a2021-11-12 17:06:02 -08006842 struct rtnl_link_stats64 *vsi_stats;
Przemek Kitszel257310e2024-02-27 15:31:06 +01006843 struct ice_pf *pf = vsi->back;
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006844 u64 pkts, bytes;
6845 int i;
6846
Jesse Brandeburg1a0f25a2021-11-12 17:06:02 -08006847 vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
6848 if (!vsi_stats)
6849 return;
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006850
6851 /* reset non-netdev (extended) stats */
6852 vsi->tx_restart = 0;
6853 vsi->tx_busy = 0;
6854 vsi->tx_linearize = 0;
6855 vsi->rx_buf_failed = 0;
6856 vsi->rx_page_failed = 0;
6857
6858 rcu_read_lock();
6859
6860 /* update Tx rings counters */
Jesse Brandeburg1a0f25a2021-11-12 17:06:02 -08006861 ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
6862 vsi->num_txq);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006863
6864 /* update Rx rings counters */
6865 ice_for_each_rxq(vsi, i) {
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02006866 struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -05006867 struct ice_ring_stats *ring_stats;
Paul M Stillwell Jrb6b05012021-05-06 08:40:07 -07006868
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -05006869 ring_stats = ring->ring_stats;
6870 ice_fetch_u64_stats_per_ring(&ring_stats->syncp,
6871 ring_stats->stats, &pkts,
6872 &bytes);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006873 vsi_stats->rx_packets += pkts;
6874 vsi_stats->rx_bytes += bytes;
Benjamin Mikailenko288ecf42022-11-18 16:20:02 -05006875 vsi->rx_buf_failed += ring_stats->rx_stats.alloc_buf_failed;
6876 vsi->rx_page_failed += ring_stats->rx_stats.alloc_page_failed;
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006877 }
6878
Marta Plantykow49d358e2020-05-15 17:42:16 -07006879 /* update XDP Tx rings counters */
6880 if (ice_is_xdp_ena_vsi(vsi))
Jesse Brandeburg1a0f25a2021-11-12 17:06:02 -08006881 ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
Marta Plantykow49d358e2020-05-15 17:42:16 -07006882 vsi->num_xdp_txq);
6883
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006884 rcu_read_unlock();
Jesse Brandeburg1a0f25a2021-11-12 17:06:02 -08006885
Benjamin Mikailenko2fd5e432022-11-18 16:20:01 -05006886 net_stats = &vsi->net_stats;
6887 stats_prev = &vsi->net_stats_prev;
6888
Przemek Kitszel257310e2024-02-27 15:31:06 +01006889 /* Update netdev counters, but keep in mind that values could start at
6890 * random value after PF reset. And as we increase the reported stat by
6891 * diff of Prev-Cur, we need to be sure that Prev is valid. If it's not,
6892 * let's skip this round.
6893 */
6894 if (likely(pf->stat_prev_loaded)) {
6895 net_stats->tx_packets += vsi_stats->tx_packets - stats_prev->tx_packets;
6896 net_stats->tx_bytes += vsi_stats->tx_bytes - stats_prev->tx_bytes;
6897 net_stats->rx_packets += vsi_stats->rx_packets - stats_prev->rx_packets;
6898 net_stats->rx_bytes += vsi_stats->rx_bytes - stats_prev->rx_bytes;
Benjamin Mikailenko2fd5e432022-11-18 16:20:01 -05006899 }
6900
Benjamin Mikailenko2fd5e432022-11-18 16:20:01 -05006901 stats_prev->tx_packets = vsi_stats->tx_packets;
6902 stats_prev->tx_bytes = vsi_stats->tx_bytes;
6903 stats_prev->rx_packets = vsi_stats->rx_packets;
6904 stats_prev->rx_bytes = vsi_stats->rx_bytes;
Jesse Brandeburg1a0f25a2021-11-12 17:06:02 -08006905
6906 kfree(vsi_stats);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006907}
6908
6909/**
6910 * ice_update_vsi_stats - Update VSI stats counters
6911 * @vsi: the VSI to be updated
6912 */
Bruce Allan5a4a8672019-07-25 02:53:50 -07006913void ice_update_vsi_stats(struct ice_vsi *vsi)
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006914{
6915 struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6916 struct ice_eth_stats *cur_es = &vsi->eth_stats;
6917 struct ice_pf *pf = vsi->back;
6918
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -08006919 if (test_bit(ICE_VSI_DOWN, vsi->state) ||
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08006920 test_bit(ICE_CFG_BUSY, pf->state))
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006921 return;
6922
6923 /* get stats as recorded by Tx/Rx rings */
6924 ice_update_vsi_ring_stats(vsi);
6925
6926 /* get VSI stats as recorded by the hardware */
6927 ice_update_eth_stats(vsi);
6928
6929 cur_ns->tx_errors = cur_es->tx_errors;
Anirudh Venkataramanan51fe27e12021-03-25 15:35:16 -07006930 cur_ns->rx_dropped = cur_es->rx_discards;
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006931 cur_ns->tx_dropped = cur_es->tx_discards;
6932 cur_ns->multicast = cur_es->rx_multicast;
6933
6934 /* update some more netdev stats if this is main VSI */
6935 if (vsi->type == ICE_VSI_PF) {
6936 cur_ns->rx_crc_errors = pf->stats.crc_errors;
6937 cur_ns->rx_errors = pf->stats.crc_errors +
Brett Creeley4f1fe432020-05-15 17:36:44 -07006938 pf->stats.illegal_bytes +
Brett Creeley4f1fe432020-05-15 17:36:44 -07006939 pf->stats.rx_undersize +
6940 pf->hw_csum_rx_error +
6941 pf->stats.rx_jabber +
6942 pf->stats.rx_fragments +
6943 pf->stats.rx_oversize;
Brett Creeley56923ab2019-06-26 02:20:22 -07006944 /* record drops from the port level */
6945 cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006946 }
6947}
6948
6949/**
6950 * ice_update_pf_stats - Update PF port stats counters
6951 * @pf: PF whose stats needs to be updated
6952 */
Bruce Allan5a4a8672019-07-25 02:53:50 -07006953void ice_update_pf_stats(struct ice_pf *pf)
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006954{
6955 struct ice_hw_port_stats *prev_ps, *cur_ps;
6956 struct ice_hw *hw = &pf->hw;
Henry Tieman4ab95642020-05-11 18:01:41 -07006957 u16 fd_ctr_base;
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07006958 u8 port;
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006959
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07006960 port = hw->port_info->lport;
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006961 prev_ps = &pf->stats_prev;
6962 cur_ps = &pf->stats;
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006963
Benjamin Mikailenko2fd5e432022-11-18 16:20:01 -05006964 if (ice_is_reset_in_progress(pf->state))
6965 pf->stat_prev_loaded = false;
6966
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07006967 ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07006968 &prev_ps->eth.rx_bytes,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006969 &cur_ps->eth.rx_bytes);
6970
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07006971 ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07006972 &prev_ps->eth.rx_unicast,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006973 &cur_ps->eth.rx_unicast);
6974
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07006975 ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07006976 &prev_ps->eth.rx_multicast,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006977 &cur_ps->eth.rx_multicast);
6978
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07006979 ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07006980 &prev_ps->eth.rx_broadcast,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006981 &cur_ps->eth.rx_broadcast);
6982
Brett Creeley56923ab2019-06-26 02:20:22 -07006983 ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
6984 &prev_ps->eth.rx_discards,
6985 &cur_ps->eth.rx_discards);
6986
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07006987 ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07006988 &prev_ps->eth.tx_bytes,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006989 &cur_ps->eth.tx_bytes);
6990
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07006991 ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07006992 &prev_ps->eth.tx_unicast,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006993 &cur_ps->eth.tx_unicast);
6994
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07006995 ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07006996 &prev_ps->eth.tx_multicast,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07006997 &cur_ps->eth.tx_multicast);
6998
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07006999 ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07007000 &prev_ps->eth.tx_broadcast,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007001 &cur_ps->eth.tx_broadcast);
7002
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007003 ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007004 &prev_ps->tx_dropped_link_down,
7005 &cur_ps->tx_dropped_link_down);
7006
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007007 ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07007008 &prev_ps->rx_size_64, &cur_ps->rx_size_64);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007009
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007010 ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07007011 &prev_ps->rx_size_127, &cur_ps->rx_size_127);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007012
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007013 ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07007014 &prev_ps->rx_size_255, &cur_ps->rx_size_255);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007015
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007016 ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07007017 &prev_ps->rx_size_511, &cur_ps->rx_size_511);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007018
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007019 ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007020 &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
7021
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007022 ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007023 &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
7024
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007025 ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007026 &prev_ps->rx_size_big, &cur_ps->rx_size_big);
7027
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007028 ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07007029 &prev_ps->tx_size_64, &cur_ps->tx_size_64);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007030
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007031 ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07007032 &prev_ps->tx_size_127, &cur_ps->tx_size_127);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007033
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007034 ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07007035 &prev_ps->tx_size_255, &cur_ps->tx_size_255);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007036
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007037 ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
Jacob Keller36517fd2019-06-26 02:20:13 -07007038 &prev_ps->tx_size_511, &cur_ps->tx_size_511);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007039
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007040 ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007041 &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
7042
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007043 ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007044 &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
7045
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007046 ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007047 &prev_ps->tx_size_big, &cur_ps->tx_size_big);
7048
Henry Tieman4ab95642020-05-11 18:01:41 -07007049 fd_ctr_base = hw->fd_ctr_base;
7050
7051 ice_stat_update40(hw,
7052 GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
7053 pf->stat_prev_loaded, &prev_ps->fd_sb_match,
7054 &cur_ps->fd_sb_match);
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007055 ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007056 &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
7057
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007058 ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007059 &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
7060
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007061 ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007062 &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
7063
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007064 ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007065 &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
7066
Anirudh Venkataramanan4b0fdce2019-02-28 15:24:29 -08007067 ice_update_dcb_stats(pf);
7068
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007069 ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007070 &prev_ps->crc_errors, &cur_ps->crc_errors);
7071
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007072 ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007073 &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
7074
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007075 ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007076 &prev_ps->mac_local_faults,
7077 &cur_ps->mac_local_faults);
7078
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007079 ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007080 &prev_ps->mac_remote_faults,
7081 &cur_ps->mac_remote_faults);
7082
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007083 ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007084 &prev_ps->rx_undersize, &cur_ps->rx_undersize);
7085
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007086 ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007087 &prev_ps->rx_fragments, &cur_ps->rx_fragments);
7088
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007089 ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007090 &prev_ps->rx_oversize, &cur_ps->rx_oversize);
7091
Usha Ketineni9e7a5d12019-07-25 02:53:53 -07007092 ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007093 &prev_ps->rx_jabber, &cur_ps->rx_jabber);
7094
Henry Tieman4ab95642020-05-11 18:01:41 -07007095 cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
7096
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007097 pf->stat_prev_loaded = true;
7098}
7099
7100/**
7101 * ice_get_stats64 - get statistics for network device structure
7102 * @netdev: network interface device structure
7103 * @stats: main device statistics structure
7104 */
7105static
7106void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
7107{
7108 struct ice_netdev_priv *np = netdev_priv(netdev);
7109 struct rtnl_link_stats64 *vsi_stats;
7110 struct ice_vsi *vsi = np->vsi;
7111
7112 vsi_stats = &vsi->net_stats;
7113
Dave Ertman3d57fd102019-08-08 07:39:28 -07007114 if (!vsi->num_txq || !vsi->num_rxq)
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007115 return;
Dave Ertman3d57fd102019-08-08 07:39:28 -07007116
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007117 /* netdev packet/byte stats come from ring counter. These are obtained
7118 * by summing up ring counters (done by ice_update_vsi_ring_stats).
Dave Ertman3d57fd102019-08-08 07:39:28 -07007119 * But, only call the update routine and read the registers if VSI is
7120 * not down.
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007121 */
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -08007122 if (!test_bit(ICE_VSI_DOWN, vsi->state))
Dave Ertman3d57fd102019-08-08 07:39:28 -07007123 ice_update_vsi_ring_stats(vsi);
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007124 stats->tx_packets = vsi_stats->tx_packets;
7125 stats->tx_bytes = vsi_stats->tx_bytes;
7126 stats->rx_packets = vsi_stats->rx_packets;
7127 stats->rx_bytes = vsi_stats->rx_bytes;
7128
7129 /* The rest of the stats can be read from the hardware but instead we
7130 * just return values that the watchdog task has already obtained from
7131 * the hardware.
7132 */
7133 stats->multicast = vsi_stats->multicast;
7134 stats->tx_errors = vsi_stats->tx_errors;
7135 stats->tx_dropped = vsi_stats->tx_dropped;
7136 stats->rx_errors = vsi_stats->rx_errors;
7137 stats->rx_dropped = vsi_stats->rx_dropped;
7138 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
7139 stats->rx_length_errors = vsi_stats->rx_length_errors;
7140}
7141
7142/**
Anirudh Venkataramanan2b245cb2018-03-20 07:58:14 -07007143 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
7144 * @vsi: VSI having NAPI disabled
7145 */
7146static void ice_napi_disable_all(struct ice_vsi *vsi)
7147{
7148 int q_idx;
7149
7150 if (!vsi->netdev)
7151 return;
7152
Brett Creeley0c2561c2019-02-28 15:25:53 -08007153 ice_for_each_q_vector(vsi, q_idx) {
Young Xiaoeec90372018-11-29 01:54:10 +00007154 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
7155
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02007156 if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
Young Xiaoeec90372018-11-29 01:54:10 +00007157 napi_disable(&q_vector->napi);
Jacob Kellercdf1f1f2021-03-31 14:16:57 -07007158
7159 cancel_work_sync(&q_vector->tx.dim.work);
7160 cancel_work_sync(&q_vector->rx.dim.work);
Young Xiaoeec90372018-11-29 01:54:10 +00007161 }
Anirudh Venkataramanan2b245cb2018-03-20 07:58:14 -07007162}
7163
7164/**
Maciej Fijalkowskid5926e02024-02-23 17:06:27 +01007165 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
7166 * @vsi: the VSI being un-configured
7167 */
7168static void ice_vsi_dis_irq(struct ice_vsi *vsi)
7169{
7170 struct ice_pf *pf = vsi->back;
7171 struct ice_hw *hw = &pf->hw;
7172 u32 val;
7173 int i;
7174
7175 /* disable interrupt causation from each Rx queue; Tx queues are
7176 * handled in ice_vsi_stop_tx_ring()
7177 */
7178 if (vsi->rx_rings) {
7179 ice_for_each_rxq(vsi, i) {
7180 if (vsi->rx_rings[i]) {
7181 u16 reg;
7182
7183 reg = vsi->rx_rings[i]->reg_idx;
7184 val = rd32(hw, QINT_RQCTL(reg));
7185 val &= ~QINT_RQCTL_CAUSE_ENA_M;
7186 wr32(hw, QINT_RQCTL(reg), val);
7187 }
7188 }
7189 }
7190
7191 /* disable each interrupt */
7192 ice_for_each_q_vector(vsi, i) {
7193 if (!vsi->q_vectors[i])
7194 continue;
7195 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
7196 }
7197
7198 ice_flush(hw);
7199
7200 /* don't call synchronize_irq() for VF's from the host */
7201 if (vsi->type == ICE_VSI_VF)
7202 return;
7203
7204 ice_for_each_q_vector(vsi, i)
7205 synchronize_irq(vsi->q_vectors[i]->irq.virq);
7206}
7207
7208/**
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007209 * ice_down - Shutdown the connection
7210 * @vsi: The VSI being stopped
Jesse Brandeburg21c6e362021-10-25 17:08:25 -07007211 *
7212 * Caller of this function is expected to set the vsi->state ICE_DOWN bit
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007213 */
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07007214int ice_down(struct ice_vsi *vsi)
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007215{
Mateusz Palczewski8ac71322022-08-26 10:31:23 +02007216 int i, tx_err, rx_err, vlan_err = 0;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007217
Jesse Brandeburg21c6e362021-10-25 17:08:25 -07007218 WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
7219
Michal Swiatkowski33bf1e862024-03-01 12:54:11 +01007220 if (vsi->netdev) {
Brett Creeleyc31af682021-12-02 08:38:46 -08007221 vlan_err = ice_vsi_del_vlan_zero(vsi);
Jacob Keller6b1ff5d2022-12-05 11:52:43 -08007222 ice_ptp_link_change(vsi->back, vsi->back->hw.pf_id, false);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007223 netif_carrier_off(vsi->netdev);
7224 netif_tx_disable(vsi->netdev);
7225 }
7226
7227 ice_vsi_dis_irq(vsi);
Anirudh Venkataramanan03f7a982018-12-19 10:03:27 -08007228
7229 tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
Anirudh Venkataramanan72adf242018-09-19 17:23:05 -07007230 if (tx_err)
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08007231 netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
Anirudh Venkataramanan72adf242018-09-19 17:23:05 -07007232 vsi->vsi_num, tx_err);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08007233 if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
7234 tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
7235 if (tx_err)
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08007236 netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08007237 vsi->vsi_num, tx_err);
7238 }
Anirudh Venkataramanan72adf242018-09-19 17:23:05 -07007239
Brett Creeley13a62332020-01-22 07:21:29 -08007240 rx_err = ice_vsi_stop_all_rx_rings(vsi);
Anirudh Venkataramanan72adf242018-09-19 17:23:05 -07007241 if (rx_err)
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08007242 netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
Anirudh Venkataramanan72adf242018-09-19 17:23:05 -07007243 vsi->vsi_num, rx_err);
7244
Anirudh Venkataramanan2b245cb2018-03-20 07:58:14 -07007245 ice_napi_disable_all(vsi);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007246
7247 ice_for_each_txq(vsi, i)
7248 ice_clean_tx_ring(vsi->tx_rings[i]);
7249
Kamil Maziarz78c50d62023-06-06 12:33:58 +02007250 if (ice_is_xdp_ena_vsi(vsi))
7251 ice_for_each_xdp_txq(vsi, i)
7252 ice_clean_tx_ring(vsi->xdp_rings[i]);
7253
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007254 ice_for_each_rxq(vsi, i)
7255 ice_clean_rx_ring(vsi->rx_rings[i]);
7256
Mateusz Palczewski8ac71322022-08-26 10:31:23 +02007257 if (tx_err || rx_err || vlan_err) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08007258 netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007259 vsi->vsi_num, vsi->vsw->sw_id);
Anirudh Venkataramanan72adf242018-09-19 17:23:05 -07007260 return -EIO;
7261 }
7262
7263 return 0;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007264}
7265
7266/**
Jesse Brandeburgdddd4062022-07-27 09:24:05 +02007267 * ice_down_up - shutdown the VSI connection and bring it up
7268 * @vsi: the VSI to be reconnected
7269 */
7270int ice_down_up(struct ice_vsi *vsi)
7271{
7272 int ret;
7273
7274 /* if DOWN already set, nothing to do */
7275 if (test_and_set_bit(ICE_VSI_DOWN, vsi->state))
7276 return 0;
7277
7278 ret = ice_down(vsi);
7279 if (ret)
7280 return ret;
7281
7282 ret = ice_up(vsi);
7283 if (ret) {
7284 netdev_err(vsi->netdev, "reallocating resources failed during netdev features change, may need to reload driver\n");
7285 return ret;
7286 }
7287
7288 return 0;
7289}
7290
7291/**
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007292 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
7293 * @vsi: VSI having resources allocated
7294 *
7295 * Return 0 on success, negative on failure
7296 */
Anirudh Venkataramanan0e674ae2019-04-16 10:30:43 -07007297int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007298{
Jesse Brandeburgdab05882018-08-09 06:29:01 -07007299 int i, err = 0;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007300
7301 if (!vsi->num_txq) {
Anirudh Venkataramanan9a946842020-02-06 01:20:09 -08007302 dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007303 vsi->vsi_num);
7304 return -EINVAL;
7305 }
7306
7307 ice_for_each_txq(vsi, i) {
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02007308 struct ice_tx_ring *ring = vsi->tx_rings[i];
Michal Swiatkowskieb0ee8a2019-10-09 07:09:47 -07007309
7310 if (!ring)
7311 return -EINVAL;
7312
Grzegorz Nitka1c54c832021-08-19 17:08:56 -07007313 if (vsi->netdev)
7314 ring->netdev = vsi->netdev;
Michal Swiatkowskieb0ee8a2019-10-09 07:09:47 -07007315 err = ice_setup_tx_ring(ring);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007316 if (err)
7317 break;
7318 }
7319
7320 return err;
7321}
7322
7323/**
7324 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
7325 * @vsi: VSI having resources allocated
7326 *
7327 * Return 0 on success, negative on failure
7328 */
Anirudh Venkataramanan0e674ae2019-04-16 10:30:43 -07007329int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007330{
Jesse Brandeburgdab05882018-08-09 06:29:01 -07007331 int i, err = 0;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007332
7333 if (!vsi->num_rxq) {
Anirudh Venkataramanan9a946842020-02-06 01:20:09 -08007334 dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007335 vsi->vsi_num);
7336 return -EINVAL;
7337 }
7338
7339 ice_for_each_rxq(vsi, i) {
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02007340 struct ice_rx_ring *ring = vsi->rx_rings[i];
Michal Swiatkowskieb0ee8a2019-10-09 07:09:47 -07007341
7342 if (!ring)
7343 return -EINVAL;
7344
Grzegorz Nitka1c54c832021-08-19 17:08:56 -07007345 if (vsi->netdev)
7346 ring->netdev = vsi->netdev;
Michal Swiatkowskieb0ee8a2019-10-09 07:09:47 -07007347 err = ice_setup_rx_ring(ring);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007348 if (err)
7349 break;
7350 }
7351
7352 return err;
7353}
7354
7355/**
Henry Tieman148beb62020-05-11 18:01:40 -07007356 * ice_vsi_open_ctrl - open control VSI for use
7357 * @vsi: the VSI to open
7358 *
7359 * Initialization of the Control VSI
7360 *
7361 * Returns 0 on success, negative value on error
7362 */
7363int ice_vsi_open_ctrl(struct ice_vsi *vsi)
7364{
7365 char int_name[ICE_INT_NAME_STR_LEN];
7366 struct ice_pf *pf = vsi->back;
7367 struct device *dev;
7368 int err;
7369
7370 dev = ice_pf_to_dev(pf);
7371 /* allocate descriptors */
7372 err = ice_vsi_setup_tx_rings(vsi);
7373 if (err)
7374 goto err_setup_tx;
7375
7376 err = ice_vsi_setup_rx_rings(vsi);
7377 if (err)
7378 goto err_setup_rx;
7379
Michal Swiatkowski0db66d22022-12-21 12:38:15 +01007380 err = ice_vsi_cfg_lan(vsi);
Henry Tieman148beb62020-05-11 18:01:40 -07007381 if (err)
7382 goto err_setup_rx;
7383
7384 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
7385 dev_driver_string(dev), dev_name(dev));
7386 err = ice_vsi_req_irq_msix(vsi, int_name);
7387 if (err)
7388 goto err_setup_rx;
7389
7390 ice_vsi_cfg_msix(vsi);
7391
7392 err = ice_vsi_start_all_rx_rings(vsi);
7393 if (err)
7394 goto err_up_complete;
7395
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -08007396 clear_bit(ICE_VSI_DOWN, vsi->state);
Henry Tieman148beb62020-05-11 18:01:40 -07007397 ice_vsi_ena_irq(vsi);
7398
7399 return 0;
7400
7401err_up_complete:
7402 ice_down(vsi);
7403err_setup_rx:
7404 ice_vsi_free_rx_rings(vsi);
7405err_setup_tx:
7406 ice_vsi_free_tx_rings(vsi);
7407
7408 return err;
7409}
7410
7411/**
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007412 * ice_vsi_open - Called when a network interface is made active
7413 * @vsi: the VSI to open
7414 *
7415 * Initialization of the VSI
7416 *
7417 * Returns 0 on success, negative value on error
7418 */
Grzegorz Nitka1a1c40d2021-08-19 17:08:54 -07007419int ice_vsi_open(struct ice_vsi *vsi)
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007420{
7421 char int_name[ICE_INT_NAME_STR_LEN];
7422 struct ice_pf *pf = vsi->back;
7423 int err;
7424
7425 /* allocate descriptors */
7426 err = ice_vsi_setup_tx_rings(vsi);
7427 if (err)
7428 goto err_setup_tx;
7429
7430 err = ice_vsi_setup_rx_rings(vsi);
7431 if (err)
7432 goto err_setup_rx;
7433
Michal Swiatkowski0db66d22022-12-21 12:38:15 +01007434 err = ice_vsi_cfg_lan(vsi);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007435 if (err)
7436 goto err_setup_rx;
7437
7438 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
Brett Creeley4015d112019-11-08 06:23:26 -08007439 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
Brett Creeleyba880732019-06-26 02:20:25 -07007440 err = ice_vsi_req_irq_msix(vsi, int_name);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007441 if (err)
7442 goto err_setup_rx;
7443
Michal Swiatkowski122045c2022-08-08 11:58:54 +02007444 ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
7445
Grzegorz Nitka1a1c40d2021-08-19 17:08:54 -07007446 if (vsi->type == ICE_VSI_PF) {
7447 /* Notify the stack of the actual queue counts. */
7448 err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
7449 if (err)
7450 goto err_set_qs;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007451
Grzegorz Nitka1a1c40d2021-08-19 17:08:54 -07007452 err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
7453 if (err)
7454 goto err_set_qs;
7455 }
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007456
7457 err = ice_up_complete(vsi);
7458 if (err)
7459 goto err_up_complete;
7460
7461 return 0;
7462
7463err_up_complete:
7464 ice_down(vsi);
7465err_set_qs:
7466 ice_vsi_free_irq(vsi);
7467err_setup_rx:
7468 ice_vsi_free_rx_rings(vsi);
7469err_setup_tx:
7470 ice_vsi_free_tx_rings(vsi);
7471
7472 return err;
7473}
7474
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07007475/**
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007476 * ice_vsi_release_all - Delete all VSIs
7477 * @pf: PF from which all VSIs are being removed
7478 */
7479static void ice_vsi_release_all(struct ice_pf *pf)
7480{
7481 int err, i;
7482
7483 if (!pf->vsi)
7484 return;
7485
Brett Creeley80ed404a2019-02-08 12:50:54 -08007486 ice_for_each_vsi(pf, i) {
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007487 if (!pf->vsi[i])
7488 continue;
7489
Kiran Patilfbc7b272021-10-15 16:35:16 -07007490 if (pf->vsi[i]->type == ICE_VSI_CHNL)
7491 continue;
7492
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007493 err = ice_vsi_release(pf->vsi[i]);
7494 if (err)
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08007495 dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007496 i, err, pf->vsi[i]->vsi_num);
7497 }
7498}
7499
7500/**
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007501 * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
7502 * @pf: pointer to the PF instance
7503 * @type: VSI type to rebuild
7504 *
7505 * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007506 */
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007507static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007508{
Brett Creeley4015d112019-11-08 06:23:26 -08007509 struct device *dev = ice_pf_to_dev(pf);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007510 int i, err;
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007511
Brett Creeley80ed404a2019-02-08 12:50:54 -08007512 ice_for_each_vsi(pf, i) {
Krzysztof Kazimierczak4425e052019-07-25 02:53:57 -07007513 struct ice_vsi *vsi = pf->vsi[i];
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007514
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007515 if (!vsi || vsi->type != type)
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007516 continue;
7517
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007518 /* rebuild the VSI */
Michal Swiatkowski6624e782022-12-21 12:38:16 +01007519 err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007520 if (err) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08007521 dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
Anirudh Venkataramanan964674f2019-11-06 02:05:39 -08007522 err, vsi->idx, ice_vsi_type_str(type));
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007523 return err;
7524 }
7525
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007526 /* replay filters for the VSI */
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07007527 err = ice_replay_vsi(&pf->hw, vsi->idx);
7528 if (err) {
Tony Nguyen5f87ec42021-10-07 15:56:02 -07007529 dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07007530 err, vsi->idx, ice_vsi_type_str(type));
Tony Nguyenc1484692021-10-07 16:01:58 -07007531 return err;
Anirudh Venkataramanan334cb062018-09-19 17:23:14 -07007532 }
7533
7534 /* Re-map HW VSI number, using VSI handle that has been
7535 * previously validated in ice_replay_vsi() call above
7536 */
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007537 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
Anirudh Venkataramanan334cb062018-09-19 17:23:14 -07007538
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007539 /* enable the VSI */
7540 err = ice_ena_vsi(vsi, false);
7541 if (err) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08007542 dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
Anirudh Venkataramanan964674f2019-11-06 02:05:39 -08007543 err, vsi->idx, ice_vsi_type_str(type));
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007544 return err;
7545 }
7546
Brett Creeley4015d112019-11-08 06:23:26 -08007547 dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
7548 ice_vsi_type_str(type));
Anirudh Venkataramanan334cb062018-09-19 17:23:14 -07007549 }
7550
Anirudh Venkataramanan334cb062018-09-19 17:23:14 -07007551 return 0;
7552}
7553
7554/**
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007555 * ice_update_pf_netdev_link - Update PF netdev link status
7556 * @pf: pointer to the PF instance
7557 */
7558static void ice_update_pf_netdev_link(struct ice_pf *pf)
7559{
7560 bool link_up;
7561 int i;
7562
7563 ice_for_each_vsi(pf, i) {
7564 struct ice_vsi *vsi = pf->vsi[i];
7565
7566 if (!vsi || vsi->type != ICE_VSI_PF)
7567 return;
7568
7569 ice_get_link_status(pf->vsi[i]->port_info, &link_up);
7570 if (link_up) {
7571 netif_carrier_on(pf->vsi[i]->netdev);
7572 netif_tx_wake_all_queues(pf->vsi[i]->netdev);
7573 } else {
7574 netif_carrier_off(pf->vsi[i]->netdev);
7575 netif_tx_stop_all_queues(pf->vsi[i]->netdev);
7576 }
7577 }
7578}
7579
7580/**
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007581 * ice_rebuild - rebuild after reset
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -07007582 * @pf: PF to rebuild
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007583 * @reset_type: type of reset
Brett Creeley12bb0182020-05-15 17:51:15 -07007584 *
7585 * Do not rebuild VF VSI in this flow because that is already handled via
7586 * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
7587 * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
7588 * to reset/rebuild all the VF VSI twice.
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007589 */
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007590static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007591{
Brett Creeley4015d112019-11-08 06:23:26 -08007592 struct device *dev = ice_pf_to_dev(pf);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007593 struct ice_hw *hw = &pf->hw;
Brett Creeleya1ffafb2021-12-02 08:38:49 -08007594 bool dvm;
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007595 int err;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007596
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08007597 if (test_bit(ICE_DOWN, pf->state))
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007598 goto clear_recovery;
7599
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007600 dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007601
Petr Orosb5377522022-04-13 17:37:45 +02007602#define ICE_EMP_RESET_SLEEP_MS 5000
Jacob Keller399e27d2021-10-27 16:22:55 -07007603 if (reset_type == ICE_RESET_EMPR) {
7604 /* If an EMP reset has occurred, any previously pending flash
7605 * update will have completed. We no longer know whether or
7606 * not the NVM update EMP reset is restricted.
7607 */
7608 pf->fw_emp_reset_disabled = false;
Petr Orosb5377522022-04-13 17:37:45 +02007609
7610 msleep(ICE_EMP_RESET_SLEEP_MS);
Jacob Keller399e27d2021-10-27 16:22:55 -07007611 }
7612
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07007613 err = ice_init_all_ctrlq(hw);
7614 if (err) {
7615 dev_err(dev, "control queues init failed %d\n", err);
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007616 goto err_init_ctrlq;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007617 }
7618
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007619 /* if DDP was previously loaded successfully */
7620 if (!ice_is_safe_mode(pf)) {
7621 /* reload the SW DB of filter tables */
7622 if (reset_type == ICE_RESET_PFR)
7623 ice_fill_blk_tbls(hw);
7624 else
7625 /* Reload DDP Package after CORER/GLOBR reset */
7626 ice_load_pkg(NULL, pf);
7627 }
7628
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07007629 err = ice_clear_pf_cfg(hw);
7630 if (err) {
7631 dev_err(dev, "clear PF configuration failed %d\n", err);
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007632 goto err_init_ctrlq;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007633 }
7634
7635 ice_clear_pxe_mode(hw);
7636
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07007637 err = ice_init_nvm(hw);
7638 if (err) {
7639 dev_err(dev, "ice_init_nvm failed %d\n", err);
Jacob Keller97a4ec02021-05-06 08:40:00 -07007640 goto err_init_ctrlq;
7641 }
7642
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07007643 err = ice_get_caps(hw);
7644 if (err) {
7645 dev_err(dev, "ice_get_caps failed %d\n", err);
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007646 goto err_init_ctrlq;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007647 }
7648
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07007649 err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
7650 if (err) {
7651 dev_err(dev, "set_mac_cfg failed %d\n", err);
Anirudh Venkataramanan42449102020-05-15 17:36:30 -07007652 goto err_init_ctrlq;
7653 }
7654
Brett Creeleya1ffafb2021-12-02 08:38:49 -08007655 dvm = ice_is_dvm_ena(hw);
7656
7657 err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
7658 if (err)
7659 goto err_init_ctrlq;
7660
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007661 err = ice_sched_init_port(hw->port_info);
7662 if (err)
7663 goto err_sched_init_port;
7664
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007665 /* start misc vector */
Brett Creeleyba880732019-06-26 02:20:25 -07007666 err = ice_req_irq_msix_misc(pf);
7667 if (err) {
7668 dev_err(dev, "misc vector setup failed: %d\n", err);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007669 goto err_sched_init_port;
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007670 }
7671
Henry Tieman83af0032020-05-11 18:01:45 -07007672 if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7673 wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
7674 if (!rd32(hw, PFQF_FD_SIZE)) {
7675 u16 unused, guar, b_effort;
7676
7677 guar = hw->func_caps.fd_fltr_guar;
7678 b_effort = hw->func_caps.fd_fltr_best_effort;
7679
7680 /* force guaranteed filter pool for PF */
7681 ice_alloc_fd_guar_item(hw, &unused, guar);
7682 /* force shared filter pool for PF */
7683 ice_alloc_fd_shrd_item(hw, &unused, b_effort);
7684 }
7685 }
7686
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007687 if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
7688 ice_dcb_rebuild(pf);
7689
Jacob Keller06c16d82021-06-09 09:39:50 -07007690 /* If the PF previously had enabled PTP, PTP init needs to happen before
7691 * the VSI rebuild. If not, this causes the PTP link status events to
7692 * fail.
7693 */
7694 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
Jacob Keller803bef82024-01-25 13:57:54 -08007695 ice_ptp_rebuild(pf, reset_type);
Jacob Keller06c16d82021-06-09 09:39:50 -07007696
Karol Kolacinski43113ff2022-03-01 10:38:03 -08007697 if (ice_is_feature_supported(pf, ICE_F_GNSS))
7698 ice_gnss_init(pf);
7699
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007700 /* rebuild PF VSI */
7701 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007702 if (err) {
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007703 dev_err(dev, "PF VSI rebuild failed: %d\n", err);
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007704 goto err_vsi_rebuild;
7705 }
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007706
Kiran Patilfbc7b272021-10-15 16:35:16 -07007707 if (reset_type == ICE_RESET_PFR) {
7708 err = ice_rebuild_channels(pf);
7709 if (err) {
7710 dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
7711 err);
7712 goto err_vsi_rebuild;
7713 }
7714 }
7715
Henry Tieman83af0032020-05-11 18:01:45 -07007716 /* If Flow Director is active */
7717 if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7718 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
7719 if (err) {
7720 dev_err(dev, "control VSI rebuild failed: %d\n", err);
7721 goto err_vsi_rebuild;
7722 }
7723
7724 /* replay HW Flow Director recipes */
7725 if (hw->fdir_prof)
7726 ice_fdir_replay_flows(hw);
7727
7728 /* replay Flow Director filters */
7729 ice_fdir_replay_fltrs(pf);
Brett Creeley28bf2672020-05-11 18:01:46 -07007730
7731 ice_rebuild_arfs(pf);
Henry Tieman83af0032020-05-11 18:01:45 -07007732 }
7733
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007734 ice_update_pf_netdev_link(pf);
7735
7736 /* tell the firmware we are up */
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07007737 err = ice_send_version(pf);
7738 if (err) {
Tony Nguyen5f87ec42021-10-07 15:56:02 -07007739 dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07007740 err);
Tony Nguyen462acf6a2019-09-09 06:47:46 -07007741 goto err_vsi_rebuild;
7742 }
7743
7744 ice_replay_post(hw);
7745
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007746 /* if we get here, reset flow is successful */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08007747 clear_bit(ICE_RESET_FAILED, pf->state);
Dave Ertmanf9f53012021-05-20 09:37:51 -05007748
7749 ice_plug_aux_dev(pf);
Dave Ertman3579aa82023-06-20 15:18:54 -07007750 if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
7751 ice_lag_rebuild(pf);
Jacob Keller77580172023-11-21 13:12:57 -08007752
7753 /* Restore timestamp mode settings after VSI rebuild */
7754 ice_ptp_restore_timestamp_mode(pf);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007755 return;
7756
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007757err_vsi_rebuild:
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007758err_sched_init_port:
7759 ice_sched_cleanup_all(hw);
7760err_init_ctrlq:
Piotr Gardockifdd288e2024-06-14 12:38:11 +02007761 ice_shutdown_all_ctrlq(hw, false);
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08007762 set_bit(ICE_RESET_FAILED, pf->state);
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007763clear_recovery:
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007764 /* set this bit in PF state to control service task scheduling */
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08007765 set_bit(ICE_NEEDS_RESTART, pf->state);
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07007766 dev_err(dev, "Rebuild failed, unload and reload driver\n");
Anirudh Venkataramanan0b28b702018-03-20 07:58:18 -07007767}
7768
7769/**
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07007770 * ice_change_mtu - NDO callback to change the MTU
7771 * @netdev: network interface device structure
7772 * @new_mtu: new value for maximum frame size
7773 *
7774 * Returns 0 on success, negative on failure
7775 */
7776static int ice_change_mtu(struct net_device *netdev, int new_mtu)
7777{
7778 struct ice_netdev_priv *np = netdev_priv(netdev);
7779 struct ice_vsi *vsi = np->vsi;
7780 struct ice_pf *pf = vsi->back;
Maciej Fijalkowski2fba7dc2023-01-31 21:45:03 +01007781 struct bpf_prog *prog;
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07007782 u8 count = 0;
Dave Ertman348048e2021-05-20 09:37:50 -05007783 int err = 0;
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07007784
Jesse Brandeburg22bef5e2020-05-15 17:36:38 -07007785 if (new_mtu == (int)netdev->mtu) {
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -07007786 netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07007787 return 0;
7788 }
7789
Maciej Fijalkowski2fba7dc2023-01-31 21:45:03 +01007790 prog = vsi->xdp_prog;
7791 if (prog && !prog->aux->xdp_has_frags) {
Maciej Fijalkowski23b44512019-10-24 01:11:25 -07007792 int frame_size = ice_max_xdp_frame_size(vsi);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08007793
7794 if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
7795 netdev_err(netdev, "max MTU for XDP usage is %d\n",
Maciej Fijalkowski23b44512019-10-24 01:11:25 -07007796 frame_size - ICE_ETH_PKT_HDR_PAD);
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08007797 return -EINVAL;
7798 }
Maciej Fijalkowskic61bceb2023-01-31 21:44:54 +01007799 } else if (test_bit(ICE_FLAG_LEGACY_RX, pf->flags)) {
7800 if (new_mtu + ICE_ETH_PKT_HDR_PAD > ICE_MAX_FRAME_LEGACY_RX) {
7801 netdev_err(netdev, "Too big MTU for legacy-rx; Max is %d\n",
7802 ICE_MAX_FRAME_LEGACY_RX - ICE_ETH_PKT_HDR_PAD);
7803 return -EINVAL;
7804 }
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08007805 }
7806
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07007807 /* if a reset is in progress, wait for some time for it to complete */
7808 do {
Dave Ertman5df7e452018-09-19 17:23:11 -07007809 if (ice_is_reset_in_progress(pf->state)) {
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07007810 count++;
7811 usleep_range(1000, 2000);
7812 } else {
7813 break;
7814 }
7815
7816 } while (count < 100);
7817
7818 if (count == 100) {
Anirudh Venkataramanan2f2da362019-04-16 10:35:03 -07007819 netdev_err(netdev, "can't change MTU. Device is busy\n");
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07007820 return -EBUSY;
7821 }
7822
Eric Dumazet1eb2cde2024-05-06 10:28:12 +00007823 WRITE_ONCE(netdev->mtu, (unsigned int)new_mtu);
Maciej Fijalkowskib7a03452023-06-13 13:35:52 +02007824 err = ice_down_up(vsi);
7825 if (err)
7826 return err;
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07007827
Tony Nguyenbda5b7d2019-12-12 03:13:03 -08007828 netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
Dave Ertman97b01292022-02-18 12:39:25 -08007829 set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
Dave Ertman348048e2021-05-20 09:37:50 -05007830
7831 return err;
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07007832}
7833
7834/**
Arnd Bergmanna7605372021-07-27 15:45:13 +02007835 * ice_eth_ioctl - Access the hwtstamp interface
Jacob Keller77a78112021-06-09 09:39:52 -07007836 * @netdev: network interface device structure
7837 * @ifr: interface request data
7838 * @cmd: ioctl command
7839 */
Arnd Bergmanna7605372021-07-27 15:45:13 +02007840static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
Jacob Keller77a78112021-06-09 09:39:52 -07007841{
7842 struct ice_netdev_priv *np = netdev_priv(netdev);
7843 struct ice_pf *pf = np->vsi->back;
7844
7845 switch (cmd) {
7846 case SIOCGHWTSTAMP:
7847 return ice_ptp_get_ts_config(pf, ifr);
7848 case SIOCSHWTSTAMP:
7849 return ice_ptp_set_ts_config(pf, ifr);
7850 default:
7851 return -EOPNOTSUPP;
7852 }
7853}
7854
7855/**
Lihong Yang0fee3572020-05-07 17:41:04 -07007856 * ice_aq_str - convert AQ err code to a string
7857 * @aq_err: the AQ error code to convert
7858 */
7859const char *ice_aq_str(enum ice_aq_err aq_err)
7860{
7861 switch (aq_err) {
7862 case ICE_AQ_RC_OK:
7863 return "OK";
7864 case ICE_AQ_RC_EPERM:
7865 return "ICE_AQ_RC_EPERM";
7866 case ICE_AQ_RC_ENOENT:
7867 return "ICE_AQ_RC_ENOENT";
7868 case ICE_AQ_RC_ENOMEM:
7869 return "ICE_AQ_RC_ENOMEM";
7870 case ICE_AQ_RC_EBUSY:
7871 return "ICE_AQ_RC_EBUSY";
7872 case ICE_AQ_RC_EEXIST:
7873 return "ICE_AQ_RC_EEXIST";
7874 case ICE_AQ_RC_EINVAL:
7875 return "ICE_AQ_RC_EINVAL";
7876 case ICE_AQ_RC_ENOSPC:
7877 return "ICE_AQ_RC_ENOSPC";
7878 case ICE_AQ_RC_ENOSYS:
7879 return "ICE_AQ_RC_ENOSYS";
Chinh T Caob5e19a62020-05-15 17:55:06 -07007880 case ICE_AQ_RC_EMODE:
7881 return "ICE_AQ_RC_EMODE";
Lihong Yang0fee3572020-05-07 17:41:04 -07007882 case ICE_AQ_RC_ENOSEC:
7883 return "ICE_AQ_RC_ENOSEC";
7884 case ICE_AQ_RC_EBADSIG:
7885 return "ICE_AQ_RC_EBADSIG";
7886 case ICE_AQ_RC_ESVN:
7887 return "ICE_AQ_RC_ESVN";
7888 case ICE_AQ_RC_EBADMAN:
7889 return "ICE_AQ_RC_EBADMAN";
7890 case ICE_AQ_RC_EBADBUF:
7891 return "ICE_AQ_RC_EBADBUF";
7892 }
7893
7894 return "ICE_AQ_RC_UNKNOWN";
7895}
7896
7897/**
Brett Creeleyb66a9722021-03-02 10:15:36 -08007898 * ice_set_rss_lut - Set RSS LUT
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007899 * @vsi: Pointer to VSI structure
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007900 * @lut: Lookup table
7901 * @lut_size: Lookup table size
7902 *
7903 * Returns 0 on success, negative on failure
7904 */
Brett Creeleyb66a9722021-03-02 10:15:36 -08007905int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007906{
Brett Creeleyb66a9722021-03-02 10:15:36 -08007907 struct ice_aq_get_set_rss_lut_params params = {};
7908 struct ice_hw *hw = &vsi->back->hw;
Tony Nguyen5e24d592021-10-07 15:56:57 -07007909 int status;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007910
Brett Creeleyb66a9722021-03-02 10:15:36 -08007911 if (!lut)
7912 return -EINVAL;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007913
Brett Creeleyb66a9722021-03-02 10:15:36 -08007914 params.vsi_handle = vsi->idx;
7915 params.lut_size = lut_size;
7916 params.lut_type = vsi->rss_lut_type;
7917 params.lut = lut;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007918
Brett Creeleyb66a9722021-03-02 10:15:36 -08007919 status = ice_aq_set_rss_lut(hw, &params);
Tony Nguyenc1484692021-10-07 16:01:58 -07007920 if (status)
Tony Nguyen5f87ec42021-10-07 15:56:02 -07007921 dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
Tony Nguyen5518ac22021-10-07 15:59:03 -07007922 status, ice_aq_str(hw->adminq.sq_last_status));
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007923
Tony Nguyenc1484692021-10-07 16:01:58 -07007924 return status;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007925}
7926
7927/**
Brett Creeleyb66a9722021-03-02 10:15:36 -08007928 * ice_set_rss_key - Set RSS key
7929 * @vsi: Pointer to the VSI structure
7930 * @seed: RSS hash seed
7931 *
7932 * Returns 0 on success, negative on failure
7933 */
7934int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
7935{
7936 struct ice_hw *hw = &vsi->back->hw;
Tony Nguyen5e24d592021-10-07 15:56:57 -07007937 int status;
Brett Creeleyb66a9722021-03-02 10:15:36 -08007938
7939 if (!seed)
7940 return -EINVAL;
7941
7942 status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
Tony Nguyenc1484692021-10-07 16:01:58 -07007943 if (status)
Tony Nguyen5f87ec42021-10-07 15:56:02 -07007944 dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
Tony Nguyen5518ac22021-10-07 15:59:03 -07007945 status, ice_aq_str(hw->adminq.sq_last_status));
Brett Creeleyb66a9722021-03-02 10:15:36 -08007946
Tony Nguyenc1484692021-10-07 16:01:58 -07007947 return status;
Brett Creeleyb66a9722021-03-02 10:15:36 -08007948}
7949
7950/**
7951 * ice_get_rss_lut - Get RSS LUT
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007952 * @vsi: Pointer to VSI structure
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007953 * @lut: Buffer to store the lookup table entries
7954 * @lut_size: Size of buffer to store the lookup table entries
7955 *
7956 * Returns 0 on success, negative on failure
7957 */
Brett Creeleyb66a9722021-03-02 10:15:36 -08007958int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007959{
Brett Creeleyb66a9722021-03-02 10:15:36 -08007960 struct ice_aq_get_set_rss_lut_params params = {};
7961 struct ice_hw *hw = &vsi->back->hw;
Tony Nguyen5e24d592021-10-07 15:56:57 -07007962 int status;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007963
Brett Creeleyb66a9722021-03-02 10:15:36 -08007964 if (!lut)
7965 return -EINVAL;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007966
Brett Creeleyb66a9722021-03-02 10:15:36 -08007967 params.vsi_handle = vsi->idx;
7968 params.lut_size = lut_size;
7969 params.lut_type = vsi->rss_lut_type;
7970 params.lut = lut;
7971
7972 status = ice_aq_get_rss_lut(hw, &params);
Tony Nguyenc1484692021-10-07 16:01:58 -07007973 if (status)
Tony Nguyen5f87ec42021-10-07 15:56:02 -07007974 dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
Tony Nguyen5518ac22021-10-07 15:59:03 -07007975 status, ice_aq_str(hw->adminq.sq_last_status));
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007976
Tony Nguyenc1484692021-10-07 16:01:58 -07007977 return status;
Brett Creeleyb66a9722021-03-02 10:15:36 -08007978}
Brett Creeleye3c53922021-03-02 10:15:35 -08007979
Brett Creeleyb66a9722021-03-02 10:15:36 -08007980/**
7981 * ice_get_rss_key - Get RSS key
7982 * @vsi: Pointer to VSI structure
7983 * @seed: Buffer to store the key in
7984 *
7985 * Returns 0 on success, negative on failure
7986 */
7987int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
7988{
7989 struct ice_hw *hw = &vsi->back->hw;
Tony Nguyen5e24d592021-10-07 15:56:57 -07007990 int status;
Brett Creeleyb66a9722021-03-02 10:15:36 -08007991
7992 if (!seed)
7993 return -EINVAL;
7994
7995 status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
Tony Nguyenc1484692021-10-07 16:01:58 -07007996 if (status)
Tony Nguyen5f87ec42021-10-07 15:56:02 -07007997 dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
Tony Nguyen5518ac22021-10-07 15:59:03 -07007998 status, ice_aq_str(hw->adminq.sq_last_status));
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07007999
Tony Nguyenc1484692021-10-07 16:01:58 -07008000 return status;
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07008001}
8002
8003/**
Jeff Guo352e9bf2023-12-12 17:33:20 -07008004 * ice_set_rss_hfunc - Set RSS HASH function
8005 * @vsi: Pointer to VSI structure
8006 * @hfunc: hash function (ICE_AQ_VSI_Q_OPT_RSS_*)
8007 *
8008 * Returns 0 on success, negative on failure
8009 */
8010int ice_set_rss_hfunc(struct ice_vsi *vsi, u8 hfunc)
8011{
8012 struct ice_hw *hw = &vsi->back->hw;
8013 struct ice_vsi_ctx *ctx;
8014 bool symm;
8015 int err;
8016
8017 if (hfunc == vsi->rss_hfunc)
8018 return 0;
8019
8020 if (hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ &&
8021 hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ)
8022 return -EOPNOTSUPP;
8023
8024 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8025 if (!ctx)
8026 return -ENOMEM;
8027
8028 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
8029 ctx->info.q_opt_rss = vsi->info.q_opt_rss;
8030 ctx->info.q_opt_rss &= ~ICE_AQ_VSI_Q_OPT_RSS_HASH_M;
8031 ctx->info.q_opt_rss |=
8032 FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hfunc);
8033 ctx->info.q_opt_tc = vsi->info.q_opt_tc;
8034 ctx->info.q_opt_flags = vsi->info.q_opt_rss;
8035
8036 err = ice_update_vsi(hw, vsi->idx, ctx, NULL);
8037 if (err) {
8038 dev_err(ice_pf_to_dev(vsi->back), "Failed to configure RSS hash for VSI %d, error %d\n",
8039 vsi->vsi_num, err);
8040 } else {
8041 vsi->info.q_opt_rss = ctx->info.q_opt_rss;
8042 vsi->rss_hfunc = hfunc;
8043 netdev_info(vsi->netdev, "Hash function set to: %sToeplitz\n",
8044 hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ ?
8045 "Symmetric " : "");
8046 }
8047 kfree(ctx);
8048 if (err)
8049 return err;
8050
8051 /* Fix the symmetry setting for all existing RSS configurations */
8052 symm = !!(hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ);
8053 return ice_set_rss_cfg_symm(hw, vsi, symm);
8054}
8055
8056/**
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008057 * ice_bridge_getlink - Get the hardware bridge mode
8058 * @skb: skb buff
Anirudh Venkataramananf9867df2019-02-19 15:04:13 -08008059 * @pid: process ID
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008060 * @seq: RTNL message seq
8061 * @dev: the netdev being configured
8062 * @filter_mask: filter mask passed in
8063 * @nlflags: netlink flags passed in
8064 *
8065 * Return the bridge mode (VEB/VEPA)
8066 */
8067static int
8068ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8069 struct net_device *dev, u32 filter_mask, int nlflags)
8070{
8071 struct ice_netdev_priv *np = netdev_priv(dev);
8072 struct ice_vsi *vsi = np->vsi;
8073 struct ice_pf *pf = vsi->back;
8074 u16 bmode;
8075
8076 bmode = pf->first_sw->bridge_mode;
8077
8078 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
8079 filter_mask, NULL);
8080}
8081
8082/**
8083 * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
8084 * @vsi: Pointer to VSI structure
8085 * @bmode: Hardware bridge mode (VEB/VEPA)
8086 *
8087 * Returns 0 on success, negative on failure
8088 */
8089static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
8090{
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008091 struct ice_aqc_vsi_props *vsi_props;
8092 struct ice_hw *hw = &vsi->back->hw;
Bruce Allan198a6662019-02-08 12:50:32 -08008093 struct ice_vsi_ctx *ctxt;
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07008094 int ret;
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008095
8096 vsi_props = &vsi->info;
Bruce Allan198a6662019-02-08 12:50:32 -08008097
Tony Nguyen9efe35d02019-11-08 06:23:25 -08008098 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
Bruce Allan198a6662019-02-08 12:50:32 -08008099 if (!ctxt)
8100 return -ENOMEM;
8101
8102 ctxt->info = vsi->info;
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008103
8104 if (bmode == BRIDGE_MODE_VEB)
8105 /* change from VEPA to VEB mode */
Bruce Allan198a6662019-02-08 12:50:32 -08008106 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008107 else
8108 /* change from VEB to VEPA mode */
Bruce Allan198a6662019-02-08 12:50:32 -08008109 ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
8110 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
Anirudh Venkataramanan5726ca02018-09-19 17:23:12 -07008111
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07008112 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
8113 if (ret) {
Tony Nguyen5f87ec42021-10-07 15:56:02 -07008114 dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07008115 bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
Bruce Allan198a6662019-02-08 12:50:32 -08008116 goto out;
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008117 }
8118 /* Update sw flags for book keeping */
Bruce Allan198a6662019-02-08 12:50:32 -08008119 vsi_props->sw_flags = ctxt->info.sw_flags;
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008120
Bruce Allan198a6662019-02-08 12:50:32 -08008121out:
Tony Nguyen9efe35d02019-11-08 06:23:25 -08008122 kfree(ctxt);
Bruce Allan198a6662019-02-08 12:50:32 -08008123 return ret;
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008124}
8125
8126/**
8127 * ice_bridge_setlink - Set the hardware bridge mode
8128 * @dev: the netdev being configured
8129 * @nlh: RTNL message
8130 * @flags: bridge setlink flags
Petr Machata2fd527b2018-12-12 17:02:48 +00008131 * @extack: netlink extended ack
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008132 *
8133 * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
8134 * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
8135 * not already set for all VSIs connected to this switch. And also update the
8136 * unicast switch filter rules for the corresponding switch of the netdev.
8137 */
8138static int
8139ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
Bruce Allan3d505142018-12-19 10:03:20 -08008140 u16 __always_unused flags,
8141 struct netlink_ext_ack __always_unused *extack)
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008142{
8143 struct ice_netdev_priv *np = netdev_priv(dev);
8144 struct ice_pf *pf = np->vsi->back;
8145 struct nlattr *attr, *br_spec;
8146 struct ice_hw *hw = &pf->hw;
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008147 struct ice_sw *pf_sw;
8148 int rem, v, err = 0;
8149
8150 pf_sw = pf->first_sw;
8151 /* find the attribute in the netlink message */
8152 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Rand Deeb06e456a2024-02-28 18:54:48 +03008153 if (!br_spec)
8154 return -EINVAL;
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008155
Johannes Berge8058a42024-03-28 20:31:45 +01008156 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
8157 __u16 mode = nla_get_u16(attr);
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008158
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008159 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
8160 return -EINVAL;
8161 /* Continue if bridge mode is not being flipped */
8162 if (mode == pf_sw->bridge_mode)
8163 continue;
8164 /* Iterates through the PF VSI list and update the loopback
8165 * mode of the VSI
8166 */
8167 ice_for_each_vsi(pf, v) {
8168 if (!pf->vsi[v])
8169 continue;
8170 err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
8171 if (err)
8172 return err;
8173 }
8174
8175 hw->evb_veb = (mode == BRIDGE_MODE_VEB);
8176 /* Update the unicast switch filter rules for the corresponding
8177 * switch of the netdev
8178 */
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07008179 err = ice_update_sw_rule_bridge_mode(hw);
8180 if (err) {
Tony Nguyen5f87ec42021-10-07 15:56:02 -07008181 netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07008182 mode, err,
Lihong Yang0fee3572020-05-07 17:41:04 -07008183 ice_aq_str(hw->adminq.sq_last_status));
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008184 /* revert hw->evb_veb */
8185 hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
Tony Nguyenc1484692021-10-07 16:01:58 -07008186 return err;
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07008187 }
8188
8189 pf_sw->bridge_mode = mode;
8190 }
8191
8192 return 0;
8193}
8194
8195/**
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008196 * ice_tx_timeout - Respond to a Tx Hang
8197 * @netdev: network interface device structure
Bruce Allan644f40e2020-01-22 07:21:37 -08008198 * @txqueue: Tx queue
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008199 */
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05008200static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008201{
8202 struct ice_netdev_priv *np = netdev_priv(netdev);
Maciej Fijalkowskie72bba22021-08-19 13:59:58 +02008203 struct ice_tx_ring *tx_ring = NULL;
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008204 struct ice_vsi *vsi = np->vsi;
8205 struct ice_pf *pf = vsi->back;
Brett Creeley807bc982018-10-26 10:41:00 -07008206 u32 i;
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008207
8208 pf->tx_timeout_count++;
8209
Avinash JD610ed0e2020-05-07 17:41:00 -07008210 /* Check if PFC is enabled for the TC to which the queue belongs
8211 * to. If yes then Tx timeout is not caused by a hung queue, no
8212 * need to reset and rebuild
8213 */
8214 if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
8215 dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
8216 txqueue);
8217 return;
8218 }
8219
Julio Faraccoed5a3f62019-12-18 15:38:45 -03008220 /* now that we have an index, find the tx_ring struct */
Maciej Fijalkowski2faf63b2021-08-19 14:00:04 +02008221 ice_for_each_txq(vsi, i)
Julio Faraccoed5a3f62019-12-18 15:38:45 -03008222 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
8223 if (txqueue == vsi->tx_rings[i]->q_index) {
8224 tx_ring = vsi->tx_rings[i];
8225 break;
8226 }
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008227
8228 /* Reset recovery level if enough time has elapsed after last timeout.
8229 * Also ensure no new reset action happens before next timeout period.
8230 */
8231 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
8232 pf->tx_timeout_recovery_level = 1;
8233 else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
8234 netdev->watchdog_timeo)))
8235 return;
8236
8237 if (tx_ring) {
Brett Creeley807bc982018-10-26 10:41:00 -07008238 struct ice_hw *hw = &pf->hw;
8239 u32 head, val = 0;
8240
Jesse Brandeburg5a259f82023-12-05 17:01:12 -08008241 head = FIELD_GET(QTX_COMM_HEAD_HEAD_M,
8242 rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])));
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008243 /* Read interrupt register */
Brett Creeleyba880732019-06-26 02:20:25 -07008244 val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008245
Tony Nguyen93ff4852020-02-27 10:15:02 -08008246 netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
Julio Faraccoed5a3f62019-12-18 15:38:45 -03008247 vsi->vsi_num, txqueue, tx_ring->next_to_clean,
Brett Creeley807bc982018-10-26 10:41:00 -07008248 head, tx_ring->next_to_use, val);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008249 }
8250
8251 pf->tx_timeout_last_recovery = jiffies;
Tony Nguyen93ff4852020-02-27 10:15:02 -08008252 netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
Julio Faraccoed5a3f62019-12-18 15:38:45 -03008253 pf->tx_timeout_recovery_level, txqueue);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008254
8255 switch (pf->tx_timeout_recovery_level) {
8256 case 1:
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08008257 set_bit(ICE_PFR_REQ, pf->state);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008258 break;
8259 case 2:
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08008260 set_bit(ICE_CORER_REQ, pf->state);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008261 break;
8262 case 3:
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08008263 set_bit(ICE_GLOBR_REQ, pf->state);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008264 break;
8265 default:
8266 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08008267 set_bit(ICE_DOWN, pf->state);
Anirudh Venkataramanane97fb1a2021-03-02 10:15:37 -08008268 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08008269 set_bit(ICE_SERVICE_DIS, pf->state);
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07008270 break;
8271 }
8272
8273 ice_service_task_schedule(pf);
8274 pf->tx_timeout_recovery_level++;
8275}
8276
8277/**
Kiran Patil0d08a442021-08-06 10:49:05 +02008278 * ice_setup_tc_cls_flower - flower classifier offloads
8279 * @np: net device to configure
8280 * @filter_dev: device on which filter is added
8281 * @cls_flower: offload data
8282 */
8283static int
8284ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
8285 struct net_device *filter_dev,
8286 struct flow_cls_offload *cls_flower)
8287{
8288 struct ice_vsi *vsi = np->vsi;
8289
8290 if (cls_flower->common.chain_index)
8291 return -EOPNOTSUPP;
8292
8293 switch (cls_flower->command) {
8294 case FLOW_CLS_REPLACE:
8295 return ice_add_cls_flower(filter_dev, vsi, cls_flower);
8296 case FLOW_CLS_DESTROY:
8297 return ice_del_cls_flower(vsi, cls_flower);
8298 default:
8299 return -EINVAL;
8300 }
8301}
8302
8303/**
8304 * ice_setup_tc_block_cb - callback handler registered for TC block
8305 * @type: TC SETUP type
8306 * @type_data: TC flower offload data that contains user input
8307 * @cb_priv: netdev private data
8308 */
8309static int
8310ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
8311{
8312 struct ice_netdev_priv *np = cb_priv;
8313
8314 switch (type) {
8315 case TC_SETUP_CLSFLOWER:
8316 return ice_setup_tc_cls_flower(np, np->vsi->netdev,
8317 type_data);
8318 default:
8319 return -EOPNOTSUPP;
8320 }
8321}
8322
Kiran Patilfbc7b272021-10-15 16:35:16 -07008323/**
8324 * ice_validate_mqprio_qopt - Validate TCF input parameters
8325 * @vsi: Pointer to VSI
8326 * @mqprio_qopt: input parameters for mqprio queue configuration
8327 *
8328 * This function validates MQPRIO params, such as qcount (power of 2 wherever
8329 * needed), and make sure user doesn't specify qcount and BW rate limit
8330 * for TCs, which are more than "num_tc"
8331 */
8332static int
8333ice_validate_mqprio_qopt(struct ice_vsi *vsi,
8334 struct tc_mqprio_qopt_offload *mqprio_qopt)
8335{
Kiran Patilfbc7b272021-10-15 16:35:16 -07008336 int non_power_of_2_qcount = 0;
8337 struct ice_pf *pf = vsi->back;
8338 int max_rss_q_cnt = 0;
Sridhar Samudrala5f16da62023-06-09 17:40:23 -07008339 u64 sum_min_rate = 0;
Kiran Patilfbc7b272021-10-15 16:35:16 -07008340 struct device *dev;
8341 int i, speed;
8342 u8 num_tc;
8343
8344 if (vsi->type != ICE_VSI_PF)
8345 return -EINVAL;
8346
8347 if (mqprio_qopt->qopt.offset[0] != 0 ||
8348 mqprio_qopt->qopt.num_tc < 1 ||
8349 mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
8350 return -EINVAL;
8351
8352 dev = ice_pf_to_dev(pf);
8353 vsi->ch_rss_size = 0;
8354 num_tc = mqprio_qopt->qopt.num_tc;
Sridhar Samudrala5f16da62023-06-09 17:40:23 -07008355 speed = ice_get_link_speed_kbps(vsi);
Kiran Patilfbc7b272021-10-15 16:35:16 -07008356
8357 for (i = 0; num_tc; i++) {
8358 int qcount = mqprio_qopt->qopt.count[i];
8359 u64 max_rate, min_rate, rem;
8360
8361 if (!qcount)
8362 return -EINVAL;
8363
8364 if (is_power_of_2(qcount)) {
8365 if (non_power_of_2_qcount &&
8366 qcount > non_power_of_2_qcount) {
8367 dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
8368 qcount, non_power_of_2_qcount);
8369 return -EINVAL;
8370 }
8371 if (qcount > max_rss_q_cnt)
8372 max_rss_q_cnt = qcount;
8373 } else {
8374 if (non_power_of_2_qcount &&
8375 qcount != non_power_of_2_qcount) {
8376 dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
8377 qcount, non_power_of_2_qcount);
8378 return -EINVAL;
8379 }
8380 if (qcount < max_rss_q_cnt) {
8381 dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
8382 qcount, max_rss_q_cnt);
8383 return -EINVAL;
8384 }
8385 max_rss_q_cnt = qcount;
8386 non_power_of_2_qcount = qcount;
8387 }
8388
8389 /* TC command takes input in K/N/Gbps or K/M/Gbit etc but
8390 * converts the bandwidth rate limit into Bytes/s when
8391 * passing it down to the driver. So convert input bandwidth
8392 * from Bytes/s to Kbps
8393 */
8394 max_rate = mqprio_qopt->max_rate[i];
8395 max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
Kiran Patilfbc7b272021-10-15 16:35:16 -07008396
8397 /* min_rate is minimum guaranteed rate and it can't be zero */
8398 min_rate = mqprio_qopt->min_rate[i];
8399 min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
8400 sum_min_rate += min_rate;
8401
8402 if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
8403 dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
8404 min_rate, ICE_MIN_BW_LIMIT);
8405 return -EINVAL;
8406 }
8407
Sridhar Samudrala5f16da62023-06-09 17:40:23 -07008408 if (max_rate && max_rate > speed) {
8409 dev_err(dev, "TC%d: max_rate(%llu Kbps) > link speed of %u Kbps\n",
8410 i, max_rate, speed);
8411 return -EINVAL;
8412 }
8413
Kiran Patilfbc7b272021-10-15 16:35:16 -07008414 iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
8415 if (rem) {
8416 dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
8417 i, ICE_MIN_BW_LIMIT);
8418 return -EINVAL;
8419 }
8420
8421 iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
8422 if (rem) {
8423 dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
8424 i, ICE_MIN_BW_LIMIT);
8425 return -EINVAL;
8426 }
8427
8428 /* min_rate can't be more than max_rate, except when max_rate
8429 * is zero (implies max_rate sought is max line rate). In such
8430 * a case min_rate can be more than max.
8431 */
8432 if (max_rate && min_rate > max_rate) {
8433 dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
8434 min_rate, max_rate);
8435 return -EINVAL;
8436 }
8437
8438 if (i >= mqprio_qopt->qopt.num_tc - 1)
8439 break;
8440 if (mqprio_qopt->qopt.offset[i + 1] !=
8441 (mqprio_qopt->qopt.offset[i] + qcount))
8442 return -EINVAL;
8443 }
8444 if (vsi->num_rxq <
8445 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8446 return -EINVAL;
8447 if (vsi->num_txq <
8448 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8449 return -EINVAL;
8450
Kiran Patilfbc7b272021-10-15 16:35:16 -07008451 if (sum_min_rate && sum_min_rate > (u64)speed) {
8452 dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
8453 sum_min_rate, speed);
8454 return -EINVAL;
8455 }
8456
8457 /* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
8458 vsi->ch_rss_size = max_rss_q_cnt;
8459
8460 return 0;
8461}
8462
8463/**
Kiran Patil40319792021-12-29 10:54:33 -08008464 * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
8465 * @pf: ptr to PF device
8466 * @vsi: ptr to VSI
8467 */
8468static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
8469{
8470 struct device *dev = ice_pf_to_dev(pf);
8471 bool added = false;
8472 struct ice_hw *hw;
8473 int flow;
8474
8475 if (!(vsi->num_gfltr || vsi->num_bfltr))
8476 return -EINVAL;
8477
8478 hw = &pf->hw;
8479 for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
8480 struct ice_fd_hw_prof *prof;
8481 int tun, status;
8482 u64 entry_h;
8483
8484 if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
8485 hw->fdir_prof[flow]->cnt))
8486 continue;
8487
8488 for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
8489 enum ice_flow_priority prio;
Kiran Patil40319792021-12-29 10:54:33 -08008490
8491 /* add this VSI to FDir profile for this flow */
8492 prio = ICE_FLOW_PRIO_NORMAL;
8493 prof = hw->fdir_prof[flow];
Ahmed Zakib1f59212023-12-12 17:33:19 -07008494 status = ice_flow_add_entry(hw, ICE_BLK_FD,
8495 prof->prof_id[tun],
Kiran Patil40319792021-12-29 10:54:33 -08008496 prof->vsi_h[0], vsi->idx,
8497 prio, prof->fdir_seg[tun],
8498 &entry_h);
8499 if (status) {
8500 dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
8501 vsi->idx, flow);
8502 continue;
8503 }
8504
8505 prof->entry_h[prof->cnt][tun] = entry_h;
8506 }
8507
8508 /* store VSI for filter replay and delete */
8509 prof->vsi_h[prof->cnt] = vsi->idx;
8510 prof->cnt++;
8511
8512 added = true;
8513 dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
8514 flow);
8515 }
8516
8517 if (!added)
8518 dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
8519
8520 return 0;
8521}
8522
8523/**
Kiran Patilfbc7b272021-10-15 16:35:16 -07008524 * ice_add_channel - add a channel by adding VSI
8525 * @pf: ptr to PF device
8526 * @sw_id: underlying HW switching element ID
8527 * @ch: ptr to channel structure
8528 *
8529 * Add a channel (VSI) using add_vsi and queue_map
8530 */
8531static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
8532{
8533 struct device *dev = ice_pf_to_dev(pf);
8534 struct ice_vsi *vsi;
8535
8536 if (ch->type != ICE_VSI_CHNL) {
8537 dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
8538 return -EINVAL;
8539 }
8540
8541 vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
8542 if (!vsi || vsi->type != ICE_VSI_CHNL) {
8543 dev_err(dev, "create chnl VSI failure\n");
8544 return -EINVAL;
8545 }
8546
Kiran Patil40319792021-12-29 10:54:33 -08008547 ice_add_vsi_to_fdir(pf, vsi);
8548
Kiran Patilfbc7b272021-10-15 16:35:16 -07008549 ch->sw_id = sw_id;
8550 ch->vsi_num = vsi->vsi_num;
8551 ch->info.mapping_flags = vsi->info.mapping_flags;
8552 ch->ch_vsi = vsi;
8553 /* set the back pointer of channel for newly created VSI */
8554 vsi->ch = ch;
8555
8556 memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
8557 sizeof(vsi->info.q_mapping));
8558 memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
8559 sizeof(vsi->info.tc_mapping));
8560
8561 return 0;
8562}
8563
8564/**
8565 * ice_chnl_cfg_res
8566 * @vsi: the VSI being setup
8567 * @ch: ptr to channel structure
8568 *
8569 * Configure channel specific resources such as rings, vector.
8570 */
8571static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
8572{
8573 int i;
8574
8575 for (i = 0; i < ch->num_txq; i++) {
8576 struct ice_q_vector *tx_q_vector, *rx_q_vector;
8577 struct ice_ring_container *rc;
8578 struct ice_tx_ring *tx_ring;
8579 struct ice_rx_ring *rx_ring;
8580
8581 tx_ring = vsi->tx_rings[ch->base_q + i];
8582 rx_ring = vsi->rx_rings[ch->base_q + i];
8583 if (!tx_ring || !rx_ring)
8584 continue;
8585
8586 /* setup ring being channel enabled */
8587 tx_ring->ch = ch;
8588 rx_ring->ch = ch;
8589
8590 /* following code block sets up vector specific attributes */
8591 tx_q_vector = tx_ring->q_vector;
8592 rx_q_vector = rx_ring->q_vector;
8593 if (!tx_q_vector && !rx_q_vector)
8594 continue;
8595
8596 if (tx_q_vector) {
8597 tx_q_vector->ch = ch;
8598 /* setup Tx and Rx ITR setting if DIM is off */
8599 rc = &tx_q_vector->tx;
8600 if (!ITR_IS_DYNAMIC(rc))
8601 ice_write_itr(rc, rc->itr_setting);
8602 }
8603 if (rx_q_vector) {
8604 rx_q_vector->ch = ch;
8605 /* setup Tx and Rx ITR setting if DIM is off */
8606 rc = &rx_q_vector->rx;
8607 if (!ITR_IS_DYNAMIC(rc))
8608 ice_write_itr(rc, rc->itr_setting);
8609 }
8610 }
8611
8612 /* it is safe to assume that, if channel has non-zero num_t[r]xq, then
8613 * GLINT_ITR register would have written to perform in-context
8614 * update, hence perform flush
8615 */
8616 if (ch->num_txq || ch->num_rxq)
8617 ice_flush(&vsi->back->hw);
8618}
8619
8620/**
8621 * ice_cfg_chnl_all_res - configure channel resources
8622 * @vsi: pte to main_vsi
8623 * @ch: ptr to channel structure
8624 *
8625 * This function configures channel specific resources such as flow-director
8626 * counter index, and other resources such as queues, vectors, ITR settings
8627 */
8628static void
8629ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
8630{
8631 /* configure channel (aka ADQ) resources such as queues, vectors,
8632 * ITR settings for channel specific vectors and anything else
8633 */
8634 ice_chnl_cfg_res(vsi, ch);
8635}
8636
8637/**
8638 * ice_setup_hw_channel - setup new channel
8639 * @pf: ptr to PF device
8640 * @vsi: the VSI being setup
8641 * @ch: ptr to channel structure
8642 * @sw_id: underlying HW switching element ID
8643 * @type: type of channel to be created (VMDq2/VF)
8644 *
8645 * Setup new channel (VSI) based on specified type (VMDq2/VF)
8646 * and configures Tx rings accordingly
8647 */
8648static int
8649ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8650 struct ice_channel *ch, u16 sw_id, u8 type)
8651{
8652 struct device *dev = ice_pf_to_dev(pf);
8653 int ret;
8654
8655 ch->base_q = vsi->next_base_q;
8656 ch->type = type;
8657
8658 ret = ice_add_channel(pf, sw_id, ch);
8659 if (ret) {
8660 dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
8661 return ret;
8662 }
8663
8664 /* configure/setup ADQ specific resources */
8665 ice_cfg_chnl_all_res(vsi, ch);
8666
8667 /* make sure to update the next_base_q so that subsequent channel's
8668 * (aka ADQ) VSI queue map is correct
8669 */
8670 vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
8671 dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
8672 ch->num_rxq);
8673
8674 return 0;
8675}
8676
8677/**
8678 * ice_setup_channel - setup new channel using uplink element
8679 * @pf: ptr to PF device
8680 * @vsi: the VSI being setup
8681 * @ch: ptr to channel structure
8682 *
8683 * Setup new channel (VSI) based on specified type (VMDq2/VF)
8684 * and uplink switching element
8685 */
8686static bool
8687ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8688 struct ice_channel *ch)
8689{
8690 struct device *dev = ice_pf_to_dev(pf);
8691 u16 sw_id;
8692 int ret;
8693
8694 if (vsi->type != ICE_VSI_PF) {
8695 dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
8696 return false;
8697 }
8698
8699 sw_id = pf->first_sw->sw_id;
8700
8701 /* create channel (VSI) */
8702 ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
8703 if (ret) {
8704 dev_err(dev, "failed to setup hw_channel\n");
8705 return false;
8706 }
8707 dev_dbg(dev, "successfully created channel()\n");
8708
8709 return ch->ch_vsi ? true : false;
8710}
8711
8712/**
8713 * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
8714 * @vsi: VSI to be configured
8715 * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
8716 * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
8717 */
8718static int
8719ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
8720{
8721 int err;
8722
8723 err = ice_set_min_bw_limit(vsi, min_tx_rate);
8724 if (err)
8725 return err;
8726
8727 return ice_set_max_bw_limit(vsi, max_tx_rate);
8728}
8729
8730/**
8731 * ice_create_q_channel - function to create channel
8732 * @vsi: VSI to be configured
8733 * @ch: ptr to channel (it contains channel specific params)
8734 *
8735 * This function creates channel (VSI) using num_queues specified by user,
8736 * reconfigs RSS if needed.
8737 */
8738static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
8739{
8740 struct ice_pf *pf = vsi->back;
8741 struct device *dev;
8742
8743 if (!ch)
8744 return -EINVAL;
8745
8746 dev = ice_pf_to_dev(pf);
8747 if (!ch->num_txq || !ch->num_rxq) {
8748 dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
8749 return -EINVAL;
8750 }
8751
8752 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
8753 dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
8754 vsi->cnt_q_avail, ch->num_txq);
8755 return -EINVAL;
8756 }
8757
8758 if (!ice_setup_channel(pf, vsi, ch)) {
8759 dev_info(dev, "Failed to setup channel\n");
8760 return -EINVAL;
8761 }
8762 /* configure BW rate limit */
8763 if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
8764 int ret;
8765
8766 ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
8767 ch->min_tx_rate);
8768 if (ret)
8769 dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
8770 ch->max_tx_rate, ch->ch_vsi->vsi_num);
8771 else
8772 dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
8773 ch->max_tx_rate, ch->ch_vsi->vsi_num);
8774 }
8775
8776 vsi->cnt_q_avail -= ch->num_txq;
8777
8778 return 0;
8779}
8780
8781/**
Kiran Patil9fea7492021-10-15 16:35:17 -07008782 * ice_rem_all_chnl_fltrs - removes all channel filters
8783 * @pf: ptr to PF, TC-flower based filter are tracked at PF level
8784 *
8785 * Remove all advanced switch filters only if they are channel specific
8786 * tc-flower based filter
8787 */
8788static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
8789{
8790 struct ice_tc_flower_fltr *fltr;
8791 struct hlist_node *node;
8792
8793 /* to remove all channel filters, iterate an ordered list of filters */
8794 hlist_for_each_entry_safe(fltr, node,
8795 &pf->tc_flower_fltr_list,
8796 tc_flower_node) {
8797 struct ice_rule_query_data rule;
8798 int status;
8799
8800 /* for now process only channel specific filters */
8801 if (!ice_is_chnl_fltr(fltr))
8802 continue;
8803
8804 rule.rid = fltr->rid;
8805 rule.rule_id = fltr->rule_id;
Amritha Nambiar143b86f2022-10-21 00:58:45 -07008806 rule.vsi_handle = fltr->dest_vsi_handle;
Kiran Patil9fea7492021-10-15 16:35:17 -07008807 status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
8808 if (status) {
8809 if (status == -ENOENT)
8810 dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
8811 rule.rule_id);
8812 else
8813 dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
8814 status);
8815 } else if (fltr->dest_vsi) {
8816 /* update advanced switch filter count */
8817 if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
8818 u32 flags = fltr->flags;
8819
8820 fltr->dest_vsi->num_chnl_fltr--;
8821 if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
8822 ICE_TC_FLWR_FIELD_ENC_DST_MAC))
8823 pf->num_dmac_chnl_fltrs--;
8824 }
8825 }
8826
8827 hlist_del(&fltr->tc_flower_node);
8828 kfree(fltr);
8829 }
8830}
8831
8832/**
Kiran Patilfbc7b272021-10-15 16:35:16 -07008833 * ice_remove_q_channels - Remove queue channels for the TCs
8834 * @vsi: VSI to be configured
8835 * @rem_fltr: delete advanced switch filter or not
8836 *
8837 * Remove queue channels for the TCs
8838 */
Kiran Patil9fea7492021-10-15 16:35:17 -07008839static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
Kiran Patilfbc7b272021-10-15 16:35:16 -07008840{
8841 struct ice_channel *ch, *ch_tmp;
Kiran Patil9fea7492021-10-15 16:35:17 -07008842 struct ice_pf *pf = vsi->back;
Kiran Patilfbc7b272021-10-15 16:35:16 -07008843 int i;
8844
Kiran Patil9fea7492021-10-15 16:35:17 -07008845 /* remove all tc-flower based filter if they are channel filters only */
8846 if (rem_fltr)
8847 ice_rem_all_chnl_fltrs(pf);
8848
Kiran Patil40319792021-12-29 10:54:33 -08008849 /* remove ntuple filters since queue configuration is being changed */
8850 if (vsi->netdev->features & NETIF_F_NTUPLE) {
8851 struct ice_hw *hw = &pf->hw;
8852
8853 mutex_lock(&hw->fdir_fltr_lock);
8854 ice_fdir_del_all_fltrs(vsi);
8855 mutex_unlock(&hw->fdir_fltr_lock);
8856 }
8857
Kiran Patilfbc7b272021-10-15 16:35:16 -07008858 /* perform cleanup for channels if they exist */
8859 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8860 struct ice_vsi *ch_vsi;
8861
8862 list_del(&ch->list);
8863 ch_vsi = ch->ch_vsi;
8864 if (!ch_vsi) {
8865 kfree(ch);
8866 continue;
8867 }
8868
8869 /* Reset queue contexts */
8870 for (i = 0; i < ch->num_rxq; i++) {
8871 struct ice_tx_ring *tx_ring;
8872 struct ice_rx_ring *rx_ring;
8873
8874 tx_ring = vsi->tx_rings[ch->base_q + i];
8875 rx_ring = vsi->rx_rings[ch->base_q + i];
8876 if (tx_ring) {
8877 tx_ring->ch = NULL;
8878 if (tx_ring->q_vector)
8879 tx_ring->q_vector->ch = NULL;
8880 }
8881 if (rx_ring) {
8882 rx_ring->ch = NULL;
8883 if (rx_ring->q_vector)
8884 rx_ring->q_vector->ch = NULL;
8885 }
8886 }
8887
Kiran Patil40319792021-12-29 10:54:33 -08008888 /* Release FD resources for the channel VSI */
8889 ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
8890
Kiran Patilfbc7b272021-10-15 16:35:16 -07008891 /* clear the VSI from scheduler tree */
8892 ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
8893
Michal Swiatkowski227bf452022-12-21 12:38:20 +01008894 /* Delete VSI from FW, PF and HW VSI arrays */
Kiran Patilfbc7b272021-10-15 16:35:16 -07008895 ice_vsi_delete(ch->ch_vsi);
8896
Kiran Patilfbc7b272021-10-15 16:35:16 -07008897 /* free the channel */
8898 kfree(ch);
8899 }
8900
8901 /* clear the channel VSI map which is stored in main VSI */
8902 ice_for_each_chnl_tc(i)
8903 vsi->tc_map_vsi[i] = NULL;
8904
8905 /* reset main VSI's all TC information */
8906 vsi->all_enatc = 0;
8907 vsi->all_numtc = 0;
8908}
8909
8910/**
8911 * ice_rebuild_channels - rebuild channel
8912 * @pf: ptr to PF
8913 *
8914 * Recreate channel VSIs and replay filters
8915 */
8916static int ice_rebuild_channels(struct ice_pf *pf)
8917{
8918 struct device *dev = ice_pf_to_dev(pf);
8919 struct ice_vsi *main_vsi;
8920 bool rem_adv_fltr = true;
8921 struct ice_channel *ch;
8922 struct ice_vsi *vsi;
8923 int tc_idx = 1;
8924 int i, err;
8925
8926 main_vsi = ice_get_main_vsi(pf);
8927 if (!main_vsi)
8928 return 0;
8929
8930 if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
8931 main_vsi->old_numtc == 1)
8932 return 0; /* nothing to be done */
8933
8934 /* reconfigure main VSI based on old value of TC and cached values
8935 * for MQPRIO opts
8936 */
8937 err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
8938 if (err) {
8939 dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
8940 main_vsi->old_ena_tc, main_vsi->vsi_num);
8941 return err;
8942 }
8943
8944 /* rebuild ADQ VSIs */
8945 ice_for_each_vsi(pf, i) {
8946 enum ice_vsi_type type;
8947
8948 vsi = pf->vsi[i];
8949 if (!vsi || vsi->type != ICE_VSI_CHNL)
8950 continue;
8951
8952 type = vsi->type;
8953
8954 /* rebuild ADQ VSI */
Michal Swiatkowski6624e782022-12-21 12:38:16 +01008955 err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
Kiran Patilfbc7b272021-10-15 16:35:16 -07008956 if (err) {
8957 dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
8958 ice_vsi_type_str(type), vsi->idx, err);
8959 goto cleanup;
8960 }
8961
8962 /* Re-map HW VSI number, using VSI handle that has been
8963 * previously validated in ice_replay_vsi() call above
8964 */
8965 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
8966
8967 /* replay filters for the VSI */
8968 err = ice_replay_vsi(&pf->hw, vsi->idx);
8969 if (err) {
8970 dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
8971 ice_vsi_type_str(type), err, vsi->idx);
8972 rem_adv_fltr = false;
8973 goto cleanup;
8974 }
8975 dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
8976 ice_vsi_type_str(type), vsi->idx);
8977
8978 /* store ADQ VSI at correct TC index in main VSI's
8979 * map of TC to VSI
8980 */
8981 main_vsi->tc_map_vsi[tc_idx++] = vsi;
8982 }
8983
8984 /* ADQ VSI(s) has been rebuilt successfully, so setup
8985 * channel for main VSI's Tx and Rx rings
8986 */
8987 list_for_each_entry(ch, &main_vsi->ch_list, list) {
8988 struct ice_vsi *ch_vsi;
8989
8990 ch_vsi = ch->ch_vsi;
8991 if (!ch_vsi)
8992 continue;
8993
8994 /* reconfig channel resources */
8995 ice_cfg_chnl_all_res(main_vsi, ch);
8996
8997 /* replay BW rate limit if it is non-zero */
8998 if (!ch->max_tx_rate && !ch->min_tx_rate)
8999 continue;
9000
9001 err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
9002 ch->min_tx_rate);
9003 if (err)
9004 dev_err(dev, "failed (err:%d) to rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
9005 err, ch->max_tx_rate, ch->min_tx_rate,
9006 ch_vsi->vsi_num);
9007 else
9008 dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
9009 ch->max_tx_rate, ch->min_tx_rate,
9010 ch_vsi->vsi_num);
9011 }
9012
9013 /* reconfig RSS for main VSI */
9014 if (main_vsi->ch_rss_size)
9015 ice_vsi_cfg_rss_lut_key(main_vsi);
9016
9017 return 0;
9018
9019cleanup:
9020 ice_remove_q_channels(main_vsi, rem_adv_fltr);
9021 return err;
9022}
9023
9024/**
9025 * ice_create_q_channels - Add queue channel for the given TCs
9026 * @vsi: VSI to be configured
9027 *
9028 * Configures queue channel mapping to the given TCs
9029 */
9030static int ice_create_q_channels(struct ice_vsi *vsi)
9031{
9032 struct ice_pf *pf = vsi->back;
9033 struct ice_channel *ch;
9034 int ret = 0, i;
9035
9036 ice_for_each_chnl_tc(i) {
9037 if (!(vsi->all_enatc & BIT(i)))
9038 continue;
9039
9040 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
9041 if (!ch) {
9042 ret = -ENOMEM;
9043 goto err_free;
9044 }
9045 INIT_LIST_HEAD(&ch->list);
9046 ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
9047 ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
9048 ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
9049 ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
9050 ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
9051
9052 /* convert to Kbits/s */
9053 if (ch->max_tx_rate)
9054 ch->max_tx_rate = div_u64(ch->max_tx_rate,
9055 ICE_BW_KBPS_DIVISOR);
9056 if (ch->min_tx_rate)
9057 ch->min_tx_rate = div_u64(ch->min_tx_rate,
9058 ICE_BW_KBPS_DIVISOR);
9059
9060 ret = ice_create_q_channel(vsi, ch);
9061 if (ret) {
9062 dev_err(ice_pf_to_dev(pf),
9063 "failed creating channel TC:%d\n", i);
9064 kfree(ch);
9065 goto err_free;
9066 }
9067 list_add_tail(&ch->list, &vsi->ch_list);
9068 vsi->tc_map_vsi[i] = ch->ch_vsi;
9069 dev_dbg(ice_pf_to_dev(pf),
9070 "successfully created channel: VSI %pK\n", ch->ch_vsi);
9071 }
9072 return 0;
9073
9074err_free:
9075 ice_remove_q_channels(vsi, false);
9076
9077 return ret;
9078}
9079
9080/**
9081 * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
9082 * @netdev: net device to configure
9083 * @type_data: TC offload data
9084 */
9085static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
9086{
9087 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
9088 struct ice_netdev_priv *np = netdev_priv(netdev);
9089 struct ice_vsi *vsi = np->vsi;
9090 struct ice_pf *pf = vsi->back;
9091 u16 mode, ena_tc_qdisc = 0;
9092 int cur_txq, cur_rxq;
9093 u8 hw = 0, num_tcf;
9094 struct device *dev;
9095 int ret, i;
9096
9097 dev = ice_pf_to_dev(pf);
9098 num_tcf = mqprio_qopt->qopt.num_tc;
9099 hw = mqprio_qopt->qopt.hw;
9100 mode = mqprio_qopt->mode;
9101 if (!hw) {
9102 clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
9103 vsi->ch_rss_size = 0;
9104 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
9105 goto config_tcf;
9106 }
9107
9108 /* Generate queue region map for number of TCF requested */
9109 for (i = 0; i < num_tcf; i++)
9110 ena_tc_qdisc |= BIT(i);
9111
9112 switch (mode) {
9113 case TC_MQPRIO_MODE_CHANNEL:
9114
Michal Wilczynski80fe30a2022-11-15 11:48:23 +01009115 if (pf->hw.port_info->is_custom_tx_enabled) {
9116 dev_err(dev, "Custom Tx scheduler feature enabled, can't configure ADQ\n");
9117 return -EBUSY;
9118 }
9119 ice_tear_down_devlink_rate_tree(pf);
9120
Kiran Patilfbc7b272021-10-15 16:35:16 -07009121 ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
9122 if (ret) {
9123 netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
9124 ret);
9125 return ret;
9126 }
9127 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
9128 set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
Kiran Patil9fea7492021-10-15 16:35:17 -07009129 /* don't assume state of hw_tc_offload during driver load
9130 * and set the flag for TC flower filter if hw_tc_offload
9131 * already ON
9132 */
9133 if (vsi->netdev->features & NETIF_F_HW_TC)
9134 set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
Kiran Patilfbc7b272021-10-15 16:35:16 -07009135 break;
9136 default:
9137 return -EINVAL;
9138 }
9139
9140config_tcf:
9141
9142 /* Requesting same TCF configuration as already enabled */
9143 if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
9144 mode != TC_MQPRIO_MODE_CHANNEL)
9145 return 0;
9146
9147 /* Pause VSI queues */
9148 ice_dis_vsi(vsi, true);
9149
9150 if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
9151 ice_remove_q_channels(vsi, true);
9152
9153 if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
9154 vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
9155 num_online_cpus());
9156 vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
9157 num_online_cpus());
9158 } else {
9159 /* logic to rebuild VSI, same like ethtool -L */
9160 u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
9161
9162 for (i = 0; i < num_tcf; i++) {
9163 if (!(ena_tc_qdisc & BIT(i)))
9164 continue;
9165
9166 offset = vsi->mqprio_qopt.qopt.offset[i];
9167 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
9168 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
9169 }
9170 vsi->req_txq = offset + qcount_tx;
9171 vsi->req_rxq = offset + qcount_rx;
9172
9173 /* store away original rss_size info, so that it gets reused
9174 * form ice_vsi_rebuild during tc-qdisc delete stage - to
9175 * determine, what should be the rss_sizefor main VSI
9176 */
9177 vsi->orig_rss_size = vsi->rss_size;
9178 }
9179
9180 /* save current values of Tx and Rx queues before calling VSI rebuild
9181 * for fallback option
9182 */
9183 cur_txq = vsi->num_txq;
9184 cur_rxq = vsi->num_rxq;
9185
9186 /* proceed with rebuild main VSI using correct number of queues */
Michal Swiatkowski6624e782022-12-21 12:38:16 +01009187 ret = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
Kiran Patilfbc7b272021-10-15 16:35:16 -07009188 if (ret) {
9189 /* fallback to current number of queues */
9190 dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
9191 vsi->req_txq = cur_txq;
9192 vsi->req_rxq = cur_rxq;
9193 clear_bit(ICE_RESET_FAILED, pf->state);
Michal Swiatkowski6624e782022-12-21 12:38:16 +01009194 if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT)) {
Kiran Patilfbc7b272021-10-15 16:35:16 -07009195 dev_err(dev, "Rebuild of main VSI failed again\n");
9196 return ret;
9197 }
9198 }
9199
9200 vsi->all_numtc = num_tcf;
9201 vsi->all_enatc = ena_tc_qdisc;
9202 ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
9203 if (ret) {
9204 netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
9205 vsi->vsi_num);
9206 goto exit;
9207 }
9208
9209 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
9210 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9211 u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
9212
9213 /* set TC0 rate limit if specified */
9214 if (max_tx_rate || min_tx_rate) {
9215 /* convert to Kbits/s */
9216 if (max_tx_rate)
9217 max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
9218 if (min_tx_rate)
9219 min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
9220
9221 ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
9222 if (!ret) {
9223 dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
9224 max_tx_rate, min_tx_rate, vsi->vsi_num);
9225 } else {
9226 dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
9227 max_tx_rate, min_tx_rate, vsi->vsi_num);
9228 goto exit;
9229 }
9230 }
9231 ret = ice_create_q_channels(vsi);
9232 if (ret) {
9233 netdev_err(netdev, "failed configuring queue channels\n");
9234 goto exit;
9235 } else {
9236 netdev_dbg(netdev, "successfully configured channels\n");
9237 }
9238 }
9239
9240 if (vsi->ch_rss_size)
9241 ice_vsi_cfg_rss_lut_key(vsi);
9242
9243exit:
9244 /* if error, reset the all_numtc and all_enatc */
9245 if (ret) {
9246 vsi->all_numtc = 0;
9247 vsi->all_enatc = 0;
9248 }
9249 /* resume VSI */
9250 ice_ena_vsi(vsi, true);
9251
9252 return ret;
9253}
9254
Kiran Patil0d08a442021-08-06 10:49:05 +02009255static LIST_HEAD(ice_block_cb_list);
9256
9257static int
9258ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
9259 void *type_data)
9260{
9261 struct ice_netdev_priv *np = netdev_priv(netdev);
Kiran Patilfbc7b272021-10-15 16:35:16 -07009262 struct ice_pf *pf = np->vsi->back;
Rafal Rogalski4b31fd42023-07-28 10:12:43 -07009263 bool locked = false;
Kiran Patilfbc7b272021-10-15 16:35:16 -07009264 int err;
Kiran Patil0d08a442021-08-06 10:49:05 +02009265
9266 switch (type) {
9267 case TC_SETUP_BLOCK:
9268 return flow_block_cb_setup_simple(type_data,
9269 &ice_block_cb_list,
9270 ice_setup_tc_block_cb,
9271 np, np, true);
Kiran Patilfbc7b272021-10-15 16:35:16 -07009272 case TC_SETUP_QDISC_MQPRIO:
Marcin Szycik43d00e12023-08-16 12:34:05 -07009273 if (ice_is_eswitch_mode_switchdev(pf)) {
9274 netdev_err(netdev, "TC MQPRIO offload not supported, switchdev is enabled\n");
9275 return -EOPNOTSUPP;
9276 }
9277
Rafal Rogalski4b31fd42023-07-28 10:12:43 -07009278 if (pf->adev) {
9279 mutex_lock(&pf->adev_mutex);
9280 device_lock(&pf->adev->dev);
9281 locked = true;
9282 if (pf->adev->dev.driver) {
9283 netdev_err(netdev, "Cannot change qdisc when RDMA is active\n");
9284 err = -EBUSY;
9285 goto adev_unlock;
9286 }
9287 }
9288
Kiran Patilfbc7b272021-10-15 16:35:16 -07009289 /* setup traffic classifier for receive side */
9290 mutex_lock(&pf->tc_mutex);
9291 err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
9292 mutex_unlock(&pf->tc_mutex);
Rafal Rogalski4b31fd42023-07-28 10:12:43 -07009293
9294adev_unlock:
9295 if (locked) {
9296 device_unlock(&pf->adev->dev);
9297 mutex_unlock(&pf->adev_mutex);
9298 }
Kiran Patilfbc7b272021-10-15 16:35:16 -07009299 return err;
Kiran Patil0d08a442021-08-06 10:49:05 +02009300 default:
9301 return -EOPNOTSUPP;
9302 }
9303 return -EOPNOTSUPP;
9304}
9305
Michal Swiatkowski195bb482021-10-12 11:31:03 -07009306static struct ice_indr_block_priv *
9307ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
9308 struct net_device *netdev)
9309{
9310 struct ice_indr_block_priv *cb_priv;
9311
9312 list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
9313 if (!cb_priv->netdev)
9314 return NULL;
9315 if (cb_priv->netdev == netdev)
9316 return cb_priv;
9317 }
9318 return NULL;
9319}
9320
9321static int
9322ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
9323 void *indr_priv)
9324{
9325 struct ice_indr_block_priv *priv = indr_priv;
9326 struct ice_netdev_priv *np = priv->np;
9327
9328 switch (type) {
9329 case TC_SETUP_CLSFLOWER:
9330 return ice_setup_tc_cls_flower(np, priv->netdev,
9331 (struct flow_cls_offload *)
9332 type_data);
9333 default:
9334 return -EOPNOTSUPP;
9335 }
9336}
9337
9338static int
9339ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
9340 struct ice_netdev_priv *np,
9341 struct flow_block_offload *f, void *data,
9342 void (*cleanup)(struct flow_block_cb *block_cb))
9343{
9344 struct ice_indr_block_priv *indr_priv;
9345 struct flow_block_cb *block_cb;
9346
Michal Swiatkowski9e300982021-10-12 11:31:04 -07009347 if (!ice_is_tunnel_supported(netdev) &&
9348 !(is_vlan_dev(netdev) &&
9349 vlan_dev_real_dev(netdev) == np->vsi->netdev))
9350 return -EOPNOTSUPP;
9351
Michal Swiatkowski195bb482021-10-12 11:31:03 -07009352 if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
9353 return -EOPNOTSUPP;
9354
9355 switch (f->command) {
9356 case FLOW_BLOCK_BIND:
9357 indr_priv = ice_indr_block_priv_lookup(np, netdev);
9358 if (indr_priv)
9359 return -EEXIST;
9360
9361 indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
9362 if (!indr_priv)
9363 return -ENOMEM;
9364
9365 indr_priv->netdev = netdev;
9366 indr_priv->np = np;
9367 list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
9368
9369 block_cb =
9370 flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
9371 indr_priv, indr_priv,
9372 ice_rep_indr_tc_block_unbind,
9373 f, netdev, sch, data, np,
9374 cleanup);
9375
9376 if (IS_ERR(block_cb)) {
9377 list_del(&indr_priv->list);
9378 kfree(indr_priv);
9379 return PTR_ERR(block_cb);
9380 }
9381 flow_block_cb_add(block_cb, f);
9382 list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
9383 break;
9384 case FLOW_BLOCK_UNBIND:
9385 indr_priv = ice_indr_block_priv_lookup(np, netdev);
9386 if (!indr_priv)
9387 return -ENOENT;
9388
9389 block_cb = flow_block_cb_lookup(f->block,
9390 ice_indr_setup_block_cb,
9391 indr_priv);
9392 if (!block_cb)
9393 return -ENOENT;
9394
9395 flow_indr_block_cb_remove(block_cb, f);
9396
9397 list_del(&block_cb->driver_list);
9398 break;
9399 default:
9400 return -EOPNOTSUPP;
9401 }
9402 return 0;
9403}
9404
9405static int
9406ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
9407 void *cb_priv, enum tc_setup_type type, void *type_data,
9408 void *data,
9409 void (*cleanup)(struct flow_block_cb *block_cb))
9410{
9411 switch (type) {
9412 case TC_SETUP_BLOCK:
9413 return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
9414 data, cleanup);
9415
9416 default:
9417 return -EOPNOTSUPP;
9418 }
9419}
9420
Kiran Patil0d08a442021-08-06 10:49:05 +02009421/**
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009422 * ice_open - Called when a network interface becomes active
9423 * @netdev: network interface device structure
9424 *
9425 * The open entry point is called when a network interface is made
Anirudh Venkataramanandf17b7e2018-10-26 11:44:46 -07009426 * active by the system (IFF_UP). At this point all resources needed
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009427 * for transmit and receive operations are allocated, the interrupt
9428 * handler is registered with the OS, the netdev watchdog is enabled,
9429 * and the stack is notified that the interface is ready.
9430 *
9431 * Returns 0 on success, negative value on failure
9432 */
Anirudh Venkataramanan0e674ae2019-04-16 10:30:43 -07009433int ice_open(struct net_device *netdev)
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009434{
9435 struct ice_netdev_priv *np = netdev_priv(netdev);
Krzysztof Gorecznye95fc852021-02-26 13:19:26 -08009436 struct ice_pf *pf = np->vsi->back;
9437
9438 if (ice_is_reset_in_progress(pf->state)) {
9439 netdev_err(netdev, "can't open net device while reset is in progress");
9440 return -EBUSY;
9441 }
9442
9443 return ice_open_internal(netdev);
9444}
9445
9446/**
9447 * ice_open_internal - Called when a network interface becomes active
9448 * @netdev: network interface device structure
9449 *
9450 * Internal ice_open implementation. Should not be used directly except for ice_open and reset
9451 * handling routine
9452 *
9453 * Returns 0 on success, negative value on failure
9454 */
9455int ice_open_internal(struct net_device *netdev)
9456{
9457 struct ice_netdev_priv *np = netdev_priv(netdev);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009458 struct ice_vsi *vsi = np->vsi;
Anirudh Venkataramanande751352020-05-07 17:41:03 -07009459 struct ice_pf *pf = vsi->back;
Tony Nguyen6d599942019-06-26 02:20:17 -07009460 struct ice_port_info *pi;
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009461 int err;
9462
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08009463 if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
Anirudh Venkataramanan0f9d5022018-08-09 06:29:50 -07009464 netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
9465 return -EIO;
9466 }
9467
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009468 netif_carrier_off(netdev);
9469
Tony Nguyen6d599942019-06-26 02:20:17 -07009470 pi = vsi->port_info;
Tony Nguyen2ccc1c12021-10-07 16:00:23 -07009471 err = ice_update_link_info(pi);
9472 if (err) {
9473 netdev_err(netdev, "Failed to get link info, error %d\n", err);
Tony Nguyenc1484692021-10-07 16:01:58 -07009474 return err;
Brett Creeleyb6f934f2018-12-19 10:03:25 -08009475 }
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009476
Brett Creeley99d40752021-10-13 09:02:19 -07009477 ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
Anirudh Venkataramananc77849f52021-05-06 08:40:01 -07009478
Tony Nguyen6d599942019-06-26 02:20:17 -07009479 /* Set PHY if there is media, otherwise, turn off PHY */
9480 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07009481 clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
Anirudh Venkataramanan7e408e02021-03-02 10:15:38 -08009482 if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07009483 err = ice_init_phy_user_cfg(pi);
9484 if (err) {
9485 netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
9486 err);
9487 return err;
9488 }
9489 }
9490
9491 err = ice_configure_phy(vsi);
Tony Nguyen6d599942019-06-26 02:20:17 -07009492 if (err) {
Anirudh Venkataramanan19cce2c2020-02-06 01:20:10 -08009493 netdev_err(netdev, "Failed to set physical link up, error %d\n",
Tony Nguyen6d599942019-06-26 02:20:17 -07009494 err);
9495 return err;
9496 }
9497 } else {
Paul Greenwalt1a3571b2020-07-09 09:16:06 -07009498 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
Anirudh Venkataramanand348d512021-03-25 15:35:07 -07009499 ice_set_link(vsi, false);
Tony Nguyen6d599942019-06-26 02:20:17 -07009500 }
9501
Brett Creeleyb6f934f2018-12-19 10:03:25 -08009502 err = ice_vsi_open(vsi);
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009503 if (err)
9504 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
9505 vsi->vsi_num, vsi->vsw->sw_id);
Tony Nguyena4e82a82020-05-06 09:32:30 -07009506
9507 /* Update existing tunnels information */
9508 udp_tunnel_get_rx_info(netdev);
9509
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009510 return err;
9511}
9512
9513/**
9514 * ice_stop - Disables a network interface
9515 * @netdev: network interface device structure
9516 *
9517 * The stop entry point is called when an interface is de-activated by the OS,
Anirudh Venkataramanandf17b7e2018-10-26 11:44:46 -07009518 * and the netdevice enters the DOWN state. The hardware is still under the
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009519 * driver's control, but the netdev interface is disabled.
9520 *
9521 * Returns success only - not allowed to fail
9522 */
Anirudh Venkataramanan0e674ae2019-04-16 10:30:43 -07009523int ice_stop(struct net_device *netdev)
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009524{
9525 struct ice_netdev_priv *np = netdev_priv(netdev);
9526 struct ice_vsi *vsi = np->vsi;
Krzysztof Gorecznye95fc852021-02-26 13:19:26 -08009527 struct ice_pf *pf = vsi->back;
9528
9529 if (ice_is_reset_in_progress(pf->state)) {
9530 netdev_err(netdev, "can't stop net device while reset is in progress");
9531 return -EBUSY;
9532 }
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009533
Mateusz Palczewski8ac71322022-08-26 10:31:23 +02009534 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
9535 int link_err = ice_force_phys_link_state(vsi, false);
9536
9537 if (link_err) {
Katarzyna Wieczerzycka6a8d8bb52023-12-15 12:01:56 +01009538 if (link_err == -ENOMEDIUM)
9539 netdev_info(vsi->netdev, "Skipping link reconfig - no media attached, VSI %d\n",
9540 vsi->vsi_num);
9541 else
9542 netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
9543 vsi->vsi_num, link_err);
Ngai-Mint Kwan6d05ff52023-12-15 12:01:57 +01009544
9545 ice_vsi_close(vsi);
Mateusz Palczewski8ac71322022-08-26 10:31:23 +02009546 return -EIO;
9547 }
9548 }
9549
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009550 ice_vsi_close(vsi);
9551
9552 return 0;
9553}
9554
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07009555/**
9556 * ice_features_check - Validate encapsulated packet conforms to limits
9557 * @skb: skb buffer
9558 * @netdev: This port's netdev
9559 * @features: Offload features that the stack believes apply
9560 */
9561static netdev_features_t
9562ice_features_check(struct sk_buff *skb,
9563 struct net_device __always_unused *netdev,
9564 netdev_features_t features)
9565{
Jesse Brandeburg46b699c2022-01-14 15:38:39 -08009566 bool gso = skb_is_gso(skb);
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07009567 size_t len;
9568
9569 /* No point in doing any of this if neither checksum nor GSO are
Anirudh Venkataramanandf17b7e2018-10-26 11:44:46 -07009570 * being requested for this frame. We can rule out both by just
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07009571 * checking for CHECKSUM_PARTIAL
9572 */
9573 if (skb->ip_summed != CHECKSUM_PARTIAL)
9574 return features;
9575
9576 /* We cannot support GSO if the MSS is going to be less than
Anirudh Venkataramanandf17b7e2018-10-26 11:44:46 -07009577 * 64 bytes. If it is then we need to drop support for GSO.
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07009578 */
Jesse Brandeburg46b699c2022-01-14 15:38:39 -08009579 if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07009580 features &= ~NETIF_F_GSO_MASK;
9581
Jesse Brandeburg46b699c2022-01-14 15:38:39 -08009582 len = skb_network_offset(skb);
Tony Nguyena4e82a82020-05-06 09:32:30 -07009583 if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07009584 goto out_rm_features;
9585
Jesse Brandeburg46b699c2022-01-14 15:38:39 -08009586 len = skb_network_header_len(skb);
Tony Nguyena4e82a82020-05-06 09:32:30 -07009587 if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07009588 goto out_rm_features;
9589
9590 if (skb->encapsulation) {
Jesse Brandeburg46b699c2022-01-14 15:38:39 -08009591 /* this must work for VXLAN frames AND IPIP/SIT frames, and in
9592 * the case of IPIP frames, the transport header pointer is
9593 * after the inner header! So check to make sure that this
9594 * is a GRE or UDP_TUNNEL frame before doing that math.
9595 */
9596 if (gso && (skb_shinfo(skb)->gso_type &
9597 (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
9598 len = skb_inner_network_header(skb) -
9599 skb_transport_header(skb);
9600 if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
9601 goto out_rm_features;
9602 }
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07009603
Jesse Brandeburg46b699c2022-01-14 15:38:39 -08009604 len = skb_inner_network_header_len(skb);
Tony Nguyena4e82a82020-05-06 09:32:30 -07009605 if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07009606 goto out_rm_features;
9607 }
9608
9609 return features;
9610out_rm_features:
9611 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
9612}
9613
Tony Nguyen462acf6a2019-09-09 06:47:46 -07009614static const struct net_device_ops ice_netdev_safe_mode_ops = {
9615 .ndo_open = ice_open,
9616 .ndo_stop = ice_stop,
9617 .ndo_start_xmit = ice_start_xmit,
9618 .ndo_set_mac_address = ice_set_mac_address,
9619 .ndo_validate_addr = eth_validate_addr,
9620 .ndo_change_mtu = ice_change_mtu,
9621 .ndo_get_stats64 = ice_get_stats64,
9622 .ndo_tx_timeout = ice_tx_timeout,
Maciej Fijalkowskiebc53992021-05-20 08:34:59 +02009623 .ndo_bpf = ice_xdp_safe_mode,
Tony Nguyen462acf6a2019-09-09 06:47:46 -07009624};
9625
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009626static const struct net_device_ops ice_netdev_ops = {
9627 .ndo_open = ice_open,
9628 .ndo_stop = ice_stop,
Anirudh Venkataramanan2b245cb2018-03-20 07:58:14 -07009629 .ndo_start_xmit = ice_start_xmit,
Dave Ertman2a87bd72021-08-06 13:53:56 -07009630 .ndo_select_queue = ice_select_queue,
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07009631 .ndo_features_check = ice_features_check,
Brett Creeley1babaf72021-12-02 08:38:50 -08009632 .ndo_fix_features = ice_fix_features,
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07009633 .ndo_set_rx_mode = ice_set_rx_mode,
9634 .ndo_set_mac_address = ice_set_mac_address,
9635 .ndo_validate_addr = eth_validate_addr,
9636 .ndo_change_mtu = ice_change_mtu,
Anirudh Venkataramananfcea6f32018-03-20 07:58:16 -07009637 .ndo_get_stats64 = ice_get_stats64,
Usha Ketineni1ddef452019-11-06 02:05:28 -08009638 .ndo_set_tx_maxrate = ice_set_tx_maxrate,
Arnd Bergmanna7605372021-07-27 15:45:13 +02009639 .ndo_eth_ioctl = ice_eth_ioctl,
Anirudh Venkataramanan7c710862018-09-19 17:42:58 -07009640 .ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
9641 .ndo_set_vf_mac = ice_set_vf_mac,
9642 .ndo_get_vf_config = ice_get_vf_cfg,
9643 .ndo_set_vf_trust = ice_set_vf_trust,
9644 .ndo_set_vf_vlan = ice_set_vf_port_vlan,
9645 .ndo_set_vf_link_state = ice_set_vf_link_state,
Jesse Brandeburg730fdea2019-11-08 06:23:28 -08009646 .ndo_get_vf_stats = ice_get_vf_stats,
Brett Creeley4ecc8632021-09-13 11:22:19 -07009647 .ndo_set_vf_rate = ice_set_vf_bw,
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07009648 .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
9649 .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
Kiran Patil0d08a442021-08-06 10:49:05 +02009650 .ndo_setup_tc = ice_setup_tc,
Anirudh Venkataramanand76a60b2018-03-20 07:58:15 -07009651 .ndo_set_features = ice_set_features,
Md Fahad Iqbal Polashb1edc142018-08-09 06:29:54 -07009652 .ndo_bridge_getlink = ice_bridge_getlink,
9653 .ndo_bridge_setlink = ice_bridge_setlink,
Anirudh Venkataramanane94d4472018-03-20 07:58:19 -07009654 .ndo_fdb_add = ice_fdb_add,
9655 .ndo_fdb_del = ice_fdb_del,
Brett Creeley28bf2672020-05-11 18:01:46 -07009656#ifdef CONFIG_RFS_ACCEL
9657 .ndo_rx_flow_steer = ice_rx_flow_steer,
9658#endif
Sudheer Mogilappagarib3969fd2018-08-09 06:29:53 -07009659 .ndo_tx_timeout = ice_tx_timeout,
Maciej Fijalkowskiefc22142019-11-04 09:38:56 -08009660 .ndo_bpf = ice_xdp,
9661 .ndo_xdp_xmit = ice_xdp_xmit,
Krzysztof Kazimierczak2d4238f2019-11-04 09:38:56 -08009662 .ndo_xsk_wakeup = ice_xsk_wakeup,
Anirudh Venkataramanancdedef52018-03-20 07:58:13 -07009663};