1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018-2023, Intel Corporation. */
3
4 /* Intel(R) Ethernet Connection E800 Series Linux Driver */
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 #include "ice.h"
11 #include "ice_base.h"
12 #include "ice_lib.h"
13 #include "ice_fltr.h"
14 #include "ice_dcb_lib.h"
15 #include "ice_dcb_nl.h"
16 #include "ice_devlink.h"
17 /* Including ice_trace.h with CREATE_TRACE_POINTS defined will generate the
18 * ice tracepoint functions. This must be done exactly once across the
19 * ice driver.
20 */
21 #define CREATE_TRACE_POINTS
22 #include "ice_trace.h"
23 #include "ice_eswitch.h"
24 #include "ice_tc_lib.h"
25 #include "ice_vsi_vlan_ops.h"
26 #include <net/xdp_sock_drv.h>
27
28 #define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
29 static const char ice_driver_string[] = DRV_SUMMARY;
30 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
31
32 /* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
33 #define ICE_DDP_PKG_PATH "intel/ice/ddp/"
34 #define ICE_DDP_PKG_FILE ICE_DDP_PKG_PATH "ice.pkg"
35
36 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
37 MODULE_DESCRIPTION(DRV_SUMMARY);
38 MODULE_LICENSE("GPL v2");
39 MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
40
41 static int debug = -1;
42 module_param(debug, int, 0644);
43 #ifndef CONFIG_DYNAMIC_DEBUG
44 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
45 #else
46 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
47 #endif /* !CONFIG_DYNAMIC_DEBUG */
48
49 DEFINE_STATIC_KEY_FALSE(ice_xdp_locking_key);
50 EXPORT_SYMBOL(ice_xdp_locking_key);
51
52 /**
53 * ice_hw_to_dev - Get device pointer from the hardware structure
54 * @hw: pointer to the device HW structure
55 *
56 * Used to access the device pointer from compilation units which can't easily
57 * include the definition of struct ice_pf without leading to circular header
58 * dependencies.
59 */
ice_hw_to_dev(struct ice_hw * hw)60 struct device *ice_hw_to_dev(struct ice_hw *hw)
61 {
62 struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
63
64 return &pf->pdev->dev;
65 }
66
67 static struct workqueue_struct *ice_wq;
68 struct workqueue_struct *ice_lag_wq;
69 static const struct net_device_ops ice_netdev_safe_mode_ops;
70 static const struct net_device_ops ice_netdev_ops;
71
72 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
73
74 static void ice_vsi_release_all(struct ice_pf *pf);
75
76 static int ice_rebuild_channels(struct ice_pf *pf);
77 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_adv_fltr);
78
79 static int
80 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
81 void *cb_priv, enum tc_setup_type type, void *type_data,
82 void *data,
83 void (*cleanup)(struct flow_block_cb *block_cb));
84
netif_is_ice(const struct net_device * dev)85 bool netif_is_ice(const struct net_device *dev)
86 {
87 return dev && (dev->netdev_ops == &ice_netdev_ops ||
88 dev->netdev_ops == &ice_netdev_safe_mode_ops);
89 }
90
91 /**
92 * ice_get_tx_pending - returns number of Tx descriptors not processed
93 * @ring: the ring of descriptors
94 */
ice_get_tx_pending(struct ice_tx_ring * ring)95 static u16 ice_get_tx_pending(struct ice_tx_ring *ring)
96 {
97 u16 head, tail;
98
99 head = ring->next_to_clean;
100 tail = ring->next_to_use;
101
102 if (head != tail)
103 return (head < tail) ?
104 tail - head : (tail + ring->count - head);
105 return 0;
106 }
107
108 /**
109 * ice_check_for_hang_subtask - check for and recover hung queues
110 * @pf: pointer to PF struct
111 */
ice_check_for_hang_subtask(struct ice_pf * pf)112 static void ice_check_for_hang_subtask(struct ice_pf *pf)
113 {
114 struct ice_vsi *vsi = NULL;
115 struct ice_hw *hw;
116 unsigned int i;
117 int packets;
118 u32 v;
119
120 ice_for_each_vsi(pf, v)
121 if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
122 vsi = pf->vsi[v];
123 break;
124 }
125
126 if (!vsi || test_bit(ICE_VSI_DOWN, vsi->state))
127 return;
128
129 if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
130 return;
131
132 hw = &vsi->back->hw;
133
134 ice_for_each_txq(vsi, i) {
135 struct ice_tx_ring *tx_ring = vsi->tx_rings[i];
136 struct ice_ring_stats *ring_stats;
137
138 if (!tx_ring)
139 continue;
140 if (ice_ring_ch_enabled(tx_ring))
141 continue;
142
143 ring_stats = tx_ring->ring_stats;
144 if (!ring_stats)
145 continue;
146
147 if (tx_ring->desc) {
148 /* If packet counter has not changed the queue is
149 * likely stalled, so force an interrupt for this
150 * queue.
151 *
152 * prev_pkt would be negative if there was no
153 * pending work.
154 */
155 packets = ring_stats->stats.pkts & INT_MAX;
156 if (ring_stats->tx_stats.prev_pkt == packets) {
157 /* Trigger sw interrupt to revive the queue */
158 ice_trigger_sw_intr(hw, tx_ring->q_vector);
159 continue;
160 }
161
162 /* Memory barrier between read of packet count and call
163 * to ice_get_tx_pending()
164 */
165 smp_rmb();
166 ring_stats->tx_stats.prev_pkt =
167 ice_get_tx_pending(tx_ring) ? packets : -1;
168 }
169 }
170 }
171
172 /**
173 * ice_init_mac_fltr - Set initial MAC filters
174 * @pf: board private structure
175 *
176 * Set initial set of MAC filters for PF VSI; configure filters for permanent
177 * address and broadcast address. If an error is encountered, netdevice will be
178 * unregistered.
179 */
ice_init_mac_fltr(struct ice_pf * pf)180 static int ice_init_mac_fltr(struct ice_pf *pf)
181 {
182 struct ice_vsi *vsi;
183 u8 *perm_addr;
184
185 vsi = ice_get_main_vsi(pf);
186 if (!vsi)
187 return -EINVAL;
188
189 perm_addr = vsi->port_info->mac.perm_addr;
190 return ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
191 }
192
193 /**
194 * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
195 * @netdev: the net device on which the sync is happening
196 * @addr: MAC address to sync
197 *
198 * This is a callback function which is called by the in kernel device sync
199 * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
200 * populates the tmp_sync_list, which is later used by ice_add_mac to add the
201 * MAC filters from the hardware.
202 */
ice_add_mac_to_sync_list(struct net_device * netdev,const u8 * addr)203 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
204 {
205 struct ice_netdev_priv *np = netdev_priv(netdev);
206 struct ice_vsi *vsi = np->vsi;
207
208 if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
209 ICE_FWD_TO_VSI))
210 return -EINVAL;
211
212 return 0;
213 }
214
215 /**
216 * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
217 * @netdev: the net device on which the unsync is happening
218 * @addr: MAC address to unsync
219 *
220 * This is a callback function which is called by the in kernel device unsync
221 * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
222 * populates the tmp_unsync_list, which is later used by ice_remove_mac to
223 * delete the MAC filters from the hardware.
224 */
ice_add_mac_to_unsync_list(struct net_device * netdev,const u8 * addr)225 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
226 {
227 struct ice_netdev_priv *np = netdev_priv(netdev);
228 struct ice_vsi *vsi = np->vsi;
229
230 /* Under some circumstances, we might receive a request to delete our
231 * own device address from our uc list. Because we store the device
232 * address in the VSI's MAC filter list, we need to ignore such
233 * requests and not delete our device address from this list.
234 */
235 if (ether_addr_equal(addr, netdev->dev_addr))
236 return 0;
237
238 if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
239 ICE_FWD_TO_VSI))
240 return -EINVAL;
241
242 return 0;
243 }
244
245 /**
246 * ice_vsi_fltr_changed - check if filter state changed
247 * @vsi: VSI to be checked
248 *
249 * returns true if filter state has changed, false otherwise.
250 */
ice_vsi_fltr_changed(struct ice_vsi * vsi)251 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
252 {
253 return test_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state) ||
254 test_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
255 }
256
257 /**
258 * ice_set_promisc - Enable promiscuous mode for a given PF
259 * @vsi: the VSI being configured
260 * @promisc_m: mask of promiscuous config bits
261 *
262 */
ice_set_promisc(struct ice_vsi * vsi,u8 promisc_m)263 static int ice_set_promisc(struct ice_vsi *vsi, u8 promisc_m)
264 {
265 int status;
266
267 if (vsi->type != ICE_VSI_PF)
268 return 0;
269
270 if (ice_vsi_has_non_zero_vlans(vsi)) {
271 promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
272 status = ice_fltr_set_vlan_vsi_promisc(&vsi->back->hw, vsi,
273 promisc_m);
274 } else {
275 status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
276 promisc_m, 0);
277 }
278 if (status && status != -EEXIST)
279 return status;
280
281 netdev_dbg(vsi->netdev, "set promisc filter bits for VSI %i: 0x%x\n",
282 vsi->vsi_num, promisc_m);
283 return 0;
284 }
285
286 /**
287 * ice_clear_promisc - Disable promiscuous mode for a given PF
288 * @vsi: the VSI being configured
289 * @promisc_m: mask of promiscuous config bits
290 *
291 */
ice_clear_promisc(struct ice_vsi * vsi,u8 promisc_m)292 static int ice_clear_promisc(struct ice_vsi *vsi, u8 promisc_m)
293 {
294 int status;
295
296 if (vsi->type != ICE_VSI_PF)
297 return 0;
298
299 if (ice_vsi_has_non_zero_vlans(vsi)) {
300 promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
301 status = ice_fltr_clear_vlan_vsi_promisc(&vsi->back->hw, vsi,
302 promisc_m);
303 } else {
304 status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
305 promisc_m, 0);
306 }
307
308 netdev_dbg(vsi->netdev, "clear promisc filter bits for VSI %i: 0x%x\n",
309 vsi->vsi_num, promisc_m);
310 return status;
311 }
312
313 /**
314 * ice_vsi_sync_fltr - Update the VSI filter list to the HW
315 * @vsi: ptr to the VSI
316 *
317 * Push any outstanding VSI filter changes through the AdminQ.
318 */
ice_vsi_sync_fltr(struct ice_vsi * vsi)319 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
320 {
321 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
322 struct device *dev = ice_pf_to_dev(vsi->back);
323 struct net_device *netdev = vsi->netdev;
324 bool promisc_forced_on = false;
325 struct ice_pf *pf = vsi->back;
326 struct ice_hw *hw = &pf->hw;
327 u32 changed_flags = 0;
328 int err;
329
330 if (!vsi->netdev)
331 return -EINVAL;
332
333 while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
334 usleep_range(1000, 2000);
335
336 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
337 vsi->current_netdev_flags = vsi->netdev->flags;
338
339 INIT_LIST_HEAD(&vsi->tmp_sync_list);
340 INIT_LIST_HEAD(&vsi->tmp_unsync_list);
341
342 if (ice_vsi_fltr_changed(vsi)) {
343 clear_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
344 clear_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
345
346 /* grab the netdev's addr_list_lock */
347 netif_addr_lock_bh(netdev);
348 __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
349 ice_add_mac_to_unsync_list);
350 __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
351 ice_add_mac_to_unsync_list);
352 /* our temp lists are populated. release lock */
353 netif_addr_unlock_bh(netdev);
354 }
355
356 /* Remove MAC addresses in the unsync list */
357 err = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
358 ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
359 if (err) {
360 netdev_err(netdev, "Failed to delete MAC filters\n");
361 /* if we failed because of alloc failures, just bail */
362 if (err == -ENOMEM)
363 goto out;
364 }
365
366 /* Add MAC addresses in the sync list */
367 err = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
368 ice_fltr_free_list(dev, &vsi->tmp_sync_list);
369 /* If filter is added successfully or already exists, do not go into
370 * 'if' condition and report it as error. Instead continue processing
371 * rest of the function.
372 */
373 if (err && err != -EEXIST) {
374 netdev_err(netdev, "Failed to add MAC filters\n");
375 /* If there is no more space for new umac filters, VSI
376 * should go into promiscuous mode. There should be some
377 * space reserved for promiscuous filters.
378 */
379 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
380 !test_and_set_bit(ICE_FLTR_OVERFLOW_PROMISC,
381 vsi->state)) {
382 promisc_forced_on = true;
383 netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
384 vsi->vsi_num);
385 } else {
386 goto out;
387 }
388 }
389 err = 0;
390 /* check for changes in promiscuous modes */
391 if (changed_flags & IFF_ALLMULTI) {
392 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
393 err = ice_set_promisc(vsi, ICE_MCAST_PROMISC_BITS);
394 if (err) {
395 vsi->current_netdev_flags &= ~IFF_ALLMULTI;
396 goto out_promisc;
397 }
398 } else {
399 /* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
400 err = ice_clear_promisc(vsi, ICE_MCAST_PROMISC_BITS);
401 if (err) {
402 vsi->current_netdev_flags |= IFF_ALLMULTI;
403 goto out_promisc;
404 }
405 }
406 }
407
408 if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
409 test_bit(ICE_VSI_PROMISC_CHANGED, vsi->state)) {
410 clear_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
411 if (vsi->current_netdev_flags & IFF_PROMISC) {
412 /* Apply Rx filter rule to get traffic from wire */
413 if (!ice_is_dflt_vsi_in_use(vsi->port_info)) {
414 err = ice_set_dflt_vsi(vsi);
415 if (err && err != -EEXIST) {
416 netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
417 err, vsi->vsi_num);
418 vsi->current_netdev_flags &=
419 ~IFF_PROMISC;
420 goto out_promisc;
421 }
422 err = 0;
423 vlan_ops->dis_rx_filtering(vsi);
424
425 /* promiscuous mode implies allmulticast so
426 * that VSIs that are in promiscuous mode are
427 * subscribed to multicast packets coming to
428 * the port
429 */
430 err = ice_set_promisc(vsi,
431 ICE_MCAST_PROMISC_BITS);
432 if (err)
433 goto out_promisc;
434 }
435 } else {
436 /* Clear Rx filter to remove traffic from wire */
437 if (ice_is_vsi_dflt_vsi(vsi)) {
438 err = ice_clear_dflt_vsi(vsi);
439 if (err) {
440 netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
441 err, vsi->vsi_num);
442 vsi->current_netdev_flags |=
443 IFF_PROMISC;
444 goto out_promisc;
445 }
446 if (vsi->netdev->features &
447 NETIF_F_HW_VLAN_CTAG_FILTER)
448 vlan_ops->ena_rx_filtering(vsi);
449 }
450
451 /* disable allmulti here, but only if allmulti is not
452 * still enabled for the netdev
453 */
454 if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) {
455 err = ice_clear_promisc(vsi,
456 ICE_MCAST_PROMISC_BITS);
457 if (err) {
458 netdev_err(netdev, "Error %d clearing multicast promiscuous on VSI %i\n",
459 err, vsi->vsi_num);
460 }
461 }
462 }
463 }
464 goto exit;
465
466 out_promisc:
467 set_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
468 goto exit;
469 out:
470 /* if something went wrong then set the changed flag so we try again */
471 set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
472 set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
473 exit:
474 clear_bit(ICE_CFG_BUSY, vsi->state);
475 return err;
476 }
477
478 /**
479 * ice_sync_fltr_subtask - Sync the VSI filter list with HW
480 * @pf: board private structure
481 */
ice_sync_fltr_subtask(struct ice_pf * pf)482 static void ice_sync_fltr_subtask(struct ice_pf *pf)
483 {
484 int v;
485
486 if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
487 return;
488
489 clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
490
491 ice_for_each_vsi(pf, v)
492 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
493 ice_vsi_sync_fltr(pf->vsi[v])) {
494 /* come back and try again later */
495 set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
496 break;
497 }
498 }
499
500 /**
501 * ice_pf_dis_all_vsi - Pause all VSIs on a PF
502 * @pf: the PF
503 * @locked: is the rtnl_lock already held
504 */
ice_pf_dis_all_vsi(struct ice_pf * pf,bool locked)505 static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
506 {
507 int node;
508 int v;
509
510 ice_for_each_vsi(pf, v)
511 if (pf->vsi[v])
512 ice_dis_vsi(pf->vsi[v], locked);
513
514 for (node = 0; node < ICE_MAX_PF_AGG_NODES; node++)
515 pf->pf_agg_node[node].num_vsis = 0;
516
517 for (node = 0; node < ICE_MAX_VF_AGG_NODES; node++)
518 pf->vf_agg_node[node].num_vsis = 0;
519 }
520
521 /**
522 * ice_prepare_for_reset - prep for reset
523 * @pf: board private structure
524 * @reset_type: reset type requested
525 *
526 * Inform or close all dependent features in prep for reset.
527 */
528 static void
ice_prepare_for_reset(struct ice_pf * pf,enum ice_reset_req reset_type)529 ice_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
530 {
531 struct ice_hw *hw = &pf->hw;
532 struct ice_vsi *vsi;
533 struct ice_vf *vf;
534 unsigned int bkt;
535
536 dev_dbg(ice_pf_to_dev(pf), "reset_type=%d\n", reset_type);
537
538 /* already prepared for reset */
539 if (test_bit(ICE_PREPARED_FOR_RESET, pf->state))
540 return;
541
542 synchronize_irq(pf->oicr_irq.virq);
543
544 ice_unplug_aux_dev(pf);
545
546 /* Notify VFs of impending reset */
547 if (ice_check_sq_alive(hw, &hw->mailboxq))
548 ice_vc_notify_reset(pf);
549
550 /* Disable VFs until reset is completed */
551 mutex_lock(&pf->vfs.table_lock);
552 ice_for_each_vf(pf, bkt, vf)
553 ice_set_vf_state_dis(vf);
554 mutex_unlock(&pf->vfs.table_lock);
555
556 if (ice_is_eswitch_mode_switchdev(pf)) {
557 rtnl_lock();
558 ice_eswitch_br_fdb_flush(pf->eswitch.br_offloads->bridge);
559 rtnl_unlock();
560 }
561
562 /* release ADQ specific HW and SW resources */
563 vsi = ice_get_main_vsi(pf);
564 if (!vsi)
565 goto skip;
566
567 /* to be on safe side, reset orig_rss_size so that normal flow
568 * of deciding rss_size can take precedence
569 */
570 vsi->orig_rss_size = 0;
571
572 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
573 if (reset_type == ICE_RESET_PFR) {
574 vsi->old_ena_tc = vsi->all_enatc;
575 vsi->old_numtc = vsi->all_numtc;
576 } else {
577 ice_remove_q_channels(vsi, true);
578
579 /* for other reset type, do not support channel rebuild
580 * hence reset needed info
581 */
582 vsi->old_ena_tc = 0;
583 vsi->all_enatc = 0;
584 vsi->old_numtc = 0;
585 vsi->all_numtc = 0;
586 vsi->req_txq = 0;
587 vsi->req_rxq = 0;
588 clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
589 memset(&vsi->mqprio_qopt, 0, sizeof(vsi->mqprio_qopt));
590 }
591 }
592
593 if (vsi->netdev)
594 netif_device_detach(vsi->netdev);
595 skip:
596
597 /* clear SW filtering DB */
598 ice_clear_hw_tbls(hw);
599 /* disable the VSIs and their queues that are not already DOWN */
600 set_bit(ICE_VSI_REBUILD_PENDING, ice_get_main_vsi(pf)->state);
601 ice_pf_dis_all_vsi(pf, false);
602
603 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
604 ice_ptp_prepare_for_reset(pf);
605
606 if (ice_is_feature_supported(pf, ICE_F_GNSS))
607 ice_gnss_exit(pf);
608
609 if (hw->port_info)
610 ice_sched_clear_port(hw->port_info);
611
612 ice_shutdown_all_ctrlq(hw);
613
614 set_bit(ICE_PREPARED_FOR_RESET, pf->state);
615 }
616
617 /**
618 * ice_do_reset - Initiate one of many types of resets
619 * @pf: board private structure
620 * @reset_type: reset type requested before this function was called.
621 */
ice_do_reset(struct ice_pf * pf,enum ice_reset_req reset_type)622 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
623 {
624 struct device *dev = ice_pf_to_dev(pf);
625 struct ice_hw *hw = &pf->hw;
626
627 dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
628
629 if (pf->lag && pf->lag->bonded && reset_type == ICE_RESET_PFR) {
630 dev_dbg(dev, "PFR on a bonded interface, promoting to CORER\n");
631 reset_type = ICE_RESET_CORER;
632 }
633
634 ice_prepare_for_reset(pf, reset_type);
635
636 /* trigger the reset */
637 if (ice_reset(hw, reset_type)) {
638 dev_err(dev, "reset %d failed\n", reset_type);
639 set_bit(ICE_RESET_FAILED, pf->state);
640 clear_bit(ICE_RESET_OICR_RECV, pf->state);
641 clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
642 clear_bit(ICE_PFR_REQ, pf->state);
643 clear_bit(ICE_CORER_REQ, pf->state);
644 clear_bit(ICE_GLOBR_REQ, pf->state);
645 wake_up(&pf->reset_wait_queue);
646 return;
647 }
648
649 /* PFR is a bit of a special case because it doesn't result in an OICR
650 * interrupt. So for PFR, rebuild after the reset and clear the reset-
651 * associated state bits.
652 */
653 if (reset_type == ICE_RESET_PFR) {
654 pf->pfr_count++;
655 ice_rebuild(pf, reset_type);
656 clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
657 clear_bit(ICE_PFR_REQ, pf->state);
658 wake_up(&pf->reset_wait_queue);
659 ice_reset_all_vfs(pf);
660 }
661 }
662
663 /**
664 * ice_reset_subtask - Set up for resetting the device and driver
665 * @pf: board private structure
666 */
ice_reset_subtask(struct ice_pf * pf)667 static void ice_reset_subtask(struct ice_pf *pf)
668 {
669 enum ice_reset_req reset_type = ICE_RESET_INVAL;
670
671 /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
672 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
673 * of reset is pending and sets bits in pf->state indicating the reset
674 * type and ICE_RESET_OICR_RECV. So, if the latter bit is set
675 * prepare for pending reset if not already (for PF software-initiated
676 * global resets the software should already be prepared for it as
677 * indicated by ICE_PREPARED_FOR_RESET; for global resets initiated
678 * by firmware or software on other PFs, that bit is not set so prepare
679 * for the reset now), poll for reset done, rebuild and return.
680 */
681 if (test_bit(ICE_RESET_OICR_RECV, pf->state)) {
682 /* Perform the largest reset requested */
683 if (test_and_clear_bit(ICE_CORER_RECV, pf->state))
684 reset_type = ICE_RESET_CORER;
685 if (test_and_clear_bit(ICE_GLOBR_RECV, pf->state))
686 reset_type = ICE_RESET_GLOBR;
687 if (test_and_clear_bit(ICE_EMPR_RECV, pf->state))
688 reset_type = ICE_RESET_EMPR;
689 /* return if no valid reset type requested */
690 if (reset_type == ICE_RESET_INVAL)
691 return;
692 ice_prepare_for_reset(pf, reset_type);
693
694 /* make sure we are ready to rebuild */
695 if (ice_check_reset(&pf->hw)) {
696 set_bit(ICE_RESET_FAILED, pf->state);
697 } else {
698 /* done with reset. start rebuild */
699 pf->hw.reset_ongoing = false;
700 ice_rebuild(pf, reset_type);
701 /* clear bit to resume normal operations, but
702 * ICE_NEEDS_RESTART bit is set in case rebuild failed
703 */
704 clear_bit(ICE_RESET_OICR_RECV, pf->state);
705 clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
706 clear_bit(ICE_PFR_REQ, pf->state);
707 clear_bit(ICE_CORER_REQ, pf->state);
708 clear_bit(ICE_GLOBR_REQ, pf->state);
709 wake_up(&pf->reset_wait_queue);
710 ice_reset_all_vfs(pf);
711 }
712
713 return;
714 }
715
716 /* No pending resets to finish processing. Check for new resets */
717 if (test_bit(ICE_PFR_REQ, pf->state)) {
718 reset_type = ICE_RESET_PFR;
719 if (pf->lag && pf->lag->bonded) {
720 dev_dbg(ice_pf_to_dev(pf), "PFR on a bonded interface, promoting to CORER\n");
721 reset_type = ICE_RESET_CORER;
722 }
723 }
724 if (test_bit(ICE_CORER_REQ, pf->state))
725 reset_type = ICE_RESET_CORER;
726 if (test_bit(ICE_GLOBR_REQ, pf->state))
727 reset_type = ICE_RESET_GLOBR;
728 /* If no valid reset type requested just return */
729 if (reset_type == ICE_RESET_INVAL)
730 return;
731
732 /* reset if not already down or busy */
733 if (!test_bit(ICE_DOWN, pf->state) &&
734 !test_bit(ICE_CFG_BUSY, pf->state)) {
735 ice_do_reset(pf, reset_type);
736 }
737 }
738
739 /**
740 * ice_print_topo_conflict - print topology conflict message
741 * @vsi: the VSI whose topology status is being checked
742 */
ice_print_topo_conflict(struct ice_vsi * vsi)743 static void ice_print_topo_conflict(struct ice_vsi *vsi)
744 {
745 switch (vsi->port_info->phy.link_info.topo_media_conflict) {
746 case ICE_AQ_LINK_TOPO_CONFLICT:
747 case ICE_AQ_LINK_MEDIA_CONFLICT:
748 case ICE_AQ_LINK_TOPO_UNREACH_PRT:
749 case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
750 case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
751 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");
752 break;
753 case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
754 if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, vsi->back->flags))
755 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");
756 else
757 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");
758 break;
759 default:
760 break;
761 }
762 }
763
764 /**
765 * ice_print_link_msg - print link up or down message
766 * @vsi: the VSI whose link status is being queried
767 * @isup: boolean for if the link is now up or down
768 */
ice_print_link_msg(struct ice_vsi * vsi,bool isup)769 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
770 {
771 struct ice_aqc_get_phy_caps_data *caps;
772 const char *an_advertised;
773 const char *fec_req;
774 const char *speed;
775 const char *fec;
776 const char *fc;
777 const char *an;
778 int status;
779
780 if (!vsi)
781 return;
782
783 if (vsi->current_isup == isup)
784 return;
785
786 vsi->current_isup = isup;
787
788 if (!isup) {
789 netdev_info(vsi->netdev, "NIC Link is Down\n");
790 return;
791 }
792
793 switch (vsi->port_info->phy.link_info.link_speed) {
794 case ICE_AQ_LINK_SPEED_100GB:
795 speed = "100 G";
796 break;
797 case ICE_AQ_LINK_SPEED_50GB:
798 speed = "50 G";
799 break;
800 case ICE_AQ_LINK_SPEED_40GB:
801 speed = "40 G";
802 break;
803 case ICE_AQ_LINK_SPEED_25GB:
804 speed = "25 G";
805 break;
806 case ICE_AQ_LINK_SPEED_20GB:
807 speed = "20 G";
808 break;
809 case ICE_AQ_LINK_SPEED_10GB:
810 speed = "10 G";
811 break;
812 case ICE_AQ_LINK_SPEED_5GB:
813 speed = "5 G";
814 break;
815 case ICE_AQ_LINK_SPEED_2500MB:
816 speed = "2.5 G";
817 break;
818 case ICE_AQ_LINK_SPEED_1000MB:
819 speed = "1 G";
820 break;
821 case ICE_AQ_LINK_SPEED_100MB:
822 speed = "100 M";
823 break;
824 default:
825 speed = "Unknown ";
826 break;
827 }
828
829 switch (vsi->port_info->fc.current_mode) {
830 case ICE_FC_FULL:
831 fc = "Rx/Tx";
832 break;
833 case ICE_FC_TX_PAUSE:
834 fc = "Tx";
835 break;
836 case ICE_FC_RX_PAUSE:
837 fc = "Rx";
838 break;
839 case ICE_FC_NONE:
840 fc = "None";
841 break;
842 default:
843 fc = "Unknown";
844 break;
845 }
846
847 /* Get FEC mode based on negotiated link info */
848 switch (vsi->port_info->phy.link_info.fec_info) {
849 case ICE_AQ_LINK_25G_RS_528_FEC_EN:
850 case ICE_AQ_LINK_25G_RS_544_FEC_EN:
851 fec = "RS-FEC";
852 break;
853 case ICE_AQ_LINK_25G_KR_FEC_EN:
854 fec = "FC-FEC/BASE-R";
855 break;
856 default:
857 fec = "NONE";
858 break;
859 }
860
861 /* check if autoneg completed, might be false due to not supported */
862 if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
863 an = "True";
864 else
865 an = "False";
866
867 /* Get FEC mode requested based on PHY caps last SW configuration */
868 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
869 if (!caps) {
870 fec_req = "Unknown";
871 an_advertised = "Unknown";
872 goto done;
873 }
874
875 status = ice_aq_get_phy_caps(vsi->port_info, false,
876 ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
877 if (status)
878 netdev_info(vsi->netdev, "Get phy capability failed.\n");
879
880 an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
881
882 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
883 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
884 fec_req = "RS-FEC";
885 else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
886 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
887 fec_req = "FC-FEC/BASE-R";
888 else
889 fec_req = "NONE";
890
891 kfree(caps);
892
893 done:
894 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",
895 speed, fec_req, fec, an_advertised, an, fc);
896 ice_print_topo_conflict(vsi);
897 }
898
899 /**
900 * ice_vsi_link_event - update the VSI's netdev
901 * @vsi: the VSI on which the link event occurred
902 * @link_up: whether or not the VSI needs to be set up or down
903 */
ice_vsi_link_event(struct ice_vsi * vsi,bool link_up)904 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
905 {
906 if (!vsi)
907 return;
908
909 if (test_bit(ICE_VSI_DOWN, vsi->state) || !vsi->netdev)
910 return;
911
912 if (vsi->type == ICE_VSI_PF) {
913 if (link_up == netif_carrier_ok(vsi->netdev))
914 return;
915
916 if (link_up) {
917 netif_carrier_on(vsi->netdev);
918 netif_tx_wake_all_queues(vsi->netdev);
919 } else {
920 netif_carrier_off(vsi->netdev);
921 netif_tx_stop_all_queues(vsi->netdev);
922 }
923 }
924 }
925
926 /**
927 * ice_set_dflt_mib - send a default config MIB to the FW
928 * @pf: private PF struct
929 *
930 * This function sends a default configuration MIB to the FW.
931 *
932 * If this function errors out at any point, the driver is still able to
933 * function. The main impact is that LFC may not operate as expected.
934 * Therefore an error state in this function should be treated with a DBG
935 * message and continue on with driver rebuild/reenable.
936 */
ice_set_dflt_mib(struct ice_pf * pf)937 static void ice_set_dflt_mib(struct ice_pf *pf)
938 {
939 struct device *dev = ice_pf_to_dev(pf);
940 u8 mib_type, *buf, *lldpmib = NULL;
941 u16 len, typelen, offset = 0;
942 struct ice_lldp_org_tlv *tlv;
943 struct ice_hw *hw = &pf->hw;
944 u32 ouisubtype;
945
946 mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
947 lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
948 if (!lldpmib) {
949 dev_dbg(dev, "%s Failed to allocate MIB memory\n",
950 __func__);
951 return;
952 }
953
954 /* Add ETS CFG TLV */
955 tlv = (struct ice_lldp_org_tlv *)lldpmib;
956 typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
957 ICE_IEEE_ETS_TLV_LEN);
958 tlv->typelen = htons(typelen);
959 ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
960 ICE_IEEE_SUBTYPE_ETS_CFG);
961 tlv->ouisubtype = htonl(ouisubtype);
962
963 buf = tlv->tlvinfo;
964 buf[0] = 0;
965
966 /* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
967 * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
968 * Octets 13 - 20 are TSA values - leave as zeros
969 */
970 buf[5] = 0x64;
971 len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
972 offset += len + 2;
973 tlv = (struct ice_lldp_org_tlv *)
974 ((char *)tlv + sizeof(tlv->typelen) + len);
975
976 /* Add ETS REC TLV */
977 buf = tlv->tlvinfo;
978 tlv->typelen = htons(typelen);
979
980 ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
981 ICE_IEEE_SUBTYPE_ETS_REC);
982 tlv->ouisubtype = htonl(ouisubtype);
983
984 /* First octet of buf is reserved
985 * Octets 1 - 4 map UP to TC - all UPs map to zero
986 * Octets 5 - 12 are BW values - set TC 0 to 100%.
987 * Octets 13 - 20 are TSA value - leave as zeros
988 */
989 buf[5] = 0x64;
990 offset += len + 2;
991 tlv = (struct ice_lldp_org_tlv *)
992 ((char *)tlv + sizeof(tlv->typelen) + len);
993
994 /* Add PFC CFG TLV */
995 typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
996 ICE_IEEE_PFC_TLV_LEN);
997 tlv->typelen = htons(typelen);
998
999 ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
1000 ICE_IEEE_SUBTYPE_PFC_CFG);
1001 tlv->ouisubtype = htonl(ouisubtype);
1002
1003 /* Octet 1 left as all zeros - PFC disabled */
1004 buf[0] = 0x08;
1005 len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
1006 offset += len + 2;
1007
1008 if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
1009 dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
1010
1011 kfree(lldpmib);
1012 }
1013
1014 /**
1015 * ice_check_phy_fw_load - check if PHY FW load failed
1016 * @pf: pointer to PF struct
1017 * @link_cfg_err: bitmap from the link info structure
1018 *
1019 * check if external PHY FW load failed and print an error message if it did
1020 */
ice_check_phy_fw_load(struct ice_pf * pf,u8 link_cfg_err)1021 static void ice_check_phy_fw_load(struct ice_pf *pf, u8 link_cfg_err)
1022 {
1023 if (!(link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE)) {
1024 clear_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1025 return;
1026 }
1027
1028 if (test_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags))
1029 return;
1030
1031 if (link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE) {
1032 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");
1033 set_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1034 }
1035 }
1036
1037 /**
1038 * ice_check_module_power
1039 * @pf: pointer to PF struct
1040 * @link_cfg_err: bitmap from the link info structure
1041 *
1042 * check module power level returned by a previous call to aq_get_link_info
1043 * and print error messages if module power level is not supported
1044 */
ice_check_module_power(struct ice_pf * pf,u8 link_cfg_err)1045 static void ice_check_module_power(struct ice_pf *pf, u8 link_cfg_err)
1046 {
1047 /* if module power level is supported, clear the flag */
1048 if (!(link_cfg_err & (ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT |
1049 ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED))) {
1050 clear_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1051 return;
1052 }
1053
1054 /* if ICE_FLAG_MOD_POWER_UNSUPPORTED was previously set and the
1055 * above block didn't clear this bit, there's nothing to do
1056 */
1057 if (test_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags))
1058 return;
1059
1060 if (link_cfg_err & ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT) {
1061 dev_err(ice_pf_to_dev(pf), "The installed module is incompatible with the device's NVM image. Cannot start link\n");
1062 set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1063 } else if (link_cfg_err & ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED) {
1064 dev_err(ice_pf_to_dev(pf), "The module's power requirements exceed the device's power supply. Cannot start link\n");
1065 set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1066 }
1067 }
1068
1069 /**
1070 * ice_check_link_cfg_err - check if link configuration failed
1071 * @pf: pointer to the PF struct
1072 * @link_cfg_err: bitmap from the link info structure
1073 *
1074 * print if any link configuration failure happens due to the value in the
1075 * link_cfg_err parameter in the link info structure
1076 */
ice_check_link_cfg_err(struct ice_pf * pf,u8 link_cfg_err)1077 static void ice_check_link_cfg_err(struct ice_pf *pf, u8 link_cfg_err)
1078 {
1079 ice_check_module_power(pf, link_cfg_err);
1080 ice_check_phy_fw_load(pf, link_cfg_err);
1081 }
1082
1083 /**
1084 * ice_link_event - process the link event
1085 * @pf: PF that the link event is associated with
1086 * @pi: port_info for the port that the link event is associated with
1087 * @link_up: true if the physical link is up and false if it is down
1088 * @link_speed: current link speed received from the link event
1089 *
1090 * Returns 0 on success and negative on failure
1091 */
1092 static int
ice_link_event(struct ice_pf * pf,struct ice_port_info * pi,bool link_up,u16 link_speed)1093 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
1094 u16 link_speed)
1095 {
1096 struct device *dev = ice_pf_to_dev(pf);
1097 struct ice_phy_info *phy_info;
1098 struct ice_vsi *vsi;
1099 u16 old_link_speed;
1100 bool old_link;
1101 int status;
1102
1103 phy_info = &pi->phy;
1104 phy_info->link_info_old = phy_info->link_info;
1105
1106 old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
1107 old_link_speed = phy_info->link_info_old.link_speed;
1108
1109 /* update the link info structures and re-enable link events,
1110 * don't bail on failure due to other book keeping needed
1111 */
1112 status = ice_update_link_info(pi);
1113 if (status)
1114 dev_dbg(dev, "Failed to update link status on port %d, err %d aq_err %s\n",
1115 pi->lport, status,
1116 ice_aq_str(pi->hw->adminq.sq_last_status));
1117
1118 ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
1119
1120 /* Check if the link state is up after updating link info, and treat
1121 * this event as an UP event since the link is actually UP now.
1122 */
1123 if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
1124 link_up = true;
1125
1126 vsi = ice_get_main_vsi(pf);
1127 if (!vsi || !vsi->port_info)
1128 return -EINVAL;
1129
1130 /* turn off PHY if media was removed */
1131 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
1132 !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
1133 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
1134 ice_set_link(vsi, false);
1135 }
1136
1137 /* if the old link up/down and speed is the same as the new */
1138 if (link_up == old_link && link_speed == old_link_speed)
1139 return 0;
1140
1141 ice_ptp_link_change(pf, pf->hw.pf_id, link_up);
1142
1143 if (ice_is_dcb_active(pf)) {
1144 if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
1145 ice_dcb_rebuild(pf);
1146 } else {
1147 if (link_up)
1148 ice_set_dflt_mib(pf);
1149 }
1150 ice_vsi_link_event(vsi, link_up);
1151 ice_print_link_msg(vsi, link_up);
1152
1153 ice_vc_notify_link_state(pf);
1154
1155 return 0;
1156 }
1157
1158 /**
1159 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
1160 * @pf: board private structure
1161 */
ice_watchdog_subtask(struct ice_pf * pf)1162 static void ice_watchdog_subtask(struct ice_pf *pf)
1163 {
1164 int i;
1165
1166 /* if interface is down do nothing */
1167 if (test_bit(ICE_DOWN, pf->state) ||
1168 test_bit(ICE_CFG_BUSY, pf->state))
1169 return;
1170
1171 /* make sure we don't do these things too often */
1172 if (time_before(jiffies,
1173 pf->serv_tmr_prev + pf->serv_tmr_period))
1174 return;
1175
1176 pf->serv_tmr_prev = jiffies;
1177
1178 /* Update the stats for active netdevs so the network stack
1179 * can look at updated numbers whenever it cares to
1180 */
1181 ice_update_pf_stats(pf);
1182 ice_for_each_vsi(pf, i)
1183 if (pf->vsi[i] && pf->vsi[i]->netdev)
1184 ice_update_vsi_stats(pf->vsi[i]);
1185 }
1186
1187 /**
1188 * ice_init_link_events - enable/initialize link events
1189 * @pi: pointer to the port_info instance
1190 *
1191 * Returns -EIO on failure, 0 on success
1192 */
ice_init_link_events(struct ice_port_info * pi)1193 static int ice_init_link_events(struct ice_port_info *pi)
1194 {
1195 u16 mask;
1196
1197 mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
1198 ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL |
1199 ICE_AQ_LINK_EVENT_PHY_FW_LOAD_FAIL));
1200
1201 if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
1202 dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
1203 pi->lport);
1204 return -EIO;
1205 }
1206
1207 if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
1208 dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
1209 pi->lport);
1210 return -EIO;
1211 }
1212
1213 return 0;
1214 }
1215
1216 /**
1217 * ice_handle_link_event - handle link event via ARQ
1218 * @pf: PF that the link event is associated with
1219 * @event: event structure containing link status info
1220 */
1221 static int
ice_handle_link_event(struct ice_pf * pf,struct ice_rq_event_info * event)1222 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1223 {
1224 struct ice_aqc_get_link_status_data *link_data;
1225 struct ice_port_info *port_info;
1226 int status;
1227
1228 link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
1229 port_info = pf->hw.port_info;
1230 if (!port_info)
1231 return -EINVAL;
1232
1233 status = ice_link_event(pf, port_info,
1234 !!(link_data->link_info & ICE_AQ_LINK_UP),
1235 le16_to_cpu(link_data->link_speed));
1236 if (status)
1237 dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
1238 status);
1239
1240 return status;
1241 }
1242
1243 /**
1244 * ice_aq_prep_for_event - Prepare to wait for an AdminQ event from firmware
1245 * @pf: pointer to the PF private structure
1246 * @task: intermediate helper storage and identifier for waiting
1247 * @opcode: the opcode to wait for
1248 *
1249 * Prepares to wait for a specific AdminQ completion event on the ARQ for
1250 * a given PF. Actual wait would be done by a call to ice_aq_wait_for_event().
1251 *
1252 * Calls are separated to allow caller registering for event before sending
1253 * the command, which mitigates a race between registering and FW responding.
1254 *
1255 * To obtain only the descriptor contents, pass an task->event with null
1256 * msg_buf. If the complete data buffer is desired, allocate the
1257 * task->event.msg_buf with enough space ahead of time.
1258 */
ice_aq_prep_for_event(struct ice_pf * pf,struct ice_aq_task * task,u16 opcode)1259 void ice_aq_prep_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1260 u16 opcode)
1261 {
1262 INIT_HLIST_NODE(&task->entry);
1263 task->opcode = opcode;
1264 task->state = ICE_AQ_TASK_WAITING;
1265
1266 spin_lock_bh(&pf->aq_wait_lock);
1267 hlist_add_head(&task->entry, &pf->aq_wait_list);
1268 spin_unlock_bh(&pf->aq_wait_lock);
1269 }
1270
1271 /**
1272 * ice_aq_wait_for_event - Wait for an AdminQ event from firmware
1273 * @pf: pointer to the PF private structure
1274 * @task: ptr prepared by ice_aq_prep_for_event()
1275 * @timeout: how long to wait, in jiffies
1276 *
1277 * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1278 * current thread will be put to sleep until the specified event occurs or
1279 * until the given timeout is reached.
1280 *
1281 * Returns: zero on success, or a negative error code on failure.
1282 */
ice_aq_wait_for_event(struct ice_pf * pf,struct ice_aq_task * task,unsigned long timeout)1283 int ice_aq_wait_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1284 unsigned long timeout)
1285 {
1286 enum ice_aq_task_state *state = &task->state;
1287 struct device *dev = ice_pf_to_dev(pf);
1288 unsigned long start = jiffies;
1289 long ret;
1290 int err;
1291
1292 ret = wait_event_interruptible_timeout(pf->aq_wait_queue,
1293 *state != ICE_AQ_TASK_WAITING,
1294 timeout);
1295 switch (*state) {
1296 case ICE_AQ_TASK_NOT_PREPARED:
1297 WARN(1, "call to %s without ice_aq_prep_for_event()", __func__);
1298 err = -EINVAL;
1299 break;
1300 case ICE_AQ_TASK_WAITING:
1301 err = ret < 0 ? ret : -ETIMEDOUT;
1302 break;
1303 case ICE_AQ_TASK_CANCELED:
1304 err = ret < 0 ? ret : -ECANCELED;
1305 break;
1306 case ICE_AQ_TASK_COMPLETE:
1307 err = ret < 0 ? ret : 0;
1308 break;
1309 default:
1310 WARN(1, "Unexpected AdminQ wait task state %u", *state);
1311 err = -EINVAL;
1312 break;
1313 }
1314
1315 dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
1316 jiffies_to_msecs(jiffies - start),
1317 jiffies_to_msecs(timeout),
1318 task->opcode);
1319
1320 spin_lock_bh(&pf->aq_wait_lock);
1321 hlist_del(&task->entry);
1322 spin_unlock_bh(&pf->aq_wait_lock);
1323
1324 return err;
1325 }
1326
1327 /**
1328 * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
1329 * @pf: pointer to the PF private structure
1330 * @opcode: the opcode of the event
1331 * @event: the event to check
1332 *
1333 * Loops over the current list of pending threads waiting for an AdminQ event.
1334 * For each matching task, copy the contents of the event into the task
1335 * structure and wake up the thread.
1336 *
1337 * If multiple threads wait for the same opcode, they will all be woken up.
1338 *
1339 * Note that event->msg_buf will only be duplicated if the event has a buffer
1340 * with enough space already allocated. Otherwise, only the descriptor and
1341 * message length will be copied.
1342 *
1343 * Returns: true if an event was found, false otherwise
1344 */
ice_aq_check_events(struct ice_pf * pf,u16 opcode,struct ice_rq_event_info * event)1345 static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
1346 struct ice_rq_event_info *event)
1347 {
1348 struct ice_rq_event_info *task_ev;
1349 struct ice_aq_task *task;
1350 bool found = false;
1351
1352 spin_lock_bh(&pf->aq_wait_lock);
1353 hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
1354 if (task->state != ICE_AQ_TASK_WAITING)
1355 continue;
1356 if (task->opcode != opcode)
1357 continue;
1358
1359 task_ev = &task->event;
1360 memcpy(&task_ev->desc, &event->desc, sizeof(event->desc));
1361 task_ev->msg_len = event->msg_len;
1362
1363 /* Only copy the data buffer if a destination was set */
1364 if (task_ev->msg_buf && task_ev->buf_len >= event->buf_len) {
1365 memcpy(task_ev->msg_buf, event->msg_buf,
1366 event->buf_len);
1367 task_ev->buf_len = event->buf_len;
1368 }
1369
1370 task->state = ICE_AQ_TASK_COMPLETE;
1371 found = true;
1372 }
1373 spin_unlock_bh(&pf->aq_wait_lock);
1374
1375 if (found)
1376 wake_up(&pf->aq_wait_queue);
1377 }
1378
1379 /**
1380 * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
1381 * @pf: the PF private structure
1382 *
1383 * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
1384 * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
1385 */
ice_aq_cancel_waiting_tasks(struct ice_pf * pf)1386 static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
1387 {
1388 struct ice_aq_task *task;
1389
1390 spin_lock_bh(&pf->aq_wait_lock);
1391 hlist_for_each_entry(task, &pf->aq_wait_list, entry)
1392 task->state = ICE_AQ_TASK_CANCELED;
1393 spin_unlock_bh(&pf->aq_wait_lock);
1394
1395 wake_up(&pf->aq_wait_queue);
1396 }
1397
1398 #define ICE_MBX_OVERFLOW_WATERMARK 64
1399
1400 /**
1401 * __ice_clean_ctrlq - helper function to clean controlq rings
1402 * @pf: ptr to struct ice_pf
1403 * @q_type: specific Control queue type
1404 */
__ice_clean_ctrlq(struct ice_pf * pf,enum ice_ctl_q q_type)1405 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
1406 {
1407 struct device *dev = ice_pf_to_dev(pf);
1408 struct ice_rq_event_info event;
1409 struct ice_hw *hw = &pf->hw;
1410 struct ice_ctl_q_info *cq;
1411 u16 pending, i = 0;
1412 const char *qtype;
1413 u32 oldval, val;
1414
1415 /* Do not clean control queue if/when PF reset fails */
1416 if (test_bit(ICE_RESET_FAILED, pf->state))
1417 return 0;
1418
1419 switch (q_type) {
1420 case ICE_CTL_Q_ADMIN:
1421 cq = &hw->adminq;
1422 qtype = "Admin";
1423 break;
1424 case ICE_CTL_Q_SB:
1425 cq = &hw->sbq;
1426 qtype = "Sideband";
1427 break;
1428 case ICE_CTL_Q_MAILBOX:
1429 cq = &hw->mailboxq;
1430 qtype = "Mailbox";
1431 /* we are going to try to detect a malicious VF, so set the
1432 * state to begin detection
1433 */
1434 hw->mbx_snapshot.mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
1435 break;
1436 default:
1437 dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
1438 return 0;
1439 }
1440
1441 /* check for error indications - PF_xx_AxQLEN register layout for
1442 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
1443 */
1444 val = rd32(hw, cq->rq.len);
1445 if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1446 PF_FW_ARQLEN_ARQCRIT_M)) {
1447 oldval = val;
1448 if (val & PF_FW_ARQLEN_ARQVFE_M)
1449 dev_dbg(dev, "%s Receive Queue VF Error detected\n",
1450 qtype);
1451 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
1452 dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
1453 qtype);
1454 }
1455 if (val & PF_FW_ARQLEN_ARQCRIT_M)
1456 dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
1457 qtype);
1458 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1459 PF_FW_ARQLEN_ARQCRIT_M);
1460 if (oldval != val)
1461 wr32(hw, cq->rq.len, val);
1462 }
1463
1464 val = rd32(hw, cq->sq.len);
1465 if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1466 PF_FW_ATQLEN_ATQCRIT_M)) {
1467 oldval = val;
1468 if (val & PF_FW_ATQLEN_ATQVFE_M)
1469 dev_dbg(dev, "%s Send Queue VF Error detected\n",
1470 qtype);
1471 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1472 dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
1473 qtype);
1474 }
1475 if (val & PF_FW_ATQLEN_ATQCRIT_M)
1476 dev_dbg(dev, "%s Send Queue Critical Error detected\n",
1477 qtype);
1478 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1479 PF_FW_ATQLEN_ATQCRIT_M);
1480 if (oldval != val)
1481 wr32(hw, cq->sq.len, val);
1482 }
1483
1484 event.buf_len = cq->rq_buf_size;
1485 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1486 if (!event.msg_buf)
1487 return 0;
1488
1489 do {
1490 struct ice_mbx_data data = {};
1491 u16 opcode;
1492 int ret;
1493
1494 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1495 if (ret == -EALREADY)
1496 break;
1497 if (ret) {
1498 dev_err(dev, "%s Receive Queue event error %d\n", qtype,
1499 ret);
1500 break;
1501 }
1502
1503 opcode = le16_to_cpu(event.desc.opcode);
1504
1505 /* Notify any thread that might be waiting for this event */
1506 ice_aq_check_events(pf, opcode, &event);
1507
1508 switch (opcode) {
1509 case ice_aqc_opc_get_link_status:
1510 if (ice_handle_link_event(pf, &event))
1511 dev_err(dev, "Could not handle link event\n");
1512 break;
1513 case ice_aqc_opc_event_lan_overflow:
1514 ice_vf_lan_overflow_event(pf, &event);
1515 break;
1516 case ice_mbx_opc_send_msg_to_pf:
1517 if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT)) {
1518 ice_vc_process_vf_msg(pf, &event, NULL);
1519 ice_mbx_vf_dec_trig_e830(hw, &event);
1520 } else {
1521 u16 val = hw->mailboxq.num_rq_entries;
1522
1523 data.max_num_msgs_mbx = val;
1524 val = ICE_MBX_OVERFLOW_WATERMARK;
1525 data.async_watermark_val = val;
1526 data.num_msg_proc = i;
1527 data.num_pending_arq = pending;
1528
1529 ice_vc_process_vf_msg(pf, &event, &data);
1530 }
1531 break;
1532 case ice_aqc_opc_fw_logging:
1533 ice_output_fw_log(hw, &event.desc, event.msg_buf);
1534 break;
1535 case ice_aqc_opc_lldp_set_mib_change:
1536 ice_dcb_process_lldp_set_mib_change(pf, &event);
1537 break;
1538 default:
1539 dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
1540 qtype, opcode);
1541 break;
1542 }
1543 } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1544
1545 kfree(event.msg_buf);
1546
1547 return pending && (i == ICE_DFLT_IRQ_WORK);
1548 }
1549
1550 /**
1551 * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1552 * @hw: pointer to hardware info
1553 * @cq: control queue information
1554 *
1555 * returns true if there are pending messages in a queue, false if there aren't
1556 */
ice_ctrlq_pending(struct ice_hw * hw,struct ice_ctl_q_info * cq)1557 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1558 {
1559 u16 ntu;
1560
1561 ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1562 return cq->rq.next_to_clean != ntu;
1563 }
1564
1565 /**
1566 * ice_clean_adminq_subtask - clean the AdminQ rings
1567 * @pf: board private structure
1568 */
ice_clean_adminq_subtask(struct ice_pf * pf)1569 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1570 {
1571 struct ice_hw *hw = &pf->hw;
1572
1573 if (!test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
1574 return;
1575
1576 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1577 return;
1578
1579 clear_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
1580
1581 /* There might be a situation where new messages arrive to a control
1582 * queue between processing the last message and clearing the
1583 * EVENT_PENDING bit. So before exiting, check queue head again (using
1584 * ice_ctrlq_pending) and process new messages if any.
1585 */
1586 if (ice_ctrlq_pending(hw, &hw->adminq))
1587 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1588
1589 ice_flush(hw);
1590 }
1591
1592 /**
1593 * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1594 * @pf: board private structure
1595 */
ice_clean_mailboxq_subtask(struct ice_pf * pf)1596 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1597 {
1598 struct ice_hw *hw = &pf->hw;
1599
1600 if (!test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1601 return;
1602
1603 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1604 return;
1605
1606 clear_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1607
1608 if (ice_ctrlq_pending(hw, &hw->mailboxq))
1609 __ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1610
1611 ice_flush(hw);
1612 }
1613
1614 /**
1615 * ice_clean_sbq_subtask - clean the Sideband Queue rings
1616 * @pf: board private structure
1617 */
ice_clean_sbq_subtask(struct ice_pf * pf)1618 static void ice_clean_sbq_subtask(struct ice_pf *pf)
1619 {
1620 struct ice_hw *hw = &pf->hw;
1621
1622 /* Nothing to do here if sideband queue is not supported */
1623 if (!ice_is_sbq_supported(hw)) {
1624 clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1625 return;
1626 }
1627
1628 if (!test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state))
1629 return;
1630
1631 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_SB))
1632 return;
1633
1634 clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1635
1636 if (ice_ctrlq_pending(hw, &hw->sbq))
1637 __ice_clean_ctrlq(pf, ICE_CTL_Q_SB);
1638
1639 ice_flush(hw);
1640 }
1641
1642 /**
1643 * ice_service_task_schedule - schedule the service task to wake up
1644 * @pf: board private structure
1645 *
1646 * If not already scheduled, this puts the task into the work queue.
1647 */
ice_service_task_schedule(struct ice_pf * pf)1648 void ice_service_task_schedule(struct ice_pf *pf)
1649 {
1650 if (!test_bit(ICE_SERVICE_DIS, pf->state) &&
1651 !test_and_set_bit(ICE_SERVICE_SCHED, pf->state) &&
1652 !test_bit(ICE_NEEDS_RESTART, pf->state))
1653 queue_work(ice_wq, &pf->serv_task);
1654 }
1655
1656 /**
1657 * ice_service_task_complete - finish up the service task
1658 * @pf: board private structure
1659 */
ice_service_task_complete(struct ice_pf * pf)1660 static void ice_service_task_complete(struct ice_pf *pf)
1661 {
1662 WARN_ON(!test_bit(ICE_SERVICE_SCHED, pf->state));
1663
1664 /* force memory (pf->state) to sync before next service task */
1665 smp_mb__before_atomic();
1666 clear_bit(ICE_SERVICE_SCHED, pf->state);
1667 }
1668
1669 /**
1670 * ice_service_task_stop - stop service task and cancel works
1671 * @pf: board private structure
1672 *
1673 * Return 0 if the ICE_SERVICE_DIS bit was not already set,
1674 * 1 otherwise.
1675 */
ice_service_task_stop(struct ice_pf * pf)1676 static int ice_service_task_stop(struct ice_pf *pf)
1677 {
1678 int ret;
1679
1680 ret = test_and_set_bit(ICE_SERVICE_DIS, pf->state);
1681
1682 if (pf->serv_tmr.function)
1683 del_timer_sync(&pf->serv_tmr);
1684 if (pf->serv_task.func)
1685 cancel_work_sync(&pf->serv_task);
1686
1687 clear_bit(ICE_SERVICE_SCHED, pf->state);
1688 return ret;
1689 }
1690
1691 /**
1692 * ice_service_task_restart - restart service task and schedule works
1693 * @pf: board private structure
1694 *
1695 * This function is needed for suspend and resume works (e.g WoL scenario)
1696 */
ice_service_task_restart(struct ice_pf * pf)1697 static void ice_service_task_restart(struct ice_pf *pf)
1698 {
1699 clear_bit(ICE_SERVICE_DIS, pf->state);
1700 ice_service_task_schedule(pf);
1701 }
1702
1703 /**
1704 * ice_service_timer - timer callback to schedule service task
1705 * @t: pointer to timer_list
1706 */
ice_service_timer(struct timer_list * t)1707 static void ice_service_timer(struct timer_list *t)
1708 {
1709 struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1710
1711 mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1712 ice_service_task_schedule(pf);
1713 }
1714
1715 /**
1716 * ice_handle_mdd_event - handle malicious driver detect event
1717 * @pf: pointer to the PF structure
1718 *
1719 * Called from service task. OICR interrupt handler indicates MDD event.
1720 * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
1721 * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
1722 * disable the queue, the PF can be configured to reset the VF using ethtool
1723 * private flag mdd-auto-reset-vf.
1724 */
ice_handle_mdd_event(struct ice_pf * pf)1725 static void ice_handle_mdd_event(struct ice_pf *pf)
1726 {
1727 struct device *dev = ice_pf_to_dev(pf);
1728 struct ice_hw *hw = &pf->hw;
1729 struct ice_vf *vf;
1730 unsigned int bkt;
1731 u32 reg;
1732
1733 if (!test_and_clear_bit(ICE_MDD_EVENT_PENDING, pf->state)) {
1734 /* Since the VF MDD event logging is rate limited, check if
1735 * there are pending MDD events.
1736 */
1737 ice_print_vfs_mdd_events(pf);
1738 return;
1739 }
1740
1741 /* find what triggered an MDD event */
1742 reg = rd32(hw, GL_MDET_TX_PQM);
1743 if (reg & GL_MDET_TX_PQM_VALID_M) {
1744 u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1745 GL_MDET_TX_PQM_PF_NUM_S;
1746 u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1747 GL_MDET_TX_PQM_VF_NUM_S;
1748 u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1749 GL_MDET_TX_PQM_MAL_TYPE_S;
1750 u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1751 GL_MDET_TX_PQM_QNUM_S);
1752
1753 if (netif_msg_tx_err(pf))
1754 dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1755 event, queue, pf_num, vf_num);
1756 wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1757 }
1758
1759 reg = rd32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw));
1760 if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1761 u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1762 GL_MDET_TX_TCLAN_PF_NUM_S;
1763 u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1764 GL_MDET_TX_TCLAN_VF_NUM_S;
1765 u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1766 GL_MDET_TX_TCLAN_MAL_TYPE_S;
1767 u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1768 GL_MDET_TX_TCLAN_QNUM_S);
1769
1770 if (netif_msg_tx_err(pf))
1771 dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1772 event, queue, pf_num, vf_num);
1773 wr32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw), U32_MAX);
1774 }
1775
1776 reg = rd32(hw, GL_MDET_RX);
1777 if (reg & GL_MDET_RX_VALID_M) {
1778 u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1779 GL_MDET_RX_PF_NUM_S;
1780 u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1781 GL_MDET_RX_VF_NUM_S;
1782 u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1783 GL_MDET_RX_MAL_TYPE_S;
1784 u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1785 GL_MDET_RX_QNUM_S);
1786
1787 if (netif_msg_rx_err(pf))
1788 dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1789 event, queue, pf_num, vf_num);
1790 wr32(hw, GL_MDET_RX, 0xffffffff);
1791 }
1792
1793 /* check to see if this PF caused an MDD event */
1794 reg = rd32(hw, PF_MDET_TX_PQM);
1795 if (reg & PF_MDET_TX_PQM_VALID_M) {
1796 wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1797 if (netif_msg_tx_err(pf))
1798 dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
1799 }
1800
1801 reg = rd32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw));
1802 if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1803 wr32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw), 0xffff);
1804 if (netif_msg_tx_err(pf))
1805 dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
1806 }
1807
1808 reg = rd32(hw, PF_MDET_RX);
1809 if (reg & PF_MDET_RX_VALID_M) {
1810 wr32(hw, PF_MDET_RX, 0xFFFF);
1811 if (netif_msg_rx_err(pf))
1812 dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
1813 }
1814
1815 /* Check to see if one of the VFs caused an MDD event, and then
1816 * increment counters and set print pending
1817 */
1818 mutex_lock(&pf->vfs.table_lock);
1819 ice_for_each_vf(pf, bkt, vf) {
1820 reg = rd32(hw, VP_MDET_TX_PQM(vf->vf_id));
1821 if (reg & VP_MDET_TX_PQM_VALID_M) {
1822 wr32(hw, VP_MDET_TX_PQM(vf->vf_id), 0xFFFF);
1823 vf->mdd_tx_events.count++;
1824 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1825 if (netif_msg_tx_err(pf))
1826 dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
1827 vf->vf_id);
1828 }
1829
1830 reg = rd32(hw, VP_MDET_TX_TCLAN(vf->vf_id));
1831 if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1832 wr32(hw, VP_MDET_TX_TCLAN(vf->vf_id), 0xFFFF);
1833 vf->mdd_tx_events.count++;
1834 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1835 if (netif_msg_tx_err(pf))
1836 dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
1837 vf->vf_id);
1838 }
1839
1840 reg = rd32(hw, VP_MDET_TX_TDPU(vf->vf_id));
1841 if (reg & VP_MDET_TX_TDPU_VALID_M) {
1842 wr32(hw, VP_MDET_TX_TDPU(vf->vf_id), 0xFFFF);
1843 vf->mdd_tx_events.count++;
1844 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1845 if (netif_msg_tx_err(pf))
1846 dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
1847 vf->vf_id);
1848 }
1849
1850 reg = rd32(hw, VP_MDET_RX(vf->vf_id));
1851 if (reg & VP_MDET_RX_VALID_M) {
1852 wr32(hw, VP_MDET_RX(vf->vf_id), 0xFFFF);
1853 vf->mdd_rx_events.count++;
1854 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1855 if (netif_msg_rx_err(pf))
1856 dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
1857 vf->vf_id);
1858
1859 /* Since the queue is disabled on VF Rx MDD events, the
1860 * PF can be configured to reset the VF through ethtool
1861 * private flag mdd-auto-reset-vf.
1862 */
1863 if (test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
1864 /* VF MDD event counters will be cleared by
1865 * reset, so print the event prior to reset.
1866 */
1867 ice_print_vf_rx_mdd_event(vf);
1868 ice_reset_vf(vf, ICE_VF_RESET_LOCK);
1869 }
1870 }
1871 }
1872 mutex_unlock(&pf->vfs.table_lock);
1873
1874 ice_print_vfs_mdd_events(pf);
1875 }
1876
1877 /**
1878 * ice_force_phys_link_state - Force the physical link state
1879 * @vsi: VSI to force the physical link state to up/down
1880 * @link_up: true/false indicates to set the physical link to up/down
1881 *
1882 * Force the physical link state by getting the current PHY capabilities from
1883 * hardware and setting the PHY config based on the determined capabilities. If
1884 * link changes a link event will be triggered because both the Enable Automatic
1885 * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1886 *
1887 * Returns 0 on success, negative on failure
1888 */
ice_force_phys_link_state(struct ice_vsi * vsi,bool link_up)1889 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1890 {
1891 struct ice_aqc_get_phy_caps_data *pcaps;
1892 struct ice_aqc_set_phy_cfg_data *cfg;
1893 struct ice_port_info *pi;
1894 struct device *dev;
1895 int retcode;
1896
1897 if (!vsi || !vsi->port_info || !vsi->back)
1898 return -EINVAL;
1899 if (vsi->type != ICE_VSI_PF)
1900 return 0;
1901
1902 dev = ice_pf_to_dev(vsi->back);
1903
1904 pi = vsi->port_info;
1905
1906 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1907 if (!pcaps)
1908 return -ENOMEM;
1909
1910 retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
1911 NULL);
1912 if (retcode) {
1913 dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
1914 vsi->vsi_num, retcode);
1915 retcode = -EIO;
1916 goto out;
1917 }
1918
1919 /* No change in link */
1920 if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1921 link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1922 goto out;
1923
1924 /* Use the current user PHY configuration. The current user PHY
1925 * configuration is initialized during probe from PHY capabilities
1926 * software mode, and updated on set PHY configuration.
1927 */
1928 cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
1929 if (!cfg) {
1930 retcode = -ENOMEM;
1931 goto out;
1932 }
1933
1934 cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1935 if (link_up)
1936 cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1937 else
1938 cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1939
1940 retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1941 if (retcode) {
1942 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
1943 vsi->vsi_num, retcode);
1944 retcode = -EIO;
1945 }
1946
1947 kfree(cfg);
1948 out:
1949 kfree(pcaps);
1950 return retcode;
1951 }
1952
1953 /**
1954 * ice_init_nvm_phy_type - Initialize the NVM PHY type
1955 * @pi: port info structure
1956 *
1957 * Initialize nvm_phy_type_[low|high] for link lenient mode support
1958 */
ice_init_nvm_phy_type(struct ice_port_info * pi)1959 static int ice_init_nvm_phy_type(struct ice_port_info *pi)
1960 {
1961 struct ice_aqc_get_phy_caps_data *pcaps;
1962 struct ice_pf *pf = pi->hw->back;
1963 int err;
1964
1965 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1966 if (!pcaps)
1967 return -ENOMEM;
1968
1969 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA,
1970 pcaps, NULL);
1971
1972 if (err) {
1973 dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1974 goto out;
1975 }
1976
1977 pf->nvm_phy_type_hi = pcaps->phy_type_high;
1978 pf->nvm_phy_type_lo = pcaps->phy_type_low;
1979
1980 out:
1981 kfree(pcaps);
1982 return err;
1983 }
1984
1985 /**
1986 * ice_init_link_dflt_override - Initialize link default override
1987 * @pi: port info structure
1988 *
1989 * Initialize link default override and PHY total port shutdown during probe
1990 */
ice_init_link_dflt_override(struct ice_port_info * pi)1991 static void ice_init_link_dflt_override(struct ice_port_info *pi)
1992 {
1993 struct ice_link_default_override_tlv *ldo;
1994 struct ice_pf *pf = pi->hw->back;
1995
1996 ldo = &pf->link_dflt_override;
1997 if (ice_get_link_default_override(ldo, pi))
1998 return;
1999
2000 if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
2001 return;
2002
2003 /* Enable Total Port Shutdown (override/replace link-down-on-close
2004 * ethtool private flag) for ports with Port Disable bit set.
2005 */
2006 set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
2007 set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
2008 }
2009
2010 /**
2011 * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
2012 * @pi: port info structure
2013 *
2014 * If default override is enabled, initialize the user PHY cfg speed and FEC
2015 * settings using the default override mask from the NVM.
2016 *
2017 * The PHY should only be configured with the default override settings the
2018 * first time media is available. The ICE_LINK_DEFAULT_OVERRIDE_PENDING state
2019 * is used to indicate that the user PHY cfg default override is initialized
2020 * and the PHY has not been configured with the default override settings. The
2021 * state is set here, and cleared in ice_configure_phy the first time the PHY is
2022 * configured.
2023 *
2024 * This function should be called only if the FW doesn't support default
2025 * configuration mode, as reported by ice_fw_supports_report_dflt_cfg.
2026 */
ice_init_phy_cfg_dflt_override(struct ice_port_info * pi)2027 static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
2028 {
2029 struct ice_link_default_override_tlv *ldo;
2030 struct ice_aqc_set_phy_cfg_data *cfg;
2031 struct ice_phy_info *phy = &pi->phy;
2032 struct ice_pf *pf = pi->hw->back;
2033
2034 ldo = &pf->link_dflt_override;
2035
2036 /* If link default override is enabled, use to mask NVM PHY capabilities
2037 * for speed and FEC default configuration.
2038 */
2039 cfg = &phy->curr_user_phy_cfg;
2040
2041 if (ldo->phy_type_low || ldo->phy_type_high) {
2042 cfg->phy_type_low = pf->nvm_phy_type_lo &
2043 cpu_to_le64(ldo->phy_type_low);
2044 cfg->phy_type_high = pf->nvm_phy_type_hi &
2045 cpu_to_le64(ldo->phy_type_high);
2046 }
2047 cfg->link_fec_opt = ldo->fec_options;
2048 phy->curr_user_fec_req = ICE_FEC_AUTO;
2049
2050 set_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
2051 }
2052
2053 /**
2054 * ice_init_phy_user_cfg - Initialize the PHY user configuration
2055 * @pi: port info structure
2056 *
2057 * Initialize the current user PHY configuration, speed, FEC, and FC requested
2058 * mode to default. The PHY defaults are from get PHY capabilities topology
2059 * with media so call when media is first available. An error is returned if
2060 * called when media is not available. The PHY initialization completed state is
2061 * set here.
2062 *
2063 * These configurations are used when setting PHY
2064 * configuration. The user PHY configuration is updated on set PHY
2065 * configuration. Returns 0 on success, negative on failure
2066 */
ice_init_phy_user_cfg(struct ice_port_info * pi)2067 static int ice_init_phy_user_cfg(struct ice_port_info *pi)
2068 {
2069 struct ice_aqc_get_phy_caps_data *pcaps;
2070 struct ice_phy_info *phy = &pi->phy;
2071 struct ice_pf *pf = pi->hw->back;
2072 int err;
2073
2074 if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2075 return -EIO;
2076
2077 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2078 if (!pcaps)
2079 return -ENOMEM;
2080
2081 if (ice_fw_supports_report_dflt_cfg(pi->hw))
2082 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2083 pcaps, NULL);
2084 else
2085 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2086 pcaps, NULL);
2087 if (err) {
2088 dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
2089 goto err_out;
2090 }
2091
2092 ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
2093
2094 /* check if lenient mode is supported and enabled */
2095 if (ice_fw_supports_link_override(pi->hw) &&
2096 !(pcaps->module_compliance_enforcement &
2097 ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
2098 set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
2099
2100 /* if the FW supports default PHY configuration mode, then the driver
2101 * does not have to apply link override settings. If not,
2102 * initialize user PHY configuration with link override values
2103 */
2104 if (!ice_fw_supports_report_dflt_cfg(pi->hw) &&
2105 (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN)) {
2106 ice_init_phy_cfg_dflt_override(pi);
2107 goto out;
2108 }
2109 }
2110
2111 /* if link default override is not enabled, set user flow control and
2112 * FEC settings based on what get_phy_caps returned
2113 */
2114 phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
2115 pcaps->link_fec_options);
2116 phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
2117
2118 out:
2119 phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
2120 set_bit(ICE_PHY_INIT_COMPLETE, pf->state);
2121 err_out:
2122 kfree(pcaps);
2123 return err;
2124 }
2125
2126 /**
2127 * ice_configure_phy - configure PHY
2128 * @vsi: VSI of PHY
2129 *
2130 * Set the PHY configuration. If the current PHY configuration is the same as
2131 * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
2132 * configure the based get PHY capabilities for topology with media.
2133 */
ice_configure_phy(struct ice_vsi * vsi)2134 static int ice_configure_phy(struct ice_vsi *vsi)
2135 {
2136 struct device *dev = ice_pf_to_dev(vsi->back);
2137 struct ice_port_info *pi = vsi->port_info;
2138 struct ice_aqc_get_phy_caps_data *pcaps;
2139 struct ice_aqc_set_phy_cfg_data *cfg;
2140 struct ice_phy_info *phy = &pi->phy;
2141 struct ice_pf *pf = vsi->back;
2142 int err;
2143
2144 /* Ensure we have media as we cannot configure a medialess port */
2145 if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2146 return -ENOMEDIUM;
2147
2148 ice_print_topo_conflict(vsi);
2149
2150 if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags) &&
2151 phy->link_info.topo_media_conflict == ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
2152 return -EPERM;
2153
2154 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))
2155 return ice_force_phys_link_state(vsi, true);
2156
2157 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2158 if (!pcaps)
2159 return -ENOMEM;
2160
2161 /* Get current PHY config */
2162 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
2163 NULL);
2164 if (err) {
2165 dev_err(dev, "Failed to get PHY configuration, VSI %d error %d\n",
2166 vsi->vsi_num, err);
2167 goto done;
2168 }
2169
2170 /* If PHY enable link is configured and configuration has not changed,
2171 * there's nothing to do
2172 */
2173 if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
2174 ice_phy_caps_equals_cfg(pcaps, &phy->curr_user_phy_cfg))
2175 goto done;
2176
2177 /* Use PHY topology as baseline for configuration */
2178 memset(pcaps, 0, sizeof(*pcaps));
2179 if (ice_fw_supports_report_dflt_cfg(pi->hw))
2180 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2181 pcaps, NULL);
2182 else
2183 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2184 pcaps, NULL);
2185 if (err) {
2186 dev_err(dev, "Failed to get PHY caps, VSI %d error %d\n",
2187 vsi->vsi_num, err);
2188 goto done;
2189 }
2190
2191 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2192 if (!cfg) {
2193 err = -ENOMEM;
2194 goto done;
2195 }
2196
2197 ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
2198
2199 /* Speed - If default override pending, use curr_user_phy_cfg set in
2200 * ice_init_phy_user_cfg_ldo.
2201 */
2202 if (test_and_clear_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING,
2203 vsi->back->state)) {
2204 cfg->phy_type_low = phy->curr_user_phy_cfg.phy_type_low;
2205 cfg->phy_type_high = phy->curr_user_phy_cfg.phy_type_high;
2206 } else {
2207 u64 phy_low = 0, phy_high = 0;
2208
2209 ice_update_phy_type(&phy_low, &phy_high,
2210 pi->phy.curr_user_speed_req);
2211 cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
2212 cfg->phy_type_high = pcaps->phy_type_high &
2213 cpu_to_le64(phy_high);
2214 }
2215
2216 /* Can't provide what was requested; use PHY capabilities */
2217 if (!cfg->phy_type_low && !cfg->phy_type_high) {
2218 cfg->phy_type_low = pcaps->phy_type_low;
2219 cfg->phy_type_high = pcaps->phy_type_high;
2220 }
2221
2222 /* FEC */
2223 ice_cfg_phy_fec(pi, cfg, phy->curr_user_fec_req);
2224
2225 /* Can't provide what was requested; use PHY capabilities */
2226 if (cfg->link_fec_opt !=
2227 (cfg->link_fec_opt & pcaps->link_fec_options)) {
2228 cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
2229 cfg->link_fec_opt = pcaps->link_fec_options;
2230 }
2231
2232 /* Flow Control - always supported; no need to check against
2233 * capabilities
2234 */
2235 ice_cfg_phy_fc(pi, cfg, phy->curr_user_fc_req);
2236
2237 /* Enable link and link update */
2238 cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
2239
2240 err = ice_aq_set_phy_cfg(&pf->hw, pi, cfg, NULL);
2241 if (err)
2242 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
2243 vsi->vsi_num, err);
2244
2245 kfree(cfg);
2246 done:
2247 kfree(pcaps);
2248 return err;
2249 }
2250
2251 /**
2252 * ice_check_media_subtask - Check for media
2253 * @pf: pointer to PF struct
2254 *
2255 * If media is available, then initialize PHY user configuration if it is not
2256 * been, and configure the PHY if the interface is up.
2257 */
ice_check_media_subtask(struct ice_pf * pf)2258 static void ice_check_media_subtask(struct ice_pf *pf)
2259 {
2260 struct ice_port_info *pi;
2261 struct ice_vsi *vsi;
2262 int err;
2263
2264 /* No need to check for media if it's already present */
2265 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
2266 return;
2267
2268 vsi = ice_get_main_vsi(pf);
2269 if (!vsi)
2270 return;
2271
2272 /* Refresh link info and check if media is present */
2273 pi = vsi->port_info;
2274 err = ice_update_link_info(pi);
2275 if (err)
2276 return;
2277
2278 ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
2279
2280 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2281 if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state))
2282 ice_init_phy_user_cfg(pi);
2283
2284 /* PHY settings are reset on media insertion, reconfigure
2285 * PHY to preserve settings.
2286 */
2287 if (test_bit(ICE_VSI_DOWN, vsi->state) &&
2288 test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
2289 return;
2290
2291 err = ice_configure_phy(vsi);
2292 if (!err)
2293 clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
2294
2295 /* A Link Status Event will be generated; the event handler
2296 * will complete bringing the interface up
2297 */
2298 }
2299 }
2300
2301 /**
2302 * ice_service_task - manage and run subtasks
2303 * @work: pointer to work_struct contained by the PF struct
2304 */
ice_service_task(struct work_struct * work)2305 static void ice_service_task(struct work_struct *work)
2306 {
2307 struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
2308 unsigned long start_time = jiffies;
2309
2310 /* subtasks */
2311
2312 /* process reset requests first */
2313 ice_reset_subtask(pf);
2314
2315 /* bail if a reset/recovery cycle is pending or rebuild failed */
2316 if (ice_is_reset_in_progress(pf->state) ||
2317 test_bit(ICE_SUSPENDED, pf->state) ||
2318 test_bit(ICE_NEEDS_RESTART, pf->state)) {
2319 ice_service_task_complete(pf);
2320 return;
2321 }
2322
2323 if (test_and_clear_bit(ICE_AUX_ERR_PENDING, pf->state)) {
2324 struct iidc_event *event;
2325
2326 event = kzalloc(sizeof(*event), GFP_KERNEL);
2327 if (event) {
2328 set_bit(IIDC_EVENT_CRIT_ERR, event->type);
2329 /* report the entire OICR value to AUX driver */
2330 swap(event->reg, pf->oicr_err_reg);
2331 ice_send_event_to_aux(pf, event);
2332 kfree(event);
2333 }
2334 }
2335
2336 /* unplug aux dev per request, if an unplug request came in
2337 * while processing a plug request, this will handle it
2338 */
2339 if (test_and_clear_bit(ICE_FLAG_UNPLUG_AUX_DEV, pf->flags))
2340 ice_unplug_aux_dev(pf);
2341
2342 /* Plug aux device per request */
2343 if (test_and_clear_bit(ICE_FLAG_PLUG_AUX_DEV, pf->flags))
2344 ice_plug_aux_dev(pf);
2345
2346 if (test_and_clear_bit(ICE_FLAG_MTU_CHANGED, pf->flags)) {
2347 struct iidc_event *event;
2348
2349 event = kzalloc(sizeof(*event), GFP_KERNEL);
2350 if (event) {
2351 set_bit(IIDC_EVENT_AFTER_MTU_CHANGE, event->type);
2352 ice_send_event_to_aux(pf, event);
2353 kfree(event);
2354 }
2355 }
2356
2357 ice_clean_adminq_subtask(pf);
2358 ice_check_media_subtask(pf);
2359 ice_check_for_hang_subtask(pf);
2360 ice_sync_fltr_subtask(pf);
2361 ice_handle_mdd_event(pf);
2362 ice_watchdog_subtask(pf);
2363
2364 if (ice_is_safe_mode(pf)) {
2365 ice_service_task_complete(pf);
2366 return;
2367 }
2368
2369 ice_process_vflr_event(pf);
2370 ice_clean_mailboxq_subtask(pf);
2371 ice_clean_sbq_subtask(pf);
2372 ice_sync_arfs_fltrs(pf);
2373 ice_flush_fdir_ctx(pf);
2374
2375 /* Clear ICE_SERVICE_SCHED flag to allow scheduling next event */
2376 ice_service_task_complete(pf);
2377
2378 /* If the tasks have taken longer than one service timer period
2379 * or there is more work to be done, reset the service timer to
2380 * schedule the service task now.
2381 */
2382 if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
2383 test_bit(ICE_MDD_EVENT_PENDING, pf->state) ||
2384 test_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2385 test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
2386 test_bit(ICE_FD_VF_FLUSH_CTX, pf->state) ||
2387 test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state) ||
2388 test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
2389 mod_timer(&pf->serv_tmr, jiffies);
2390 }
2391
2392 /**
2393 * ice_set_ctrlq_len - helper function to set controlq length
2394 * @hw: pointer to the HW instance
2395 */
ice_set_ctrlq_len(struct ice_hw * hw)2396 static void ice_set_ctrlq_len(struct ice_hw *hw)
2397 {
2398 hw->adminq.num_rq_entries = ICE_AQ_LEN;
2399 hw->adminq.num_sq_entries = ICE_AQ_LEN;
2400 hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
2401 hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
2402 hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
2403 hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
2404 hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2405 hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2406 hw->sbq.num_rq_entries = ICE_SBQ_LEN;
2407 hw->sbq.num_sq_entries = ICE_SBQ_LEN;
2408 hw->sbq.rq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2409 hw->sbq.sq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2410 }
2411
2412 /**
2413 * ice_schedule_reset - schedule a reset
2414 * @pf: board private structure
2415 * @reset: reset being requested
2416 */
ice_schedule_reset(struct ice_pf * pf,enum ice_reset_req reset)2417 int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
2418 {
2419 struct device *dev = ice_pf_to_dev(pf);
2420
2421 /* bail out if earlier reset has failed */
2422 if (test_bit(ICE_RESET_FAILED, pf->state)) {
2423 dev_dbg(dev, "earlier reset has failed\n");
2424 return -EIO;
2425 }
2426 /* bail if reset/recovery already in progress */
2427 if (ice_is_reset_in_progress(pf->state)) {
2428 dev_dbg(dev, "Reset already in progress\n");
2429 return -EBUSY;
2430 }
2431
2432 switch (reset) {
2433 case ICE_RESET_PFR:
2434 set_bit(ICE_PFR_REQ, pf->state);
2435 break;
2436 case ICE_RESET_CORER:
2437 set_bit(ICE_CORER_REQ, pf->state);
2438 break;
2439 case ICE_RESET_GLOBR:
2440 set_bit(ICE_GLOBR_REQ, pf->state);
2441 break;
2442 default:
2443 return -EINVAL;
2444 }
2445
2446 ice_service_task_schedule(pf);
2447 return 0;
2448 }
2449
2450 /**
2451 * ice_irq_affinity_notify - Callback for affinity changes
2452 * @notify: context as to what irq was changed
2453 * @mask: the new affinity mask
2454 *
2455 * This is a callback function used by the irq_set_affinity_notifier function
2456 * so that we may register to receive changes to the irq affinity masks.
2457 */
2458 static void
ice_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)2459 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2460 const cpumask_t *mask)
2461 {
2462 struct ice_q_vector *q_vector =
2463 container_of(notify, struct ice_q_vector, affinity_notify);
2464
2465 cpumask_copy(&q_vector->affinity_mask, mask);
2466 }
2467
2468 /**
2469 * ice_irq_affinity_release - Callback for affinity notifier release
2470 * @ref: internal core kernel usage
2471 *
2472 * This is a callback function used by the irq_set_affinity_notifier function
2473 * to inform the current notification subscriber that they will no longer
2474 * receive notifications.
2475 */
ice_irq_affinity_release(struct kref __always_unused * ref)2476 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2477
2478 /**
2479 * ice_vsi_ena_irq - Enable IRQ for the given VSI
2480 * @vsi: the VSI being configured
2481 */
ice_vsi_ena_irq(struct ice_vsi * vsi)2482 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2483 {
2484 struct ice_hw *hw = &vsi->back->hw;
2485 int i;
2486
2487 ice_for_each_q_vector(vsi, i)
2488 ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
2489
2490 ice_flush(hw);
2491 return 0;
2492 }
2493
2494 /**
2495 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2496 * @vsi: the VSI being configured
2497 * @basename: name for the vector
2498 */
ice_vsi_req_irq_msix(struct ice_vsi * vsi,char * basename)2499 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2500 {
2501 int q_vectors = vsi->num_q_vectors;
2502 struct ice_pf *pf = vsi->back;
2503 struct device *dev;
2504 int rx_int_idx = 0;
2505 int tx_int_idx = 0;
2506 int vector, err;
2507 int irq_num;
2508
2509 dev = ice_pf_to_dev(pf);
2510 for (vector = 0; vector < q_vectors; vector++) {
2511 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2512
2513 irq_num = q_vector->irq.virq;
2514
2515 if (q_vector->tx.tx_ring && q_vector->rx.rx_ring) {
2516 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2517 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2518 tx_int_idx++;
2519 } else if (q_vector->rx.rx_ring) {
2520 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2521 "%s-%s-%d", basename, "rx", rx_int_idx++);
2522 } else if (q_vector->tx.tx_ring) {
2523 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2524 "%s-%s-%d", basename, "tx", tx_int_idx++);
2525 } else {
2526 /* skip this unused q_vector */
2527 continue;
2528 }
2529 if (vsi->type == ICE_VSI_CTRL && vsi->vf)
2530 err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2531 IRQF_SHARED, q_vector->name,
2532 q_vector);
2533 else
2534 err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2535 0, q_vector->name, q_vector);
2536 if (err) {
2537 netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2538 err);
2539 goto free_q_irqs;
2540 }
2541
2542 /* register for affinity change notifications */
2543 if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2544 struct irq_affinity_notify *affinity_notify;
2545
2546 affinity_notify = &q_vector->affinity_notify;
2547 affinity_notify->notify = ice_irq_affinity_notify;
2548 affinity_notify->release = ice_irq_affinity_release;
2549 irq_set_affinity_notifier(irq_num, affinity_notify);
2550 }
2551
2552 /* assign the mask for this irq */
2553 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
2554 }
2555
2556 err = ice_set_cpu_rx_rmap(vsi);
2557 if (err) {
2558 netdev_err(vsi->netdev, "Failed to setup CPU RMAP on VSI %u: %pe\n",
2559 vsi->vsi_num, ERR_PTR(err));
2560 goto free_q_irqs;
2561 }
2562
2563 vsi->irqs_ready = true;
2564 return 0;
2565
2566 free_q_irqs:
2567 while (vector--) {
2568 irq_num = vsi->q_vectors[vector]->irq.virq;
2569 if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2570 irq_set_affinity_notifier(irq_num, NULL);
2571 irq_set_affinity_hint(irq_num, NULL);
2572 devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
2573 }
2574 return err;
2575 }
2576
2577 /**
2578 * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2579 * @vsi: VSI to setup Tx rings used by XDP
2580 *
2581 * Return 0 on success and negative value on error
2582 */
ice_xdp_alloc_setup_rings(struct ice_vsi * vsi)2583 static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2584 {
2585 struct device *dev = ice_pf_to_dev(vsi->back);
2586 struct ice_tx_desc *tx_desc;
2587 int i, j;
2588
2589 ice_for_each_xdp_txq(vsi, i) {
2590 u16 xdp_q_idx = vsi->alloc_txq + i;
2591 struct ice_ring_stats *ring_stats;
2592 struct ice_tx_ring *xdp_ring;
2593
2594 xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
2595 if (!xdp_ring)
2596 goto free_xdp_rings;
2597
2598 ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
2599 if (!ring_stats) {
2600 ice_free_tx_ring(xdp_ring);
2601 goto free_xdp_rings;
2602 }
2603
2604 xdp_ring->ring_stats = ring_stats;
2605 xdp_ring->q_index = xdp_q_idx;
2606 xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
2607 xdp_ring->vsi = vsi;
2608 xdp_ring->netdev = NULL;
2609 xdp_ring->dev = dev;
2610 xdp_ring->count = vsi->num_tx_desc;
2611 WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
2612 if (ice_setup_tx_ring(xdp_ring))
2613 goto free_xdp_rings;
2614 ice_set_ring_xdp(xdp_ring);
2615 spin_lock_init(&xdp_ring->tx_lock);
2616 for (j = 0; j < xdp_ring->count; j++) {
2617 tx_desc = ICE_TX_DESC(xdp_ring, j);
2618 tx_desc->cmd_type_offset_bsz = 0;
2619 }
2620 }
2621
2622 return 0;
2623
2624 free_xdp_rings:
2625 for (; i >= 0; i--) {
2626 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc) {
2627 kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2628 vsi->xdp_rings[i]->ring_stats = NULL;
2629 ice_free_tx_ring(vsi->xdp_rings[i]);
2630 }
2631 }
2632 return -ENOMEM;
2633 }
2634
2635 /**
2636 * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2637 * @vsi: VSI to set the bpf prog on
2638 * @prog: the bpf prog pointer
2639 */
ice_vsi_assign_bpf_prog(struct ice_vsi * vsi,struct bpf_prog * prog)2640 static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2641 {
2642 struct bpf_prog *old_prog;
2643 int i;
2644
2645 old_prog = xchg(&vsi->xdp_prog, prog);
2646 ice_for_each_rxq(vsi, i)
2647 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
2648
2649 if (old_prog)
2650 bpf_prog_put(old_prog);
2651 }
2652
2653 /**
2654 * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2655 * @vsi: VSI to bring up Tx rings used by XDP
2656 * @prog: bpf program that will be assigned to VSI
2657 * @cfg_type: create from scratch or restore the existing configuration
2658 *
2659 * Return 0 on success and negative value on error
2660 */
ice_prepare_xdp_rings(struct ice_vsi * vsi,struct bpf_prog * prog,enum ice_xdp_cfg cfg_type)2661 int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog,
2662 enum ice_xdp_cfg cfg_type)
2663 {
2664 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2665 int xdp_rings_rem = vsi->num_xdp_txq;
2666 struct ice_pf *pf = vsi->back;
2667 struct ice_qs_cfg xdp_qs_cfg = {
2668 .qs_mutex = &pf->avail_q_mutex,
2669 .pf_map = pf->avail_txqs,
2670 .pf_map_size = pf->max_pf_txqs,
2671 .q_count = vsi->num_xdp_txq,
2672 .scatter_count = ICE_MAX_SCATTER_TXQS,
2673 .vsi_map = vsi->txq_map,
2674 .vsi_map_offset = vsi->alloc_txq,
2675 .mapping_mode = ICE_VSI_MAP_CONTIG
2676 };
2677 struct device *dev;
2678 int i, v_idx;
2679 int status;
2680
2681 dev = ice_pf_to_dev(pf);
2682 vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2683 sizeof(*vsi->xdp_rings), GFP_KERNEL);
2684 if (!vsi->xdp_rings)
2685 return -ENOMEM;
2686
2687 vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2688 if (__ice_vsi_get_qs(&xdp_qs_cfg))
2689 goto err_map_xdp;
2690
2691 if (static_key_enabled(&ice_xdp_locking_key))
2692 netdev_warn(vsi->netdev,
2693 "Could not allocate one XDP Tx ring per CPU, XDP_TX/XDP_REDIRECT actions will be slower\n");
2694
2695 if (ice_xdp_alloc_setup_rings(vsi))
2696 goto clear_xdp_rings;
2697
2698 /* follow the logic from ice_vsi_map_rings_to_vectors */
2699 ice_for_each_q_vector(vsi, v_idx) {
2700 struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2701 int xdp_rings_per_v, q_id, q_base;
2702
2703 xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2704 vsi->num_q_vectors - v_idx);
2705 q_base = vsi->num_xdp_txq - xdp_rings_rem;
2706
2707 for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
2708 struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_id];
2709
2710 xdp_ring->q_vector = q_vector;
2711 xdp_ring->next = q_vector->tx.tx_ring;
2712 q_vector->tx.tx_ring = xdp_ring;
2713 }
2714 xdp_rings_rem -= xdp_rings_per_v;
2715 }
2716
2717 ice_for_each_rxq(vsi, i) {
2718 if (static_key_enabled(&ice_xdp_locking_key)) {
2719 vsi->rx_rings[i]->xdp_ring = vsi->xdp_rings[i % vsi->num_xdp_txq];
2720 } else {
2721 struct ice_q_vector *q_vector = vsi->rx_rings[i]->q_vector;
2722 struct ice_tx_ring *ring;
2723
2724 ice_for_each_tx_ring(ring, q_vector->tx) {
2725 if (ice_ring_is_xdp(ring)) {
2726 vsi->rx_rings[i]->xdp_ring = ring;
2727 break;
2728 }
2729 }
2730 }
2731 ice_tx_xsk_pool(vsi, i);
2732 }
2733
2734 /* omit the scheduler update if in reset path; XDP queues will be
2735 * taken into account at the end of ice_vsi_rebuild, where
2736 * ice_cfg_vsi_lan is being called
2737 */
2738 if (cfg_type == ICE_XDP_CFG_PART)
2739 return 0;
2740
2741 /* tell the Tx scheduler that right now we have
2742 * additional queues
2743 */
2744 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2745 max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2746
2747 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2748 max_txqs);
2749 if (status) {
2750 dev_err(dev, "Failed VSI LAN queue config for XDP, error: %d\n",
2751 status);
2752 goto clear_xdp_rings;
2753 }
2754
2755 /* assign the prog only when it's not already present on VSI;
2756 * this flow is a subject of both ethtool -L and ndo_bpf flows;
2757 * VSI rebuild that happens under ethtool -L can expose us to
2758 * the bpf_prog refcount issues as we would be swapping same
2759 * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
2760 * on it as it would be treated as an 'old_prog'; for ndo_bpf
2761 * this is not harmful as dev_xdp_install bumps the refcount
2762 * before calling the op exposed by the driver;
2763 */
2764 if (!ice_is_xdp_ena_vsi(vsi))
2765 ice_vsi_assign_bpf_prog(vsi, prog);
2766
2767 return 0;
2768 clear_xdp_rings:
2769 ice_for_each_xdp_txq(vsi, i)
2770 if (vsi->xdp_rings[i]) {
2771 kfree_rcu(vsi->xdp_rings[i], rcu);
2772 vsi->xdp_rings[i] = NULL;
2773 }
2774
2775 err_map_xdp:
2776 mutex_lock(&pf->avail_q_mutex);
2777 ice_for_each_xdp_txq(vsi, i) {
2778 clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2779 vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2780 }
2781 mutex_unlock(&pf->avail_q_mutex);
2782
2783 devm_kfree(dev, vsi->xdp_rings);
2784 return -ENOMEM;
2785 }
2786
2787 /**
2788 * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2789 * @vsi: VSI to remove XDP rings
2790 * @cfg_type: disable XDP permanently or allow it to be restored later
2791 *
2792 * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2793 * resources
2794 */
ice_destroy_xdp_rings(struct ice_vsi * vsi,enum ice_xdp_cfg cfg_type)2795 int ice_destroy_xdp_rings(struct ice_vsi *vsi, enum ice_xdp_cfg cfg_type)
2796 {
2797 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2798 struct ice_pf *pf = vsi->back;
2799 int i, v_idx;
2800
2801 /* q_vectors are freed in reset path so there's no point in detaching
2802 * rings
2803 */
2804 if (cfg_type == ICE_XDP_CFG_PART)
2805 goto free_qmap;
2806
2807 ice_for_each_q_vector(vsi, v_idx) {
2808 struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2809 struct ice_tx_ring *ring;
2810
2811 ice_for_each_tx_ring(ring, q_vector->tx)
2812 if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2813 break;
2814
2815 /* restore the value of last node prior to XDP setup */
2816 q_vector->tx.tx_ring = ring;
2817 }
2818
2819 free_qmap:
2820 mutex_lock(&pf->avail_q_mutex);
2821 ice_for_each_xdp_txq(vsi, i) {
2822 clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2823 vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2824 }
2825 mutex_unlock(&pf->avail_q_mutex);
2826
2827 ice_for_each_xdp_txq(vsi, i)
2828 if (vsi->xdp_rings[i]) {
2829 if (vsi->xdp_rings[i]->desc) {
2830 synchronize_rcu();
2831 ice_free_tx_ring(vsi->xdp_rings[i]);
2832 }
2833 kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2834 vsi->xdp_rings[i]->ring_stats = NULL;
2835 kfree_rcu(vsi->xdp_rings[i], rcu);
2836 vsi->xdp_rings[i] = NULL;
2837 }
2838
2839 devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2840 vsi->xdp_rings = NULL;
2841
2842 if (static_key_enabled(&ice_xdp_locking_key))
2843 static_branch_dec(&ice_xdp_locking_key);
2844
2845 if (cfg_type == ICE_XDP_CFG_PART)
2846 return 0;
2847
2848 ice_vsi_assign_bpf_prog(vsi, NULL);
2849
2850 /* notify Tx scheduler that we destroyed XDP queues and bring
2851 * back the old number of child nodes
2852 */
2853 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2854 max_txqs[i] = vsi->num_txq;
2855
2856 /* change number of XDP Tx queues to 0 */
2857 vsi->num_xdp_txq = 0;
2858
2859 return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2860 max_txqs);
2861 }
2862
2863 /**
2864 * ice_vsi_rx_napi_schedule - Schedule napi on RX queues from VSI
2865 * @vsi: VSI to schedule napi on
2866 */
ice_vsi_rx_napi_schedule(struct ice_vsi * vsi)2867 static void ice_vsi_rx_napi_schedule(struct ice_vsi *vsi)
2868 {
2869 int i;
2870
2871 ice_for_each_rxq(vsi, i) {
2872 struct ice_rx_ring *rx_ring = vsi->rx_rings[i];
2873
2874 if (rx_ring->xsk_pool)
2875 napi_schedule(&rx_ring->q_vector->napi);
2876 }
2877 }
2878
2879 /**
2880 * ice_vsi_determine_xdp_res - figure out how many Tx qs can XDP have
2881 * @vsi: VSI to determine the count of XDP Tx qs
2882 *
2883 * returns 0 if Tx qs count is higher than at least half of CPU count,
2884 * -ENOMEM otherwise
2885 */
ice_vsi_determine_xdp_res(struct ice_vsi * vsi)2886 int ice_vsi_determine_xdp_res(struct ice_vsi *vsi)
2887 {
2888 u16 avail = ice_get_avail_txq_count(vsi->back);
2889 u16 cpus = num_possible_cpus();
2890
2891 if (avail < cpus / 2)
2892 return -ENOMEM;
2893
2894 vsi->num_xdp_txq = min_t(u16, avail, cpus);
2895
2896 if (vsi->num_xdp_txq < cpus)
2897 static_branch_inc(&ice_xdp_locking_key);
2898
2899 return 0;
2900 }
2901
2902 /**
2903 * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2904 * @vsi: Pointer to VSI structure
2905 */
ice_max_xdp_frame_size(struct ice_vsi * vsi)2906 static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
2907 {
2908 if (test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
2909 return ICE_RXBUF_1664;
2910 else
2911 return ICE_RXBUF_3072;
2912 }
2913
2914 /**
2915 * ice_xdp_setup_prog - Add or remove XDP eBPF program
2916 * @vsi: VSI to setup XDP for
2917 * @prog: XDP program
2918 * @extack: netlink extended ack
2919 */
2920 static int
ice_xdp_setup_prog(struct ice_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)2921 ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
2922 struct netlink_ext_ack *extack)
2923 {
2924 unsigned int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
2925 int ret = 0, xdp_ring_err = 0;
2926 bool if_running;
2927
2928 if (prog && !prog->aux->xdp_has_frags) {
2929 if (frame_size > ice_max_xdp_frame_size(vsi)) {
2930 NL_SET_ERR_MSG_MOD(extack,
2931 "MTU is too large for linear frames and XDP prog does not support frags");
2932 return -EOPNOTSUPP;
2933 }
2934 }
2935
2936 /* hot swap progs and avoid toggling link */
2937 if (ice_is_xdp_ena_vsi(vsi) == !!prog ||
2938 test_bit(ICE_VSI_REBUILD_PENDING, vsi->state)) {
2939 ice_vsi_assign_bpf_prog(vsi, prog);
2940 return 0;
2941 }
2942
2943 if_running = netif_running(vsi->netdev) &&
2944 !test_and_set_bit(ICE_VSI_DOWN, vsi->state);
2945
2946 /* need to stop netdev while setting up the program for Rx rings */
2947 if (if_running) {
2948 ret = ice_down(vsi);
2949 if (ret) {
2950 NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
2951 return ret;
2952 }
2953 }
2954
2955 if (!ice_is_xdp_ena_vsi(vsi) && prog) {
2956 xdp_ring_err = ice_vsi_determine_xdp_res(vsi);
2957 if (xdp_ring_err) {
2958 NL_SET_ERR_MSG_MOD(extack, "Not enough Tx resources for XDP");
2959 } else {
2960 xdp_ring_err = ice_prepare_xdp_rings(vsi, prog,
2961 ICE_XDP_CFG_FULL);
2962 if (xdp_ring_err)
2963 NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
2964 }
2965 xdp_features_set_redirect_target(vsi->netdev, true);
2966 /* reallocate Rx queues that are used for zero-copy */
2967 xdp_ring_err = ice_realloc_zc_buf(vsi, true);
2968 if (xdp_ring_err)
2969 NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Rx resources failed");
2970 } else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
2971 xdp_features_clear_redirect_target(vsi->netdev);
2972 xdp_ring_err = ice_destroy_xdp_rings(vsi, ICE_XDP_CFG_FULL);
2973 if (xdp_ring_err)
2974 NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
2975 /* reallocate Rx queues that were used for zero-copy */
2976 xdp_ring_err = ice_realloc_zc_buf(vsi, false);
2977 if (xdp_ring_err)
2978 NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Rx resources failed");
2979 }
2980
2981 if (if_running)
2982 ret = ice_up(vsi);
2983
2984 if (!ret && prog)
2985 ice_vsi_rx_napi_schedule(vsi);
2986
2987 return (ret || xdp_ring_err) ? -ENOMEM : 0;
2988 }
2989
2990 /**
2991 * ice_xdp_safe_mode - XDP handler for safe mode
2992 * @dev: netdevice
2993 * @xdp: XDP command
2994 */
ice_xdp_safe_mode(struct net_device __always_unused * dev,struct netdev_bpf * xdp)2995 static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
2996 struct netdev_bpf *xdp)
2997 {
2998 NL_SET_ERR_MSG_MOD(xdp->extack,
2999 "Please provide working DDP firmware package in order to use XDP\n"
3000 "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
3001 return -EOPNOTSUPP;
3002 }
3003
3004 /**
3005 * ice_xdp - implements XDP handler
3006 * @dev: netdevice
3007 * @xdp: XDP command
3008 */
ice_xdp(struct net_device * dev,struct netdev_bpf * xdp)3009 static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3010 {
3011 struct ice_netdev_priv *np = netdev_priv(dev);
3012 struct ice_vsi *vsi = np->vsi;
3013 int ret;
3014
3015 if (vsi->type != ICE_VSI_PF) {
3016 NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
3017 return -EINVAL;
3018 }
3019
3020 mutex_lock(&vsi->xdp_state_lock);
3021
3022 switch (xdp->command) {
3023 case XDP_SETUP_PROG:
3024 ret = ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
3025 break;
3026 case XDP_SETUP_XSK_POOL:
3027 ret = ice_xsk_pool_setup(vsi, xdp->xsk.pool, xdp->xsk.queue_id);
3028 break;
3029 default:
3030 ret = -EINVAL;
3031 }
3032
3033 mutex_unlock(&vsi->xdp_state_lock);
3034 return ret;
3035 }
3036
3037 /**
3038 * ice_ena_misc_vector - enable the non-queue interrupts
3039 * @pf: board private structure
3040 */
ice_ena_misc_vector(struct ice_pf * pf)3041 static void ice_ena_misc_vector(struct ice_pf *pf)
3042 {
3043 struct ice_hw *hw = &pf->hw;
3044 u32 val;
3045
3046 /* Disable anti-spoof detection interrupt to prevent spurious event
3047 * interrupts during a function reset. Anti-spoof functionally is
3048 * still supported.
3049 */
3050 val = rd32(hw, GL_MDCK_TX_TDPU);
3051 val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
3052 wr32(hw, GL_MDCK_TX_TDPU, val);
3053
3054 /* clear things first */
3055 wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
3056 rd32(hw, PFINT_OICR); /* read to clear */
3057
3058 val = (PFINT_OICR_ECC_ERR_M |
3059 PFINT_OICR_MAL_DETECT_M |
3060 PFINT_OICR_GRST_M |
3061 PFINT_OICR_PCI_EXCEPTION_M |
3062 PFINT_OICR_VFLR_M |
3063 PFINT_OICR_HMC_ERR_M |
3064 PFINT_OICR_PE_PUSH_M |
3065 PFINT_OICR_PE_CRITERR_M);
3066
3067 wr32(hw, PFINT_OICR_ENA, val);
3068
3069 /* SW_ITR_IDX = 0, but don't change INTENA */
3070 wr32(hw, GLINT_DYN_CTL(pf->oicr_irq.index),
3071 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
3072 }
3073
3074 /**
3075 * ice_misc_intr - misc interrupt handler
3076 * @irq: interrupt number
3077 * @data: pointer to a q_vector
3078 */
ice_misc_intr(int __always_unused irq,void * data)3079 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
3080 {
3081 struct ice_pf *pf = (struct ice_pf *)data;
3082 struct ice_hw *hw = &pf->hw;
3083 struct device *dev;
3084 u32 oicr, ena_mask;
3085
3086 dev = ice_pf_to_dev(pf);
3087 set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
3088 set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
3089 set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
3090
3091 oicr = rd32(hw, PFINT_OICR);
3092 ena_mask = rd32(hw, PFINT_OICR_ENA);
3093
3094 if (oicr & PFINT_OICR_SWINT_M) {
3095 ena_mask &= ~PFINT_OICR_SWINT_M;
3096 pf->sw_int_count++;
3097 }
3098
3099 if (oicr & PFINT_OICR_MAL_DETECT_M) {
3100 ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
3101 set_bit(ICE_MDD_EVENT_PENDING, pf->state);
3102 }
3103 if (oicr & PFINT_OICR_VFLR_M) {
3104 /* disable any further VFLR event notifications */
3105 if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
3106 u32 reg = rd32(hw, PFINT_OICR_ENA);
3107
3108 reg &= ~PFINT_OICR_VFLR_M;
3109 wr32(hw, PFINT_OICR_ENA, reg);
3110 } else {
3111 ena_mask &= ~PFINT_OICR_VFLR_M;
3112 set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
3113 }
3114 }
3115
3116 if (oicr & PFINT_OICR_GRST_M) {
3117 u32 reset;
3118
3119 /* we have a reset warning */
3120 ena_mask &= ~PFINT_OICR_GRST_M;
3121 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
3122 GLGEN_RSTAT_RESET_TYPE_S;
3123
3124 if (reset == ICE_RESET_CORER)
3125 pf->corer_count++;
3126 else if (reset == ICE_RESET_GLOBR)
3127 pf->globr_count++;
3128 else if (reset == ICE_RESET_EMPR)
3129 pf->empr_count++;
3130 else
3131 dev_dbg(dev, "Invalid reset type %d\n", reset);
3132
3133 /* If a reset cycle isn't already in progress, we set a bit in
3134 * pf->state so that the service task can start a reset/rebuild.
3135 */
3136 if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
3137 if (reset == ICE_RESET_CORER)
3138 set_bit(ICE_CORER_RECV, pf->state);
3139 else if (reset == ICE_RESET_GLOBR)
3140 set_bit(ICE_GLOBR_RECV, pf->state);
3141 else
3142 set_bit(ICE_EMPR_RECV, pf->state);
3143
3144 /* There are couple of different bits at play here.
3145 * hw->reset_ongoing indicates whether the hardware is
3146 * in reset. This is set to true when a reset interrupt
3147 * is received and set back to false after the driver
3148 * has determined that the hardware is out of reset.
3149 *
3150 * ICE_RESET_OICR_RECV in pf->state indicates
3151 * that a post reset rebuild is required before the
3152 * driver is operational again. This is set above.
3153 *
3154 * As this is the start of the reset/rebuild cycle, set
3155 * both to indicate that.
3156 */
3157 hw->reset_ongoing = true;
3158 }
3159 }
3160
3161 if (oicr & PFINT_OICR_TSYN_TX_M) {
3162 ena_mask &= ~PFINT_OICR_TSYN_TX_M;
3163 if (!hw->reset_ongoing)
3164 set_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread);
3165 }
3166
3167 if (oicr & PFINT_OICR_TSYN_EVNT_M) {
3168 u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3169 u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
3170
3171 ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
3172
3173 if (hw->func_caps.ts_func_info.src_tmr_owned) {
3174 /* Save EVENTs from GLTSYN register */
3175 pf->ptp.ext_ts_irq |= gltsyn_stat &
3176 (GLTSYN_STAT_EVENT0_M |
3177 GLTSYN_STAT_EVENT1_M |
3178 GLTSYN_STAT_EVENT2_M);
3179
3180 set_bit(ICE_MISC_THREAD_EXTTS_EVENT, pf->misc_thread);
3181 }
3182 }
3183
3184 #define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
3185 if (oicr & ICE_AUX_CRIT_ERR) {
3186 pf->oicr_err_reg |= oicr;
3187 set_bit(ICE_AUX_ERR_PENDING, pf->state);
3188 ena_mask &= ~ICE_AUX_CRIT_ERR;
3189 }
3190
3191 /* Report any remaining unexpected interrupts */
3192 oicr &= ena_mask;
3193 if (oicr) {
3194 dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
3195 /* If a critical error is pending there is no choice but to
3196 * reset the device.
3197 */
3198 if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
3199 PFINT_OICR_ECC_ERR_M)) {
3200 set_bit(ICE_PFR_REQ, pf->state);
3201 }
3202 }
3203
3204 return IRQ_WAKE_THREAD;
3205 }
3206
3207 /**
3208 * ice_misc_intr_thread_fn - misc interrupt thread function
3209 * @irq: interrupt number
3210 * @data: pointer to a q_vector
3211 */
ice_misc_intr_thread_fn(int __always_unused irq,void * data)3212 static irqreturn_t ice_misc_intr_thread_fn(int __always_unused irq, void *data)
3213 {
3214 struct ice_pf *pf = data;
3215 struct ice_hw *hw;
3216
3217 hw = &pf->hw;
3218
3219 if (ice_is_reset_in_progress(pf->state))
3220 return IRQ_HANDLED;
3221
3222 ice_service_task_schedule(pf);
3223
3224 if (test_and_clear_bit(ICE_MISC_THREAD_EXTTS_EVENT, pf->misc_thread))
3225 ice_ptp_extts_event(pf);
3226
3227 if (test_and_clear_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread)) {
3228 /* Process outstanding Tx timestamps. If there is more work,
3229 * re-arm the interrupt to trigger again.
3230 */
3231 if (ice_ptp_process_ts(pf) == ICE_TX_TSTAMP_WORK_PENDING) {
3232 wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
3233 ice_flush(hw);
3234 }
3235 }
3236
3237 ice_irq_dynamic_ena(hw, NULL, NULL);
3238
3239 return IRQ_HANDLED;
3240 }
3241
3242 /**
3243 * ice_dis_ctrlq_interrupts - disable control queue interrupts
3244 * @hw: pointer to HW structure
3245 */
ice_dis_ctrlq_interrupts(struct ice_hw * hw)3246 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
3247 {
3248 /* disable Admin queue Interrupt causes */
3249 wr32(hw, PFINT_FW_CTL,
3250 rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
3251
3252 /* disable Mailbox queue Interrupt causes */
3253 wr32(hw, PFINT_MBX_CTL,
3254 rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
3255
3256 wr32(hw, PFINT_SB_CTL,
3257 rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
3258
3259 /* disable Control queue Interrupt causes */
3260 wr32(hw, PFINT_OICR_CTL,
3261 rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
3262
3263 ice_flush(hw);
3264 }
3265
3266 /**
3267 * ice_free_irq_msix_misc - Unroll misc vector setup
3268 * @pf: board private structure
3269 */
ice_free_irq_msix_misc(struct ice_pf * pf)3270 static void ice_free_irq_msix_misc(struct ice_pf *pf)
3271 {
3272 int misc_irq_num = pf->oicr_irq.virq;
3273 struct ice_hw *hw = &pf->hw;
3274
3275 ice_dis_ctrlq_interrupts(hw);
3276
3277 /* disable OICR interrupt */
3278 wr32(hw, PFINT_OICR_ENA, 0);
3279 ice_flush(hw);
3280
3281 synchronize_irq(misc_irq_num);
3282 devm_free_irq(ice_pf_to_dev(pf), misc_irq_num, pf);
3283
3284 ice_free_irq(pf, pf->oicr_irq);
3285 }
3286
3287 /**
3288 * ice_ena_ctrlq_interrupts - enable control queue interrupts
3289 * @hw: pointer to HW structure
3290 * @reg_idx: HW vector index to associate the control queue interrupts with
3291 */
ice_ena_ctrlq_interrupts(struct ice_hw * hw,u16 reg_idx)3292 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
3293 {
3294 u32 val;
3295
3296 val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
3297 PFINT_OICR_CTL_CAUSE_ENA_M);
3298 wr32(hw, PFINT_OICR_CTL, val);
3299
3300 /* enable Admin queue Interrupt causes */
3301 val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
3302 PFINT_FW_CTL_CAUSE_ENA_M);
3303 wr32(hw, PFINT_FW_CTL, val);
3304
3305 /* enable Mailbox queue Interrupt causes */
3306 val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
3307 PFINT_MBX_CTL_CAUSE_ENA_M);
3308 wr32(hw, PFINT_MBX_CTL, val);
3309
3310 /* This enables Sideband queue Interrupt causes */
3311 val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
3312 PFINT_SB_CTL_CAUSE_ENA_M);
3313 wr32(hw, PFINT_SB_CTL, val);
3314
3315 ice_flush(hw);
3316 }
3317
3318 /**
3319 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
3320 * @pf: board private structure
3321 *
3322 * This sets up the handler for MSIX 0, which is used to manage the
3323 * non-queue interrupts, e.g. AdminQ and errors. This is not used
3324 * when in MSI or Legacy interrupt mode.
3325 */
ice_req_irq_msix_misc(struct ice_pf * pf)3326 static int ice_req_irq_msix_misc(struct ice_pf *pf)
3327 {
3328 struct device *dev = ice_pf_to_dev(pf);
3329 struct ice_hw *hw = &pf->hw;
3330 struct msi_map oicr_irq;
3331 int err = 0;
3332
3333 if (!pf->int_name[0])
3334 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
3335 dev_driver_string(dev), dev_name(dev));
3336
3337 /* Do not request IRQ but do enable OICR interrupt since settings are
3338 * lost during reset. Note that this function is called only during
3339 * rebuild path and not while reset is in progress.
3340 */
3341 if (ice_is_reset_in_progress(pf->state))
3342 goto skip_req_irq;
3343
3344 /* reserve one vector in irq_tracker for misc interrupts */
3345 oicr_irq = ice_alloc_irq(pf, false);
3346 if (oicr_irq.index < 0)
3347 return oicr_irq.index;
3348
3349 pf->oicr_irq = oicr_irq;
3350 err = devm_request_threaded_irq(dev, pf->oicr_irq.virq, ice_misc_intr,
3351 ice_misc_intr_thread_fn, 0,
3352 pf->int_name, pf);
3353 if (err) {
3354 dev_err(dev, "devm_request_threaded_irq for %s failed: %d\n",
3355 pf->int_name, err);
3356 ice_free_irq(pf, pf->oicr_irq);
3357 return err;
3358 }
3359
3360 skip_req_irq:
3361 ice_ena_misc_vector(pf);
3362
3363 ice_ena_ctrlq_interrupts(hw, pf->oicr_irq.index);
3364 wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_irq.index),
3365 ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
3366
3367 ice_flush(hw);
3368 ice_irq_dynamic_ena(hw, NULL, NULL);
3369
3370 return 0;
3371 }
3372
3373 /**
3374 * ice_napi_add - register NAPI handler for the VSI
3375 * @vsi: VSI for which NAPI handler is to be registered
3376 *
3377 * This function is only called in the driver's load path. Registering the NAPI
3378 * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
3379 * reset/rebuild, etc.)
3380 */
ice_napi_add(struct ice_vsi * vsi)3381 static void ice_napi_add(struct ice_vsi *vsi)
3382 {
3383 int v_idx;
3384
3385 if (!vsi->netdev)
3386 return;
3387
3388 ice_for_each_q_vector(vsi, v_idx)
3389 netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
3390 ice_napi_poll);
3391 }
3392
3393 /**
3394 * ice_set_ops - set netdev and ethtools ops for the given netdev
3395 * @vsi: the VSI associated with the new netdev
3396 */
ice_set_ops(struct ice_vsi * vsi)3397 static void ice_set_ops(struct ice_vsi *vsi)
3398 {
3399 struct net_device *netdev = vsi->netdev;
3400 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3401
3402 if (ice_is_safe_mode(pf)) {
3403 netdev->netdev_ops = &ice_netdev_safe_mode_ops;
3404 ice_set_ethtool_safe_mode_ops(netdev);
3405 return;
3406 }
3407
3408 netdev->netdev_ops = &ice_netdev_ops;
3409 netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
3410 ice_set_ethtool_ops(netdev);
3411
3412 if (vsi->type != ICE_VSI_PF)
3413 return;
3414
3415 netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
3416 NETDEV_XDP_ACT_XSK_ZEROCOPY |
3417 NETDEV_XDP_ACT_RX_SG;
3418 netdev->xdp_zc_max_segs = ICE_MAX_BUF_TXD;
3419 }
3420
3421 /**
3422 * ice_set_netdev_features - set features for the given netdev
3423 * @netdev: netdev instance
3424 */
ice_set_netdev_features(struct net_device * netdev)3425 static void ice_set_netdev_features(struct net_device *netdev)
3426 {
3427 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3428 bool is_dvm_ena = ice_is_dvm_ena(&pf->hw);
3429 netdev_features_t csumo_features;
3430 netdev_features_t vlano_features;
3431 netdev_features_t dflt_features;
3432 netdev_features_t tso_features;
3433
3434 if (ice_is_safe_mode(pf)) {
3435 /* safe mode */
3436 netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
3437 netdev->hw_features = netdev->features;
3438 return;
3439 }
3440
3441 dflt_features = NETIF_F_SG |
3442 NETIF_F_HIGHDMA |
3443 NETIF_F_NTUPLE |
3444 NETIF_F_RXHASH;
3445
3446 csumo_features = NETIF_F_RXCSUM |
3447 NETIF_F_IP_CSUM |
3448 NETIF_F_SCTP_CRC |
3449 NETIF_F_IPV6_CSUM;
3450
3451 vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
3452 NETIF_F_HW_VLAN_CTAG_TX |
3453 NETIF_F_HW_VLAN_CTAG_RX;
3454
3455 /* Enable CTAG/STAG filtering by default in Double VLAN Mode (DVM) */
3456 if (is_dvm_ena)
3457 vlano_features |= NETIF_F_HW_VLAN_STAG_FILTER;
3458
3459 tso_features = NETIF_F_TSO |
3460 NETIF_F_TSO_ECN |
3461 NETIF_F_TSO6 |
3462 NETIF_F_GSO_GRE |
3463 NETIF_F_GSO_UDP_TUNNEL |
3464 NETIF_F_GSO_GRE_CSUM |
3465 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3466 NETIF_F_GSO_PARTIAL |
3467 NETIF_F_GSO_IPXIP4 |
3468 NETIF_F_GSO_IPXIP6 |
3469 NETIF_F_GSO_UDP_L4;
3470
3471 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3472 NETIF_F_GSO_GRE_CSUM;
3473 /* set features that user can change */
3474 netdev->hw_features = dflt_features | csumo_features |
3475 vlano_features | tso_features;
3476
3477 /* add support for HW_CSUM on packets with MPLS header */
3478 netdev->mpls_features = NETIF_F_HW_CSUM |
3479 NETIF_F_TSO |
3480 NETIF_F_TSO6;
3481
3482 /* enable features */
3483 netdev->features |= netdev->hw_features;
3484
3485 netdev->hw_features |= NETIF_F_HW_TC;
3486 netdev->hw_features |= NETIF_F_LOOPBACK;
3487
3488 /* encap and VLAN devices inherit default, csumo and tso features */
3489 netdev->hw_enc_features |= dflt_features | csumo_features |
3490 tso_features;
3491 netdev->vlan_features |= dflt_features | csumo_features |
3492 tso_features;
3493
3494 /* advertise support but don't enable by default since only one type of
3495 * VLAN offload can be enabled at a time (i.e. CTAG or STAG). When one
3496 * type turns on the other has to be turned off. This is enforced by the
3497 * ice_fix_features() ndo callback.
3498 */
3499 if (is_dvm_ena)
3500 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX |
3501 NETIF_F_HW_VLAN_STAG_TX;
3502
3503 /* Leave CRC / FCS stripping enabled by default, but allow the value to
3504 * be changed at runtime
3505 */
3506 netdev->hw_features |= NETIF_F_RXFCS;
3507
3508 netif_set_tso_max_size(netdev, ICE_MAX_TSO_SIZE);
3509 }
3510
3511 /**
3512 * ice_fill_rss_lut - Fill the RSS lookup table with default values
3513 * @lut: Lookup table
3514 * @rss_table_size: Lookup table size
3515 * @rss_size: Range of queue number for hashing
3516 */
ice_fill_rss_lut(u8 * lut,u16 rss_table_size,u16 rss_size)3517 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3518 {
3519 u16 i;
3520
3521 for (i = 0; i < rss_table_size; i++)
3522 lut[i] = i % rss_size;
3523 }
3524
3525 /**
3526 * ice_pf_vsi_setup - Set up a PF VSI
3527 * @pf: board private structure
3528 * @pi: pointer to the port_info instance
3529 *
3530 * Returns pointer to the successfully allocated VSI software struct
3531 * on success, otherwise returns NULL on failure.
3532 */
3533 static struct ice_vsi *
ice_pf_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3534 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3535 {
3536 struct ice_vsi_cfg_params params = {};
3537
3538 params.type = ICE_VSI_PF;
3539 params.pi = pi;
3540 params.flags = ICE_VSI_FLAG_INIT;
3541
3542 return ice_vsi_setup(pf, ¶ms);
3543 }
3544
3545 static struct ice_vsi *
ice_chnl_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi,struct ice_channel * ch)3546 ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3547 struct ice_channel *ch)
3548 {
3549 struct ice_vsi_cfg_params params = {};
3550
3551 params.type = ICE_VSI_CHNL;
3552 params.pi = pi;
3553 params.ch = ch;
3554 params.flags = ICE_VSI_FLAG_INIT;
3555
3556 return ice_vsi_setup(pf, ¶ms);
3557 }
3558
3559 /**
3560 * ice_ctrl_vsi_setup - Set up a control VSI
3561 * @pf: board private structure
3562 * @pi: pointer to the port_info instance
3563 *
3564 * Returns pointer to the successfully allocated VSI software struct
3565 * on success, otherwise returns NULL on failure.
3566 */
3567 static struct ice_vsi *
ice_ctrl_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3568 ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3569 {
3570 struct ice_vsi_cfg_params params = {};
3571
3572 params.type = ICE_VSI_CTRL;
3573 params.pi = pi;
3574 params.flags = ICE_VSI_FLAG_INIT;
3575
3576 return ice_vsi_setup(pf, ¶ms);
3577 }
3578
3579 /**
3580 * ice_lb_vsi_setup - Set up a loopback VSI
3581 * @pf: board private structure
3582 * @pi: pointer to the port_info instance
3583 *
3584 * Returns pointer to the successfully allocated VSI software struct
3585 * on success, otherwise returns NULL on failure.
3586 */
3587 struct ice_vsi *
ice_lb_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3588 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3589 {
3590 struct ice_vsi_cfg_params params = {};
3591
3592 params.type = ICE_VSI_LB;
3593 params.pi = pi;
3594 params.flags = ICE_VSI_FLAG_INIT;
3595
3596 return ice_vsi_setup(pf, ¶ms);
3597 }
3598
3599 /**
3600 * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3601 * @netdev: network interface to be adjusted
3602 * @proto: VLAN TPID
3603 * @vid: VLAN ID to be added
3604 *
3605 * net_device_ops implementation for adding VLAN IDs
3606 */
3607 static int
ice_vlan_rx_add_vid(struct net_device * netdev,__be16 proto,u16 vid)3608 ice_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3609 {
3610 struct ice_netdev_priv *np = netdev_priv(netdev);
3611 struct ice_vsi_vlan_ops *vlan_ops;
3612 struct ice_vsi *vsi = np->vsi;
3613 struct ice_vlan vlan;
3614 int ret;
3615
3616 /* VLAN 0 is added by default during load/reset */
3617 if (!vid)
3618 return 0;
3619
3620 while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3621 usleep_range(1000, 2000);
3622
3623 /* Add multicast promisc rule for the VLAN ID to be added if
3624 * all-multicast is currently enabled.
3625 */
3626 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3627 ret = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3628 ICE_MCAST_VLAN_PROMISC_BITS,
3629 vid);
3630 if (ret)
3631 goto finish;
3632 }
3633
3634 vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3635
3636 /* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3637 * packets aren't pruned by the device's internal switch on Rx
3638 */
3639 vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3640 ret = vlan_ops->add_vlan(vsi, &vlan);
3641 if (ret)
3642 goto finish;
3643
3644 /* If all-multicast is currently enabled and this VLAN ID is only one
3645 * besides VLAN-0 we have to update look-up type of multicast promisc
3646 * rule for VLAN-0 from ICE_SW_LKUP_PROMISC to ICE_SW_LKUP_PROMISC_VLAN.
3647 */
3648 if ((vsi->current_netdev_flags & IFF_ALLMULTI) &&
3649 ice_vsi_num_non_zero_vlans(vsi) == 1) {
3650 ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3651 ICE_MCAST_PROMISC_BITS, 0);
3652 ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3653 ICE_MCAST_VLAN_PROMISC_BITS, 0);
3654 }
3655
3656 finish:
3657 clear_bit(ICE_CFG_BUSY, vsi->state);
3658
3659 return ret;
3660 }
3661
3662 /**
3663 * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3664 * @netdev: network interface to be adjusted
3665 * @proto: VLAN TPID
3666 * @vid: VLAN ID to be removed
3667 *
3668 * net_device_ops implementation for removing VLAN IDs
3669 */
3670 static int
ice_vlan_rx_kill_vid(struct net_device * netdev,__be16 proto,u16 vid)3671 ice_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3672 {
3673 struct ice_netdev_priv *np = netdev_priv(netdev);
3674 struct ice_vsi_vlan_ops *vlan_ops;
3675 struct ice_vsi *vsi = np->vsi;
3676 struct ice_vlan vlan;
3677 int ret;
3678
3679 /* don't allow removal of VLAN 0 */
3680 if (!vid)
3681 return 0;
3682
3683 while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3684 usleep_range(1000, 2000);
3685
3686 ret = ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3687 ICE_MCAST_VLAN_PROMISC_BITS, vid);
3688 if (ret) {
3689 netdev_err(netdev, "Error clearing multicast promiscuous mode on VSI %i\n",
3690 vsi->vsi_num);
3691 vsi->current_netdev_flags |= IFF_ALLMULTI;
3692 }
3693
3694 vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3695
3696 /* Make sure VLAN delete is successful before updating VLAN
3697 * information
3698 */
3699 vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3700 ret = vlan_ops->del_vlan(vsi, &vlan);
3701 if (ret)
3702 goto finish;
3703
3704 /* Remove multicast promisc rule for the removed VLAN ID if
3705 * all-multicast is enabled.
3706 */
3707 if (vsi->current_netdev_flags & IFF_ALLMULTI)
3708 ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3709 ICE_MCAST_VLAN_PROMISC_BITS, vid);
3710
3711 if (!ice_vsi_has_non_zero_vlans(vsi)) {
3712 /* Update look-up type of multicast promisc rule for VLAN 0
3713 * from ICE_SW_LKUP_PROMISC_VLAN to ICE_SW_LKUP_PROMISC when
3714 * all-multicast is enabled and VLAN 0 is the only VLAN rule.
3715 */
3716 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3717 ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3718 ICE_MCAST_VLAN_PROMISC_BITS,
3719 0);
3720 ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3721 ICE_MCAST_PROMISC_BITS, 0);
3722 }
3723 }
3724
3725 finish:
3726 clear_bit(ICE_CFG_BUSY, vsi->state);
3727
3728 return ret;
3729 }
3730
3731 /**
3732 * ice_rep_indr_tc_block_unbind
3733 * @cb_priv: indirection block private data
3734 */
ice_rep_indr_tc_block_unbind(void * cb_priv)3735 static void ice_rep_indr_tc_block_unbind(void *cb_priv)
3736 {
3737 struct ice_indr_block_priv *indr_priv = cb_priv;
3738
3739 list_del(&indr_priv->list);
3740 kfree(indr_priv);
3741 }
3742
3743 /**
3744 * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
3745 * @vsi: VSI struct which has the netdev
3746 */
ice_tc_indir_block_unregister(struct ice_vsi * vsi)3747 static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
3748 {
3749 struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
3750
3751 flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
3752 ice_rep_indr_tc_block_unbind);
3753 }
3754
3755 /**
3756 * ice_tc_indir_block_register - Register TC indirect block notifications
3757 * @vsi: VSI struct which has the netdev
3758 *
3759 * Returns 0 on success, negative value on failure
3760 */
ice_tc_indir_block_register(struct ice_vsi * vsi)3761 static int ice_tc_indir_block_register(struct ice_vsi *vsi)
3762 {
3763 struct ice_netdev_priv *np;
3764
3765 if (!vsi || !vsi->netdev)
3766 return -EINVAL;
3767
3768 np = netdev_priv(vsi->netdev);
3769
3770 INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
3771 return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
3772 }
3773
3774 /**
3775 * ice_get_avail_q_count - Get count of queues in use
3776 * @pf_qmap: bitmap to get queue use count from
3777 * @lock: pointer to a mutex that protects access to pf_qmap
3778 * @size: size of the bitmap
3779 */
3780 static u16
ice_get_avail_q_count(unsigned long * pf_qmap,struct mutex * lock,u16 size)3781 ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3782 {
3783 unsigned long bit;
3784 u16 count = 0;
3785
3786 mutex_lock(lock);
3787 for_each_clear_bit(bit, pf_qmap, size)
3788 count++;
3789 mutex_unlock(lock);
3790
3791 return count;
3792 }
3793
3794 /**
3795 * ice_get_avail_txq_count - Get count of Tx queues in use
3796 * @pf: pointer to an ice_pf instance
3797 */
ice_get_avail_txq_count(struct ice_pf * pf)3798 u16 ice_get_avail_txq_count(struct ice_pf *pf)
3799 {
3800 return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3801 pf->max_pf_txqs);
3802 }
3803
3804 /**
3805 * ice_get_avail_rxq_count - Get count of Rx queues in use
3806 * @pf: pointer to an ice_pf instance
3807 */
ice_get_avail_rxq_count(struct ice_pf * pf)3808 u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3809 {
3810 return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3811 pf->max_pf_rxqs);
3812 }
3813
3814 /**
3815 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3816 * @pf: board private structure to initialize
3817 */
ice_deinit_pf(struct ice_pf * pf)3818 static void ice_deinit_pf(struct ice_pf *pf)
3819 {
3820 ice_service_task_stop(pf);
3821 mutex_destroy(&pf->lag_mutex);
3822 mutex_destroy(&pf->adev_mutex);
3823 mutex_destroy(&pf->sw_mutex);
3824 mutex_destroy(&pf->tc_mutex);
3825 mutex_destroy(&pf->avail_q_mutex);
3826 mutex_destroy(&pf->vfs.table_lock);
3827
3828 if (pf->avail_txqs) {
3829 bitmap_free(pf->avail_txqs);
3830 pf->avail_txqs = NULL;
3831 }
3832
3833 if (pf->avail_rxqs) {
3834 bitmap_free(pf->avail_rxqs);
3835 pf->avail_rxqs = NULL;
3836 }
3837
3838 if (pf->ptp.clock)
3839 ptp_clock_unregister(pf->ptp.clock);
3840 }
3841
3842 /**
3843 * ice_set_pf_caps - set PFs capability flags
3844 * @pf: pointer to the PF instance
3845 */
ice_set_pf_caps(struct ice_pf * pf)3846 static void ice_set_pf_caps(struct ice_pf *pf)
3847 {
3848 struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
3849
3850 clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3851 if (func_caps->common_cap.rdma)
3852 set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3853 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3854 if (func_caps->common_cap.dcb)
3855 set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3856 clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3857 if (func_caps->common_cap.sr_iov_1_1) {
3858 set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3859 pf->vfs.num_supported = min_t(int, func_caps->num_allocd_vfs,
3860 ICE_MAX_SRIOV_VFS);
3861 }
3862 clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
3863 if (func_caps->common_cap.rss_table_size)
3864 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3865
3866 clear_bit(ICE_FLAG_FD_ENA, pf->flags);
3867 if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
3868 u16 unused;
3869
3870 /* ctrl_vsi_idx will be set to a valid value when flow director
3871 * is setup by ice_init_fdir
3872 */
3873 pf->ctrl_vsi_idx = ICE_NO_VSI;
3874 set_bit(ICE_FLAG_FD_ENA, pf->flags);
3875 /* force guaranteed filter pool for PF */
3876 ice_alloc_fd_guar_item(&pf->hw, &unused,
3877 func_caps->fd_fltr_guar);
3878 /* force shared filter pool for PF */
3879 ice_alloc_fd_shrd_item(&pf->hw, &unused,
3880 func_caps->fd_fltr_best_effort);
3881 }
3882
3883 clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3884 if (func_caps->common_cap.ieee_1588 &&
3885 !(pf->hw.mac_type == ICE_MAC_E830))
3886 set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3887
3888 pf->max_pf_txqs = func_caps->common_cap.num_txq;
3889 pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
3890 }
3891
3892 /**
3893 * ice_init_pf - Initialize general software structures (struct ice_pf)
3894 * @pf: board private structure to initialize
3895 */
ice_init_pf(struct ice_pf * pf)3896 static int ice_init_pf(struct ice_pf *pf)
3897 {
3898 ice_set_pf_caps(pf);
3899
3900 mutex_init(&pf->sw_mutex);
3901 mutex_init(&pf->tc_mutex);
3902 mutex_init(&pf->adev_mutex);
3903 mutex_init(&pf->lag_mutex);
3904
3905 INIT_HLIST_HEAD(&pf->aq_wait_list);
3906 spin_lock_init(&pf->aq_wait_lock);
3907 init_waitqueue_head(&pf->aq_wait_queue);
3908
3909 init_waitqueue_head(&pf->reset_wait_queue);
3910
3911 /* setup service timer and periodic service task */
3912 timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3913 pf->serv_tmr_period = HZ;
3914 INIT_WORK(&pf->serv_task, ice_service_task);
3915 clear_bit(ICE_SERVICE_SCHED, pf->state);
3916
3917 mutex_init(&pf->avail_q_mutex);
3918 pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
3919 if (!pf->avail_txqs)
3920 return -ENOMEM;
3921
3922 pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
3923 if (!pf->avail_rxqs) {
3924 bitmap_free(pf->avail_txqs);
3925 pf->avail_txqs = NULL;
3926 return -ENOMEM;
3927 }
3928
3929 mutex_init(&pf->vfs.table_lock);
3930 hash_init(pf->vfs.table);
3931 if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT))
3932 wr32(&pf->hw, E830_MBX_PF_IN_FLIGHT_VF_MSGS_THRESH,
3933 ICE_MBX_OVERFLOW_WATERMARK);
3934 else
3935 ice_mbx_init_snapshot(&pf->hw);
3936
3937 return 0;
3938 }
3939
3940 /**
3941 * ice_is_wol_supported - check if WoL is supported
3942 * @hw: pointer to hardware info
3943 *
3944 * Check if WoL is supported based on the HW configuration.
3945 * Returns true if NVM supports and enables WoL for this port, false otherwise
3946 */
ice_is_wol_supported(struct ice_hw * hw)3947 bool ice_is_wol_supported(struct ice_hw *hw)
3948 {
3949 u16 wol_ctrl;
3950
3951 /* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
3952 * word) indicates WoL is not supported on the corresponding PF ID.
3953 */
3954 if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
3955 return false;
3956
3957 return !(BIT(hw->port_info->lport) & wol_ctrl);
3958 }
3959
3960 /**
3961 * ice_vsi_recfg_qs - Change the number of queues on a VSI
3962 * @vsi: VSI being changed
3963 * @new_rx: new number of Rx queues
3964 * @new_tx: new number of Tx queues
3965 * @locked: is adev device_lock held
3966 *
3967 * Only change the number of queues if new_tx, or new_rx is non-0.
3968 *
3969 * Returns 0 on success.
3970 */
ice_vsi_recfg_qs(struct ice_vsi * vsi,int new_rx,int new_tx,bool locked)3971 int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx, bool locked)
3972 {
3973 struct ice_pf *pf = vsi->back;
3974 int i, err = 0, timeout = 50;
3975
3976 if (!new_rx && !new_tx)
3977 return -EINVAL;
3978
3979 while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
3980 timeout--;
3981 if (!timeout)
3982 return -EBUSY;
3983 usleep_range(1000, 2000);
3984 }
3985
3986 if (new_tx)
3987 vsi->req_txq = (u16)new_tx;
3988 if (new_rx)
3989 vsi->req_rxq = (u16)new_rx;
3990
3991 /* set for the next time the netdev is started */
3992 if (!netif_running(vsi->netdev)) {
3993 err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
3994 if (err)
3995 goto rebuild_err;
3996 dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
3997 goto done;
3998 }
3999
4000 ice_vsi_close(vsi);
4001 err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4002 if (err)
4003 goto rebuild_err;
4004
4005 ice_for_each_traffic_class(i) {
4006 if (vsi->tc_cfg.ena_tc & BIT(i))
4007 netdev_set_tc_queue(vsi->netdev,
4008 vsi->tc_cfg.tc_info[i].netdev_tc,
4009 vsi->tc_cfg.tc_info[i].qcount_tx,
4010 vsi->tc_cfg.tc_info[i].qoffset);
4011 }
4012 ice_pf_dcb_recfg(pf, locked);
4013 ice_vsi_open(vsi);
4014 goto done;
4015
4016 rebuild_err:
4017 dev_err(ice_pf_to_dev(pf), "Error during VSI rebuild: %d. Unload and reload the driver.\n",
4018 err);
4019 done:
4020 clear_bit(ICE_CFG_BUSY, pf->state);
4021 return err;
4022 }
4023
4024 /**
4025 * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
4026 * @pf: PF to configure
4027 *
4028 * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
4029 * VSI can still Tx/Rx VLAN tagged packets.
4030 */
ice_set_safe_mode_vlan_cfg(struct ice_pf * pf)4031 static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
4032 {
4033 struct ice_vsi *vsi = ice_get_main_vsi(pf);
4034 struct ice_vsi_ctx *ctxt;
4035 struct ice_hw *hw;
4036 int status;
4037
4038 if (!vsi)
4039 return;
4040
4041 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
4042 if (!ctxt)
4043 return;
4044
4045 hw = &pf->hw;
4046 ctxt->info = vsi->info;
4047
4048 ctxt->info.valid_sections =
4049 cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
4050 ICE_AQ_VSI_PROP_SECURITY_VALID |
4051 ICE_AQ_VSI_PROP_SW_VALID);
4052
4053 /* disable VLAN anti-spoof */
4054 ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4055 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4056
4057 /* disable VLAN pruning and keep all other settings */
4058 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
4059
4060 /* allow all VLANs on Tx and don't strip on Rx */
4061 ctxt->info.inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL |
4062 ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
4063
4064 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4065 if (status) {
4066 dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %d aq_err %s\n",
4067 status, ice_aq_str(hw->adminq.sq_last_status));
4068 } else {
4069 vsi->info.sec_flags = ctxt->info.sec_flags;
4070 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
4071 vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
4072 }
4073
4074 kfree(ctxt);
4075 }
4076
4077 /**
4078 * ice_log_pkg_init - log result of DDP package load
4079 * @hw: pointer to hardware info
4080 * @state: state of package load
4081 */
ice_log_pkg_init(struct ice_hw * hw,enum ice_ddp_state state)4082 static void ice_log_pkg_init(struct ice_hw *hw, enum ice_ddp_state state)
4083 {
4084 struct ice_pf *pf = hw->back;
4085 struct device *dev;
4086
4087 dev = ice_pf_to_dev(pf);
4088
4089 switch (state) {
4090 case ICE_DDP_PKG_SUCCESS:
4091 dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
4092 hw->active_pkg_name,
4093 hw->active_pkg_ver.major,
4094 hw->active_pkg_ver.minor,
4095 hw->active_pkg_ver.update,
4096 hw->active_pkg_ver.draft);
4097 break;
4098 case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
4099 dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
4100 hw->active_pkg_name,
4101 hw->active_pkg_ver.major,
4102 hw->active_pkg_ver.minor,
4103 hw->active_pkg_ver.update,
4104 hw->active_pkg_ver.draft);
4105 break;
4106 case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
4107 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",
4108 hw->active_pkg_name,
4109 hw->active_pkg_ver.major,
4110 hw->active_pkg_ver.minor,
4111 ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4112 break;
4113 case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
4114 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",
4115 hw->active_pkg_name,
4116 hw->active_pkg_ver.major,
4117 hw->active_pkg_ver.minor,
4118 hw->active_pkg_ver.update,
4119 hw->active_pkg_ver.draft,
4120 hw->pkg_name,
4121 hw->pkg_ver.major,
4122 hw->pkg_ver.minor,
4123 hw->pkg_ver.update,
4124 hw->pkg_ver.draft);
4125 break;
4126 case ICE_DDP_PKG_FW_MISMATCH:
4127 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");
4128 break;
4129 case ICE_DDP_PKG_INVALID_FILE:
4130 dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
4131 break;
4132 case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
4133 dev_err(dev, "The DDP package file version is higher than the driver supports. Please use an updated driver. Entering Safe Mode.\n");
4134 break;
4135 case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
4136 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",
4137 ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4138 break;
4139 case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
4140 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");
4141 break;
4142 case ICE_DDP_PKG_FILE_REVISION_TOO_LOW:
4143 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");
4144 break;
4145 case ICE_DDP_PKG_LOAD_ERROR:
4146 dev_err(dev, "An error occurred on the device while loading the DDP package. The device will be reset.\n");
4147 /* poll for reset to complete */
4148 if (ice_check_reset(hw))
4149 dev_err(dev, "Error resetting device. Please reload the driver\n");
4150 break;
4151 case ICE_DDP_PKG_ERR:
4152 default:
4153 dev_err(dev, "An unknown error occurred when loading the DDP package. Entering Safe Mode.\n");
4154 break;
4155 }
4156 }
4157
4158 /**
4159 * ice_load_pkg - load/reload the DDP Package file
4160 * @firmware: firmware structure when firmware requested or NULL for reload
4161 * @pf: pointer to the PF instance
4162 *
4163 * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
4164 * initialize HW tables.
4165 */
4166 static void
ice_load_pkg(const struct firmware * firmware,struct ice_pf * pf)4167 ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
4168 {
4169 enum ice_ddp_state state = ICE_DDP_PKG_ERR;
4170 struct device *dev = ice_pf_to_dev(pf);
4171 struct ice_hw *hw = &pf->hw;
4172
4173 /* Load DDP Package */
4174 if (firmware && !hw->pkg_copy) {
4175 state = ice_copy_and_init_pkg(hw, firmware->data,
4176 firmware->size);
4177 ice_log_pkg_init(hw, state);
4178 } else if (!firmware && hw->pkg_copy) {
4179 /* Reload package during rebuild after CORER/GLOBR reset */
4180 state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
4181 ice_log_pkg_init(hw, state);
4182 } else {
4183 dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
4184 }
4185
4186 if (!ice_is_init_pkg_successful(state)) {
4187 /* Safe Mode */
4188 clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4189 return;
4190 }
4191
4192 /* Successful download package is the precondition for advanced
4193 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
4194 */
4195 set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4196 }
4197
4198 /**
4199 * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
4200 * @pf: pointer to the PF structure
4201 *
4202 * There is no error returned here because the driver should be able to handle
4203 * 128 Byte cache lines, so we only print a warning in case issues are seen,
4204 * specifically with Tx.
4205 */
ice_verify_cacheline_size(struct ice_pf * pf)4206 static void ice_verify_cacheline_size(struct ice_pf *pf)
4207 {
4208 if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
4209 dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
4210 ICE_CACHE_LINE_BYTES);
4211 }
4212
4213 /**
4214 * ice_send_version - update firmware with driver version
4215 * @pf: PF struct
4216 *
4217 * Returns 0 on success, else error code
4218 */
ice_send_version(struct ice_pf * pf)4219 static int ice_send_version(struct ice_pf *pf)
4220 {
4221 struct ice_driver_ver dv;
4222
4223 dv.major_ver = 0xff;
4224 dv.minor_ver = 0xff;
4225 dv.build_ver = 0xff;
4226 dv.subbuild_ver = 0;
4227 strscpy((char *)dv.driver_string, UTS_RELEASE,
4228 sizeof(dv.driver_string));
4229 return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
4230 }
4231
4232 /**
4233 * ice_init_fdir - Initialize flow director VSI and configuration
4234 * @pf: pointer to the PF instance
4235 *
4236 * returns 0 on success, negative on error
4237 */
ice_init_fdir(struct ice_pf * pf)4238 static int ice_init_fdir(struct ice_pf *pf)
4239 {
4240 struct device *dev = ice_pf_to_dev(pf);
4241 struct ice_vsi *ctrl_vsi;
4242 int err;
4243
4244 /* Side Band Flow Director needs to have a control VSI.
4245 * Allocate it and store it in the PF.
4246 */
4247 ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
4248 if (!ctrl_vsi) {
4249 dev_dbg(dev, "could not create control VSI\n");
4250 return -ENOMEM;
4251 }
4252
4253 err = ice_vsi_open_ctrl(ctrl_vsi);
4254 if (err) {
4255 dev_dbg(dev, "could not open control VSI\n");
4256 goto err_vsi_open;
4257 }
4258
4259 mutex_init(&pf->hw.fdir_fltr_lock);
4260
4261 err = ice_fdir_create_dflt_rules(pf);
4262 if (err)
4263 goto err_fdir_rule;
4264
4265 return 0;
4266
4267 err_fdir_rule:
4268 ice_fdir_release_flows(&pf->hw);
4269 ice_vsi_close(ctrl_vsi);
4270 err_vsi_open:
4271 ice_vsi_release(ctrl_vsi);
4272 if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4273 pf->vsi[pf->ctrl_vsi_idx] = NULL;
4274 pf->ctrl_vsi_idx = ICE_NO_VSI;
4275 }
4276 return err;
4277 }
4278
ice_deinit_fdir(struct ice_pf * pf)4279 static void ice_deinit_fdir(struct ice_pf *pf)
4280 {
4281 struct ice_vsi *vsi = ice_get_ctrl_vsi(pf);
4282
4283 if (!vsi)
4284 return;
4285
4286 ice_vsi_manage_fdir(vsi, false);
4287 ice_vsi_release(vsi);
4288 if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4289 pf->vsi[pf->ctrl_vsi_idx] = NULL;
4290 pf->ctrl_vsi_idx = ICE_NO_VSI;
4291 }
4292
4293 mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4294 }
4295
4296 /**
4297 * ice_get_opt_fw_name - return optional firmware file name or NULL
4298 * @pf: pointer to the PF instance
4299 */
ice_get_opt_fw_name(struct ice_pf * pf)4300 static char *ice_get_opt_fw_name(struct ice_pf *pf)
4301 {
4302 /* Optional firmware name same as default with additional dash
4303 * followed by a EUI-64 identifier (PCIe Device Serial Number)
4304 */
4305 struct pci_dev *pdev = pf->pdev;
4306 char *opt_fw_filename;
4307 u64 dsn;
4308
4309 /* Determine the name of the optional file using the DSN (two
4310 * dwords following the start of the DSN Capability).
4311 */
4312 dsn = pci_get_dsn(pdev);
4313 if (!dsn)
4314 return NULL;
4315
4316 opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
4317 if (!opt_fw_filename)
4318 return NULL;
4319
4320 snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
4321 ICE_DDP_PKG_PATH, dsn);
4322
4323 return opt_fw_filename;
4324 }
4325
4326 /**
4327 * ice_request_fw - Device initialization routine
4328 * @pf: pointer to the PF instance
4329 */
ice_request_fw(struct ice_pf * pf)4330 static void ice_request_fw(struct ice_pf *pf)
4331 {
4332 char *opt_fw_filename = ice_get_opt_fw_name(pf);
4333 const struct firmware *firmware = NULL;
4334 struct device *dev = ice_pf_to_dev(pf);
4335 int err = 0;
4336
4337 /* optional device-specific DDP (if present) overrides the default DDP
4338 * package file. kernel logs a debug message if the file doesn't exist,
4339 * and warning messages for other errors.
4340 */
4341 if (opt_fw_filename) {
4342 err = firmware_request_nowarn(&firmware, opt_fw_filename, dev);
4343 if (err) {
4344 kfree(opt_fw_filename);
4345 goto dflt_pkg_load;
4346 }
4347
4348 /* request for firmware was successful. Download to device */
4349 ice_load_pkg(firmware, pf);
4350 kfree(opt_fw_filename);
4351 release_firmware(firmware);
4352 return;
4353 }
4354
4355 dflt_pkg_load:
4356 err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev);
4357 if (err) {
4358 dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
4359 return;
4360 }
4361
4362 /* request for firmware was successful. Download to device */
4363 ice_load_pkg(firmware, pf);
4364 release_firmware(firmware);
4365 }
4366
4367 /**
4368 * ice_print_wake_reason - show the wake up cause in the log
4369 * @pf: pointer to the PF struct
4370 */
ice_print_wake_reason(struct ice_pf * pf)4371 static void ice_print_wake_reason(struct ice_pf *pf)
4372 {
4373 u32 wus = pf->wakeup_reason;
4374 const char *wake_str;
4375
4376 /* if no wake event, nothing to print */
4377 if (!wus)
4378 return;
4379
4380 if (wus & PFPM_WUS_LNKC_M)
4381 wake_str = "Link\n";
4382 else if (wus & PFPM_WUS_MAG_M)
4383 wake_str = "Magic Packet\n";
4384 else if (wus & PFPM_WUS_MNG_M)
4385 wake_str = "Management\n";
4386 else if (wus & PFPM_WUS_FW_RST_WK_M)
4387 wake_str = "Firmware Reset\n";
4388 else
4389 wake_str = "Unknown\n";
4390
4391 dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4392 }
4393
4394 /**
4395 * ice_register_netdev - register netdev
4396 * @vsi: pointer to the VSI struct
4397 */
ice_register_netdev(struct ice_vsi * vsi)4398 static int ice_register_netdev(struct ice_vsi *vsi)
4399 {
4400 int err;
4401
4402 if (!vsi || !vsi->netdev)
4403 return -EIO;
4404
4405 err = register_netdev(vsi->netdev);
4406 if (err)
4407 return err;
4408
4409 set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4410 netif_carrier_off(vsi->netdev);
4411 netif_tx_stop_all_queues(vsi->netdev);
4412
4413 return 0;
4414 }
4415
ice_unregister_netdev(struct ice_vsi * vsi)4416 static void ice_unregister_netdev(struct ice_vsi *vsi)
4417 {
4418 if (!vsi || !vsi->netdev)
4419 return;
4420
4421 unregister_netdev(vsi->netdev);
4422 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4423 }
4424
4425 /**
4426 * ice_cfg_netdev - Allocate, configure and register a netdev
4427 * @vsi: the VSI associated with the new netdev
4428 *
4429 * Returns 0 on success, negative value on failure
4430 */
ice_cfg_netdev(struct ice_vsi * vsi)4431 static int ice_cfg_netdev(struct ice_vsi *vsi)
4432 {
4433 struct ice_netdev_priv *np;
4434 struct net_device *netdev;
4435 u8 mac_addr[ETH_ALEN];
4436
4437 netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
4438 vsi->alloc_rxq);
4439 if (!netdev)
4440 return -ENOMEM;
4441
4442 set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4443 vsi->netdev = netdev;
4444 np = netdev_priv(netdev);
4445 np->vsi = vsi;
4446
4447 ice_set_netdev_features(netdev);
4448 ice_set_ops(vsi);
4449
4450 if (vsi->type == ICE_VSI_PF) {
4451 SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
4452 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4453 eth_hw_addr_set(netdev, mac_addr);
4454 }
4455
4456 netdev->priv_flags |= IFF_UNICAST_FLT;
4457
4458 /* Setup netdev TC information */
4459 ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
4460
4461 netdev->max_mtu = ICE_MAX_MTU;
4462
4463 return 0;
4464 }
4465
ice_decfg_netdev(struct ice_vsi * vsi)4466 static void ice_decfg_netdev(struct ice_vsi *vsi)
4467 {
4468 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4469 free_netdev(vsi->netdev);
4470 vsi->netdev = NULL;
4471 }
4472
ice_start_eth(struct ice_vsi * vsi)4473 static int ice_start_eth(struct ice_vsi *vsi)
4474 {
4475 int err;
4476
4477 err = ice_init_mac_fltr(vsi->back);
4478 if (err)
4479 return err;
4480
4481 err = ice_vsi_open(vsi);
4482 if (err)
4483 ice_fltr_remove_all(vsi);
4484
4485 return err;
4486 }
4487
ice_stop_eth(struct ice_vsi * vsi)4488 static void ice_stop_eth(struct ice_vsi *vsi)
4489 {
4490 ice_fltr_remove_all(vsi);
4491 ice_vsi_close(vsi);
4492 }
4493
ice_init_eth(struct ice_pf * pf)4494 static int ice_init_eth(struct ice_pf *pf)
4495 {
4496 struct ice_vsi *vsi = ice_get_main_vsi(pf);
4497 int err;
4498
4499 if (!vsi)
4500 return -EINVAL;
4501
4502 /* init channel list */
4503 INIT_LIST_HEAD(&vsi->ch_list);
4504
4505 err = ice_cfg_netdev(vsi);
4506 if (err)
4507 return err;
4508 /* Setup DCB netlink interface */
4509 ice_dcbnl_setup(vsi);
4510
4511 err = ice_init_mac_fltr(pf);
4512 if (err)
4513 goto err_init_mac_fltr;
4514
4515 err = ice_devlink_create_pf_port(pf);
4516 if (err)
4517 goto err_devlink_create_pf_port;
4518
4519 SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
4520
4521 err = ice_register_netdev(vsi);
4522 if (err)
4523 goto err_register_netdev;
4524
4525 err = ice_tc_indir_block_register(vsi);
4526 if (err)
4527 goto err_tc_indir_block_register;
4528
4529 ice_napi_add(vsi);
4530
4531 return 0;
4532
4533 err_tc_indir_block_register:
4534 ice_unregister_netdev(vsi);
4535 err_register_netdev:
4536 ice_devlink_destroy_pf_port(pf);
4537 err_devlink_create_pf_port:
4538 err_init_mac_fltr:
4539 ice_decfg_netdev(vsi);
4540 return err;
4541 }
4542
ice_deinit_eth(struct ice_pf * pf)4543 static void ice_deinit_eth(struct ice_pf *pf)
4544 {
4545 struct ice_vsi *vsi = ice_get_main_vsi(pf);
4546
4547 if (!vsi)
4548 return;
4549
4550 ice_vsi_close(vsi);
4551 ice_unregister_netdev(vsi);
4552 ice_devlink_destroy_pf_port(pf);
4553 ice_tc_indir_block_unregister(vsi);
4554 ice_decfg_netdev(vsi);
4555 }
4556
4557 /**
4558 * ice_wait_for_fw - wait for full FW readiness
4559 * @hw: pointer to the hardware structure
4560 * @timeout: milliseconds that can elapse before timing out
4561 */
ice_wait_for_fw(struct ice_hw * hw,u32 timeout)4562 static int ice_wait_for_fw(struct ice_hw *hw, u32 timeout)
4563 {
4564 int fw_loading;
4565 u32 elapsed = 0;
4566
4567 while (elapsed <= timeout) {
4568 fw_loading = rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M;
4569
4570 /* firmware was not yet loaded, we have to wait more */
4571 if (fw_loading) {
4572 elapsed += 100;
4573 msleep(100);
4574 continue;
4575 }
4576 return 0;
4577 }
4578
4579 return -ETIMEDOUT;
4580 }
4581
ice_init_dev(struct ice_pf * pf)4582 static int ice_init_dev(struct ice_pf *pf)
4583 {
4584 struct device *dev = ice_pf_to_dev(pf);
4585 struct ice_hw *hw = &pf->hw;
4586 int err;
4587
4588 err = ice_init_hw(hw);
4589 if (err) {
4590 dev_err(dev, "ice_init_hw failed: %d\n", err);
4591 return err;
4592 }
4593
4594 /* Some cards require longer initialization times
4595 * due to necessity of loading FW from an external source.
4596 * This can take even half a minute.
4597 */
4598 if (ice_is_pf_c827(hw)) {
4599 err = ice_wait_for_fw(hw, 30000);
4600 if (err) {
4601 dev_err(dev, "ice_wait_for_fw timed out");
4602 return err;
4603 }
4604 }
4605
4606 ice_init_feature_support(pf);
4607
4608 ice_request_fw(pf);
4609
4610 /* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
4611 * set in pf->state, which will cause ice_is_safe_mode to return
4612 * true
4613 */
4614 if (ice_is_safe_mode(pf)) {
4615 /* we already got function/device capabilities but these don't
4616 * reflect what the driver needs to do in safe mode. Instead of
4617 * adding conditional logic everywhere to ignore these
4618 * device/function capabilities, override them.
4619 */
4620 ice_set_safe_mode_caps(hw);
4621 }
4622
4623 err = ice_init_pf(pf);
4624 if (err) {
4625 dev_err(dev, "ice_init_pf failed: %d\n", err);
4626 goto err_init_pf;
4627 }
4628
4629 pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4630 pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4631 pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4632 pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4633 if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4634 pf->hw.udp_tunnel_nic.tables[0].n_entries =
4635 pf->hw.tnl.valid_count[TNL_VXLAN];
4636 pf->hw.udp_tunnel_nic.tables[0].tunnel_types =
4637 UDP_TUNNEL_TYPE_VXLAN;
4638 }
4639 if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4640 pf->hw.udp_tunnel_nic.tables[1].n_entries =
4641 pf->hw.tnl.valid_count[TNL_GENEVE];
4642 pf->hw.udp_tunnel_nic.tables[1].tunnel_types =
4643 UDP_TUNNEL_TYPE_GENEVE;
4644 }
4645
4646 err = ice_init_interrupt_scheme(pf);
4647 if (err) {
4648 dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4649 err = -EIO;
4650 goto err_init_interrupt_scheme;
4651 }
4652
4653 /* In case of MSIX we are going to setup the misc vector right here
4654 * to handle admin queue events etc. In case of legacy and MSI
4655 * the misc functionality and queue processing is combined in
4656 * the same vector and that gets setup at open.
4657 */
4658 err = ice_req_irq_msix_misc(pf);
4659 if (err) {
4660 dev_err(dev, "setup of misc vector failed: %d\n", err);
4661 goto err_req_irq_msix_misc;
4662 }
4663
4664 return 0;
4665
4666 err_req_irq_msix_misc:
4667 ice_clear_interrupt_scheme(pf);
4668 err_init_interrupt_scheme:
4669 ice_deinit_pf(pf);
4670 err_init_pf:
4671 ice_deinit_hw(hw);
4672 return err;
4673 }
4674
ice_deinit_dev(struct ice_pf * pf)4675 static void ice_deinit_dev(struct ice_pf *pf)
4676 {
4677 ice_free_irq_msix_misc(pf);
4678 ice_deinit_pf(pf);
4679 ice_deinit_hw(&pf->hw);
4680
4681 /* Service task is already stopped, so call reset directly. */
4682 ice_reset(&pf->hw, ICE_RESET_PFR);
4683 pci_wait_for_pending_transaction(pf->pdev);
4684 ice_clear_interrupt_scheme(pf);
4685 }
4686
ice_init_features(struct ice_pf * pf)4687 static void ice_init_features(struct ice_pf *pf)
4688 {
4689 struct device *dev = ice_pf_to_dev(pf);
4690
4691 if (ice_is_safe_mode(pf))
4692 return;
4693
4694 /* initialize DDP driven features */
4695 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4696 ice_ptp_init(pf);
4697
4698 if (ice_is_feature_supported(pf, ICE_F_GNSS))
4699 ice_gnss_init(pf);
4700
4701 /* Note: Flow director init failure is non-fatal to load */
4702 if (ice_init_fdir(pf))
4703 dev_err(dev, "could not initialize flow director\n");
4704
4705 /* Note: DCB init failure is non-fatal to load */
4706 if (ice_init_pf_dcb(pf, false)) {
4707 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4708 clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4709 } else {
4710 ice_cfg_lldp_mib_change(&pf->hw, true);
4711 }
4712
4713 if (ice_init_lag(pf))
4714 dev_warn(dev, "Failed to init link aggregation support\n");
4715 }
4716
ice_deinit_features(struct ice_pf * pf)4717 static void ice_deinit_features(struct ice_pf *pf)
4718 {
4719 if (ice_is_safe_mode(pf))
4720 return;
4721
4722 ice_deinit_lag(pf);
4723 if (test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags))
4724 ice_cfg_lldp_mib_change(&pf->hw, false);
4725 ice_deinit_fdir(pf);
4726 if (ice_is_feature_supported(pf, ICE_F_GNSS))
4727 ice_gnss_exit(pf);
4728 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4729 ice_ptp_release(pf);
4730 }
4731
ice_init_wakeup(struct ice_pf * pf)4732 static void ice_init_wakeup(struct ice_pf *pf)
4733 {
4734 /* Save wakeup reason register for later use */
4735 pf->wakeup_reason = rd32(&pf->hw, PFPM_WUS);
4736
4737 /* check for a power management event */
4738 ice_print_wake_reason(pf);
4739
4740 /* clear wake status, all bits */
4741 wr32(&pf->hw, PFPM_WUS, U32_MAX);
4742
4743 /* Disable WoL at init, wait for user to enable */
4744 device_set_wakeup_enable(ice_pf_to_dev(pf), false);
4745 }
4746
ice_init_link(struct ice_pf * pf)4747 static int ice_init_link(struct ice_pf *pf)
4748 {
4749 struct device *dev = ice_pf_to_dev(pf);
4750 int err;
4751
4752 err = ice_init_link_events(pf->hw.port_info);
4753 if (err) {
4754 dev_err(dev, "ice_init_link_events failed: %d\n", err);
4755 return err;
4756 }
4757
4758 /* not a fatal error if this fails */
4759 err = ice_init_nvm_phy_type(pf->hw.port_info);
4760 if (err)
4761 dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4762
4763 /* not a fatal error if this fails */
4764 err = ice_update_link_info(pf->hw.port_info);
4765 if (err)
4766 dev_err(dev, "ice_update_link_info failed: %d\n", err);
4767
4768 ice_init_link_dflt_override(pf->hw.port_info);
4769
4770 ice_check_link_cfg_err(pf,
4771 pf->hw.port_info->phy.link_info.link_cfg_err);
4772
4773 /* if media available, initialize PHY settings */
4774 if (pf->hw.port_info->phy.link_info.link_info &
4775 ICE_AQ_MEDIA_AVAILABLE) {
4776 /* not a fatal error if this fails */
4777 err = ice_init_phy_user_cfg(pf->hw.port_info);
4778 if (err)
4779 dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4780
4781 if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4782 struct ice_vsi *vsi = ice_get_main_vsi(pf);
4783
4784 if (vsi)
4785 ice_configure_phy(vsi);
4786 }
4787 } else {
4788 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4789 }
4790
4791 return err;
4792 }
4793
ice_init_pf_sw(struct ice_pf * pf)4794 static int ice_init_pf_sw(struct ice_pf *pf)
4795 {
4796 bool dvm = ice_is_dvm_ena(&pf->hw);
4797 struct ice_vsi *vsi;
4798 int err;
4799
4800 /* create switch struct for the switch element created by FW on boot */
4801 pf->first_sw = kzalloc(sizeof(*pf->first_sw), GFP_KERNEL);
4802 if (!pf->first_sw)
4803 return -ENOMEM;
4804
4805 if (pf->hw.evb_veb)
4806 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4807 else
4808 pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4809
4810 pf->first_sw->pf = pf;
4811
4812 /* record the sw_id available for later use */
4813 pf->first_sw->sw_id = pf->hw.port_info->sw_id;
4814
4815 err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
4816 if (err)
4817 goto err_aq_set_port_params;
4818
4819 vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
4820 if (!vsi) {
4821 err = -ENOMEM;
4822 goto err_pf_vsi_setup;
4823 }
4824
4825 return 0;
4826
4827 err_pf_vsi_setup:
4828 err_aq_set_port_params:
4829 kfree(pf->first_sw);
4830 return err;
4831 }
4832
ice_deinit_pf_sw(struct ice_pf * pf)4833 static void ice_deinit_pf_sw(struct ice_pf *pf)
4834 {
4835 struct ice_vsi *vsi = ice_get_main_vsi(pf);
4836
4837 if (!vsi)
4838 return;
4839
4840 ice_vsi_release(vsi);
4841 kfree(pf->first_sw);
4842 }
4843
ice_alloc_vsis(struct ice_pf * pf)4844 static int ice_alloc_vsis(struct ice_pf *pf)
4845 {
4846 struct device *dev = ice_pf_to_dev(pf);
4847
4848 pf->num_alloc_vsi = pf->hw.func_caps.guar_num_vsi;
4849 if (!pf->num_alloc_vsi)
4850 return -EIO;
4851
4852 if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
4853 dev_warn(dev,
4854 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
4855 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
4856 pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
4857 }
4858
4859 pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
4860 GFP_KERNEL);
4861 if (!pf->vsi)
4862 return -ENOMEM;
4863
4864 pf->vsi_stats = devm_kcalloc(dev, pf->num_alloc_vsi,
4865 sizeof(*pf->vsi_stats), GFP_KERNEL);
4866 if (!pf->vsi_stats) {
4867 devm_kfree(dev, pf->vsi);
4868 return -ENOMEM;
4869 }
4870
4871 return 0;
4872 }
4873
ice_dealloc_vsis(struct ice_pf * pf)4874 static void ice_dealloc_vsis(struct ice_pf *pf)
4875 {
4876 devm_kfree(ice_pf_to_dev(pf), pf->vsi_stats);
4877 pf->vsi_stats = NULL;
4878
4879 pf->num_alloc_vsi = 0;
4880 devm_kfree(ice_pf_to_dev(pf), pf->vsi);
4881 pf->vsi = NULL;
4882 }
4883
ice_init_devlink(struct ice_pf * pf)4884 static int ice_init_devlink(struct ice_pf *pf)
4885 {
4886 int err;
4887
4888 err = ice_devlink_register_params(pf);
4889 if (err)
4890 return err;
4891
4892 ice_devlink_init_regions(pf);
4893 ice_devlink_register(pf);
4894
4895 return 0;
4896 }
4897
ice_deinit_devlink(struct ice_pf * pf)4898 static void ice_deinit_devlink(struct ice_pf *pf)
4899 {
4900 ice_devlink_unregister(pf);
4901 ice_devlink_destroy_regions(pf);
4902 ice_devlink_unregister_params(pf);
4903 }
4904
ice_init(struct ice_pf * pf)4905 static int ice_init(struct ice_pf *pf)
4906 {
4907 int err;
4908
4909 err = ice_init_dev(pf);
4910 if (err)
4911 return err;
4912
4913 err = ice_alloc_vsis(pf);
4914 if (err)
4915 goto err_alloc_vsis;
4916
4917 err = ice_init_pf_sw(pf);
4918 if (err)
4919 goto err_init_pf_sw;
4920
4921 ice_init_wakeup(pf);
4922
4923 err = ice_init_link(pf);
4924 if (err)
4925 goto err_init_link;
4926
4927 err = ice_send_version(pf);
4928 if (err)
4929 goto err_init_link;
4930
4931 ice_verify_cacheline_size(pf);
4932
4933 if (ice_is_safe_mode(pf))
4934 ice_set_safe_mode_vlan_cfg(pf);
4935 else
4936 /* print PCI link speed and width */
4937 pcie_print_link_status(pf->pdev);
4938
4939 /* ready to go, so clear down state bit */
4940 clear_bit(ICE_DOWN, pf->state);
4941 clear_bit(ICE_SERVICE_DIS, pf->state);
4942
4943 /* since everything is good, start the service timer */
4944 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4945
4946 return 0;
4947
4948 err_init_link:
4949 ice_deinit_pf_sw(pf);
4950 err_init_pf_sw:
4951 ice_dealloc_vsis(pf);
4952 err_alloc_vsis:
4953 ice_deinit_dev(pf);
4954 return err;
4955 }
4956
ice_deinit(struct ice_pf * pf)4957 static void ice_deinit(struct ice_pf *pf)
4958 {
4959 set_bit(ICE_SERVICE_DIS, pf->state);
4960 set_bit(ICE_DOWN, pf->state);
4961
4962 ice_deinit_pf_sw(pf);
4963 ice_dealloc_vsis(pf);
4964 ice_deinit_dev(pf);
4965 }
4966
4967 /**
4968 * ice_load - load pf by init hw and starting VSI
4969 * @pf: pointer to the pf instance
4970 */
ice_load(struct ice_pf * pf)4971 int ice_load(struct ice_pf *pf)
4972 {
4973 struct ice_vsi_cfg_params params = {};
4974 struct ice_vsi *vsi;
4975 int err;
4976
4977 err = ice_init_dev(pf);
4978 if (err)
4979 return err;
4980
4981 vsi = ice_get_main_vsi(pf);
4982
4983 params = ice_vsi_to_params(vsi);
4984 params.flags = ICE_VSI_FLAG_INIT;
4985
4986 rtnl_lock();
4987 err = ice_vsi_cfg(vsi, ¶ms);
4988 if (err)
4989 goto err_vsi_cfg;
4990
4991 err = ice_start_eth(ice_get_main_vsi(pf));
4992 if (err)
4993 goto err_start_eth;
4994 rtnl_unlock();
4995
4996 err = ice_init_rdma(pf);
4997 if (err)
4998 goto err_init_rdma;
4999
5000 ice_init_features(pf);
5001 ice_service_task_restart(pf);
5002
5003 clear_bit(ICE_DOWN, pf->state);
5004
5005 return 0;
5006
5007 err_init_rdma:
5008 ice_vsi_close(ice_get_main_vsi(pf));
5009 rtnl_lock();
5010 err_start_eth:
5011 ice_vsi_decfg(ice_get_main_vsi(pf));
5012 err_vsi_cfg:
5013 rtnl_unlock();
5014 ice_deinit_dev(pf);
5015 return err;
5016 }
5017
5018 /**
5019 * ice_unload - unload pf by stopping VSI and deinit hw
5020 * @pf: pointer to the pf instance
5021 */
ice_unload(struct ice_pf * pf)5022 void ice_unload(struct ice_pf *pf)
5023 {
5024 ice_deinit_features(pf);
5025 ice_deinit_rdma(pf);
5026 rtnl_lock();
5027 ice_stop_eth(ice_get_main_vsi(pf));
5028 ice_vsi_decfg(ice_get_main_vsi(pf));
5029 rtnl_unlock();
5030 ice_deinit_dev(pf);
5031 }
5032
5033 /**
5034 * ice_probe - Device initialization routine
5035 * @pdev: PCI device information struct
5036 * @ent: entry in ice_pci_tbl
5037 *
5038 * Returns 0 on success, negative on failure
5039 */
5040 static int
ice_probe(struct pci_dev * pdev,const struct pci_device_id __always_unused * ent)5041 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
5042 {
5043 struct device *dev = &pdev->dev;
5044 struct ice_pf *pf;
5045 struct ice_hw *hw;
5046 int err;
5047
5048 if (pdev->is_virtfn) {
5049 dev_err(dev, "can't probe a virtual function\n");
5050 return -EINVAL;
5051 }
5052
5053 /* when under a kdump kernel initiate a reset before enabling the
5054 * device in order to clear out any pending DMA transactions. These
5055 * transactions can cause some systems to machine check when doing
5056 * the pcim_enable_device() below.
5057 */
5058 if (is_kdump_kernel()) {
5059 pci_save_state(pdev);
5060 pci_clear_master(pdev);
5061 err = pcie_flr(pdev);
5062 if (err)
5063 return err;
5064 pci_restore_state(pdev);
5065 }
5066
5067 /* this driver uses devres, see
5068 * Documentation/driver-api/driver-model/devres.rst
5069 */
5070 err = pcim_enable_device(pdev);
5071 if (err)
5072 return err;
5073
5074 err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
5075 if (err) {
5076 dev_err(dev, "BAR0 I/O map error %d\n", err);
5077 return err;
5078 }
5079
5080 pf = ice_allocate_pf(dev);
5081 if (!pf)
5082 return -ENOMEM;
5083
5084 /* initialize Auxiliary index to invalid value */
5085 pf->aux_idx = -1;
5086
5087 /* set up for high or low DMA */
5088 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
5089 if (err) {
5090 dev_err(dev, "DMA configuration failed: 0x%x\n", err);
5091 return err;
5092 }
5093
5094 pci_set_master(pdev);
5095
5096 pf->pdev = pdev;
5097 pci_set_drvdata(pdev, pf);
5098 set_bit(ICE_DOWN, pf->state);
5099 /* Disable service task until DOWN bit is cleared */
5100 set_bit(ICE_SERVICE_DIS, pf->state);
5101
5102 hw = &pf->hw;
5103 hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
5104 pci_save_state(pdev);
5105
5106 hw->back = pf;
5107 hw->port_info = NULL;
5108 hw->vendor_id = pdev->vendor;
5109 hw->device_id = pdev->device;
5110 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
5111 hw->subsystem_vendor_id = pdev->subsystem_vendor;
5112 hw->subsystem_device_id = pdev->subsystem_device;
5113 hw->bus.device = PCI_SLOT(pdev->devfn);
5114 hw->bus.func = PCI_FUNC(pdev->devfn);
5115 ice_set_ctrlq_len(hw);
5116
5117 pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
5118
5119 #ifndef CONFIG_DYNAMIC_DEBUG
5120 if (debug < -1)
5121 hw->debug_mask = debug;
5122 #endif
5123
5124 err = ice_init(pf);
5125 if (err)
5126 goto err_init;
5127
5128 err = ice_init_eth(pf);
5129 if (err)
5130 goto err_init_eth;
5131
5132 err = ice_init_rdma(pf);
5133 if (err)
5134 goto err_init_rdma;
5135
5136 err = ice_init_devlink(pf);
5137 if (err)
5138 goto err_init_devlink;
5139
5140 ice_init_features(pf);
5141
5142 return 0;
5143
5144 err_init_devlink:
5145 ice_deinit_rdma(pf);
5146 err_init_rdma:
5147 ice_deinit_eth(pf);
5148 err_init_eth:
5149 ice_deinit(pf);
5150 err_init:
5151 pci_disable_device(pdev);
5152 return err;
5153 }
5154
5155 /**
5156 * ice_set_wake - enable or disable Wake on LAN
5157 * @pf: pointer to the PF struct
5158 *
5159 * Simple helper for WoL control
5160 */
ice_set_wake(struct ice_pf * pf)5161 static void ice_set_wake(struct ice_pf *pf)
5162 {
5163 struct ice_hw *hw = &pf->hw;
5164 bool wol = pf->wol_ena;
5165
5166 /* clear wake state, otherwise new wake events won't fire */
5167 wr32(hw, PFPM_WUS, U32_MAX);
5168
5169 /* enable / disable APM wake up, no RMW needed */
5170 wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
5171
5172 /* set magic packet filter enabled */
5173 wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
5174 }
5175
5176 /**
5177 * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
5178 * @pf: pointer to the PF struct
5179 *
5180 * Issue firmware command to enable multicast magic wake, making
5181 * sure that any locally administered address (LAA) is used for
5182 * wake, and that PF reset doesn't undo the LAA.
5183 */
ice_setup_mc_magic_wake(struct ice_pf * pf)5184 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
5185 {
5186 struct device *dev = ice_pf_to_dev(pf);
5187 struct ice_hw *hw = &pf->hw;
5188 u8 mac_addr[ETH_ALEN];
5189 struct ice_vsi *vsi;
5190 int status;
5191 u8 flags;
5192
5193 if (!pf->wol_ena)
5194 return;
5195
5196 vsi = ice_get_main_vsi(pf);
5197 if (!vsi)
5198 return;
5199
5200 /* Get current MAC address in case it's an LAA */
5201 if (vsi->netdev)
5202 ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
5203 else
5204 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
5205
5206 flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
5207 ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
5208 ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
5209
5210 status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
5211 if (status)
5212 dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
5213 status, ice_aq_str(hw->adminq.sq_last_status));
5214 }
5215
5216 /**
5217 * ice_remove - Device removal routine
5218 * @pdev: PCI device information struct
5219 */
ice_remove(struct pci_dev * pdev)5220 static void ice_remove(struct pci_dev *pdev)
5221 {
5222 struct ice_pf *pf = pci_get_drvdata(pdev);
5223 int i;
5224
5225 for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
5226 if (!ice_is_reset_in_progress(pf->state))
5227 break;
5228 msleep(100);
5229 }
5230
5231 if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
5232 set_bit(ICE_VF_RESETS_DISABLED, pf->state);
5233 ice_free_vfs(pf);
5234 }
5235
5236 ice_service_task_stop(pf);
5237 ice_aq_cancel_waiting_tasks(pf);
5238 set_bit(ICE_DOWN, pf->state);
5239
5240 if (!ice_is_safe_mode(pf))
5241 ice_remove_arfs(pf);
5242 ice_deinit_features(pf);
5243 ice_deinit_devlink(pf);
5244 ice_deinit_rdma(pf);
5245 ice_deinit_eth(pf);
5246 ice_deinit(pf);
5247
5248 ice_vsi_release_all(pf);
5249
5250 ice_setup_mc_magic_wake(pf);
5251 ice_set_wake(pf);
5252
5253 pci_disable_device(pdev);
5254 }
5255
5256 /**
5257 * ice_shutdown - PCI callback for shutting down device
5258 * @pdev: PCI device information struct
5259 */
ice_shutdown(struct pci_dev * pdev)5260 static void ice_shutdown(struct pci_dev *pdev)
5261 {
5262 struct ice_pf *pf = pci_get_drvdata(pdev);
5263
5264 ice_remove(pdev);
5265
5266 if (system_state == SYSTEM_POWER_OFF) {
5267 pci_wake_from_d3(pdev, pf->wol_ena);
5268 pci_set_power_state(pdev, PCI_D3hot);
5269 }
5270 }
5271
5272 #ifdef CONFIG_PM
5273 /**
5274 * ice_prepare_for_shutdown - prep for PCI shutdown
5275 * @pf: board private structure
5276 *
5277 * Inform or close all dependent features in prep for PCI device shutdown
5278 */
ice_prepare_for_shutdown(struct ice_pf * pf)5279 static void ice_prepare_for_shutdown(struct ice_pf *pf)
5280 {
5281 struct ice_hw *hw = &pf->hw;
5282 u32 v;
5283
5284 /* Notify VFs of impending reset */
5285 if (ice_check_sq_alive(hw, &hw->mailboxq))
5286 ice_vc_notify_reset(pf);
5287
5288 dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
5289
5290 /* disable the VSIs and their queues that are not already DOWN */
5291 ice_pf_dis_all_vsi(pf, false);
5292
5293 ice_for_each_vsi(pf, v)
5294 if (pf->vsi[v])
5295 pf->vsi[v]->vsi_num = 0;
5296
5297 ice_shutdown_all_ctrlq(hw);
5298 }
5299
5300 /**
5301 * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
5302 * @pf: board private structure to reinitialize
5303 *
5304 * This routine reinitialize interrupt scheme that was cleared during
5305 * power management suspend callback.
5306 *
5307 * This should be called during resume routine to re-allocate the q_vectors
5308 * and reacquire interrupts.
5309 */
ice_reinit_interrupt_scheme(struct ice_pf * pf)5310 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
5311 {
5312 struct device *dev = ice_pf_to_dev(pf);
5313 int ret, v;
5314
5315 /* Since we clear MSIX flag during suspend, we need to
5316 * set it back during resume...
5317 */
5318
5319 ret = ice_init_interrupt_scheme(pf);
5320 if (ret) {
5321 dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
5322 return ret;
5323 }
5324
5325 /* Remap vectors and rings, after successful re-init interrupts */
5326 ice_for_each_vsi(pf, v) {
5327 if (!pf->vsi[v])
5328 continue;
5329
5330 ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
5331 if (ret)
5332 goto err_reinit;
5333 ice_vsi_map_rings_to_vectors(pf->vsi[v]);
5334 }
5335
5336 ret = ice_req_irq_msix_misc(pf);
5337 if (ret) {
5338 dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
5339 ret);
5340 goto err_reinit;
5341 }
5342
5343 return 0;
5344
5345 err_reinit:
5346 while (v--)
5347 if (pf->vsi[v])
5348 ice_vsi_free_q_vectors(pf->vsi[v]);
5349
5350 return ret;
5351 }
5352
5353 /**
5354 * ice_suspend
5355 * @dev: generic device information structure
5356 *
5357 * Power Management callback to quiesce the device and prepare
5358 * for D3 transition.
5359 */
ice_suspend(struct device * dev)5360 static int __maybe_unused ice_suspend(struct device *dev)
5361 {
5362 struct pci_dev *pdev = to_pci_dev(dev);
5363 struct ice_pf *pf;
5364 int disabled, v;
5365
5366 pf = pci_get_drvdata(pdev);
5367
5368 if (!ice_pf_state_is_nominal(pf)) {
5369 dev_err(dev, "Device is not ready, no need to suspend it\n");
5370 return -EBUSY;
5371 }
5372
5373 /* Stop watchdog tasks until resume completion.
5374 * Even though it is most likely that the service task is
5375 * disabled if the device is suspended or down, the service task's
5376 * state is controlled by a different state bit, and we should
5377 * store and honor whatever state that bit is in at this point.
5378 */
5379 disabled = ice_service_task_stop(pf);
5380
5381 ice_deinit_rdma(pf);
5382
5383 /* Already suspended?, then there is nothing to do */
5384 if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
5385 if (!disabled)
5386 ice_service_task_restart(pf);
5387 return 0;
5388 }
5389
5390 if (test_bit(ICE_DOWN, pf->state) ||
5391 ice_is_reset_in_progress(pf->state)) {
5392 dev_err(dev, "can't suspend device in reset or already down\n");
5393 if (!disabled)
5394 ice_service_task_restart(pf);
5395 return 0;
5396 }
5397
5398 ice_setup_mc_magic_wake(pf);
5399
5400 ice_prepare_for_shutdown(pf);
5401
5402 ice_set_wake(pf);
5403
5404 /* Free vectors, clear the interrupt scheme and release IRQs
5405 * for proper hibernation, especially with large number of CPUs.
5406 * Otherwise hibernation might fail when mapping all the vectors back
5407 * to CPU0.
5408 */
5409 ice_free_irq_msix_misc(pf);
5410 ice_for_each_vsi(pf, v) {
5411 if (!pf->vsi[v])
5412 continue;
5413 ice_vsi_free_q_vectors(pf->vsi[v]);
5414 }
5415 ice_clear_interrupt_scheme(pf);
5416
5417 pci_save_state(pdev);
5418 pci_wake_from_d3(pdev, pf->wol_ena);
5419 pci_set_power_state(pdev, PCI_D3hot);
5420 return 0;
5421 }
5422
5423 /**
5424 * ice_resume - PM callback for waking up from D3
5425 * @dev: generic device information structure
5426 */
ice_resume(struct device * dev)5427 static int __maybe_unused ice_resume(struct device *dev)
5428 {
5429 struct pci_dev *pdev = to_pci_dev(dev);
5430 enum ice_reset_req reset_type;
5431 struct ice_pf *pf;
5432 struct ice_hw *hw;
5433 int ret;
5434
5435 pci_set_power_state(pdev, PCI_D0);
5436 pci_restore_state(pdev);
5437 pci_save_state(pdev);
5438
5439 if (!pci_device_is_present(pdev))
5440 return -ENODEV;
5441
5442 ret = pci_enable_device_mem(pdev);
5443 if (ret) {
5444 dev_err(dev, "Cannot enable device after suspend\n");
5445 return ret;
5446 }
5447
5448 pf = pci_get_drvdata(pdev);
5449 hw = &pf->hw;
5450
5451 pf->wakeup_reason = rd32(hw, PFPM_WUS);
5452 ice_print_wake_reason(pf);
5453
5454 /* We cleared the interrupt scheme when we suspended, so we need to
5455 * restore it now to resume device functionality.
5456 */
5457 ret = ice_reinit_interrupt_scheme(pf);
5458 if (ret)
5459 dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5460
5461 ret = ice_init_rdma(pf);
5462 if (ret)
5463 dev_err(dev, "Reinitialize RDMA during resume failed: %d\n",
5464 ret);
5465
5466 clear_bit(ICE_DOWN, pf->state);
5467 /* Now perform PF reset and rebuild */
5468 reset_type = ICE_RESET_PFR;
5469 /* re-enable service task for reset, but allow reset to schedule it */
5470 clear_bit(ICE_SERVICE_DIS, pf->state);
5471
5472 if (ice_schedule_reset(pf, reset_type))
5473 dev_err(dev, "Reset during resume failed.\n");
5474
5475 clear_bit(ICE_SUSPENDED, pf->state);
5476 ice_service_task_restart(pf);
5477
5478 /* Restart the service task */
5479 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5480
5481 return 0;
5482 }
5483 #endif /* CONFIG_PM */
5484
5485 /**
5486 * ice_pci_err_detected - warning that PCI error has been detected
5487 * @pdev: PCI device information struct
5488 * @err: the type of PCI error
5489 *
5490 * Called to warn that something happened on the PCI bus and the error handling
5491 * is in progress. Allows the driver to gracefully prepare/handle PCI errors.
5492 */
5493 static pci_ers_result_t
ice_pci_err_detected(struct pci_dev * pdev,pci_channel_state_t err)5494 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
5495 {
5496 struct ice_pf *pf = pci_get_drvdata(pdev);
5497
5498 if (!pf) {
5499 dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5500 __func__, err);
5501 return PCI_ERS_RESULT_DISCONNECT;
5502 }
5503
5504 if (!test_bit(ICE_SUSPENDED, pf->state)) {
5505 ice_service_task_stop(pf);
5506
5507 if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5508 set_bit(ICE_PFR_REQ, pf->state);
5509 ice_prepare_for_reset(pf, ICE_RESET_PFR);
5510 }
5511 }
5512
5513 return PCI_ERS_RESULT_NEED_RESET;
5514 }
5515
5516 /**
5517 * ice_pci_err_slot_reset - a PCI slot reset has just happened
5518 * @pdev: PCI device information struct
5519 *
5520 * Called to determine if the driver can recover from the PCI slot reset by
5521 * using a register read to determine if the device is recoverable.
5522 */
ice_pci_err_slot_reset(struct pci_dev * pdev)5523 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5524 {
5525 struct ice_pf *pf = pci_get_drvdata(pdev);
5526 pci_ers_result_t result;
5527 int err;
5528 u32 reg;
5529
5530 err = pci_enable_device_mem(pdev);
5531 if (err) {
5532 dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
5533 err);
5534 result = PCI_ERS_RESULT_DISCONNECT;
5535 } else {
5536 pci_set_master(pdev);
5537 pci_restore_state(pdev);
5538 pci_save_state(pdev);
5539 pci_wake_from_d3(pdev, false);
5540
5541 /* Check for life */
5542 reg = rd32(&pf->hw, GLGEN_RTRIG);
5543 if (!reg)
5544 result = PCI_ERS_RESULT_RECOVERED;
5545 else
5546 result = PCI_ERS_RESULT_DISCONNECT;
5547 }
5548
5549 return result;
5550 }
5551
5552 /**
5553 * ice_pci_err_resume - restart operations after PCI error recovery
5554 * @pdev: PCI device information struct
5555 *
5556 * Called to allow the driver to bring things back up after PCI error and/or
5557 * reset recovery have finished
5558 */
ice_pci_err_resume(struct pci_dev * pdev)5559 static void ice_pci_err_resume(struct pci_dev *pdev)
5560 {
5561 struct ice_pf *pf = pci_get_drvdata(pdev);
5562
5563 if (!pf) {
5564 dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5565 __func__);
5566 return;
5567 }
5568
5569 if (test_bit(ICE_SUSPENDED, pf->state)) {
5570 dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5571 __func__);
5572 return;
5573 }
5574
5575 ice_restore_all_vfs_msi_state(pdev);
5576
5577 ice_do_reset(pf, ICE_RESET_PFR);
5578 ice_service_task_restart(pf);
5579 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5580 }
5581
5582 /**
5583 * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5584 * @pdev: PCI device information struct
5585 */
ice_pci_err_reset_prepare(struct pci_dev * pdev)5586 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5587 {
5588 struct ice_pf *pf = pci_get_drvdata(pdev);
5589
5590 if (!test_bit(ICE_SUSPENDED, pf->state)) {
5591 ice_service_task_stop(pf);
5592
5593 if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5594 set_bit(ICE_PFR_REQ, pf->state);
5595 ice_prepare_for_reset(pf, ICE_RESET_PFR);
5596 }
5597 }
5598 }
5599
5600 /**
5601 * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5602 * @pdev: PCI device information struct
5603 */
ice_pci_err_reset_done(struct pci_dev * pdev)5604 static void ice_pci_err_reset_done(struct pci_dev *pdev)
5605 {
5606 ice_pci_err_resume(pdev);
5607 }
5608
5609 /* ice_pci_tbl - PCI Device ID Table
5610 *
5611 * Wildcard entries (PCI_ANY_ID) should come last
5612 * Last entry must be all 0s
5613 *
5614 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5615 * Class, Class Mask, private data (not used) }
5616 */
5617 static const struct pci_device_id ice_pci_tbl[] = {
5618 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
5619 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
5620 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
5621 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE), 0 },
5622 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP), 0 },
5623 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP), 0 },
5624 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE), 0 },
5625 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP), 0 },
5626 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP), 0 },
5627 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T), 0 },
5628 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII), 0 },
5629 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 },
5630 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 },
5631 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 },
5632 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 },
5633 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 },
5634 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE), 0 },
5635 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 },
5636 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 },
5637 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 },
5638 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE), 0 },
5639 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP), 0 },
5640 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T), 0 },
5641 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE), 0 },
5642 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP), 0 },
5643 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822_SI_DFLT), 0 },
5644 /* required last entry */
5645 { 0, }
5646 };
5647 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5648
5649 static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
5650
5651 static const struct pci_error_handlers ice_pci_err_handler = {
5652 .error_detected = ice_pci_err_detected,
5653 .slot_reset = ice_pci_err_slot_reset,
5654 .reset_prepare = ice_pci_err_reset_prepare,
5655 .reset_done = ice_pci_err_reset_done,
5656 .resume = ice_pci_err_resume
5657 };
5658
5659 static struct pci_driver ice_driver = {
5660 .name = KBUILD_MODNAME,
5661 .id_table = ice_pci_tbl,
5662 .probe = ice_probe,
5663 .remove = ice_remove,
5664 #ifdef CONFIG_PM
5665 .driver.pm = &ice_pm_ops,
5666 #endif /* CONFIG_PM */
5667 .shutdown = ice_shutdown,
5668 .sriov_configure = ice_sriov_configure,
5669 .err_handler = &ice_pci_err_handler
5670 };
5671
5672 /**
5673 * ice_module_init - Driver registration routine
5674 *
5675 * ice_module_init is the first routine called when the driver is
5676 * loaded. All it does is register with the PCI subsystem.
5677 */
ice_module_init(void)5678 static int __init ice_module_init(void)
5679 {
5680 int status = -ENOMEM;
5681
5682 pr_info("%s\n", ice_driver_string);
5683 pr_info("%s\n", ice_copyright);
5684
5685 ice_wq = alloc_workqueue("%s", 0, 0, KBUILD_MODNAME);
5686 if (!ice_wq) {
5687 pr_err("Failed to create workqueue\n");
5688 return status;
5689 }
5690
5691 ice_lag_wq = alloc_ordered_workqueue("ice_lag_wq", 0);
5692 if (!ice_lag_wq) {
5693 pr_err("Failed to create LAG workqueue\n");
5694 goto err_dest_wq;
5695 }
5696
5697 status = pci_register_driver(&ice_driver);
5698 if (status) {
5699 pr_err("failed to register PCI driver, err %d\n", status);
5700 goto err_dest_lag_wq;
5701 }
5702
5703 return 0;
5704
5705 err_dest_lag_wq:
5706 destroy_workqueue(ice_lag_wq);
5707 err_dest_wq:
5708 destroy_workqueue(ice_wq);
5709 return status;
5710 }
5711 module_init(ice_module_init);
5712
5713 /**
5714 * ice_module_exit - Driver exit cleanup routine
5715 *
5716 * ice_module_exit is called just before the driver is removed
5717 * from memory.
5718 */
ice_module_exit(void)5719 static void __exit ice_module_exit(void)
5720 {
5721 pci_unregister_driver(&ice_driver);
5722 destroy_workqueue(ice_wq);
5723 destroy_workqueue(ice_lag_wq);
5724 pr_info("module unloaded\n");
5725 }
5726 module_exit(ice_module_exit);
5727
5728 /**
5729 * ice_set_mac_address - NDO callback to set MAC address
5730 * @netdev: network interface device structure
5731 * @pi: pointer to an address structure
5732 *
5733 * Returns 0 on success, negative on failure
5734 */
ice_set_mac_address(struct net_device * netdev,void * pi)5735 static int ice_set_mac_address(struct net_device *netdev, void *pi)
5736 {
5737 struct ice_netdev_priv *np = netdev_priv(netdev);
5738 struct ice_vsi *vsi = np->vsi;
5739 struct ice_pf *pf = vsi->back;
5740 struct ice_hw *hw = &pf->hw;
5741 struct sockaddr *addr = pi;
5742 u8 old_mac[ETH_ALEN];
5743 u8 flags = 0;
5744 u8 *mac;
5745 int err;
5746
5747 mac = (u8 *)addr->sa_data;
5748
5749 if (!is_valid_ether_addr(mac))
5750 return -EADDRNOTAVAIL;
5751
5752 if (test_bit(ICE_DOWN, pf->state) ||
5753 ice_is_reset_in_progress(pf->state)) {
5754 netdev_err(netdev, "can't set mac %pM. device not ready\n",
5755 mac);
5756 return -EBUSY;
5757 }
5758
5759 if (ice_chnl_dmac_fltr_cnt(pf)) {
5760 netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
5761 mac);
5762 return -EAGAIN;
5763 }
5764
5765 netif_addr_lock_bh(netdev);
5766 ether_addr_copy(old_mac, netdev->dev_addr);
5767 /* change the netdev's MAC address */
5768 eth_hw_addr_set(netdev, mac);
5769 netif_addr_unlock_bh(netdev);
5770
5771 /* Clean up old MAC filter. Not an error if old filter doesn't exist */
5772 err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
5773 if (err && err != -ENOENT) {
5774 err = -EADDRNOTAVAIL;
5775 goto err_update_filters;
5776 }
5777
5778 /* Add filter for new MAC. If filter exists, return success */
5779 err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
5780 if (err == -EEXIST) {
5781 /* Although this MAC filter is already present in hardware it's
5782 * possible in some cases (e.g. bonding) that dev_addr was
5783 * modified outside of the driver and needs to be restored back
5784 * to this value.
5785 */
5786 netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
5787
5788 return 0;
5789 } else if (err) {
5790 /* error if the new filter addition failed */
5791 err = -EADDRNOTAVAIL;
5792 }
5793
5794 err_update_filters:
5795 if (err) {
5796 netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
5797 mac);
5798 netif_addr_lock_bh(netdev);
5799 eth_hw_addr_set(netdev, old_mac);
5800 netif_addr_unlock_bh(netdev);
5801 return err;
5802 }
5803
5804 netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
5805 netdev->dev_addr);
5806
5807 /* write new MAC address to the firmware */
5808 flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
5809 err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
5810 if (err) {
5811 netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
5812 mac, err);
5813 }
5814 return 0;
5815 }
5816
5817 /**
5818 * ice_set_rx_mode - NDO callback to set the netdev filters
5819 * @netdev: network interface device structure
5820 */
ice_set_rx_mode(struct net_device * netdev)5821 static void ice_set_rx_mode(struct net_device *netdev)
5822 {
5823 struct ice_netdev_priv *np = netdev_priv(netdev);
5824 struct ice_vsi *vsi = np->vsi;
5825
5826 if (!vsi || ice_is_switchdev_running(vsi->back))
5827 return;
5828
5829 /* Set the flags to synchronize filters
5830 * ndo_set_rx_mode may be triggered even without a change in netdev
5831 * flags
5832 */
5833 set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
5834 set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
5835 set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
5836
5837 /* schedule our worker thread which will take care of
5838 * applying the new filter changes
5839 */
5840 ice_service_task_schedule(vsi->back);
5841 }
5842
5843 /**
5844 * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
5845 * @netdev: network interface device structure
5846 * @queue_index: Queue ID
5847 * @maxrate: maximum bandwidth in Mbps
5848 */
5849 static int
ice_set_tx_maxrate(struct net_device * netdev,int queue_index,u32 maxrate)5850 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
5851 {
5852 struct ice_netdev_priv *np = netdev_priv(netdev);
5853 struct ice_vsi *vsi = np->vsi;
5854 u16 q_handle;
5855 int status;
5856 u8 tc;
5857
5858 /* Validate maxrate requested is within permitted range */
5859 if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
5860 netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
5861 maxrate, queue_index);
5862 return -EINVAL;
5863 }
5864
5865 q_handle = vsi->tx_rings[queue_index]->q_handle;
5866 tc = ice_dcb_get_tc(vsi, queue_index);
5867
5868 vsi = ice_locate_vsi_using_queue(vsi, queue_index);
5869 if (!vsi) {
5870 netdev_err(netdev, "Invalid VSI for given queue %d\n",
5871 queue_index);
5872 return -EINVAL;
5873 }
5874
5875 /* Set BW back to default, when user set maxrate to 0 */
5876 if (!maxrate)
5877 status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
5878 q_handle, ICE_MAX_BW);
5879 else
5880 status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
5881 q_handle, ICE_MAX_BW, maxrate * 1000);
5882 if (status)
5883 netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
5884 status);
5885
5886 return status;
5887 }
5888
5889 /**
5890 * ice_fdb_add - add an entry to the hardware database
5891 * @ndm: the input from the stack
5892 * @tb: pointer to array of nladdr (unused)
5893 * @dev: the net device pointer
5894 * @addr: the MAC address entry being added
5895 * @vid: VLAN ID
5896 * @flags: instructions from stack about fdb operation
5897 * @extack: netlink extended ack
5898 */
5899 static int
ice_fdb_add(struct ndmsg * ndm,struct nlattr __always_unused * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags,struct netlink_ext_ack __always_unused * extack)5900 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
5901 struct net_device *dev, const unsigned char *addr, u16 vid,
5902 u16 flags, struct netlink_ext_ack __always_unused *extack)
5903 {
5904 int err;
5905
5906 if (vid) {
5907 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
5908 return -EINVAL;
5909 }
5910 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
5911 netdev_err(dev, "FDB only supports static addresses\n");
5912 return -EINVAL;
5913 }
5914
5915 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
5916 err = dev_uc_add_excl(dev, addr);
5917 else if (is_multicast_ether_addr(addr))
5918 err = dev_mc_add_excl(dev, addr);
5919 else
5920 err = -EINVAL;
5921
5922 /* Only return duplicate errors if NLM_F_EXCL is set */
5923 if (err == -EEXIST && !(flags & NLM_F_EXCL))
5924 err = 0;
5925
5926 return err;
5927 }
5928
5929 /**
5930 * ice_fdb_del - delete an entry from the hardware database
5931 * @ndm: the input from the stack
5932 * @tb: pointer to array of nladdr (unused)
5933 * @dev: the net device pointer
5934 * @addr: the MAC address entry being added
5935 * @vid: VLAN ID
5936 * @extack: netlink extended ack
5937 */
5938 static int
ice_fdb_del(struct ndmsg * ndm,__always_unused struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,__always_unused u16 vid,struct netlink_ext_ack * extack)5939 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
5940 struct net_device *dev, const unsigned char *addr,
5941 __always_unused u16 vid, struct netlink_ext_ack *extack)
5942 {
5943 int err;
5944
5945 if (ndm->ndm_state & NUD_PERMANENT) {
5946 netdev_err(dev, "FDB only supports static addresses\n");
5947 return -EINVAL;
5948 }
5949
5950 if (is_unicast_ether_addr(addr))
5951 err = dev_uc_del(dev, addr);
5952 else if (is_multicast_ether_addr(addr))
5953 err = dev_mc_del(dev, addr);
5954 else
5955 err = -EINVAL;
5956
5957 return err;
5958 }
5959
5960 #define NETIF_VLAN_OFFLOAD_FEATURES (NETIF_F_HW_VLAN_CTAG_RX | \
5961 NETIF_F_HW_VLAN_CTAG_TX | \
5962 NETIF_F_HW_VLAN_STAG_RX | \
5963 NETIF_F_HW_VLAN_STAG_TX)
5964
5965 #define NETIF_VLAN_STRIPPING_FEATURES (NETIF_F_HW_VLAN_CTAG_RX | \
5966 NETIF_F_HW_VLAN_STAG_RX)
5967
5968 #define NETIF_VLAN_FILTERING_FEATURES (NETIF_F_HW_VLAN_CTAG_FILTER | \
5969 NETIF_F_HW_VLAN_STAG_FILTER)
5970
5971 /**
5972 * ice_fix_features - fix the netdev features flags based on device limitations
5973 * @netdev: ptr to the netdev that flags are being fixed on
5974 * @features: features that need to be checked and possibly fixed
5975 *
5976 * Make sure any fixups are made to features in this callback. This enables the
5977 * driver to not have to check unsupported configurations throughout the driver
5978 * because that's the responsiblity of this callback.
5979 *
5980 * Single VLAN Mode (SVM) Supported Features:
5981 * NETIF_F_HW_VLAN_CTAG_FILTER
5982 * NETIF_F_HW_VLAN_CTAG_RX
5983 * NETIF_F_HW_VLAN_CTAG_TX
5984 *
5985 * Double VLAN Mode (DVM) Supported Features:
5986 * NETIF_F_HW_VLAN_CTAG_FILTER
5987 * NETIF_F_HW_VLAN_CTAG_RX
5988 * NETIF_F_HW_VLAN_CTAG_TX
5989 *
5990 * NETIF_F_HW_VLAN_STAG_FILTER
5991 * NETIF_HW_VLAN_STAG_RX
5992 * NETIF_HW_VLAN_STAG_TX
5993 *
5994 * Features that need fixing:
5995 * Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
5996 * These are mutually exlusive as the VSI context cannot support multiple
5997 * VLAN ethertypes simultaneously for stripping and/or insertion. If this
5998 * is not done, then default to clearing the requested STAG offload
5999 * settings.
6000 *
6001 * All supported filtering has to be enabled or disabled together. For
6002 * example, in DVM, CTAG and STAG filtering have to be enabled and disabled
6003 * together. If this is not done, then default to VLAN filtering disabled.
6004 * These are mutually exclusive as there is currently no way to
6005 * enable/disable VLAN filtering based on VLAN ethertype when using VLAN
6006 * prune rules.
6007 */
6008 static netdev_features_t
ice_fix_features(struct net_device * netdev,netdev_features_t features)6009 ice_fix_features(struct net_device *netdev, netdev_features_t features)
6010 {
6011 struct ice_netdev_priv *np = netdev_priv(netdev);
6012 netdev_features_t req_vlan_fltr, cur_vlan_fltr;
6013 bool cur_ctag, cur_stag, req_ctag, req_stag;
6014
6015 cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
6016 cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6017 cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6018
6019 req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
6020 req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6021 req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6022
6023 if (req_vlan_fltr != cur_vlan_fltr) {
6024 if (ice_is_dvm_ena(&np->vsi->back->hw)) {
6025 if (req_ctag && req_stag) {
6026 features |= NETIF_VLAN_FILTERING_FEATURES;
6027 } else if (!req_ctag && !req_stag) {
6028 features &= ~NETIF_VLAN_FILTERING_FEATURES;
6029 } else if ((!cur_ctag && req_ctag && !cur_stag) ||
6030 (!cur_stag && req_stag && !cur_ctag)) {
6031 features |= NETIF_VLAN_FILTERING_FEATURES;
6032 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");
6033 } else if ((cur_ctag && !req_ctag && cur_stag) ||
6034 (cur_stag && !req_stag && cur_ctag)) {
6035 features &= ~NETIF_VLAN_FILTERING_FEATURES;
6036 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");
6037 }
6038 } else {
6039 if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
6040 netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
6041
6042 if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
6043 features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6044 }
6045 }
6046
6047 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
6048 (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
6049 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");
6050 features &= ~(NETIF_F_HW_VLAN_STAG_RX |
6051 NETIF_F_HW_VLAN_STAG_TX);
6052 }
6053
6054 if (!(netdev->features & NETIF_F_RXFCS) &&
6055 (features & NETIF_F_RXFCS) &&
6056 (features & NETIF_VLAN_STRIPPING_FEATURES) &&
6057 !ice_vsi_has_non_zero_vlans(np->vsi)) {
6058 netdev_warn(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
6059 features &= ~NETIF_VLAN_STRIPPING_FEATURES;
6060 }
6061
6062 return features;
6063 }
6064
6065 /**
6066 * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
6067 * @vsi: PF's VSI
6068 * @features: features used to determine VLAN offload settings
6069 *
6070 * First, determine the vlan_ethertype based on the VLAN offload bits in
6071 * features. Then determine if stripping and insertion should be enabled or
6072 * disabled. Finally enable or disable VLAN stripping and insertion.
6073 */
6074 static int
ice_set_vlan_offload_features(struct ice_vsi * vsi,netdev_features_t features)6075 ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
6076 {
6077 bool enable_stripping = true, enable_insertion = true;
6078 struct ice_vsi_vlan_ops *vlan_ops;
6079 int strip_err = 0, insert_err = 0;
6080 u16 vlan_ethertype = 0;
6081
6082 vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6083
6084 if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
6085 vlan_ethertype = ETH_P_8021AD;
6086 else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
6087 vlan_ethertype = ETH_P_8021Q;
6088
6089 if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
6090 enable_stripping = false;
6091 if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
6092 enable_insertion = false;
6093
6094 if (enable_stripping)
6095 strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
6096 else
6097 strip_err = vlan_ops->dis_stripping(vsi);
6098
6099 if (enable_insertion)
6100 insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
6101 else
6102 insert_err = vlan_ops->dis_insertion(vsi);
6103
6104 if (strip_err || insert_err)
6105 return -EIO;
6106
6107 return 0;
6108 }
6109
6110 /**
6111 * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
6112 * @vsi: PF's VSI
6113 * @features: features used to determine VLAN filtering settings
6114 *
6115 * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
6116 * features.
6117 */
6118 static int
ice_set_vlan_filtering_features(struct ice_vsi * vsi,netdev_features_t features)6119 ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
6120 {
6121 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6122 int err = 0;
6123
6124 /* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
6125 * if either bit is set
6126 */
6127 if (features &
6128 (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER))
6129 err = vlan_ops->ena_rx_filtering(vsi);
6130 else
6131 err = vlan_ops->dis_rx_filtering(vsi);
6132
6133 return err;
6134 }
6135
6136 /**
6137 * ice_set_vlan_features - set VLAN settings based on suggested feature set
6138 * @netdev: ptr to the netdev being adjusted
6139 * @features: the feature set that the stack is suggesting
6140 *
6141 * Only update VLAN settings if the requested_vlan_features are different than
6142 * the current_vlan_features.
6143 */
6144 static int
ice_set_vlan_features(struct net_device * netdev,netdev_features_t features)6145 ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
6146 {
6147 netdev_features_t current_vlan_features, requested_vlan_features;
6148 struct ice_netdev_priv *np = netdev_priv(netdev);
6149 struct ice_vsi *vsi = np->vsi;
6150 int err;
6151
6152 current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
6153 requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
6154 if (current_vlan_features ^ requested_vlan_features) {
6155 if ((features & NETIF_F_RXFCS) &&
6156 (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6157 dev_err(ice_pf_to_dev(vsi->back),
6158 "To enable VLAN stripping, you must first enable FCS/CRC stripping\n");
6159 return -EIO;
6160 }
6161
6162 err = ice_set_vlan_offload_features(vsi, features);
6163 if (err)
6164 return err;
6165 }
6166
6167 current_vlan_features = netdev->features &
6168 NETIF_VLAN_FILTERING_FEATURES;
6169 requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
6170 if (current_vlan_features ^ requested_vlan_features) {
6171 err = ice_set_vlan_filtering_features(vsi, features);
6172 if (err)
6173 return err;
6174 }
6175
6176 return 0;
6177 }
6178
6179 /**
6180 * ice_set_loopback - turn on/off loopback mode on underlying PF
6181 * @vsi: ptr to VSI
6182 * @ena: flag to indicate the on/off setting
6183 */
ice_set_loopback(struct ice_vsi * vsi,bool ena)6184 static int ice_set_loopback(struct ice_vsi *vsi, bool ena)
6185 {
6186 bool if_running = netif_running(vsi->netdev);
6187 int ret;
6188
6189 if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
6190 ret = ice_down(vsi);
6191 if (ret) {
6192 netdev_err(vsi->netdev, "Preparing device to toggle loopback failed\n");
6193 return ret;
6194 }
6195 }
6196 ret = ice_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
6197 if (ret)
6198 netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
6199 if (if_running)
6200 ret = ice_up(vsi);
6201
6202 return ret;
6203 }
6204
6205 /**
6206 * ice_set_features - set the netdev feature flags
6207 * @netdev: ptr to the netdev being adjusted
6208 * @features: the feature set that the stack is suggesting
6209 */
6210 static int
ice_set_features(struct net_device * netdev,netdev_features_t features)6211 ice_set_features(struct net_device *netdev, netdev_features_t features)
6212 {
6213 netdev_features_t changed = netdev->features ^ features;
6214 struct ice_netdev_priv *np = netdev_priv(netdev);
6215 struct ice_vsi *vsi = np->vsi;
6216 struct ice_pf *pf = vsi->back;
6217 int ret = 0;
6218
6219 /* Don't set any netdev advanced features with device in Safe Mode */
6220 if (ice_is_safe_mode(pf)) {
6221 dev_err(ice_pf_to_dev(pf),
6222 "Device is in Safe Mode - not enabling advanced netdev features\n");
6223 return ret;
6224 }
6225
6226 /* Do not change setting during reset */
6227 if (ice_is_reset_in_progress(pf->state)) {
6228 dev_err(ice_pf_to_dev(pf),
6229 "Device is resetting, changing advanced netdev features temporarily unavailable.\n");
6230 return -EBUSY;
6231 }
6232
6233 /* Multiple features can be changed in one call so keep features in
6234 * separate if/else statements to guarantee each feature is checked
6235 */
6236 if (changed & NETIF_F_RXHASH)
6237 ice_vsi_manage_rss_lut(vsi, !!(features & NETIF_F_RXHASH));
6238
6239 ret = ice_set_vlan_features(netdev, features);
6240 if (ret)
6241 return ret;
6242
6243 /* Turn on receive of FCS aka CRC, and after setting this
6244 * flag the packet data will have the 4 byte CRC appended
6245 */
6246 if (changed & NETIF_F_RXFCS) {
6247 if ((features & NETIF_F_RXFCS) &&
6248 (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6249 dev_err(ice_pf_to_dev(vsi->back),
6250 "To disable FCS/CRC stripping, you must first disable VLAN stripping\n");
6251 return -EIO;
6252 }
6253
6254 ice_vsi_cfg_crc_strip(vsi, !!(features & NETIF_F_RXFCS));
6255 ret = ice_down_up(vsi);
6256 if (ret)
6257 return ret;
6258 }
6259
6260 if (changed & NETIF_F_NTUPLE) {
6261 bool ena = !!(features & NETIF_F_NTUPLE);
6262
6263 ice_vsi_manage_fdir(vsi, ena);
6264 ena ? ice_init_arfs(vsi) : ice_clear_arfs(vsi);
6265 }
6266
6267 /* don't turn off hw_tc_offload when ADQ is already enabled */
6268 if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
6269 dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
6270 return -EACCES;
6271 }
6272
6273 if (changed & NETIF_F_HW_TC) {
6274 bool ena = !!(features & NETIF_F_HW_TC);
6275
6276 ena ? set_bit(ICE_FLAG_CLS_FLOWER, pf->flags) :
6277 clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
6278 }
6279
6280 if (changed & NETIF_F_LOOPBACK)
6281 ret = ice_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
6282
6283 return ret;
6284 }
6285
6286 /**
6287 * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
6288 * @vsi: VSI to setup VLAN properties for
6289 */
ice_vsi_vlan_setup(struct ice_vsi * vsi)6290 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
6291 {
6292 int err;
6293
6294 err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
6295 if (err)
6296 return err;
6297
6298 err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
6299 if (err)
6300 return err;
6301
6302 return ice_vsi_add_vlan_zero(vsi);
6303 }
6304
6305 /**
6306 * ice_vsi_cfg_lan - Setup the VSI lan related config
6307 * @vsi: the VSI being configured
6308 *
6309 * Return 0 on success and negative value on error
6310 */
ice_vsi_cfg_lan(struct ice_vsi * vsi)6311 int ice_vsi_cfg_lan(struct ice_vsi *vsi)
6312 {
6313 int err;
6314
6315 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6316 ice_set_rx_mode(vsi->netdev);
6317
6318 err = ice_vsi_vlan_setup(vsi);
6319 if (err)
6320 return err;
6321 }
6322 ice_vsi_cfg_dcb_rings(vsi);
6323
6324 err = ice_vsi_cfg_lan_txqs(vsi);
6325 if (!err && ice_is_xdp_ena_vsi(vsi))
6326 err = ice_vsi_cfg_xdp_txqs(vsi);
6327 if (!err)
6328 err = ice_vsi_cfg_rxqs(vsi);
6329
6330 return err;
6331 }
6332
6333 /* THEORY OF MODERATION:
6334 * The ice driver hardware works differently than the hardware that DIMLIB was
6335 * originally made for. ice hardware doesn't have packet count limits that
6336 * can trigger an interrupt, but it *does* have interrupt rate limit support,
6337 * which is hard-coded to a limit of 250,000 ints/second.
6338 * If not using dynamic moderation, the INTRL value can be modified
6339 * by ethtool rx-usecs-high.
6340 */
6341 struct ice_dim {
6342 /* the throttle rate for interrupts, basically worst case delay before
6343 * an initial interrupt fires, value is stored in microseconds.
6344 */
6345 u16 itr;
6346 };
6347
6348 /* Make a different profile for Rx that doesn't allow quite so aggressive
6349 * moderation at the high end (it maxes out at 126us or about 8k interrupts a
6350 * second.
6351 */
6352 static const struct ice_dim rx_profile[] = {
6353 {2}, /* 500,000 ints/s, capped at 250K by INTRL */
6354 {8}, /* 125,000 ints/s */
6355 {16}, /* 62,500 ints/s */
6356 {62}, /* 16,129 ints/s */
6357 {126} /* 7,936 ints/s */
6358 };
6359
6360 /* The transmit profile, which has the same sorts of values
6361 * as the previous struct
6362 */
6363 static const struct ice_dim tx_profile[] = {
6364 {2}, /* 500,000 ints/s, capped at 250K by INTRL */
6365 {8}, /* 125,000 ints/s */
6366 {40}, /* 16,125 ints/s */
6367 {128}, /* 7,812 ints/s */
6368 {256} /* 3,906 ints/s */
6369 };
6370
ice_tx_dim_work(struct work_struct * work)6371 static void ice_tx_dim_work(struct work_struct *work)
6372 {
6373 struct ice_ring_container *rc;
6374 struct dim *dim;
6375 u16 itr;
6376
6377 dim = container_of(work, struct dim, work);
6378 rc = dim->priv;
6379
6380 WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
6381
6382 /* look up the values in our local table */
6383 itr = tx_profile[dim->profile_ix].itr;
6384
6385 ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
6386 ice_write_itr(rc, itr);
6387
6388 dim->state = DIM_START_MEASURE;
6389 }
6390
ice_rx_dim_work(struct work_struct * work)6391 static void ice_rx_dim_work(struct work_struct *work)
6392 {
6393 struct ice_ring_container *rc;
6394 struct dim *dim;
6395 u16 itr;
6396
6397 dim = container_of(work, struct dim, work);
6398 rc = dim->priv;
6399
6400 WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
6401
6402 /* look up the values in our local table */
6403 itr = rx_profile[dim->profile_ix].itr;
6404
6405 ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
6406 ice_write_itr(rc, itr);
6407
6408 dim->state = DIM_START_MEASURE;
6409 }
6410
6411 #define ICE_DIM_DEFAULT_PROFILE_IX 1
6412
6413 /**
6414 * ice_init_moderation - set up interrupt moderation
6415 * @q_vector: the vector containing rings to be configured
6416 *
6417 * Set up interrupt moderation registers, with the intent to do the right thing
6418 * when called from reset or from probe, and whether or not dynamic moderation
6419 * is enabled or not. Take special care to write all the registers in both
6420 * dynamic moderation mode or not in order to make sure hardware is in a known
6421 * state.
6422 */
ice_init_moderation(struct ice_q_vector * q_vector)6423 static void ice_init_moderation(struct ice_q_vector *q_vector)
6424 {
6425 struct ice_ring_container *rc;
6426 bool tx_dynamic, rx_dynamic;
6427
6428 rc = &q_vector->tx;
6429 INIT_WORK(&rc->dim.work, ice_tx_dim_work);
6430 rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6431 rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6432 rc->dim.priv = rc;
6433 tx_dynamic = ITR_IS_DYNAMIC(rc);
6434
6435 /* set the initial TX ITR to match the above */
6436 ice_write_itr(rc, tx_dynamic ?
6437 tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
6438
6439 rc = &q_vector->rx;
6440 INIT_WORK(&rc->dim.work, ice_rx_dim_work);
6441 rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6442 rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6443 rc->dim.priv = rc;
6444 rx_dynamic = ITR_IS_DYNAMIC(rc);
6445
6446 /* set the initial RX ITR to match the above */
6447 ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
6448 rc->itr_setting);
6449
6450 ice_set_q_vector_intrl(q_vector);
6451 }
6452
6453 /**
6454 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
6455 * @vsi: the VSI being configured
6456 */
ice_napi_enable_all(struct ice_vsi * vsi)6457 static void ice_napi_enable_all(struct ice_vsi *vsi)
6458 {
6459 int q_idx;
6460
6461 if (!vsi->netdev)
6462 return;
6463
6464 ice_for_each_q_vector(vsi, q_idx) {
6465 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6466
6467 ice_init_moderation(q_vector);
6468
6469 if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6470 napi_enable(&q_vector->napi);
6471 }
6472 }
6473
6474 /**
6475 * ice_up_complete - Finish the last steps of bringing up a connection
6476 * @vsi: The VSI being configured
6477 *
6478 * Return 0 on success and negative value on error
6479 */
ice_up_complete(struct ice_vsi * vsi)6480 static int ice_up_complete(struct ice_vsi *vsi)
6481 {
6482 struct ice_pf *pf = vsi->back;
6483 int err;
6484
6485 ice_vsi_cfg_msix(vsi);
6486
6487 /* Enable only Rx rings, Tx rings were enabled by the FW when the
6488 * Tx queue group list was configured and the context bits were
6489 * programmed using ice_vsi_cfg_txqs
6490 */
6491 err = ice_vsi_start_all_rx_rings(vsi);
6492 if (err)
6493 return err;
6494
6495 clear_bit(ICE_VSI_DOWN, vsi->state);
6496 ice_napi_enable_all(vsi);
6497 ice_vsi_ena_irq(vsi);
6498
6499 if (vsi->port_info &&
6500 (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
6501 vsi->netdev && vsi->type == ICE_VSI_PF) {
6502 ice_print_link_msg(vsi, true);
6503 netif_tx_start_all_queues(vsi->netdev);
6504 netif_carrier_on(vsi->netdev);
6505 ice_ptp_link_change(pf, pf->hw.pf_id, true);
6506 }
6507
6508 /* Perform an initial read of the statistics registers now to
6509 * set the baseline so counters are ready when interface is up
6510 */
6511 ice_update_eth_stats(vsi);
6512
6513 if (vsi->type == ICE_VSI_PF)
6514 ice_service_task_schedule(pf);
6515
6516 return 0;
6517 }
6518
6519 /**
6520 * ice_up - Bring the connection back up after being down
6521 * @vsi: VSI being configured
6522 */
ice_up(struct ice_vsi * vsi)6523 int ice_up(struct ice_vsi *vsi)
6524 {
6525 int err;
6526
6527 err = ice_vsi_cfg_lan(vsi);
6528 if (!err)
6529 err = ice_up_complete(vsi);
6530
6531 return err;
6532 }
6533
6534 /**
6535 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
6536 * @syncp: pointer to u64_stats_sync
6537 * @stats: stats that pkts and bytes count will be taken from
6538 * @pkts: packets stats counter
6539 * @bytes: bytes stats counter
6540 *
6541 * This function fetches stats from the ring considering the atomic operations
6542 * that needs to be performed to read u64 values in 32 bit machine.
6543 */
6544 void
ice_fetch_u64_stats_per_ring(struct u64_stats_sync * syncp,struct ice_q_stats stats,u64 * pkts,u64 * bytes)6545 ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
6546 struct ice_q_stats stats, u64 *pkts, u64 *bytes)
6547 {
6548 unsigned int start;
6549
6550 do {
6551 start = u64_stats_fetch_begin(syncp);
6552 *pkts = stats.pkts;
6553 *bytes = stats.bytes;
6554 } while (u64_stats_fetch_retry(syncp, start));
6555 }
6556
6557 /**
6558 * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
6559 * @vsi: the VSI to be updated
6560 * @vsi_stats: the stats struct to be updated
6561 * @rings: rings to work on
6562 * @count: number of rings
6563 */
6564 static void
ice_update_vsi_tx_ring_stats(struct ice_vsi * vsi,struct rtnl_link_stats64 * vsi_stats,struct ice_tx_ring ** rings,u16 count)6565 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
6566 struct rtnl_link_stats64 *vsi_stats,
6567 struct ice_tx_ring **rings, u16 count)
6568 {
6569 u16 i;
6570
6571 for (i = 0; i < count; i++) {
6572 struct ice_tx_ring *ring;
6573 u64 pkts = 0, bytes = 0;
6574
6575 ring = READ_ONCE(rings[i]);
6576 if (!ring || !ring->ring_stats)
6577 continue;
6578 ice_fetch_u64_stats_per_ring(&ring->ring_stats->syncp,
6579 ring->ring_stats->stats, &pkts,
6580 &bytes);
6581 vsi_stats->tx_packets += pkts;
6582 vsi_stats->tx_bytes += bytes;
6583 vsi->tx_restart += ring->ring_stats->tx_stats.restart_q;
6584 vsi->tx_busy += ring->ring_stats->tx_stats.tx_busy;
6585 vsi->tx_linearize += ring->ring_stats->tx_stats.tx_linearize;
6586 }
6587 }
6588
6589 /**
6590 * ice_update_vsi_ring_stats - Update VSI stats counters
6591 * @vsi: the VSI to be updated
6592 */
ice_update_vsi_ring_stats(struct ice_vsi * vsi)6593 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
6594 {
6595 struct rtnl_link_stats64 *net_stats, *stats_prev;
6596 struct rtnl_link_stats64 *vsi_stats;
6597 struct ice_pf *pf = vsi->back;
6598 u64 pkts, bytes;
6599 int i;
6600
6601 vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
6602 if (!vsi_stats)
6603 return;
6604
6605 /* reset non-netdev (extended) stats */
6606 vsi->tx_restart = 0;
6607 vsi->tx_busy = 0;
6608 vsi->tx_linearize = 0;
6609 vsi->rx_buf_failed = 0;
6610 vsi->rx_page_failed = 0;
6611
6612 rcu_read_lock();
6613
6614 /* update Tx rings counters */
6615 ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
6616 vsi->num_txq);
6617
6618 /* update Rx rings counters */
6619 ice_for_each_rxq(vsi, i) {
6620 struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
6621 struct ice_ring_stats *ring_stats;
6622
6623 ring_stats = ring->ring_stats;
6624 ice_fetch_u64_stats_per_ring(&ring_stats->syncp,
6625 ring_stats->stats, &pkts,
6626 &bytes);
6627 vsi_stats->rx_packets += pkts;
6628 vsi_stats->rx_bytes += bytes;
6629 vsi->rx_buf_failed += ring_stats->rx_stats.alloc_buf_failed;
6630 vsi->rx_page_failed += ring_stats->rx_stats.alloc_page_failed;
6631 }
6632
6633 /* update XDP Tx rings counters */
6634 if (ice_is_xdp_ena_vsi(vsi))
6635 ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
6636 vsi->num_xdp_txq);
6637
6638 rcu_read_unlock();
6639
6640 net_stats = &vsi->net_stats;
6641 stats_prev = &vsi->net_stats_prev;
6642
6643 /* Update netdev counters, but keep in mind that values could start at
6644 * random value after PF reset. And as we increase the reported stat by
6645 * diff of Prev-Cur, we need to be sure that Prev is valid. If it's not,
6646 * let's skip this round.
6647 */
6648 if (likely(pf->stat_prev_loaded)) {
6649 net_stats->tx_packets += vsi_stats->tx_packets - stats_prev->tx_packets;
6650 net_stats->tx_bytes += vsi_stats->tx_bytes - stats_prev->tx_bytes;
6651 net_stats->rx_packets += vsi_stats->rx_packets - stats_prev->rx_packets;
6652 net_stats->rx_bytes += vsi_stats->rx_bytes - stats_prev->rx_bytes;
6653 }
6654
6655 stats_prev->tx_packets = vsi_stats->tx_packets;
6656 stats_prev->tx_bytes = vsi_stats->tx_bytes;
6657 stats_prev->rx_packets = vsi_stats->rx_packets;
6658 stats_prev->rx_bytes = vsi_stats->rx_bytes;
6659
6660 kfree(vsi_stats);
6661 }
6662
6663 /**
6664 * ice_update_vsi_stats - Update VSI stats counters
6665 * @vsi: the VSI to be updated
6666 */
ice_update_vsi_stats(struct ice_vsi * vsi)6667 void ice_update_vsi_stats(struct ice_vsi *vsi)
6668 {
6669 struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6670 struct ice_eth_stats *cur_es = &vsi->eth_stats;
6671 struct ice_pf *pf = vsi->back;
6672
6673 if (test_bit(ICE_VSI_DOWN, vsi->state) ||
6674 test_bit(ICE_CFG_BUSY, pf->state))
6675 return;
6676
6677 /* get stats as recorded by Tx/Rx rings */
6678 ice_update_vsi_ring_stats(vsi);
6679
6680 /* get VSI stats as recorded by the hardware */
6681 ice_update_eth_stats(vsi);
6682
6683 cur_ns->tx_errors = cur_es->tx_errors;
6684 cur_ns->rx_dropped = cur_es->rx_discards;
6685 cur_ns->tx_dropped = cur_es->tx_discards;
6686 cur_ns->multicast = cur_es->rx_multicast;
6687
6688 /* update some more netdev stats if this is main VSI */
6689 if (vsi->type == ICE_VSI_PF) {
6690 cur_ns->rx_crc_errors = pf->stats.crc_errors;
6691 cur_ns->rx_errors = pf->stats.crc_errors +
6692 pf->stats.illegal_bytes +
6693 pf->stats.rx_len_errors +
6694 pf->stats.rx_undersize +
6695 pf->hw_csum_rx_error +
6696 pf->stats.rx_jabber +
6697 pf->stats.rx_fragments +
6698 pf->stats.rx_oversize;
6699 cur_ns->rx_length_errors = pf->stats.rx_len_errors;
6700 /* record drops from the port level */
6701 cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
6702 }
6703 }
6704
6705 /**
6706 * ice_update_pf_stats - Update PF port stats counters
6707 * @pf: PF whose stats needs to be updated
6708 */
ice_update_pf_stats(struct ice_pf * pf)6709 void ice_update_pf_stats(struct ice_pf *pf)
6710 {
6711 struct ice_hw_port_stats *prev_ps, *cur_ps;
6712 struct ice_hw *hw = &pf->hw;
6713 u16 fd_ctr_base;
6714 u8 port;
6715
6716 port = hw->port_info->lport;
6717 prev_ps = &pf->stats_prev;
6718 cur_ps = &pf->stats;
6719
6720 if (ice_is_reset_in_progress(pf->state))
6721 pf->stat_prev_loaded = false;
6722
6723 ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
6724 &prev_ps->eth.rx_bytes,
6725 &cur_ps->eth.rx_bytes);
6726
6727 ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
6728 &prev_ps->eth.rx_unicast,
6729 &cur_ps->eth.rx_unicast);
6730
6731 ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
6732 &prev_ps->eth.rx_multicast,
6733 &cur_ps->eth.rx_multicast);
6734
6735 ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
6736 &prev_ps->eth.rx_broadcast,
6737 &cur_ps->eth.rx_broadcast);
6738
6739 ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
6740 &prev_ps->eth.rx_discards,
6741 &cur_ps->eth.rx_discards);
6742
6743 ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
6744 &prev_ps->eth.tx_bytes,
6745 &cur_ps->eth.tx_bytes);
6746
6747 ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
6748 &prev_ps->eth.tx_unicast,
6749 &cur_ps->eth.tx_unicast);
6750
6751 ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
6752 &prev_ps->eth.tx_multicast,
6753 &cur_ps->eth.tx_multicast);
6754
6755 ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
6756 &prev_ps->eth.tx_broadcast,
6757 &cur_ps->eth.tx_broadcast);
6758
6759 ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
6760 &prev_ps->tx_dropped_link_down,
6761 &cur_ps->tx_dropped_link_down);
6762
6763 ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
6764 &prev_ps->rx_size_64, &cur_ps->rx_size_64);
6765
6766 ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
6767 &prev_ps->rx_size_127, &cur_ps->rx_size_127);
6768
6769 ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
6770 &prev_ps->rx_size_255, &cur_ps->rx_size_255);
6771
6772 ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
6773 &prev_ps->rx_size_511, &cur_ps->rx_size_511);
6774
6775 ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
6776 &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
6777
6778 ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
6779 &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
6780
6781 ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
6782 &prev_ps->rx_size_big, &cur_ps->rx_size_big);
6783
6784 ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
6785 &prev_ps->tx_size_64, &cur_ps->tx_size_64);
6786
6787 ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
6788 &prev_ps->tx_size_127, &cur_ps->tx_size_127);
6789
6790 ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
6791 &prev_ps->tx_size_255, &cur_ps->tx_size_255);
6792
6793 ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
6794 &prev_ps->tx_size_511, &cur_ps->tx_size_511);
6795
6796 ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
6797 &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
6798
6799 ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
6800 &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
6801
6802 ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
6803 &prev_ps->tx_size_big, &cur_ps->tx_size_big);
6804
6805 fd_ctr_base = hw->fd_ctr_base;
6806
6807 ice_stat_update40(hw,
6808 GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
6809 pf->stat_prev_loaded, &prev_ps->fd_sb_match,
6810 &cur_ps->fd_sb_match);
6811 ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
6812 &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
6813
6814 ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
6815 &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
6816
6817 ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
6818 &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
6819
6820 ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
6821 &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
6822
6823 ice_update_dcb_stats(pf);
6824
6825 ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
6826 &prev_ps->crc_errors, &cur_ps->crc_errors);
6827
6828 ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
6829 &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
6830
6831 ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
6832 &prev_ps->mac_local_faults,
6833 &cur_ps->mac_local_faults);
6834
6835 ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
6836 &prev_ps->mac_remote_faults,
6837 &cur_ps->mac_remote_faults);
6838
6839 ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
6840 &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
6841
6842 ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
6843 &prev_ps->rx_undersize, &cur_ps->rx_undersize);
6844
6845 ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
6846 &prev_ps->rx_fragments, &cur_ps->rx_fragments);
6847
6848 ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
6849 &prev_ps->rx_oversize, &cur_ps->rx_oversize);
6850
6851 ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
6852 &prev_ps->rx_jabber, &cur_ps->rx_jabber);
6853
6854 cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
6855
6856 pf->stat_prev_loaded = true;
6857 }
6858
6859 /**
6860 * ice_get_stats64 - get statistics for network device structure
6861 * @netdev: network interface device structure
6862 * @stats: main device statistics structure
6863 */
6864 static
ice_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)6865 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
6866 {
6867 struct ice_netdev_priv *np = netdev_priv(netdev);
6868 struct rtnl_link_stats64 *vsi_stats;
6869 struct ice_vsi *vsi = np->vsi;
6870
6871 vsi_stats = &vsi->net_stats;
6872
6873 if (!vsi->num_txq || !vsi->num_rxq)
6874 return;
6875
6876 /* netdev packet/byte stats come from ring counter. These are obtained
6877 * by summing up ring counters (done by ice_update_vsi_ring_stats).
6878 * But, only call the update routine and read the registers if VSI is
6879 * not down.
6880 */
6881 if (!test_bit(ICE_VSI_DOWN, vsi->state))
6882 ice_update_vsi_ring_stats(vsi);
6883 stats->tx_packets = vsi_stats->tx_packets;
6884 stats->tx_bytes = vsi_stats->tx_bytes;
6885 stats->rx_packets = vsi_stats->rx_packets;
6886 stats->rx_bytes = vsi_stats->rx_bytes;
6887
6888 /* The rest of the stats can be read from the hardware but instead we
6889 * just return values that the watchdog task has already obtained from
6890 * the hardware.
6891 */
6892 stats->multicast = vsi_stats->multicast;
6893 stats->tx_errors = vsi_stats->tx_errors;
6894 stats->tx_dropped = vsi_stats->tx_dropped;
6895 stats->rx_errors = vsi_stats->rx_errors;
6896 stats->rx_dropped = vsi_stats->rx_dropped;
6897 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
6898 stats->rx_length_errors = vsi_stats->rx_length_errors;
6899 }
6900
6901 /**
6902 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
6903 * @vsi: VSI having NAPI disabled
6904 */
ice_napi_disable_all(struct ice_vsi * vsi)6905 static void ice_napi_disable_all(struct ice_vsi *vsi)
6906 {
6907 int q_idx;
6908
6909 if (!vsi->netdev)
6910 return;
6911
6912 ice_for_each_q_vector(vsi, q_idx) {
6913 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6914
6915 if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6916 napi_disable(&q_vector->napi);
6917
6918 cancel_work_sync(&q_vector->tx.dim.work);
6919 cancel_work_sync(&q_vector->rx.dim.work);
6920 }
6921 }
6922
6923 /**
6924 * ice_down - Shutdown the connection
6925 * @vsi: The VSI being stopped
6926 *
6927 * Caller of this function is expected to set the vsi->state ICE_DOWN bit
6928 */
ice_down(struct ice_vsi * vsi)6929 int ice_down(struct ice_vsi *vsi)
6930 {
6931 int i, tx_err, rx_err, vlan_err = 0;
6932
6933 WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
6934
6935 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6936 vlan_err = ice_vsi_del_vlan_zero(vsi);
6937 ice_ptp_link_change(vsi->back, vsi->back->hw.pf_id, false);
6938 netif_carrier_off(vsi->netdev);
6939 netif_tx_disable(vsi->netdev);
6940 } else if (vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
6941 ice_eswitch_stop_all_tx_queues(vsi->back);
6942 }
6943
6944 ice_vsi_dis_irq(vsi);
6945
6946 tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
6947 if (tx_err)
6948 netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
6949 vsi->vsi_num, tx_err);
6950 if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
6951 tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
6952 if (tx_err)
6953 netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
6954 vsi->vsi_num, tx_err);
6955 }
6956
6957 rx_err = ice_vsi_stop_all_rx_rings(vsi);
6958 if (rx_err)
6959 netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
6960 vsi->vsi_num, rx_err);
6961
6962 ice_napi_disable_all(vsi);
6963
6964 ice_for_each_txq(vsi, i)
6965 ice_clean_tx_ring(vsi->tx_rings[i]);
6966
6967 if (ice_is_xdp_ena_vsi(vsi))
6968 ice_for_each_xdp_txq(vsi, i)
6969 ice_clean_tx_ring(vsi->xdp_rings[i]);
6970
6971 ice_for_each_rxq(vsi, i)
6972 ice_clean_rx_ring(vsi->rx_rings[i]);
6973
6974 if (tx_err || rx_err || vlan_err) {
6975 netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
6976 vsi->vsi_num, vsi->vsw->sw_id);
6977 return -EIO;
6978 }
6979
6980 return 0;
6981 }
6982
6983 /**
6984 * ice_down_up - shutdown the VSI connection and bring it up
6985 * @vsi: the VSI to be reconnected
6986 */
ice_down_up(struct ice_vsi * vsi)6987 int ice_down_up(struct ice_vsi *vsi)
6988 {
6989 int ret;
6990
6991 /* if DOWN already set, nothing to do */
6992 if (test_and_set_bit(ICE_VSI_DOWN, vsi->state))
6993 return 0;
6994
6995 ret = ice_down(vsi);
6996 if (ret)
6997 return ret;
6998
6999 ret = ice_up(vsi);
7000 if (ret) {
7001 netdev_err(vsi->netdev, "reallocating resources failed during netdev features change, may need to reload driver\n");
7002 return ret;
7003 }
7004
7005 return 0;
7006 }
7007
7008 /**
7009 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
7010 * @vsi: VSI having resources allocated
7011 *
7012 * Return 0 on success, negative on failure
7013 */
ice_vsi_setup_tx_rings(struct ice_vsi * vsi)7014 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
7015 {
7016 int i, err = 0;
7017
7018 if (!vsi->num_txq) {
7019 dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
7020 vsi->vsi_num);
7021 return -EINVAL;
7022 }
7023
7024 ice_for_each_txq(vsi, i) {
7025 struct ice_tx_ring *ring = vsi->tx_rings[i];
7026
7027 if (!ring)
7028 return -EINVAL;
7029
7030 if (vsi->netdev)
7031 ring->netdev = vsi->netdev;
7032 err = ice_setup_tx_ring(ring);
7033 if (err)
7034 break;
7035 }
7036
7037 return err;
7038 }
7039
7040 /**
7041 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
7042 * @vsi: VSI having resources allocated
7043 *
7044 * Return 0 on success, negative on failure
7045 */
ice_vsi_setup_rx_rings(struct ice_vsi * vsi)7046 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
7047 {
7048 int i, err = 0;
7049
7050 if (!vsi->num_rxq) {
7051 dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
7052 vsi->vsi_num);
7053 return -EINVAL;
7054 }
7055
7056 ice_for_each_rxq(vsi, i) {
7057 struct ice_rx_ring *ring = vsi->rx_rings[i];
7058
7059 if (!ring)
7060 return -EINVAL;
7061
7062 if (vsi->netdev)
7063 ring->netdev = vsi->netdev;
7064 err = ice_setup_rx_ring(ring);
7065 if (err)
7066 break;
7067 }
7068
7069 return err;
7070 }
7071
7072 /**
7073 * ice_vsi_open_ctrl - open control VSI for use
7074 * @vsi: the VSI to open
7075 *
7076 * Initialization of the Control VSI
7077 *
7078 * Returns 0 on success, negative value on error
7079 */
ice_vsi_open_ctrl(struct ice_vsi * vsi)7080 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
7081 {
7082 char int_name[ICE_INT_NAME_STR_LEN];
7083 struct ice_pf *pf = vsi->back;
7084 struct device *dev;
7085 int err;
7086
7087 dev = ice_pf_to_dev(pf);
7088 /* allocate descriptors */
7089 err = ice_vsi_setup_tx_rings(vsi);
7090 if (err)
7091 goto err_setup_tx;
7092
7093 err = ice_vsi_setup_rx_rings(vsi);
7094 if (err)
7095 goto err_setup_rx;
7096
7097 err = ice_vsi_cfg_lan(vsi);
7098 if (err)
7099 goto err_setup_rx;
7100
7101 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
7102 dev_driver_string(dev), dev_name(dev));
7103 err = ice_vsi_req_irq_msix(vsi, int_name);
7104 if (err)
7105 goto err_setup_rx;
7106
7107 ice_vsi_cfg_msix(vsi);
7108
7109 err = ice_vsi_start_all_rx_rings(vsi);
7110 if (err)
7111 goto err_up_complete;
7112
7113 clear_bit(ICE_VSI_DOWN, vsi->state);
7114 ice_vsi_ena_irq(vsi);
7115
7116 return 0;
7117
7118 err_up_complete:
7119 ice_down(vsi);
7120 err_setup_rx:
7121 ice_vsi_free_rx_rings(vsi);
7122 err_setup_tx:
7123 ice_vsi_free_tx_rings(vsi);
7124
7125 return err;
7126 }
7127
7128 /**
7129 * ice_vsi_open - Called when a network interface is made active
7130 * @vsi: the VSI to open
7131 *
7132 * Initialization of the VSI
7133 *
7134 * Returns 0 on success, negative value on error
7135 */
ice_vsi_open(struct ice_vsi * vsi)7136 int ice_vsi_open(struct ice_vsi *vsi)
7137 {
7138 char int_name[ICE_INT_NAME_STR_LEN];
7139 struct ice_pf *pf = vsi->back;
7140 int err;
7141
7142 /* allocate descriptors */
7143 err = ice_vsi_setup_tx_rings(vsi);
7144 if (err)
7145 goto err_setup_tx;
7146
7147 err = ice_vsi_setup_rx_rings(vsi);
7148 if (err)
7149 goto err_setup_rx;
7150
7151 err = ice_vsi_cfg_lan(vsi);
7152 if (err)
7153 goto err_setup_rx;
7154
7155 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7156 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
7157 err = ice_vsi_req_irq_msix(vsi, int_name);
7158 if (err)
7159 goto err_setup_rx;
7160
7161 ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
7162
7163 if (vsi->type == ICE_VSI_PF) {
7164 /* Notify the stack of the actual queue counts. */
7165 err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
7166 if (err)
7167 goto err_set_qs;
7168
7169 err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
7170 if (err)
7171 goto err_set_qs;
7172 }
7173
7174 err = ice_up_complete(vsi);
7175 if (err)
7176 goto err_up_complete;
7177
7178 return 0;
7179
7180 err_up_complete:
7181 ice_down(vsi);
7182 err_set_qs:
7183 ice_vsi_free_irq(vsi);
7184 err_setup_rx:
7185 ice_vsi_free_rx_rings(vsi);
7186 err_setup_tx:
7187 ice_vsi_free_tx_rings(vsi);
7188
7189 return err;
7190 }
7191
7192 /**
7193 * ice_vsi_release_all - Delete all VSIs
7194 * @pf: PF from which all VSIs are being removed
7195 */
ice_vsi_release_all(struct ice_pf * pf)7196 static void ice_vsi_release_all(struct ice_pf *pf)
7197 {
7198 int err, i;
7199
7200 if (!pf->vsi)
7201 return;
7202
7203 ice_for_each_vsi(pf, i) {
7204 if (!pf->vsi[i])
7205 continue;
7206
7207 if (pf->vsi[i]->type == ICE_VSI_CHNL)
7208 continue;
7209
7210 err = ice_vsi_release(pf->vsi[i]);
7211 if (err)
7212 dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
7213 i, err, pf->vsi[i]->vsi_num);
7214 }
7215 }
7216
7217 /**
7218 * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
7219 * @pf: pointer to the PF instance
7220 * @type: VSI type to rebuild
7221 *
7222 * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
7223 */
ice_vsi_rebuild_by_type(struct ice_pf * pf,enum ice_vsi_type type)7224 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
7225 {
7226 struct device *dev = ice_pf_to_dev(pf);
7227 int i, err;
7228
7229 ice_for_each_vsi(pf, i) {
7230 struct ice_vsi *vsi = pf->vsi[i];
7231
7232 if (!vsi || vsi->type != type)
7233 continue;
7234
7235 /* rebuild the VSI */
7236 err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
7237 if (err) {
7238 dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
7239 err, vsi->idx, ice_vsi_type_str(type));
7240 return err;
7241 }
7242
7243 /* replay filters for the VSI */
7244 err = ice_replay_vsi(&pf->hw, vsi->idx);
7245 if (err) {
7246 dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
7247 err, vsi->idx, ice_vsi_type_str(type));
7248 return err;
7249 }
7250
7251 /* Re-map HW VSI number, using VSI handle that has been
7252 * previously validated in ice_replay_vsi() call above
7253 */
7254 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
7255
7256 /* enable the VSI */
7257 err = ice_ena_vsi(vsi, false);
7258 if (err) {
7259 dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
7260 err, vsi->idx, ice_vsi_type_str(type));
7261 return err;
7262 }
7263
7264 dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
7265 ice_vsi_type_str(type));
7266 }
7267
7268 return 0;
7269 }
7270
7271 /**
7272 * ice_update_pf_netdev_link - Update PF netdev link status
7273 * @pf: pointer to the PF instance
7274 */
ice_update_pf_netdev_link(struct ice_pf * pf)7275 static void ice_update_pf_netdev_link(struct ice_pf *pf)
7276 {
7277 bool link_up;
7278 int i;
7279
7280 ice_for_each_vsi(pf, i) {
7281 struct ice_vsi *vsi = pf->vsi[i];
7282
7283 if (!vsi || vsi->type != ICE_VSI_PF)
7284 return;
7285
7286 ice_get_link_status(pf->vsi[i]->port_info, &link_up);
7287 if (link_up) {
7288 netif_carrier_on(pf->vsi[i]->netdev);
7289 netif_tx_wake_all_queues(pf->vsi[i]->netdev);
7290 } else {
7291 netif_carrier_off(pf->vsi[i]->netdev);
7292 netif_tx_stop_all_queues(pf->vsi[i]->netdev);
7293 }
7294 }
7295 }
7296
7297 /**
7298 * ice_rebuild - rebuild after reset
7299 * @pf: PF to rebuild
7300 * @reset_type: type of reset
7301 *
7302 * Do not rebuild VF VSI in this flow because that is already handled via
7303 * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
7304 * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
7305 * to reset/rebuild all the VF VSI twice.
7306 */
ice_rebuild(struct ice_pf * pf,enum ice_reset_req reset_type)7307 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
7308 {
7309 struct ice_vsi *vsi = ice_get_main_vsi(pf);
7310 struct device *dev = ice_pf_to_dev(pf);
7311 struct ice_hw *hw = &pf->hw;
7312 bool dvm;
7313 int err;
7314
7315 if (test_bit(ICE_DOWN, pf->state))
7316 goto clear_recovery;
7317
7318 dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
7319
7320 #define ICE_EMP_RESET_SLEEP_MS 5000
7321 if (reset_type == ICE_RESET_EMPR) {
7322 /* If an EMP reset has occurred, any previously pending flash
7323 * update will have completed. We no longer know whether or
7324 * not the NVM update EMP reset is restricted.
7325 */
7326 pf->fw_emp_reset_disabled = false;
7327
7328 msleep(ICE_EMP_RESET_SLEEP_MS);
7329 }
7330
7331 err = ice_init_all_ctrlq(hw);
7332 if (err) {
7333 dev_err(dev, "control queues init failed %d\n", err);
7334 goto err_init_ctrlq;
7335 }
7336
7337 /* if DDP was previously loaded successfully */
7338 if (!ice_is_safe_mode(pf)) {
7339 /* reload the SW DB of filter tables */
7340 if (reset_type == ICE_RESET_PFR)
7341 ice_fill_blk_tbls(hw);
7342 else
7343 /* Reload DDP Package after CORER/GLOBR reset */
7344 ice_load_pkg(NULL, pf);
7345 }
7346
7347 err = ice_clear_pf_cfg(hw);
7348 if (err) {
7349 dev_err(dev, "clear PF configuration failed %d\n", err);
7350 goto err_init_ctrlq;
7351 }
7352
7353 ice_clear_pxe_mode(hw);
7354
7355 err = ice_init_nvm(hw);
7356 if (err) {
7357 dev_err(dev, "ice_init_nvm failed %d\n", err);
7358 goto err_init_ctrlq;
7359 }
7360
7361 err = ice_get_caps(hw);
7362 if (err) {
7363 dev_err(dev, "ice_get_caps failed %d\n", err);
7364 goto err_init_ctrlq;
7365 }
7366
7367 err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
7368 if (err) {
7369 dev_err(dev, "set_mac_cfg failed %d\n", err);
7370 goto err_init_ctrlq;
7371 }
7372
7373 dvm = ice_is_dvm_ena(hw);
7374
7375 err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
7376 if (err)
7377 goto err_init_ctrlq;
7378
7379 err = ice_sched_init_port(hw->port_info);
7380 if (err)
7381 goto err_sched_init_port;
7382
7383 /* start misc vector */
7384 err = ice_req_irq_msix_misc(pf);
7385 if (err) {
7386 dev_err(dev, "misc vector setup failed: %d\n", err);
7387 goto err_sched_init_port;
7388 }
7389
7390 if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7391 wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
7392 if (!rd32(hw, PFQF_FD_SIZE)) {
7393 u16 unused, guar, b_effort;
7394
7395 guar = hw->func_caps.fd_fltr_guar;
7396 b_effort = hw->func_caps.fd_fltr_best_effort;
7397
7398 /* force guaranteed filter pool for PF */
7399 ice_alloc_fd_guar_item(hw, &unused, guar);
7400 /* force shared filter pool for PF */
7401 ice_alloc_fd_shrd_item(hw, &unused, b_effort);
7402 }
7403 }
7404
7405 if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
7406 ice_dcb_rebuild(pf);
7407
7408 /* If the PF previously had enabled PTP, PTP init needs to happen before
7409 * the VSI rebuild. If not, this causes the PTP link status events to
7410 * fail.
7411 */
7412 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7413 ice_ptp_reset(pf);
7414
7415 if (ice_is_feature_supported(pf, ICE_F_GNSS))
7416 ice_gnss_init(pf);
7417
7418 /* rebuild PF VSI */
7419 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
7420 if (err) {
7421 dev_err(dev, "PF VSI rebuild failed: %d\n", err);
7422 goto err_vsi_rebuild;
7423 }
7424
7425 /* configure PTP timestamping after VSI rebuild */
7426 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7427 ice_ptp_cfg_timestamp(pf, false);
7428
7429 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_SWITCHDEV_CTRL);
7430 if (err) {
7431 dev_err(dev, "Switchdev CTRL VSI rebuild failed: %d\n", err);
7432 goto err_vsi_rebuild;
7433 }
7434
7435 if (reset_type == ICE_RESET_PFR) {
7436 err = ice_rebuild_channels(pf);
7437 if (err) {
7438 dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
7439 err);
7440 goto err_vsi_rebuild;
7441 }
7442 }
7443
7444 /* If Flow Director is active */
7445 if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7446 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
7447 if (err) {
7448 dev_err(dev, "control VSI rebuild failed: %d\n", err);
7449 goto err_vsi_rebuild;
7450 }
7451
7452 /* replay HW Flow Director recipes */
7453 if (hw->fdir_prof)
7454 ice_fdir_replay_flows(hw);
7455
7456 /* replay Flow Director filters */
7457 ice_fdir_replay_fltrs(pf);
7458
7459 ice_rebuild_arfs(pf);
7460 }
7461
7462 if (vsi && vsi->netdev)
7463 netif_device_attach(vsi->netdev);
7464
7465 ice_update_pf_netdev_link(pf);
7466
7467 /* tell the firmware we are up */
7468 err = ice_send_version(pf);
7469 if (err) {
7470 dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
7471 err);
7472 goto err_vsi_rebuild;
7473 }
7474
7475 ice_replay_post(hw);
7476
7477 /* if we get here, reset flow is successful */
7478 clear_bit(ICE_RESET_FAILED, pf->state);
7479
7480 ice_plug_aux_dev(pf);
7481 if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
7482 ice_lag_rebuild(pf);
7483 return;
7484
7485 err_vsi_rebuild:
7486 err_sched_init_port:
7487 ice_sched_cleanup_all(hw);
7488 err_init_ctrlq:
7489 ice_shutdown_all_ctrlq(hw);
7490 set_bit(ICE_RESET_FAILED, pf->state);
7491 clear_recovery:
7492 /* set this bit in PF state to control service task scheduling */
7493 set_bit(ICE_NEEDS_RESTART, pf->state);
7494 dev_err(dev, "Rebuild failed, unload and reload driver\n");
7495 }
7496
7497 /**
7498 * ice_change_mtu - NDO callback to change the MTU
7499 * @netdev: network interface device structure
7500 * @new_mtu: new value for maximum frame size
7501 *
7502 * Returns 0 on success, negative on failure
7503 */
ice_change_mtu(struct net_device * netdev,int new_mtu)7504 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
7505 {
7506 struct ice_netdev_priv *np = netdev_priv(netdev);
7507 struct ice_vsi *vsi = np->vsi;
7508 struct ice_pf *pf = vsi->back;
7509 struct bpf_prog *prog;
7510 u8 count = 0;
7511 int err = 0;
7512
7513 if (new_mtu == (int)netdev->mtu) {
7514 netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
7515 return 0;
7516 }
7517
7518 prog = vsi->xdp_prog;
7519 if (prog && !prog->aux->xdp_has_frags) {
7520 int frame_size = ice_max_xdp_frame_size(vsi);
7521
7522 if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
7523 netdev_err(netdev, "max MTU for XDP usage is %d\n",
7524 frame_size - ICE_ETH_PKT_HDR_PAD);
7525 return -EINVAL;
7526 }
7527 } else if (test_bit(ICE_FLAG_LEGACY_RX, pf->flags)) {
7528 if (new_mtu + ICE_ETH_PKT_HDR_PAD > ICE_MAX_FRAME_LEGACY_RX) {
7529 netdev_err(netdev, "Too big MTU for legacy-rx; Max is %d\n",
7530 ICE_MAX_FRAME_LEGACY_RX - ICE_ETH_PKT_HDR_PAD);
7531 return -EINVAL;
7532 }
7533 }
7534
7535 /* if a reset is in progress, wait for some time for it to complete */
7536 do {
7537 if (ice_is_reset_in_progress(pf->state)) {
7538 count++;
7539 usleep_range(1000, 2000);
7540 } else {
7541 break;
7542 }
7543
7544 } while (count < 100);
7545
7546 if (count == 100) {
7547 netdev_err(netdev, "can't change MTU. Device is busy\n");
7548 return -EBUSY;
7549 }
7550
7551 netdev->mtu = (unsigned int)new_mtu;
7552 err = ice_down_up(vsi);
7553 if (err)
7554 return err;
7555
7556 netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
7557 set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
7558
7559 return err;
7560 }
7561
7562 /**
7563 * ice_eth_ioctl - Access the hwtstamp interface
7564 * @netdev: network interface device structure
7565 * @ifr: interface request data
7566 * @cmd: ioctl command
7567 */
ice_eth_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)7568 static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
7569 {
7570 struct ice_netdev_priv *np = netdev_priv(netdev);
7571 struct ice_pf *pf = np->vsi->back;
7572
7573 switch (cmd) {
7574 case SIOCGHWTSTAMP:
7575 return ice_ptp_get_ts_config(pf, ifr);
7576 case SIOCSHWTSTAMP:
7577 return ice_ptp_set_ts_config(pf, ifr);
7578 default:
7579 return -EOPNOTSUPP;
7580 }
7581 }
7582
7583 /**
7584 * ice_aq_str - convert AQ err code to a string
7585 * @aq_err: the AQ error code to convert
7586 */
ice_aq_str(enum ice_aq_err aq_err)7587 const char *ice_aq_str(enum ice_aq_err aq_err)
7588 {
7589 switch (aq_err) {
7590 case ICE_AQ_RC_OK:
7591 return "OK";
7592 case ICE_AQ_RC_EPERM:
7593 return "ICE_AQ_RC_EPERM";
7594 case ICE_AQ_RC_ENOENT:
7595 return "ICE_AQ_RC_ENOENT";
7596 case ICE_AQ_RC_ENOMEM:
7597 return "ICE_AQ_RC_ENOMEM";
7598 case ICE_AQ_RC_EBUSY:
7599 return "ICE_AQ_RC_EBUSY";
7600 case ICE_AQ_RC_EEXIST:
7601 return "ICE_AQ_RC_EEXIST";
7602 case ICE_AQ_RC_EINVAL:
7603 return "ICE_AQ_RC_EINVAL";
7604 case ICE_AQ_RC_ENOSPC:
7605 return "ICE_AQ_RC_ENOSPC";
7606 case ICE_AQ_RC_ENOSYS:
7607 return "ICE_AQ_RC_ENOSYS";
7608 case ICE_AQ_RC_EMODE:
7609 return "ICE_AQ_RC_EMODE";
7610 case ICE_AQ_RC_ENOSEC:
7611 return "ICE_AQ_RC_ENOSEC";
7612 case ICE_AQ_RC_EBADSIG:
7613 return "ICE_AQ_RC_EBADSIG";
7614 case ICE_AQ_RC_ESVN:
7615 return "ICE_AQ_RC_ESVN";
7616 case ICE_AQ_RC_EBADMAN:
7617 return "ICE_AQ_RC_EBADMAN";
7618 case ICE_AQ_RC_EBADBUF:
7619 return "ICE_AQ_RC_EBADBUF";
7620 }
7621
7622 return "ICE_AQ_RC_UNKNOWN";
7623 }
7624
7625 /**
7626 * ice_set_rss_lut - Set RSS LUT
7627 * @vsi: Pointer to VSI structure
7628 * @lut: Lookup table
7629 * @lut_size: Lookup table size
7630 *
7631 * Returns 0 on success, negative on failure
7632 */
ice_set_rss_lut(struct ice_vsi * vsi,u8 * lut,u16 lut_size)7633 int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7634 {
7635 struct ice_aq_get_set_rss_lut_params params = {};
7636 struct ice_hw *hw = &vsi->back->hw;
7637 int status;
7638
7639 if (!lut)
7640 return -EINVAL;
7641
7642 params.vsi_handle = vsi->idx;
7643 params.lut_size = lut_size;
7644 params.lut_type = vsi->rss_lut_type;
7645 params.lut = lut;
7646
7647 status = ice_aq_set_rss_lut(hw, ¶ms);
7648 if (status)
7649 dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
7650 status, ice_aq_str(hw->adminq.sq_last_status));
7651
7652 return status;
7653 }
7654
7655 /**
7656 * ice_set_rss_key - Set RSS key
7657 * @vsi: Pointer to the VSI structure
7658 * @seed: RSS hash seed
7659 *
7660 * Returns 0 on success, negative on failure
7661 */
ice_set_rss_key(struct ice_vsi * vsi,u8 * seed)7662 int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
7663 {
7664 struct ice_hw *hw = &vsi->back->hw;
7665 int status;
7666
7667 if (!seed)
7668 return -EINVAL;
7669
7670 status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7671 if (status)
7672 dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
7673 status, ice_aq_str(hw->adminq.sq_last_status));
7674
7675 return status;
7676 }
7677
7678 /**
7679 * ice_get_rss_lut - Get RSS LUT
7680 * @vsi: Pointer to VSI structure
7681 * @lut: Buffer to store the lookup table entries
7682 * @lut_size: Size of buffer to store the lookup table entries
7683 *
7684 * Returns 0 on success, negative on failure
7685 */
ice_get_rss_lut(struct ice_vsi * vsi,u8 * lut,u16 lut_size)7686 int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7687 {
7688 struct ice_aq_get_set_rss_lut_params params = {};
7689 struct ice_hw *hw = &vsi->back->hw;
7690 int status;
7691
7692 if (!lut)
7693 return -EINVAL;
7694
7695 params.vsi_handle = vsi->idx;
7696 params.lut_size = lut_size;
7697 params.lut_type = vsi->rss_lut_type;
7698 params.lut = lut;
7699
7700 status = ice_aq_get_rss_lut(hw, ¶ms);
7701 if (status)
7702 dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
7703 status, ice_aq_str(hw->adminq.sq_last_status));
7704
7705 return status;
7706 }
7707
7708 /**
7709 * ice_get_rss_key - Get RSS key
7710 * @vsi: Pointer to VSI structure
7711 * @seed: Buffer to store the key in
7712 *
7713 * Returns 0 on success, negative on failure
7714 */
ice_get_rss_key(struct ice_vsi * vsi,u8 * seed)7715 int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
7716 {
7717 struct ice_hw *hw = &vsi->back->hw;
7718 int status;
7719
7720 if (!seed)
7721 return -EINVAL;
7722
7723 status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7724 if (status)
7725 dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
7726 status, ice_aq_str(hw->adminq.sq_last_status));
7727
7728 return status;
7729 }
7730
7731 /**
7732 * ice_bridge_getlink - Get the hardware bridge mode
7733 * @skb: skb buff
7734 * @pid: process ID
7735 * @seq: RTNL message seq
7736 * @dev: the netdev being configured
7737 * @filter_mask: filter mask passed in
7738 * @nlflags: netlink flags passed in
7739 *
7740 * Return the bridge mode (VEB/VEPA)
7741 */
7742 static int
ice_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)7743 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7744 struct net_device *dev, u32 filter_mask, int nlflags)
7745 {
7746 struct ice_netdev_priv *np = netdev_priv(dev);
7747 struct ice_vsi *vsi = np->vsi;
7748 struct ice_pf *pf = vsi->back;
7749 u16 bmode;
7750
7751 bmode = pf->first_sw->bridge_mode;
7752
7753 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
7754 filter_mask, NULL);
7755 }
7756
7757 /**
7758 * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
7759 * @vsi: Pointer to VSI structure
7760 * @bmode: Hardware bridge mode (VEB/VEPA)
7761 *
7762 * Returns 0 on success, negative on failure
7763 */
ice_vsi_update_bridge_mode(struct ice_vsi * vsi,u16 bmode)7764 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
7765 {
7766 struct ice_aqc_vsi_props *vsi_props;
7767 struct ice_hw *hw = &vsi->back->hw;
7768 struct ice_vsi_ctx *ctxt;
7769 int ret;
7770
7771 vsi_props = &vsi->info;
7772
7773 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
7774 if (!ctxt)
7775 return -ENOMEM;
7776
7777 ctxt->info = vsi->info;
7778
7779 if (bmode == BRIDGE_MODE_VEB)
7780 /* change from VEPA to VEB mode */
7781 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7782 else
7783 /* change from VEB to VEPA mode */
7784 ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7785 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
7786
7787 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
7788 if (ret) {
7789 dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
7790 bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
7791 goto out;
7792 }
7793 /* Update sw flags for book keeping */
7794 vsi_props->sw_flags = ctxt->info.sw_flags;
7795
7796 out:
7797 kfree(ctxt);
7798 return ret;
7799 }
7800
7801 /**
7802 * ice_bridge_setlink - Set the hardware bridge mode
7803 * @dev: the netdev being configured
7804 * @nlh: RTNL message
7805 * @flags: bridge setlink flags
7806 * @extack: netlink extended ack
7807 *
7808 * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
7809 * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
7810 * not already set for all VSIs connected to this switch. And also update the
7811 * unicast switch filter rules for the corresponding switch of the netdev.
7812 */
7813 static int
ice_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 __always_unused flags,struct netlink_ext_ack __always_unused * extack)7814 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7815 u16 __always_unused flags,
7816 struct netlink_ext_ack __always_unused *extack)
7817 {
7818 struct ice_netdev_priv *np = netdev_priv(dev);
7819 struct ice_pf *pf = np->vsi->back;
7820 struct nlattr *attr, *br_spec;
7821 struct ice_hw *hw = &pf->hw;
7822 struct ice_sw *pf_sw;
7823 int rem, v, err = 0;
7824
7825 pf_sw = pf->first_sw;
7826 /* find the attribute in the netlink message */
7827 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7828 if (!br_spec)
7829 return -EINVAL;
7830
7831 nla_for_each_nested(attr, br_spec, rem) {
7832 __u16 mode;
7833
7834 if (nla_type(attr) != IFLA_BRIDGE_MODE)
7835 continue;
7836 mode = nla_get_u16(attr);
7837 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
7838 return -EINVAL;
7839 /* Continue if bridge mode is not being flipped */
7840 if (mode == pf_sw->bridge_mode)
7841 continue;
7842 /* Iterates through the PF VSI list and update the loopback
7843 * mode of the VSI
7844 */
7845 ice_for_each_vsi(pf, v) {
7846 if (!pf->vsi[v])
7847 continue;
7848 err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
7849 if (err)
7850 return err;
7851 }
7852
7853 hw->evb_veb = (mode == BRIDGE_MODE_VEB);
7854 /* Update the unicast switch filter rules for the corresponding
7855 * switch of the netdev
7856 */
7857 err = ice_update_sw_rule_bridge_mode(hw);
7858 if (err) {
7859 netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
7860 mode, err,
7861 ice_aq_str(hw->adminq.sq_last_status));
7862 /* revert hw->evb_veb */
7863 hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
7864 return err;
7865 }
7866
7867 pf_sw->bridge_mode = mode;
7868 }
7869
7870 return 0;
7871 }
7872
7873 /**
7874 * ice_tx_timeout - Respond to a Tx Hang
7875 * @netdev: network interface device structure
7876 * @txqueue: Tx queue
7877 */
ice_tx_timeout(struct net_device * netdev,unsigned int txqueue)7878 static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
7879 {
7880 struct ice_netdev_priv *np = netdev_priv(netdev);
7881 struct ice_tx_ring *tx_ring = NULL;
7882 struct ice_vsi *vsi = np->vsi;
7883 struct ice_pf *pf = vsi->back;
7884 u32 i;
7885
7886 pf->tx_timeout_count++;
7887
7888 /* Check if PFC is enabled for the TC to which the queue belongs
7889 * to. If yes then Tx timeout is not caused by a hung queue, no
7890 * need to reset and rebuild
7891 */
7892 if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
7893 dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
7894 txqueue);
7895 return;
7896 }
7897
7898 /* now that we have an index, find the tx_ring struct */
7899 ice_for_each_txq(vsi, i)
7900 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
7901 if (txqueue == vsi->tx_rings[i]->q_index) {
7902 tx_ring = vsi->tx_rings[i];
7903 break;
7904 }
7905
7906 /* Reset recovery level if enough time has elapsed after last timeout.
7907 * Also ensure no new reset action happens before next timeout period.
7908 */
7909 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
7910 pf->tx_timeout_recovery_level = 1;
7911 else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
7912 netdev->watchdog_timeo)))
7913 return;
7914
7915 if (tx_ring) {
7916 struct ice_hw *hw = &pf->hw;
7917 u32 head, val = 0;
7918
7919 head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) &
7920 QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
7921 /* Read interrupt register */
7922 val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
7923
7924 netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
7925 vsi->vsi_num, txqueue, tx_ring->next_to_clean,
7926 head, tx_ring->next_to_use, val);
7927 }
7928
7929 pf->tx_timeout_last_recovery = jiffies;
7930 netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
7931 pf->tx_timeout_recovery_level, txqueue);
7932
7933 switch (pf->tx_timeout_recovery_level) {
7934 case 1:
7935 set_bit(ICE_PFR_REQ, pf->state);
7936 break;
7937 case 2:
7938 set_bit(ICE_CORER_REQ, pf->state);
7939 break;
7940 case 3:
7941 set_bit(ICE_GLOBR_REQ, pf->state);
7942 break;
7943 default:
7944 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
7945 set_bit(ICE_DOWN, pf->state);
7946 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
7947 set_bit(ICE_SERVICE_DIS, pf->state);
7948 break;
7949 }
7950
7951 ice_service_task_schedule(pf);
7952 pf->tx_timeout_recovery_level++;
7953 }
7954
7955 /**
7956 * ice_setup_tc_cls_flower - flower classifier offloads
7957 * @np: net device to configure
7958 * @filter_dev: device on which filter is added
7959 * @cls_flower: offload data
7960 */
7961 static int
ice_setup_tc_cls_flower(struct ice_netdev_priv * np,struct net_device * filter_dev,struct flow_cls_offload * cls_flower)7962 ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
7963 struct net_device *filter_dev,
7964 struct flow_cls_offload *cls_flower)
7965 {
7966 struct ice_vsi *vsi = np->vsi;
7967
7968 if (cls_flower->common.chain_index)
7969 return -EOPNOTSUPP;
7970
7971 switch (cls_flower->command) {
7972 case FLOW_CLS_REPLACE:
7973 return ice_add_cls_flower(filter_dev, vsi, cls_flower);
7974 case FLOW_CLS_DESTROY:
7975 return ice_del_cls_flower(vsi, cls_flower);
7976 default:
7977 return -EINVAL;
7978 }
7979 }
7980
7981 /**
7982 * ice_setup_tc_block_cb - callback handler registered for TC block
7983 * @type: TC SETUP type
7984 * @type_data: TC flower offload data that contains user input
7985 * @cb_priv: netdev private data
7986 */
7987 static int
ice_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)7988 ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
7989 {
7990 struct ice_netdev_priv *np = cb_priv;
7991
7992 switch (type) {
7993 case TC_SETUP_CLSFLOWER:
7994 return ice_setup_tc_cls_flower(np, np->vsi->netdev,
7995 type_data);
7996 default:
7997 return -EOPNOTSUPP;
7998 }
7999 }
8000
8001 /**
8002 * ice_validate_mqprio_qopt - Validate TCF input parameters
8003 * @vsi: Pointer to VSI
8004 * @mqprio_qopt: input parameters for mqprio queue configuration
8005 *
8006 * This function validates MQPRIO params, such as qcount (power of 2 wherever
8007 * needed), and make sure user doesn't specify qcount and BW rate limit
8008 * for TCs, which are more than "num_tc"
8009 */
8010 static int
ice_validate_mqprio_qopt(struct ice_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)8011 ice_validate_mqprio_qopt(struct ice_vsi *vsi,
8012 struct tc_mqprio_qopt_offload *mqprio_qopt)
8013 {
8014 int non_power_of_2_qcount = 0;
8015 struct ice_pf *pf = vsi->back;
8016 int max_rss_q_cnt = 0;
8017 u64 sum_min_rate = 0;
8018 struct device *dev;
8019 int i, speed;
8020 u8 num_tc;
8021
8022 if (vsi->type != ICE_VSI_PF)
8023 return -EINVAL;
8024
8025 if (mqprio_qopt->qopt.offset[0] != 0 ||
8026 mqprio_qopt->qopt.num_tc < 1 ||
8027 mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
8028 return -EINVAL;
8029
8030 dev = ice_pf_to_dev(pf);
8031 vsi->ch_rss_size = 0;
8032 num_tc = mqprio_qopt->qopt.num_tc;
8033 speed = ice_get_link_speed_kbps(vsi);
8034
8035 for (i = 0; num_tc; i++) {
8036 int qcount = mqprio_qopt->qopt.count[i];
8037 u64 max_rate, min_rate, rem;
8038
8039 if (!qcount)
8040 return -EINVAL;
8041
8042 if (is_power_of_2(qcount)) {
8043 if (non_power_of_2_qcount &&
8044 qcount > non_power_of_2_qcount) {
8045 dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
8046 qcount, non_power_of_2_qcount);
8047 return -EINVAL;
8048 }
8049 if (qcount > max_rss_q_cnt)
8050 max_rss_q_cnt = qcount;
8051 } else {
8052 if (non_power_of_2_qcount &&
8053 qcount != non_power_of_2_qcount) {
8054 dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
8055 qcount, non_power_of_2_qcount);
8056 return -EINVAL;
8057 }
8058 if (qcount < max_rss_q_cnt) {
8059 dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
8060 qcount, max_rss_q_cnt);
8061 return -EINVAL;
8062 }
8063 max_rss_q_cnt = qcount;
8064 non_power_of_2_qcount = qcount;
8065 }
8066
8067 /* TC command takes input in K/N/Gbps or K/M/Gbit etc but
8068 * converts the bandwidth rate limit into Bytes/s when
8069 * passing it down to the driver. So convert input bandwidth
8070 * from Bytes/s to Kbps
8071 */
8072 max_rate = mqprio_qopt->max_rate[i];
8073 max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
8074
8075 /* min_rate is minimum guaranteed rate and it can't be zero */
8076 min_rate = mqprio_qopt->min_rate[i];
8077 min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
8078 sum_min_rate += min_rate;
8079
8080 if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
8081 dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
8082 min_rate, ICE_MIN_BW_LIMIT);
8083 return -EINVAL;
8084 }
8085
8086 if (max_rate && max_rate > speed) {
8087 dev_err(dev, "TC%d: max_rate(%llu Kbps) > link speed of %u Kbps\n",
8088 i, max_rate, speed);
8089 return -EINVAL;
8090 }
8091
8092 iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
8093 if (rem) {
8094 dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
8095 i, ICE_MIN_BW_LIMIT);
8096 return -EINVAL;
8097 }
8098
8099 iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
8100 if (rem) {
8101 dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
8102 i, ICE_MIN_BW_LIMIT);
8103 return -EINVAL;
8104 }
8105
8106 /* min_rate can't be more than max_rate, except when max_rate
8107 * is zero (implies max_rate sought is max line rate). In such
8108 * a case min_rate can be more than max.
8109 */
8110 if (max_rate && min_rate > max_rate) {
8111 dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
8112 min_rate, max_rate);
8113 return -EINVAL;
8114 }
8115
8116 if (i >= mqprio_qopt->qopt.num_tc - 1)
8117 break;
8118 if (mqprio_qopt->qopt.offset[i + 1] !=
8119 (mqprio_qopt->qopt.offset[i] + qcount))
8120 return -EINVAL;
8121 }
8122 if (vsi->num_rxq <
8123 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8124 return -EINVAL;
8125 if (vsi->num_txq <
8126 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8127 return -EINVAL;
8128
8129 if (sum_min_rate && sum_min_rate > (u64)speed) {
8130 dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
8131 sum_min_rate, speed);
8132 return -EINVAL;
8133 }
8134
8135 /* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
8136 vsi->ch_rss_size = max_rss_q_cnt;
8137
8138 return 0;
8139 }
8140
8141 /**
8142 * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
8143 * @pf: ptr to PF device
8144 * @vsi: ptr to VSI
8145 */
ice_add_vsi_to_fdir(struct ice_pf * pf,struct ice_vsi * vsi)8146 static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
8147 {
8148 struct device *dev = ice_pf_to_dev(pf);
8149 bool added = false;
8150 struct ice_hw *hw;
8151 int flow;
8152
8153 if (!(vsi->num_gfltr || vsi->num_bfltr))
8154 return -EINVAL;
8155
8156 hw = &pf->hw;
8157 for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
8158 struct ice_fd_hw_prof *prof;
8159 int tun, status;
8160 u64 entry_h;
8161
8162 if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
8163 hw->fdir_prof[flow]->cnt))
8164 continue;
8165
8166 for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
8167 enum ice_flow_priority prio;
8168 u64 prof_id;
8169
8170 /* add this VSI to FDir profile for this flow */
8171 prio = ICE_FLOW_PRIO_NORMAL;
8172 prof = hw->fdir_prof[flow];
8173 prof_id = flow + tun * ICE_FLTR_PTYPE_MAX;
8174 status = ice_flow_add_entry(hw, ICE_BLK_FD, prof_id,
8175 prof->vsi_h[0], vsi->idx,
8176 prio, prof->fdir_seg[tun],
8177 &entry_h);
8178 if (status) {
8179 dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
8180 vsi->idx, flow);
8181 continue;
8182 }
8183
8184 prof->entry_h[prof->cnt][tun] = entry_h;
8185 }
8186
8187 /* store VSI for filter replay and delete */
8188 prof->vsi_h[prof->cnt] = vsi->idx;
8189 prof->cnt++;
8190
8191 added = true;
8192 dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
8193 flow);
8194 }
8195
8196 if (!added)
8197 dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
8198
8199 return 0;
8200 }
8201
8202 /**
8203 * ice_add_channel - add a channel by adding VSI
8204 * @pf: ptr to PF device
8205 * @sw_id: underlying HW switching element ID
8206 * @ch: ptr to channel structure
8207 *
8208 * Add a channel (VSI) using add_vsi and queue_map
8209 */
ice_add_channel(struct ice_pf * pf,u16 sw_id,struct ice_channel * ch)8210 static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
8211 {
8212 struct device *dev = ice_pf_to_dev(pf);
8213 struct ice_vsi *vsi;
8214
8215 if (ch->type != ICE_VSI_CHNL) {
8216 dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
8217 return -EINVAL;
8218 }
8219
8220 vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
8221 if (!vsi || vsi->type != ICE_VSI_CHNL) {
8222 dev_err(dev, "create chnl VSI failure\n");
8223 return -EINVAL;
8224 }
8225
8226 ice_add_vsi_to_fdir(pf, vsi);
8227
8228 ch->sw_id = sw_id;
8229 ch->vsi_num = vsi->vsi_num;
8230 ch->info.mapping_flags = vsi->info.mapping_flags;
8231 ch->ch_vsi = vsi;
8232 /* set the back pointer of channel for newly created VSI */
8233 vsi->ch = ch;
8234
8235 memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
8236 sizeof(vsi->info.q_mapping));
8237 memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
8238 sizeof(vsi->info.tc_mapping));
8239
8240 return 0;
8241 }
8242
8243 /**
8244 * ice_chnl_cfg_res
8245 * @vsi: the VSI being setup
8246 * @ch: ptr to channel structure
8247 *
8248 * Configure channel specific resources such as rings, vector.
8249 */
ice_chnl_cfg_res(struct ice_vsi * vsi,struct ice_channel * ch)8250 static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
8251 {
8252 int i;
8253
8254 for (i = 0; i < ch->num_txq; i++) {
8255 struct ice_q_vector *tx_q_vector, *rx_q_vector;
8256 struct ice_ring_container *rc;
8257 struct ice_tx_ring *tx_ring;
8258 struct ice_rx_ring *rx_ring;
8259
8260 tx_ring = vsi->tx_rings[ch->base_q + i];
8261 rx_ring = vsi->rx_rings[ch->base_q + i];
8262 if (!tx_ring || !rx_ring)
8263 continue;
8264
8265 /* setup ring being channel enabled */
8266 tx_ring->ch = ch;
8267 rx_ring->ch = ch;
8268
8269 /* following code block sets up vector specific attributes */
8270 tx_q_vector = tx_ring->q_vector;
8271 rx_q_vector = rx_ring->q_vector;
8272 if (!tx_q_vector && !rx_q_vector)
8273 continue;
8274
8275 if (tx_q_vector) {
8276 tx_q_vector->ch = ch;
8277 /* setup Tx and Rx ITR setting if DIM is off */
8278 rc = &tx_q_vector->tx;
8279 if (!ITR_IS_DYNAMIC(rc))
8280 ice_write_itr(rc, rc->itr_setting);
8281 }
8282 if (rx_q_vector) {
8283 rx_q_vector->ch = ch;
8284 /* setup Tx and Rx ITR setting if DIM is off */
8285 rc = &rx_q_vector->rx;
8286 if (!ITR_IS_DYNAMIC(rc))
8287 ice_write_itr(rc, rc->itr_setting);
8288 }
8289 }
8290
8291 /* it is safe to assume that, if channel has non-zero num_t[r]xq, then
8292 * GLINT_ITR register would have written to perform in-context
8293 * update, hence perform flush
8294 */
8295 if (ch->num_txq || ch->num_rxq)
8296 ice_flush(&vsi->back->hw);
8297 }
8298
8299 /**
8300 * ice_cfg_chnl_all_res - configure channel resources
8301 * @vsi: pte to main_vsi
8302 * @ch: ptr to channel structure
8303 *
8304 * This function configures channel specific resources such as flow-director
8305 * counter index, and other resources such as queues, vectors, ITR settings
8306 */
8307 static void
ice_cfg_chnl_all_res(struct ice_vsi * vsi,struct ice_channel * ch)8308 ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
8309 {
8310 /* configure channel (aka ADQ) resources such as queues, vectors,
8311 * ITR settings for channel specific vectors and anything else
8312 */
8313 ice_chnl_cfg_res(vsi, ch);
8314 }
8315
8316 /**
8317 * ice_setup_hw_channel - setup new channel
8318 * @pf: ptr to PF device
8319 * @vsi: the VSI being setup
8320 * @ch: ptr to channel structure
8321 * @sw_id: underlying HW switching element ID
8322 * @type: type of channel to be created (VMDq2/VF)
8323 *
8324 * Setup new channel (VSI) based on specified type (VMDq2/VF)
8325 * and configures Tx rings accordingly
8326 */
8327 static int
ice_setup_hw_channel(struct ice_pf * pf,struct ice_vsi * vsi,struct ice_channel * ch,u16 sw_id,u8 type)8328 ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8329 struct ice_channel *ch, u16 sw_id, u8 type)
8330 {
8331 struct device *dev = ice_pf_to_dev(pf);
8332 int ret;
8333
8334 ch->base_q = vsi->next_base_q;
8335 ch->type = type;
8336
8337 ret = ice_add_channel(pf, sw_id, ch);
8338 if (ret) {
8339 dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
8340 return ret;
8341 }
8342
8343 /* configure/setup ADQ specific resources */
8344 ice_cfg_chnl_all_res(vsi, ch);
8345
8346 /* make sure to update the next_base_q so that subsequent channel's
8347 * (aka ADQ) VSI queue map is correct
8348 */
8349 vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
8350 dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
8351 ch->num_rxq);
8352
8353 return 0;
8354 }
8355
8356 /**
8357 * ice_setup_channel - setup new channel using uplink element
8358 * @pf: ptr to PF device
8359 * @vsi: the VSI being setup
8360 * @ch: ptr to channel structure
8361 *
8362 * Setup new channel (VSI) based on specified type (VMDq2/VF)
8363 * and uplink switching element
8364 */
8365 static bool
ice_setup_channel(struct ice_pf * pf,struct ice_vsi * vsi,struct ice_channel * ch)8366 ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8367 struct ice_channel *ch)
8368 {
8369 struct device *dev = ice_pf_to_dev(pf);
8370 u16 sw_id;
8371 int ret;
8372
8373 if (vsi->type != ICE_VSI_PF) {
8374 dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
8375 return false;
8376 }
8377
8378 sw_id = pf->first_sw->sw_id;
8379
8380 /* create channel (VSI) */
8381 ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
8382 if (ret) {
8383 dev_err(dev, "failed to setup hw_channel\n");
8384 return false;
8385 }
8386 dev_dbg(dev, "successfully created channel()\n");
8387
8388 return ch->ch_vsi ? true : false;
8389 }
8390
8391 /**
8392 * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
8393 * @vsi: VSI to be configured
8394 * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
8395 * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
8396 */
8397 static int
ice_set_bw_limit(struct ice_vsi * vsi,u64 max_tx_rate,u64 min_tx_rate)8398 ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
8399 {
8400 int err;
8401
8402 err = ice_set_min_bw_limit(vsi, min_tx_rate);
8403 if (err)
8404 return err;
8405
8406 return ice_set_max_bw_limit(vsi, max_tx_rate);
8407 }
8408
8409 /**
8410 * ice_create_q_channel - function to create channel
8411 * @vsi: VSI to be configured
8412 * @ch: ptr to channel (it contains channel specific params)
8413 *
8414 * This function creates channel (VSI) using num_queues specified by user,
8415 * reconfigs RSS if needed.
8416 */
ice_create_q_channel(struct ice_vsi * vsi,struct ice_channel * ch)8417 static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
8418 {
8419 struct ice_pf *pf = vsi->back;
8420 struct device *dev;
8421
8422 if (!ch)
8423 return -EINVAL;
8424
8425 dev = ice_pf_to_dev(pf);
8426 if (!ch->num_txq || !ch->num_rxq) {
8427 dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
8428 return -EINVAL;
8429 }
8430
8431 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
8432 dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
8433 vsi->cnt_q_avail, ch->num_txq);
8434 return -EINVAL;
8435 }
8436
8437 if (!ice_setup_channel(pf, vsi, ch)) {
8438 dev_info(dev, "Failed to setup channel\n");
8439 return -EINVAL;
8440 }
8441 /* configure BW rate limit */
8442 if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
8443 int ret;
8444
8445 ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
8446 ch->min_tx_rate);
8447 if (ret)
8448 dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
8449 ch->max_tx_rate, ch->ch_vsi->vsi_num);
8450 else
8451 dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
8452 ch->max_tx_rate, ch->ch_vsi->vsi_num);
8453 }
8454
8455 vsi->cnt_q_avail -= ch->num_txq;
8456
8457 return 0;
8458 }
8459
8460 /**
8461 * ice_rem_all_chnl_fltrs - removes all channel filters
8462 * @pf: ptr to PF, TC-flower based filter are tracked at PF level
8463 *
8464 * Remove all advanced switch filters only if they are channel specific
8465 * tc-flower based filter
8466 */
ice_rem_all_chnl_fltrs(struct ice_pf * pf)8467 static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
8468 {
8469 struct ice_tc_flower_fltr *fltr;
8470 struct hlist_node *node;
8471
8472 /* to remove all channel filters, iterate an ordered list of filters */
8473 hlist_for_each_entry_safe(fltr, node,
8474 &pf->tc_flower_fltr_list,
8475 tc_flower_node) {
8476 struct ice_rule_query_data rule;
8477 int status;
8478
8479 /* for now process only channel specific filters */
8480 if (!ice_is_chnl_fltr(fltr))
8481 continue;
8482
8483 rule.rid = fltr->rid;
8484 rule.rule_id = fltr->rule_id;
8485 rule.vsi_handle = fltr->dest_vsi_handle;
8486 status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
8487 if (status) {
8488 if (status == -ENOENT)
8489 dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
8490 rule.rule_id);
8491 else
8492 dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
8493 status);
8494 } else if (fltr->dest_vsi) {
8495 /* update advanced switch filter count */
8496 if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
8497 u32 flags = fltr->flags;
8498
8499 fltr->dest_vsi->num_chnl_fltr--;
8500 if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
8501 ICE_TC_FLWR_FIELD_ENC_DST_MAC))
8502 pf->num_dmac_chnl_fltrs--;
8503 }
8504 }
8505
8506 hlist_del(&fltr->tc_flower_node);
8507 kfree(fltr);
8508 }
8509 }
8510
8511 /**
8512 * ice_remove_q_channels - Remove queue channels for the TCs
8513 * @vsi: VSI to be configured
8514 * @rem_fltr: delete advanced switch filter or not
8515 *
8516 * Remove queue channels for the TCs
8517 */
ice_remove_q_channels(struct ice_vsi * vsi,bool rem_fltr)8518 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
8519 {
8520 struct ice_channel *ch, *ch_tmp;
8521 struct ice_pf *pf = vsi->back;
8522 int i;
8523
8524 /* remove all tc-flower based filter if they are channel filters only */
8525 if (rem_fltr)
8526 ice_rem_all_chnl_fltrs(pf);
8527
8528 /* remove ntuple filters since queue configuration is being changed */
8529 if (vsi->netdev->features & NETIF_F_NTUPLE) {
8530 struct ice_hw *hw = &pf->hw;
8531
8532 mutex_lock(&hw->fdir_fltr_lock);
8533 ice_fdir_del_all_fltrs(vsi);
8534 mutex_unlock(&hw->fdir_fltr_lock);
8535 }
8536
8537 /* perform cleanup for channels if they exist */
8538 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8539 struct ice_vsi *ch_vsi;
8540
8541 list_del(&ch->list);
8542 ch_vsi = ch->ch_vsi;
8543 if (!ch_vsi) {
8544 kfree(ch);
8545 continue;
8546 }
8547
8548 /* Reset queue contexts */
8549 for (i = 0; i < ch->num_rxq; i++) {
8550 struct ice_tx_ring *tx_ring;
8551 struct ice_rx_ring *rx_ring;
8552
8553 tx_ring = vsi->tx_rings[ch->base_q + i];
8554 rx_ring = vsi->rx_rings[ch->base_q + i];
8555 if (tx_ring) {
8556 tx_ring->ch = NULL;
8557 if (tx_ring->q_vector)
8558 tx_ring->q_vector->ch = NULL;
8559 }
8560 if (rx_ring) {
8561 rx_ring->ch = NULL;
8562 if (rx_ring->q_vector)
8563 rx_ring->q_vector->ch = NULL;
8564 }
8565 }
8566
8567 /* Release FD resources for the channel VSI */
8568 ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
8569
8570 /* clear the VSI from scheduler tree */
8571 ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
8572
8573 /* Delete VSI from FW, PF and HW VSI arrays */
8574 ice_vsi_delete(ch->ch_vsi);
8575
8576 /* free the channel */
8577 kfree(ch);
8578 }
8579
8580 /* clear the channel VSI map which is stored in main VSI */
8581 ice_for_each_chnl_tc(i)
8582 vsi->tc_map_vsi[i] = NULL;
8583
8584 /* reset main VSI's all TC information */
8585 vsi->all_enatc = 0;
8586 vsi->all_numtc = 0;
8587 }
8588
8589 /**
8590 * ice_rebuild_channels - rebuild channel
8591 * @pf: ptr to PF
8592 *
8593 * Recreate channel VSIs and replay filters
8594 */
ice_rebuild_channels(struct ice_pf * pf)8595 static int ice_rebuild_channels(struct ice_pf *pf)
8596 {
8597 struct device *dev = ice_pf_to_dev(pf);
8598 struct ice_vsi *main_vsi;
8599 bool rem_adv_fltr = true;
8600 struct ice_channel *ch;
8601 struct ice_vsi *vsi;
8602 int tc_idx = 1;
8603 int i, err;
8604
8605 main_vsi = ice_get_main_vsi(pf);
8606 if (!main_vsi)
8607 return 0;
8608
8609 if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
8610 main_vsi->old_numtc == 1)
8611 return 0; /* nothing to be done */
8612
8613 /* reconfigure main VSI based on old value of TC and cached values
8614 * for MQPRIO opts
8615 */
8616 err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
8617 if (err) {
8618 dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
8619 main_vsi->old_ena_tc, main_vsi->vsi_num);
8620 return err;
8621 }
8622
8623 /* rebuild ADQ VSIs */
8624 ice_for_each_vsi(pf, i) {
8625 enum ice_vsi_type type;
8626
8627 vsi = pf->vsi[i];
8628 if (!vsi || vsi->type != ICE_VSI_CHNL)
8629 continue;
8630
8631 type = vsi->type;
8632
8633 /* rebuild ADQ VSI */
8634 err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
8635 if (err) {
8636 dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
8637 ice_vsi_type_str(type), vsi->idx, err);
8638 goto cleanup;
8639 }
8640
8641 /* Re-map HW VSI number, using VSI handle that has been
8642 * previously validated in ice_replay_vsi() call above
8643 */
8644 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
8645
8646 /* replay filters for the VSI */
8647 err = ice_replay_vsi(&pf->hw, vsi->idx);
8648 if (err) {
8649 dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
8650 ice_vsi_type_str(type), err, vsi->idx);
8651 rem_adv_fltr = false;
8652 goto cleanup;
8653 }
8654 dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
8655 ice_vsi_type_str(type), vsi->idx);
8656
8657 /* store ADQ VSI at correct TC index in main VSI's
8658 * map of TC to VSI
8659 */
8660 main_vsi->tc_map_vsi[tc_idx++] = vsi;
8661 }
8662
8663 /* ADQ VSI(s) has been rebuilt successfully, so setup
8664 * channel for main VSI's Tx and Rx rings
8665 */
8666 list_for_each_entry(ch, &main_vsi->ch_list, list) {
8667 struct ice_vsi *ch_vsi;
8668
8669 ch_vsi = ch->ch_vsi;
8670 if (!ch_vsi)
8671 continue;
8672
8673 /* reconfig channel resources */
8674 ice_cfg_chnl_all_res(main_vsi, ch);
8675
8676 /* replay BW rate limit if it is non-zero */
8677 if (!ch->max_tx_rate && !ch->min_tx_rate)
8678 continue;
8679
8680 err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
8681 ch->min_tx_rate);
8682 if (err)
8683 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",
8684 err, ch->max_tx_rate, ch->min_tx_rate,
8685 ch_vsi->vsi_num);
8686 else
8687 dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
8688 ch->max_tx_rate, ch->min_tx_rate,
8689 ch_vsi->vsi_num);
8690 }
8691
8692 /* reconfig RSS for main VSI */
8693 if (main_vsi->ch_rss_size)
8694 ice_vsi_cfg_rss_lut_key(main_vsi);
8695
8696 return 0;
8697
8698 cleanup:
8699 ice_remove_q_channels(main_vsi, rem_adv_fltr);
8700 return err;
8701 }
8702
8703 /**
8704 * ice_create_q_channels - Add queue channel for the given TCs
8705 * @vsi: VSI to be configured
8706 *
8707 * Configures queue channel mapping to the given TCs
8708 */
ice_create_q_channels(struct ice_vsi * vsi)8709 static int ice_create_q_channels(struct ice_vsi *vsi)
8710 {
8711 struct ice_pf *pf = vsi->back;
8712 struct ice_channel *ch;
8713 int ret = 0, i;
8714
8715 ice_for_each_chnl_tc(i) {
8716 if (!(vsi->all_enatc & BIT(i)))
8717 continue;
8718
8719 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
8720 if (!ch) {
8721 ret = -ENOMEM;
8722 goto err_free;
8723 }
8724 INIT_LIST_HEAD(&ch->list);
8725 ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
8726 ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
8727 ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
8728 ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
8729 ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
8730
8731 /* convert to Kbits/s */
8732 if (ch->max_tx_rate)
8733 ch->max_tx_rate = div_u64(ch->max_tx_rate,
8734 ICE_BW_KBPS_DIVISOR);
8735 if (ch->min_tx_rate)
8736 ch->min_tx_rate = div_u64(ch->min_tx_rate,
8737 ICE_BW_KBPS_DIVISOR);
8738
8739 ret = ice_create_q_channel(vsi, ch);
8740 if (ret) {
8741 dev_err(ice_pf_to_dev(pf),
8742 "failed creating channel TC:%d\n", i);
8743 kfree(ch);
8744 goto err_free;
8745 }
8746 list_add_tail(&ch->list, &vsi->ch_list);
8747 vsi->tc_map_vsi[i] = ch->ch_vsi;
8748 dev_dbg(ice_pf_to_dev(pf),
8749 "successfully created channel: VSI %pK\n", ch->ch_vsi);
8750 }
8751 return 0;
8752
8753 err_free:
8754 ice_remove_q_channels(vsi, false);
8755
8756 return ret;
8757 }
8758
8759 /**
8760 * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
8761 * @netdev: net device to configure
8762 * @type_data: TC offload data
8763 */
ice_setup_tc_mqprio_qdisc(struct net_device * netdev,void * type_data)8764 static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
8765 {
8766 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8767 struct ice_netdev_priv *np = netdev_priv(netdev);
8768 struct ice_vsi *vsi = np->vsi;
8769 struct ice_pf *pf = vsi->back;
8770 u16 mode, ena_tc_qdisc = 0;
8771 int cur_txq, cur_rxq;
8772 u8 hw = 0, num_tcf;
8773 struct device *dev;
8774 int ret, i;
8775
8776 dev = ice_pf_to_dev(pf);
8777 num_tcf = mqprio_qopt->qopt.num_tc;
8778 hw = mqprio_qopt->qopt.hw;
8779 mode = mqprio_qopt->mode;
8780 if (!hw) {
8781 clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8782 vsi->ch_rss_size = 0;
8783 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8784 goto config_tcf;
8785 }
8786
8787 /* Generate queue region map for number of TCF requested */
8788 for (i = 0; i < num_tcf; i++)
8789 ena_tc_qdisc |= BIT(i);
8790
8791 switch (mode) {
8792 case TC_MQPRIO_MODE_CHANNEL:
8793
8794 if (pf->hw.port_info->is_custom_tx_enabled) {
8795 dev_err(dev, "Custom Tx scheduler feature enabled, can't configure ADQ\n");
8796 return -EBUSY;
8797 }
8798 ice_tear_down_devlink_rate_tree(pf);
8799
8800 ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
8801 if (ret) {
8802 netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
8803 ret);
8804 return ret;
8805 }
8806 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8807 set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8808 /* don't assume state of hw_tc_offload during driver load
8809 * and set the flag for TC flower filter if hw_tc_offload
8810 * already ON
8811 */
8812 if (vsi->netdev->features & NETIF_F_HW_TC)
8813 set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
8814 break;
8815 default:
8816 return -EINVAL;
8817 }
8818
8819 config_tcf:
8820
8821 /* Requesting same TCF configuration as already enabled */
8822 if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
8823 mode != TC_MQPRIO_MODE_CHANNEL)
8824 return 0;
8825
8826 /* Pause VSI queues */
8827 ice_dis_vsi(vsi, true);
8828
8829 if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
8830 ice_remove_q_channels(vsi, true);
8831
8832 if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8833 vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
8834 num_online_cpus());
8835 vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
8836 num_online_cpus());
8837 } else {
8838 /* logic to rebuild VSI, same like ethtool -L */
8839 u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
8840
8841 for (i = 0; i < num_tcf; i++) {
8842 if (!(ena_tc_qdisc & BIT(i)))
8843 continue;
8844
8845 offset = vsi->mqprio_qopt.qopt.offset[i];
8846 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
8847 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
8848 }
8849 vsi->req_txq = offset + qcount_tx;
8850 vsi->req_rxq = offset + qcount_rx;
8851
8852 /* store away original rss_size info, so that it gets reused
8853 * form ice_vsi_rebuild during tc-qdisc delete stage - to
8854 * determine, what should be the rss_sizefor main VSI
8855 */
8856 vsi->orig_rss_size = vsi->rss_size;
8857 }
8858
8859 /* save current values of Tx and Rx queues before calling VSI rebuild
8860 * for fallback option
8861 */
8862 cur_txq = vsi->num_txq;
8863 cur_rxq = vsi->num_rxq;
8864
8865 /* proceed with rebuild main VSI using correct number of queues */
8866 ret = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
8867 if (ret) {
8868 /* fallback to current number of queues */
8869 dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
8870 vsi->req_txq = cur_txq;
8871 vsi->req_rxq = cur_rxq;
8872 clear_bit(ICE_RESET_FAILED, pf->state);
8873 if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT)) {
8874 dev_err(dev, "Rebuild of main VSI failed again\n");
8875 return ret;
8876 }
8877 }
8878
8879 vsi->all_numtc = num_tcf;
8880 vsi->all_enatc = ena_tc_qdisc;
8881 ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
8882 if (ret) {
8883 netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
8884 vsi->vsi_num);
8885 goto exit;
8886 }
8887
8888 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8889 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8890 u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
8891
8892 /* set TC0 rate limit if specified */
8893 if (max_tx_rate || min_tx_rate) {
8894 /* convert to Kbits/s */
8895 if (max_tx_rate)
8896 max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
8897 if (min_tx_rate)
8898 min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
8899
8900 ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
8901 if (!ret) {
8902 dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
8903 max_tx_rate, min_tx_rate, vsi->vsi_num);
8904 } else {
8905 dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
8906 max_tx_rate, min_tx_rate, vsi->vsi_num);
8907 goto exit;
8908 }
8909 }
8910 ret = ice_create_q_channels(vsi);
8911 if (ret) {
8912 netdev_err(netdev, "failed configuring queue channels\n");
8913 goto exit;
8914 } else {
8915 netdev_dbg(netdev, "successfully configured channels\n");
8916 }
8917 }
8918
8919 if (vsi->ch_rss_size)
8920 ice_vsi_cfg_rss_lut_key(vsi);
8921
8922 exit:
8923 /* if error, reset the all_numtc and all_enatc */
8924 if (ret) {
8925 vsi->all_numtc = 0;
8926 vsi->all_enatc = 0;
8927 }
8928 /* resume VSI */
8929 ice_ena_vsi(vsi, true);
8930
8931 return ret;
8932 }
8933
8934 static LIST_HEAD(ice_block_cb_list);
8935
8936 static int
ice_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8937 ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8938 void *type_data)
8939 {
8940 struct ice_netdev_priv *np = netdev_priv(netdev);
8941 struct ice_pf *pf = np->vsi->back;
8942 bool locked = false;
8943 int err;
8944
8945 switch (type) {
8946 case TC_SETUP_BLOCK:
8947 return flow_block_cb_setup_simple(type_data,
8948 &ice_block_cb_list,
8949 ice_setup_tc_block_cb,
8950 np, np, true);
8951 case TC_SETUP_QDISC_MQPRIO:
8952 if (ice_is_eswitch_mode_switchdev(pf)) {
8953 netdev_err(netdev, "TC MQPRIO offload not supported, switchdev is enabled\n");
8954 return -EOPNOTSUPP;
8955 }
8956
8957 if (pf->adev) {
8958 mutex_lock(&pf->adev_mutex);
8959 device_lock(&pf->adev->dev);
8960 locked = true;
8961 if (pf->adev->dev.driver) {
8962 netdev_err(netdev, "Cannot change qdisc when RDMA is active\n");
8963 err = -EBUSY;
8964 goto adev_unlock;
8965 }
8966 }
8967
8968 /* setup traffic classifier for receive side */
8969 mutex_lock(&pf->tc_mutex);
8970 err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
8971 mutex_unlock(&pf->tc_mutex);
8972
8973 adev_unlock:
8974 if (locked) {
8975 device_unlock(&pf->adev->dev);
8976 mutex_unlock(&pf->adev_mutex);
8977 }
8978 return err;
8979 default:
8980 return -EOPNOTSUPP;
8981 }
8982 return -EOPNOTSUPP;
8983 }
8984
8985 static struct ice_indr_block_priv *
ice_indr_block_priv_lookup(struct ice_netdev_priv * np,struct net_device * netdev)8986 ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
8987 struct net_device *netdev)
8988 {
8989 struct ice_indr_block_priv *cb_priv;
8990
8991 list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
8992 if (!cb_priv->netdev)
8993 return NULL;
8994 if (cb_priv->netdev == netdev)
8995 return cb_priv;
8996 }
8997 return NULL;
8998 }
8999
9000 static int
ice_indr_setup_block_cb(enum tc_setup_type type,void * type_data,void * indr_priv)9001 ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
9002 void *indr_priv)
9003 {
9004 struct ice_indr_block_priv *priv = indr_priv;
9005 struct ice_netdev_priv *np = priv->np;
9006
9007 switch (type) {
9008 case TC_SETUP_CLSFLOWER:
9009 return ice_setup_tc_cls_flower(np, priv->netdev,
9010 (struct flow_cls_offload *)
9011 type_data);
9012 default:
9013 return -EOPNOTSUPP;
9014 }
9015 }
9016
9017 static int
ice_indr_setup_tc_block(struct net_device * netdev,struct Qdisc * sch,struct ice_netdev_priv * np,struct flow_block_offload * f,void * data,void (* cleanup)(struct flow_block_cb * block_cb))9018 ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
9019 struct ice_netdev_priv *np,
9020 struct flow_block_offload *f, void *data,
9021 void (*cleanup)(struct flow_block_cb *block_cb))
9022 {
9023 struct ice_indr_block_priv *indr_priv;
9024 struct flow_block_cb *block_cb;
9025
9026 if (!ice_is_tunnel_supported(netdev) &&
9027 !(is_vlan_dev(netdev) &&
9028 vlan_dev_real_dev(netdev) == np->vsi->netdev))
9029 return -EOPNOTSUPP;
9030
9031 if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
9032 return -EOPNOTSUPP;
9033
9034 switch (f->command) {
9035 case FLOW_BLOCK_BIND:
9036 indr_priv = ice_indr_block_priv_lookup(np, netdev);
9037 if (indr_priv)
9038 return -EEXIST;
9039
9040 indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
9041 if (!indr_priv)
9042 return -ENOMEM;
9043
9044 indr_priv->netdev = netdev;
9045 indr_priv->np = np;
9046 list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
9047
9048 block_cb =
9049 flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
9050 indr_priv, indr_priv,
9051 ice_rep_indr_tc_block_unbind,
9052 f, netdev, sch, data, np,
9053 cleanup);
9054
9055 if (IS_ERR(block_cb)) {
9056 list_del(&indr_priv->list);
9057 kfree(indr_priv);
9058 return PTR_ERR(block_cb);
9059 }
9060 flow_block_cb_add(block_cb, f);
9061 list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
9062 break;
9063 case FLOW_BLOCK_UNBIND:
9064 indr_priv = ice_indr_block_priv_lookup(np, netdev);
9065 if (!indr_priv)
9066 return -ENOENT;
9067
9068 block_cb = flow_block_cb_lookup(f->block,
9069 ice_indr_setup_block_cb,
9070 indr_priv);
9071 if (!block_cb)
9072 return -ENOENT;
9073
9074 flow_indr_block_cb_remove(block_cb, f);
9075
9076 list_del(&block_cb->driver_list);
9077 break;
9078 default:
9079 return -EOPNOTSUPP;
9080 }
9081 return 0;
9082 }
9083
9084 static int
ice_indr_setup_tc_cb(struct net_device * netdev,struct Qdisc * sch,void * cb_priv,enum tc_setup_type type,void * type_data,void * data,void (* cleanup)(struct flow_block_cb * block_cb))9085 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
9086 void *cb_priv, enum tc_setup_type type, void *type_data,
9087 void *data,
9088 void (*cleanup)(struct flow_block_cb *block_cb))
9089 {
9090 switch (type) {
9091 case TC_SETUP_BLOCK:
9092 return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
9093 data, cleanup);
9094
9095 default:
9096 return -EOPNOTSUPP;
9097 }
9098 }
9099
9100 /**
9101 * ice_open - Called when a network interface becomes active
9102 * @netdev: network interface device structure
9103 *
9104 * The open entry point is called when a network interface is made
9105 * active by the system (IFF_UP). At this point all resources needed
9106 * for transmit and receive operations are allocated, the interrupt
9107 * handler is registered with the OS, the netdev watchdog is enabled,
9108 * and the stack is notified that the interface is ready.
9109 *
9110 * Returns 0 on success, negative value on failure
9111 */
ice_open(struct net_device * netdev)9112 int ice_open(struct net_device *netdev)
9113 {
9114 struct ice_netdev_priv *np = netdev_priv(netdev);
9115 struct ice_pf *pf = np->vsi->back;
9116
9117 if (ice_is_reset_in_progress(pf->state)) {
9118 netdev_err(netdev, "can't open net device while reset is in progress");
9119 return -EBUSY;
9120 }
9121
9122 return ice_open_internal(netdev);
9123 }
9124
9125 /**
9126 * ice_open_internal - Called when a network interface becomes active
9127 * @netdev: network interface device structure
9128 *
9129 * Internal ice_open implementation. Should not be used directly except for ice_open and reset
9130 * handling routine
9131 *
9132 * Returns 0 on success, negative value on failure
9133 */
ice_open_internal(struct net_device * netdev)9134 int ice_open_internal(struct net_device *netdev)
9135 {
9136 struct ice_netdev_priv *np = netdev_priv(netdev);
9137 struct ice_vsi *vsi = np->vsi;
9138 struct ice_pf *pf = vsi->back;
9139 struct ice_port_info *pi;
9140 int err;
9141
9142 if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
9143 netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
9144 return -EIO;
9145 }
9146
9147 netif_carrier_off(netdev);
9148
9149 pi = vsi->port_info;
9150 err = ice_update_link_info(pi);
9151 if (err) {
9152 netdev_err(netdev, "Failed to get link info, error %d\n", err);
9153 return err;
9154 }
9155
9156 ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
9157
9158 /* Set PHY if there is media, otherwise, turn off PHY */
9159 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
9160 clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9161 if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
9162 err = ice_init_phy_user_cfg(pi);
9163 if (err) {
9164 netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
9165 err);
9166 return err;
9167 }
9168 }
9169
9170 err = ice_configure_phy(vsi);
9171 if (err) {
9172 netdev_err(netdev, "Failed to set physical link up, error %d\n",
9173 err);
9174 return err;
9175 }
9176 } else {
9177 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9178 ice_set_link(vsi, false);
9179 }
9180
9181 err = ice_vsi_open(vsi);
9182 if (err)
9183 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
9184 vsi->vsi_num, vsi->vsw->sw_id);
9185
9186 /* Update existing tunnels information */
9187 udp_tunnel_get_rx_info(netdev);
9188
9189 return err;
9190 }
9191
9192 /**
9193 * ice_stop - Disables a network interface
9194 * @netdev: network interface device structure
9195 *
9196 * The stop entry point is called when an interface is de-activated by the OS,
9197 * and the netdevice enters the DOWN state. The hardware is still under the
9198 * driver's control, but the netdev interface is disabled.
9199 *
9200 * Returns success only - not allowed to fail
9201 */
ice_stop(struct net_device * netdev)9202 int ice_stop(struct net_device *netdev)
9203 {
9204 struct ice_netdev_priv *np = netdev_priv(netdev);
9205 struct ice_vsi *vsi = np->vsi;
9206 struct ice_pf *pf = vsi->back;
9207
9208 if (ice_is_reset_in_progress(pf->state)) {
9209 netdev_err(netdev, "can't stop net device while reset is in progress");
9210 return -EBUSY;
9211 }
9212
9213 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
9214 int link_err = ice_force_phys_link_state(vsi, false);
9215
9216 if (link_err) {
9217 if (link_err == -ENOMEDIUM)
9218 netdev_info(vsi->netdev, "Skipping link reconfig - no media attached, VSI %d\n",
9219 vsi->vsi_num);
9220 else
9221 netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
9222 vsi->vsi_num, link_err);
9223
9224 ice_vsi_close(vsi);
9225 return -EIO;
9226 }
9227 }
9228
9229 ice_vsi_close(vsi);
9230
9231 return 0;
9232 }
9233
9234 /**
9235 * ice_features_check - Validate encapsulated packet conforms to limits
9236 * @skb: skb buffer
9237 * @netdev: This port's netdev
9238 * @features: Offload features that the stack believes apply
9239 */
9240 static netdev_features_t
ice_features_check(struct sk_buff * skb,struct net_device __always_unused * netdev,netdev_features_t features)9241 ice_features_check(struct sk_buff *skb,
9242 struct net_device __always_unused *netdev,
9243 netdev_features_t features)
9244 {
9245 bool gso = skb_is_gso(skb);
9246 size_t len;
9247
9248 /* No point in doing any of this if neither checksum nor GSO are
9249 * being requested for this frame. We can rule out both by just
9250 * checking for CHECKSUM_PARTIAL
9251 */
9252 if (skb->ip_summed != CHECKSUM_PARTIAL)
9253 return features;
9254
9255 /* We cannot support GSO if the MSS is going to be less than
9256 * 64 bytes. If it is then we need to drop support for GSO.
9257 */
9258 if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
9259 features &= ~NETIF_F_GSO_MASK;
9260
9261 len = skb_network_offset(skb);
9262 if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
9263 goto out_rm_features;
9264
9265 len = skb_network_header_len(skb);
9266 if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9267 goto out_rm_features;
9268
9269 if (skb->encapsulation) {
9270 /* this must work for VXLAN frames AND IPIP/SIT frames, and in
9271 * the case of IPIP frames, the transport header pointer is
9272 * after the inner header! So check to make sure that this
9273 * is a GRE or UDP_TUNNEL frame before doing that math.
9274 */
9275 if (gso && (skb_shinfo(skb)->gso_type &
9276 (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
9277 len = skb_inner_network_header(skb) -
9278 skb_transport_header(skb);
9279 if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
9280 goto out_rm_features;
9281 }
9282
9283 len = skb_inner_network_header_len(skb);
9284 if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9285 goto out_rm_features;
9286 }
9287
9288 return features;
9289 out_rm_features:
9290 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
9291 }
9292
9293 static const struct net_device_ops ice_netdev_safe_mode_ops = {
9294 .ndo_open = ice_open,
9295 .ndo_stop = ice_stop,
9296 .ndo_start_xmit = ice_start_xmit,
9297 .ndo_set_mac_address = ice_set_mac_address,
9298 .ndo_validate_addr = eth_validate_addr,
9299 .ndo_change_mtu = ice_change_mtu,
9300 .ndo_get_stats64 = ice_get_stats64,
9301 .ndo_tx_timeout = ice_tx_timeout,
9302 .ndo_bpf = ice_xdp_safe_mode,
9303 };
9304
9305 static const struct net_device_ops ice_netdev_ops = {
9306 .ndo_open = ice_open,
9307 .ndo_stop = ice_stop,
9308 .ndo_start_xmit = ice_start_xmit,
9309 .ndo_select_queue = ice_select_queue,
9310 .ndo_features_check = ice_features_check,
9311 .ndo_fix_features = ice_fix_features,
9312 .ndo_set_rx_mode = ice_set_rx_mode,
9313 .ndo_set_mac_address = ice_set_mac_address,
9314 .ndo_validate_addr = eth_validate_addr,
9315 .ndo_change_mtu = ice_change_mtu,
9316 .ndo_get_stats64 = ice_get_stats64,
9317 .ndo_set_tx_maxrate = ice_set_tx_maxrate,
9318 .ndo_eth_ioctl = ice_eth_ioctl,
9319 .ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
9320 .ndo_set_vf_mac = ice_set_vf_mac,
9321 .ndo_get_vf_config = ice_get_vf_cfg,
9322 .ndo_set_vf_trust = ice_set_vf_trust,
9323 .ndo_set_vf_vlan = ice_set_vf_port_vlan,
9324 .ndo_set_vf_link_state = ice_set_vf_link_state,
9325 .ndo_get_vf_stats = ice_get_vf_stats,
9326 .ndo_set_vf_rate = ice_set_vf_bw,
9327 .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
9328 .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
9329 .ndo_setup_tc = ice_setup_tc,
9330 .ndo_set_features = ice_set_features,
9331 .ndo_bridge_getlink = ice_bridge_getlink,
9332 .ndo_bridge_setlink = ice_bridge_setlink,
9333 .ndo_fdb_add = ice_fdb_add,
9334 .ndo_fdb_del = ice_fdb_del,
9335 #ifdef CONFIG_RFS_ACCEL
9336 .ndo_rx_flow_steer = ice_rx_flow_steer,
9337 #endif
9338 .ndo_tx_timeout = ice_tx_timeout,
9339 .ndo_bpf = ice_xdp,
9340 .ndo_xdp_xmit = ice_xdp_xmit,
9341 .ndo_xsk_wakeup = ice_xsk_wakeup,
9342 };
9343