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_flow.h"
8 #include "ice_fltr.h"
9 #include "ice_lib.h"
10 #include "ice_dcb_lib.h"
11 #include <net/dcbnl.h>
12
13 struct ice_stats {
14 char stat_string[ETH_GSTRING_LEN];
15 int sizeof_stat;
16 int stat_offset;
17 };
18
19 #define ICE_STAT(_type, _name, _stat) { \
20 .stat_string = _name, \
21 .sizeof_stat = sizeof_field(_type, _stat), \
22 .stat_offset = offsetof(_type, _stat) \
23 }
24
25 #define ICE_VSI_STAT(_name, _stat) \
26 ICE_STAT(struct ice_vsi, _name, _stat)
27 #define ICE_PF_STAT(_name, _stat) \
28 ICE_STAT(struct ice_pf, _name, _stat)
29
ice_q_stats_len(struct net_device * netdev)30 static int ice_q_stats_len(struct net_device *netdev)
31 {
32 struct ice_netdev_priv *np = netdev_priv(netdev);
33
34 return ((np->vsi->alloc_txq + np->vsi->alloc_rxq) *
35 (sizeof(struct ice_q_stats) / sizeof(u64)));
36 }
37
38 #define ICE_PF_STATS_LEN ARRAY_SIZE(ice_gstrings_pf_stats)
39 #define ICE_VSI_STATS_LEN ARRAY_SIZE(ice_gstrings_vsi_stats)
40
41 #define ICE_PFC_STATS_LEN ( \
42 (sizeof_field(struct ice_pf, stats.priority_xoff_rx) + \
43 sizeof_field(struct ice_pf, stats.priority_xon_rx) + \
44 sizeof_field(struct ice_pf, stats.priority_xoff_tx) + \
45 sizeof_field(struct ice_pf, stats.priority_xon_tx)) \
46 / sizeof(u64))
47 #define ICE_ALL_STATS_LEN(n) (ICE_PF_STATS_LEN + ICE_PFC_STATS_LEN + \
48 ICE_VSI_STATS_LEN + ice_q_stats_len(n))
49
50 static const struct ice_stats ice_gstrings_vsi_stats[] = {
51 ICE_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
52 ICE_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
53 ICE_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
54 ICE_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
55 ICE_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
56 ICE_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
57 ICE_VSI_STAT("rx_bytes", eth_stats.rx_bytes),
58 ICE_VSI_STAT("tx_bytes", eth_stats.tx_bytes),
59 ICE_VSI_STAT("rx_dropped", eth_stats.rx_discards),
60 ICE_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
61 ICE_VSI_STAT("rx_alloc_fail", rx_buf_failed),
62 ICE_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
63 ICE_VSI_STAT("tx_errors", eth_stats.tx_errors),
64 ICE_VSI_STAT("tx_linearize", tx_linearize),
65 ICE_VSI_STAT("tx_busy", tx_busy),
66 ICE_VSI_STAT("tx_restart", tx_restart),
67 };
68
69 enum ice_ethtool_test_id {
70 ICE_ETH_TEST_REG = 0,
71 ICE_ETH_TEST_EEPROM,
72 ICE_ETH_TEST_INTR,
73 ICE_ETH_TEST_LOOP,
74 ICE_ETH_TEST_LINK,
75 };
76
77 static const char ice_gstrings_test[][ETH_GSTRING_LEN] = {
78 "Register test (offline)",
79 "EEPROM test (offline)",
80 "Interrupt test (offline)",
81 "Loopback test (offline)",
82 "Link test (on/offline)",
83 };
84
85 #define ICE_TEST_LEN (sizeof(ice_gstrings_test) / ETH_GSTRING_LEN)
86
87 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
88 * but they aren't. This device is capable of supporting multiple
89 * VSIs/netdevs on a single PF. The NETDEV_STATs are for individual
90 * netdevs whereas the PF_STATs are for the physical function that's
91 * hosting these netdevs.
92 *
93 * The PF_STATs are appended to the netdev stats only when ethtool -S
94 * is queried on the base PF netdev.
95 */
96 static const struct ice_stats ice_gstrings_pf_stats[] = {
97 ICE_PF_STAT("rx_bytes.nic", stats.eth.rx_bytes),
98 ICE_PF_STAT("tx_bytes.nic", stats.eth.tx_bytes),
99 ICE_PF_STAT("rx_unicast.nic", stats.eth.rx_unicast),
100 ICE_PF_STAT("tx_unicast.nic", stats.eth.tx_unicast),
101 ICE_PF_STAT("rx_multicast.nic", stats.eth.rx_multicast),
102 ICE_PF_STAT("tx_multicast.nic", stats.eth.tx_multicast),
103 ICE_PF_STAT("rx_broadcast.nic", stats.eth.rx_broadcast),
104 ICE_PF_STAT("tx_broadcast.nic", stats.eth.tx_broadcast),
105 ICE_PF_STAT("tx_errors.nic", stats.eth.tx_errors),
106 ICE_PF_STAT("tx_timeout.nic", tx_timeout_count),
107 ICE_PF_STAT("rx_size_64.nic", stats.rx_size_64),
108 ICE_PF_STAT("tx_size_64.nic", stats.tx_size_64),
109 ICE_PF_STAT("rx_size_127.nic", stats.rx_size_127),
110 ICE_PF_STAT("tx_size_127.nic", stats.tx_size_127),
111 ICE_PF_STAT("rx_size_255.nic", stats.rx_size_255),
112 ICE_PF_STAT("tx_size_255.nic", stats.tx_size_255),
113 ICE_PF_STAT("rx_size_511.nic", stats.rx_size_511),
114 ICE_PF_STAT("tx_size_511.nic", stats.tx_size_511),
115 ICE_PF_STAT("rx_size_1023.nic", stats.rx_size_1023),
116 ICE_PF_STAT("tx_size_1023.nic", stats.tx_size_1023),
117 ICE_PF_STAT("rx_size_1522.nic", stats.rx_size_1522),
118 ICE_PF_STAT("tx_size_1522.nic", stats.tx_size_1522),
119 ICE_PF_STAT("rx_size_big.nic", stats.rx_size_big),
120 ICE_PF_STAT("tx_size_big.nic", stats.tx_size_big),
121 ICE_PF_STAT("link_xon_rx.nic", stats.link_xon_rx),
122 ICE_PF_STAT("link_xon_tx.nic", stats.link_xon_tx),
123 ICE_PF_STAT("link_xoff_rx.nic", stats.link_xoff_rx),
124 ICE_PF_STAT("link_xoff_tx.nic", stats.link_xoff_tx),
125 ICE_PF_STAT("tx_dropped_link_down.nic", stats.tx_dropped_link_down),
126 ICE_PF_STAT("rx_undersize.nic", stats.rx_undersize),
127 ICE_PF_STAT("rx_fragments.nic", stats.rx_fragments),
128 ICE_PF_STAT("rx_oversize.nic", stats.rx_oversize),
129 ICE_PF_STAT("rx_jabber.nic", stats.rx_jabber),
130 ICE_PF_STAT("rx_csum_bad.nic", hw_csum_rx_error),
131 ICE_PF_STAT("rx_length_errors.nic", stats.rx_len_errors),
132 ICE_PF_STAT("rx_dropped.nic", stats.eth.rx_discards),
133 ICE_PF_STAT("rx_crc_errors.nic", stats.crc_errors),
134 ICE_PF_STAT("illegal_bytes.nic", stats.illegal_bytes),
135 ICE_PF_STAT("mac_local_faults.nic", stats.mac_local_faults),
136 ICE_PF_STAT("mac_remote_faults.nic", stats.mac_remote_faults),
137 ICE_PF_STAT("fdir_sb_match.nic", stats.fd_sb_match),
138 ICE_PF_STAT("fdir_sb_status.nic", stats.fd_sb_status),
139 };
140
141 static const u32 ice_regs_dump_list[] = {
142 PFGEN_STATE,
143 PRTGEN_STATUS,
144 QRX_CTRL(0),
145 QINT_TQCTL(0),
146 QINT_RQCTL(0),
147 PFINT_OICR_ENA,
148 QRX_ITR(0),
149 };
150
151 struct ice_priv_flag {
152 char name[ETH_GSTRING_LEN];
153 u32 bitno; /* bit position in pf->flags */
154 };
155
156 #define ICE_PRIV_FLAG(_name, _bitno) { \
157 .name = _name, \
158 .bitno = _bitno, \
159 }
160
161 static const struct ice_priv_flag ice_gstrings_priv_flags[] = {
162 ICE_PRIV_FLAG("link-down-on-close", ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA),
163 ICE_PRIV_FLAG("fw-lldp-agent", ICE_FLAG_FW_LLDP_AGENT),
164 ICE_PRIV_FLAG("vf-true-promisc-support",
165 ICE_FLAG_VF_TRUE_PROMISC_ENA),
166 ICE_PRIV_FLAG("mdd-auto-reset-vf", ICE_FLAG_MDD_AUTO_RESET_VF),
167 ICE_PRIV_FLAG("legacy-rx", ICE_FLAG_LEGACY_RX),
168 };
169
170 #define ICE_PRIV_FLAG_ARRAY_SIZE ARRAY_SIZE(ice_gstrings_priv_flags)
171
172 static void
ice_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)173 ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
174 {
175 struct ice_netdev_priv *np = netdev_priv(netdev);
176 struct ice_vsi *vsi = np->vsi;
177 struct ice_pf *pf = vsi->back;
178 struct ice_hw *hw = &pf->hw;
179 struct ice_orom_info *orom;
180 struct ice_nvm_info *nvm;
181
182 nvm = &hw->flash.nvm;
183 orom = &hw->flash.orom;
184
185 strscpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
186
187 /* Display NVM version (from which the firmware version can be
188 * determined) which contains more pertinent information.
189 */
190 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
191 "%x.%02x 0x%x %d.%d.%d", nvm->major, nvm->minor,
192 nvm->eetrack, orom->major, orom->build, orom->patch);
193
194 strscpy(drvinfo->bus_info, pci_name(pf->pdev),
195 sizeof(drvinfo->bus_info));
196 drvinfo->n_priv_flags = ICE_PRIV_FLAG_ARRAY_SIZE;
197 }
198
ice_get_regs_len(struct net_device __always_unused * netdev)199 static int ice_get_regs_len(struct net_device __always_unused *netdev)
200 {
201 return sizeof(ice_regs_dump_list);
202 }
203
204 static void
ice_get_regs(struct net_device * netdev,struct ethtool_regs * regs,void * p)205 ice_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
206 {
207 struct ice_netdev_priv *np = netdev_priv(netdev);
208 struct ice_pf *pf = np->vsi->back;
209 struct ice_hw *hw = &pf->hw;
210 u32 *regs_buf = (u32 *)p;
211 unsigned int i;
212
213 regs->version = 1;
214
215 for (i = 0; i < ARRAY_SIZE(ice_regs_dump_list); ++i)
216 regs_buf[i] = rd32(hw, ice_regs_dump_list[i]);
217 }
218
ice_get_msglevel(struct net_device * netdev)219 static u32 ice_get_msglevel(struct net_device *netdev)
220 {
221 struct ice_netdev_priv *np = netdev_priv(netdev);
222 struct ice_pf *pf = np->vsi->back;
223
224 #ifndef CONFIG_DYNAMIC_DEBUG
225 if (pf->hw.debug_mask)
226 netdev_info(netdev, "hw debug_mask: 0x%llX\n",
227 pf->hw.debug_mask);
228 #endif /* !CONFIG_DYNAMIC_DEBUG */
229
230 return pf->msg_enable;
231 }
232
ice_set_msglevel(struct net_device * netdev,u32 data)233 static void ice_set_msglevel(struct net_device *netdev, u32 data)
234 {
235 struct ice_netdev_priv *np = netdev_priv(netdev);
236 struct ice_pf *pf = np->vsi->back;
237
238 #ifndef CONFIG_DYNAMIC_DEBUG
239 if (ICE_DBG_USER & data)
240 pf->hw.debug_mask = data;
241 else
242 pf->msg_enable = data;
243 #else
244 pf->msg_enable = data;
245 #endif /* !CONFIG_DYNAMIC_DEBUG */
246 }
247
ice_get_eeprom_len(struct net_device * netdev)248 static int ice_get_eeprom_len(struct net_device *netdev)
249 {
250 struct ice_netdev_priv *np = netdev_priv(netdev);
251 struct ice_pf *pf = np->vsi->back;
252
253 return (int)pf->hw.flash.flash_size;
254 }
255
256 static int
ice_get_eeprom(struct net_device * netdev,struct ethtool_eeprom * eeprom,u8 * bytes)257 ice_get_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
258 u8 *bytes)
259 {
260 struct ice_netdev_priv *np = netdev_priv(netdev);
261 struct ice_vsi *vsi = np->vsi;
262 struct ice_pf *pf = vsi->back;
263 struct ice_hw *hw = &pf->hw;
264 enum ice_status status;
265 struct device *dev;
266 int ret = 0;
267 u8 *buf;
268
269 dev = ice_pf_to_dev(pf);
270
271 eeprom->magic = hw->vendor_id | (hw->device_id << 16);
272 netdev_dbg(netdev, "GEEPROM cmd 0x%08x, offset 0x%08x, len 0x%08x\n",
273 eeprom->cmd, eeprom->offset, eeprom->len);
274
275 buf = kzalloc(eeprom->len, GFP_KERNEL);
276 if (!buf)
277 return -ENOMEM;
278
279 status = ice_acquire_nvm(hw, ICE_RES_READ);
280 if (status) {
281 dev_err(dev, "ice_acquire_nvm failed, err %s aq_err %s\n",
282 ice_stat_str(status),
283 ice_aq_str(hw->adminq.sq_last_status));
284 ret = -EIO;
285 goto out;
286 }
287
288 status = ice_read_flat_nvm(hw, eeprom->offset, &eeprom->len, buf,
289 false);
290 if (status) {
291 dev_err(dev, "ice_read_flat_nvm failed, err %s aq_err %s\n",
292 ice_stat_str(status),
293 ice_aq_str(hw->adminq.sq_last_status));
294 ret = -EIO;
295 goto release;
296 }
297
298 memcpy(bytes, buf, eeprom->len);
299 release:
300 ice_release_nvm(hw);
301 out:
302 kfree(buf);
303 return ret;
304 }
305
306 /**
307 * ice_active_vfs - check if there are any active VFs
308 * @pf: board private structure
309 *
310 * Returns true if an active VF is found, otherwise returns false
311 */
ice_active_vfs(struct ice_pf * pf)312 static bool ice_active_vfs(struct ice_pf *pf)
313 {
314 unsigned int i;
315
316 ice_for_each_vf(pf, i) {
317 struct ice_vf *vf = &pf->vf[i];
318
319 if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
320 return true;
321 }
322
323 return false;
324 }
325
326 /**
327 * ice_link_test - perform a link test on a given net_device
328 * @netdev: network interface device structure
329 *
330 * This function performs one of the self-tests required by ethtool.
331 * Returns 0 on success, non-zero on failure.
332 */
ice_link_test(struct net_device * netdev)333 static u64 ice_link_test(struct net_device *netdev)
334 {
335 struct ice_netdev_priv *np = netdev_priv(netdev);
336 enum ice_status status;
337 bool link_up = false;
338
339 netdev_info(netdev, "link test\n");
340 status = ice_get_link_status(np->vsi->port_info, &link_up);
341 if (status) {
342 netdev_err(netdev, "link query error, status = %s\n",
343 ice_stat_str(status));
344 return 1;
345 }
346
347 if (!link_up)
348 return 2;
349
350 return 0;
351 }
352
353 /**
354 * ice_eeprom_test - perform an EEPROM test on a given net_device
355 * @netdev: network interface device structure
356 *
357 * This function performs one of the self-tests required by ethtool.
358 * Returns 0 on success, non-zero on failure.
359 */
ice_eeprom_test(struct net_device * netdev)360 static u64 ice_eeprom_test(struct net_device *netdev)
361 {
362 struct ice_netdev_priv *np = netdev_priv(netdev);
363 struct ice_pf *pf = np->vsi->back;
364
365 netdev_info(netdev, "EEPROM test\n");
366 return !!(ice_nvm_validate_checksum(&pf->hw));
367 }
368
369 /**
370 * ice_reg_pattern_test
371 * @hw: pointer to the HW struct
372 * @reg: reg to be tested
373 * @mask: bits to be touched
374 */
ice_reg_pattern_test(struct ice_hw * hw,u32 reg,u32 mask)375 static int ice_reg_pattern_test(struct ice_hw *hw, u32 reg, u32 mask)
376 {
377 struct ice_pf *pf = (struct ice_pf *)hw->back;
378 struct device *dev = ice_pf_to_dev(pf);
379 static const u32 patterns[] = {
380 0x5A5A5A5A, 0xA5A5A5A5,
381 0x00000000, 0xFFFFFFFF
382 };
383 u32 val, orig_val;
384 unsigned int i;
385
386 orig_val = rd32(hw, reg);
387 for (i = 0; i < ARRAY_SIZE(patterns); ++i) {
388 u32 pattern = patterns[i] & mask;
389
390 wr32(hw, reg, pattern);
391 val = rd32(hw, reg);
392 if (val == pattern)
393 continue;
394 dev_err(dev, "%s: reg pattern test failed - reg 0x%08x pat 0x%08x val 0x%08x\n"
395 , __func__, reg, pattern, val);
396 return 1;
397 }
398
399 wr32(hw, reg, orig_val);
400 val = rd32(hw, reg);
401 if (val != orig_val) {
402 dev_err(dev, "%s: reg restore test failed - reg 0x%08x orig 0x%08x val 0x%08x\n"
403 , __func__, reg, orig_val, val);
404 return 1;
405 }
406
407 return 0;
408 }
409
410 /**
411 * ice_reg_test - perform a register test on a given net_device
412 * @netdev: network interface device structure
413 *
414 * This function performs one of the self-tests required by ethtool.
415 * Returns 0 on success, non-zero on failure.
416 */
ice_reg_test(struct net_device * netdev)417 static u64 ice_reg_test(struct net_device *netdev)
418 {
419 struct ice_netdev_priv *np = netdev_priv(netdev);
420 struct ice_hw *hw = np->vsi->port_info->hw;
421 u32 int_elements = hw->func_caps.common_cap.num_msix_vectors ?
422 hw->func_caps.common_cap.num_msix_vectors - 1 : 1;
423 struct ice_diag_reg_test_info {
424 u32 address;
425 u32 mask;
426 u32 elem_num;
427 u32 elem_size;
428 } ice_reg_list[] = {
429 {GLINT_ITR(0, 0), 0x00000fff, int_elements,
430 GLINT_ITR(0, 1) - GLINT_ITR(0, 0)},
431 {GLINT_ITR(1, 0), 0x00000fff, int_elements,
432 GLINT_ITR(1, 1) - GLINT_ITR(1, 0)},
433 {GLINT_ITR(0, 0), 0x00000fff, int_elements,
434 GLINT_ITR(2, 1) - GLINT_ITR(2, 0)},
435 {GLINT_CTL, 0xffff0001, 1, 0}
436 };
437 unsigned int i;
438
439 netdev_dbg(netdev, "Register test\n");
440 for (i = 0; i < ARRAY_SIZE(ice_reg_list); ++i) {
441 u32 j;
442
443 for (j = 0; j < ice_reg_list[i].elem_num; ++j) {
444 u32 mask = ice_reg_list[i].mask;
445 u32 reg = ice_reg_list[i].address +
446 (j * ice_reg_list[i].elem_size);
447
448 /* bail on failure (non-zero return) */
449 if (ice_reg_pattern_test(hw, reg, mask))
450 return 1;
451 }
452 }
453
454 return 0;
455 }
456
457 /**
458 * ice_lbtest_prepare_rings - configure Tx/Rx test rings
459 * @vsi: pointer to the VSI structure
460 *
461 * Function configures rings of a VSI for loopback test without
462 * enabling interrupts or informing the kernel about new queues.
463 *
464 * Returns 0 on success, negative on failure.
465 */
ice_lbtest_prepare_rings(struct ice_vsi * vsi)466 static int ice_lbtest_prepare_rings(struct ice_vsi *vsi)
467 {
468 int status;
469
470 status = ice_vsi_setup_tx_rings(vsi);
471 if (status)
472 goto err_setup_tx_ring;
473
474 status = ice_vsi_setup_rx_rings(vsi);
475 if (status)
476 goto err_setup_rx_ring;
477
478 status = ice_vsi_cfg(vsi);
479 if (status)
480 goto err_setup_rx_ring;
481
482 status = ice_vsi_start_all_rx_rings(vsi);
483 if (status)
484 goto err_start_rx_ring;
485
486 return status;
487
488 err_start_rx_ring:
489 ice_vsi_free_rx_rings(vsi);
490 err_setup_rx_ring:
491 ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
492 err_setup_tx_ring:
493 ice_vsi_free_tx_rings(vsi);
494
495 return status;
496 }
497
498 /**
499 * ice_lbtest_disable_rings - disable Tx/Rx test rings after loopback test
500 * @vsi: pointer to the VSI structure
501 *
502 * Function stops and frees VSI rings after a loopback test.
503 * Returns 0 on success, negative on failure.
504 */
ice_lbtest_disable_rings(struct ice_vsi * vsi)505 static int ice_lbtest_disable_rings(struct ice_vsi *vsi)
506 {
507 int status;
508
509 status = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
510 if (status)
511 netdev_err(vsi->netdev, "Failed to stop Tx rings, VSI %d error %d\n",
512 vsi->vsi_num, status);
513
514 status = ice_vsi_stop_all_rx_rings(vsi);
515 if (status)
516 netdev_err(vsi->netdev, "Failed to stop Rx rings, VSI %d error %d\n",
517 vsi->vsi_num, status);
518
519 ice_vsi_free_tx_rings(vsi);
520 ice_vsi_free_rx_rings(vsi);
521
522 return status;
523 }
524
525 /**
526 * ice_lbtest_create_frame - create test packet
527 * @pf: pointer to the PF structure
528 * @ret_data: allocated frame buffer
529 * @size: size of the packet data
530 *
531 * Function allocates a frame with a test pattern on specific offsets.
532 * Returns 0 on success, non-zero on failure.
533 */
ice_lbtest_create_frame(struct ice_pf * pf,u8 ** ret_data,u16 size)534 static int ice_lbtest_create_frame(struct ice_pf *pf, u8 **ret_data, u16 size)
535 {
536 u8 *data;
537
538 if (!pf)
539 return -EINVAL;
540
541 data = devm_kzalloc(ice_pf_to_dev(pf), size, GFP_KERNEL);
542 if (!data)
543 return -ENOMEM;
544
545 /* Since the ethernet test frame should always be at least
546 * 64 bytes long, fill some octets in the payload with test data.
547 */
548 memset(data, 0xFF, size);
549 data[32] = 0xDE;
550 data[42] = 0xAD;
551 data[44] = 0xBE;
552 data[46] = 0xEF;
553
554 *ret_data = data;
555
556 return 0;
557 }
558
559 /**
560 * ice_lbtest_check_frame - verify received loopback frame
561 * @frame: pointer to the raw packet data
562 *
563 * Function verifies received test frame with a pattern.
564 * Returns true if frame matches the pattern, false otherwise.
565 */
ice_lbtest_check_frame(u8 * frame)566 static bool ice_lbtest_check_frame(u8 *frame)
567 {
568 /* Validate bytes of a frame under offsets chosen earlier */
569 if (frame[32] == 0xDE &&
570 frame[42] == 0xAD &&
571 frame[44] == 0xBE &&
572 frame[46] == 0xEF &&
573 frame[48] == 0xFF)
574 return true;
575
576 return false;
577 }
578
579 /**
580 * ice_diag_send - send test frames to the test ring
581 * @tx_ring: pointer to the transmit ring
582 * @data: pointer to the raw packet data
583 * @size: size of the packet to send
584 *
585 * Function sends loopback packets on a test Tx ring.
586 */
ice_diag_send(struct ice_ring * tx_ring,u8 * data,u16 size)587 static int ice_diag_send(struct ice_ring *tx_ring, u8 *data, u16 size)
588 {
589 struct ice_tx_desc *tx_desc;
590 struct ice_tx_buf *tx_buf;
591 dma_addr_t dma;
592 u64 td_cmd;
593
594 tx_desc = ICE_TX_DESC(tx_ring, tx_ring->next_to_use);
595 tx_buf = &tx_ring->tx_buf[tx_ring->next_to_use];
596
597 dma = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE);
598 if (dma_mapping_error(tx_ring->dev, dma))
599 return -EINVAL;
600
601 tx_desc->buf_addr = cpu_to_le64(dma);
602
603 /* These flags are required for a descriptor to be pushed out */
604 td_cmd = (u64)(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS);
605 tx_desc->cmd_type_offset_bsz =
606 cpu_to_le64(ICE_TX_DESC_DTYPE_DATA |
607 (td_cmd << ICE_TXD_QW1_CMD_S) |
608 ((u64)0 << ICE_TXD_QW1_OFFSET_S) |
609 ((u64)size << ICE_TXD_QW1_TX_BUF_SZ_S) |
610 ((u64)0 << ICE_TXD_QW1_L2TAG1_S));
611
612 tx_buf->next_to_watch = tx_desc;
613
614 /* Force memory write to complete before letting h/w know
615 * there are new descriptors to fetch.
616 */
617 wmb();
618
619 tx_ring->next_to_use++;
620 if (tx_ring->next_to_use >= tx_ring->count)
621 tx_ring->next_to_use = 0;
622
623 writel_relaxed(tx_ring->next_to_use, tx_ring->tail);
624
625 /* Wait until the packets get transmitted to the receive queue. */
626 usleep_range(1000, 2000);
627 dma_unmap_single(tx_ring->dev, dma, size, DMA_TO_DEVICE);
628
629 return 0;
630 }
631
632 #define ICE_LB_FRAME_SIZE 64
633 /**
634 * ice_lbtest_receive_frames - receive and verify test frames
635 * @rx_ring: pointer to the receive ring
636 *
637 * Function receives loopback packets and verify their correctness.
638 * Returns number of received valid frames.
639 */
ice_lbtest_receive_frames(struct ice_ring * rx_ring)640 static int ice_lbtest_receive_frames(struct ice_ring *rx_ring)
641 {
642 struct ice_rx_buf *rx_buf;
643 int valid_frames, i;
644 u8 *received_buf;
645
646 valid_frames = 0;
647
648 for (i = 0; i < rx_ring->count; i++) {
649 union ice_32b_rx_flex_desc *rx_desc;
650
651 rx_desc = ICE_RX_DESC(rx_ring, i);
652
653 if (!(rx_desc->wb.status_error0 &
654 (cpu_to_le16(BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S)) |
655 cpu_to_le16(BIT(ICE_RX_FLEX_DESC_STATUS0_EOF_S)))))
656 continue;
657
658 rx_buf = &rx_ring->rx_buf[i];
659 received_buf = page_address(rx_buf->page) + rx_buf->page_offset;
660
661 if (ice_lbtest_check_frame(received_buf))
662 valid_frames++;
663 }
664
665 return valid_frames;
666 }
667
668 /**
669 * ice_loopback_test - perform a loopback test on a given net_device
670 * @netdev: network interface device structure
671 *
672 * This function performs one of the self-tests required by ethtool.
673 * Returns 0 on success, non-zero on failure.
674 */
ice_loopback_test(struct net_device * netdev)675 static u64 ice_loopback_test(struct net_device *netdev)
676 {
677 struct ice_netdev_priv *np = netdev_priv(netdev);
678 struct ice_vsi *orig_vsi = np->vsi, *test_vsi;
679 struct ice_pf *pf = orig_vsi->back;
680 struct ice_ring *tx_ring, *rx_ring;
681 u8 broadcast[ETH_ALEN], ret = 0;
682 int num_frames, valid_frames;
683 struct device *dev;
684 u8 *tx_frame;
685 int i;
686
687 dev = ice_pf_to_dev(pf);
688 netdev_info(netdev, "loopback test\n");
689
690 test_vsi = ice_lb_vsi_setup(pf, pf->hw.port_info);
691 if (!test_vsi) {
692 netdev_err(netdev, "Failed to create a VSI for the loopback test\n");
693 return 1;
694 }
695
696 test_vsi->netdev = netdev;
697 tx_ring = test_vsi->tx_rings[0];
698 rx_ring = test_vsi->rx_rings[0];
699
700 if (ice_lbtest_prepare_rings(test_vsi)) {
701 ret = 2;
702 goto lbtest_vsi_close;
703 }
704
705 if (ice_alloc_rx_bufs(rx_ring, rx_ring->count)) {
706 ret = 3;
707 goto lbtest_rings_dis;
708 }
709
710 /* Enable MAC loopback in firmware */
711 if (ice_aq_set_mac_loopback(&pf->hw, true, NULL)) {
712 ret = 4;
713 goto lbtest_mac_dis;
714 }
715
716 /* Test VSI needs to receive broadcast packets */
717 eth_broadcast_addr(broadcast);
718 if (ice_fltr_add_mac(test_vsi, broadcast, ICE_FWD_TO_VSI)) {
719 ret = 5;
720 goto lbtest_mac_dis;
721 }
722
723 if (ice_lbtest_create_frame(pf, &tx_frame, ICE_LB_FRAME_SIZE)) {
724 ret = 7;
725 goto remove_mac_filters;
726 }
727
728 num_frames = min_t(int, tx_ring->count, 32);
729 for (i = 0; i < num_frames; i++) {
730 if (ice_diag_send(tx_ring, tx_frame, ICE_LB_FRAME_SIZE)) {
731 ret = 8;
732 goto lbtest_free_frame;
733 }
734 }
735
736 valid_frames = ice_lbtest_receive_frames(rx_ring);
737 if (!valid_frames)
738 ret = 9;
739 else if (valid_frames != num_frames)
740 ret = 10;
741
742 lbtest_free_frame:
743 devm_kfree(dev, tx_frame);
744 remove_mac_filters:
745 if (ice_fltr_remove_mac(test_vsi, broadcast, ICE_FWD_TO_VSI))
746 netdev_err(netdev, "Could not remove MAC filter for the test VSI\n");
747 lbtest_mac_dis:
748 /* Disable MAC loopback after the test is completed. */
749 if (ice_aq_set_mac_loopback(&pf->hw, false, NULL))
750 netdev_err(netdev, "Could not disable MAC loopback\n");
751 lbtest_rings_dis:
752 if (ice_lbtest_disable_rings(test_vsi))
753 netdev_err(netdev, "Could not disable test rings\n");
754 lbtest_vsi_close:
755 test_vsi->netdev = NULL;
756 if (ice_vsi_release(test_vsi))
757 netdev_err(netdev, "Failed to remove the test VSI\n");
758
759 return ret;
760 }
761
762 /**
763 * ice_intr_test - perform an interrupt test on a given net_device
764 * @netdev: network interface device structure
765 *
766 * This function performs one of the self-tests required by ethtool.
767 * Returns 0 on success, non-zero on failure.
768 */
ice_intr_test(struct net_device * netdev)769 static u64 ice_intr_test(struct net_device *netdev)
770 {
771 struct ice_netdev_priv *np = netdev_priv(netdev);
772 struct ice_pf *pf = np->vsi->back;
773 u16 swic_old = pf->sw_int_count;
774
775 netdev_info(netdev, "interrupt test\n");
776
777 wr32(&pf->hw, GLINT_DYN_CTL(pf->oicr_idx),
778 GLINT_DYN_CTL_SW_ITR_INDX_M |
779 GLINT_DYN_CTL_INTENA_MSK_M |
780 GLINT_DYN_CTL_SWINT_TRIG_M);
781
782 usleep_range(1000, 2000);
783 return (swic_old == pf->sw_int_count);
784 }
785
786 /**
787 * ice_self_test - handler function for performing a self-test by ethtool
788 * @netdev: network interface device structure
789 * @eth_test: ethtool_test structure
790 * @data: required by ethtool.self_test
791 *
792 * This function is called after invoking 'ethtool -t devname' command where
793 * devname is the name of the network device on which ethtool should operate.
794 * It performs a set of self-tests to check if a device works properly.
795 */
796 static void
ice_self_test(struct net_device * netdev,struct ethtool_test * eth_test,u64 * data)797 ice_self_test(struct net_device *netdev, struct ethtool_test *eth_test,
798 u64 *data)
799 {
800 struct ice_netdev_priv *np = netdev_priv(netdev);
801 bool if_running = netif_running(netdev);
802 struct ice_pf *pf = np->vsi->back;
803 struct device *dev;
804
805 dev = ice_pf_to_dev(pf);
806
807 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
808 netdev_info(netdev, "offline testing starting\n");
809
810 set_bit(ICE_TESTING, pf->state);
811
812 if (ice_active_vfs(pf)) {
813 dev_warn(dev, "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
814 data[ICE_ETH_TEST_REG] = 1;
815 data[ICE_ETH_TEST_EEPROM] = 1;
816 data[ICE_ETH_TEST_INTR] = 1;
817 data[ICE_ETH_TEST_LOOP] = 1;
818 data[ICE_ETH_TEST_LINK] = 1;
819 eth_test->flags |= ETH_TEST_FL_FAILED;
820 clear_bit(ICE_TESTING, pf->state);
821 goto skip_ol_tests;
822 }
823 /* If the device is online then take it offline */
824 if (if_running)
825 /* indicate we're in test mode */
826 ice_stop(netdev);
827
828 data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
829 data[ICE_ETH_TEST_EEPROM] = ice_eeprom_test(netdev);
830 data[ICE_ETH_TEST_INTR] = ice_intr_test(netdev);
831 data[ICE_ETH_TEST_LOOP] = ice_loopback_test(netdev);
832 data[ICE_ETH_TEST_REG] = ice_reg_test(netdev);
833
834 if (data[ICE_ETH_TEST_LINK] ||
835 data[ICE_ETH_TEST_EEPROM] ||
836 data[ICE_ETH_TEST_LOOP] ||
837 data[ICE_ETH_TEST_INTR] ||
838 data[ICE_ETH_TEST_REG])
839 eth_test->flags |= ETH_TEST_FL_FAILED;
840
841 clear_bit(ICE_TESTING, pf->state);
842
843 if (if_running) {
844 int status = ice_open(netdev);
845
846 if (status) {
847 dev_err(dev, "Could not open device %s, err %d\n",
848 pf->int_name, status);
849 }
850 }
851 } else {
852 /* Online tests */
853 netdev_info(netdev, "online testing starting\n");
854
855 data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
856 if (data[ICE_ETH_TEST_LINK])
857 eth_test->flags |= ETH_TEST_FL_FAILED;
858
859 /* Offline only tests, not run in online; pass by default */
860 data[ICE_ETH_TEST_REG] = 0;
861 data[ICE_ETH_TEST_EEPROM] = 0;
862 data[ICE_ETH_TEST_INTR] = 0;
863 data[ICE_ETH_TEST_LOOP] = 0;
864 }
865
866 skip_ol_tests:
867 netdev_info(netdev, "testing finished\n");
868 }
869
ice_get_strings(struct net_device * netdev,u32 stringset,u8 * data)870 static void ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
871 {
872 struct ice_netdev_priv *np = netdev_priv(netdev);
873 struct ice_vsi *vsi = np->vsi;
874 unsigned int i;
875 u8 *p = data;
876
877 switch (stringset) {
878 case ETH_SS_STATS:
879 for (i = 0; i < ICE_VSI_STATS_LEN; i++)
880 ethtool_sprintf(&p,
881 ice_gstrings_vsi_stats[i].stat_string);
882
883 ice_for_each_alloc_txq(vsi, i) {
884 ethtool_sprintf(&p, "tx_queue_%u_packets", i);
885 ethtool_sprintf(&p, "tx_queue_%u_bytes", i);
886 }
887
888 ice_for_each_alloc_rxq(vsi, i) {
889 ethtool_sprintf(&p, "rx_queue_%u_packets", i);
890 ethtool_sprintf(&p, "rx_queue_%u_bytes", i);
891 }
892
893 if (vsi->type != ICE_VSI_PF)
894 return;
895
896 for (i = 0; i < ICE_PF_STATS_LEN; i++)
897 ethtool_sprintf(&p,
898 ice_gstrings_pf_stats[i].stat_string);
899
900 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
901 ethtool_sprintf(&p, "tx_priority_%u_xon.nic", i);
902 ethtool_sprintf(&p, "tx_priority_%u_xoff.nic", i);
903 }
904 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
905 ethtool_sprintf(&p, "rx_priority_%u_xon.nic", i);
906 ethtool_sprintf(&p, "rx_priority_%u_xoff.nic", i);
907 }
908 break;
909 case ETH_SS_TEST:
910 memcpy(data, ice_gstrings_test, ICE_TEST_LEN * ETH_GSTRING_LEN);
911 break;
912 case ETH_SS_PRIV_FLAGS:
913 for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++)
914 ethtool_sprintf(&p, ice_gstrings_priv_flags[i].name);
915 break;
916 default:
917 break;
918 }
919 }
920
921 static int
ice_set_phys_id(struct net_device * netdev,enum ethtool_phys_id_state state)922 ice_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
923 {
924 struct ice_netdev_priv *np = netdev_priv(netdev);
925 bool led_active;
926
927 switch (state) {
928 case ETHTOOL_ID_ACTIVE:
929 led_active = true;
930 break;
931 case ETHTOOL_ID_INACTIVE:
932 led_active = false;
933 break;
934 default:
935 return -EINVAL;
936 }
937
938 if (ice_aq_set_port_id_led(np->vsi->port_info, !led_active, NULL))
939 return -EIO;
940
941 return 0;
942 }
943
944 /**
945 * ice_set_fec_cfg - Set link FEC options
946 * @netdev: network interface device structure
947 * @req_fec: FEC mode to configure
948 */
ice_set_fec_cfg(struct net_device * netdev,enum ice_fec_mode req_fec)949 static int ice_set_fec_cfg(struct net_device *netdev, enum ice_fec_mode req_fec)
950 {
951 struct ice_netdev_priv *np = netdev_priv(netdev);
952 struct ice_aqc_set_phy_cfg_data config = { 0 };
953 struct ice_vsi *vsi = np->vsi;
954 struct ice_port_info *pi;
955
956 pi = vsi->port_info;
957 if (!pi)
958 return -EOPNOTSUPP;
959
960 /* Changing the FEC parameters is not supported if not the PF VSI */
961 if (vsi->type != ICE_VSI_PF) {
962 netdev_info(netdev, "Changing FEC parameters only supported for PF VSI\n");
963 return -EOPNOTSUPP;
964 }
965
966 /* Proceed only if requesting different FEC mode */
967 if (pi->phy.curr_user_fec_req == req_fec)
968 return 0;
969
970 /* Copy the current user PHY configuration. The current user PHY
971 * configuration is initialized during probe from PHY capabilities
972 * software mode, and updated on set PHY configuration.
973 */
974 memcpy(&config, &pi->phy.curr_user_phy_cfg, sizeof(config));
975
976 ice_cfg_phy_fec(pi, &config, req_fec);
977 config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
978
979 if (ice_aq_set_phy_cfg(pi->hw, pi, &config, NULL))
980 return -EAGAIN;
981
982 /* Save requested FEC config */
983 pi->phy.curr_user_fec_req = req_fec;
984
985 return 0;
986 }
987
988 /**
989 * ice_set_fecparam - Set FEC link options
990 * @netdev: network interface device structure
991 * @fecparam: Ethtool structure to retrieve FEC parameters
992 */
993 static int
ice_set_fecparam(struct net_device * netdev,struct ethtool_fecparam * fecparam)994 ice_set_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
995 {
996 struct ice_netdev_priv *np = netdev_priv(netdev);
997 struct ice_vsi *vsi = np->vsi;
998 enum ice_fec_mode fec;
999
1000 switch (fecparam->fec) {
1001 case ETHTOOL_FEC_AUTO:
1002 fec = ICE_FEC_AUTO;
1003 break;
1004 case ETHTOOL_FEC_RS:
1005 fec = ICE_FEC_RS;
1006 break;
1007 case ETHTOOL_FEC_BASER:
1008 fec = ICE_FEC_BASER;
1009 break;
1010 case ETHTOOL_FEC_OFF:
1011 case ETHTOOL_FEC_NONE:
1012 fec = ICE_FEC_NONE;
1013 break;
1014 default:
1015 dev_warn(ice_pf_to_dev(vsi->back), "Unsupported FEC mode: %d\n",
1016 fecparam->fec);
1017 return -EINVAL;
1018 }
1019
1020 return ice_set_fec_cfg(netdev, fec);
1021 }
1022
1023 /**
1024 * ice_get_fecparam - Get link FEC options
1025 * @netdev: network interface device structure
1026 * @fecparam: Ethtool structure to retrieve FEC parameters
1027 */
1028 static int
ice_get_fecparam(struct net_device * netdev,struct ethtool_fecparam * fecparam)1029 ice_get_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
1030 {
1031 struct ice_netdev_priv *np = netdev_priv(netdev);
1032 struct ice_aqc_get_phy_caps_data *caps;
1033 struct ice_link_status *link_info;
1034 struct ice_vsi *vsi = np->vsi;
1035 struct ice_port_info *pi;
1036 enum ice_status status;
1037 int err = 0;
1038
1039 pi = vsi->port_info;
1040
1041 if (!pi)
1042 return -EOPNOTSUPP;
1043 link_info = &pi->phy.link_info;
1044
1045 /* Set FEC mode based on negotiated link info */
1046 switch (link_info->fec_info) {
1047 case ICE_AQ_LINK_25G_KR_FEC_EN:
1048 fecparam->active_fec = ETHTOOL_FEC_BASER;
1049 break;
1050 case ICE_AQ_LINK_25G_RS_528_FEC_EN:
1051 case ICE_AQ_LINK_25G_RS_544_FEC_EN:
1052 fecparam->active_fec = ETHTOOL_FEC_RS;
1053 break;
1054 default:
1055 fecparam->active_fec = ETHTOOL_FEC_OFF;
1056 break;
1057 }
1058
1059 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
1060 if (!caps)
1061 return -ENOMEM;
1062
1063 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
1064 caps, NULL);
1065 if (status) {
1066 err = -EAGAIN;
1067 goto done;
1068 }
1069
1070 /* Set supported/configured FEC modes based on PHY capability */
1071 if (caps->caps & ICE_AQC_PHY_EN_AUTO_FEC)
1072 fecparam->fec |= ETHTOOL_FEC_AUTO;
1073 if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
1074 caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
1075 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN ||
1076 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
1077 fecparam->fec |= ETHTOOL_FEC_BASER;
1078 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
1079 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ ||
1080 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
1081 fecparam->fec |= ETHTOOL_FEC_RS;
1082 if (caps->link_fec_options == 0)
1083 fecparam->fec |= ETHTOOL_FEC_OFF;
1084
1085 done:
1086 kfree(caps);
1087 return err;
1088 }
1089
1090 /**
1091 * ice_nway_reset - restart autonegotiation
1092 * @netdev: network interface device structure
1093 */
ice_nway_reset(struct net_device * netdev)1094 static int ice_nway_reset(struct net_device *netdev)
1095 {
1096 struct ice_netdev_priv *np = netdev_priv(netdev);
1097 struct ice_vsi *vsi = np->vsi;
1098 int err;
1099
1100 /* If VSI state is up, then restart autoneg with link up */
1101 if (!test_bit(ICE_DOWN, vsi->back->state))
1102 err = ice_set_link(vsi, true);
1103 else
1104 err = ice_set_link(vsi, false);
1105
1106 return err;
1107 }
1108
1109 /**
1110 * ice_get_priv_flags - report device private flags
1111 * @netdev: network interface device structure
1112 *
1113 * The get string set count and the string set should be matched for each
1114 * flag returned. Add new strings for each flag to the ice_gstrings_priv_flags
1115 * array.
1116 *
1117 * Returns a u32 bitmap of flags.
1118 */
ice_get_priv_flags(struct net_device * netdev)1119 static u32 ice_get_priv_flags(struct net_device *netdev)
1120 {
1121 struct ice_netdev_priv *np = netdev_priv(netdev);
1122 struct ice_vsi *vsi = np->vsi;
1123 struct ice_pf *pf = vsi->back;
1124 u32 i, ret_flags = 0;
1125
1126 for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
1127 const struct ice_priv_flag *priv_flag;
1128
1129 priv_flag = &ice_gstrings_priv_flags[i];
1130
1131 if (test_bit(priv_flag->bitno, pf->flags))
1132 ret_flags |= BIT(i);
1133 }
1134
1135 return ret_flags;
1136 }
1137
1138 /**
1139 * ice_set_priv_flags - set private flags
1140 * @netdev: network interface device structure
1141 * @flags: bit flags to be set
1142 */
ice_set_priv_flags(struct net_device * netdev,u32 flags)1143 static int ice_set_priv_flags(struct net_device *netdev, u32 flags)
1144 {
1145 struct ice_netdev_priv *np = netdev_priv(netdev);
1146 DECLARE_BITMAP(change_flags, ICE_PF_FLAGS_NBITS);
1147 DECLARE_BITMAP(orig_flags, ICE_PF_FLAGS_NBITS);
1148 struct ice_vsi *vsi = np->vsi;
1149 struct ice_pf *pf = vsi->back;
1150 struct device *dev;
1151 int ret = 0;
1152 u32 i;
1153
1154 if (flags > BIT(ICE_PRIV_FLAG_ARRAY_SIZE))
1155 return -EINVAL;
1156
1157 dev = ice_pf_to_dev(pf);
1158 set_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
1159
1160 bitmap_copy(orig_flags, pf->flags, ICE_PF_FLAGS_NBITS);
1161 for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
1162 const struct ice_priv_flag *priv_flag;
1163
1164 priv_flag = &ice_gstrings_priv_flags[i];
1165
1166 if (flags & BIT(i))
1167 set_bit(priv_flag->bitno, pf->flags);
1168 else
1169 clear_bit(priv_flag->bitno, pf->flags);
1170 }
1171
1172 bitmap_xor(change_flags, pf->flags, orig_flags, ICE_PF_FLAGS_NBITS);
1173
1174 /* Do not allow change to link-down-on-close when Total Port Shutdown
1175 * is enabled.
1176 */
1177 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, change_flags) &&
1178 test_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
1179 dev_err(dev, "Setting link-down-on-close not supported on this port\n");
1180 set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
1181 ret = -EINVAL;
1182 goto ethtool_exit;
1183 }
1184
1185 if (test_bit(ICE_FLAG_FW_LLDP_AGENT, change_flags)) {
1186 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags)) {
1187 enum ice_status status;
1188
1189 /* Disable FW LLDP engine */
1190 status = ice_cfg_lldp_mib_change(&pf->hw, false);
1191
1192 /* If unregistering for LLDP events fails, this is
1193 * not an error state, as there shouldn't be any
1194 * events to respond to.
1195 */
1196 if (status)
1197 dev_info(dev, "Failed to unreg for LLDP events\n");
1198
1199 /* The AQ call to stop the FW LLDP agent will generate
1200 * an error if the agent is already stopped.
1201 */
1202 status = ice_aq_stop_lldp(&pf->hw, true, true, NULL);
1203 if (status)
1204 dev_warn(dev, "Fail to stop LLDP agent\n");
1205 /* Use case for having the FW LLDP agent stopped
1206 * will likely not need DCB, so failure to init is
1207 * not a concern of ethtool
1208 */
1209 status = ice_init_pf_dcb(pf, true);
1210 if (status)
1211 dev_warn(dev, "Fail to init DCB\n");
1212
1213 pf->dcbx_cap &= ~DCB_CAP_DCBX_LLD_MANAGED;
1214 pf->dcbx_cap |= DCB_CAP_DCBX_HOST;
1215 } else {
1216 enum ice_status status;
1217 bool dcbx_agent_status;
1218
1219 /* Remove rule to direct LLDP packets to default VSI.
1220 * The FW LLDP engine will now be consuming them.
1221 */
1222 ice_cfg_sw_lldp(vsi, false, false);
1223
1224 /* AQ command to start FW LLDP agent will return an
1225 * error if the agent is already started
1226 */
1227 status = ice_aq_start_lldp(&pf->hw, true, NULL);
1228 if (status)
1229 dev_warn(dev, "Fail to start LLDP Agent\n");
1230
1231 /* AQ command to start FW DCBX agent will fail if
1232 * the agent is already started
1233 */
1234 status = ice_aq_start_stop_dcbx(&pf->hw, true,
1235 &dcbx_agent_status,
1236 NULL);
1237 if (status)
1238 dev_dbg(dev, "Failed to start FW DCBX\n");
1239
1240 dev_info(dev, "FW DCBX agent is %s\n",
1241 dcbx_agent_status ? "ACTIVE" : "DISABLED");
1242
1243 /* Failure to configure MIB change or init DCB is not
1244 * relevant to ethtool. Print notification that
1245 * registration/init failed but do not return error
1246 * state to ethtool
1247 */
1248 status = ice_init_pf_dcb(pf, true);
1249 if (status)
1250 dev_dbg(dev, "Fail to init DCB\n");
1251
1252 /* Register for MIB change events */
1253 status = ice_cfg_lldp_mib_change(&pf->hw, true);
1254 if (status)
1255 dev_dbg(dev, "Fail to enable MIB change events\n");
1256
1257 pf->dcbx_cap &= ~DCB_CAP_DCBX_HOST;
1258 pf->dcbx_cap |= DCB_CAP_DCBX_LLD_MANAGED;
1259
1260 ice_nway_reset(netdev);
1261 }
1262 }
1263 if (test_bit(ICE_FLAG_LEGACY_RX, change_flags)) {
1264 /* down and up VSI so that changes of Rx cfg are reflected. */
1265 ice_down(vsi);
1266 ice_up(vsi);
1267 }
1268 /* don't allow modification of this flag when a single VF is in
1269 * promiscuous mode because it's not supported
1270 */
1271 if (test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, change_flags) &&
1272 ice_is_any_vf_in_promisc(pf)) {
1273 dev_err(dev, "Changing vf-true-promisc-support flag while VF(s) are in promiscuous mode not supported\n");
1274 /* toggle bit back to previous state */
1275 change_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags);
1276 ret = -EAGAIN;
1277 }
1278 ethtool_exit:
1279 clear_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
1280 return ret;
1281 }
1282
ice_get_sset_count(struct net_device * netdev,int sset)1283 static int ice_get_sset_count(struct net_device *netdev, int sset)
1284 {
1285 switch (sset) {
1286 case ETH_SS_STATS:
1287 /* The number (and order) of strings reported *must* remain
1288 * constant for a given netdevice. This function must not
1289 * report a different number based on run time parameters
1290 * (such as the number of queues in use, or the setting of
1291 * a private ethtool flag). This is due to the nature of the
1292 * ethtool stats API.
1293 *
1294 * Userspace programs such as ethtool must make 3 separate
1295 * ioctl requests, one for size, one for the strings, and
1296 * finally one for the stats. Since these cross into
1297 * userspace, changes to the number or size could result in
1298 * undefined memory access or incorrect string<->value
1299 * correlations for statistics.
1300 *
1301 * Even if it appears to be safe, changes to the size or
1302 * order of strings will suffer from race conditions and are
1303 * not safe.
1304 */
1305 return ICE_ALL_STATS_LEN(netdev);
1306 case ETH_SS_TEST:
1307 return ICE_TEST_LEN;
1308 case ETH_SS_PRIV_FLAGS:
1309 return ICE_PRIV_FLAG_ARRAY_SIZE;
1310 default:
1311 return -EOPNOTSUPP;
1312 }
1313 }
1314
1315 static void
ice_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats __always_unused * stats,u64 * data)1316 ice_get_ethtool_stats(struct net_device *netdev,
1317 struct ethtool_stats __always_unused *stats, u64 *data)
1318 {
1319 struct ice_netdev_priv *np = netdev_priv(netdev);
1320 struct ice_vsi *vsi = np->vsi;
1321 struct ice_pf *pf = vsi->back;
1322 struct ice_ring *ring;
1323 unsigned int j;
1324 int i = 0;
1325 char *p;
1326
1327 ice_update_pf_stats(pf);
1328 ice_update_vsi_stats(vsi);
1329
1330 for (j = 0; j < ICE_VSI_STATS_LEN; j++) {
1331 p = (char *)vsi + ice_gstrings_vsi_stats[j].stat_offset;
1332 data[i++] = (ice_gstrings_vsi_stats[j].sizeof_stat ==
1333 sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1334 }
1335
1336 /* populate per queue stats */
1337 rcu_read_lock();
1338
1339 ice_for_each_alloc_txq(vsi, j) {
1340 ring = READ_ONCE(vsi->tx_rings[j]);
1341 if (ring) {
1342 data[i++] = ring->stats.pkts;
1343 data[i++] = ring->stats.bytes;
1344 } else {
1345 data[i++] = 0;
1346 data[i++] = 0;
1347 }
1348 }
1349
1350 ice_for_each_alloc_rxq(vsi, j) {
1351 ring = READ_ONCE(vsi->rx_rings[j]);
1352 if (ring) {
1353 data[i++] = ring->stats.pkts;
1354 data[i++] = ring->stats.bytes;
1355 } else {
1356 data[i++] = 0;
1357 data[i++] = 0;
1358 }
1359 }
1360
1361 rcu_read_unlock();
1362
1363 if (vsi->type != ICE_VSI_PF)
1364 return;
1365
1366 for (j = 0; j < ICE_PF_STATS_LEN; j++) {
1367 p = (char *)pf + ice_gstrings_pf_stats[j].stat_offset;
1368 data[i++] = (ice_gstrings_pf_stats[j].sizeof_stat ==
1369 sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1370 }
1371
1372 for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
1373 data[i++] = pf->stats.priority_xon_tx[j];
1374 data[i++] = pf->stats.priority_xoff_tx[j];
1375 }
1376
1377 for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
1378 data[i++] = pf->stats.priority_xon_rx[j];
1379 data[i++] = pf->stats.priority_xoff_rx[j];
1380 }
1381 }
1382
1383 #define ICE_PHY_TYPE_LOW_MASK_MIN_1G (ICE_PHY_TYPE_LOW_100BASE_TX | \
1384 ICE_PHY_TYPE_LOW_100M_SGMII)
1385
1386 #define ICE_PHY_TYPE_LOW_MASK_MIN_25G (ICE_PHY_TYPE_LOW_MASK_MIN_1G | \
1387 ICE_PHY_TYPE_LOW_1000BASE_T | \
1388 ICE_PHY_TYPE_LOW_1000BASE_SX | \
1389 ICE_PHY_TYPE_LOW_1000BASE_LX | \
1390 ICE_PHY_TYPE_LOW_1000BASE_KX | \
1391 ICE_PHY_TYPE_LOW_1G_SGMII | \
1392 ICE_PHY_TYPE_LOW_2500BASE_T | \
1393 ICE_PHY_TYPE_LOW_2500BASE_X | \
1394 ICE_PHY_TYPE_LOW_2500BASE_KX | \
1395 ICE_PHY_TYPE_LOW_5GBASE_T | \
1396 ICE_PHY_TYPE_LOW_5GBASE_KR | \
1397 ICE_PHY_TYPE_LOW_10GBASE_T | \
1398 ICE_PHY_TYPE_LOW_10G_SFI_DA | \
1399 ICE_PHY_TYPE_LOW_10GBASE_SR | \
1400 ICE_PHY_TYPE_LOW_10GBASE_LR | \
1401 ICE_PHY_TYPE_LOW_10GBASE_KR_CR1 | \
1402 ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC | \
1403 ICE_PHY_TYPE_LOW_10G_SFI_C2C)
1404
1405 #define ICE_PHY_TYPE_LOW_MASK_100G (ICE_PHY_TYPE_LOW_100GBASE_CR4 | \
1406 ICE_PHY_TYPE_LOW_100GBASE_SR4 | \
1407 ICE_PHY_TYPE_LOW_100GBASE_LR4 | \
1408 ICE_PHY_TYPE_LOW_100GBASE_KR4 | \
1409 ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC | \
1410 ICE_PHY_TYPE_LOW_100G_CAUI4 | \
1411 ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC | \
1412 ICE_PHY_TYPE_LOW_100G_AUI4 | \
1413 ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 | \
1414 ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 | \
1415 ICE_PHY_TYPE_LOW_100GBASE_CP2 | \
1416 ICE_PHY_TYPE_LOW_100GBASE_SR2 | \
1417 ICE_PHY_TYPE_LOW_100GBASE_DR)
1418
1419 #define ICE_PHY_TYPE_HIGH_MASK_100G (ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4 | \
1420 ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC |\
1421 ICE_PHY_TYPE_HIGH_100G_CAUI2 | \
1422 ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC | \
1423 ICE_PHY_TYPE_HIGH_100G_AUI2)
1424
1425 /**
1426 * ice_mask_min_supported_speeds
1427 * @phy_types_high: PHY type high
1428 * @phy_types_low: PHY type low to apply minimum supported speeds mask
1429 *
1430 * Apply minimum supported speeds mask to PHY type low. These are the speeds
1431 * for ethtool supported link mode.
1432 */
1433 static
ice_mask_min_supported_speeds(u64 phy_types_high,u64 * phy_types_low)1434 void ice_mask_min_supported_speeds(u64 phy_types_high, u64 *phy_types_low)
1435 {
1436 /* if QSFP connection with 100G speed, minimum supported speed is 25G */
1437 if (*phy_types_low & ICE_PHY_TYPE_LOW_MASK_100G ||
1438 phy_types_high & ICE_PHY_TYPE_HIGH_MASK_100G)
1439 *phy_types_low &= ~ICE_PHY_TYPE_LOW_MASK_MIN_25G;
1440 else
1441 *phy_types_low &= ~ICE_PHY_TYPE_LOW_MASK_MIN_1G;
1442 }
1443
1444 #define ice_ethtool_advertise_link_mode(aq_link_speed, ethtool_link_mode) \
1445 do { \
1446 if (req_speeds & (aq_link_speed) || \
1447 (!req_speeds && \
1448 (advert_phy_type_lo & phy_type_mask_lo || \
1449 advert_phy_type_hi & phy_type_mask_hi))) \
1450 ethtool_link_ksettings_add_link_mode(ks, advertising,\
1451 ethtool_link_mode); \
1452 } while (0)
1453
1454 /**
1455 * ice_phy_type_to_ethtool - convert the phy_types to ethtool link modes
1456 * @netdev: network interface device structure
1457 * @ks: ethtool link ksettings struct to fill out
1458 */
1459 static void
ice_phy_type_to_ethtool(struct net_device * netdev,struct ethtool_link_ksettings * ks)1460 ice_phy_type_to_ethtool(struct net_device *netdev,
1461 struct ethtool_link_ksettings *ks)
1462 {
1463 struct ice_netdev_priv *np = netdev_priv(netdev);
1464 struct ice_vsi *vsi = np->vsi;
1465 struct ice_pf *pf = vsi->back;
1466 u64 advert_phy_type_lo = 0;
1467 u64 advert_phy_type_hi = 0;
1468 u64 phy_type_mask_lo = 0;
1469 u64 phy_type_mask_hi = 0;
1470 u64 phy_types_high = 0;
1471 u64 phy_types_low = 0;
1472 u16 req_speeds;
1473
1474 req_speeds = vsi->port_info->phy.link_info.req_speeds;
1475
1476 /* Check if lenient mode is supported and enabled, or in strict mode.
1477 *
1478 * In lenient mode the Supported link modes are the PHY types without
1479 * media. The Advertising link mode is either 1. the user requested
1480 * speed, 2. the override PHY mask, or 3. the PHY types with media.
1481 *
1482 * In strict mode Supported link mode are the PHY type with media,
1483 * and Advertising link modes are the media PHY type or the speed
1484 * requested by user.
1485 */
1486 if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags)) {
1487 phy_types_low = le64_to_cpu(pf->nvm_phy_type_lo);
1488 phy_types_high = le64_to_cpu(pf->nvm_phy_type_hi);
1489
1490 ice_mask_min_supported_speeds(phy_types_high, &phy_types_low);
1491 /* determine advertised modes based on link override only
1492 * if it's supported and if the FW doesn't abstract the
1493 * driver from having to account for link overrides
1494 */
1495 if (ice_fw_supports_link_override(&pf->hw) &&
1496 !ice_fw_supports_report_dflt_cfg(&pf->hw)) {
1497 struct ice_link_default_override_tlv *ldo;
1498
1499 ldo = &pf->link_dflt_override;
1500 /* If override enabled and PHY mask set, then
1501 * Advertising link mode is the intersection of the PHY
1502 * types without media and the override PHY mask.
1503 */
1504 if (ldo->options & ICE_LINK_OVERRIDE_EN &&
1505 (ldo->phy_type_low || ldo->phy_type_high)) {
1506 advert_phy_type_lo =
1507 le64_to_cpu(pf->nvm_phy_type_lo) &
1508 ldo->phy_type_low;
1509 advert_phy_type_hi =
1510 le64_to_cpu(pf->nvm_phy_type_hi) &
1511 ldo->phy_type_high;
1512 }
1513 }
1514 } else {
1515 /* strict mode */
1516 phy_types_low = vsi->port_info->phy.phy_type_low;
1517 phy_types_high = vsi->port_info->phy.phy_type_high;
1518 }
1519
1520 /* If Advertising link mode PHY type is not using override PHY type,
1521 * then use PHY type with media.
1522 */
1523 if (!advert_phy_type_lo && !advert_phy_type_hi) {
1524 advert_phy_type_lo = vsi->port_info->phy.phy_type_low;
1525 advert_phy_type_hi = vsi->port_info->phy.phy_type_high;
1526 }
1527
1528 ethtool_link_ksettings_zero_link_mode(ks, supported);
1529 ethtool_link_ksettings_zero_link_mode(ks, advertising);
1530
1531 phy_type_mask_lo = ICE_PHY_TYPE_LOW_100BASE_TX |
1532 ICE_PHY_TYPE_LOW_100M_SGMII;
1533 if (phy_types_low & phy_type_mask_lo) {
1534 ethtool_link_ksettings_add_link_mode(ks, supported,
1535 100baseT_Full);
1536
1537 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100MB,
1538 100baseT_Full);
1539 }
1540
1541 phy_type_mask_lo = ICE_PHY_TYPE_LOW_1000BASE_T |
1542 ICE_PHY_TYPE_LOW_1G_SGMII;
1543 if (phy_types_low & phy_type_mask_lo) {
1544 ethtool_link_ksettings_add_link_mode(ks, supported,
1545 1000baseT_Full);
1546 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_1000MB,
1547 1000baseT_Full);
1548 }
1549
1550 phy_type_mask_lo = ICE_PHY_TYPE_LOW_1000BASE_KX;
1551 if (phy_types_low & phy_type_mask_lo) {
1552 ethtool_link_ksettings_add_link_mode(ks, supported,
1553 1000baseKX_Full);
1554 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_1000MB,
1555 1000baseKX_Full);
1556 }
1557
1558 phy_type_mask_lo = ICE_PHY_TYPE_LOW_1000BASE_SX |
1559 ICE_PHY_TYPE_LOW_1000BASE_LX;
1560 if (phy_types_low & phy_type_mask_lo) {
1561 ethtool_link_ksettings_add_link_mode(ks, supported,
1562 1000baseX_Full);
1563 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_1000MB,
1564 1000baseX_Full);
1565 }
1566
1567 phy_type_mask_lo = ICE_PHY_TYPE_LOW_2500BASE_T;
1568 if (phy_types_low & phy_type_mask_lo) {
1569 ethtool_link_ksettings_add_link_mode(ks, supported,
1570 2500baseT_Full);
1571 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_2500MB,
1572 2500baseT_Full);
1573 }
1574
1575 phy_type_mask_lo = ICE_PHY_TYPE_LOW_2500BASE_X |
1576 ICE_PHY_TYPE_LOW_2500BASE_KX;
1577 if (phy_types_low & phy_type_mask_lo) {
1578 ethtool_link_ksettings_add_link_mode(ks, supported,
1579 2500baseX_Full);
1580 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_2500MB,
1581 2500baseX_Full);
1582 }
1583
1584 phy_type_mask_lo = ICE_PHY_TYPE_LOW_5GBASE_T |
1585 ICE_PHY_TYPE_LOW_5GBASE_KR;
1586 if (phy_types_low & phy_type_mask_lo) {
1587 ethtool_link_ksettings_add_link_mode(ks, supported,
1588 5000baseT_Full);
1589 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_5GB,
1590 5000baseT_Full);
1591 }
1592
1593 phy_type_mask_lo = ICE_PHY_TYPE_LOW_10GBASE_T |
1594 ICE_PHY_TYPE_LOW_10G_SFI_DA |
1595 ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC |
1596 ICE_PHY_TYPE_LOW_10G_SFI_C2C;
1597 if (phy_types_low & phy_type_mask_lo) {
1598 ethtool_link_ksettings_add_link_mode(ks, supported,
1599 10000baseT_Full);
1600 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_10GB,
1601 10000baseT_Full);
1602 }
1603
1604 phy_type_mask_lo = ICE_PHY_TYPE_LOW_10GBASE_KR_CR1;
1605 if (phy_types_low & phy_type_mask_lo) {
1606 ethtool_link_ksettings_add_link_mode(ks, supported,
1607 10000baseKR_Full);
1608 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_10GB,
1609 10000baseKR_Full);
1610 }
1611
1612 phy_type_mask_lo = ICE_PHY_TYPE_LOW_10GBASE_SR;
1613 if (phy_types_low & phy_type_mask_lo) {
1614 ethtool_link_ksettings_add_link_mode(ks, supported,
1615 10000baseSR_Full);
1616 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_10GB,
1617 10000baseSR_Full);
1618 }
1619
1620 phy_type_mask_lo = ICE_PHY_TYPE_LOW_10GBASE_LR;
1621 if (phy_types_low & phy_type_mask_lo) {
1622 ethtool_link_ksettings_add_link_mode(ks, supported,
1623 10000baseLR_Full);
1624 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_10GB,
1625 10000baseLR_Full);
1626 }
1627
1628 phy_type_mask_lo = ICE_PHY_TYPE_LOW_25GBASE_T |
1629 ICE_PHY_TYPE_LOW_25GBASE_CR |
1630 ICE_PHY_TYPE_LOW_25GBASE_CR_S |
1631 ICE_PHY_TYPE_LOW_25GBASE_CR1 |
1632 ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC |
1633 ICE_PHY_TYPE_LOW_25G_AUI_C2C;
1634 if (phy_types_low & phy_type_mask_lo) {
1635 ethtool_link_ksettings_add_link_mode(ks, supported,
1636 25000baseCR_Full);
1637 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_25GB,
1638 25000baseCR_Full);
1639 }
1640
1641 phy_type_mask_lo = ICE_PHY_TYPE_LOW_25GBASE_SR |
1642 ICE_PHY_TYPE_LOW_25GBASE_LR;
1643 if (phy_types_low & phy_type_mask_lo) {
1644 ethtool_link_ksettings_add_link_mode(ks, supported,
1645 25000baseSR_Full);
1646 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_25GB,
1647 25000baseSR_Full);
1648 }
1649
1650 phy_type_mask_lo = ICE_PHY_TYPE_LOW_25GBASE_KR |
1651 ICE_PHY_TYPE_LOW_25GBASE_KR_S |
1652 ICE_PHY_TYPE_LOW_25GBASE_KR1;
1653 if (phy_types_low & phy_type_mask_lo) {
1654 ethtool_link_ksettings_add_link_mode(ks, supported,
1655 25000baseKR_Full);
1656 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_25GB,
1657 25000baseKR_Full);
1658 }
1659
1660 phy_type_mask_lo = ICE_PHY_TYPE_LOW_40GBASE_KR4;
1661 if (phy_types_low & phy_type_mask_lo) {
1662 ethtool_link_ksettings_add_link_mode(ks, supported,
1663 40000baseKR4_Full);
1664 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_40GB,
1665 40000baseKR4_Full);
1666 }
1667
1668 phy_type_mask_lo = ICE_PHY_TYPE_LOW_40GBASE_CR4 |
1669 ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC |
1670 ICE_PHY_TYPE_LOW_40G_XLAUI;
1671 if (phy_types_low & phy_type_mask_lo) {
1672 ethtool_link_ksettings_add_link_mode(ks, supported,
1673 40000baseCR4_Full);
1674 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_40GB,
1675 40000baseCR4_Full);
1676 }
1677
1678 phy_type_mask_lo = ICE_PHY_TYPE_LOW_40GBASE_SR4;
1679 if (phy_types_low & phy_type_mask_lo) {
1680 ethtool_link_ksettings_add_link_mode(ks, supported,
1681 40000baseSR4_Full);
1682 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_40GB,
1683 40000baseSR4_Full);
1684 }
1685
1686 phy_type_mask_lo = ICE_PHY_TYPE_LOW_40GBASE_LR4;
1687 if (phy_types_low & phy_type_mask_lo) {
1688 ethtool_link_ksettings_add_link_mode(ks, supported,
1689 40000baseLR4_Full);
1690 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_40GB,
1691 40000baseLR4_Full);
1692 }
1693
1694 phy_type_mask_lo = ICE_PHY_TYPE_LOW_50GBASE_CR2 |
1695 ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC |
1696 ICE_PHY_TYPE_LOW_50G_LAUI2 |
1697 ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC |
1698 ICE_PHY_TYPE_LOW_50G_AUI2 |
1699 ICE_PHY_TYPE_LOW_50GBASE_CP |
1700 ICE_PHY_TYPE_LOW_50GBASE_SR |
1701 ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC |
1702 ICE_PHY_TYPE_LOW_50G_AUI1;
1703 if (phy_types_low & phy_type_mask_lo) {
1704 ethtool_link_ksettings_add_link_mode(ks, supported,
1705 50000baseCR2_Full);
1706 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_50GB,
1707 50000baseCR2_Full);
1708 }
1709
1710 phy_type_mask_lo = ICE_PHY_TYPE_LOW_50GBASE_KR2 |
1711 ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4;
1712 if (phy_types_low & phy_type_mask_lo) {
1713 ethtool_link_ksettings_add_link_mode(ks, supported,
1714 50000baseKR2_Full);
1715 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_50GB,
1716 50000baseKR2_Full);
1717 }
1718
1719 phy_type_mask_lo = ICE_PHY_TYPE_LOW_50GBASE_SR2 |
1720 ICE_PHY_TYPE_LOW_50GBASE_LR2 |
1721 ICE_PHY_TYPE_LOW_50GBASE_FR |
1722 ICE_PHY_TYPE_LOW_50GBASE_LR;
1723 if (phy_types_low & phy_type_mask_lo) {
1724 ethtool_link_ksettings_add_link_mode(ks, supported,
1725 50000baseSR2_Full);
1726 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_50GB,
1727 50000baseSR2_Full);
1728 }
1729
1730 phy_type_mask_lo = ICE_PHY_TYPE_LOW_100GBASE_CR4 |
1731 ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC |
1732 ICE_PHY_TYPE_LOW_100G_CAUI4 |
1733 ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC |
1734 ICE_PHY_TYPE_LOW_100G_AUI4 |
1735 ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 |
1736 ICE_PHY_TYPE_LOW_100GBASE_CP2;
1737 phy_type_mask_hi = ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC |
1738 ICE_PHY_TYPE_HIGH_100G_CAUI2 |
1739 ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC |
1740 ICE_PHY_TYPE_HIGH_100G_AUI2;
1741 if (phy_types_low & phy_type_mask_lo ||
1742 phy_types_high & phy_type_mask_hi) {
1743 ethtool_link_ksettings_add_link_mode(ks, supported,
1744 100000baseCR4_Full);
1745 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100GB,
1746 100000baseCR4_Full);
1747 }
1748
1749 phy_type_mask_lo = ICE_PHY_TYPE_LOW_100GBASE_SR4 |
1750 ICE_PHY_TYPE_LOW_100GBASE_SR2;
1751 if (phy_types_low & phy_type_mask_lo) {
1752 ethtool_link_ksettings_add_link_mode(ks, supported,
1753 100000baseSR4_Full);
1754 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100GB,
1755 100000baseSR4_Full);
1756 }
1757
1758 phy_type_mask_lo = ICE_PHY_TYPE_LOW_100GBASE_LR4 |
1759 ICE_PHY_TYPE_LOW_100GBASE_DR;
1760 if (phy_types_low & phy_type_mask_lo) {
1761 ethtool_link_ksettings_add_link_mode(ks, supported,
1762 100000baseLR4_ER4_Full);
1763 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100GB,
1764 100000baseLR4_ER4_Full);
1765 }
1766
1767 phy_type_mask_lo = ICE_PHY_TYPE_LOW_100GBASE_KR4 |
1768 ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4;
1769 phy_type_mask_hi = ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4;
1770 if (phy_types_low & phy_type_mask_lo ||
1771 phy_types_high & phy_type_mask_hi) {
1772 ethtool_link_ksettings_add_link_mode(ks, supported,
1773 100000baseKR4_Full);
1774 ice_ethtool_advertise_link_mode(ICE_AQ_LINK_SPEED_100GB,
1775 100000baseKR4_Full);
1776 }
1777 }
1778
1779 #define TEST_SET_BITS_TIMEOUT 50
1780 #define TEST_SET_BITS_SLEEP_MAX 2000
1781 #define TEST_SET_BITS_SLEEP_MIN 1000
1782
1783 /**
1784 * ice_get_settings_link_up - Get Link settings for when link is up
1785 * @ks: ethtool ksettings to fill in
1786 * @netdev: network interface device structure
1787 */
1788 static void
ice_get_settings_link_up(struct ethtool_link_ksettings * ks,struct net_device * netdev)1789 ice_get_settings_link_up(struct ethtool_link_ksettings *ks,
1790 struct net_device *netdev)
1791 {
1792 struct ice_netdev_priv *np = netdev_priv(netdev);
1793 struct ice_port_info *pi = np->vsi->port_info;
1794 struct ice_link_status *link_info;
1795 struct ice_vsi *vsi = np->vsi;
1796
1797 link_info = &vsi->port_info->phy.link_info;
1798
1799 /* Get supported and advertised settings from PHY ability with media */
1800 ice_phy_type_to_ethtool(netdev, ks);
1801
1802 switch (link_info->link_speed) {
1803 case ICE_AQ_LINK_SPEED_100GB:
1804 ks->base.speed = SPEED_100000;
1805 break;
1806 case ICE_AQ_LINK_SPEED_50GB:
1807 ks->base.speed = SPEED_50000;
1808 break;
1809 case ICE_AQ_LINK_SPEED_40GB:
1810 ks->base.speed = SPEED_40000;
1811 break;
1812 case ICE_AQ_LINK_SPEED_25GB:
1813 ks->base.speed = SPEED_25000;
1814 break;
1815 case ICE_AQ_LINK_SPEED_20GB:
1816 ks->base.speed = SPEED_20000;
1817 break;
1818 case ICE_AQ_LINK_SPEED_10GB:
1819 ks->base.speed = SPEED_10000;
1820 break;
1821 case ICE_AQ_LINK_SPEED_5GB:
1822 ks->base.speed = SPEED_5000;
1823 break;
1824 case ICE_AQ_LINK_SPEED_2500MB:
1825 ks->base.speed = SPEED_2500;
1826 break;
1827 case ICE_AQ_LINK_SPEED_1000MB:
1828 ks->base.speed = SPEED_1000;
1829 break;
1830 case ICE_AQ_LINK_SPEED_100MB:
1831 ks->base.speed = SPEED_100;
1832 break;
1833 default:
1834 netdev_info(netdev, "WARNING: Unrecognized link_speed (0x%x).\n",
1835 link_info->link_speed);
1836 break;
1837 }
1838 ks->base.duplex = DUPLEX_FULL;
1839
1840 if (link_info->an_info & ICE_AQ_AN_COMPLETED)
1841 ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
1842 Autoneg);
1843
1844 /* Set flow control negotiated Rx/Tx pause */
1845 switch (pi->fc.current_mode) {
1846 case ICE_FC_FULL:
1847 ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
1848 break;
1849 case ICE_FC_TX_PAUSE:
1850 ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
1851 ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
1852 Asym_Pause);
1853 break;
1854 case ICE_FC_RX_PAUSE:
1855 ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
1856 Asym_Pause);
1857 break;
1858 case ICE_FC_PFC:
1859 default:
1860 ethtool_link_ksettings_del_link_mode(ks, lp_advertising, Pause);
1861 ethtool_link_ksettings_del_link_mode(ks, lp_advertising,
1862 Asym_Pause);
1863 break;
1864 }
1865 }
1866
1867 /**
1868 * ice_get_settings_link_down - Get the Link settings when link is down
1869 * @ks: ethtool ksettings to fill in
1870 * @netdev: network interface device structure
1871 *
1872 * Reports link settings that can be determined when link is down
1873 */
1874 static void
ice_get_settings_link_down(struct ethtool_link_ksettings * ks,struct net_device * netdev)1875 ice_get_settings_link_down(struct ethtool_link_ksettings *ks,
1876 struct net_device *netdev)
1877 {
1878 /* link is down and the driver needs to fall back on
1879 * supported PHY types to figure out what info to display
1880 */
1881 ice_phy_type_to_ethtool(netdev, ks);
1882
1883 /* With no link, speed and duplex are unknown */
1884 ks->base.speed = SPEED_UNKNOWN;
1885 ks->base.duplex = DUPLEX_UNKNOWN;
1886 }
1887
1888 /**
1889 * ice_get_link_ksettings - Get Link Speed and Duplex settings
1890 * @netdev: network interface device structure
1891 * @ks: ethtool ksettings
1892 *
1893 * Reports speed/duplex settings based on media_type
1894 */
1895 static int
ice_get_link_ksettings(struct net_device * netdev,struct ethtool_link_ksettings * ks)1896 ice_get_link_ksettings(struct net_device *netdev,
1897 struct ethtool_link_ksettings *ks)
1898 {
1899 struct ice_netdev_priv *np = netdev_priv(netdev);
1900 struct ice_aqc_get_phy_caps_data *caps;
1901 struct ice_link_status *hw_link_info;
1902 struct ice_vsi *vsi = np->vsi;
1903 enum ice_status status;
1904 int err = 0;
1905
1906 ethtool_link_ksettings_zero_link_mode(ks, supported);
1907 ethtool_link_ksettings_zero_link_mode(ks, advertising);
1908 ethtool_link_ksettings_zero_link_mode(ks, lp_advertising);
1909 hw_link_info = &vsi->port_info->phy.link_info;
1910
1911 /* set speed and duplex */
1912 if (hw_link_info->link_info & ICE_AQ_LINK_UP)
1913 ice_get_settings_link_up(ks, netdev);
1914 else
1915 ice_get_settings_link_down(ks, netdev);
1916
1917 /* set autoneg settings */
1918 ks->base.autoneg = (hw_link_info->an_info & ICE_AQ_AN_COMPLETED) ?
1919 AUTONEG_ENABLE : AUTONEG_DISABLE;
1920
1921 /* set media type settings */
1922 switch (vsi->port_info->phy.media_type) {
1923 case ICE_MEDIA_FIBER:
1924 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
1925 ks->base.port = PORT_FIBRE;
1926 break;
1927 case ICE_MEDIA_BASET:
1928 ethtool_link_ksettings_add_link_mode(ks, supported, TP);
1929 ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
1930 ks->base.port = PORT_TP;
1931 break;
1932 case ICE_MEDIA_BACKPLANE:
1933 ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
1934 ethtool_link_ksettings_add_link_mode(ks, advertising,
1935 Backplane);
1936 ks->base.port = PORT_NONE;
1937 break;
1938 case ICE_MEDIA_DA:
1939 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
1940 ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
1941 ks->base.port = PORT_DA;
1942 break;
1943 default:
1944 ks->base.port = PORT_OTHER;
1945 break;
1946 }
1947
1948 /* flow control is symmetric and always supported */
1949 ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
1950
1951 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
1952 if (!caps)
1953 return -ENOMEM;
1954
1955 status = ice_aq_get_phy_caps(vsi->port_info, false,
1956 ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
1957 if (status) {
1958 err = -EIO;
1959 goto done;
1960 }
1961
1962 /* Set the advertised flow control based on the PHY capability */
1963 if ((caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) &&
1964 (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)) {
1965 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
1966 ethtool_link_ksettings_add_link_mode(ks, advertising,
1967 Asym_Pause);
1968 } else if (caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) {
1969 ethtool_link_ksettings_add_link_mode(ks, advertising,
1970 Asym_Pause);
1971 } else if (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE) {
1972 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
1973 ethtool_link_ksettings_add_link_mode(ks, advertising,
1974 Asym_Pause);
1975 } else {
1976 ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
1977 ethtool_link_ksettings_del_link_mode(ks, advertising,
1978 Asym_Pause);
1979 }
1980
1981 /* Set advertised FEC modes based on PHY capability */
1982 ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_NONE);
1983
1984 if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
1985 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
1986 ethtool_link_ksettings_add_link_mode(ks, advertising,
1987 FEC_BASER);
1988 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
1989 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
1990 ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS);
1991
1992 status = ice_aq_get_phy_caps(vsi->port_info, false,
1993 ICE_AQC_REPORT_TOPO_CAP_MEDIA, caps, NULL);
1994 if (status) {
1995 err = -EIO;
1996 goto done;
1997 }
1998
1999 /* Set supported FEC modes based on PHY capability */
2000 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_NONE);
2001
2002 if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
2003 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN)
2004 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER);
2005 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
2006 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS);
2007
2008 /* Set supported and advertised autoneg */
2009 if (ice_is_phy_caps_an_enabled(caps)) {
2010 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
2011 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
2012 }
2013
2014 done:
2015 kfree(caps);
2016 return err;
2017 }
2018
2019 /**
2020 * ice_ksettings_find_adv_link_speed - Find advertising link speed
2021 * @ks: ethtool ksettings
2022 */
2023 static u16
ice_ksettings_find_adv_link_speed(const struct ethtool_link_ksettings * ks)2024 ice_ksettings_find_adv_link_speed(const struct ethtool_link_ksettings *ks)
2025 {
2026 u16 adv_link_speed = 0;
2027
2028 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2029 100baseT_Full))
2030 adv_link_speed |= ICE_AQ_LINK_SPEED_100MB;
2031 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2032 1000baseX_Full))
2033 adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
2034 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2035 1000baseT_Full) ||
2036 ethtool_link_ksettings_test_link_mode(ks, advertising,
2037 1000baseKX_Full))
2038 adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
2039 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2040 2500baseT_Full))
2041 adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
2042 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2043 2500baseX_Full))
2044 adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
2045 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2046 5000baseT_Full))
2047 adv_link_speed |= ICE_AQ_LINK_SPEED_5GB;
2048 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2049 10000baseT_Full) ||
2050 ethtool_link_ksettings_test_link_mode(ks, advertising,
2051 10000baseKR_Full))
2052 adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
2053 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2054 10000baseSR_Full) ||
2055 ethtool_link_ksettings_test_link_mode(ks, advertising,
2056 10000baseLR_Full))
2057 adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
2058 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2059 25000baseCR_Full) ||
2060 ethtool_link_ksettings_test_link_mode(ks, advertising,
2061 25000baseSR_Full) ||
2062 ethtool_link_ksettings_test_link_mode(ks, advertising,
2063 25000baseKR_Full))
2064 adv_link_speed |= ICE_AQ_LINK_SPEED_25GB;
2065 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2066 40000baseCR4_Full) ||
2067 ethtool_link_ksettings_test_link_mode(ks, advertising,
2068 40000baseSR4_Full) ||
2069 ethtool_link_ksettings_test_link_mode(ks, advertising,
2070 40000baseLR4_Full) ||
2071 ethtool_link_ksettings_test_link_mode(ks, advertising,
2072 40000baseKR4_Full))
2073 adv_link_speed |= ICE_AQ_LINK_SPEED_40GB;
2074 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2075 50000baseCR2_Full) ||
2076 ethtool_link_ksettings_test_link_mode(ks, advertising,
2077 50000baseKR2_Full))
2078 adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
2079 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2080 50000baseSR2_Full))
2081 adv_link_speed |= ICE_AQ_LINK_SPEED_50GB;
2082 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2083 100000baseCR4_Full) ||
2084 ethtool_link_ksettings_test_link_mode(ks, advertising,
2085 100000baseSR4_Full) ||
2086 ethtool_link_ksettings_test_link_mode(ks, advertising,
2087 100000baseLR4_ER4_Full) ||
2088 ethtool_link_ksettings_test_link_mode(ks, advertising,
2089 100000baseKR4_Full))
2090 adv_link_speed |= ICE_AQ_LINK_SPEED_100GB;
2091
2092 return adv_link_speed;
2093 }
2094
2095 /**
2096 * ice_setup_autoneg
2097 * @p: port info
2098 * @ks: ethtool_link_ksettings
2099 * @config: configuration that will be sent down to FW
2100 * @autoneg_enabled: autonegotiation is enabled or not
2101 * @autoneg_changed: will there a change in autonegotiation
2102 * @netdev: network interface device structure
2103 *
2104 * Setup PHY autonegotiation feature
2105 */
2106 static int
ice_setup_autoneg(struct ice_port_info * p,struct ethtool_link_ksettings * ks,struct ice_aqc_set_phy_cfg_data * config,u8 autoneg_enabled,u8 * autoneg_changed,struct net_device * netdev)2107 ice_setup_autoneg(struct ice_port_info *p, struct ethtool_link_ksettings *ks,
2108 struct ice_aqc_set_phy_cfg_data *config,
2109 u8 autoneg_enabled, u8 *autoneg_changed,
2110 struct net_device *netdev)
2111 {
2112 int err = 0;
2113
2114 *autoneg_changed = 0;
2115
2116 /* Check autoneg */
2117 if (autoneg_enabled == AUTONEG_ENABLE) {
2118 /* If autoneg was not already enabled */
2119 if (!(p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)) {
2120 /* If autoneg is not supported, return error */
2121 if (!ethtool_link_ksettings_test_link_mode(ks,
2122 supported,
2123 Autoneg)) {
2124 netdev_info(netdev, "Autoneg not supported on this phy.\n");
2125 err = -EINVAL;
2126 } else {
2127 /* Autoneg is allowed to change */
2128 config->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2129 *autoneg_changed = 1;
2130 }
2131 }
2132 } else {
2133 /* If autoneg is currently enabled */
2134 if (p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) {
2135 /* If autoneg is supported 10GBASE_T is the only PHY
2136 * that can disable it, so otherwise return error
2137 */
2138 if (ethtool_link_ksettings_test_link_mode(ks,
2139 supported,
2140 Autoneg)) {
2141 netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
2142 err = -EINVAL;
2143 } else {
2144 /* Autoneg is allowed to change */
2145 config->caps &= ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2146 *autoneg_changed = 1;
2147 }
2148 }
2149 }
2150
2151 return err;
2152 }
2153
2154 /**
2155 * ice_set_phy_type_from_speed - set phy_types based on speeds
2156 * and advertised modes
2157 * @ks: ethtool link ksettings struct
2158 * @phy_type_low: pointer to the lower part of phy_type
2159 * @phy_type_high: pointer to the higher part of phy_type
2160 * @adv_link_speed: targeted link speeds bitmap
2161 */
2162 static void
ice_set_phy_type_from_speed(const struct ethtool_link_ksettings * ks,u64 * phy_type_low,u64 * phy_type_high,u16 adv_link_speed)2163 ice_set_phy_type_from_speed(const struct ethtool_link_ksettings *ks,
2164 u64 *phy_type_low, u64 *phy_type_high,
2165 u16 adv_link_speed)
2166 {
2167 /* Handle 1000M speed in a special way because ice_update_phy_type
2168 * enables all link modes, but having mixed copper and optical
2169 * standards is not supported.
2170 */
2171 adv_link_speed &= ~ICE_AQ_LINK_SPEED_1000MB;
2172
2173 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2174 1000baseT_Full))
2175 *phy_type_low |= ICE_PHY_TYPE_LOW_1000BASE_T |
2176 ICE_PHY_TYPE_LOW_1G_SGMII;
2177
2178 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2179 1000baseKX_Full))
2180 *phy_type_low |= ICE_PHY_TYPE_LOW_1000BASE_KX;
2181
2182 if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2183 1000baseX_Full))
2184 *phy_type_low |= ICE_PHY_TYPE_LOW_1000BASE_SX |
2185 ICE_PHY_TYPE_LOW_1000BASE_LX;
2186
2187 ice_update_phy_type(phy_type_low, phy_type_high, adv_link_speed);
2188 }
2189
2190 /**
2191 * ice_set_link_ksettings - Set Speed and Duplex
2192 * @netdev: network interface device structure
2193 * @ks: ethtool ksettings
2194 *
2195 * Set speed/duplex per media_types advertised/forced
2196 */
2197 static int
ice_set_link_ksettings(struct net_device * netdev,const struct ethtool_link_ksettings * ks)2198 ice_set_link_ksettings(struct net_device *netdev,
2199 const struct ethtool_link_ksettings *ks)
2200 {
2201 struct ice_netdev_priv *np = netdev_priv(netdev);
2202 u8 autoneg, timeout = TEST_SET_BITS_TIMEOUT;
2203 struct ethtool_link_ksettings copy_ks = *ks;
2204 struct ethtool_link_ksettings safe_ks = {};
2205 struct ice_aqc_get_phy_caps_data *phy_caps;
2206 struct ice_aqc_set_phy_cfg_data config;
2207 u16 adv_link_speed, curr_link_speed;
2208 struct ice_pf *pf = np->vsi->back;
2209 struct ice_port_info *pi;
2210 u8 autoneg_changed = 0;
2211 enum ice_status status;
2212 u64 phy_type_high = 0;
2213 u64 phy_type_low = 0;
2214 int err = 0;
2215 bool linkup;
2216
2217 pi = np->vsi->port_info;
2218
2219 if (!pi)
2220 return -EIO;
2221
2222 if (pi->phy.media_type != ICE_MEDIA_BASET &&
2223 pi->phy.media_type != ICE_MEDIA_FIBER &&
2224 pi->phy.media_type != ICE_MEDIA_BACKPLANE &&
2225 pi->phy.media_type != ICE_MEDIA_DA &&
2226 pi->phy.link_info.link_info & ICE_AQ_LINK_UP)
2227 return -EOPNOTSUPP;
2228
2229 phy_caps = kzalloc(sizeof(*phy_caps), GFP_KERNEL);
2230 if (!phy_caps)
2231 return -ENOMEM;
2232
2233 /* Get the PHY capabilities based on media */
2234 if (ice_fw_supports_report_dflt_cfg(pi->hw))
2235 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2236 phy_caps, NULL);
2237 else
2238 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2239 phy_caps, NULL);
2240 if (status) {
2241 err = -EIO;
2242 goto done;
2243 }
2244
2245 /* save autoneg out of ksettings */
2246 autoneg = copy_ks.base.autoneg;
2247
2248 /* Get link modes supported by hardware.*/
2249 ice_phy_type_to_ethtool(netdev, &safe_ks);
2250
2251 /* and check against modes requested by user.
2252 * Return an error if unsupported mode was set.
2253 */
2254 if (!bitmap_subset(copy_ks.link_modes.advertising,
2255 safe_ks.link_modes.supported,
2256 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
2257 if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags))
2258 netdev_info(netdev, "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
2259 err = -EOPNOTSUPP;
2260 goto done;
2261 }
2262
2263 /* get our own copy of the bits to check against */
2264 memset(&safe_ks, 0, sizeof(safe_ks));
2265 safe_ks.base.cmd = copy_ks.base.cmd;
2266 safe_ks.base.link_mode_masks_nwords =
2267 copy_ks.base.link_mode_masks_nwords;
2268 ice_get_link_ksettings(netdev, &safe_ks);
2269
2270 /* set autoneg back to what it currently is */
2271 copy_ks.base.autoneg = safe_ks.base.autoneg;
2272 /* we don't compare the speed */
2273 copy_ks.base.speed = safe_ks.base.speed;
2274
2275 /* If copy_ks.base and safe_ks.base are not the same now, then they are
2276 * trying to set something that we do not support.
2277 */
2278 if (memcmp(©_ks.base, &safe_ks.base, sizeof(copy_ks.base))) {
2279 err = -EOPNOTSUPP;
2280 goto done;
2281 }
2282
2283 while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
2284 timeout--;
2285 if (!timeout) {
2286 err = -EBUSY;
2287 goto done;
2288 }
2289 usleep_range(TEST_SET_BITS_SLEEP_MIN, TEST_SET_BITS_SLEEP_MAX);
2290 }
2291
2292 /* Copy the current user PHY configuration. The current user PHY
2293 * configuration is initialized during probe from PHY capabilities
2294 * software mode, and updated on set PHY configuration.
2295 */
2296 config = pi->phy.curr_user_phy_cfg;
2297
2298 config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2299
2300 /* Check autoneg */
2301 err = ice_setup_autoneg(pi, &safe_ks, &config, autoneg, &autoneg_changed,
2302 netdev);
2303
2304 if (err)
2305 goto done;
2306
2307 /* Call to get the current link speed */
2308 pi->phy.get_link_info = true;
2309 status = ice_get_link_status(pi, &linkup);
2310 if (status) {
2311 err = -EIO;
2312 goto done;
2313 }
2314
2315 curr_link_speed = pi->phy.curr_user_speed_req;
2316 adv_link_speed = ice_ksettings_find_adv_link_speed(ks);
2317
2318 /* If speed didn't get set, set it to what it currently is.
2319 * This is needed because if advertise is 0 (as it is when autoneg
2320 * is disabled) then speed won't get set.
2321 */
2322 if (!adv_link_speed)
2323 adv_link_speed = curr_link_speed;
2324
2325 /* Convert the advertise link speeds to their corresponded PHY_TYPE */
2326 ice_set_phy_type_from_speed(ks, &phy_type_low, &phy_type_high,
2327 adv_link_speed);
2328
2329 if (!autoneg_changed && adv_link_speed == curr_link_speed) {
2330 netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
2331 goto done;
2332 }
2333
2334 /* save the requested speeds */
2335 pi->phy.link_info.req_speeds = adv_link_speed;
2336
2337 /* set link and auto negotiation so changes take effect */
2338 config.caps |= ICE_AQ_PHY_ENA_LINK;
2339
2340 /* check if there is a PHY type for the requested advertised speed */
2341 if (!(phy_type_low || phy_type_high)) {
2342 netdev_info(netdev, "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
2343 err = -EOPNOTSUPP;
2344 goto done;
2345 }
2346
2347 /* intersect requested advertised speed PHY types with media PHY types
2348 * for set PHY configuration
2349 */
2350 config.phy_type_high = cpu_to_le64(phy_type_high) &
2351 phy_caps->phy_type_high;
2352 config.phy_type_low = cpu_to_le64(phy_type_low) &
2353 phy_caps->phy_type_low;
2354
2355 if (!(config.phy_type_high || config.phy_type_low)) {
2356 /* If there is no intersection and lenient mode is enabled, then
2357 * intersect the requested advertised speed with NVM media type
2358 * PHY types.
2359 */
2360 if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags)) {
2361 config.phy_type_high = cpu_to_le64(phy_type_high) &
2362 pf->nvm_phy_type_hi;
2363 config.phy_type_low = cpu_to_le64(phy_type_low) &
2364 pf->nvm_phy_type_lo;
2365 } else {
2366 netdev_info(netdev, "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
2367 err = -EOPNOTSUPP;
2368 goto done;
2369 }
2370 }
2371
2372 /* If link is up put link down */
2373 if (pi->phy.link_info.link_info & ICE_AQ_LINK_UP) {
2374 /* Tell the OS link is going down, the link will go
2375 * back up when fw says it is ready asynchronously
2376 */
2377 ice_print_link_msg(np->vsi, false);
2378 netif_carrier_off(netdev);
2379 netif_tx_stop_all_queues(netdev);
2380 }
2381
2382 /* make the aq call */
2383 status = ice_aq_set_phy_cfg(&pf->hw, pi, &config, NULL);
2384 if (status) {
2385 netdev_info(netdev, "Set phy config failed,\n");
2386 err = -EIO;
2387 goto done;
2388 }
2389
2390 /* Save speed request */
2391 pi->phy.curr_user_speed_req = adv_link_speed;
2392 done:
2393 kfree(phy_caps);
2394 clear_bit(ICE_CFG_BUSY, pf->state);
2395
2396 return err;
2397 }
2398
2399 /**
2400 * ice_parse_hdrs - parses headers from RSS hash input
2401 * @nfc: ethtool rxnfc command
2402 *
2403 * This function parses the rxnfc command and returns intended
2404 * header types for RSS configuration
2405 */
ice_parse_hdrs(struct ethtool_rxnfc * nfc)2406 static u32 ice_parse_hdrs(struct ethtool_rxnfc *nfc)
2407 {
2408 u32 hdrs = ICE_FLOW_SEG_HDR_NONE;
2409
2410 switch (nfc->flow_type) {
2411 case TCP_V4_FLOW:
2412 hdrs |= ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4;
2413 break;
2414 case UDP_V4_FLOW:
2415 hdrs |= ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4;
2416 break;
2417 case SCTP_V4_FLOW:
2418 hdrs |= ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4;
2419 break;
2420 case TCP_V6_FLOW:
2421 hdrs |= ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6;
2422 break;
2423 case UDP_V6_FLOW:
2424 hdrs |= ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6;
2425 break;
2426 case SCTP_V6_FLOW:
2427 hdrs |= ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6;
2428 break;
2429 default:
2430 break;
2431 }
2432 return hdrs;
2433 }
2434
2435 #define ICE_FLOW_HASH_FLD_IPV4_SA BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA)
2436 #define ICE_FLOW_HASH_FLD_IPV6_SA BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA)
2437 #define ICE_FLOW_HASH_FLD_IPV4_DA BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA)
2438 #define ICE_FLOW_HASH_FLD_IPV6_DA BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA)
2439 #define ICE_FLOW_HASH_FLD_TCP_SRC_PORT BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT)
2440 #define ICE_FLOW_HASH_FLD_TCP_DST_PORT BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT)
2441 #define ICE_FLOW_HASH_FLD_UDP_SRC_PORT BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT)
2442 #define ICE_FLOW_HASH_FLD_UDP_DST_PORT BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT)
2443 #define ICE_FLOW_HASH_FLD_SCTP_SRC_PORT \
2444 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT)
2445 #define ICE_FLOW_HASH_FLD_SCTP_DST_PORT \
2446 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT)
2447
2448 /**
2449 * ice_parse_hash_flds - parses hash fields from RSS hash input
2450 * @nfc: ethtool rxnfc command
2451 *
2452 * This function parses the rxnfc command and returns intended
2453 * hash fields for RSS configuration
2454 */
ice_parse_hash_flds(struct ethtool_rxnfc * nfc)2455 static u64 ice_parse_hash_flds(struct ethtool_rxnfc *nfc)
2456 {
2457 u64 hfld = ICE_HASH_INVALID;
2458
2459 if (nfc->data & RXH_IP_SRC || nfc->data & RXH_IP_DST) {
2460 switch (nfc->flow_type) {
2461 case TCP_V4_FLOW:
2462 case UDP_V4_FLOW:
2463 case SCTP_V4_FLOW:
2464 if (nfc->data & RXH_IP_SRC)
2465 hfld |= ICE_FLOW_HASH_FLD_IPV4_SA;
2466 if (nfc->data & RXH_IP_DST)
2467 hfld |= ICE_FLOW_HASH_FLD_IPV4_DA;
2468 break;
2469 case TCP_V6_FLOW:
2470 case UDP_V6_FLOW:
2471 case SCTP_V6_FLOW:
2472 if (nfc->data & RXH_IP_SRC)
2473 hfld |= ICE_FLOW_HASH_FLD_IPV6_SA;
2474 if (nfc->data & RXH_IP_DST)
2475 hfld |= ICE_FLOW_HASH_FLD_IPV6_DA;
2476 break;
2477 default:
2478 break;
2479 }
2480 }
2481
2482 if (nfc->data & RXH_L4_B_0_1 || nfc->data & RXH_L4_B_2_3) {
2483 switch (nfc->flow_type) {
2484 case TCP_V4_FLOW:
2485 case TCP_V6_FLOW:
2486 if (nfc->data & RXH_L4_B_0_1)
2487 hfld |= ICE_FLOW_HASH_FLD_TCP_SRC_PORT;
2488 if (nfc->data & RXH_L4_B_2_3)
2489 hfld |= ICE_FLOW_HASH_FLD_TCP_DST_PORT;
2490 break;
2491 case UDP_V4_FLOW:
2492 case UDP_V6_FLOW:
2493 if (nfc->data & RXH_L4_B_0_1)
2494 hfld |= ICE_FLOW_HASH_FLD_UDP_SRC_PORT;
2495 if (nfc->data & RXH_L4_B_2_3)
2496 hfld |= ICE_FLOW_HASH_FLD_UDP_DST_PORT;
2497 break;
2498 case SCTP_V4_FLOW:
2499 case SCTP_V6_FLOW:
2500 if (nfc->data & RXH_L4_B_0_1)
2501 hfld |= ICE_FLOW_HASH_FLD_SCTP_SRC_PORT;
2502 if (nfc->data & RXH_L4_B_2_3)
2503 hfld |= ICE_FLOW_HASH_FLD_SCTP_DST_PORT;
2504 break;
2505 default:
2506 break;
2507 }
2508 }
2509
2510 return hfld;
2511 }
2512
2513 /**
2514 * ice_set_rss_hash_opt - Enable/Disable flow types for RSS hash
2515 * @vsi: the VSI being configured
2516 * @nfc: ethtool rxnfc command
2517 *
2518 * Returns Success if the flow input set is supported.
2519 */
2520 static int
ice_set_rss_hash_opt(struct ice_vsi * vsi,struct ethtool_rxnfc * nfc)2521 ice_set_rss_hash_opt(struct ice_vsi *vsi, struct ethtool_rxnfc *nfc)
2522 {
2523 struct ice_pf *pf = vsi->back;
2524 enum ice_status status;
2525 struct device *dev;
2526 u64 hashed_flds;
2527 u32 hdrs;
2528
2529 dev = ice_pf_to_dev(pf);
2530 if (ice_is_safe_mode(pf)) {
2531 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
2532 vsi->vsi_num);
2533 return -EINVAL;
2534 }
2535
2536 hashed_flds = ice_parse_hash_flds(nfc);
2537 if (hashed_flds == ICE_HASH_INVALID) {
2538 dev_dbg(dev, "Invalid hash fields, vsi num = %d\n",
2539 vsi->vsi_num);
2540 return -EINVAL;
2541 }
2542
2543 hdrs = ice_parse_hdrs(nfc);
2544 if (hdrs == ICE_FLOW_SEG_HDR_NONE) {
2545 dev_dbg(dev, "Header type is not valid, vsi num = %d\n",
2546 vsi->vsi_num);
2547 return -EINVAL;
2548 }
2549
2550 status = ice_add_rss_cfg(&pf->hw, vsi->idx, hashed_flds, hdrs);
2551 if (status) {
2552 dev_dbg(dev, "ice_add_rss_cfg failed, vsi num = %d, error = %s\n",
2553 vsi->vsi_num, ice_stat_str(status));
2554 return -EINVAL;
2555 }
2556
2557 return 0;
2558 }
2559
2560 /**
2561 * ice_get_rss_hash_opt - Retrieve hash fields for a given flow-type
2562 * @vsi: the VSI being configured
2563 * @nfc: ethtool rxnfc command
2564 */
2565 static void
ice_get_rss_hash_opt(struct ice_vsi * vsi,struct ethtool_rxnfc * nfc)2566 ice_get_rss_hash_opt(struct ice_vsi *vsi, struct ethtool_rxnfc *nfc)
2567 {
2568 struct ice_pf *pf = vsi->back;
2569 struct device *dev;
2570 u64 hash_flds;
2571 u32 hdrs;
2572
2573 dev = ice_pf_to_dev(pf);
2574
2575 nfc->data = 0;
2576 if (ice_is_safe_mode(pf)) {
2577 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
2578 vsi->vsi_num);
2579 return;
2580 }
2581
2582 hdrs = ice_parse_hdrs(nfc);
2583 if (hdrs == ICE_FLOW_SEG_HDR_NONE) {
2584 dev_dbg(dev, "Header type is not valid, vsi num = %d\n",
2585 vsi->vsi_num);
2586 return;
2587 }
2588
2589 hash_flds = ice_get_rss_cfg(&pf->hw, vsi->idx, hdrs);
2590 if (hash_flds == ICE_HASH_INVALID) {
2591 dev_dbg(dev, "No hash fields found for the given header type, vsi num = %d\n",
2592 vsi->vsi_num);
2593 return;
2594 }
2595
2596 if (hash_flds & ICE_FLOW_HASH_FLD_IPV4_SA ||
2597 hash_flds & ICE_FLOW_HASH_FLD_IPV6_SA)
2598 nfc->data |= (u64)RXH_IP_SRC;
2599
2600 if (hash_flds & ICE_FLOW_HASH_FLD_IPV4_DA ||
2601 hash_flds & ICE_FLOW_HASH_FLD_IPV6_DA)
2602 nfc->data |= (u64)RXH_IP_DST;
2603
2604 if (hash_flds & ICE_FLOW_HASH_FLD_TCP_SRC_PORT ||
2605 hash_flds & ICE_FLOW_HASH_FLD_UDP_SRC_PORT ||
2606 hash_flds & ICE_FLOW_HASH_FLD_SCTP_SRC_PORT)
2607 nfc->data |= (u64)RXH_L4_B_0_1;
2608
2609 if (hash_flds & ICE_FLOW_HASH_FLD_TCP_DST_PORT ||
2610 hash_flds & ICE_FLOW_HASH_FLD_UDP_DST_PORT ||
2611 hash_flds & ICE_FLOW_HASH_FLD_SCTP_DST_PORT)
2612 nfc->data |= (u64)RXH_L4_B_2_3;
2613 }
2614
2615 /**
2616 * ice_set_rxnfc - command to set Rx flow rules.
2617 * @netdev: network interface device structure
2618 * @cmd: ethtool rxnfc command
2619 *
2620 * Returns 0 for success and negative values for errors
2621 */
ice_set_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd)2622 static int ice_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
2623 {
2624 struct ice_netdev_priv *np = netdev_priv(netdev);
2625 struct ice_vsi *vsi = np->vsi;
2626
2627 switch (cmd->cmd) {
2628 case ETHTOOL_SRXCLSRLINS:
2629 return ice_add_fdir_ethtool(vsi, cmd);
2630 case ETHTOOL_SRXCLSRLDEL:
2631 return ice_del_fdir_ethtool(vsi, cmd);
2632 case ETHTOOL_SRXFH:
2633 return ice_set_rss_hash_opt(vsi, cmd);
2634 default:
2635 break;
2636 }
2637 return -EOPNOTSUPP;
2638 }
2639
2640 /**
2641 * ice_get_rxnfc - command to get Rx flow classification rules
2642 * @netdev: network interface device structure
2643 * @cmd: ethtool rxnfc command
2644 * @rule_locs: buffer to rturn Rx flow classification rules
2645 *
2646 * Returns Success if the command is supported.
2647 */
2648 static int
ice_get_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd,u32 __always_unused * rule_locs)2649 ice_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
2650 u32 __always_unused *rule_locs)
2651 {
2652 struct ice_netdev_priv *np = netdev_priv(netdev);
2653 struct ice_vsi *vsi = np->vsi;
2654 int ret = -EOPNOTSUPP;
2655 struct ice_hw *hw;
2656
2657 hw = &vsi->back->hw;
2658
2659 switch (cmd->cmd) {
2660 case ETHTOOL_GRXRINGS:
2661 cmd->data = vsi->rss_size;
2662 ret = 0;
2663 break;
2664 case ETHTOOL_GRXCLSRLCNT:
2665 cmd->rule_cnt = hw->fdir_active_fltr;
2666 /* report total rule count */
2667 cmd->data = ice_get_fdir_cnt_all(hw);
2668 ret = 0;
2669 break;
2670 case ETHTOOL_GRXCLSRULE:
2671 ret = ice_get_ethtool_fdir_entry(hw, cmd);
2672 break;
2673 case ETHTOOL_GRXCLSRLALL:
2674 ret = ice_get_fdir_fltr_ids(hw, cmd, (u32 *)rule_locs);
2675 break;
2676 case ETHTOOL_GRXFH:
2677 ice_get_rss_hash_opt(vsi, cmd);
2678 ret = 0;
2679 break;
2680 default:
2681 break;
2682 }
2683
2684 return ret;
2685 }
2686
2687 static void
ice_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring)2688 ice_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
2689 {
2690 struct ice_netdev_priv *np = netdev_priv(netdev);
2691 struct ice_vsi *vsi = np->vsi;
2692
2693 ring->rx_max_pending = ICE_MAX_NUM_DESC;
2694 ring->tx_max_pending = ICE_MAX_NUM_DESC;
2695 ring->rx_pending = vsi->rx_rings[0]->count;
2696 ring->tx_pending = vsi->tx_rings[0]->count;
2697
2698 /* Rx mini and jumbo rings are not supported */
2699 ring->rx_mini_max_pending = 0;
2700 ring->rx_jumbo_max_pending = 0;
2701 ring->rx_mini_pending = 0;
2702 ring->rx_jumbo_pending = 0;
2703 }
2704
2705 static int
ice_set_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring)2706 ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
2707 {
2708 struct ice_ring *tx_rings = NULL, *rx_rings = NULL;
2709 struct ice_netdev_priv *np = netdev_priv(netdev);
2710 struct ice_ring *xdp_rings = NULL;
2711 struct ice_vsi *vsi = np->vsi;
2712 struct ice_pf *pf = vsi->back;
2713 int i, timeout = 50, err = 0;
2714 u16 new_rx_cnt, new_tx_cnt;
2715
2716 if (ring->tx_pending > ICE_MAX_NUM_DESC ||
2717 ring->tx_pending < ICE_MIN_NUM_DESC ||
2718 ring->rx_pending > ICE_MAX_NUM_DESC ||
2719 ring->rx_pending < ICE_MIN_NUM_DESC) {
2720 netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
2721 ring->tx_pending, ring->rx_pending,
2722 ICE_MIN_NUM_DESC, ICE_MAX_NUM_DESC,
2723 ICE_REQ_DESC_MULTIPLE);
2724 return -EINVAL;
2725 }
2726
2727 new_tx_cnt = ALIGN(ring->tx_pending, ICE_REQ_DESC_MULTIPLE);
2728 if (new_tx_cnt != ring->tx_pending)
2729 netdev_info(netdev, "Requested Tx descriptor count rounded up to %d\n",
2730 new_tx_cnt);
2731 new_rx_cnt = ALIGN(ring->rx_pending, ICE_REQ_DESC_MULTIPLE);
2732 if (new_rx_cnt != ring->rx_pending)
2733 netdev_info(netdev, "Requested Rx descriptor count rounded up to %d\n",
2734 new_rx_cnt);
2735
2736 /* if nothing to do return success */
2737 if (new_tx_cnt == vsi->tx_rings[0]->count &&
2738 new_rx_cnt == vsi->rx_rings[0]->count) {
2739 netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
2740 return 0;
2741 }
2742
2743 /* If there is a AF_XDP UMEM attached to any of Rx rings,
2744 * disallow changing the number of descriptors -- regardless
2745 * if the netdev is running or not.
2746 */
2747 if (ice_xsk_any_rx_ring_ena(vsi))
2748 return -EBUSY;
2749
2750 while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
2751 timeout--;
2752 if (!timeout)
2753 return -EBUSY;
2754 usleep_range(1000, 2000);
2755 }
2756
2757 /* set for the next time the netdev is started */
2758 if (!netif_running(vsi->netdev)) {
2759 for (i = 0; i < vsi->alloc_txq; i++)
2760 vsi->tx_rings[i]->count = new_tx_cnt;
2761 for (i = 0; i < vsi->alloc_rxq; i++)
2762 vsi->rx_rings[i]->count = new_rx_cnt;
2763 if (ice_is_xdp_ena_vsi(vsi))
2764 for (i = 0; i < vsi->num_xdp_txq; i++)
2765 vsi->xdp_rings[i]->count = new_tx_cnt;
2766 vsi->num_tx_desc = (u16)new_tx_cnt;
2767 vsi->num_rx_desc = (u16)new_rx_cnt;
2768 netdev_dbg(netdev, "Link is down, descriptor count change happens when link is brought up\n");
2769 goto done;
2770 }
2771
2772 if (new_tx_cnt == vsi->tx_rings[0]->count)
2773 goto process_rx;
2774
2775 /* alloc updated Tx resources */
2776 netdev_info(netdev, "Changing Tx descriptor count from %d to %d\n",
2777 vsi->tx_rings[0]->count, new_tx_cnt);
2778
2779 tx_rings = kcalloc(vsi->num_txq, sizeof(*tx_rings), GFP_KERNEL);
2780 if (!tx_rings) {
2781 err = -ENOMEM;
2782 goto done;
2783 }
2784
2785 ice_for_each_txq(vsi, i) {
2786 /* clone ring and setup updated count */
2787 tx_rings[i] = *vsi->tx_rings[i];
2788 tx_rings[i].count = new_tx_cnt;
2789 tx_rings[i].desc = NULL;
2790 tx_rings[i].tx_buf = NULL;
2791 tx_rings[i].tx_tstamps = &pf->ptp.port.tx;
2792 err = ice_setup_tx_ring(&tx_rings[i]);
2793 if (err) {
2794 while (i--)
2795 ice_clean_tx_ring(&tx_rings[i]);
2796 kfree(tx_rings);
2797 goto done;
2798 }
2799 }
2800
2801 if (!ice_is_xdp_ena_vsi(vsi))
2802 goto process_rx;
2803
2804 /* alloc updated XDP resources */
2805 netdev_info(netdev, "Changing XDP descriptor count from %d to %d\n",
2806 vsi->xdp_rings[0]->count, new_tx_cnt);
2807
2808 xdp_rings = kcalloc(vsi->num_xdp_txq, sizeof(*xdp_rings), GFP_KERNEL);
2809 if (!xdp_rings) {
2810 err = -ENOMEM;
2811 goto free_tx;
2812 }
2813
2814 for (i = 0; i < vsi->num_xdp_txq; i++) {
2815 /* clone ring and setup updated count */
2816 xdp_rings[i] = *vsi->xdp_rings[i];
2817 xdp_rings[i].count = new_tx_cnt;
2818 xdp_rings[i].desc = NULL;
2819 xdp_rings[i].tx_buf = NULL;
2820 err = ice_setup_tx_ring(&xdp_rings[i]);
2821 if (err) {
2822 while (i--)
2823 ice_clean_tx_ring(&xdp_rings[i]);
2824 kfree(xdp_rings);
2825 goto free_tx;
2826 }
2827 ice_set_ring_xdp(&xdp_rings[i]);
2828 }
2829
2830 process_rx:
2831 if (new_rx_cnt == vsi->rx_rings[0]->count)
2832 goto process_link;
2833
2834 /* alloc updated Rx resources */
2835 netdev_info(netdev, "Changing Rx descriptor count from %d to %d\n",
2836 vsi->rx_rings[0]->count, new_rx_cnt);
2837
2838 rx_rings = kcalloc(vsi->num_rxq, sizeof(*rx_rings), GFP_KERNEL);
2839 if (!rx_rings) {
2840 err = -ENOMEM;
2841 goto done;
2842 }
2843
2844 ice_for_each_rxq(vsi, i) {
2845 /* clone ring and setup updated count */
2846 rx_rings[i] = *vsi->rx_rings[i];
2847 rx_rings[i].count = new_rx_cnt;
2848 rx_rings[i].desc = NULL;
2849 rx_rings[i].rx_buf = NULL;
2850 /* this is to allow wr32 to have something to write to
2851 * during early allocation of Rx buffers
2852 */
2853 rx_rings[i].tail = vsi->back->hw.hw_addr + PRTGEN_STATUS;
2854
2855 err = ice_setup_rx_ring(&rx_rings[i]);
2856 if (err)
2857 goto rx_unwind;
2858
2859 /* allocate Rx buffers */
2860 err = ice_alloc_rx_bufs(&rx_rings[i],
2861 ICE_DESC_UNUSED(&rx_rings[i]));
2862 rx_unwind:
2863 if (err) {
2864 while (i) {
2865 i--;
2866 ice_free_rx_ring(&rx_rings[i]);
2867 }
2868 kfree(rx_rings);
2869 err = -ENOMEM;
2870 goto free_tx;
2871 }
2872 }
2873
2874 process_link:
2875 /* Bring interface down, copy in the new ring info, then restore the
2876 * interface. if VSI is up, bring it down and then back up
2877 */
2878 if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
2879 ice_down(vsi);
2880
2881 if (tx_rings) {
2882 ice_for_each_txq(vsi, i) {
2883 ice_free_tx_ring(vsi->tx_rings[i]);
2884 *vsi->tx_rings[i] = tx_rings[i];
2885 }
2886 kfree(tx_rings);
2887 }
2888
2889 if (rx_rings) {
2890 ice_for_each_rxq(vsi, i) {
2891 ice_free_rx_ring(vsi->rx_rings[i]);
2892 /* copy the real tail offset */
2893 rx_rings[i].tail = vsi->rx_rings[i]->tail;
2894 /* this is to fake out the allocation routine
2895 * into thinking it has to realloc everything
2896 * but the recycling logic will let us re-use
2897 * the buffers allocated above
2898 */
2899 rx_rings[i].next_to_use = 0;
2900 rx_rings[i].next_to_clean = 0;
2901 rx_rings[i].next_to_alloc = 0;
2902 *vsi->rx_rings[i] = rx_rings[i];
2903 }
2904 kfree(rx_rings);
2905 }
2906
2907 if (xdp_rings) {
2908 for (i = 0; i < vsi->num_xdp_txq; i++) {
2909 ice_free_tx_ring(vsi->xdp_rings[i]);
2910 *vsi->xdp_rings[i] = xdp_rings[i];
2911 }
2912 kfree(xdp_rings);
2913 }
2914
2915 vsi->num_tx_desc = new_tx_cnt;
2916 vsi->num_rx_desc = new_rx_cnt;
2917 ice_up(vsi);
2918 }
2919 goto done;
2920
2921 free_tx:
2922 /* error cleanup if the Rx allocations failed after getting Tx */
2923 if (tx_rings) {
2924 ice_for_each_txq(vsi, i)
2925 ice_free_tx_ring(&tx_rings[i]);
2926 kfree(tx_rings);
2927 }
2928
2929 done:
2930 clear_bit(ICE_CFG_BUSY, pf->state);
2931 return err;
2932 }
2933
2934 /**
2935 * ice_get_pauseparam - Get Flow Control status
2936 * @netdev: network interface device structure
2937 * @pause: ethernet pause (flow control) parameters
2938 *
2939 * Get requested flow control status from PHY capability.
2940 * If autoneg is true, then ethtool will send the ETHTOOL_GSET ioctl which
2941 * is handled by ice_get_link_ksettings. ice_get_link_ksettings will report
2942 * the negotiated Rx/Tx pause via lp_advertising.
2943 */
2944 static void
ice_get_pauseparam(struct net_device * netdev,struct ethtool_pauseparam * pause)2945 ice_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2946 {
2947 struct ice_netdev_priv *np = netdev_priv(netdev);
2948 struct ice_port_info *pi = np->vsi->port_info;
2949 struct ice_aqc_get_phy_caps_data *pcaps;
2950 struct ice_dcbx_cfg *dcbx_cfg;
2951 enum ice_status status;
2952
2953 /* Initialize pause params */
2954 pause->rx_pause = 0;
2955 pause->tx_pause = 0;
2956
2957 dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
2958
2959 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2960 if (!pcaps)
2961 return;
2962
2963 /* Get current PHY config */
2964 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
2965 NULL);
2966 if (status)
2967 goto out;
2968
2969 pause->autoneg = ice_is_phy_caps_an_enabled(pcaps) ? AUTONEG_ENABLE :
2970 AUTONEG_DISABLE;
2971
2972 if (dcbx_cfg->pfc.pfcena)
2973 /* PFC enabled so report LFC as off */
2974 goto out;
2975
2976 if (pcaps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
2977 pause->tx_pause = 1;
2978 if (pcaps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
2979 pause->rx_pause = 1;
2980
2981 out:
2982 kfree(pcaps);
2983 }
2984
2985 /**
2986 * ice_set_pauseparam - Set Flow Control parameter
2987 * @netdev: network interface device structure
2988 * @pause: return Tx/Rx flow control status
2989 */
2990 static int
ice_set_pauseparam(struct net_device * netdev,struct ethtool_pauseparam * pause)2991 ice_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2992 {
2993 struct ice_netdev_priv *np = netdev_priv(netdev);
2994 struct ice_aqc_get_phy_caps_data *pcaps;
2995 struct ice_link_status *hw_link_info;
2996 struct ice_pf *pf = np->vsi->back;
2997 struct ice_dcbx_cfg *dcbx_cfg;
2998 struct ice_vsi *vsi = np->vsi;
2999 struct ice_hw *hw = &pf->hw;
3000 struct ice_port_info *pi;
3001 enum ice_status status;
3002 u8 aq_failures;
3003 bool link_up;
3004 int err = 0;
3005 u32 is_an;
3006
3007 pi = vsi->port_info;
3008 hw_link_info = &pi->phy.link_info;
3009 dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
3010 link_up = hw_link_info->link_info & ICE_AQ_LINK_UP;
3011
3012 /* Changing the port's flow control is not supported if this isn't the
3013 * PF VSI
3014 */
3015 if (vsi->type != ICE_VSI_PF) {
3016 netdev_info(netdev, "Changing flow control parameters only supported for PF VSI\n");
3017 return -EOPNOTSUPP;
3018 }
3019
3020 /* Get pause param reports configured and negotiated flow control pause
3021 * when ETHTOOL_GLINKSETTINGS is defined. Since ETHTOOL_GLINKSETTINGS is
3022 * defined get pause param pause->autoneg reports SW configured setting,
3023 * so compare pause->autoneg with SW configured to prevent the user from
3024 * using set pause param to chance autoneg.
3025 */
3026 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
3027 if (!pcaps)
3028 return -ENOMEM;
3029
3030 /* Get current PHY config */
3031 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
3032 NULL);
3033 if (status) {
3034 kfree(pcaps);
3035 return -EIO;
3036 }
3037
3038 is_an = ice_is_phy_caps_an_enabled(pcaps) ? AUTONEG_ENABLE :
3039 AUTONEG_DISABLE;
3040
3041 kfree(pcaps);
3042
3043 if (pause->autoneg != is_an) {
3044 netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
3045 return -EOPNOTSUPP;
3046 }
3047
3048 /* If we have link and don't have autoneg */
3049 if (!test_bit(ICE_DOWN, pf->state) &&
3050 !(hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
3051 /* Send message that it might not necessarily work*/
3052 netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
3053 }
3054
3055 if (dcbx_cfg->pfc.pfcena) {
3056 netdev_info(netdev, "Priority flow control enabled. Cannot set link flow control.\n");
3057 return -EOPNOTSUPP;
3058 }
3059 if (pause->rx_pause && pause->tx_pause)
3060 pi->fc.req_mode = ICE_FC_FULL;
3061 else if (pause->rx_pause && !pause->tx_pause)
3062 pi->fc.req_mode = ICE_FC_RX_PAUSE;
3063 else if (!pause->rx_pause && pause->tx_pause)
3064 pi->fc.req_mode = ICE_FC_TX_PAUSE;
3065 else if (!pause->rx_pause && !pause->tx_pause)
3066 pi->fc.req_mode = ICE_FC_NONE;
3067 else
3068 return -EINVAL;
3069
3070 /* Set the FC mode and only restart AN if link is up */
3071 status = ice_set_fc(pi, &aq_failures, link_up);
3072
3073 if (aq_failures & ICE_SET_FC_AQ_FAIL_GET) {
3074 netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %s aq_err %s\n",
3075 ice_stat_str(status),
3076 ice_aq_str(hw->adminq.sq_last_status));
3077 err = -EAGAIN;
3078 } else if (aq_failures & ICE_SET_FC_AQ_FAIL_SET) {
3079 netdev_info(netdev, "Set fc failed on the set_phy_config call with err %s aq_err %s\n",
3080 ice_stat_str(status),
3081 ice_aq_str(hw->adminq.sq_last_status));
3082 err = -EAGAIN;
3083 } else if (aq_failures & ICE_SET_FC_AQ_FAIL_UPDATE) {
3084 netdev_info(netdev, "Set fc failed on the get_link_info call with err %s aq_err %s\n",
3085 ice_stat_str(status),
3086 ice_aq_str(hw->adminq.sq_last_status));
3087 err = -EAGAIN;
3088 }
3089
3090 return err;
3091 }
3092
3093 /**
3094 * ice_get_rxfh_key_size - get the RSS hash key size
3095 * @netdev: network interface device structure
3096 *
3097 * Returns the table size.
3098 */
ice_get_rxfh_key_size(struct net_device __always_unused * netdev)3099 static u32 ice_get_rxfh_key_size(struct net_device __always_unused *netdev)
3100 {
3101 return ICE_VSIQF_HKEY_ARRAY_SIZE;
3102 }
3103
3104 /**
3105 * ice_get_rxfh_indir_size - get the Rx flow hash indirection table size
3106 * @netdev: network interface device structure
3107 *
3108 * Returns the table size.
3109 */
ice_get_rxfh_indir_size(struct net_device * netdev)3110 static u32 ice_get_rxfh_indir_size(struct net_device *netdev)
3111 {
3112 struct ice_netdev_priv *np = netdev_priv(netdev);
3113
3114 return np->vsi->rss_table_size;
3115 }
3116
3117 /**
3118 * ice_get_rxfh - get the Rx flow hash indirection table
3119 * @netdev: network interface device structure
3120 * @indir: indirection table
3121 * @key: hash key
3122 * @hfunc: hash function
3123 *
3124 * Reads the indirection table directly from the hardware.
3125 */
3126 static int
ice_get_rxfh(struct net_device * netdev,u32 * indir,u8 * key,u8 * hfunc)3127 ice_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
3128 {
3129 struct ice_netdev_priv *np = netdev_priv(netdev);
3130 struct ice_vsi *vsi = np->vsi;
3131 struct ice_pf *pf = vsi->back;
3132 int err, i;
3133 u8 *lut;
3134
3135 if (hfunc)
3136 *hfunc = ETH_RSS_HASH_TOP;
3137
3138 if (!indir)
3139 return 0;
3140
3141 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3142 /* RSS not supported return error here */
3143 netdev_warn(netdev, "RSS is not configured on this VSI!\n");
3144 return -EIO;
3145 }
3146
3147 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
3148 if (!lut)
3149 return -ENOMEM;
3150
3151 err = ice_get_rss_key(vsi, key);
3152 if (err)
3153 goto out;
3154
3155 err = ice_get_rss_lut(vsi, lut, vsi->rss_table_size);
3156 if (err)
3157 goto out;
3158
3159 for (i = 0; i < vsi->rss_table_size; i++)
3160 indir[i] = (u32)(lut[i]);
3161
3162 out:
3163 kfree(lut);
3164 return err;
3165 }
3166
3167 /**
3168 * ice_set_rxfh - set the Rx flow hash indirection table
3169 * @netdev: network interface device structure
3170 * @indir: indirection table
3171 * @key: hash key
3172 * @hfunc: hash function
3173 *
3174 * Returns -EINVAL if the table specifies an invalid queue ID, otherwise
3175 * returns 0 after programming the table.
3176 */
3177 static int
ice_set_rxfh(struct net_device * netdev,const u32 * indir,const u8 * key,const u8 hfunc)3178 ice_set_rxfh(struct net_device *netdev, const u32 *indir, const u8 *key,
3179 const u8 hfunc)
3180 {
3181 struct ice_netdev_priv *np = netdev_priv(netdev);
3182 struct ice_vsi *vsi = np->vsi;
3183 struct ice_pf *pf = vsi->back;
3184 struct device *dev;
3185 int err;
3186
3187 dev = ice_pf_to_dev(pf);
3188 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3189 return -EOPNOTSUPP;
3190
3191 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3192 /* RSS not supported return error here */
3193 netdev_warn(netdev, "RSS is not configured on this VSI!\n");
3194 return -EIO;
3195 }
3196
3197 if (key) {
3198 if (!vsi->rss_hkey_user) {
3199 vsi->rss_hkey_user =
3200 devm_kzalloc(dev, ICE_VSIQF_HKEY_ARRAY_SIZE,
3201 GFP_KERNEL);
3202 if (!vsi->rss_hkey_user)
3203 return -ENOMEM;
3204 }
3205 memcpy(vsi->rss_hkey_user, key, ICE_VSIQF_HKEY_ARRAY_SIZE);
3206
3207 err = ice_set_rss_key(vsi, vsi->rss_hkey_user);
3208 if (err)
3209 return err;
3210 }
3211
3212 if (!vsi->rss_lut_user) {
3213 vsi->rss_lut_user = devm_kzalloc(dev, vsi->rss_table_size,
3214 GFP_KERNEL);
3215 if (!vsi->rss_lut_user)
3216 return -ENOMEM;
3217 }
3218
3219 /* Each 32 bits pointed by 'indir' is stored with a lut entry */
3220 if (indir) {
3221 int i;
3222
3223 for (i = 0; i < vsi->rss_table_size; i++)
3224 vsi->rss_lut_user[i] = (u8)(indir[i]);
3225 } else {
3226 ice_fill_rss_lut(vsi->rss_lut_user, vsi->rss_table_size,
3227 vsi->rss_size);
3228 }
3229
3230 err = ice_set_rss_lut(vsi, vsi->rss_lut_user, vsi->rss_table_size);
3231 if (err)
3232 return err;
3233
3234 return 0;
3235 }
3236
3237 static int
ice_get_ts_info(struct net_device * dev,struct ethtool_ts_info * info)3238 ice_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
3239 {
3240 struct ice_pf *pf = ice_netdev_to_pf(dev);
3241
3242 /* only report timestamping if PTP is enabled */
3243 if (!test_bit(ICE_FLAG_PTP, pf->flags))
3244 return ethtool_op_get_ts_info(dev, info);
3245
3246 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
3247 SOF_TIMESTAMPING_RX_SOFTWARE |
3248 SOF_TIMESTAMPING_SOFTWARE |
3249 SOF_TIMESTAMPING_TX_HARDWARE |
3250 SOF_TIMESTAMPING_RX_HARDWARE |
3251 SOF_TIMESTAMPING_RAW_HARDWARE;
3252
3253 info->phc_index = ice_get_ptp_clock_index(pf);
3254
3255 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
3256
3257 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
3258
3259 return 0;
3260 }
3261
3262 /**
3263 * ice_get_max_txq - return the maximum number of Tx queues for in a PF
3264 * @pf: PF structure
3265 */
ice_get_max_txq(struct ice_pf * pf)3266 static int ice_get_max_txq(struct ice_pf *pf)
3267 {
3268 return min3(pf->num_lan_msix, (u16)num_online_cpus(),
3269 (u16)pf->hw.func_caps.common_cap.num_txq);
3270 }
3271
3272 /**
3273 * ice_get_max_rxq - return the maximum number of Rx queues for in a PF
3274 * @pf: PF structure
3275 */
ice_get_max_rxq(struct ice_pf * pf)3276 static int ice_get_max_rxq(struct ice_pf *pf)
3277 {
3278 return min3(pf->num_lan_msix, (u16)num_online_cpus(),
3279 (u16)pf->hw.func_caps.common_cap.num_rxq);
3280 }
3281
3282 /**
3283 * ice_get_combined_cnt - return the current number of combined channels
3284 * @vsi: PF VSI pointer
3285 *
3286 * Go through all queue vectors and count ones that have both Rx and Tx ring
3287 * attached
3288 */
ice_get_combined_cnt(struct ice_vsi * vsi)3289 static u32 ice_get_combined_cnt(struct ice_vsi *vsi)
3290 {
3291 u32 combined = 0;
3292 int q_idx;
3293
3294 ice_for_each_q_vector(vsi, q_idx) {
3295 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
3296
3297 if (q_vector->rx.ring && q_vector->tx.ring)
3298 combined++;
3299 }
3300
3301 return combined;
3302 }
3303
3304 /**
3305 * ice_get_channels - get the current and max supported channels
3306 * @dev: network interface device structure
3307 * @ch: ethtool channel data structure
3308 */
3309 static void
ice_get_channels(struct net_device * dev,struct ethtool_channels * ch)3310 ice_get_channels(struct net_device *dev, struct ethtool_channels *ch)
3311 {
3312 struct ice_netdev_priv *np = netdev_priv(dev);
3313 struct ice_vsi *vsi = np->vsi;
3314 struct ice_pf *pf = vsi->back;
3315
3316 /* report maximum channels */
3317 ch->max_rx = ice_get_max_rxq(pf);
3318 ch->max_tx = ice_get_max_txq(pf);
3319 ch->max_combined = min_t(int, ch->max_rx, ch->max_tx);
3320
3321 /* report current channels */
3322 ch->combined_count = ice_get_combined_cnt(vsi);
3323 ch->rx_count = vsi->num_rxq - ch->combined_count;
3324 ch->tx_count = vsi->num_txq - ch->combined_count;
3325
3326 /* report other queues */
3327 ch->other_count = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
3328 ch->max_other = ch->other_count;
3329 }
3330
3331 /**
3332 * ice_get_valid_rss_size - return valid number of RSS queues
3333 * @hw: pointer to the HW structure
3334 * @new_size: requested RSS queues
3335 */
ice_get_valid_rss_size(struct ice_hw * hw,int new_size)3336 static int ice_get_valid_rss_size(struct ice_hw *hw, int new_size)
3337 {
3338 struct ice_hw_common_caps *caps = &hw->func_caps.common_cap;
3339
3340 return min_t(int, new_size, BIT(caps->rss_table_entry_width));
3341 }
3342
3343 /**
3344 * ice_vsi_set_dflt_rss_lut - set default RSS LUT with requested RSS size
3345 * @vsi: VSI to reconfigure RSS LUT on
3346 * @req_rss_size: requested range of queue numbers for hashing
3347 *
3348 * Set the VSI's RSS parameters, configure the RSS LUT based on these.
3349 */
ice_vsi_set_dflt_rss_lut(struct ice_vsi * vsi,int req_rss_size)3350 static int ice_vsi_set_dflt_rss_lut(struct ice_vsi *vsi, int req_rss_size)
3351 {
3352 struct ice_pf *pf = vsi->back;
3353 struct device *dev;
3354 struct ice_hw *hw;
3355 int err;
3356 u8 *lut;
3357
3358 dev = ice_pf_to_dev(pf);
3359 hw = &pf->hw;
3360
3361 if (!req_rss_size)
3362 return -EINVAL;
3363
3364 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
3365 if (!lut)
3366 return -ENOMEM;
3367
3368 /* set RSS LUT parameters */
3369 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3370 vsi->rss_size = 1;
3371 else
3372 vsi->rss_size = ice_get_valid_rss_size(hw, req_rss_size);
3373
3374 /* create/set RSS LUT */
3375 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
3376 err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
3377 if (err)
3378 dev_err(dev, "Cannot set RSS lut, err %d aq_err %s\n", err,
3379 ice_aq_str(hw->adminq.sq_last_status));
3380
3381 kfree(lut);
3382 return err;
3383 }
3384
3385 /**
3386 * ice_set_channels - set the number channels
3387 * @dev: network interface device structure
3388 * @ch: ethtool channel data structure
3389 */
ice_set_channels(struct net_device * dev,struct ethtool_channels * ch)3390 static int ice_set_channels(struct net_device *dev, struct ethtool_channels *ch)
3391 {
3392 struct ice_netdev_priv *np = netdev_priv(dev);
3393 struct ice_vsi *vsi = np->vsi;
3394 struct ice_pf *pf = vsi->back;
3395 int new_rx = 0, new_tx = 0;
3396 bool locked = false;
3397 u32 curr_combined;
3398 int ret = 0;
3399
3400 /* do not support changing channels in Safe Mode */
3401 if (ice_is_safe_mode(pf)) {
3402 netdev_err(dev, "Changing channel in Safe Mode is not supported\n");
3403 return -EOPNOTSUPP;
3404 }
3405 /* do not support changing other_count */
3406 if (ch->other_count != (test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1U : 0U))
3407 return -EINVAL;
3408
3409 if (test_bit(ICE_FLAG_FD_ENA, pf->flags) && pf->hw.fdir_active_fltr) {
3410 netdev_err(dev, "Cannot set channels when Flow Director filters are active\n");
3411 return -EOPNOTSUPP;
3412 }
3413
3414 curr_combined = ice_get_combined_cnt(vsi);
3415
3416 /* these checks are for cases where user didn't specify a particular
3417 * value on cmd line but we get non-zero value anyway via
3418 * get_channels(); look at ethtool.c in ethtool repository (the user
3419 * space part), particularly, do_schannels() routine
3420 */
3421 if (ch->rx_count == vsi->num_rxq - curr_combined)
3422 ch->rx_count = 0;
3423 if (ch->tx_count == vsi->num_txq - curr_combined)
3424 ch->tx_count = 0;
3425 if (ch->combined_count == curr_combined)
3426 ch->combined_count = 0;
3427
3428 if (!(ch->combined_count || (ch->rx_count && ch->tx_count))) {
3429 netdev_err(dev, "Please specify at least 1 Rx and 1 Tx channel\n");
3430 return -EINVAL;
3431 }
3432
3433 new_rx = ch->combined_count + ch->rx_count;
3434 new_tx = ch->combined_count + ch->tx_count;
3435
3436 if (new_rx > ice_get_max_rxq(pf)) {
3437 netdev_err(dev, "Maximum allowed Rx channels is %d\n",
3438 ice_get_max_rxq(pf));
3439 return -EINVAL;
3440 }
3441 if (new_tx > ice_get_max_txq(pf)) {
3442 netdev_err(dev, "Maximum allowed Tx channels is %d\n",
3443 ice_get_max_txq(pf));
3444 return -EINVAL;
3445 }
3446
3447 if (pf->adev) {
3448 mutex_lock(&pf->adev_mutex);
3449 device_lock(&pf->adev->dev);
3450 locked = true;
3451 if (pf->adev->dev.driver) {
3452 netdev_err(dev, "Cannot change channels when RDMA is active\n");
3453 ret = -EBUSY;
3454 goto adev_unlock;
3455 }
3456 }
3457
3458 ice_vsi_recfg_qs(vsi, new_rx, new_tx, locked);
3459
3460 if (!netif_is_rxfh_configured(dev)) {
3461 ret = ice_vsi_set_dflt_rss_lut(vsi, new_rx);
3462 goto adev_unlock;
3463 }
3464
3465 /* Update rss_size due to change in Rx queues */
3466 vsi->rss_size = ice_get_valid_rss_size(&pf->hw, new_rx);
3467
3468 adev_unlock:
3469 if (locked) {
3470 device_unlock(&pf->adev->dev);
3471 mutex_unlock(&pf->adev_mutex);
3472 }
3473 return ret;
3474 }
3475
3476 /**
3477 * ice_get_wol - get current Wake on LAN configuration
3478 * @netdev: network interface device structure
3479 * @wol: Ethtool structure to retrieve WoL settings
3480 */
ice_get_wol(struct net_device * netdev,struct ethtool_wolinfo * wol)3481 static void ice_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3482 {
3483 struct ice_netdev_priv *np = netdev_priv(netdev);
3484 struct ice_pf *pf = np->vsi->back;
3485
3486 if (np->vsi->type != ICE_VSI_PF)
3487 netdev_warn(netdev, "Wake on LAN is not supported on this interface!\n");
3488
3489 /* Get WoL settings based on the HW capability */
3490 if (ice_is_wol_supported(&pf->hw)) {
3491 wol->supported = WAKE_MAGIC;
3492 wol->wolopts = pf->wol_ena ? WAKE_MAGIC : 0;
3493 } else {
3494 wol->supported = 0;
3495 wol->wolopts = 0;
3496 }
3497 }
3498
3499 /**
3500 * ice_set_wol - set Wake on LAN on supported device
3501 * @netdev: network interface device structure
3502 * @wol: Ethtool structure to set WoL
3503 */
ice_set_wol(struct net_device * netdev,struct ethtool_wolinfo * wol)3504 static int ice_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3505 {
3506 struct ice_netdev_priv *np = netdev_priv(netdev);
3507 struct ice_vsi *vsi = np->vsi;
3508 struct ice_pf *pf = vsi->back;
3509
3510 if (vsi->type != ICE_VSI_PF || !ice_is_wol_supported(&pf->hw))
3511 return -EOPNOTSUPP;
3512
3513 /* only magic packet is supported */
3514 if (wol->wolopts && wol->wolopts != WAKE_MAGIC)
3515 return -EOPNOTSUPP;
3516
3517 /* Set WoL only if there is a new value */
3518 if (pf->wol_ena != !!wol->wolopts) {
3519 pf->wol_ena = !!wol->wolopts;
3520 device_set_wakeup_enable(ice_pf_to_dev(pf), pf->wol_ena);
3521 netdev_dbg(netdev, "WoL magic packet %sabled\n",
3522 pf->wol_ena ? "en" : "dis");
3523 }
3524
3525 return 0;
3526 }
3527
3528 /**
3529 * ice_get_rc_coalesce - get ITR values for specific ring container
3530 * @ec: ethtool structure to fill with driver's coalesce settings
3531 * @rc: ring container that the ITR values will come from
3532 *
3533 * Query the device for ice_ring_container specific ITR values. This is
3534 * done per ice_ring_container because each q_vector can have 1 or more rings
3535 * and all of said ring(s) will have the same ITR values.
3536 *
3537 * Returns 0 on success, negative otherwise.
3538 */
3539 static int
ice_get_rc_coalesce(struct ethtool_coalesce * ec,struct ice_ring_container * rc)3540 ice_get_rc_coalesce(struct ethtool_coalesce *ec, struct ice_ring_container *rc)
3541 {
3542 if (!rc->ring)
3543 return -EINVAL;
3544
3545 switch (rc->type) {
3546 case ICE_RX_CONTAINER:
3547 ec->use_adaptive_rx_coalesce = ITR_IS_DYNAMIC(rc);
3548 ec->rx_coalesce_usecs = rc->itr_setting;
3549 ec->rx_coalesce_usecs_high = rc->ring->q_vector->intrl;
3550 break;
3551 case ICE_TX_CONTAINER:
3552 ec->use_adaptive_tx_coalesce = ITR_IS_DYNAMIC(rc);
3553 ec->tx_coalesce_usecs = rc->itr_setting;
3554 break;
3555 default:
3556 dev_dbg(ice_pf_to_dev(rc->ring->vsi->back), "Invalid c_type %d\n", rc->type);
3557 return -EINVAL;
3558 }
3559
3560 return 0;
3561 }
3562
3563 /**
3564 * ice_get_q_coalesce - get a queue's ITR/INTRL (coalesce) settings
3565 * @vsi: VSI associated to the queue for getting ITR/INTRL (coalesce) settings
3566 * @ec: coalesce settings to program the device with
3567 * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3568 *
3569 * Return 0 on success, and negative under the following conditions:
3570 * 1. Getting Tx or Rx ITR/INTRL (coalesce) settings failed.
3571 * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3572 */
3573 static int
ice_get_q_coalesce(struct ice_vsi * vsi,struct ethtool_coalesce * ec,int q_num)3574 ice_get_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3575 {
3576 if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3577 if (ice_get_rc_coalesce(ec,
3578 &vsi->rx_rings[q_num]->q_vector->rx))
3579 return -EINVAL;
3580 if (ice_get_rc_coalesce(ec,
3581 &vsi->tx_rings[q_num]->q_vector->tx))
3582 return -EINVAL;
3583 } else if (q_num < vsi->num_rxq) {
3584 if (ice_get_rc_coalesce(ec,
3585 &vsi->rx_rings[q_num]->q_vector->rx))
3586 return -EINVAL;
3587 } else if (q_num < vsi->num_txq) {
3588 if (ice_get_rc_coalesce(ec,
3589 &vsi->tx_rings[q_num]->q_vector->tx))
3590 return -EINVAL;
3591 } else {
3592 return -EINVAL;
3593 }
3594
3595 return 0;
3596 }
3597
3598 /**
3599 * __ice_get_coalesce - get ITR/INTRL values for the device
3600 * @netdev: pointer to the netdev associated with this query
3601 * @ec: ethtool structure to fill with driver's coalesce settings
3602 * @q_num: queue number to get the coalesce settings for
3603 *
3604 * If the caller passes in a negative q_num then we return coalesce settings
3605 * based on queue number 0, else use the actual q_num passed in.
3606 */
3607 static int
__ice_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,int q_num)3608 __ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3609 int q_num)
3610 {
3611 struct ice_netdev_priv *np = netdev_priv(netdev);
3612 struct ice_vsi *vsi = np->vsi;
3613
3614 if (q_num < 0)
3615 q_num = 0;
3616
3617 if (ice_get_q_coalesce(vsi, ec, q_num))
3618 return -EINVAL;
3619
3620 return 0;
3621 }
3622
ice_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)3623 static int ice_get_coalesce(struct net_device *netdev,
3624 struct ethtool_coalesce *ec,
3625 struct kernel_ethtool_coalesce *kernel_coal,
3626 struct netlink_ext_ack *extack)
3627 {
3628 return __ice_get_coalesce(netdev, ec, -1);
3629 }
3630
3631 static int
ice_get_per_q_coalesce(struct net_device * netdev,u32 q_num,struct ethtool_coalesce * ec)3632 ice_get_per_q_coalesce(struct net_device *netdev, u32 q_num,
3633 struct ethtool_coalesce *ec)
3634 {
3635 return __ice_get_coalesce(netdev, ec, q_num);
3636 }
3637
3638 /**
3639 * ice_set_rc_coalesce - set ITR values for specific ring container
3640 * @ec: ethtool structure from user to update ITR settings
3641 * @rc: ring container that the ITR values will come from
3642 * @vsi: VSI associated to the ring container
3643 *
3644 * Set specific ITR values. This is done per ice_ring_container because each
3645 * q_vector can have 1 or more rings and all of said ring(s) will have the same
3646 * ITR values.
3647 *
3648 * Returns 0 on success, negative otherwise.
3649 */
3650 static int
ice_set_rc_coalesce(struct ethtool_coalesce * ec,struct ice_ring_container * rc,struct ice_vsi * vsi)3651 ice_set_rc_coalesce(struct ethtool_coalesce *ec,
3652 struct ice_ring_container *rc, struct ice_vsi *vsi)
3653 {
3654 const char *c_type_str = (rc->type == ICE_RX_CONTAINER) ? "rx" : "tx";
3655 u32 use_adaptive_coalesce, coalesce_usecs;
3656 struct ice_pf *pf = vsi->back;
3657 u16 itr_setting;
3658
3659 if (!rc->ring)
3660 return -EINVAL;
3661
3662 switch (rc->type) {
3663 case ICE_RX_CONTAINER:
3664 if (ec->rx_coalesce_usecs_high > ICE_MAX_INTRL ||
3665 (ec->rx_coalesce_usecs_high &&
3666 ec->rx_coalesce_usecs_high < pf->hw.intrl_gran)) {
3667 netdev_info(vsi->netdev, "Invalid value, %s-usecs-high valid values are 0 (disabled), %d-%d\n",
3668 c_type_str, pf->hw.intrl_gran,
3669 ICE_MAX_INTRL);
3670 return -EINVAL;
3671 }
3672 if (ec->rx_coalesce_usecs_high != rc->ring->q_vector->intrl &&
3673 (ec->use_adaptive_rx_coalesce || ec->use_adaptive_tx_coalesce)) {
3674 netdev_info(vsi->netdev, "Invalid value, %s-usecs-high cannot be changed if adaptive-tx or adaptive-rx is enabled\n",
3675 c_type_str);
3676 return -EINVAL;
3677 }
3678 if (ec->rx_coalesce_usecs_high != rc->ring->q_vector->intrl) {
3679 rc->ring->q_vector->intrl = ec->rx_coalesce_usecs_high;
3680 ice_write_intrl(rc->ring->q_vector,
3681 ec->rx_coalesce_usecs_high);
3682 }
3683
3684 use_adaptive_coalesce = ec->use_adaptive_rx_coalesce;
3685 coalesce_usecs = ec->rx_coalesce_usecs;
3686
3687 break;
3688 case ICE_TX_CONTAINER:
3689 use_adaptive_coalesce = ec->use_adaptive_tx_coalesce;
3690 coalesce_usecs = ec->tx_coalesce_usecs;
3691
3692 break;
3693 default:
3694 dev_dbg(ice_pf_to_dev(pf), "Invalid container type %d\n",
3695 rc->type);
3696 return -EINVAL;
3697 }
3698
3699 itr_setting = rc->itr_setting;
3700 if (coalesce_usecs != itr_setting && use_adaptive_coalesce) {
3701 netdev_info(vsi->netdev, "%s interrupt throttling cannot be changed if adaptive-%s is enabled\n",
3702 c_type_str, c_type_str);
3703 return -EINVAL;
3704 }
3705
3706 if (coalesce_usecs > ICE_ITR_MAX) {
3707 netdev_info(vsi->netdev, "Invalid value, %s-usecs range is 0-%d\n",
3708 c_type_str, ICE_ITR_MAX);
3709 return -EINVAL;
3710 }
3711
3712 if (use_adaptive_coalesce) {
3713 rc->itr_mode = ITR_DYNAMIC;
3714 } else {
3715 rc->itr_mode = ITR_STATIC;
3716 /* store user facing value how it was set */
3717 rc->itr_setting = coalesce_usecs;
3718 /* write the change to the register */
3719 ice_write_itr(rc, coalesce_usecs);
3720 /* force writes to take effect immediately, the flush shouldn't
3721 * be done in the functions above because the intent is for
3722 * them to do lazy writes.
3723 */
3724 ice_flush(&pf->hw);
3725 }
3726
3727 return 0;
3728 }
3729
3730 /**
3731 * ice_set_q_coalesce - set a queue's ITR/INTRL (coalesce) settings
3732 * @vsi: VSI associated to the queue that need updating
3733 * @ec: coalesce settings to program the device with
3734 * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3735 *
3736 * Return 0 on success, and negative under the following conditions:
3737 * 1. Setting Tx or Rx ITR/INTRL (coalesce) settings failed.
3738 * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3739 */
3740 static int
ice_set_q_coalesce(struct ice_vsi * vsi,struct ethtool_coalesce * ec,int q_num)3741 ice_set_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3742 {
3743 if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3744 if (ice_set_rc_coalesce(ec,
3745 &vsi->rx_rings[q_num]->q_vector->rx,
3746 vsi))
3747 return -EINVAL;
3748
3749 if (ice_set_rc_coalesce(ec,
3750 &vsi->tx_rings[q_num]->q_vector->tx,
3751 vsi))
3752 return -EINVAL;
3753 } else if (q_num < vsi->num_rxq) {
3754 if (ice_set_rc_coalesce(ec,
3755 &vsi->rx_rings[q_num]->q_vector->rx,
3756 vsi))
3757 return -EINVAL;
3758 } else if (q_num < vsi->num_txq) {
3759 if (ice_set_rc_coalesce(ec,
3760 &vsi->tx_rings[q_num]->q_vector->tx,
3761 vsi))
3762 return -EINVAL;
3763 } else {
3764 return -EINVAL;
3765 }
3766
3767 return 0;
3768 }
3769
3770 /**
3771 * ice_print_if_odd_usecs - print message if user tries to set odd [tx|rx]-usecs
3772 * @netdev: netdev used for print
3773 * @itr_setting: previous user setting
3774 * @use_adaptive_coalesce: if adaptive coalesce is enabled or being enabled
3775 * @coalesce_usecs: requested value of [tx|rx]-usecs
3776 * @c_type_str: either "rx" or "tx" to match user set field of [tx|rx]-usecs
3777 */
3778 static void
ice_print_if_odd_usecs(struct net_device * netdev,u16 itr_setting,u32 use_adaptive_coalesce,u32 coalesce_usecs,const char * c_type_str)3779 ice_print_if_odd_usecs(struct net_device *netdev, u16 itr_setting,
3780 u32 use_adaptive_coalesce, u32 coalesce_usecs,
3781 const char *c_type_str)
3782 {
3783 if (use_adaptive_coalesce)
3784 return;
3785
3786 if (itr_setting != coalesce_usecs && (coalesce_usecs % 2))
3787 netdev_info(netdev, "User set %s-usecs to %d, device only supports even values. Rounding down and attempting to set %s-usecs to %d\n",
3788 c_type_str, coalesce_usecs, c_type_str,
3789 ITR_REG_ALIGN(coalesce_usecs));
3790 }
3791
3792 /**
3793 * __ice_set_coalesce - set ITR/INTRL values for the device
3794 * @netdev: pointer to the netdev associated with this query
3795 * @ec: ethtool structure to fill with driver's coalesce settings
3796 * @q_num: queue number to get the coalesce settings for
3797 *
3798 * If the caller passes in a negative q_num then we set the coalesce settings
3799 * for all Tx/Rx queues, else use the actual q_num passed in.
3800 */
3801 static int
__ice_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,int q_num)3802 __ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3803 int q_num)
3804 {
3805 struct ice_netdev_priv *np = netdev_priv(netdev);
3806 struct ice_vsi *vsi = np->vsi;
3807
3808 if (q_num < 0) {
3809 struct ice_q_vector *q_vector = vsi->q_vectors[0];
3810 int v_idx;
3811
3812 if (q_vector) {
3813 ice_print_if_odd_usecs(netdev, q_vector->rx.itr_setting,
3814 ec->use_adaptive_rx_coalesce,
3815 ec->rx_coalesce_usecs, "rx");
3816
3817 ice_print_if_odd_usecs(netdev, q_vector->tx.itr_setting,
3818 ec->use_adaptive_tx_coalesce,
3819 ec->tx_coalesce_usecs, "tx");
3820 }
3821
3822 ice_for_each_q_vector(vsi, v_idx) {
3823 /* In some cases if DCB is configured the num_[rx|tx]q
3824 * can be less than vsi->num_q_vectors. This check
3825 * accounts for that so we don't report a false failure
3826 */
3827 if (v_idx >= vsi->num_rxq && v_idx >= vsi->num_txq)
3828 goto set_complete;
3829
3830 if (ice_set_q_coalesce(vsi, ec, v_idx))
3831 return -EINVAL;
3832 }
3833 goto set_complete;
3834 }
3835
3836 if (ice_set_q_coalesce(vsi, ec, q_num))
3837 return -EINVAL;
3838
3839 set_complete:
3840 return 0;
3841 }
3842
ice_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)3843 static int ice_set_coalesce(struct net_device *netdev,
3844 struct ethtool_coalesce *ec,
3845 struct kernel_ethtool_coalesce *kernel_coal,
3846 struct netlink_ext_ack *extack)
3847 {
3848 return __ice_set_coalesce(netdev, ec, -1);
3849 }
3850
3851 static int
ice_set_per_q_coalesce(struct net_device * netdev,u32 q_num,struct ethtool_coalesce * ec)3852 ice_set_per_q_coalesce(struct net_device *netdev, u32 q_num,
3853 struct ethtool_coalesce *ec)
3854 {
3855 return __ice_set_coalesce(netdev, ec, q_num);
3856 }
3857
3858 #define ICE_I2C_EEPROM_DEV_ADDR 0xA0
3859 #define ICE_I2C_EEPROM_DEV_ADDR2 0xA2
3860 #define ICE_MODULE_TYPE_SFP 0x03
3861 #define ICE_MODULE_TYPE_QSFP_PLUS 0x0D
3862 #define ICE_MODULE_TYPE_QSFP28 0x11
3863 #define ICE_MODULE_SFF_ADDR_MODE 0x04
3864 #define ICE_MODULE_SFF_DIAG_CAPAB 0x40
3865 #define ICE_MODULE_REVISION_ADDR 0x01
3866 #define ICE_MODULE_SFF_8472_COMP 0x5E
3867 #define ICE_MODULE_SFF_8472_SWAP 0x5C
3868 #define ICE_MODULE_QSFP_MAX_LEN 640
3869
3870 /**
3871 * ice_get_module_info - get SFF module type and revision information
3872 * @netdev: network interface device structure
3873 * @modinfo: module EEPROM size and layout information structure
3874 */
3875 static int
ice_get_module_info(struct net_device * netdev,struct ethtool_modinfo * modinfo)3876 ice_get_module_info(struct net_device *netdev,
3877 struct ethtool_modinfo *modinfo)
3878 {
3879 struct ice_netdev_priv *np = netdev_priv(netdev);
3880 struct ice_vsi *vsi = np->vsi;
3881 struct ice_pf *pf = vsi->back;
3882 struct ice_hw *hw = &pf->hw;
3883 enum ice_status status;
3884 u8 sff8472_comp = 0;
3885 u8 sff8472_swap = 0;
3886 u8 sff8636_rev = 0;
3887 u8 value = 0;
3888
3889 status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR, 0x00, 0x00,
3890 0, &value, 1, 0, NULL);
3891 if (status)
3892 return -EIO;
3893
3894 switch (value) {
3895 case ICE_MODULE_TYPE_SFP:
3896 status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
3897 ICE_MODULE_SFF_8472_COMP, 0x00, 0,
3898 &sff8472_comp, 1, 0, NULL);
3899 if (status)
3900 return -EIO;
3901 status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
3902 ICE_MODULE_SFF_8472_SWAP, 0x00, 0,
3903 &sff8472_swap, 1, 0, NULL);
3904 if (status)
3905 return -EIO;
3906
3907 if (sff8472_swap & ICE_MODULE_SFF_ADDR_MODE) {
3908 modinfo->type = ETH_MODULE_SFF_8079;
3909 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
3910 } else if (sff8472_comp &&
3911 (sff8472_swap & ICE_MODULE_SFF_DIAG_CAPAB)) {
3912 modinfo->type = ETH_MODULE_SFF_8472;
3913 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
3914 } else {
3915 modinfo->type = ETH_MODULE_SFF_8079;
3916 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
3917 }
3918 break;
3919 case ICE_MODULE_TYPE_QSFP_PLUS:
3920 case ICE_MODULE_TYPE_QSFP28:
3921 status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
3922 ICE_MODULE_REVISION_ADDR, 0x00, 0,
3923 &sff8636_rev, 1, 0, NULL);
3924 if (status)
3925 return -EIO;
3926 /* Check revision compliance */
3927 if (sff8636_rev > 0x02) {
3928 /* Module is SFF-8636 compliant */
3929 modinfo->type = ETH_MODULE_SFF_8636;
3930 modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN;
3931 } else {
3932 modinfo->type = ETH_MODULE_SFF_8436;
3933 modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN;
3934 }
3935 break;
3936 default:
3937 netdev_warn(netdev, "SFF Module Type not recognized.\n");
3938 return -EINVAL;
3939 }
3940 return 0;
3941 }
3942
3943 /**
3944 * ice_get_module_eeprom - fill buffer with SFF EEPROM contents
3945 * @netdev: network interface device structure
3946 * @ee: EEPROM dump request structure
3947 * @data: buffer to be filled with EEPROM contents
3948 */
3949 static int
ice_get_module_eeprom(struct net_device * netdev,struct ethtool_eeprom * ee,u8 * data)3950 ice_get_module_eeprom(struct net_device *netdev,
3951 struct ethtool_eeprom *ee, u8 *data)
3952 {
3953 struct ice_netdev_priv *np = netdev_priv(netdev);
3954 #define SFF_READ_BLOCK_SIZE 8
3955 u8 value[SFF_READ_BLOCK_SIZE] = { 0 };
3956 u8 addr = ICE_I2C_EEPROM_DEV_ADDR;
3957 struct ice_vsi *vsi = np->vsi;
3958 struct ice_pf *pf = vsi->back;
3959 struct ice_hw *hw = &pf->hw;
3960 enum ice_status status;
3961 bool is_sfp = false;
3962 unsigned int i, j;
3963 u16 offset = 0;
3964 u8 page = 0;
3965
3966 if (!ee || !ee->len || !data)
3967 return -EINVAL;
3968
3969 status = ice_aq_sff_eeprom(hw, 0, addr, offset, page, 0, value, 1, 0,
3970 NULL);
3971 if (status)
3972 return -EIO;
3973
3974 if (value[0] == ICE_MODULE_TYPE_SFP)
3975 is_sfp = true;
3976
3977 memset(data, 0, ee->len);
3978 for (i = 0; i < ee->len; i += SFF_READ_BLOCK_SIZE) {
3979 offset = i + ee->offset;
3980 page = 0;
3981
3982 /* Check if we need to access the other memory page */
3983 if (is_sfp) {
3984 if (offset >= ETH_MODULE_SFF_8079_LEN) {
3985 offset -= ETH_MODULE_SFF_8079_LEN;
3986 addr = ICE_I2C_EEPROM_DEV_ADDR2;
3987 }
3988 } else {
3989 while (offset >= ETH_MODULE_SFF_8436_LEN) {
3990 /* Compute memory page number and offset. */
3991 offset -= ETH_MODULE_SFF_8436_LEN / 2;
3992 page++;
3993 }
3994 }
3995
3996 /* Bit 2 of EEPROM address 0x02 declares upper
3997 * pages are disabled on QSFP modules.
3998 * SFP modules only ever use page 0.
3999 */
4000 if (page == 0 || !(data[0x2] & 0x4)) {
4001 u32 copy_len;
4002
4003 /* If i2c bus is busy due to slow page change or
4004 * link management access, call can fail. This is normal.
4005 * So we retry this a few times.
4006 */
4007 for (j = 0; j < 4; j++) {
4008 status = ice_aq_sff_eeprom(hw, 0, addr, offset, page,
4009 !is_sfp, value,
4010 SFF_READ_BLOCK_SIZE,
4011 0, NULL);
4012 netdev_dbg(netdev, "SFF %02X %02X %02X %X = %02X%02X%02X%02X.%02X%02X%02X%02X (%X)\n",
4013 addr, offset, page, is_sfp,
4014 value[0], value[1], value[2], value[3],
4015 value[4], value[5], value[6], value[7],
4016 status);
4017 if (status) {
4018 usleep_range(1500, 2500);
4019 memset(value, 0, SFF_READ_BLOCK_SIZE);
4020 continue;
4021 }
4022 break;
4023 }
4024
4025 /* Make sure we have enough room for the new block */
4026 copy_len = min_t(u32, SFF_READ_BLOCK_SIZE, ee->len - i);
4027 memcpy(data + i, value, copy_len);
4028 }
4029 }
4030 return 0;
4031 }
4032
4033 static const struct ethtool_ops ice_ethtool_ops = {
4034 .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
4035 ETHTOOL_COALESCE_USE_ADAPTIVE |
4036 ETHTOOL_COALESCE_RX_USECS_HIGH,
4037 .get_link_ksettings = ice_get_link_ksettings,
4038 .set_link_ksettings = ice_set_link_ksettings,
4039 .get_drvinfo = ice_get_drvinfo,
4040 .get_regs_len = ice_get_regs_len,
4041 .get_regs = ice_get_regs,
4042 .get_wol = ice_get_wol,
4043 .set_wol = ice_set_wol,
4044 .get_msglevel = ice_get_msglevel,
4045 .set_msglevel = ice_set_msglevel,
4046 .self_test = ice_self_test,
4047 .get_link = ethtool_op_get_link,
4048 .get_eeprom_len = ice_get_eeprom_len,
4049 .get_eeprom = ice_get_eeprom,
4050 .get_coalesce = ice_get_coalesce,
4051 .set_coalesce = ice_set_coalesce,
4052 .get_strings = ice_get_strings,
4053 .set_phys_id = ice_set_phys_id,
4054 .get_ethtool_stats = ice_get_ethtool_stats,
4055 .get_priv_flags = ice_get_priv_flags,
4056 .set_priv_flags = ice_set_priv_flags,
4057 .get_sset_count = ice_get_sset_count,
4058 .get_rxnfc = ice_get_rxnfc,
4059 .set_rxnfc = ice_set_rxnfc,
4060 .get_ringparam = ice_get_ringparam,
4061 .set_ringparam = ice_set_ringparam,
4062 .nway_reset = ice_nway_reset,
4063 .get_pauseparam = ice_get_pauseparam,
4064 .set_pauseparam = ice_set_pauseparam,
4065 .get_rxfh_key_size = ice_get_rxfh_key_size,
4066 .get_rxfh_indir_size = ice_get_rxfh_indir_size,
4067 .get_rxfh = ice_get_rxfh,
4068 .set_rxfh = ice_set_rxfh,
4069 .get_channels = ice_get_channels,
4070 .set_channels = ice_set_channels,
4071 .get_ts_info = ice_get_ts_info,
4072 .get_per_queue_coalesce = ice_get_per_q_coalesce,
4073 .set_per_queue_coalesce = ice_set_per_q_coalesce,
4074 .get_fecparam = ice_get_fecparam,
4075 .set_fecparam = ice_set_fecparam,
4076 .get_module_info = ice_get_module_info,
4077 .get_module_eeprom = ice_get_module_eeprom,
4078 };
4079
4080 static const struct ethtool_ops ice_ethtool_safe_mode_ops = {
4081 .get_link_ksettings = ice_get_link_ksettings,
4082 .set_link_ksettings = ice_set_link_ksettings,
4083 .get_drvinfo = ice_get_drvinfo,
4084 .get_regs_len = ice_get_regs_len,
4085 .get_regs = ice_get_regs,
4086 .get_wol = ice_get_wol,
4087 .set_wol = ice_set_wol,
4088 .get_msglevel = ice_get_msglevel,
4089 .set_msglevel = ice_set_msglevel,
4090 .get_link = ethtool_op_get_link,
4091 .get_eeprom_len = ice_get_eeprom_len,
4092 .get_eeprom = ice_get_eeprom,
4093 .get_strings = ice_get_strings,
4094 .get_ethtool_stats = ice_get_ethtool_stats,
4095 .get_sset_count = ice_get_sset_count,
4096 .get_ringparam = ice_get_ringparam,
4097 .set_ringparam = ice_set_ringparam,
4098 .nway_reset = ice_nway_reset,
4099 .get_channels = ice_get_channels,
4100 };
4101
4102 /**
4103 * ice_set_ethtool_safe_mode_ops - setup safe mode ethtool ops
4104 * @netdev: network interface device structure
4105 */
ice_set_ethtool_safe_mode_ops(struct net_device * netdev)4106 void ice_set_ethtool_safe_mode_ops(struct net_device *netdev)
4107 {
4108 netdev->ethtool_ops = &ice_ethtool_safe_mode_ops;
4109 }
4110
4111 /**
4112 * ice_set_ethtool_ops - setup netdev ethtool ops
4113 * @netdev: network interface device structure
4114 *
4115 * setup netdev ethtool ops with ice specific ops
4116 */
ice_set_ethtool_ops(struct net_device * netdev)4117 void ice_set_ethtool_ops(struct net_device *netdev)
4118 {
4119 netdev->ethtool_ops = &ice_ethtool_ops;
4120 }
4121