Loading...
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 "devlink/devlink.h"
17#include "devlink/devlink_port.h"
18#include "ice_sf_eth.h"
19#include "ice_hwmon.h"
20/* Including ice_trace.h with CREATE_TRACE_POINTS defined will generate the
21 * ice tracepoint functions. This must be done exactly once across the
22 * ice driver.
23 */
24#define CREATE_TRACE_POINTS
25#include "ice_trace.h"
26#include "ice_eswitch.h"
27#include "ice_tc_lib.h"
28#include "ice_vsi_vlan_ops.h"
29#include <net/xdp_sock_drv.h>
30
31#define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
32static const char ice_driver_string[] = DRV_SUMMARY;
33static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
34
35/* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
36#define ICE_DDP_PKG_PATH "intel/ice/ddp/"
37#define ICE_DDP_PKG_FILE ICE_DDP_PKG_PATH "ice.pkg"
38
39MODULE_DESCRIPTION(DRV_SUMMARY);
40MODULE_IMPORT_NS("LIBIE");
41MODULE_LICENSE("GPL v2");
42MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
43
44static int debug = -1;
45module_param(debug, int, 0644);
46#ifndef CONFIG_DYNAMIC_DEBUG
47MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
48#else
49MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
50#endif /* !CONFIG_DYNAMIC_DEBUG */
51
52DEFINE_STATIC_KEY_FALSE(ice_xdp_locking_key);
53EXPORT_SYMBOL(ice_xdp_locking_key);
54
55/**
56 * ice_hw_to_dev - Get device pointer from the hardware structure
57 * @hw: pointer to the device HW structure
58 *
59 * Used to access the device pointer from compilation units which can't easily
60 * include the definition of struct ice_pf without leading to circular header
61 * dependencies.
62 */
63struct device *ice_hw_to_dev(struct ice_hw *hw)
64{
65 struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
66
67 return &pf->pdev->dev;
68}
69
70static struct workqueue_struct *ice_wq;
71struct workqueue_struct *ice_lag_wq;
72static const struct net_device_ops ice_netdev_safe_mode_ops;
73static const struct net_device_ops ice_netdev_ops;
74
75static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
76
77static void ice_vsi_release_all(struct ice_pf *pf);
78
79static int ice_rebuild_channels(struct ice_pf *pf);
80static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_adv_fltr);
81
82static int
83ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
84 void *cb_priv, enum tc_setup_type type, void *type_data,
85 void *data,
86 void (*cleanup)(struct flow_block_cb *block_cb));
87
88bool netif_is_ice(const struct net_device *dev)
89{
90 return dev && (dev->netdev_ops == &ice_netdev_ops ||
91 dev->netdev_ops == &ice_netdev_safe_mode_ops);
92}
93
94/**
95 * ice_get_tx_pending - returns number of Tx descriptors not processed
96 * @ring: the ring of descriptors
97 */
98static u16 ice_get_tx_pending(struct ice_tx_ring *ring)
99{
100 u16 head, tail;
101
102 head = ring->next_to_clean;
103 tail = ring->next_to_use;
104
105 if (head != tail)
106 return (head < tail) ?
107 tail - head : (tail + ring->count - head);
108 return 0;
109}
110
111/**
112 * ice_check_for_hang_subtask - check for and recover hung queues
113 * @pf: pointer to PF struct
114 */
115static void ice_check_for_hang_subtask(struct ice_pf *pf)
116{
117 struct ice_vsi *vsi = NULL;
118 struct ice_hw *hw;
119 unsigned int i;
120 int packets;
121 u32 v;
122
123 ice_for_each_vsi(pf, v)
124 if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
125 vsi = pf->vsi[v];
126 break;
127 }
128
129 if (!vsi || test_bit(ICE_VSI_DOWN, vsi->state))
130 return;
131
132 if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
133 return;
134
135 hw = &vsi->back->hw;
136
137 ice_for_each_txq(vsi, i) {
138 struct ice_tx_ring *tx_ring = vsi->tx_rings[i];
139 struct ice_ring_stats *ring_stats;
140
141 if (!tx_ring)
142 continue;
143 if (ice_ring_ch_enabled(tx_ring))
144 continue;
145
146 ring_stats = tx_ring->ring_stats;
147 if (!ring_stats)
148 continue;
149
150 if (tx_ring->desc) {
151 /* If packet counter has not changed the queue is
152 * likely stalled, so force an interrupt for this
153 * queue.
154 *
155 * prev_pkt would be negative if there was no
156 * pending work.
157 */
158 packets = ring_stats->stats.pkts & INT_MAX;
159 if (ring_stats->tx_stats.prev_pkt == packets) {
160 /* Trigger sw interrupt to revive the queue */
161 ice_trigger_sw_intr(hw, tx_ring->q_vector);
162 continue;
163 }
164
165 /* Memory barrier between read of packet count and call
166 * to ice_get_tx_pending()
167 */
168 smp_rmb();
169 ring_stats->tx_stats.prev_pkt =
170 ice_get_tx_pending(tx_ring) ? packets : -1;
171 }
172 }
173}
174
175/**
176 * ice_init_mac_fltr - Set initial MAC filters
177 * @pf: board private structure
178 *
179 * Set initial set of MAC filters for PF VSI; configure filters for permanent
180 * address and broadcast address. If an error is encountered, netdevice will be
181 * unregistered.
182 */
183static int ice_init_mac_fltr(struct ice_pf *pf)
184{
185 struct ice_vsi *vsi;
186 u8 *perm_addr;
187
188 vsi = ice_get_main_vsi(pf);
189 if (!vsi)
190 return -EINVAL;
191
192 perm_addr = vsi->port_info->mac.perm_addr;
193 return ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
194}
195
196/**
197 * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
198 * @netdev: the net device on which the sync is happening
199 * @addr: MAC address to sync
200 *
201 * This is a callback function which is called by the in kernel device sync
202 * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
203 * populates the tmp_sync_list, which is later used by ice_add_mac to add the
204 * MAC filters from the hardware.
205 */
206static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
207{
208 struct ice_netdev_priv *np = netdev_priv(netdev);
209 struct ice_vsi *vsi = np->vsi;
210
211 if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
212 ICE_FWD_TO_VSI))
213 return -EINVAL;
214
215 return 0;
216}
217
218/**
219 * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
220 * @netdev: the net device on which the unsync is happening
221 * @addr: MAC address to unsync
222 *
223 * This is a callback function which is called by the in kernel device unsync
224 * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
225 * populates the tmp_unsync_list, which is later used by ice_remove_mac to
226 * delete the MAC filters from the hardware.
227 */
228static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
229{
230 struct ice_netdev_priv *np = netdev_priv(netdev);
231 struct ice_vsi *vsi = np->vsi;
232
233 /* Under some circumstances, we might receive a request to delete our
234 * own device address from our uc list. Because we store the device
235 * address in the VSI's MAC filter list, we need to ignore such
236 * requests and not delete our device address from this list.
237 */
238 if (ether_addr_equal(addr, netdev->dev_addr))
239 return 0;
240
241 if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
242 ICE_FWD_TO_VSI))
243 return -EINVAL;
244
245 return 0;
246}
247
248/**
249 * ice_vsi_fltr_changed - check if filter state changed
250 * @vsi: VSI to be checked
251 *
252 * returns true if filter state has changed, false otherwise.
253 */
254static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
255{
256 return test_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state) ||
257 test_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
258}
259
260/**
261 * ice_set_promisc - Enable promiscuous mode for a given PF
262 * @vsi: the VSI being configured
263 * @promisc_m: mask of promiscuous config bits
264 *
265 */
266static int ice_set_promisc(struct ice_vsi *vsi, u8 promisc_m)
267{
268 int status;
269
270 if (vsi->type != ICE_VSI_PF)
271 return 0;
272
273 if (ice_vsi_has_non_zero_vlans(vsi)) {
274 promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
275 status = ice_fltr_set_vlan_vsi_promisc(&vsi->back->hw, vsi,
276 promisc_m);
277 } else {
278 status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
279 promisc_m, 0);
280 }
281 if (status && status != -EEXIST)
282 return status;
283
284 netdev_dbg(vsi->netdev, "set promisc filter bits for VSI %i: 0x%x\n",
285 vsi->vsi_num, promisc_m);
286 return 0;
287}
288
289/**
290 * ice_clear_promisc - Disable promiscuous mode for a given PF
291 * @vsi: the VSI being configured
292 * @promisc_m: mask of promiscuous config bits
293 *
294 */
295static int ice_clear_promisc(struct ice_vsi *vsi, u8 promisc_m)
296{
297 int status;
298
299 if (vsi->type != ICE_VSI_PF)
300 return 0;
301
302 if (ice_vsi_has_non_zero_vlans(vsi)) {
303 promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
304 status = ice_fltr_clear_vlan_vsi_promisc(&vsi->back->hw, vsi,
305 promisc_m);
306 } else {
307 status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
308 promisc_m, 0);
309 }
310
311 netdev_dbg(vsi->netdev, "clear promisc filter bits for VSI %i: 0x%x\n",
312 vsi->vsi_num, promisc_m);
313 return status;
314}
315
316/**
317 * ice_vsi_sync_fltr - Update the VSI filter list to the HW
318 * @vsi: ptr to the VSI
319 *
320 * Push any outstanding VSI filter changes through the AdminQ.
321 */
322static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
323{
324 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
325 struct device *dev = ice_pf_to_dev(vsi->back);
326 struct net_device *netdev = vsi->netdev;
327 bool promisc_forced_on = false;
328 struct ice_pf *pf = vsi->back;
329 struct ice_hw *hw = &pf->hw;
330 u32 changed_flags = 0;
331 int err;
332
333 if (!vsi->netdev)
334 return -EINVAL;
335
336 while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
337 usleep_range(1000, 2000);
338
339 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
340 vsi->current_netdev_flags = vsi->netdev->flags;
341
342 INIT_LIST_HEAD(&vsi->tmp_sync_list);
343 INIT_LIST_HEAD(&vsi->tmp_unsync_list);
344
345 if (ice_vsi_fltr_changed(vsi)) {
346 clear_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
347 clear_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
348
349 /* grab the netdev's addr_list_lock */
350 netif_addr_lock_bh(netdev);
351 __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
352 ice_add_mac_to_unsync_list);
353 __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
354 ice_add_mac_to_unsync_list);
355 /* our temp lists are populated. release lock */
356 netif_addr_unlock_bh(netdev);
357 }
358
359 /* Remove MAC addresses in the unsync list */
360 err = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
361 ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
362 if (err) {
363 netdev_err(netdev, "Failed to delete MAC filters\n");
364 /* if we failed because of alloc failures, just bail */
365 if (err == -ENOMEM)
366 goto out;
367 }
368
369 /* Add MAC addresses in the sync list */
370 err = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
371 ice_fltr_free_list(dev, &vsi->tmp_sync_list);
372 /* If filter is added successfully or already exists, do not go into
373 * 'if' condition and report it as error. Instead continue processing
374 * rest of the function.
375 */
376 if (err && err != -EEXIST) {
377 netdev_err(netdev, "Failed to add MAC filters\n");
378 /* If there is no more space for new umac filters, VSI
379 * should go into promiscuous mode. There should be some
380 * space reserved for promiscuous filters.
381 */
382 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
383 !test_and_set_bit(ICE_FLTR_OVERFLOW_PROMISC,
384 vsi->state)) {
385 promisc_forced_on = true;
386 netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
387 vsi->vsi_num);
388 } else {
389 goto out;
390 }
391 }
392 err = 0;
393 /* check for changes in promiscuous modes */
394 if (changed_flags & IFF_ALLMULTI) {
395 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
396 err = ice_set_promisc(vsi, ICE_MCAST_PROMISC_BITS);
397 if (err) {
398 vsi->current_netdev_flags &= ~IFF_ALLMULTI;
399 goto out_promisc;
400 }
401 } else {
402 /* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
403 err = ice_clear_promisc(vsi, ICE_MCAST_PROMISC_BITS);
404 if (err) {
405 vsi->current_netdev_flags |= IFF_ALLMULTI;
406 goto out_promisc;
407 }
408 }
409 }
410
411 if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
412 test_bit(ICE_VSI_PROMISC_CHANGED, vsi->state)) {
413 clear_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
414 if (vsi->current_netdev_flags & IFF_PROMISC) {
415 /* Apply Rx filter rule to get traffic from wire */
416 if (!ice_is_dflt_vsi_in_use(vsi->port_info)) {
417 err = ice_set_dflt_vsi(vsi);
418 if (err && err != -EEXIST) {
419 netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
420 err, vsi->vsi_num);
421 vsi->current_netdev_flags &=
422 ~IFF_PROMISC;
423 goto out_promisc;
424 }
425 err = 0;
426 vlan_ops->dis_rx_filtering(vsi);
427
428 /* promiscuous mode implies allmulticast so
429 * that VSIs that are in promiscuous mode are
430 * subscribed to multicast packets coming to
431 * the port
432 */
433 err = ice_set_promisc(vsi,
434 ICE_MCAST_PROMISC_BITS);
435 if (err)
436 goto out_promisc;
437 }
438 } else {
439 /* Clear Rx filter to remove traffic from wire */
440 if (ice_is_vsi_dflt_vsi(vsi)) {
441 err = ice_clear_dflt_vsi(vsi);
442 if (err) {
443 netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
444 err, vsi->vsi_num);
445 vsi->current_netdev_flags |=
446 IFF_PROMISC;
447 goto out_promisc;
448 }
449 if (vsi->netdev->features &
450 NETIF_F_HW_VLAN_CTAG_FILTER)
451 vlan_ops->ena_rx_filtering(vsi);
452 }
453
454 /* disable allmulti here, but only if allmulti is not
455 * still enabled for the netdev
456 */
457 if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) {
458 err = ice_clear_promisc(vsi,
459 ICE_MCAST_PROMISC_BITS);
460 if (err) {
461 netdev_err(netdev, "Error %d clearing multicast promiscuous on VSI %i\n",
462 err, vsi->vsi_num);
463 }
464 }
465 }
466 }
467 goto exit;
468
469out_promisc:
470 set_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
471 goto exit;
472out:
473 /* if something went wrong then set the changed flag so we try again */
474 set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
475 set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
476exit:
477 clear_bit(ICE_CFG_BUSY, vsi->state);
478 return err;
479}
480
481/**
482 * ice_sync_fltr_subtask - Sync the VSI filter list with HW
483 * @pf: board private structure
484 */
485static void ice_sync_fltr_subtask(struct ice_pf *pf)
486{
487 int v;
488
489 if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
490 return;
491
492 clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
493
494 ice_for_each_vsi(pf, v)
495 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
496 ice_vsi_sync_fltr(pf->vsi[v])) {
497 /* come back and try again later */
498 set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
499 break;
500 }
501}
502
503/**
504 * ice_pf_dis_all_vsi - Pause all VSIs on a PF
505 * @pf: the PF
506 * @locked: is the rtnl_lock already held
507 */
508static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
509{
510 int node;
511 int v;
512
513 ice_for_each_vsi(pf, v)
514 if (pf->vsi[v])
515 ice_dis_vsi(pf->vsi[v], locked);
516
517 for (node = 0; node < ICE_MAX_PF_AGG_NODES; node++)
518 pf->pf_agg_node[node].num_vsis = 0;
519
520 for (node = 0; node < ICE_MAX_VF_AGG_NODES; node++)
521 pf->vf_agg_node[node].num_vsis = 0;
522}
523
524/**
525 * ice_prepare_for_reset - prep for reset
526 * @pf: board private structure
527 * @reset_type: reset type requested
528 *
529 * Inform or close all dependent features in prep for reset.
530 */
531static void
532ice_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
533{
534 struct ice_hw *hw = &pf->hw;
535 struct ice_vsi *vsi;
536 struct ice_vf *vf;
537 unsigned int bkt;
538
539 dev_dbg(ice_pf_to_dev(pf), "reset_type=%d\n", reset_type);
540
541 /* already prepared for reset */
542 if (test_bit(ICE_PREPARED_FOR_RESET, pf->state))
543 return;
544
545 synchronize_irq(pf->oicr_irq.virq);
546
547 ice_unplug_aux_dev(pf);
548
549 /* Notify VFs of impending reset */
550 if (ice_check_sq_alive(hw, &hw->mailboxq))
551 ice_vc_notify_reset(pf);
552
553 /* Disable VFs until reset is completed */
554 mutex_lock(&pf->vfs.table_lock);
555 ice_for_each_vf(pf, bkt, vf)
556 ice_set_vf_state_dis(vf);
557 mutex_unlock(&pf->vfs.table_lock);
558
559 if (ice_is_eswitch_mode_switchdev(pf)) {
560 rtnl_lock();
561 ice_eswitch_br_fdb_flush(pf->eswitch.br_offloads->bridge);
562 rtnl_unlock();
563 }
564
565 /* release ADQ specific HW and SW resources */
566 vsi = ice_get_main_vsi(pf);
567 if (!vsi)
568 goto skip;
569
570 /* to be on safe side, reset orig_rss_size so that normal flow
571 * of deciding rss_size can take precedence
572 */
573 vsi->orig_rss_size = 0;
574
575 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
576 if (reset_type == ICE_RESET_PFR) {
577 vsi->old_ena_tc = vsi->all_enatc;
578 vsi->old_numtc = vsi->all_numtc;
579 } else {
580 ice_remove_q_channels(vsi, true);
581
582 /* for other reset type, do not support channel rebuild
583 * hence reset needed info
584 */
585 vsi->old_ena_tc = 0;
586 vsi->all_enatc = 0;
587 vsi->old_numtc = 0;
588 vsi->all_numtc = 0;
589 vsi->req_txq = 0;
590 vsi->req_rxq = 0;
591 clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
592 memset(&vsi->mqprio_qopt, 0, sizeof(vsi->mqprio_qopt));
593 }
594 }
595
596 if (vsi->netdev)
597 netif_device_detach(vsi->netdev);
598skip:
599
600 /* clear SW filtering DB */
601 ice_clear_hw_tbls(hw);
602 /* disable the VSIs and their queues that are not already DOWN */
603 set_bit(ICE_VSI_REBUILD_PENDING, ice_get_main_vsi(pf)->state);
604 ice_pf_dis_all_vsi(pf, false);
605
606 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
607 ice_ptp_prepare_for_reset(pf, reset_type);
608
609 if (ice_is_feature_supported(pf, ICE_F_GNSS))
610 ice_gnss_exit(pf);
611
612 if (hw->port_info)
613 ice_sched_clear_port(hw->port_info);
614
615 ice_shutdown_all_ctrlq(hw, false);
616
617 set_bit(ICE_PREPARED_FOR_RESET, pf->state);
618}
619
620/**
621 * ice_do_reset - Initiate one of many types of resets
622 * @pf: board private structure
623 * @reset_type: reset type requested before this function was called.
624 */
625static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
626{
627 struct device *dev = ice_pf_to_dev(pf);
628 struct ice_hw *hw = &pf->hw;
629
630 dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
631
632 if (pf->lag && pf->lag->bonded && reset_type == ICE_RESET_PFR) {
633 dev_dbg(dev, "PFR on a bonded interface, promoting to CORER\n");
634 reset_type = ICE_RESET_CORER;
635 }
636
637 ice_prepare_for_reset(pf, reset_type);
638
639 /* trigger the reset */
640 if (ice_reset(hw, reset_type)) {
641 dev_err(dev, "reset %d failed\n", reset_type);
642 set_bit(ICE_RESET_FAILED, pf->state);
643 clear_bit(ICE_RESET_OICR_RECV, pf->state);
644 clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
645 clear_bit(ICE_PFR_REQ, pf->state);
646 clear_bit(ICE_CORER_REQ, pf->state);
647 clear_bit(ICE_GLOBR_REQ, pf->state);
648 wake_up(&pf->reset_wait_queue);
649 return;
650 }
651
652 /* PFR is a bit of a special case because it doesn't result in an OICR
653 * interrupt. So for PFR, rebuild after the reset and clear the reset-
654 * associated state bits.
655 */
656 if (reset_type == ICE_RESET_PFR) {
657 pf->pfr_count++;
658 ice_rebuild(pf, reset_type);
659 clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
660 clear_bit(ICE_PFR_REQ, pf->state);
661 wake_up(&pf->reset_wait_queue);
662 ice_reset_all_vfs(pf);
663 }
664}
665
666/**
667 * ice_reset_subtask - Set up for resetting the device and driver
668 * @pf: board private structure
669 */
670static void ice_reset_subtask(struct ice_pf *pf)
671{
672 enum ice_reset_req reset_type = ICE_RESET_INVAL;
673
674 /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
675 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
676 * of reset is pending and sets bits in pf->state indicating the reset
677 * type and ICE_RESET_OICR_RECV. So, if the latter bit is set
678 * prepare for pending reset if not already (for PF software-initiated
679 * global resets the software should already be prepared for it as
680 * indicated by ICE_PREPARED_FOR_RESET; for global resets initiated
681 * by firmware or software on other PFs, that bit is not set so prepare
682 * for the reset now), poll for reset done, rebuild and return.
683 */
684 if (test_bit(ICE_RESET_OICR_RECV, pf->state)) {
685 /* Perform the largest reset requested */
686 if (test_and_clear_bit(ICE_CORER_RECV, pf->state))
687 reset_type = ICE_RESET_CORER;
688 if (test_and_clear_bit(ICE_GLOBR_RECV, pf->state))
689 reset_type = ICE_RESET_GLOBR;
690 if (test_and_clear_bit(ICE_EMPR_RECV, pf->state))
691 reset_type = ICE_RESET_EMPR;
692 /* return if no valid reset type requested */
693 if (reset_type == ICE_RESET_INVAL)
694 return;
695 ice_prepare_for_reset(pf, reset_type);
696
697 /* make sure we are ready to rebuild */
698 if (ice_check_reset(&pf->hw)) {
699 set_bit(ICE_RESET_FAILED, pf->state);
700 } else {
701 /* done with reset. start rebuild */
702 pf->hw.reset_ongoing = false;
703 ice_rebuild(pf, reset_type);
704 /* clear bit to resume normal operations, but
705 * ICE_NEEDS_RESTART bit is set in case rebuild failed
706 */
707 clear_bit(ICE_RESET_OICR_RECV, pf->state);
708 clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
709 clear_bit(ICE_PFR_REQ, pf->state);
710 clear_bit(ICE_CORER_REQ, pf->state);
711 clear_bit(ICE_GLOBR_REQ, pf->state);
712 wake_up(&pf->reset_wait_queue);
713 ice_reset_all_vfs(pf);
714 }
715
716 return;
717 }
718
719 /* No pending resets to finish processing. Check for new resets */
720 if (test_bit(ICE_PFR_REQ, pf->state)) {
721 reset_type = ICE_RESET_PFR;
722 if (pf->lag && pf->lag->bonded) {
723 dev_dbg(ice_pf_to_dev(pf), "PFR on a bonded interface, promoting to CORER\n");
724 reset_type = ICE_RESET_CORER;
725 }
726 }
727 if (test_bit(ICE_CORER_REQ, pf->state))
728 reset_type = ICE_RESET_CORER;
729 if (test_bit(ICE_GLOBR_REQ, pf->state))
730 reset_type = ICE_RESET_GLOBR;
731 /* If no valid reset type requested just return */
732 if (reset_type == ICE_RESET_INVAL)
733 return;
734
735 /* reset if not already down or busy */
736 if (!test_bit(ICE_DOWN, pf->state) &&
737 !test_bit(ICE_CFG_BUSY, pf->state)) {
738 ice_do_reset(pf, reset_type);
739 }
740}
741
742/**
743 * ice_print_topo_conflict - print topology conflict message
744 * @vsi: the VSI whose topology status is being checked
745 */
746static void ice_print_topo_conflict(struct ice_vsi *vsi)
747{
748 switch (vsi->port_info->phy.link_info.topo_media_conflict) {
749 case ICE_AQ_LINK_TOPO_CONFLICT:
750 case ICE_AQ_LINK_MEDIA_CONFLICT:
751 case ICE_AQ_LINK_TOPO_UNREACH_PRT:
752 case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
753 case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
754 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");
755 break;
756 case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
757 if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, vsi->back->flags))
758 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");
759 else
760 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");
761 break;
762 default:
763 break;
764 }
765}
766
767/**
768 * ice_print_link_msg - print link up or down message
769 * @vsi: the VSI whose link status is being queried
770 * @isup: boolean for if the link is now up or down
771 */
772void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
773{
774 struct ice_aqc_get_phy_caps_data *caps;
775 const char *an_advertised;
776 const char *fec_req;
777 const char *speed;
778 const char *fec;
779 const char *fc;
780 const char *an;
781 int status;
782
783 if (!vsi)
784 return;
785
786 if (vsi->current_isup == isup)
787 return;
788
789 vsi->current_isup = isup;
790
791 if (!isup) {
792 netdev_info(vsi->netdev, "NIC Link is Down\n");
793 return;
794 }
795
796 switch (vsi->port_info->phy.link_info.link_speed) {
797 case ICE_AQ_LINK_SPEED_200GB:
798 speed = "200 G";
799 break;
800 case ICE_AQ_LINK_SPEED_100GB:
801 speed = "100 G";
802 break;
803 case ICE_AQ_LINK_SPEED_50GB:
804 speed = "50 G";
805 break;
806 case ICE_AQ_LINK_SPEED_40GB:
807 speed = "40 G";
808 break;
809 case ICE_AQ_LINK_SPEED_25GB:
810 speed = "25 G";
811 break;
812 case ICE_AQ_LINK_SPEED_20GB:
813 speed = "20 G";
814 break;
815 case ICE_AQ_LINK_SPEED_10GB:
816 speed = "10 G";
817 break;
818 case ICE_AQ_LINK_SPEED_5GB:
819 speed = "5 G";
820 break;
821 case ICE_AQ_LINK_SPEED_2500MB:
822 speed = "2.5 G";
823 break;
824 case ICE_AQ_LINK_SPEED_1000MB:
825 speed = "1 G";
826 break;
827 case ICE_AQ_LINK_SPEED_100MB:
828 speed = "100 M";
829 break;
830 default:
831 speed = "Unknown ";
832 break;
833 }
834
835 switch (vsi->port_info->fc.current_mode) {
836 case ICE_FC_FULL:
837 fc = "Rx/Tx";
838 break;
839 case ICE_FC_TX_PAUSE:
840 fc = "Tx";
841 break;
842 case ICE_FC_RX_PAUSE:
843 fc = "Rx";
844 break;
845 case ICE_FC_NONE:
846 fc = "None";
847 break;
848 default:
849 fc = "Unknown";
850 break;
851 }
852
853 /* Get FEC mode based on negotiated link info */
854 switch (vsi->port_info->phy.link_info.fec_info) {
855 case ICE_AQ_LINK_25G_RS_528_FEC_EN:
856 case ICE_AQ_LINK_25G_RS_544_FEC_EN:
857 fec = "RS-FEC";
858 break;
859 case ICE_AQ_LINK_25G_KR_FEC_EN:
860 fec = "FC-FEC/BASE-R";
861 break;
862 default:
863 fec = "NONE";
864 break;
865 }
866
867 /* check if autoneg completed, might be false due to not supported */
868 if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
869 an = "True";
870 else
871 an = "False";
872
873 /* Get FEC mode requested based on PHY caps last SW configuration */
874 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
875 if (!caps) {
876 fec_req = "Unknown";
877 an_advertised = "Unknown";
878 goto done;
879 }
880
881 status = ice_aq_get_phy_caps(vsi->port_info, false,
882 ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
883 if (status)
884 netdev_info(vsi->netdev, "Get phy capability failed.\n");
885
886 an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
887
888 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
889 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
890 fec_req = "RS-FEC";
891 else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
892 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
893 fec_req = "FC-FEC/BASE-R";
894 else
895 fec_req = "NONE";
896
897 kfree(caps);
898
899done:
900 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",
901 speed, fec_req, fec, an_advertised, an, fc);
902 ice_print_topo_conflict(vsi);
903}
904
905/**
906 * ice_vsi_link_event - update the VSI's netdev
907 * @vsi: the VSI on which the link event occurred
908 * @link_up: whether or not the VSI needs to be set up or down
909 */
910static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
911{
912 if (!vsi)
913 return;
914
915 if (test_bit(ICE_VSI_DOWN, vsi->state) || !vsi->netdev)
916 return;
917
918 if (vsi->type == ICE_VSI_PF) {
919 if (link_up == netif_carrier_ok(vsi->netdev))
920 return;
921
922 if (link_up) {
923 netif_carrier_on(vsi->netdev);
924 netif_tx_wake_all_queues(vsi->netdev);
925 } else {
926 netif_carrier_off(vsi->netdev);
927 netif_tx_stop_all_queues(vsi->netdev);
928 }
929 }
930}
931
932/**
933 * ice_set_dflt_mib - send a default config MIB to the FW
934 * @pf: private PF struct
935 *
936 * This function sends a default configuration MIB to the FW.
937 *
938 * If this function errors out at any point, the driver is still able to
939 * function. The main impact is that LFC may not operate as expected.
940 * Therefore an error state in this function should be treated with a DBG
941 * message and continue on with driver rebuild/reenable.
942 */
943static void ice_set_dflt_mib(struct ice_pf *pf)
944{
945 struct device *dev = ice_pf_to_dev(pf);
946 u8 mib_type, *buf, *lldpmib = NULL;
947 u16 len, typelen, offset = 0;
948 struct ice_lldp_org_tlv *tlv;
949 struct ice_hw *hw = &pf->hw;
950 u32 ouisubtype;
951
952 mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
953 lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
954 if (!lldpmib) {
955 dev_dbg(dev, "%s Failed to allocate MIB memory\n",
956 __func__);
957 return;
958 }
959
960 /* Add ETS CFG TLV */
961 tlv = (struct ice_lldp_org_tlv *)lldpmib;
962 typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
963 ICE_IEEE_ETS_TLV_LEN);
964 tlv->typelen = htons(typelen);
965 ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
966 ICE_IEEE_SUBTYPE_ETS_CFG);
967 tlv->ouisubtype = htonl(ouisubtype);
968
969 buf = tlv->tlvinfo;
970 buf[0] = 0;
971
972 /* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
973 * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
974 * Octets 13 - 20 are TSA values - leave as zeros
975 */
976 buf[5] = 0x64;
977 len = FIELD_GET(ICE_LLDP_TLV_LEN_M, typelen);
978 offset += len + 2;
979 tlv = (struct ice_lldp_org_tlv *)
980 ((char *)tlv + sizeof(tlv->typelen) + len);
981
982 /* Add ETS REC TLV */
983 buf = tlv->tlvinfo;
984 tlv->typelen = htons(typelen);
985
986 ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
987 ICE_IEEE_SUBTYPE_ETS_REC);
988 tlv->ouisubtype = htonl(ouisubtype);
989
990 /* First octet of buf is reserved
991 * Octets 1 - 4 map UP to TC - all UPs map to zero
992 * Octets 5 - 12 are BW values - set TC 0 to 100%.
993 * Octets 13 - 20 are TSA value - leave as zeros
994 */
995 buf[5] = 0x64;
996 offset += len + 2;
997 tlv = (struct ice_lldp_org_tlv *)
998 ((char *)tlv + sizeof(tlv->typelen) + len);
999
1000 /* Add PFC CFG TLV */
1001 typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
1002 ICE_IEEE_PFC_TLV_LEN);
1003 tlv->typelen = htons(typelen);
1004
1005 ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
1006 ICE_IEEE_SUBTYPE_PFC_CFG);
1007 tlv->ouisubtype = htonl(ouisubtype);
1008
1009 /* Octet 1 left as all zeros - PFC disabled */
1010 buf[0] = 0x08;
1011 len = FIELD_GET(ICE_LLDP_TLV_LEN_M, typelen);
1012 offset += len + 2;
1013
1014 if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
1015 dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
1016
1017 kfree(lldpmib);
1018}
1019
1020/**
1021 * ice_check_phy_fw_load - check if PHY FW load failed
1022 * @pf: pointer to PF struct
1023 * @link_cfg_err: bitmap from the link info structure
1024 *
1025 * check if external PHY FW load failed and print an error message if it did
1026 */
1027static void ice_check_phy_fw_load(struct ice_pf *pf, u8 link_cfg_err)
1028{
1029 if (!(link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE)) {
1030 clear_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1031 return;
1032 }
1033
1034 if (test_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags))
1035 return;
1036
1037 if (link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE) {
1038 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");
1039 set_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1040 }
1041}
1042
1043/**
1044 * ice_check_module_power
1045 * @pf: pointer to PF struct
1046 * @link_cfg_err: bitmap from the link info structure
1047 *
1048 * check module power level returned by a previous call to aq_get_link_info
1049 * and print error messages if module power level is not supported
1050 */
1051static void ice_check_module_power(struct ice_pf *pf, u8 link_cfg_err)
1052{
1053 /* if module power level is supported, clear the flag */
1054 if (!(link_cfg_err & (ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT |
1055 ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED))) {
1056 clear_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1057 return;
1058 }
1059
1060 /* if ICE_FLAG_MOD_POWER_UNSUPPORTED was previously set and the
1061 * above block didn't clear this bit, there's nothing to do
1062 */
1063 if (test_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags))
1064 return;
1065
1066 if (link_cfg_err & ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT) {
1067 dev_err(ice_pf_to_dev(pf), "The installed module is incompatible with the device's NVM image. Cannot start link\n");
1068 set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1069 } else if (link_cfg_err & ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED) {
1070 dev_err(ice_pf_to_dev(pf), "The module's power requirements exceed the device's power supply. Cannot start link\n");
1071 set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1072 }
1073}
1074
1075/**
1076 * ice_check_link_cfg_err - check if link configuration failed
1077 * @pf: pointer to the PF struct
1078 * @link_cfg_err: bitmap from the link info structure
1079 *
1080 * print if any link configuration failure happens due to the value in the
1081 * link_cfg_err parameter in the link info structure
1082 */
1083static void ice_check_link_cfg_err(struct ice_pf *pf, u8 link_cfg_err)
1084{
1085 ice_check_module_power(pf, link_cfg_err);
1086 ice_check_phy_fw_load(pf, link_cfg_err);
1087}
1088
1089/**
1090 * ice_link_event - process the link event
1091 * @pf: PF that the link event is associated with
1092 * @pi: port_info for the port that the link event is associated with
1093 * @link_up: true if the physical link is up and false if it is down
1094 * @link_speed: current link speed received from the link event
1095 *
1096 * Returns 0 on success and negative on failure
1097 */
1098static int
1099ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
1100 u16 link_speed)
1101{
1102 struct device *dev = ice_pf_to_dev(pf);
1103 struct ice_phy_info *phy_info;
1104 struct ice_vsi *vsi;
1105 u16 old_link_speed;
1106 bool old_link;
1107 int status;
1108
1109 phy_info = &pi->phy;
1110 phy_info->link_info_old = phy_info->link_info;
1111
1112 old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
1113 old_link_speed = phy_info->link_info_old.link_speed;
1114
1115 /* update the link info structures and re-enable link events,
1116 * don't bail on failure due to other book keeping needed
1117 */
1118 status = ice_update_link_info(pi);
1119 if (status)
1120 dev_dbg(dev, "Failed to update link status on port %d, err %d aq_err %s\n",
1121 pi->lport, status,
1122 ice_aq_str(pi->hw->adminq.sq_last_status));
1123
1124 ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
1125
1126 /* Check if the link state is up after updating link info, and treat
1127 * this event as an UP event since the link is actually UP now.
1128 */
1129 if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
1130 link_up = true;
1131
1132 vsi = ice_get_main_vsi(pf);
1133 if (!vsi || !vsi->port_info)
1134 return -EINVAL;
1135
1136 /* turn off PHY if media was removed */
1137 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
1138 !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
1139 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
1140 ice_set_link(vsi, false);
1141 }
1142
1143 /* if the old link up/down and speed is the same as the new */
1144 if (link_up == old_link && link_speed == old_link_speed)
1145 return 0;
1146
1147 ice_ptp_link_change(pf, link_up);
1148
1149 if (ice_is_dcb_active(pf)) {
1150 if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
1151 ice_dcb_rebuild(pf);
1152 } else {
1153 if (link_up)
1154 ice_set_dflt_mib(pf);
1155 }
1156 ice_vsi_link_event(vsi, link_up);
1157 ice_print_link_msg(vsi, link_up);
1158
1159 ice_vc_notify_link_state(pf);
1160
1161 return 0;
1162}
1163
1164/**
1165 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
1166 * @pf: board private structure
1167 */
1168static void ice_watchdog_subtask(struct ice_pf *pf)
1169{
1170 int i;
1171
1172 /* if interface is down do nothing */
1173 if (test_bit(ICE_DOWN, pf->state) ||
1174 test_bit(ICE_CFG_BUSY, pf->state))
1175 return;
1176
1177 /* make sure we don't do these things too often */
1178 if (time_before(jiffies,
1179 pf->serv_tmr_prev + pf->serv_tmr_period))
1180 return;
1181
1182 pf->serv_tmr_prev = jiffies;
1183
1184 /* Update the stats for active netdevs so the network stack
1185 * can look at updated numbers whenever it cares to
1186 */
1187 ice_update_pf_stats(pf);
1188 ice_for_each_vsi(pf, i)
1189 if (pf->vsi[i] && pf->vsi[i]->netdev)
1190 ice_update_vsi_stats(pf->vsi[i]);
1191}
1192
1193/**
1194 * ice_init_link_events - enable/initialize link events
1195 * @pi: pointer to the port_info instance
1196 *
1197 * Returns -EIO on failure, 0 on success
1198 */
1199static int ice_init_link_events(struct ice_port_info *pi)
1200{
1201 u16 mask;
1202
1203 mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
1204 ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL |
1205 ICE_AQ_LINK_EVENT_PHY_FW_LOAD_FAIL));
1206
1207 if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
1208 dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
1209 pi->lport);
1210 return -EIO;
1211 }
1212
1213 if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
1214 dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
1215 pi->lport);
1216 return -EIO;
1217 }
1218
1219 return 0;
1220}
1221
1222/**
1223 * ice_handle_link_event - handle link event via ARQ
1224 * @pf: PF that the link event is associated with
1225 * @event: event structure containing link status info
1226 */
1227static int
1228ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1229{
1230 struct ice_aqc_get_link_status_data *link_data;
1231 struct ice_port_info *port_info;
1232 int status;
1233
1234 link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
1235 port_info = pf->hw.port_info;
1236 if (!port_info)
1237 return -EINVAL;
1238
1239 status = ice_link_event(pf, port_info,
1240 !!(link_data->link_info & ICE_AQ_LINK_UP),
1241 le16_to_cpu(link_data->link_speed));
1242 if (status)
1243 dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
1244 status);
1245
1246 return status;
1247}
1248
1249/**
1250 * ice_get_fwlog_data - copy the FW log data from ARQ event
1251 * @pf: PF that the FW log event is associated with
1252 * @event: event structure containing FW log data
1253 */
1254static void
1255ice_get_fwlog_data(struct ice_pf *pf, struct ice_rq_event_info *event)
1256{
1257 struct ice_fwlog_data *fwlog;
1258 struct ice_hw *hw = &pf->hw;
1259
1260 fwlog = &hw->fwlog_ring.rings[hw->fwlog_ring.tail];
1261
1262 memset(fwlog->data, 0, PAGE_SIZE);
1263 fwlog->data_size = le16_to_cpu(event->desc.datalen);
1264
1265 memcpy(fwlog->data, event->msg_buf, fwlog->data_size);
1266 ice_fwlog_ring_increment(&hw->fwlog_ring.tail, hw->fwlog_ring.size);
1267
1268 if (ice_fwlog_ring_full(&hw->fwlog_ring)) {
1269 /* the rings are full so bump the head to create room */
1270 ice_fwlog_ring_increment(&hw->fwlog_ring.head,
1271 hw->fwlog_ring.size);
1272 }
1273}
1274
1275/**
1276 * ice_aq_prep_for_event - Prepare to wait for an AdminQ event from firmware
1277 * @pf: pointer to the PF private structure
1278 * @task: intermediate helper storage and identifier for waiting
1279 * @opcode: the opcode to wait for
1280 *
1281 * Prepares to wait for a specific AdminQ completion event on the ARQ for
1282 * a given PF. Actual wait would be done by a call to ice_aq_wait_for_event().
1283 *
1284 * Calls are separated to allow caller registering for event before sending
1285 * the command, which mitigates a race between registering and FW responding.
1286 *
1287 * To obtain only the descriptor contents, pass an task->event with null
1288 * msg_buf. If the complete data buffer is desired, allocate the
1289 * task->event.msg_buf with enough space ahead of time.
1290 */
1291void ice_aq_prep_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1292 u16 opcode)
1293{
1294 INIT_HLIST_NODE(&task->entry);
1295 task->opcode = opcode;
1296 task->state = ICE_AQ_TASK_WAITING;
1297
1298 spin_lock_bh(&pf->aq_wait_lock);
1299 hlist_add_head(&task->entry, &pf->aq_wait_list);
1300 spin_unlock_bh(&pf->aq_wait_lock);
1301}
1302
1303/**
1304 * ice_aq_wait_for_event - Wait for an AdminQ event from firmware
1305 * @pf: pointer to the PF private structure
1306 * @task: ptr prepared by ice_aq_prep_for_event()
1307 * @timeout: how long to wait, in jiffies
1308 *
1309 * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1310 * current thread will be put to sleep until the specified event occurs or
1311 * until the given timeout is reached.
1312 *
1313 * Returns: zero on success, or a negative error code on failure.
1314 */
1315int ice_aq_wait_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1316 unsigned long timeout)
1317{
1318 enum ice_aq_task_state *state = &task->state;
1319 struct device *dev = ice_pf_to_dev(pf);
1320 unsigned long start = jiffies;
1321 long ret;
1322 int err;
1323
1324 ret = wait_event_interruptible_timeout(pf->aq_wait_queue,
1325 *state != ICE_AQ_TASK_WAITING,
1326 timeout);
1327 switch (*state) {
1328 case ICE_AQ_TASK_NOT_PREPARED:
1329 WARN(1, "call to %s without ice_aq_prep_for_event()", __func__);
1330 err = -EINVAL;
1331 break;
1332 case ICE_AQ_TASK_WAITING:
1333 err = ret < 0 ? ret : -ETIMEDOUT;
1334 break;
1335 case ICE_AQ_TASK_CANCELED:
1336 err = ret < 0 ? ret : -ECANCELED;
1337 break;
1338 case ICE_AQ_TASK_COMPLETE:
1339 err = ret < 0 ? ret : 0;
1340 break;
1341 default:
1342 WARN(1, "Unexpected AdminQ wait task state %u", *state);
1343 err = -EINVAL;
1344 break;
1345 }
1346
1347 dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
1348 jiffies_to_msecs(jiffies - start),
1349 jiffies_to_msecs(timeout),
1350 task->opcode);
1351
1352 spin_lock_bh(&pf->aq_wait_lock);
1353 hlist_del(&task->entry);
1354 spin_unlock_bh(&pf->aq_wait_lock);
1355
1356 return err;
1357}
1358
1359/**
1360 * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
1361 * @pf: pointer to the PF private structure
1362 * @opcode: the opcode of the event
1363 * @event: the event to check
1364 *
1365 * Loops over the current list of pending threads waiting for an AdminQ event.
1366 * For each matching task, copy the contents of the event into the task
1367 * structure and wake up the thread.
1368 *
1369 * If multiple threads wait for the same opcode, they will all be woken up.
1370 *
1371 * Note that event->msg_buf will only be duplicated if the event has a buffer
1372 * with enough space already allocated. Otherwise, only the descriptor and
1373 * message length will be copied.
1374 *
1375 * Returns: true if an event was found, false otherwise
1376 */
1377static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
1378 struct ice_rq_event_info *event)
1379{
1380 struct ice_rq_event_info *task_ev;
1381 struct ice_aq_task *task;
1382 bool found = false;
1383
1384 spin_lock_bh(&pf->aq_wait_lock);
1385 hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
1386 if (task->state != ICE_AQ_TASK_WAITING)
1387 continue;
1388 if (task->opcode != opcode)
1389 continue;
1390
1391 task_ev = &task->event;
1392 memcpy(&task_ev->desc, &event->desc, sizeof(event->desc));
1393 task_ev->msg_len = event->msg_len;
1394
1395 /* Only copy the data buffer if a destination was set */
1396 if (task_ev->msg_buf && task_ev->buf_len >= event->buf_len) {
1397 memcpy(task_ev->msg_buf, event->msg_buf,
1398 event->buf_len);
1399 task_ev->buf_len = event->buf_len;
1400 }
1401
1402 task->state = ICE_AQ_TASK_COMPLETE;
1403 found = true;
1404 }
1405 spin_unlock_bh(&pf->aq_wait_lock);
1406
1407 if (found)
1408 wake_up(&pf->aq_wait_queue);
1409}
1410
1411/**
1412 * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
1413 * @pf: the PF private structure
1414 *
1415 * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
1416 * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
1417 */
1418static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
1419{
1420 struct ice_aq_task *task;
1421
1422 spin_lock_bh(&pf->aq_wait_lock);
1423 hlist_for_each_entry(task, &pf->aq_wait_list, entry)
1424 task->state = ICE_AQ_TASK_CANCELED;
1425 spin_unlock_bh(&pf->aq_wait_lock);
1426
1427 wake_up(&pf->aq_wait_queue);
1428}
1429
1430#define ICE_MBX_OVERFLOW_WATERMARK 64
1431
1432/**
1433 * __ice_clean_ctrlq - helper function to clean controlq rings
1434 * @pf: ptr to struct ice_pf
1435 * @q_type: specific Control queue type
1436 */
1437static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
1438{
1439 struct device *dev = ice_pf_to_dev(pf);
1440 struct ice_rq_event_info event;
1441 struct ice_hw *hw = &pf->hw;
1442 struct ice_ctl_q_info *cq;
1443 u16 pending, i = 0;
1444 const char *qtype;
1445 u32 oldval, val;
1446
1447 /* Do not clean control queue if/when PF reset fails */
1448 if (test_bit(ICE_RESET_FAILED, pf->state))
1449 return 0;
1450
1451 switch (q_type) {
1452 case ICE_CTL_Q_ADMIN:
1453 cq = &hw->adminq;
1454 qtype = "Admin";
1455 break;
1456 case ICE_CTL_Q_SB:
1457 cq = &hw->sbq;
1458 qtype = "Sideband";
1459 break;
1460 case ICE_CTL_Q_MAILBOX:
1461 cq = &hw->mailboxq;
1462 qtype = "Mailbox";
1463 /* we are going to try to detect a malicious VF, so set the
1464 * state to begin detection
1465 */
1466 hw->mbx_snapshot.mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
1467 break;
1468 default:
1469 dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
1470 return 0;
1471 }
1472
1473 /* check for error indications - PF_xx_AxQLEN register layout for
1474 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
1475 */
1476 val = rd32(hw, cq->rq.len);
1477 if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1478 PF_FW_ARQLEN_ARQCRIT_M)) {
1479 oldval = val;
1480 if (val & PF_FW_ARQLEN_ARQVFE_M)
1481 dev_dbg(dev, "%s Receive Queue VF Error detected\n",
1482 qtype);
1483 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
1484 dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
1485 qtype);
1486 }
1487 if (val & PF_FW_ARQLEN_ARQCRIT_M)
1488 dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
1489 qtype);
1490 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1491 PF_FW_ARQLEN_ARQCRIT_M);
1492 if (oldval != val)
1493 wr32(hw, cq->rq.len, val);
1494 }
1495
1496 val = rd32(hw, cq->sq.len);
1497 if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1498 PF_FW_ATQLEN_ATQCRIT_M)) {
1499 oldval = val;
1500 if (val & PF_FW_ATQLEN_ATQVFE_M)
1501 dev_dbg(dev, "%s Send Queue VF Error detected\n",
1502 qtype);
1503 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1504 dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
1505 qtype);
1506 }
1507 if (val & PF_FW_ATQLEN_ATQCRIT_M)
1508 dev_dbg(dev, "%s Send Queue Critical Error detected\n",
1509 qtype);
1510 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1511 PF_FW_ATQLEN_ATQCRIT_M);
1512 if (oldval != val)
1513 wr32(hw, cq->sq.len, val);
1514 }
1515
1516 event.buf_len = cq->rq_buf_size;
1517 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1518 if (!event.msg_buf)
1519 return 0;
1520
1521 do {
1522 struct ice_mbx_data data = {};
1523 u16 opcode;
1524 int ret;
1525
1526 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1527 if (ret == -EALREADY)
1528 break;
1529 if (ret) {
1530 dev_err(dev, "%s Receive Queue event error %d\n", qtype,
1531 ret);
1532 break;
1533 }
1534
1535 opcode = le16_to_cpu(event.desc.opcode);
1536
1537 /* Notify any thread that might be waiting for this event */
1538 ice_aq_check_events(pf, opcode, &event);
1539
1540 switch (opcode) {
1541 case ice_aqc_opc_get_link_status:
1542 if (ice_handle_link_event(pf, &event))
1543 dev_err(dev, "Could not handle link event\n");
1544 break;
1545 case ice_aqc_opc_event_lan_overflow:
1546 ice_vf_lan_overflow_event(pf, &event);
1547 break;
1548 case ice_mbx_opc_send_msg_to_pf:
1549 if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT)) {
1550 ice_vc_process_vf_msg(pf, &event, NULL);
1551 ice_mbx_vf_dec_trig_e830(hw, &event);
1552 } else {
1553 u16 val = hw->mailboxq.num_rq_entries;
1554
1555 data.max_num_msgs_mbx = val;
1556 val = ICE_MBX_OVERFLOW_WATERMARK;
1557 data.async_watermark_val = val;
1558 data.num_msg_proc = i;
1559 data.num_pending_arq = pending;
1560
1561 ice_vc_process_vf_msg(pf, &event, &data);
1562 }
1563 break;
1564 case ice_aqc_opc_fw_logs_event:
1565 ice_get_fwlog_data(pf, &event);
1566 break;
1567 case ice_aqc_opc_lldp_set_mib_change:
1568 ice_dcb_process_lldp_set_mib_change(pf, &event);
1569 break;
1570 default:
1571 dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
1572 qtype, opcode);
1573 break;
1574 }
1575 } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1576
1577 kfree(event.msg_buf);
1578
1579 return pending && (i == ICE_DFLT_IRQ_WORK);
1580}
1581
1582/**
1583 * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1584 * @hw: pointer to hardware info
1585 * @cq: control queue information
1586 *
1587 * returns true if there are pending messages in a queue, false if there aren't
1588 */
1589static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1590{
1591 u16 ntu;
1592
1593 ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1594 return cq->rq.next_to_clean != ntu;
1595}
1596
1597/**
1598 * ice_clean_adminq_subtask - clean the AdminQ rings
1599 * @pf: board private structure
1600 */
1601static void ice_clean_adminq_subtask(struct ice_pf *pf)
1602{
1603 struct ice_hw *hw = &pf->hw;
1604
1605 if (!test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
1606 return;
1607
1608 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1609 return;
1610
1611 clear_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
1612
1613 /* There might be a situation where new messages arrive to a control
1614 * queue between processing the last message and clearing the
1615 * EVENT_PENDING bit. So before exiting, check queue head again (using
1616 * ice_ctrlq_pending) and process new messages if any.
1617 */
1618 if (ice_ctrlq_pending(hw, &hw->adminq))
1619 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1620
1621 ice_flush(hw);
1622}
1623
1624/**
1625 * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1626 * @pf: board private structure
1627 */
1628static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1629{
1630 struct ice_hw *hw = &pf->hw;
1631
1632 if (!test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1633 return;
1634
1635 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1636 return;
1637
1638 clear_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1639
1640 if (ice_ctrlq_pending(hw, &hw->mailboxq))
1641 __ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1642
1643 ice_flush(hw);
1644}
1645
1646/**
1647 * ice_clean_sbq_subtask - clean the Sideband Queue rings
1648 * @pf: board private structure
1649 */
1650static void ice_clean_sbq_subtask(struct ice_pf *pf)
1651{
1652 struct ice_hw *hw = &pf->hw;
1653
1654 /* if mac_type is not generic, sideband is not supported
1655 * and there's nothing to do here
1656 */
1657 if (!ice_is_generic_mac(hw)) {
1658 clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1659 return;
1660 }
1661
1662 if (!test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state))
1663 return;
1664
1665 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_SB))
1666 return;
1667
1668 clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1669
1670 if (ice_ctrlq_pending(hw, &hw->sbq))
1671 __ice_clean_ctrlq(pf, ICE_CTL_Q_SB);
1672
1673 ice_flush(hw);
1674}
1675
1676/**
1677 * ice_service_task_schedule - schedule the service task to wake up
1678 * @pf: board private structure
1679 *
1680 * If not already scheduled, this puts the task into the work queue.
1681 */
1682void ice_service_task_schedule(struct ice_pf *pf)
1683{
1684 if (!test_bit(ICE_SERVICE_DIS, pf->state) &&
1685 !test_and_set_bit(ICE_SERVICE_SCHED, pf->state) &&
1686 !test_bit(ICE_NEEDS_RESTART, pf->state))
1687 queue_work(ice_wq, &pf->serv_task);
1688}
1689
1690/**
1691 * ice_service_task_complete - finish up the service task
1692 * @pf: board private structure
1693 */
1694static void ice_service_task_complete(struct ice_pf *pf)
1695{
1696 WARN_ON(!test_bit(ICE_SERVICE_SCHED, pf->state));
1697
1698 /* force memory (pf->state) to sync before next service task */
1699 smp_mb__before_atomic();
1700 clear_bit(ICE_SERVICE_SCHED, pf->state);
1701}
1702
1703/**
1704 * ice_service_task_stop - stop service task and cancel works
1705 * @pf: board private structure
1706 *
1707 * Return 0 if the ICE_SERVICE_DIS bit was not already set,
1708 * 1 otherwise.
1709 */
1710static int ice_service_task_stop(struct ice_pf *pf)
1711{
1712 int ret;
1713
1714 ret = test_and_set_bit(ICE_SERVICE_DIS, pf->state);
1715
1716 if (pf->serv_tmr.function)
1717 del_timer_sync(&pf->serv_tmr);
1718 if (pf->serv_task.func)
1719 cancel_work_sync(&pf->serv_task);
1720
1721 clear_bit(ICE_SERVICE_SCHED, pf->state);
1722 return ret;
1723}
1724
1725/**
1726 * ice_service_task_restart - restart service task and schedule works
1727 * @pf: board private structure
1728 *
1729 * This function is needed for suspend and resume works (e.g WoL scenario)
1730 */
1731static void ice_service_task_restart(struct ice_pf *pf)
1732{
1733 clear_bit(ICE_SERVICE_DIS, pf->state);
1734 ice_service_task_schedule(pf);
1735}
1736
1737/**
1738 * ice_service_timer - timer callback to schedule service task
1739 * @t: pointer to timer_list
1740 */
1741static void ice_service_timer(struct timer_list *t)
1742{
1743 struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1744
1745 mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1746 ice_service_task_schedule(pf);
1747}
1748
1749/**
1750 * ice_mdd_maybe_reset_vf - reset VF after MDD event
1751 * @pf: pointer to the PF structure
1752 * @vf: pointer to the VF structure
1753 * @reset_vf_tx: whether Tx MDD has occurred
1754 * @reset_vf_rx: whether Rx MDD has occurred
1755 *
1756 * Since the queue can get stuck on VF MDD events, the PF can be configured to
1757 * automatically reset the VF by enabling the private ethtool flag
1758 * mdd-auto-reset-vf.
1759 */
1760static void ice_mdd_maybe_reset_vf(struct ice_pf *pf, struct ice_vf *vf,
1761 bool reset_vf_tx, bool reset_vf_rx)
1762{
1763 struct device *dev = ice_pf_to_dev(pf);
1764
1765 if (!test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags))
1766 return;
1767
1768 /* VF MDD event counters will be cleared by reset, so print the event
1769 * prior to reset.
1770 */
1771 if (reset_vf_tx)
1772 ice_print_vf_tx_mdd_event(vf);
1773
1774 if (reset_vf_rx)
1775 ice_print_vf_rx_mdd_event(vf);
1776
1777 dev_info(dev, "PF-to-VF reset on PF %d VF %d due to MDD event\n",
1778 pf->hw.pf_id, vf->vf_id);
1779 ice_reset_vf(vf, ICE_VF_RESET_NOTIFY | ICE_VF_RESET_LOCK);
1780}
1781
1782/**
1783 * ice_handle_mdd_event - handle malicious driver detect event
1784 * @pf: pointer to the PF structure
1785 *
1786 * Called from service task. OICR interrupt handler indicates MDD event.
1787 * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
1788 * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
1789 * disable the queue, the PF can be configured to reset the VF using ethtool
1790 * private flag mdd-auto-reset-vf.
1791 */
1792static void ice_handle_mdd_event(struct ice_pf *pf)
1793{
1794 struct device *dev = ice_pf_to_dev(pf);
1795 struct ice_hw *hw = &pf->hw;
1796 struct ice_vf *vf;
1797 unsigned int bkt;
1798 u32 reg;
1799
1800 if (!test_and_clear_bit(ICE_MDD_EVENT_PENDING, pf->state)) {
1801 /* Since the VF MDD event logging is rate limited, check if
1802 * there are pending MDD events.
1803 */
1804 ice_print_vfs_mdd_events(pf);
1805 return;
1806 }
1807
1808 /* find what triggered an MDD event */
1809 reg = rd32(hw, GL_MDET_TX_PQM);
1810 if (reg & GL_MDET_TX_PQM_VALID_M) {
1811 u8 pf_num = FIELD_GET(GL_MDET_TX_PQM_PF_NUM_M, reg);
1812 u16 vf_num = FIELD_GET(GL_MDET_TX_PQM_VF_NUM_M, reg);
1813 u8 event = FIELD_GET(GL_MDET_TX_PQM_MAL_TYPE_M, reg);
1814 u16 queue = FIELD_GET(GL_MDET_TX_PQM_QNUM_M, reg);
1815
1816 if (netif_msg_tx_err(pf))
1817 dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1818 event, queue, pf_num, vf_num);
1819 wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1820 }
1821
1822 reg = rd32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw));
1823 if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1824 u8 pf_num = FIELD_GET(GL_MDET_TX_TCLAN_PF_NUM_M, reg);
1825 u16 vf_num = FIELD_GET(GL_MDET_TX_TCLAN_VF_NUM_M, reg);
1826 u8 event = FIELD_GET(GL_MDET_TX_TCLAN_MAL_TYPE_M, reg);
1827 u16 queue = FIELD_GET(GL_MDET_TX_TCLAN_QNUM_M, reg);
1828
1829 if (netif_msg_tx_err(pf))
1830 dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1831 event, queue, pf_num, vf_num);
1832 wr32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw), U32_MAX);
1833 }
1834
1835 reg = rd32(hw, GL_MDET_RX);
1836 if (reg & GL_MDET_RX_VALID_M) {
1837 u8 pf_num = FIELD_GET(GL_MDET_RX_PF_NUM_M, reg);
1838 u16 vf_num = FIELD_GET(GL_MDET_RX_VF_NUM_M, reg);
1839 u8 event = FIELD_GET(GL_MDET_RX_MAL_TYPE_M, reg);
1840 u16 queue = FIELD_GET(GL_MDET_RX_QNUM_M, reg);
1841
1842 if (netif_msg_rx_err(pf))
1843 dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1844 event, queue, pf_num, vf_num);
1845 wr32(hw, GL_MDET_RX, 0xffffffff);
1846 }
1847
1848 /* check to see if this PF caused an MDD event */
1849 reg = rd32(hw, PF_MDET_TX_PQM);
1850 if (reg & PF_MDET_TX_PQM_VALID_M) {
1851 wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1852 if (netif_msg_tx_err(pf))
1853 dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
1854 }
1855
1856 reg = rd32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw));
1857 if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1858 wr32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw), 0xffff);
1859 if (netif_msg_tx_err(pf))
1860 dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
1861 }
1862
1863 reg = rd32(hw, PF_MDET_RX);
1864 if (reg & PF_MDET_RX_VALID_M) {
1865 wr32(hw, PF_MDET_RX, 0xFFFF);
1866 if (netif_msg_rx_err(pf))
1867 dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
1868 }
1869
1870 /* Check to see if one of the VFs caused an MDD event, and then
1871 * increment counters and set print pending
1872 */
1873 mutex_lock(&pf->vfs.table_lock);
1874 ice_for_each_vf(pf, bkt, vf) {
1875 bool reset_vf_tx = false, reset_vf_rx = false;
1876
1877 reg = rd32(hw, VP_MDET_TX_PQM(vf->vf_id));
1878 if (reg & VP_MDET_TX_PQM_VALID_M) {
1879 wr32(hw, VP_MDET_TX_PQM(vf->vf_id), 0xFFFF);
1880 vf->mdd_tx_events.count++;
1881 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1882 if (netif_msg_tx_err(pf))
1883 dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
1884 vf->vf_id);
1885
1886 reset_vf_tx = true;
1887 }
1888
1889 reg = rd32(hw, VP_MDET_TX_TCLAN(vf->vf_id));
1890 if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1891 wr32(hw, VP_MDET_TX_TCLAN(vf->vf_id), 0xFFFF);
1892 vf->mdd_tx_events.count++;
1893 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1894 if (netif_msg_tx_err(pf))
1895 dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
1896 vf->vf_id);
1897
1898 reset_vf_tx = true;
1899 }
1900
1901 reg = rd32(hw, VP_MDET_TX_TDPU(vf->vf_id));
1902 if (reg & VP_MDET_TX_TDPU_VALID_M) {
1903 wr32(hw, VP_MDET_TX_TDPU(vf->vf_id), 0xFFFF);
1904 vf->mdd_tx_events.count++;
1905 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1906 if (netif_msg_tx_err(pf))
1907 dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
1908 vf->vf_id);
1909
1910 reset_vf_tx = true;
1911 }
1912
1913 reg = rd32(hw, VP_MDET_RX(vf->vf_id));
1914 if (reg & VP_MDET_RX_VALID_M) {
1915 wr32(hw, VP_MDET_RX(vf->vf_id), 0xFFFF);
1916 vf->mdd_rx_events.count++;
1917 set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1918 if (netif_msg_rx_err(pf))
1919 dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
1920 vf->vf_id);
1921
1922 reset_vf_rx = true;
1923 }
1924
1925 if (reset_vf_tx || reset_vf_rx)
1926 ice_mdd_maybe_reset_vf(pf, vf, reset_vf_tx,
1927 reset_vf_rx);
1928 }
1929 mutex_unlock(&pf->vfs.table_lock);
1930
1931 ice_print_vfs_mdd_events(pf);
1932}
1933
1934/**
1935 * ice_force_phys_link_state - Force the physical link state
1936 * @vsi: VSI to force the physical link state to up/down
1937 * @link_up: true/false indicates to set the physical link to up/down
1938 *
1939 * Force the physical link state by getting the current PHY capabilities from
1940 * hardware and setting the PHY config based on the determined capabilities. If
1941 * link changes a link event will be triggered because both the Enable Automatic
1942 * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1943 *
1944 * Returns 0 on success, negative on failure
1945 */
1946static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1947{
1948 struct ice_aqc_get_phy_caps_data *pcaps;
1949 struct ice_aqc_set_phy_cfg_data *cfg;
1950 struct ice_port_info *pi;
1951 struct device *dev;
1952 int retcode;
1953
1954 if (!vsi || !vsi->port_info || !vsi->back)
1955 return -EINVAL;
1956 if (vsi->type != ICE_VSI_PF)
1957 return 0;
1958
1959 dev = ice_pf_to_dev(vsi->back);
1960
1961 pi = vsi->port_info;
1962
1963 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1964 if (!pcaps)
1965 return -ENOMEM;
1966
1967 retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
1968 NULL);
1969 if (retcode) {
1970 dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
1971 vsi->vsi_num, retcode);
1972 retcode = -EIO;
1973 goto out;
1974 }
1975
1976 /* No change in link */
1977 if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1978 link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1979 goto out;
1980
1981 /* Use the current user PHY configuration. The current user PHY
1982 * configuration is initialized during probe from PHY capabilities
1983 * software mode, and updated on set PHY configuration.
1984 */
1985 cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
1986 if (!cfg) {
1987 retcode = -ENOMEM;
1988 goto out;
1989 }
1990
1991 cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1992 if (link_up)
1993 cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1994 else
1995 cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1996
1997 retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1998 if (retcode) {
1999 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
2000 vsi->vsi_num, retcode);
2001 retcode = -EIO;
2002 }
2003
2004 kfree(cfg);
2005out:
2006 kfree(pcaps);
2007 return retcode;
2008}
2009
2010/**
2011 * ice_init_nvm_phy_type - Initialize the NVM PHY type
2012 * @pi: port info structure
2013 *
2014 * Initialize nvm_phy_type_[low|high] for link lenient mode support
2015 */
2016static int ice_init_nvm_phy_type(struct ice_port_info *pi)
2017{
2018 struct ice_aqc_get_phy_caps_data *pcaps;
2019 struct ice_pf *pf = pi->hw->back;
2020 int err;
2021
2022 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2023 if (!pcaps)
2024 return -ENOMEM;
2025
2026 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA,
2027 pcaps, NULL);
2028
2029 if (err) {
2030 dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
2031 goto out;
2032 }
2033
2034 pf->nvm_phy_type_hi = pcaps->phy_type_high;
2035 pf->nvm_phy_type_lo = pcaps->phy_type_low;
2036
2037out:
2038 kfree(pcaps);
2039 return err;
2040}
2041
2042/**
2043 * ice_init_link_dflt_override - Initialize link default override
2044 * @pi: port info structure
2045 *
2046 * Initialize link default override and PHY total port shutdown during probe
2047 */
2048static void ice_init_link_dflt_override(struct ice_port_info *pi)
2049{
2050 struct ice_link_default_override_tlv *ldo;
2051 struct ice_pf *pf = pi->hw->back;
2052
2053 ldo = &pf->link_dflt_override;
2054 if (ice_get_link_default_override(ldo, pi))
2055 return;
2056
2057 if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
2058 return;
2059
2060 /* Enable Total Port Shutdown (override/replace link-down-on-close
2061 * ethtool private flag) for ports with Port Disable bit set.
2062 */
2063 set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
2064 set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
2065}
2066
2067/**
2068 * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
2069 * @pi: port info structure
2070 *
2071 * If default override is enabled, initialize the user PHY cfg speed and FEC
2072 * settings using the default override mask from the NVM.
2073 *
2074 * The PHY should only be configured with the default override settings the
2075 * first time media is available. The ICE_LINK_DEFAULT_OVERRIDE_PENDING state
2076 * is used to indicate that the user PHY cfg default override is initialized
2077 * and the PHY has not been configured with the default override settings. The
2078 * state is set here, and cleared in ice_configure_phy the first time the PHY is
2079 * configured.
2080 *
2081 * This function should be called only if the FW doesn't support default
2082 * configuration mode, as reported by ice_fw_supports_report_dflt_cfg.
2083 */
2084static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
2085{
2086 struct ice_link_default_override_tlv *ldo;
2087 struct ice_aqc_set_phy_cfg_data *cfg;
2088 struct ice_phy_info *phy = &pi->phy;
2089 struct ice_pf *pf = pi->hw->back;
2090
2091 ldo = &pf->link_dflt_override;
2092
2093 /* If link default override is enabled, use to mask NVM PHY capabilities
2094 * for speed and FEC default configuration.
2095 */
2096 cfg = &phy->curr_user_phy_cfg;
2097
2098 if (ldo->phy_type_low || ldo->phy_type_high) {
2099 cfg->phy_type_low = pf->nvm_phy_type_lo &
2100 cpu_to_le64(ldo->phy_type_low);
2101 cfg->phy_type_high = pf->nvm_phy_type_hi &
2102 cpu_to_le64(ldo->phy_type_high);
2103 }
2104 cfg->link_fec_opt = ldo->fec_options;
2105 phy->curr_user_fec_req = ICE_FEC_AUTO;
2106
2107 set_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
2108}
2109
2110/**
2111 * ice_init_phy_user_cfg - Initialize the PHY user configuration
2112 * @pi: port info structure
2113 *
2114 * Initialize the current user PHY configuration, speed, FEC, and FC requested
2115 * mode to default. The PHY defaults are from get PHY capabilities topology
2116 * with media so call when media is first available. An error is returned if
2117 * called when media is not available. The PHY initialization completed state is
2118 * set here.
2119 *
2120 * These configurations are used when setting PHY
2121 * configuration. The user PHY configuration is updated on set PHY
2122 * configuration. Returns 0 on success, negative on failure
2123 */
2124static int ice_init_phy_user_cfg(struct ice_port_info *pi)
2125{
2126 struct ice_aqc_get_phy_caps_data *pcaps;
2127 struct ice_phy_info *phy = &pi->phy;
2128 struct ice_pf *pf = pi->hw->back;
2129 int err;
2130
2131 if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2132 return -EIO;
2133
2134 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2135 if (!pcaps)
2136 return -ENOMEM;
2137
2138 if (ice_fw_supports_report_dflt_cfg(pi->hw))
2139 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2140 pcaps, NULL);
2141 else
2142 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2143 pcaps, NULL);
2144 if (err) {
2145 dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
2146 goto err_out;
2147 }
2148
2149 ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
2150
2151 /* check if lenient mode is supported and enabled */
2152 if (ice_fw_supports_link_override(pi->hw) &&
2153 !(pcaps->module_compliance_enforcement &
2154 ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
2155 set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
2156
2157 /* if the FW supports default PHY configuration mode, then the driver
2158 * does not have to apply link override settings. If not,
2159 * initialize user PHY configuration with link override values
2160 */
2161 if (!ice_fw_supports_report_dflt_cfg(pi->hw) &&
2162 (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN)) {
2163 ice_init_phy_cfg_dflt_override(pi);
2164 goto out;
2165 }
2166 }
2167
2168 /* if link default override is not enabled, set user flow control and
2169 * FEC settings based on what get_phy_caps returned
2170 */
2171 phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
2172 pcaps->link_fec_options);
2173 phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
2174
2175out:
2176 phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
2177 set_bit(ICE_PHY_INIT_COMPLETE, pf->state);
2178err_out:
2179 kfree(pcaps);
2180 return err;
2181}
2182
2183/**
2184 * ice_configure_phy - configure PHY
2185 * @vsi: VSI of PHY
2186 *
2187 * Set the PHY configuration. If the current PHY configuration is the same as
2188 * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
2189 * configure the based get PHY capabilities for topology with media.
2190 */
2191static int ice_configure_phy(struct ice_vsi *vsi)
2192{
2193 struct device *dev = ice_pf_to_dev(vsi->back);
2194 struct ice_port_info *pi = vsi->port_info;
2195 struct ice_aqc_get_phy_caps_data *pcaps;
2196 struct ice_aqc_set_phy_cfg_data *cfg;
2197 struct ice_phy_info *phy = &pi->phy;
2198 struct ice_pf *pf = vsi->back;
2199 int err;
2200
2201 /* Ensure we have media as we cannot configure a medialess port */
2202 if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2203 return -ENOMEDIUM;
2204
2205 ice_print_topo_conflict(vsi);
2206
2207 if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags) &&
2208 phy->link_info.topo_media_conflict == ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
2209 return -EPERM;
2210
2211 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))
2212 return ice_force_phys_link_state(vsi, true);
2213
2214 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2215 if (!pcaps)
2216 return -ENOMEM;
2217
2218 /* Get current PHY config */
2219 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
2220 NULL);
2221 if (err) {
2222 dev_err(dev, "Failed to get PHY configuration, VSI %d error %d\n",
2223 vsi->vsi_num, err);
2224 goto done;
2225 }
2226
2227 /* If PHY enable link is configured and configuration has not changed,
2228 * there's nothing to do
2229 */
2230 if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
2231 ice_phy_caps_equals_cfg(pcaps, &phy->curr_user_phy_cfg))
2232 goto done;
2233
2234 /* Use PHY topology as baseline for configuration */
2235 memset(pcaps, 0, sizeof(*pcaps));
2236 if (ice_fw_supports_report_dflt_cfg(pi->hw))
2237 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2238 pcaps, NULL);
2239 else
2240 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2241 pcaps, NULL);
2242 if (err) {
2243 dev_err(dev, "Failed to get PHY caps, VSI %d error %d\n",
2244 vsi->vsi_num, err);
2245 goto done;
2246 }
2247
2248 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2249 if (!cfg) {
2250 err = -ENOMEM;
2251 goto done;
2252 }
2253
2254 ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
2255
2256 /* Speed - If default override pending, use curr_user_phy_cfg set in
2257 * ice_init_phy_user_cfg_ldo.
2258 */
2259 if (test_and_clear_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING,
2260 vsi->back->state)) {
2261 cfg->phy_type_low = phy->curr_user_phy_cfg.phy_type_low;
2262 cfg->phy_type_high = phy->curr_user_phy_cfg.phy_type_high;
2263 } else {
2264 u64 phy_low = 0, phy_high = 0;
2265
2266 ice_update_phy_type(&phy_low, &phy_high,
2267 pi->phy.curr_user_speed_req);
2268 cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
2269 cfg->phy_type_high = pcaps->phy_type_high &
2270 cpu_to_le64(phy_high);
2271 }
2272
2273 /* Can't provide what was requested; use PHY capabilities */
2274 if (!cfg->phy_type_low && !cfg->phy_type_high) {
2275 cfg->phy_type_low = pcaps->phy_type_low;
2276 cfg->phy_type_high = pcaps->phy_type_high;
2277 }
2278
2279 /* FEC */
2280 ice_cfg_phy_fec(pi, cfg, phy->curr_user_fec_req);
2281
2282 /* Can't provide what was requested; use PHY capabilities */
2283 if (cfg->link_fec_opt !=
2284 (cfg->link_fec_opt & pcaps->link_fec_options)) {
2285 cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
2286 cfg->link_fec_opt = pcaps->link_fec_options;
2287 }
2288
2289 /* Flow Control - always supported; no need to check against
2290 * capabilities
2291 */
2292 ice_cfg_phy_fc(pi, cfg, phy->curr_user_fc_req);
2293
2294 /* Enable link and link update */
2295 cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
2296
2297 err = ice_aq_set_phy_cfg(&pf->hw, pi, cfg, NULL);
2298 if (err)
2299 dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
2300 vsi->vsi_num, err);
2301
2302 kfree(cfg);
2303done:
2304 kfree(pcaps);
2305 return err;
2306}
2307
2308/**
2309 * ice_check_media_subtask - Check for media
2310 * @pf: pointer to PF struct
2311 *
2312 * If media is available, then initialize PHY user configuration if it is not
2313 * been, and configure the PHY if the interface is up.
2314 */
2315static void ice_check_media_subtask(struct ice_pf *pf)
2316{
2317 struct ice_port_info *pi;
2318 struct ice_vsi *vsi;
2319 int err;
2320
2321 /* No need to check for media if it's already present */
2322 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
2323 return;
2324
2325 vsi = ice_get_main_vsi(pf);
2326 if (!vsi)
2327 return;
2328
2329 /* Refresh link info and check if media is present */
2330 pi = vsi->port_info;
2331 err = ice_update_link_info(pi);
2332 if (err)
2333 return;
2334
2335 ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
2336
2337 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2338 if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state))
2339 ice_init_phy_user_cfg(pi);
2340
2341 /* PHY settings are reset on media insertion, reconfigure
2342 * PHY to preserve settings.
2343 */
2344 if (test_bit(ICE_VSI_DOWN, vsi->state) &&
2345 test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
2346 return;
2347
2348 err = ice_configure_phy(vsi);
2349 if (!err)
2350 clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
2351
2352 /* A Link Status Event will be generated; the event handler
2353 * will complete bringing the interface up
2354 */
2355 }
2356}
2357
2358/**
2359 * ice_service_task - manage and run subtasks
2360 * @work: pointer to work_struct contained by the PF struct
2361 */
2362static void ice_service_task(struct work_struct *work)
2363{
2364 struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
2365 unsigned long start_time = jiffies;
2366
2367 /* subtasks */
2368
2369 /* process reset requests first */
2370 ice_reset_subtask(pf);
2371
2372 /* bail if a reset/recovery cycle is pending or rebuild failed */
2373 if (ice_is_reset_in_progress(pf->state) ||
2374 test_bit(ICE_SUSPENDED, pf->state) ||
2375 test_bit(ICE_NEEDS_RESTART, pf->state)) {
2376 ice_service_task_complete(pf);
2377 return;
2378 }
2379
2380 if (test_and_clear_bit(ICE_AUX_ERR_PENDING, pf->state)) {
2381 struct iidc_event *event;
2382
2383 event = kzalloc(sizeof(*event), GFP_KERNEL);
2384 if (event) {
2385 set_bit(IIDC_EVENT_CRIT_ERR, event->type);
2386 /* report the entire OICR value to AUX driver */
2387 swap(event->reg, pf->oicr_err_reg);
2388 ice_send_event_to_aux(pf, event);
2389 kfree(event);
2390 }
2391 }
2392
2393 /* unplug aux dev per request, if an unplug request came in
2394 * while processing a plug request, this will handle it
2395 */
2396 if (test_and_clear_bit(ICE_FLAG_UNPLUG_AUX_DEV, pf->flags))
2397 ice_unplug_aux_dev(pf);
2398
2399 /* Plug aux device per request */
2400 if (test_and_clear_bit(ICE_FLAG_PLUG_AUX_DEV, pf->flags))
2401 ice_plug_aux_dev(pf);
2402
2403 if (test_and_clear_bit(ICE_FLAG_MTU_CHANGED, pf->flags)) {
2404 struct iidc_event *event;
2405
2406 event = kzalloc(sizeof(*event), GFP_KERNEL);
2407 if (event) {
2408 set_bit(IIDC_EVENT_AFTER_MTU_CHANGE, event->type);
2409 ice_send_event_to_aux(pf, event);
2410 kfree(event);
2411 }
2412 }
2413
2414 ice_clean_adminq_subtask(pf);
2415 ice_check_media_subtask(pf);
2416 ice_check_for_hang_subtask(pf);
2417 ice_sync_fltr_subtask(pf);
2418 ice_handle_mdd_event(pf);
2419 ice_watchdog_subtask(pf);
2420
2421 if (ice_is_safe_mode(pf)) {
2422 ice_service_task_complete(pf);
2423 return;
2424 }
2425
2426 ice_process_vflr_event(pf);
2427 ice_clean_mailboxq_subtask(pf);
2428 ice_clean_sbq_subtask(pf);
2429 ice_sync_arfs_fltrs(pf);
2430 ice_flush_fdir_ctx(pf);
2431
2432 /* Clear ICE_SERVICE_SCHED flag to allow scheduling next event */
2433 ice_service_task_complete(pf);
2434
2435 /* If the tasks have taken longer than one service timer period
2436 * or there is more work to be done, reset the service timer to
2437 * schedule the service task now.
2438 */
2439 if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
2440 test_bit(ICE_MDD_EVENT_PENDING, pf->state) ||
2441 test_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2442 test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
2443 test_bit(ICE_FD_VF_FLUSH_CTX, pf->state) ||
2444 test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state) ||
2445 test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
2446 mod_timer(&pf->serv_tmr, jiffies);
2447}
2448
2449/**
2450 * ice_set_ctrlq_len - helper function to set controlq length
2451 * @hw: pointer to the HW instance
2452 */
2453static void ice_set_ctrlq_len(struct ice_hw *hw)
2454{
2455 hw->adminq.num_rq_entries = ICE_AQ_LEN;
2456 hw->adminq.num_sq_entries = ICE_AQ_LEN;
2457 hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
2458 hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
2459 hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
2460 hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
2461 hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2462 hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2463 hw->sbq.num_rq_entries = ICE_SBQ_LEN;
2464 hw->sbq.num_sq_entries = ICE_SBQ_LEN;
2465 hw->sbq.rq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2466 hw->sbq.sq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2467}
2468
2469/**
2470 * ice_schedule_reset - schedule a reset
2471 * @pf: board private structure
2472 * @reset: reset being requested
2473 */
2474int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
2475{
2476 struct device *dev = ice_pf_to_dev(pf);
2477
2478 /* bail out if earlier reset has failed */
2479 if (test_bit(ICE_RESET_FAILED, pf->state)) {
2480 dev_dbg(dev, "earlier reset has failed\n");
2481 return -EIO;
2482 }
2483 /* bail if reset/recovery already in progress */
2484 if (ice_is_reset_in_progress(pf->state)) {
2485 dev_dbg(dev, "Reset already in progress\n");
2486 return -EBUSY;
2487 }
2488
2489 switch (reset) {
2490 case ICE_RESET_PFR:
2491 set_bit(ICE_PFR_REQ, pf->state);
2492 break;
2493 case ICE_RESET_CORER:
2494 set_bit(ICE_CORER_REQ, pf->state);
2495 break;
2496 case ICE_RESET_GLOBR:
2497 set_bit(ICE_GLOBR_REQ, pf->state);
2498 break;
2499 default:
2500 return -EINVAL;
2501 }
2502
2503 ice_service_task_schedule(pf);
2504 return 0;
2505}
2506
2507/**
2508 * ice_irq_affinity_notify - Callback for affinity changes
2509 * @notify: context as to what irq was changed
2510 * @mask: the new affinity mask
2511 *
2512 * This is a callback function used by the irq_set_affinity_notifier function
2513 * so that we may register to receive changes to the irq affinity masks.
2514 */
2515static void
2516ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2517 const cpumask_t *mask)
2518{
2519 struct ice_q_vector *q_vector =
2520 container_of(notify, struct ice_q_vector, affinity_notify);
2521
2522 cpumask_copy(&q_vector->affinity_mask, mask);
2523}
2524
2525/**
2526 * ice_irq_affinity_release - Callback for affinity notifier release
2527 * @ref: internal core kernel usage
2528 *
2529 * This is a callback function used by the irq_set_affinity_notifier function
2530 * to inform the current notification subscriber that they will no longer
2531 * receive notifications.
2532 */
2533static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2534
2535/**
2536 * ice_vsi_ena_irq - Enable IRQ for the given VSI
2537 * @vsi: the VSI being configured
2538 */
2539static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2540{
2541 struct ice_hw *hw = &vsi->back->hw;
2542 int i;
2543
2544 ice_for_each_q_vector(vsi, i)
2545 ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
2546
2547 ice_flush(hw);
2548 return 0;
2549}
2550
2551/**
2552 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2553 * @vsi: the VSI being configured
2554 * @basename: name for the vector
2555 */
2556static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2557{
2558 int q_vectors = vsi->num_q_vectors;
2559 struct ice_pf *pf = vsi->back;
2560 struct device *dev;
2561 int rx_int_idx = 0;
2562 int tx_int_idx = 0;
2563 int vector, err;
2564 int irq_num;
2565
2566 dev = ice_pf_to_dev(pf);
2567 for (vector = 0; vector < q_vectors; vector++) {
2568 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2569
2570 irq_num = q_vector->irq.virq;
2571
2572 if (q_vector->tx.tx_ring && q_vector->rx.rx_ring) {
2573 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2574 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2575 tx_int_idx++;
2576 } else if (q_vector->rx.rx_ring) {
2577 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2578 "%s-%s-%d", basename, "rx", rx_int_idx++);
2579 } else if (q_vector->tx.tx_ring) {
2580 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2581 "%s-%s-%d", basename, "tx", tx_int_idx++);
2582 } else {
2583 /* skip this unused q_vector */
2584 continue;
2585 }
2586 if (vsi->type == ICE_VSI_CTRL && vsi->vf)
2587 err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2588 IRQF_SHARED, q_vector->name,
2589 q_vector);
2590 else
2591 err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2592 0, q_vector->name, q_vector);
2593 if (err) {
2594 netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2595 err);
2596 goto free_q_irqs;
2597 }
2598
2599 /* register for affinity change notifications */
2600 if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2601 struct irq_affinity_notify *affinity_notify;
2602
2603 affinity_notify = &q_vector->affinity_notify;
2604 affinity_notify->notify = ice_irq_affinity_notify;
2605 affinity_notify->release = ice_irq_affinity_release;
2606 irq_set_affinity_notifier(irq_num, affinity_notify);
2607 }
2608
2609 /* assign the mask for this irq */
2610 irq_update_affinity_hint(irq_num, &q_vector->affinity_mask);
2611 }
2612
2613 err = ice_set_cpu_rx_rmap(vsi);
2614 if (err) {
2615 netdev_err(vsi->netdev, "Failed to setup CPU RMAP on VSI %u: %pe\n",
2616 vsi->vsi_num, ERR_PTR(err));
2617 goto free_q_irqs;
2618 }
2619
2620 vsi->irqs_ready = true;
2621 return 0;
2622
2623free_q_irqs:
2624 while (vector--) {
2625 irq_num = vsi->q_vectors[vector]->irq.virq;
2626 if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2627 irq_set_affinity_notifier(irq_num, NULL);
2628 irq_update_affinity_hint(irq_num, NULL);
2629 devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
2630 }
2631 return err;
2632}
2633
2634/**
2635 * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2636 * @vsi: VSI to setup Tx rings used by XDP
2637 *
2638 * Return 0 on success and negative value on error
2639 */
2640static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2641{
2642 struct device *dev = ice_pf_to_dev(vsi->back);
2643 struct ice_tx_desc *tx_desc;
2644 int i, j;
2645
2646 ice_for_each_xdp_txq(vsi, i) {
2647 u16 xdp_q_idx = vsi->alloc_txq + i;
2648 struct ice_ring_stats *ring_stats;
2649 struct ice_tx_ring *xdp_ring;
2650
2651 xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
2652 if (!xdp_ring)
2653 goto free_xdp_rings;
2654
2655 ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
2656 if (!ring_stats) {
2657 ice_free_tx_ring(xdp_ring);
2658 goto free_xdp_rings;
2659 }
2660
2661 xdp_ring->ring_stats = ring_stats;
2662 xdp_ring->q_index = xdp_q_idx;
2663 xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
2664 xdp_ring->vsi = vsi;
2665 xdp_ring->netdev = NULL;
2666 xdp_ring->dev = dev;
2667 xdp_ring->count = vsi->num_tx_desc;
2668 WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
2669 if (ice_setup_tx_ring(xdp_ring))
2670 goto free_xdp_rings;
2671 ice_set_ring_xdp(xdp_ring);
2672 spin_lock_init(&xdp_ring->tx_lock);
2673 for (j = 0; j < xdp_ring->count; j++) {
2674 tx_desc = ICE_TX_DESC(xdp_ring, j);
2675 tx_desc->cmd_type_offset_bsz = 0;
2676 }
2677 }
2678
2679 return 0;
2680
2681free_xdp_rings:
2682 for (; i >= 0; i--) {
2683 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc) {
2684 kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2685 vsi->xdp_rings[i]->ring_stats = NULL;
2686 ice_free_tx_ring(vsi->xdp_rings[i]);
2687 }
2688 }
2689 return -ENOMEM;
2690}
2691
2692/**
2693 * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2694 * @vsi: VSI to set the bpf prog on
2695 * @prog: the bpf prog pointer
2696 */
2697static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2698{
2699 struct bpf_prog *old_prog;
2700 int i;
2701
2702 old_prog = xchg(&vsi->xdp_prog, prog);
2703 ice_for_each_rxq(vsi, i)
2704 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
2705
2706 if (old_prog)
2707 bpf_prog_put(old_prog);
2708}
2709
2710static struct ice_tx_ring *ice_xdp_ring_from_qid(struct ice_vsi *vsi, int qid)
2711{
2712 struct ice_q_vector *q_vector;
2713 struct ice_tx_ring *ring;
2714
2715 if (static_key_enabled(&ice_xdp_locking_key))
2716 return vsi->xdp_rings[qid % vsi->num_xdp_txq];
2717
2718 q_vector = vsi->rx_rings[qid]->q_vector;
2719 ice_for_each_tx_ring(ring, q_vector->tx)
2720 if (ice_ring_is_xdp(ring))
2721 return ring;
2722
2723 return NULL;
2724}
2725
2726/**
2727 * ice_map_xdp_rings - Map XDP rings to interrupt vectors
2728 * @vsi: the VSI with XDP rings being configured
2729 *
2730 * Map XDP rings to interrupt vectors and perform the configuration steps
2731 * dependent on the mapping.
2732 */
2733void ice_map_xdp_rings(struct ice_vsi *vsi)
2734{
2735 int xdp_rings_rem = vsi->num_xdp_txq;
2736 int v_idx, q_idx;
2737
2738 /* follow the logic from ice_vsi_map_rings_to_vectors */
2739 ice_for_each_q_vector(vsi, v_idx) {
2740 struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2741 int xdp_rings_per_v, q_id, q_base;
2742
2743 xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2744 vsi->num_q_vectors - v_idx);
2745 q_base = vsi->num_xdp_txq - xdp_rings_rem;
2746
2747 for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
2748 struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_id];
2749
2750 xdp_ring->q_vector = q_vector;
2751 xdp_ring->next = q_vector->tx.tx_ring;
2752 q_vector->tx.tx_ring = xdp_ring;
2753 }
2754 xdp_rings_rem -= xdp_rings_per_v;
2755 }
2756
2757 ice_for_each_rxq(vsi, q_idx) {
2758 vsi->rx_rings[q_idx]->xdp_ring = ice_xdp_ring_from_qid(vsi,
2759 q_idx);
2760 ice_tx_xsk_pool(vsi, q_idx);
2761 }
2762}
2763
2764/**
2765 * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2766 * @vsi: VSI to bring up Tx rings used by XDP
2767 * @prog: bpf program that will be assigned to VSI
2768 * @cfg_type: create from scratch or restore the existing configuration
2769 *
2770 * Return 0 on success and negative value on error
2771 */
2772int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog,
2773 enum ice_xdp_cfg cfg_type)
2774{
2775 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2776 struct ice_pf *pf = vsi->back;
2777 struct ice_qs_cfg xdp_qs_cfg = {
2778 .qs_mutex = &pf->avail_q_mutex,
2779 .pf_map = pf->avail_txqs,
2780 .pf_map_size = pf->max_pf_txqs,
2781 .q_count = vsi->num_xdp_txq,
2782 .scatter_count = ICE_MAX_SCATTER_TXQS,
2783 .vsi_map = vsi->txq_map,
2784 .vsi_map_offset = vsi->alloc_txq,
2785 .mapping_mode = ICE_VSI_MAP_CONTIG
2786 };
2787 struct device *dev;
2788 int status, i;
2789
2790 dev = ice_pf_to_dev(pf);
2791 vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2792 sizeof(*vsi->xdp_rings), GFP_KERNEL);
2793 if (!vsi->xdp_rings)
2794 return -ENOMEM;
2795
2796 vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2797 if (__ice_vsi_get_qs(&xdp_qs_cfg))
2798 goto err_map_xdp;
2799
2800 if (static_key_enabled(&ice_xdp_locking_key))
2801 netdev_warn(vsi->netdev,
2802 "Could not allocate one XDP Tx ring per CPU, XDP_TX/XDP_REDIRECT actions will be slower\n");
2803
2804 if (ice_xdp_alloc_setup_rings(vsi))
2805 goto clear_xdp_rings;
2806
2807 /* omit the scheduler update if in reset path; XDP queues will be
2808 * taken into account at the end of ice_vsi_rebuild, where
2809 * ice_cfg_vsi_lan is being called
2810 */
2811 if (cfg_type == ICE_XDP_CFG_PART)
2812 return 0;
2813
2814 ice_map_xdp_rings(vsi);
2815
2816 /* tell the Tx scheduler that right now we have
2817 * additional queues
2818 */
2819 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2820 max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2821
2822 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2823 max_txqs);
2824 if (status) {
2825 dev_err(dev, "Failed VSI LAN queue config for XDP, error: %d\n",
2826 status);
2827 goto clear_xdp_rings;
2828 }
2829
2830 /* assign the prog only when it's not already present on VSI;
2831 * this flow is a subject of both ethtool -L and ndo_bpf flows;
2832 * VSI rebuild that happens under ethtool -L can expose us to
2833 * the bpf_prog refcount issues as we would be swapping same
2834 * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
2835 * on it as it would be treated as an 'old_prog'; for ndo_bpf
2836 * this is not harmful as dev_xdp_install bumps the refcount
2837 * before calling the op exposed by the driver;
2838 */
2839 if (!ice_is_xdp_ena_vsi(vsi))
2840 ice_vsi_assign_bpf_prog(vsi, prog);
2841
2842 return 0;
2843clear_xdp_rings:
2844 ice_for_each_xdp_txq(vsi, i)
2845 if (vsi->xdp_rings[i]) {
2846 kfree_rcu(vsi->xdp_rings[i], rcu);
2847 vsi->xdp_rings[i] = NULL;
2848 }
2849
2850err_map_xdp:
2851 mutex_lock(&pf->avail_q_mutex);
2852 ice_for_each_xdp_txq(vsi, i) {
2853 clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2854 vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2855 }
2856 mutex_unlock(&pf->avail_q_mutex);
2857
2858 devm_kfree(dev, vsi->xdp_rings);
2859 return -ENOMEM;
2860}
2861
2862/**
2863 * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2864 * @vsi: VSI to remove XDP rings
2865 * @cfg_type: disable XDP permanently or allow it to be restored later
2866 *
2867 * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2868 * resources
2869 */
2870int ice_destroy_xdp_rings(struct ice_vsi *vsi, enum ice_xdp_cfg cfg_type)
2871{
2872 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2873 struct ice_pf *pf = vsi->back;
2874 int i, v_idx;
2875
2876 /* q_vectors are freed in reset path so there's no point in detaching
2877 * rings
2878 */
2879 if (cfg_type == ICE_XDP_CFG_PART)
2880 goto free_qmap;
2881
2882 ice_for_each_q_vector(vsi, v_idx) {
2883 struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2884 struct ice_tx_ring *ring;
2885
2886 ice_for_each_tx_ring(ring, q_vector->tx)
2887 if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2888 break;
2889
2890 /* restore the value of last node prior to XDP setup */
2891 q_vector->tx.tx_ring = ring;
2892 }
2893
2894free_qmap:
2895 mutex_lock(&pf->avail_q_mutex);
2896 ice_for_each_xdp_txq(vsi, i) {
2897 clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2898 vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2899 }
2900 mutex_unlock(&pf->avail_q_mutex);
2901
2902 ice_for_each_xdp_txq(vsi, i)
2903 if (vsi->xdp_rings[i]) {
2904 if (vsi->xdp_rings[i]->desc) {
2905 synchronize_rcu();
2906 ice_free_tx_ring(vsi->xdp_rings[i]);
2907 }
2908 kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2909 vsi->xdp_rings[i]->ring_stats = NULL;
2910 kfree_rcu(vsi->xdp_rings[i], rcu);
2911 vsi->xdp_rings[i] = NULL;
2912 }
2913
2914 devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2915 vsi->xdp_rings = NULL;
2916
2917 if (static_key_enabled(&ice_xdp_locking_key))
2918 static_branch_dec(&ice_xdp_locking_key);
2919
2920 if (cfg_type == ICE_XDP_CFG_PART)
2921 return 0;
2922
2923 ice_vsi_assign_bpf_prog(vsi, NULL);
2924
2925 /* notify Tx scheduler that we destroyed XDP queues and bring
2926 * back the old number of child nodes
2927 */
2928 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2929 max_txqs[i] = vsi->num_txq;
2930
2931 /* change number of XDP Tx queues to 0 */
2932 vsi->num_xdp_txq = 0;
2933
2934 return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2935 max_txqs);
2936}
2937
2938/**
2939 * ice_vsi_rx_napi_schedule - Schedule napi on RX queues from VSI
2940 * @vsi: VSI to schedule napi on
2941 */
2942static void ice_vsi_rx_napi_schedule(struct ice_vsi *vsi)
2943{
2944 int i;
2945
2946 ice_for_each_rxq(vsi, i) {
2947 struct ice_rx_ring *rx_ring = vsi->rx_rings[i];
2948
2949 if (READ_ONCE(rx_ring->xsk_pool))
2950 napi_schedule(&rx_ring->q_vector->napi);
2951 }
2952}
2953
2954/**
2955 * ice_vsi_determine_xdp_res - figure out how many Tx qs can XDP have
2956 * @vsi: VSI to determine the count of XDP Tx qs
2957 *
2958 * returns 0 if Tx qs count is higher than at least half of CPU count,
2959 * -ENOMEM otherwise
2960 */
2961int ice_vsi_determine_xdp_res(struct ice_vsi *vsi)
2962{
2963 u16 avail = ice_get_avail_txq_count(vsi->back);
2964 u16 cpus = num_possible_cpus();
2965
2966 if (avail < cpus / 2)
2967 return -ENOMEM;
2968
2969 if (vsi->type == ICE_VSI_SF)
2970 avail = vsi->alloc_txq;
2971
2972 vsi->num_xdp_txq = min_t(u16, avail, cpus);
2973
2974 if (vsi->num_xdp_txq < cpus)
2975 static_branch_inc(&ice_xdp_locking_key);
2976
2977 return 0;
2978}
2979
2980/**
2981 * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2982 * @vsi: Pointer to VSI structure
2983 */
2984static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
2985{
2986 if (test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
2987 return ICE_RXBUF_1664;
2988 else
2989 return ICE_RXBUF_3072;
2990}
2991
2992/**
2993 * ice_xdp_setup_prog - Add or remove XDP eBPF program
2994 * @vsi: VSI to setup XDP for
2995 * @prog: XDP program
2996 * @extack: netlink extended ack
2997 */
2998static int
2999ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
3000 struct netlink_ext_ack *extack)
3001{
3002 unsigned int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
3003 int ret = 0, xdp_ring_err = 0;
3004 bool if_running;
3005
3006 if (prog && !prog->aux->xdp_has_frags) {
3007 if (frame_size > ice_max_xdp_frame_size(vsi)) {
3008 NL_SET_ERR_MSG_MOD(extack,
3009 "MTU is too large for linear frames and XDP prog does not support frags");
3010 return -EOPNOTSUPP;
3011 }
3012 }
3013
3014 /* hot swap progs and avoid toggling link */
3015 if (ice_is_xdp_ena_vsi(vsi) == !!prog ||
3016 test_bit(ICE_VSI_REBUILD_PENDING, vsi->state)) {
3017 ice_vsi_assign_bpf_prog(vsi, prog);
3018 return 0;
3019 }
3020
3021 if_running = netif_running(vsi->netdev) &&
3022 !test_and_set_bit(ICE_VSI_DOWN, vsi->state);
3023
3024 /* need to stop netdev while setting up the program for Rx rings */
3025 if (if_running) {
3026 ret = ice_down(vsi);
3027 if (ret) {
3028 NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
3029 return ret;
3030 }
3031 }
3032
3033 if (!ice_is_xdp_ena_vsi(vsi) && prog) {
3034 xdp_ring_err = ice_vsi_determine_xdp_res(vsi);
3035 if (xdp_ring_err) {
3036 NL_SET_ERR_MSG_MOD(extack, "Not enough Tx resources for XDP");
3037 } else {
3038 xdp_ring_err = ice_prepare_xdp_rings(vsi, prog,
3039 ICE_XDP_CFG_FULL);
3040 if (xdp_ring_err)
3041 NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
3042 }
3043 xdp_features_set_redirect_target(vsi->netdev, true);
3044 /* reallocate Rx queues that are used for zero-copy */
3045 xdp_ring_err = ice_realloc_zc_buf(vsi, true);
3046 if (xdp_ring_err)
3047 NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Rx resources failed");
3048 } else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
3049 xdp_features_clear_redirect_target(vsi->netdev);
3050 xdp_ring_err = ice_destroy_xdp_rings(vsi, ICE_XDP_CFG_FULL);
3051 if (xdp_ring_err)
3052 NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
3053 /* reallocate Rx queues that were used for zero-copy */
3054 xdp_ring_err = ice_realloc_zc_buf(vsi, false);
3055 if (xdp_ring_err)
3056 NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Rx resources failed");
3057 }
3058
3059 if (if_running)
3060 ret = ice_up(vsi);
3061
3062 if (!ret && prog)
3063 ice_vsi_rx_napi_schedule(vsi);
3064
3065 return (ret || xdp_ring_err) ? -ENOMEM : 0;
3066}
3067
3068/**
3069 * ice_xdp_safe_mode - XDP handler for safe mode
3070 * @dev: netdevice
3071 * @xdp: XDP command
3072 */
3073static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
3074 struct netdev_bpf *xdp)
3075{
3076 NL_SET_ERR_MSG_MOD(xdp->extack,
3077 "Please provide working DDP firmware package in order to use XDP\n"
3078 "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
3079 return -EOPNOTSUPP;
3080}
3081
3082/**
3083 * ice_xdp - implements XDP handler
3084 * @dev: netdevice
3085 * @xdp: XDP command
3086 */
3087int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3088{
3089 struct ice_netdev_priv *np = netdev_priv(dev);
3090 struct ice_vsi *vsi = np->vsi;
3091 int ret;
3092
3093 if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_SF) {
3094 NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF or SF VSI");
3095 return -EINVAL;
3096 }
3097
3098 mutex_lock(&vsi->xdp_state_lock);
3099
3100 switch (xdp->command) {
3101 case XDP_SETUP_PROG:
3102 ret = ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
3103 break;
3104 case XDP_SETUP_XSK_POOL:
3105 ret = ice_xsk_pool_setup(vsi, xdp->xsk.pool, xdp->xsk.queue_id);
3106 break;
3107 default:
3108 ret = -EINVAL;
3109 }
3110
3111 mutex_unlock(&vsi->xdp_state_lock);
3112 return ret;
3113}
3114
3115/**
3116 * ice_ena_misc_vector - enable the non-queue interrupts
3117 * @pf: board private structure
3118 */
3119static void ice_ena_misc_vector(struct ice_pf *pf)
3120{
3121 struct ice_hw *hw = &pf->hw;
3122 u32 pf_intr_start_offset;
3123 u32 val;
3124
3125 /* Disable anti-spoof detection interrupt to prevent spurious event
3126 * interrupts during a function reset. Anti-spoof functionally is
3127 * still supported.
3128 */
3129 val = rd32(hw, GL_MDCK_TX_TDPU);
3130 val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
3131 wr32(hw, GL_MDCK_TX_TDPU, val);
3132
3133 /* clear things first */
3134 wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
3135 rd32(hw, PFINT_OICR); /* read to clear */
3136
3137 val = (PFINT_OICR_ECC_ERR_M |
3138 PFINT_OICR_MAL_DETECT_M |
3139 PFINT_OICR_GRST_M |
3140 PFINT_OICR_PCI_EXCEPTION_M |
3141 PFINT_OICR_VFLR_M |
3142 PFINT_OICR_HMC_ERR_M |
3143 PFINT_OICR_PE_PUSH_M |
3144 PFINT_OICR_PE_CRITERR_M);
3145
3146 wr32(hw, PFINT_OICR_ENA, val);
3147
3148 /* SW_ITR_IDX = 0, but don't change INTENA */
3149 wr32(hw, GLINT_DYN_CTL(pf->oicr_irq.index),
3150 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
3151
3152 if (!pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3153 return;
3154 pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3155 wr32(hw, GLINT_DYN_CTL(pf->ll_ts_irq.index + pf_intr_start_offset),
3156 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
3157}
3158
3159/**
3160 * ice_ll_ts_intr - ll_ts interrupt handler
3161 * @irq: interrupt number
3162 * @data: pointer to a q_vector
3163 */
3164static irqreturn_t ice_ll_ts_intr(int __always_unused irq, void *data)
3165{
3166 struct ice_pf *pf = data;
3167 u32 pf_intr_start_offset;
3168 struct ice_ptp_tx *tx;
3169 unsigned long flags;
3170 struct ice_hw *hw;
3171 u32 val;
3172 u8 idx;
3173
3174 hw = &pf->hw;
3175 tx = &pf->ptp.port.tx;
3176 spin_lock_irqsave(&tx->lock, flags);
3177 ice_ptp_complete_tx_single_tstamp(tx);
3178
3179 idx = find_next_bit_wrap(tx->in_use, tx->len,
3180 tx->last_ll_ts_idx_read + 1);
3181 if (idx != tx->len)
3182 ice_ptp_req_tx_single_tstamp(tx, idx);
3183 spin_unlock_irqrestore(&tx->lock, flags);
3184
3185 val = GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M |
3186 (ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
3187 pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3188 wr32(hw, GLINT_DYN_CTL(pf->ll_ts_irq.index + pf_intr_start_offset),
3189 val);
3190
3191 return IRQ_HANDLED;
3192}
3193
3194/**
3195 * ice_misc_intr - misc interrupt handler
3196 * @irq: interrupt number
3197 * @data: pointer to a q_vector
3198 */
3199static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
3200{
3201 struct ice_pf *pf = (struct ice_pf *)data;
3202 irqreturn_t ret = IRQ_HANDLED;
3203 struct ice_hw *hw = &pf->hw;
3204 struct device *dev;
3205 u32 oicr, ena_mask;
3206
3207 dev = ice_pf_to_dev(pf);
3208 set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
3209 set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
3210 set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
3211
3212 oicr = rd32(hw, PFINT_OICR);
3213 ena_mask = rd32(hw, PFINT_OICR_ENA);
3214
3215 if (oicr & PFINT_OICR_SWINT_M) {
3216 ena_mask &= ~PFINT_OICR_SWINT_M;
3217 pf->sw_int_count++;
3218 }
3219
3220 if (oicr & PFINT_OICR_MAL_DETECT_M) {
3221 ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
3222 set_bit(ICE_MDD_EVENT_PENDING, pf->state);
3223 }
3224 if (oicr & PFINT_OICR_VFLR_M) {
3225 /* disable any further VFLR event notifications */
3226 if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
3227 u32 reg = rd32(hw, PFINT_OICR_ENA);
3228
3229 reg &= ~PFINT_OICR_VFLR_M;
3230 wr32(hw, PFINT_OICR_ENA, reg);
3231 } else {
3232 ena_mask &= ~PFINT_OICR_VFLR_M;
3233 set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
3234 }
3235 }
3236
3237 if (oicr & PFINT_OICR_GRST_M) {
3238 u32 reset;
3239
3240 /* we have a reset warning */
3241 ena_mask &= ~PFINT_OICR_GRST_M;
3242 reset = FIELD_GET(GLGEN_RSTAT_RESET_TYPE_M,
3243 rd32(hw, GLGEN_RSTAT));
3244
3245 if (reset == ICE_RESET_CORER)
3246 pf->corer_count++;
3247 else if (reset == ICE_RESET_GLOBR)
3248 pf->globr_count++;
3249 else if (reset == ICE_RESET_EMPR)
3250 pf->empr_count++;
3251 else
3252 dev_dbg(dev, "Invalid reset type %d\n", reset);
3253
3254 /* If a reset cycle isn't already in progress, we set a bit in
3255 * pf->state so that the service task can start a reset/rebuild.
3256 */
3257 if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
3258 if (reset == ICE_RESET_CORER)
3259 set_bit(ICE_CORER_RECV, pf->state);
3260 else if (reset == ICE_RESET_GLOBR)
3261 set_bit(ICE_GLOBR_RECV, pf->state);
3262 else
3263 set_bit(ICE_EMPR_RECV, pf->state);
3264
3265 /* There are couple of different bits at play here.
3266 * hw->reset_ongoing indicates whether the hardware is
3267 * in reset. This is set to true when a reset interrupt
3268 * is received and set back to false after the driver
3269 * has determined that the hardware is out of reset.
3270 *
3271 * ICE_RESET_OICR_RECV in pf->state indicates
3272 * that a post reset rebuild is required before the
3273 * driver is operational again. This is set above.
3274 *
3275 * As this is the start of the reset/rebuild cycle, set
3276 * both to indicate that.
3277 */
3278 hw->reset_ongoing = true;
3279 }
3280 }
3281
3282 if (oicr & PFINT_OICR_TSYN_TX_M) {
3283 ena_mask &= ~PFINT_OICR_TSYN_TX_M;
3284 if (ice_pf_state_is_nominal(pf) &&
3285 pf->hw.dev_caps.ts_dev_info.ts_ll_int_read) {
3286 struct ice_ptp_tx *tx = &pf->ptp.port.tx;
3287 unsigned long flags;
3288 u8 idx;
3289
3290 spin_lock_irqsave(&tx->lock, flags);
3291 idx = find_next_bit_wrap(tx->in_use, tx->len,
3292 tx->last_ll_ts_idx_read + 1);
3293 if (idx != tx->len)
3294 ice_ptp_req_tx_single_tstamp(tx, idx);
3295 spin_unlock_irqrestore(&tx->lock, flags);
3296 } else if (ice_ptp_pf_handles_tx_interrupt(pf)) {
3297 set_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread);
3298 ret = IRQ_WAKE_THREAD;
3299 }
3300 }
3301
3302 if (oicr & PFINT_OICR_TSYN_EVNT_M) {
3303 u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3304 u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
3305
3306 ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
3307
3308 if (ice_pf_src_tmr_owned(pf)) {
3309 /* Save EVENTs from GLTSYN register */
3310 pf->ptp.ext_ts_irq |= gltsyn_stat &
3311 (GLTSYN_STAT_EVENT0_M |
3312 GLTSYN_STAT_EVENT1_M |
3313 GLTSYN_STAT_EVENT2_M);
3314
3315 ice_ptp_extts_event(pf);
3316 }
3317 }
3318
3319#define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
3320 if (oicr & ICE_AUX_CRIT_ERR) {
3321 pf->oicr_err_reg |= oicr;
3322 set_bit(ICE_AUX_ERR_PENDING, pf->state);
3323 ena_mask &= ~ICE_AUX_CRIT_ERR;
3324 }
3325
3326 /* Report any remaining unexpected interrupts */
3327 oicr &= ena_mask;
3328 if (oicr) {
3329 dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
3330 /* If a critical error is pending there is no choice but to
3331 * reset the device.
3332 */
3333 if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
3334 PFINT_OICR_ECC_ERR_M)) {
3335 set_bit(ICE_PFR_REQ, pf->state);
3336 }
3337 }
3338 ice_service_task_schedule(pf);
3339 if (ret == IRQ_HANDLED)
3340 ice_irq_dynamic_ena(hw, NULL, NULL);
3341
3342 return ret;
3343}
3344
3345/**
3346 * ice_misc_intr_thread_fn - misc interrupt thread function
3347 * @irq: interrupt number
3348 * @data: pointer to a q_vector
3349 */
3350static irqreturn_t ice_misc_intr_thread_fn(int __always_unused irq, void *data)
3351{
3352 struct ice_pf *pf = data;
3353 struct ice_hw *hw;
3354
3355 hw = &pf->hw;
3356
3357 if (ice_is_reset_in_progress(pf->state))
3358 goto skip_irq;
3359
3360 if (test_and_clear_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread)) {
3361 /* Process outstanding Tx timestamps. If there is more work,
3362 * re-arm the interrupt to trigger again.
3363 */
3364 if (ice_ptp_process_ts(pf) == ICE_TX_TSTAMP_WORK_PENDING) {
3365 wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
3366 ice_flush(hw);
3367 }
3368 }
3369
3370skip_irq:
3371 ice_irq_dynamic_ena(hw, NULL, NULL);
3372
3373 return IRQ_HANDLED;
3374}
3375
3376/**
3377 * ice_dis_ctrlq_interrupts - disable control queue interrupts
3378 * @hw: pointer to HW structure
3379 */
3380static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
3381{
3382 /* disable Admin queue Interrupt causes */
3383 wr32(hw, PFINT_FW_CTL,
3384 rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
3385
3386 /* disable Mailbox queue Interrupt causes */
3387 wr32(hw, PFINT_MBX_CTL,
3388 rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
3389
3390 wr32(hw, PFINT_SB_CTL,
3391 rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
3392
3393 /* disable Control queue Interrupt causes */
3394 wr32(hw, PFINT_OICR_CTL,
3395 rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
3396
3397 ice_flush(hw);
3398}
3399
3400/**
3401 * ice_free_irq_msix_ll_ts- Unroll ll_ts vector setup
3402 * @pf: board private structure
3403 */
3404static void ice_free_irq_msix_ll_ts(struct ice_pf *pf)
3405{
3406 int irq_num = pf->ll_ts_irq.virq;
3407
3408 synchronize_irq(irq_num);
3409 devm_free_irq(ice_pf_to_dev(pf), irq_num, pf);
3410
3411 ice_free_irq(pf, pf->ll_ts_irq);
3412}
3413
3414/**
3415 * ice_free_irq_msix_misc - Unroll misc vector setup
3416 * @pf: board private structure
3417 */
3418static void ice_free_irq_msix_misc(struct ice_pf *pf)
3419{
3420 int misc_irq_num = pf->oicr_irq.virq;
3421 struct ice_hw *hw = &pf->hw;
3422
3423 ice_dis_ctrlq_interrupts(hw);
3424
3425 /* disable OICR interrupt */
3426 wr32(hw, PFINT_OICR_ENA, 0);
3427 ice_flush(hw);
3428
3429 synchronize_irq(misc_irq_num);
3430 devm_free_irq(ice_pf_to_dev(pf), misc_irq_num, pf);
3431
3432 ice_free_irq(pf, pf->oicr_irq);
3433 if (pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3434 ice_free_irq_msix_ll_ts(pf);
3435}
3436
3437/**
3438 * ice_ena_ctrlq_interrupts - enable control queue interrupts
3439 * @hw: pointer to HW structure
3440 * @reg_idx: HW vector index to associate the control queue interrupts with
3441 */
3442static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
3443{
3444 u32 val;
3445
3446 val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
3447 PFINT_OICR_CTL_CAUSE_ENA_M);
3448 wr32(hw, PFINT_OICR_CTL, val);
3449
3450 /* enable Admin queue Interrupt causes */
3451 val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
3452 PFINT_FW_CTL_CAUSE_ENA_M);
3453 wr32(hw, PFINT_FW_CTL, val);
3454
3455 /* enable Mailbox queue Interrupt causes */
3456 val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
3457 PFINT_MBX_CTL_CAUSE_ENA_M);
3458 wr32(hw, PFINT_MBX_CTL, val);
3459
3460 if (!hw->dev_caps.ts_dev_info.ts_ll_int_read) {
3461 /* enable Sideband queue Interrupt causes */
3462 val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
3463 PFINT_SB_CTL_CAUSE_ENA_M);
3464 wr32(hw, PFINT_SB_CTL, val);
3465 }
3466
3467 ice_flush(hw);
3468}
3469
3470/**
3471 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
3472 * @pf: board private structure
3473 *
3474 * This sets up the handler for MSIX 0, which is used to manage the
3475 * non-queue interrupts, e.g. AdminQ and errors. This is not used
3476 * when in MSI or Legacy interrupt mode.
3477 */
3478static int ice_req_irq_msix_misc(struct ice_pf *pf)
3479{
3480 struct device *dev = ice_pf_to_dev(pf);
3481 struct ice_hw *hw = &pf->hw;
3482 u32 pf_intr_start_offset;
3483 struct msi_map irq;
3484 int err = 0;
3485
3486 if (!pf->int_name[0])
3487 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
3488 dev_driver_string(dev), dev_name(dev));
3489
3490 if (!pf->int_name_ll_ts[0])
3491 snprintf(pf->int_name_ll_ts, sizeof(pf->int_name_ll_ts) - 1,
3492 "%s-%s:ll_ts", dev_driver_string(dev), dev_name(dev));
3493 /* Do not request IRQ but do enable OICR interrupt since settings are
3494 * lost during reset. Note that this function is called only during
3495 * rebuild path and not while reset is in progress.
3496 */
3497 if (ice_is_reset_in_progress(pf->state))
3498 goto skip_req_irq;
3499
3500 /* reserve one vector in irq_tracker for misc interrupts */
3501 irq = ice_alloc_irq(pf, false);
3502 if (irq.index < 0)
3503 return irq.index;
3504
3505 pf->oicr_irq = irq;
3506 err = devm_request_threaded_irq(dev, pf->oicr_irq.virq, ice_misc_intr,
3507 ice_misc_intr_thread_fn, 0,
3508 pf->int_name, pf);
3509 if (err) {
3510 dev_err(dev, "devm_request_threaded_irq for %s failed: %d\n",
3511 pf->int_name, err);
3512 ice_free_irq(pf, pf->oicr_irq);
3513 return err;
3514 }
3515
3516 /* reserve one vector in irq_tracker for ll_ts interrupt */
3517 if (!pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3518 goto skip_req_irq;
3519
3520 irq = ice_alloc_irq(pf, false);
3521 if (irq.index < 0)
3522 return irq.index;
3523
3524 pf->ll_ts_irq = irq;
3525 err = devm_request_irq(dev, pf->ll_ts_irq.virq, ice_ll_ts_intr, 0,
3526 pf->int_name_ll_ts, pf);
3527 if (err) {
3528 dev_err(dev, "devm_request_irq for %s failed: %d\n",
3529 pf->int_name_ll_ts, err);
3530 ice_free_irq(pf, pf->ll_ts_irq);
3531 return err;
3532 }
3533
3534skip_req_irq:
3535 ice_ena_misc_vector(pf);
3536
3537 ice_ena_ctrlq_interrupts(hw, pf->oicr_irq.index);
3538 /* This enables LL TS interrupt */
3539 pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3540 if (pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3541 wr32(hw, PFINT_SB_CTL,
3542 ((pf->ll_ts_irq.index + pf_intr_start_offset) &
3543 PFINT_SB_CTL_MSIX_INDX_M) | PFINT_SB_CTL_CAUSE_ENA_M);
3544 wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_irq.index),
3545 ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
3546
3547 ice_flush(hw);
3548 ice_irq_dynamic_ena(hw, NULL, NULL);
3549
3550 return 0;
3551}
3552
3553/**
3554 * ice_set_ops - set netdev and ethtools ops for the given netdev
3555 * @vsi: the VSI associated with the new netdev
3556 */
3557static void ice_set_ops(struct ice_vsi *vsi)
3558{
3559 struct net_device *netdev = vsi->netdev;
3560 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3561
3562 if (ice_is_safe_mode(pf)) {
3563 netdev->netdev_ops = &ice_netdev_safe_mode_ops;
3564 ice_set_ethtool_safe_mode_ops(netdev);
3565 return;
3566 }
3567
3568 netdev->netdev_ops = &ice_netdev_ops;
3569 netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
3570 netdev->xdp_metadata_ops = &ice_xdp_md_ops;
3571 ice_set_ethtool_ops(netdev);
3572
3573 if (vsi->type != ICE_VSI_PF)
3574 return;
3575
3576 netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
3577 NETDEV_XDP_ACT_XSK_ZEROCOPY |
3578 NETDEV_XDP_ACT_RX_SG;
3579 netdev->xdp_zc_max_segs = ICE_MAX_BUF_TXD;
3580}
3581
3582/**
3583 * ice_set_netdev_features - set features for the given netdev
3584 * @netdev: netdev instance
3585 */
3586void ice_set_netdev_features(struct net_device *netdev)
3587{
3588 struct ice_pf *pf = ice_netdev_to_pf(netdev);
3589 bool is_dvm_ena = ice_is_dvm_ena(&pf->hw);
3590 netdev_features_t csumo_features;
3591 netdev_features_t vlano_features;
3592 netdev_features_t dflt_features;
3593 netdev_features_t tso_features;
3594
3595 if (ice_is_safe_mode(pf)) {
3596 /* safe mode */
3597 netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
3598 netdev->hw_features = netdev->features;
3599 return;
3600 }
3601
3602 dflt_features = NETIF_F_SG |
3603 NETIF_F_HIGHDMA |
3604 NETIF_F_NTUPLE |
3605 NETIF_F_RXHASH;
3606
3607 csumo_features = NETIF_F_RXCSUM |
3608 NETIF_F_IP_CSUM |
3609 NETIF_F_SCTP_CRC |
3610 NETIF_F_IPV6_CSUM;
3611
3612 vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
3613 NETIF_F_HW_VLAN_CTAG_TX |
3614 NETIF_F_HW_VLAN_CTAG_RX;
3615
3616 /* Enable CTAG/STAG filtering by default in Double VLAN Mode (DVM) */
3617 if (is_dvm_ena)
3618 vlano_features |= NETIF_F_HW_VLAN_STAG_FILTER;
3619
3620 tso_features = NETIF_F_TSO |
3621 NETIF_F_TSO_ECN |
3622 NETIF_F_TSO6 |
3623 NETIF_F_GSO_GRE |
3624 NETIF_F_GSO_UDP_TUNNEL |
3625 NETIF_F_GSO_GRE_CSUM |
3626 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3627 NETIF_F_GSO_PARTIAL |
3628 NETIF_F_GSO_IPXIP4 |
3629 NETIF_F_GSO_IPXIP6 |
3630 NETIF_F_GSO_UDP_L4;
3631
3632 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3633 NETIF_F_GSO_GRE_CSUM;
3634 /* set features that user can change */
3635 netdev->hw_features = dflt_features | csumo_features |
3636 vlano_features | tso_features;
3637
3638 /* add support for HW_CSUM on packets with MPLS header */
3639 netdev->mpls_features = NETIF_F_HW_CSUM |
3640 NETIF_F_TSO |
3641 NETIF_F_TSO6;
3642
3643 /* enable features */
3644 netdev->features |= netdev->hw_features;
3645
3646 netdev->hw_features |= NETIF_F_HW_TC;
3647 netdev->hw_features |= NETIF_F_LOOPBACK;
3648
3649 /* encap and VLAN devices inherit default, csumo and tso features */
3650 netdev->hw_enc_features |= dflt_features | csumo_features |
3651 tso_features;
3652 netdev->vlan_features |= dflt_features | csumo_features |
3653 tso_features;
3654
3655 /* advertise support but don't enable by default since only one type of
3656 * VLAN offload can be enabled at a time (i.e. CTAG or STAG). When one
3657 * type turns on the other has to be turned off. This is enforced by the
3658 * ice_fix_features() ndo callback.
3659 */
3660 if (is_dvm_ena)
3661 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX |
3662 NETIF_F_HW_VLAN_STAG_TX;
3663
3664 /* Leave CRC / FCS stripping enabled by default, but allow the value to
3665 * be changed at runtime
3666 */
3667 netdev->hw_features |= NETIF_F_RXFCS;
3668
3669 netif_set_tso_max_size(netdev, ICE_MAX_TSO_SIZE);
3670}
3671
3672/**
3673 * ice_fill_rss_lut - Fill the RSS lookup table with default values
3674 * @lut: Lookup table
3675 * @rss_table_size: Lookup table size
3676 * @rss_size: Range of queue number for hashing
3677 */
3678void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3679{
3680 u16 i;
3681
3682 for (i = 0; i < rss_table_size; i++)
3683 lut[i] = i % rss_size;
3684}
3685
3686/**
3687 * ice_pf_vsi_setup - Set up a PF VSI
3688 * @pf: board private structure
3689 * @pi: pointer to the port_info instance
3690 *
3691 * Returns pointer to the successfully allocated VSI software struct
3692 * on success, otherwise returns NULL on failure.
3693 */
3694static struct ice_vsi *
3695ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3696{
3697 struct ice_vsi_cfg_params params = {};
3698
3699 params.type = ICE_VSI_PF;
3700 params.port_info = pi;
3701 params.flags = ICE_VSI_FLAG_INIT;
3702
3703 return ice_vsi_setup(pf, ¶ms);
3704}
3705
3706static struct ice_vsi *
3707ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3708 struct ice_channel *ch)
3709{
3710 struct ice_vsi_cfg_params params = {};
3711
3712 params.type = ICE_VSI_CHNL;
3713 params.port_info = pi;
3714 params.ch = ch;
3715 params.flags = ICE_VSI_FLAG_INIT;
3716
3717 return ice_vsi_setup(pf, ¶ms);
3718}
3719
3720/**
3721 * ice_ctrl_vsi_setup - Set up a control VSI
3722 * @pf: board private structure
3723 * @pi: pointer to the port_info instance
3724 *
3725 * Returns pointer to the successfully allocated VSI software struct
3726 * on success, otherwise returns NULL on failure.
3727 */
3728static struct ice_vsi *
3729ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3730{
3731 struct ice_vsi_cfg_params params = {};
3732
3733 params.type = ICE_VSI_CTRL;
3734 params.port_info = pi;
3735 params.flags = ICE_VSI_FLAG_INIT;
3736
3737 return ice_vsi_setup(pf, ¶ms);
3738}
3739
3740/**
3741 * ice_lb_vsi_setup - Set up a loopback VSI
3742 * @pf: board private structure
3743 * @pi: pointer to the port_info instance
3744 *
3745 * Returns pointer to the successfully allocated VSI software struct
3746 * on success, otherwise returns NULL on failure.
3747 */
3748struct ice_vsi *
3749ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3750{
3751 struct ice_vsi_cfg_params params = {};
3752
3753 params.type = ICE_VSI_LB;
3754 params.port_info = pi;
3755 params.flags = ICE_VSI_FLAG_INIT;
3756
3757 return ice_vsi_setup(pf, ¶ms);
3758}
3759
3760/**
3761 * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3762 * @netdev: network interface to be adjusted
3763 * @proto: VLAN TPID
3764 * @vid: VLAN ID to be added
3765 *
3766 * net_device_ops implementation for adding VLAN IDs
3767 */
3768int ice_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3769{
3770 struct ice_netdev_priv *np = netdev_priv(netdev);
3771 struct ice_vsi_vlan_ops *vlan_ops;
3772 struct ice_vsi *vsi = np->vsi;
3773 struct ice_vlan vlan;
3774 int ret;
3775
3776 /* VLAN 0 is added by default during load/reset */
3777 if (!vid)
3778 return 0;
3779
3780 while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3781 usleep_range(1000, 2000);
3782
3783 /* Add multicast promisc rule for the VLAN ID to be added if
3784 * all-multicast is currently enabled.
3785 */
3786 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3787 ret = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3788 ICE_MCAST_VLAN_PROMISC_BITS,
3789 vid);
3790 if (ret)
3791 goto finish;
3792 }
3793
3794 vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3795
3796 /* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3797 * packets aren't pruned by the device's internal switch on Rx
3798 */
3799 vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3800 ret = vlan_ops->add_vlan(vsi, &vlan);
3801 if (ret)
3802 goto finish;
3803
3804 /* If all-multicast is currently enabled and this VLAN ID is only one
3805 * besides VLAN-0 we have to update look-up type of multicast promisc
3806 * rule for VLAN-0 from ICE_SW_LKUP_PROMISC to ICE_SW_LKUP_PROMISC_VLAN.
3807 */
3808 if ((vsi->current_netdev_flags & IFF_ALLMULTI) &&
3809 ice_vsi_num_non_zero_vlans(vsi) == 1) {
3810 ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3811 ICE_MCAST_PROMISC_BITS, 0);
3812 ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3813 ICE_MCAST_VLAN_PROMISC_BITS, 0);
3814 }
3815
3816finish:
3817 clear_bit(ICE_CFG_BUSY, vsi->state);
3818
3819 return ret;
3820}
3821
3822/**
3823 * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3824 * @netdev: network interface to be adjusted
3825 * @proto: VLAN TPID
3826 * @vid: VLAN ID to be removed
3827 *
3828 * net_device_ops implementation for removing VLAN IDs
3829 */
3830int ice_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3831{
3832 struct ice_netdev_priv *np = netdev_priv(netdev);
3833 struct ice_vsi_vlan_ops *vlan_ops;
3834 struct ice_vsi *vsi = np->vsi;
3835 struct ice_vlan vlan;
3836 int ret;
3837
3838 /* don't allow removal of VLAN 0 */
3839 if (!vid)
3840 return 0;
3841
3842 while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3843 usleep_range(1000, 2000);
3844
3845 ret = ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3846 ICE_MCAST_VLAN_PROMISC_BITS, vid);
3847 if (ret) {
3848 netdev_err(netdev, "Error clearing multicast promiscuous mode on VSI %i\n",
3849 vsi->vsi_num);
3850 vsi->current_netdev_flags |= IFF_ALLMULTI;
3851 }
3852
3853 vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3854
3855 /* Make sure VLAN delete is successful before updating VLAN
3856 * information
3857 */
3858 vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3859 ret = vlan_ops->del_vlan(vsi, &vlan);
3860 if (ret)
3861 goto finish;
3862
3863 /* Remove multicast promisc rule for the removed VLAN ID if
3864 * all-multicast is enabled.
3865 */
3866 if (vsi->current_netdev_flags & IFF_ALLMULTI)
3867 ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3868 ICE_MCAST_VLAN_PROMISC_BITS, vid);
3869
3870 if (!ice_vsi_has_non_zero_vlans(vsi)) {
3871 /* Update look-up type of multicast promisc rule for VLAN 0
3872 * from ICE_SW_LKUP_PROMISC_VLAN to ICE_SW_LKUP_PROMISC when
3873 * all-multicast is enabled and VLAN 0 is the only VLAN rule.
3874 */
3875 if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3876 ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3877 ICE_MCAST_VLAN_PROMISC_BITS,
3878 0);
3879 ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3880 ICE_MCAST_PROMISC_BITS, 0);
3881 }
3882 }
3883
3884finish:
3885 clear_bit(ICE_CFG_BUSY, vsi->state);
3886
3887 return ret;
3888}
3889
3890/**
3891 * ice_rep_indr_tc_block_unbind
3892 * @cb_priv: indirection block private data
3893 */
3894static void ice_rep_indr_tc_block_unbind(void *cb_priv)
3895{
3896 struct ice_indr_block_priv *indr_priv = cb_priv;
3897
3898 list_del(&indr_priv->list);
3899 kfree(indr_priv);
3900}
3901
3902/**
3903 * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
3904 * @vsi: VSI struct which has the netdev
3905 */
3906static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
3907{
3908 struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
3909
3910 flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
3911 ice_rep_indr_tc_block_unbind);
3912}
3913
3914/**
3915 * ice_tc_indir_block_register - Register TC indirect block notifications
3916 * @vsi: VSI struct which has the netdev
3917 *
3918 * Returns 0 on success, negative value on failure
3919 */
3920static int ice_tc_indir_block_register(struct ice_vsi *vsi)
3921{
3922 struct ice_netdev_priv *np;
3923
3924 if (!vsi || !vsi->netdev)
3925 return -EINVAL;
3926
3927 np = netdev_priv(vsi->netdev);
3928
3929 INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
3930 return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
3931}
3932
3933/**
3934 * ice_get_avail_q_count - Get count of queues in use
3935 * @pf_qmap: bitmap to get queue use count from
3936 * @lock: pointer to a mutex that protects access to pf_qmap
3937 * @size: size of the bitmap
3938 */
3939static u16
3940ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3941{
3942 unsigned long bit;
3943 u16 count = 0;
3944
3945 mutex_lock(lock);
3946 for_each_clear_bit(bit, pf_qmap, size)
3947 count++;
3948 mutex_unlock(lock);
3949
3950 return count;
3951}
3952
3953/**
3954 * ice_get_avail_txq_count - Get count of Tx queues in use
3955 * @pf: pointer to an ice_pf instance
3956 */
3957u16 ice_get_avail_txq_count(struct ice_pf *pf)
3958{
3959 return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3960 pf->max_pf_txqs);
3961}
3962
3963/**
3964 * ice_get_avail_rxq_count - Get count of Rx queues in use
3965 * @pf: pointer to an ice_pf instance
3966 */
3967u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3968{
3969 return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3970 pf->max_pf_rxqs);
3971}
3972
3973/**
3974 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3975 * @pf: board private structure to initialize
3976 */
3977static void ice_deinit_pf(struct ice_pf *pf)
3978{
3979 ice_service_task_stop(pf);
3980 mutex_destroy(&pf->lag_mutex);
3981 mutex_destroy(&pf->adev_mutex);
3982 mutex_destroy(&pf->sw_mutex);
3983 mutex_destroy(&pf->tc_mutex);
3984 mutex_destroy(&pf->avail_q_mutex);
3985 mutex_destroy(&pf->vfs.table_lock);
3986
3987 if (pf->avail_txqs) {
3988 bitmap_free(pf->avail_txqs);
3989 pf->avail_txqs = NULL;
3990 }
3991
3992 if (pf->avail_rxqs) {
3993 bitmap_free(pf->avail_rxqs);
3994 pf->avail_rxqs = NULL;
3995 }
3996
3997 if (pf->ptp.clock)
3998 ptp_clock_unregister(pf->ptp.clock);
3999
4000 xa_destroy(&pf->dyn_ports);
4001 xa_destroy(&pf->sf_nums);
4002}
4003
4004/**
4005 * ice_set_pf_caps - set PFs capability flags
4006 * @pf: pointer to the PF instance
4007 */
4008static void ice_set_pf_caps(struct ice_pf *pf)
4009{
4010 struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
4011
4012 clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
4013 if (func_caps->common_cap.rdma)
4014 set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
4015 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4016 if (func_caps->common_cap.dcb)
4017 set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4018 clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
4019 if (func_caps->common_cap.sr_iov_1_1) {
4020 set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
4021 pf->vfs.num_supported = min_t(int, func_caps->num_allocd_vfs,
4022 ICE_MAX_SRIOV_VFS);
4023 }
4024 clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
4025 if (func_caps->common_cap.rss_table_size)
4026 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
4027
4028 clear_bit(ICE_FLAG_FD_ENA, pf->flags);
4029 if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
4030 u16 unused;
4031
4032 /* ctrl_vsi_idx will be set to a valid value when flow director
4033 * is setup by ice_init_fdir
4034 */
4035 pf->ctrl_vsi_idx = ICE_NO_VSI;
4036 set_bit(ICE_FLAG_FD_ENA, pf->flags);
4037 /* force guaranteed filter pool for PF */
4038 ice_alloc_fd_guar_item(&pf->hw, &unused,
4039 func_caps->fd_fltr_guar);
4040 /* force shared filter pool for PF */
4041 ice_alloc_fd_shrd_item(&pf->hw, &unused,
4042 func_caps->fd_fltr_best_effort);
4043 }
4044
4045 clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
4046 if (func_caps->common_cap.ieee_1588 &&
4047 !(pf->hw.mac_type == ICE_MAC_E830))
4048 set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
4049
4050 pf->max_pf_txqs = func_caps->common_cap.num_txq;
4051 pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
4052}
4053
4054/**
4055 * ice_init_pf - Initialize general software structures (struct ice_pf)
4056 * @pf: board private structure to initialize
4057 */
4058static int ice_init_pf(struct ice_pf *pf)
4059{
4060 ice_set_pf_caps(pf);
4061
4062 mutex_init(&pf->sw_mutex);
4063 mutex_init(&pf->tc_mutex);
4064 mutex_init(&pf->adev_mutex);
4065 mutex_init(&pf->lag_mutex);
4066
4067 INIT_HLIST_HEAD(&pf->aq_wait_list);
4068 spin_lock_init(&pf->aq_wait_lock);
4069 init_waitqueue_head(&pf->aq_wait_queue);
4070
4071 init_waitqueue_head(&pf->reset_wait_queue);
4072
4073 /* setup service timer and periodic service task */
4074 timer_setup(&pf->serv_tmr, ice_service_timer, 0);
4075 pf->serv_tmr_period = HZ;
4076 INIT_WORK(&pf->serv_task, ice_service_task);
4077 clear_bit(ICE_SERVICE_SCHED, pf->state);
4078
4079 mutex_init(&pf->avail_q_mutex);
4080 pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
4081 if (!pf->avail_txqs)
4082 return -ENOMEM;
4083
4084 pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
4085 if (!pf->avail_rxqs) {
4086 bitmap_free(pf->avail_txqs);
4087 pf->avail_txqs = NULL;
4088 return -ENOMEM;
4089 }
4090
4091 mutex_init(&pf->vfs.table_lock);
4092 hash_init(pf->vfs.table);
4093 if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT))
4094 wr32(&pf->hw, E830_MBX_PF_IN_FLIGHT_VF_MSGS_THRESH,
4095 ICE_MBX_OVERFLOW_WATERMARK);
4096 else
4097 ice_mbx_init_snapshot(&pf->hw);
4098
4099 xa_init(&pf->dyn_ports);
4100 xa_init(&pf->sf_nums);
4101
4102 return 0;
4103}
4104
4105/**
4106 * ice_is_wol_supported - check if WoL is supported
4107 * @hw: pointer to hardware info
4108 *
4109 * Check if WoL is supported based on the HW configuration.
4110 * Returns true if NVM supports and enables WoL for this port, false otherwise
4111 */
4112bool ice_is_wol_supported(struct ice_hw *hw)
4113{
4114 u16 wol_ctrl;
4115
4116 /* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
4117 * word) indicates WoL is not supported on the corresponding PF ID.
4118 */
4119 if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
4120 return false;
4121
4122 return !(BIT(hw->port_info->lport) & wol_ctrl);
4123}
4124
4125/**
4126 * ice_vsi_recfg_qs - Change the number of queues on a VSI
4127 * @vsi: VSI being changed
4128 * @new_rx: new number of Rx queues
4129 * @new_tx: new number of Tx queues
4130 * @locked: is adev device_lock held
4131 *
4132 * Only change the number of queues if new_tx, or new_rx is non-0.
4133 *
4134 * Returns 0 on success.
4135 */
4136int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx, bool locked)
4137{
4138 struct ice_pf *pf = vsi->back;
4139 int i, err = 0, timeout = 50;
4140
4141 if (!new_rx && !new_tx)
4142 return -EINVAL;
4143
4144 while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
4145 timeout--;
4146 if (!timeout)
4147 return -EBUSY;
4148 usleep_range(1000, 2000);
4149 }
4150
4151 if (new_tx)
4152 vsi->req_txq = (u16)new_tx;
4153 if (new_rx)
4154 vsi->req_rxq = (u16)new_rx;
4155
4156 /* set for the next time the netdev is started */
4157 if (!netif_running(vsi->netdev)) {
4158 err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4159 if (err)
4160 goto rebuild_err;
4161 dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
4162 goto done;
4163 }
4164
4165 ice_vsi_close(vsi);
4166 err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4167 if (err)
4168 goto rebuild_err;
4169
4170 ice_for_each_traffic_class(i) {
4171 if (vsi->tc_cfg.ena_tc & BIT(i))
4172 netdev_set_tc_queue(vsi->netdev,
4173 vsi->tc_cfg.tc_info[i].netdev_tc,
4174 vsi->tc_cfg.tc_info[i].qcount_tx,
4175 vsi->tc_cfg.tc_info[i].qoffset);
4176 }
4177 ice_pf_dcb_recfg(pf, locked);
4178 ice_vsi_open(vsi);
4179 goto done;
4180
4181rebuild_err:
4182 dev_err(ice_pf_to_dev(pf), "Error during VSI rebuild: %d. Unload and reload the driver.\n",
4183 err);
4184done:
4185 clear_bit(ICE_CFG_BUSY, pf->state);
4186 return err;
4187}
4188
4189/**
4190 * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
4191 * @pf: PF to configure
4192 *
4193 * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
4194 * VSI can still Tx/Rx VLAN tagged packets.
4195 */
4196static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
4197{
4198 struct ice_vsi *vsi = ice_get_main_vsi(pf);
4199 struct ice_vsi_ctx *ctxt;
4200 struct ice_hw *hw;
4201 int status;
4202
4203 if (!vsi)
4204 return;
4205
4206 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
4207 if (!ctxt)
4208 return;
4209
4210 hw = &pf->hw;
4211 ctxt->info = vsi->info;
4212
4213 ctxt->info.valid_sections =
4214 cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
4215 ICE_AQ_VSI_PROP_SECURITY_VALID |
4216 ICE_AQ_VSI_PROP_SW_VALID);
4217
4218 /* disable VLAN anti-spoof */
4219 ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4220 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4221
4222 /* disable VLAN pruning and keep all other settings */
4223 ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
4224
4225 /* allow all VLANs on Tx and don't strip on Rx */
4226 ctxt->info.inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL |
4227 ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
4228
4229 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4230 if (status) {
4231 dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %d aq_err %s\n",
4232 status, ice_aq_str(hw->adminq.sq_last_status));
4233 } else {
4234 vsi->info.sec_flags = ctxt->info.sec_flags;
4235 vsi->info.sw_flags2 = ctxt->info.sw_flags2;
4236 vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
4237 }
4238
4239 kfree(ctxt);
4240}
4241
4242/**
4243 * ice_log_pkg_init - log result of DDP package load
4244 * @hw: pointer to hardware info
4245 * @state: state of package load
4246 */
4247static void ice_log_pkg_init(struct ice_hw *hw, enum ice_ddp_state state)
4248{
4249 struct ice_pf *pf = hw->back;
4250 struct device *dev;
4251
4252 dev = ice_pf_to_dev(pf);
4253
4254 switch (state) {
4255 case ICE_DDP_PKG_SUCCESS:
4256 dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
4257 hw->active_pkg_name,
4258 hw->active_pkg_ver.major,
4259 hw->active_pkg_ver.minor,
4260 hw->active_pkg_ver.update,
4261 hw->active_pkg_ver.draft);
4262 break;
4263 case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
4264 dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
4265 hw->active_pkg_name,
4266 hw->active_pkg_ver.major,
4267 hw->active_pkg_ver.minor,
4268 hw->active_pkg_ver.update,
4269 hw->active_pkg_ver.draft);
4270 break;
4271 case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
4272 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",
4273 hw->active_pkg_name,
4274 hw->active_pkg_ver.major,
4275 hw->active_pkg_ver.minor,
4276 ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4277 break;
4278 case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
4279 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",
4280 hw->active_pkg_name,
4281 hw->active_pkg_ver.major,
4282 hw->active_pkg_ver.minor,
4283 hw->active_pkg_ver.update,
4284 hw->active_pkg_ver.draft,
4285 hw->pkg_name,
4286 hw->pkg_ver.major,
4287 hw->pkg_ver.minor,
4288 hw->pkg_ver.update,
4289 hw->pkg_ver.draft);
4290 break;
4291 case ICE_DDP_PKG_FW_MISMATCH:
4292 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");
4293 break;
4294 case ICE_DDP_PKG_INVALID_FILE:
4295 dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
4296 break;
4297 case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
4298 dev_err(dev, "The DDP package file version is higher than the driver supports. Please use an updated driver. Entering Safe Mode.\n");
4299 break;
4300 case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
4301 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",
4302 ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4303 break;
4304 case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
4305 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");
4306 break;
4307 case ICE_DDP_PKG_FILE_REVISION_TOO_LOW:
4308 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");
4309 break;
4310 case ICE_DDP_PKG_LOAD_ERROR:
4311 dev_err(dev, "An error occurred on the device while loading the DDP package. The device will be reset.\n");
4312 /* poll for reset to complete */
4313 if (ice_check_reset(hw))
4314 dev_err(dev, "Error resetting device. Please reload the driver\n");
4315 break;
4316 case ICE_DDP_PKG_ERR:
4317 default:
4318 dev_err(dev, "An unknown error occurred when loading the DDP package. Entering Safe Mode.\n");
4319 break;
4320 }
4321}
4322
4323/**
4324 * ice_load_pkg - load/reload the DDP Package file
4325 * @firmware: firmware structure when firmware requested or NULL for reload
4326 * @pf: pointer to the PF instance
4327 *
4328 * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
4329 * initialize HW tables.
4330 */
4331static void
4332ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
4333{
4334 enum ice_ddp_state state = ICE_DDP_PKG_ERR;
4335 struct device *dev = ice_pf_to_dev(pf);
4336 struct ice_hw *hw = &pf->hw;
4337
4338 /* Load DDP Package */
4339 if (firmware && !hw->pkg_copy) {
4340 state = ice_copy_and_init_pkg(hw, firmware->data,
4341 firmware->size);
4342 ice_log_pkg_init(hw, state);
4343 } else if (!firmware && hw->pkg_copy) {
4344 /* Reload package during rebuild after CORER/GLOBR reset */
4345 state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
4346 ice_log_pkg_init(hw, state);
4347 } else {
4348 dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
4349 }
4350
4351 if (!ice_is_init_pkg_successful(state)) {
4352 /* Safe Mode */
4353 clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4354 return;
4355 }
4356
4357 /* Successful download package is the precondition for advanced
4358 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
4359 */
4360 set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4361}
4362
4363/**
4364 * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
4365 * @pf: pointer to the PF structure
4366 *
4367 * There is no error returned here because the driver should be able to handle
4368 * 128 Byte cache lines, so we only print a warning in case issues are seen,
4369 * specifically with Tx.
4370 */
4371static void ice_verify_cacheline_size(struct ice_pf *pf)
4372{
4373 if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
4374 dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
4375 ICE_CACHE_LINE_BYTES);
4376}
4377
4378/**
4379 * ice_send_version - update firmware with driver version
4380 * @pf: PF struct
4381 *
4382 * Returns 0 on success, else error code
4383 */
4384static int ice_send_version(struct ice_pf *pf)
4385{
4386 struct ice_driver_ver dv;
4387
4388 dv.major_ver = 0xff;
4389 dv.minor_ver = 0xff;
4390 dv.build_ver = 0xff;
4391 dv.subbuild_ver = 0;
4392 strscpy((char *)dv.driver_string, UTS_RELEASE,
4393 sizeof(dv.driver_string));
4394 return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
4395}
4396
4397/**
4398 * ice_init_fdir - Initialize flow director VSI and configuration
4399 * @pf: pointer to the PF instance
4400 *
4401 * returns 0 on success, negative on error
4402 */
4403static int ice_init_fdir(struct ice_pf *pf)
4404{
4405 struct device *dev = ice_pf_to_dev(pf);
4406 struct ice_vsi *ctrl_vsi;
4407 int err;
4408
4409 /* Side Band Flow Director needs to have a control VSI.
4410 * Allocate it and store it in the PF.
4411 */
4412 ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
4413 if (!ctrl_vsi) {
4414 dev_dbg(dev, "could not create control VSI\n");
4415 return -ENOMEM;
4416 }
4417
4418 err = ice_vsi_open_ctrl(ctrl_vsi);
4419 if (err) {
4420 dev_dbg(dev, "could not open control VSI\n");
4421 goto err_vsi_open;
4422 }
4423
4424 mutex_init(&pf->hw.fdir_fltr_lock);
4425
4426 err = ice_fdir_create_dflt_rules(pf);
4427 if (err)
4428 goto err_fdir_rule;
4429
4430 return 0;
4431
4432err_fdir_rule:
4433 ice_fdir_release_flows(&pf->hw);
4434 ice_vsi_close(ctrl_vsi);
4435err_vsi_open:
4436 ice_vsi_release(ctrl_vsi);
4437 if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4438 pf->vsi[pf->ctrl_vsi_idx] = NULL;
4439 pf->ctrl_vsi_idx = ICE_NO_VSI;
4440 }
4441 return err;
4442}
4443
4444static void ice_deinit_fdir(struct ice_pf *pf)
4445{
4446 struct ice_vsi *vsi = ice_get_ctrl_vsi(pf);
4447
4448 if (!vsi)
4449 return;
4450
4451 ice_vsi_manage_fdir(vsi, false);
4452 ice_vsi_release(vsi);
4453 if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4454 pf->vsi[pf->ctrl_vsi_idx] = NULL;
4455 pf->ctrl_vsi_idx = ICE_NO_VSI;
4456 }
4457
4458 mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4459}
4460
4461/**
4462 * ice_get_opt_fw_name - return optional firmware file name or NULL
4463 * @pf: pointer to the PF instance
4464 */
4465static char *ice_get_opt_fw_name(struct ice_pf *pf)
4466{
4467 /* Optional firmware name same as default with additional dash
4468 * followed by a EUI-64 identifier (PCIe Device Serial Number)
4469 */
4470 struct pci_dev *pdev = pf->pdev;
4471 char *opt_fw_filename;
4472 u64 dsn;
4473
4474 /* Determine the name of the optional file using the DSN (two
4475 * dwords following the start of the DSN Capability).
4476 */
4477 dsn = pci_get_dsn(pdev);
4478 if (!dsn)
4479 return NULL;
4480
4481 opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
4482 if (!opt_fw_filename)
4483 return NULL;
4484
4485 snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
4486 ICE_DDP_PKG_PATH, dsn);
4487
4488 return opt_fw_filename;
4489}
4490
4491/**
4492 * ice_request_fw - Device initialization routine
4493 * @pf: pointer to the PF instance
4494 * @firmware: double pointer to firmware struct
4495 *
4496 * Return: zero when successful, negative values otherwise.
4497 */
4498static int ice_request_fw(struct ice_pf *pf, const struct firmware **firmware)
4499{
4500 char *opt_fw_filename = ice_get_opt_fw_name(pf);
4501 struct device *dev = ice_pf_to_dev(pf);
4502 int err = 0;
4503
4504 /* optional device-specific DDP (if present) overrides the default DDP
4505 * package file. kernel logs a debug message if the file doesn't exist,
4506 * and warning messages for other errors.
4507 */
4508 if (opt_fw_filename) {
4509 err = firmware_request_nowarn(firmware, opt_fw_filename, dev);
4510 kfree(opt_fw_filename);
4511 if (!err)
4512 return err;
4513 }
4514 err = request_firmware(firmware, ICE_DDP_PKG_FILE, dev);
4515 if (err)
4516 dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
4517
4518 return err;
4519}
4520
4521/**
4522 * ice_init_tx_topology - performs Tx topology initialization
4523 * @hw: pointer to the hardware structure
4524 * @firmware: pointer to firmware structure
4525 *
4526 * Return: zero when init was successful, negative values otherwise.
4527 */
4528static int
4529ice_init_tx_topology(struct ice_hw *hw, const struct firmware *firmware)
4530{
4531 u8 num_tx_sched_layers = hw->num_tx_sched_layers;
4532 struct ice_pf *pf = hw->back;
4533 struct device *dev;
4534 int err;
4535
4536 dev = ice_pf_to_dev(pf);
4537 err = ice_cfg_tx_topo(hw, firmware->data, firmware->size);
4538 if (!err) {
4539 if (hw->num_tx_sched_layers > num_tx_sched_layers)
4540 dev_info(dev, "Tx scheduling layers switching feature disabled\n");
4541 else
4542 dev_info(dev, "Tx scheduling layers switching feature enabled\n");
4543 /* if there was a change in topology ice_cfg_tx_topo triggered
4544 * a CORER and we need to re-init hw
4545 */
4546 ice_deinit_hw(hw);
4547 err = ice_init_hw(hw);
4548
4549 return err;
4550 } else if (err == -EIO) {
4551 dev_info(dev, "DDP package does not support Tx scheduling layers switching feature - please update to the latest DDP package and try again\n");
4552 }
4553
4554 return 0;
4555}
4556
4557/**
4558 * ice_init_supported_rxdids - Initialize supported Rx descriptor IDs
4559 * @hw: pointer to the hardware structure
4560 * @pf: pointer to pf structure
4561 *
4562 * The pf->supported_rxdids bitmap is used to indicate to VFs which descriptor
4563 * formats the PF hardware supports. The exact list of supported RXDIDs
4564 * depends on the loaded DDP package. The IDs can be determined by reading the
4565 * GLFLXP_RXDID_FLAGS register after the DDP package is loaded.
4566 *
4567 * Note that the legacy 32-byte RXDID 0 is always supported but is not listed
4568 * in the DDP package. The 16-byte legacy descriptor is never supported by
4569 * VFs.
4570 */
4571static void ice_init_supported_rxdids(struct ice_hw *hw, struct ice_pf *pf)
4572{
4573 pf->supported_rxdids = BIT(ICE_RXDID_LEGACY_1);
4574
4575 for (int i = ICE_RXDID_FLEX_NIC; i < ICE_FLEX_DESC_RXDID_MAX_NUM; i++) {
4576 u32 regval;
4577
4578 regval = rd32(hw, GLFLXP_RXDID_FLAGS(i, 0));
4579 if ((regval >> GLFLXP_RXDID_FLAGS_FLEXIFLAG_4N_S)
4580 & GLFLXP_RXDID_FLAGS_FLEXIFLAG_4N_M)
4581 pf->supported_rxdids |= BIT(i);
4582 }
4583}
4584
4585/**
4586 * ice_init_ddp_config - DDP related configuration
4587 * @hw: pointer to the hardware structure
4588 * @pf: pointer to pf structure
4589 *
4590 * This function loads DDP file from the disk, then initializes Tx
4591 * topology. At the end DDP package is loaded on the card.
4592 *
4593 * Return: zero when init was successful, negative values otherwise.
4594 */
4595static int ice_init_ddp_config(struct ice_hw *hw, struct ice_pf *pf)
4596{
4597 struct device *dev = ice_pf_to_dev(pf);
4598 const struct firmware *firmware = NULL;
4599 int err;
4600
4601 err = ice_request_fw(pf, &firmware);
4602 if (err) {
4603 dev_err(dev, "Fail during requesting FW: %d\n", err);
4604 return err;
4605 }
4606
4607 err = ice_init_tx_topology(hw, firmware);
4608 if (err) {
4609 dev_err(dev, "Fail during initialization of Tx topology: %d\n",
4610 err);
4611 release_firmware(firmware);
4612 return err;
4613 }
4614
4615 /* Download firmware to device */
4616 ice_load_pkg(firmware, pf);
4617 release_firmware(firmware);
4618
4619 /* Initialize the supported Rx descriptor IDs after loading DDP */
4620 ice_init_supported_rxdids(hw, pf);
4621
4622 return 0;
4623}
4624
4625/**
4626 * ice_print_wake_reason - show the wake up cause in the log
4627 * @pf: pointer to the PF struct
4628 */
4629static void ice_print_wake_reason(struct ice_pf *pf)
4630{
4631 u32 wus = pf->wakeup_reason;
4632 const char *wake_str;
4633
4634 /* if no wake event, nothing to print */
4635 if (!wus)
4636 return;
4637
4638 if (wus & PFPM_WUS_LNKC_M)
4639 wake_str = "Link\n";
4640 else if (wus & PFPM_WUS_MAG_M)
4641 wake_str = "Magic Packet\n";
4642 else if (wus & PFPM_WUS_MNG_M)
4643 wake_str = "Management\n";
4644 else if (wus & PFPM_WUS_FW_RST_WK_M)
4645 wake_str = "Firmware Reset\n";
4646 else
4647 wake_str = "Unknown\n";
4648
4649 dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4650}
4651
4652/**
4653 * ice_pf_fwlog_update_module - update 1 module
4654 * @pf: pointer to the PF struct
4655 * @log_level: log_level to use for the @module
4656 * @module: module to update
4657 */
4658void ice_pf_fwlog_update_module(struct ice_pf *pf, int log_level, int module)
4659{
4660 struct ice_hw *hw = &pf->hw;
4661
4662 hw->fwlog_cfg.module_entries[module].log_level = log_level;
4663}
4664
4665/**
4666 * ice_register_netdev - register netdev
4667 * @vsi: pointer to the VSI struct
4668 */
4669static int ice_register_netdev(struct ice_vsi *vsi)
4670{
4671 int err;
4672
4673 if (!vsi || !vsi->netdev)
4674 return -EIO;
4675
4676 err = register_netdev(vsi->netdev);
4677 if (err)
4678 return err;
4679
4680 set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4681 netif_carrier_off(vsi->netdev);
4682 netif_tx_stop_all_queues(vsi->netdev);
4683
4684 return 0;
4685}
4686
4687static void ice_unregister_netdev(struct ice_vsi *vsi)
4688{
4689 if (!vsi || !vsi->netdev)
4690 return;
4691
4692 unregister_netdev(vsi->netdev);
4693 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4694}
4695
4696/**
4697 * ice_cfg_netdev - Allocate, configure and register a netdev
4698 * @vsi: the VSI associated with the new netdev
4699 *
4700 * Returns 0 on success, negative value on failure
4701 */
4702static int ice_cfg_netdev(struct ice_vsi *vsi)
4703{
4704 struct ice_netdev_priv *np;
4705 struct net_device *netdev;
4706 u8 mac_addr[ETH_ALEN];
4707
4708 netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
4709 vsi->alloc_rxq);
4710 if (!netdev)
4711 return -ENOMEM;
4712
4713 set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4714 vsi->netdev = netdev;
4715 np = netdev_priv(netdev);
4716 np->vsi = vsi;
4717
4718 ice_set_netdev_features(netdev);
4719 ice_set_ops(vsi);
4720
4721 if (vsi->type == ICE_VSI_PF) {
4722 SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
4723 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4724 eth_hw_addr_set(netdev, mac_addr);
4725 }
4726
4727 netdev->priv_flags |= IFF_UNICAST_FLT;
4728
4729 /* Setup netdev TC information */
4730 ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
4731
4732 netdev->max_mtu = ICE_MAX_MTU;
4733
4734 return 0;
4735}
4736
4737static void ice_decfg_netdev(struct ice_vsi *vsi)
4738{
4739 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4740 free_netdev(vsi->netdev);
4741 vsi->netdev = NULL;
4742}
4743
4744/**
4745 * ice_wait_for_fw - wait for full FW readiness
4746 * @hw: pointer to the hardware structure
4747 * @timeout: milliseconds that can elapse before timing out
4748 */
4749static int ice_wait_for_fw(struct ice_hw *hw, u32 timeout)
4750{
4751 int fw_loading;
4752 u32 elapsed = 0;
4753
4754 while (elapsed <= timeout) {
4755 fw_loading = rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M;
4756
4757 /* firmware was not yet loaded, we have to wait more */
4758 if (fw_loading) {
4759 elapsed += 100;
4760 msleep(100);
4761 continue;
4762 }
4763 return 0;
4764 }
4765
4766 return -ETIMEDOUT;
4767}
4768
4769int ice_init_dev(struct ice_pf *pf)
4770{
4771 struct device *dev = ice_pf_to_dev(pf);
4772 struct ice_hw *hw = &pf->hw;
4773 int err;
4774
4775 err = ice_init_hw(hw);
4776 if (err) {
4777 dev_err(dev, "ice_init_hw failed: %d\n", err);
4778 return err;
4779 }
4780
4781 /* Some cards require longer initialization times
4782 * due to necessity of loading FW from an external source.
4783 * This can take even half a minute.
4784 */
4785 if (ice_is_pf_c827(hw)) {
4786 err = ice_wait_for_fw(hw, 30000);
4787 if (err) {
4788 dev_err(dev, "ice_wait_for_fw timed out");
4789 return err;
4790 }
4791 }
4792
4793 ice_init_feature_support(pf);
4794
4795 err = ice_init_ddp_config(hw, pf);
4796
4797 /* if ice_init_ddp_config fails, ICE_FLAG_ADV_FEATURES bit won't be
4798 * set in pf->state, which will cause ice_is_safe_mode to return
4799 * true
4800 */
4801 if (err || ice_is_safe_mode(pf)) {
4802 /* we already got function/device capabilities but these don't
4803 * reflect what the driver needs to do in safe mode. Instead of
4804 * adding conditional logic everywhere to ignore these
4805 * device/function capabilities, override them.
4806 */
4807 ice_set_safe_mode_caps(hw);
4808 }
4809
4810 err = ice_init_pf(pf);
4811 if (err) {
4812 dev_err(dev, "ice_init_pf failed: %d\n", err);
4813 goto err_init_pf;
4814 }
4815
4816 pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4817 pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4818 pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4819 pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4820 if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4821 pf->hw.udp_tunnel_nic.tables[0].n_entries =
4822 pf->hw.tnl.valid_count[TNL_VXLAN];
4823 pf->hw.udp_tunnel_nic.tables[0].tunnel_types =
4824 UDP_TUNNEL_TYPE_VXLAN;
4825 }
4826 if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4827 pf->hw.udp_tunnel_nic.tables[1].n_entries =
4828 pf->hw.tnl.valid_count[TNL_GENEVE];
4829 pf->hw.udp_tunnel_nic.tables[1].tunnel_types =
4830 UDP_TUNNEL_TYPE_GENEVE;
4831 }
4832
4833 err = ice_init_interrupt_scheme(pf);
4834 if (err) {
4835 dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4836 err = -EIO;
4837 goto err_init_interrupt_scheme;
4838 }
4839
4840 /* In case of MSIX we are going to setup the misc vector right here
4841 * to handle admin queue events etc. In case of legacy and MSI
4842 * the misc functionality and queue processing is combined in
4843 * the same vector and that gets setup at open.
4844 */
4845 err = ice_req_irq_msix_misc(pf);
4846 if (err) {
4847 dev_err(dev, "setup of misc vector failed: %d\n", err);
4848 goto err_req_irq_msix_misc;
4849 }
4850
4851 return 0;
4852
4853err_req_irq_msix_misc:
4854 ice_clear_interrupt_scheme(pf);
4855err_init_interrupt_scheme:
4856 ice_deinit_pf(pf);
4857err_init_pf:
4858 ice_deinit_hw(hw);
4859 return err;
4860}
4861
4862void ice_deinit_dev(struct ice_pf *pf)
4863{
4864 ice_free_irq_msix_misc(pf);
4865 ice_deinit_pf(pf);
4866 ice_deinit_hw(&pf->hw);
4867
4868 /* Service task is already stopped, so call reset directly. */
4869 ice_reset(&pf->hw, ICE_RESET_PFR);
4870 pci_wait_for_pending_transaction(pf->pdev);
4871 ice_clear_interrupt_scheme(pf);
4872}
4873
4874static void ice_init_features(struct ice_pf *pf)
4875{
4876 struct device *dev = ice_pf_to_dev(pf);
4877
4878 if (ice_is_safe_mode(pf))
4879 return;
4880
4881 /* initialize DDP driven features */
4882 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4883 ice_ptp_init(pf);
4884
4885 if (ice_is_feature_supported(pf, ICE_F_GNSS))
4886 ice_gnss_init(pf);
4887
4888 if (ice_is_feature_supported(pf, ICE_F_CGU) ||
4889 ice_is_feature_supported(pf, ICE_F_PHY_RCLK))
4890 ice_dpll_init(pf);
4891
4892 /* Note: Flow director init failure is non-fatal to load */
4893 if (ice_init_fdir(pf))
4894 dev_err(dev, "could not initialize flow director\n");
4895
4896 /* Note: DCB init failure is non-fatal to load */
4897 if (ice_init_pf_dcb(pf, false)) {
4898 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4899 clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4900 } else {
4901 ice_cfg_lldp_mib_change(&pf->hw, true);
4902 }
4903
4904 if (ice_init_lag(pf))
4905 dev_warn(dev, "Failed to init link aggregation support\n");
4906
4907 ice_hwmon_init(pf);
4908}
4909
4910static void ice_deinit_features(struct ice_pf *pf)
4911{
4912 if (ice_is_safe_mode(pf))
4913 return;
4914
4915 ice_deinit_lag(pf);
4916 if (test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags))
4917 ice_cfg_lldp_mib_change(&pf->hw, false);
4918 ice_deinit_fdir(pf);
4919 if (ice_is_feature_supported(pf, ICE_F_GNSS))
4920 ice_gnss_exit(pf);
4921 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4922 ice_ptp_release(pf);
4923 if (test_bit(ICE_FLAG_DPLL, pf->flags))
4924 ice_dpll_deinit(pf);
4925 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
4926 xa_destroy(&pf->eswitch.reprs);
4927}
4928
4929static void ice_init_wakeup(struct ice_pf *pf)
4930{
4931 /* Save wakeup reason register for later use */
4932 pf->wakeup_reason = rd32(&pf->hw, PFPM_WUS);
4933
4934 /* check for a power management event */
4935 ice_print_wake_reason(pf);
4936
4937 /* clear wake status, all bits */
4938 wr32(&pf->hw, PFPM_WUS, U32_MAX);
4939
4940 /* Disable WoL at init, wait for user to enable */
4941 device_set_wakeup_enable(ice_pf_to_dev(pf), false);
4942}
4943
4944static int ice_init_link(struct ice_pf *pf)
4945{
4946 struct device *dev = ice_pf_to_dev(pf);
4947 int err;
4948
4949 err = ice_init_link_events(pf->hw.port_info);
4950 if (err) {
4951 dev_err(dev, "ice_init_link_events failed: %d\n", err);
4952 return err;
4953 }
4954
4955 /* not a fatal error if this fails */
4956 err = ice_init_nvm_phy_type(pf->hw.port_info);
4957 if (err)
4958 dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4959
4960 /* not a fatal error if this fails */
4961 err = ice_update_link_info(pf->hw.port_info);
4962 if (err)
4963 dev_err(dev, "ice_update_link_info failed: %d\n", err);
4964
4965 ice_init_link_dflt_override(pf->hw.port_info);
4966
4967 ice_check_link_cfg_err(pf,
4968 pf->hw.port_info->phy.link_info.link_cfg_err);
4969
4970 /* if media available, initialize PHY settings */
4971 if (pf->hw.port_info->phy.link_info.link_info &
4972 ICE_AQ_MEDIA_AVAILABLE) {
4973 /* not a fatal error if this fails */
4974 err = ice_init_phy_user_cfg(pf->hw.port_info);
4975 if (err)
4976 dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4977
4978 if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4979 struct ice_vsi *vsi = ice_get_main_vsi(pf);
4980
4981 if (vsi)
4982 ice_configure_phy(vsi);
4983 }
4984 } else {
4985 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4986 }
4987
4988 return err;
4989}
4990
4991static int ice_init_pf_sw(struct ice_pf *pf)
4992{
4993 bool dvm = ice_is_dvm_ena(&pf->hw);
4994 struct ice_vsi *vsi;
4995 int err;
4996
4997 /* create switch struct for the switch element created by FW on boot */
4998 pf->first_sw = kzalloc(sizeof(*pf->first_sw), GFP_KERNEL);
4999 if (!pf->first_sw)
5000 return -ENOMEM;
5001
5002 if (pf->hw.evb_veb)
5003 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
5004 else
5005 pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
5006
5007 pf->first_sw->pf = pf;
5008
5009 /* record the sw_id available for later use */
5010 pf->first_sw->sw_id = pf->hw.port_info->sw_id;
5011
5012 err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
5013 if (err)
5014 goto err_aq_set_port_params;
5015
5016 vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
5017 if (!vsi) {
5018 err = -ENOMEM;
5019 goto err_pf_vsi_setup;
5020 }
5021
5022 return 0;
5023
5024err_pf_vsi_setup:
5025err_aq_set_port_params:
5026 kfree(pf->first_sw);
5027 return err;
5028}
5029
5030static void ice_deinit_pf_sw(struct ice_pf *pf)
5031{
5032 struct ice_vsi *vsi = ice_get_main_vsi(pf);
5033
5034 if (!vsi)
5035 return;
5036
5037 ice_vsi_release(vsi);
5038 kfree(pf->first_sw);
5039}
5040
5041static int ice_alloc_vsis(struct ice_pf *pf)
5042{
5043 struct device *dev = ice_pf_to_dev(pf);
5044
5045 pf->num_alloc_vsi = pf->hw.func_caps.guar_num_vsi;
5046 if (!pf->num_alloc_vsi)
5047 return -EIO;
5048
5049 if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
5050 dev_warn(dev,
5051 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
5052 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
5053 pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
5054 }
5055
5056 pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
5057 GFP_KERNEL);
5058 if (!pf->vsi)
5059 return -ENOMEM;
5060
5061 pf->vsi_stats = devm_kcalloc(dev, pf->num_alloc_vsi,
5062 sizeof(*pf->vsi_stats), GFP_KERNEL);
5063 if (!pf->vsi_stats) {
5064 devm_kfree(dev, pf->vsi);
5065 return -ENOMEM;
5066 }
5067
5068 return 0;
5069}
5070
5071static void ice_dealloc_vsis(struct ice_pf *pf)
5072{
5073 devm_kfree(ice_pf_to_dev(pf), pf->vsi_stats);
5074 pf->vsi_stats = NULL;
5075
5076 pf->num_alloc_vsi = 0;
5077 devm_kfree(ice_pf_to_dev(pf), pf->vsi);
5078 pf->vsi = NULL;
5079}
5080
5081static int ice_init_devlink(struct ice_pf *pf)
5082{
5083 int err;
5084
5085 err = ice_devlink_register_params(pf);
5086 if (err)
5087 return err;
5088
5089 ice_devlink_init_regions(pf);
5090 ice_devlink_register(pf);
5091
5092 return 0;
5093}
5094
5095static void ice_deinit_devlink(struct ice_pf *pf)
5096{
5097 ice_devlink_unregister(pf);
5098 ice_devlink_destroy_regions(pf);
5099 ice_devlink_unregister_params(pf);
5100}
5101
5102static int ice_init(struct ice_pf *pf)
5103{
5104 int err;
5105
5106 err = ice_init_dev(pf);
5107 if (err)
5108 return err;
5109
5110 err = ice_alloc_vsis(pf);
5111 if (err)
5112 goto err_alloc_vsis;
5113
5114 err = ice_init_pf_sw(pf);
5115 if (err)
5116 goto err_init_pf_sw;
5117
5118 ice_init_wakeup(pf);
5119
5120 err = ice_init_link(pf);
5121 if (err)
5122 goto err_init_link;
5123
5124 err = ice_send_version(pf);
5125 if (err)
5126 goto err_init_link;
5127
5128 ice_verify_cacheline_size(pf);
5129
5130 if (ice_is_safe_mode(pf))
5131 ice_set_safe_mode_vlan_cfg(pf);
5132 else
5133 /* print PCI link speed and width */
5134 pcie_print_link_status(pf->pdev);
5135
5136 /* ready to go, so clear down state bit */
5137 clear_bit(ICE_DOWN, pf->state);
5138 clear_bit(ICE_SERVICE_DIS, pf->state);
5139
5140 /* since everything is good, start the service timer */
5141 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5142
5143 return 0;
5144
5145err_init_link:
5146 ice_deinit_pf_sw(pf);
5147err_init_pf_sw:
5148 ice_dealloc_vsis(pf);
5149err_alloc_vsis:
5150 ice_deinit_dev(pf);
5151 return err;
5152}
5153
5154static void ice_deinit(struct ice_pf *pf)
5155{
5156 set_bit(ICE_SERVICE_DIS, pf->state);
5157 set_bit(ICE_DOWN, pf->state);
5158
5159 ice_deinit_pf_sw(pf);
5160 ice_dealloc_vsis(pf);
5161 ice_deinit_dev(pf);
5162}
5163
5164/**
5165 * ice_load - load pf by init hw and starting VSI
5166 * @pf: pointer to the pf instance
5167 *
5168 * This function has to be called under devl_lock.
5169 */
5170int ice_load(struct ice_pf *pf)
5171{
5172 struct ice_vsi *vsi;
5173 int err;
5174
5175 devl_assert_locked(priv_to_devlink(pf));
5176
5177 vsi = ice_get_main_vsi(pf);
5178
5179 /* init channel list */
5180 INIT_LIST_HEAD(&vsi->ch_list);
5181
5182 err = ice_cfg_netdev(vsi);
5183 if (err)
5184 return err;
5185
5186 /* Setup DCB netlink interface */
5187 ice_dcbnl_setup(vsi);
5188
5189 err = ice_init_mac_fltr(pf);
5190 if (err)
5191 goto err_init_mac_fltr;
5192
5193 err = ice_devlink_create_pf_port(pf);
5194 if (err)
5195 goto err_devlink_create_pf_port;
5196
5197 SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
5198
5199 err = ice_register_netdev(vsi);
5200 if (err)
5201 goto err_register_netdev;
5202
5203 err = ice_tc_indir_block_register(vsi);
5204 if (err)
5205 goto err_tc_indir_block_register;
5206
5207 ice_napi_add(vsi);
5208
5209 err = ice_init_rdma(pf);
5210 if (err)
5211 goto err_init_rdma;
5212
5213 ice_init_features(pf);
5214 ice_service_task_restart(pf);
5215
5216 clear_bit(ICE_DOWN, pf->state);
5217
5218 return 0;
5219
5220err_init_rdma:
5221 ice_tc_indir_block_unregister(vsi);
5222err_tc_indir_block_register:
5223 ice_unregister_netdev(vsi);
5224err_register_netdev:
5225 ice_devlink_destroy_pf_port(pf);
5226err_devlink_create_pf_port:
5227err_init_mac_fltr:
5228 ice_decfg_netdev(vsi);
5229 return err;
5230}
5231
5232/**
5233 * ice_unload - unload pf by stopping VSI and deinit hw
5234 * @pf: pointer to the pf instance
5235 *
5236 * This function has to be called under devl_lock.
5237 */
5238void ice_unload(struct ice_pf *pf)
5239{
5240 struct ice_vsi *vsi = ice_get_main_vsi(pf);
5241
5242 devl_assert_locked(priv_to_devlink(pf));
5243
5244 ice_deinit_features(pf);
5245 ice_deinit_rdma(pf);
5246 ice_tc_indir_block_unregister(vsi);
5247 ice_unregister_netdev(vsi);
5248 ice_devlink_destroy_pf_port(pf);
5249 ice_decfg_netdev(vsi);
5250}
5251
5252/**
5253 * ice_probe - Device initialization routine
5254 * @pdev: PCI device information struct
5255 * @ent: entry in ice_pci_tbl
5256 *
5257 * Returns 0 on success, negative on failure
5258 */
5259static int
5260ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
5261{
5262 struct device *dev = &pdev->dev;
5263 struct ice_adapter *adapter;
5264 struct ice_pf *pf;
5265 struct ice_hw *hw;
5266 int err;
5267
5268 if (pdev->is_virtfn) {
5269 dev_err(dev, "can't probe a virtual function\n");
5270 return -EINVAL;
5271 }
5272
5273 /* when under a kdump kernel initiate a reset before enabling the
5274 * device in order to clear out any pending DMA transactions. These
5275 * transactions can cause some systems to machine check when doing
5276 * the pcim_enable_device() below.
5277 */
5278 if (is_kdump_kernel()) {
5279 pci_save_state(pdev);
5280 pci_clear_master(pdev);
5281 err = pcie_flr(pdev);
5282 if (err)
5283 return err;
5284 pci_restore_state(pdev);
5285 }
5286
5287 /* this driver uses devres, see
5288 * Documentation/driver-api/driver-model/devres.rst
5289 */
5290 err = pcim_enable_device(pdev);
5291 if (err)
5292 return err;
5293
5294 err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
5295 if (err) {
5296 dev_err(dev, "BAR0 I/O map error %d\n", err);
5297 return err;
5298 }
5299
5300 pf = ice_allocate_pf(dev);
5301 if (!pf)
5302 return -ENOMEM;
5303
5304 /* initialize Auxiliary index to invalid value */
5305 pf->aux_idx = -1;
5306
5307 /* set up for high or low DMA */
5308 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
5309 if (err) {
5310 dev_err(dev, "DMA configuration failed: 0x%x\n", err);
5311 return err;
5312 }
5313
5314 pci_set_master(pdev);
5315
5316 adapter = ice_adapter_get(pdev);
5317 if (IS_ERR(adapter))
5318 return PTR_ERR(adapter);
5319
5320 pf->pdev = pdev;
5321 pf->adapter = adapter;
5322 pci_set_drvdata(pdev, pf);
5323 set_bit(ICE_DOWN, pf->state);
5324 /* Disable service task until DOWN bit is cleared */
5325 set_bit(ICE_SERVICE_DIS, pf->state);
5326
5327 hw = &pf->hw;
5328 hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
5329 pci_save_state(pdev);
5330
5331 hw->back = pf;
5332 hw->port_info = NULL;
5333 hw->vendor_id = pdev->vendor;
5334 hw->device_id = pdev->device;
5335 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
5336 hw->subsystem_vendor_id = pdev->subsystem_vendor;
5337 hw->subsystem_device_id = pdev->subsystem_device;
5338 hw->bus.device = PCI_SLOT(pdev->devfn);
5339 hw->bus.func = PCI_FUNC(pdev->devfn);
5340 ice_set_ctrlq_len(hw);
5341
5342 pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
5343
5344#ifndef CONFIG_DYNAMIC_DEBUG
5345 if (debug < -1)
5346 hw->debug_mask = debug;
5347#endif
5348
5349 err = ice_init(pf);
5350 if (err)
5351 goto err_init;
5352
5353 devl_lock(priv_to_devlink(pf));
5354 err = ice_load(pf);
5355 if (err)
5356 goto err_load;
5357
5358 err = ice_init_devlink(pf);
5359 if (err)
5360 goto err_init_devlink;
5361 devl_unlock(priv_to_devlink(pf));
5362
5363 return 0;
5364
5365err_init_devlink:
5366 ice_unload(pf);
5367err_load:
5368 devl_unlock(priv_to_devlink(pf));
5369 ice_deinit(pf);
5370err_init:
5371 ice_adapter_put(pdev);
5372 return err;
5373}
5374
5375/**
5376 * ice_set_wake - enable or disable Wake on LAN
5377 * @pf: pointer to the PF struct
5378 *
5379 * Simple helper for WoL control
5380 */
5381static void ice_set_wake(struct ice_pf *pf)
5382{
5383 struct ice_hw *hw = &pf->hw;
5384 bool wol = pf->wol_ena;
5385
5386 /* clear wake state, otherwise new wake events won't fire */
5387 wr32(hw, PFPM_WUS, U32_MAX);
5388
5389 /* enable / disable APM wake up, no RMW needed */
5390 wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
5391
5392 /* set magic packet filter enabled */
5393 wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
5394}
5395
5396/**
5397 * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
5398 * @pf: pointer to the PF struct
5399 *
5400 * Issue firmware command to enable multicast magic wake, making
5401 * sure that any locally administered address (LAA) is used for
5402 * wake, and that PF reset doesn't undo the LAA.
5403 */
5404static void ice_setup_mc_magic_wake(struct ice_pf *pf)
5405{
5406 struct device *dev = ice_pf_to_dev(pf);
5407 struct ice_hw *hw = &pf->hw;
5408 u8 mac_addr[ETH_ALEN];
5409 struct ice_vsi *vsi;
5410 int status;
5411 u8 flags;
5412
5413 if (!pf->wol_ena)
5414 return;
5415
5416 vsi = ice_get_main_vsi(pf);
5417 if (!vsi)
5418 return;
5419
5420 /* Get current MAC address in case it's an LAA */
5421 if (vsi->netdev)
5422 ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
5423 else
5424 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
5425
5426 flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
5427 ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
5428 ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
5429
5430 status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
5431 if (status)
5432 dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
5433 status, ice_aq_str(hw->adminq.sq_last_status));
5434}
5435
5436/**
5437 * ice_remove - Device removal routine
5438 * @pdev: PCI device information struct
5439 */
5440static void ice_remove(struct pci_dev *pdev)
5441{
5442 struct ice_pf *pf = pci_get_drvdata(pdev);
5443 int i;
5444
5445 for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
5446 if (!ice_is_reset_in_progress(pf->state))
5447 break;
5448 msleep(100);
5449 }
5450
5451 if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
5452 set_bit(ICE_VF_RESETS_DISABLED, pf->state);
5453 ice_free_vfs(pf);
5454 }
5455
5456 ice_hwmon_exit(pf);
5457
5458 ice_service_task_stop(pf);
5459 ice_aq_cancel_waiting_tasks(pf);
5460 set_bit(ICE_DOWN, pf->state);
5461
5462 if (!ice_is_safe_mode(pf))
5463 ice_remove_arfs(pf);
5464
5465 devl_lock(priv_to_devlink(pf));
5466 ice_dealloc_all_dynamic_ports(pf);
5467 ice_deinit_devlink(pf);
5468
5469 ice_unload(pf);
5470 devl_unlock(priv_to_devlink(pf));
5471
5472 ice_deinit(pf);
5473 ice_vsi_release_all(pf);
5474
5475 ice_setup_mc_magic_wake(pf);
5476 ice_set_wake(pf);
5477
5478 ice_adapter_put(pdev);
5479}
5480
5481/**
5482 * ice_shutdown - PCI callback for shutting down device
5483 * @pdev: PCI device information struct
5484 */
5485static void ice_shutdown(struct pci_dev *pdev)
5486{
5487 struct ice_pf *pf = pci_get_drvdata(pdev);
5488
5489 ice_remove(pdev);
5490
5491 if (system_state == SYSTEM_POWER_OFF) {
5492 pci_wake_from_d3(pdev, pf->wol_ena);
5493 pci_set_power_state(pdev, PCI_D3hot);
5494 }
5495}
5496
5497/**
5498 * ice_prepare_for_shutdown - prep for PCI shutdown
5499 * @pf: board private structure
5500 *
5501 * Inform or close all dependent features in prep for PCI device shutdown
5502 */
5503static void ice_prepare_for_shutdown(struct ice_pf *pf)
5504{
5505 struct ice_hw *hw = &pf->hw;
5506 u32 v;
5507
5508 /* Notify VFs of impending reset */
5509 if (ice_check_sq_alive(hw, &hw->mailboxq))
5510 ice_vc_notify_reset(pf);
5511
5512 dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
5513
5514 /* disable the VSIs and their queues that are not already DOWN */
5515 ice_pf_dis_all_vsi(pf, false);
5516
5517 ice_for_each_vsi(pf, v)
5518 if (pf->vsi[v])
5519 pf->vsi[v]->vsi_num = 0;
5520
5521 ice_shutdown_all_ctrlq(hw, true);
5522}
5523
5524/**
5525 * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
5526 * @pf: board private structure to reinitialize
5527 *
5528 * This routine reinitialize interrupt scheme that was cleared during
5529 * power management suspend callback.
5530 *
5531 * This should be called during resume routine to re-allocate the q_vectors
5532 * and reacquire interrupts.
5533 */
5534static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
5535{
5536 struct device *dev = ice_pf_to_dev(pf);
5537 int ret, v;
5538
5539 /* Since we clear MSIX flag during suspend, we need to
5540 * set it back during resume...
5541 */
5542
5543 ret = ice_init_interrupt_scheme(pf);
5544 if (ret) {
5545 dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
5546 return ret;
5547 }
5548
5549 /* Remap vectors and rings, after successful re-init interrupts */
5550 ice_for_each_vsi(pf, v) {
5551 if (!pf->vsi[v])
5552 continue;
5553
5554 ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
5555 if (ret)
5556 goto err_reinit;
5557 ice_vsi_map_rings_to_vectors(pf->vsi[v]);
5558 rtnl_lock();
5559 ice_vsi_set_napi_queues(pf->vsi[v]);
5560 rtnl_unlock();
5561 }
5562
5563 ret = ice_req_irq_msix_misc(pf);
5564 if (ret) {
5565 dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
5566 ret);
5567 goto err_reinit;
5568 }
5569
5570 return 0;
5571
5572err_reinit:
5573 while (v--)
5574 if (pf->vsi[v]) {
5575 rtnl_lock();
5576 ice_vsi_clear_napi_queues(pf->vsi[v]);
5577 rtnl_unlock();
5578 ice_vsi_free_q_vectors(pf->vsi[v]);
5579 }
5580
5581 return ret;
5582}
5583
5584/**
5585 * ice_suspend
5586 * @dev: generic device information structure
5587 *
5588 * Power Management callback to quiesce the device and prepare
5589 * for D3 transition.
5590 */
5591static int ice_suspend(struct device *dev)
5592{
5593 struct pci_dev *pdev = to_pci_dev(dev);
5594 struct ice_pf *pf;
5595 int disabled, v;
5596
5597 pf = pci_get_drvdata(pdev);
5598
5599 if (!ice_pf_state_is_nominal(pf)) {
5600 dev_err(dev, "Device is not ready, no need to suspend it\n");
5601 return -EBUSY;
5602 }
5603
5604 /* Stop watchdog tasks until resume completion.
5605 * Even though it is most likely that the service task is
5606 * disabled if the device is suspended or down, the service task's
5607 * state is controlled by a different state bit, and we should
5608 * store and honor whatever state that bit is in at this point.
5609 */
5610 disabled = ice_service_task_stop(pf);
5611
5612 ice_deinit_rdma(pf);
5613
5614 /* Already suspended?, then there is nothing to do */
5615 if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
5616 if (!disabled)
5617 ice_service_task_restart(pf);
5618 return 0;
5619 }
5620
5621 if (test_bit(ICE_DOWN, pf->state) ||
5622 ice_is_reset_in_progress(pf->state)) {
5623 dev_err(dev, "can't suspend device in reset or already down\n");
5624 if (!disabled)
5625 ice_service_task_restart(pf);
5626 return 0;
5627 }
5628
5629 ice_setup_mc_magic_wake(pf);
5630
5631 ice_prepare_for_shutdown(pf);
5632
5633 ice_set_wake(pf);
5634
5635 /* Free vectors, clear the interrupt scheme and release IRQs
5636 * for proper hibernation, especially with large number of CPUs.
5637 * Otherwise hibernation might fail when mapping all the vectors back
5638 * to CPU0.
5639 */
5640 ice_free_irq_msix_misc(pf);
5641 ice_for_each_vsi(pf, v) {
5642 if (!pf->vsi[v])
5643 continue;
5644 rtnl_lock();
5645 ice_vsi_clear_napi_queues(pf->vsi[v]);
5646 rtnl_unlock();
5647 ice_vsi_free_q_vectors(pf->vsi[v]);
5648 }
5649 ice_clear_interrupt_scheme(pf);
5650
5651 pci_save_state(pdev);
5652 pci_wake_from_d3(pdev, pf->wol_ena);
5653 pci_set_power_state(pdev, PCI_D3hot);
5654 return 0;
5655}
5656
5657/**
5658 * ice_resume - PM callback for waking up from D3
5659 * @dev: generic device information structure
5660 */
5661static int ice_resume(struct device *dev)
5662{
5663 struct pci_dev *pdev = to_pci_dev(dev);
5664 enum ice_reset_req reset_type;
5665 struct ice_pf *pf;
5666 struct ice_hw *hw;
5667 int ret;
5668
5669 pci_set_power_state(pdev, PCI_D0);
5670 pci_restore_state(pdev);
5671 pci_save_state(pdev);
5672
5673 if (!pci_device_is_present(pdev))
5674 return -ENODEV;
5675
5676 ret = pci_enable_device_mem(pdev);
5677 if (ret) {
5678 dev_err(dev, "Cannot enable device after suspend\n");
5679 return ret;
5680 }
5681
5682 pf = pci_get_drvdata(pdev);
5683 hw = &pf->hw;
5684
5685 pf->wakeup_reason = rd32(hw, PFPM_WUS);
5686 ice_print_wake_reason(pf);
5687
5688 /* We cleared the interrupt scheme when we suspended, so we need to
5689 * restore it now to resume device functionality.
5690 */
5691 ret = ice_reinit_interrupt_scheme(pf);
5692 if (ret)
5693 dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5694
5695 ret = ice_init_rdma(pf);
5696 if (ret)
5697 dev_err(dev, "Reinitialize RDMA during resume failed: %d\n",
5698 ret);
5699
5700 clear_bit(ICE_DOWN, pf->state);
5701 /* Now perform PF reset and rebuild */
5702 reset_type = ICE_RESET_PFR;
5703 /* re-enable service task for reset, but allow reset to schedule it */
5704 clear_bit(ICE_SERVICE_DIS, pf->state);
5705
5706 if (ice_schedule_reset(pf, reset_type))
5707 dev_err(dev, "Reset during resume failed.\n");
5708
5709 clear_bit(ICE_SUSPENDED, pf->state);
5710 ice_service_task_restart(pf);
5711
5712 /* Restart the service task */
5713 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5714
5715 return 0;
5716}
5717
5718/**
5719 * ice_pci_err_detected - warning that PCI error has been detected
5720 * @pdev: PCI device information struct
5721 * @err: the type of PCI error
5722 *
5723 * Called to warn that something happened on the PCI bus and the error handling
5724 * is in progress. Allows the driver to gracefully prepare/handle PCI errors.
5725 */
5726static pci_ers_result_t
5727ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
5728{
5729 struct ice_pf *pf = pci_get_drvdata(pdev);
5730
5731 if (!pf) {
5732 dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5733 __func__, err);
5734 return PCI_ERS_RESULT_DISCONNECT;
5735 }
5736
5737 if (!test_bit(ICE_SUSPENDED, pf->state)) {
5738 ice_service_task_stop(pf);
5739
5740 if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5741 set_bit(ICE_PFR_REQ, pf->state);
5742 ice_prepare_for_reset(pf, ICE_RESET_PFR);
5743 }
5744 }
5745
5746 return PCI_ERS_RESULT_NEED_RESET;
5747}
5748
5749/**
5750 * ice_pci_err_slot_reset - a PCI slot reset has just happened
5751 * @pdev: PCI device information struct
5752 *
5753 * Called to determine if the driver can recover from the PCI slot reset by
5754 * using a register read to determine if the device is recoverable.
5755 */
5756static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5757{
5758 struct ice_pf *pf = pci_get_drvdata(pdev);
5759 pci_ers_result_t result;
5760 int err;
5761 u32 reg;
5762
5763 err = pci_enable_device_mem(pdev);
5764 if (err) {
5765 dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
5766 err);
5767 result = PCI_ERS_RESULT_DISCONNECT;
5768 } else {
5769 pci_set_master(pdev);
5770 pci_restore_state(pdev);
5771 pci_save_state(pdev);
5772 pci_wake_from_d3(pdev, false);
5773
5774 /* Check for life */
5775 reg = rd32(&pf->hw, GLGEN_RTRIG);
5776 if (!reg)
5777 result = PCI_ERS_RESULT_RECOVERED;
5778 else
5779 result = PCI_ERS_RESULT_DISCONNECT;
5780 }
5781
5782 return result;
5783}
5784
5785/**
5786 * ice_pci_err_resume - restart operations after PCI error recovery
5787 * @pdev: PCI device information struct
5788 *
5789 * Called to allow the driver to bring things back up after PCI error and/or
5790 * reset recovery have finished
5791 */
5792static void ice_pci_err_resume(struct pci_dev *pdev)
5793{
5794 struct ice_pf *pf = pci_get_drvdata(pdev);
5795
5796 if (!pf) {
5797 dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5798 __func__);
5799 return;
5800 }
5801
5802 if (test_bit(ICE_SUSPENDED, pf->state)) {
5803 dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5804 __func__);
5805 return;
5806 }
5807
5808 ice_restore_all_vfs_msi_state(pf);
5809
5810 ice_do_reset(pf, ICE_RESET_PFR);
5811 ice_service_task_restart(pf);
5812 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5813}
5814
5815/**
5816 * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5817 * @pdev: PCI device information struct
5818 */
5819static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5820{
5821 struct ice_pf *pf = pci_get_drvdata(pdev);
5822
5823 if (!test_bit(ICE_SUSPENDED, pf->state)) {
5824 ice_service_task_stop(pf);
5825
5826 if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5827 set_bit(ICE_PFR_REQ, pf->state);
5828 ice_prepare_for_reset(pf, ICE_RESET_PFR);
5829 }
5830 }
5831}
5832
5833/**
5834 * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5835 * @pdev: PCI device information struct
5836 */
5837static void ice_pci_err_reset_done(struct pci_dev *pdev)
5838{
5839 ice_pci_err_resume(pdev);
5840}
5841
5842/* ice_pci_tbl - PCI Device ID Table
5843 *
5844 * Wildcard entries (PCI_ANY_ID) should come last
5845 * Last entry must be all 0s
5846 *
5847 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5848 * Class, Class Mask, private data (not used) }
5849 */
5850static const struct pci_device_id ice_pci_tbl[] = {
5851 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE) },
5852 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP) },
5853 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP) },
5854 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE) },
5855 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP) },
5856 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP) },
5857 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE) },
5858 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP) },
5859 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP) },
5860 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T) },
5861 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII) },
5862 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE) },
5863 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP) },
5864 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP) },
5865 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T) },
5866 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII) },
5867 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE) },
5868 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP) },
5869 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T) },
5870 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII) },
5871 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE) },
5872 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP) },
5873 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T) },
5874 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE) },
5875 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP) },
5876 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822_SI_DFLT) },
5877 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_BACKPLANE), },
5878 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_QSFP), },
5879 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_SFP), },
5880 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_SGMII), },
5881 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_BACKPLANE) },
5882 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_QSFP56) },
5883 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_SFP) },
5884 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_SFP_DD) },
5885 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_BACKPLANE), },
5886 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_BACKPLANE), },
5887 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_QSFP), },
5888 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_QSFP), },
5889 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_SFP), },
5890 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_SFP), },
5891 /* required last entry */
5892 {}
5893};
5894MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5895
5896static DEFINE_SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
5897
5898static const struct pci_error_handlers ice_pci_err_handler = {
5899 .error_detected = ice_pci_err_detected,
5900 .slot_reset = ice_pci_err_slot_reset,
5901 .reset_prepare = ice_pci_err_reset_prepare,
5902 .reset_done = ice_pci_err_reset_done,
5903 .resume = ice_pci_err_resume
5904};
5905
5906static struct pci_driver ice_driver = {
5907 .name = KBUILD_MODNAME,
5908 .id_table = ice_pci_tbl,
5909 .probe = ice_probe,
5910 .remove = ice_remove,
5911 .driver.pm = pm_sleep_ptr(&ice_pm_ops),
5912 .shutdown = ice_shutdown,
5913 .sriov_configure = ice_sriov_configure,
5914 .sriov_get_vf_total_msix = ice_sriov_get_vf_total_msix,
5915 .sriov_set_msix_vec_count = ice_sriov_set_msix_vec_count,
5916 .err_handler = &ice_pci_err_handler
5917};
5918
5919/**
5920 * ice_module_init - Driver registration routine
5921 *
5922 * ice_module_init is the first routine called when the driver is
5923 * loaded. All it does is register with the PCI subsystem.
5924 */
5925static int __init ice_module_init(void)
5926{
5927 int status = -ENOMEM;
5928
5929 pr_info("%s\n", ice_driver_string);
5930 pr_info("%s\n", ice_copyright);
5931
5932 ice_adv_lnk_speed_maps_init();
5933
5934 ice_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, KBUILD_MODNAME);
5935 if (!ice_wq) {
5936 pr_err("Failed to create workqueue\n");
5937 return status;
5938 }
5939
5940 ice_lag_wq = alloc_ordered_workqueue("ice_lag_wq", 0);
5941 if (!ice_lag_wq) {
5942 pr_err("Failed to create LAG workqueue\n");
5943 goto err_dest_wq;
5944 }
5945
5946 ice_debugfs_init();
5947
5948 status = pci_register_driver(&ice_driver);
5949 if (status) {
5950 pr_err("failed to register PCI driver, err %d\n", status);
5951 goto err_dest_lag_wq;
5952 }
5953
5954 status = ice_sf_driver_register();
5955 if (status) {
5956 pr_err("Failed to register SF driver, err %d\n", status);
5957 goto err_sf_driver;
5958 }
5959
5960 return 0;
5961
5962err_sf_driver:
5963 pci_unregister_driver(&ice_driver);
5964err_dest_lag_wq:
5965 destroy_workqueue(ice_lag_wq);
5966 ice_debugfs_exit();
5967err_dest_wq:
5968 destroy_workqueue(ice_wq);
5969 return status;
5970}
5971module_init(ice_module_init);
5972
5973/**
5974 * ice_module_exit - Driver exit cleanup routine
5975 *
5976 * ice_module_exit is called just before the driver is removed
5977 * from memory.
5978 */
5979static void __exit ice_module_exit(void)
5980{
5981 ice_sf_driver_unregister();
5982 pci_unregister_driver(&ice_driver);
5983 ice_debugfs_exit();
5984 destroy_workqueue(ice_wq);
5985 destroy_workqueue(ice_lag_wq);
5986 pr_info("module unloaded\n");
5987}
5988module_exit(ice_module_exit);
5989
5990/**
5991 * ice_set_mac_address - NDO callback to set MAC address
5992 * @netdev: network interface device structure
5993 * @pi: pointer to an address structure
5994 *
5995 * Returns 0 on success, negative on failure
5996 */
5997static int ice_set_mac_address(struct net_device *netdev, void *pi)
5998{
5999 struct ice_netdev_priv *np = netdev_priv(netdev);
6000 struct ice_vsi *vsi = np->vsi;
6001 struct ice_pf *pf = vsi->back;
6002 struct ice_hw *hw = &pf->hw;
6003 struct sockaddr *addr = pi;
6004 u8 old_mac[ETH_ALEN];
6005 u8 flags = 0;
6006 u8 *mac;
6007 int err;
6008
6009 mac = (u8 *)addr->sa_data;
6010
6011 if (!is_valid_ether_addr(mac))
6012 return -EADDRNOTAVAIL;
6013
6014 if (test_bit(ICE_DOWN, pf->state) ||
6015 ice_is_reset_in_progress(pf->state)) {
6016 netdev_err(netdev, "can't set mac %pM. device not ready\n",
6017 mac);
6018 return -EBUSY;
6019 }
6020
6021 if (ice_chnl_dmac_fltr_cnt(pf)) {
6022 netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
6023 mac);
6024 return -EAGAIN;
6025 }
6026
6027 netif_addr_lock_bh(netdev);
6028 ether_addr_copy(old_mac, netdev->dev_addr);
6029 /* change the netdev's MAC address */
6030 eth_hw_addr_set(netdev, mac);
6031 netif_addr_unlock_bh(netdev);
6032
6033 /* Clean up old MAC filter. Not an error if old filter doesn't exist */
6034 err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
6035 if (err && err != -ENOENT) {
6036 err = -EADDRNOTAVAIL;
6037 goto err_update_filters;
6038 }
6039
6040 /* Add filter for new MAC. If filter exists, return success */
6041 err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
6042 if (err == -EEXIST) {
6043 /* Although this MAC filter is already present in hardware it's
6044 * possible in some cases (e.g. bonding) that dev_addr was
6045 * modified outside of the driver and needs to be restored back
6046 * to this value.
6047 */
6048 netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
6049
6050 return 0;
6051 } else if (err) {
6052 /* error if the new filter addition failed */
6053 err = -EADDRNOTAVAIL;
6054 }
6055
6056err_update_filters:
6057 if (err) {
6058 netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
6059 mac);
6060 netif_addr_lock_bh(netdev);
6061 eth_hw_addr_set(netdev, old_mac);
6062 netif_addr_unlock_bh(netdev);
6063 return err;
6064 }
6065
6066 netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
6067 netdev->dev_addr);
6068
6069 /* write new MAC address to the firmware */
6070 flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
6071 err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
6072 if (err) {
6073 netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
6074 mac, err);
6075 }
6076 return 0;
6077}
6078
6079/**
6080 * ice_set_rx_mode - NDO callback to set the netdev filters
6081 * @netdev: network interface device structure
6082 */
6083static void ice_set_rx_mode(struct net_device *netdev)
6084{
6085 struct ice_netdev_priv *np = netdev_priv(netdev);
6086 struct ice_vsi *vsi = np->vsi;
6087
6088 if (!vsi || ice_is_switchdev_running(vsi->back))
6089 return;
6090
6091 /* Set the flags to synchronize filters
6092 * ndo_set_rx_mode may be triggered even without a change in netdev
6093 * flags
6094 */
6095 set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
6096 set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
6097 set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
6098
6099 /* schedule our worker thread which will take care of
6100 * applying the new filter changes
6101 */
6102 ice_service_task_schedule(vsi->back);
6103}
6104
6105/**
6106 * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
6107 * @netdev: network interface device structure
6108 * @queue_index: Queue ID
6109 * @maxrate: maximum bandwidth in Mbps
6110 */
6111static int
6112ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
6113{
6114 struct ice_netdev_priv *np = netdev_priv(netdev);
6115 struct ice_vsi *vsi = np->vsi;
6116 u16 q_handle;
6117 int status;
6118 u8 tc;
6119
6120 /* Validate maxrate requested is within permitted range */
6121 if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
6122 netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
6123 maxrate, queue_index);
6124 return -EINVAL;
6125 }
6126
6127 q_handle = vsi->tx_rings[queue_index]->q_handle;
6128 tc = ice_dcb_get_tc(vsi, queue_index);
6129
6130 vsi = ice_locate_vsi_using_queue(vsi, queue_index);
6131 if (!vsi) {
6132 netdev_err(netdev, "Invalid VSI for given queue %d\n",
6133 queue_index);
6134 return -EINVAL;
6135 }
6136
6137 /* Set BW back to default, when user set maxrate to 0 */
6138 if (!maxrate)
6139 status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
6140 q_handle, ICE_MAX_BW);
6141 else
6142 status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
6143 q_handle, ICE_MAX_BW, maxrate * 1000);
6144 if (status)
6145 netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
6146 status);
6147
6148 return status;
6149}
6150
6151/**
6152 * ice_fdb_add - add an entry to the hardware database
6153 * @ndm: the input from the stack
6154 * @tb: pointer to array of nladdr (unused)
6155 * @dev: the net device pointer
6156 * @addr: the MAC address entry being added
6157 * @vid: VLAN ID
6158 * @flags: instructions from stack about fdb operation
6159 * @notified: whether notification was emitted
6160 * @extack: netlink extended ack
6161 */
6162static int
6163ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
6164 struct net_device *dev, const unsigned char *addr, u16 vid,
6165 u16 flags, bool *notified,
6166 struct netlink_ext_ack __always_unused *extack)
6167{
6168 int err;
6169
6170 if (vid) {
6171 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
6172 return -EINVAL;
6173 }
6174 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6175 netdev_err(dev, "FDB only supports static addresses\n");
6176 return -EINVAL;
6177 }
6178
6179 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6180 err = dev_uc_add_excl(dev, addr);
6181 else if (is_multicast_ether_addr(addr))
6182 err = dev_mc_add_excl(dev, addr);
6183 else
6184 err = -EINVAL;
6185
6186 /* Only return duplicate errors if NLM_F_EXCL is set */
6187 if (err == -EEXIST && !(flags & NLM_F_EXCL))
6188 err = 0;
6189
6190 return err;
6191}
6192
6193/**
6194 * ice_fdb_del - delete an entry from the hardware database
6195 * @ndm: the input from the stack
6196 * @tb: pointer to array of nladdr (unused)
6197 * @dev: the net device pointer
6198 * @addr: the MAC address entry being added
6199 * @vid: VLAN ID
6200 * @notified: whether notification was emitted
6201 * @extack: netlink extended ack
6202 */
6203static int
6204ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
6205 struct net_device *dev, const unsigned char *addr,
6206 __always_unused u16 vid, bool *notified,
6207 struct netlink_ext_ack *extack)
6208{
6209 int err;
6210
6211 if (ndm->ndm_state & NUD_PERMANENT) {
6212 netdev_err(dev, "FDB only supports static addresses\n");
6213 return -EINVAL;
6214 }
6215
6216 if (is_unicast_ether_addr(addr))
6217 err = dev_uc_del(dev, addr);
6218 else if (is_multicast_ether_addr(addr))
6219 err = dev_mc_del(dev, addr);
6220 else
6221 err = -EINVAL;
6222
6223 return err;
6224}
6225
6226#define NETIF_VLAN_OFFLOAD_FEATURES (NETIF_F_HW_VLAN_CTAG_RX | \
6227 NETIF_F_HW_VLAN_CTAG_TX | \
6228 NETIF_F_HW_VLAN_STAG_RX | \
6229 NETIF_F_HW_VLAN_STAG_TX)
6230
6231#define NETIF_VLAN_STRIPPING_FEATURES (NETIF_F_HW_VLAN_CTAG_RX | \
6232 NETIF_F_HW_VLAN_STAG_RX)
6233
6234#define NETIF_VLAN_FILTERING_FEATURES (NETIF_F_HW_VLAN_CTAG_FILTER | \
6235 NETIF_F_HW_VLAN_STAG_FILTER)
6236
6237/**
6238 * ice_fix_features - fix the netdev features flags based on device limitations
6239 * @netdev: ptr to the netdev that flags are being fixed on
6240 * @features: features that need to be checked and possibly fixed
6241 *
6242 * Make sure any fixups are made to features in this callback. This enables the
6243 * driver to not have to check unsupported configurations throughout the driver
6244 * because that's the responsiblity of this callback.
6245 *
6246 * Single VLAN Mode (SVM) Supported Features:
6247 * NETIF_F_HW_VLAN_CTAG_FILTER
6248 * NETIF_F_HW_VLAN_CTAG_RX
6249 * NETIF_F_HW_VLAN_CTAG_TX
6250 *
6251 * Double VLAN Mode (DVM) Supported Features:
6252 * NETIF_F_HW_VLAN_CTAG_FILTER
6253 * NETIF_F_HW_VLAN_CTAG_RX
6254 * NETIF_F_HW_VLAN_CTAG_TX
6255 *
6256 * NETIF_F_HW_VLAN_STAG_FILTER
6257 * NETIF_HW_VLAN_STAG_RX
6258 * NETIF_HW_VLAN_STAG_TX
6259 *
6260 * Features that need fixing:
6261 * Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
6262 * These are mutually exlusive as the VSI context cannot support multiple
6263 * VLAN ethertypes simultaneously for stripping and/or insertion. If this
6264 * is not done, then default to clearing the requested STAG offload
6265 * settings.
6266 *
6267 * All supported filtering has to be enabled or disabled together. For
6268 * example, in DVM, CTAG and STAG filtering have to be enabled and disabled
6269 * together. If this is not done, then default to VLAN filtering disabled.
6270 * These are mutually exclusive as there is currently no way to
6271 * enable/disable VLAN filtering based on VLAN ethertype when using VLAN
6272 * prune rules.
6273 */
6274static netdev_features_t
6275ice_fix_features(struct net_device *netdev, netdev_features_t features)
6276{
6277 struct ice_netdev_priv *np = netdev_priv(netdev);
6278 netdev_features_t req_vlan_fltr, cur_vlan_fltr;
6279 bool cur_ctag, cur_stag, req_ctag, req_stag;
6280
6281 cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
6282 cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6283 cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6284
6285 req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
6286 req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6287 req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6288
6289 if (req_vlan_fltr != cur_vlan_fltr) {
6290 if (ice_is_dvm_ena(&np->vsi->back->hw)) {
6291 if (req_ctag && req_stag) {
6292 features |= NETIF_VLAN_FILTERING_FEATURES;
6293 } else if (!req_ctag && !req_stag) {
6294 features &= ~NETIF_VLAN_FILTERING_FEATURES;
6295 } else if ((!cur_ctag && req_ctag && !cur_stag) ||
6296 (!cur_stag && req_stag && !cur_ctag)) {
6297 features |= NETIF_VLAN_FILTERING_FEATURES;
6298 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");
6299 } else if ((cur_ctag && !req_ctag && cur_stag) ||
6300 (cur_stag && !req_stag && cur_ctag)) {
6301 features &= ~NETIF_VLAN_FILTERING_FEATURES;
6302 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");
6303 }
6304 } else {
6305 if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
6306 netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
6307
6308 if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
6309 features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6310 }
6311 }
6312
6313 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
6314 (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
6315 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");
6316 features &= ~(NETIF_F_HW_VLAN_STAG_RX |
6317 NETIF_F_HW_VLAN_STAG_TX);
6318 }
6319
6320 if (!(netdev->features & NETIF_F_RXFCS) &&
6321 (features & NETIF_F_RXFCS) &&
6322 (features & NETIF_VLAN_STRIPPING_FEATURES) &&
6323 !ice_vsi_has_non_zero_vlans(np->vsi)) {
6324 netdev_warn(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
6325 features &= ~NETIF_VLAN_STRIPPING_FEATURES;
6326 }
6327
6328 return features;
6329}
6330
6331/**
6332 * ice_set_rx_rings_vlan_proto - update rings with new stripped VLAN proto
6333 * @vsi: PF's VSI
6334 * @vlan_ethertype: VLAN ethertype (802.1Q or 802.1ad) in network byte order
6335 *
6336 * Store current stripped VLAN proto in ring packet context,
6337 * so it can be accessed more efficiently by packet processing code.
6338 */
6339static void
6340ice_set_rx_rings_vlan_proto(struct ice_vsi *vsi, __be16 vlan_ethertype)
6341{
6342 u16 i;
6343
6344 ice_for_each_alloc_rxq(vsi, i)
6345 vsi->rx_rings[i]->pkt_ctx.vlan_proto = vlan_ethertype;
6346}
6347
6348/**
6349 * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
6350 * @vsi: PF's VSI
6351 * @features: features used to determine VLAN offload settings
6352 *
6353 * First, determine the vlan_ethertype based on the VLAN offload bits in
6354 * features. Then determine if stripping and insertion should be enabled or
6355 * disabled. Finally enable or disable VLAN stripping and insertion.
6356 */
6357static int
6358ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
6359{
6360 bool enable_stripping = true, enable_insertion = true;
6361 struct ice_vsi_vlan_ops *vlan_ops;
6362 int strip_err = 0, insert_err = 0;
6363 u16 vlan_ethertype = 0;
6364
6365 vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6366
6367 if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
6368 vlan_ethertype = ETH_P_8021AD;
6369 else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
6370 vlan_ethertype = ETH_P_8021Q;
6371
6372 if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
6373 enable_stripping = false;
6374 if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
6375 enable_insertion = false;
6376
6377 if (enable_stripping)
6378 strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
6379 else
6380 strip_err = vlan_ops->dis_stripping(vsi);
6381
6382 if (enable_insertion)
6383 insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
6384 else
6385 insert_err = vlan_ops->dis_insertion(vsi);
6386
6387 if (strip_err || insert_err)
6388 return -EIO;
6389
6390 ice_set_rx_rings_vlan_proto(vsi, enable_stripping ?
6391 htons(vlan_ethertype) : 0);
6392
6393 return 0;
6394}
6395
6396/**
6397 * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
6398 * @vsi: PF's VSI
6399 * @features: features used to determine VLAN filtering settings
6400 *
6401 * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
6402 * features.
6403 */
6404static int
6405ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
6406{
6407 struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6408 int err = 0;
6409
6410 /* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
6411 * if either bit is set. In switchdev mode Rx filtering should never be
6412 * enabled.
6413 */
6414 if ((features &
6415 (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)) &&
6416 !ice_is_eswitch_mode_switchdev(vsi->back))
6417 err = vlan_ops->ena_rx_filtering(vsi);
6418 else
6419 err = vlan_ops->dis_rx_filtering(vsi);
6420
6421 return err;
6422}
6423
6424/**
6425 * ice_set_vlan_features - set VLAN settings based on suggested feature set
6426 * @netdev: ptr to the netdev being adjusted
6427 * @features: the feature set that the stack is suggesting
6428 *
6429 * Only update VLAN settings if the requested_vlan_features are different than
6430 * the current_vlan_features.
6431 */
6432static int
6433ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
6434{
6435 netdev_features_t current_vlan_features, requested_vlan_features;
6436 struct ice_netdev_priv *np = netdev_priv(netdev);
6437 struct ice_vsi *vsi = np->vsi;
6438 int err;
6439
6440 current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
6441 requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
6442 if (current_vlan_features ^ requested_vlan_features) {
6443 if ((features & NETIF_F_RXFCS) &&
6444 (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6445 dev_err(ice_pf_to_dev(vsi->back),
6446 "To enable VLAN stripping, you must first enable FCS/CRC stripping\n");
6447 return -EIO;
6448 }
6449
6450 err = ice_set_vlan_offload_features(vsi, features);
6451 if (err)
6452 return err;
6453 }
6454
6455 current_vlan_features = netdev->features &
6456 NETIF_VLAN_FILTERING_FEATURES;
6457 requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
6458 if (current_vlan_features ^ requested_vlan_features) {
6459 err = ice_set_vlan_filtering_features(vsi, features);
6460 if (err)
6461 return err;
6462 }
6463
6464 return 0;
6465}
6466
6467/**
6468 * ice_set_loopback - turn on/off loopback mode on underlying PF
6469 * @vsi: ptr to VSI
6470 * @ena: flag to indicate the on/off setting
6471 */
6472static int ice_set_loopback(struct ice_vsi *vsi, bool ena)
6473{
6474 bool if_running = netif_running(vsi->netdev);
6475 int ret;
6476
6477 if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
6478 ret = ice_down(vsi);
6479 if (ret) {
6480 netdev_err(vsi->netdev, "Preparing device to toggle loopback failed\n");
6481 return ret;
6482 }
6483 }
6484 ret = ice_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
6485 if (ret)
6486 netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
6487 if (if_running)
6488 ret = ice_up(vsi);
6489
6490 return ret;
6491}
6492
6493/**
6494 * ice_set_features - set the netdev feature flags
6495 * @netdev: ptr to the netdev being adjusted
6496 * @features: the feature set that the stack is suggesting
6497 */
6498static int
6499ice_set_features(struct net_device *netdev, netdev_features_t features)
6500{
6501 netdev_features_t changed = netdev->features ^ features;
6502 struct ice_netdev_priv *np = netdev_priv(netdev);
6503 struct ice_vsi *vsi = np->vsi;
6504 struct ice_pf *pf = vsi->back;
6505 int ret = 0;
6506
6507 /* Don't set any netdev advanced features with device in Safe Mode */
6508 if (ice_is_safe_mode(pf)) {
6509 dev_err(ice_pf_to_dev(pf),
6510 "Device is in Safe Mode - not enabling advanced netdev features\n");
6511 return ret;
6512 }
6513
6514 /* Do not change setting during reset */
6515 if (ice_is_reset_in_progress(pf->state)) {
6516 dev_err(ice_pf_to_dev(pf),
6517 "Device is resetting, changing advanced netdev features temporarily unavailable.\n");
6518 return -EBUSY;
6519 }
6520
6521 /* Multiple features can be changed in one call so keep features in
6522 * separate if/else statements to guarantee each feature is checked
6523 */
6524 if (changed & NETIF_F_RXHASH)
6525 ice_vsi_manage_rss_lut(vsi, !!(features & NETIF_F_RXHASH));
6526
6527 ret = ice_set_vlan_features(netdev, features);
6528 if (ret)
6529 return ret;
6530
6531 /* Turn on receive of FCS aka CRC, and after setting this
6532 * flag the packet data will have the 4 byte CRC appended
6533 */
6534 if (changed & NETIF_F_RXFCS) {
6535 if ((features & NETIF_F_RXFCS) &&
6536 (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6537 dev_err(ice_pf_to_dev(vsi->back),
6538 "To disable FCS/CRC stripping, you must first disable VLAN stripping\n");
6539 return -EIO;
6540 }
6541
6542 ice_vsi_cfg_crc_strip(vsi, !!(features & NETIF_F_RXFCS));
6543 ret = ice_down_up(vsi);
6544 if (ret)
6545 return ret;
6546 }
6547
6548 if (changed & NETIF_F_NTUPLE) {
6549 bool ena = !!(features & NETIF_F_NTUPLE);
6550
6551 ice_vsi_manage_fdir(vsi, ena);
6552 ena ? ice_init_arfs(vsi) : ice_clear_arfs(vsi);
6553 }
6554
6555 /* don't turn off hw_tc_offload when ADQ is already enabled */
6556 if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
6557 dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
6558 return -EACCES;
6559 }
6560
6561 if (changed & NETIF_F_HW_TC) {
6562 bool ena = !!(features & NETIF_F_HW_TC);
6563
6564 assign_bit(ICE_FLAG_CLS_FLOWER, pf->flags, ena);
6565 }
6566
6567 if (changed & NETIF_F_LOOPBACK)
6568 ret = ice_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
6569
6570 return ret;
6571}
6572
6573/**
6574 * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
6575 * @vsi: VSI to setup VLAN properties for
6576 */
6577static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
6578{
6579 int err;
6580
6581 err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
6582 if (err)
6583 return err;
6584
6585 err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
6586 if (err)
6587 return err;
6588
6589 return ice_vsi_add_vlan_zero(vsi);
6590}
6591
6592/**
6593 * ice_vsi_cfg_lan - Setup the VSI lan related config
6594 * @vsi: the VSI being configured
6595 *
6596 * Return 0 on success and negative value on error
6597 */
6598int ice_vsi_cfg_lan(struct ice_vsi *vsi)
6599{
6600 int err;
6601
6602 if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6603 ice_set_rx_mode(vsi->netdev);
6604
6605 err = ice_vsi_vlan_setup(vsi);
6606 if (err)
6607 return err;
6608 }
6609 ice_vsi_cfg_dcb_rings(vsi);
6610
6611 err = ice_vsi_cfg_lan_txqs(vsi);
6612 if (!err && ice_is_xdp_ena_vsi(vsi))
6613 err = ice_vsi_cfg_xdp_txqs(vsi);
6614 if (!err)
6615 err = ice_vsi_cfg_rxqs(vsi);
6616
6617 return err;
6618}
6619
6620/* THEORY OF MODERATION:
6621 * The ice driver hardware works differently than the hardware that DIMLIB was
6622 * originally made for. ice hardware doesn't have packet count limits that
6623 * can trigger an interrupt, but it *does* have interrupt rate limit support,
6624 * which is hard-coded to a limit of 250,000 ints/second.
6625 * If not using dynamic moderation, the INTRL value can be modified
6626 * by ethtool rx-usecs-high.
6627 */
6628struct ice_dim {
6629 /* the throttle rate for interrupts, basically worst case delay before
6630 * an initial interrupt fires, value is stored in microseconds.
6631 */
6632 u16 itr;
6633};
6634
6635/* Make a different profile for Rx that doesn't allow quite so aggressive
6636 * moderation at the high end (it maxes out at 126us or about 8k interrupts a
6637 * second.
6638 */
6639static const struct ice_dim rx_profile[] = {
6640 {2}, /* 500,000 ints/s, capped at 250K by INTRL */
6641 {8}, /* 125,000 ints/s */
6642 {16}, /* 62,500 ints/s */
6643 {62}, /* 16,129 ints/s */
6644 {126} /* 7,936 ints/s */
6645};
6646
6647/* The transmit profile, which has the same sorts of values
6648 * as the previous struct
6649 */
6650static const struct ice_dim tx_profile[] = {
6651 {2}, /* 500,000 ints/s, capped at 250K by INTRL */
6652 {8}, /* 125,000 ints/s */
6653 {40}, /* 16,125 ints/s */
6654 {128}, /* 7,812 ints/s */
6655 {256} /* 3,906 ints/s */
6656};
6657
6658static void ice_tx_dim_work(struct work_struct *work)
6659{
6660 struct ice_ring_container *rc;
6661 struct dim *dim;
6662 u16 itr;
6663
6664 dim = container_of(work, struct dim, work);
6665 rc = dim->priv;
6666
6667 WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
6668
6669 /* look up the values in our local table */
6670 itr = tx_profile[dim->profile_ix].itr;
6671
6672 ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
6673 ice_write_itr(rc, itr);
6674
6675 dim->state = DIM_START_MEASURE;
6676}
6677
6678static void ice_rx_dim_work(struct work_struct *work)
6679{
6680 struct ice_ring_container *rc;
6681 struct dim *dim;
6682 u16 itr;
6683
6684 dim = container_of(work, struct dim, work);
6685 rc = dim->priv;
6686
6687 WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
6688
6689 /* look up the values in our local table */
6690 itr = rx_profile[dim->profile_ix].itr;
6691
6692 ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
6693 ice_write_itr(rc, itr);
6694
6695 dim->state = DIM_START_MEASURE;
6696}
6697
6698#define ICE_DIM_DEFAULT_PROFILE_IX 1
6699
6700/**
6701 * ice_init_moderation - set up interrupt moderation
6702 * @q_vector: the vector containing rings to be configured
6703 *
6704 * Set up interrupt moderation registers, with the intent to do the right thing
6705 * when called from reset or from probe, and whether or not dynamic moderation
6706 * is enabled or not. Take special care to write all the registers in both
6707 * dynamic moderation mode or not in order to make sure hardware is in a known
6708 * state.
6709 */
6710static void ice_init_moderation(struct ice_q_vector *q_vector)
6711{
6712 struct ice_ring_container *rc;
6713 bool tx_dynamic, rx_dynamic;
6714
6715 rc = &q_vector->tx;
6716 INIT_WORK(&rc->dim.work, ice_tx_dim_work);
6717 rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6718 rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6719 rc->dim.priv = rc;
6720 tx_dynamic = ITR_IS_DYNAMIC(rc);
6721
6722 /* set the initial TX ITR to match the above */
6723 ice_write_itr(rc, tx_dynamic ?
6724 tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
6725
6726 rc = &q_vector->rx;
6727 INIT_WORK(&rc->dim.work, ice_rx_dim_work);
6728 rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6729 rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6730 rc->dim.priv = rc;
6731 rx_dynamic = ITR_IS_DYNAMIC(rc);
6732
6733 /* set the initial RX ITR to match the above */
6734 ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
6735 rc->itr_setting);
6736
6737 ice_set_q_vector_intrl(q_vector);
6738}
6739
6740/**
6741 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
6742 * @vsi: the VSI being configured
6743 */
6744static void ice_napi_enable_all(struct ice_vsi *vsi)
6745{
6746 int q_idx;
6747
6748 if (!vsi->netdev)
6749 return;
6750
6751 ice_for_each_q_vector(vsi, q_idx) {
6752 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6753
6754 ice_init_moderation(q_vector);
6755
6756 if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6757 napi_enable(&q_vector->napi);
6758 }
6759}
6760
6761/**
6762 * ice_up_complete - Finish the last steps of bringing up a connection
6763 * @vsi: The VSI being configured
6764 *
6765 * Return 0 on success and negative value on error
6766 */
6767static int ice_up_complete(struct ice_vsi *vsi)
6768{
6769 struct ice_pf *pf = vsi->back;
6770 int err;
6771
6772 ice_vsi_cfg_msix(vsi);
6773
6774 /* Enable only Rx rings, Tx rings were enabled by the FW when the
6775 * Tx queue group list was configured and the context bits were
6776 * programmed using ice_vsi_cfg_txqs
6777 */
6778 err = ice_vsi_start_all_rx_rings(vsi);
6779 if (err)
6780 return err;
6781
6782 clear_bit(ICE_VSI_DOWN, vsi->state);
6783 ice_napi_enable_all(vsi);
6784 ice_vsi_ena_irq(vsi);
6785
6786 if (vsi->port_info &&
6787 (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
6788 ((vsi->netdev && (vsi->type == ICE_VSI_PF ||
6789 vsi->type == ICE_VSI_SF)))) {
6790 ice_print_link_msg(vsi, true);
6791 netif_tx_start_all_queues(vsi->netdev);
6792 netif_carrier_on(vsi->netdev);
6793 ice_ptp_link_change(pf, true);
6794 }
6795
6796 /* Perform an initial read of the statistics registers now to
6797 * set the baseline so counters are ready when interface is up
6798 */
6799 ice_update_eth_stats(vsi);
6800
6801 if (vsi->type == ICE_VSI_PF)
6802 ice_service_task_schedule(pf);
6803
6804 return 0;
6805}
6806
6807/**
6808 * ice_up - Bring the connection back up after being down
6809 * @vsi: VSI being configured
6810 */
6811int ice_up(struct ice_vsi *vsi)
6812{
6813 int err;
6814
6815 err = ice_vsi_cfg_lan(vsi);
6816 if (!err)
6817 err = ice_up_complete(vsi);
6818
6819 return err;
6820}
6821
6822/**
6823 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
6824 * @syncp: pointer to u64_stats_sync
6825 * @stats: stats that pkts and bytes count will be taken from
6826 * @pkts: packets stats counter
6827 * @bytes: bytes stats counter
6828 *
6829 * This function fetches stats from the ring considering the atomic operations
6830 * that needs to be performed to read u64 values in 32 bit machine.
6831 */
6832void
6833ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
6834 struct ice_q_stats stats, u64 *pkts, u64 *bytes)
6835{
6836 unsigned int start;
6837
6838 do {
6839 start = u64_stats_fetch_begin(syncp);
6840 *pkts = stats.pkts;
6841 *bytes = stats.bytes;
6842 } while (u64_stats_fetch_retry(syncp, start));
6843}
6844
6845/**
6846 * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
6847 * @vsi: the VSI to be updated
6848 * @vsi_stats: the stats struct to be updated
6849 * @rings: rings to work on
6850 * @count: number of rings
6851 */
6852static void
6853ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
6854 struct rtnl_link_stats64 *vsi_stats,
6855 struct ice_tx_ring **rings, u16 count)
6856{
6857 u16 i;
6858
6859 for (i = 0; i < count; i++) {
6860 struct ice_tx_ring *ring;
6861 u64 pkts = 0, bytes = 0;
6862
6863 ring = READ_ONCE(rings[i]);
6864 if (!ring || !ring->ring_stats)
6865 continue;
6866 ice_fetch_u64_stats_per_ring(&ring->ring_stats->syncp,
6867 ring->ring_stats->stats, &pkts,
6868 &bytes);
6869 vsi_stats->tx_packets += pkts;
6870 vsi_stats->tx_bytes += bytes;
6871 vsi->tx_restart += ring->ring_stats->tx_stats.restart_q;
6872 vsi->tx_busy += ring->ring_stats->tx_stats.tx_busy;
6873 vsi->tx_linearize += ring->ring_stats->tx_stats.tx_linearize;
6874 }
6875}
6876
6877/**
6878 * ice_update_vsi_ring_stats - Update VSI stats counters
6879 * @vsi: the VSI to be updated
6880 */
6881static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
6882{
6883 struct rtnl_link_stats64 *net_stats, *stats_prev;
6884 struct rtnl_link_stats64 *vsi_stats;
6885 struct ice_pf *pf = vsi->back;
6886 u64 pkts, bytes;
6887 int i;
6888
6889 vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
6890 if (!vsi_stats)
6891 return;
6892
6893 /* reset non-netdev (extended) stats */
6894 vsi->tx_restart = 0;
6895 vsi->tx_busy = 0;
6896 vsi->tx_linearize = 0;
6897 vsi->rx_buf_failed = 0;
6898 vsi->rx_page_failed = 0;
6899
6900 rcu_read_lock();
6901
6902 /* update Tx rings counters */
6903 ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
6904 vsi->num_txq);
6905
6906 /* update Rx rings counters */
6907 ice_for_each_rxq(vsi, i) {
6908 struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
6909 struct ice_ring_stats *ring_stats;
6910
6911 ring_stats = ring->ring_stats;
6912 ice_fetch_u64_stats_per_ring(&ring_stats->syncp,
6913 ring_stats->stats, &pkts,
6914 &bytes);
6915 vsi_stats->rx_packets += pkts;
6916 vsi_stats->rx_bytes += bytes;
6917 vsi->rx_buf_failed += ring_stats->rx_stats.alloc_buf_failed;
6918 vsi->rx_page_failed += ring_stats->rx_stats.alloc_page_failed;
6919 }
6920
6921 /* update XDP Tx rings counters */
6922 if (ice_is_xdp_ena_vsi(vsi))
6923 ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
6924 vsi->num_xdp_txq);
6925
6926 rcu_read_unlock();
6927
6928 net_stats = &vsi->net_stats;
6929 stats_prev = &vsi->net_stats_prev;
6930
6931 /* Update netdev counters, but keep in mind that values could start at
6932 * random value after PF reset. And as we increase the reported stat by
6933 * diff of Prev-Cur, we need to be sure that Prev is valid. If it's not,
6934 * let's skip this round.
6935 */
6936 if (likely(pf->stat_prev_loaded)) {
6937 net_stats->tx_packets += vsi_stats->tx_packets - stats_prev->tx_packets;
6938 net_stats->tx_bytes += vsi_stats->tx_bytes - stats_prev->tx_bytes;
6939 net_stats->rx_packets += vsi_stats->rx_packets - stats_prev->rx_packets;
6940 net_stats->rx_bytes += vsi_stats->rx_bytes - stats_prev->rx_bytes;
6941 }
6942
6943 stats_prev->tx_packets = vsi_stats->tx_packets;
6944 stats_prev->tx_bytes = vsi_stats->tx_bytes;
6945 stats_prev->rx_packets = vsi_stats->rx_packets;
6946 stats_prev->rx_bytes = vsi_stats->rx_bytes;
6947
6948 kfree(vsi_stats);
6949}
6950
6951/**
6952 * ice_update_vsi_stats - Update VSI stats counters
6953 * @vsi: the VSI to be updated
6954 */
6955void ice_update_vsi_stats(struct ice_vsi *vsi)
6956{
6957 struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6958 struct ice_eth_stats *cur_es = &vsi->eth_stats;
6959 struct ice_pf *pf = vsi->back;
6960
6961 if (test_bit(ICE_VSI_DOWN, vsi->state) ||
6962 test_bit(ICE_CFG_BUSY, pf->state))
6963 return;
6964
6965 /* get stats as recorded by Tx/Rx rings */
6966 ice_update_vsi_ring_stats(vsi);
6967
6968 /* get VSI stats as recorded by the hardware */
6969 ice_update_eth_stats(vsi);
6970
6971 cur_ns->tx_errors = cur_es->tx_errors;
6972 cur_ns->rx_dropped = cur_es->rx_discards;
6973 cur_ns->tx_dropped = cur_es->tx_discards;
6974 cur_ns->multicast = cur_es->rx_multicast;
6975
6976 /* update some more netdev stats if this is main VSI */
6977 if (vsi->type == ICE_VSI_PF) {
6978 cur_ns->rx_crc_errors = pf->stats.crc_errors;
6979 cur_ns->rx_errors = pf->stats.crc_errors +
6980 pf->stats.illegal_bytes +
6981 pf->stats.rx_undersize +
6982 pf->hw_csum_rx_error +
6983 pf->stats.rx_jabber +
6984 pf->stats.rx_fragments +
6985 pf->stats.rx_oversize;
6986 /* record drops from the port level */
6987 cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
6988 }
6989}
6990
6991/**
6992 * ice_update_pf_stats - Update PF port stats counters
6993 * @pf: PF whose stats needs to be updated
6994 */
6995void ice_update_pf_stats(struct ice_pf *pf)
6996{
6997 struct ice_hw_port_stats *prev_ps, *cur_ps;
6998 struct ice_hw *hw = &pf->hw;
6999 u16 fd_ctr_base;
7000 u8 port;
7001
7002 port = hw->port_info->lport;
7003 prev_ps = &pf->stats_prev;
7004 cur_ps = &pf->stats;
7005
7006 if (ice_is_reset_in_progress(pf->state))
7007 pf->stat_prev_loaded = false;
7008
7009 ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
7010 &prev_ps->eth.rx_bytes,
7011 &cur_ps->eth.rx_bytes);
7012
7013 ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
7014 &prev_ps->eth.rx_unicast,
7015 &cur_ps->eth.rx_unicast);
7016
7017 ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
7018 &prev_ps->eth.rx_multicast,
7019 &cur_ps->eth.rx_multicast);
7020
7021 ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
7022 &prev_ps->eth.rx_broadcast,
7023 &cur_ps->eth.rx_broadcast);
7024
7025 ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
7026 &prev_ps->eth.rx_discards,
7027 &cur_ps->eth.rx_discards);
7028
7029 ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
7030 &prev_ps->eth.tx_bytes,
7031 &cur_ps->eth.tx_bytes);
7032
7033 ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
7034 &prev_ps->eth.tx_unicast,
7035 &cur_ps->eth.tx_unicast);
7036
7037 ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
7038 &prev_ps->eth.tx_multicast,
7039 &cur_ps->eth.tx_multicast);
7040
7041 ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
7042 &prev_ps->eth.tx_broadcast,
7043 &cur_ps->eth.tx_broadcast);
7044
7045 ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
7046 &prev_ps->tx_dropped_link_down,
7047 &cur_ps->tx_dropped_link_down);
7048
7049 ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
7050 &prev_ps->rx_size_64, &cur_ps->rx_size_64);
7051
7052 ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
7053 &prev_ps->rx_size_127, &cur_ps->rx_size_127);
7054
7055 ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
7056 &prev_ps->rx_size_255, &cur_ps->rx_size_255);
7057
7058 ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
7059 &prev_ps->rx_size_511, &cur_ps->rx_size_511);
7060
7061 ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
7062 &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
7063
7064 ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
7065 &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
7066
7067 ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
7068 &prev_ps->rx_size_big, &cur_ps->rx_size_big);
7069
7070 ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
7071 &prev_ps->tx_size_64, &cur_ps->tx_size_64);
7072
7073 ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
7074 &prev_ps->tx_size_127, &cur_ps->tx_size_127);
7075
7076 ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
7077 &prev_ps->tx_size_255, &cur_ps->tx_size_255);
7078
7079 ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
7080 &prev_ps->tx_size_511, &cur_ps->tx_size_511);
7081
7082 ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
7083 &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
7084
7085 ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
7086 &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
7087
7088 ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
7089 &prev_ps->tx_size_big, &cur_ps->tx_size_big);
7090
7091 fd_ctr_base = hw->fd_ctr_base;
7092
7093 ice_stat_update40(hw,
7094 GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
7095 pf->stat_prev_loaded, &prev_ps->fd_sb_match,
7096 &cur_ps->fd_sb_match);
7097 ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
7098 &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
7099
7100 ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
7101 &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
7102
7103 ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
7104 &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
7105
7106 ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
7107 &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
7108
7109 ice_update_dcb_stats(pf);
7110
7111 ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
7112 &prev_ps->crc_errors, &cur_ps->crc_errors);
7113
7114 ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
7115 &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
7116
7117 ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
7118 &prev_ps->mac_local_faults,
7119 &cur_ps->mac_local_faults);
7120
7121 ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
7122 &prev_ps->mac_remote_faults,
7123 &cur_ps->mac_remote_faults);
7124
7125 ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
7126 &prev_ps->rx_undersize, &cur_ps->rx_undersize);
7127
7128 ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
7129 &prev_ps->rx_fragments, &cur_ps->rx_fragments);
7130
7131 ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
7132 &prev_ps->rx_oversize, &cur_ps->rx_oversize);
7133
7134 ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
7135 &prev_ps->rx_jabber, &cur_ps->rx_jabber);
7136
7137 cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
7138
7139 pf->stat_prev_loaded = true;
7140}
7141
7142/**
7143 * ice_get_stats64 - get statistics for network device structure
7144 * @netdev: network interface device structure
7145 * @stats: main device statistics structure
7146 */
7147void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
7148{
7149 struct ice_netdev_priv *np = netdev_priv(netdev);
7150 struct rtnl_link_stats64 *vsi_stats;
7151 struct ice_vsi *vsi = np->vsi;
7152
7153 vsi_stats = &vsi->net_stats;
7154
7155 if (!vsi->num_txq || !vsi->num_rxq)
7156 return;
7157
7158 /* netdev packet/byte stats come from ring counter. These are obtained
7159 * by summing up ring counters (done by ice_update_vsi_ring_stats).
7160 * But, only call the update routine and read the registers if VSI is
7161 * not down.
7162 */
7163 if (!test_bit(ICE_VSI_DOWN, vsi->state))
7164 ice_update_vsi_ring_stats(vsi);
7165 stats->tx_packets = vsi_stats->tx_packets;
7166 stats->tx_bytes = vsi_stats->tx_bytes;
7167 stats->rx_packets = vsi_stats->rx_packets;
7168 stats->rx_bytes = vsi_stats->rx_bytes;
7169
7170 /* The rest of the stats can be read from the hardware but instead we
7171 * just return values that the watchdog task has already obtained from
7172 * the hardware.
7173 */
7174 stats->multicast = vsi_stats->multicast;
7175 stats->tx_errors = vsi_stats->tx_errors;
7176 stats->tx_dropped = vsi_stats->tx_dropped;
7177 stats->rx_errors = vsi_stats->rx_errors;
7178 stats->rx_dropped = vsi_stats->rx_dropped;
7179 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
7180 stats->rx_length_errors = vsi_stats->rx_length_errors;
7181}
7182
7183/**
7184 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
7185 * @vsi: VSI having NAPI disabled
7186 */
7187static void ice_napi_disable_all(struct ice_vsi *vsi)
7188{
7189 int q_idx;
7190
7191 if (!vsi->netdev)
7192 return;
7193
7194 ice_for_each_q_vector(vsi, q_idx) {
7195 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
7196
7197 if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
7198 napi_disable(&q_vector->napi);
7199
7200 cancel_work_sync(&q_vector->tx.dim.work);
7201 cancel_work_sync(&q_vector->rx.dim.work);
7202 }
7203}
7204
7205/**
7206 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
7207 * @vsi: the VSI being un-configured
7208 */
7209static void ice_vsi_dis_irq(struct ice_vsi *vsi)
7210{
7211 struct ice_pf *pf = vsi->back;
7212 struct ice_hw *hw = &pf->hw;
7213 u32 val;
7214 int i;
7215
7216 /* disable interrupt causation from each Rx queue; Tx queues are
7217 * handled in ice_vsi_stop_tx_ring()
7218 */
7219 if (vsi->rx_rings) {
7220 ice_for_each_rxq(vsi, i) {
7221 if (vsi->rx_rings[i]) {
7222 u16 reg;
7223
7224 reg = vsi->rx_rings[i]->reg_idx;
7225 val = rd32(hw, QINT_RQCTL(reg));
7226 val &= ~QINT_RQCTL_CAUSE_ENA_M;
7227 wr32(hw, QINT_RQCTL(reg), val);
7228 }
7229 }
7230 }
7231
7232 /* disable each interrupt */
7233 ice_for_each_q_vector(vsi, i) {
7234 if (!vsi->q_vectors[i])
7235 continue;
7236 wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
7237 }
7238
7239 ice_flush(hw);
7240
7241 /* don't call synchronize_irq() for VF's from the host */
7242 if (vsi->type == ICE_VSI_VF)
7243 return;
7244
7245 ice_for_each_q_vector(vsi, i)
7246 synchronize_irq(vsi->q_vectors[i]->irq.virq);
7247}
7248
7249/**
7250 * ice_down - Shutdown the connection
7251 * @vsi: The VSI being stopped
7252 *
7253 * Caller of this function is expected to set the vsi->state ICE_DOWN bit
7254 */
7255int ice_down(struct ice_vsi *vsi)
7256{
7257 int i, tx_err, rx_err, vlan_err = 0;
7258
7259 WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
7260
7261 if (vsi->netdev) {
7262 vlan_err = ice_vsi_del_vlan_zero(vsi);
7263 ice_ptp_link_change(vsi->back, false);
7264 netif_carrier_off(vsi->netdev);
7265 netif_tx_disable(vsi->netdev);
7266 }
7267
7268 ice_vsi_dis_irq(vsi);
7269
7270 tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
7271 if (tx_err)
7272 netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
7273 vsi->vsi_num, tx_err);
7274 if (!tx_err && vsi->xdp_rings) {
7275 tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
7276 if (tx_err)
7277 netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
7278 vsi->vsi_num, tx_err);
7279 }
7280
7281 rx_err = ice_vsi_stop_all_rx_rings(vsi);
7282 if (rx_err)
7283 netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
7284 vsi->vsi_num, rx_err);
7285
7286 ice_napi_disable_all(vsi);
7287
7288 ice_for_each_txq(vsi, i)
7289 ice_clean_tx_ring(vsi->tx_rings[i]);
7290
7291 if (vsi->xdp_rings)
7292 ice_for_each_xdp_txq(vsi, i)
7293 ice_clean_tx_ring(vsi->xdp_rings[i]);
7294
7295 ice_for_each_rxq(vsi, i)
7296 ice_clean_rx_ring(vsi->rx_rings[i]);
7297
7298 if (tx_err || rx_err || vlan_err) {
7299 netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
7300 vsi->vsi_num, vsi->vsw->sw_id);
7301 return -EIO;
7302 }
7303
7304 return 0;
7305}
7306
7307/**
7308 * ice_down_up - shutdown the VSI connection and bring it up
7309 * @vsi: the VSI to be reconnected
7310 */
7311int ice_down_up(struct ice_vsi *vsi)
7312{
7313 int ret;
7314
7315 /* if DOWN already set, nothing to do */
7316 if (test_and_set_bit(ICE_VSI_DOWN, vsi->state))
7317 return 0;
7318
7319 ret = ice_down(vsi);
7320 if (ret)
7321 return ret;
7322
7323 ret = ice_up(vsi);
7324 if (ret) {
7325 netdev_err(vsi->netdev, "reallocating resources failed during netdev features change, may need to reload driver\n");
7326 return ret;
7327 }
7328
7329 return 0;
7330}
7331
7332/**
7333 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
7334 * @vsi: VSI having resources allocated
7335 *
7336 * Return 0 on success, negative on failure
7337 */
7338int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
7339{
7340 int i, err = 0;
7341
7342 if (!vsi->num_txq) {
7343 dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
7344 vsi->vsi_num);
7345 return -EINVAL;
7346 }
7347
7348 ice_for_each_txq(vsi, i) {
7349 struct ice_tx_ring *ring = vsi->tx_rings[i];
7350
7351 if (!ring)
7352 return -EINVAL;
7353
7354 if (vsi->netdev)
7355 ring->netdev = vsi->netdev;
7356 err = ice_setup_tx_ring(ring);
7357 if (err)
7358 break;
7359 }
7360
7361 return err;
7362}
7363
7364/**
7365 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
7366 * @vsi: VSI having resources allocated
7367 *
7368 * Return 0 on success, negative on failure
7369 */
7370int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
7371{
7372 int i, err = 0;
7373
7374 if (!vsi->num_rxq) {
7375 dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
7376 vsi->vsi_num);
7377 return -EINVAL;
7378 }
7379
7380 ice_for_each_rxq(vsi, i) {
7381 struct ice_rx_ring *ring = vsi->rx_rings[i];
7382
7383 if (!ring)
7384 return -EINVAL;
7385
7386 if (vsi->netdev)
7387 ring->netdev = vsi->netdev;
7388 err = ice_setup_rx_ring(ring);
7389 if (err)
7390 break;
7391 }
7392
7393 return err;
7394}
7395
7396/**
7397 * ice_vsi_open_ctrl - open control VSI for use
7398 * @vsi: the VSI to open
7399 *
7400 * Initialization of the Control VSI
7401 *
7402 * Returns 0 on success, negative value on error
7403 */
7404int ice_vsi_open_ctrl(struct ice_vsi *vsi)
7405{
7406 char int_name[ICE_INT_NAME_STR_LEN];
7407 struct ice_pf *pf = vsi->back;
7408 struct device *dev;
7409 int err;
7410
7411 dev = ice_pf_to_dev(pf);
7412 /* allocate descriptors */
7413 err = ice_vsi_setup_tx_rings(vsi);
7414 if (err)
7415 goto err_setup_tx;
7416
7417 err = ice_vsi_setup_rx_rings(vsi);
7418 if (err)
7419 goto err_setup_rx;
7420
7421 err = ice_vsi_cfg_lan(vsi);
7422 if (err)
7423 goto err_setup_rx;
7424
7425 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
7426 dev_driver_string(dev), dev_name(dev));
7427 err = ice_vsi_req_irq_msix(vsi, int_name);
7428 if (err)
7429 goto err_setup_rx;
7430
7431 ice_vsi_cfg_msix(vsi);
7432
7433 err = ice_vsi_start_all_rx_rings(vsi);
7434 if (err)
7435 goto err_up_complete;
7436
7437 clear_bit(ICE_VSI_DOWN, vsi->state);
7438 ice_vsi_ena_irq(vsi);
7439
7440 return 0;
7441
7442err_up_complete:
7443 ice_down(vsi);
7444err_setup_rx:
7445 ice_vsi_free_rx_rings(vsi);
7446err_setup_tx:
7447 ice_vsi_free_tx_rings(vsi);
7448
7449 return err;
7450}
7451
7452/**
7453 * ice_vsi_open - Called when a network interface is made active
7454 * @vsi: the VSI to open
7455 *
7456 * Initialization of the VSI
7457 *
7458 * Returns 0 on success, negative value on error
7459 */
7460int ice_vsi_open(struct ice_vsi *vsi)
7461{
7462 char int_name[ICE_INT_NAME_STR_LEN];
7463 struct ice_pf *pf = vsi->back;
7464 int err;
7465
7466 /* allocate descriptors */
7467 err = ice_vsi_setup_tx_rings(vsi);
7468 if (err)
7469 goto err_setup_tx;
7470
7471 err = ice_vsi_setup_rx_rings(vsi);
7472 if (err)
7473 goto err_setup_rx;
7474
7475 err = ice_vsi_cfg_lan(vsi);
7476 if (err)
7477 goto err_setup_rx;
7478
7479 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7480 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
7481 err = ice_vsi_req_irq_msix(vsi, int_name);
7482 if (err)
7483 goto err_setup_rx;
7484
7485 ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
7486
7487 if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_SF) {
7488 /* Notify the stack of the actual queue counts. */
7489 err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
7490 if (err)
7491 goto err_set_qs;
7492
7493 err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
7494 if (err)
7495 goto err_set_qs;
7496
7497 ice_vsi_set_napi_queues(vsi);
7498 }
7499
7500 err = ice_up_complete(vsi);
7501 if (err)
7502 goto err_up_complete;
7503
7504 return 0;
7505
7506err_up_complete:
7507 ice_down(vsi);
7508err_set_qs:
7509 ice_vsi_free_irq(vsi);
7510err_setup_rx:
7511 ice_vsi_free_rx_rings(vsi);
7512err_setup_tx:
7513 ice_vsi_free_tx_rings(vsi);
7514
7515 return err;
7516}
7517
7518/**
7519 * ice_vsi_release_all - Delete all VSIs
7520 * @pf: PF from which all VSIs are being removed
7521 */
7522static void ice_vsi_release_all(struct ice_pf *pf)
7523{
7524 int err, i;
7525
7526 if (!pf->vsi)
7527 return;
7528
7529 ice_for_each_vsi(pf, i) {
7530 if (!pf->vsi[i])
7531 continue;
7532
7533 if (pf->vsi[i]->type == ICE_VSI_CHNL)
7534 continue;
7535
7536 err = ice_vsi_release(pf->vsi[i]);
7537 if (err)
7538 dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
7539 i, err, pf->vsi[i]->vsi_num);
7540 }
7541}
7542
7543/**
7544 * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
7545 * @pf: pointer to the PF instance
7546 * @type: VSI type to rebuild
7547 *
7548 * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
7549 */
7550static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
7551{
7552 struct device *dev = ice_pf_to_dev(pf);
7553 int i, err;
7554
7555 ice_for_each_vsi(pf, i) {
7556 struct ice_vsi *vsi = pf->vsi[i];
7557
7558 if (!vsi || vsi->type != type)
7559 continue;
7560
7561 /* rebuild the VSI */
7562 err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
7563 if (err) {
7564 dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
7565 err, vsi->idx, ice_vsi_type_str(type));
7566 return err;
7567 }
7568
7569 /* replay filters for the VSI */
7570 err = ice_replay_vsi(&pf->hw, vsi->idx);
7571 if (err) {
7572 dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
7573 err, vsi->idx, ice_vsi_type_str(type));
7574 return err;
7575 }
7576
7577 /* Re-map HW VSI number, using VSI handle that has been
7578 * previously validated in ice_replay_vsi() call above
7579 */
7580 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
7581
7582 /* enable the VSI */
7583 err = ice_ena_vsi(vsi, false);
7584 if (err) {
7585 dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
7586 err, vsi->idx, ice_vsi_type_str(type));
7587 return err;
7588 }
7589
7590 dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
7591 ice_vsi_type_str(type));
7592 }
7593
7594 return 0;
7595}
7596
7597/**
7598 * ice_update_pf_netdev_link - Update PF netdev link status
7599 * @pf: pointer to the PF instance
7600 */
7601static void ice_update_pf_netdev_link(struct ice_pf *pf)
7602{
7603 bool link_up;
7604 int i;
7605
7606 ice_for_each_vsi(pf, i) {
7607 struct ice_vsi *vsi = pf->vsi[i];
7608
7609 if (!vsi || vsi->type != ICE_VSI_PF)
7610 return;
7611
7612 ice_get_link_status(pf->vsi[i]->port_info, &link_up);
7613 if (link_up) {
7614 netif_carrier_on(pf->vsi[i]->netdev);
7615 netif_tx_wake_all_queues(pf->vsi[i]->netdev);
7616 } else {
7617 netif_carrier_off(pf->vsi[i]->netdev);
7618 netif_tx_stop_all_queues(pf->vsi[i]->netdev);
7619 }
7620 }
7621}
7622
7623/**
7624 * ice_rebuild - rebuild after reset
7625 * @pf: PF to rebuild
7626 * @reset_type: type of reset
7627 *
7628 * Do not rebuild VF VSI in this flow because that is already handled via
7629 * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
7630 * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
7631 * to reset/rebuild all the VF VSI twice.
7632 */
7633static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
7634{
7635 struct ice_vsi *vsi = ice_get_main_vsi(pf);
7636 struct device *dev = ice_pf_to_dev(pf);
7637 struct ice_hw *hw = &pf->hw;
7638 bool dvm;
7639 int err;
7640
7641 if (test_bit(ICE_DOWN, pf->state))
7642 goto clear_recovery;
7643
7644 dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
7645
7646#define ICE_EMP_RESET_SLEEP_MS 5000
7647 if (reset_type == ICE_RESET_EMPR) {
7648 /* If an EMP reset has occurred, any previously pending flash
7649 * update will have completed. We no longer know whether or
7650 * not the NVM update EMP reset is restricted.
7651 */
7652 pf->fw_emp_reset_disabled = false;
7653
7654 msleep(ICE_EMP_RESET_SLEEP_MS);
7655 }
7656
7657 err = ice_init_all_ctrlq(hw);
7658 if (err) {
7659 dev_err(dev, "control queues init failed %d\n", err);
7660 goto err_init_ctrlq;
7661 }
7662
7663 /* if DDP was previously loaded successfully */
7664 if (!ice_is_safe_mode(pf)) {
7665 /* reload the SW DB of filter tables */
7666 if (reset_type == ICE_RESET_PFR)
7667 ice_fill_blk_tbls(hw);
7668 else
7669 /* Reload DDP Package after CORER/GLOBR reset */
7670 ice_load_pkg(NULL, pf);
7671 }
7672
7673 err = ice_clear_pf_cfg(hw);
7674 if (err) {
7675 dev_err(dev, "clear PF configuration failed %d\n", err);
7676 goto err_init_ctrlq;
7677 }
7678
7679 ice_clear_pxe_mode(hw);
7680
7681 err = ice_init_nvm(hw);
7682 if (err) {
7683 dev_err(dev, "ice_init_nvm failed %d\n", err);
7684 goto err_init_ctrlq;
7685 }
7686
7687 err = ice_get_caps(hw);
7688 if (err) {
7689 dev_err(dev, "ice_get_caps failed %d\n", err);
7690 goto err_init_ctrlq;
7691 }
7692
7693 err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
7694 if (err) {
7695 dev_err(dev, "set_mac_cfg failed %d\n", err);
7696 goto err_init_ctrlq;
7697 }
7698
7699 dvm = ice_is_dvm_ena(hw);
7700
7701 err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
7702 if (err)
7703 goto err_init_ctrlq;
7704
7705 err = ice_sched_init_port(hw->port_info);
7706 if (err)
7707 goto err_sched_init_port;
7708
7709 /* start misc vector */
7710 err = ice_req_irq_msix_misc(pf);
7711 if (err) {
7712 dev_err(dev, "misc vector setup failed: %d\n", err);
7713 goto err_sched_init_port;
7714 }
7715
7716 if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7717 wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
7718 if (!rd32(hw, PFQF_FD_SIZE)) {
7719 u16 unused, guar, b_effort;
7720
7721 guar = hw->func_caps.fd_fltr_guar;
7722 b_effort = hw->func_caps.fd_fltr_best_effort;
7723
7724 /* force guaranteed filter pool for PF */
7725 ice_alloc_fd_guar_item(hw, &unused, guar);
7726 /* force shared filter pool for PF */
7727 ice_alloc_fd_shrd_item(hw, &unused, b_effort);
7728 }
7729 }
7730
7731 if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
7732 ice_dcb_rebuild(pf);
7733
7734 /* If the PF previously had enabled PTP, PTP init needs to happen before
7735 * the VSI rebuild. If not, this causes the PTP link status events to
7736 * fail.
7737 */
7738 if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7739 ice_ptp_rebuild(pf, reset_type);
7740
7741 if (ice_is_feature_supported(pf, ICE_F_GNSS))
7742 ice_gnss_init(pf);
7743
7744 /* rebuild PF VSI */
7745 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
7746 if (err) {
7747 dev_err(dev, "PF VSI rebuild failed: %d\n", err);
7748 goto err_vsi_rebuild;
7749 }
7750
7751 if (reset_type == ICE_RESET_PFR) {
7752 err = ice_rebuild_channels(pf);
7753 if (err) {
7754 dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
7755 err);
7756 goto err_vsi_rebuild;
7757 }
7758 }
7759
7760 /* If Flow Director is active */
7761 if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7762 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
7763 if (err) {
7764 dev_err(dev, "control VSI rebuild failed: %d\n", err);
7765 goto err_vsi_rebuild;
7766 }
7767
7768 /* replay HW Flow Director recipes */
7769 if (hw->fdir_prof)
7770 ice_fdir_replay_flows(hw);
7771
7772 /* replay Flow Director filters */
7773 ice_fdir_replay_fltrs(pf);
7774
7775 ice_rebuild_arfs(pf);
7776 }
7777
7778 if (vsi && vsi->netdev)
7779 netif_device_attach(vsi->netdev);
7780
7781 ice_update_pf_netdev_link(pf);
7782
7783 /* tell the firmware we are up */
7784 err = ice_send_version(pf);
7785 if (err) {
7786 dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
7787 err);
7788 goto err_vsi_rebuild;
7789 }
7790
7791 ice_replay_post(hw);
7792
7793 /* if we get here, reset flow is successful */
7794 clear_bit(ICE_RESET_FAILED, pf->state);
7795
7796 ice_plug_aux_dev(pf);
7797 if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
7798 ice_lag_rebuild(pf);
7799
7800 /* Restore timestamp mode settings after VSI rebuild */
7801 ice_ptp_restore_timestamp_mode(pf);
7802 return;
7803
7804err_vsi_rebuild:
7805err_sched_init_port:
7806 ice_sched_cleanup_all(hw);
7807err_init_ctrlq:
7808 ice_shutdown_all_ctrlq(hw, false);
7809 set_bit(ICE_RESET_FAILED, pf->state);
7810clear_recovery:
7811 /* set this bit in PF state to control service task scheduling */
7812 set_bit(ICE_NEEDS_RESTART, pf->state);
7813 dev_err(dev, "Rebuild failed, unload and reload driver\n");
7814}
7815
7816/**
7817 * ice_change_mtu - NDO callback to change the MTU
7818 * @netdev: network interface device structure
7819 * @new_mtu: new value for maximum frame size
7820 *
7821 * Returns 0 on success, negative on failure
7822 */
7823int ice_change_mtu(struct net_device *netdev, int new_mtu)
7824{
7825 struct ice_netdev_priv *np = netdev_priv(netdev);
7826 struct ice_vsi *vsi = np->vsi;
7827 struct ice_pf *pf = vsi->back;
7828 struct bpf_prog *prog;
7829 u8 count = 0;
7830 int err = 0;
7831
7832 if (new_mtu == (int)netdev->mtu) {
7833 netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
7834 return 0;
7835 }
7836
7837 prog = vsi->xdp_prog;
7838 if (prog && !prog->aux->xdp_has_frags) {
7839 int frame_size = ice_max_xdp_frame_size(vsi);
7840
7841 if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
7842 netdev_err(netdev, "max MTU for XDP usage is %d\n",
7843 frame_size - ICE_ETH_PKT_HDR_PAD);
7844 return -EINVAL;
7845 }
7846 } else if (test_bit(ICE_FLAG_LEGACY_RX, pf->flags)) {
7847 if (new_mtu + ICE_ETH_PKT_HDR_PAD > ICE_MAX_FRAME_LEGACY_RX) {
7848 netdev_err(netdev, "Too big MTU for legacy-rx; Max is %d\n",
7849 ICE_MAX_FRAME_LEGACY_RX - ICE_ETH_PKT_HDR_PAD);
7850 return -EINVAL;
7851 }
7852 }
7853
7854 /* if a reset is in progress, wait for some time for it to complete */
7855 do {
7856 if (ice_is_reset_in_progress(pf->state)) {
7857 count++;
7858 usleep_range(1000, 2000);
7859 } else {
7860 break;
7861 }
7862
7863 } while (count < 100);
7864
7865 if (count == 100) {
7866 netdev_err(netdev, "can't change MTU. Device is busy\n");
7867 return -EBUSY;
7868 }
7869
7870 WRITE_ONCE(netdev->mtu, (unsigned int)new_mtu);
7871 err = ice_down_up(vsi);
7872 if (err)
7873 return err;
7874
7875 netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
7876 set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
7877
7878 return err;
7879}
7880
7881/**
7882 * ice_eth_ioctl - Access the hwtstamp interface
7883 * @netdev: network interface device structure
7884 * @ifr: interface request data
7885 * @cmd: ioctl command
7886 */
7887static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
7888{
7889 struct ice_netdev_priv *np = netdev_priv(netdev);
7890 struct ice_pf *pf = np->vsi->back;
7891
7892 switch (cmd) {
7893 case SIOCGHWTSTAMP:
7894 return ice_ptp_get_ts_config(pf, ifr);
7895 case SIOCSHWTSTAMP:
7896 return ice_ptp_set_ts_config(pf, ifr);
7897 default:
7898 return -EOPNOTSUPP;
7899 }
7900}
7901
7902/**
7903 * ice_aq_str - convert AQ err code to a string
7904 * @aq_err: the AQ error code to convert
7905 */
7906const char *ice_aq_str(enum ice_aq_err aq_err)
7907{
7908 switch (aq_err) {
7909 case ICE_AQ_RC_OK:
7910 return "OK";
7911 case ICE_AQ_RC_EPERM:
7912 return "ICE_AQ_RC_EPERM";
7913 case ICE_AQ_RC_ENOENT:
7914 return "ICE_AQ_RC_ENOENT";
7915 case ICE_AQ_RC_ENOMEM:
7916 return "ICE_AQ_RC_ENOMEM";
7917 case ICE_AQ_RC_EBUSY:
7918 return "ICE_AQ_RC_EBUSY";
7919 case ICE_AQ_RC_EEXIST:
7920 return "ICE_AQ_RC_EEXIST";
7921 case ICE_AQ_RC_EINVAL:
7922 return "ICE_AQ_RC_EINVAL";
7923 case ICE_AQ_RC_ENOSPC:
7924 return "ICE_AQ_RC_ENOSPC";
7925 case ICE_AQ_RC_ENOSYS:
7926 return "ICE_AQ_RC_ENOSYS";
7927 case ICE_AQ_RC_EMODE:
7928 return "ICE_AQ_RC_EMODE";
7929 case ICE_AQ_RC_ENOSEC:
7930 return "ICE_AQ_RC_ENOSEC";
7931 case ICE_AQ_RC_EBADSIG:
7932 return "ICE_AQ_RC_EBADSIG";
7933 case ICE_AQ_RC_ESVN:
7934 return "ICE_AQ_RC_ESVN";
7935 case ICE_AQ_RC_EBADMAN:
7936 return "ICE_AQ_RC_EBADMAN";
7937 case ICE_AQ_RC_EBADBUF:
7938 return "ICE_AQ_RC_EBADBUF";
7939 }
7940
7941 return "ICE_AQ_RC_UNKNOWN";
7942}
7943
7944/**
7945 * ice_set_rss_lut - Set RSS LUT
7946 * @vsi: Pointer to VSI structure
7947 * @lut: Lookup table
7948 * @lut_size: Lookup table size
7949 *
7950 * Returns 0 on success, negative on failure
7951 */
7952int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7953{
7954 struct ice_aq_get_set_rss_lut_params params = {};
7955 struct ice_hw *hw = &vsi->back->hw;
7956 int status;
7957
7958 if (!lut)
7959 return -EINVAL;
7960
7961 params.vsi_handle = vsi->idx;
7962 params.lut_size = lut_size;
7963 params.lut_type = vsi->rss_lut_type;
7964 params.lut = lut;
7965
7966 status = ice_aq_set_rss_lut(hw, ¶ms);
7967 if (status)
7968 dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
7969 status, ice_aq_str(hw->adminq.sq_last_status));
7970
7971 return status;
7972}
7973
7974/**
7975 * ice_set_rss_key - Set RSS key
7976 * @vsi: Pointer to the VSI structure
7977 * @seed: RSS hash seed
7978 *
7979 * Returns 0 on success, negative on failure
7980 */
7981int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
7982{
7983 struct ice_hw *hw = &vsi->back->hw;
7984 int status;
7985
7986 if (!seed)
7987 return -EINVAL;
7988
7989 status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7990 if (status)
7991 dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
7992 status, ice_aq_str(hw->adminq.sq_last_status));
7993
7994 return status;
7995}
7996
7997/**
7998 * ice_get_rss_lut - Get RSS LUT
7999 * @vsi: Pointer to VSI structure
8000 * @lut: Buffer to store the lookup table entries
8001 * @lut_size: Size of buffer to store the lookup table entries
8002 *
8003 * Returns 0 on success, negative on failure
8004 */
8005int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
8006{
8007 struct ice_aq_get_set_rss_lut_params params = {};
8008 struct ice_hw *hw = &vsi->back->hw;
8009 int status;
8010
8011 if (!lut)
8012 return -EINVAL;
8013
8014 params.vsi_handle = vsi->idx;
8015 params.lut_size = lut_size;
8016 params.lut_type = vsi->rss_lut_type;
8017 params.lut = lut;
8018
8019 status = ice_aq_get_rss_lut(hw, ¶ms);
8020 if (status)
8021 dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
8022 status, ice_aq_str(hw->adminq.sq_last_status));
8023
8024 return status;
8025}
8026
8027/**
8028 * ice_get_rss_key - Get RSS key
8029 * @vsi: Pointer to VSI structure
8030 * @seed: Buffer to store the key in
8031 *
8032 * Returns 0 on success, negative on failure
8033 */
8034int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
8035{
8036 struct ice_hw *hw = &vsi->back->hw;
8037 int status;
8038
8039 if (!seed)
8040 return -EINVAL;
8041
8042 status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
8043 if (status)
8044 dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
8045 status, ice_aq_str(hw->adminq.sq_last_status));
8046
8047 return status;
8048}
8049
8050/**
8051 * ice_set_rss_hfunc - Set RSS HASH function
8052 * @vsi: Pointer to VSI structure
8053 * @hfunc: hash function (ICE_AQ_VSI_Q_OPT_RSS_*)
8054 *
8055 * Returns 0 on success, negative on failure
8056 */
8057int ice_set_rss_hfunc(struct ice_vsi *vsi, u8 hfunc)
8058{
8059 struct ice_hw *hw = &vsi->back->hw;
8060 struct ice_vsi_ctx *ctx;
8061 bool symm;
8062 int err;
8063
8064 if (hfunc == vsi->rss_hfunc)
8065 return 0;
8066
8067 if (hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ &&
8068 hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ)
8069 return -EOPNOTSUPP;
8070
8071 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8072 if (!ctx)
8073 return -ENOMEM;
8074
8075 ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
8076 ctx->info.q_opt_rss = vsi->info.q_opt_rss;
8077 ctx->info.q_opt_rss &= ~ICE_AQ_VSI_Q_OPT_RSS_HASH_M;
8078 ctx->info.q_opt_rss |=
8079 FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hfunc);
8080 ctx->info.q_opt_tc = vsi->info.q_opt_tc;
8081 ctx->info.q_opt_flags = vsi->info.q_opt_rss;
8082
8083 err = ice_update_vsi(hw, vsi->idx, ctx, NULL);
8084 if (err) {
8085 dev_err(ice_pf_to_dev(vsi->back), "Failed to configure RSS hash for VSI %d, error %d\n",
8086 vsi->vsi_num, err);
8087 } else {
8088 vsi->info.q_opt_rss = ctx->info.q_opt_rss;
8089 vsi->rss_hfunc = hfunc;
8090 netdev_info(vsi->netdev, "Hash function set to: %sToeplitz\n",
8091 hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ ?
8092 "Symmetric " : "");
8093 }
8094 kfree(ctx);
8095 if (err)
8096 return err;
8097
8098 /* Fix the symmetry setting for all existing RSS configurations */
8099 symm = !!(hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ);
8100 return ice_set_rss_cfg_symm(hw, vsi, symm);
8101}
8102
8103/**
8104 * ice_bridge_getlink - Get the hardware bridge mode
8105 * @skb: skb buff
8106 * @pid: process ID
8107 * @seq: RTNL message seq
8108 * @dev: the netdev being configured
8109 * @filter_mask: filter mask passed in
8110 * @nlflags: netlink flags passed in
8111 *
8112 * Return the bridge mode (VEB/VEPA)
8113 */
8114static int
8115ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8116 struct net_device *dev, u32 filter_mask, int nlflags)
8117{
8118 struct ice_netdev_priv *np = netdev_priv(dev);
8119 struct ice_vsi *vsi = np->vsi;
8120 struct ice_pf *pf = vsi->back;
8121 u16 bmode;
8122
8123 bmode = pf->first_sw->bridge_mode;
8124
8125 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
8126 filter_mask, NULL);
8127}
8128
8129/**
8130 * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
8131 * @vsi: Pointer to VSI structure
8132 * @bmode: Hardware bridge mode (VEB/VEPA)
8133 *
8134 * Returns 0 on success, negative on failure
8135 */
8136static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
8137{
8138 struct ice_aqc_vsi_props *vsi_props;
8139 struct ice_hw *hw = &vsi->back->hw;
8140 struct ice_vsi_ctx *ctxt;
8141 int ret;
8142
8143 vsi_props = &vsi->info;
8144
8145 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
8146 if (!ctxt)
8147 return -ENOMEM;
8148
8149 ctxt->info = vsi->info;
8150
8151 if (bmode == BRIDGE_MODE_VEB)
8152 /* change from VEPA to VEB mode */
8153 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
8154 else
8155 /* change from VEB to VEPA mode */
8156 ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
8157 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
8158
8159 ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
8160 if (ret) {
8161 dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
8162 bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
8163 goto out;
8164 }
8165 /* Update sw flags for book keeping */
8166 vsi_props->sw_flags = ctxt->info.sw_flags;
8167
8168out:
8169 kfree(ctxt);
8170 return ret;
8171}
8172
8173/**
8174 * ice_bridge_setlink - Set the hardware bridge mode
8175 * @dev: the netdev being configured
8176 * @nlh: RTNL message
8177 * @flags: bridge setlink flags
8178 * @extack: netlink extended ack
8179 *
8180 * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
8181 * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
8182 * not already set for all VSIs connected to this switch. And also update the
8183 * unicast switch filter rules for the corresponding switch of the netdev.
8184 */
8185static int
8186ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
8187 u16 __always_unused flags,
8188 struct netlink_ext_ack __always_unused *extack)
8189{
8190 struct ice_netdev_priv *np = netdev_priv(dev);
8191 struct ice_pf *pf = np->vsi->back;
8192 struct nlattr *attr, *br_spec;
8193 struct ice_hw *hw = &pf->hw;
8194 struct ice_sw *pf_sw;
8195 int rem, v, err = 0;
8196
8197 pf_sw = pf->first_sw;
8198 /* find the attribute in the netlink message */
8199 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8200 if (!br_spec)
8201 return -EINVAL;
8202
8203 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
8204 __u16 mode = nla_get_u16(attr);
8205
8206 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
8207 return -EINVAL;
8208 /* Continue if bridge mode is not being flipped */
8209 if (mode == pf_sw->bridge_mode)
8210 continue;
8211 /* Iterates through the PF VSI list and update the loopback
8212 * mode of the VSI
8213 */
8214 ice_for_each_vsi(pf, v) {
8215 if (!pf->vsi[v])
8216 continue;
8217 err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
8218 if (err)
8219 return err;
8220 }
8221
8222 hw->evb_veb = (mode == BRIDGE_MODE_VEB);
8223 /* Update the unicast switch filter rules for the corresponding
8224 * switch of the netdev
8225 */
8226 err = ice_update_sw_rule_bridge_mode(hw);
8227 if (err) {
8228 netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
8229 mode, err,
8230 ice_aq_str(hw->adminq.sq_last_status));
8231 /* revert hw->evb_veb */
8232 hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
8233 return err;
8234 }
8235
8236 pf_sw->bridge_mode = mode;
8237 }
8238
8239 return 0;
8240}
8241
8242/**
8243 * ice_tx_timeout - Respond to a Tx Hang
8244 * @netdev: network interface device structure
8245 * @txqueue: Tx queue
8246 */
8247void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
8248{
8249 struct ice_netdev_priv *np = netdev_priv(netdev);
8250 struct ice_tx_ring *tx_ring = NULL;
8251 struct ice_vsi *vsi = np->vsi;
8252 struct ice_pf *pf = vsi->back;
8253 u32 i;
8254
8255 pf->tx_timeout_count++;
8256
8257 /* Check if PFC is enabled for the TC to which the queue belongs
8258 * to. If yes then Tx timeout is not caused by a hung queue, no
8259 * need to reset and rebuild
8260 */
8261 if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
8262 dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
8263 txqueue);
8264 return;
8265 }
8266
8267 /* now that we have an index, find the tx_ring struct */
8268 ice_for_each_txq(vsi, i)
8269 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
8270 if (txqueue == vsi->tx_rings[i]->q_index) {
8271 tx_ring = vsi->tx_rings[i];
8272 break;
8273 }
8274
8275 /* Reset recovery level if enough time has elapsed after last timeout.
8276 * Also ensure no new reset action happens before next timeout period.
8277 */
8278 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
8279 pf->tx_timeout_recovery_level = 1;
8280 else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
8281 netdev->watchdog_timeo)))
8282 return;
8283
8284 if (tx_ring) {
8285 struct ice_hw *hw = &pf->hw;
8286 u32 head, val = 0;
8287
8288 head = FIELD_GET(QTX_COMM_HEAD_HEAD_M,
8289 rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])));
8290 /* Read interrupt register */
8291 val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
8292
8293 netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
8294 vsi->vsi_num, txqueue, tx_ring->next_to_clean,
8295 head, tx_ring->next_to_use, val);
8296 }
8297
8298 pf->tx_timeout_last_recovery = jiffies;
8299 netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
8300 pf->tx_timeout_recovery_level, txqueue);
8301
8302 switch (pf->tx_timeout_recovery_level) {
8303 case 1:
8304 set_bit(ICE_PFR_REQ, pf->state);
8305 break;
8306 case 2:
8307 set_bit(ICE_CORER_REQ, pf->state);
8308 break;
8309 case 3:
8310 set_bit(ICE_GLOBR_REQ, pf->state);
8311 break;
8312 default:
8313 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
8314 set_bit(ICE_DOWN, pf->state);
8315 set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
8316 set_bit(ICE_SERVICE_DIS, pf->state);
8317 break;
8318 }
8319
8320 ice_service_task_schedule(pf);
8321 pf->tx_timeout_recovery_level++;
8322}
8323
8324/**
8325 * ice_setup_tc_cls_flower - flower classifier offloads
8326 * @np: net device to configure
8327 * @filter_dev: device on which filter is added
8328 * @cls_flower: offload data
8329 */
8330static int
8331ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
8332 struct net_device *filter_dev,
8333 struct flow_cls_offload *cls_flower)
8334{
8335 struct ice_vsi *vsi = np->vsi;
8336
8337 if (cls_flower->common.chain_index)
8338 return -EOPNOTSUPP;
8339
8340 switch (cls_flower->command) {
8341 case FLOW_CLS_REPLACE:
8342 return ice_add_cls_flower(filter_dev, vsi, cls_flower);
8343 case FLOW_CLS_DESTROY:
8344 return ice_del_cls_flower(vsi, cls_flower);
8345 default:
8346 return -EINVAL;
8347 }
8348}
8349
8350/**
8351 * ice_setup_tc_block_cb - callback handler registered for TC block
8352 * @type: TC SETUP type
8353 * @type_data: TC flower offload data that contains user input
8354 * @cb_priv: netdev private data
8355 */
8356static int
8357ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
8358{
8359 struct ice_netdev_priv *np = cb_priv;
8360
8361 switch (type) {
8362 case TC_SETUP_CLSFLOWER:
8363 return ice_setup_tc_cls_flower(np, np->vsi->netdev,
8364 type_data);
8365 default:
8366 return -EOPNOTSUPP;
8367 }
8368}
8369
8370/**
8371 * ice_validate_mqprio_qopt - Validate TCF input parameters
8372 * @vsi: Pointer to VSI
8373 * @mqprio_qopt: input parameters for mqprio queue configuration
8374 *
8375 * This function validates MQPRIO params, such as qcount (power of 2 wherever
8376 * needed), and make sure user doesn't specify qcount and BW rate limit
8377 * for TCs, which are more than "num_tc"
8378 */
8379static int
8380ice_validate_mqprio_qopt(struct ice_vsi *vsi,
8381 struct tc_mqprio_qopt_offload *mqprio_qopt)
8382{
8383 int non_power_of_2_qcount = 0;
8384 struct ice_pf *pf = vsi->back;
8385 int max_rss_q_cnt = 0;
8386 u64 sum_min_rate = 0;
8387 struct device *dev;
8388 int i, speed;
8389 u8 num_tc;
8390
8391 if (vsi->type != ICE_VSI_PF)
8392 return -EINVAL;
8393
8394 if (mqprio_qopt->qopt.offset[0] != 0 ||
8395 mqprio_qopt->qopt.num_tc < 1 ||
8396 mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
8397 return -EINVAL;
8398
8399 dev = ice_pf_to_dev(pf);
8400 vsi->ch_rss_size = 0;
8401 num_tc = mqprio_qopt->qopt.num_tc;
8402 speed = ice_get_link_speed_kbps(vsi);
8403
8404 for (i = 0; num_tc; i++) {
8405 int qcount = mqprio_qopt->qopt.count[i];
8406 u64 max_rate, min_rate, rem;
8407
8408 if (!qcount)
8409 return -EINVAL;
8410
8411 if (is_power_of_2(qcount)) {
8412 if (non_power_of_2_qcount &&
8413 qcount > non_power_of_2_qcount) {
8414 dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
8415 qcount, non_power_of_2_qcount);
8416 return -EINVAL;
8417 }
8418 if (qcount > max_rss_q_cnt)
8419 max_rss_q_cnt = qcount;
8420 } else {
8421 if (non_power_of_2_qcount &&
8422 qcount != non_power_of_2_qcount) {
8423 dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
8424 qcount, non_power_of_2_qcount);
8425 return -EINVAL;
8426 }
8427 if (qcount < max_rss_q_cnt) {
8428 dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
8429 qcount, max_rss_q_cnt);
8430 return -EINVAL;
8431 }
8432 max_rss_q_cnt = qcount;
8433 non_power_of_2_qcount = qcount;
8434 }
8435
8436 /* TC command takes input in K/N/Gbps or K/M/Gbit etc but
8437 * converts the bandwidth rate limit into Bytes/s when
8438 * passing it down to the driver. So convert input bandwidth
8439 * from Bytes/s to Kbps
8440 */
8441 max_rate = mqprio_qopt->max_rate[i];
8442 max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
8443
8444 /* min_rate is minimum guaranteed rate and it can't be zero */
8445 min_rate = mqprio_qopt->min_rate[i];
8446 min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
8447 sum_min_rate += min_rate;
8448
8449 if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
8450 dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
8451 min_rate, ICE_MIN_BW_LIMIT);
8452 return -EINVAL;
8453 }
8454
8455 if (max_rate && max_rate > speed) {
8456 dev_err(dev, "TC%d: max_rate(%llu Kbps) > link speed of %u Kbps\n",
8457 i, max_rate, speed);
8458 return -EINVAL;
8459 }
8460
8461 iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
8462 if (rem) {
8463 dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
8464 i, ICE_MIN_BW_LIMIT);
8465 return -EINVAL;
8466 }
8467
8468 iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
8469 if (rem) {
8470 dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
8471 i, ICE_MIN_BW_LIMIT);
8472 return -EINVAL;
8473 }
8474
8475 /* min_rate can't be more than max_rate, except when max_rate
8476 * is zero (implies max_rate sought is max line rate). In such
8477 * a case min_rate can be more than max.
8478 */
8479 if (max_rate && min_rate > max_rate) {
8480 dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
8481 min_rate, max_rate);
8482 return -EINVAL;
8483 }
8484
8485 if (i >= mqprio_qopt->qopt.num_tc - 1)
8486 break;
8487 if (mqprio_qopt->qopt.offset[i + 1] !=
8488 (mqprio_qopt->qopt.offset[i] + qcount))
8489 return -EINVAL;
8490 }
8491 if (vsi->num_rxq <
8492 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8493 return -EINVAL;
8494 if (vsi->num_txq <
8495 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8496 return -EINVAL;
8497
8498 if (sum_min_rate && sum_min_rate > (u64)speed) {
8499 dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
8500 sum_min_rate, speed);
8501 return -EINVAL;
8502 }
8503
8504 /* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
8505 vsi->ch_rss_size = max_rss_q_cnt;
8506
8507 return 0;
8508}
8509
8510/**
8511 * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
8512 * @pf: ptr to PF device
8513 * @vsi: ptr to VSI
8514 */
8515static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
8516{
8517 struct device *dev = ice_pf_to_dev(pf);
8518 bool added = false;
8519 struct ice_hw *hw;
8520 int flow;
8521
8522 if (!(vsi->num_gfltr || vsi->num_bfltr))
8523 return -EINVAL;
8524
8525 hw = &pf->hw;
8526 for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
8527 struct ice_fd_hw_prof *prof;
8528 int tun, status;
8529 u64 entry_h;
8530
8531 if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
8532 hw->fdir_prof[flow]->cnt))
8533 continue;
8534
8535 for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
8536 enum ice_flow_priority prio;
8537
8538 /* add this VSI to FDir profile for this flow */
8539 prio = ICE_FLOW_PRIO_NORMAL;
8540 prof = hw->fdir_prof[flow];
8541 status = ice_flow_add_entry(hw, ICE_BLK_FD,
8542 prof->prof_id[tun],
8543 prof->vsi_h[0], vsi->idx,
8544 prio, prof->fdir_seg[tun],
8545 &entry_h);
8546 if (status) {
8547 dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
8548 vsi->idx, flow);
8549 continue;
8550 }
8551
8552 prof->entry_h[prof->cnt][tun] = entry_h;
8553 }
8554
8555 /* store VSI for filter replay and delete */
8556 prof->vsi_h[prof->cnt] = vsi->idx;
8557 prof->cnt++;
8558
8559 added = true;
8560 dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
8561 flow);
8562 }
8563
8564 if (!added)
8565 dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
8566
8567 return 0;
8568}
8569
8570/**
8571 * ice_add_channel - add a channel by adding VSI
8572 * @pf: ptr to PF device
8573 * @sw_id: underlying HW switching element ID
8574 * @ch: ptr to channel structure
8575 *
8576 * Add a channel (VSI) using add_vsi and queue_map
8577 */
8578static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
8579{
8580 struct device *dev = ice_pf_to_dev(pf);
8581 struct ice_vsi *vsi;
8582
8583 if (ch->type != ICE_VSI_CHNL) {
8584 dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
8585 return -EINVAL;
8586 }
8587
8588 vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
8589 if (!vsi || vsi->type != ICE_VSI_CHNL) {
8590 dev_err(dev, "create chnl VSI failure\n");
8591 return -EINVAL;
8592 }
8593
8594 ice_add_vsi_to_fdir(pf, vsi);
8595
8596 ch->sw_id = sw_id;
8597 ch->vsi_num = vsi->vsi_num;
8598 ch->info.mapping_flags = vsi->info.mapping_flags;
8599 ch->ch_vsi = vsi;
8600 /* set the back pointer of channel for newly created VSI */
8601 vsi->ch = ch;
8602
8603 memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
8604 sizeof(vsi->info.q_mapping));
8605 memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
8606 sizeof(vsi->info.tc_mapping));
8607
8608 return 0;
8609}
8610
8611/**
8612 * ice_chnl_cfg_res
8613 * @vsi: the VSI being setup
8614 * @ch: ptr to channel structure
8615 *
8616 * Configure channel specific resources such as rings, vector.
8617 */
8618static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
8619{
8620 int i;
8621
8622 for (i = 0; i < ch->num_txq; i++) {
8623 struct ice_q_vector *tx_q_vector, *rx_q_vector;
8624 struct ice_ring_container *rc;
8625 struct ice_tx_ring *tx_ring;
8626 struct ice_rx_ring *rx_ring;
8627
8628 tx_ring = vsi->tx_rings[ch->base_q + i];
8629 rx_ring = vsi->rx_rings[ch->base_q + i];
8630 if (!tx_ring || !rx_ring)
8631 continue;
8632
8633 /* setup ring being channel enabled */
8634 tx_ring->ch = ch;
8635 rx_ring->ch = ch;
8636
8637 /* following code block sets up vector specific attributes */
8638 tx_q_vector = tx_ring->q_vector;
8639 rx_q_vector = rx_ring->q_vector;
8640 if (!tx_q_vector && !rx_q_vector)
8641 continue;
8642
8643 if (tx_q_vector) {
8644 tx_q_vector->ch = ch;
8645 /* setup Tx and Rx ITR setting if DIM is off */
8646 rc = &tx_q_vector->tx;
8647 if (!ITR_IS_DYNAMIC(rc))
8648 ice_write_itr(rc, rc->itr_setting);
8649 }
8650 if (rx_q_vector) {
8651 rx_q_vector->ch = ch;
8652 /* setup Tx and Rx ITR setting if DIM is off */
8653 rc = &rx_q_vector->rx;
8654 if (!ITR_IS_DYNAMIC(rc))
8655 ice_write_itr(rc, rc->itr_setting);
8656 }
8657 }
8658
8659 /* it is safe to assume that, if channel has non-zero num_t[r]xq, then
8660 * GLINT_ITR register would have written to perform in-context
8661 * update, hence perform flush
8662 */
8663 if (ch->num_txq || ch->num_rxq)
8664 ice_flush(&vsi->back->hw);
8665}
8666
8667/**
8668 * ice_cfg_chnl_all_res - configure channel resources
8669 * @vsi: pte to main_vsi
8670 * @ch: ptr to channel structure
8671 *
8672 * This function configures channel specific resources such as flow-director
8673 * counter index, and other resources such as queues, vectors, ITR settings
8674 */
8675static void
8676ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
8677{
8678 /* configure channel (aka ADQ) resources such as queues, vectors,
8679 * ITR settings for channel specific vectors and anything else
8680 */
8681 ice_chnl_cfg_res(vsi, ch);
8682}
8683
8684/**
8685 * ice_setup_hw_channel - setup new channel
8686 * @pf: ptr to PF device
8687 * @vsi: the VSI being setup
8688 * @ch: ptr to channel structure
8689 * @sw_id: underlying HW switching element ID
8690 * @type: type of channel to be created (VMDq2/VF)
8691 *
8692 * Setup new channel (VSI) based on specified type (VMDq2/VF)
8693 * and configures Tx rings accordingly
8694 */
8695static int
8696ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8697 struct ice_channel *ch, u16 sw_id, u8 type)
8698{
8699 struct device *dev = ice_pf_to_dev(pf);
8700 int ret;
8701
8702 ch->base_q = vsi->next_base_q;
8703 ch->type = type;
8704
8705 ret = ice_add_channel(pf, sw_id, ch);
8706 if (ret) {
8707 dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
8708 return ret;
8709 }
8710
8711 /* configure/setup ADQ specific resources */
8712 ice_cfg_chnl_all_res(vsi, ch);
8713
8714 /* make sure to update the next_base_q so that subsequent channel's
8715 * (aka ADQ) VSI queue map is correct
8716 */
8717 vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
8718 dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
8719 ch->num_rxq);
8720
8721 return 0;
8722}
8723
8724/**
8725 * ice_setup_channel - setup new channel using uplink element
8726 * @pf: ptr to PF device
8727 * @vsi: the VSI being setup
8728 * @ch: ptr to channel structure
8729 *
8730 * Setup new channel (VSI) based on specified type (VMDq2/VF)
8731 * and uplink switching element
8732 */
8733static bool
8734ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8735 struct ice_channel *ch)
8736{
8737 struct device *dev = ice_pf_to_dev(pf);
8738 u16 sw_id;
8739 int ret;
8740
8741 if (vsi->type != ICE_VSI_PF) {
8742 dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
8743 return false;
8744 }
8745
8746 sw_id = pf->first_sw->sw_id;
8747
8748 /* create channel (VSI) */
8749 ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
8750 if (ret) {
8751 dev_err(dev, "failed to setup hw_channel\n");
8752 return false;
8753 }
8754 dev_dbg(dev, "successfully created channel()\n");
8755
8756 return ch->ch_vsi ? true : false;
8757}
8758
8759/**
8760 * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
8761 * @vsi: VSI to be configured
8762 * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
8763 * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
8764 */
8765static int
8766ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
8767{
8768 int err;
8769
8770 err = ice_set_min_bw_limit(vsi, min_tx_rate);
8771 if (err)
8772 return err;
8773
8774 return ice_set_max_bw_limit(vsi, max_tx_rate);
8775}
8776
8777/**
8778 * ice_create_q_channel - function to create channel
8779 * @vsi: VSI to be configured
8780 * @ch: ptr to channel (it contains channel specific params)
8781 *
8782 * This function creates channel (VSI) using num_queues specified by user,
8783 * reconfigs RSS if needed.
8784 */
8785static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
8786{
8787 struct ice_pf *pf = vsi->back;
8788 struct device *dev;
8789
8790 if (!ch)
8791 return -EINVAL;
8792
8793 dev = ice_pf_to_dev(pf);
8794 if (!ch->num_txq || !ch->num_rxq) {
8795 dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
8796 return -EINVAL;
8797 }
8798
8799 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
8800 dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
8801 vsi->cnt_q_avail, ch->num_txq);
8802 return -EINVAL;
8803 }
8804
8805 if (!ice_setup_channel(pf, vsi, ch)) {
8806 dev_info(dev, "Failed to setup channel\n");
8807 return -EINVAL;
8808 }
8809 /* configure BW rate limit */
8810 if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
8811 int ret;
8812
8813 ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
8814 ch->min_tx_rate);
8815 if (ret)
8816 dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
8817 ch->max_tx_rate, ch->ch_vsi->vsi_num);
8818 else
8819 dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
8820 ch->max_tx_rate, ch->ch_vsi->vsi_num);
8821 }
8822
8823 vsi->cnt_q_avail -= ch->num_txq;
8824
8825 return 0;
8826}
8827
8828/**
8829 * ice_rem_all_chnl_fltrs - removes all channel filters
8830 * @pf: ptr to PF, TC-flower based filter are tracked at PF level
8831 *
8832 * Remove all advanced switch filters only if they are channel specific
8833 * tc-flower based filter
8834 */
8835static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
8836{
8837 struct ice_tc_flower_fltr *fltr;
8838 struct hlist_node *node;
8839
8840 /* to remove all channel filters, iterate an ordered list of filters */
8841 hlist_for_each_entry_safe(fltr, node,
8842 &pf->tc_flower_fltr_list,
8843 tc_flower_node) {
8844 struct ice_rule_query_data rule;
8845 int status;
8846
8847 /* for now process only channel specific filters */
8848 if (!ice_is_chnl_fltr(fltr))
8849 continue;
8850
8851 rule.rid = fltr->rid;
8852 rule.rule_id = fltr->rule_id;
8853 rule.vsi_handle = fltr->dest_vsi_handle;
8854 status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
8855 if (status) {
8856 if (status == -ENOENT)
8857 dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
8858 rule.rule_id);
8859 else
8860 dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
8861 status);
8862 } else if (fltr->dest_vsi) {
8863 /* update advanced switch filter count */
8864 if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
8865 u32 flags = fltr->flags;
8866
8867 fltr->dest_vsi->num_chnl_fltr--;
8868 if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
8869 ICE_TC_FLWR_FIELD_ENC_DST_MAC))
8870 pf->num_dmac_chnl_fltrs--;
8871 }
8872 }
8873
8874 hlist_del(&fltr->tc_flower_node);
8875 kfree(fltr);
8876 }
8877}
8878
8879/**
8880 * ice_remove_q_channels - Remove queue channels for the TCs
8881 * @vsi: VSI to be configured
8882 * @rem_fltr: delete advanced switch filter or not
8883 *
8884 * Remove queue channels for the TCs
8885 */
8886static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
8887{
8888 struct ice_channel *ch, *ch_tmp;
8889 struct ice_pf *pf = vsi->back;
8890 int i;
8891
8892 /* remove all tc-flower based filter if they are channel filters only */
8893 if (rem_fltr)
8894 ice_rem_all_chnl_fltrs(pf);
8895
8896 /* remove ntuple filters since queue configuration is being changed */
8897 if (vsi->netdev->features & NETIF_F_NTUPLE) {
8898 struct ice_hw *hw = &pf->hw;
8899
8900 mutex_lock(&hw->fdir_fltr_lock);
8901 ice_fdir_del_all_fltrs(vsi);
8902 mutex_unlock(&hw->fdir_fltr_lock);
8903 }
8904
8905 /* perform cleanup for channels if they exist */
8906 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8907 struct ice_vsi *ch_vsi;
8908
8909 list_del(&ch->list);
8910 ch_vsi = ch->ch_vsi;
8911 if (!ch_vsi) {
8912 kfree(ch);
8913 continue;
8914 }
8915
8916 /* Reset queue contexts */
8917 for (i = 0; i < ch->num_rxq; i++) {
8918 struct ice_tx_ring *tx_ring;
8919 struct ice_rx_ring *rx_ring;
8920
8921 tx_ring = vsi->tx_rings[ch->base_q + i];
8922 rx_ring = vsi->rx_rings[ch->base_q + i];
8923 if (tx_ring) {
8924 tx_ring->ch = NULL;
8925 if (tx_ring->q_vector)
8926 tx_ring->q_vector->ch = NULL;
8927 }
8928 if (rx_ring) {
8929 rx_ring->ch = NULL;
8930 if (rx_ring->q_vector)
8931 rx_ring->q_vector->ch = NULL;
8932 }
8933 }
8934
8935 /* Release FD resources for the channel VSI */
8936 ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
8937
8938 /* clear the VSI from scheduler tree */
8939 ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
8940
8941 /* Delete VSI from FW, PF and HW VSI arrays */
8942 ice_vsi_delete(ch->ch_vsi);
8943
8944 /* free the channel */
8945 kfree(ch);
8946 }
8947
8948 /* clear the channel VSI map which is stored in main VSI */
8949 ice_for_each_chnl_tc(i)
8950 vsi->tc_map_vsi[i] = NULL;
8951
8952 /* reset main VSI's all TC information */
8953 vsi->all_enatc = 0;
8954 vsi->all_numtc = 0;
8955}
8956
8957/**
8958 * ice_rebuild_channels - rebuild channel
8959 * @pf: ptr to PF
8960 *
8961 * Recreate channel VSIs and replay filters
8962 */
8963static int ice_rebuild_channels(struct ice_pf *pf)
8964{
8965 struct device *dev = ice_pf_to_dev(pf);
8966 struct ice_vsi *main_vsi;
8967 bool rem_adv_fltr = true;
8968 struct ice_channel *ch;
8969 struct ice_vsi *vsi;
8970 int tc_idx = 1;
8971 int i, err;
8972
8973 main_vsi = ice_get_main_vsi(pf);
8974 if (!main_vsi)
8975 return 0;
8976
8977 if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
8978 main_vsi->old_numtc == 1)
8979 return 0; /* nothing to be done */
8980
8981 /* reconfigure main VSI based on old value of TC and cached values
8982 * for MQPRIO opts
8983 */
8984 err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
8985 if (err) {
8986 dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
8987 main_vsi->old_ena_tc, main_vsi->vsi_num);
8988 return err;
8989 }
8990
8991 /* rebuild ADQ VSIs */
8992 ice_for_each_vsi(pf, i) {
8993 enum ice_vsi_type type;
8994
8995 vsi = pf->vsi[i];
8996 if (!vsi || vsi->type != ICE_VSI_CHNL)
8997 continue;
8998
8999 type = vsi->type;
9000
9001 /* rebuild ADQ VSI */
9002 err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
9003 if (err) {
9004 dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
9005 ice_vsi_type_str(type), vsi->idx, err);
9006 goto cleanup;
9007 }
9008
9009 /* Re-map HW VSI number, using VSI handle that has been
9010 * previously validated in ice_replay_vsi() call above
9011 */
9012 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
9013
9014 /* replay filters for the VSI */
9015 err = ice_replay_vsi(&pf->hw, vsi->idx);
9016 if (err) {
9017 dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
9018 ice_vsi_type_str(type), err, vsi->idx);
9019 rem_adv_fltr = false;
9020 goto cleanup;
9021 }
9022 dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
9023 ice_vsi_type_str(type), vsi->idx);
9024
9025 /* store ADQ VSI at correct TC index in main VSI's
9026 * map of TC to VSI
9027 */
9028 main_vsi->tc_map_vsi[tc_idx++] = vsi;
9029 }
9030
9031 /* ADQ VSI(s) has been rebuilt successfully, so setup
9032 * channel for main VSI's Tx and Rx rings
9033 */
9034 list_for_each_entry(ch, &main_vsi->ch_list, list) {
9035 struct ice_vsi *ch_vsi;
9036
9037 ch_vsi = ch->ch_vsi;
9038 if (!ch_vsi)
9039 continue;
9040
9041 /* reconfig channel resources */
9042 ice_cfg_chnl_all_res(main_vsi, ch);
9043
9044 /* replay BW rate limit if it is non-zero */
9045 if (!ch->max_tx_rate && !ch->min_tx_rate)
9046 continue;
9047
9048 err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
9049 ch->min_tx_rate);
9050 if (err)
9051 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",
9052 err, ch->max_tx_rate, ch->min_tx_rate,
9053 ch_vsi->vsi_num);
9054 else
9055 dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
9056 ch->max_tx_rate, ch->min_tx_rate,
9057 ch_vsi->vsi_num);
9058 }
9059
9060 /* reconfig RSS for main VSI */
9061 if (main_vsi->ch_rss_size)
9062 ice_vsi_cfg_rss_lut_key(main_vsi);
9063
9064 return 0;
9065
9066cleanup:
9067 ice_remove_q_channels(main_vsi, rem_adv_fltr);
9068 return err;
9069}
9070
9071/**
9072 * ice_create_q_channels - Add queue channel for the given TCs
9073 * @vsi: VSI to be configured
9074 *
9075 * Configures queue channel mapping to the given TCs
9076 */
9077static int ice_create_q_channels(struct ice_vsi *vsi)
9078{
9079 struct ice_pf *pf = vsi->back;
9080 struct ice_channel *ch;
9081 int ret = 0, i;
9082
9083 ice_for_each_chnl_tc(i) {
9084 if (!(vsi->all_enatc & BIT(i)))
9085 continue;
9086
9087 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
9088 if (!ch) {
9089 ret = -ENOMEM;
9090 goto err_free;
9091 }
9092 INIT_LIST_HEAD(&ch->list);
9093 ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
9094 ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
9095 ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
9096 ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
9097 ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
9098
9099 /* convert to Kbits/s */
9100 if (ch->max_tx_rate)
9101 ch->max_tx_rate = div_u64(ch->max_tx_rate,
9102 ICE_BW_KBPS_DIVISOR);
9103 if (ch->min_tx_rate)
9104 ch->min_tx_rate = div_u64(ch->min_tx_rate,
9105 ICE_BW_KBPS_DIVISOR);
9106
9107 ret = ice_create_q_channel(vsi, ch);
9108 if (ret) {
9109 dev_err(ice_pf_to_dev(pf),
9110 "failed creating channel TC:%d\n", i);
9111 kfree(ch);
9112 goto err_free;
9113 }
9114 list_add_tail(&ch->list, &vsi->ch_list);
9115 vsi->tc_map_vsi[i] = ch->ch_vsi;
9116 dev_dbg(ice_pf_to_dev(pf),
9117 "successfully created channel: VSI %pK\n", ch->ch_vsi);
9118 }
9119 return 0;
9120
9121err_free:
9122 ice_remove_q_channels(vsi, false);
9123
9124 return ret;
9125}
9126
9127/**
9128 * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
9129 * @netdev: net device to configure
9130 * @type_data: TC offload data
9131 */
9132static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
9133{
9134 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
9135 struct ice_netdev_priv *np = netdev_priv(netdev);
9136 struct ice_vsi *vsi = np->vsi;
9137 struct ice_pf *pf = vsi->back;
9138 u16 mode, ena_tc_qdisc = 0;
9139 int cur_txq, cur_rxq;
9140 u8 hw = 0, num_tcf;
9141 struct device *dev;
9142 int ret, i;
9143
9144 dev = ice_pf_to_dev(pf);
9145 num_tcf = mqprio_qopt->qopt.num_tc;
9146 hw = mqprio_qopt->qopt.hw;
9147 mode = mqprio_qopt->mode;
9148 if (!hw) {
9149 clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
9150 vsi->ch_rss_size = 0;
9151 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
9152 goto config_tcf;
9153 }
9154
9155 /* Generate queue region map for number of TCF requested */
9156 for (i = 0; i < num_tcf; i++)
9157 ena_tc_qdisc |= BIT(i);
9158
9159 switch (mode) {
9160 case TC_MQPRIO_MODE_CHANNEL:
9161
9162 if (pf->hw.port_info->is_custom_tx_enabled) {
9163 dev_err(dev, "Custom Tx scheduler feature enabled, can't configure ADQ\n");
9164 return -EBUSY;
9165 }
9166 ice_tear_down_devlink_rate_tree(pf);
9167
9168 ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
9169 if (ret) {
9170 netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
9171 ret);
9172 return ret;
9173 }
9174 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
9175 set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
9176 /* don't assume state of hw_tc_offload during driver load
9177 * and set the flag for TC flower filter if hw_tc_offload
9178 * already ON
9179 */
9180 if (vsi->netdev->features & NETIF_F_HW_TC)
9181 set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
9182 break;
9183 default:
9184 return -EINVAL;
9185 }
9186
9187config_tcf:
9188
9189 /* Requesting same TCF configuration as already enabled */
9190 if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
9191 mode != TC_MQPRIO_MODE_CHANNEL)
9192 return 0;
9193
9194 /* Pause VSI queues */
9195 ice_dis_vsi(vsi, true);
9196
9197 if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
9198 ice_remove_q_channels(vsi, true);
9199
9200 if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
9201 vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
9202 num_online_cpus());
9203 vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
9204 num_online_cpus());
9205 } else {
9206 /* logic to rebuild VSI, same like ethtool -L */
9207 u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
9208
9209 for (i = 0; i < num_tcf; i++) {
9210 if (!(ena_tc_qdisc & BIT(i)))
9211 continue;
9212
9213 offset = vsi->mqprio_qopt.qopt.offset[i];
9214 qcount_rx = vsi->mqprio_qopt.qopt.count[i];
9215 qcount_tx = vsi->mqprio_qopt.qopt.count[i];
9216 }
9217 vsi->req_txq = offset + qcount_tx;
9218 vsi->req_rxq = offset + qcount_rx;
9219
9220 /* store away original rss_size info, so that it gets reused
9221 * form ice_vsi_rebuild during tc-qdisc delete stage - to
9222 * determine, what should be the rss_sizefor main VSI
9223 */
9224 vsi->orig_rss_size = vsi->rss_size;
9225 }
9226
9227 /* save current values of Tx and Rx queues before calling VSI rebuild
9228 * for fallback option
9229 */
9230 cur_txq = vsi->num_txq;
9231 cur_rxq = vsi->num_rxq;
9232
9233 /* proceed with rebuild main VSI using correct number of queues */
9234 ret = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
9235 if (ret) {
9236 /* fallback to current number of queues */
9237 dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
9238 vsi->req_txq = cur_txq;
9239 vsi->req_rxq = cur_rxq;
9240 clear_bit(ICE_RESET_FAILED, pf->state);
9241 if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT)) {
9242 dev_err(dev, "Rebuild of main VSI failed again\n");
9243 return ret;
9244 }
9245 }
9246
9247 vsi->all_numtc = num_tcf;
9248 vsi->all_enatc = ena_tc_qdisc;
9249 ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
9250 if (ret) {
9251 netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
9252 vsi->vsi_num);
9253 goto exit;
9254 }
9255
9256 if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
9257 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9258 u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
9259
9260 /* set TC0 rate limit if specified */
9261 if (max_tx_rate || min_tx_rate) {
9262 /* convert to Kbits/s */
9263 if (max_tx_rate)
9264 max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
9265 if (min_tx_rate)
9266 min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
9267
9268 ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
9269 if (!ret) {
9270 dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
9271 max_tx_rate, min_tx_rate, vsi->vsi_num);
9272 } else {
9273 dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
9274 max_tx_rate, min_tx_rate, vsi->vsi_num);
9275 goto exit;
9276 }
9277 }
9278 ret = ice_create_q_channels(vsi);
9279 if (ret) {
9280 netdev_err(netdev, "failed configuring queue channels\n");
9281 goto exit;
9282 } else {
9283 netdev_dbg(netdev, "successfully configured channels\n");
9284 }
9285 }
9286
9287 if (vsi->ch_rss_size)
9288 ice_vsi_cfg_rss_lut_key(vsi);
9289
9290exit:
9291 /* if error, reset the all_numtc and all_enatc */
9292 if (ret) {
9293 vsi->all_numtc = 0;
9294 vsi->all_enatc = 0;
9295 }
9296 /* resume VSI */
9297 ice_ena_vsi(vsi, true);
9298
9299 return ret;
9300}
9301
9302static LIST_HEAD(ice_block_cb_list);
9303
9304static int
9305ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
9306 void *type_data)
9307{
9308 struct ice_netdev_priv *np = netdev_priv(netdev);
9309 struct ice_pf *pf = np->vsi->back;
9310 bool locked = false;
9311 int err;
9312
9313 switch (type) {
9314 case TC_SETUP_BLOCK:
9315 return flow_block_cb_setup_simple(type_data,
9316 &ice_block_cb_list,
9317 ice_setup_tc_block_cb,
9318 np, np, true);
9319 case TC_SETUP_QDISC_MQPRIO:
9320 if (ice_is_eswitch_mode_switchdev(pf)) {
9321 netdev_err(netdev, "TC MQPRIO offload not supported, switchdev is enabled\n");
9322 return -EOPNOTSUPP;
9323 }
9324
9325 if (pf->adev) {
9326 mutex_lock(&pf->adev_mutex);
9327 device_lock(&pf->adev->dev);
9328 locked = true;
9329 if (pf->adev->dev.driver) {
9330 netdev_err(netdev, "Cannot change qdisc when RDMA is active\n");
9331 err = -EBUSY;
9332 goto adev_unlock;
9333 }
9334 }
9335
9336 /* setup traffic classifier for receive side */
9337 mutex_lock(&pf->tc_mutex);
9338 err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
9339 mutex_unlock(&pf->tc_mutex);
9340
9341adev_unlock:
9342 if (locked) {
9343 device_unlock(&pf->adev->dev);
9344 mutex_unlock(&pf->adev_mutex);
9345 }
9346 return err;
9347 default:
9348 return -EOPNOTSUPP;
9349 }
9350 return -EOPNOTSUPP;
9351}
9352
9353static struct ice_indr_block_priv *
9354ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
9355 struct net_device *netdev)
9356{
9357 struct ice_indr_block_priv *cb_priv;
9358
9359 list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
9360 if (!cb_priv->netdev)
9361 return NULL;
9362 if (cb_priv->netdev == netdev)
9363 return cb_priv;
9364 }
9365 return NULL;
9366}
9367
9368static int
9369ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
9370 void *indr_priv)
9371{
9372 struct ice_indr_block_priv *priv = indr_priv;
9373 struct ice_netdev_priv *np = priv->np;
9374
9375 switch (type) {
9376 case TC_SETUP_CLSFLOWER:
9377 return ice_setup_tc_cls_flower(np, priv->netdev,
9378 (struct flow_cls_offload *)
9379 type_data);
9380 default:
9381 return -EOPNOTSUPP;
9382 }
9383}
9384
9385static int
9386ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
9387 struct ice_netdev_priv *np,
9388 struct flow_block_offload *f, void *data,
9389 void (*cleanup)(struct flow_block_cb *block_cb))
9390{
9391 struct ice_indr_block_priv *indr_priv;
9392 struct flow_block_cb *block_cb;
9393
9394 if (!ice_is_tunnel_supported(netdev) &&
9395 !(is_vlan_dev(netdev) &&
9396 vlan_dev_real_dev(netdev) == np->vsi->netdev))
9397 return -EOPNOTSUPP;
9398
9399 if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
9400 return -EOPNOTSUPP;
9401
9402 switch (f->command) {
9403 case FLOW_BLOCK_BIND:
9404 indr_priv = ice_indr_block_priv_lookup(np, netdev);
9405 if (indr_priv)
9406 return -EEXIST;
9407
9408 indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
9409 if (!indr_priv)
9410 return -ENOMEM;
9411
9412 indr_priv->netdev = netdev;
9413 indr_priv->np = np;
9414 list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
9415
9416 block_cb =
9417 flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
9418 indr_priv, indr_priv,
9419 ice_rep_indr_tc_block_unbind,
9420 f, netdev, sch, data, np,
9421 cleanup);
9422
9423 if (IS_ERR(block_cb)) {
9424 list_del(&indr_priv->list);
9425 kfree(indr_priv);
9426 return PTR_ERR(block_cb);
9427 }
9428 flow_block_cb_add(block_cb, f);
9429 list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
9430 break;
9431 case FLOW_BLOCK_UNBIND:
9432 indr_priv = ice_indr_block_priv_lookup(np, netdev);
9433 if (!indr_priv)
9434 return -ENOENT;
9435
9436 block_cb = flow_block_cb_lookup(f->block,
9437 ice_indr_setup_block_cb,
9438 indr_priv);
9439 if (!block_cb)
9440 return -ENOENT;
9441
9442 flow_indr_block_cb_remove(block_cb, f);
9443
9444 list_del(&block_cb->driver_list);
9445 break;
9446 default:
9447 return -EOPNOTSUPP;
9448 }
9449 return 0;
9450}
9451
9452static int
9453ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
9454 void *cb_priv, enum tc_setup_type type, void *type_data,
9455 void *data,
9456 void (*cleanup)(struct flow_block_cb *block_cb))
9457{
9458 switch (type) {
9459 case TC_SETUP_BLOCK:
9460 return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
9461 data, cleanup);
9462
9463 default:
9464 return -EOPNOTSUPP;
9465 }
9466}
9467
9468/**
9469 * ice_open - Called when a network interface becomes active
9470 * @netdev: network interface device structure
9471 *
9472 * The open entry point is called when a network interface is made
9473 * active by the system (IFF_UP). At this point all resources needed
9474 * for transmit and receive operations are allocated, the interrupt
9475 * handler is registered with the OS, the netdev watchdog is enabled,
9476 * and the stack is notified that the interface is ready.
9477 *
9478 * Returns 0 on success, negative value on failure
9479 */
9480int ice_open(struct net_device *netdev)
9481{
9482 struct ice_netdev_priv *np = netdev_priv(netdev);
9483 struct ice_pf *pf = np->vsi->back;
9484
9485 if (ice_is_reset_in_progress(pf->state)) {
9486 netdev_err(netdev, "can't open net device while reset is in progress");
9487 return -EBUSY;
9488 }
9489
9490 return ice_open_internal(netdev);
9491}
9492
9493/**
9494 * ice_open_internal - Called when a network interface becomes active
9495 * @netdev: network interface device structure
9496 *
9497 * Internal ice_open implementation. Should not be used directly except for ice_open and reset
9498 * handling routine
9499 *
9500 * Returns 0 on success, negative value on failure
9501 */
9502int ice_open_internal(struct net_device *netdev)
9503{
9504 struct ice_netdev_priv *np = netdev_priv(netdev);
9505 struct ice_vsi *vsi = np->vsi;
9506 struct ice_pf *pf = vsi->back;
9507 struct ice_port_info *pi;
9508 int err;
9509
9510 if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
9511 netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
9512 return -EIO;
9513 }
9514
9515 netif_carrier_off(netdev);
9516
9517 pi = vsi->port_info;
9518 err = ice_update_link_info(pi);
9519 if (err) {
9520 netdev_err(netdev, "Failed to get link info, error %d\n", err);
9521 return err;
9522 }
9523
9524 ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
9525
9526 /* Set PHY if there is media, otherwise, turn off PHY */
9527 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
9528 clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9529 if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
9530 err = ice_init_phy_user_cfg(pi);
9531 if (err) {
9532 netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
9533 err);
9534 return err;
9535 }
9536 }
9537
9538 err = ice_configure_phy(vsi);
9539 if (err) {
9540 netdev_err(netdev, "Failed to set physical link up, error %d\n",
9541 err);
9542 return err;
9543 }
9544 } else {
9545 set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9546 ice_set_link(vsi, false);
9547 }
9548
9549 err = ice_vsi_open(vsi);
9550 if (err)
9551 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
9552 vsi->vsi_num, vsi->vsw->sw_id);
9553
9554 /* Update existing tunnels information */
9555 udp_tunnel_get_rx_info(netdev);
9556
9557 return err;
9558}
9559
9560/**
9561 * ice_stop - Disables a network interface
9562 * @netdev: network interface device structure
9563 *
9564 * The stop entry point is called when an interface is de-activated by the OS,
9565 * and the netdevice enters the DOWN state. The hardware is still under the
9566 * driver's control, but the netdev interface is disabled.
9567 *
9568 * Returns success only - not allowed to fail
9569 */
9570int ice_stop(struct net_device *netdev)
9571{
9572 struct ice_netdev_priv *np = netdev_priv(netdev);
9573 struct ice_vsi *vsi = np->vsi;
9574 struct ice_pf *pf = vsi->back;
9575
9576 if (ice_is_reset_in_progress(pf->state)) {
9577 netdev_err(netdev, "can't stop net device while reset is in progress");
9578 return -EBUSY;
9579 }
9580
9581 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
9582 int link_err = ice_force_phys_link_state(vsi, false);
9583
9584 if (link_err) {
9585 if (link_err == -ENOMEDIUM)
9586 netdev_info(vsi->netdev, "Skipping link reconfig - no media attached, VSI %d\n",
9587 vsi->vsi_num);
9588 else
9589 netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
9590 vsi->vsi_num, link_err);
9591
9592 ice_vsi_close(vsi);
9593 return -EIO;
9594 }
9595 }
9596
9597 ice_vsi_close(vsi);
9598
9599 return 0;
9600}
9601
9602/**
9603 * ice_features_check - Validate encapsulated packet conforms to limits
9604 * @skb: skb buffer
9605 * @netdev: This port's netdev
9606 * @features: Offload features that the stack believes apply
9607 */
9608static netdev_features_t
9609ice_features_check(struct sk_buff *skb,
9610 struct net_device __always_unused *netdev,
9611 netdev_features_t features)
9612{
9613 bool gso = skb_is_gso(skb);
9614 size_t len;
9615
9616 /* No point in doing any of this if neither checksum nor GSO are
9617 * being requested for this frame. We can rule out both by just
9618 * checking for CHECKSUM_PARTIAL
9619 */
9620 if (skb->ip_summed != CHECKSUM_PARTIAL)
9621 return features;
9622
9623 /* We cannot support GSO if the MSS is going to be less than
9624 * 64 bytes. If it is then we need to drop support for GSO.
9625 */
9626 if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
9627 features &= ~NETIF_F_GSO_MASK;
9628
9629 len = skb_network_offset(skb);
9630 if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
9631 goto out_rm_features;
9632
9633 len = skb_network_header_len(skb);
9634 if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9635 goto out_rm_features;
9636
9637 if (skb->encapsulation) {
9638 /* this must work for VXLAN frames AND IPIP/SIT frames, and in
9639 * the case of IPIP frames, the transport header pointer is
9640 * after the inner header! So check to make sure that this
9641 * is a GRE or UDP_TUNNEL frame before doing that math.
9642 */
9643 if (gso && (skb_shinfo(skb)->gso_type &
9644 (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
9645 len = skb_inner_network_header(skb) -
9646 skb_transport_header(skb);
9647 if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
9648 goto out_rm_features;
9649 }
9650
9651 len = skb_inner_network_header_len(skb);
9652 if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9653 goto out_rm_features;
9654 }
9655
9656 return features;
9657out_rm_features:
9658 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
9659}
9660
9661static const struct net_device_ops ice_netdev_safe_mode_ops = {
9662 .ndo_open = ice_open,
9663 .ndo_stop = ice_stop,
9664 .ndo_start_xmit = ice_start_xmit,
9665 .ndo_set_mac_address = ice_set_mac_address,
9666 .ndo_validate_addr = eth_validate_addr,
9667 .ndo_change_mtu = ice_change_mtu,
9668 .ndo_get_stats64 = ice_get_stats64,
9669 .ndo_tx_timeout = ice_tx_timeout,
9670 .ndo_bpf = ice_xdp_safe_mode,
9671};
9672
9673static const struct net_device_ops ice_netdev_ops = {
9674 .ndo_open = ice_open,
9675 .ndo_stop = ice_stop,
9676 .ndo_start_xmit = ice_start_xmit,
9677 .ndo_select_queue = ice_select_queue,
9678 .ndo_features_check = ice_features_check,
9679 .ndo_fix_features = ice_fix_features,
9680 .ndo_set_rx_mode = ice_set_rx_mode,
9681 .ndo_set_mac_address = ice_set_mac_address,
9682 .ndo_validate_addr = eth_validate_addr,
9683 .ndo_change_mtu = ice_change_mtu,
9684 .ndo_get_stats64 = ice_get_stats64,
9685 .ndo_set_tx_maxrate = ice_set_tx_maxrate,
9686 .ndo_eth_ioctl = ice_eth_ioctl,
9687 .ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
9688 .ndo_set_vf_mac = ice_set_vf_mac,
9689 .ndo_get_vf_config = ice_get_vf_cfg,
9690 .ndo_set_vf_trust = ice_set_vf_trust,
9691 .ndo_set_vf_vlan = ice_set_vf_port_vlan,
9692 .ndo_set_vf_link_state = ice_set_vf_link_state,
9693 .ndo_get_vf_stats = ice_get_vf_stats,
9694 .ndo_set_vf_rate = ice_set_vf_bw,
9695 .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
9696 .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
9697 .ndo_setup_tc = ice_setup_tc,
9698 .ndo_set_features = ice_set_features,
9699 .ndo_bridge_getlink = ice_bridge_getlink,
9700 .ndo_bridge_setlink = ice_bridge_setlink,
9701 .ndo_fdb_add = ice_fdb_add,
9702 .ndo_fdb_del = ice_fdb_del,
9703#ifdef CONFIG_RFS_ACCEL
9704 .ndo_rx_flow_steer = ice_rx_flow_steer,
9705#endif
9706 .ndo_tx_timeout = ice_tx_timeout,
9707 .ndo_bpf = ice_xdp,
9708 .ndo_xdp_xmit = ice_xdp_xmit,
9709 .ndo_xsk_wakeup = ice_xsk_wakeup,
9710};
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2018, Intel Corporation. */
3
4/* Intel(R) Ethernet Connection E800 Series Linux Driver */
5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8#include "ice.h"
9
10#define DRV_VERSION "ice-0.7.0-k"
11#define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
12const char ice_drv_ver[] = DRV_VERSION;
13static const char ice_driver_string[] = DRV_SUMMARY;
14static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
15
16MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
17MODULE_DESCRIPTION(DRV_SUMMARY);
18MODULE_LICENSE("GPL");
19MODULE_VERSION(DRV_VERSION);
20
21static int debug = -1;
22module_param(debug, int, 0644);
23#ifndef CONFIG_DYNAMIC_DEBUG
24MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
25#else
26MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
27#endif /* !CONFIG_DYNAMIC_DEBUG */
28
29static struct workqueue_struct *ice_wq;
30static const struct net_device_ops ice_netdev_ops;
31
32static void ice_pf_dis_all_vsi(struct ice_pf *pf);
33static void ice_rebuild(struct ice_pf *pf);
34static int ice_vsi_release(struct ice_vsi *vsi);
35static void ice_update_vsi_stats(struct ice_vsi *vsi);
36static void ice_update_pf_stats(struct ice_pf *pf);
37
38/**
39 * ice_get_free_slot - get the next non-NULL location index in array
40 * @array: array to search
41 * @size: size of the array
42 * @curr: last known occupied index to be used as a search hint
43 *
44 * void * is being used to keep the functionality generic. This lets us use this
45 * function on any array of pointers.
46 */
47static int ice_get_free_slot(void *array, int size, int curr)
48{
49 int **tmp_array = (int **)array;
50 int next;
51
52 if (curr < (size - 1) && !tmp_array[curr + 1]) {
53 next = curr + 1;
54 } else {
55 int i = 0;
56
57 while ((i < size) && (tmp_array[i]))
58 i++;
59 if (i == size)
60 next = ICE_NO_VSI;
61 else
62 next = i;
63 }
64 return next;
65}
66
67/**
68 * ice_search_res - Search the tracker for a block of resources
69 * @res: pointer to the resource
70 * @needed: size of the block needed
71 * @id: identifier to track owner
72 * Returns the base item index of the block, or -ENOMEM for error
73 */
74static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
75{
76 int start = res->search_hint;
77 int end = start;
78
79 id |= ICE_RES_VALID_BIT;
80
81 do {
82 /* skip already allocated entries */
83 if (res->list[end++] & ICE_RES_VALID_BIT) {
84 start = end;
85 if ((start + needed) > res->num_entries)
86 break;
87 }
88
89 if (end == (start + needed)) {
90 int i = start;
91
92 /* there was enough, so assign it to the requestor */
93 while (i != end)
94 res->list[i++] = id;
95
96 if (end == res->num_entries)
97 end = 0;
98
99 res->search_hint = end;
100 return start;
101 }
102 } while (1);
103
104 return -ENOMEM;
105}
106
107/**
108 * ice_get_res - get a block of resources
109 * @pf: board private structure
110 * @res: pointer to the resource
111 * @needed: size of the block needed
112 * @id: identifier to track owner
113 *
114 * Returns the base item index of the block, or -ENOMEM for error
115 * The search_hint trick and lack of advanced fit-finding only works
116 * because we're highly likely to have all the same sized requests.
117 * Linear search time and any fragmentation should be minimal.
118 */
119static int
120ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
121{
122 int ret;
123
124 if (!res || !pf)
125 return -EINVAL;
126
127 if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
128 dev_err(&pf->pdev->dev,
129 "param err: needed=%d, num_entries = %d id=0x%04x\n",
130 needed, res->num_entries, id);
131 return -EINVAL;
132 }
133
134 /* search based on search_hint */
135 ret = ice_search_res(res, needed, id);
136
137 if (ret < 0) {
138 /* previous search failed. Reset search hint and try again */
139 res->search_hint = 0;
140 ret = ice_search_res(res, needed, id);
141 }
142
143 return ret;
144}
145
146/**
147 * ice_free_res - free a block of resources
148 * @res: pointer to the resource
149 * @index: starting index previously returned by ice_get_res
150 * @id: identifier to track owner
151 * Returns number of resources freed
152 */
153static int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
154{
155 int count = 0;
156 int i;
157
158 if (!res || index >= res->num_entries)
159 return -EINVAL;
160
161 id |= ICE_RES_VALID_BIT;
162 for (i = index; i < res->num_entries && res->list[i] == id; i++) {
163 res->list[i] = 0;
164 count++;
165 }
166
167 return count;
168}
169
170/**
171 * ice_add_mac_to_list - Add a mac address filter entry to the list
172 * @vsi: the VSI to be forwarded to
173 * @add_list: pointer to the list which contains MAC filter entries
174 * @macaddr: the MAC address to be added.
175 *
176 * Adds mac address filter entry to the temp list
177 *
178 * Returns 0 on success or ENOMEM on failure.
179 */
180static int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
181 const u8 *macaddr)
182{
183 struct ice_fltr_list_entry *tmp;
184 struct ice_pf *pf = vsi->back;
185
186 tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
187 if (!tmp)
188 return -ENOMEM;
189
190 tmp->fltr_info.flag = ICE_FLTR_TX;
191 tmp->fltr_info.src = vsi->vsi_num;
192 tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
193 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
194 tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
195 ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
196
197 INIT_LIST_HEAD(&tmp->list_entry);
198 list_add(&tmp->list_entry, add_list);
199
200 return 0;
201}
202
203/**
204 * ice_add_mac_to_sync_list - creates list of mac addresses to be synced
205 * @netdev: the net device on which the sync is happening
206 * @addr: mac address to sync
207 *
208 * This is a callback function which is called by the in kernel device sync
209 * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
210 * populates the tmp_sync_list, which is later used by ice_add_mac to add the
211 * mac filters from the hardware.
212 */
213static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
214{
215 struct ice_netdev_priv *np = netdev_priv(netdev);
216 struct ice_vsi *vsi = np->vsi;
217
218 if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
219 return -EINVAL;
220
221 return 0;
222}
223
224/**
225 * ice_add_mac_to_unsync_list - creates list of mac addresses to be unsynced
226 * @netdev: the net device on which the unsync is happening
227 * @addr: mac address to unsync
228 *
229 * This is a callback function which is called by the in kernel device unsync
230 * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
231 * populates the tmp_unsync_list, which is later used by ice_remove_mac to
232 * delete the mac filters from the hardware.
233 */
234static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
235{
236 struct ice_netdev_priv *np = netdev_priv(netdev);
237 struct ice_vsi *vsi = np->vsi;
238
239 if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
240 return -EINVAL;
241
242 return 0;
243}
244
245/**
246 * ice_free_fltr_list - free filter lists helper
247 * @dev: pointer to the device struct
248 * @h: pointer to the list head to be freed
249 *
250 * Helper function to free filter lists previously created using
251 * ice_add_mac_to_list
252 */
253static void ice_free_fltr_list(struct device *dev, struct list_head *h)
254{
255 struct ice_fltr_list_entry *e, *tmp;
256
257 list_for_each_entry_safe(e, tmp, h, list_entry) {
258 list_del(&e->list_entry);
259 devm_kfree(dev, e);
260 }
261}
262
263/**
264 * ice_vsi_fltr_changed - check if filter state changed
265 * @vsi: VSI to be checked
266 *
267 * returns true if filter state has changed, false otherwise.
268 */
269static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
270{
271 return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
272 test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
273 test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
274}
275
276/**
277 * ice_vsi_sync_fltr - Update the VSI filter list to the HW
278 * @vsi: ptr to the VSI
279 *
280 * Push any outstanding VSI filter changes through the AdminQ.
281 */
282static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
283{
284 struct device *dev = &vsi->back->pdev->dev;
285 struct net_device *netdev = vsi->netdev;
286 bool promisc_forced_on = false;
287 struct ice_pf *pf = vsi->back;
288 struct ice_hw *hw = &pf->hw;
289 enum ice_status status = 0;
290 u32 changed_flags = 0;
291 int err = 0;
292
293 if (!vsi->netdev)
294 return -EINVAL;
295
296 while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
297 usleep_range(1000, 2000);
298
299 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
300 vsi->current_netdev_flags = vsi->netdev->flags;
301
302 INIT_LIST_HEAD(&vsi->tmp_sync_list);
303 INIT_LIST_HEAD(&vsi->tmp_unsync_list);
304
305 if (ice_vsi_fltr_changed(vsi)) {
306 clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
307 clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
308 clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
309
310 /* grab the netdev's addr_list_lock */
311 netif_addr_lock_bh(netdev);
312 __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
313 ice_add_mac_to_unsync_list);
314 __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
315 ice_add_mac_to_unsync_list);
316 /* our temp lists are populated. release lock */
317 netif_addr_unlock_bh(netdev);
318 }
319
320 /* Remove mac addresses in the unsync list */
321 status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
322 ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
323 if (status) {
324 netdev_err(netdev, "Failed to delete MAC filters\n");
325 /* if we failed because of alloc failures, just bail */
326 if (status == ICE_ERR_NO_MEMORY) {
327 err = -ENOMEM;
328 goto out;
329 }
330 }
331
332 /* Add mac addresses in the sync list */
333 status = ice_add_mac(hw, &vsi->tmp_sync_list);
334 ice_free_fltr_list(dev, &vsi->tmp_sync_list);
335 if (status) {
336 netdev_err(netdev, "Failed to add MAC filters\n");
337 /* If there is no more space for new umac filters, vsi
338 * should go into promiscuous mode. There should be some
339 * space reserved for promiscuous filters.
340 */
341 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
342 !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
343 vsi->state)) {
344 promisc_forced_on = true;
345 netdev_warn(netdev,
346 "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
347 vsi->vsi_num);
348 } else {
349 err = -EIO;
350 goto out;
351 }
352 }
353 /* check for changes in promiscuous modes */
354 if (changed_flags & IFF_ALLMULTI)
355 netdev_warn(netdev, "Unsupported configuration\n");
356
357 if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
358 test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
359 clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
360 if (vsi->current_netdev_flags & IFF_PROMISC) {
361 /* Apply TX filter rule to get traffic from VMs */
362 status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
363 ICE_FLTR_TX);
364 if (status) {
365 netdev_err(netdev, "Error setting default VSI %i tx rule\n",
366 vsi->vsi_num);
367 vsi->current_netdev_flags &= ~IFF_PROMISC;
368 err = -EIO;
369 goto out_promisc;
370 }
371 /* Apply RX filter rule to get traffic from wire */
372 status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
373 ICE_FLTR_RX);
374 if (status) {
375 netdev_err(netdev, "Error setting default VSI %i rx rule\n",
376 vsi->vsi_num);
377 vsi->current_netdev_flags &= ~IFF_PROMISC;
378 err = -EIO;
379 goto out_promisc;
380 }
381 } else {
382 /* Clear TX filter rule to stop traffic from VMs */
383 status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
384 ICE_FLTR_TX);
385 if (status) {
386 netdev_err(netdev, "Error clearing default VSI %i tx rule\n",
387 vsi->vsi_num);
388 vsi->current_netdev_flags |= IFF_PROMISC;
389 err = -EIO;
390 goto out_promisc;
391 }
392 /* Clear filter RX to remove traffic from wire */
393 status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
394 ICE_FLTR_RX);
395 if (status) {
396 netdev_err(netdev, "Error clearing default VSI %i rx rule\n",
397 vsi->vsi_num);
398 vsi->current_netdev_flags |= IFF_PROMISC;
399 err = -EIO;
400 goto out_promisc;
401 }
402 }
403 }
404 goto exit;
405
406out_promisc:
407 set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
408 goto exit;
409out:
410 /* if something went wrong then set the changed flag so we try again */
411 set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
412 set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
413exit:
414 clear_bit(__ICE_CFG_BUSY, vsi->state);
415 return err;
416}
417
418/**
419 * ice_sync_fltr_subtask - Sync the VSI filter list with HW
420 * @pf: board private structure
421 */
422static void ice_sync_fltr_subtask(struct ice_pf *pf)
423{
424 int v;
425
426 if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
427 return;
428
429 clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
430
431 for (v = 0; v < pf->num_alloc_vsi; v++)
432 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
433 ice_vsi_sync_fltr(pf->vsi[v])) {
434 /* come back and try again later */
435 set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
436 break;
437 }
438}
439
440/**
441 * ice_is_reset_recovery_pending - schedule a reset
442 * @state: pf state field
443 */
444static bool ice_is_reset_recovery_pending(unsigned long int *state)
445{
446 return test_bit(__ICE_RESET_RECOVERY_PENDING, state);
447}
448
449/**
450 * ice_prepare_for_reset - prep for the core to reset
451 * @pf: board private structure
452 *
453 * Inform or close all dependent features in prep for reset.
454 */
455static void
456ice_prepare_for_reset(struct ice_pf *pf)
457{
458 struct ice_hw *hw = &pf->hw;
459 u32 v;
460
461 ice_for_each_vsi(pf, v)
462 if (pf->vsi[v])
463 ice_remove_vsi_fltr(hw, pf->vsi[v]->vsi_num);
464
465 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
466
467 /* disable the VSIs and their queues that are not already DOWN */
468 /* pf_dis_all_vsi modifies netdev structures -rtnl_lock needed */
469 ice_pf_dis_all_vsi(pf);
470
471 ice_for_each_vsi(pf, v)
472 if (pf->vsi[v])
473 pf->vsi[v]->vsi_num = 0;
474
475 ice_shutdown_all_ctrlq(hw);
476}
477
478/**
479 * ice_do_reset - Initiate one of many types of resets
480 * @pf: board private structure
481 * @reset_type: reset type requested
482 * before this function was called.
483 */
484static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
485{
486 struct device *dev = &pf->pdev->dev;
487 struct ice_hw *hw = &pf->hw;
488
489 dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
490 WARN_ON(in_interrupt());
491
492 /* PFR is a bit of a special case because it doesn't result in an OICR
493 * interrupt. So for PFR, we prepare for reset, issue the reset and
494 * rebuild sequentially.
495 */
496 if (reset_type == ICE_RESET_PFR) {
497 set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
498 ice_prepare_for_reset(pf);
499 }
500
501 /* trigger the reset */
502 if (ice_reset(hw, reset_type)) {
503 dev_err(dev, "reset %d failed\n", reset_type);
504 set_bit(__ICE_RESET_FAILED, pf->state);
505 clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
506 return;
507 }
508
509 if (reset_type == ICE_RESET_PFR) {
510 pf->pfr_count++;
511 ice_rebuild(pf);
512 clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
513 }
514}
515
516/**
517 * ice_reset_subtask - Set up for resetting the device and driver
518 * @pf: board private structure
519 */
520static void ice_reset_subtask(struct ice_pf *pf)
521{
522 enum ice_reset_req reset_type;
523
524 rtnl_lock();
525
526 /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
527 * OICR interrupt. The OICR handler (ice_misc_intr) determines what
528 * type of reset happened and sets __ICE_RESET_RECOVERY_PENDING bit in
529 * pf->state. So if reset/recovery is pending (as indicated by this bit)
530 * we do a rebuild and return.
531 */
532 if (ice_is_reset_recovery_pending(pf->state)) {
533 clear_bit(__ICE_GLOBR_RECV, pf->state);
534 clear_bit(__ICE_CORER_RECV, pf->state);
535 ice_prepare_for_reset(pf);
536
537 /* make sure we are ready to rebuild */
538 if (ice_check_reset(&pf->hw))
539 set_bit(__ICE_RESET_FAILED, pf->state);
540 else
541 ice_rebuild(pf);
542 clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
543 goto unlock;
544 }
545
546 /* No pending resets to finish processing. Check for new resets */
547 if (test_and_clear_bit(__ICE_GLOBR_REQ, pf->state))
548 reset_type = ICE_RESET_GLOBR;
549 else if (test_and_clear_bit(__ICE_CORER_REQ, pf->state))
550 reset_type = ICE_RESET_CORER;
551 else if (test_and_clear_bit(__ICE_PFR_REQ, pf->state))
552 reset_type = ICE_RESET_PFR;
553 else
554 goto unlock;
555
556 /* reset if not already down or resetting */
557 if (!test_bit(__ICE_DOWN, pf->state) &&
558 !test_bit(__ICE_CFG_BUSY, pf->state)) {
559 ice_do_reset(pf, reset_type);
560 }
561
562unlock:
563 rtnl_unlock();
564}
565
566/**
567 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
568 * @pf: board private structure
569 */
570static void ice_watchdog_subtask(struct ice_pf *pf)
571{
572 int i;
573
574 /* if interface is down do nothing */
575 if (test_bit(__ICE_DOWN, pf->state) ||
576 test_bit(__ICE_CFG_BUSY, pf->state))
577 return;
578
579 /* make sure we don't do these things too often */
580 if (time_before(jiffies,
581 pf->serv_tmr_prev + pf->serv_tmr_period))
582 return;
583
584 pf->serv_tmr_prev = jiffies;
585
586 /* Update the stats for active netdevs so the network stack
587 * can look at updated numbers whenever it cares to
588 */
589 ice_update_pf_stats(pf);
590 for (i = 0; i < pf->num_alloc_vsi; i++)
591 if (pf->vsi[i] && pf->vsi[i]->netdev)
592 ice_update_vsi_stats(pf->vsi[i]);
593}
594
595/**
596 * ice_print_link_msg - print link up or down message
597 * @vsi: the VSI whose link status is being queried
598 * @isup: boolean for if the link is now up or down
599 */
600void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
601{
602 const char *speed;
603 const char *fc;
604
605 if (vsi->current_isup == isup)
606 return;
607
608 vsi->current_isup = isup;
609
610 if (!isup) {
611 netdev_info(vsi->netdev, "NIC Link is Down\n");
612 return;
613 }
614
615 switch (vsi->port_info->phy.link_info.link_speed) {
616 case ICE_AQ_LINK_SPEED_40GB:
617 speed = "40 G";
618 break;
619 case ICE_AQ_LINK_SPEED_25GB:
620 speed = "25 G";
621 break;
622 case ICE_AQ_LINK_SPEED_20GB:
623 speed = "20 G";
624 break;
625 case ICE_AQ_LINK_SPEED_10GB:
626 speed = "10 G";
627 break;
628 case ICE_AQ_LINK_SPEED_5GB:
629 speed = "5 G";
630 break;
631 case ICE_AQ_LINK_SPEED_2500MB:
632 speed = "2.5 G";
633 break;
634 case ICE_AQ_LINK_SPEED_1000MB:
635 speed = "1 G";
636 break;
637 case ICE_AQ_LINK_SPEED_100MB:
638 speed = "100 M";
639 break;
640 default:
641 speed = "Unknown";
642 break;
643 }
644
645 switch (vsi->port_info->fc.current_mode) {
646 case ICE_FC_FULL:
647 fc = "RX/TX";
648 break;
649 case ICE_FC_TX_PAUSE:
650 fc = "TX";
651 break;
652 case ICE_FC_RX_PAUSE:
653 fc = "RX";
654 break;
655 default:
656 fc = "Unknown";
657 break;
658 }
659
660 netdev_info(vsi->netdev, "NIC Link is up %sbps, Flow Control: %s\n",
661 speed, fc);
662}
663
664/**
665 * ice_init_link_events - enable/initialize link events
666 * @pi: pointer to the port_info instance
667 *
668 * Returns -EIO on failure, 0 on success
669 */
670static int ice_init_link_events(struct ice_port_info *pi)
671{
672 u16 mask;
673
674 mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
675 ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
676
677 if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
678 dev_dbg(ice_hw_to_dev(pi->hw),
679 "Failed to set link event mask for port %d\n",
680 pi->lport);
681 return -EIO;
682 }
683
684 if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
685 dev_dbg(ice_hw_to_dev(pi->hw),
686 "Failed to enable link events for port %d\n",
687 pi->lport);
688 return -EIO;
689 }
690
691 return 0;
692}
693
694/**
695 * ice_vsi_link_event - update the vsi's netdev
696 * @vsi: the vsi on which the link event occurred
697 * @link_up: whether or not the vsi needs to be set up or down
698 */
699static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
700{
701 if (!vsi || test_bit(__ICE_DOWN, vsi->state))
702 return;
703
704 if (vsi->type == ICE_VSI_PF) {
705 if (!vsi->netdev) {
706 dev_dbg(&vsi->back->pdev->dev,
707 "vsi->netdev is not initialized!\n");
708 return;
709 }
710 if (link_up) {
711 netif_carrier_on(vsi->netdev);
712 netif_tx_wake_all_queues(vsi->netdev);
713 } else {
714 netif_carrier_off(vsi->netdev);
715 netif_tx_stop_all_queues(vsi->netdev);
716 }
717 }
718}
719
720/**
721 * ice_link_event - process the link event
722 * @pf: pf that the link event is associated with
723 * @pi: port_info for the port that the link event is associated with
724 *
725 * Returns -EIO if ice_get_link_status() fails
726 * Returns 0 on success
727 */
728static int
729ice_link_event(struct ice_pf *pf, struct ice_port_info *pi)
730{
731 u8 new_link_speed, old_link_speed;
732 struct ice_phy_info *phy_info;
733 bool new_link_same_as_old;
734 bool new_link, old_link;
735 u8 lport;
736 u16 v;
737
738 phy_info = &pi->phy;
739 phy_info->link_info_old = phy_info->link_info;
740 /* Force ice_get_link_status() to update link info */
741 phy_info->get_link_info = true;
742
743 old_link = (phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
744 old_link_speed = phy_info->link_info_old.link_speed;
745
746 lport = pi->lport;
747 if (ice_get_link_status(pi, &new_link)) {
748 dev_dbg(&pf->pdev->dev,
749 "Could not get link status for port %d\n", lport);
750 return -EIO;
751 }
752
753 new_link_speed = phy_info->link_info.link_speed;
754
755 new_link_same_as_old = (new_link == old_link &&
756 new_link_speed == old_link_speed);
757
758 ice_for_each_vsi(pf, v) {
759 struct ice_vsi *vsi = pf->vsi[v];
760
761 if (!vsi || !vsi->port_info)
762 continue;
763
764 if (new_link_same_as_old &&
765 (test_bit(__ICE_DOWN, vsi->state) ||
766 new_link == netif_carrier_ok(vsi->netdev)))
767 continue;
768
769 if (vsi->port_info->lport == lport) {
770 ice_print_link_msg(vsi, new_link);
771 ice_vsi_link_event(vsi, new_link);
772 }
773 }
774
775 return 0;
776}
777
778/**
779 * ice_handle_link_event - handle link event via ARQ
780 * @pf: pf that the link event is associated with
781 *
782 * Return -EINVAL if port_info is null
783 * Return status on succes
784 */
785static int ice_handle_link_event(struct ice_pf *pf)
786{
787 struct ice_port_info *port_info;
788 int status;
789
790 port_info = pf->hw.port_info;
791 if (!port_info)
792 return -EINVAL;
793
794 status = ice_link_event(pf, port_info);
795 if (status)
796 dev_dbg(&pf->pdev->dev,
797 "Could not process link event, error %d\n", status);
798
799 return status;
800}
801
802/**
803 * __ice_clean_ctrlq - helper function to clean controlq rings
804 * @pf: ptr to struct ice_pf
805 * @q_type: specific Control queue type
806 */
807static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
808{
809 struct ice_rq_event_info event;
810 struct ice_hw *hw = &pf->hw;
811 struct ice_ctl_q_info *cq;
812 u16 pending, i = 0;
813 const char *qtype;
814 u32 oldval, val;
815
816 /* Do not clean control queue if/when PF reset fails */
817 if (test_bit(__ICE_RESET_FAILED, pf->state))
818 return 0;
819
820 switch (q_type) {
821 case ICE_CTL_Q_ADMIN:
822 cq = &hw->adminq;
823 qtype = "Admin";
824 break;
825 default:
826 dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
827 q_type);
828 return 0;
829 }
830
831 /* check for error indications - PF_xx_AxQLEN register layout for
832 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
833 */
834 val = rd32(hw, cq->rq.len);
835 if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
836 PF_FW_ARQLEN_ARQCRIT_M)) {
837 oldval = val;
838 if (val & PF_FW_ARQLEN_ARQVFE_M)
839 dev_dbg(&pf->pdev->dev,
840 "%s Receive Queue VF Error detected\n", qtype);
841 if (val & PF_FW_ARQLEN_ARQOVFL_M) {
842 dev_dbg(&pf->pdev->dev,
843 "%s Receive Queue Overflow Error detected\n",
844 qtype);
845 }
846 if (val & PF_FW_ARQLEN_ARQCRIT_M)
847 dev_dbg(&pf->pdev->dev,
848 "%s Receive Queue Critical Error detected\n",
849 qtype);
850 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
851 PF_FW_ARQLEN_ARQCRIT_M);
852 if (oldval != val)
853 wr32(hw, cq->rq.len, val);
854 }
855
856 val = rd32(hw, cq->sq.len);
857 if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
858 PF_FW_ATQLEN_ATQCRIT_M)) {
859 oldval = val;
860 if (val & PF_FW_ATQLEN_ATQVFE_M)
861 dev_dbg(&pf->pdev->dev,
862 "%s Send Queue VF Error detected\n", qtype);
863 if (val & PF_FW_ATQLEN_ATQOVFL_M) {
864 dev_dbg(&pf->pdev->dev,
865 "%s Send Queue Overflow Error detected\n",
866 qtype);
867 }
868 if (val & PF_FW_ATQLEN_ATQCRIT_M)
869 dev_dbg(&pf->pdev->dev,
870 "%s Send Queue Critical Error detected\n",
871 qtype);
872 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
873 PF_FW_ATQLEN_ATQCRIT_M);
874 if (oldval != val)
875 wr32(hw, cq->sq.len, val);
876 }
877
878 event.buf_len = cq->rq_buf_size;
879 event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
880 GFP_KERNEL);
881 if (!event.msg_buf)
882 return 0;
883
884 do {
885 enum ice_status ret;
886 u16 opcode;
887
888 ret = ice_clean_rq_elem(hw, cq, &event, &pending);
889 if (ret == ICE_ERR_AQ_NO_WORK)
890 break;
891 if (ret) {
892 dev_err(&pf->pdev->dev,
893 "%s Receive Queue event error %d\n", qtype,
894 ret);
895 break;
896 }
897
898 opcode = le16_to_cpu(event.desc.opcode);
899
900 switch (opcode) {
901 case ice_aqc_opc_get_link_status:
902 if (ice_handle_link_event(pf))
903 dev_err(&pf->pdev->dev,
904 "Could not handle link event");
905 break;
906 default:
907 dev_dbg(&pf->pdev->dev,
908 "%s Receive Queue unknown event 0x%04x ignored\n",
909 qtype, opcode);
910 break;
911 }
912 } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
913
914 devm_kfree(&pf->pdev->dev, event.msg_buf);
915
916 return pending && (i == ICE_DFLT_IRQ_WORK);
917}
918
919/**
920 * ice_clean_adminq_subtask - clean the AdminQ rings
921 * @pf: board private structure
922 */
923static void ice_clean_adminq_subtask(struct ice_pf *pf)
924{
925 struct ice_hw *hw = &pf->hw;
926 u32 val;
927
928 if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
929 return;
930
931 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
932 return;
933
934 clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
935
936 /* re-enable Admin queue interrupt causes */
937 val = rd32(hw, PFINT_FW_CTL);
938 wr32(hw, PFINT_FW_CTL, (val | PFINT_FW_CTL_CAUSE_ENA_M));
939
940 ice_flush(hw);
941}
942
943/**
944 * ice_service_task_schedule - schedule the service task to wake up
945 * @pf: board private structure
946 *
947 * If not already scheduled, this puts the task into the work queue.
948 */
949static void ice_service_task_schedule(struct ice_pf *pf)
950{
951 if (!test_bit(__ICE_DOWN, pf->state) &&
952 !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state))
953 queue_work(ice_wq, &pf->serv_task);
954}
955
956/**
957 * ice_service_task_complete - finish up the service task
958 * @pf: board private structure
959 */
960static void ice_service_task_complete(struct ice_pf *pf)
961{
962 WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
963
964 /* force memory (pf->state) to sync before next service task */
965 smp_mb__before_atomic();
966 clear_bit(__ICE_SERVICE_SCHED, pf->state);
967}
968
969/**
970 * ice_service_timer - timer callback to schedule service task
971 * @t: pointer to timer_list
972 */
973static void ice_service_timer(struct timer_list *t)
974{
975 struct ice_pf *pf = from_timer(pf, t, serv_tmr);
976
977 mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
978 ice_service_task_schedule(pf);
979}
980
981/**
982 * ice_service_task - manage and run subtasks
983 * @work: pointer to work_struct contained by the PF struct
984 */
985static void ice_service_task(struct work_struct *work)
986{
987 struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
988 unsigned long start_time = jiffies;
989
990 /* subtasks */
991
992 /* process reset requests first */
993 ice_reset_subtask(pf);
994
995 /* bail if a reset/recovery cycle is pending */
996 if (ice_is_reset_recovery_pending(pf->state) ||
997 test_bit(__ICE_SUSPENDED, pf->state)) {
998 ice_service_task_complete(pf);
999 return;
1000 }
1001
1002 ice_sync_fltr_subtask(pf);
1003 ice_watchdog_subtask(pf);
1004 ice_clean_adminq_subtask(pf);
1005
1006 /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
1007 ice_service_task_complete(pf);
1008
1009 /* If the tasks have taken longer than one service timer period
1010 * or there is more work to be done, reset the service timer to
1011 * schedule the service task now.
1012 */
1013 if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
1014 test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1015 mod_timer(&pf->serv_tmr, jiffies);
1016}
1017
1018/**
1019 * ice_set_ctrlq_len - helper function to set controlq length
1020 * @hw: pointer to the hw instance
1021 */
1022static void ice_set_ctrlq_len(struct ice_hw *hw)
1023{
1024 hw->adminq.num_rq_entries = ICE_AQ_LEN;
1025 hw->adminq.num_sq_entries = ICE_AQ_LEN;
1026 hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
1027 hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
1028}
1029
1030/**
1031 * ice_irq_affinity_notify - Callback for affinity changes
1032 * @notify: context as to what irq was changed
1033 * @mask: the new affinity mask
1034 *
1035 * This is a callback function used by the irq_set_affinity_notifier function
1036 * so that we may register to receive changes to the irq affinity masks.
1037 */
1038static void ice_irq_affinity_notify(struct irq_affinity_notify *notify,
1039 const cpumask_t *mask)
1040{
1041 struct ice_q_vector *q_vector =
1042 container_of(notify, struct ice_q_vector, affinity_notify);
1043
1044 cpumask_copy(&q_vector->affinity_mask, mask);
1045}
1046
1047/**
1048 * ice_irq_affinity_release - Callback for affinity notifier release
1049 * @ref: internal core kernel usage
1050 *
1051 * This is a callback function used by the irq_set_affinity_notifier function
1052 * to inform the current notification subscriber that they will no longer
1053 * receive notifications.
1054 */
1055static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
1056
1057/**
1058 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
1059 * @vsi: the VSI being un-configured
1060 */
1061static void ice_vsi_dis_irq(struct ice_vsi *vsi)
1062{
1063 struct ice_pf *pf = vsi->back;
1064 struct ice_hw *hw = &pf->hw;
1065 int base = vsi->base_vector;
1066 u32 val;
1067 int i;
1068
1069 /* disable interrupt causation from each queue */
1070 if (vsi->tx_rings) {
1071 ice_for_each_txq(vsi, i) {
1072 if (vsi->tx_rings[i]) {
1073 u16 reg;
1074
1075 reg = vsi->tx_rings[i]->reg_idx;
1076 val = rd32(hw, QINT_TQCTL(reg));
1077 val &= ~QINT_TQCTL_CAUSE_ENA_M;
1078 wr32(hw, QINT_TQCTL(reg), val);
1079 }
1080 }
1081 }
1082
1083 if (vsi->rx_rings) {
1084 ice_for_each_rxq(vsi, i) {
1085 if (vsi->rx_rings[i]) {
1086 u16 reg;
1087
1088 reg = vsi->rx_rings[i]->reg_idx;
1089 val = rd32(hw, QINT_RQCTL(reg));
1090 val &= ~QINT_RQCTL_CAUSE_ENA_M;
1091 wr32(hw, QINT_RQCTL(reg), val);
1092 }
1093 }
1094 }
1095
1096 /* disable each interrupt */
1097 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1098 for (i = vsi->base_vector;
1099 i < (vsi->num_q_vectors + vsi->base_vector); i++)
1100 wr32(hw, GLINT_DYN_CTL(i), 0);
1101
1102 ice_flush(hw);
1103 for (i = 0; i < vsi->num_q_vectors; i++)
1104 synchronize_irq(pf->msix_entries[i + base].vector);
1105 }
1106}
1107
1108/**
1109 * ice_vsi_ena_irq - Enable IRQ for the given VSI
1110 * @vsi: the VSI being configured
1111 */
1112static int ice_vsi_ena_irq(struct ice_vsi *vsi)
1113{
1114 struct ice_pf *pf = vsi->back;
1115 struct ice_hw *hw = &pf->hw;
1116
1117 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1118 int i;
1119
1120 for (i = 0; i < vsi->num_q_vectors; i++)
1121 ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
1122 }
1123
1124 ice_flush(hw);
1125 return 0;
1126}
1127
1128/**
1129 * ice_vsi_delete - delete a VSI from the switch
1130 * @vsi: pointer to VSI being removed
1131 */
1132static void ice_vsi_delete(struct ice_vsi *vsi)
1133{
1134 struct ice_pf *pf = vsi->back;
1135 struct ice_vsi_ctx ctxt;
1136 enum ice_status status;
1137
1138 ctxt.vsi_num = vsi->vsi_num;
1139
1140 memcpy(&ctxt.info, &vsi->info, sizeof(struct ice_aqc_vsi_props));
1141
1142 status = ice_aq_free_vsi(&pf->hw, &ctxt, false, NULL);
1143 if (status)
1144 dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
1145 vsi->vsi_num);
1146}
1147
1148/**
1149 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
1150 * @vsi: the VSI being configured
1151 * @basename: name for the vector
1152 */
1153static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
1154{
1155 int q_vectors = vsi->num_q_vectors;
1156 struct ice_pf *pf = vsi->back;
1157 int base = vsi->base_vector;
1158 int rx_int_idx = 0;
1159 int tx_int_idx = 0;
1160 int vector, err;
1161 int irq_num;
1162
1163 for (vector = 0; vector < q_vectors; vector++) {
1164 struct ice_q_vector *q_vector = vsi->q_vectors[vector];
1165
1166 irq_num = pf->msix_entries[base + vector].vector;
1167
1168 if (q_vector->tx.ring && q_vector->rx.ring) {
1169 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1170 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
1171 tx_int_idx++;
1172 } else if (q_vector->rx.ring) {
1173 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1174 "%s-%s-%d", basename, "rx", rx_int_idx++);
1175 } else if (q_vector->tx.ring) {
1176 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1177 "%s-%s-%d", basename, "tx", tx_int_idx++);
1178 } else {
1179 /* skip this unused q_vector */
1180 continue;
1181 }
1182 err = devm_request_irq(&pf->pdev->dev,
1183 pf->msix_entries[base + vector].vector,
1184 vsi->irq_handler, 0, q_vector->name,
1185 q_vector);
1186 if (err) {
1187 netdev_err(vsi->netdev,
1188 "MSIX request_irq failed, error: %d\n", err);
1189 goto free_q_irqs;
1190 }
1191
1192 /* register for affinity change notifications */
1193 q_vector->affinity_notify.notify = ice_irq_affinity_notify;
1194 q_vector->affinity_notify.release = ice_irq_affinity_release;
1195 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
1196
1197 /* assign the mask for this irq */
1198 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
1199 }
1200
1201 vsi->irqs_ready = true;
1202 return 0;
1203
1204free_q_irqs:
1205 while (vector) {
1206 vector--;
1207 irq_num = pf->msix_entries[base + vector].vector,
1208 irq_set_affinity_notifier(irq_num, NULL);
1209 irq_set_affinity_hint(irq_num, NULL);
1210 devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
1211 }
1212 return err;
1213}
1214
1215/**
1216 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
1217 * @vsi: the VSI being configured
1218 */
1219static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
1220{
1221 struct ice_hw_common_caps *cap;
1222 struct ice_pf *pf = vsi->back;
1223
1224 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
1225 vsi->rss_size = 1;
1226 return;
1227 }
1228
1229 cap = &pf->hw.func_caps.common_cap;
1230 switch (vsi->type) {
1231 case ICE_VSI_PF:
1232 /* PF VSI will inherit RSS instance of PF */
1233 vsi->rss_table_size = cap->rss_table_size;
1234 vsi->rss_size = min_t(int, num_online_cpus(),
1235 BIT(cap->rss_table_entry_width));
1236 vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
1237 break;
1238 default:
1239 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
1240 break;
1241 }
1242}
1243
1244/**
1245 * ice_vsi_setup_q_map - Setup a VSI queue map
1246 * @vsi: the VSI being configured
1247 * @ctxt: VSI context structure
1248 */
1249static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1250{
1251 u16 offset = 0, qmap = 0, numq_tc;
1252 u16 pow = 0, max_rss = 0, qcount;
1253 u16 qcount_tx = vsi->alloc_txq;
1254 u16 qcount_rx = vsi->alloc_rxq;
1255 bool ena_tc0 = false;
1256 int i;
1257
1258 /* at least TC0 should be enabled by default */
1259 if (vsi->tc_cfg.numtc) {
1260 if (!(vsi->tc_cfg.ena_tc & BIT(0)))
1261 ena_tc0 = true;
1262 } else {
1263 ena_tc0 = true;
1264 }
1265
1266 if (ena_tc0) {
1267 vsi->tc_cfg.numtc++;
1268 vsi->tc_cfg.ena_tc |= 1;
1269 }
1270
1271 numq_tc = qcount_rx / vsi->tc_cfg.numtc;
1272
1273 /* TC mapping is a function of the number of Rx queues assigned to the
1274 * VSI for each traffic class and the offset of these queues.
1275 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
1276 * queues allocated to TC0. No:of queues is a power-of-2.
1277 *
1278 * If TC is not enabled, the queue offset is set to 0, and allocate one
1279 * queue, this way, traffic for the given TC will be sent to the default
1280 * queue.
1281 *
1282 * Setup number and offset of Rx queues for all TCs for the VSI
1283 */
1284
1285 /* qcount will change if RSS is enabled */
1286 if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
1287 if (vsi->type == ICE_VSI_PF)
1288 max_rss = ICE_MAX_LG_RSS_QS;
1289 else
1290 max_rss = ICE_MAX_SMALL_RSS_QS;
1291
1292 qcount = min_t(int, numq_tc, max_rss);
1293 qcount = min_t(int, qcount, vsi->rss_size);
1294 } else {
1295 qcount = numq_tc;
1296 }
1297
1298 /* find higher power-of-2 of qcount */
1299 pow = ilog2(qcount);
1300
1301 if (!is_power_of_2(qcount))
1302 pow++;
1303
1304 for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
1305 if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
1306 /* TC is not enabled */
1307 vsi->tc_cfg.tc_info[i].qoffset = 0;
1308 vsi->tc_cfg.tc_info[i].qcount = 1;
1309 ctxt->info.tc_mapping[i] = 0;
1310 continue;
1311 }
1312
1313 /* TC is enabled */
1314 vsi->tc_cfg.tc_info[i].qoffset = offset;
1315 vsi->tc_cfg.tc_info[i].qcount = qcount;
1316
1317 qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
1318 ICE_AQ_VSI_TC_Q_OFFSET_M) |
1319 ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
1320 ICE_AQ_VSI_TC_Q_NUM_M);
1321 offset += qcount;
1322 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1323 }
1324
1325 vsi->num_txq = qcount_tx;
1326 vsi->num_rxq = offset;
1327
1328 /* Rx queue mapping */
1329 ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1330 /* q_mapping buffer holds the info for the first queue allocated for
1331 * this VSI in the PF space and also the number of queues associated
1332 * with this VSI.
1333 */
1334 ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
1335 ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
1336}
1337
1338/**
1339 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
1340 * @ctxt: the VSI context being set
1341 *
1342 * This initializes a default VSI context for all sections except the Queues.
1343 */
1344static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
1345{
1346 u32 table = 0;
1347
1348 memset(&ctxt->info, 0, sizeof(ctxt->info));
1349 /* VSI's should be allocated from shared pool */
1350 ctxt->alloc_from_pool = true;
1351 /* Src pruning enabled by default */
1352 ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
1353 /* Traffic from VSI can be sent to LAN */
1354 ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
1355 /* Allow all packets untagged/tagged */
1356 ctxt->info.port_vlan_flags = ((ICE_AQ_VSI_PVLAN_MODE_ALL &
1357 ICE_AQ_VSI_PVLAN_MODE_M) >>
1358 ICE_AQ_VSI_PVLAN_MODE_S);
1359 /* Show VLAN/UP from packets in Rx descriptors */
1360 ctxt->info.port_vlan_flags |= ((ICE_AQ_VSI_PVLAN_EMOD_STR_BOTH &
1361 ICE_AQ_VSI_PVLAN_EMOD_M) >>
1362 ICE_AQ_VSI_PVLAN_EMOD_S);
1363 /* Have 1:1 UP mapping for both ingress/egress tables */
1364 table |= ICE_UP_TABLE_TRANSLATE(0, 0);
1365 table |= ICE_UP_TABLE_TRANSLATE(1, 1);
1366 table |= ICE_UP_TABLE_TRANSLATE(2, 2);
1367 table |= ICE_UP_TABLE_TRANSLATE(3, 3);
1368 table |= ICE_UP_TABLE_TRANSLATE(4, 4);
1369 table |= ICE_UP_TABLE_TRANSLATE(5, 5);
1370 table |= ICE_UP_TABLE_TRANSLATE(6, 6);
1371 table |= ICE_UP_TABLE_TRANSLATE(7, 7);
1372 ctxt->info.ingress_table = cpu_to_le32(table);
1373 ctxt->info.egress_table = cpu_to_le32(table);
1374 /* Have 1:1 UP mapping for outer to inner UP table */
1375 ctxt->info.outer_up_table = cpu_to_le32(table);
1376 /* No Outer tag support outer_tag_flags remains to zero */
1377}
1378
1379/**
1380 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1381 * @ctxt: the VSI context being set
1382 * @vsi: the VSI being configured
1383 */
1384static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1385{
1386 u8 lut_type, hash_type;
1387
1388 switch (vsi->type) {
1389 case ICE_VSI_PF:
1390 /* PF VSI will inherit RSS instance of PF */
1391 lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
1392 hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
1393 break;
1394 default:
1395 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
1396 vsi->type);
1397 return;
1398 }
1399
1400 ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
1401 ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
1402 ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
1403 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
1404}
1405
1406/**
1407 * ice_vsi_add - Create a new VSI or fetch preallocated VSI
1408 * @vsi: the VSI being configured
1409 *
1410 * This initializes a VSI context depending on the VSI type to be added and
1411 * passes it down to the add_vsi aq command to create a new VSI.
1412 */
1413static int ice_vsi_add(struct ice_vsi *vsi)
1414{
1415 struct ice_vsi_ctx ctxt = { 0 };
1416 struct ice_pf *pf = vsi->back;
1417 struct ice_hw *hw = &pf->hw;
1418 int ret = 0;
1419
1420 switch (vsi->type) {
1421 case ICE_VSI_PF:
1422 ctxt.flags = ICE_AQ_VSI_TYPE_PF;
1423 break;
1424 default:
1425 return -ENODEV;
1426 }
1427
1428 ice_set_dflt_vsi_ctx(&ctxt);
1429 /* if the switch is in VEB mode, allow VSI loopback */
1430 if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1431 ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1432
1433 /* Set LUT type and HASH type if RSS is enabled */
1434 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
1435 ice_set_rss_vsi_ctx(&ctxt, vsi);
1436
1437 ctxt.info.sw_id = vsi->port_info->sw_id;
1438 ice_vsi_setup_q_map(vsi, &ctxt);
1439
1440 ret = ice_aq_add_vsi(hw, &ctxt, NULL);
1441 if (ret) {
1442 dev_err(&vsi->back->pdev->dev,
1443 "Add VSI AQ call failed, err %d\n", ret);
1444 return -EIO;
1445 }
1446 vsi->info = ctxt.info;
1447 vsi->vsi_num = ctxt.vsi_num;
1448
1449 return ret;
1450}
1451
1452/**
1453 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
1454 * @vsi: the VSI being cleaned up
1455 */
1456static void ice_vsi_release_msix(struct ice_vsi *vsi)
1457{
1458 struct ice_pf *pf = vsi->back;
1459 u16 vector = vsi->base_vector;
1460 struct ice_hw *hw = &pf->hw;
1461 u32 txq = 0;
1462 u32 rxq = 0;
1463 int i, q;
1464
1465 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1466 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1467
1468 wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
1469 wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
1470 for (q = 0; q < q_vector->num_ring_tx; q++) {
1471 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
1472 txq++;
1473 }
1474
1475 for (q = 0; q < q_vector->num_ring_rx; q++) {
1476 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
1477 rxq++;
1478 }
1479 }
1480
1481 ice_flush(hw);
1482}
1483
1484/**
1485 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1486 * @vsi: the VSI having rings deallocated
1487 */
1488static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1489{
1490 int i;
1491
1492 if (vsi->tx_rings) {
1493 for (i = 0; i < vsi->alloc_txq; i++) {
1494 if (vsi->tx_rings[i]) {
1495 kfree_rcu(vsi->tx_rings[i], rcu);
1496 vsi->tx_rings[i] = NULL;
1497 }
1498 }
1499 }
1500 if (vsi->rx_rings) {
1501 for (i = 0; i < vsi->alloc_rxq; i++) {
1502 if (vsi->rx_rings[i]) {
1503 kfree_rcu(vsi->rx_rings[i], rcu);
1504 vsi->rx_rings[i] = NULL;
1505 }
1506 }
1507 }
1508}
1509
1510/**
1511 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1512 * @vsi: VSI which is having rings allocated
1513 */
1514static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1515{
1516 struct ice_pf *pf = vsi->back;
1517 int i;
1518
1519 /* Allocate tx_rings */
1520 for (i = 0; i < vsi->alloc_txq; i++) {
1521 struct ice_ring *ring;
1522
1523 /* allocate with kzalloc(), free with kfree_rcu() */
1524 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1525
1526 if (!ring)
1527 goto err_out;
1528
1529 ring->q_index = i;
1530 ring->reg_idx = vsi->txq_map[i];
1531 ring->ring_active = false;
1532 ring->vsi = vsi;
1533 ring->netdev = vsi->netdev;
1534 ring->dev = &pf->pdev->dev;
1535 ring->count = vsi->num_desc;
1536
1537 vsi->tx_rings[i] = ring;
1538 }
1539
1540 /* Allocate rx_rings */
1541 for (i = 0; i < vsi->alloc_rxq; i++) {
1542 struct ice_ring *ring;
1543
1544 /* allocate with kzalloc(), free with kfree_rcu() */
1545 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1546 if (!ring)
1547 goto err_out;
1548
1549 ring->q_index = i;
1550 ring->reg_idx = vsi->rxq_map[i];
1551 ring->ring_active = false;
1552 ring->vsi = vsi;
1553 ring->netdev = vsi->netdev;
1554 ring->dev = &pf->pdev->dev;
1555 ring->count = vsi->num_desc;
1556 vsi->rx_rings[i] = ring;
1557 }
1558
1559 return 0;
1560
1561err_out:
1562 ice_vsi_clear_rings(vsi);
1563 return -ENOMEM;
1564}
1565
1566/**
1567 * ice_vsi_free_irq - Free the irq association with the OS
1568 * @vsi: the VSI being configured
1569 */
1570static void ice_vsi_free_irq(struct ice_vsi *vsi)
1571{
1572 struct ice_pf *pf = vsi->back;
1573 int base = vsi->base_vector;
1574
1575 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
1576 int i;
1577
1578 if (!vsi->q_vectors || !vsi->irqs_ready)
1579 return;
1580
1581 vsi->irqs_ready = false;
1582 for (i = 0; i < vsi->num_q_vectors; i++) {
1583 u16 vector = i + base;
1584 int irq_num;
1585
1586 irq_num = pf->msix_entries[vector].vector;
1587
1588 /* free only the irqs that were actually requested */
1589 if (!vsi->q_vectors[i] ||
1590 !(vsi->q_vectors[i]->num_ring_tx ||
1591 vsi->q_vectors[i]->num_ring_rx))
1592 continue;
1593
1594 /* clear the affinity notifier in the IRQ descriptor */
1595 irq_set_affinity_notifier(irq_num, NULL);
1596
1597 /* clear the affinity_mask in the IRQ descriptor */
1598 irq_set_affinity_hint(irq_num, NULL);
1599 synchronize_irq(irq_num);
1600 devm_free_irq(&pf->pdev->dev, irq_num,
1601 vsi->q_vectors[i]);
1602 }
1603 ice_vsi_release_msix(vsi);
1604 }
1605}
1606
1607/**
1608 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1609 * @vsi: the VSI being configured
1610 */
1611static void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1612{
1613 struct ice_pf *pf = vsi->back;
1614 u16 vector = vsi->base_vector;
1615 struct ice_hw *hw = &pf->hw;
1616 u32 txq = 0, rxq = 0;
1617 int i, q, itr;
1618 u8 itr_gran;
1619
1620 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1621 struct ice_q_vector *q_vector = vsi->q_vectors[i];
1622
1623 itr_gran = hw->itr_gran_200;
1624
1625 if (q_vector->num_ring_rx) {
1626 q_vector->rx.itr =
1627 ITR_TO_REG(vsi->rx_rings[rxq]->rx_itr_setting,
1628 itr_gran);
1629 q_vector->rx.latency_range = ICE_LOW_LATENCY;
1630 }
1631
1632 if (q_vector->num_ring_tx) {
1633 q_vector->tx.itr =
1634 ITR_TO_REG(vsi->tx_rings[txq]->tx_itr_setting,
1635 itr_gran);
1636 q_vector->tx.latency_range = ICE_LOW_LATENCY;
1637 }
1638 wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), q_vector->rx.itr);
1639 wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), q_vector->tx.itr);
1640
1641 /* Both Transmit Queue Interrupt Cause Control register
1642 * and Receive Queue Interrupt Cause control register
1643 * expects MSIX_INDX field to be the vector index
1644 * within the function space and not the absolute
1645 * vector index across PF or across device.
1646 * For SR-IOV VF VSIs queue vector index always starts
1647 * with 1 since first vector index(0) is used for OICR
1648 * in VF space. Since VMDq and other PF VSIs are withtin
1649 * the PF function space, use the vector index thats
1650 * tracked for this PF.
1651 */
1652 for (q = 0; q < q_vector->num_ring_tx; q++) {
1653 u32 val;
1654
1655 itr = ICE_TX_ITR;
1656 val = QINT_TQCTL_CAUSE_ENA_M |
1657 (itr << QINT_TQCTL_ITR_INDX_S) |
1658 (vector << QINT_TQCTL_MSIX_INDX_S);
1659 wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
1660 txq++;
1661 }
1662
1663 for (q = 0; q < q_vector->num_ring_rx; q++) {
1664 u32 val;
1665
1666 itr = ICE_RX_ITR;
1667 val = QINT_RQCTL_CAUSE_ENA_M |
1668 (itr << QINT_RQCTL_ITR_INDX_S) |
1669 (vector << QINT_RQCTL_MSIX_INDX_S);
1670 wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
1671 rxq++;
1672 }
1673 }
1674
1675 ice_flush(hw);
1676}
1677
1678/**
1679 * ice_ena_misc_vector - enable the non-queue interrupts
1680 * @pf: board private structure
1681 */
1682static void ice_ena_misc_vector(struct ice_pf *pf)
1683{
1684 struct ice_hw *hw = &pf->hw;
1685 u32 val;
1686
1687 /* clear things first */
1688 wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
1689 rd32(hw, PFINT_OICR); /* read to clear */
1690
1691 val = (PFINT_OICR_HLP_RDY_M |
1692 PFINT_OICR_CPM_RDY_M |
1693 PFINT_OICR_ECC_ERR_M |
1694 PFINT_OICR_MAL_DETECT_M |
1695 PFINT_OICR_GRST_M |
1696 PFINT_OICR_PCI_EXCEPTION_M |
1697 PFINT_OICR_GPIO_M |
1698 PFINT_OICR_STORM_DETECT_M |
1699 PFINT_OICR_HMC_ERR_M);
1700
1701 wr32(hw, PFINT_OICR_ENA, val);
1702
1703 /* SW_ITR_IDX = 0, but don't change INTENA */
1704 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
1705 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
1706}
1707
1708/**
1709 * ice_misc_intr - misc interrupt handler
1710 * @irq: interrupt number
1711 * @data: pointer to a q_vector
1712 */
1713static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
1714{
1715 struct ice_pf *pf = (struct ice_pf *)data;
1716 struct ice_hw *hw = &pf->hw;
1717 irqreturn_t ret = IRQ_NONE;
1718 u32 oicr, ena_mask;
1719
1720 set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1721
1722 oicr = rd32(hw, PFINT_OICR);
1723 ena_mask = rd32(hw, PFINT_OICR_ENA);
1724
1725 if (oicr & PFINT_OICR_GRST_M) {
1726 u32 reset;
1727 /* we have a reset warning */
1728 ena_mask &= ~PFINT_OICR_GRST_M;
1729 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
1730 GLGEN_RSTAT_RESET_TYPE_S;
1731
1732 if (reset == ICE_RESET_CORER)
1733 pf->corer_count++;
1734 else if (reset == ICE_RESET_GLOBR)
1735 pf->globr_count++;
1736 else
1737 pf->empr_count++;
1738
1739 /* If a reset cycle isn't already in progress, we set a bit in
1740 * pf->state so that the service task can start a reset/rebuild.
1741 * We also make note of which reset happened so that peer
1742 * devices/drivers can be informed.
1743 */
1744 if (!test_bit(__ICE_RESET_RECOVERY_PENDING, pf->state)) {
1745 if (reset == ICE_RESET_CORER)
1746 set_bit(__ICE_CORER_RECV, pf->state);
1747 else if (reset == ICE_RESET_GLOBR)
1748 set_bit(__ICE_GLOBR_RECV, pf->state);
1749 else
1750 set_bit(__ICE_EMPR_RECV, pf->state);
1751
1752 set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
1753 }
1754 }
1755
1756 if (oicr & PFINT_OICR_HMC_ERR_M) {
1757 ena_mask &= ~PFINT_OICR_HMC_ERR_M;
1758 dev_dbg(&pf->pdev->dev,
1759 "HMC Error interrupt - info 0x%x, data 0x%x\n",
1760 rd32(hw, PFHMC_ERRORINFO),
1761 rd32(hw, PFHMC_ERRORDATA));
1762 }
1763
1764 /* Report and mask off any remaining unexpected interrupts */
1765 oicr &= ena_mask;
1766 if (oicr) {
1767 dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
1768 oicr);
1769 /* If a critical error is pending there is no choice but to
1770 * reset the device.
1771 */
1772 if (oicr & (PFINT_OICR_PE_CRITERR_M |
1773 PFINT_OICR_PCI_EXCEPTION_M |
1774 PFINT_OICR_ECC_ERR_M)) {
1775 set_bit(__ICE_PFR_REQ, pf->state);
1776 ice_service_task_schedule(pf);
1777 }
1778 ena_mask &= ~oicr;
1779 }
1780 ret = IRQ_HANDLED;
1781
1782 /* re-enable interrupt causes that are not handled during this pass */
1783 wr32(hw, PFINT_OICR_ENA, ena_mask);
1784 if (!test_bit(__ICE_DOWN, pf->state)) {
1785 ice_service_task_schedule(pf);
1786 ice_irq_dynamic_ena(hw, NULL, NULL);
1787 }
1788
1789 return ret;
1790}
1791
1792/**
1793 * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
1794 * @vsi: the VSI being configured
1795 *
1796 * This function maps descriptor rings to the queue-specific vectors allotted
1797 * through the MSI-X enabling code. On a constrained vector budget, we map Tx
1798 * and Rx rings to the vector as "efficiently" as possible.
1799 */
1800static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
1801{
1802 int q_vectors = vsi->num_q_vectors;
1803 int tx_rings_rem, rx_rings_rem;
1804 int v_id;
1805
1806 /* initially assigning remaining rings count to VSIs num queue value */
1807 tx_rings_rem = vsi->num_txq;
1808 rx_rings_rem = vsi->num_rxq;
1809
1810 for (v_id = 0; v_id < q_vectors; v_id++) {
1811 struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
1812 int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
1813
1814 /* Tx rings mapping to vector */
1815 tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
1816 q_vector->num_ring_tx = tx_rings_per_v;
1817 q_vector->tx.ring = NULL;
1818 q_base = vsi->num_txq - tx_rings_rem;
1819
1820 for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
1821 struct ice_ring *tx_ring = vsi->tx_rings[q_id];
1822
1823 tx_ring->q_vector = q_vector;
1824 tx_ring->next = q_vector->tx.ring;
1825 q_vector->tx.ring = tx_ring;
1826 }
1827 tx_rings_rem -= tx_rings_per_v;
1828
1829 /* Rx rings mapping to vector */
1830 rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
1831 q_vector->num_ring_rx = rx_rings_per_v;
1832 q_vector->rx.ring = NULL;
1833 q_base = vsi->num_rxq - rx_rings_rem;
1834
1835 for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
1836 struct ice_ring *rx_ring = vsi->rx_rings[q_id];
1837
1838 rx_ring->q_vector = q_vector;
1839 rx_ring->next = q_vector->rx.ring;
1840 q_vector->rx.ring = rx_ring;
1841 }
1842 rx_rings_rem -= rx_rings_per_v;
1843 }
1844}
1845
1846/**
1847 * ice_vsi_set_num_qs - Set num queues, descriptors and vectors for a VSI
1848 * @vsi: the VSI being configured
1849 *
1850 * Return 0 on success and a negative value on error
1851 */
1852static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
1853{
1854 struct ice_pf *pf = vsi->back;
1855
1856 switch (vsi->type) {
1857 case ICE_VSI_PF:
1858 vsi->alloc_txq = pf->num_lan_tx;
1859 vsi->alloc_rxq = pf->num_lan_rx;
1860 vsi->num_desc = ALIGN(ICE_DFLT_NUM_DESC, ICE_REQ_DESC_MULTIPLE);
1861 vsi->num_q_vectors = max_t(int, pf->num_lan_rx, pf->num_lan_tx);
1862 break;
1863 default:
1864 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
1865 vsi->type);
1866 break;
1867 }
1868}
1869
1870/**
1871 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
1872 * @vsi: VSI pointer
1873 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
1874 *
1875 * On error: returns error code (negative)
1876 * On success: returns 0
1877 */
1878static int ice_vsi_alloc_arrays(struct ice_vsi *vsi, bool alloc_qvectors)
1879{
1880 struct ice_pf *pf = vsi->back;
1881
1882 /* allocate memory for both Tx and Rx ring pointers */
1883 vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
1884 sizeof(struct ice_ring *), GFP_KERNEL);
1885 if (!vsi->tx_rings)
1886 goto err_txrings;
1887
1888 vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
1889 sizeof(struct ice_ring *), GFP_KERNEL);
1890 if (!vsi->rx_rings)
1891 goto err_rxrings;
1892
1893 if (alloc_qvectors) {
1894 /* allocate memory for q_vector pointers */
1895 vsi->q_vectors = devm_kcalloc(&pf->pdev->dev,
1896 vsi->num_q_vectors,
1897 sizeof(struct ice_q_vector *),
1898 GFP_KERNEL);
1899 if (!vsi->q_vectors)
1900 goto err_vectors;
1901 }
1902
1903 return 0;
1904
1905err_vectors:
1906 devm_kfree(&pf->pdev->dev, vsi->rx_rings);
1907err_rxrings:
1908 devm_kfree(&pf->pdev->dev, vsi->tx_rings);
1909err_txrings:
1910 return -ENOMEM;
1911}
1912
1913/**
1914 * ice_msix_clean_rings - MSIX mode Interrupt Handler
1915 * @irq: interrupt number
1916 * @data: pointer to a q_vector
1917 */
1918static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
1919{
1920 struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
1921
1922 if (!q_vector->tx.ring && !q_vector->rx.ring)
1923 return IRQ_HANDLED;
1924
1925 napi_schedule(&q_vector->napi);
1926
1927 return IRQ_HANDLED;
1928}
1929
1930/**
1931 * ice_vsi_alloc - Allocates the next available struct vsi in the PF
1932 * @pf: board private structure
1933 * @type: type of VSI
1934 *
1935 * returns a pointer to a VSI on success, NULL on failure.
1936 */
1937static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type)
1938{
1939 struct ice_vsi *vsi = NULL;
1940
1941 /* Need to protect the allocation of the VSIs at the PF level */
1942 mutex_lock(&pf->sw_mutex);
1943
1944 /* If we have already allocated our maximum number of VSIs,
1945 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
1946 * is available to be populated
1947 */
1948 if (pf->next_vsi == ICE_NO_VSI) {
1949 dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
1950 goto unlock_pf;
1951 }
1952
1953 vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
1954 if (!vsi)
1955 goto unlock_pf;
1956
1957 vsi->type = type;
1958 vsi->back = pf;
1959 set_bit(__ICE_DOWN, vsi->state);
1960 vsi->idx = pf->next_vsi;
1961 vsi->work_lmt = ICE_DFLT_IRQ_WORK;
1962
1963 ice_vsi_set_num_qs(vsi);
1964
1965 switch (vsi->type) {
1966 case ICE_VSI_PF:
1967 if (ice_vsi_alloc_arrays(vsi, true))
1968 goto err_rings;
1969
1970 /* Setup default MSIX irq handler for VSI */
1971 vsi->irq_handler = ice_msix_clean_rings;
1972 break;
1973 default:
1974 dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
1975 goto unlock_pf;
1976 }
1977
1978 /* fill VSI slot in the PF struct */
1979 pf->vsi[pf->next_vsi] = vsi;
1980
1981 /* prepare pf->next_vsi for next use */
1982 pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
1983 pf->next_vsi);
1984 goto unlock_pf;
1985
1986err_rings:
1987 devm_kfree(&pf->pdev->dev, vsi);
1988 vsi = NULL;
1989unlock_pf:
1990 mutex_unlock(&pf->sw_mutex);
1991 return vsi;
1992}
1993
1994/**
1995 * ice_free_irq_msix_misc - Unroll misc vector setup
1996 * @pf: board private structure
1997 */
1998static void ice_free_irq_msix_misc(struct ice_pf *pf)
1999{
2000 /* disable OICR interrupt */
2001 wr32(&pf->hw, PFINT_OICR_ENA, 0);
2002 ice_flush(&pf->hw);
2003
2004 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
2005 synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
2006 devm_free_irq(&pf->pdev->dev,
2007 pf->msix_entries[pf->oicr_idx].vector, pf);
2008 }
2009
2010 ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
2011}
2012
2013/**
2014 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
2015 * @pf: board private structure
2016 *
2017 * This sets up the handler for MSIX 0, which is used to manage the
2018 * non-queue interrupts, e.g. AdminQ and errors. This is not used
2019 * when in MSI or Legacy interrupt mode.
2020 */
2021static int ice_req_irq_msix_misc(struct ice_pf *pf)
2022{
2023 struct ice_hw *hw = &pf->hw;
2024 int oicr_idx, err = 0;
2025 u8 itr_gran;
2026 u32 val;
2027
2028 if (!pf->int_name[0])
2029 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
2030 dev_driver_string(&pf->pdev->dev),
2031 dev_name(&pf->pdev->dev));
2032
2033 /* Do not request IRQ but do enable OICR interrupt since settings are
2034 * lost during reset. Note that this function is called only during
2035 * rebuild path and not while reset is in progress.
2036 */
2037 if (ice_is_reset_recovery_pending(pf->state))
2038 goto skip_req_irq;
2039
2040 /* reserve one vector in irq_tracker for misc interrupts */
2041 oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2042 if (oicr_idx < 0)
2043 return oicr_idx;
2044
2045 pf->oicr_idx = oicr_idx;
2046
2047 err = devm_request_irq(&pf->pdev->dev,
2048 pf->msix_entries[pf->oicr_idx].vector,
2049 ice_misc_intr, 0, pf->int_name, pf);
2050 if (err) {
2051 dev_err(&pf->pdev->dev,
2052 "devm_request_irq for %s failed: %d\n",
2053 pf->int_name, err);
2054 ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2055 return err;
2056 }
2057
2058skip_req_irq:
2059 ice_ena_misc_vector(pf);
2060
2061 val = (pf->oicr_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
2062 (ICE_RX_ITR & PFINT_OICR_CTL_ITR_INDX_M) |
2063 PFINT_OICR_CTL_CAUSE_ENA_M;
2064 wr32(hw, PFINT_OICR_CTL, val);
2065
2066 /* This enables Admin queue Interrupt causes */
2067 val = (pf->oicr_idx & PFINT_FW_CTL_MSIX_INDX_M) |
2068 (ICE_RX_ITR & PFINT_FW_CTL_ITR_INDX_M) |
2069 PFINT_FW_CTL_CAUSE_ENA_M;
2070 wr32(hw, PFINT_FW_CTL, val);
2071
2072 itr_gran = hw->itr_gran_200;
2073
2074 wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
2075 ITR_TO_REG(ICE_ITR_8K, itr_gran));
2076
2077 ice_flush(hw);
2078 ice_irq_dynamic_ena(hw, NULL, NULL);
2079
2080 return 0;
2081}
2082
2083/**
2084 * ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
2085 * @vsi: the VSI getting queues
2086 *
2087 * Return 0 on success and a negative value on error
2088 */
2089static int ice_vsi_get_qs_contig(struct ice_vsi *vsi)
2090{
2091 struct ice_pf *pf = vsi->back;
2092 int offset, ret = 0;
2093
2094 mutex_lock(&pf->avail_q_mutex);
2095 /* look for contiguous block of queues for tx */
2096 offset = bitmap_find_next_zero_area(pf->avail_txqs, ICE_MAX_TXQS,
2097 0, vsi->alloc_txq, 0);
2098 if (offset < ICE_MAX_TXQS) {
2099 int i;
2100
2101 bitmap_set(pf->avail_txqs, offset, vsi->alloc_txq);
2102 for (i = 0; i < vsi->alloc_txq; i++)
2103 vsi->txq_map[i] = i + offset;
2104 } else {
2105 ret = -ENOMEM;
2106 vsi->tx_mapping_mode = ICE_VSI_MAP_SCATTER;
2107 }
2108
2109 /* look for contiguous block of queues for rx */
2110 offset = bitmap_find_next_zero_area(pf->avail_rxqs, ICE_MAX_RXQS,
2111 0, vsi->alloc_rxq, 0);
2112 if (offset < ICE_MAX_RXQS) {
2113 int i;
2114
2115 bitmap_set(pf->avail_rxqs, offset, vsi->alloc_rxq);
2116 for (i = 0; i < vsi->alloc_rxq; i++)
2117 vsi->rxq_map[i] = i + offset;
2118 } else {
2119 ret = -ENOMEM;
2120 vsi->rx_mapping_mode = ICE_VSI_MAP_SCATTER;
2121 }
2122 mutex_unlock(&pf->avail_q_mutex);
2123
2124 return ret;
2125}
2126
2127/**
2128 * ice_vsi_get_qs_scatter - Assign a scattered queues to VSI
2129 * @vsi: the VSI getting queues
2130 *
2131 * Return 0 on success and a negative value on error
2132 */
2133static int ice_vsi_get_qs_scatter(struct ice_vsi *vsi)
2134{
2135 struct ice_pf *pf = vsi->back;
2136 int i, index = 0;
2137
2138 mutex_lock(&pf->avail_q_mutex);
2139
2140 if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER) {
2141 for (i = 0; i < vsi->alloc_txq; i++) {
2142 index = find_next_zero_bit(pf->avail_txqs,
2143 ICE_MAX_TXQS, index);
2144 if (index < ICE_MAX_TXQS) {
2145 set_bit(index, pf->avail_txqs);
2146 vsi->txq_map[i] = index;
2147 } else {
2148 goto err_scatter_tx;
2149 }
2150 }
2151 }
2152
2153 if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER) {
2154 for (i = 0; i < vsi->alloc_rxq; i++) {
2155 index = find_next_zero_bit(pf->avail_rxqs,
2156 ICE_MAX_RXQS, index);
2157 if (index < ICE_MAX_RXQS) {
2158 set_bit(index, pf->avail_rxqs);
2159 vsi->rxq_map[i] = index;
2160 } else {
2161 goto err_scatter_rx;
2162 }
2163 }
2164 }
2165
2166 mutex_unlock(&pf->avail_q_mutex);
2167 return 0;
2168
2169err_scatter_rx:
2170 /* unflag any queues we have grabbed (i is failed position) */
2171 for (index = 0; index < i; index++) {
2172 clear_bit(vsi->rxq_map[index], pf->avail_rxqs);
2173 vsi->rxq_map[index] = 0;
2174 }
2175 i = vsi->alloc_txq;
2176err_scatter_tx:
2177 /* i is either position of failed attempt or vsi->alloc_txq */
2178 for (index = 0; index < i; index++) {
2179 clear_bit(vsi->txq_map[index], pf->avail_txqs);
2180 vsi->txq_map[index] = 0;
2181 }
2182
2183 mutex_unlock(&pf->avail_q_mutex);
2184 return -ENOMEM;
2185}
2186
2187/**
2188 * ice_vsi_get_qs - Assign queues from PF to VSI
2189 * @vsi: the VSI to assign queues to
2190 *
2191 * Returns 0 on success and a negative value on error
2192 */
2193static int ice_vsi_get_qs(struct ice_vsi *vsi)
2194{
2195 int ret = 0;
2196
2197 vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
2198 vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
2199
2200 /* NOTE: ice_vsi_get_qs_contig() will set the rx/tx mapping
2201 * modes individually to scatter if assigning contiguous queues
2202 * to rx or tx fails
2203 */
2204 ret = ice_vsi_get_qs_contig(vsi);
2205 if (ret < 0) {
2206 if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER)
2207 vsi->alloc_txq = max_t(u16, vsi->alloc_txq,
2208 ICE_MAX_SCATTER_TXQS);
2209 if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER)
2210 vsi->alloc_rxq = max_t(u16, vsi->alloc_rxq,
2211 ICE_MAX_SCATTER_RXQS);
2212 ret = ice_vsi_get_qs_scatter(vsi);
2213 }
2214
2215 return ret;
2216}
2217
2218/**
2219 * ice_vsi_put_qs - Release queues from VSI to PF
2220 * @vsi: the VSI thats going to release queues
2221 */
2222static void ice_vsi_put_qs(struct ice_vsi *vsi)
2223{
2224 struct ice_pf *pf = vsi->back;
2225 int i;
2226
2227 mutex_lock(&pf->avail_q_mutex);
2228
2229 for (i = 0; i < vsi->alloc_txq; i++) {
2230 clear_bit(vsi->txq_map[i], pf->avail_txqs);
2231 vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
2232 }
2233
2234 for (i = 0; i < vsi->alloc_rxq; i++) {
2235 clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
2236 vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
2237 }
2238
2239 mutex_unlock(&pf->avail_q_mutex);
2240}
2241
2242/**
2243 * ice_free_q_vector - Free memory allocated for a specific interrupt vector
2244 * @vsi: VSI having the memory freed
2245 * @v_idx: index of the vector to be freed
2246 */
2247static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
2248{
2249 struct ice_q_vector *q_vector;
2250 struct ice_ring *ring;
2251
2252 if (!vsi->q_vectors[v_idx]) {
2253 dev_dbg(&vsi->back->pdev->dev, "Queue vector at index %d not found\n",
2254 v_idx);
2255 return;
2256 }
2257 q_vector = vsi->q_vectors[v_idx];
2258
2259 ice_for_each_ring(ring, q_vector->tx)
2260 ring->q_vector = NULL;
2261 ice_for_each_ring(ring, q_vector->rx)
2262 ring->q_vector = NULL;
2263
2264 /* only VSI with an associated netdev is set up with NAPI */
2265 if (vsi->netdev)
2266 netif_napi_del(&q_vector->napi);
2267
2268 devm_kfree(&vsi->back->pdev->dev, q_vector);
2269 vsi->q_vectors[v_idx] = NULL;
2270}
2271
2272/**
2273 * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
2274 * @vsi: the VSI having memory freed
2275 */
2276static void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
2277{
2278 int v_idx;
2279
2280 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
2281 ice_free_q_vector(vsi, v_idx);
2282}
2283
2284/**
2285 * ice_cfg_netdev - Setup the netdev flags
2286 * @vsi: the VSI being configured
2287 *
2288 * Returns 0 on success, negative value on failure
2289 */
2290static int ice_cfg_netdev(struct ice_vsi *vsi)
2291{
2292 netdev_features_t csumo_features;
2293 netdev_features_t vlano_features;
2294 netdev_features_t dflt_features;
2295 netdev_features_t tso_features;
2296 struct ice_netdev_priv *np;
2297 struct net_device *netdev;
2298 u8 mac_addr[ETH_ALEN];
2299
2300 netdev = alloc_etherdev_mqs(sizeof(struct ice_netdev_priv),
2301 vsi->alloc_txq, vsi->alloc_rxq);
2302 if (!netdev)
2303 return -ENOMEM;
2304
2305 vsi->netdev = netdev;
2306 np = netdev_priv(netdev);
2307 np->vsi = vsi;
2308
2309 dflt_features = NETIF_F_SG |
2310 NETIF_F_HIGHDMA |
2311 NETIF_F_RXHASH;
2312
2313 csumo_features = NETIF_F_RXCSUM |
2314 NETIF_F_IP_CSUM |
2315 NETIF_F_IPV6_CSUM;
2316
2317 vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
2318 NETIF_F_HW_VLAN_CTAG_TX |
2319 NETIF_F_HW_VLAN_CTAG_RX;
2320
2321 tso_features = NETIF_F_TSO;
2322
2323 /* set features that user can change */
2324 netdev->hw_features = dflt_features | csumo_features |
2325 vlano_features | tso_features;
2326
2327 /* enable features */
2328 netdev->features |= netdev->hw_features;
2329 /* encap and VLAN devices inherit default, csumo and tso features */
2330 netdev->hw_enc_features |= dflt_features | csumo_features |
2331 tso_features;
2332 netdev->vlan_features |= dflt_features | csumo_features |
2333 tso_features;
2334
2335 if (vsi->type == ICE_VSI_PF) {
2336 SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
2337 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
2338
2339 ether_addr_copy(netdev->dev_addr, mac_addr);
2340 ether_addr_copy(netdev->perm_addr, mac_addr);
2341 }
2342
2343 netdev->priv_flags |= IFF_UNICAST_FLT;
2344
2345 /* assign netdev_ops */
2346 netdev->netdev_ops = &ice_netdev_ops;
2347
2348 /* setup watchdog timeout value to be 5 second */
2349 netdev->watchdog_timeo = 5 * HZ;
2350
2351 ice_set_ethtool_ops(netdev);
2352
2353 netdev->min_mtu = ETH_MIN_MTU;
2354 netdev->max_mtu = ICE_MAX_MTU;
2355
2356 return 0;
2357}
2358
2359/**
2360 * ice_vsi_free_arrays - clean up vsi resources
2361 * @vsi: pointer to VSI being cleared
2362 * @free_qvectors: bool to specify if q_vectors should be deallocated
2363 */
2364static void ice_vsi_free_arrays(struct ice_vsi *vsi, bool free_qvectors)
2365{
2366 struct ice_pf *pf = vsi->back;
2367
2368 /* free the ring and vector containers */
2369 if (free_qvectors && vsi->q_vectors) {
2370 devm_kfree(&pf->pdev->dev, vsi->q_vectors);
2371 vsi->q_vectors = NULL;
2372 }
2373 if (vsi->tx_rings) {
2374 devm_kfree(&pf->pdev->dev, vsi->tx_rings);
2375 vsi->tx_rings = NULL;
2376 }
2377 if (vsi->rx_rings) {
2378 devm_kfree(&pf->pdev->dev, vsi->rx_rings);
2379 vsi->rx_rings = NULL;
2380 }
2381}
2382
2383/**
2384 * ice_vsi_clear - clean up and deallocate the provided vsi
2385 * @vsi: pointer to VSI being cleared
2386 *
2387 * This deallocates the vsi's queue resources, removes it from the PF's
2388 * VSI array if necessary, and deallocates the VSI
2389 *
2390 * Returns 0 on success, negative on failure
2391 */
2392static int ice_vsi_clear(struct ice_vsi *vsi)
2393{
2394 struct ice_pf *pf = NULL;
2395
2396 if (!vsi)
2397 return 0;
2398
2399 if (!vsi->back)
2400 return -EINVAL;
2401
2402 pf = vsi->back;
2403
2404 if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
2405 dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
2406 vsi->idx);
2407 return -EINVAL;
2408 }
2409
2410 mutex_lock(&pf->sw_mutex);
2411 /* updates the PF for this cleared vsi */
2412
2413 pf->vsi[vsi->idx] = NULL;
2414 if (vsi->idx < pf->next_vsi)
2415 pf->next_vsi = vsi->idx;
2416
2417 ice_vsi_free_arrays(vsi, true);
2418 mutex_unlock(&pf->sw_mutex);
2419 devm_kfree(&pf->pdev->dev, vsi);
2420
2421 return 0;
2422}
2423
2424/**
2425 * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
2426 * @vsi: the VSI being configured
2427 * @v_idx: index of the vector in the vsi struct
2428 *
2429 * We allocate one q_vector. If allocation fails we return -ENOMEM.
2430 */
2431static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, int v_idx)
2432{
2433 struct ice_pf *pf = vsi->back;
2434 struct ice_q_vector *q_vector;
2435
2436 /* allocate q_vector */
2437 q_vector = devm_kzalloc(&pf->pdev->dev, sizeof(*q_vector), GFP_KERNEL);
2438 if (!q_vector)
2439 return -ENOMEM;
2440
2441 q_vector->vsi = vsi;
2442 q_vector->v_idx = v_idx;
2443 /* only set affinity_mask if the CPU is online */
2444 if (cpu_online(v_idx))
2445 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
2446
2447 if (vsi->netdev)
2448 netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
2449 NAPI_POLL_WEIGHT);
2450 /* tie q_vector and vsi together */
2451 vsi->q_vectors[v_idx] = q_vector;
2452
2453 return 0;
2454}
2455
2456/**
2457 * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
2458 * @vsi: the VSI being configured
2459 *
2460 * We allocate one q_vector per queue interrupt. If allocation fails we
2461 * return -ENOMEM.
2462 */
2463static int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
2464{
2465 struct ice_pf *pf = vsi->back;
2466 int v_idx = 0, num_q_vectors;
2467 int err;
2468
2469 if (vsi->q_vectors[0]) {
2470 dev_dbg(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
2471 vsi->vsi_num);
2472 return -EEXIST;
2473 }
2474
2475 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
2476 num_q_vectors = vsi->num_q_vectors;
2477 } else {
2478 err = -EINVAL;
2479 goto err_out;
2480 }
2481
2482 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
2483 err = ice_vsi_alloc_q_vector(vsi, v_idx);
2484 if (err)
2485 goto err_out;
2486 }
2487
2488 return 0;
2489
2490err_out:
2491 while (v_idx--)
2492 ice_free_q_vector(vsi, v_idx);
2493
2494 dev_err(&pf->pdev->dev,
2495 "Failed to allocate %d q_vector for VSI %d, ret=%d\n",
2496 vsi->num_q_vectors, vsi->vsi_num, err);
2497 vsi->num_q_vectors = 0;
2498 return err;
2499}
2500
2501/**
2502 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
2503 * @vsi: ptr to the VSI
2504 *
2505 * This should only be called after ice_vsi_alloc() which allocates the
2506 * corresponding SW VSI structure and initializes num_queue_pairs for the
2507 * newly allocated VSI.
2508 *
2509 * Returns 0 on success or negative on failure
2510 */
2511static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
2512{
2513 struct ice_pf *pf = vsi->back;
2514 int num_q_vectors = 0;
2515
2516 if (vsi->base_vector) {
2517 dev_dbg(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
2518 vsi->vsi_num, vsi->base_vector);
2519 return -EEXIST;
2520 }
2521
2522 if (!test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
2523 return -ENOENT;
2524
2525 switch (vsi->type) {
2526 case ICE_VSI_PF:
2527 num_q_vectors = vsi->num_q_vectors;
2528 break;
2529 default:
2530 dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
2531 vsi->type);
2532 break;
2533 }
2534
2535 if (num_q_vectors)
2536 vsi->base_vector = ice_get_res(pf, pf->irq_tracker,
2537 num_q_vectors, vsi->idx);
2538
2539 if (vsi->base_vector < 0) {
2540 dev_err(&pf->pdev->dev,
2541 "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
2542 num_q_vectors, vsi->vsi_num, vsi->base_vector);
2543 return -ENOENT;
2544 }
2545
2546 return 0;
2547}
2548
2549/**
2550 * ice_fill_rss_lut - Fill the RSS lookup table with default values
2551 * @lut: Lookup table
2552 * @rss_table_size: Lookup table size
2553 * @rss_size: Range of queue number for hashing
2554 */
2555void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
2556{
2557 u16 i;
2558
2559 for (i = 0; i < rss_table_size; i++)
2560 lut[i] = i % rss_size;
2561}
2562
2563/**
2564 * ice_vsi_cfg_rss - Configure RSS params for a VSI
2565 * @vsi: VSI to be configured
2566 */
2567static int ice_vsi_cfg_rss(struct ice_vsi *vsi)
2568{
2569 u8 seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE];
2570 struct ice_aqc_get_set_rss_keys *key;
2571 struct ice_pf *pf = vsi->back;
2572 enum ice_status status;
2573 int err = 0;
2574 u8 *lut;
2575
2576 vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
2577
2578 lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
2579 if (!lut)
2580 return -ENOMEM;
2581
2582 if (vsi->rss_lut_user)
2583 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
2584 else
2585 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
2586
2587 status = ice_aq_set_rss_lut(&pf->hw, vsi->vsi_num, vsi->rss_lut_type,
2588 lut, vsi->rss_table_size);
2589
2590 if (status) {
2591 dev_err(&vsi->back->pdev->dev,
2592 "set_rss_lut failed, error %d\n", status);
2593 err = -EIO;
2594 goto ice_vsi_cfg_rss_exit;
2595 }
2596
2597 key = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*key), GFP_KERNEL);
2598 if (!key) {
2599 err = -ENOMEM;
2600 goto ice_vsi_cfg_rss_exit;
2601 }
2602
2603 if (vsi->rss_hkey_user)
2604 memcpy(seed, vsi->rss_hkey_user,
2605 ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2606 else
2607 netdev_rss_key_fill((void *)seed,
2608 ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2609 memcpy(&key->standard_rss_key, seed,
2610 ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
2611
2612 status = ice_aq_set_rss_key(&pf->hw, vsi->vsi_num, key);
2613
2614 if (status) {
2615 dev_err(&vsi->back->pdev->dev, "set_rss_key failed, error %d\n",
2616 status);
2617 err = -EIO;
2618 }
2619
2620 devm_kfree(&pf->pdev->dev, key);
2621ice_vsi_cfg_rss_exit:
2622 devm_kfree(&pf->pdev->dev, lut);
2623 return err;
2624}
2625
2626/**
2627 * ice_vsi_reinit_setup - return resource and reallocate resource for a VSI
2628 * @vsi: pointer to the ice_vsi
2629 *
2630 * This reallocates the VSIs queue resources
2631 *
2632 * Returns 0 on success and negative value on failure
2633 */
2634static int ice_vsi_reinit_setup(struct ice_vsi *vsi)
2635{
2636 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2637 int ret, i;
2638
2639 if (!vsi)
2640 return -EINVAL;
2641
2642 ice_vsi_free_q_vectors(vsi);
2643 ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
2644 vsi->base_vector = 0;
2645 ice_vsi_clear_rings(vsi);
2646 ice_vsi_free_arrays(vsi, false);
2647 ice_vsi_set_num_qs(vsi);
2648
2649 /* Initialize VSI struct elements and create VSI in FW */
2650 ret = ice_vsi_add(vsi);
2651 if (ret < 0)
2652 goto err_vsi;
2653
2654 ret = ice_vsi_alloc_arrays(vsi, false);
2655 if (ret < 0)
2656 goto err_vsi;
2657
2658 switch (vsi->type) {
2659 case ICE_VSI_PF:
2660 if (!vsi->netdev) {
2661 ret = ice_cfg_netdev(vsi);
2662 if (ret)
2663 goto err_rings;
2664
2665 ret = register_netdev(vsi->netdev);
2666 if (ret)
2667 goto err_rings;
2668
2669 netif_carrier_off(vsi->netdev);
2670 netif_tx_stop_all_queues(vsi->netdev);
2671 }
2672
2673 ret = ice_vsi_alloc_q_vectors(vsi);
2674 if (ret)
2675 goto err_rings;
2676
2677 ret = ice_vsi_setup_vector_base(vsi);
2678 if (ret)
2679 goto err_vectors;
2680
2681 ret = ice_vsi_alloc_rings(vsi);
2682 if (ret)
2683 goto err_vectors;
2684
2685 ice_vsi_map_rings_to_vectors(vsi);
2686 break;
2687 default:
2688 break;
2689 }
2690
2691 ice_vsi_set_tc_cfg(vsi);
2692
2693 /* configure VSI nodes based on number of queues and TC's */
2694 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2695 max_txqs[i] = vsi->num_txq;
2696
2697 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
2698 vsi->tc_cfg.ena_tc, max_txqs);
2699 if (ret) {
2700 dev_info(&vsi->back->pdev->dev,
2701 "Failed VSI lan queue config\n");
2702 goto err_vectors;
2703 }
2704 return 0;
2705
2706err_vectors:
2707 ice_vsi_free_q_vectors(vsi);
2708err_rings:
2709 if (vsi->netdev) {
2710 vsi->current_netdev_flags = 0;
2711 unregister_netdev(vsi->netdev);
2712 free_netdev(vsi->netdev);
2713 vsi->netdev = NULL;
2714 }
2715err_vsi:
2716 ice_vsi_clear(vsi);
2717 set_bit(__ICE_RESET_FAILED, vsi->back->state);
2718 return ret;
2719}
2720
2721/**
2722 * ice_vsi_setup - Set up a VSI by a given type
2723 * @pf: board private structure
2724 * @type: VSI type
2725 * @pi: pointer to the port_info instance
2726 *
2727 * This allocates the sw VSI structure and its queue resources.
2728 *
2729 * Returns pointer to the successfully allocated and configure VSI sw struct on
2730 * success, otherwise returns NULL on failure.
2731 */
2732static struct ice_vsi *
2733ice_vsi_setup(struct ice_pf *pf, enum ice_vsi_type type,
2734 struct ice_port_info *pi)
2735{
2736 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2737 struct device *dev = &pf->pdev->dev;
2738 struct ice_vsi_ctx ctxt = { 0 };
2739 struct ice_vsi *vsi;
2740 int ret, i;
2741
2742 vsi = ice_vsi_alloc(pf, type);
2743 if (!vsi) {
2744 dev_err(dev, "could not allocate VSI\n");
2745 return NULL;
2746 }
2747
2748 vsi->port_info = pi;
2749 vsi->vsw = pf->first_sw;
2750
2751 if (ice_vsi_get_qs(vsi)) {
2752 dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2753 vsi->idx);
2754 goto err_get_qs;
2755 }
2756
2757 /* set RSS capabilities */
2758 ice_vsi_set_rss_params(vsi);
2759
2760 /* create the VSI */
2761 ret = ice_vsi_add(vsi);
2762 if (ret)
2763 goto err_vsi;
2764
2765 ctxt.vsi_num = vsi->vsi_num;
2766
2767 switch (vsi->type) {
2768 case ICE_VSI_PF:
2769 ret = ice_cfg_netdev(vsi);
2770 if (ret)
2771 goto err_cfg_netdev;
2772
2773 ret = register_netdev(vsi->netdev);
2774 if (ret)
2775 goto err_register_netdev;
2776
2777 netif_carrier_off(vsi->netdev);
2778
2779 /* make sure transmit queues start off as stopped */
2780 netif_tx_stop_all_queues(vsi->netdev);
2781 ret = ice_vsi_alloc_q_vectors(vsi);
2782 if (ret)
2783 goto err_msix;
2784
2785 ret = ice_vsi_setup_vector_base(vsi);
2786 if (ret)
2787 goto err_rings;
2788
2789 ret = ice_vsi_alloc_rings(vsi);
2790 if (ret)
2791 goto err_rings;
2792
2793 ice_vsi_map_rings_to_vectors(vsi);
2794
2795 /* Do not exit if configuring RSS had an issue, at least
2796 * receive traffic on first queue. Hence no need to capture
2797 * return value
2798 */
2799 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2800 ice_vsi_cfg_rss(vsi);
2801 break;
2802 default:
2803 /* if vsi type is not recognized, clean up the resources and
2804 * exit
2805 */
2806 goto err_rings;
2807 }
2808
2809 ice_vsi_set_tc_cfg(vsi);
2810
2811 /* configure VSI nodes based on number of queues and TC's */
2812 for (i = 0; i < vsi->tc_cfg.numtc; i++)
2813 max_txqs[i] = vsi->num_txq;
2814
2815 ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
2816 vsi->tc_cfg.ena_tc, max_txqs);
2817 if (ret) {
2818 dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
2819 goto err_rings;
2820 }
2821
2822 return vsi;
2823
2824err_rings:
2825 ice_vsi_free_q_vectors(vsi);
2826err_msix:
2827 if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
2828 unregister_netdev(vsi->netdev);
2829err_register_netdev:
2830 if (vsi->netdev) {
2831 free_netdev(vsi->netdev);
2832 vsi->netdev = NULL;
2833 }
2834err_cfg_netdev:
2835 ret = ice_aq_free_vsi(&pf->hw, &ctxt, false, NULL);
2836 if (ret)
2837 dev_err(&vsi->back->pdev->dev,
2838 "Free VSI AQ call failed, err %d\n", ret);
2839err_vsi:
2840 ice_vsi_put_qs(vsi);
2841err_get_qs:
2842 pf->q_left_tx += vsi->alloc_txq;
2843 pf->q_left_rx += vsi->alloc_rxq;
2844 ice_vsi_clear(vsi);
2845
2846 return NULL;
2847}
2848
2849/**
2850 * ice_vsi_add_vlan - Add vsi membership for given vlan
2851 * @vsi: the vsi being configured
2852 * @vid: vlan id to be added
2853 */
2854static int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
2855{
2856 struct ice_fltr_list_entry *tmp;
2857 struct ice_pf *pf = vsi->back;
2858 LIST_HEAD(tmp_add_list);
2859 enum ice_status status;
2860 int err = 0;
2861
2862 tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
2863 if (!tmp)
2864 return -ENOMEM;
2865
2866 tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
2867 tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2868 tmp->fltr_info.flag = ICE_FLTR_TX;
2869 tmp->fltr_info.src = vsi->vsi_num;
2870 tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
2871 tmp->fltr_info.l_data.vlan.vlan_id = vid;
2872
2873 INIT_LIST_HEAD(&tmp->list_entry);
2874 list_add(&tmp->list_entry, &tmp_add_list);
2875
2876 status = ice_add_vlan(&pf->hw, &tmp_add_list);
2877 if (status) {
2878 err = -ENODEV;
2879 dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
2880 vid, vsi->vsi_num);
2881 }
2882
2883 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
2884 return err;
2885}
2886
2887/**
2888 * ice_vlan_rx_add_vid - Add a vlan id filter to HW offload
2889 * @netdev: network interface to be adjusted
2890 * @proto: unused protocol
2891 * @vid: vlan id to be added
2892 *
2893 * net_device_ops implementation for adding vlan ids
2894 */
2895static int ice_vlan_rx_add_vid(struct net_device *netdev,
2896 __always_unused __be16 proto, u16 vid)
2897{
2898 struct ice_netdev_priv *np = netdev_priv(netdev);
2899 struct ice_vsi *vsi = np->vsi;
2900 int ret = 0;
2901
2902 if (vid >= VLAN_N_VID) {
2903 netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
2904 vid, VLAN_N_VID);
2905 return -EINVAL;
2906 }
2907
2908 if (vsi->info.pvid)
2909 return -EINVAL;
2910
2911 /* Add all VLAN ids including 0 to the switch filter. VLAN id 0 is
2912 * needed to continue allowing all untagged packets since VLAN prune
2913 * list is applied to all packets by the switch
2914 */
2915 ret = ice_vsi_add_vlan(vsi, vid);
2916
2917 if (!ret)
2918 set_bit(vid, vsi->active_vlans);
2919
2920 return ret;
2921}
2922
2923/**
2924 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
2925 * @vsi: the VSI being configured
2926 * @vid: VLAN id to be removed
2927 */
2928static void ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
2929{
2930 struct ice_fltr_list_entry *list;
2931 struct ice_pf *pf = vsi->back;
2932 LIST_HEAD(tmp_add_list);
2933
2934 list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
2935 if (!list)
2936 return;
2937
2938 list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
2939 list->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
2940 list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
2941 list->fltr_info.l_data.vlan.vlan_id = vid;
2942 list->fltr_info.flag = ICE_FLTR_TX;
2943 list->fltr_info.src = vsi->vsi_num;
2944
2945 INIT_LIST_HEAD(&list->list_entry);
2946 list_add(&list->list_entry, &tmp_add_list);
2947
2948 if (ice_remove_vlan(&pf->hw, &tmp_add_list))
2949 dev_err(&pf->pdev->dev, "Error removing VLAN %d on vsi %i\n",
2950 vid, vsi->vsi_num);
2951
2952 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
2953}
2954
2955/**
2956 * ice_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2957 * @netdev: network interface to be adjusted
2958 * @proto: unused protocol
2959 * @vid: vlan id to be removed
2960 *
2961 * net_device_ops implementation for removing vlan ids
2962 */
2963static int ice_vlan_rx_kill_vid(struct net_device *netdev,
2964 __always_unused __be16 proto, u16 vid)
2965{
2966 struct ice_netdev_priv *np = netdev_priv(netdev);
2967 struct ice_vsi *vsi = np->vsi;
2968
2969 if (vsi->info.pvid)
2970 return -EINVAL;
2971
2972 /* return code is ignored as there is nothing a user
2973 * can do about failure to remove and a log message was
2974 * already printed from the other function
2975 */
2976 ice_vsi_kill_vlan(vsi, vid);
2977
2978 clear_bit(vid, vsi->active_vlans);
2979
2980 return 0;
2981}
2982
2983/**
2984 * ice_setup_pf_sw - Setup the HW switch on startup or after reset
2985 * @pf: board private structure
2986 *
2987 * Returns 0 on success, negative value on failure
2988 */
2989static int ice_setup_pf_sw(struct ice_pf *pf)
2990{
2991 LIST_HEAD(tmp_add_list);
2992 u8 broadcast[ETH_ALEN];
2993 struct ice_vsi *vsi;
2994 int status = 0;
2995
2996 if (!ice_is_reset_recovery_pending(pf->state)) {
2997 vsi = ice_vsi_setup(pf, ICE_VSI_PF, pf->hw.port_info);
2998 if (!vsi) {
2999 status = -ENOMEM;
3000 goto error_exit;
3001 }
3002 } else {
3003 vsi = pf->vsi[0];
3004 status = ice_vsi_reinit_setup(vsi);
3005 if (status < 0)
3006 return -EIO;
3007 }
3008
3009 /* tmp_add_list contains a list of MAC addresses for which MAC
3010 * filters need to be programmed. Add the VSI's unicast MAC to
3011 * this list
3012 */
3013 status = ice_add_mac_to_list(vsi, &tmp_add_list,
3014 vsi->port_info->mac.perm_addr);
3015 if (status)
3016 goto error_exit;
3017
3018 /* VSI needs to receive broadcast traffic, so add the broadcast
3019 * MAC address to the list.
3020 */
3021 eth_broadcast_addr(broadcast);
3022 status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
3023 if (status)
3024 goto error_exit;
3025
3026 /* program MAC filters for entries in tmp_add_list */
3027 status = ice_add_mac(&pf->hw, &tmp_add_list);
3028 if (status) {
3029 dev_err(&pf->pdev->dev, "Could not add MAC filters\n");
3030 status = -ENOMEM;
3031 goto error_exit;
3032 }
3033
3034 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
3035 return status;
3036
3037error_exit:
3038 ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
3039
3040 if (vsi) {
3041 ice_vsi_free_q_vectors(vsi);
3042 if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
3043 unregister_netdev(vsi->netdev);
3044 if (vsi->netdev) {
3045 free_netdev(vsi->netdev);
3046 vsi->netdev = NULL;
3047 }
3048
3049 ice_vsi_delete(vsi);
3050 ice_vsi_put_qs(vsi);
3051 pf->q_left_tx += vsi->alloc_txq;
3052 pf->q_left_rx += vsi->alloc_rxq;
3053 ice_vsi_clear(vsi);
3054 }
3055 return status;
3056}
3057
3058/**
3059 * ice_determine_q_usage - Calculate queue distribution
3060 * @pf: board private structure
3061 *
3062 * Return -ENOMEM if we don't get enough queues for all ports
3063 */
3064static void ice_determine_q_usage(struct ice_pf *pf)
3065{
3066 u16 q_left_tx, q_left_rx;
3067
3068 q_left_tx = pf->hw.func_caps.common_cap.num_txq;
3069 q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
3070
3071 pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
3072
3073 /* only 1 rx queue unless RSS is enabled */
3074 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3075 pf->num_lan_rx = 1;
3076 else
3077 pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
3078
3079 pf->q_left_tx = q_left_tx - pf->num_lan_tx;
3080 pf->q_left_rx = q_left_rx - pf->num_lan_rx;
3081}
3082
3083/**
3084 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3085 * @pf: board private structure to initialize
3086 */
3087static void ice_deinit_pf(struct ice_pf *pf)
3088{
3089 if (pf->serv_tmr.function)
3090 del_timer_sync(&pf->serv_tmr);
3091 if (pf->serv_task.func)
3092 cancel_work_sync(&pf->serv_task);
3093 mutex_destroy(&pf->sw_mutex);
3094 mutex_destroy(&pf->avail_q_mutex);
3095}
3096
3097/**
3098 * ice_init_pf - Initialize general software structures (struct ice_pf)
3099 * @pf: board private structure to initialize
3100 */
3101static void ice_init_pf(struct ice_pf *pf)
3102{
3103 bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
3104 set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3105
3106 mutex_init(&pf->sw_mutex);
3107 mutex_init(&pf->avail_q_mutex);
3108
3109 /* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
3110 mutex_lock(&pf->avail_q_mutex);
3111 bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
3112 bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
3113 mutex_unlock(&pf->avail_q_mutex);
3114
3115 if (pf->hw.func_caps.common_cap.rss_table_size)
3116 set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3117
3118 /* setup service timer and periodic service task */
3119 timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3120 pf->serv_tmr_period = HZ;
3121 INIT_WORK(&pf->serv_task, ice_service_task);
3122 clear_bit(__ICE_SERVICE_SCHED, pf->state);
3123}
3124
3125/**
3126 * ice_ena_msix_range - Request a range of MSIX vectors from the OS
3127 * @pf: board private structure
3128 *
3129 * compute the number of MSIX vectors required (v_budget) and request from
3130 * the OS. Return the number of vectors reserved or negative on failure
3131 */
3132static int ice_ena_msix_range(struct ice_pf *pf)
3133{
3134 int v_left, v_actual, v_budget = 0;
3135 int needed, err, i;
3136
3137 v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
3138
3139 /* reserve one vector for miscellaneous handler */
3140 needed = 1;
3141 v_budget += needed;
3142 v_left -= needed;
3143
3144 /* reserve vectors for LAN traffic */
3145 pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
3146 v_budget += pf->num_lan_msix;
3147
3148 pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
3149 sizeof(struct msix_entry), GFP_KERNEL);
3150
3151 if (!pf->msix_entries) {
3152 err = -ENOMEM;
3153 goto exit_err;
3154 }
3155
3156 for (i = 0; i < v_budget; i++)
3157 pf->msix_entries[i].entry = i;
3158
3159 /* actually reserve the vectors */
3160 v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
3161 ICE_MIN_MSIX, v_budget);
3162
3163 if (v_actual < 0) {
3164 dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
3165 err = v_actual;
3166 goto msix_err;
3167 }
3168
3169 if (v_actual < v_budget) {
3170 dev_warn(&pf->pdev->dev,
3171 "not enough vectors. requested = %d, obtained = %d\n",
3172 v_budget, v_actual);
3173 if (v_actual >= (pf->num_lan_msix + 1)) {
3174 pf->num_avail_msix = v_actual - (pf->num_lan_msix + 1);
3175 } else if (v_actual >= 2) {
3176 pf->num_lan_msix = 1;
3177 pf->num_avail_msix = v_actual - 2;
3178 } else {
3179 pci_disable_msix(pf->pdev);
3180 err = -ERANGE;
3181 goto msix_err;
3182 }
3183 }
3184
3185 return v_actual;
3186
3187msix_err:
3188 devm_kfree(&pf->pdev->dev, pf->msix_entries);
3189 goto exit_err;
3190
3191exit_err:
3192 pf->num_lan_msix = 0;
3193 clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3194 return err;
3195}
3196
3197/**
3198 * ice_dis_msix - Disable MSI-X interrupt setup in OS
3199 * @pf: board private structure
3200 */
3201static void ice_dis_msix(struct ice_pf *pf)
3202{
3203 pci_disable_msix(pf->pdev);
3204 devm_kfree(&pf->pdev->dev, pf->msix_entries);
3205 pf->msix_entries = NULL;
3206 clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
3207}
3208
3209/**
3210 * ice_init_interrupt_scheme - Determine proper interrupt scheme
3211 * @pf: board private structure to initialize
3212 */
3213static int ice_init_interrupt_scheme(struct ice_pf *pf)
3214{
3215 int vectors = 0;
3216 ssize_t size;
3217
3218 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3219 vectors = ice_ena_msix_range(pf);
3220 else
3221 return -ENODEV;
3222
3223 if (vectors < 0)
3224 return vectors;
3225
3226 /* set up vector assignment tracking */
3227 size = sizeof(struct ice_res_tracker) + (sizeof(u16) * vectors);
3228
3229 pf->irq_tracker = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
3230 if (!pf->irq_tracker) {
3231 ice_dis_msix(pf);
3232 return -ENOMEM;
3233 }
3234
3235 pf->irq_tracker->num_entries = vectors;
3236
3237 return 0;
3238}
3239
3240/**
3241 * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
3242 * @pf: board private structure
3243 */
3244static void ice_clear_interrupt_scheme(struct ice_pf *pf)
3245{
3246 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
3247 ice_dis_msix(pf);
3248
3249 devm_kfree(&pf->pdev->dev, pf->irq_tracker);
3250 pf->irq_tracker = NULL;
3251}
3252
3253/**
3254 * ice_probe - Device initialization routine
3255 * @pdev: PCI device information struct
3256 * @ent: entry in ice_pci_tbl
3257 *
3258 * Returns 0 on success, negative on failure
3259 */
3260static int ice_probe(struct pci_dev *pdev,
3261 const struct pci_device_id __always_unused *ent)
3262{
3263 struct ice_pf *pf;
3264 struct ice_hw *hw;
3265 int err;
3266
3267 /* this driver uses devres, see Documentation/driver-model/devres.txt */
3268 err = pcim_enable_device(pdev);
3269 if (err)
3270 return err;
3271
3272 err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
3273 if (err) {
3274 dev_err(&pdev->dev, "I/O map error %d\n", err);
3275 return err;
3276 }
3277
3278 pf = devm_kzalloc(&pdev->dev, sizeof(*pf), GFP_KERNEL);
3279 if (!pf)
3280 return -ENOMEM;
3281
3282 /* set up for high or low dma */
3283 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3284 if (err)
3285 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3286 if (err) {
3287 dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err);
3288 return err;
3289 }
3290
3291 pci_enable_pcie_error_reporting(pdev);
3292 pci_set_master(pdev);
3293
3294 pf->pdev = pdev;
3295 pci_set_drvdata(pdev, pf);
3296 set_bit(__ICE_DOWN, pf->state);
3297
3298 hw = &pf->hw;
3299 hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
3300 hw->back = pf;
3301 hw->vendor_id = pdev->vendor;
3302 hw->device_id = pdev->device;
3303 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
3304 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3305 hw->subsystem_device_id = pdev->subsystem_device;
3306 hw->bus.device = PCI_SLOT(pdev->devfn);
3307 hw->bus.func = PCI_FUNC(pdev->devfn);
3308 ice_set_ctrlq_len(hw);
3309
3310 pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
3311
3312#ifndef CONFIG_DYNAMIC_DEBUG
3313 if (debug < -1)
3314 hw->debug_mask = debug;
3315#endif
3316
3317 err = ice_init_hw(hw);
3318 if (err) {
3319 dev_err(&pdev->dev, "ice_init_hw failed: %d\n", err);
3320 err = -EIO;
3321 goto err_exit_unroll;
3322 }
3323
3324 dev_info(&pdev->dev, "firmware %d.%d.%05d api %d.%d\n",
3325 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
3326 hw->api_maj_ver, hw->api_min_ver);
3327
3328 ice_init_pf(pf);
3329
3330 ice_determine_q_usage(pf);
3331
3332 pf->num_alloc_vsi = min_t(u16, ICE_MAX_VSI_ALLOC,
3333 hw->func_caps.guaranteed_num_vsi);
3334 if (!pf->num_alloc_vsi) {
3335 err = -EIO;
3336 goto err_init_pf_unroll;
3337 }
3338
3339 pf->vsi = devm_kcalloc(&pdev->dev, pf->num_alloc_vsi,
3340 sizeof(struct ice_vsi *), GFP_KERNEL);
3341 if (!pf->vsi) {
3342 err = -ENOMEM;
3343 goto err_init_pf_unroll;
3344 }
3345
3346 err = ice_init_interrupt_scheme(pf);
3347 if (err) {
3348 dev_err(&pdev->dev,
3349 "ice_init_interrupt_scheme failed: %d\n", err);
3350 err = -EIO;
3351 goto err_init_interrupt_unroll;
3352 }
3353
3354 /* In case of MSIX we are going to setup the misc vector right here
3355 * to handle admin queue events etc. In case of legacy and MSI
3356 * the misc functionality and queue processing is combined in
3357 * the same vector and that gets setup at open.
3358 */
3359 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
3360 err = ice_req_irq_msix_misc(pf);
3361 if (err) {
3362 dev_err(&pdev->dev,
3363 "setup of misc vector failed: %d\n", err);
3364 goto err_init_interrupt_unroll;
3365 }
3366 }
3367
3368 /* create switch struct for the switch element created by FW on boot */
3369 pf->first_sw = devm_kzalloc(&pdev->dev, sizeof(struct ice_sw),
3370 GFP_KERNEL);
3371 if (!pf->first_sw) {
3372 err = -ENOMEM;
3373 goto err_msix_misc_unroll;
3374 }
3375
3376 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
3377 pf->first_sw->pf = pf;
3378
3379 /* record the sw_id available for later use */
3380 pf->first_sw->sw_id = hw->port_info->sw_id;
3381
3382 err = ice_setup_pf_sw(pf);
3383 if (err) {
3384 dev_err(&pdev->dev,
3385 "probe failed due to setup pf switch:%d\n", err);
3386 goto err_alloc_sw_unroll;
3387 }
3388
3389 /* Driver is mostly up */
3390 clear_bit(__ICE_DOWN, pf->state);
3391
3392 /* since everything is good, start the service timer */
3393 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
3394
3395 err = ice_init_link_events(pf->hw.port_info);
3396 if (err) {
3397 dev_err(&pdev->dev, "ice_init_link_events failed: %d\n", err);
3398 goto err_alloc_sw_unroll;
3399 }
3400
3401 return 0;
3402
3403err_alloc_sw_unroll:
3404 set_bit(__ICE_DOWN, pf->state);
3405 devm_kfree(&pf->pdev->dev, pf->first_sw);
3406err_msix_misc_unroll:
3407 ice_free_irq_msix_misc(pf);
3408err_init_interrupt_unroll:
3409 ice_clear_interrupt_scheme(pf);
3410 devm_kfree(&pdev->dev, pf->vsi);
3411err_init_pf_unroll:
3412 ice_deinit_pf(pf);
3413 ice_deinit_hw(hw);
3414err_exit_unroll:
3415 pci_disable_pcie_error_reporting(pdev);
3416 return err;
3417}
3418
3419/**
3420 * ice_remove - Device removal routine
3421 * @pdev: PCI device information struct
3422 */
3423static void ice_remove(struct pci_dev *pdev)
3424{
3425 struct ice_pf *pf = pci_get_drvdata(pdev);
3426 int i = 0;
3427 int err;
3428
3429 if (!pf)
3430 return;
3431
3432 set_bit(__ICE_DOWN, pf->state);
3433
3434 for (i = 0; i < pf->num_alloc_vsi; i++) {
3435 if (!pf->vsi[i])
3436 continue;
3437
3438 err = ice_vsi_release(pf->vsi[i]);
3439 if (err)
3440 dev_dbg(&pf->pdev->dev, "Failed to release VSI index %d (err %d)\n",
3441 i, err);
3442 }
3443
3444 ice_free_irq_msix_misc(pf);
3445 ice_clear_interrupt_scheme(pf);
3446 ice_deinit_pf(pf);
3447 ice_deinit_hw(&pf->hw);
3448 pci_disable_pcie_error_reporting(pdev);
3449}
3450
3451/* ice_pci_tbl - PCI Device ID Table
3452 *
3453 * Wildcard entries (PCI_ANY_ID) should come last
3454 * Last entry must be all 0s
3455 *
3456 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
3457 * Class, Class Mask, private data (not used) }
3458 */
3459static const struct pci_device_id ice_pci_tbl[] = {
3460 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_BACKPLANE), 0 },
3461 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_QSFP), 0 },
3462 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SFP), 0 },
3463 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_10G_BASE_T), 0 },
3464 { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SGMII), 0 },
3465 /* required last entry */
3466 { 0, }
3467};
3468MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
3469
3470static struct pci_driver ice_driver = {
3471 .name = KBUILD_MODNAME,
3472 .id_table = ice_pci_tbl,
3473 .probe = ice_probe,
3474 .remove = ice_remove,
3475};
3476
3477/**
3478 * ice_module_init - Driver registration routine
3479 *
3480 * ice_module_init is the first routine called when the driver is
3481 * loaded. All it does is register with the PCI subsystem.
3482 */
3483static int __init ice_module_init(void)
3484{
3485 int status;
3486
3487 pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
3488 pr_info("%s\n", ice_copyright);
3489
3490 ice_wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM, KBUILD_MODNAME);
3491 if (!ice_wq) {
3492 pr_err("Failed to create workqueue\n");
3493 return -ENOMEM;
3494 }
3495
3496 status = pci_register_driver(&ice_driver);
3497 if (status) {
3498 pr_err("failed to register pci driver, err %d\n", status);
3499 destroy_workqueue(ice_wq);
3500 }
3501
3502 return status;
3503}
3504module_init(ice_module_init);
3505
3506/**
3507 * ice_module_exit - Driver exit cleanup routine
3508 *
3509 * ice_module_exit is called just before the driver is removed
3510 * from memory.
3511 */
3512static void __exit ice_module_exit(void)
3513{
3514 pci_unregister_driver(&ice_driver);
3515 destroy_workqueue(ice_wq);
3516 pr_info("module unloaded\n");
3517}
3518module_exit(ice_module_exit);
3519
3520/**
3521 * ice_set_mac_address - NDO callback to set mac address
3522 * @netdev: network interface device structure
3523 * @pi: pointer to an address structure
3524 *
3525 * Returns 0 on success, negative on failure
3526 */
3527static int ice_set_mac_address(struct net_device *netdev, void *pi)
3528{
3529 struct ice_netdev_priv *np = netdev_priv(netdev);
3530 struct ice_vsi *vsi = np->vsi;
3531 struct ice_pf *pf = vsi->back;
3532 struct ice_hw *hw = &pf->hw;
3533 struct sockaddr *addr = pi;
3534 enum ice_status status;
3535 LIST_HEAD(a_mac_list);
3536 LIST_HEAD(r_mac_list);
3537 u8 flags = 0;
3538 int err;
3539 u8 *mac;
3540
3541 mac = (u8 *)addr->sa_data;
3542
3543 if (!is_valid_ether_addr(mac))
3544 return -EADDRNOTAVAIL;
3545
3546 if (ether_addr_equal(netdev->dev_addr, mac)) {
3547 netdev_warn(netdev, "already using mac %pM\n", mac);
3548 return 0;
3549 }
3550
3551 if (test_bit(__ICE_DOWN, pf->state) ||
3552 ice_is_reset_recovery_pending(pf->state)) {
3553 netdev_err(netdev, "can't set mac %pM. device not ready\n",
3554 mac);
3555 return -EBUSY;
3556 }
3557
3558 /* When we change the mac address we also have to change the mac address
3559 * based filter rules that were created previously for the old mac
3560 * address. So first, we remove the old filter rule using ice_remove_mac
3561 * and then create a new filter rule using ice_add_mac. Note that for
3562 * both these operations, we first need to form a "list" of mac
3563 * addresses (even though in this case, we have only 1 mac address to be
3564 * added/removed) and this done using ice_add_mac_to_list. Depending on
3565 * the ensuing operation this "list" of mac addresses is either to be
3566 * added or removed from the filter.
3567 */
3568 err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
3569 if (err) {
3570 err = -EADDRNOTAVAIL;
3571 goto free_lists;
3572 }
3573
3574 status = ice_remove_mac(hw, &r_mac_list);
3575 if (status) {
3576 err = -EADDRNOTAVAIL;
3577 goto free_lists;
3578 }
3579
3580 err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
3581 if (err) {
3582 err = -EADDRNOTAVAIL;
3583 goto free_lists;
3584 }
3585
3586 status = ice_add_mac(hw, &a_mac_list);
3587 if (status) {
3588 err = -EADDRNOTAVAIL;
3589 goto free_lists;
3590 }
3591
3592free_lists:
3593 /* free list entries */
3594 ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
3595 ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
3596
3597 if (err) {
3598 netdev_err(netdev, "can't set mac %pM. filter update failed\n",
3599 mac);
3600 return err;
3601 }
3602
3603 /* change the netdev's mac address */
3604 memcpy(netdev->dev_addr, mac, netdev->addr_len);
3605 netdev_dbg(vsi->netdev, "updated mac address to %pM\n",
3606 netdev->dev_addr);
3607
3608 /* write new mac address to the firmware */
3609 flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
3610 status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
3611 if (status) {
3612 netdev_err(netdev, "can't set mac %pM. write to firmware failed.\n",
3613 mac);
3614 }
3615 return 0;
3616}
3617
3618/**
3619 * ice_set_rx_mode - NDO callback to set the netdev filters
3620 * @netdev: network interface device structure
3621 */
3622static void ice_set_rx_mode(struct net_device *netdev)
3623{
3624 struct ice_netdev_priv *np = netdev_priv(netdev);
3625 struct ice_vsi *vsi = np->vsi;
3626
3627 if (!vsi)
3628 return;
3629
3630 /* Set the flags to synchronize filters
3631 * ndo_set_rx_mode may be triggered even without a change in netdev
3632 * flags
3633 */
3634 set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
3635 set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
3636 set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
3637
3638 /* schedule our worker thread which will take care of
3639 * applying the new filter changes
3640 */
3641 ice_service_task_schedule(vsi->back);
3642}
3643
3644/**
3645 * ice_fdb_add - add an entry to the hardware database
3646 * @ndm: the input from the stack
3647 * @tb: pointer to array of nladdr (unused)
3648 * @dev: the net device pointer
3649 * @addr: the MAC address entry being added
3650 * @vid: VLAN id
3651 * @flags: instructions from stack about fdb operation
3652 */
3653static int ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
3654 struct net_device *dev, const unsigned char *addr,
3655 u16 vid, u16 flags)
3656{
3657 int err;
3658
3659 if (vid) {
3660 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
3661 return -EINVAL;
3662 }
3663 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
3664 netdev_err(dev, "FDB only supports static addresses\n");
3665 return -EINVAL;
3666 }
3667
3668 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
3669 err = dev_uc_add_excl(dev, addr);
3670 else if (is_multicast_ether_addr(addr))
3671 err = dev_mc_add_excl(dev, addr);
3672 else
3673 err = -EINVAL;
3674
3675 /* Only return duplicate errors if NLM_F_EXCL is set */
3676 if (err == -EEXIST && !(flags & NLM_F_EXCL))
3677 err = 0;
3678
3679 return err;
3680}
3681
3682/**
3683 * ice_fdb_del - delete an entry from the hardware database
3684 * @ndm: the input from the stack
3685 * @tb: pointer to array of nladdr (unused)
3686 * @dev: the net device pointer
3687 * @addr: the MAC address entry being added
3688 * @vid: VLAN id
3689 */
3690static int ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
3691 struct net_device *dev, const unsigned char *addr,
3692 __always_unused u16 vid)
3693{
3694 int err;
3695
3696 if (ndm->ndm_state & NUD_PERMANENT) {
3697 netdev_err(dev, "FDB only supports static addresses\n");
3698 return -EINVAL;
3699 }
3700
3701 if (is_unicast_ether_addr(addr))
3702 err = dev_uc_del(dev, addr);
3703 else if (is_multicast_ether_addr(addr))
3704 err = dev_mc_del(dev, addr);
3705 else
3706 err = -EINVAL;
3707
3708 return err;
3709}
3710
3711/**
3712 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
3713 * @vsi: the vsi being changed
3714 */
3715static int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
3716{
3717 struct device *dev = &vsi->back->pdev->dev;
3718 struct ice_hw *hw = &vsi->back->hw;
3719 struct ice_vsi_ctx ctxt = { 0 };
3720 enum ice_status status;
3721
3722 /* Here we are configuring the VSI to let the driver add VLAN tags by
3723 * setting port_vlan_flags to ICE_AQ_VSI_PVLAN_MODE_ALL. The actual VLAN
3724 * tag insertion happens in the Tx hot path, in ice_tx_map.
3725 */
3726 ctxt.info.port_vlan_flags = ICE_AQ_VSI_PVLAN_MODE_ALL;
3727
3728 ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
3729 ctxt.vsi_num = vsi->vsi_num;
3730
3731 status = ice_aq_update_vsi(hw, &ctxt, NULL);
3732 if (status) {
3733 dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
3734 status, hw->adminq.sq_last_status);
3735 return -EIO;
3736 }
3737
3738 vsi->info.port_vlan_flags = ctxt.info.port_vlan_flags;
3739 return 0;
3740}
3741
3742/**
3743 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
3744 * @vsi: the vsi being changed
3745 * @ena: boolean value indicating if this is a enable or disable request
3746 */
3747static int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
3748{
3749 struct device *dev = &vsi->back->pdev->dev;
3750 struct ice_hw *hw = &vsi->back->hw;
3751 struct ice_vsi_ctx ctxt = { 0 };
3752 enum ice_status status;
3753
3754 /* Here we are configuring what the VSI should do with the VLAN tag in
3755 * the Rx packet. We can either leave the tag in the packet or put it in
3756 * the Rx descriptor.
3757 */
3758 if (ena) {
3759 /* Strip VLAN tag from Rx packet and put it in the desc */
3760 ctxt.info.port_vlan_flags = ICE_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3761 } else {
3762 /* Disable stripping. Leave tag in packet */
3763 ctxt.info.port_vlan_flags = ICE_AQ_VSI_PVLAN_EMOD_NOTHING;
3764 }
3765
3766 ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
3767 ctxt.vsi_num = vsi->vsi_num;
3768
3769 status = ice_aq_update_vsi(hw, &ctxt, NULL);
3770 if (status) {
3771 dev_err(dev, "update VSI for VALN strip failed, ena = %d err %d aq_err %d\n",
3772 ena, status, hw->adminq.sq_last_status);
3773 return -EIO;
3774 }
3775
3776 vsi->info.port_vlan_flags = ctxt.info.port_vlan_flags;
3777 return 0;
3778}
3779
3780/**
3781 * ice_set_features - set the netdev feature flags
3782 * @netdev: ptr to the netdev being adjusted
3783 * @features: the feature set that the stack is suggesting
3784 */
3785static int ice_set_features(struct net_device *netdev,
3786 netdev_features_t features)
3787{
3788 struct ice_netdev_priv *np = netdev_priv(netdev);
3789 struct ice_vsi *vsi = np->vsi;
3790 int ret = 0;
3791
3792 if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
3793 !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
3794 ret = ice_vsi_manage_vlan_stripping(vsi, true);
3795 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
3796 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
3797 ret = ice_vsi_manage_vlan_stripping(vsi, false);
3798 else if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
3799 !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
3800 ret = ice_vsi_manage_vlan_insertion(vsi);
3801 else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
3802 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
3803 ret = ice_vsi_manage_vlan_insertion(vsi);
3804
3805 return ret;
3806}
3807
3808/**
3809 * ice_vsi_vlan_setup - Setup vlan offload properties on a VSI
3810 * @vsi: VSI to setup vlan properties for
3811 */
3812static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
3813{
3814 int ret = 0;
3815
3816 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3817 ret = ice_vsi_manage_vlan_stripping(vsi, true);
3818 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
3819 ret = ice_vsi_manage_vlan_insertion(vsi);
3820
3821 return ret;
3822}
3823
3824/**
3825 * ice_restore_vlan - Reinstate VLANs when vsi/netdev comes back up
3826 * @vsi: the VSI being brought back up
3827 */
3828static int ice_restore_vlan(struct ice_vsi *vsi)
3829{
3830 int err;
3831 u16 vid;
3832
3833 if (!vsi->netdev)
3834 return -EINVAL;
3835
3836 err = ice_vsi_vlan_setup(vsi);
3837 if (err)
3838 return err;
3839
3840 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID) {
3841 err = ice_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q), vid);
3842 if (err)
3843 break;
3844 }
3845
3846 return err;
3847}
3848
3849/**
3850 * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
3851 * @ring: The Tx ring to configure
3852 * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
3853 * @pf_q: queue index in the PF space
3854 *
3855 * Configure the Tx descriptor ring in TLAN context.
3856 */
3857static void
3858ice_setup_tx_ctx(struct ice_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
3859{
3860 struct ice_vsi *vsi = ring->vsi;
3861 struct ice_hw *hw = &vsi->back->hw;
3862
3863 tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
3864
3865 tlan_ctx->port_num = vsi->port_info->lport;
3866
3867 /* Transmit Queue Length */
3868 tlan_ctx->qlen = ring->count;
3869
3870 /* PF number */
3871 tlan_ctx->pf_num = hw->pf_id;
3872
3873 /* queue belongs to a specific VSI type
3874 * VF / VM index should be programmed per vmvf_type setting:
3875 * for vmvf_type = VF, it is VF number between 0-256
3876 * for vmvf_type = VM, it is VM number between 0-767
3877 * for PF or EMP this field should be set to zero
3878 */
3879 switch (vsi->type) {
3880 case ICE_VSI_PF:
3881 tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
3882 break;
3883 default:
3884 return;
3885 }
3886
3887 /* make sure the context is associated with the right VSI */
3888 tlan_ctx->src_vsi = vsi->vsi_num;
3889
3890 tlan_ctx->tso_ena = ICE_TX_LEGACY;
3891 tlan_ctx->tso_qnum = pf_q;
3892
3893 /* Legacy or Advanced Host Interface:
3894 * 0: Advanced Host Interface
3895 * 1: Legacy Host Interface
3896 */
3897 tlan_ctx->legacy_int = ICE_TX_LEGACY;
3898}
3899
3900/**
3901 * ice_vsi_cfg_txqs - Configure the VSI for Tx
3902 * @vsi: the VSI being configured
3903 *
3904 * Return 0 on success and a negative value on error
3905 * Configure the Tx VSI for operation.
3906 */
3907static int ice_vsi_cfg_txqs(struct ice_vsi *vsi)
3908{
3909 struct ice_aqc_add_tx_qgrp *qg_buf;
3910 struct ice_aqc_add_txqs_perq *txq;
3911 struct ice_pf *pf = vsi->back;
3912 enum ice_status status;
3913 u16 buf_len, i, pf_q;
3914 int err = 0, tc = 0;
3915 u8 num_q_grps;
3916
3917 buf_len = sizeof(struct ice_aqc_add_tx_qgrp);
3918 qg_buf = devm_kzalloc(&pf->pdev->dev, buf_len, GFP_KERNEL);
3919 if (!qg_buf)
3920 return -ENOMEM;
3921
3922 if (vsi->num_txq > ICE_MAX_TXQ_PER_TXQG) {
3923 err = -EINVAL;
3924 goto err_cfg_txqs;
3925 }
3926 qg_buf->num_txqs = 1;
3927 num_q_grps = 1;
3928
3929 /* set up and configure the tx queues */
3930 ice_for_each_txq(vsi, i) {
3931 struct ice_tlan_ctx tlan_ctx = { 0 };
3932
3933 pf_q = vsi->txq_map[i];
3934 ice_setup_tx_ctx(vsi->tx_rings[i], &tlan_ctx, pf_q);
3935 /* copy context contents into the qg_buf */
3936 qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
3937 ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
3938 ice_tlan_ctx_info);
3939
3940 /* init queue specific tail reg. It is referred as transmit
3941 * comm scheduler queue doorbell.
3942 */
3943 vsi->tx_rings[i]->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
3944 status = ice_ena_vsi_txq(vsi->port_info, vsi->vsi_num, tc,
3945 num_q_grps, qg_buf, buf_len, NULL);
3946 if (status) {
3947 dev_err(&vsi->back->pdev->dev,
3948 "Failed to set LAN Tx queue context, error: %d\n",
3949 status);
3950 err = -ENODEV;
3951 goto err_cfg_txqs;
3952 }
3953
3954 /* Add Tx Queue TEID into the VSI tx ring from the response
3955 * This will complete configuring and enabling the queue.
3956 */
3957 txq = &qg_buf->txqs[0];
3958 if (pf_q == le16_to_cpu(txq->txq_id))
3959 vsi->tx_rings[i]->txq_teid =
3960 le32_to_cpu(txq->q_teid);
3961 }
3962err_cfg_txqs:
3963 devm_kfree(&pf->pdev->dev, qg_buf);
3964 return err;
3965}
3966
3967/**
3968 * ice_setup_rx_ctx - Configure a receive ring context
3969 * @ring: The Rx ring to configure
3970 *
3971 * Configure the Rx descriptor ring in RLAN context.
3972 */
3973static int ice_setup_rx_ctx(struct ice_ring *ring)
3974{
3975 struct ice_vsi *vsi = ring->vsi;
3976 struct ice_hw *hw = &vsi->back->hw;
3977 u32 rxdid = ICE_RXDID_FLEX_NIC;
3978 struct ice_rlan_ctx rlan_ctx;
3979 u32 regval;
3980 u16 pf_q;
3981 int err;
3982
3983 /* what is RX queue number in global space of 2K rx queues */
3984 pf_q = vsi->rxq_map[ring->q_index];
3985
3986 /* clear the context structure first */
3987 memset(&rlan_ctx, 0, sizeof(rlan_ctx));
3988
3989 rlan_ctx.base = ring->dma >> 7;
3990
3991 rlan_ctx.qlen = ring->count;
3992
3993 /* Receive Packet Data Buffer Size.
3994 * The Packet Data Buffer Size is defined in 128 byte units.
3995 */
3996 rlan_ctx.dbuf = vsi->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
3997
3998 /* use 32 byte descriptors */
3999 rlan_ctx.dsize = 1;
4000
4001 /* Strip the Ethernet CRC bytes before the packet is posted to host
4002 * memory.
4003 */
4004 rlan_ctx.crcstrip = 1;
4005
4006 /* L2TSEL flag defines the reported L2 Tags in the receive descriptor */
4007 rlan_ctx.l2tsel = 1;
4008
4009 rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
4010 rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
4011 rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
4012
4013 /* This controls whether VLAN is stripped from inner headers
4014 * The VLAN in the inner L2 header is stripped to the receive
4015 * descriptor if enabled by this flag.
4016 */
4017 rlan_ctx.showiv = 0;
4018
4019 /* Max packet size for this queue - must not be set to a larger value
4020 * than 5 x DBUF
4021 */
4022 rlan_ctx.rxmax = min_t(u16, vsi->max_frame,
4023 ICE_MAX_CHAINED_RX_BUFS * vsi->rx_buf_len);
4024
4025 /* Rx queue threshold in units of 64 */
4026 rlan_ctx.lrxqthresh = 1;
4027
4028 /* Enable Flexible Descriptors in the queue context which
4029 * allows this driver to select a specific receive descriptor format
4030 */
4031 regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
4032 regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
4033 QRXFLXP_CNTXT_RXDID_IDX_M;
4034
4035 /* increasing context priority to pick up profile id;
4036 * default is 0x01; setting to 0x03 to ensure profile
4037 * is programming if prev context is of same priority
4038 */
4039 regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
4040 QRXFLXP_CNTXT_RXDID_PRIO_M;
4041
4042 wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
4043
4044 /* Absolute queue number out of 2K needs to be passed */
4045 err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
4046 if (err) {
4047 dev_err(&vsi->back->pdev->dev,
4048 "Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
4049 pf_q, err);
4050 return -EIO;
4051 }
4052
4053 /* init queue specific tail register */
4054 ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
4055 writel(0, ring->tail);
4056 ice_alloc_rx_bufs(ring, ICE_DESC_UNUSED(ring));
4057
4058 return 0;
4059}
4060
4061/**
4062 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
4063 * @vsi: the VSI being configured
4064 *
4065 * Return 0 on success and a negative value on error
4066 * Configure the Rx VSI for operation.
4067 */
4068static int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
4069{
4070 int err = 0;
4071 u16 i;
4072
4073 if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
4074 vsi->max_frame = vsi->netdev->mtu +
4075 ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4076 else
4077 vsi->max_frame = ICE_RXBUF_2048;
4078
4079 vsi->rx_buf_len = ICE_RXBUF_2048;
4080 /* set up individual rings */
4081 for (i = 0; i < vsi->num_rxq && !err; i++)
4082 err = ice_setup_rx_ctx(vsi->rx_rings[i]);
4083
4084 if (err) {
4085 dev_err(&vsi->back->pdev->dev, "ice_setup_rx_ctx failed\n");
4086 return -EIO;
4087 }
4088 return err;
4089}
4090
4091/**
4092 * ice_vsi_cfg - Setup the VSI
4093 * @vsi: the VSI being configured
4094 *
4095 * Return 0 on success and negative value on error
4096 */
4097static int ice_vsi_cfg(struct ice_vsi *vsi)
4098{
4099 int err;
4100
4101 ice_set_rx_mode(vsi->netdev);
4102
4103 err = ice_restore_vlan(vsi);
4104 if (err)
4105 return err;
4106
4107 err = ice_vsi_cfg_txqs(vsi);
4108 if (!err)
4109 err = ice_vsi_cfg_rxqs(vsi);
4110
4111 return err;
4112}
4113
4114/**
4115 * ice_vsi_stop_tx_rings - Disable Tx rings
4116 * @vsi: the VSI being configured
4117 */
4118static int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
4119{
4120 struct ice_pf *pf = vsi->back;
4121 struct ice_hw *hw = &pf->hw;
4122 enum ice_status status;
4123 u32 *q_teids, val;
4124 u16 *q_ids, i;
4125 int err = 0;
4126
4127 if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
4128 return -EINVAL;
4129
4130 q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
4131 GFP_KERNEL);
4132 if (!q_teids)
4133 return -ENOMEM;
4134
4135 q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
4136 GFP_KERNEL);
4137 if (!q_ids) {
4138 err = -ENOMEM;
4139 goto err_alloc_q_ids;
4140 }
4141
4142 /* set up the tx queue list to be disabled */
4143 ice_for_each_txq(vsi, i) {
4144 u16 v_idx;
4145
4146 if (!vsi->tx_rings || !vsi->tx_rings[i]) {
4147 err = -EINVAL;
4148 goto err_out;
4149 }
4150
4151 q_ids[i] = vsi->txq_map[i];
4152 q_teids[i] = vsi->tx_rings[i]->txq_teid;
4153
4154 /* clear cause_ena bit for disabled queues */
4155 val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4156 val &= ~QINT_TQCTL_CAUSE_ENA_M;
4157 wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4158
4159 /* software is expected to wait for 100 ns */
4160 ndelay(100);
4161
4162 /* trigger a software interrupt for the vector associated to
4163 * the queue to schedule napi handler
4164 */
4165 v_idx = vsi->tx_rings[i]->q_vector->v_idx;
4166 wr32(hw, GLINT_DYN_CTL(vsi->base_vector + v_idx),
4167 GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
4168 }
4169 status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
4170 NULL);
4171 if (status) {
4172 dev_err(&pf->pdev->dev,
4173 "Failed to disable LAN Tx queues, error: %d\n",
4174 status);
4175 err = -ENODEV;
4176 }
4177
4178err_out:
4179 devm_kfree(&pf->pdev->dev, q_ids);
4180
4181err_alloc_q_ids:
4182 devm_kfree(&pf->pdev->dev, q_teids);
4183
4184 return err;
4185}
4186
4187/**
4188 * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4189 * @pf: the PF being configured
4190 * @pf_q: the PF queue
4191 * @ena: enable or disable state of the queue
4192 *
4193 * This routine will wait for the given Rx queue of the PF to reach the
4194 * enabled or disabled state.
4195 * Returns -ETIMEDOUT in case of failing to reach the requested state after
4196 * multiple retries; else will return 0 in case of success.
4197 */
4198static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
4199{
4200 int i;
4201
4202 for (i = 0; i < ICE_Q_WAIT_RETRY_LIMIT; i++) {
4203 u32 rx_reg = rd32(&pf->hw, QRX_CTRL(pf_q));
4204
4205 if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
4206 break;
4207
4208 usleep_range(10, 20);
4209 }
4210 if (i >= ICE_Q_WAIT_RETRY_LIMIT)
4211 return -ETIMEDOUT;
4212
4213 return 0;
4214}
4215
4216/**
4217 * ice_vsi_ctrl_rx_rings - Start or stop a VSI's rx rings
4218 * @vsi: the VSI being configured
4219 * @ena: start or stop the rx rings
4220 */
4221static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
4222{
4223 struct ice_pf *pf = vsi->back;
4224 struct ice_hw *hw = &pf->hw;
4225 int i, j, ret = 0;
4226
4227 for (i = 0; i < vsi->num_rxq; i++) {
4228 int pf_q = vsi->rxq_map[i];
4229 u32 rx_reg;
4230
4231 for (j = 0; j < ICE_Q_WAIT_MAX_RETRY; j++) {
4232 rx_reg = rd32(hw, QRX_CTRL(pf_q));
4233 if (((rx_reg >> QRX_CTRL_QENA_REQ_S) & 1) ==
4234 ((rx_reg >> QRX_CTRL_QENA_STAT_S) & 1))
4235 break;
4236 usleep_range(1000, 2000);
4237 }
4238
4239 /* Skip if the queue is already in the requested state */
4240 if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
4241 continue;
4242
4243 /* turn on/off the queue */
4244 if (ena)
4245 rx_reg |= QRX_CTRL_QENA_REQ_M;
4246 else
4247 rx_reg &= ~QRX_CTRL_QENA_REQ_M;
4248 wr32(hw, QRX_CTRL(pf_q), rx_reg);
4249
4250 /* wait for the change to finish */
4251 ret = ice_pf_rxq_wait(pf, pf_q, ena);
4252 if (ret) {
4253 dev_err(&pf->pdev->dev,
4254 "VSI idx %d Rx ring %d %sable timeout\n",
4255 vsi->idx, pf_q, (ena ? "en" : "dis"));
4256 break;
4257 }
4258 }
4259
4260 return ret;
4261}
4262
4263/**
4264 * ice_vsi_start_rx_rings - start VSI's rx rings
4265 * @vsi: the VSI whose rings are to be started
4266 *
4267 * Returns 0 on success and a negative value on error
4268 */
4269static int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
4270{
4271 return ice_vsi_ctrl_rx_rings(vsi, true);
4272}
4273
4274/**
4275 * ice_vsi_stop_rx_rings - stop VSI's rx rings
4276 * @vsi: the VSI
4277 *
4278 * Returns 0 on success and a negative value on error
4279 */
4280static int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
4281{
4282 return ice_vsi_ctrl_rx_rings(vsi, false);
4283}
4284
4285/**
4286 * ice_vsi_stop_tx_rx_rings - stop VSI's tx and rx rings
4287 * @vsi: the VSI
4288 * Returns 0 on success and a negative value on error
4289 */
4290static int ice_vsi_stop_tx_rx_rings(struct ice_vsi *vsi)
4291{
4292 int err_tx, err_rx;
4293
4294 err_tx = ice_vsi_stop_tx_rings(vsi);
4295 if (err_tx)
4296 dev_dbg(&vsi->back->pdev->dev, "Failed to disable Tx rings\n");
4297
4298 err_rx = ice_vsi_stop_rx_rings(vsi);
4299 if (err_rx)
4300 dev_dbg(&vsi->back->pdev->dev, "Failed to disable Rx rings\n");
4301
4302 if (err_tx || err_rx)
4303 return -EIO;
4304
4305 return 0;
4306}
4307
4308/**
4309 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4310 * @vsi: the VSI being configured
4311 */
4312static void ice_napi_enable_all(struct ice_vsi *vsi)
4313{
4314 int q_idx;
4315
4316 if (!vsi->netdev)
4317 return;
4318
4319 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
4320 napi_enable(&vsi->q_vectors[q_idx]->napi);
4321}
4322
4323/**
4324 * ice_up_complete - Finish the last steps of bringing up a connection
4325 * @vsi: The VSI being configured
4326 *
4327 * Return 0 on success and negative value on error
4328 */
4329static int ice_up_complete(struct ice_vsi *vsi)
4330{
4331 struct ice_pf *pf = vsi->back;
4332 int err;
4333
4334 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4335 ice_vsi_cfg_msix(vsi);
4336 else
4337 return -ENOTSUPP;
4338
4339 /* Enable only Rx rings, Tx rings were enabled by the FW when the
4340 * Tx queue group list was configured and the context bits were
4341 * programmed using ice_vsi_cfg_txqs
4342 */
4343 err = ice_vsi_start_rx_rings(vsi);
4344 if (err)
4345 return err;
4346
4347 clear_bit(__ICE_DOWN, vsi->state);
4348 ice_napi_enable_all(vsi);
4349 ice_vsi_ena_irq(vsi);
4350
4351 if (vsi->port_info &&
4352 (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
4353 vsi->netdev) {
4354 ice_print_link_msg(vsi, true);
4355 netif_tx_start_all_queues(vsi->netdev);
4356 netif_carrier_on(vsi->netdev);
4357 }
4358
4359 ice_service_task_schedule(pf);
4360
4361 return err;
4362}
4363
4364/**
4365 * ice_up - Bring the connection back up after being down
4366 * @vsi: VSI being configured
4367 */
4368int ice_up(struct ice_vsi *vsi)
4369{
4370 int err;
4371
4372 err = ice_vsi_cfg(vsi);
4373 if (!err)
4374 err = ice_up_complete(vsi);
4375
4376 return err;
4377}
4378
4379/**
4380 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
4381 * @ring: Tx or Rx ring to read stats from
4382 * @pkts: packets stats counter
4383 * @bytes: bytes stats counter
4384 *
4385 * This function fetches stats from the ring considering the atomic operations
4386 * that needs to be performed to read u64 values in 32 bit machine.
4387 */
4388static void ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts,
4389 u64 *bytes)
4390{
4391 unsigned int start;
4392 *pkts = 0;
4393 *bytes = 0;
4394
4395 if (!ring)
4396 return;
4397 do {
4398 start = u64_stats_fetch_begin_irq(&ring->syncp);
4399 *pkts = ring->stats.pkts;
4400 *bytes = ring->stats.bytes;
4401 } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
4402}
4403
4404/**
4405 * ice_stat_update40 - read 40 bit stat from the chip and update stat values
4406 * @hw: ptr to the hardware info
4407 * @hireg: high 32 bit HW register to read from
4408 * @loreg: low 32 bit HW register to read from
4409 * @prev_stat_loaded: bool to specify if previous stats are loaded
4410 * @prev_stat: ptr to previous loaded stat value
4411 * @cur_stat: ptr to current stat value
4412 */
4413static void ice_stat_update40(struct ice_hw *hw, u32 hireg, u32 loreg,
4414 bool prev_stat_loaded, u64 *prev_stat,
4415 u64 *cur_stat)
4416{
4417 u64 new_data;
4418
4419 new_data = rd32(hw, loreg);
4420 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
4421
4422 /* device stats are not reset at PFR, they likely will not be zeroed
4423 * when the driver starts. So save the first values read and use them as
4424 * offsets to be subtracted from the raw values in order to report stats
4425 * that count from zero.
4426 */
4427 if (!prev_stat_loaded)
4428 *prev_stat = new_data;
4429 if (likely(new_data >= *prev_stat))
4430 *cur_stat = new_data - *prev_stat;
4431 else
4432 /* to manage the potential roll-over */
4433 *cur_stat = (new_data + BIT_ULL(40)) - *prev_stat;
4434 *cur_stat &= 0xFFFFFFFFFFULL;
4435}
4436
4437/**
4438 * ice_stat_update32 - read 32 bit stat from the chip and update stat values
4439 * @hw: ptr to the hardware info
4440 * @reg: HW register to read from
4441 * @prev_stat_loaded: bool to specify if previous stats are loaded
4442 * @prev_stat: ptr to previous loaded stat value
4443 * @cur_stat: ptr to current stat value
4444 */
4445static void ice_stat_update32(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
4446 u64 *prev_stat, u64 *cur_stat)
4447{
4448 u32 new_data;
4449
4450 new_data = rd32(hw, reg);
4451
4452 /* device stats are not reset at PFR, they likely will not be zeroed
4453 * when the driver starts. So save the first values read and use them as
4454 * offsets to be subtracted from the raw values in order to report stats
4455 * that count from zero.
4456 */
4457 if (!prev_stat_loaded)
4458 *prev_stat = new_data;
4459 if (likely(new_data >= *prev_stat))
4460 *cur_stat = new_data - *prev_stat;
4461 else
4462 /* to manage the potential roll-over */
4463 *cur_stat = (new_data + BIT_ULL(32)) - *prev_stat;
4464}
4465
4466/**
4467 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
4468 * @vsi: the VSI to be updated
4469 */
4470static void ice_update_eth_stats(struct ice_vsi *vsi)
4471{
4472 struct ice_eth_stats *prev_es, *cur_es;
4473 struct ice_hw *hw = &vsi->back->hw;
4474 u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
4475
4476 prev_es = &vsi->eth_stats_prev;
4477 cur_es = &vsi->eth_stats;
4478
4479 ice_stat_update40(hw, GLV_GORCH(vsi_num), GLV_GORCL(vsi_num),
4480 vsi->stat_offsets_loaded, &prev_es->rx_bytes,
4481 &cur_es->rx_bytes);
4482
4483 ice_stat_update40(hw, GLV_UPRCH(vsi_num), GLV_UPRCL(vsi_num),
4484 vsi->stat_offsets_loaded, &prev_es->rx_unicast,
4485 &cur_es->rx_unicast);
4486
4487 ice_stat_update40(hw, GLV_MPRCH(vsi_num), GLV_MPRCL(vsi_num),
4488 vsi->stat_offsets_loaded, &prev_es->rx_multicast,
4489 &cur_es->rx_multicast);
4490
4491 ice_stat_update40(hw, GLV_BPRCH(vsi_num), GLV_BPRCL(vsi_num),
4492 vsi->stat_offsets_loaded, &prev_es->rx_broadcast,
4493 &cur_es->rx_broadcast);
4494
4495 ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
4496 &prev_es->rx_discards, &cur_es->rx_discards);
4497
4498 ice_stat_update40(hw, GLV_GOTCH(vsi_num), GLV_GOTCL(vsi_num),
4499 vsi->stat_offsets_loaded, &prev_es->tx_bytes,
4500 &cur_es->tx_bytes);
4501
4502 ice_stat_update40(hw, GLV_UPTCH(vsi_num), GLV_UPTCL(vsi_num),
4503 vsi->stat_offsets_loaded, &prev_es->tx_unicast,
4504 &cur_es->tx_unicast);
4505
4506 ice_stat_update40(hw, GLV_MPTCH(vsi_num), GLV_MPTCL(vsi_num),
4507 vsi->stat_offsets_loaded, &prev_es->tx_multicast,
4508 &cur_es->tx_multicast);
4509
4510 ice_stat_update40(hw, GLV_BPTCH(vsi_num), GLV_BPTCL(vsi_num),
4511 vsi->stat_offsets_loaded, &prev_es->tx_broadcast,
4512 &cur_es->tx_broadcast);
4513
4514 ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
4515 &prev_es->tx_errors, &cur_es->tx_errors);
4516
4517 vsi->stat_offsets_loaded = true;
4518}
4519
4520/**
4521 * ice_update_vsi_ring_stats - Update VSI stats counters
4522 * @vsi: the VSI to be updated
4523 */
4524static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
4525{
4526 struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
4527 struct ice_ring *ring;
4528 u64 pkts, bytes;
4529 int i;
4530
4531 /* reset netdev stats */
4532 vsi_stats->tx_packets = 0;
4533 vsi_stats->tx_bytes = 0;
4534 vsi_stats->rx_packets = 0;
4535 vsi_stats->rx_bytes = 0;
4536
4537 /* reset non-netdev (extended) stats */
4538 vsi->tx_restart = 0;
4539 vsi->tx_busy = 0;
4540 vsi->tx_linearize = 0;
4541 vsi->rx_buf_failed = 0;
4542 vsi->rx_page_failed = 0;
4543
4544 rcu_read_lock();
4545
4546 /* update Tx rings counters */
4547 ice_for_each_txq(vsi, i) {
4548 ring = READ_ONCE(vsi->tx_rings[i]);
4549 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
4550 vsi_stats->tx_packets += pkts;
4551 vsi_stats->tx_bytes += bytes;
4552 vsi->tx_restart += ring->tx_stats.restart_q;
4553 vsi->tx_busy += ring->tx_stats.tx_busy;
4554 vsi->tx_linearize += ring->tx_stats.tx_linearize;
4555 }
4556
4557 /* update Rx rings counters */
4558 ice_for_each_rxq(vsi, i) {
4559 ring = READ_ONCE(vsi->rx_rings[i]);
4560 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
4561 vsi_stats->rx_packets += pkts;
4562 vsi_stats->rx_bytes += bytes;
4563 vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
4564 vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
4565 }
4566
4567 rcu_read_unlock();
4568}
4569
4570/**
4571 * ice_update_vsi_stats - Update VSI stats counters
4572 * @vsi: the VSI to be updated
4573 */
4574static void ice_update_vsi_stats(struct ice_vsi *vsi)
4575{
4576 struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
4577 struct ice_eth_stats *cur_es = &vsi->eth_stats;
4578 struct ice_pf *pf = vsi->back;
4579
4580 if (test_bit(__ICE_DOWN, vsi->state) ||
4581 test_bit(__ICE_CFG_BUSY, pf->state))
4582 return;
4583
4584 /* get stats as recorded by Tx/Rx rings */
4585 ice_update_vsi_ring_stats(vsi);
4586
4587 /* get VSI stats as recorded by the hardware */
4588 ice_update_eth_stats(vsi);
4589
4590 cur_ns->tx_errors = cur_es->tx_errors;
4591 cur_ns->rx_dropped = cur_es->rx_discards;
4592 cur_ns->tx_dropped = cur_es->tx_discards;
4593 cur_ns->multicast = cur_es->rx_multicast;
4594
4595 /* update some more netdev stats if this is main VSI */
4596 if (vsi->type == ICE_VSI_PF) {
4597 cur_ns->rx_crc_errors = pf->stats.crc_errors;
4598 cur_ns->rx_errors = pf->stats.crc_errors +
4599 pf->stats.illegal_bytes;
4600 cur_ns->rx_length_errors = pf->stats.rx_len_errors;
4601 }
4602}
4603
4604/**
4605 * ice_update_pf_stats - Update PF port stats counters
4606 * @pf: PF whose stats needs to be updated
4607 */
4608static void ice_update_pf_stats(struct ice_pf *pf)
4609{
4610 struct ice_hw_port_stats *prev_ps, *cur_ps;
4611 struct ice_hw *hw = &pf->hw;
4612 u8 pf_id;
4613
4614 prev_ps = &pf->stats_prev;
4615 cur_ps = &pf->stats;
4616 pf_id = hw->pf_id;
4617
4618 ice_stat_update40(hw, GLPRT_GORCH(pf_id), GLPRT_GORCL(pf_id),
4619 pf->stat_prev_loaded, &prev_ps->eth.rx_bytes,
4620 &cur_ps->eth.rx_bytes);
4621
4622 ice_stat_update40(hw, GLPRT_UPRCH(pf_id), GLPRT_UPRCL(pf_id),
4623 pf->stat_prev_loaded, &prev_ps->eth.rx_unicast,
4624 &cur_ps->eth.rx_unicast);
4625
4626 ice_stat_update40(hw, GLPRT_MPRCH(pf_id), GLPRT_MPRCL(pf_id),
4627 pf->stat_prev_loaded, &prev_ps->eth.rx_multicast,
4628 &cur_ps->eth.rx_multicast);
4629
4630 ice_stat_update40(hw, GLPRT_BPRCH(pf_id), GLPRT_BPRCL(pf_id),
4631 pf->stat_prev_loaded, &prev_ps->eth.rx_broadcast,
4632 &cur_ps->eth.rx_broadcast);
4633
4634 ice_stat_update40(hw, GLPRT_GOTCH(pf_id), GLPRT_GOTCL(pf_id),
4635 pf->stat_prev_loaded, &prev_ps->eth.tx_bytes,
4636 &cur_ps->eth.tx_bytes);
4637
4638 ice_stat_update40(hw, GLPRT_UPTCH(pf_id), GLPRT_UPTCL(pf_id),
4639 pf->stat_prev_loaded, &prev_ps->eth.tx_unicast,
4640 &cur_ps->eth.tx_unicast);
4641
4642 ice_stat_update40(hw, GLPRT_MPTCH(pf_id), GLPRT_MPTCL(pf_id),
4643 pf->stat_prev_loaded, &prev_ps->eth.tx_multicast,
4644 &cur_ps->eth.tx_multicast);
4645
4646 ice_stat_update40(hw, GLPRT_BPTCH(pf_id), GLPRT_BPTCL(pf_id),
4647 pf->stat_prev_loaded, &prev_ps->eth.tx_broadcast,
4648 &cur_ps->eth.tx_broadcast);
4649
4650 ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
4651 &prev_ps->tx_dropped_link_down,
4652 &cur_ps->tx_dropped_link_down);
4653
4654 ice_stat_update40(hw, GLPRT_PRC64H(pf_id), GLPRT_PRC64L(pf_id),
4655 pf->stat_prev_loaded, &prev_ps->rx_size_64,
4656 &cur_ps->rx_size_64);
4657
4658 ice_stat_update40(hw, GLPRT_PRC127H(pf_id), GLPRT_PRC127L(pf_id),
4659 pf->stat_prev_loaded, &prev_ps->rx_size_127,
4660 &cur_ps->rx_size_127);
4661
4662 ice_stat_update40(hw, GLPRT_PRC255H(pf_id), GLPRT_PRC255L(pf_id),
4663 pf->stat_prev_loaded, &prev_ps->rx_size_255,
4664 &cur_ps->rx_size_255);
4665
4666 ice_stat_update40(hw, GLPRT_PRC511H(pf_id), GLPRT_PRC511L(pf_id),
4667 pf->stat_prev_loaded, &prev_ps->rx_size_511,
4668 &cur_ps->rx_size_511);
4669
4670 ice_stat_update40(hw, GLPRT_PRC1023H(pf_id),
4671 GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
4672 &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
4673
4674 ice_stat_update40(hw, GLPRT_PRC1522H(pf_id),
4675 GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
4676 &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
4677
4678 ice_stat_update40(hw, GLPRT_PRC9522H(pf_id),
4679 GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
4680 &prev_ps->rx_size_big, &cur_ps->rx_size_big);
4681
4682 ice_stat_update40(hw, GLPRT_PTC64H(pf_id), GLPRT_PTC64L(pf_id),
4683 pf->stat_prev_loaded, &prev_ps->tx_size_64,
4684 &cur_ps->tx_size_64);
4685
4686 ice_stat_update40(hw, GLPRT_PTC127H(pf_id), GLPRT_PTC127L(pf_id),
4687 pf->stat_prev_loaded, &prev_ps->tx_size_127,
4688 &cur_ps->tx_size_127);
4689
4690 ice_stat_update40(hw, GLPRT_PTC255H(pf_id), GLPRT_PTC255L(pf_id),
4691 pf->stat_prev_loaded, &prev_ps->tx_size_255,
4692 &cur_ps->tx_size_255);
4693
4694 ice_stat_update40(hw, GLPRT_PTC511H(pf_id), GLPRT_PTC511L(pf_id),
4695 pf->stat_prev_loaded, &prev_ps->tx_size_511,
4696 &cur_ps->tx_size_511);
4697
4698 ice_stat_update40(hw, GLPRT_PTC1023H(pf_id),
4699 GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
4700 &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
4701
4702 ice_stat_update40(hw, GLPRT_PTC1522H(pf_id),
4703 GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
4704 &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
4705
4706 ice_stat_update40(hw, GLPRT_PTC9522H(pf_id),
4707 GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
4708 &prev_ps->tx_size_big, &cur_ps->tx_size_big);
4709
4710 ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
4711 &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
4712
4713 ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
4714 &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
4715
4716 ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
4717 &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
4718
4719 ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
4720 &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
4721
4722 ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
4723 &prev_ps->crc_errors, &cur_ps->crc_errors);
4724
4725 ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
4726 &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
4727
4728 ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
4729 &prev_ps->mac_local_faults,
4730 &cur_ps->mac_local_faults);
4731
4732 ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
4733 &prev_ps->mac_remote_faults,
4734 &cur_ps->mac_remote_faults);
4735
4736 ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
4737 &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
4738
4739 ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
4740 &prev_ps->rx_undersize, &cur_ps->rx_undersize);
4741
4742 ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
4743 &prev_ps->rx_fragments, &cur_ps->rx_fragments);
4744
4745 ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
4746 &prev_ps->rx_oversize, &cur_ps->rx_oversize);
4747
4748 ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
4749 &prev_ps->rx_jabber, &cur_ps->rx_jabber);
4750
4751 pf->stat_prev_loaded = true;
4752}
4753
4754/**
4755 * ice_get_stats64 - get statistics for network device structure
4756 * @netdev: network interface device structure
4757 * @stats: main device statistics structure
4758 */
4759static
4760void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
4761{
4762 struct ice_netdev_priv *np = netdev_priv(netdev);
4763 struct rtnl_link_stats64 *vsi_stats;
4764 struct ice_vsi *vsi = np->vsi;
4765
4766 vsi_stats = &vsi->net_stats;
4767
4768 if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
4769 return;
4770 /* netdev packet/byte stats come from ring counter. These are obtained
4771 * by summing up ring counters (done by ice_update_vsi_ring_stats).
4772 */
4773 ice_update_vsi_ring_stats(vsi);
4774 stats->tx_packets = vsi_stats->tx_packets;
4775 stats->tx_bytes = vsi_stats->tx_bytes;
4776 stats->rx_packets = vsi_stats->rx_packets;
4777 stats->rx_bytes = vsi_stats->rx_bytes;
4778
4779 /* The rest of the stats can be read from the hardware but instead we
4780 * just return values that the watchdog task has already obtained from
4781 * the hardware.
4782 */
4783 stats->multicast = vsi_stats->multicast;
4784 stats->tx_errors = vsi_stats->tx_errors;
4785 stats->tx_dropped = vsi_stats->tx_dropped;
4786 stats->rx_errors = vsi_stats->rx_errors;
4787 stats->rx_dropped = vsi_stats->rx_dropped;
4788 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
4789 stats->rx_length_errors = vsi_stats->rx_length_errors;
4790}
4791
4792#ifdef CONFIG_NET_POLL_CONTROLLER
4793/**
4794 * ice_netpoll - polling "interrupt" handler
4795 * @netdev: network interface device structure
4796 *
4797 * Used by netconsole to send skbs without having to re-enable interrupts.
4798 * This is not called in the normal interrupt path.
4799 */
4800static void ice_netpoll(struct net_device *netdev)
4801{
4802 struct ice_netdev_priv *np = netdev_priv(netdev);
4803 struct ice_vsi *vsi = np->vsi;
4804 struct ice_pf *pf = vsi->back;
4805 int i;
4806
4807 if (test_bit(__ICE_DOWN, vsi->state) ||
4808 !test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4809 return;
4810
4811 for (i = 0; i < vsi->num_q_vectors; i++)
4812 ice_msix_clean_rings(0, vsi->q_vectors[i]);
4813}
4814#endif /* CONFIG_NET_POLL_CONTROLLER */
4815
4816/**
4817 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4818 * @vsi: VSI having NAPI disabled
4819 */
4820static void ice_napi_disable_all(struct ice_vsi *vsi)
4821{
4822 int q_idx;
4823
4824 if (!vsi->netdev)
4825 return;
4826
4827 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
4828 napi_disable(&vsi->q_vectors[q_idx]->napi);
4829}
4830
4831/**
4832 * ice_down - Shutdown the connection
4833 * @vsi: The VSI being stopped
4834 */
4835int ice_down(struct ice_vsi *vsi)
4836{
4837 int i, err;
4838
4839 /* Caller of this function is expected to set the
4840 * vsi->state __ICE_DOWN bit
4841 */
4842 if (vsi->netdev) {
4843 netif_carrier_off(vsi->netdev);
4844 netif_tx_disable(vsi->netdev);
4845 }
4846
4847 ice_vsi_dis_irq(vsi);
4848 err = ice_vsi_stop_tx_rx_rings(vsi);
4849 ice_napi_disable_all(vsi);
4850
4851 ice_for_each_txq(vsi, i)
4852 ice_clean_tx_ring(vsi->tx_rings[i]);
4853
4854 ice_for_each_rxq(vsi, i)
4855 ice_clean_rx_ring(vsi->rx_rings[i]);
4856
4857 if (err)
4858 netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
4859 vsi->vsi_num, vsi->vsw->sw_id);
4860 return err;
4861}
4862
4863/**
4864 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
4865 * @vsi: VSI having resources allocated
4866 *
4867 * Return 0 on success, negative on failure
4868 */
4869static int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
4870{
4871 int i, err;
4872
4873 if (!vsi->num_txq) {
4874 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
4875 vsi->vsi_num);
4876 return -EINVAL;
4877 }
4878
4879 ice_for_each_txq(vsi, i) {
4880 err = ice_setup_tx_ring(vsi->tx_rings[i]);
4881 if (err)
4882 break;
4883 }
4884
4885 return err;
4886}
4887
4888/**
4889 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
4890 * @vsi: VSI having resources allocated
4891 *
4892 * Return 0 on success, negative on failure
4893 */
4894static int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
4895{
4896 int i, err;
4897
4898 if (!vsi->num_rxq) {
4899 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
4900 vsi->vsi_num);
4901 return -EINVAL;
4902 }
4903
4904 ice_for_each_rxq(vsi, i) {
4905 err = ice_setup_rx_ring(vsi->rx_rings[i]);
4906 if (err)
4907 break;
4908 }
4909
4910 return err;
4911}
4912
4913/**
4914 * ice_vsi_req_irq - Request IRQ from the OS
4915 * @vsi: The VSI IRQ is being requested for
4916 * @basename: name for the vector
4917 *
4918 * Return 0 on success and a negative value on error
4919 */
4920static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
4921{
4922 struct ice_pf *pf = vsi->back;
4923 int err = -EINVAL;
4924
4925 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
4926 err = ice_vsi_req_irq_msix(vsi, basename);
4927
4928 return err;
4929}
4930
4931/**
4932 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
4933 * @vsi: the VSI having resources freed
4934 */
4935static void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
4936{
4937 int i;
4938
4939 if (!vsi->tx_rings)
4940 return;
4941
4942 ice_for_each_txq(vsi, i)
4943 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
4944 ice_free_tx_ring(vsi->tx_rings[i]);
4945}
4946
4947/**
4948 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
4949 * @vsi: the VSI having resources freed
4950 */
4951static void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
4952{
4953 int i;
4954
4955 if (!vsi->rx_rings)
4956 return;
4957
4958 ice_for_each_rxq(vsi, i)
4959 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
4960 ice_free_rx_ring(vsi->rx_rings[i]);
4961}
4962
4963/**
4964 * ice_vsi_open - Called when a network interface is made active
4965 * @vsi: the VSI to open
4966 *
4967 * Initialization of the VSI
4968 *
4969 * Returns 0 on success, negative value on error
4970 */
4971static int ice_vsi_open(struct ice_vsi *vsi)
4972{
4973 char int_name[ICE_INT_NAME_STR_LEN];
4974 struct ice_pf *pf = vsi->back;
4975 int err;
4976
4977 /* allocate descriptors */
4978 err = ice_vsi_setup_tx_rings(vsi);
4979 if (err)
4980 goto err_setup_tx;
4981
4982 err = ice_vsi_setup_rx_rings(vsi);
4983 if (err)
4984 goto err_setup_rx;
4985
4986 err = ice_vsi_cfg(vsi);
4987 if (err)
4988 goto err_setup_rx;
4989
4990 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
4991 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
4992 err = ice_vsi_req_irq(vsi, int_name);
4993 if (err)
4994 goto err_setup_rx;
4995
4996 /* Notify the stack of the actual queue counts. */
4997 err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
4998 if (err)
4999 goto err_set_qs;
5000
5001 err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
5002 if (err)
5003 goto err_set_qs;
5004
5005 err = ice_up_complete(vsi);
5006 if (err)
5007 goto err_up_complete;
5008
5009 return 0;
5010
5011err_up_complete:
5012 ice_down(vsi);
5013err_set_qs:
5014 ice_vsi_free_irq(vsi);
5015err_setup_rx:
5016 ice_vsi_free_rx_rings(vsi);
5017err_setup_tx:
5018 ice_vsi_free_tx_rings(vsi);
5019
5020 return err;
5021}
5022
5023/**
5024 * ice_vsi_close - Shut down a VSI
5025 * @vsi: the VSI being shut down
5026 */
5027static void ice_vsi_close(struct ice_vsi *vsi)
5028{
5029 if (!test_and_set_bit(__ICE_DOWN, vsi->state))
5030 ice_down(vsi);
5031
5032 ice_vsi_free_irq(vsi);
5033 ice_vsi_free_tx_rings(vsi);
5034 ice_vsi_free_rx_rings(vsi);
5035}
5036
5037/**
5038 * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
5039 * @vsi: the VSI being removed
5040 */
5041static void ice_rss_clean(struct ice_vsi *vsi)
5042{
5043 struct ice_pf *pf;
5044
5045 pf = vsi->back;
5046
5047 if (vsi->rss_hkey_user)
5048 devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
5049 if (vsi->rss_lut_user)
5050 devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
5051}
5052
5053/**
5054 * ice_vsi_release - Delete a VSI and free its resources
5055 * @vsi: the VSI being removed
5056 *
5057 * Returns 0 on success or < 0 on error
5058 */
5059static int ice_vsi_release(struct ice_vsi *vsi)
5060{
5061 struct ice_pf *pf;
5062
5063 if (!vsi->back)
5064 return -ENODEV;
5065 pf = vsi->back;
5066
5067 if (vsi->netdev) {
5068 unregister_netdev(vsi->netdev);
5069 free_netdev(vsi->netdev);
5070 vsi->netdev = NULL;
5071 }
5072
5073 if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
5074 ice_rss_clean(vsi);
5075
5076 /* Disable VSI and free resources */
5077 ice_vsi_dis_irq(vsi);
5078 ice_vsi_close(vsi);
5079
5080 /* reclaim interrupt vectors back to PF */
5081 ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
5082 pf->num_avail_msix += vsi->num_q_vectors;
5083
5084 ice_remove_vsi_fltr(&pf->hw, vsi->vsi_num);
5085 ice_vsi_delete(vsi);
5086 ice_vsi_free_q_vectors(vsi);
5087 ice_vsi_clear_rings(vsi);
5088
5089 ice_vsi_put_qs(vsi);
5090 pf->q_left_tx += vsi->alloc_txq;
5091 pf->q_left_rx += vsi->alloc_rxq;
5092
5093 ice_vsi_clear(vsi);
5094
5095 return 0;
5096}
5097
5098/**
5099 * ice_dis_vsi - pause a VSI
5100 * @vsi: the VSI being paused
5101 */
5102static void ice_dis_vsi(struct ice_vsi *vsi)
5103{
5104 if (test_bit(__ICE_DOWN, vsi->state))
5105 return;
5106
5107 set_bit(__ICE_NEEDS_RESTART, vsi->state);
5108
5109 if (vsi->netdev && netif_running(vsi->netdev) &&
5110 vsi->type == ICE_VSI_PF)
5111 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5112
5113 ice_vsi_close(vsi);
5114}
5115
5116/**
5117 * ice_ena_vsi - resume a VSI
5118 * @vsi: the VSI being resume
5119 */
5120static void ice_ena_vsi(struct ice_vsi *vsi)
5121{
5122 if (!test_and_clear_bit(__ICE_NEEDS_RESTART, vsi->state))
5123 return;
5124
5125 if (vsi->netdev && netif_running(vsi->netdev))
5126 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5127 else if (ice_vsi_open(vsi))
5128 /* this clears the DOWN bit */
5129 dev_dbg(&vsi->back->pdev->dev, "Failed open VSI 0x%04X on switch 0x%04X\n",
5130 vsi->vsi_num, vsi->vsw->sw_id);
5131}
5132
5133/**
5134 * ice_pf_dis_all_vsi - Pause all VSIs on a PF
5135 * @pf: the PF
5136 */
5137static void ice_pf_dis_all_vsi(struct ice_pf *pf)
5138{
5139 int v;
5140
5141 ice_for_each_vsi(pf, v)
5142 if (pf->vsi[v])
5143 ice_dis_vsi(pf->vsi[v]);
5144}
5145
5146/**
5147 * ice_pf_ena_all_vsi - Resume all VSIs on a PF
5148 * @pf: the PF
5149 */
5150static void ice_pf_ena_all_vsi(struct ice_pf *pf)
5151{
5152 int v;
5153
5154 ice_for_each_vsi(pf, v)
5155 if (pf->vsi[v])
5156 ice_ena_vsi(pf->vsi[v]);
5157}
5158
5159/**
5160 * ice_rebuild - rebuild after reset
5161 * @pf: pf to rebuild
5162 */
5163static void ice_rebuild(struct ice_pf *pf)
5164{
5165 struct device *dev = &pf->pdev->dev;
5166 struct ice_hw *hw = &pf->hw;
5167 enum ice_status ret;
5168 int err;
5169
5170 if (test_bit(__ICE_DOWN, pf->state))
5171 goto clear_recovery;
5172
5173 dev_dbg(dev, "rebuilding pf\n");
5174
5175 ret = ice_init_all_ctrlq(hw);
5176 if (ret) {
5177 dev_err(dev, "control queues init failed %d\n", ret);
5178 goto fail_reset;
5179 }
5180
5181 ret = ice_clear_pf_cfg(hw);
5182 if (ret) {
5183 dev_err(dev, "clear PF configuration failed %d\n", ret);
5184 goto fail_reset;
5185 }
5186
5187 ice_clear_pxe_mode(hw);
5188
5189 ret = ice_get_caps(hw);
5190 if (ret) {
5191 dev_err(dev, "ice_get_caps failed %d\n", ret);
5192 goto fail_reset;
5193 }
5194
5195 /* basic nic switch setup */
5196 err = ice_setup_pf_sw(pf);
5197 if (err) {
5198 dev_err(dev, "ice_setup_pf_sw failed\n");
5199 goto fail_reset;
5200 }
5201
5202 /* start misc vector */
5203 if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
5204 err = ice_req_irq_msix_misc(pf);
5205 if (err) {
5206 dev_err(dev, "misc vector setup failed: %d\n", err);
5207 goto fail_reset;
5208 }
5209 }
5210
5211 /* restart the VSIs that were rebuilt and running before the reset */
5212 ice_pf_ena_all_vsi(pf);
5213
5214 return;
5215
5216fail_reset:
5217 ice_shutdown_all_ctrlq(hw);
5218 set_bit(__ICE_RESET_FAILED, pf->state);
5219clear_recovery:
5220 set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
5221}
5222
5223/**
5224 * ice_change_mtu - NDO callback to change the MTU
5225 * @netdev: network interface device structure
5226 * @new_mtu: new value for maximum frame size
5227 *
5228 * Returns 0 on success, negative on failure
5229 */
5230static int ice_change_mtu(struct net_device *netdev, int new_mtu)
5231{
5232 struct ice_netdev_priv *np = netdev_priv(netdev);
5233 struct ice_vsi *vsi = np->vsi;
5234 struct ice_pf *pf = vsi->back;
5235 u8 count = 0;
5236
5237 if (new_mtu == netdev->mtu) {
5238 netdev_warn(netdev, "mtu is already %d\n", netdev->mtu);
5239 return 0;
5240 }
5241
5242 if (new_mtu < netdev->min_mtu) {
5243 netdev_err(netdev, "new mtu invalid. min_mtu is %d\n",
5244 netdev->min_mtu);
5245 return -EINVAL;
5246 } else if (new_mtu > netdev->max_mtu) {
5247 netdev_err(netdev, "new mtu invalid. max_mtu is %d\n",
5248 netdev->min_mtu);
5249 return -EINVAL;
5250 }
5251 /* if a reset is in progress, wait for some time for it to complete */
5252 do {
5253 if (ice_is_reset_recovery_pending(pf->state)) {
5254 count++;
5255 usleep_range(1000, 2000);
5256 } else {
5257 break;
5258 }
5259
5260 } while (count < 100);
5261
5262 if (count == 100) {
5263 netdev_err(netdev, "can't change mtu. Device is busy\n");
5264 return -EBUSY;
5265 }
5266
5267 netdev->mtu = new_mtu;
5268
5269 /* if VSI is up, bring it down and then back up */
5270 if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
5271 int err;
5272
5273 err = ice_down(vsi);
5274 if (err) {
5275 netdev_err(netdev, "change mtu if_up err %d\n", err);
5276 return err;
5277 }
5278
5279 err = ice_up(vsi);
5280 if (err) {
5281 netdev_err(netdev, "change mtu if_up err %d\n", err);
5282 return err;
5283 }
5284 }
5285
5286 netdev_dbg(netdev, "changed mtu to %d\n", new_mtu);
5287 return 0;
5288}
5289
5290/**
5291 * ice_set_rss - Set RSS keys and lut
5292 * @vsi: Pointer to VSI structure
5293 * @seed: RSS hash seed
5294 * @lut: Lookup table
5295 * @lut_size: Lookup table size
5296 *
5297 * Returns 0 on success, negative on failure
5298 */
5299int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
5300{
5301 struct ice_pf *pf = vsi->back;
5302 struct ice_hw *hw = &pf->hw;
5303 enum ice_status status;
5304
5305 if (seed) {
5306 struct ice_aqc_get_set_rss_keys *buf =
5307 (struct ice_aqc_get_set_rss_keys *)seed;
5308
5309 status = ice_aq_set_rss_key(hw, vsi->vsi_num, buf);
5310
5311 if (status) {
5312 dev_err(&pf->pdev->dev,
5313 "Cannot set RSS key, err %d aq_err %d\n",
5314 status, hw->adminq.rq_last_status);
5315 return -EIO;
5316 }
5317 }
5318
5319 if (lut) {
5320 status = ice_aq_set_rss_lut(hw, vsi->vsi_num,
5321 vsi->rss_lut_type, lut, lut_size);
5322 if (status) {
5323 dev_err(&pf->pdev->dev,
5324 "Cannot set RSS lut, err %d aq_err %d\n",
5325 status, hw->adminq.rq_last_status);
5326 return -EIO;
5327 }
5328 }
5329
5330 return 0;
5331}
5332
5333/**
5334 * ice_get_rss - Get RSS keys and lut
5335 * @vsi: Pointer to VSI structure
5336 * @seed: Buffer to store the keys
5337 * @lut: Buffer to store the lookup table entries
5338 * @lut_size: Size of buffer to store the lookup table entries
5339 *
5340 * Returns 0 on success, negative on failure
5341 */
5342int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
5343{
5344 struct ice_pf *pf = vsi->back;
5345 struct ice_hw *hw = &pf->hw;
5346 enum ice_status status;
5347
5348 if (seed) {
5349 struct ice_aqc_get_set_rss_keys *buf =
5350 (struct ice_aqc_get_set_rss_keys *)seed;
5351
5352 status = ice_aq_get_rss_key(hw, vsi->vsi_num, buf);
5353 if (status) {
5354 dev_err(&pf->pdev->dev,
5355 "Cannot get RSS key, err %d aq_err %d\n",
5356 status, hw->adminq.rq_last_status);
5357 return -EIO;
5358 }
5359 }
5360
5361 if (lut) {
5362 status = ice_aq_get_rss_lut(hw, vsi->vsi_num,
5363 vsi->rss_lut_type, lut, lut_size);
5364 if (status) {
5365 dev_err(&pf->pdev->dev,
5366 "Cannot get RSS lut, err %d aq_err %d\n",
5367 status, hw->adminq.rq_last_status);
5368 return -EIO;
5369 }
5370 }
5371
5372 return 0;
5373}
5374
5375/**
5376 * ice_open - Called when a network interface becomes active
5377 * @netdev: network interface device structure
5378 *
5379 * The open entry point is called when a network interface is made
5380 * active by the system (IFF_UP). At this point all resources needed
5381 * for transmit and receive operations are allocated, the interrupt
5382 * handler is registered with the OS, the netdev watchdog is enabled,
5383 * and the stack is notified that the interface is ready.
5384 *
5385 * Returns 0 on success, negative value on failure
5386 */
5387static int ice_open(struct net_device *netdev)
5388{
5389 struct ice_netdev_priv *np = netdev_priv(netdev);
5390 struct ice_vsi *vsi = np->vsi;
5391 int err;
5392
5393 netif_carrier_off(netdev);
5394
5395 err = ice_vsi_open(vsi);
5396
5397 if (err)
5398 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
5399 vsi->vsi_num, vsi->vsw->sw_id);
5400 return err;
5401}
5402
5403/**
5404 * ice_stop - Disables a network interface
5405 * @netdev: network interface device structure
5406 *
5407 * The stop entry point is called when an interface is de-activated by the OS,
5408 * and the netdevice enters the DOWN state. The hardware is still under the
5409 * driver's control, but the netdev interface is disabled.
5410 *
5411 * Returns success only - not allowed to fail
5412 */
5413static int ice_stop(struct net_device *netdev)
5414{
5415 struct ice_netdev_priv *np = netdev_priv(netdev);
5416 struct ice_vsi *vsi = np->vsi;
5417
5418 ice_vsi_close(vsi);
5419
5420 return 0;
5421}
5422
5423/**
5424 * ice_features_check - Validate encapsulated packet conforms to limits
5425 * @skb: skb buffer
5426 * @netdev: This port's netdev
5427 * @features: Offload features that the stack believes apply
5428 */
5429static netdev_features_t
5430ice_features_check(struct sk_buff *skb,
5431 struct net_device __always_unused *netdev,
5432 netdev_features_t features)
5433{
5434 size_t len;
5435
5436 /* No point in doing any of this if neither checksum nor GSO are
5437 * being requested for this frame. We can rule out both by just
5438 * checking for CHECKSUM_PARTIAL
5439 */
5440 if (skb->ip_summed != CHECKSUM_PARTIAL)
5441 return features;
5442
5443 /* We cannot support GSO if the MSS is going to be less than
5444 * 64 bytes. If it is then we need to drop support for GSO.
5445 */
5446 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
5447 features &= ~NETIF_F_GSO_MASK;
5448
5449 len = skb_network_header(skb) - skb->data;
5450 if (len & ~(ICE_TXD_MACLEN_MAX))
5451 goto out_rm_features;
5452
5453 len = skb_transport_header(skb) - skb_network_header(skb);
5454 if (len & ~(ICE_TXD_IPLEN_MAX))
5455 goto out_rm_features;
5456
5457 if (skb->encapsulation) {
5458 len = skb_inner_network_header(skb) - skb_transport_header(skb);
5459 if (len & ~(ICE_TXD_L4LEN_MAX))
5460 goto out_rm_features;
5461
5462 len = skb_inner_transport_header(skb) -
5463 skb_inner_network_header(skb);
5464 if (len & ~(ICE_TXD_IPLEN_MAX))
5465 goto out_rm_features;
5466 }
5467
5468 return features;
5469out_rm_features:
5470 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
5471}
5472
5473static const struct net_device_ops ice_netdev_ops = {
5474 .ndo_open = ice_open,
5475 .ndo_stop = ice_stop,
5476 .ndo_start_xmit = ice_start_xmit,
5477 .ndo_features_check = ice_features_check,
5478 .ndo_set_rx_mode = ice_set_rx_mode,
5479 .ndo_set_mac_address = ice_set_mac_address,
5480 .ndo_validate_addr = eth_validate_addr,
5481 .ndo_change_mtu = ice_change_mtu,
5482 .ndo_get_stats64 = ice_get_stats64,
5483#ifdef CONFIG_NET_POLL_CONTROLLER
5484 .ndo_poll_controller = ice_netpoll,
5485#endif /* CONFIG_NET_POLL_CONTROLLER */
5486 .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
5487 .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
5488 .ndo_set_features = ice_set_features,
5489 .ndo_fdb_add = ice_fdb_add,
5490 .ndo_fdb_del = ice_fdb_del,
5491};