ice: Report what the user set for coalesce [tx|rx]-usecs
[linux-2.6-microblaze.git] / drivers / net / ethernet / intel / ice / ice_ethtool.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 /* ethtool support for ice */
5
6 #include "ice.h"
7 #include "ice_lib.h"
8 #include "ice_dcb_lib.h"
9
10 struct ice_stats {
11         char stat_string[ETH_GSTRING_LEN];
12         int sizeof_stat;
13         int stat_offset;
14 };
15
16 #define ICE_STAT(_type, _name, _stat) { \
17         .stat_string = _name, \
18         .sizeof_stat = FIELD_SIZEOF(_type, _stat), \
19         .stat_offset = offsetof(_type, _stat) \
20 }
21
22 #define ICE_VSI_STAT(_name, _stat) \
23                 ICE_STAT(struct ice_vsi, _name, _stat)
24 #define ICE_PF_STAT(_name, _stat) \
25                 ICE_STAT(struct ice_pf, _name, _stat)
26
27 static int ice_q_stats_len(struct net_device *netdev)
28 {
29         struct ice_netdev_priv *np = netdev_priv(netdev);
30
31         return ((np->vsi->alloc_txq + np->vsi->alloc_rxq) *
32                 (sizeof(struct ice_q_stats) / sizeof(u64)));
33 }
34
35 #define ICE_PF_STATS_LEN        ARRAY_SIZE(ice_gstrings_pf_stats)
36 #define ICE_VSI_STATS_LEN       ARRAY_SIZE(ice_gstrings_vsi_stats)
37
38 #define ICE_PFC_STATS_LEN ( \
39                 (FIELD_SIZEOF(struct ice_pf, stats.priority_xoff_rx) + \
40                  FIELD_SIZEOF(struct ice_pf, stats.priority_xon_rx) + \
41                  FIELD_SIZEOF(struct ice_pf, stats.priority_xoff_tx) + \
42                  FIELD_SIZEOF(struct ice_pf, stats.priority_xon_tx)) \
43                  / sizeof(u64))
44 #define ICE_ALL_STATS_LEN(n)    (ICE_PF_STATS_LEN + ICE_PFC_STATS_LEN + \
45                                  ICE_VSI_STATS_LEN + ice_q_stats_len(n))
46
47 static const struct ice_stats ice_gstrings_vsi_stats[] = {
48         ICE_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
49         ICE_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
50         ICE_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
51         ICE_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
52         ICE_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
53         ICE_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
54         ICE_VSI_STAT("rx_bytes", eth_stats.rx_bytes),
55         ICE_VSI_STAT("tx_bytes", eth_stats.tx_bytes),
56         ICE_VSI_STAT("rx_dropped", eth_stats.rx_discards),
57         ICE_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
58         ICE_VSI_STAT("rx_alloc_fail", rx_buf_failed),
59         ICE_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
60         ICE_VSI_STAT("tx_errors", eth_stats.tx_errors),
61         ICE_VSI_STAT("tx_linearize", tx_linearize),
62 };
63
64 enum ice_ethtool_test_id {
65         ICE_ETH_TEST_REG = 0,
66         ICE_ETH_TEST_EEPROM,
67         ICE_ETH_TEST_INTR,
68         ICE_ETH_TEST_LOOP,
69         ICE_ETH_TEST_LINK,
70 };
71
72 static const char ice_gstrings_test[][ETH_GSTRING_LEN] = {
73         "Register test  (offline)",
74         "EEPROM test    (offline)",
75         "Interrupt test (offline)",
76         "Loopback test  (offline)",
77         "Link test   (on/offline)",
78 };
79
80 #define ICE_TEST_LEN (sizeof(ice_gstrings_test) / ETH_GSTRING_LEN)
81
82 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
83  * but they aren't. This device is capable of supporting multiple
84  * VSIs/netdevs on a single PF. The NETDEV_STATs are for individual
85  * netdevs whereas the PF_STATs are for the physical function that's
86  * hosting these netdevs.
87  *
88  * The PF_STATs are appended to the netdev stats only when ethtool -S
89  * is queried on the base PF netdev.
90  */
91 static const struct ice_stats ice_gstrings_pf_stats[] = {
92         ICE_PF_STAT("rx_bytes.nic", stats.eth.rx_bytes),
93         ICE_PF_STAT("tx_bytes.nic", stats.eth.tx_bytes),
94         ICE_PF_STAT("rx_unicast.nic", stats.eth.rx_unicast),
95         ICE_PF_STAT("tx_unicast.nic", stats.eth.tx_unicast),
96         ICE_PF_STAT("rx_multicast.nic", stats.eth.rx_multicast),
97         ICE_PF_STAT("tx_multicast.nic", stats.eth.tx_multicast),
98         ICE_PF_STAT("rx_broadcast.nic", stats.eth.rx_broadcast),
99         ICE_PF_STAT("tx_broadcast.nic", stats.eth.tx_broadcast),
100         ICE_PF_STAT("tx_errors.nic", stats.eth.tx_errors),
101         ICE_PF_STAT("rx_size_64.nic", stats.rx_size_64),
102         ICE_PF_STAT("tx_size_64.nic", stats.tx_size_64),
103         ICE_PF_STAT("rx_size_127.nic", stats.rx_size_127),
104         ICE_PF_STAT("tx_size_127.nic", stats.tx_size_127),
105         ICE_PF_STAT("rx_size_255.nic", stats.rx_size_255),
106         ICE_PF_STAT("tx_size_255.nic", stats.tx_size_255),
107         ICE_PF_STAT("rx_size_511.nic", stats.rx_size_511),
108         ICE_PF_STAT("tx_size_511.nic", stats.tx_size_511),
109         ICE_PF_STAT("rx_size_1023.nic", stats.rx_size_1023),
110         ICE_PF_STAT("tx_size_1023.nic", stats.tx_size_1023),
111         ICE_PF_STAT("rx_size_1522.nic", stats.rx_size_1522),
112         ICE_PF_STAT("tx_size_1522.nic", stats.tx_size_1522),
113         ICE_PF_STAT("rx_size_big.nic", stats.rx_size_big),
114         ICE_PF_STAT("tx_size_big.nic", stats.tx_size_big),
115         ICE_PF_STAT("link_xon_rx.nic", stats.link_xon_rx),
116         ICE_PF_STAT("link_xon_tx.nic", stats.link_xon_tx),
117         ICE_PF_STAT("link_xoff_rx.nic", stats.link_xoff_rx),
118         ICE_PF_STAT("link_xoff_tx.nic", stats.link_xoff_tx),
119         ICE_PF_STAT("tx_dropped_link_down.nic", stats.tx_dropped_link_down),
120         ICE_PF_STAT("rx_undersize.nic", stats.rx_undersize),
121         ICE_PF_STAT("rx_fragments.nic", stats.rx_fragments),
122         ICE_PF_STAT("rx_oversize.nic", stats.rx_oversize),
123         ICE_PF_STAT("rx_jabber.nic", stats.rx_jabber),
124         ICE_PF_STAT("rx_csum_bad.nic", hw_csum_rx_error),
125         ICE_PF_STAT("rx_length_errors.nic", stats.rx_len_errors),
126         ICE_PF_STAT("rx_dropped.nic", stats.eth.rx_discards),
127         ICE_PF_STAT("rx_crc_errors.nic", stats.crc_errors),
128         ICE_PF_STAT("illegal_bytes.nic", stats.illegal_bytes),
129         ICE_PF_STAT("mac_local_faults.nic", stats.mac_local_faults),
130         ICE_PF_STAT("mac_remote_faults.nic", stats.mac_remote_faults),
131 };
132
133 static const u32 ice_regs_dump_list[] = {
134         PFGEN_STATE,
135         PRTGEN_STATUS,
136         QRX_CTRL(0),
137         QINT_TQCTL(0),
138         QINT_RQCTL(0),
139         PFINT_OICR_ENA,
140         QRX_ITR(0),
141         PF0INT_ITR_0(0),
142         PF0INT_ITR_1(0),
143         PF0INT_ITR_2(0),
144 };
145
146 struct ice_priv_flag {
147         char name[ETH_GSTRING_LEN];
148         u32 bitno;                      /* bit position in pf->flags */
149 };
150
151 #define ICE_PRIV_FLAG(_name, _bitno) { \
152         .name = _name, \
153         .bitno = _bitno, \
154 }
155
156 static const struct ice_priv_flag ice_gstrings_priv_flags[] = {
157         ICE_PRIV_FLAG("link-down-on-close", ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA),
158         ICE_PRIV_FLAG("fw-lldp-agent", ICE_FLAG_FW_LLDP_AGENT),
159 };
160
161 #define ICE_PRIV_FLAG_ARRAY_SIZE        ARRAY_SIZE(ice_gstrings_priv_flags)
162
163 /**
164  * ice_nvm_version_str - format the NVM version strings
165  * @hw: ptr to the hardware info
166  */
167 static char *ice_nvm_version_str(struct ice_hw *hw)
168 {
169         static char buf[ICE_ETHTOOL_FWVER_LEN];
170         u8 ver, patch;
171         u32 full_ver;
172         u16 build;
173
174         full_ver = hw->nvm.oem_ver;
175         ver = (u8)((full_ver & ICE_OEM_VER_MASK) >> ICE_OEM_VER_SHIFT);
176         build = (u16)((full_ver & ICE_OEM_VER_BUILD_MASK) >>
177                       ICE_OEM_VER_BUILD_SHIFT);
178         patch = (u8)(full_ver & ICE_OEM_VER_PATCH_MASK);
179
180         snprintf(buf, sizeof(buf), "%x.%02x 0x%x %d.%d.%d",
181                  (hw->nvm.ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT,
182                  (hw->nvm.ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT,
183                  hw->nvm.eetrack, ver, build, patch);
184
185         return buf;
186 }
187
188 static void
189 ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
190 {
191         struct ice_netdev_priv *np = netdev_priv(netdev);
192         struct ice_vsi *vsi = np->vsi;
193         struct ice_pf *pf = vsi->back;
194
195         strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
196         strlcpy(drvinfo->version, ice_drv_ver, sizeof(drvinfo->version));
197         strlcpy(drvinfo->fw_version, ice_nvm_version_str(&pf->hw),
198                 sizeof(drvinfo->fw_version));
199         strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
200                 sizeof(drvinfo->bus_info));
201         drvinfo->n_priv_flags = ICE_PRIV_FLAG_ARRAY_SIZE;
202 }
203
204 static int ice_get_regs_len(struct net_device __always_unused *netdev)
205 {
206         return sizeof(ice_regs_dump_list);
207 }
208
209 static void
210 ice_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
211 {
212         struct ice_netdev_priv *np = netdev_priv(netdev);
213         struct ice_pf *pf = np->vsi->back;
214         struct ice_hw *hw = &pf->hw;
215         u32 *regs_buf = (u32 *)p;
216         int i;
217
218         regs->version = 1;
219
220         for (i = 0; i < ARRAY_SIZE(ice_regs_dump_list); ++i)
221                 regs_buf[i] = rd32(hw, ice_regs_dump_list[i]);
222 }
223
224 static u32 ice_get_msglevel(struct net_device *netdev)
225 {
226         struct ice_netdev_priv *np = netdev_priv(netdev);
227         struct ice_pf *pf = np->vsi->back;
228
229 #ifndef CONFIG_DYNAMIC_DEBUG
230         if (pf->hw.debug_mask)
231                 netdev_info(netdev, "hw debug_mask: 0x%llX\n",
232                             pf->hw.debug_mask);
233 #endif /* !CONFIG_DYNAMIC_DEBUG */
234
235         return pf->msg_enable;
236 }
237
238 static void ice_set_msglevel(struct net_device *netdev, u32 data)
239 {
240         struct ice_netdev_priv *np = netdev_priv(netdev);
241         struct ice_pf *pf = np->vsi->back;
242
243 #ifndef CONFIG_DYNAMIC_DEBUG
244         if (ICE_DBG_USER & data)
245                 pf->hw.debug_mask = data;
246         else
247                 pf->msg_enable = data;
248 #else
249         pf->msg_enable = data;
250 #endif /* !CONFIG_DYNAMIC_DEBUG */
251 }
252
253 static int ice_get_eeprom_len(struct net_device *netdev)
254 {
255         struct ice_netdev_priv *np = netdev_priv(netdev);
256         struct ice_pf *pf = np->vsi->back;
257
258         return (int)(pf->hw.nvm.sr_words * sizeof(u16));
259 }
260
261 static int
262 ice_get_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
263                u8 *bytes)
264 {
265         struct ice_netdev_priv *np = netdev_priv(netdev);
266         u16 first_word, last_word, nwords;
267         struct ice_vsi *vsi = np->vsi;
268         struct ice_pf *pf = vsi->back;
269         struct ice_hw *hw = &pf->hw;
270         enum ice_status status;
271         struct device *dev;
272         int ret = 0;
273         u16 *buf;
274
275         dev = &pf->pdev->dev;
276
277         eeprom->magic = hw->vendor_id | (hw->device_id << 16);
278
279         first_word = eeprom->offset >> 1;
280         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
281         nwords = last_word - first_word + 1;
282
283         buf = devm_kcalloc(dev, nwords, sizeof(u16), GFP_KERNEL);
284         if (!buf)
285                 return -ENOMEM;
286
287         status = ice_read_sr_buf(hw, first_word, &nwords, buf);
288         if (status) {
289                 dev_err(dev, "ice_read_sr_buf failed, err %d aq_err %d\n",
290                         status, hw->adminq.sq_last_status);
291                 eeprom->len = sizeof(u16) * nwords;
292                 ret = -EIO;
293                 goto out;
294         }
295
296         memcpy(bytes, (u8 *)buf + (eeprom->offset & 1), eeprom->len);
297 out:
298         devm_kfree(dev, buf);
299         return ret;
300 }
301
302 /**
303  * ice_active_vfs - check if there are any active VFs
304  * @pf: board private structure
305  *
306  * Returns true if an active VF is found, otherwise returns false
307  */
308 static bool ice_active_vfs(struct ice_pf *pf)
309 {
310         struct ice_vf *vf = pf->vf;
311         int i;
312
313         for (i = 0; i < pf->num_alloc_vfs; i++, vf++)
314                 if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
315                         return true;
316         return false;
317 }
318
319 /**
320  * ice_link_test - perform a link test on a given net_device
321  * @netdev: network interface device structure
322  *
323  * This function performs one of the self-tests required by ethtool.
324  * Returns 0 on success, non-zero on failure.
325  */
326 static u64 ice_link_test(struct net_device *netdev)
327 {
328         struct ice_netdev_priv *np = netdev_priv(netdev);
329         enum ice_status status;
330         bool link_up = false;
331
332         netdev_info(netdev, "link test\n");
333         status = ice_get_link_status(np->vsi->port_info, &link_up);
334         if (status) {
335                 netdev_err(netdev, "link query error, status = %d\n", status);
336                 return 1;
337         }
338
339         if (!link_up)
340                 return 2;
341
342         return 0;
343 }
344
345 /**
346  * ice_eeprom_test - perform an EEPROM test on a given net_device
347  * @netdev: network interface device structure
348  *
349  * This function performs one of the self-tests required by ethtool.
350  * Returns 0 on success, non-zero on failure.
351  */
352 static u64 ice_eeprom_test(struct net_device *netdev)
353 {
354         struct ice_netdev_priv *np = netdev_priv(netdev);
355         struct ice_pf *pf = np->vsi->back;
356
357         netdev_info(netdev, "EEPROM test\n");
358         return !!(ice_nvm_validate_checksum(&pf->hw));
359 }
360
361 /**
362  * ice_reg_pattern_test
363  * @hw: pointer to the HW struct
364  * @reg: reg to be tested
365  * @mask: bits to be touched
366  */
367 static int ice_reg_pattern_test(struct ice_hw *hw, u32 reg, u32 mask)
368 {
369         struct ice_pf *pf = (struct ice_pf *)hw->back;
370         static const u32 patterns[] = {
371                 0x5A5A5A5A, 0xA5A5A5A5,
372                 0x00000000, 0xFFFFFFFF
373         };
374         u32 val, orig_val;
375         int i;
376
377         orig_val = rd32(hw, reg);
378         for (i = 0; i < ARRAY_SIZE(patterns); ++i) {
379                 u32 pattern = patterns[i] & mask;
380
381                 wr32(hw, reg, pattern);
382                 val = rd32(hw, reg);
383                 if (val == pattern)
384                         continue;
385                 dev_err(&pf->pdev->dev,
386                         "%s: reg pattern test failed - reg 0x%08x pat 0x%08x val 0x%08x\n"
387                         , __func__, reg, pattern, val);
388                 return 1;
389         }
390
391         wr32(hw, reg, orig_val);
392         val = rd32(hw, reg);
393         if (val != orig_val) {
394                 dev_err(&pf->pdev->dev,
395                         "%s: reg restore test failed - reg 0x%08x orig 0x%08x val 0x%08x\n"
396                         , __func__, reg, orig_val, val);
397                 return 1;
398         }
399
400         return 0;
401 }
402
403 /**
404  * ice_reg_test - perform a register test on a given net_device
405  * @netdev: network interface device structure
406  *
407  * This function performs one of the self-tests required by ethtool.
408  * Returns 0 on success, non-zero on failure.
409  */
410 static u64 ice_reg_test(struct net_device *netdev)
411 {
412         struct ice_netdev_priv *np = netdev_priv(netdev);
413         struct ice_hw *hw = np->vsi->port_info->hw;
414         u32 int_elements = hw->func_caps.common_cap.num_msix_vectors ?
415                 hw->func_caps.common_cap.num_msix_vectors - 1 : 1;
416         struct ice_diag_reg_test_info {
417                 u32 address;
418                 u32 mask;
419                 u32 elem_num;
420                 u32 elem_size;
421         } ice_reg_list[] = {
422                 {GLINT_ITR(0, 0), 0x00000fff, int_elements,
423                         GLINT_ITR(0, 1) - GLINT_ITR(0, 0)},
424                 {GLINT_ITR(1, 0), 0x00000fff, int_elements,
425                         GLINT_ITR(1, 1) - GLINT_ITR(1, 0)},
426                 {GLINT_ITR(0, 0), 0x00000fff, int_elements,
427                         GLINT_ITR(2, 1) - GLINT_ITR(2, 0)},
428                 {GLINT_CTL, 0xffff0001, 1, 0}
429         };
430         int i;
431
432         netdev_dbg(netdev, "Register test\n");
433         for (i = 0; i < ARRAY_SIZE(ice_reg_list); ++i) {
434                 u32 j;
435
436                 for (j = 0; j < ice_reg_list[i].elem_num; ++j) {
437                         u32 mask = ice_reg_list[i].mask;
438                         u32 reg = ice_reg_list[i].address +
439                                 (j * ice_reg_list[i].elem_size);
440
441                         /* bail on failure (non-zero return) */
442                         if (ice_reg_pattern_test(hw, reg, mask))
443                                 return 1;
444                 }
445         }
446
447         return 0;
448 }
449
450 /**
451  * ice_lbtest_prepare_rings - configure Tx/Rx test rings
452  * @vsi: pointer to the VSI structure
453  *
454  * Function configures rings of a VSI for loopback test without
455  * enabling interrupts or informing the kernel about new queues.
456  *
457  * Returns 0 on success, negative on failure.
458  */
459 static int ice_lbtest_prepare_rings(struct ice_vsi *vsi)
460 {
461         int status;
462
463         status = ice_vsi_setup_tx_rings(vsi);
464         if (status)
465                 goto err_setup_tx_ring;
466
467         status = ice_vsi_setup_rx_rings(vsi);
468         if (status)
469                 goto err_setup_rx_ring;
470
471         status = ice_vsi_cfg(vsi);
472         if (status)
473                 goto err_setup_rx_ring;
474
475         status = ice_vsi_start_rx_rings(vsi);
476         if (status)
477                 goto err_start_rx_ring;
478
479         return status;
480
481 err_start_rx_ring:
482         ice_vsi_free_rx_rings(vsi);
483 err_setup_rx_ring:
484         ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
485 err_setup_tx_ring:
486         ice_vsi_free_tx_rings(vsi);
487
488         return status;
489 }
490
491 /**
492  * ice_lbtest_disable_rings - disable Tx/Rx test rings after loopback test
493  * @vsi: pointer to the VSI structure
494  *
495  * Function stops and frees VSI rings after a loopback test.
496  * Returns 0 on success, negative on failure.
497  */
498 static int ice_lbtest_disable_rings(struct ice_vsi *vsi)
499 {
500         int status;
501
502         status = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
503         if (status)
504                 netdev_err(vsi->netdev, "Failed to stop Tx rings, VSI %d error %d\n",
505                            vsi->vsi_num, status);
506
507         status = ice_vsi_stop_rx_rings(vsi);
508         if (status)
509                 netdev_err(vsi->netdev, "Failed to stop Rx rings, VSI %d error %d\n",
510                            vsi->vsi_num, status);
511
512         ice_vsi_free_tx_rings(vsi);
513         ice_vsi_free_rx_rings(vsi);
514
515         return status;
516 }
517
518 /**
519  * ice_lbtest_create_frame - create test packet
520  * @pf: pointer to the PF structure
521  * @ret_data: allocated frame buffer
522  * @size: size of the packet data
523  *
524  * Function allocates a frame with a test pattern on specific offsets.
525  * Returns 0 on success, non-zero on failure.
526  */
527 static int ice_lbtest_create_frame(struct ice_pf *pf, u8 **ret_data, u16 size)
528 {
529         u8 *data;
530
531         if (!pf)
532                 return -EINVAL;
533
534         data = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
535         if (!data)
536                 return -ENOMEM;
537
538         /* Since the ethernet test frame should always be at least
539          * 64 bytes long, fill some octets in the payload with test data.
540          */
541         memset(data, 0xFF, size);
542         data[32] = 0xDE;
543         data[42] = 0xAD;
544         data[44] = 0xBE;
545         data[46] = 0xEF;
546
547         *ret_data = data;
548
549         return 0;
550 }
551
552 /**
553  * ice_lbtest_check_frame - verify received loopback frame
554  * @frame: pointer to the raw packet data
555  *
556  * Function verifies received test frame with a pattern.
557  * Returns true if frame matches the pattern, false otherwise.
558  */
559 static bool ice_lbtest_check_frame(u8 *frame)
560 {
561         /* Validate bytes of a frame under offsets chosen earlier */
562         if (frame[32] == 0xDE &&
563             frame[42] == 0xAD &&
564             frame[44] == 0xBE &&
565             frame[46] == 0xEF &&
566             frame[48] == 0xFF)
567                 return true;
568
569         return false;
570 }
571
572 /**
573  * ice_diag_send - send test frames to the test ring
574  * @tx_ring: pointer to the transmit ring
575  * @data: pointer to the raw packet data
576  * @size: size of the packet to send
577  *
578  * Function sends loopback packets on a test Tx ring.
579  */
580 static int ice_diag_send(struct ice_ring *tx_ring, u8 *data, u16 size)
581 {
582         struct ice_tx_desc *tx_desc;
583         struct ice_tx_buf *tx_buf;
584         dma_addr_t dma;
585         u64 td_cmd;
586
587         tx_desc = ICE_TX_DESC(tx_ring, tx_ring->next_to_use);
588         tx_buf = &tx_ring->tx_buf[tx_ring->next_to_use];
589
590         dma = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE);
591         if (dma_mapping_error(tx_ring->dev, dma))
592                 return -EINVAL;
593
594         tx_desc->buf_addr = cpu_to_le64(dma);
595
596         /* These flags are required for a descriptor to be pushed out */
597         td_cmd = (u64)(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS);
598         tx_desc->cmd_type_offset_bsz =
599                 cpu_to_le64(ICE_TX_DESC_DTYPE_DATA |
600                             (td_cmd << ICE_TXD_QW1_CMD_S) |
601                             ((u64)0 << ICE_TXD_QW1_OFFSET_S) |
602                             ((u64)size << ICE_TXD_QW1_TX_BUF_SZ_S) |
603                             ((u64)0 << ICE_TXD_QW1_L2TAG1_S));
604
605         tx_buf->next_to_watch = tx_desc;
606
607         /* Force memory write to complete before letting h/w know
608          * there are new descriptors to fetch.
609          */
610         wmb();
611
612         tx_ring->next_to_use++;
613         if (tx_ring->next_to_use >= tx_ring->count)
614                 tx_ring->next_to_use = 0;
615
616         writel_relaxed(tx_ring->next_to_use, tx_ring->tail);
617
618         /* Wait until the packets get transmitted to the receive queue. */
619         usleep_range(1000, 2000);
620         dma_unmap_single(tx_ring->dev, dma, size, DMA_TO_DEVICE);
621
622         return 0;
623 }
624
625 #define ICE_LB_FRAME_SIZE 64
626 /**
627  * ice_lbtest_receive_frames - receive and verify test frames
628  * @rx_ring: pointer to the receive ring
629  *
630  * Function receives loopback packets and verify their correctness.
631  * Returns number of received valid frames.
632  */
633 static int ice_lbtest_receive_frames(struct ice_ring *rx_ring)
634 {
635         struct ice_rx_buf *rx_buf;
636         int valid_frames, i;
637         u8 *received_buf;
638
639         valid_frames = 0;
640
641         for (i = 0; i < rx_ring->count; i++) {
642                 union ice_32b_rx_flex_desc *rx_desc;
643
644                 rx_desc = ICE_RX_DESC(rx_ring, i);
645
646                 if (!(rx_desc->wb.status_error0 &
647                     cpu_to_le16(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS)))
648                         continue;
649
650                 rx_buf = &rx_ring->rx_buf[i];
651                 received_buf = page_address(rx_buf->page);
652
653                 if (ice_lbtest_check_frame(received_buf))
654                         valid_frames++;
655         }
656
657         return valid_frames;
658 }
659
660 /**
661  * ice_loopback_test - perform a loopback test on a given net_device
662  * @netdev: network interface device structure
663  *
664  * This function performs one of the self-tests required by ethtool.
665  * Returns 0 on success, non-zero on failure.
666  */
667 static u64 ice_loopback_test(struct net_device *netdev)
668 {
669         struct ice_netdev_priv *np = netdev_priv(netdev);
670         struct ice_vsi *orig_vsi = np->vsi, *test_vsi;
671         struct ice_pf *pf = orig_vsi->back;
672         struct ice_ring *tx_ring, *rx_ring;
673         u8 broadcast[ETH_ALEN], ret = 0;
674         int num_frames, valid_frames;
675         LIST_HEAD(tmp_list);
676         u8 *tx_frame;
677         int i;
678
679         netdev_info(netdev, "loopback test\n");
680
681         test_vsi = ice_lb_vsi_setup(pf, pf->hw.port_info);
682         if (!test_vsi) {
683                 netdev_err(netdev, "Failed to create a VSI for the loopback test");
684                 return 1;
685         }
686
687         test_vsi->netdev = netdev;
688         tx_ring = test_vsi->tx_rings[0];
689         rx_ring = test_vsi->rx_rings[0];
690
691         if (ice_lbtest_prepare_rings(test_vsi)) {
692                 ret = 2;
693                 goto lbtest_vsi_close;
694         }
695
696         if (ice_alloc_rx_bufs(rx_ring, rx_ring->count)) {
697                 ret = 3;
698                 goto lbtest_rings_dis;
699         }
700
701         /* Enable MAC loopback in firmware */
702         if (ice_aq_set_mac_loopback(&pf->hw, true, NULL)) {
703                 ret = 4;
704                 goto lbtest_mac_dis;
705         }
706
707         /* Test VSI needs to receive broadcast packets */
708         eth_broadcast_addr(broadcast);
709         if (ice_add_mac_to_list(test_vsi, &tmp_list, broadcast)) {
710                 ret = 5;
711                 goto lbtest_mac_dis;
712         }
713
714         if (ice_add_mac(&pf->hw, &tmp_list)) {
715                 ret = 6;
716                 goto free_mac_list;
717         }
718
719         if (ice_lbtest_create_frame(pf, &tx_frame, ICE_LB_FRAME_SIZE)) {
720                 ret = 7;
721                 goto remove_mac_filters;
722         }
723
724         num_frames = min_t(int, tx_ring->count, 32);
725         for (i = 0; i < num_frames; i++) {
726                 if (ice_diag_send(tx_ring, tx_frame, ICE_LB_FRAME_SIZE)) {
727                         ret = 8;
728                         goto lbtest_free_frame;
729                 }
730         }
731
732         valid_frames = ice_lbtest_receive_frames(rx_ring);
733         if (!valid_frames)
734                 ret = 9;
735         else if (valid_frames != num_frames)
736                 ret = 10;
737
738 lbtest_free_frame:
739         devm_kfree(&pf->pdev->dev, tx_frame);
740 remove_mac_filters:
741         if (ice_remove_mac(&pf->hw, &tmp_list))
742                 netdev_err(netdev, "Could not remove MAC filter for the test VSI");
743 free_mac_list:
744         ice_free_fltr_list(&pf->pdev->dev, &tmp_list);
745 lbtest_mac_dis:
746         /* Disable MAC loopback after the test is completed. */
747         if (ice_aq_set_mac_loopback(&pf->hw, false, NULL))
748                 netdev_err(netdev, "Could not disable MAC loopback\n");
749 lbtest_rings_dis:
750         if (ice_lbtest_disable_rings(test_vsi))
751                 netdev_err(netdev, "Could not disable test rings\n");
752 lbtest_vsi_close:
753         test_vsi->netdev = NULL;
754         if (ice_vsi_release(test_vsi))
755                 netdev_err(netdev, "Failed to remove the test VSI");
756
757         return ret;
758 }
759
760 /**
761  * ice_intr_test - perform an interrupt test on a given net_device
762  * @netdev: network interface device structure
763  *
764  * This function performs one of the self-tests required by ethtool.
765  * Returns 0 on success, non-zero on failure.
766  */
767 static u64 ice_intr_test(struct net_device *netdev)
768 {
769         struct ice_netdev_priv *np = netdev_priv(netdev);
770         struct ice_pf *pf = np->vsi->back;
771         u16 swic_old = pf->sw_int_count;
772
773         netdev_info(netdev, "interrupt test\n");
774
775         wr32(&pf->hw, GLINT_DYN_CTL(pf->oicr_idx),
776              GLINT_DYN_CTL_SW_ITR_INDX_M |
777              GLINT_DYN_CTL_INTENA_MSK_M |
778              GLINT_DYN_CTL_SWINT_TRIG_M);
779
780         usleep_range(1000, 2000);
781         return (swic_old == pf->sw_int_count);
782 }
783
784 /**
785  * ice_self_test - handler function for performing a self-test by ethtool
786  * @netdev: network interface device structure
787  * @eth_test: ethtool_test structure
788  * @data: required by ethtool.self_test
789  *
790  * This function is called after invoking 'ethtool -t devname' command where
791  * devname is the name of the network device on which ethtool should operate.
792  * It performs a set of self-tests to check if a device works properly.
793  */
794 static void
795 ice_self_test(struct net_device *netdev, struct ethtool_test *eth_test,
796               u64 *data)
797 {
798         struct ice_netdev_priv *np = netdev_priv(netdev);
799         bool if_running = netif_running(netdev);
800         struct ice_pf *pf = np->vsi->back;
801
802         if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
803                 netdev_info(netdev, "offline testing starting\n");
804
805                 set_bit(__ICE_TESTING, pf->state);
806
807                 if (ice_active_vfs(pf)) {
808                         dev_warn(&pf->pdev->dev,
809                                  "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
810                         data[ICE_ETH_TEST_REG] = 1;
811                         data[ICE_ETH_TEST_EEPROM] = 1;
812                         data[ICE_ETH_TEST_INTR] = 1;
813                         data[ICE_ETH_TEST_LOOP] = 1;
814                         data[ICE_ETH_TEST_LINK] = 1;
815                         eth_test->flags |= ETH_TEST_FL_FAILED;
816                         clear_bit(__ICE_TESTING, pf->state);
817                         goto skip_ol_tests;
818                 }
819                 /* If the device is online then take it offline */
820                 if (if_running)
821                         /* indicate we're in test mode */
822                         ice_stop(netdev);
823
824                 data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
825                 data[ICE_ETH_TEST_EEPROM] = ice_eeprom_test(netdev);
826                 data[ICE_ETH_TEST_INTR] = ice_intr_test(netdev);
827                 data[ICE_ETH_TEST_LOOP] = ice_loopback_test(netdev);
828                 data[ICE_ETH_TEST_REG] = ice_reg_test(netdev);
829
830                 if (data[ICE_ETH_TEST_LINK] ||
831                     data[ICE_ETH_TEST_EEPROM] ||
832                     data[ICE_ETH_TEST_LOOP] ||
833                     data[ICE_ETH_TEST_INTR] ||
834                     data[ICE_ETH_TEST_REG])
835                         eth_test->flags |= ETH_TEST_FL_FAILED;
836
837                 clear_bit(__ICE_TESTING, pf->state);
838
839                 if (if_running) {
840                         int status = ice_open(netdev);
841
842                         if (status) {
843                                 dev_err(&pf->pdev->dev,
844                                         "Could not open device %s, err %d",
845                                         pf->int_name, status);
846                         }
847                 }
848         } else {
849                 /* Online tests */
850                 netdev_info(netdev, "online testing starting\n");
851
852                 data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
853                 if (data[ICE_ETH_TEST_LINK])
854                         eth_test->flags |= ETH_TEST_FL_FAILED;
855
856                 /* Offline only tests, not run in online; pass by default */
857                 data[ICE_ETH_TEST_REG] = 0;
858                 data[ICE_ETH_TEST_EEPROM] = 0;
859                 data[ICE_ETH_TEST_INTR] = 0;
860                 data[ICE_ETH_TEST_LOOP] = 0;
861         }
862
863 skip_ol_tests:
864         netdev_info(netdev, "testing finished\n");
865 }
866
867 static void ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
868 {
869         struct ice_netdev_priv *np = netdev_priv(netdev);
870         struct ice_vsi *vsi = np->vsi;
871         char *p = (char *)data;
872         unsigned int i;
873
874         switch (stringset) {
875         case ETH_SS_STATS:
876                 for (i = 0; i < ICE_VSI_STATS_LEN; i++) {
877                         snprintf(p, ETH_GSTRING_LEN, "%s",
878                                  ice_gstrings_vsi_stats[i].stat_string);
879                         p += ETH_GSTRING_LEN;
880                 }
881
882                 ice_for_each_alloc_txq(vsi, i) {
883                         snprintf(p, ETH_GSTRING_LEN,
884                                  "tx_queue_%u_packets", i);
885                         p += ETH_GSTRING_LEN;
886                         snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_bytes", i);
887                         p += ETH_GSTRING_LEN;
888                 }
889
890                 ice_for_each_alloc_rxq(vsi, i) {
891                         snprintf(p, ETH_GSTRING_LEN,
892                                  "rx_queue_%u_packets", i);
893                         p += ETH_GSTRING_LEN;
894                         snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_bytes", i);
895                         p += ETH_GSTRING_LEN;
896                 }
897
898                 if (vsi->type != ICE_VSI_PF)
899                         return;
900
901                 for (i = 0; i < ICE_PF_STATS_LEN; i++) {
902                         snprintf(p, ETH_GSTRING_LEN, "%s",
903                                  ice_gstrings_pf_stats[i].stat_string);
904                         p += ETH_GSTRING_LEN;
905                 }
906
907                 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
908                         snprintf(p, ETH_GSTRING_LEN,
909                                  "tx_priority_%u_xon.nic", i);
910                         p += ETH_GSTRING_LEN;
911                         snprintf(p, ETH_GSTRING_LEN,
912                                  "tx_priority_%u_xoff.nic", i);
913                         p += ETH_GSTRING_LEN;
914                 }
915                 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
916                         snprintf(p, ETH_GSTRING_LEN,
917                                  "rx_priority_%u_xon.nic", i);
918                         p += ETH_GSTRING_LEN;
919                         snprintf(p, ETH_GSTRING_LEN,
920                                  "rx_priority_%u_xoff.nic", i);
921                         p += ETH_GSTRING_LEN;
922                 }
923                 break;
924         case ETH_SS_TEST:
925                 memcpy(data, ice_gstrings_test, ICE_TEST_LEN * ETH_GSTRING_LEN);
926                 break;
927         case ETH_SS_PRIV_FLAGS:
928                 for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
929                         snprintf(p, ETH_GSTRING_LEN, "%s",
930                                  ice_gstrings_priv_flags[i].name);
931                         p += ETH_GSTRING_LEN;
932                 }
933                 break;
934         default:
935                 break;
936         }
937 }
938
939 static int
940 ice_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
941 {
942         struct ice_netdev_priv *np = netdev_priv(netdev);
943         bool led_active;
944
945         switch (state) {
946         case ETHTOOL_ID_ACTIVE:
947                 led_active = true;
948                 break;
949         case ETHTOOL_ID_INACTIVE:
950                 led_active = false;
951                 break;
952         default:
953                 return -EINVAL;
954         }
955
956         if (ice_aq_set_port_id_led(np->vsi->port_info, !led_active, NULL))
957                 return -EIO;
958
959         return 0;
960 }
961
962 /**
963  * ice_set_fec_cfg - Set link FEC options
964  * @netdev: network interface device structure
965  * @req_fec: FEC mode to configure
966  */
967 static int ice_set_fec_cfg(struct net_device *netdev, enum ice_fec_mode req_fec)
968 {
969         struct ice_netdev_priv *np = netdev_priv(netdev);
970         struct ice_aqc_set_phy_cfg_data config = { 0 };
971         struct ice_aqc_get_phy_caps_data *caps;
972         struct ice_vsi *vsi = np->vsi;
973         u8 sw_cfg_caps, sw_cfg_fec;
974         struct ice_port_info *pi;
975         enum ice_status status;
976         int err = 0;
977
978         pi = vsi->port_info;
979         if (!pi)
980                 return -EOPNOTSUPP;
981
982         /* Changing the FEC parameters is not supported if not the PF VSI */
983         if (vsi->type != ICE_VSI_PF) {
984                 netdev_info(netdev, "Changing FEC parameters only supported for PF VSI\n");
985                 return -EOPNOTSUPP;
986         }
987
988         /* Get last SW configuration */
989         caps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*caps), GFP_KERNEL);
990         if (!caps)
991                 return -ENOMEM;
992
993         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG,
994                                      caps, NULL);
995         if (status) {
996                 err = -EAGAIN;
997                 goto done;
998         }
999
1000         /* Copy SW configuration returned from PHY caps to PHY config */
1001         ice_copy_phy_caps_to_cfg(caps, &config);
1002         sw_cfg_caps = caps->caps;
1003         sw_cfg_fec = caps->link_fec_options;
1004
1005         /* Get toloplogy caps, then copy PHY FEC topoloy caps to PHY config */
1006         memset(caps, 0, sizeof(*caps));
1007
1008         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP,
1009                                      caps, NULL);
1010         if (status) {
1011                 err = -EAGAIN;
1012                 goto done;
1013         }
1014
1015         config.caps |= (caps->caps & ICE_AQC_PHY_EN_AUTO_FEC);
1016         config.link_fec_opt = caps->link_fec_options;
1017
1018         ice_cfg_phy_fec(&config, req_fec);
1019
1020         /* If FEC mode has changed, then set PHY configuration and enable AN. */
1021         if ((config.caps & ICE_AQ_PHY_ENA_AUTO_FEC) !=
1022             (sw_cfg_caps & ICE_AQC_PHY_EN_AUTO_FEC) ||
1023             config.link_fec_opt != sw_cfg_fec) {
1024                 if (caps->caps & ICE_AQC_PHY_AN_MODE)
1025                         config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1026
1027                 status = ice_aq_set_phy_cfg(pi->hw, pi->lport, &config, NULL);
1028
1029                 if (status)
1030                         err = -EAGAIN;
1031         }
1032
1033 done:
1034         devm_kfree(&vsi->back->pdev->dev, caps);
1035         return err;
1036 }
1037
1038 /**
1039  * ice_set_fecparam - Set FEC link options
1040  * @netdev: network interface device structure
1041  * @fecparam: Ethtool structure to retrieve FEC parameters
1042  */
1043 static int
1044 ice_set_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
1045 {
1046         struct ice_netdev_priv *np = netdev_priv(netdev);
1047         struct ice_vsi *vsi = np->vsi;
1048         enum ice_fec_mode fec;
1049
1050         switch (fecparam->fec) {
1051         case ETHTOOL_FEC_AUTO:
1052                 fec = ICE_FEC_AUTO;
1053                 break;
1054         case ETHTOOL_FEC_RS:
1055                 fec = ICE_FEC_RS;
1056                 break;
1057         case ETHTOOL_FEC_BASER:
1058                 fec = ICE_FEC_BASER;
1059                 break;
1060         case ETHTOOL_FEC_OFF:
1061         case ETHTOOL_FEC_NONE:
1062                 fec = ICE_FEC_NONE;
1063                 break;
1064         default:
1065                 dev_warn(&vsi->back->pdev->dev, "Unsupported FEC mode: %d\n",
1066                          fecparam->fec);
1067                 return -EINVAL;
1068         }
1069
1070         return ice_set_fec_cfg(netdev, fec);
1071 }
1072
1073 /**
1074  * ice_get_fecparam - Get link FEC options
1075  * @netdev: network interface device structure
1076  * @fecparam: Ethtool structure to retrieve FEC parameters
1077  */
1078 static int
1079 ice_get_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
1080 {
1081         struct ice_netdev_priv *np = netdev_priv(netdev);
1082         struct ice_aqc_get_phy_caps_data *caps;
1083         struct ice_link_status *link_info;
1084         struct ice_vsi *vsi = np->vsi;
1085         struct ice_port_info *pi;
1086         enum ice_status status;
1087         int err = 0;
1088
1089         pi = vsi->port_info;
1090
1091         if (!pi)
1092                 return -EOPNOTSUPP;
1093         link_info = &pi->phy.link_info;
1094
1095         /* Set FEC mode based on negotiated link info */
1096         switch (link_info->fec_info) {
1097         case ICE_AQ_LINK_25G_KR_FEC_EN:
1098                 fecparam->active_fec = ETHTOOL_FEC_BASER;
1099                 break;
1100         case ICE_AQ_LINK_25G_RS_528_FEC_EN:
1101                 /* fall through */
1102         case ICE_AQ_LINK_25G_RS_544_FEC_EN:
1103                 fecparam->active_fec = ETHTOOL_FEC_RS;
1104                 break;
1105         default:
1106                 fecparam->active_fec = ETHTOOL_FEC_OFF;
1107                 break;
1108         }
1109
1110         caps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*caps), GFP_KERNEL);
1111         if (!caps)
1112                 return -ENOMEM;
1113
1114         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP,
1115                                      caps, NULL);
1116         if (status) {
1117                 err = -EAGAIN;
1118                 goto done;
1119         }
1120
1121         /* Set supported/configured FEC modes based on PHY capability */
1122         if (caps->caps & ICE_AQC_PHY_EN_AUTO_FEC)
1123                 fecparam->fec |= ETHTOOL_FEC_AUTO;
1124         if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
1125             caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
1126             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN ||
1127             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
1128                 fecparam->fec |= ETHTOOL_FEC_BASER;
1129         if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
1130             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ ||
1131             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
1132                 fecparam->fec |= ETHTOOL_FEC_RS;
1133         if (caps->link_fec_options == 0)
1134                 fecparam->fec |= ETHTOOL_FEC_OFF;
1135
1136 done:
1137         devm_kfree(&vsi->back->pdev->dev, caps);
1138         return err;
1139 }
1140
1141 /**
1142  * ice_get_priv_flags - report device private flags
1143  * @netdev: network interface device structure
1144  *
1145  * The get string set count and the string set should be matched for each
1146  * flag returned.  Add new strings for each flag to the ice_gstrings_priv_flags
1147  * array.
1148  *
1149  * Returns a u32 bitmap of flags.
1150  */
1151 static u32 ice_get_priv_flags(struct net_device *netdev)
1152 {
1153         struct ice_netdev_priv *np = netdev_priv(netdev);
1154         struct ice_vsi *vsi = np->vsi;
1155         struct ice_pf *pf = vsi->back;
1156         u32 i, ret_flags = 0;
1157
1158         for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
1159                 const struct ice_priv_flag *priv_flag;
1160
1161                 priv_flag = &ice_gstrings_priv_flags[i];
1162
1163                 if (test_bit(priv_flag->bitno, pf->flags))
1164                         ret_flags |= BIT(i);
1165         }
1166
1167         return ret_flags;
1168 }
1169
1170 /**
1171  * ice_set_priv_flags - set private flags
1172  * @netdev: network interface device structure
1173  * @flags: bit flags to be set
1174  */
1175 static int ice_set_priv_flags(struct net_device *netdev, u32 flags)
1176 {
1177         struct ice_netdev_priv *np = netdev_priv(netdev);
1178         DECLARE_BITMAP(change_flags, ICE_PF_FLAGS_NBITS);
1179         DECLARE_BITMAP(orig_flags, ICE_PF_FLAGS_NBITS);
1180         struct ice_vsi *vsi = np->vsi;
1181         struct ice_pf *pf = vsi->back;
1182         int ret = 0;
1183         u32 i;
1184
1185         if (flags > BIT(ICE_PRIV_FLAG_ARRAY_SIZE))
1186                 return -EINVAL;
1187
1188         set_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
1189
1190         bitmap_copy(orig_flags, pf->flags, ICE_PF_FLAGS_NBITS);
1191         for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
1192                 const struct ice_priv_flag *priv_flag;
1193
1194                 priv_flag = &ice_gstrings_priv_flags[i];
1195
1196                 if (flags & BIT(i))
1197                         set_bit(priv_flag->bitno, pf->flags);
1198                 else
1199                         clear_bit(priv_flag->bitno, pf->flags);
1200         }
1201
1202         bitmap_xor(change_flags, pf->flags, orig_flags, ICE_PF_FLAGS_NBITS);
1203
1204         if (test_bit(ICE_FLAG_FW_LLDP_AGENT, change_flags)) {
1205                 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags)) {
1206                         enum ice_status status;
1207
1208                         /* Disable FW LLDP engine */
1209                         status = ice_aq_cfg_lldp_mib_change(&pf->hw, false,
1210                                                             NULL);
1211                         /* If unregistering for LLDP events fails, this is
1212                          * not an error state, as there shouldn't be any
1213                          * events to respond to.
1214                          */
1215                         if (status)
1216                                 dev_info(&pf->pdev->dev,
1217                                          "Failed to unreg for LLDP events\n");
1218
1219                         /* The AQ call to stop the FW LLDP agent will generate
1220                          * an error if the agent is already stopped.
1221                          */
1222                         status = ice_aq_stop_lldp(&pf->hw, true, true, NULL);
1223                         if (status)
1224                                 dev_warn(&pf->pdev->dev,
1225                                          "Fail to stop LLDP agent\n");
1226                         /* Use case for having the FW LLDP agent stopped
1227                          * will likely not need DCB, so failure to init is
1228                          * not a concern of ethtool
1229                          */
1230                         status = ice_init_pf_dcb(pf, true);
1231                         if (status)
1232                                 dev_warn(&pf->pdev->dev, "Fail to init DCB\n");
1233
1234                         /* Forward LLDP packets to default VSI so that they
1235                          * are passed up the stack
1236                          */
1237                         ice_cfg_sw_lldp(vsi, false, true);
1238                 } else {
1239                         enum ice_status status;
1240                         bool dcbx_agent_status;
1241
1242                         /* AQ command to start FW LLDP agent will return an
1243                          * error if the agent is already started
1244                          */
1245                         status = ice_aq_start_lldp(&pf->hw, true, NULL);
1246                         if (status)
1247                                 dev_warn(&pf->pdev->dev,
1248                                          "Fail to start LLDP Agent\n");
1249
1250                         /* AQ command to start FW DCBX agent will fail if
1251                          * the agent is already started
1252                          */
1253                         status = ice_aq_start_stop_dcbx(&pf->hw, true,
1254                                                         &dcbx_agent_status,
1255                                                         NULL);
1256                         if (status)
1257                                 dev_dbg(&pf->pdev->dev,
1258                                         "Failed to start FW DCBX\n");
1259
1260                         dev_info(&pf->pdev->dev, "FW DCBX agent is %s\n",
1261                                  dcbx_agent_status ? "ACTIVE" : "DISABLED");
1262
1263                         /* Failure to configure MIB change or init DCB is not
1264                          * relevant to ethtool.  Print notification that
1265                          * registration/init failed but do not return error
1266                          * state to ethtool
1267                          */
1268                         status = ice_init_pf_dcb(pf, true);
1269                         if (status)
1270                                 dev_dbg(&pf->pdev->dev, "Fail to init DCB\n");
1271
1272                         /* Remove rule to direct LLDP packets to default VSI.
1273                          * The FW LLDP engine will now be consuming them.
1274                          */
1275                         ice_cfg_sw_lldp(vsi, false, false);
1276                 }
1277         }
1278         clear_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
1279         return ret;
1280 }
1281
1282 static int ice_get_sset_count(struct net_device *netdev, int sset)
1283 {
1284         switch (sset) {
1285         case ETH_SS_STATS:
1286                 /* The number (and order) of strings reported *must* remain
1287                  * constant for a given netdevice. This function must not
1288                  * report a different number based on run time parameters
1289                  * (such as the number of queues in use, or the setting of
1290                  * a private ethtool flag). This is due to the nature of the
1291                  * ethtool stats API.
1292                  *
1293                  * Userspace programs such as ethtool must make 3 separate
1294                  * ioctl requests, one for size, one for the strings, and
1295                  * finally one for the stats. Since these cross into
1296                  * userspace, changes to the number or size could result in
1297                  * undefined memory access or incorrect string<->value
1298                  * correlations for statistics.
1299                  *
1300                  * Even if it appears to be safe, changes to the size or
1301                  * order of strings will suffer from race conditions and are
1302                  * not safe.
1303                  */
1304                 return ICE_ALL_STATS_LEN(netdev);
1305         case ETH_SS_TEST:
1306                 return ICE_TEST_LEN;
1307         case ETH_SS_PRIV_FLAGS:
1308                 return ICE_PRIV_FLAG_ARRAY_SIZE;
1309         default:
1310                 return -EOPNOTSUPP;
1311         }
1312 }
1313
1314 static void
1315 ice_get_ethtool_stats(struct net_device *netdev,
1316                       struct ethtool_stats __always_unused *stats, u64 *data)
1317 {
1318         struct ice_netdev_priv *np = netdev_priv(netdev);
1319         struct ice_vsi *vsi = np->vsi;
1320         struct ice_pf *pf = vsi->back;
1321         struct ice_ring *ring;
1322         unsigned int j;
1323         int i = 0;
1324         char *p;
1325
1326         ice_update_pf_stats(pf);
1327         ice_update_vsi_stats(vsi);
1328
1329         for (j = 0; j < ICE_VSI_STATS_LEN; j++) {
1330                 p = (char *)vsi + ice_gstrings_vsi_stats[j].stat_offset;
1331                 data[i++] = (ice_gstrings_vsi_stats[j].sizeof_stat ==
1332                              sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1333         }
1334
1335         /* populate per queue stats */
1336         rcu_read_lock();
1337
1338         ice_for_each_alloc_txq(vsi, j) {
1339                 ring = READ_ONCE(vsi->tx_rings[j]);
1340                 if (ring) {
1341                         data[i++] = ring->stats.pkts;
1342                         data[i++] = ring->stats.bytes;
1343                 } else {
1344                         data[i++] = 0;
1345                         data[i++] = 0;
1346                 }
1347         }
1348
1349         ice_for_each_alloc_rxq(vsi, j) {
1350                 ring = READ_ONCE(vsi->rx_rings[j]);
1351                 if (ring) {
1352                         data[i++] = ring->stats.pkts;
1353                         data[i++] = ring->stats.bytes;
1354                 } else {
1355                         data[i++] = 0;
1356                         data[i++] = 0;
1357                 }
1358         }
1359
1360         rcu_read_unlock();
1361
1362         if (vsi->type != ICE_VSI_PF)
1363                 return;
1364
1365         for (j = 0; j < ICE_PF_STATS_LEN; j++) {
1366                 p = (char *)pf + ice_gstrings_pf_stats[j].stat_offset;
1367                 data[i++] = (ice_gstrings_pf_stats[j].sizeof_stat ==
1368                              sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1369         }
1370
1371         for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
1372                 data[i++] = pf->stats.priority_xon_tx[j];
1373                 data[i++] = pf->stats.priority_xoff_tx[j];
1374         }
1375
1376         for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
1377                 data[i++] = pf->stats.priority_xon_rx[j];
1378                 data[i++] = pf->stats.priority_xoff_rx[j];
1379         }
1380 }
1381
1382 /**
1383  * ice_phy_type_to_ethtool - convert the phy_types to ethtool link modes
1384  * @netdev: network interface device structure
1385  * @ks: ethtool link ksettings struct to fill out
1386  */
1387 static void
1388 ice_phy_type_to_ethtool(struct net_device *netdev,
1389                         struct ethtool_link_ksettings *ks)
1390 {
1391         struct ice_netdev_priv *np = netdev_priv(netdev);
1392         struct ice_link_status *hw_link_info;
1393         bool need_add_adv_mode = false;
1394         struct ice_vsi *vsi = np->vsi;
1395         u64 phy_types_high;
1396         u64 phy_types_low;
1397
1398         hw_link_info = &vsi->port_info->phy.link_info;
1399         phy_types_low = vsi->port_info->phy.phy_type_low;
1400         phy_types_high = vsi->port_info->phy.phy_type_high;
1401
1402         ethtool_link_ksettings_zero_link_mode(ks, supported);
1403         ethtool_link_ksettings_zero_link_mode(ks, advertising);
1404
1405         if (phy_types_low & ICE_PHY_TYPE_LOW_100BASE_TX ||
1406             phy_types_low & ICE_PHY_TYPE_LOW_100M_SGMII) {
1407                 ethtool_link_ksettings_add_link_mode(ks, supported,
1408                                                      100baseT_Full);
1409                 if (!hw_link_info->req_speeds ||
1410                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100MB)
1411                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1412                                                              100baseT_Full);
1413         }
1414         if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_T ||
1415             phy_types_low & ICE_PHY_TYPE_LOW_1G_SGMII) {
1416                 ethtool_link_ksettings_add_link_mode(ks, supported,
1417                                                      1000baseT_Full);
1418                 if (!hw_link_info->req_speeds ||
1419                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
1420                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1421                                                              1000baseT_Full);
1422         }
1423         if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_KX) {
1424                 ethtool_link_ksettings_add_link_mode(ks, supported,
1425                                                      1000baseKX_Full);
1426                 if (!hw_link_info->req_speeds ||
1427                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
1428                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1429                                                              1000baseKX_Full);
1430         }
1431         if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_SX ||
1432             phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_LX) {
1433                 ethtool_link_ksettings_add_link_mode(ks, supported,
1434                                                      1000baseX_Full);
1435                 if (!hw_link_info->req_speeds ||
1436                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
1437                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1438                                                              1000baseX_Full);
1439         }
1440         if (phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_T) {
1441                 ethtool_link_ksettings_add_link_mode(ks, supported,
1442                                                      2500baseT_Full);
1443                 if (!hw_link_info->req_speeds ||
1444                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_2500MB)
1445                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1446                                                              2500baseT_Full);
1447         }
1448         if (phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_X ||
1449             phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_KX) {
1450                 ethtool_link_ksettings_add_link_mode(ks, supported,
1451                                                      2500baseX_Full);
1452                 if (!hw_link_info->req_speeds ||
1453                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_2500MB)
1454                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1455                                                              2500baseX_Full);
1456         }
1457         if (phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_T ||
1458             phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_KR) {
1459                 ethtool_link_ksettings_add_link_mode(ks, supported,
1460                                                      5000baseT_Full);
1461                 if (!hw_link_info->req_speeds ||
1462                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_5GB)
1463                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1464                                                              5000baseT_Full);
1465         }
1466         if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_T ||
1467             phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_DA ||
1468             phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC ||
1469             phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_C2C) {
1470                 ethtool_link_ksettings_add_link_mode(ks, supported,
1471                                                      10000baseT_Full);
1472                 if (!hw_link_info->req_speeds ||
1473                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
1474                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1475                                                              10000baseT_Full);
1476         }
1477         if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_KR_CR1) {
1478                 ethtool_link_ksettings_add_link_mode(ks, supported,
1479                                                      10000baseKR_Full);
1480                 if (!hw_link_info->req_speeds ||
1481                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
1482                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1483                                                              10000baseKR_Full);
1484         }
1485         if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_SR) {
1486                 ethtool_link_ksettings_add_link_mode(ks, supported,
1487                                                      10000baseSR_Full);
1488                 if (!hw_link_info->req_speeds ||
1489                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
1490                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1491                                                              10000baseSR_Full);
1492         }
1493         if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_LR) {
1494                 ethtool_link_ksettings_add_link_mode(ks, supported,
1495                                                      10000baseLR_Full);
1496                 if (!hw_link_info->req_speeds ||
1497                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
1498                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1499                                                              10000baseLR_Full);
1500         }
1501         if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_T ||
1502             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR ||
1503             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR_S ||
1504             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR1 ||
1505             phy_types_low & ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC ||
1506             phy_types_low & ICE_PHY_TYPE_LOW_25G_AUI_C2C) {
1507                 ethtool_link_ksettings_add_link_mode(ks, supported,
1508                                                      25000baseCR_Full);
1509                 if (!hw_link_info->req_speeds ||
1510                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
1511                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1512                                                              25000baseCR_Full);
1513         }
1514         if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_SR ||
1515             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_LR) {
1516                 ethtool_link_ksettings_add_link_mode(ks, supported,
1517                                                      25000baseSR_Full);
1518                 if (!hw_link_info->req_speeds ||
1519                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
1520                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1521                                                              25000baseSR_Full);
1522         }
1523         if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR ||
1524             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR_S ||
1525             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR1) {
1526                 ethtool_link_ksettings_add_link_mode(ks, supported,
1527                                                      25000baseKR_Full);
1528                 if (!hw_link_info->req_speeds ||
1529                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
1530                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1531                                                              25000baseKR_Full);
1532         }
1533         if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_KR4) {
1534                 ethtool_link_ksettings_add_link_mode(ks, supported,
1535                                                      40000baseKR4_Full);
1536                 if (!hw_link_info->req_speeds ||
1537                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
1538                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1539                                                              40000baseKR4_Full);
1540         }
1541         if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_CR4 ||
1542             phy_types_low & ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC ||
1543             phy_types_low & ICE_PHY_TYPE_LOW_40G_XLAUI) {
1544                 ethtool_link_ksettings_add_link_mode(ks, supported,
1545                                                      40000baseCR4_Full);
1546                 if (!hw_link_info->req_speeds ||
1547                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
1548                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1549                                                              40000baseCR4_Full);
1550         }
1551         if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_SR4) {
1552                 ethtool_link_ksettings_add_link_mode(ks, supported,
1553                                                      40000baseSR4_Full);
1554                 if (!hw_link_info->req_speeds ||
1555                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
1556                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1557                                                              40000baseSR4_Full);
1558         }
1559         if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_LR4) {
1560                 ethtool_link_ksettings_add_link_mode(ks, supported,
1561                                                      40000baseLR4_Full);
1562                 if (!hw_link_info->req_speeds ||
1563                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
1564                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1565                                                              40000baseLR4_Full);
1566         }
1567         if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CR2 ||
1568             phy_types_low & ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC ||
1569             phy_types_low & ICE_PHY_TYPE_LOW_50G_LAUI2 ||
1570             phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC ||
1571             phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI2 ||
1572             phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CP ||
1573             phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_SR ||
1574             phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC ||
1575             phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI1) {
1576                 ethtool_link_ksettings_add_link_mode(ks, supported,
1577                                                      50000baseCR2_Full);
1578                 if (!hw_link_info->req_speeds ||
1579                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB)
1580                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1581                                                              50000baseCR2_Full);
1582         }
1583         if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR2 ||
1584             phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4) {
1585                 ethtool_link_ksettings_add_link_mode(ks, supported,
1586                                                      50000baseKR2_Full);
1587                 if (!hw_link_info->req_speeds ||
1588                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB)
1589                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1590                                                              50000baseKR2_Full);
1591         }
1592         if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_SR2 ||
1593             phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_LR2 ||
1594             phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_FR ||
1595             phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_LR) {
1596                 ethtool_link_ksettings_add_link_mode(ks, supported,
1597                                                      50000baseSR2_Full);
1598                 if (!hw_link_info->req_speeds ||
1599                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB)
1600                         ethtool_link_ksettings_add_link_mode(ks, advertising,
1601                                                              50000baseSR2_Full);
1602         }
1603         if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR4 ||
1604             phy_types_low & ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC ||
1605             phy_types_low & ICE_PHY_TYPE_LOW_100G_CAUI4 ||
1606             phy_types_low & ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC ||
1607             phy_types_low & ICE_PHY_TYPE_LOW_100G_AUI4 ||
1608             phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 ||
1609             phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CP2  ||
1610             phy_types_high & ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC ||
1611             phy_types_high & ICE_PHY_TYPE_HIGH_100G_CAUI2 ||
1612             phy_types_high & ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC ||
1613             phy_types_high & ICE_PHY_TYPE_HIGH_100G_AUI2) {
1614                 ethtool_link_ksettings_add_link_mode(ks, supported,
1615                                                      100000baseCR4_Full);
1616                 if (!hw_link_info->req_speeds ||
1617                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
1618                         need_add_adv_mode = true;
1619         }
1620         if (need_add_adv_mode) {
1621                 need_add_adv_mode = false;
1622                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1623                                                      100000baseCR4_Full);
1624         }
1625         if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_SR4 ||
1626             phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_SR2) {
1627                 ethtool_link_ksettings_add_link_mode(ks, supported,
1628                                                      100000baseSR4_Full);
1629                 if (!hw_link_info->req_speeds ||
1630                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
1631                         need_add_adv_mode = true;
1632         }
1633         if (need_add_adv_mode) {
1634                 need_add_adv_mode = false;
1635                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1636                                                      100000baseSR4_Full);
1637         }
1638         if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_LR4 ||
1639             phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_DR) {
1640                 ethtool_link_ksettings_add_link_mode(ks, supported,
1641                                                      100000baseLR4_ER4_Full);
1642                 if (!hw_link_info->req_speeds ||
1643                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
1644                         need_add_adv_mode = true;
1645         }
1646         if (need_add_adv_mode) {
1647                 need_add_adv_mode = false;
1648                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1649                                                      100000baseLR4_ER4_Full);
1650         }
1651         if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR4 ||
1652             phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 ||
1653             phy_types_high & ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4) {
1654                 ethtool_link_ksettings_add_link_mode(ks, supported,
1655                                                      100000baseKR4_Full);
1656                 if (!hw_link_info->req_speeds ||
1657                     hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB)
1658                         need_add_adv_mode = true;
1659         }
1660         if (need_add_adv_mode)
1661                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1662                                                      100000baseKR4_Full);
1663
1664         /* Autoneg PHY types */
1665         if (phy_types_low & ICE_PHY_TYPE_LOW_100BASE_TX ||
1666             phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_T ||
1667             phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_KX ||
1668             phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_T ||
1669             phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_KX ||
1670             phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_T ||
1671             phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_KR ||
1672             phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_T ||
1673             phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_KR_CR1 ||
1674             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_T ||
1675             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR ||
1676             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR_S ||
1677             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR1 ||
1678             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR ||
1679             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR_S ||
1680             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR1 ||
1681             phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_CR4 ||
1682             phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_KR4) {
1683                 ethtool_link_ksettings_add_link_mode(ks, supported,
1684                                                      Autoneg);
1685                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1686                                                      Autoneg);
1687         }
1688         if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CR2 ||
1689             phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR2 ||
1690             phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CP ||
1691             phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4) {
1692                 ethtool_link_ksettings_add_link_mode(ks, supported,
1693                                                      Autoneg);
1694                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1695                                                      Autoneg);
1696         }
1697         if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR4 ||
1698             phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR4 ||
1699             phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 ||
1700             phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CP2) {
1701                 ethtool_link_ksettings_add_link_mode(ks, supported,
1702                                                      Autoneg);
1703                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1704                                                      Autoneg);
1705         }
1706 }
1707
1708 #define TEST_SET_BITS_TIMEOUT   50
1709 #define TEST_SET_BITS_SLEEP_MAX 2000
1710 #define TEST_SET_BITS_SLEEP_MIN 1000
1711
1712 /**
1713  * ice_get_settings_link_up - Get Link settings for when link is up
1714  * @ks: ethtool ksettings to fill in
1715  * @netdev: network interface device structure
1716  */
1717 static void
1718 ice_get_settings_link_up(struct ethtool_link_ksettings *ks,
1719                          struct net_device *netdev)
1720 {
1721         struct ice_netdev_priv *np = netdev_priv(netdev);
1722         struct ice_port_info *pi = np->vsi->port_info;
1723         struct ethtool_link_ksettings cap_ksettings;
1724         struct ice_link_status *link_info;
1725         struct ice_vsi *vsi = np->vsi;
1726         bool unrecog_phy_high = false;
1727         bool unrecog_phy_low = false;
1728
1729         link_info = &vsi->port_info->phy.link_info;
1730
1731         /* Initialize supported and advertised settings based on PHY settings */
1732         switch (link_info->phy_type_low) {
1733         case ICE_PHY_TYPE_LOW_100BASE_TX:
1734                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1735                 ethtool_link_ksettings_add_link_mode(ks, supported,
1736                                                      100baseT_Full);
1737                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1738                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1739                                                      100baseT_Full);
1740                 break;
1741         case ICE_PHY_TYPE_LOW_100M_SGMII:
1742                 ethtool_link_ksettings_add_link_mode(ks, supported,
1743                                                      100baseT_Full);
1744                 break;
1745         case ICE_PHY_TYPE_LOW_1000BASE_T:
1746                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1747                 ethtool_link_ksettings_add_link_mode(ks, supported,
1748                                                      1000baseT_Full);
1749                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1750                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1751                                                      1000baseT_Full);
1752                 break;
1753         case ICE_PHY_TYPE_LOW_1G_SGMII:
1754                 ethtool_link_ksettings_add_link_mode(ks, supported,
1755                                                      1000baseT_Full);
1756                 break;
1757         case ICE_PHY_TYPE_LOW_1000BASE_SX:
1758         case ICE_PHY_TYPE_LOW_1000BASE_LX:
1759                 ethtool_link_ksettings_add_link_mode(ks, supported,
1760                                                      1000baseX_Full);
1761                 break;
1762         case ICE_PHY_TYPE_LOW_1000BASE_KX:
1763                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1764                 ethtool_link_ksettings_add_link_mode(ks, supported,
1765                                                      1000baseKX_Full);
1766                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1767                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1768                                                      1000baseKX_Full);
1769                 break;
1770         case ICE_PHY_TYPE_LOW_2500BASE_T:
1771                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1772                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1773                 ethtool_link_ksettings_add_link_mode(ks, supported,
1774                                                      2500baseT_Full);
1775                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1776                                                      2500baseT_Full);
1777                 break;
1778         case ICE_PHY_TYPE_LOW_2500BASE_X:
1779                 ethtool_link_ksettings_add_link_mode(ks, supported,
1780                                                      2500baseX_Full);
1781                 break;
1782         case ICE_PHY_TYPE_LOW_2500BASE_KX:
1783                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1784                 ethtool_link_ksettings_add_link_mode(ks, supported,
1785                                                      2500baseX_Full);
1786                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1787                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1788                                                      2500baseX_Full);
1789                 break;
1790         case ICE_PHY_TYPE_LOW_5GBASE_T:
1791         case ICE_PHY_TYPE_LOW_5GBASE_KR:
1792                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1793                 ethtool_link_ksettings_add_link_mode(ks, supported,
1794                                                      5000baseT_Full);
1795                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1796                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1797                                                      5000baseT_Full);
1798                 break;
1799         case ICE_PHY_TYPE_LOW_10GBASE_T:
1800                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1801                 ethtool_link_ksettings_add_link_mode(ks, supported,
1802                                                      10000baseT_Full);
1803                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1804                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1805                                                      10000baseT_Full);
1806                 break;
1807         case ICE_PHY_TYPE_LOW_10G_SFI_DA:
1808         case ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC:
1809         case ICE_PHY_TYPE_LOW_10G_SFI_C2C:
1810                 ethtool_link_ksettings_add_link_mode(ks, supported,
1811                                                      10000baseT_Full);
1812                 break;
1813         case ICE_PHY_TYPE_LOW_10GBASE_SR:
1814                 ethtool_link_ksettings_add_link_mode(ks, supported,
1815                                                      10000baseSR_Full);
1816                 break;
1817         case ICE_PHY_TYPE_LOW_10GBASE_LR:
1818                 ethtool_link_ksettings_add_link_mode(ks, supported,
1819                                                      10000baseLR_Full);
1820                 break;
1821         case ICE_PHY_TYPE_LOW_10GBASE_KR_CR1:
1822                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1823                 ethtool_link_ksettings_add_link_mode(ks, supported,
1824                                                      10000baseKR_Full);
1825                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1826                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1827                                                      10000baseKR_Full);
1828                 break;
1829         case ICE_PHY_TYPE_LOW_25GBASE_T:
1830         case ICE_PHY_TYPE_LOW_25GBASE_CR:
1831         case ICE_PHY_TYPE_LOW_25GBASE_CR_S:
1832         case ICE_PHY_TYPE_LOW_25GBASE_CR1:
1833                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1834                 ethtool_link_ksettings_add_link_mode(ks, supported,
1835                                                      25000baseCR_Full);
1836                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1837                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1838                                                      25000baseCR_Full);
1839                 break;
1840         case ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC:
1841         case ICE_PHY_TYPE_LOW_25G_AUI_C2C:
1842                 ethtool_link_ksettings_add_link_mode(ks, supported,
1843                                                      25000baseCR_Full);
1844                 break;
1845         case ICE_PHY_TYPE_LOW_25GBASE_SR:
1846         case ICE_PHY_TYPE_LOW_25GBASE_LR:
1847                 ethtool_link_ksettings_add_link_mode(ks, supported,
1848                                                      25000baseSR_Full);
1849                 break;
1850         case ICE_PHY_TYPE_LOW_25GBASE_KR:
1851         case ICE_PHY_TYPE_LOW_25GBASE_KR1:
1852         case ICE_PHY_TYPE_LOW_25GBASE_KR_S:
1853                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1854                 ethtool_link_ksettings_add_link_mode(ks, supported,
1855                                                      25000baseKR_Full);
1856                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1857                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1858                                                      25000baseKR_Full);
1859                 break;
1860         case ICE_PHY_TYPE_LOW_40GBASE_CR4:
1861                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1862                 ethtool_link_ksettings_add_link_mode(ks, supported,
1863                                                      40000baseCR4_Full);
1864                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1865                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1866                                                      40000baseCR4_Full);
1867                 break;
1868         case ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC:
1869         case ICE_PHY_TYPE_LOW_40G_XLAUI:
1870                 ethtool_link_ksettings_add_link_mode(ks, supported,
1871                                                      40000baseCR4_Full);
1872                 break;
1873         case ICE_PHY_TYPE_LOW_40GBASE_SR4:
1874                 ethtool_link_ksettings_add_link_mode(ks, supported,
1875                                                      40000baseSR4_Full);
1876                 break;
1877         case ICE_PHY_TYPE_LOW_40GBASE_LR4:
1878                 ethtool_link_ksettings_add_link_mode(ks, supported,
1879                                                      40000baseLR4_Full);
1880                 break;
1881         case ICE_PHY_TYPE_LOW_40GBASE_KR4:
1882                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1883                 ethtool_link_ksettings_add_link_mode(ks, supported,
1884                                                      40000baseKR4_Full);
1885                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1886                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1887                                                      40000baseKR4_Full);
1888                 break;
1889         case ICE_PHY_TYPE_LOW_50GBASE_CR2:
1890         case ICE_PHY_TYPE_LOW_50GBASE_CP:
1891                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1892                 ethtool_link_ksettings_add_link_mode(ks, supported,
1893                                                      50000baseCR2_Full);
1894                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1895                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1896                                                      50000baseCR2_Full);
1897                 break;
1898         case ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC:
1899         case ICE_PHY_TYPE_LOW_50G_LAUI2:
1900         case ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC:
1901         case ICE_PHY_TYPE_LOW_50G_AUI2:
1902         case ICE_PHY_TYPE_LOW_50GBASE_SR:
1903         case ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC:
1904         case ICE_PHY_TYPE_LOW_50G_AUI1:
1905                 ethtool_link_ksettings_add_link_mode(ks, supported,
1906                                                      50000baseCR2_Full);
1907                 break;
1908         case ICE_PHY_TYPE_LOW_50GBASE_KR2:
1909         case ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4:
1910                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1911                 ethtool_link_ksettings_add_link_mode(ks, supported,
1912                                                      50000baseKR2_Full);
1913                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1914                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1915                                                      50000baseKR2_Full);
1916                 break;
1917         case ICE_PHY_TYPE_LOW_50GBASE_SR2:
1918         case ICE_PHY_TYPE_LOW_50GBASE_LR2:
1919         case ICE_PHY_TYPE_LOW_50GBASE_FR:
1920         case ICE_PHY_TYPE_LOW_50GBASE_LR:
1921                 ethtool_link_ksettings_add_link_mode(ks, supported,
1922                                                      50000baseSR2_Full);
1923                 break;
1924         case ICE_PHY_TYPE_LOW_100GBASE_CR4:
1925                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1926                 ethtool_link_ksettings_add_link_mode(ks, supported,
1927                                                      100000baseCR4_Full);
1928                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1929                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1930                                                      100000baseCR4_Full);
1931                 break;
1932         case ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC:
1933         case ICE_PHY_TYPE_LOW_100G_CAUI4:
1934         case ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC:
1935         case ICE_PHY_TYPE_LOW_100G_AUI4:
1936         case ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4:
1937                 ethtool_link_ksettings_add_link_mode(ks, supported,
1938                                                      100000baseCR4_Full);
1939                 break;
1940         case ICE_PHY_TYPE_LOW_100GBASE_CP2:
1941                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1942                 ethtool_link_ksettings_add_link_mode(ks, supported,
1943                                                      100000baseCR4_Full);
1944                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1945                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1946                                                      100000baseCR4_Full);
1947                 break;
1948         case ICE_PHY_TYPE_LOW_100GBASE_SR4:
1949         case ICE_PHY_TYPE_LOW_100GBASE_SR2:
1950                 ethtool_link_ksettings_add_link_mode(ks, supported,
1951                                                      100000baseSR4_Full);
1952                 break;
1953         case ICE_PHY_TYPE_LOW_100GBASE_LR4:
1954         case ICE_PHY_TYPE_LOW_100GBASE_DR:
1955                 ethtool_link_ksettings_add_link_mode(ks, supported,
1956                                                      100000baseLR4_ER4_Full);
1957                 break;
1958         case ICE_PHY_TYPE_LOW_100GBASE_KR4:
1959         case ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4:
1960                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1961                 ethtool_link_ksettings_add_link_mode(ks, supported,
1962                                                      100000baseKR4_Full);
1963                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1964                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1965                                                      100000baseKR4_Full);
1966                 break;
1967         default:
1968                 unrecog_phy_low = true;
1969         }
1970
1971         switch (link_info->phy_type_high) {
1972         case ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4:
1973                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1974                 ethtool_link_ksettings_add_link_mode(ks, supported,
1975                                                      100000baseKR4_Full);
1976                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1977                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1978                                                      100000baseKR4_Full);
1979                 break;
1980         case ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC:
1981         case ICE_PHY_TYPE_HIGH_100G_CAUI2:
1982         case ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC:
1983         case ICE_PHY_TYPE_HIGH_100G_AUI2:
1984                 ethtool_link_ksettings_add_link_mode(ks, supported,
1985                                                      100000baseCR4_Full);
1986                 break;
1987         default:
1988                 unrecog_phy_high = true;
1989         }
1990
1991         if (unrecog_phy_low && unrecog_phy_high) {
1992                 /* if we got here and link is up something bad is afoot */
1993                 netdev_info(netdev,
1994                             "WARNING: Unrecognized PHY_Low (0x%llx).\n",
1995                             (u64)link_info->phy_type_low);
1996                 netdev_info(netdev,
1997                             "WARNING: Unrecognized PHY_High (0x%llx).\n",
1998                             (u64)link_info->phy_type_high);
1999         }
2000
2001         /* Now that we've worked out everything that could be supported by the
2002          * current PHY type, get what is supported by the NVM and intersect
2003          * them to get what is truly supported
2004          */
2005         memset(&cap_ksettings, 0, sizeof(cap_ksettings));
2006         ice_phy_type_to_ethtool(netdev, &cap_ksettings);
2007         ethtool_intersect_link_masks(ks, &cap_ksettings);
2008
2009         switch (link_info->link_speed) {
2010         case ICE_AQ_LINK_SPEED_100GB:
2011                 ks->base.speed = SPEED_100000;
2012                 break;
2013         case ICE_AQ_LINK_SPEED_50GB:
2014                 ks->base.speed = SPEED_50000;
2015                 break;
2016         case ICE_AQ_LINK_SPEED_40GB:
2017                 ks->base.speed = SPEED_40000;
2018                 break;
2019         case ICE_AQ_LINK_SPEED_25GB:
2020                 ks->base.speed = SPEED_25000;
2021                 break;
2022         case ICE_AQ_LINK_SPEED_20GB:
2023                 ks->base.speed = SPEED_20000;
2024                 break;
2025         case ICE_AQ_LINK_SPEED_10GB:
2026                 ks->base.speed = SPEED_10000;
2027                 break;
2028         case ICE_AQ_LINK_SPEED_5GB:
2029                 ks->base.speed = SPEED_5000;
2030                 break;
2031         case ICE_AQ_LINK_SPEED_2500MB:
2032                 ks->base.speed = SPEED_2500;
2033                 break;
2034         case ICE_AQ_LINK_SPEED_1000MB:
2035                 ks->base.speed = SPEED_1000;
2036                 break;
2037         case ICE_AQ_LINK_SPEED_100MB:
2038                 ks->base.speed = SPEED_100;
2039                 break;
2040         default:
2041                 netdev_info(netdev,
2042                             "WARNING: Unrecognized link_speed (0x%x).\n",
2043                             link_info->link_speed);
2044                 break;
2045         }
2046         ks->base.duplex = DUPLEX_FULL;
2047
2048         if (link_info->an_info & ICE_AQ_AN_COMPLETED)
2049                 ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
2050                                                      Autoneg);
2051
2052         /* Set flow control negotiated Rx/Tx pause */
2053         switch (pi->fc.current_mode) {
2054         case ICE_FC_FULL:
2055                 ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
2056                 break;
2057         case ICE_FC_TX_PAUSE:
2058                 ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
2059                 ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
2060                                                      Asym_Pause);
2061                 break;
2062         case ICE_FC_RX_PAUSE:
2063                 ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
2064                                                      Asym_Pause);
2065                 break;
2066         case ICE_FC_PFC:
2067                 /* fall through */
2068         default:
2069                 ethtool_link_ksettings_del_link_mode(ks, lp_advertising, Pause);
2070                 ethtool_link_ksettings_del_link_mode(ks, lp_advertising,
2071                                                      Asym_Pause);
2072                 break;
2073         }
2074 }
2075
2076 /**
2077  * ice_get_settings_link_down - Get the Link settings when link is down
2078  * @ks: ethtool ksettings to fill in
2079  * @netdev: network interface device structure
2080  *
2081  * Reports link settings that can be determined when link is down
2082  */
2083 static void
2084 ice_get_settings_link_down(struct ethtool_link_ksettings *ks,
2085                            struct net_device *netdev)
2086 {
2087         /* link is down and the driver needs to fall back on
2088          * supported PHY types to figure out what info to display
2089          */
2090         ice_phy_type_to_ethtool(netdev, ks);
2091
2092         /* With no link, speed and duplex are unknown */
2093         ks->base.speed = SPEED_UNKNOWN;
2094         ks->base.duplex = DUPLEX_UNKNOWN;
2095 }
2096
2097 /**
2098  * ice_get_link_ksettings - Get Link Speed and Duplex settings
2099  * @netdev: network interface device structure
2100  * @ks: ethtool ksettings
2101  *
2102  * Reports speed/duplex settings based on media_type
2103  */
2104 static int
2105 ice_get_link_ksettings(struct net_device *netdev,
2106                        struct ethtool_link_ksettings *ks)
2107 {
2108         struct ice_netdev_priv *np = netdev_priv(netdev);
2109         struct ice_aqc_get_phy_caps_data *caps;
2110         struct ice_link_status *hw_link_info;
2111         struct ice_vsi *vsi = np->vsi;
2112         enum ice_status status;
2113         int err = 0;
2114
2115         ethtool_link_ksettings_zero_link_mode(ks, supported);
2116         ethtool_link_ksettings_zero_link_mode(ks, advertising);
2117         ethtool_link_ksettings_zero_link_mode(ks, lp_advertising);
2118         hw_link_info = &vsi->port_info->phy.link_info;
2119
2120         /* set speed and duplex */
2121         if (hw_link_info->link_info & ICE_AQ_LINK_UP)
2122                 ice_get_settings_link_up(ks, netdev);
2123         else
2124                 ice_get_settings_link_down(ks, netdev);
2125
2126         /* set autoneg settings */
2127         ks->base.autoneg = (hw_link_info->an_info & ICE_AQ_AN_COMPLETED) ?
2128                 AUTONEG_ENABLE : AUTONEG_DISABLE;
2129
2130         /* set media type settings */
2131         switch (vsi->port_info->phy.media_type) {
2132         case ICE_MEDIA_FIBER:
2133                 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
2134                 ks->base.port = PORT_FIBRE;
2135                 break;
2136         case ICE_MEDIA_BASET:
2137                 ethtool_link_ksettings_add_link_mode(ks, supported, TP);
2138                 ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
2139                 ks->base.port = PORT_TP;
2140                 break;
2141         case ICE_MEDIA_BACKPLANE:
2142                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
2143                 ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
2144                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
2145                 ethtool_link_ksettings_add_link_mode(ks, advertising,
2146                                                      Backplane);
2147                 ks->base.port = PORT_NONE;
2148                 break;
2149         case ICE_MEDIA_DA:
2150                 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
2151                 ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
2152                 ks->base.port = PORT_DA;
2153                 break;
2154         default:
2155                 ks->base.port = PORT_OTHER;
2156                 break;
2157         }
2158
2159         /* flow control is symmetric and always supported */
2160         ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
2161
2162         caps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*caps), GFP_KERNEL);
2163         if (!caps)
2164                 return -ENOMEM;
2165
2166         status = ice_aq_get_phy_caps(vsi->port_info, false,
2167                                      ICE_AQC_REPORT_SW_CFG, caps, NULL);
2168         if (status) {
2169                 err = -EIO;
2170                 goto done;
2171         }
2172
2173         /* Set the advertised flow control based on the PHY capability */
2174         if ((caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) &&
2175             (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)) {
2176                 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
2177                 ethtool_link_ksettings_add_link_mode(ks, advertising,
2178                                                      Asym_Pause);
2179         } else if (caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) {
2180                 ethtool_link_ksettings_add_link_mode(ks, advertising,
2181                                                      Asym_Pause);
2182         } else if (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE) {
2183                 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
2184                 ethtool_link_ksettings_add_link_mode(ks, advertising,
2185                                                      Asym_Pause);
2186         } else {
2187                 ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
2188                 ethtool_link_ksettings_del_link_mode(ks, advertising,
2189                                                      Asym_Pause);
2190         }
2191
2192         /* Set advertised FEC modes based on PHY capability */
2193         ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_NONE);
2194
2195         if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
2196             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
2197                 ethtool_link_ksettings_add_link_mode(ks, advertising,
2198                                                      FEC_BASER);
2199         if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
2200             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
2201                 ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS);
2202
2203         status = ice_aq_get_phy_caps(vsi->port_info, false,
2204                                      ICE_AQC_REPORT_TOPO_CAP, caps, NULL);
2205         if (status) {
2206                 err = -EIO;
2207                 goto done;
2208         }
2209
2210         /* Set supported FEC modes based on PHY capability */
2211         ethtool_link_ksettings_add_link_mode(ks, supported, FEC_NONE);
2212
2213         if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
2214             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN)
2215                 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER);
2216         if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
2217                 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS);
2218
2219 done:
2220         devm_kfree(&vsi->back->pdev->dev, caps);
2221         return err;
2222 }
2223
2224 /**
2225  * ice_ksettings_find_adv_link_speed - Find advertising link speed
2226  * @ks: ethtool ksettings
2227  */
2228 static u16
2229 ice_ksettings_find_adv_link_speed(const struct ethtool_link_ksettings *ks)
2230 {
2231         u16 adv_link_speed = 0;
2232
2233         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2234                                                   100baseT_Full))
2235                 adv_link_speed |= ICE_AQ_LINK_SPEED_100MB;
2236         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2237                                                   1000baseX_Full))
2238                 adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
2239         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2240                                                   1000baseT_Full) ||
2241             ethtool_link_ksettings_test_link_mode(ks, advertising,
2242                                                   1000baseKX_Full))
2243                 adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
2244         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2245                                                   2500baseT_Full))
2246                 adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
2247         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2248                                                   2500baseX_Full))
2249                 adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
2250         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2251                                                   5000baseT_Full))
2252                 adv_link_speed |= ICE_AQ_LINK_SPEED_5GB;
2253         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2254                                                   10000baseT_Full) ||
2255             ethtool_link_ksettings_test_link_mode(ks, advertising,
2256                                                   10000baseKR_Full))
2257                 adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
2258         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2259                                                   10000baseSR_Full) ||
2260             ethtool_link_ksettings_test_link_mode(ks, advertising,
2261                                                   10000baseLR_Full))
2262                 adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
2263         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2264                                                   25000baseCR_Full) ||
2265             ethtool_link_ksettings_test_link_mode(ks, advertising,
2266                                                   25000baseSR_Full) ||
2267             ethtool_link_ksettings_test_link_mode(ks, advertising,
2268                                                   25000baseKR_Full))
2269                 adv_link_speed |= ICE_AQ_LINK_SPEED_25GB;
2270         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2271                                                   40000baseCR4_Full) ||
2272             ethtool_link_ksettings_test_link_mode(ks, advertising,
2273                                                   40000baseSR4_Full) ||
2274             ethtool_link_ksettings_test_link_mode(ks, advertising,
2275                                                   40000baseLR4_Full) ||
2276             ethtool_link_ksettings_test_link_mode(ks, advertising,
2277                                                   40000baseKR4_Full))
2278                 adv_link_speed |= ICE_AQ_LINK_SPEED_40GB;
2279         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2280                                                   50000baseCR2_Full) ||
2281             ethtool_link_ksettings_test_link_mode(ks, advertising,
2282                                                   50000baseKR2_Full))
2283                 adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
2284         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2285                                                   50000baseSR2_Full))
2286                 adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
2287         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2288                                                   100000baseCR4_Full) ||
2289             ethtool_link_ksettings_test_link_mode(ks, advertising,
2290                                                   100000baseSR4_Full) ||
2291             ethtool_link_ksettings_test_link_mode(ks, advertising,
2292                                                   100000baseLR4_ER4_Full) ||
2293             ethtool_link_ksettings_test_link_mode(ks, advertising,
2294                                                   100000baseKR4_Full))
2295                 adv_link_speed |= ICE_AQ_LINK_SPEED_100GB;
2296
2297         return adv_link_speed;
2298 }
2299
2300 /**
2301  * ice_setup_autoneg
2302  * @p: port info
2303  * @ks: ethtool_link_ksettings
2304  * @config: configuration that will be sent down to FW
2305  * @autoneg_enabled: autonegotiation is enabled or not
2306  * @autoneg_changed: will there a change in autonegotiation
2307  * @netdev: network interface device structure
2308  *
2309  * Setup PHY autonegotiation feature
2310  */
2311 static int
2312 ice_setup_autoneg(struct ice_port_info *p, struct ethtool_link_ksettings *ks,
2313                   struct ice_aqc_set_phy_cfg_data *config,
2314                   u8 autoneg_enabled, u8 *autoneg_changed,
2315                   struct net_device *netdev)
2316 {
2317         int err = 0;
2318
2319         *autoneg_changed = 0;
2320
2321         /* Check autoneg */
2322         if (autoneg_enabled == AUTONEG_ENABLE) {
2323                 /* If autoneg was not already enabled */
2324                 if (!(p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)) {
2325                         /* If autoneg is not supported, return error */
2326                         if (!ethtool_link_ksettings_test_link_mode(ks,
2327                                                                    supported,
2328                                                                    Autoneg)) {
2329                                 netdev_info(netdev, "Autoneg not supported on this phy.\n");
2330                                 err = -EINVAL;
2331                         } else {
2332                                 /* Autoneg is allowed to change */
2333                                 config->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2334                                 *autoneg_changed = 1;
2335                         }
2336                 }
2337         } else {
2338                 /* If autoneg is currently enabled */
2339                 if (p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) {
2340                         /* If autoneg is supported 10GBASE_T is the only PHY
2341                          * that can disable it, so otherwise return error
2342                          */
2343                         if (ethtool_link_ksettings_test_link_mode(ks,
2344                                                                   supported,
2345                                                                   Autoneg)) {
2346                                 netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
2347                                 err = -EINVAL;
2348                         } else {
2349                                 /* Autoneg is allowed to change */
2350                                 config->caps &= ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2351                                 *autoneg_changed = 1;
2352                         }
2353                 }
2354         }
2355
2356         return err;
2357 }
2358
2359 /**
2360  * ice_set_link_ksettings - Set Speed and Duplex
2361  * @netdev: network interface device structure
2362  * @ks: ethtool ksettings
2363  *
2364  * Set speed/duplex per media_types advertised/forced
2365  */
2366 static int
2367 ice_set_link_ksettings(struct net_device *netdev,
2368                        const struct ethtool_link_ksettings *ks)
2369 {
2370         u8 autoneg, timeout = TEST_SET_BITS_TIMEOUT, lport = 0;
2371         struct ice_netdev_priv *np = netdev_priv(netdev);
2372         struct ethtool_link_ksettings safe_ks, copy_ks;
2373         struct ice_aqc_get_phy_caps_data *abilities;
2374         u16 adv_link_speed, curr_link_speed, idx;
2375         struct ice_aqc_set_phy_cfg_data config;
2376         struct ice_pf *pf = np->vsi->back;
2377         struct ice_port_info *p;
2378         u8 autoneg_changed = 0;
2379         enum ice_status status;
2380         u64 phy_type_high;
2381         u64 phy_type_low;
2382         int err = 0;
2383         bool linkup;
2384
2385         p = np->vsi->port_info;
2386
2387         if (!p)
2388                 return -EOPNOTSUPP;
2389
2390         /* Check if this is LAN VSI */
2391         ice_for_each_vsi(pf, idx)
2392                 if (pf->vsi[idx]->type == ICE_VSI_PF) {
2393                         if (np->vsi != pf->vsi[idx])
2394                                 return -EOPNOTSUPP;
2395                         break;
2396                 }
2397
2398         if (p->phy.media_type != ICE_MEDIA_BASET &&
2399             p->phy.media_type != ICE_MEDIA_FIBER &&
2400             p->phy.media_type != ICE_MEDIA_BACKPLANE &&
2401             p->phy.media_type != ICE_MEDIA_DA &&
2402             p->phy.link_info.link_info & ICE_AQ_LINK_UP)
2403                 return -EOPNOTSUPP;
2404
2405         /* copy the ksettings to copy_ks to avoid modifying the original */
2406         memcpy(&copy_ks, ks, sizeof(copy_ks));
2407
2408         /* save autoneg out of ksettings */
2409         autoneg = copy_ks.base.autoneg;
2410
2411         memset(&safe_ks, 0, sizeof(safe_ks));
2412
2413         /* Get link modes supported by hardware.*/
2414         ice_phy_type_to_ethtool(netdev, &safe_ks);
2415
2416         /* and check against modes requested by user.
2417          * Return an error if unsupported mode was set.
2418          */
2419         if (!bitmap_subset(copy_ks.link_modes.advertising,
2420                            safe_ks.link_modes.supported,
2421                            __ETHTOOL_LINK_MODE_MASK_NBITS))
2422                 return -EINVAL;
2423
2424         /* get our own copy of the bits to check against */
2425         memset(&safe_ks, 0, sizeof(safe_ks));
2426         safe_ks.base.cmd = copy_ks.base.cmd;
2427         safe_ks.base.link_mode_masks_nwords =
2428                 copy_ks.base.link_mode_masks_nwords;
2429         ice_get_link_ksettings(netdev, &safe_ks);
2430
2431         /* set autoneg back to what it currently is */
2432         copy_ks.base.autoneg = safe_ks.base.autoneg;
2433         /* we don't compare the speed */
2434         copy_ks.base.speed = safe_ks.base.speed;
2435
2436         /* If copy_ks.base and safe_ks.base are not the same now, then they are
2437          * trying to set something that we do not support.
2438          */
2439         if (memcmp(&copy_ks.base, &safe_ks.base, sizeof(copy_ks.base)))
2440                 return -EOPNOTSUPP;
2441
2442         while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
2443                 timeout--;
2444                 if (!timeout)
2445                         return -EBUSY;
2446                 usleep_range(TEST_SET_BITS_SLEEP_MIN, TEST_SET_BITS_SLEEP_MAX);
2447         }
2448
2449         abilities = devm_kzalloc(&pf->pdev->dev, sizeof(*abilities),
2450                                  GFP_KERNEL);
2451         if (!abilities)
2452                 return -ENOMEM;
2453
2454         /* Get the current PHY config */
2455         status = ice_aq_get_phy_caps(p, false, ICE_AQC_REPORT_SW_CFG, abilities,
2456                                      NULL);
2457         if (status) {
2458                 err = -EAGAIN;
2459                 goto done;
2460         }
2461
2462         /* Copy abilities to config in case autoneg is not set below */
2463         memset(&config, 0, sizeof(config));
2464         config.caps = abilities->caps & ~ICE_AQC_PHY_AN_MODE;
2465         if (abilities->caps & ICE_AQC_PHY_AN_MODE)
2466                 config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2467
2468         /* Check autoneg */
2469         err = ice_setup_autoneg(p, &safe_ks, &config, autoneg, &autoneg_changed,
2470                                 netdev);
2471
2472         if (err)
2473                 goto done;
2474
2475         /* Call to get the current link speed */
2476         p->phy.get_link_info = true;
2477         status = ice_get_link_status(p, &linkup);
2478         if (status) {
2479                 err = -EAGAIN;
2480                 goto done;
2481         }
2482
2483         curr_link_speed = p->phy.link_info.link_speed;
2484         adv_link_speed = ice_ksettings_find_adv_link_speed(ks);
2485
2486         /* If speed didn't get set, set it to what it currently is.
2487          * This is needed because if advertise is 0 (as it is when autoneg
2488          * is disabled) then speed won't get set.
2489          */
2490         if (!adv_link_speed)
2491                 adv_link_speed = curr_link_speed;
2492
2493         /* Convert the advertise link speeds to their corresponded PHY_TYPE */
2494         ice_update_phy_type(&phy_type_low, &phy_type_high, adv_link_speed);
2495
2496         if (!autoneg_changed && adv_link_speed == curr_link_speed) {
2497                 netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
2498                 goto done;
2499         }
2500
2501         /* copy over the rest of the abilities */
2502         config.low_power_ctrl = abilities->low_power_ctrl;
2503         config.eee_cap = abilities->eee_cap;
2504         config.eeer_value = abilities->eeer_value;
2505         config.link_fec_opt = abilities->link_fec_options;
2506
2507         /* save the requested speeds */
2508         p->phy.link_info.req_speeds = adv_link_speed;
2509
2510         /* set link and auto negotiation so changes take effect */
2511         config.caps |= ICE_AQ_PHY_ENA_LINK;
2512
2513         if (phy_type_low || phy_type_high) {
2514                 config.phy_type_high = cpu_to_le64(phy_type_high) &
2515                         abilities->phy_type_high;
2516                 config.phy_type_low = cpu_to_le64(phy_type_low) &
2517                         abilities->phy_type_low;
2518         } else {
2519                 err = -EAGAIN;
2520                 netdev_info(netdev, "Nothing changed. No PHY_TYPE is corresponded to advertised link speed.\n");
2521                 goto done;
2522         }
2523
2524         /* If link is up put link down */
2525         if (p->phy.link_info.link_info & ICE_AQ_LINK_UP) {
2526                 /* Tell the OS link is going down, the link will go
2527                  * back up when fw says it is ready asynchronously
2528                  */
2529                 ice_print_link_msg(np->vsi, false);
2530                 netif_carrier_off(netdev);
2531                 netif_tx_stop_all_queues(netdev);
2532         }
2533
2534         /* make the aq call */
2535         status = ice_aq_set_phy_cfg(&pf->hw, lport, &config, NULL);
2536         if (status) {
2537                 netdev_info(netdev, "Set phy config failed,\n");
2538                 err = -EAGAIN;
2539         }
2540
2541 done:
2542         devm_kfree(&pf->pdev->dev, abilities);
2543         clear_bit(__ICE_CFG_BUSY, pf->state);
2544
2545         return err;
2546 }
2547
2548 /**
2549  * ice_get_rxnfc - command to get Rx flow classification rules
2550  * @netdev: network interface device structure
2551  * @cmd: ethtool rxnfc command
2552  * @rule_locs: buffer to rturn Rx flow classification rules
2553  *
2554  * Returns Success if the command is supported.
2555  */
2556 static int
2557 ice_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
2558               u32 __always_unused *rule_locs)
2559 {
2560         struct ice_netdev_priv *np = netdev_priv(netdev);
2561         struct ice_vsi *vsi = np->vsi;
2562         int ret = -EOPNOTSUPP;
2563
2564         switch (cmd->cmd) {
2565         case ETHTOOL_GRXRINGS:
2566                 cmd->data = vsi->rss_size;
2567                 ret = 0;
2568                 break;
2569         default:
2570                 break;
2571         }
2572
2573         return ret;
2574 }
2575
2576 static void
2577 ice_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
2578 {
2579         struct ice_netdev_priv *np = netdev_priv(netdev);
2580         struct ice_vsi *vsi = np->vsi;
2581
2582         ring->rx_max_pending = ICE_MAX_NUM_DESC;
2583         ring->tx_max_pending = ICE_MAX_NUM_DESC;
2584         ring->rx_pending = vsi->rx_rings[0]->count;
2585         ring->tx_pending = vsi->tx_rings[0]->count;
2586
2587         /* Rx mini and jumbo rings are not supported */
2588         ring->rx_mini_max_pending = 0;
2589         ring->rx_jumbo_max_pending = 0;
2590         ring->rx_mini_pending = 0;
2591         ring->rx_jumbo_pending = 0;
2592 }
2593
2594 static int
2595 ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
2596 {
2597         struct ice_ring *tx_rings = NULL, *rx_rings = NULL;
2598         struct ice_netdev_priv *np = netdev_priv(netdev);
2599         struct ice_vsi *vsi = np->vsi;
2600         struct ice_pf *pf = vsi->back;
2601         int i, timeout = 50, err = 0;
2602         u32 new_rx_cnt, new_tx_cnt;
2603
2604         if (ring->tx_pending > ICE_MAX_NUM_DESC ||
2605             ring->tx_pending < ICE_MIN_NUM_DESC ||
2606             ring->rx_pending > ICE_MAX_NUM_DESC ||
2607             ring->rx_pending < ICE_MIN_NUM_DESC) {
2608                 netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
2609                            ring->tx_pending, ring->rx_pending,
2610                            ICE_MIN_NUM_DESC, ICE_MAX_NUM_DESC,
2611                            ICE_REQ_DESC_MULTIPLE);
2612                 return -EINVAL;
2613         }
2614
2615         new_tx_cnt = ALIGN(ring->tx_pending, ICE_REQ_DESC_MULTIPLE);
2616         if (new_tx_cnt != ring->tx_pending)
2617                 netdev_info(netdev,
2618                             "Requested Tx descriptor count rounded up to %d\n",
2619                             new_tx_cnt);
2620         new_rx_cnt = ALIGN(ring->rx_pending, ICE_REQ_DESC_MULTIPLE);
2621         if (new_rx_cnt != ring->rx_pending)
2622                 netdev_info(netdev,
2623                             "Requested Rx descriptor count rounded up to %d\n",
2624                             new_rx_cnt);
2625
2626         /* if nothing to do return success */
2627         if (new_tx_cnt == vsi->tx_rings[0]->count &&
2628             new_rx_cnt == vsi->rx_rings[0]->count) {
2629                 netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
2630                 return 0;
2631         }
2632
2633         while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
2634                 timeout--;
2635                 if (!timeout)
2636                         return -EBUSY;
2637                 usleep_range(1000, 2000);
2638         }
2639
2640         /* set for the next time the netdev is started */
2641         if (!netif_running(vsi->netdev)) {
2642                 for (i = 0; i < vsi->alloc_txq; i++)
2643                         vsi->tx_rings[i]->count = new_tx_cnt;
2644                 for (i = 0; i < vsi->alloc_rxq; i++)
2645                         vsi->rx_rings[i]->count = new_rx_cnt;
2646                 netdev_dbg(netdev, "Link is down, descriptor count change happens when link is brought up\n");
2647                 goto done;
2648         }
2649
2650         if (new_tx_cnt == vsi->tx_rings[0]->count)
2651                 goto process_rx;
2652
2653         /* alloc updated Tx resources */
2654         netdev_info(netdev, "Changing Tx descriptor count from %d to %d\n",
2655                     vsi->tx_rings[0]->count, new_tx_cnt);
2656
2657         tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
2658                                 sizeof(*tx_rings), GFP_KERNEL);
2659         if (!tx_rings) {
2660                 err = -ENOMEM;
2661                 goto done;
2662         }
2663
2664         for (i = 0; i < vsi->alloc_txq; i++) {
2665                 /* clone ring and setup updated count */
2666                 tx_rings[i] = *vsi->tx_rings[i];
2667                 tx_rings[i].count = new_tx_cnt;
2668                 tx_rings[i].desc = NULL;
2669                 tx_rings[i].tx_buf = NULL;
2670                 err = ice_setup_tx_ring(&tx_rings[i]);
2671                 if (err) {
2672                         while (i) {
2673                                 i--;
2674                                 ice_clean_tx_ring(&tx_rings[i]);
2675                         }
2676                         devm_kfree(&pf->pdev->dev, tx_rings);
2677                         goto done;
2678                 }
2679         }
2680
2681 process_rx:
2682         if (new_rx_cnt == vsi->rx_rings[0]->count)
2683                 goto process_link;
2684
2685         /* alloc updated Rx resources */
2686         netdev_info(netdev, "Changing Rx descriptor count from %d to %d\n",
2687                     vsi->rx_rings[0]->count, new_rx_cnt);
2688
2689         rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
2690                                 sizeof(*rx_rings), GFP_KERNEL);
2691         if (!rx_rings) {
2692                 err = -ENOMEM;
2693                 goto done;
2694         }
2695
2696         for (i = 0; i < vsi->alloc_rxq; i++) {
2697                 /* clone ring and setup updated count */
2698                 rx_rings[i] = *vsi->rx_rings[i];
2699                 rx_rings[i].count = new_rx_cnt;
2700                 rx_rings[i].desc = NULL;
2701                 rx_rings[i].rx_buf = NULL;
2702                 /* this is to allow wr32 to have something to write to
2703                  * during early allocation of Rx buffers
2704                  */
2705                 rx_rings[i].tail = vsi->back->hw.hw_addr + PRTGEN_STATUS;
2706
2707                 err = ice_setup_rx_ring(&rx_rings[i]);
2708                 if (err)
2709                         goto rx_unwind;
2710
2711                 /* allocate Rx buffers */
2712                 err = ice_alloc_rx_bufs(&rx_rings[i],
2713                                         ICE_DESC_UNUSED(&rx_rings[i]));
2714 rx_unwind:
2715                 if (err) {
2716                         while (i) {
2717                                 i--;
2718                                 ice_free_rx_ring(&rx_rings[i]);
2719                         }
2720                         devm_kfree(&pf->pdev->dev, rx_rings);
2721                         err = -ENOMEM;
2722                         goto free_tx;
2723                 }
2724         }
2725
2726 process_link:
2727         /* Bring interface down, copy in the new ring info, then restore the
2728          * interface. if VSI is up, bring it down and then back up
2729          */
2730         if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
2731                 ice_down(vsi);
2732
2733                 if (tx_rings) {
2734                         for (i = 0; i < vsi->alloc_txq; i++) {
2735                                 ice_free_tx_ring(vsi->tx_rings[i]);
2736                                 *vsi->tx_rings[i] = tx_rings[i];
2737                         }
2738                         devm_kfree(&pf->pdev->dev, tx_rings);
2739                 }
2740
2741                 if (rx_rings) {
2742                         for (i = 0; i < vsi->alloc_rxq; i++) {
2743                                 ice_free_rx_ring(vsi->rx_rings[i]);
2744                                 /* copy the real tail offset */
2745                                 rx_rings[i].tail = vsi->rx_rings[i]->tail;
2746                                 /* this is to fake out the allocation routine
2747                                  * into thinking it has to realloc everything
2748                                  * but the recycling logic will let us re-use
2749                                  * the buffers allocated above
2750                                  */
2751                                 rx_rings[i].next_to_use = 0;
2752                                 rx_rings[i].next_to_clean = 0;
2753                                 rx_rings[i].next_to_alloc = 0;
2754                                 *vsi->rx_rings[i] = rx_rings[i];
2755                         }
2756                         devm_kfree(&pf->pdev->dev, rx_rings);
2757                 }
2758
2759                 ice_up(vsi);
2760         }
2761         goto done;
2762
2763 free_tx:
2764         /* error cleanup if the Rx allocations failed after getting Tx */
2765         if (tx_rings) {
2766                 for (i = 0; i < vsi->alloc_txq; i++)
2767                         ice_free_tx_ring(&tx_rings[i]);
2768                 devm_kfree(&pf->pdev->dev, tx_rings);
2769         }
2770
2771 done:
2772         clear_bit(__ICE_CFG_BUSY, pf->state);
2773         return err;
2774 }
2775
2776 static int ice_nway_reset(struct net_device *netdev)
2777 {
2778         /* restart autonegotiation */
2779         struct ice_netdev_priv *np = netdev_priv(netdev);
2780         struct ice_vsi *vsi = np->vsi;
2781         struct ice_port_info *pi;
2782         enum ice_status status;
2783
2784         pi = vsi->port_info;
2785         /* If VSI state is up, then restart autoneg with link up */
2786         if (!test_bit(__ICE_DOWN, vsi->back->state))
2787                 status = ice_aq_set_link_restart_an(pi, true, NULL);
2788         else
2789                 status = ice_aq_set_link_restart_an(pi, false, NULL);
2790
2791         if (status) {
2792                 netdev_info(netdev, "link restart failed, err %d aq_err %d\n",
2793                             status, pi->hw->adminq.sq_last_status);
2794                 return -EIO;
2795         }
2796
2797         return 0;
2798 }
2799
2800 /**
2801  * ice_get_pauseparam - Get Flow Control status
2802  * @netdev: network interface device structure
2803  * @pause: ethernet pause (flow control) parameters
2804  *
2805  * Get requested flow control status from PHY capability.
2806  * If autoneg is true, then ethtool will send the ETHTOOL_GSET ioctl which
2807  * is handled by ice_get_link_ksettings. ice_get_link_ksettings will report
2808  * the negotiated Rx/Tx pause via lp_advertising.
2809  */
2810 static void
2811 ice_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2812 {
2813         struct ice_netdev_priv *np = netdev_priv(netdev);
2814         struct ice_port_info *pi = np->vsi->port_info;
2815         struct ice_aqc_get_phy_caps_data *pcaps;
2816         struct ice_vsi *vsi = np->vsi;
2817         struct ice_dcbx_cfg *dcbx_cfg;
2818         enum ice_status status;
2819
2820         /* Initialize pause params */
2821         pause->rx_pause = 0;
2822         pause->tx_pause = 0;
2823
2824         dcbx_cfg = &pi->local_dcbx_cfg;
2825
2826         pcaps = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*pcaps),
2827                              GFP_KERNEL);
2828         if (!pcaps)
2829                 return;
2830
2831         /* Get current PHY config */
2832         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
2833                                      NULL);
2834         if (status)
2835                 goto out;
2836
2837         pause->autoneg = ((pcaps->caps & ICE_AQC_PHY_AN_MODE) ?
2838                         AUTONEG_ENABLE : AUTONEG_DISABLE);
2839
2840         if (dcbx_cfg->pfc.pfcena)
2841                 /* PFC enabled so report LFC as off */
2842                 goto out;
2843
2844         if (pcaps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
2845                 pause->tx_pause = 1;
2846         if (pcaps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
2847                 pause->rx_pause = 1;
2848
2849 out:
2850         devm_kfree(&vsi->back->pdev->dev, pcaps);
2851 }
2852
2853 /**
2854  * ice_set_pauseparam - Set Flow Control parameter
2855  * @netdev: network interface device structure
2856  * @pause: return Tx/Rx flow control status
2857  */
2858 static int
2859 ice_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2860 {
2861         struct ice_netdev_priv *np = netdev_priv(netdev);
2862         struct ice_aqc_get_phy_caps_data *pcaps;
2863         struct ice_link_status *hw_link_info;
2864         struct ice_pf *pf = np->vsi->back;
2865         struct ice_dcbx_cfg *dcbx_cfg;
2866         struct ice_vsi *vsi = np->vsi;
2867         struct ice_hw *hw = &pf->hw;
2868         struct ice_port_info *pi;
2869         enum ice_status status;
2870         u8 aq_failures;
2871         bool link_up;
2872         int err = 0;
2873         u32 is_an;
2874
2875         pi = vsi->port_info;
2876         hw_link_info = &pi->phy.link_info;
2877         dcbx_cfg = &pi->local_dcbx_cfg;
2878         link_up = hw_link_info->link_info & ICE_AQ_LINK_UP;
2879
2880         /* Changing the port's flow control is not supported if this isn't the
2881          * PF VSI
2882          */
2883         if (vsi->type != ICE_VSI_PF) {
2884                 netdev_info(netdev, "Changing flow control parameters only supported for PF VSI\n");
2885                 return -EOPNOTSUPP;
2886         }
2887
2888         /* Get pause param reports configured and negotiated flow control pause
2889          * when ETHTOOL_GLINKSETTINGS is defined. Since ETHTOOL_GLINKSETTINGS is
2890          * defined get pause param pause->autoneg reports SW configured setting,
2891          * so compare pause->autoneg with SW configured to prevent the user from
2892          * using set pause param to chance autoneg.
2893          */
2894         pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2895         if (!pcaps)
2896                 return -ENOMEM;
2897
2898         /* Get current PHY config */
2899         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
2900                                      NULL);
2901         if (status) {
2902                 kfree(pcaps);
2903                 return -EIO;
2904         }
2905
2906         is_an = ((pcaps->caps & ICE_AQC_PHY_AN_MODE) ?
2907                         AUTONEG_ENABLE : AUTONEG_DISABLE);
2908
2909         kfree(pcaps);
2910
2911         if (pause->autoneg != is_an) {
2912                 netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
2913                 return -EOPNOTSUPP;
2914         }
2915
2916         /* If we have link and don't have autoneg */
2917         if (!test_bit(__ICE_DOWN, pf->state) &&
2918             !(hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
2919                 /* Send message that it might not necessarily work*/
2920                 netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
2921         }
2922
2923         if (dcbx_cfg->pfc.pfcena) {
2924                 netdev_info(netdev, "Priority flow control enabled. Cannot set link flow control.\n");
2925                 return -EOPNOTSUPP;
2926         }
2927         if (pause->rx_pause && pause->tx_pause)
2928                 pi->fc.req_mode = ICE_FC_FULL;
2929         else if (pause->rx_pause && !pause->tx_pause)
2930                 pi->fc.req_mode = ICE_FC_RX_PAUSE;
2931         else if (!pause->rx_pause && pause->tx_pause)
2932                 pi->fc.req_mode = ICE_FC_TX_PAUSE;
2933         else if (!pause->rx_pause && !pause->tx_pause)
2934                 pi->fc.req_mode = ICE_FC_NONE;
2935         else
2936                 return -EINVAL;
2937
2938         /* Tell the OS link is going down, the link will go back up when fw
2939          * says it is ready asynchronously
2940          */
2941         ice_print_link_msg(vsi, false);
2942         netif_carrier_off(netdev);
2943         netif_tx_stop_all_queues(netdev);
2944
2945         /* Set the FC mode and only restart AN if link is up */
2946         status = ice_set_fc(pi, &aq_failures, link_up);
2947
2948         if (aq_failures & ICE_SET_FC_AQ_FAIL_GET) {
2949                 netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %d aq_err %d\n",
2950                             status, hw->adminq.sq_last_status);
2951                 err = -EAGAIN;
2952         } else if (aq_failures & ICE_SET_FC_AQ_FAIL_SET) {
2953                 netdev_info(netdev, "Set fc failed on the set_phy_config call with err %d aq_err %d\n",
2954                             status, hw->adminq.sq_last_status);
2955                 err = -EAGAIN;
2956         } else if (aq_failures & ICE_SET_FC_AQ_FAIL_UPDATE) {
2957                 netdev_info(netdev, "Set fc failed on the get_link_info call with err %d aq_err %d\n",
2958                             status, hw->adminq.sq_last_status);
2959                 err = -EAGAIN;
2960         }
2961
2962         if (!test_bit(__ICE_DOWN, pf->state)) {
2963                 /* Give it a little more time to try to come back. If still
2964                  * down, restart autoneg link or reinitialize the interface.
2965                  */
2966                 msleep(75);
2967                 if (!test_bit(__ICE_DOWN, pf->state))
2968                         return ice_nway_reset(netdev);
2969
2970                 ice_down(vsi);
2971                 ice_up(vsi);
2972         }
2973
2974         return err;
2975 }
2976
2977 /**
2978  * ice_get_rxfh_key_size - get the RSS hash key size
2979  * @netdev: network interface device structure
2980  *
2981  * Returns the table size.
2982  */
2983 static u32 ice_get_rxfh_key_size(struct net_device __always_unused *netdev)
2984 {
2985         return ICE_VSIQF_HKEY_ARRAY_SIZE;
2986 }
2987
2988 /**
2989  * ice_get_rxfh_indir_size - get the Rx flow hash indirection table size
2990  * @netdev: network interface device structure
2991  *
2992  * Returns the table size.
2993  */
2994 static u32 ice_get_rxfh_indir_size(struct net_device *netdev)
2995 {
2996         struct ice_netdev_priv *np = netdev_priv(netdev);
2997
2998         return np->vsi->rss_table_size;
2999 }
3000
3001 /**
3002  * ice_get_rxfh - get the Rx flow hash indirection table
3003  * @netdev: network interface device structure
3004  * @indir: indirection table
3005  * @key: hash key
3006  * @hfunc: hash function
3007  *
3008  * Reads the indirection table directly from the hardware.
3009  */
3010 static int
3011 ice_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
3012 {
3013         struct ice_netdev_priv *np = netdev_priv(netdev);
3014         struct ice_vsi *vsi = np->vsi;
3015         struct ice_pf *pf = vsi->back;
3016         int ret = 0, i;
3017         u8 *lut;
3018
3019         if (hfunc)
3020                 *hfunc = ETH_RSS_HASH_TOP;
3021
3022         if (!indir)
3023                 return 0;
3024
3025         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3026                 /* RSS not supported return error here */
3027                 netdev_warn(netdev, "RSS is not configured on this VSI!\n");
3028                 return -EIO;
3029         }
3030
3031         lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
3032         if (!lut)
3033                 return -ENOMEM;
3034
3035         if (ice_get_rss(vsi, key, lut, vsi->rss_table_size)) {
3036                 ret = -EIO;
3037                 goto out;
3038         }
3039
3040         for (i = 0; i < vsi->rss_table_size; i++)
3041                 indir[i] = (u32)(lut[i]);
3042
3043 out:
3044         devm_kfree(&pf->pdev->dev, lut);
3045         return ret;
3046 }
3047
3048 /**
3049  * ice_set_rxfh - set the Rx flow hash indirection table
3050  * @netdev: network interface device structure
3051  * @indir: indirection table
3052  * @key: hash key
3053  * @hfunc: hash function
3054  *
3055  * Returns -EINVAL if the table specifies an invalid queue ID, otherwise
3056  * returns 0 after programming the table.
3057  */
3058 static int
3059 ice_set_rxfh(struct net_device *netdev, const u32 *indir, const u8 *key,
3060              const u8 hfunc)
3061 {
3062         struct ice_netdev_priv *np = netdev_priv(netdev);
3063         struct ice_vsi *vsi = np->vsi;
3064         struct ice_pf *pf = vsi->back;
3065         u8 *seed = NULL;
3066
3067         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3068                 return -EOPNOTSUPP;
3069
3070         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3071                 /* RSS not supported return error here */
3072                 netdev_warn(netdev, "RSS is not configured on this VSI!\n");
3073                 return -EIO;
3074         }
3075
3076         if (key) {
3077                 if (!vsi->rss_hkey_user) {
3078                         vsi->rss_hkey_user =
3079                                 devm_kzalloc(&pf->pdev->dev,
3080                                              ICE_VSIQF_HKEY_ARRAY_SIZE,
3081                                              GFP_KERNEL);
3082                         if (!vsi->rss_hkey_user)
3083                                 return -ENOMEM;
3084                 }
3085                 memcpy(vsi->rss_hkey_user, key, ICE_VSIQF_HKEY_ARRAY_SIZE);
3086                 seed = vsi->rss_hkey_user;
3087         }
3088
3089         if (!vsi->rss_lut_user) {
3090                 vsi->rss_lut_user = devm_kzalloc(&pf->pdev->dev,
3091                                                  vsi->rss_table_size,
3092                                                  GFP_KERNEL);
3093                 if (!vsi->rss_lut_user)
3094                         return -ENOMEM;
3095         }
3096
3097         /* Each 32 bits pointed by 'indir' is stored with a lut entry */
3098         if (indir) {
3099                 int i;
3100
3101                 for (i = 0; i < vsi->rss_table_size; i++)
3102                         vsi->rss_lut_user[i] = (u8)(indir[i]);
3103         } else {
3104                 ice_fill_rss_lut(vsi->rss_lut_user, vsi->rss_table_size,
3105                                  vsi->rss_size);
3106         }
3107
3108         if (ice_set_rss(vsi, seed, vsi->rss_lut_user, vsi->rss_table_size))
3109                 return -EIO;
3110
3111         return 0;
3112 }
3113
3114 enum ice_container_type {
3115         ICE_RX_CONTAINER,
3116         ICE_TX_CONTAINER,
3117 };
3118
3119 /**
3120  * ice_get_rc_coalesce - get ITR values for specific ring container
3121  * @ec: ethtool structure to fill with driver's coalesce settings
3122  * @c_type: container type, Rx or Tx
3123  * @rc: ring container that the ITR values will come from
3124  *
3125  * Query the device for ice_ring_container specific ITR values. This is
3126  * done per ice_ring_container because each q_vector can have 1 or more rings
3127  * and all of said ring(s) will have the same ITR values.
3128  *
3129  * Returns 0 on success, negative otherwise.
3130  */
3131 static int
3132 ice_get_rc_coalesce(struct ethtool_coalesce *ec, enum ice_container_type c_type,
3133                     struct ice_ring_container *rc)
3134 {
3135         struct ice_pf *pf;
3136
3137         if (!rc->ring)
3138                 return -EINVAL;
3139
3140         pf = rc->ring->vsi->back;
3141
3142         switch (c_type) {
3143         case ICE_RX_CONTAINER:
3144                 ec->use_adaptive_rx_coalesce = ITR_IS_DYNAMIC(rc->itr_setting);
3145                 ec->rx_coalesce_usecs = rc->itr_setting & ~ICE_ITR_DYNAMIC;
3146                 ec->rx_coalesce_usecs_high = rc->ring->q_vector->intrl;
3147                 break;
3148         case ICE_TX_CONTAINER:
3149                 ec->use_adaptive_tx_coalesce = ITR_IS_DYNAMIC(rc->itr_setting);
3150                 ec->tx_coalesce_usecs = rc->itr_setting & ~ICE_ITR_DYNAMIC;
3151                 break;
3152         default:
3153                 dev_dbg(&pf->pdev->dev, "Invalid c_type %d\n", c_type);
3154                 return -EINVAL;
3155         }
3156
3157         return 0;
3158 }
3159
3160 /**
3161  * ice_get_q_coalesce - get a queue's ITR/INTRL (coalesce) settings
3162  * @vsi: VSI associated to the queue for getting ITR/INTRL (coalesce) settings
3163  * @ec: coalesce settings to program the device with
3164  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3165  *
3166  * Return 0 on success, and negative under the following conditions:
3167  * 1. Getting Tx or Rx ITR/INTRL (coalesce) settings failed.
3168  * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3169  */
3170 static int
3171 ice_get_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3172 {
3173         if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3174                 if (ice_get_rc_coalesce(ec, ICE_RX_CONTAINER,
3175                                         &vsi->rx_rings[q_num]->q_vector->rx))
3176                         return -EINVAL;
3177                 if (ice_get_rc_coalesce(ec, ICE_TX_CONTAINER,
3178                                         &vsi->tx_rings[q_num]->q_vector->tx))
3179                         return -EINVAL;
3180         } else if (q_num < vsi->num_rxq) {
3181                 if (ice_get_rc_coalesce(ec, ICE_RX_CONTAINER,
3182                                         &vsi->rx_rings[q_num]->q_vector->rx))
3183                         return -EINVAL;
3184         } else if (q_num < vsi->num_txq) {
3185                 if (ice_get_rc_coalesce(ec, ICE_TX_CONTAINER,
3186                                         &vsi->tx_rings[q_num]->q_vector->tx))
3187                         return -EINVAL;
3188         } else {
3189                 return -EINVAL;
3190         }
3191
3192         return 0;
3193 }
3194
3195 /**
3196  * __ice_get_coalesce - get ITR/INTRL values for the device
3197  * @netdev: pointer to the netdev associated with this query
3198  * @ec: ethtool structure to fill with driver's coalesce settings
3199  * @q_num: queue number to get the coalesce settings for
3200  *
3201  * If the caller passes in a negative q_num then we return coalesce settings
3202  * based on queue number 0, else use the actual q_num passed in.
3203  */
3204 static int
3205 __ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3206                    int q_num)
3207 {
3208         struct ice_netdev_priv *np = netdev_priv(netdev);
3209         struct ice_vsi *vsi = np->vsi;
3210
3211         if (q_num < 0)
3212                 q_num = 0;
3213
3214         if (ice_get_q_coalesce(vsi, ec, q_num))
3215                 return -EINVAL;
3216
3217         if (q_num < vsi->num_txq)
3218                 ec->tx_max_coalesced_frames_irq = vsi->work_lmt;
3219
3220         if (q_num < vsi->num_rxq)
3221                 ec->rx_max_coalesced_frames_irq = vsi->work_lmt;
3222
3223         return 0;
3224 }
3225
3226 static int
3227 ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
3228 {
3229         return __ice_get_coalesce(netdev, ec, -1);
3230 }
3231
3232 static int
3233 ice_get_per_q_coalesce(struct net_device *netdev, u32 q_num,
3234                        struct ethtool_coalesce *ec)
3235 {
3236         return __ice_get_coalesce(netdev, ec, q_num);
3237 }
3238
3239 /**
3240  * ice_set_rc_coalesce - set ITR values for specific ring container
3241  * @c_type: container type, Rx or Tx
3242  * @ec: ethtool structure from user to update ITR settings
3243  * @rc: ring container that the ITR values will come from
3244  * @vsi: VSI associated to the ring container
3245  *
3246  * Set specific ITR values. This is done per ice_ring_container because each
3247  * q_vector can have 1 or more rings and all of said ring(s) will have the same
3248  * ITR values.
3249  *
3250  * Returns 0 on success, negative otherwise.
3251  */
3252 static int
3253 ice_set_rc_coalesce(enum ice_container_type c_type, struct ethtool_coalesce *ec,
3254                     struct ice_ring_container *rc, struct ice_vsi *vsi)
3255 {
3256         const char *c_type_str = (c_type == ICE_RX_CONTAINER) ? "rx" : "tx";
3257         u32 use_adaptive_coalesce, coalesce_usecs;
3258         struct ice_pf *pf = vsi->back;
3259         u16 itr_setting;
3260
3261         if (!rc->ring)
3262                 return -EINVAL;
3263
3264         switch (c_type) {
3265         case ICE_RX_CONTAINER:
3266                 if (ec->rx_coalesce_usecs_high > ICE_MAX_INTRL ||
3267                     (ec->rx_coalesce_usecs_high &&
3268                      ec->rx_coalesce_usecs_high < pf->hw.intrl_gran)) {
3269                         netdev_info(vsi->netdev,
3270                                     "Invalid value, %s-usecs-high valid values are 0 (disabled), %d-%d\n",
3271                                     c_type_str, pf->hw.intrl_gran,
3272                                     ICE_MAX_INTRL);
3273                         return -EINVAL;
3274                 }
3275                 if (ec->rx_coalesce_usecs_high != rc->ring->q_vector->intrl) {
3276                         rc->ring->q_vector->intrl = ec->rx_coalesce_usecs_high;
3277                         wr32(&pf->hw, GLINT_RATE(rc->ring->q_vector->reg_idx),
3278                              ice_intrl_usec_to_reg(ec->rx_coalesce_usecs_high,
3279                                                    pf->hw.intrl_gran));
3280                 }
3281
3282                 use_adaptive_coalesce = ec->use_adaptive_rx_coalesce;
3283                 coalesce_usecs = ec->rx_coalesce_usecs;
3284
3285                 break;
3286         case ICE_TX_CONTAINER:
3287                 if (ec->tx_coalesce_usecs_high) {
3288                         netdev_info(vsi->netdev,
3289                                     "setting %s-usecs-high is not supported\n",
3290                                     c_type_str);
3291                         return -EINVAL;
3292                 }
3293
3294                 use_adaptive_coalesce = ec->use_adaptive_tx_coalesce;
3295                 coalesce_usecs = ec->tx_coalesce_usecs;
3296
3297                 break;
3298         default:
3299                 dev_dbg(&pf->pdev->dev, "Invalid container type %d\n", c_type);
3300                 return -EINVAL;
3301         }
3302
3303         itr_setting = rc->itr_setting & ~ICE_ITR_DYNAMIC;
3304         if (coalesce_usecs != itr_setting && use_adaptive_coalesce) {
3305                 netdev_info(vsi->netdev,
3306                             "%s interrupt throttling cannot be changed if adaptive-%s is enabled\n",
3307                             c_type_str, c_type_str);
3308                 return -EINVAL;
3309         }
3310
3311         if (coalesce_usecs > ICE_ITR_MAX) {
3312                 netdev_info(vsi->netdev,
3313                             "Invalid value, %s-usecs range is 0-%d\n",
3314                             c_type_str, ICE_ITR_MAX);
3315                 return -EINVAL;
3316         }
3317
3318         /* hardware only supports an ITR granularity of 2us */
3319         if (coalesce_usecs % 2 != 0) {
3320                 netdev_info(vsi->netdev,
3321                             "Invalid value, %s-usecs must be even\n",
3322                             c_type_str);
3323                 return -EINVAL;
3324         }
3325
3326         if (use_adaptive_coalesce) {
3327                 rc->itr_setting |= ICE_ITR_DYNAMIC;
3328         } else {
3329                 /* store user facing value how it was set */
3330                 rc->itr_setting = coalesce_usecs;
3331                 /* set to static and convert to value HW understands */
3332                 rc->target_itr =
3333                         ITR_TO_REG(ITR_REG_ALIGN(rc->itr_setting));
3334         }
3335
3336         return 0;
3337 }
3338
3339 /**
3340  * ice_set_q_coalesce - set a queue's ITR/INTRL (coalesce) settings
3341  * @vsi: VSI associated to the queue that need updating
3342  * @ec: coalesce settings to program the device with
3343  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3344  *
3345  * Return 0 on success, and negative under the following conditions:
3346  * 1. Setting Tx or Rx ITR/INTRL (coalesce) settings failed.
3347  * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3348  */
3349 static int
3350 ice_set_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3351 {
3352         if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3353                 if (ice_set_rc_coalesce(ICE_RX_CONTAINER, ec,
3354                                         &vsi->rx_rings[q_num]->q_vector->rx,
3355                                         vsi))
3356                         return -EINVAL;
3357
3358                 if (ice_set_rc_coalesce(ICE_TX_CONTAINER, ec,
3359                                         &vsi->tx_rings[q_num]->q_vector->tx,
3360                                         vsi))
3361                         return -EINVAL;
3362         } else if (q_num < vsi->num_rxq) {
3363                 if (ice_set_rc_coalesce(ICE_RX_CONTAINER, ec,
3364                                         &vsi->rx_rings[q_num]->q_vector->rx,
3365                                         vsi))
3366                         return -EINVAL;
3367         } else if (q_num < vsi->num_txq) {
3368                 if (ice_set_rc_coalesce(ICE_TX_CONTAINER, ec,
3369                                         &vsi->tx_rings[q_num]->q_vector->tx,
3370                                         vsi))
3371                         return -EINVAL;
3372         } else {
3373                 return -EINVAL;
3374         }
3375
3376         return 0;
3377 }
3378
3379 /**
3380  * __ice_set_coalesce - set ITR/INTRL values for the device
3381  * @netdev: pointer to the netdev associated with this query
3382  * @ec: ethtool structure to fill with driver's coalesce settings
3383  * @q_num: queue number to get the coalesce settings for
3384  *
3385  * If the caller passes in a negative q_num then we set the coalesce settings
3386  * for all Tx/Rx queues, else use the actual q_num passed in.
3387  */
3388 static int
3389 __ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3390                    int q_num)
3391 {
3392         struct ice_netdev_priv *np = netdev_priv(netdev);
3393         struct ice_vsi *vsi = np->vsi;
3394
3395         if (q_num < 0) {
3396                 int i;
3397
3398                 ice_for_each_q_vector(vsi, i) {
3399                         if (ice_set_q_coalesce(vsi, ec, i))
3400                                 return -EINVAL;
3401                 }
3402                 goto set_work_lmt;
3403         }
3404
3405         if (ice_set_q_coalesce(vsi, ec, q_num))
3406                 return -EINVAL;
3407
3408 set_work_lmt:
3409
3410         if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
3411                 vsi->work_lmt = max(ec->tx_max_coalesced_frames_irq,
3412                                     ec->rx_max_coalesced_frames_irq);
3413
3414         return 0;
3415 }
3416
3417 static int
3418 ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec)
3419 {
3420         return __ice_set_coalesce(netdev, ec, -1);
3421 }
3422
3423 static int
3424 ice_set_per_q_coalesce(struct net_device *netdev, u32 q_num,
3425                        struct ethtool_coalesce *ec)
3426 {
3427         return __ice_set_coalesce(netdev, ec, q_num);
3428 }
3429
3430 static const struct ethtool_ops ice_ethtool_ops = {
3431         .get_link_ksettings     = ice_get_link_ksettings,
3432         .set_link_ksettings     = ice_set_link_ksettings,
3433         .get_drvinfo            = ice_get_drvinfo,
3434         .get_regs_len           = ice_get_regs_len,
3435         .get_regs               = ice_get_regs,
3436         .get_msglevel           = ice_get_msglevel,
3437         .set_msglevel           = ice_set_msglevel,
3438         .self_test              = ice_self_test,
3439         .get_link               = ethtool_op_get_link,
3440         .get_eeprom_len         = ice_get_eeprom_len,
3441         .get_eeprom             = ice_get_eeprom,
3442         .get_coalesce           = ice_get_coalesce,
3443         .set_coalesce           = ice_set_coalesce,
3444         .get_strings            = ice_get_strings,
3445         .set_phys_id            = ice_set_phys_id,
3446         .get_ethtool_stats      = ice_get_ethtool_stats,
3447         .get_priv_flags         = ice_get_priv_flags,
3448         .set_priv_flags         = ice_set_priv_flags,
3449         .get_sset_count         = ice_get_sset_count,
3450         .get_rxnfc              = ice_get_rxnfc,
3451         .get_ringparam          = ice_get_ringparam,
3452         .set_ringparam          = ice_set_ringparam,
3453         .nway_reset             = ice_nway_reset,
3454         .get_pauseparam         = ice_get_pauseparam,
3455         .set_pauseparam         = ice_set_pauseparam,
3456         .get_rxfh_key_size      = ice_get_rxfh_key_size,
3457         .get_rxfh_indir_size    = ice_get_rxfh_indir_size,
3458         .get_rxfh               = ice_get_rxfh,
3459         .set_rxfh               = ice_set_rxfh,
3460         .get_ts_info            = ethtool_op_get_ts_info,
3461         .get_per_queue_coalesce = ice_get_per_q_coalesce,
3462         .set_per_queue_coalesce = ice_set_per_q_coalesce,
3463         .get_fecparam           = ice_get_fecparam,
3464         .set_fecparam           = ice_set_fecparam,
3465 };
3466
3467 /**
3468  * ice_set_ethtool_ops - setup netdev ethtool ops
3469  * @netdev: network interface device structure
3470  *
3471  * setup netdev ethtool ops with ice specific ops
3472  */
3473 void ice_set_ethtool_ops(struct net_device *netdev)
3474 {
3475         netdev->ethtool_ops = &ice_ethtool_ops;
3476 }