Linux 6.11-rc1
[linux-2.6-microblaze.git] / drivers / net / ethernet / intel / i40e / i40e_ethtool.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3
4 /* ethtool support for i40e */
5
6 #include "i40e_devids.h"
7 #include "i40e_diag.h"
8 #include "i40e_txrx_common.h"
9 #include "i40e_virtchnl_pf.h"
10
11 /* ethtool statistics helpers */
12
13 /**
14  * struct i40e_stats - definition for an ethtool statistic
15  * @stat_string: statistic name to display in ethtool -S output
16  * @sizeof_stat: the sizeof() the stat, must be no greater than sizeof(u64)
17  * @stat_offset: offsetof() the stat from a base pointer
18  *
19  * This structure defines a statistic to be added to the ethtool stats buffer.
20  * It defines a statistic as offset from a common base pointer. Stats should
21  * be defined in constant arrays using the I40E_STAT macro, with every element
22  * of the array using the same _type for calculating the sizeof_stat and
23  * stat_offset.
24  *
25  * The @sizeof_stat is expected to be sizeof(u8), sizeof(u16), sizeof(u32) or
26  * sizeof(u64). Other sizes are not expected and will produce a WARN_ONCE from
27  * the i40e_add_ethtool_stat() helper function.
28  *
29  * The @stat_string is interpreted as a format string, allowing formatted
30  * values to be inserted while looping over multiple structures for a given
31  * statistics array. Thus, every statistic string in an array should have the
32  * same type and number of format specifiers, to be formatted by variadic
33  * arguments to the i40e_add_stat_string() helper function.
34  **/
35 struct i40e_stats {
36         char stat_string[ETH_GSTRING_LEN];
37         int sizeof_stat;
38         int stat_offset;
39 };
40
41 /* Helper macro to define an i40e_stat structure with proper size and type.
42  * Use this when defining constant statistics arrays. Note that @_type expects
43  * only a type name and is used multiple times.
44  */
45 #define I40E_STAT(_type, _name, _stat) { \
46         .stat_string = _name, \
47         .sizeof_stat = sizeof_field(_type, _stat), \
48         .stat_offset = offsetof(_type, _stat) \
49 }
50
51 /* Helper macro for defining some statistics directly copied from the netdev
52  * stats structure.
53  */
54 #define I40E_NETDEV_STAT(_net_stat) \
55         I40E_STAT(struct rtnl_link_stats64, #_net_stat, _net_stat)
56
57 /* Helper macro for defining some statistics related to queues */
58 #define I40E_QUEUE_STAT(_name, _stat) \
59         I40E_STAT(struct i40e_ring, _name, _stat)
60
61 /* Stats associated with a Tx or Rx ring */
62 static const struct i40e_stats i40e_gstrings_queue_stats[] = {
63         I40E_QUEUE_STAT("%s-%u.packets", stats.packets),
64         I40E_QUEUE_STAT("%s-%u.bytes", stats.bytes),
65 };
66
67 /**
68  * i40e_add_one_ethtool_stat - copy the stat into the supplied buffer
69  * @data: location to store the stat value
70  * @pointer: basis for where to copy from
71  * @stat: the stat definition
72  *
73  * Copies the stat data defined by the pointer and stat structure pair into
74  * the memory supplied as data. Used to implement i40e_add_ethtool_stats and
75  * i40e_add_queue_stats. If the pointer is null, data will be zero'd.
76  */
77 static void
78 i40e_add_one_ethtool_stat(u64 *data, void *pointer,
79                           const struct i40e_stats *stat)
80 {
81         char *p;
82
83         if (!pointer) {
84                 /* ensure that the ethtool data buffer is zero'd for any stats
85                  * which don't have a valid pointer.
86                  */
87                 *data = 0;
88                 return;
89         }
90
91         p = (char *)pointer + stat->stat_offset;
92         switch (stat->sizeof_stat) {
93         case sizeof(u64):
94                 *data = *((u64 *)p);
95                 break;
96         case sizeof(u32):
97                 *data = *((u32 *)p);
98                 break;
99         case sizeof(u16):
100                 *data = *((u16 *)p);
101                 break;
102         case sizeof(u8):
103                 *data = *((u8 *)p);
104                 break;
105         default:
106                 WARN_ONCE(1, "unexpected stat size for %s",
107                           stat->stat_string);
108                 *data = 0;
109         }
110 }
111
112 /**
113  * __i40e_add_ethtool_stats - copy stats into the ethtool supplied buffer
114  * @data: ethtool stats buffer
115  * @pointer: location to copy stats from
116  * @stats: array of stats to copy
117  * @size: the size of the stats definition
118  *
119  * Copy the stats defined by the stats array using the pointer as a base into
120  * the data buffer supplied by ethtool. Updates the data pointer to point to
121  * the next empty location for successive calls to __i40e_add_ethtool_stats.
122  * If pointer is null, set the data values to zero and update the pointer to
123  * skip these stats.
124  **/
125 static void
126 __i40e_add_ethtool_stats(u64 **data, void *pointer,
127                          const struct i40e_stats stats[],
128                          const unsigned int size)
129 {
130         unsigned int i;
131
132         for (i = 0; i < size; i++)
133                 i40e_add_one_ethtool_stat((*data)++, pointer, &stats[i]);
134 }
135
136 /**
137  * i40e_add_ethtool_stats - copy stats into ethtool supplied buffer
138  * @data: ethtool stats buffer
139  * @pointer: location where stats are stored
140  * @stats: static const array of stat definitions
141  *
142  * Macro to ease the use of __i40e_add_ethtool_stats by taking a static
143  * constant stats array and passing the ARRAY_SIZE(). This avoids typos by
144  * ensuring that we pass the size associated with the given stats array.
145  *
146  * The parameter @stats is evaluated twice, so parameters with side effects
147  * should be avoided.
148  **/
149 #define i40e_add_ethtool_stats(data, pointer, stats) \
150         __i40e_add_ethtool_stats(data, pointer, stats, ARRAY_SIZE(stats))
151
152 /**
153  * i40e_add_queue_stats - copy queue statistics into supplied buffer
154  * @data: ethtool stats buffer
155  * @ring: the ring to copy
156  *
157  * Queue statistics must be copied while protected by
158  * u64_stats_fetch_begin, so we can't directly use i40e_add_ethtool_stats.
159  * Assumes that queue stats are defined in i40e_gstrings_queue_stats. If the
160  * ring pointer is null, zero out the queue stat values and update the data
161  * pointer. Otherwise safely copy the stats from the ring into the supplied
162  * buffer and update the data pointer when finished.
163  *
164  * This function expects to be called while under rcu_read_lock().
165  **/
166 static void
167 i40e_add_queue_stats(u64 **data, struct i40e_ring *ring)
168 {
169         const unsigned int size = ARRAY_SIZE(i40e_gstrings_queue_stats);
170         const struct i40e_stats *stats = i40e_gstrings_queue_stats;
171         unsigned int start;
172         unsigned int i;
173
174         /* To avoid invalid statistics values, ensure that we keep retrying
175          * the copy until we get a consistent value according to
176          * u64_stats_fetch_retry. But first, make sure our ring is
177          * non-null before attempting to access its syncp.
178          */
179         do {
180                 start = !ring ? 0 : u64_stats_fetch_begin(&ring->syncp);
181                 for (i = 0; i < size; i++) {
182                         i40e_add_one_ethtool_stat(&(*data)[i], ring,
183                                                   &stats[i]);
184                 }
185         } while (ring && u64_stats_fetch_retry(&ring->syncp, start));
186
187         /* Once we successfully copy the stats in, update the data pointer */
188         *data += size;
189 }
190
191 /**
192  * __i40e_add_stat_strings - copy stat strings into ethtool buffer
193  * @p: ethtool supplied buffer
194  * @stats: stat definitions array
195  * @size: size of the stats array
196  *
197  * Format and copy the strings described by stats into the buffer pointed at
198  * by p.
199  **/
200 static void __i40e_add_stat_strings(u8 **p, const struct i40e_stats stats[],
201                                     const unsigned int size, ...)
202 {
203         unsigned int i;
204
205         for (i = 0; i < size; i++) {
206                 va_list args;
207
208                 va_start(args, size);
209                 vsnprintf(*p, ETH_GSTRING_LEN, stats[i].stat_string, args);
210                 *p += ETH_GSTRING_LEN;
211                 va_end(args);
212         }
213 }
214
215 /**
216  * i40e_add_stat_strings - copy stat strings into ethtool buffer
217  * @p: ethtool supplied buffer
218  * @stats: stat definitions array
219  *
220  * Format and copy the strings described by the const static stats value into
221  * the buffer pointed at by p.
222  *
223  * The parameter @stats is evaluated twice, so parameters with side effects
224  * should be avoided. Additionally, stats must be an array such that
225  * ARRAY_SIZE can be called on it.
226  **/
227 #define i40e_add_stat_strings(p, stats, ...) \
228         __i40e_add_stat_strings(p, stats, ARRAY_SIZE(stats), ## __VA_ARGS__)
229
230 #define I40E_PF_STAT(_name, _stat) \
231         I40E_STAT(struct i40e_pf, _name, _stat)
232 #define I40E_VSI_STAT(_name, _stat) \
233         I40E_STAT(struct i40e_vsi, _name, _stat)
234 #define I40E_VEB_STAT(_name, _stat) \
235         I40E_STAT(struct i40e_veb, _name, _stat)
236 #define I40E_VEB_TC_STAT(_name, _stat) \
237         I40E_STAT(struct i40e_cp_veb_tc_stats, _name, _stat)
238 #define I40E_PFC_STAT(_name, _stat) \
239         I40E_STAT(struct i40e_pfc_stats, _name, _stat)
240
241 static const struct i40e_stats i40e_gstrings_net_stats[] = {
242         I40E_NETDEV_STAT(rx_packets),
243         I40E_NETDEV_STAT(tx_packets),
244         I40E_NETDEV_STAT(rx_bytes),
245         I40E_NETDEV_STAT(tx_bytes),
246         I40E_NETDEV_STAT(rx_errors),
247         I40E_NETDEV_STAT(tx_errors),
248         I40E_NETDEV_STAT(rx_dropped),
249         I40E_NETDEV_STAT(rx_missed_errors),
250         I40E_NETDEV_STAT(tx_dropped),
251         I40E_NETDEV_STAT(collisions),
252         I40E_NETDEV_STAT(rx_length_errors),
253         I40E_NETDEV_STAT(rx_crc_errors),
254 };
255
256 static const struct i40e_stats i40e_gstrings_veb_stats[] = {
257         I40E_VEB_STAT("veb.rx_bytes", stats.rx_bytes),
258         I40E_VEB_STAT("veb.tx_bytes", stats.tx_bytes),
259         I40E_VEB_STAT("veb.rx_unicast", stats.rx_unicast),
260         I40E_VEB_STAT("veb.tx_unicast", stats.tx_unicast),
261         I40E_VEB_STAT("veb.rx_multicast", stats.rx_multicast),
262         I40E_VEB_STAT("veb.tx_multicast", stats.tx_multicast),
263         I40E_VEB_STAT("veb.rx_broadcast", stats.rx_broadcast),
264         I40E_VEB_STAT("veb.tx_broadcast", stats.tx_broadcast),
265         I40E_VEB_STAT("veb.rx_discards", stats.rx_discards),
266         I40E_VEB_STAT("veb.tx_discards", stats.tx_discards),
267         I40E_VEB_STAT("veb.tx_errors", stats.tx_errors),
268         I40E_VEB_STAT("veb.rx_unknown_protocol", stats.rx_unknown_protocol),
269 };
270
271 struct i40e_cp_veb_tc_stats {
272         u64 tc_rx_packets;
273         u64 tc_rx_bytes;
274         u64 tc_tx_packets;
275         u64 tc_tx_bytes;
276 };
277
278 static const struct i40e_stats i40e_gstrings_veb_tc_stats[] = {
279         I40E_VEB_TC_STAT("veb.tc_%u_tx_packets", tc_tx_packets),
280         I40E_VEB_TC_STAT("veb.tc_%u_tx_bytes", tc_tx_bytes),
281         I40E_VEB_TC_STAT("veb.tc_%u_rx_packets", tc_rx_packets),
282         I40E_VEB_TC_STAT("veb.tc_%u_rx_bytes", tc_rx_bytes),
283 };
284
285 static const struct i40e_stats i40e_gstrings_misc_stats[] = {
286         I40E_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
287         I40E_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
288         I40E_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
289         I40E_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
290         I40E_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
291         I40E_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
292         I40E_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
293         I40E_VSI_STAT("tx_linearize", tx_linearize),
294         I40E_VSI_STAT("tx_force_wb", tx_force_wb),
295         I40E_VSI_STAT("tx_busy", tx_busy),
296         I40E_VSI_STAT("tx_stopped", tx_stopped),
297         I40E_VSI_STAT("rx_alloc_fail", rx_buf_failed),
298         I40E_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
299         I40E_VSI_STAT("rx_cache_reuse", rx_page_reuse),
300         I40E_VSI_STAT("rx_cache_alloc", rx_page_alloc),
301         I40E_VSI_STAT("rx_cache_waive", rx_page_waive),
302         I40E_VSI_STAT("rx_cache_busy", rx_page_busy),
303         I40E_VSI_STAT("tx_restart", tx_restart),
304 };
305
306 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
307  * but they are separate.  This device supports Virtualization, and
308  * as such might have several netdevs supporting VMDq and FCoE going
309  * through a single port.  The NETDEV_STATs are for individual netdevs
310  * seen at the top of the stack, and the PF_STATs are for the physical
311  * function at the bottom of the stack hosting those netdevs.
312  *
313  * The PF_STATs are appended to the netdev stats only when ethtool -S
314  * is queried on the base PF netdev, not on the VMDq or FCoE netdev.
315  */
316 static const struct i40e_stats i40e_gstrings_stats[] = {
317         I40E_PF_STAT("port.rx_bytes", stats.eth.rx_bytes),
318         I40E_PF_STAT("port.tx_bytes", stats.eth.tx_bytes),
319         I40E_PF_STAT("port.rx_unicast", stats.eth.rx_unicast),
320         I40E_PF_STAT("port.tx_unicast", stats.eth.tx_unicast),
321         I40E_PF_STAT("port.rx_multicast", stats.eth.rx_multicast),
322         I40E_PF_STAT("port.tx_multicast", stats.eth.tx_multicast),
323         I40E_PF_STAT("port.rx_broadcast", stats.eth.rx_broadcast),
324         I40E_PF_STAT("port.tx_broadcast", stats.eth.tx_broadcast),
325         I40E_PF_STAT("port.tx_errors", stats.eth.tx_errors),
326         I40E_PF_STAT("port.rx_discards", stats.eth.rx_discards),
327         I40E_PF_STAT("port.tx_dropped_link_down", stats.tx_dropped_link_down),
328         I40E_PF_STAT("port.rx_crc_errors", stats.crc_errors),
329         I40E_PF_STAT("port.illegal_bytes", stats.illegal_bytes),
330         I40E_PF_STAT("port.mac_local_faults", stats.mac_local_faults),
331         I40E_PF_STAT("port.mac_remote_faults", stats.mac_remote_faults),
332         I40E_PF_STAT("port.tx_timeout", tx_timeout_count),
333         I40E_PF_STAT("port.rx_csum_bad", hw_csum_rx_error),
334         I40E_PF_STAT("port.rx_length_errors", stats.rx_length_errors),
335         I40E_PF_STAT("port.link_xon_rx", stats.link_xon_rx),
336         I40E_PF_STAT("port.link_xoff_rx", stats.link_xoff_rx),
337         I40E_PF_STAT("port.link_xon_tx", stats.link_xon_tx),
338         I40E_PF_STAT("port.link_xoff_tx", stats.link_xoff_tx),
339         I40E_PF_STAT("port.rx_size_64", stats.rx_size_64),
340         I40E_PF_STAT("port.rx_size_127", stats.rx_size_127),
341         I40E_PF_STAT("port.rx_size_255", stats.rx_size_255),
342         I40E_PF_STAT("port.rx_size_511", stats.rx_size_511),
343         I40E_PF_STAT("port.rx_size_1023", stats.rx_size_1023),
344         I40E_PF_STAT("port.rx_size_1522", stats.rx_size_1522),
345         I40E_PF_STAT("port.rx_size_big", stats.rx_size_big),
346         I40E_PF_STAT("port.tx_size_64", stats.tx_size_64),
347         I40E_PF_STAT("port.tx_size_127", stats.tx_size_127),
348         I40E_PF_STAT("port.tx_size_255", stats.tx_size_255),
349         I40E_PF_STAT("port.tx_size_511", stats.tx_size_511),
350         I40E_PF_STAT("port.tx_size_1023", stats.tx_size_1023),
351         I40E_PF_STAT("port.tx_size_1522", stats.tx_size_1522),
352         I40E_PF_STAT("port.tx_size_big", stats.tx_size_big),
353         I40E_PF_STAT("port.rx_undersize", stats.rx_undersize),
354         I40E_PF_STAT("port.rx_fragments", stats.rx_fragments),
355         I40E_PF_STAT("port.rx_oversize", stats.rx_oversize),
356         I40E_PF_STAT("port.rx_jabber", stats.rx_jabber),
357         I40E_PF_STAT("port.VF_admin_queue_requests", vf_aq_requests),
358         I40E_PF_STAT("port.arq_overflows", arq_overflows),
359         I40E_PF_STAT("port.tx_hwtstamp_timeouts", tx_hwtstamp_timeouts),
360         I40E_PF_STAT("port.rx_hwtstamp_cleared", rx_hwtstamp_cleared),
361         I40E_PF_STAT("port.tx_hwtstamp_skipped", tx_hwtstamp_skipped),
362         I40E_PF_STAT("port.fdir_flush_cnt", fd_flush_cnt),
363         I40E_PF_STAT("port.fdir_atr_match", stats.fd_atr_match),
364         I40E_PF_STAT("port.fdir_atr_tunnel_match", stats.fd_atr_tunnel_match),
365         I40E_PF_STAT("port.fdir_atr_status", stats.fd_atr_status),
366         I40E_PF_STAT("port.fdir_sb_match", stats.fd_sb_match),
367         I40E_PF_STAT("port.fdir_sb_status", stats.fd_sb_status),
368
369         /* LPI stats */
370         I40E_PF_STAT("port.tx_lpi_status", stats.tx_lpi_status),
371         I40E_PF_STAT("port.rx_lpi_status", stats.rx_lpi_status),
372         I40E_PF_STAT("port.tx_lpi_count", stats.tx_lpi_count),
373         I40E_PF_STAT("port.rx_lpi_count", stats.rx_lpi_count),
374 };
375
376 struct i40e_pfc_stats {
377         u64 priority_xon_rx;
378         u64 priority_xoff_rx;
379         u64 priority_xon_tx;
380         u64 priority_xoff_tx;
381         u64 priority_xon_2_xoff;
382 };
383
384 static const struct i40e_stats i40e_gstrings_pfc_stats[] = {
385         I40E_PFC_STAT("port.tx_priority_%u_xon_tx", priority_xon_tx),
386         I40E_PFC_STAT("port.tx_priority_%u_xoff_tx", priority_xoff_tx),
387         I40E_PFC_STAT("port.rx_priority_%u_xon_rx", priority_xon_rx),
388         I40E_PFC_STAT("port.rx_priority_%u_xoff_rx", priority_xoff_rx),
389         I40E_PFC_STAT("port.rx_priority_%u_xon_2_xoff", priority_xon_2_xoff),
390 };
391
392 #define I40E_NETDEV_STATS_LEN   ARRAY_SIZE(i40e_gstrings_net_stats)
393
394 #define I40E_MISC_STATS_LEN     ARRAY_SIZE(i40e_gstrings_misc_stats)
395
396 #define I40E_VSI_STATS_LEN      (I40E_NETDEV_STATS_LEN + I40E_MISC_STATS_LEN)
397
398 #define I40E_PFC_STATS_LEN      (ARRAY_SIZE(i40e_gstrings_pfc_stats) * \
399                                  I40E_MAX_USER_PRIORITY)
400
401 #define I40E_VEB_STATS_LEN      (ARRAY_SIZE(i40e_gstrings_veb_stats) + \
402                                  (ARRAY_SIZE(i40e_gstrings_veb_tc_stats) * \
403                                   I40E_MAX_TRAFFIC_CLASS))
404
405 #define I40E_GLOBAL_STATS_LEN   ARRAY_SIZE(i40e_gstrings_stats)
406
407 #define I40E_PF_STATS_LEN       (I40E_GLOBAL_STATS_LEN + \
408                                  I40E_PFC_STATS_LEN + \
409                                  I40E_VEB_STATS_LEN + \
410                                  I40E_VSI_STATS_LEN)
411
412 /* Length of stats for a single queue */
413 #define I40E_QUEUE_STATS_LEN    ARRAY_SIZE(i40e_gstrings_queue_stats)
414
415 enum i40e_ethtool_test_id {
416         I40E_ETH_TEST_REG = 0,
417         I40E_ETH_TEST_EEPROM,
418         I40E_ETH_TEST_INTR,
419         I40E_ETH_TEST_LINK,
420 };
421
422 static const char i40e_gstrings_test[][ETH_GSTRING_LEN] = {
423         "Register test  (offline)",
424         "Eeprom test    (offline)",
425         "Interrupt test (offline)",
426         "Link test   (on/offline)"
427 };
428
429 #define I40E_TEST_LEN (sizeof(i40e_gstrings_test) / ETH_GSTRING_LEN)
430
431 struct i40e_priv_flags {
432         char flag_string[ETH_GSTRING_LEN];
433         u8 bitno;
434         bool read_only;
435 };
436
437 #define I40E_PRIV_FLAG(_name, _bitno, _read_only) { \
438         .flag_string = _name, \
439         .bitno = _bitno, \
440         .read_only = _read_only, \
441 }
442
443 static const struct i40e_priv_flags i40e_gstrings_priv_flags[] = {
444         /* NOTE: MFP setting cannot be changed */
445         I40E_PRIV_FLAG("MFP", I40E_FLAG_MFP_ENA, 1),
446         I40E_PRIV_FLAG("total-port-shutdown",
447                        I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, 1),
448         I40E_PRIV_FLAG("LinkPolling", I40E_FLAG_LINK_POLLING_ENA, 0),
449         I40E_PRIV_FLAG("flow-director-atr", I40E_FLAG_FD_ATR_ENA, 0),
450         I40E_PRIV_FLAG("veb-stats", I40E_FLAG_VEB_STATS_ENA, 0),
451         I40E_PRIV_FLAG("hw-atr-eviction", I40E_FLAG_HW_ATR_EVICT_ENA, 0),
452         I40E_PRIV_FLAG("link-down-on-close",
453                        I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, 0),
454         I40E_PRIV_FLAG("legacy-rx", I40E_FLAG_LEGACY_RX_ENA, 0),
455         I40E_PRIV_FLAG("disable-source-pruning",
456                        I40E_FLAG_SOURCE_PRUNING_DIS, 0),
457         I40E_PRIV_FLAG("disable-fw-lldp", I40E_FLAG_FW_LLDP_DIS, 0),
458         I40E_PRIV_FLAG("rs-fec", I40E_FLAG_RS_FEC, 0),
459         I40E_PRIV_FLAG("base-r-fec", I40E_FLAG_BASE_R_FEC, 0),
460         I40E_PRIV_FLAG("vf-vlan-pruning",
461                        I40E_FLAG_VF_VLAN_PRUNING_ENA, 0),
462 };
463
464 #define I40E_PRIV_FLAGS_STR_LEN ARRAY_SIZE(i40e_gstrings_priv_flags)
465
466 /* Private flags with a global effect, restricted to PF 0 */
467 static const struct i40e_priv_flags i40e_gl_gstrings_priv_flags[] = {
468         I40E_PRIV_FLAG("vf-true-promisc-support",
469                        I40E_FLAG_TRUE_PROMISC_ENA, 0),
470 };
471
472 #define I40E_GL_PRIV_FLAGS_STR_LEN ARRAY_SIZE(i40e_gl_gstrings_priv_flags)
473
474 /**
475  * i40e_partition_setting_complaint - generic complaint for MFP restriction
476  * @pf: the PF struct
477  **/
478 static void i40e_partition_setting_complaint(struct i40e_pf *pf)
479 {
480         dev_info(&pf->pdev->dev,
481                  "The link settings are allowed to be changed only from the first partition of a given port. Please switch to the first partition in order to change the setting.\n");
482 }
483
484 /**
485  * i40e_phy_type_to_ethtool - convert the phy_types to ethtool link modes
486  * @pf: PF struct with phy_types
487  * @ks: ethtool link ksettings struct to fill out
488  *
489  **/
490 static void i40e_phy_type_to_ethtool(struct i40e_pf *pf,
491                                      struct ethtool_link_ksettings *ks)
492 {
493         struct i40e_link_status *hw_link_info = &pf->hw.phy.link_info;
494         u64 phy_types = pf->hw.phy.phy_types;
495
496         ethtool_link_ksettings_zero_link_mode(ks, supported);
497         ethtool_link_ksettings_zero_link_mode(ks, advertising);
498
499         if (phy_types & I40E_CAP_PHY_TYPE_SGMII) {
500                 ethtool_link_ksettings_add_link_mode(ks, supported,
501                                                      1000baseT_Full);
502                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
503                         ethtool_link_ksettings_add_link_mode(ks, advertising,
504                                                              1000baseT_Full);
505                 if (test_bit(I40E_HW_CAP_100M_SGMII, pf->hw.caps)) {
506                         ethtool_link_ksettings_add_link_mode(ks, supported,
507                                                              100baseT_Full);
508                         ethtool_link_ksettings_add_link_mode(ks, advertising,
509                                                              100baseT_Full);
510                 }
511         }
512         if (phy_types & I40E_CAP_PHY_TYPE_XAUI ||
513             phy_types & I40E_CAP_PHY_TYPE_XFI ||
514             phy_types & I40E_CAP_PHY_TYPE_SFI ||
515             phy_types & I40E_CAP_PHY_TYPE_10GBASE_SFPP_CU ||
516             phy_types & I40E_CAP_PHY_TYPE_10GBASE_AOC) {
517                 ethtool_link_ksettings_add_link_mode(ks, supported,
518                                                      10000baseT_Full);
519                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
520                         ethtool_link_ksettings_add_link_mode(ks, advertising,
521                                                              10000baseT_Full);
522         }
523         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_T) {
524                 ethtool_link_ksettings_add_link_mode(ks, supported,
525                                                      10000baseT_Full);
526                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
527                         ethtool_link_ksettings_add_link_mode(ks, advertising,
528                                                              10000baseT_Full);
529         }
530         if (phy_types & I40E_CAP_PHY_TYPE_2_5GBASE_T) {
531                 ethtool_link_ksettings_add_link_mode(ks, supported,
532                                                      2500baseT_Full);
533                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_2_5GB)
534                         ethtool_link_ksettings_add_link_mode(ks, advertising,
535                                                              2500baseT_Full);
536         }
537         if (phy_types & I40E_CAP_PHY_TYPE_5GBASE_T) {
538                 ethtool_link_ksettings_add_link_mode(ks, supported,
539                                                      5000baseT_Full);
540                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_5GB)
541                         ethtool_link_ksettings_add_link_mode(ks, advertising,
542                                                              5000baseT_Full);
543         }
544         if (phy_types & I40E_CAP_PHY_TYPE_XLAUI ||
545             phy_types & I40E_CAP_PHY_TYPE_XLPPI ||
546             phy_types & I40E_CAP_PHY_TYPE_40GBASE_AOC)
547                 ethtool_link_ksettings_add_link_mode(ks, supported,
548                                                      40000baseCR4_Full);
549         if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4_CU ||
550             phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4) {
551                 ethtool_link_ksettings_add_link_mode(ks, supported,
552                                                      40000baseCR4_Full);
553                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_40GB)
554                         ethtool_link_ksettings_add_link_mode(ks, advertising,
555                                                              40000baseCR4_Full);
556         }
557         if (phy_types & I40E_CAP_PHY_TYPE_100BASE_TX) {
558                 ethtool_link_ksettings_add_link_mode(ks, supported,
559                                                      100baseT_Full);
560                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_100MB)
561                         ethtool_link_ksettings_add_link_mode(ks, advertising,
562                                                              100baseT_Full);
563         }
564         if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_T) {
565                 ethtool_link_ksettings_add_link_mode(ks, supported,
566                                                      1000baseT_Full);
567                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
568                         ethtool_link_ksettings_add_link_mode(ks, advertising,
569                                                              1000baseT_Full);
570         }
571         if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_SR4) {
572                 ethtool_link_ksettings_add_link_mode(ks, supported,
573                                                      40000baseSR4_Full);
574                 ethtool_link_ksettings_add_link_mode(ks, advertising,
575                                                      40000baseSR4_Full);
576         }
577         if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_LR4) {
578                 ethtool_link_ksettings_add_link_mode(ks, supported,
579                                                      40000baseLR4_Full);
580                 ethtool_link_ksettings_add_link_mode(ks, advertising,
581                                                      40000baseLR4_Full);
582         }
583         if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_KR4) {
584                 ethtool_link_ksettings_add_link_mode(ks, supported,
585                                                      40000baseKR4_Full);
586                 ethtool_link_ksettings_add_link_mode(ks, advertising,
587                                                      40000baseKR4_Full);
588         }
589         if (phy_types & I40E_CAP_PHY_TYPE_20GBASE_KR2) {
590                 ethtool_link_ksettings_add_link_mode(ks, supported,
591                                                      20000baseKR2_Full);
592                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_20GB)
593                         ethtool_link_ksettings_add_link_mode(ks, advertising,
594                                                              20000baseKR2_Full);
595         }
596         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_KX4) {
597                 ethtool_link_ksettings_add_link_mode(ks, supported,
598                                                      10000baseKX4_Full);
599                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
600                         ethtool_link_ksettings_add_link_mode(ks, advertising,
601                                                              10000baseKX4_Full);
602         }
603         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_KR &&
604             !test_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps)) {
605                 ethtool_link_ksettings_add_link_mode(ks, supported,
606                                                      10000baseKR_Full);
607                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
608                         ethtool_link_ksettings_add_link_mode(ks, advertising,
609                                                              10000baseKR_Full);
610         }
611         if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_KX &&
612             !test_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps)) {
613                 ethtool_link_ksettings_add_link_mode(ks, supported,
614                                                      1000baseKX_Full);
615                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
616                         ethtool_link_ksettings_add_link_mode(ks, advertising,
617                                                              1000baseKX_Full);
618         }
619         /* need to add 25G PHY types */
620         if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_KR) {
621                 ethtool_link_ksettings_add_link_mode(ks, supported,
622                                                      25000baseKR_Full);
623                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
624                         ethtool_link_ksettings_add_link_mode(ks, advertising,
625                                                              25000baseKR_Full);
626         }
627         if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_CR) {
628                 ethtool_link_ksettings_add_link_mode(ks, supported,
629                                                      25000baseCR_Full);
630                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
631                         ethtool_link_ksettings_add_link_mode(ks, advertising,
632                                                              25000baseCR_Full);
633         }
634         if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_SR ||
635             phy_types & I40E_CAP_PHY_TYPE_25GBASE_LR) {
636                 ethtool_link_ksettings_add_link_mode(ks, supported,
637                                                      25000baseSR_Full);
638                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
639                         ethtool_link_ksettings_add_link_mode(ks, advertising,
640                                                              25000baseSR_Full);
641         }
642         if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_AOC ||
643             phy_types & I40E_CAP_PHY_TYPE_25GBASE_ACC) {
644                 ethtool_link_ksettings_add_link_mode(ks, supported,
645                                                      25000baseCR_Full);
646                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
647                         ethtool_link_ksettings_add_link_mode(ks, advertising,
648                                                              25000baseCR_Full);
649         }
650         if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_KR ||
651             phy_types & I40E_CAP_PHY_TYPE_25GBASE_CR ||
652             phy_types & I40E_CAP_PHY_TYPE_25GBASE_SR ||
653             phy_types & I40E_CAP_PHY_TYPE_25GBASE_LR ||
654             phy_types & I40E_CAP_PHY_TYPE_25GBASE_AOC ||
655             phy_types & I40E_CAP_PHY_TYPE_25GBASE_ACC) {
656                 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_NONE);
657                 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS);
658                 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER);
659                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB) {
660                         ethtool_link_ksettings_add_link_mode(ks, advertising,
661                                                              FEC_NONE);
662                         ethtool_link_ksettings_add_link_mode(ks, advertising,
663                                                              FEC_RS);
664                         ethtool_link_ksettings_add_link_mode(ks, advertising,
665                                                              FEC_BASER);
666                 }
667         }
668         /* need to add new 10G PHY types */
669         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1 ||
670             phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1_CU) {
671                 ethtool_link_ksettings_add_link_mode(ks, supported,
672                                                      10000baseCR_Full);
673                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
674                         ethtool_link_ksettings_add_link_mode(ks, advertising,
675                                                              10000baseCR_Full);
676         }
677         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_SR) {
678                 ethtool_link_ksettings_add_link_mode(ks, supported,
679                                                      10000baseSR_Full);
680                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
681                         ethtool_link_ksettings_add_link_mode(ks, advertising,
682                                                              10000baseSR_Full);
683         }
684         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_LR) {
685                 ethtool_link_ksettings_add_link_mode(ks, supported,
686                                                      10000baseLR_Full);
687                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
688                         ethtool_link_ksettings_add_link_mode(ks, advertising,
689                                                              10000baseLR_Full);
690         }
691         if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_SX ||
692             phy_types & I40E_CAP_PHY_TYPE_1000BASE_LX ||
693             phy_types & I40E_CAP_PHY_TYPE_1000BASE_T_OPTICAL) {
694                 ethtool_link_ksettings_add_link_mode(ks, supported,
695                                                      1000baseX_Full);
696                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
697                         ethtool_link_ksettings_add_link_mode(ks, advertising,
698                                                              1000baseX_Full);
699         }
700         /* Autoneg PHY types */
701         if (phy_types & I40E_CAP_PHY_TYPE_SGMII ||
702             phy_types & I40E_CAP_PHY_TYPE_40GBASE_KR4 ||
703             phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4_CU ||
704             phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4 ||
705             phy_types & I40E_CAP_PHY_TYPE_25GBASE_SR ||
706             phy_types & I40E_CAP_PHY_TYPE_25GBASE_LR ||
707             phy_types & I40E_CAP_PHY_TYPE_25GBASE_KR ||
708             phy_types & I40E_CAP_PHY_TYPE_25GBASE_CR ||
709             phy_types & I40E_CAP_PHY_TYPE_20GBASE_KR2 ||
710             phy_types & I40E_CAP_PHY_TYPE_10GBASE_SR ||
711             phy_types & I40E_CAP_PHY_TYPE_10GBASE_LR ||
712             phy_types & I40E_CAP_PHY_TYPE_10GBASE_KX4 ||
713             phy_types & I40E_CAP_PHY_TYPE_10GBASE_KR ||
714             phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1_CU ||
715             phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1 ||
716             phy_types & I40E_CAP_PHY_TYPE_10GBASE_T ||
717             phy_types & I40E_CAP_PHY_TYPE_5GBASE_T ||
718             phy_types & I40E_CAP_PHY_TYPE_2_5GBASE_T ||
719             phy_types & I40E_CAP_PHY_TYPE_1000BASE_T_OPTICAL ||
720             phy_types & I40E_CAP_PHY_TYPE_1000BASE_T ||
721             phy_types & I40E_CAP_PHY_TYPE_1000BASE_SX ||
722             phy_types & I40E_CAP_PHY_TYPE_1000BASE_LX ||
723             phy_types & I40E_CAP_PHY_TYPE_1000BASE_KX ||
724             phy_types & I40E_CAP_PHY_TYPE_100BASE_TX) {
725                 ethtool_link_ksettings_add_link_mode(ks, supported,
726                                                      Autoneg);
727                 ethtool_link_ksettings_add_link_mode(ks, advertising,
728                                                      Autoneg);
729         }
730 }
731
732 /**
733  * i40e_get_settings_link_up_fec - Get the FEC mode encoding from mask
734  * @req_fec_info: mask request FEC info
735  * @ks: ethtool ksettings to fill in
736  **/
737 static void i40e_get_settings_link_up_fec(u8 req_fec_info,
738                                           struct ethtool_link_ksettings *ks)
739 {
740         ethtool_link_ksettings_add_link_mode(ks, supported, FEC_NONE);
741         ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS);
742         ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER);
743
744         if ((I40E_AQ_SET_FEC_REQUEST_RS & req_fec_info) &&
745             (I40E_AQ_SET_FEC_REQUEST_KR & req_fec_info)) {
746                 ethtool_link_ksettings_add_link_mode(ks, advertising,
747                                                      FEC_NONE);
748                 ethtool_link_ksettings_add_link_mode(ks, advertising,
749                                                      FEC_BASER);
750                 ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS);
751         } else if (I40E_AQ_SET_FEC_REQUEST_RS & req_fec_info) {
752                 ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS);
753         } else if (I40E_AQ_SET_FEC_REQUEST_KR & req_fec_info) {
754                 ethtool_link_ksettings_add_link_mode(ks, advertising,
755                                                      FEC_BASER);
756         } else {
757                 ethtool_link_ksettings_add_link_mode(ks, advertising,
758                                                      FEC_NONE);
759         }
760 }
761
762 /**
763  * i40e_get_settings_link_up - Get the Link settings for when link is up
764  * @hw: hw structure
765  * @ks: ethtool ksettings to fill in
766  * @netdev: network interface device structure
767  * @pf: pointer to physical function struct
768  **/
769 static void i40e_get_settings_link_up(struct i40e_hw *hw,
770                                       struct ethtool_link_ksettings *ks,
771                                       struct net_device *netdev,
772                                       struct i40e_pf *pf)
773 {
774         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
775         struct ethtool_link_ksettings cap_ksettings;
776         u32 link_speed = hw_link_info->link_speed;
777
778         /* Initialize supported and advertised settings based on phy settings */
779         switch (hw_link_info->phy_type) {
780         case I40E_PHY_TYPE_40GBASE_CR4:
781         case I40E_PHY_TYPE_40GBASE_CR4_CU:
782                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
783                 ethtool_link_ksettings_add_link_mode(ks, supported,
784                                                      40000baseCR4_Full);
785                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
786                 ethtool_link_ksettings_add_link_mode(ks, advertising,
787                                                      40000baseCR4_Full);
788                 break;
789         case I40E_PHY_TYPE_XLAUI:
790         case I40E_PHY_TYPE_XLPPI:
791         case I40E_PHY_TYPE_40GBASE_AOC:
792                 ethtool_link_ksettings_add_link_mode(ks, supported,
793                                                      40000baseCR4_Full);
794                 ethtool_link_ksettings_add_link_mode(ks, advertising,
795                                                      40000baseCR4_Full);
796                 break;
797         case I40E_PHY_TYPE_40GBASE_SR4:
798                 ethtool_link_ksettings_add_link_mode(ks, supported,
799                                                      40000baseSR4_Full);
800                 ethtool_link_ksettings_add_link_mode(ks, advertising,
801                                                      40000baseSR4_Full);
802                 break;
803         case I40E_PHY_TYPE_40GBASE_LR4:
804                 ethtool_link_ksettings_add_link_mode(ks, supported,
805                                                      40000baseLR4_Full);
806                 ethtool_link_ksettings_add_link_mode(ks, advertising,
807                                                      40000baseLR4_Full);
808                 break;
809         case I40E_PHY_TYPE_25GBASE_SR:
810         case I40E_PHY_TYPE_25GBASE_LR:
811         case I40E_PHY_TYPE_10GBASE_SR:
812         case I40E_PHY_TYPE_10GBASE_LR:
813         case I40E_PHY_TYPE_1000BASE_SX:
814         case I40E_PHY_TYPE_1000BASE_LX:
815                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
816                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
817                 ethtool_link_ksettings_add_link_mode(ks, supported,
818                                                      25000baseSR_Full);
819                 ethtool_link_ksettings_add_link_mode(ks, advertising,
820                                                      25000baseSR_Full);
821                 i40e_get_settings_link_up_fec(hw_link_info->req_fec_info, ks);
822                 ethtool_link_ksettings_add_link_mode(ks, supported,
823                                                      10000baseSR_Full);
824                 ethtool_link_ksettings_add_link_mode(ks, advertising,
825                                                      10000baseSR_Full);
826                 ethtool_link_ksettings_add_link_mode(ks, supported,
827                                                      10000baseLR_Full);
828                 ethtool_link_ksettings_add_link_mode(ks, advertising,
829                                                      10000baseLR_Full);
830                 ethtool_link_ksettings_add_link_mode(ks, supported,
831                                                      1000baseX_Full);
832                 ethtool_link_ksettings_add_link_mode(ks, advertising,
833                                                      1000baseX_Full);
834                 ethtool_link_ksettings_add_link_mode(ks, supported,
835                                                      10000baseT_Full);
836                 if (hw_link_info->module_type[2] &
837                     I40E_MODULE_TYPE_1000BASE_SX ||
838                     hw_link_info->module_type[2] &
839                     I40E_MODULE_TYPE_1000BASE_LX) {
840                         ethtool_link_ksettings_add_link_mode(ks, supported,
841                                                              1000baseT_Full);
842                         if (hw_link_info->requested_speeds &
843                             I40E_LINK_SPEED_1GB)
844                                 ethtool_link_ksettings_add_link_mode(
845                                      ks, advertising, 1000baseT_Full);
846                 }
847                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
848                         ethtool_link_ksettings_add_link_mode(ks, advertising,
849                                                              10000baseT_Full);
850                 break;
851         case I40E_PHY_TYPE_10GBASE_T:
852         case I40E_PHY_TYPE_5GBASE_T_LINK_STATUS:
853         case I40E_PHY_TYPE_2_5GBASE_T_LINK_STATUS:
854         case I40E_PHY_TYPE_1000BASE_T:
855         case I40E_PHY_TYPE_100BASE_TX:
856                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
857                 ethtool_link_ksettings_add_link_mode(ks, supported,
858                                                      10000baseT_Full);
859                 ethtool_link_ksettings_add_link_mode(ks, supported,
860                                                      5000baseT_Full);
861                 ethtool_link_ksettings_add_link_mode(ks, supported,
862                                                      2500baseT_Full);
863                 ethtool_link_ksettings_add_link_mode(ks, supported,
864                                                      1000baseT_Full);
865                 ethtool_link_ksettings_add_link_mode(ks, supported,
866                                                      100baseT_Full);
867                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
868                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
869                         ethtool_link_ksettings_add_link_mode(ks, advertising,
870                                                              10000baseT_Full);
871                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_5GB)
872                         ethtool_link_ksettings_add_link_mode(ks, advertising,
873                                                              5000baseT_Full);
874                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_2_5GB)
875                         ethtool_link_ksettings_add_link_mode(ks, advertising,
876                                                              2500baseT_Full);
877                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
878                         ethtool_link_ksettings_add_link_mode(ks, advertising,
879                                                              1000baseT_Full);
880                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_100MB)
881                         ethtool_link_ksettings_add_link_mode(ks, advertising,
882                                                              100baseT_Full);
883                 break;
884         case I40E_PHY_TYPE_1000BASE_T_OPTICAL:
885                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
886                 ethtool_link_ksettings_add_link_mode(ks, supported,
887                                                      1000baseT_Full);
888                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
889                 ethtool_link_ksettings_add_link_mode(ks, advertising,
890                                                      1000baseT_Full);
891                 break;
892         case I40E_PHY_TYPE_10GBASE_CR1_CU:
893         case I40E_PHY_TYPE_10GBASE_CR1:
894                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
895                 ethtool_link_ksettings_add_link_mode(ks, supported,
896                                                      10000baseT_Full);
897                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
898                 ethtool_link_ksettings_add_link_mode(ks, advertising,
899                                                      10000baseT_Full);
900                 break;
901         case I40E_PHY_TYPE_XAUI:
902         case I40E_PHY_TYPE_XFI:
903         case I40E_PHY_TYPE_SFI:
904         case I40E_PHY_TYPE_10GBASE_SFPP_CU:
905         case I40E_PHY_TYPE_10GBASE_AOC:
906                 ethtool_link_ksettings_add_link_mode(ks, supported,
907                                                      10000baseT_Full);
908                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
909                         ethtool_link_ksettings_add_link_mode(ks, advertising,
910                                                              10000baseT_Full);
911                 i40e_get_settings_link_up_fec(hw_link_info->req_fec_info, ks);
912                 break;
913         case I40E_PHY_TYPE_SGMII:
914                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
915                 ethtool_link_ksettings_add_link_mode(ks, supported,
916                                                      1000baseT_Full);
917                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
918                         ethtool_link_ksettings_add_link_mode(ks, advertising,
919                                                              1000baseT_Full);
920                 if (test_bit(I40E_HW_CAP_100M_SGMII, pf->hw.caps)) {
921                         ethtool_link_ksettings_add_link_mode(ks, supported,
922                                                              100baseT_Full);
923                         if (hw_link_info->requested_speeds &
924                             I40E_LINK_SPEED_100MB)
925                                 ethtool_link_ksettings_add_link_mode(
926                                       ks, advertising, 100baseT_Full);
927                 }
928                 break;
929         case I40E_PHY_TYPE_40GBASE_KR4:
930         case I40E_PHY_TYPE_25GBASE_KR:
931         case I40E_PHY_TYPE_20GBASE_KR2:
932         case I40E_PHY_TYPE_10GBASE_KR:
933         case I40E_PHY_TYPE_10GBASE_KX4:
934         case I40E_PHY_TYPE_1000BASE_KX:
935                 ethtool_link_ksettings_add_link_mode(ks, supported,
936                                                      40000baseKR4_Full);
937                 ethtool_link_ksettings_add_link_mode(ks, supported,
938                                                      25000baseKR_Full);
939                 ethtool_link_ksettings_add_link_mode(ks, supported,
940                                                      20000baseKR2_Full);
941                 ethtool_link_ksettings_add_link_mode(ks, supported,
942                                                      10000baseKR_Full);
943                 ethtool_link_ksettings_add_link_mode(ks, supported,
944                                                      10000baseKX4_Full);
945                 ethtool_link_ksettings_add_link_mode(ks, supported,
946                                                      1000baseKX_Full);
947                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
948                 ethtool_link_ksettings_add_link_mode(ks, advertising,
949                                                      40000baseKR4_Full);
950                 ethtool_link_ksettings_add_link_mode(ks, advertising,
951                                                      25000baseKR_Full);
952                 i40e_get_settings_link_up_fec(hw_link_info->req_fec_info, ks);
953                 ethtool_link_ksettings_add_link_mode(ks, advertising,
954                                                      20000baseKR2_Full);
955                 ethtool_link_ksettings_add_link_mode(ks, advertising,
956                                                      10000baseKR_Full);
957                 ethtool_link_ksettings_add_link_mode(ks, advertising,
958                                                      10000baseKX4_Full);
959                 ethtool_link_ksettings_add_link_mode(ks, advertising,
960                                                      1000baseKX_Full);
961                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
962                 break;
963         case I40E_PHY_TYPE_25GBASE_CR:
964                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
965                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
966                 ethtool_link_ksettings_add_link_mode(ks, supported,
967                                                      25000baseCR_Full);
968                 ethtool_link_ksettings_add_link_mode(ks, advertising,
969                                                      25000baseCR_Full);
970                 i40e_get_settings_link_up_fec(hw_link_info->req_fec_info, ks);
971
972                 break;
973         case I40E_PHY_TYPE_25GBASE_AOC:
974         case I40E_PHY_TYPE_25GBASE_ACC:
975                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
976                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
977                 ethtool_link_ksettings_add_link_mode(ks, supported,
978                                                      25000baseCR_Full);
979                 ethtool_link_ksettings_add_link_mode(ks, advertising,
980                                                      25000baseCR_Full);
981                 i40e_get_settings_link_up_fec(hw_link_info->req_fec_info, ks);
982
983                 ethtool_link_ksettings_add_link_mode(ks, supported,
984                                                      10000baseCR_Full);
985                 ethtool_link_ksettings_add_link_mode(ks, advertising,
986                                                      10000baseCR_Full);
987                 break;
988         default:
989                 /* if we got here and link is up something bad is afoot */
990                 netdev_info(netdev,
991                             "WARNING: Link is up but PHY type 0x%x is not recognized, or incorrect cable is in use\n",
992                             hw_link_info->phy_type);
993         }
994
995         /* Now that we've worked out everything that could be supported by the
996          * current PHY type, get what is supported by the NVM and intersect
997          * them to get what is truly supported
998          */
999         memset(&cap_ksettings, 0, sizeof(struct ethtool_link_ksettings));
1000         i40e_phy_type_to_ethtool(pf, &cap_ksettings);
1001         ethtool_intersect_link_masks(ks, &cap_ksettings);
1002
1003         /* Set speed and duplex */
1004         switch (link_speed) {
1005         case I40E_LINK_SPEED_40GB:
1006                 ks->base.speed = SPEED_40000;
1007                 break;
1008         case I40E_LINK_SPEED_25GB:
1009                 ks->base.speed = SPEED_25000;
1010                 break;
1011         case I40E_LINK_SPEED_20GB:
1012                 ks->base.speed = SPEED_20000;
1013                 break;
1014         case I40E_LINK_SPEED_10GB:
1015                 ks->base.speed = SPEED_10000;
1016                 break;
1017         case I40E_LINK_SPEED_5GB:
1018                 ks->base.speed = SPEED_5000;
1019                 break;
1020         case I40E_LINK_SPEED_2_5GB:
1021                 ks->base.speed = SPEED_2500;
1022                 break;
1023         case I40E_LINK_SPEED_1GB:
1024                 ks->base.speed = SPEED_1000;
1025                 break;
1026         case I40E_LINK_SPEED_100MB:
1027                 ks->base.speed = SPEED_100;
1028                 break;
1029         default:
1030                 ks->base.speed = SPEED_UNKNOWN;
1031                 break;
1032         }
1033         ks->base.duplex = DUPLEX_FULL;
1034 }
1035
1036 /**
1037  * i40e_get_settings_link_down - Get the Link settings for when link is down
1038  * @hw: hw structure
1039  * @ks: ethtool ksettings to fill in
1040  * @pf: pointer to physical function struct
1041  *
1042  * Reports link settings that can be determined when link is down
1043  **/
1044 static void i40e_get_settings_link_down(struct i40e_hw *hw,
1045                                         struct ethtool_link_ksettings *ks,
1046                                         struct i40e_pf *pf)
1047 {
1048         /* link is down and the driver needs to fall back on
1049          * supported phy types to figure out what info to display
1050          */
1051         i40e_phy_type_to_ethtool(pf, ks);
1052
1053         /* With no link speed and duplex are unknown */
1054         ks->base.speed = SPEED_UNKNOWN;
1055         ks->base.duplex = DUPLEX_UNKNOWN;
1056 }
1057
1058 /**
1059  * i40e_get_link_ksettings - Get Link Speed and Duplex settings
1060  * @netdev: network interface device structure
1061  * @ks: ethtool ksettings
1062  *
1063  * Reports speed/duplex settings based on media_type
1064  **/
1065 static int i40e_get_link_ksettings(struct net_device *netdev,
1066                                    struct ethtool_link_ksettings *ks)
1067 {
1068         struct i40e_netdev_priv *np = netdev_priv(netdev);
1069         struct i40e_pf *pf = np->vsi->back;
1070         struct i40e_hw *hw = &pf->hw;
1071         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
1072         bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
1073
1074         ethtool_link_ksettings_zero_link_mode(ks, supported);
1075         ethtool_link_ksettings_zero_link_mode(ks, advertising);
1076
1077         if (link_up)
1078                 i40e_get_settings_link_up(hw, ks, netdev, pf);
1079         else
1080                 i40e_get_settings_link_down(hw, ks, pf);
1081
1082         /* Now set the settings that don't rely on link being up/down */
1083         /* Set autoneg settings */
1084         ks->base.autoneg = ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
1085                             AUTONEG_ENABLE : AUTONEG_DISABLE);
1086
1087         /* Set media type settings */
1088         switch (hw->phy.media_type) {
1089         case I40E_MEDIA_TYPE_BACKPLANE:
1090                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
1091                 ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
1092                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
1093                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1094                                                      Backplane);
1095                 ks->base.port = PORT_NONE;
1096                 break;
1097         case I40E_MEDIA_TYPE_BASET:
1098                 ethtool_link_ksettings_add_link_mode(ks, supported, TP);
1099                 ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
1100                 ks->base.port = PORT_TP;
1101                 break;
1102         case I40E_MEDIA_TYPE_DA:
1103         case I40E_MEDIA_TYPE_CX4:
1104                 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
1105                 ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
1106                 ks->base.port = PORT_DA;
1107                 break;
1108         case I40E_MEDIA_TYPE_FIBER:
1109                 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
1110                 ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
1111                 ks->base.port = PORT_FIBRE;
1112                 break;
1113         case I40E_MEDIA_TYPE_UNKNOWN:
1114         default:
1115                 ks->base.port = PORT_OTHER;
1116                 break;
1117         }
1118
1119         /* Set flow control settings */
1120         ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
1121         ethtool_link_ksettings_add_link_mode(ks, supported, Asym_Pause);
1122
1123         switch (hw->fc.requested_mode) {
1124         case I40E_FC_FULL:
1125                 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
1126                 break;
1127         case I40E_FC_TX_PAUSE:
1128                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1129                                                      Asym_Pause);
1130                 break;
1131         case I40E_FC_RX_PAUSE:
1132                 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
1133                 ethtool_link_ksettings_add_link_mode(ks, advertising,
1134                                                      Asym_Pause);
1135                 break;
1136         default:
1137                 ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
1138                 ethtool_link_ksettings_del_link_mode(ks, advertising,
1139                                                      Asym_Pause);
1140                 break;
1141         }
1142
1143         return 0;
1144 }
1145
1146 #define I40E_LBIT_SIZE 8
1147 /**
1148  * i40e_speed_to_link_speed - Translate decimal speed to i40e_aq_link_speed
1149  * @speed: speed in decimal
1150  * @ks: ethtool ksettings
1151  *
1152  * Return i40e_aq_link_speed based on speed
1153  **/
1154 static enum i40e_aq_link_speed
1155 i40e_speed_to_link_speed(__u32 speed, const struct ethtool_link_ksettings *ks)
1156 {
1157         enum i40e_aq_link_speed link_speed = I40E_LINK_SPEED_UNKNOWN;
1158         bool speed_changed = false;
1159         int i, j;
1160
1161         static const struct {
1162                 __u32 speed;
1163                 enum i40e_aq_link_speed link_speed;
1164                 __u8 bit[I40E_LBIT_SIZE];
1165         } i40e_speed_lut[] = {
1166 #define I40E_LBIT(mode) ETHTOOL_LINK_MODE_ ## mode ##_Full_BIT
1167                 {SPEED_100, I40E_LINK_SPEED_100MB, {I40E_LBIT(100baseT)} },
1168                 {SPEED_1000, I40E_LINK_SPEED_1GB,
1169                  {I40E_LBIT(1000baseT), I40E_LBIT(1000baseX),
1170                   I40E_LBIT(1000baseKX)} },
1171                 {SPEED_10000, I40E_LINK_SPEED_10GB,
1172                  {I40E_LBIT(10000baseT), I40E_LBIT(10000baseKR),
1173                   I40E_LBIT(10000baseLR), I40E_LBIT(10000baseCR),
1174                   I40E_LBIT(10000baseSR), I40E_LBIT(10000baseKX4)} },
1175
1176                 {SPEED_25000, I40E_LINK_SPEED_25GB,
1177                  {I40E_LBIT(25000baseCR), I40E_LBIT(25000baseKR),
1178                   I40E_LBIT(25000baseSR)} },
1179                 {SPEED_40000, I40E_LINK_SPEED_40GB,
1180                  {I40E_LBIT(40000baseKR4), I40E_LBIT(40000baseCR4),
1181                   I40E_LBIT(40000baseSR4), I40E_LBIT(40000baseLR4)} },
1182                 {SPEED_20000, I40E_LINK_SPEED_20GB,
1183                  {I40E_LBIT(20000baseKR2)} },
1184                 {SPEED_2500, I40E_LINK_SPEED_2_5GB, {I40E_LBIT(2500baseT)} },
1185                 {SPEED_5000, I40E_LINK_SPEED_5GB, {I40E_LBIT(2500baseT)} }
1186 #undef I40E_LBIT
1187 };
1188
1189         for (i = 0; i < ARRAY_SIZE(i40e_speed_lut); i++) {
1190                 if (i40e_speed_lut[i].speed == speed) {
1191                         for (j = 0; j < I40E_LBIT_SIZE; j++) {
1192                                 if (test_bit(i40e_speed_lut[i].bit[j],
1193                                              ks->link_modes.supported)) {
1194                                         speed_changed = true;
1195                                         break;
1196                                 }
1197                                 if (!i40e_speed_lut[i].bit[j])
1198                                         break;
1199                         }
1200                         if (speed_changed) {
1201                                 link_speed = i40e_speed_lut[i].link_speed;
1202                                 break;
1203                         }
1204                 }
1205         }
1206         return link_speed;
1207 }
1208
1209 #undef I40E_LBIT_SIZE
1210
1211 /**
1212  * i40e_set_link_ksettings - Set Speed and Duplex
1213  * @netdev: network interface device structure
1214  * @ks: ethtool ksettings
1215  *
1216  * Set speed/duplex per media_types advertised/forced
1217  **/
1218 static int i40e_set_link_ksettings(struct net_device *netdev,
1219                                    const struct ethtool_link_ksettings *ks)
1220 {
1221         struct i40e_netdev_priv *np = netdev_priv(netdev);
1222         struct i40e_aq_get_phy_abilities_resp abilities;
1223         struct ethtool_link_ksettings safe_ks;
1224         struct ethtool_link_ksettings copy_ks;
1225         struct i40e_aq_set_phy_config config;
1226         struct i40e_pf *pf = np->vsi->back;
1227         enum i40e_aq_link_speed link_speed;
1228         struct i40e_vsi *vsi = np->vsi;
1229         struct i40e_hw *hw = &pf->hw;
1230         bool autoneg_changed = false;
1231         int timeout = 50;
1232         int status = 0;
1233         int err = 0;
1234         __u32 speed;
1235         u8 autoneg;
1236
1237         /* Changing port settings is not supported if this isn't the
1238          * port's controlling PF
1239          */
1240         if (hw->partition_id != 1) {
1241                 i40e_partition_setting_complaint(pf);
1242                 return -EOPNOTSUPP;
1243         }
1244         if (vsi->type != I40E_VSI_MAIN)
1245                 return -EOPNOTSUPP;
1246         if (hw->phy.media_type != I40E_MEDIA_TYPE_BASET &&
1247             hw->phy.media_type != I40E_MEDIA_TYPE_FIBER &&
1248             hw->phy.media_type != I40E_MEDIA_TYPE_BACKPLANE &&
1249             hw->phy.media_type != I40E_MEDIA_TYPE_DA &&
1250             hw->phy.link_info.link_info & I40E_AQ_LINK_UP)
1251                 return -EOPNOTSUPP;
1252         if (hw->device_id == I40E_DEV_ID_KX_B ||
1253             hw->device_id == I40E_DEV_ID_KX_C ||
1254             hw->device_id == I40E_DEV_ID_20G_KR2 ||
1255             hw->device_id == I40E_DEV_ID_20G_KR2_A ||
1256             hw->device_id == I40E_DEV_ID_25G_B ||
1257             hw->device_id == I40E_DEV_ID_KX_X722) {
1258                 netdev_info(netdev, "Changing settings is not supported on backplane.\n");
1259                 return -EOPNOTSUPP;
1260         }
1261
1262         /* copy the ksettings to copy_ks to avoid modifying the origin */
1263         memcpy(&copy_ks, ks, sizeof(struct ethtool_link_ksettings));
1264
1265         /* save autoneg out of ksettings */
1266         autoneg = copy_ks.base.autoneg;
1267         speed = copy_ks.base.speed;
1268
1269         /* get our own copy of the bits to check against */
1270         memset(&safe_ks, 0, sizeof(struct ethtool_link_ksettings));
1271         safe_ks.base.cmd = copy_ks.base.cmd;
1272         safe_ks.base.link_mode_masks_nwords =
1273                 copy_ks.base.link_mode_masks_nwords;
1274         i40e_get_link_ksettings(netdev, &safe_ks);
1275
1276         /* Get link modes supported by hardware and check against modes
1277          * requested by the user.  Return an error if unsupported mode was set.
1278          */
1279         if (!bitmap_subset(copy_ks.link_modes.advertising,
1280                            safe_ks.link_modes.supported,
1281                            __ETHTOOL_LINK_MODE_MASK_NBITS))
1282                 return -EINVAL;
1283
1284         /* set autoneg back to what it currently is */
1285         copy_ks.base.autoneg = safe_ks.base.autoneg;
1286         copy_ks.base.speed  = safe_ks.base.speed;
1287
1288         /* If copy_ks.base and safe_ks.base are not the same now, then they are
1289          * trying to set something that we do not support.
1290          */
1291         if (memcmp(&copy_ks.base, &safe_ks.base,
1292                    sizeof(struct ethtool_link_settings))) {
1293                 netdev_err(netdev, "Only speed and autoneg are supported.\n");
1294                 return -EOPNOTSUPP;
1295         }
1296
1297         while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
1298                 timeout--;
1299                 if (!timeout)
1300                         return -EBUSY;
1301                 usleep_range(1000, 2000);
1302         }
1303
1304         /* Get the current phy config */
1305         status = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
1306                                               NULL);
1307         if (status) {
1308                 err = -EAGAIN;
1309                 goto done;
1310         }
1311
1312         /* Copy abilities to config in case autoneg is not
1313          * set below
1314          */
1315         memset(&config, 0, sizeof(struct i40e_aq_set_phy_config));
1316         config.abilities = abilities.abilities;
1317
1318         /* Check autoneg */
1319         if (autoneg == AUTONEG_ENABLE) {
1320                 /* If autoneg was not already enabled */
1321                 if (!(hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED)) {
1322                         /* If autoneg is not supported, return error */
1323                         if (!ethtool_link_ksettings_test_link_mode(&safe_ks,
1324                                                                    supported,
1325                                                                    Autoneg)) {
1326                                 netdev_info(netdev, "Autoneg not supported on this phy\n");
1327                                 err = -EINVAL;
1328                                 goto done;
1329                         }
1330                         /* Autoneg is allowed to change */
1331                         config.abilities = abilities.abilities |
1332                                            I40E_AQ_PHY_ENABLE_AN;
1333                         autoneg_changed = true;
1334                 }
1335         } else {
1336                 /* If autoneg is currently enabled */
1337                 if (hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED) {
1338                         /* If autoneg is supported 10GBASE_T is the only PHY
1339                          * that can disable it, so otherwise return error
1340                          */
1341                         if (ethtool_link_ksettings_test_link_mode(&safe_ks,
1342                                                                   supported,
1343                                                                   Autoneg) &&
1344                             hw->phy.media_type != I40E_MEDIA_TYPE_BASET) {
1345                                 netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
1346                                 err = -EINVAL;
1347                                 goto done;
1348                         }
1349                         /* Autoneg is allowed to change */
1350                         config.abilities = abilities.abilities &
1351                                            ~I40E_AQ_PHY_ENABLE_AN;
1352                         autoneg_changed = true;
1353                 }
1354         }
1355
1356         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1357                                                   100baseT_Full))
1358                 config.link_speed |= I40E_LINK_SPEED_100MB;
1359         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1360                                                   1000baseT_Full) ||
1361             ethtool_link_ksettings_test_link_mode(ks, advertising,
1362                                                   1000baseX_Full) ||
1363             ethtool_link_ksettings_test_link_mode(ks, advertising,
1364                                                   1000baseKX_Full))
1365                 config.link_speed |= I40E_LINK_SPEED_1GB;
1366         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1367                                                   10000baseT_Full) ||
1368             ethtool_link_ksettings_test_link_mode(ks, advertising,
1369                                                   10000baseKX4_Full) ||
1370             ethtool_link_ksettings_test_link_mode(ks, advertising,
1371                                                   10000baseKR_Full) ||
1372             ethtool_link_ksettings_test_link_mode(ks, advertising,
1373                                                   10000baseCR_Full) ||
1374             ethtool_link_ksettings_test_link_mode(ks, advertising,
1375                                                   10000baseSR_Full) ||
1376             ethtool_link_ksettings_test_link_mode(ks, advertising,
1377                                                   10000baseLR_Full))
1378                 config.link_speed |= I40E_LINK_SPEED_10GB;
1379         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1380                                                   2500baseT_Full))
1381                 config.link_speed |= I40E_LINK_SPEED_2_5GB;
1382         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1383                                                   5000baseT_Full))
1384                 config.link_speed |= I40E_LINK_SPEED_5GB;
1385         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1386                                                   20000baseKR2_Full))
1387                 config.link_speed |= I40E_LINK_SPEED_20GB;
1388         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1389                                                   25000baseCR_Full) ||
1390             ethtool_link_ksettings_test_link_mode(ks, advertising,
1391                                                   25000baseKR_Full) ||
1392             ethtool_link_ksettings_test_link_mode(ks, advertising,
1393                                                   25000baseSR_Full))
1394                 config.link_speed |= I40E_LINK_SPEED_25GB;
1395         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1396                                                   40000baseKR4_Full) ||
1397             ethtool_link_ksettings_test_link_mode(ks, advertising,
1398                                                   40000baseCR4_Full) ||
1399             ethtool_link_ksettings_test_link_mode(ks, advertising,
1400                                                   40000baseSR4_Full) ||
1401             ethtool_link_ksettings_test_link_mode(ks, advertising,
1402                                                   40000baseLR4_Full))
1403                 config.link_speed |= I40E_LINK_SPEED_40GB;
1404
1405         /* Autonegotiation must be disabled to change speed */
1406         if ((speed != SPEED_UNKNOWN && safe_ks.base.speed != speed) &&
1407             (autoneg == AUTONEG_DISABLE ||
1408             (safe_ks.base.autoneg == AUTONEG_DISABLE && !autoneg_changed))) {
1409                 link_speed = i40e_speed_to_link_speed(speed, ks);
1410                 if (link_speed == I40E_LINK_SPEED_UNKNOWN) {
1411                         netdev_info(netdev, "Given speed is not supported\n");
1412                         err = -EOPNOTSUPP;
1413                         goto done;
1414                 } else {
1415                         config.link_speed = link_speed;
1416                 }
1417         } else {
1418                 if (safe_ks.base.speed != speed) {
1419                         netdev_info(netdev,
1420                                     "Unable to set speed, disable autoneg\n");
1421                         err = -EOPNOTSUPP;
1422                         goto done;
1423                 }
1424         }
1425
1426         /* If speed didn't get set, set it to what it currently is.
1427          * This is needed because if advertise is 0 (as it is when autoneg
1428          * is disabled) then speed won't get set.
1429          */
1430         if (!config.link_speed)
1431                 config.link_speed = abilities.link_speed;
1432         if (autoneg_changed || abilities.link_speed != config.link_speed) {
1433                 /* copy over the rest of the abilities */
1434                 config.phy_type = abilities.phy_type;
1435                 config.phy_type_ext = abilities.phy_type_ext;
1436                 config.eee_capability = abilities.eee_capability;
1437                 config.eeer = abilities.eeer_val;
1438                 config.low_power_ctrl = abilities.d3_lpan;
1439                 config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
1440                                     I40E_AQ_PHY_FEC_CONFIG_MASK;
1441
1442                 /* save the requested speeds */
1443                 hw->phy.link_info.requested_speeds = config.link_speed;
1444                 /* set link and auto negotiation so changes take effect */
1445                 config.abilities |= I40E_AQ_PHY_ENABLE_ATOMIC_LINK;
1446                 /* If link is up put link down */
1447                 if (hw->phy.link_info.link_info & I40E_AQ_LINK_UP) {
1448                         /* Tell the OS link is going down, the link will go
1449                          * back up when fw says it is ready asynchronously
1450                          */
1451                         i40e_print_link_message(vsi, false);
1452                         netif_carrier_off(netdev);
1453                         netif_tx_stop_all_queues(netdev);
1454                 }
1455
1456                 /* make the aq call */
1457                 status = i40e_aq_set_phy_config(hw, &config, NULL);
1458                 if (status) {
1459                         netdev_info(netdev,
1460                                     "Set phy config failed, err %pe aq_err %s\n",
1461                                     ERR_PTR(status),
1462                                     i40e_aq_str(hw, hw->aq.asq_last_status));
1463                         err = -EAGAIN;
1464                         goto done;
1465                 }
1466
1467                 status = i40e_update_link_info(hw);
1468                 if (status)
1469                         netdev_dbg(netdev,
1470                                    "Updating link info failed with err %pe aq_err %s\n",
1471                                    ERR_PTR(status),
1472                                    i40e_aq_str(hw, hw->aq.asq_last_status));
1473
1474         } else {
1475                 netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
1476         }
1477
1478 done:
1479         clear_bit(__I40E_CONFIG_BUSY, pf->state);
1480
1481         return err;
1482 }
1483
1484 static int i40e_set_fec_cfg(struct net_device *netdev, u8 fec_cfg)
1485 {
1486         struct i40e_netdev_priv *np = netdev_priv(netdev);
1487         struct i40e_aq_get_phy_abilities_resp abilities;
1488         struct i40e_pf *pf = np->vsi->back;
1489         struct i40e_hw *hw = &pf->hw;
1490         int status = 0;
1491         int err = 0;
1492
1493         /* Get the current phy config */
1494         memset(&abilities, 0, sizeof(abilities));
1495         status = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
1496                                               NULL);
1497         if (status) {
1498                 err = -EAGAIN;
1499                 goto done;
1500         }
1501
1502         if (abilities.fec_cfg_curr_mod_ext_info != fec_cfg) {
1503                 struct i40e_aq_set_phy_config config;
1504
1505                 memset(&config, 0, sizeof(config));
1506                 config.phy_type = abilities.phy_type;
1507                 config.abilities = abilities.abilities |
1508                                    I40E_AQ_PHY_ENABLE_ATOMIC_LINK;
1509                 config.phy_type_ext = abilities.phy_type_ext;
1510                 config.link_speed = abilities.link_speed;
1511                 config.eee_capability = abilities.eee_capability;
1512                 config.eeer = abilities.eeer_val;
1513                 config.low_power_ctrl = abilities.d3_lpan;
1514                 config.fec_config = fec_cfg & I40E_AQ_PHY_FEC_CONFIG_MASK;
1515                 status = i40e_aq_set_phy_config(hw, &config, NULL);
1516                 if (status) {
1517                         netdev_info(netdev,
1518                                     "Set phy config failed, err %pe aq_err %s\n",
1519                                     ERR_PTR(status),
1520                                     i40e_aq_str(hw, hw->aq.asq_last_status));
1521                         err = -EAGAIN;
1522                         goto done;
1523                 }
1524                 i40e_set_fec_in_flags(fec_cfg, pf->flags);
1525                 status = i40e_update_link_info(hw);
1526                 if (status)
1527                         /* debug level message only due to relation to the link
1528                          * itself rather than to the FEC settings
1529                          * (e.g. no physical connection etc.)
1530                          */
1531                         netdev_dbg(netdev,
1532                                    "Updating link info failed with err %pe aq_err %s\n",
1533                                    ERR_PTR(status),
1534                                    i40e_aq_str(hw, hw->aq.asq_last_status));
1535         }
1536
1537 done:
1538         return err;
1539 }
1540
1541 static int i40e_get_fec_param(struct net_device *netdev,
1542                               struct ethtool_fecparam *fecparam)
1543 {
1544         struct i40e_netdev_priv *np = netdev_priv(netdev);
1545         struct i40e_aq_get_phy_abilities_resp abilities;
1546         struct i40e_pf *pf = np->vsi->back;
1547         struct i40e_hw *hw = &pf->hw;
1548         int status = 0;
1549         int err = 0;
1550         u8 fec_cfg;
1551
1552         /* Get the current phy config */
1553         memset(&abilities, 0, sizeof(abilities));
1554         status = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
1555                                               NULL);
1556         if (status) {
1557                 err = -EAGAIN;
1558                 goto done;
1559         }
1560
1561         fecparam->fec = 0;
1562         fec_cfg = abilities.fec_cfg_curr_mod_ext_info;
1563         if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
1564                 fecparam->fec |= ETHTOOL_FEC_AUTO;
1565         else if (fec_cfg & (I40E_AQ_SET_FEC_REQUEST_RS |
1566                  I40E_AQ_SET_FEC_ABILITY_RS))
1567                 fecparam->fec |= ETHTOOL_FEC_RS;
1568         else if (fec_cfg & (I40E_AQ_SET_FEC_REQUEST_KR |
1569                  I40E_AQ_SET_FEC_ABILITY_KR))
1570                 fecparam->fec |= ETHTOOL_FEC_BASER;
1571         if (fec_cfg == 0)
1572                 fecparam->fec |= ETHTOOL_FEC_OFF;
1573
1574         if (hw->phy.link_info.fec_info & I40E_AQ_CONFIG_FEC_KR_ENA)
1575                 fecparam->active_fec = ETHTOOL_FEC_BASER;
1576         else if (hw->phy.link_info.fec_info & I40E_AQ_CONFIG_FEC_RS_ENA)
1577                 fecparam->active_fec = ETHTOOL_FEC_RS;
1578         else
1579                 fecparam->active_fec = ETHTOOL_FEC_OFF;
1580 done:
1581         return err;
1582 }
1583
1584 static int i40e_set_fec_param(struct net_device *netdev,
1585                               struct ethtool_fecparam *fecparam)
1586 {
1587         struct i40e_netdev_priv *np = netdev_priv(netdev);
1588         struct i40e_pf *pf = np->vsi->back;
1589         struct i40e_hw *hw = &pf->hw;
1590         u8 fec_cfg = 0;
1591
1592         if (hw->device_id != I40E_DEV_ID_25G_SFP28 &&
1593             hw->device_id != I40E_DEV_ID_25G_B &&
1594             hw->device_id != I40E_DEV_ID_KX_X722)
1595                 return -EPERM;
1596
1597         if (hw->mac.type == I40E_MAC_X722 &&
1598             !test_bit(I40E_HW_CAP_X722_FEC_REQUEST, hw->caps)) {
1599                 netdev_err(netdev, "Setting FEC encoding not supported by firmware. Please update the NVM image.\n");
1600                 return -EOPNOTSUPP;
1601         }
1602
1603         switch (fecparam->fec) {
1604         case ETHTOOL_FEC_AUTO:
1605                 fec_cfg = I40E_AQ_SET_FEC_AUTO;
1606                 break;
1607         case ETHTOOL_FEC_RS:
1608                 fec_cfg = (I40E_AQ_SET_FEC_REQUEST_RS |
1609                              I40E_AQ_SET_FEC_ABILITY_RS);
1610                 break;
1611         case ETHTOOL_FEC_BASER:
1612                 fec_cfg = (I40E_AQ_SET_FEC_REQUEST_KR |
1613                              I40E_AQ_SET_FEC_ABILITY_KR);
1614                 break;
1615         case ETHTOOL_FEC_OFF:
1616         case ETHTOOL_FEC_NONE:
1617                 fec_cfg = 0;
1618                 break;
1619         default:
1620                 dev_warn(&pf->pdev->dev, "Unsupported FEC mode: %d",
1621                          fecparam->fec);
1622                 return -EINVAL;
1623         }
1624
1625         return i40e_set_fec_cfg(netdev, fec_cfg);
1626 }
1627
1628 static int i40e_nway_reset(struct net_device *netdev)
1629 {
1630         /* restart autonegotiation */
1631         struct i40e_netdev_priv *np = netdev_priv(netdev);
1632         struct i40e_pf *pf = np->vsi->back;
1633         struct i40e_hw *hw = &pf->hw;
1634         bool link_up = hw->phy.link_info.link_info & I40E_AQ_LINK_UP;
1635         int ret = 0;
1636
1637         ret = i40e_aq_set_link_restart_an(hw, link_up, NULL);
1638         if (ret) {
1639                 netdev_info(netdev, "link restart failed, err %pe aq_err %s\n",
1640                             ERR_PTR(ret),
1641                             i40e_aq_str(hw, hw->aq.asq_last_status));
1642                 return -EIO;
1643         }
1644
1645         return 0;
1646 }
1647
1648 /**
1649  * i40e_get_pauseparam -  Get Flow Control status
1650  * @netdev: netdevice structure
1651  * @pause: buffer to return pause parameters
1652  *
1653  * Return tx/rx-pause status
1654  **/
1655 static void i40e_get_pauseparam(struct net_device *netdev,
1656                                 struct ethtool_pauseparam *pause)
1657 {
1658         struct i40e_netdev_priv *np = netdev_priv(netdev);
1659         struct i40e_pf *pf = np->vsi->back;
1660         struct i40e_hw *hw = &pf->hw;
1661         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
1662         struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
1663
1664         pause->autoneg =
1665                 ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
1666                   AUTONEG_ENABLE : AUTONEG_DISABLE);
1667
1668         /* PFC enabled so report LFC as off */
1669         if (dcbx_cfg->pfc.pfcenable) {
1670                 pause->rx_pause = 0;
1671                 pause->tx_pause = 0;
1672                 return;
1673         }
1674
1675         if (hw->fc.current_mode == I40E_FC_RX_PAUSE) {
1676                 pause->rx_pause = 1;
1677         } else if (hw->fc.current_mode == I40E_FC_TX_PAUSE) {
1678                 pause->tx_pause = 1;
1679         } else if (hw->fc.current_mode == I40E_FC_FULL) {
1680                 pause->rx_pause = 1;
1681                 pause->tx_pause = 1;
1682         }
1683 }
1684
1685 /**
1686  * i40e_set_pauseparam - Set Flow Control parameter
1687  * @netdev: network interface device structure
1688  * @pause: return tx/rx flow control status
1689  **/
1690 static int i40e_set_pauseparam(struct net_device *netdev,
1691                                struct ethtool_pauseparam *pause)
1692 {
1693         struct i40e_netdev_priv *np = netdev_priv(netdev);
1694         struct i40e_pf *pf = np->vsi->back;
1695         struct i40e_vsi *vsi = np->vsi;
1696         struct i40e_hw *hw = &pf->hw;
1697         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
1698         struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
1699         bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
1700         u8 aq_failures;
1701         int err = 0;
1702         int status;
1703         u32 is_an;
1704
1705         /* Changing the port's flow control is not supported if this isn't the
1706          * port's controlling PF
1707          */
1708         if (hw->partition_id != 1) {
1709                 i40e_partition_setting_complaint(pf);
1710                 return -EOPNOTSUPP;
1711         }
1712
1713         if (vsi->type != I40E_VSI_MAIN)
1714                 return -EOPNOTSUPP;
1715
1716         is_an = hw_link_info->an_info & I40E_AQ_AN_COMPLETED;
1717         if (pause->autoneg != is_an) {
1718                 netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
1719                 return -EOPNOTSUPP;
1720         }
1721
1722         /* If we have link and don't have autoneg */
1723         if (!test_bit(__I40E_DOWN, pf->state) && !is_an) {
1724                 /* Send message that it might not necessarily work*/
1725                 netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
1726         }
1727
1728         if (dcbx_cfg->pfc.pfcenable) {
1729                 netdev_info(netdev,
1730                             "Priority flow control enabled. Cannot set link flow control.\n");
1731                 return -EOPNOTSUPP;
1732         }
1733
1734         if (pause->rx_pause && pause->tx_pause)
1735                 hw->fc.requested_mode = I40E_FC_FULL;
1736         else if (pause->rx_pause && !pause->tx_pause)
1737                 hw->fc.requested_mode = I40E_FC_RX_PAUSE;
1738         else if (!pause->rx_pause && pause->tx_pause)
1739                 hw->fc.requested_mode = I40E_FC_TX_PAUSE;
1740         else if (!pause->rx_pause && !pause->tx_pause)
1741                 hw->fc.requested_mode = I40E_FC_NONE;
1742         else
1743                 return -EINVAL;
1744
1745         /* Tell the OS link is going down, the link will go back up when fw
1746          * says it is ready asynchronously
1747          */
1748         i40e_print_link_message(vsi, false);
1749         netif_carrier_off(netdev);
1750         netif_tx_stop_all_queues(netdev);
1751
1752         /* Set the fc mode and only restart an if link is up*/
1753         status = i40e_set_fc(hw, &aq_failures, link_up);
1754
1755         if (aq_failures & I40E_SET_FC_AQ_FAIL_GET) {
1756                 netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %pe aq_err %s\n",
1757                             ERR_PTR(status),
1758                             i40e_aq_str(hw, hw->aq.asq_last_status));
1759                 err = -EAGAIN;
1760         }
1761         if (aq_failures & I40E_SET_FC_AQ_FAIL_SET) {
1762                 netdev_info(netdev, "Set fc failed on the set_phy_config call with err %pe aq_err %s\n",
1763                             ERR_PTR(status),
1764                             i40e_aq_str(hw, hw->aq.asq_last_status));
1765                 err = -EAGAIN;
1766         }
1767         if (aq_failures & I40E_SET_FC_AQ_FAIL_UPDATE) {
1768                 netdev_info(netdev, "Set fc failed on the get_link_info call with err %pe aq_err %s\n",
1769                             ERR_PTR(status),
1770                             i40e_aq_str(hw, hw->aq.asq_last_status));
1771                 err = -EAGAIN;
1772         }
1773
1774         if (!test_bit(__I40E_DOWN, pf->state) && is_an) {
1775                 /* Give it a little more time to try to come back */
1776                 msleep(75);
1777                 if (!test_bit(__I40E_DOWN, pf->state))
1778                         return i40e_nway_reset(netdev);
1779         }
1780
1781         return err;
1782 }
1783
1784 static u32 i40e_get_msglevel(struct net_device *netdev)
1785 {
1786         struct i40e_netdev_priv *np = netdev_priv(netdev);
1787         struct i40e_pf *pf = np->vsi->back;
1788         u32 debug_mask = pf->hw.debug_mask;
1789
1790         if (debug_mask)
1791                 netdev_info(netdev, "i40e debug_mask: 0x%08X\n", debug_mask);
1792
1793         return pf->msg_enable;
1794 }
1795
1796 static void i40e_set_msglevel(struct net_device *netdev, u32 data)
1797 {
1798         struct i40e_netdev_priv *np = netdev_priv(netdev);
1799         struct i40e_pf *pf = np->vsi->back;
1800
1801         if (I40E_DEBUG_USER & data)
1802                 pf->hw.debug_mask = data;
1803         else
1804                 pf->msg_enable = data;
1805 }
1806
1807 static int i40e_get_regs_len(struct net_device *netdev)
1808 {
1809         int reg_count = 0;
1810         int i;
1811
1812         for (i = 0; i40e_reg_list[i].offset != 0; i++)
1813                 reg_count += i40e_reg_list[i].elements;
1814
1815         return reg_count * sizeof(u32);
1816 }
1817
1818 static void i40e_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
1819                           void *p)
1820 {
1821         struct i40e_netdev_priv *np = netdev_priv(netdev);
1822         struct i40e_pf *pf = np->vsi->back;
1823         struct i40e_hw *hw = &pf->hw;
1824         u32 *reg_buf = p;
1825         unsigned int i, j, ri;
1826         u32 reg;
1827
1828         /* Tell ethtool which driver-version-specific regs output we have.
1829          *
1830          * At some point, if we have ethtool doing special formatting of
1831          * this data, it will rely on this version number to know how to
1832          * interpret things.  Hence, this needs to be updated if/when the
1833          * diags register table is changed.
1834          */
1835         regs->version = 1;
1836
1837         /* loop through the diags reg table for what to print */
1838         ri = 0;
1839         for (i = 0; i40e_reg_list[i].offset != 0; i++) {
1840                 for (j = 0; j < i40e_reg_list[i].elements; j++) {
1841                         reg = i40e_reg_list[i].offset
1842                                 + (j * i40e_reg_list[i].stride);
1843                         reg_buf[ri++] = rd32(hw, reg);
1844                 }
1845         }
1846
1847 }
1848
1849 static int i40e_get_eeprom(struct net_device *netdev,
1850                            struct ethtool_eeprom *eeprom, u8 *bytes)
1851 {
1852         struct i40e_netdev_priv *np = netdev_priv(netdev);
1853         struct i40e_hw *hw = &np->vsi->back->hw;
1854         struct i40e_pf *pf = np->vsi->back;
1855         int ret_val = 0, len, offset;
1856         u8 *eeprom_buff;
1857         u16 i, sectors;
1858         bool last;
1859         u32 magic;
1860
1861 #define I40E_NVM_SECTOR_SIZE  4096
1862         if (eeprom->len == 0)
1863                 return -EINVAL;
1864
1865         /* check for NVMUpdate access method */
1866         magic = hw->vendor_id | (hw->device_id << 16);
1867         if (eeprom->magic && eeprom->magic != magic) {
1868                 struct i40e_nvm_access *cmd = (struct i40e_nvm_access *)eeprom;
1869                 int errno = 0;
1870
1871                 /* make sure it is the right magic for NVMUpdate */
1872                 if ((eeprom->magic >> 16) != hw->device_id)
1873                         errno = -EINVAL;
1874                 else if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
1875                          test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
1876                         errno = -EBUSY;
1877                 else
1878                         ret_val = i40e_nvmupd_command(hw, cmd, bytes, &errno);
1879
1880                 if ((errno || ret_val) && (hw->debug_mask & I40E_DEBUG_NVM))
1881                         dev_info(&pf->pdev->dev,
1882                                  "NVMUpdate read failed err=%d status=0x%x errno=%d module=%d offset=0x%x size=%d\n",
1883                                  ret_val, hw->aq.asq_last_status, errno,
1884                                  (u8)(cmd->config & I40E_NVM_MOD_PNT_MASK),
1885                                  cmd->offset, cmd->data_size);
1886
1887                 return errno;
1888         }
1889
1890         /* normal ethtool get_eeprom support */
1891         eeprom->magic = hw->vendor_id | (hw->device_id << 16);
1892
1893         eeprom_buff = kzalloc(eeprom->len, GFP_KERNEL);
1894         if (!eeprom_buff)
1895                 return -ENOMEM;
1896
1897         ret_val = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
1898         if (ret_val) {
1899                 dev_info(&pf->pdev->dev,
1900                          "Failed Acquiring NVM resource for read err=%d status=0x%x\n",
1901                          ret_val, hw->aq.asq_last_status);
1902                 goto free_buff;
1903         }
1904
1905         sectors = eeprom->len / I40E_NVM_SECTOR_SIZE;
1906         sectors += (eeprom->len % I40E_NVM_SECTOR_SIZE) ? 1 : 0;
1907         len = I40E_NVM_SECTOR_SIZE;
1908         last = false;
1909         for (i = 0; i < sectors; i++) {
1910                 if (i == (sectors - 1)) {
1911                         len = eeprom->len - (I40E_NVM_SECTOR_SIZE * i);
1912                         last = true;
1913                 }
1914                 offset = eeprom->offset + (I40E_NVM_SECTOR_SIZE * i);
1915                 ret_val = i40e_aq_read_nvm(hw, 0x0, offset, len,
1916                                 (u8 *)eeprom_buff + (I40E_NVM_SECTOR_SIZE * i),
1917                                 last, NULL);
1918                 if (ret_val && hw->aq.asq_last_status == I40E_AQ_RC_EPERM) {
1919                         dev_info(&pf->pdev->dev,
1920                                  "read NVM failed, invalid offset 0x%x\n",
1921                                  offset);
1922                         break;
1923                 } else if (ret_val &&
1924                            hw->aq.asq_last_status == I40E_AQ_RC_EACCES) {
1925                         dev_info(&pf->pdev->dev,
1926                                  "read NVM failed, access, offset 0x%x\n",
1927                                  offset);
1928                         break;
1929                 } else if (ret_val) {
1930                         dev_info(&pf->pdev->dev,
1931                                  "read NVM failed offset %d err=%d status=0x%x\n",
1932                                  offset, ret_val, hw->aq.asq_last_status);
1933                         break;
1934                 }
1935         }
1936
1937         i40e_release_nvm(hw);
1938         memcpy(bytes, (u8 *)eeprom_buff, eeprom->len);
1939 free_buff:
1940         kfree(eeprom_buff);
1941         return ret_val;
1942 }
1943
1944 static int i40e_get_eeprom_len(struct net_device *netdev)
1945 {
1946         struct i40e_netdev_priv *np = netdev_priv(netdev);
1947         struct i40e_hw *hw = &np->vsi->back->hw;
1948         u32 val;
1949
1950 #define X722_EEPROM_SCOPE_LIMIT 0x5B9FFF
1951         if (hw->mac.type == I40E_MAC_X722) {
1952                 val = X722_EEPROM_SCOPE_LIMIT + 1;
1953                 return val;
1954         }
1955         val = FIELD_GET(I40E_GLPCI_LBARCTRL_FL_SIZE_MASK,
1956                         rd32(hw, I40E_GLPCI_LBARCTRL));
1957         /* register returns value in power of 2, 64Kbyte chunks. */
1958         val = (64 * 1024) * BIT(val);
1959         return val;
1960 }
1961
1962 static int i40e_set_eeprom(struct net_device *netdev,
1963                            struct ethtool_eeprom *eeprom, u8 *bytes)
1964 {
1965         struct i40e_netdev_priv *np = netdev_priv(netdev);
1966         struct i40e_hw *hw = &np->vsi->back->hw;
1967         struct i40e_pf *pf = np->vsi->back;
1968         struct i40e_nvm_access *cmd = (struct i40e_nvm_access *)eeprom;
1969         int ret_val = 0;
1970         int errno = 0;
1971         u32 magic;
1972
1973         /* normal ethtool set_eeprom is not supported */
1974         magic = hw->vendor_id | (hw->device_id << 16);
1975         if (eeprom->magic == magic)
1976                 errno = -EOPNOTSUPP;
1977         /* check for NVMUpdate access method */
1978         else if (!eeprom->magic || (eeprom->magic >> 16) != hw->device_id)
1979                 errno = -EINVAL;
1980         else if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
1981                  test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
1982                 errno = -EBUSY;
1983         else
1984                 ret_val = i40e_nvmupd_command(hw, cmd, bytes, &errno);
1985
1986         if ((errno || ret_val) && (hw->debug_mask & I40E_DEBUG_NVM))
1987                 dev_info(&pf->pdev->dev,
1988                          "NVMUpdate write failed err=%d status=0x%x errno=%d module=%d offset=0x%x size=%d\n",
1989                          ret_val, hw->aq.asq_last_status, errno,
1990                          (u8)(cmd->config & I40E_NVM_MOD_PNT_MASK),
1991                          cmd->offset, cmd->data_size);
1992
1993         return errno;
1994 }
1995
1996 static void i40e_get_drvinfo(struct net_device *netdev,
1997                              struct ethtool_drvinfo *drvinfo)
1998 {
1999         struct i40e_netdev_priv *np = netdev_priv(netdev);
2000         struct i40e_vsi *vsi = np->vsi;
2001         struct i40e_pf *pf = vsi->back;
2002
2003         strscpy(drvinfo->driver, i40e_driver_name, sizeof(drvinfo->driver));
2004         i40e_nvm_version_str(&pf->hw, drvinfo->fw_version,
2005                              sizeof(drvinfo->fw_version));
2006         strscpy(drvinfo->bus_info, pci_name(pf->pdev),
2007                 sizeof(drvinfo->bus_info));
2008         drvinfo->n_priv_flags = I40E_PRIV_FLAGS_STR_LEN;
2009         if (pf->hw.pf_id == 0)
2010                 drvinfo->n_priv_flags += I40E_GL_PRIV_FLAGS_STR_LEN;
2011 }
2012
2013 static u32 i40e_get_max_num_descriptors(struct i40e_pf *pf)
2014 {
2015         struct i40e_hw *hw = &pf->hw;
2016
2017         switch (hw->mac.type) {
2018         case I40E_MAC_XL710:
2019                 return I40E_MAX_NUM_DESCRIPTORS_XL710;
2020         default:
2021                 return I40E_MAX_NUM_DESCRIPTORS;
2022         }
2023 }
2024
2025 static void i40e_get_ringparam(struct net_device *netdev,
2026                                struct ethtool_ringparam *ring,
2027                                struct kernel_ethtool_ringparam *kernel_ring,
2028                                struct netlink_ext_ack *extack)
2029 {
2030         struct i40e_netdev_priv *np = netdev_priv(netdev);
2031         struct i40e_pf *pf = np->vsi->back;
2032         struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
2033
2034         ring->rx_max_pending = i40e_get_max_num_descriptors(pf);
2035         ring->tx_max_pending = i40e_get_max_num_descriptors(pf);
2036         ring->rx_mini_max_pending = 0;
2037         ring->rx_jumbo_max_pending = 0;
2038         ring->rx_pending = vsi->rx_rings[0]->count;
2039         ring->tx_pending = vsi->tx_rings[0]->count;
2040         ring->rx_mini_pending = 0;
2041         ring->rx_jumbo_pending = 0;
2042 }
2043
2044 static bool i40e_active_tx_ring_index(struct i40e_vsi *vsi, u16 index)
2045 {
2046         if (i40e_enabled_xdp_vsi(vsi)) {
2047                 return index < vsi->num_queue_pairs ||
2048                         (index >= vsi->alloc_queue_pairs &&
2049                          index < vsi->alloc_queue_pairs + vsi->num_queue_pairs);
2050         }
2051
2052         return index < vsi->num_queue_pairs;
2053 }
2054
2055 static int i40e_set_ringparam(struct net_device *netdev,
2056                               struct ethtool_ringparam *ring,
2057                               struct kernel_ethtool_ringparam *kernel_ring,
2058                               struct netlink_ext_ack *extack)
2059 {
2060         u32 new_rx_count, new_tx_count, max_num_descriptors;
2061         struct i40e_ring *tx_rings = NULL, *rx_rings = NULL;
2062         struct i40e_netdev_priv *np = netdev_priv(netdev);
2063         struct i40e_hw *hw = &np->vsi->back->hw;
2064         struct i40e_vsi *vsi = np->vsi;
2065         struct i40e_pf *pf = vsi->back;
2066         u16 tx_alloc_queue_pairs;
2067         int timeout = 50;
2068         int i, err = 0;
2069
2070         if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2071                 return -EINVAL;
2072
2073         max_num_descriptors = i40e_get_max_num_descriptors(pf);
2074         if (ring->tx_pending > max_num_descriptors ||
2075             ring->tx_pending < I40E_MIN_NUM_DESCRIPTORS ||
2076             ring->rx_pending > max_num_descriptors ||
2077             ring->rx_pending < I40E_MIN_NUM_DESCRIPTORS) {
2078                 netdev_info(netdev,
2079                             "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d]\n",
2080                             ring->tx_pending, ring->rx_pending,
2081                             I40E_MIN_NUM_DESCRIPTORS, max_num_descriptors);
2082                 return -EINVAL;
2083         }
2084
2085         new_tx_count = ALIGN(ring->tx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
2086         new_rx_count = ALIGN(ring->rx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
2087
2088         /* if nothing to do return success */
2089         if ((new_tx_count == vsi->tx_rings[0]->count) &&
2090             (new_rx_count == vsi->rx_rings[0]->count))
2091                 return 0;
2092
2093         /* If there is a AF_XDP page pool attached to any of Rx rings,
2094          * disallow changing the number of descriptors -- regardless
2095          * if the netdev is running or not.
2096          */
2097         if (i40e_xsk_any_rx_ring_enabled(vsi))
2098                 return -EBUSY;
2099
2100         while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
2101                 timeout--;
2102                 if (!timeout)
2103                         return -EBUSY;
2104                 usleep_range(1000, 2000);
2105         }
2106
2107         if (!netif_running(vsi->netdev)) {
2108                 /* simple case - set for the next time the netdev is started */
2109                 for (i = 0; i < vsi->num_queue_pairs; i++) {
2110                         vsi->tx_rings[i]->count = new_tx_count;
2111                         vsi->rx_rings[i]->count = new_rx_count;
2112                         if (i40e_enabled_xdp_vsi(vsi))
2113                                 vsi->xdp_rings[i]->count = new_tx_count;
2114                 }
2115                 vsi->num_tx_desc = new_tx_count;
2116                 vsi->num_rx_desc = new_rx_count;
2117                 goto done;
2118         }
2119
2120         /* We can't just free everything and then setup again,
2121          * because the ISRs in MSI-X mode get passed pointers
2122          * to the Tx and Rx ring structs.
2123          */
2124
2125         /* alloc updated Tx and XDP Tx resources */
2126         tx_alloc_queue_pairs = vsi->alloc_queue_pairs *
2127                                (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
2128         if (new_tx_count != vsi->tx_rings[0]->count) {
2129                 netdev_info(netdev,
2130                             "Changing Tx descriptor count from %d to %d.\n",
2131                             vsi->tx_rings[0]->count, new_tx_count);
2132                 tx_rings = kcalloc(tx_alloc_queue_pairs,
2133                                    sizeof(struct i40e_ring), GFP_KERNEL);
2134                 if (!tx_rings) {
2135                         err = -ENOMEM;
2136                         goto done;
2137                 }
2138
2139                 for (i = 0; i < tx_alloc_queue_pairs; i++) {
2140                         if (!i40e_active_tx_ring_index(vsi, i))
2141                                 continue;
2142
2143                         tx_rings[i] = *vsi->tx_rings[i];
2144                         tx_rings[i].count = new_tx_count;
2145                         /* the desc and bi pointers will be reallocated in the
2146                          * setup call
2147                          */
2148                         tx_rings[i].desc = NULL;
2149                         tx_rings[i].rx_bi = NULL;
2150                         err = i40e_setup_tx_descriptors(&tx_rings[i]);
2151                         if (err) {
2152                                 while (i) {
2153                                         i--;
2154                                         if (!i40e_active_tx_ring_index(vsi, i))
2155                                                 continue;
2156                                         i40e_free_tx_resources(&tx_rings[i]);
2157                                 }
2158                                 kfree(tx_rings);
2159                                 tx_rings = NULL;
2160
2161                                 goto done;
2162                         }
2163                 }
2164         }
2165
2166         /* alloc updated Rx resources */
2167         if (new_rx_count != vsi->rx_rings[0]->count) {
2168                 netdev_info(netdev,
2169                             "Changing Rx descriptor count from %d to %d\n",
2170                             vsi->rx_rings[0]->count, new_rx_count);
2171                 rx_rings = kcalloc(vsi->alloc_queue_pairs,
2172                                    sizeof(struct i40e_ring), GFP_KERNEL);
2173                 if (!rx_rings) {
2174                         err = -ENOMEM;
2175                         goto free_tx;
2176                 }
2177
2178                 for (i = 0; i < vsi->num_queue_pairs; i++) {
2179                         u16 unused;
2180
2181                         /* clone ring and setup updated count */
2182                         rx_rings[i] = *vsi->rx_rings[i];
2183                         rx_rings[i].count = new_rx_count;
2184                         /* the desc and bi pointers will be reallocated in the
2185                          * setup call
2186                          */
2187                         rx_rings[i].desc = NULL;
2188                         rx_rings[i].rx_bi = NULL;
2189                         /* Clear cloned XDP RX-queue info before setup call */
2190                         memset(&rx_rings[i].xdp_rxq, 0, sizeof(rx_rings[i].xdp_rxq));
2191                         /* this is to allow wr32 to have something to write to
2192                          * during early allocation of Rx buffers
2193                          */
2194                         rx_rings[i].tail = hw->hw_addr + I40E_PRTGEN_STATUS;
2195                         err = i40e_setup_rx_descriptors(&rx_rings[i]);
2196                         if (err)
2197                                 goto rx_unwind;
2198
2199                         /* now allocate the Rx buffers to make sure the OS
2200                          * has enough memory, any failure here means abort
2201                          */
2202                         unused = I40E_DESC_UNUSED(&rx_rings[i]);
2203                         err = i40e_alloc_rx_buffers(&rx_rings[i], unused);
2204 rx_unwind:
2205                         if (err) {
2206                                 do {
2207                                         i40e_free_rx_resources(&rx_rings[i]);
2208                                 } while (i--);
2209                                 kfree(rx_rings);
2210                                 rx_rings = NULL;
2211
2212                                 goto free_tx;
2213                         }
2214                 }
2215         }
2216
2217         /* Bring interface down, copy in the new ring info,
2218          * then restore the interface
2219          */
2220         i40e_down(vsi);
2221
2222         if (tx_rings) {
2223                 for (i = 0; i < tx_alloc_queue_pairs; i++) {
2224                         if (i40e_active_tx_ring_index(vsi, i)) {
2225                                 i40e_free_tx_resources(vsi->tx_rings[i]);
2226                                 *vsi->tx_rings[i] = tx_rings[i];
2227                         }
2228                 }
2229                 kfree(tx_rings);
2230                 tx_rings = NULL;
2231         }
2232
2233         if (rx_rings) {
2234                 for (i = 0; i < vsi->num_queue_pairs; i++) {
2235                         i40e_free_rx_resources(vsi->rx_rings[i]);
2236                         /* get the real tail offset */
2237                         rx_rings[i].tail = vsi->rx_rings[i]->tail;
2238                         /* this is to fake out the allocation routine
2239                          * into thinking it has to realloc everything
2240                          * but the recycling logic will let us re-use
2241                          * the buffers allocated above
2242                          */
2243                         rx_rings[i].next_to_use = 0;
2244                         rx_rings[i].next_to_clean = 0;
2245                         rx_rings[i].next_to_alloc = 0;
2246                         /* do a struct copy */
2247                         *vsi->rx_rings[i] = rx_rings[i];
2248                 }
2249                 kfree(rx_rings);
2250                 rx_rings = NULL;
2251         }
2252
2253         vsi->num_tx_desc = new_tx_count;
2254         vsi->num_rx_desc = new_rx_count;
2255         i40e_up(vsi);
2256
2257 free_tx:
2258         /* error cleanup if the Rx allocations failed after getting Tx */
2259         if (tx_rings) {
2260                 for (i = 0; i < tx_alloc_queue_pairs; i++) {
2261                         if (i40e_active_tx_ring_index(vsi, i))
2262                                 i40e_free_tx_resources(vsi->tx_rings[i]);
2263                 }
2264                 kfree(tx_rings);
2265                 tx_rings = NULL;
2266         }
2267
2268 done:
2269         clear_bit(__I40E_CONFIG_BUSY, pf->state);
2270
2271         return err;
2272 }
2273
2274 /**
2275  * i40e_get_stats_count - return the stats count for a device
2276  * @netdev: the netdev to return the count for
2277  *
2278  * Returns the total number of statistics for this netdev. Note that even
2279  * though this is a function, it is required that the count for a specific
2280  * netdev must never change. Basing the count on static values such as the
2281  * maximum number of queues or the device type is ok. However, the API for
2282  * obtaining stats is *not* safe against changes based on non-static
2283  * values such as the *current* number of queues, or runtime flags.
2284  *
2285  * If a statistic is not always enabled, return it as part of the count
2286  * anyways, always return its string, and report its value as zero.
2287  **/
2288 static int i40e_get_stats_count(struct net_device *netdev)
2289 {
2290         struct i40e_netdev_priv *np = netdev_priv(netdev);
2291         struct i40e_vsi *vsi = np->vsi;
2292         struct i40e_pf *pf = vsi->back;
2293         int stats_len;
2294
2295         if (vsi->type == I40E_VSI_MAIN && pf->hw.partition_id == 1)
2296                 stats_len = I40E_PF_STATS_LEN;
2297         else
2298                 stats_len = I40E_VSI_STATS_LEN;
2299
2300         /* The number of stats reported for a given net_device must remain
2301          * constant throughout the life of that device.
2302          *
2303          * This is because the API for obtaining the size, strings, and stats
2304          * is spread out over three separate ethtool ioctls. There is no safe
2305          * way to lock the number of stats across these calls, so we must
2306          * assume that they will never change.
2307          *
2308          * Due to this, we report the maximum number of queues, even if not
2309          * every queue is currently configured. Since we always allocate
2310          * queues in pairs, we'll just use netdev->num_tx_queues * 2. This
2311          * works because the num_tx_queues is set at device creation and never
2312          * changes.
2313          */
2314         stats_len += I40E_QUEUE_STATS_LEN * 2 * netdev->num_tx_queues;
2315
2316         return stats_len;
2317 }
2318
2319 static int i40e_get_sset_count(struct net_device *netdev, int sset)
2320 {
2321         struct i40e_netdev_priv *np = netdev_priv(netdev);
2322         struct i40e_vsi *vsi = np->vsi;
2323         struct i40e_pf *pf = vsi->back;
2324
2325         switch (sset) {
2326         case ETH_SS_TEST:
2327                 return I40E_TEST_LEN;
2328         case ETH_SS_STATS:
2329                 return i40e_get_stats_count(netdev);
2330         case ETH_SS_PRIV_FLAGS:
2331                 return I40E_PRIV_FLAGS_STR_LEN +
2332                         (pf->hw.pf_id == 0 ? I40E_GL_PRIV_FLAGS_STR_LEN : 0);
2333         default:
2334                 return -EOPNOTSUPP;
2335         }
2336 }
2337
2338 /**
2339  * i40e_get_veb_tc_stats - copy VEB TC statistics to formatted structure
2340  * @tc: the TC statistics in VEB structure (veb->tc_stats)
2341  * @i: the index of traffic class in (veb->tc_stats) structure to copy
2342  *
2343  * Copy VEB TC statistics from structure of arrays (veb->tc_stats) to
2344  * one dimensional structure i40e_cp_veb_tc_stats.
2345  * Produce formatted i40e_cp_veb_tc_stats structure of the VEB TC
2346  * statistics for the given TC.
2347  **/
2348 static struct i40e_cp_veb_tc_stats
2349 i40e_get_veb_tc_stats(struct i40e_veb_tc_stats *tc, unsigned int i)
2350 {
2351         struct i40e_cp_veb_tc_stats veb_tc = {
2352                 .tc_rx_packets = tc->tc_rx_packets[i],
2353                 .tc_rx_bytes = tc->tc_rx_bytes[i],
2354                 .tc_tx_packets = tc->tc_tx_packets[i],
2355                 .tc_tx_bytes = tc->tc_tx_bytes[i],
2356         };
2357
2358         return veb_tc;
2359 }
2360
2361 /**
2362  * i40e_get_pfc_stats - copy HW PFC statistics to formatted structure
2363  * @pf: the PF device structure
2364  * @i: the priority value to copy
2365  *
2366  * The PFC stats are found as arrays in pf->stats, which is not easy to pass
2367  * into i40e_add_ethtool_stats. Produce a formatted i40e_pfc_stats structure
2368  * of the PFC stats for the given priority.
2369  **/
2370 static inline struct i40e_pfc_stats
2371 i40e_get_pfc_stats(struct i40e_pf *pf, unsigned int i)
2372 {
2373 #define I40E_GET_PFC_STAT(stat, priority) \
2374         .stat = pf->stats.stat[priority]
2375
2376         struct i40e_pfc_stats pfc = {
2377                 I40E_GET_PFC_STAT(priority_xon_rx, i),
2378                 I40E_GET_PFC_STAT(priority_xoff_rx, i),
2379                 I40E_GET_PFC_STAT(priority_xon_tx, i),
2380                 I40E_GET_PFC_STAT(priority_xoff_tx, i),
2381                 I40E_GET_PFC_STAT(priority_xon_2_xoff, i),
2382         };
2383         return pfc;
2384 }
2385
2386 /**
2387  * i40e_get_ethtool_stats - copy stat values into supplied buffer
2388  * @netdev: the netdev to collect stats for
2389  * @stats: ethtool stats command structure
2390  * @data: ethtool supplied buffer
2391  *
2392  * Copy the stats values for this netdev into the buffer. Expects data to be
2393  * pre-allocated to the size returned by i40e_get_stats_count.. Note that all
2394  * statistics must be copied in a static order, and the count must not change
2395  * for a given netdev. See i40e_get_stats_count for more details.
2396  *
2397  * If a statistic is not currently valid (such as a disabled queue), this
2398  * function reports its value as zero.
2399  **/
2400 static void i40e_get_ethtool_stats(struct net_device *netdev,
2401                                    struct ethtool_stats *stats, u64 *data)
2402 {
2403         struct i40e_netdev_priv *np = netdev_priv(netdev);
2404         struct i40e_vsi *vsi = np->vsi;
2405         struct i40e_pf *pf = vsi->back;
2406         struct i40e_veb *veb = NULL;
2407         unsigned int i;
2408         bool veb_stats;
2409         u64 *p = data;
2410
2411         i40e_update_stats(vsi);
2412
2413         i40e_add_ethtool_stats(&data, i40e_get_vsi_stats_struct(vsi),
2414                                i40e_gstrings_net_stats);
2415
2416         i40e_add_ethtool_stats(&data, vsi, i40e_gstrings_misc_stats);
2417
2418         rcu_read_lock();
2419         for (i = 0; i < netdev->num_tx_queues; i++) {
2420                 i40e_add_queue_stats(&data, READ_ONCE(vsi->tx_rings[i]));
2421                 i40e_add_queue_stats(&data, READ_ONCE(vsi->rx_rings[i]));
2422         }
2423         rcu_read_unlock();
2424
2425         if (vsi->type != I40E_VSI_MAIN || pf->hw.partition_id != 1)
2426                 goto check_data_pointer;
2427
2428         veb = i40e_pf_get_main_veb(pf);
2429         veb_stats = veb && test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
2430
2431         if (veb_stats)
2432                 i40e_update_veb_stats(veb);
2433
2434         /* If veb stats aren't enabled, pass NULL instead of the veb so that
2435          * we initialize stats to zero and update the data pointer
2436          * intelligently
2437          */
2438         i40e_add_ethtool_stats(&data, veb_stats ? veb : NULL,
2439                                i40e_gstrings_veb_stats);
2440
2441         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
2442                 if (veb_stats) {
2443                         struct i40e_cp_veb_tc_stats veb_tc =
2444                                 i40e_get_veb_tc_stats(&veb->tc_stats, i);
2445
2446                         i40e_add_ethtool_stats(&data, &veb_tc,
2447                                                i40e_gstrings_veb_tc_stats);
2448                 } else {
2449                         i40e_add_ethtool_stats(&data, NULL,
2450                                                i40e_gstrings_veb_tc_stats);
2451                 }
2452
2453         i40e_add_ethtool_stats(&data, pf, i40e_gstrings_stats);
2454
2455         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
2456                 struct i40e_pfc_stats pfc = i40e_get_pfc_stats(pf, i);
2457
2458                 i40e_add_ethtool_stats(&data, &pfc, i40e_gstrings_pfc_stats);
2459         }
2460
2461 check_data_pointer:
2462         WARN_ONCE(data - p != i40e_get_stats_count(netdev),
2463                   "ethtool stats count mismatch!");
2464 }
2465
2466 /**
2467  * i40e_get_stat_strings - copy stat strings into supplied buffer
2468  * @netdev: the netdev to collect strings for
2469  * @data: supplied buffer to copy strings into
2470  *
2471  * Copy the strings related to stats for this netdev. Expects data to be
2472  * pre-allocated with the size reported by i40e_get_stats_count. Note that the
2473  * strings must be copied in a static order and the total count must not
2474  * change for a given netdev. See i40e_get_stats_count for more details.
2475  **/
2476 static void i40e_get_stat_strings(struct net_device *netdev, u8 *data)
2477 {
2478         struct i40e_netdev_priv *np = netdev_priv(netdev);
2479         struct i40e_vsi *vsi = np->vsi;
2480         struct i40e_pf *pf = vsi->back;
2481         unsigned int i;
2482         u8 *p = data;
2483
2484         i40e_add_stat_strings(&data, i40e_gstrings_net_stats);
2485
2486         i40e_add_stat_strings(&data, i40e_gstrings_misc_stats);
2487
2488         for (i = 0; i < netdev->num_tx_queues; i++) {
2489                 i40e_add_stat_strings(&data, i40e_gstrings_queue_stats,
2490                                       "tx", i);
2491                 i40e_add_stat_strings(&data, i40e_gstrings_queue_stats,
2492                                       "rx", i);
2493         }
2494
2495         if (vsi->type != I40E_VSI_MAIN || pf->hw.partition_id != 1)
2496                 goto check_data_pointer;
2497
2498         i40e_add_stat_strings(&data, i40e_gstrings_veb_stats);
2499
2500         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
2501                 i40e_add_stat_strings(&data, i40e_gstrings_veb_tc_stats, i);
2502
2503         i40e_add_stat_strings(&data, i40e_gstrings_stats);
2504
2505         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
2506                 i40e_add_stat_strings(&data, i40e_gstrings_pfc_stats, i);
2507
2508 check_data_pointer:
2509         WARN_ONCE(data - p != i40e_get_stats_count(netdev) * ETH_GSTRING_LEN,
2510                   "stat strings count mismatch!");
2511 }
2512
2513 static void i40e_get_priv_flag_strings(struct net_device *netdev, u8 *data)
2514 {
2515         struct i40e_netdev_priv *np = netdev_priv(netdev);
2516         struct i40e_vsi *vsi = np->vsi;
2517         struct i40e_pf *pf = vsi->back;
2518         unsigned int i;
2519         u8 *p = data;
2520
2521         for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++)
2522                 ethtool_puts(&p, i40e_gstrings_priv_flags[i].flag_string);
2523         if (pf->hw.pf_id != 0)
2524                 return;
2525         for (i = 0; i < I40E_GL_PRIV_FLAGS_STR_LEN; i++)
2526                 ethtool_puts(&p, i40e_gl_gstrings_priv_flags[i].flag_string);
2527 }
2528
2529 static void i40e_get_strings(struct net_device *netdev, u32 stringset,
2530                              u8 *data)
2531 {
2532         switch (stringset) {
2533         case ETH_SS_TEST:
2534                 memcpy(data, i40e_gstrings_test,
2535                        I40E_TEST_LEN * ETH_GSTRING_LEN);
2536                 break;
2537         case ETH_SS_STATS:
2538                 i40e_get_stat_strings(netdev, data);
2539                 break;
2540         case ETH_SS_PRIV_FLAGS:
2541                 i40e_get_priv_flag_strings(netdev, data);
2542                 break;
2543         default:
2544                 break;
2545         }
2546 }
2547
2548 static int i40e_get_ts_info(struct net_device *dev,
2549                             struct kernel_ethtool_ts_info *info)
2550 {
2551         struct i40e_pf *pf = i40e_netdev_to_pf(dev);
2552
2553         /* only report HW timestamping if PTP is enabled */
2554         if (!test_bit(I40E_FLAG_PTP_ENA, pf->flags))
2555                 return ethtool_op_get_ts_info(dev, info);
2556
2557         info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2558                                 SOF_TIMESTAMPING_RX_SOFTWARE |
2559                                 SOF_TIMESTAMPING_SOFTWARE |
2560                                 SOF_TIMESTAMPING_TX_HARDWARE |
2561                                 SOF_TIMESTAMPING_RX_HARDWARE |
2562                                 SOF_TIMESTAMPING_RAW_HARDWARE;
2563
2564         if (pf->ptp_clock)
2565                 info->phc_index = ptp_clock_index(pf->ptp_clock);
2566         else
2567                 info->phc_index = -1;
2568
2569         info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
2570
2571         info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
2572                            BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
2573                            BIT(HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
2574                            BIT(HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ);
2575
2576         if (test_bit(I40E_HW_CAP_PTP_L4, pf->hw.caps))
2577                 info->rx_filters |= BIT(HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
2578                                     BIT(HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
2579                                     BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
2580                                     BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
2581                                     BIT(HWTSTAMP_FILTER_PTP_V2_SYNC) |
2582                                     BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
2583                                     BIT(HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
2584                                     BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
2585
2586         return 0;
2587 }
2588
2589 static u64 i40e_link_test(struct net_device *netdev, u64 *data)
2590 {
2591         struct i40e_netdev_priv *np = netdev_priv(netdev);
2592         struct i40e_pf *pf = np->vsi->back;
2593         bool link_up = false;
2594         int status;
2595
2596         netif_info(pf, hw, netdev, "link test\n");
2597         status = i40e_get_link_status(&pf->hw, &link_up);
2598         if (status) {
2599                 netif_err(pf, drv, netdev, "link query timed out, please retry test\n");
2600                 *data = 1;
2601                 return *data;
2602         }
2603
2604         if (link_up)
2605                 *data = 0;
2606         else
2607                 *data = 1;
2608
2609         return *data;
2610 }
2611
2612 static u64 i40e_reg_test(struct net_device *netdev, u64 *data)
2613 {
2614         struct i40e_netdev_priv *np = netdev_priv(netdev);
2615         struct i40e_pf *pf = np->vsi->back;
2616
2617         netif_info(pf, hw, netdev, "register test\n");
2618         *data = i40e_diag_reg_test(&pf->hw);
2619
2620         return *data;
2621 }
2622
2623 static u64 i40e_eeprom_test(struct net_device *netdev, u64 *data)
2624 {
2625         struct i40e_netdev_priv *np = netdev_priv(netdev);
2626         struct i40e_pf *pf = np->vsi->back;
2627
2628         netif_info(pf, hw, netdev, "eeprom test\n");
2629         *data = i40e_diag_eeprom_test(&pf->hw);
2630
2631         /* forcebly clear the NVM Update state machine */
2632         pf->hw.nvmupd_state = I40E_NVMUPD_STATE_INIT;
2633
2634         return *data;
2635 }
2636
2637 static u64 i40e_intr_test(struct net_device *netdev, u64 *data)
2638 {
2639         struct i40e_netdev_priv *np = netdev_priv(netdev);
2640         struct i40e_pf *pf = np->vsi->back;
2641         u16 swc_old = pf->sw_int_count;
2642
2643         netif_info(pf, hw, netdev, "interrupt test\n");
2644         wr32(&pf->hw, I40E_PFINT_DYN_CTL0,
2645              (I40E_PFINT_DYN_CTL0_INTENA_MASK |
2646               I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
2647               I40E_PFINT_DYN_CTL0_ITR_INDX_MASK |
2648               I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK |
2649               I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK));
2650         usleep_range(1000, 2000);
2651         *data = (swc_old == pf->sw_int_count);
2652
2653         return *data;
2654 }
2655
2656 static inline bool i40e_active_vfs(struct i40e_pf *pf)
2657 {
2658         struct i40e_vf *vfs = pf->vf;
2659         int i;
2660
2661         for (i = 0; i < pf->num_alloc_vfs; i++)
2662                 if (test_bit(I40E_VF_STATE_ACTIVE, &vfs[i].vf_states))
2663                         return true;
2664         return false;
2665 }
2666
2667 static inline bool i40e_active_vmdqs(struct i40e_pf *pf)
2668 {
2669         return !!i40e_find_vsi_by_type(pf, I40E_VSI_VMDQ2);
2670 }
2671
2672 static void i40e_diag_test(struct net_device *netdev,
2673                            struct ethtool_test *eth_test, u64 *data)
2674 {
2675         struct i40e_netdev_priv *np = netdev_priv(netdev);
2676         bool if_running = netif_running(netdev);
2677         struct i40e_pf *pf = np->vsi->back;
2678
2679         if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
2680                 /* Offline tests */
2681                 netif_info(pf, drv, netdev, "offline testing starting\n");
2682
2683                 set_bit(__I40E_TESTING, pf->state);
2684
2685                 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
2686                     test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
2687                         dev_warn(&pf->pdev->dev,
2688                                  "Cannot start offline testing when PF is in reset state.\n");
2689                         goto skip_ol_tests;
2690                 }
2691
2692                 if (i40e_active_vfs(pf) || i40e_active_vmdqs(pf)) {
2693                         dev_warn(&pf->pdev->dev,
2694                                  "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
2695                         goto skip_ol_tests;
2696                 }
2697
2698                 /* If the device is online then take it offline */
2699                 if (if_running)
2700                         /* indicate we're in test mode */
2701                         i40e_close(netdev);
2702                 else
2703                         /* This reset does not affect link - if it is
2704                          * changed to a type of reset that does affect
2705                          * link then the following link test would have
2706                          * to be moved to before the reset
2707                          */
2708                         i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED), true);
2709
2710                 if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
2711                         eth_test->flags |= ETH_TEST_FL_FAILED;
2712
2713                 if (i40e_eeprom_test(netdev, &data[I40E_ETH_TEST_EEPROM]))
2714                         eth_test->flags |= ETH_TEST_FL_FAILED;
2715
2716                 if (i40e_intr_test(netdev, &data[I40E_ETH_TEST_INTR]))
2717                         eth_test->flags |= ETH_TEST_FL_FAILED;
2718
2719                 /* run reg test last, a reset is required after it */
2720                 if (i40e_reg_test(netdev, &data[I40E_ETH_TEST_REG]))
2721                         eth_test->flags |= ETH_TEST_FL_FAILED;
2722
2723                 clear_bit(__I40E_TESTING, pf->state);
2724                 i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED), true);
2725
2726                 if (if_running)
2727                         i40e_open(netdev);
2728         } else {
2729                 /* Online tests */
2730                 netif_info(pf, drv, netdev, "online testing starting\n");
2731
2732                 if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
2733                         eth_test->flags |= ETH_TEST_FL_FAILED;
2734
2735                 /* Offline only tests, not run in online; pass by default */
2736                 data[I40E_ETH_TEST_REG] = 0;
2737                 data[I40E_ETH_TEST_EEPROM] = 0;
2738                 data[I40E_ETH_TEST_INTR] = 0;
2739         }
2740
2741         netif_info(pf, drv, netdev, "testing finished\n");
2742         return;
2743
2744 skip_ol_tests:
2745         data[I40E_ETH_TEST_REG]         = 1;
2746         data[I40E_ETH_TEST_EEPROM]      = 1;
2747         data[I40E_ETH_TEST_INTR]        = 1;
2748         data[I40E_ETH_TEST_LINK]        = 1;
2749         eth_test->flags |= ETH_TEST_FL_FAILED;
2750         clear_bit(__I40E_TESTING, pf->state);
2751         netif_info(pf, drv, netdev, "testing failed\n");
2752 }
2753
2754 static void i40e_get_wol(struct net_device *netdev,
2755                          struct ethtool_wolinfo *wol)
2756 {
2757         struct i40e_netdev_priv *np = netdev_priv(netdev);
2758         struct i40e_pf *pf = np->vsi->back;
2759         struct i40e_hw *hw = &pf->hw;
2760         u16 wol_nvm_bits;
2761
2762         /* NVM bit on means WoL disabled for the port */
2763         i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
2764         if ((BIT(hw->port) & wol_nvm_bits) || (hw->partition_id != 1)) {
2765                 wol->supported = 0;
2766                 wol->wolopts = 0;
2767         } else {
2768                 wol->supported = WAKE_MAGIC;
2769                 wol->wolopts = (pf->wol_en ? WAKE_MAGIC : 0);
2770         }
2771 }
2772
2773 /**
2774  * i40e_set_wol - set the WakeOnLAN configuration
2775  * @netdev: the netdev in question
2776  * @wol: the ethtool WoL setting data
2777  **/
2778 static int i40e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2779 {
2780         struct i40e_netdev_priv *np = netdev_priv(netdev);
2781         struct i40e_pf *pf = np->vsi->back;
2782         struct i40e_vsi *vsi = np->vsi;
2783         struct i40e_hw *hw = &pf->hw;
2784         u16 wol_nvm_bits;
2785
2786         /* WoL not supported if this isn't the controlling PF on the port */
2787         if (hw->partition_id != 1) {
2788                 i40e_partition_setting_complaint(pf);
2789                 return -EOPNOTSUPP;
2790         }
2791
2792         if (vsi->type != I40E_VSI_MAIN)
2793                 return -EOPNOTSUPP;
2794
2795         /* NVM bit on means WoL disabled for the port */
2796         i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
2797         if (BIT(hw->port) & wol_nvm_bits)
2798                 return -EOPNOTSUPP;
2799
2800         /* only magic packet is supported */
2801         if (wol->wolopts & ~WAKE_MAGIC)
2802                 return -EOPNOTSUPP;
2803
2804         /* is this a new value? */
2805         if (pf->wol_en != !!wol->wolopts) {
2806                 pf->wol_en = !!wol->wolopts;
2807                 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
2808         }
2809
2810         return 0;
2811 }
2812
2813 static int i40e_set_phys_id(struct net_device *netdev,
2814                             enum ethtool_phys_id_state state)
2815 {
2816         struct i40e_netdev_priv *np = netdev_priv(netdev);
2817         struct i40e_pf *pf = np->vsi->back;
2818         struct i40e_hw *hw = &pf->hw;
2819         int blink_freq = 2;
2820         u16 temp_status;
2821         int ret = 0;
2822
2823         switch (state) {
2824         case ETHTOOL_ID_ACTIVE:
2825                 if (!test_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps)) {
2826                         pf->led_status = i40e_led_get(hw);
2827                 } else {
2828                         if (!test_bit(I40E_HW_CAP_AQ_PHY_ACCESS, hw->caps))
2829                                 i40e_aq_set_phy_debug(hw, I40E_PHY_DEBUG_ALL,
2830                                                       NULL);
2831                         ret = i40e_led_get_phy(hw, &temp_status,
2832                                                &pf->phy_led_val);
2833                         pf->led_status = temp_status;
2834                 }
2835                 return blink_freq;
2836         case ETHTOOL_ID_ON:
2837                 if (!test_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps))
2838                         i40e_led_set(hw, 0xf, false);
2839                 else
2840                         ret = i40e_led_set_phy(hw, true, pf->led_status, 0);
2841                 break;
2842         case ETHTOOL_ID_OFF:
2843                 if (!test_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps))
2844                         i40e_led_set(hw, 0x0, false);
2845                 else
2846                         ret = i40e_led_set_phy(hw, false, pf->led_status, 0);
2847                 break;
2848         case ETHTOOL_ID_INACTIVE:
2849                 if (!test_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps)) {
2850                         i40e_led_set(hw, pf->led_status, false);
2851                 } else {
2852                         ret = i40e_led_set_phy(hw, false, pf->led_status,
2853                                                (pf->phy_led_val |
2854                                                I40E_PHY_LED_MODE_ORIG));
2855                         if (!test_bit(I40E_HW_CAP_AQ_PHY_ACCESS, hw->caps))
2856                                 i40e_aq_set_phy_debug(hw, 0, NULL);
2857                 }
2858                 break;
2859         default:
2860                 break;
2861         }
2862         if (ret)
2863                 return -ENOENT;
2864         else
2865                 return 0;
2866 }
2867
2868 /* NOTE: i40e hardware uses a conversion factor of 2 for Interrupt
2869  * Throttle Rate (ITR) ie. ITR(1) = 2us ITR(10) = 20 us, and also
2870  * 125us (8000 interrupts per second) == ITR(62)
2871  */
2872
2873 /**
2874  * __i40e_get_coalesce - get per-queue coalesce settings
2875  * @netdev: the netdev to check
2876  * @ec: ethtool coalesce data structure
2877  * @queue: which queue to pick
2878  *
2879  * Gets the per-queue settings for coalescence. Specifically Rx and Tx usecs
2880  * are per queue. If queue is <0 then we default to queue 0 as the
2881  * representative value.
2882  **/
2883 static int __i40e_get_coalesce(struct net_device *netdev,
2884                                struct ethtool_coalesce *ec,
2885                                int queue)
2886 {
2887         struct i40e_netdev_priv *np = netdev_priv(netdev);
2888         struct i40e_ring *rx_ring, *tx_ring;
2889         struct i40e_vsi *vsi = np->vsi;
2890
2891         ec->tx_max_coalesced_frames_irq = vsi->work_limit;
2892
2893         /* rx and tx usecs has per queue value. If user doesn't specify the
2894          * queue, return queue 0's value to represent.
2895          */
2896         if (queue < 0)
2897                 queue = 0;
2898         else if (queue >= vsi->num_queue_pairs)
2899                 return -EINVAL;
2900
2901         rx_ring = vsi->rx_rings[queue];
2902         tx_ring = vsi->tx_rings[queue];
2903
2904         if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
2905                 ec->use_adaptive_rx_coalesce = 1;
2906
2907         if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
2908                 ec->use_adaptive_tx_coalesce = 1;
2909
2910         ec->rx_coalesce_usecs = rx_ring->itr_setting & ~I40E_ITR_DYNAMIC;
2911         ec->tx_coalesce_usecs = tx_ring->itr_setting & ~I40E_ITR_DYNAMIC;
2912
2913         /* we use the _usecs_high to store/set the interrupt rate limit
2914          * that the hardware supports, that almost but not quite
2915          * fits the original intent of the ethtool variable,
2916          * the rx_coalesce_usecs_high limits total interrupts
2917          * per second from both tx/rx sources.
2918          */
2919         ec->rx_coalesce_usecs_high = vsi->int_rate_limit;
2920         ec->tx_coalesce_usecs_high = vsi->int_rate_limit;
2921
2922         return 0;
2923 }
2924
2925 /**
2926  * i40e_get_coalesce - get a netdev's coalesce settings
2927  * @netdev: the netdev to check
2928  * @ec: ethtool coalesce data structure
2929  * @kernel_coal: ethtool CQE mode setting structure
2930  * @extack: extack for reporting error messages
2931  *
2932  * Gets the coalesce settings for a particular netdev. Note that if user has
2933  * modified per-queue settings, this only guarantees to represent queue 0. See
2934  * __i40e_get_coalesce for more details.
2935  **/
2936 static int i40e_get_coalesce(struct net_device *netdev,
2937                              struct ethtool_coalesce *ec,
2938                              struct kernel_ethtool_coalesce *kernel_coal,
2939                              struct netlink_ext_ack *extack)
2940 {
2941         return __i40e_get_coalesce(netdev, ec, -1);
2942 }
2943
2944 /**
2945  * i40e_get_per_queue_coalesce - gets coalesce settings for particular queue
2946  * @netdev: netdev structure
2947  * @ec: ethtool's coalesce settings
2948  * @queue: the particular queue to read
2949  *
2950  * Will read a specific queue's coalesce settings
2951  **/
2952 static int i40e_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
2953                                        struct ethtool_coalesce *ec)
2954 {
2955         return __i40e_get_coalesce(netdev, ec, queue);
2956 }
2957
2958 /**
2959  * i40e_set_itr_per_queue - set ITR values for specific queue
2960  * @vsi: the VSI to set values for
2961  * @ec: coalesce settings from ethtool
2962  * @queue: the queue to modify
2963  *
2964  * Change the ITR settings for a specific queue.
2965  **/
2966 static void i40e_set_itr_per_queue(struct i40e_vsi *vsi,
2967                                    struct ethtool_coalesce *ec,
2968                                    int queue)
2969 {
2970         struct i40e_ring *rx_ring = vsi->rx_rings[queue];
2971         struct i40e_ring *tx_ring = vsi->tx_rings[queue];
2972         struct i40e_pf *pf = vsi->back;
2973         struct i40e_hw *hw = &pf->hw;
2974         struct i40e_q_vector *q_vector;
2975         u16 intrl;
2976
2977         intrl = i40e_intrl_usec_to_reg(vsi->int_rate_limit);
2978
2979         rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
2980         tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
2981
2982         if (ec->use_adaptive_rx_coalesce)
2983                 rx_ring->itr_setting |= I40E_ITR_DYNAMIC;
2984         else
2985                 rx_ring->itr_setting &= ~I40E_ITR_DYNAMIC;
2986
2987         if (ec->use_adaptive_tx_coalesce)
2988                 tx_ring->itr_setting |= I40E_ITR_DYNAMIC;
2989         else
2990                 tx_ring->itr_setting &= ~I40E_ITR_DYNAMIC;
2991
2992         q_vector = rx_ring->q_vector;
2993         q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
2994
2995         q_vector = tx_ring->q_vector;
2996         q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
2997
2998         /* The interrupt handler itself will take care of programming
2999          * the Tx and Rx ITR values based on the values we have entered
3000          * into the q_vector, no need to write the values now.
3001          */
3002
3003         wr32(hw, I40E_PFINT_RATEN(q_vector->reg_idx), intrl);
3004         i40e_flush(hw);
3005 }
3006
3007 /**
3008  * __i40e_set_coalesce - set coalesce settings for particular queue
3009  * @netdev: the netdev to change
3010  * @ec: ethtool coalesce settings
3011  * @queue: the queue to change
3012  *
3013  * Sets the coalesce settings for a particular queue.
3014  **/
3015 static int __i40e_set_coalesce(struct net_device *netdev,
3016                                struct ethtool_coalesce *ec,
3017                                int queue)
3018 {
3019         struct i40e_netdev_priv *np = netdev_priv(netdev);
3020         u16 intrl_reg, cur_rx_itr, cur_tx_itr;
3021         struct i40e_vsi *vsi = np->vsi;
3022         struct i40e_pf *pf = vsi->back;
3023         int i;
3024
3025         if (ec->tx_max_coalesced_frames_irq)
3026                 vsi->work_limit = ec->tx_max_coalesced_frames_irq;
3027
3028         if (queue < 0) {
3029                 cur_rx_itr = vsi->rx_rings[0]->itr_setting;
3030                 cur_tx_itr = vsi->tx_rings[0]->itr_setting;
3031         } else if (queue < vsi->num_queue_pairs) {
3032                 cur_rx_itr = vsi->rx_rings[queue]->itr_setting;
3033                 cur_tx_itr = vsi->tx_rings[queue]->itr_setting;
3034         } else {
3035                 netif_info(pf, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
3036                            vsi->num_queue_pairs - 1);
3037                 return -EINVAL;
3038         }
3039
3040         cur_tx_itr &= ~I40E_ITR_DYNAMIC;
3041         cur_rx_itr &= ~I40E_ITR_DYNAMIC;
3042
3043         /* tx_coalesce_usecs_high is ignored, use rx-usecs-high instead */
3044         if (ec->tx_coalesce_usecs_high != vsi->int_rate_limit) {
3045                 netif_info(pf, drv, netdev, "tx-usecs-high is not used, please program rx-usecs-high\n");
3046                 return -EINVAL;
3047         }
3048
3049         if (ec->rx_coalesce_usecs_high > INTRL_REG_TO_USEC(I40E_MAX_INTRL)) {
3050                 netif_info(pf, drv, netdev, "Invalid value, rx-usecs-high range is 0-%lu\n",
3051                            INTRL_REG_TO_USEC(I40E_MAX_INTRL));
3052                 return -EINVAL;
3053         }
3054
3055         if (ec->rx_coalesce_usecs != cur_rx_itr &&
3056             ec->use_adaptive_rx_coalesce) {
3057                 netif_info(pf, drv, netdev, "RX interrupt moderation cannot be changed if adaptive-rx is enabled.\n");
3058                 return -EINVAL;
3059         }
3060
3061         if (ec->rx_coalesce_usecs > I40E_MAX_ITR) {
3062                 netif_info(pf, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
3063                 return -EINVAL;
3064         }
3065
3066         if (ec->tx_coalesce_usecs != cur_tx_itr &&
3067             ec->use_adaptive_tx_coalesce) {
3068                 netif_info(pf, drv, netdev, "TX interrupt moderation cannot be changed if adaptive-tx is enabled.\n");
3069                 return -EINVAL;
3070         }
3071
3072         if (ec->tx_coalesce_usecs > I40E_MAX_ITR) {
3073                 netif_info(pf, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
3074                 return -EINVAL;
3075         }
3076
3077         if (ec->use_adaptive_rx_coalesce && !cur_rx_itr)
3078                 ec->rx_coalesce_usecs = I40E_MIN_ITR;
3079
3080         if (ec->use_adaptive_tx_coalesce && !cur_tx_itr)
3081                 ec->tx_coalesce_usecs = I40E_MIN_ITR;
3082
3083         intrl_reg = i40e_intrl_usec_to_reg(ec->rx_coalesce_usecs_high);
3084         vsi->int_rate_limit = INTRL_REG_TO_USEC(intrl_reg);
3085         if (vsi->int_rate_limit != ec->rx_coalesce_usecs_high) {
3086                 netif_info(pf, drv, netdev, "Interrupt rate limit rounded down to %d\n",
3087                            vsi->int_rate_limit);
3088         }
3089
3090         /* rx and tx usecs has per queue value. If user doesn't specify the
3091          * queue, apply to all queues.
3092          */
3093         if (queue < 0) {
3094                 for (i = 0; i < vsi->num_queue_pairs; i++)
3095                         i40e_set_itr_per_queue(vsi, ec, i);
3096         } else {
3097                 i40e_set_itr_per_queue(vsi, ec, queue);
3098         }
3099
3100         return 0;
3101 }
3102
3103 /**
3104  * i40e_set_coalesce - set coalesce settings for every queue on the netdev
3105  * @netdev: the netdev to change
3106  * @ec: ethtool coalesce settings
3107  * @kernel_coal: ethtool CQE mode setting structure
3108  * @extack: extack for reporting error messages
3109  *
3110  * This will set each queue to the same coalesce settings.
3111  **/
3112 static int i40e_set_coalesce(struct net_device *netdev,
3113                              struct ethtool_coalesce *ec,
3114                              struct kernel_ethtool_coalesce *kernel_coal,
3115                              struct netlink_ext_ack *extack)
3116 {
3117         return __i40e_set_coalesce(netdev, ec, -1);
3118 }
3119
3120 /**
3121  * i40e_set_per_queue_coalesce - set specific queue's coalesce settings
3122  * @netdev: the netdev to change
3123  * @ec: ethtool's coalesce settings
3124  * @queue: the queue to change
3125  *
3126  * Sets the specified queue's coalesce settings.
3127  **/
3128 static int i40e_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
3129                                        struct ethtool_coalesce *ec)
3130 {
3131         return __i40e_set_coalesce(netdev, ec, queue);
3132 }
3133
3134 /**
3135  * i40e_get_rss_hash_opts - Get RSS hash Input Set for each flow type
3136  * @pf: pointer to the physical function struct
3137  * @cmd: ethtool rxnfc command
3138  *
3139  * Returns Success if the flow is supported, else Invalid Input.
3140  **/
3141 static int i40e_get_rss_hash_opts(struct i40e_pf *pf, struct ethtool_rxnfc *cmd)
3142 {
3143         struct i40e_hw *hw = &pf->hw;
3144         u8 flow_pctype = 0;
3145         u64 i_set = 0;
3146
3147         cmd->data = 0;
3148
3149         switch (cmd->flow_type) {
3150         case TCP_V4_FLOW:
3151                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
3152                 break;
3153         case UDP_V4_FLOW:
3154                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
3155                 break;
3156         case TCP_V6_FLOW:
3157                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_TCP;
3158                 break;
3159         case UDP_V6_FLOW:
3160                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_UDP;
3161                 break;
3162         case SCTP_V4_FLOW:
3163         case AH_ESP_V4_FLOW:
3164         case AH_V4_FLOW:
3165         case ESP_V4_FLOW:
3166         case IPV4_FLOW:
3167         case SCTP_V6_FLOW:
3168         case AH_ESP_V6_FLOW:
3169         case AH_V6_FLOW:
3170         case ESP_V6_FLOW:
3171         case IPV6_FLOW:
3172                 /* Default is src/dest for IP, no matter the L4 hashing */
3173                 cmd->data |= RXH_IP_SRC | RXH_IP_DST;
3174                 break;
3175         default:
3176                 return -EINVAL;
3177         }
3178
3179         /* Read flow based hash input set register */
3180         if (flow_pctype) {
3181                 i_set = (u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(0,
3182                                               flow_pctype)) |
3183                         ((u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(1,
3184                                                flow_pctype)) << 32);
3185         }
3186
3187         /* Process bits of hash input set */
3188         if (i_set) {
3189                 if (i_set & I40E_L4_SRC_MASK)
3190                         cmd->data |= RXH_L4_B_0_1;
3191                 if (i_set & I40E_L4_DST_MASK)
3192                         cmd->data |= RXH_L4_B_2_3;
3193
3194                 if (cmd->flow_type == TCP_V4_FLOW ||
3195                     cmd->flow_type == UDP_V4_FLOW) {
3196                         if (hw->mac.type == I40E_MAC_X722) {
3197                                 if (i_set & I40E_X722_L3_SRC_MASK)
3198                                         cmd->data |= RXH_IP_SRC;
3199                                 if (i_set & I40E_X722_L3_DST_MASK)
3200                                         cmd->data |= RXH_IP_DST;
3201                         } else {
3202                                 if (i_set & I40E_L3_SRC_MASK)
3203                                         cmd->data |= RXH_IP_SRC;
3204                                 if (i_set & I40E_L3_DST_MASK)
3205                                         cmd->data |= RXH_IP_DST;
3206                         }
3207                 } else if (cmd->flow_type == TCP_V6_FLOW ||
3208                           cmd->flow_type == UDP_V6_FLOW) {
3209                         if (i_set & I40E_L3_V6_SRC_MASK)
3210                                 cmd->data |= RXH_IP_SRC;
3211                         if (i_set & I40E_L3_V6_DST_MASK)
3212                                 cmd->data |= RXH_IP_DST;
3213                 }
3214         }
3215
3216         return 0;
3217 }
3218
3219 /**
3220  * i40e_check_mask - Check whether a mask field is set
3221  * @mask: the full mask value
3222  * @field: mask of the field to check
3223  *
3224  * If the given mask is fully set, return positive value. If the mask for the
3225  * field is fully unset, return zero. Otherwise return a negative error code.
3226  **/
3227 static int i40e_check_mask(u64 mask, u64 field)
3228 {
3229         u64 value = mask & field;
3230
3231         if (value == field)
3232                 return 1;
3233         else if (!value)
3234                 return 0;
3235         else
3236                 return -1;
3237 }
3238
3239 /**
3240  * i40e_parse_rx_flow_user_data - Deconstruct user-defined data
3241  * @fsp: pointer to rx flow specification
3242  * @data: pointer to userdef data structure for storage
3243  *
3244  * Read the user-defined data and deconstruct the value into a structure. No
3245  * other code should read the user-defined data, so as to ensure that every
3246  * place consistently reads the value correctly.
3247  *
3248  * The user-defined field is a 64bit Big Endian format value, which we
3249  * deconstruct by reading bits or bit fields from it. Single bit flags shall
3250  * be defined starting from the highest bits, while small bit field values
3251  * shall be defined starting from the lowest bits.
3252  *
3253  * Returns 0 if the data is valid, and non-zero if the userdef data is invalid
3254  * and the filter should be rejected. The data structure will always be
3255  * modified even if FLOW_EXT is not set.
3256  *
3257  **/
3258 static int i40e_parse_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
3259                                         struct i40e_rx_flow_userdef *data)
3260 {
3261         u64 value, mask;
3262         int valid;
3263
3264         /* Zero memory first so it's always consistent. */
3265         memset(data, 0, sizeof(*data));
3266
3267         if (!(fsp->flow_type & FLOW_EXT))
3268                 return 0;
3269
3270         value = be64_to_cpu(*((__be64 *)fsp->h_ext.data));
3271         mask = be64_to_cpu(*((__be64 *)fsp->m_ext.data));
3272
3273 #define I40E_USERDEF_FLEX_WORD          GENMASK_ULL(15, 0)
3274 #define I40E_USERDEF_FLEX_OFFSET        GENMASK_ULL(31, 16)
3275 #define I40E_USERDEF_FLEX_FILTER        GENMASK_ULL(31, 0)
3276
3277         valid = i40e_check_mask(mask, I40E_USERDEF_FLEX_FILTER);
3278         if (valid < 0) {
3279                 return -EINVAL;
3280         } else if (valid) {
3281                 data->flex_word = value & I40E_USERDEF_FLEX_WORD;
3282                 data->flex_offset =
3283                         FIELD_GET(I40E_USERDEF_FLEX_OFFSET, value);
3284                 data->flex_filter = true;
3285         }
3286
3287         return 0;
3288 }
3289
3290 /**
3291  * i40e_fill_rx_flow_user_data - Fill in user-defined data field
3292  * @fsp: pointer to rx_flow specification
3293  * @data: pointer to return userdef data
3294  *
3295  * Reads the userdef data structure and properly fills in the user defined
3296  * fields of the rx_flow_spec.
3297  **/
3298 static void i40e_fill_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
3299                                         struct i40e_rx_flow_userdef *data)
3300 {
3301         u64 value = 0, mask = 0;
3302
3303         if (data->flex_filter) {
3304                 value |= data->flex_word;
3305                 value |= (u64)data->flex_offset << 16;
3306                 mask |= I40E_USERDEF_FLEX_FILTER;
3307         }
3308
3309         if (value || mask)
3310                 fsp->flow_type |= FLOW_EXT;
3311
3312         *((__be64 *)fsp->h_ext.data) = cpu_to_be64(value);
3313         *((__be64 *)fsp->m_ext.data) = cpu_to_be64(mask);
3314 }
3315
3316 /**
3317  * i40e_get_ethtool_fdir_all - Populates the rule count of a command
3318  * @pf: Pointer to the physical function struct
3319  * @cmd: The command to get or set Rx flow classification rules
3320  * @rule_locs: Array of used rule locations
3321  *
3322  * This function populates both the total and actual rule count of
3323  * the ethtool flow classification command
3324  *
3325  * Returns 0 on success or -EMSGSIZE if entry not found
3326  **/
3327 static int i40e_get_ethtool_fdir_all(struct i40e_pf *pf,
3328                                      struct ethtool_rxnfc *cmd,
3329                                      u32 *rule_locs)
3330 {
3331         struct i40e_fdir_filter *rule;
3332         struct hlist_node *node2;
3333         int cnt = 0;
3334
3335         /* report total rule count */
3336         cmd->data = i40e_get_fd_cnt_all(pf);
3337
3338         hlist_for_each_entry_safe(rule, node2,
3339                                   &pf->fdir_filter_list, fdir_node) {
3340                 if (cnt == cmd->rule_cnt)
3341                         return -EMSGSIZE;
3342
3343                 rule_locs[cnt] = rule->fd_id;
3344                 cnt++;
3345         }
3346
3347         cmd->rule_cnt = cnt;
3348
3349         return 0;
3350 }
3351
3352 /**
3353  * i40e_get_ethtool_fdir_entry - Look up a filter based on Rx flow
3354  * @pf: Pointer to the physical function struct
3355  * @cmd: The command to get or set Rx flow classification rules
3356  *
3357  * This function looks up a filter based on the Rx flow classification
3358  * command and fills the flow spec info for it if found
3359  *
3360  * Returns 0 on success or -EINVAL if filter not found
3361  **/
3362 static int i40e_get_ethtool_fdir_entry(struct i40e_pf *pf,
3363                                        struct ethtool_rxnfc *cmd)
3364 {
3365         struct ethtool_rx_flow_spec *fsp =
3366                         (struct ethtool_rx_flow_spec *)&cmd->fs;
3367         struct i40e_rx_flow_userdef userdef = {0};
3368         struct i40e_fdir_filter *rule = NULL;
3369         struct hlist_node *node2;
3370         struct i40e_vsi *vsi;
3371         u64 input_set;
3372         u16 index;
3373
3374         hlist_for_each_entry_safe(rule, node2,
3375                                   &pf->fdir_filter_list, fdir_node) {
3376                 if (fsp->location <= rule->fd_id)
3377                         break;
3378         }
3379
3380         if (!rule || fsp->location != rule->fd_id)
3381                 return -EINVAL;
3382
3383         fsp->flow_type = rule->flow_type;
3384         if (fsp->flow_type == IP_USER_FLOW) {
3385                 fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
3386                 fsp->h_u.usr_ip4_spec.proto = 0;
3387                 fsp->m_u.usr_ip4_spec.proto = 0;
3388         }
3389
3390         if (fsp->flow_type == IPV6_USER_FLOW ||
3391             fsp->flow_type == UDP_V6_FLOW ||
3392             fsp->flow_type == TCP_V6_FLOW ||
3393             fsp->flow_type == SCTP_V6_FLOW) {
3394                 /* Reverse the src and dest notion, since the HW views them
3395                  * from Tx perspective where as the user expects it from
3396                  * Rx filter view.
3397                  */
3398                 fsp->h_u.tcp_ip6_spec.psrc = rule->dst_port;
3399                 fsp->h_u.tcp_ip6_spec.pdst = rule->src_port;
3400                 memcpy(fsp->h_u.tcp_ip6_spec.ip6dst, rule->src_ip6,
3401                        sizeof(__be32) * 4);
3402                 memcpy(fsp->h_u.tcp_ip6_spec.ip6src, rule->dst_ip6,
3403                        sizeof(__be32) * 4);
3404         } else {
3405                 /* Reverse the src and dest notion, since the HW views them
3406                  * from Tx perspective where as the user expects it from
3407                  * Rx filter view.
3408                  */
3409                 fsp->h_u.tcp_ip4_spec.psrc = rule->dst_port;
3410                 fsp->h_u.tcp_ip4_spec.pdst = rule->src_port;
3411                 fsp->h_u.tcp_ip4_spec.ip4src = rule->dst_ip;
3412                 fsp->h_u.tcp_ip4_spec.ip4dst = rule->src_ip;
3413         }
3414
3415         switch (rule->flow_type) {
3416         case SCTP_V4_FLOW:
3417                 index = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
3418                 break;
3419         case TCP_V4_FLOW:
3420                 index = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
3421                 break;
3422         case UDP_V4_FLOW:
3423                 index = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
3424                 break;
3425         case SCTP_V6_FLOW:
3426                 index = I40E_FILTER_PCTYPE_NONF_IPV6_SCTP;
3427                 break;
3428         case TCP_V6_FLOW:
3429                 index = I40E_FILTER_PCTYPE_NONF_IPV6_TCP;
3430                 break;
3431         case UDP_V6_FLOW:
3432                 index = I40E_FILTER_PCTYPE_NONF_IPV6_UDP;
3433                 break;
3434         case IP_USER_FLOW:
3435                 index = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
3436                 break;
3437         case IPV6_USER_FLOW:
3438                 index = I40E_FILTER_PCTYPE_NONF_IPV6_OTHER;
3439                 break;
3440         default:
3441                 /* If we have stored a filter with a flow type not listed here
3442                  * it is almost certainly a driver bug. WARN(), and then
3443                  * assign the input_set as if all fields are enabled to avoid
3444                  * reading unassigned memory.
3445                  */
3446                 WARN(1, "Missing input set index for flow_type %d\n",
3447                      rule->flow_type);
3448                 input_set = 0xFFFFFFFFFFFFFFFFULL;
3449                 goto no_input_set;
3450         }
3451
3452         input_set = i40e_read_fd_input_set(pf, index);
3453
3454 no_input_set:
3455         if (input_set & I40E_L3_V6_SRC_MASK) {
3456                 fsp->m_u.tcp_ip6_spec.ip6src[0] = htonl(0xFFFFFFFF);
3457                 fsp->m_u.tcp_ip6_spec.ip6src[1] = htonl(0xFFFFFFFF);
3458                 fsp->m_u.tcp_ip6_spec.ip6src[2] = htonl(0xFFFFFFFF);
3459                 fsp->m_u.tcp_ip6_spec.ip6src[3] = htonl(0xFFFFFFFF);
3460         }
3461
3462         if (input_set & I40E_L3_V6_DST_MASK) {
3463                 fsp->m_u.tcp_ip6_spec.ip6dst[0] = htonl(0xFFFFFFFF);
3464                 fsp->m_u.tcp_ip6_spec.ip6dst[1] = htonl(0xFFFFFFFF);
3465                 fsp->m_u.tcp_ip6_spec.ip6dst[2] = htonl(0xFFFFFFFF);
3466                 fsp->m_u.tcp_ip6_spec.ip6dst[3] = htonl(0xFFFFFFFF);
3467         }
3468
3469         if (input_set & I40E_L3_SRC_MASK)
3470                 fsp->m_u.tcp_ip4_spec.ip4src = htonl(0xFFFFFFFF);
3471
3472         if (input_set & I40E_L3_DST_MASK)
3473                 fsp->m_u.tcp_ip4_spec.ip4dst = htonl(0xFFFFFFFF);
3474
3475         if (input_set & I40E_L4_SRC_MASK)
3476                 fsp->m_u.tcp_ip4_spec.psrc = htons(0xFFFF);
3477
3478         if (input_set & I40E_L4_DST_MASK)
3479                 fsp->m_u.tcp_ip4_spec.pdst = htons(0xFFFF);
3480
3481         if (rule->dest_ctl == I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET)
3482                 fsp->ring_cookie = RX_CLS_FLOW_DISC;
3483         else
3484                 fsp->ring_cookie = rule->q_index;
3485
3486         if (rule->vlan_tag) {
3487                 fsp->h_ext.vlan_etype = rule->vlan_etype;
3488                 fsp->m_ext.vlan_etype = htons(0xFFFF);
3489                 fsp->h_ext.vlan_tci = rule->vlan_tag;
3490                 fsp->m_ext.vlan_tci = htons(0xFFFF);
3491                 fsp->flow_type |= FLOW_EXT;
3492         }
3493
3494         vsi = i40e_pf_get_main_vsi(pf);
3495         if (rule->dest_vsi != vsi->id) {
3496                 vsi = i40e_find_vsi_from_id(pf, rule->dest_vsi);
3497                 if (vsi && vsi->type == I40E_VSI_SRIOV) {
3498                         /* VFs are zero-indexed by the driver, but ethtool
3499                          * expects them to be one-indexed, so add one here
3500                          */
3501                         u64 ring_vf = vsi->vf_id + 1;
3502
3503                         ring_vf <<= ETHTOOL_RX_FLOW_SPEC_RING_VF_OFF;
3504                         fsp->ring_cookie |= ring_vf;
3505                 }
3506         }
3507
3508         if (rule->flex_filter) {
3509                 userdef.flex_filter = true;
3510                 userdef.flex_word = be16_to_cpu(rule->flex_word);
3511                 userdef.flex_offset = rule->flex_offset;
3512         }
3513
3514         i40e_fill_rx_flow_user_data(fsp, &userdef);
3515
3516         return 0;
3517 }
3518
3519 /**
3520  * i40e_get_rxnfc - command to get RX flow classification rules
3521  * @netdev: network interface device structure
3522  * @cmd: ethtool rxnfc command
3523  * @rule_locs: pointer to store rule data
3524  *
3525  * Returns Success if the command is supported.
3526  **/
3527 static int i40e_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3528                           u32 *rule_locs)
3529 {
3530         struct i40e_netdev_priv *np = netdev_priv(netdev);
3531         struct i40e_vsi *vsi = np->vsi;
3532         struct i40e_pf *pf = vsi->back;
3533         int ret = -EOPNOTSUPP;
3534
3535         switch (cmd->cmd) {
3536         case ETHTOOL_GRXRINGS:
3537                 cmd->data = vsi->rss_size;
3538                 ret = 0;
3539                 break;
3540         case ETHTOOL_GRXFH:
3541                 ret = i40e_get_rss_hash_opts(pf, cmd);
3542                 break;
3543         case ETHTOOL_GRXCLSRLCNT:
3544                 cmd->rule_cnt = pf->fdir_pf_active_filters;
3545                 /* report total rule count */
3546                 cmd->data = i40e_get_fd_cnt_all(pf);
3547                 ret = 0;
3548                 break;
3549         case ETHTOOL_GRXCLSRULE:
3550                 ret = i40e_get_ethtool_fdir_entry(pf, cmd);
3551                 break;
3552         case ETHTOOL_GRXCLSRLALL:
3553                 ret = i40e_get_ethtool_fdir_all(pf, cmd, rule_locs);
3554                 break;
3555         default:
3556                 break;
3557         }
3558
3559         return ret;
3560 }
3561
3562 /**
3563  * i40e_get_rss_hash_bits - Read RSS Hash bits from register
3564  * @hw: hw structure
3565  * @nfc: pointer to user request
3566  * @i_setc: bits currently set
3567  *
3568  * Returns value of bits to be set per user request
3569  **/
3570 static u64 i40e_get_rss_hash_bits(struct i40e_hw *hw,
3571                                   struct ethtool_rxnfc *nfc,
3572                                   u64 i_setc)
3573 {
3574         u64 i_set = i_setc;
3575         u64 src_l3 = 0, dst_l3 = 0;
3576
3577         if (nfc->data & RXH_L4_B_0_1)
3578                 i_set |= I40E_L4_SRC_MASK;
3579         else
3580                 i_set &= ~I40E_L4_SRC_MASK;
3581         if (nfc->data & RXH_L4_B_2_3)
3582                 i_set |= I40E_L4_DST_MASK;
3583         else
3584                 i_set &= ~I40E_L4_DST_MASK;
3585
3586         if (nfc->flow_type == TCP_V6_FLOW || nfc->flow_type == UDP_V6_FLOW) {
3587                 src_l3 = I40E_L3_V6_SRC_MASK;
3588                 dst_l3 = I40E_L3_V6_DST_MASK;
3589         } else if (nfc->flow_type == TCP_V4_FLOW ||
3590                   nfc->flow_type == UDP_V4_FLOW) {
3591                 if (hw->mac.type == I40E_MAC_X722) {
3592                         src_l3 = I40E_X722_L3_SRC_MASK;
3593                         dst_l3 = I40E_X722_L3_DST_MASK;
3594                 } else {
3595                         src_l3 = I40E_L3_SRC_MASK;
3596                         dst_l3 = I40E_L3_DST_MASK;
3597                 }
3598         } else {
3599                 /* Any other flow type are not supported here */
3600                 return i_set;
3601         }
3602
3603         if (nfc->data & RXH_IP_SRC)
3604                 i_set |= src_l3;
3605         else
3606                 i_set &= ~src_l3;
3607         if (nfc->data & RXH_IP_DST)
3608                 i_set |= dst_l3;
3609         else
3610                 i_set &= ~dst_l3;
3611
3612         return i_set;
3613 }
3614
3615 #define FLOW_PCTYPES_SIZE 64
3616 /**
3617  * i40e_set_rss_hash_opt - Enable/Disable flow types for RSS hash
3618  * @pf: pointer to the physical function struct
3619  * @nfc: ethtool rxnfc command
3620  *
3621  * Returns Success if the flow input set is supported.
3622  **/
3623 static int i40e_set_rss_hash_opt(struct i40e_pf *pf, struct ethtool_rxnfc *nfc)
3624 {
3625         struct i40e_hw *hw = &pf->hw;
3626         u64 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
3627                    ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
3628         DECLARE_BITMAP(flow_pctypes, FLOW_PCTYPES_SIZE);
3629         u64 i_set, i_setc;
3630
3631         bitmap_zero(flow_pctypes, FLOW_PCTYPES_SIZE);
3632
3633         if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
3634                 dev_err(&pf->pdev->dev,
3635                         "Change of RSS hash input set is not supported when MFP mode is enabled\n");
3636                 return -EOPNOTSUPP;
3637         }
3638
3639         /* RSS does not support anything other than hashing
3640          * to queues on src and dst IPs and ports
3641          */
3642         if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
3643                           RXH_L4_B_0_1 | RXH_L4_B_2_3))
3644                 return -EINVAL;
3645
3646         switch (nfc->flow_type) {
3647         case TCP_V4_FLOW:
3648                 set_bit(I40E_FILTER_PCTYPE_NONF_IPV4_TCP, flow_pctypes);
3649                 if (test_bit(I40E_HW_CAP_MULTI_TCP_UDP_RSS_PCTYPE,
3650                              pf->hw.caps))
3651                         set_bit(I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK,
3652                                 flow_pctypes);
3653                 break;
3654         case TCP_V6_FLOW:
3655                 set_bit(I40E_FILTER_PCTYPE_NONF_IPV6_TCP, flow_pctypes);
3656                 if (test_bit(I40E_HW_CAP_MULTI_TCP_UDP_RSS_PCTYPE,
3657                              pf->hw.caps))
3658                         set_bit(I40E_FILTER_PCTYPE_NONF_IPV6_TCP_SYN_NO_ACK,
3659                                 flow_pctypes);
3660                 break;
3661         case UDP_V4_FLOW:
3662                 set_bit(I40E_FILTER_PCTYPE_NONF_IPV4_UDP, flow_pctypes);
3663                 if (test_bit(I40E_HW_CAP_MULTI_TCP_UDP_RSS_PCTYPE,
3664                              pf->hw.caps)) {
3665                         set_bit(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP,
3666                                 flow_pctypes);
3667                         set_bit(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP,
3668                                 flow_pctypes);
3669                 }
3670                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4);
3671                 break;
3672         case UDP_V6_FLOW:
3673                 set_bit(I40E_FILTER_PCTYPE_NONF_IPV6_UDP, flow_pctypes);
3674                 if (test_bit(I40E_HW_CAP_MULTI_TCP_UDP_RSS_PCTYPE,
3675                              pf->hw.caps)) {
3676                         set_bit(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP,
3677                                 flow_pctypes);
3678                         set_bit(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP,
3679                                 flow_pctypes);
3680                 }
3681                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6);
3682                 break;
3683         case AH_ESP_V4_FLOW:
3684         case AH_V4_FLOW:
3685         case ESP_V4_FLOW:
3686         case SCTP_V4_FLOW:
3687                 if ((nfc->data & RXH_L4_B_0_1) ||
3688                     (nfc->data & RXH_L4_B_2_3))
3689                         return -EINVAL;
3690                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER);
3691                 break;
3692         case AH_ESP_V6_FLOW:
3693         case AH_V6_FLOW:
3694         case ESP_V6_FLOW:
3695         case SCTP_V6_FLOW:
3696                 if ((nfc->data & RXH_L4_B_0_1) ||
3697                     (nfc->data & RXH_L4_B_2_3))
3698                         return -EINVAL;
3699                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER);
3700                 break;
3701         case IPV4_FLOW:
3702                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
3703                         BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4);
3704                 break;
3705         case IPV6_FLOW:
3706                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
3707                         BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6);
3708                 break;
3709         default:
3710                 return -EINVAL;
3711         }
3712
3713         if (bitmap_weight(flow_pctypes, FLOW_PCTYPES_SIZE)) {
3714                 u8 flow_id;
3715
3716                 for_each_set_bit(flow_id, flow_pctypes, FLOW_PCTYPES_SIZE) {
3717                         i_setc = (u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(0, flow_id)) |
3718                                  ((u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(1, flow_id)) << 32);
3719                         i_set = i40e_get_rss_hash_bits(&pf->hw, nfc, i_setc);
3720
3721                         i40e_write_rx_ctl(hw, I40E_GLQF_HASH_INSET(0, flow_id),
3722                                           (u32)i_set);
3723                         i40e_write_rx_ctl(hw, I40E_GLQF_HASH_INSET(1, flow_id),
3724                                           (u32)(i_set >> 32));
3725                         hena |= BIT_ULL(flow_id);
3726                 }
3727         }
3728
3729         i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
3730         i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
3731         i40e_flush(hw);
3732
3733         return 0;
3734 }
3735
3736 /**
3737  * i40e_update_ethtool_fdir_entry - Updates the fdir filter entry
3738  * @vsi: Pointer to the targeted VSI
3739  * @input: The filter to update or NULL to indicate deletion
3740  * @sw_idx: Software index to the filter
3741  * @cmd: The command to get or set Rx flow classification rules
3742  *
3743  * This function updates (or deletes) a Flow Director entry from
3744  * the hlist of the corresponding PF
3745  *
3746  * Returns 0 on success
3747  **/
3748 static int i40e_update_ethtool_fdir_entry(struct i40e_vsi *vsi,
3749                                           struct i40e_fdir_filter *input,
3750                                           u16 sw_idx,
3751                                           struct ethtool_rxnfc *cmd)
3752 {
3753         struct i40e_fdir_filter *rule, *parent;
3754         struct i40e_pf *pf = vsi->back;
3755         struct hlist_node *node2;
3756         int err = -EINVAL;
3757
3758         parent = NULL;
3759         rule = NULL;
3760
3761         hlist_for_each_entry_safe(rule, node2,
3762                                   &pf->fdir_filter_list, fdir_node) {
3763                 /* hash found, or no matching entry */
3764                 if (rule->fd_id >= sw_idx)
3765                         break;
3766                 parent = rule;
3767         }
3768
3769         /* if there is an old rule occupying our place remove it */
3770         if (rule && (rule->fd_id == sw_idx)) {
3771                 /* Remove this rule, since we're either deleting it, or
3772                  * replacing it.
3773                  */
3774                 err = i40e_add_del_fdir(vsi, rule, false);
3775                 hlist_del(&rule->fdir_node);
3776                 kfree(rule);
3777                 pf->fdir_pf_active_filters--;
3778         }
3779
3780         /* If we weren't given an input, this is a delete, so just return the
3781          * error code indicating if there was an entry at the requested slot
3782          */
3783         if (!input)
3784                 return err;
3785
3786         /* Otherwise, install the new rule as requested */
3787         INIT_HLIST_NODE(&input->fdir_node);
3788
3789         /* add filter to the list */
3790         if (parent)
3791                 hlist_add_behind(&input->fdir_node, &parent->fdir_node);
3792         else
3793                 hlist_add_head(&input->fdir_node,
3794                                &pf->fdir_filter_list);
3795
3796         /* update counts */
3797         pf->fdir_pf_active_filters++;
3798
3799         return 0;
3800 }
3801
3802 /**
3803  * i40e_prune_flex_pit_list - Cleanup unused entries in FLX_PIT table
3804  * @pf: pointer to PF structure
3805  *
3806  * This function searches the list of filters and determines which FLX_PIT
3807  * entries are still required. It will prune any entries which are no longer
3808  * in use after the deletion.
3809  **/
3810 static void i40e_prune_flex_pit_list(struct i40e_pf *pf)
3811 {
3812         struct i40e_flex_pit *entry, *tmp;
3813         struct i40e_fdir_filter *rule;
3814
3815         /* First, we'll check the l3 table */
3816         list_for_each_entry_safe(entry, tmp, &pf->l3_flex_pit_list, list) {
3817                 bool found = false;
3818
3819                 hlist_for_each_entry(rule, &pf->fdir_filter_list, fdir_node) {
3820                         if (rule->flow_type != IP_USER_FLOW)
3821                                 continue;
3822                         if (rule->flex_filter &&
3823                             rule->flex_offset == entry->src_offset) {
3824                                 found = true;
3825                                 break;
3826                         }
3827                 }
3828
3829                 /* If we didn't find the filter, then we can prune this entry
3830                  * from the list.
3831                  */
3832                 if (!found) {
3833                         list_del(&entry->list);
3834                         kfree(entry);
3835                 }
3836         }
3837
3838         /* Followed by the L4 table */
3839         list_for_each_entry_safe(entry, tmp, &pf->l4_flex_pit_list, list) {
3840                 bool found = false;
3841
3842                 hlist_for_each_entry(rule, &pf->fdir_filter_list, fdir_node) {
3843                         /* Skip this filter if it's L3, since we already
3844                          * checked those in the above loop
3845                          */
3846                         if (rule->flow_type == IP_USER_FLOW)
3847                                 continue;
3848                         if (rule->flex_filter &&
3849                             rule->flex_offset == entry->src_offset) {
3850                                 found = true;
3851                                 break;
3852                         }
3853                 }
3854
3855                 /* If we didn't find the filter, then we can prune this entry
3856                  * from the list.
3857                  */
3858                 if (!found) {
3859                         list_del(&entry->list);
3860                         kfree(entry);
3861                 }
3862         }
3863 }
3864
3865 /**
3866  * i40e_del_fdir_entry - Deletes a Flow Director filter entry
3867  * @vsi: Pointer to the targeted VSI
3868  * @cmd: The command to get or set Rx flow classification rules
3869  *
3870  * The function removes a Flow Director filter entry from the
3871  * hlist of the corresponding PF
3872  *
3873  * Returns 0 on success
3874  */
3875 static int i40e_del_fdir_entry(struct i40e_vsi *vsi,
3876                                struct ethtool_rxnfc *cmd)
3877 {
3878         struct ethtool_rx_flow_spec *fsp =
3879                 (struct ethtool_rx_flow_spec *)&cmd->fs;
3880         struct i40e_pf *pf = vsi->back;
3881         int ret = 0;
3882
3883         if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
3884             test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
3885                 return -EBUSY;
3886
3887         if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
3888                 return -EBUSY;
3889
3890         ret = i40e_update_ethtool_fdir_entry(vsi, NULL, fsp->location, cmd);
3891
3892         i40e_prune_flex_pit_list(pf);
3893
3894         i40e_fdir_check_and_reenable(pf);
3895         return ret;
3896 }
3897
3898 /**
3899  * i40e_unused_pit_index - Find an unused PIT index for given list
3900  * @pf: the PF data structure
3901  *
3902  * Find the first unused flexible PIT index entry. We search both the L3 and
3903  * L4 flexible PIT lists so that the returned index is unique and unused by
3904  * either currently programmed L3 or L4 filters. We use a bit field as storage
3905  * to track which indexes are already used.
3906  **/
3907 static u8 i40e_unused_pit_index(struct i40e_pf *pf)
3908 {
3909         unsigned long available_index = 0xFF;
3910         struct i40e_flex_pit *entry;
3911
3912         /* We need to make sure that the new index isn't in use by either L3
3913          * or L4 filters so that IP_USER_FLOW filters can program both L3 and
3914          * L4 to use the same index.
3915          */
3916
3917         list_for_each_entry(entry, &pf->l4_flex_pit_list, list)
3918                 clear_bit(entry->pit_index, &available_index);
3919
3920         list_for_each_entry(entry, &pf->l3_flex_pit_list, list)
3921                 clear_bit(entry->pit_index, &available_index);
3922
3923         return find_first_bit(&available_index, 8);
3924 }
3925
3926 /**
3927  * i40e_find_flex_offset - Find an existing flex src_offset
3928  * @flex_pit_list: L3 or L4 flex PIT list
3929  * @src_offset: new src_offset to find
3930  *
3931  * Searches the flex_pit_list for an existing offset. If no offset is
3932  * currently programmed, then this will return an ERR_PTR if there is no space
3933  * to add a new offset, otherwise it returns NULL.
3934  **/
3935 static
3936 struct i40e_flex_pit *i40e_find_flex_offset(struct list_head *flex_pit_list,
3937                                             u16 src_offset)
3938 {
3939         struct i40e_flex_pit *entry;
3940         int size = 0;
3941
3942         /* Search for the src_offset first. If we find a matching entry
3943          * already programmed, we can simply re-use it.
3944          */
3945         list_for_each_entry(entry, flex_pit_list, list) {
3946                 size++;
3947                 if (entry->src_offset == src_offset)
3948                         return entry;
3949         }
3950
3951         /* If we haven't found an entry yet, then the provided src offset has
3952          * not yet been programmed. We will program the src offset later on,
3953          * but we need to indicate whether there is enough space to do so
3954          * here. We'll make use of ERR_PTR for this purpose.
3955          */
3956         if (size >= I40E_FLEX_PIT_TABLE_SIZE)
3957                 return ERR_PTR(-ENOSPC);
3958
3959         return NULL;
3960 }
3961
3962 /**
3963  * i40e_add_flex_offset - Add src_offset to flex PIT table list
3964  * @flex_pit_list: L3 or L4 flex PIT list
3965  * @src_offset: new src_offset to add
3966  * @pit_index: the PIT index to program
3967  *
3968  * This function programs the new src_offset to the list. It is expected that
3969  * i40e_find_flex_offset has already been tried and returned NULL, indicating
3970  * that this offset is not programmed, and that the list has enough space to
3971  * store another offset.
3972  *
3973  * Returns 0 on success, and negative value on error.
3974  **/
3975 static int i40e_add_flex_offset(struct list_head *flex_pit_list,
3976                                 u16 src_offset,
3977                                 u8 pit_index)
3978 {
3979         struct i40e_flex_pit *new_pit, *entry;
3980
3981         new_pit = kzalloc(sizeof(*entry), GFP_KERNEL);
3982         if (!new_pit)
3983                 return -ENOMEM;
3984
3985         new_pit->src_offset = src_offset;
3986         new_pit->pit_index = pit_index;
3987
3988         /* We need to insert this item such that the list is sorted by
3989          * src_offset in ascending order.
3990          */
3991         list_for_each_entry(entry, flex_pit_list, list) {
3992                 if (new_pit->src_offset < entry->src_offset) {
3993                         list_add_tail(&new_pit->list, &entry->list);
3994                         return 0;
3995                 }
3996
3997                 /* If we found an entry with our offset already programmed we
3998                  * can simply return here, after freeing the memory. However,
3999                  * if the pit_index does not match we need to report an error.
4000                  */
4001                 if (new_pit->src_offset == entry->src_offset) {
4002                         int err = 0;
4003
4004                         /* If the PIT index is not the same we can't re-use
4005                          * the entry, so we must report an error.
4006                          */
4007                         if (new_pit->pit_index != entry->pit_index)
4008                                 err = -EINVAL;
4009
4010                         kfree(new_pit);
4011                         return err;
4012                 }
4013         }
4014
4015         /* If we reached here, then we haven't yet added the item. This means
4016          * that we should add the item at the end of the list.
4017          */
4018         list_add_tail(&new_pit->list, flex_pit_list);
4019         return 0;
4020 }
4021
4022 /**
4023  * __i40e_reprogram_flex_pit - Re-program specific FLX_PIT table
4024  * @pf: Pointer to the PF structure
4025  * @flex_pit_list: list of flexible src offsets in use
4026  * @flex_pit_start: index to first entry for this section of the table
4027  *
4028  * In order to handle flexible data, the hardware uses a table of values
4029  * called the FLX_PIT table. This table is used to indicate which sections of
4030  * the input correspond to what PIT index values. Unfortunately, hardware is
4031  * very restrictive about programming this table. Entries must be ordered by
4032  * src_offset in ascending order, without duplicates. Additionally, unused
4033  * entries must be set to the unused index value, and must have valid size and
4034  * length according to the src_offset ordering.
4035  *
4036  * This function will reprogram the FLX_PIT register from a book-keeping
4037  * structure that we guarantee is already ordered correctly, and has no more
4038  * than 3 entries.
4039  *
4040  * To make things easier, we only support flexible values of one word length,
4041  * rather than allowing variable length flexible values.
4042  **/
4043 static void __i40e_reprogram_flex_pit(struct i40e_pf *pf,
4044                                       struct list_head *flex_pit_list,
4045                                       int flex_pit_start)
4046 {
4047         struct i40e_flex_pit *entry = NULL;
4048         u16 last_offset = 0;
4049         int i = 0, j = 0;
4050
4051         /* First, loop over the list of flex PIT entries, and reprogram the
4052          * registers.
4053          */
4054         list_for_each_entry(entry, flex_pit_list, list) {
4055                 /* We have to be careful when programming values for the
4056                  * largest SRC_OFFSET value. It is possible that adding
4057                  * additional empty values at the end would overflow the space
4058                  * for the SRC_OFFSET in the FLX_PIT register. To avoid this,
4059                  * we check here and add the empty values prior to adding the
4060                  * largest value.
4061                  *
4062                  * To determine this, we will use a loop from i+1 to 3, which
4063                  * will determine whether the unused entries would have valid
4064                  * SRC_OFFSET. Note that there cannot be extra entries past
4065                  * this value, because the only valid values would have been
4066                  * larger than I40E_MAX_FLEX_SRC_OFFSET, and thus would not
4067                  * have been added to the list in the first place.
4068                  */
4069                 for (j = i + 1; j < 3; j++) {
4070                         u16 offset = entry->src_offset + j;
4071                         int index = flex_pit_start + i;
4072                         u32 value = I40E_FLEX_PREP_VAL(I40E_FLEX_DEST_UNUSED,
4073                                                        1,
4074                                                        offset - 3);
4075
4076                         if (offset > I40E_MAX_FLEX_SRC_OFFSET) {
4077                                 i40e_write_rx_ctl(&pf->hw,
4078                                                   I40E_PRTQF_FLX_PIT(index),
4079                                                   value);
4080                                 i++;
4081                         }
4082                 }
4083
4084                 /* Now, we can program the actual value into the table */
4085                 i40e_write_rx_ctl(&pf->hw,
4086                                   I40E_PRTQF_FLX_PIT(flex_pit_start + i),
4087                                   I40E_FLEX_PREP_VAL(entry->pit_index + 50,
4088                                                      1,
4089                                                      entry->src_offset));
4090                 i++;
4091         }
4092
4093         /* In order to program the last entries in the table, we need to
4094          * determine the valid offset. If the list is empty, we'll just start
4095          * with 0. Otherwise, we'll start with the last item offset and add 1.
4096          * This ensures that all entries have valid sizes. If we don't do this
4097          * correctly, the hardware will disable flexible field parsing.
4098          */
4099         if (!list_empty(flex_pit_list))
4100                 last_offset = list_prev_entry(entry, list)->src_offset + 1;
4101
4102         for (; i < 3; i++, last_offset++) {
4103                 i40e_write_rx_ctl(&pf->hw,
4104                                   I40E_PRTQF_FLX_PIT(flex_pit_start + i),
4105                                   I40E_FLEX_PREP_VAL(I40E_FLEX_DEST_UNUSED,
4106                                                      1,
4107                                                      last_offset));
4108         }
4109 }
4110
4111 /**
4112  * i40e_reprogram_flex_pit - Reprogram all FLX_PIT tables after input set change
4113  * @pf: pointer to the PF structure
4114  *
4115  * This function reprograms both the L3 and L4 FLX_PIT tables. See the
4116  * internal helper function for implementation details.
4117  **/
4118 static void i40e_reprogram_flex_pit(struct i40e_pf *pf)
4119 {
4120         __i40e_reprogram_flex_pit(pf, &pf->l3_flex_pit_list,
4121                                   I40E_FLEX_PIT_IDX_START_L3);
4122
4123         __i40e_reprogram_flex_pit(pf, &pf->l4_flex_pit_list,
4124                                   I40E_FLEX_PIT_IDX_START_L4);
4125
4126         /* We also need to program the L3 and L4 GLQF ORT register */
4127         i40e_write_rx_ctl(&pf->hw,
4128                           I40E_GLQF_ORT(I40E_L3_GLQF_ORT_IDX),
4129                           I40E_ORT_PREP_VAL(I40E_FLEX_PIT_IDX_START_L3,
4130                                             3, 1));
4131
4132         i40e_write_rx_ctl(&pf->hw,
4133                           I40E_GLQF_ORT(I40E_L4_GLQF_ORT_IDX),
4134                           I40E_ORT_PREP_VAL(I40E_FLEX_PIT_IDX_START_L4,
4135                                             3, 1));
4136 }
4137
4138 /**
4139  * i40e_flow_str - Converts a flow_type into a human readable string
4140  * @fsp: the flow specification
4141  *
4142  * Currently only flow types we support are included here, and the string
4143  * value attempts to match what ethtool would use to configure this flow type.
4144  **/
4145 static const char *i40e_flow_str(struct ethtool_rx_flow_spec *fsp)
4146 {
4147         switch (fsp->flow_type & ~FLOW_EXT) {
4148         case TCP_V4_FLOW:
4149                 return "tcp4";
4150         case UDP_V4_FLOW:
4151                 return "udp4";
4152         case SCTP_V4_FLOW:
4153                 return "sctp4";
4154         case IP_USER_FLOW:
4155                 return "ip4";
4156         case TCP_V6_FLOW:
4157                 return "tcp6";
4158         case UDP_V6_FLOW:
4159                 return "udp6";
4160         case SCTP_V6_FLOW:
4161                 return "sctp6";
4162         case IPV6_USER_FLOW:
4163                 return "ip6";
4164         default:
4165                 return "unknown";
4166         }
4167 }
4168
4169 /**
4170  * i40e_pit_index_to_mask - Return the FLEX mask for a given PIT index
4171  * @pit_index: PIT index to convert
4172  *
4173  * Returns the mask for a given PIT index. Will return 0 if the pit_index is
4174  * of range.
4175  **/
4176 static u64 i40e_pit_index_to_mask(int pit_index)
4177 {
4178         switch (pit_index) {
4179         case 0:
4180                 return I40E_FLEX_50_MASK;
4181         case 1:
4182                 return I40E_FLEX_51_MASK;
4183         case 2:
4184                 return I40E_FLEX_52_MASK;
4185         case 3:
4186                 return I40E_FLEX_53_MASK;
4187         case 4:
4188                 return I40E_FLEX_54_MASK;
4189         case 5:
4190                 return I40E_FLEX_55_MASK;
4191         case 6:
4192                 return I40E_FLEX_56_MASK;
4193         case 7:
4194                 return I40E_FLEX_57_MASK;
4195         default:
4196                 return 0;
4197         }
4198 }
4199
4200 /**
4201  * i40e_print_input_set - Show changes between two input sets
4202  * @vsi: the vsi being configured
4203  * @old: the old input set
4204  * @new: the new input set
4205  *
4206  * Print the difference between old and new input sets by showing which series
4207  * of words are toggled on or off. Only displays the bits we actually support
4208  * changing.
4209  **/
4210 static void i40e_print_input_set(struct i40e_vsi *vsi, u64 old, u64 new)
4211 {
4212         struct i40e_pf *pf = vsi->back;
4213         bool old_value, new_value;
4214         int i;
4215
4216         old_value = !!(old & I40E_L3_SRC_MASK);
4217         new_value = !!(new & I40E_L3_SRC_MASK);
4218         if (old_value != new_value)
4219                 netif_info(pf, drv, vsi->netdev, "L3 source address: %s -> %s\n",
4220                            old_value ? "ON" : "OFF",
4221                            new_value ? "ON" : "OFF");
4222
4223         old_value = !!(old & I40E_L3_DST_MASK);
4224         new_value = !!(new & I40E_L3_DST_MASK);
4225         if (old_value != new_value)
4226                 netif_info(pf, drv, vsi->netdev, "L3 destination address: %s -> %s\n",
4227                            old_value ? "ON" : "OFF",
4228                            new_value ? "ON" : "OFF");
4229
4230         old_value = !!(old & I40E_L4_SRC_MASK);
4231         new_value = !!(new & I40E_L4_SRC_MASK);
4232         if (old_value != new_value)
4233                 netif_info(pf, drv, vsi->netdev, "L4 source port: %s -> %s\n",
4234                            old_value ? "ON" : "OFF",
4235                            new_value ? "ON" : "OFF");
4236
4237         old_value = !!(old & I40E_L4_DST_MASK);
4238         new_value = !!(new & I40E_L4_DST_MASK);
4239         if (old_value != new_value)
4240                 netif_info(pf, drv, vsi->netdev, "L4 destination port: %s -> %s\n",
4241                            old_value ? "ON" : "OFF",
4242                            new_value ? "ON" : "OFF");
4243
4244         old_value = !!(old & I40E_VERIFY_TAG_MASK);
4245         new_value = !!(new & I40E_VERIFY_TAG_MASK);
4246         if (old_value != new_value)
4247                 netif_info(pf, drv, vsi->netdev, "SCTP verification tag: %s -> %s\n",
4248                            old_value ? "ON" : "OFF",
4249                            new_value ? "ON" : "OFF");
4250
4251         /* Show change of flexible filter entries */
4252         for (i = 0; i < I40E_FLEX_INDEX_ENTRIES; i++) {
4253                 u64 flex_mask = i40e_pit_index_to_mask(i);
4254
4255                 old_value = !!(old & flex_mask);
4256                 new_value = !!(new & flex_mask);
4257                 if (old_value != new_value)
4258                         netif_info(pf, drv, vsi->netdev, "FLEX index %d: %s -> %s\n",
4259                                    i,
4260                                    old_value ? "ON" : "OFF",
4261                                    new_value ? "ON" : "OFF");
4262         }
4263
4264         netif_info(pf, drv, vsi->netdev, "  Current input set: %0llx\n",
4265                    old);
4266         netif_info(pf, drv, vsi->netdev, "Requested input set: %0llx\n",
4267                    new);
4268 }
4269
4270 /**
4271  * i40e_check_fdir_input_set - Check that a given rx_flow_spec mask is valid
4272  * @vsi: pointer to the targeted VSI
4273  * @fsp: pointer to Rx flow specification
4274  * @userdef: userdefined data from flow specification
4275  *
4276  * Ensures that a given ethtool_rx_flow_spec has a valid mask. Some support
4277  * for partial matches exists with a few limitations. First, hardware only
4278  * supports masking by word boundary (2 bytes) and not per individual bit.
4279  * Second, hardware is limited to using one mask for a flow type and cannot
4280  * use a separate mask for each filter.
4281  *
4282  * To support these limitations, if we already have a configured filter for
4283  * the specified type, this function enforces that new filters of the type
4284  * match the configured input set. Otherwise, if we do not have a filter of
4285  * the specified type, we allow the input set to be updated to match the
4286  * desired filter.
4287  *
4288  * To help ensure that administrators understand why filters weren't displayed
4289  * as supported, we print a diagnostic message displaying how the input set
4290  * would change and warning to delete the preexisting filters if required.
4291  *
4292  * Returns 0 on successful input set match, and a negative return code on
4293  * failure.
4294  **/
4295 static int i40e_check_fdir_input_set(struct i40e_vsi *vsi,
4296                                      struct ethtool_rx_flow_spec *fsp,
4297                                      struct i40e_rx_flow_userdef *userdef)
4298 {
4299         static const __be32 ipv6_full_mask[4] = {cpu_to_be32(0xffffffff),
4300                 cpu_to_be32(0xffffffff), cpu_to_be32(0xffffffff),
4301                 cpu_to_be32(0xffffffff)};
4302         struct ethtool_tcpip6_spec *tcp_ip6_spec;
4303         struct ethtool_usrip6_spec *usr_ip6_spec;
4304         struct ethtool_tcpip4_spec *tcp_ip4_spec;
4305         struct ethtool_usrip4_spec *usr_ip4_spec;
4306         struct i40e_pf *pf = vsi->back;
4307         u64 current_mask, new_mask;
4308         bool new_flex_offset = false;
4309         bool flex_l3 = false;
4310         u16 *fdir_filter_count;
4311         u16 index, src_offset = 0;
4312         u8 pit_index = 0;
4313         int err;
4314
4315         switch (fsp->flow_type & ~FLOW_EXT) {
4316         case SCTP_V4_FLOW:
4317                 index = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
4318                 fdir_filter_count = &pf->fd_sctp4_filter_cnt;
4319                 break;
4320         case TCP_V4_FLOW:
4321                 index = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
4322                 fdir_filter_count = &pf->fd_tcp4_filter_cnt;
4323                 break;
4324         case UDP_V4_FLOW:
4325                 index = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
4326                 fdir_filter_count = &pf->fd_udp4_filter_cnt;
4327                 break;
4328         case SCTP_V6_FLOW:
4329                 index = I40E_FILTER_PCTYPE_NONF_IPV6_SCTP;
4330                 fdir_filter_count = &pf->fd_sctp6_filter_cnt;
4331                 break;
4332         case TCP_V6_FLOW:
4333                 index = I40E_FILTER_PCTYPE_NONF_IPV6_TCP;
4334                 fdir_filter_count = &pf->fd_tcp6_filter_cnt;
4335                 break;
4336         case UDP_V6_FLOW:
4337                 index = I40E_FILTER_PCTYPE_NONF_IPV6_UDP;
4338                 fdir_filter_count = &pf->fd_udp6_filter_cnt;
4339                 break;
4340         case IP_USER_FLOW:
4341                 index = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
4342                 fdir_filter_count = &pf->fd_ip4_filter_cnt;
4343                 flex_l3 = true;
4344                 break;
4345         case IPV6_USER_FLOW:
4346                 index = I40E_FILTER_PCTYPE_NONF_IPV6_OTHER;
4347                 fdir_filter_count = &pf->fd_ip6_filter_cnt;
4348                 flex_l3 = true;
4349                 break;
4350         default:
4351                 return -EOPNOTSUPP;
4352         }
4353
4354         /* Read the current input set from register memory. */
4355         current_mask = i40e_read_fd_input_set(pf, index);
4356         new_mask = current_mask;
4357
4358         /* Determine, if any, the required changes to the input set in order
4359          * to support the provided mask.
4360          *
4361          * Hardware only supports masking at word (2 byte) granularity and does
4362          * not support full bitwise masking. This implementation simplifies
4363          * even further and only supports fully enabled or fully disabled
4364          * masks for each field, even though we could split the ip4src and
4365          * ip4dst fields.
4366          */
4367         switch (fsp->flow_type & ~FLOW_EXT) {
4368         case SCTP_V4_FLOW:
4369                 new_mask &= ~I40E_VERIFY_TAG_MASK;
4370                 fallthrough;
4371         case TCP_V4_FLOW:
4372         case UDP_V4_FLOW:
4373                 tcp_ip4_spec = &fsp->m_u.tcp_ip4_spec;
4374
4375                 /* IPv4 source address */
4376                 if (tcp_ip4_spec->ip4src == htonl(0xFFFFFFFF))
4377                         new_mask |= I40E_L3_SRC_MASK;
4378                 else if (!tcp_ip4_spec->ip4src)
4379                         new_mask &= ~I40E_L3_SRC_MASK;
4380                 else
4381                         return -EOPNOTSUPP;
4382
4383                 /* IPv4 destination address */
4384                 if (tcp_ip4_spec->ip4dst == htonl(0xFFFFFFFF))
4385                         new_mask |= I40E_L3_DST_MASK;
4386                 else if (!tcp_ip4_spec->ip4dst)
4387                         new_mask &= ~I40E_L3_DST_MASK;
4388                 else
4389                         return -EOPNOTSUPP;
4390
4391                 /* L4 source port */
4392                 if (tcp_ip4_spec->psrc == htons(0xFFFF))
4393                         new_mask |= I40E_L4_SRC_MASK;
4394                 else if (!tcp_ip4_spec->psrc)
4395                         new_mask &= ~I40E_L4_SRC_MASK;
4396                 else
4397                         return -EOPNOTSUPP;
4398
4399                 /* L4 destination port */
4400                 if (tcp_ip4_spec->pdst == htons(0xFFFF))
4401                         new_mask |= I40E_L4_DST_MASK;
4402                 else if (!tcp_ip4_spec->pdst)
4403                         new_mask &= ~I40E_L4_DST_MASK;
4404                 else
4405                         return -EOPNOTSUPP;
4406
4407                 /* Filtering on Type of Service is not supported. */
4408                 if (tcp_ip4_spec->tos)
4409                         return -EOPNOTSUPP;
4410
4411                 break;
4412         case SCTP_V6_FLOW:
4413                 new_mask &= ~I40E_VERIFY_TAG_MASK;
4414                 fallthrough;
4415         case TCP_V6_FLOW:
4416         case UDP_V6_FLOW:
4417                 tcp_ip6_spec = &fsp->m_u.tcp_ip6_spec;
4418
4419                 /* Check if user provided IPv6 source address. */
4420                 if (ipv6_addr_equal((struct in6_addr *)&tcp_ip6_spec->ip6src,
4421                                     (struct in6_addr *)&ipv6_full_mask))
4422                         new_mask |= I40E_L3_V6_SRC_MASK;
4423                 else if (ipv6_addr_any((struct in6_addr *)
4424                                        &tcp_ip6_spec->ip6src))
4425                         new_mask &= ~I40E_L3_V6_SRC_MASK;
4426                 else
4427                         return -EOPNOTSUPP;
4428
4429                 /* Check if user provided destination address. */
4430                 if (ipv6_addr_equal((struct in6_addr *)&tcp_ip6_spec->ip6dst,
4431                                     (struct in6_addr *)&ipv6_full_mask))
4432                         new_mask |= I40E_L3_V6_DST_MASK;
4433                 else if (ipv6_addr_any((struct in6_addr *)
4434                                        &tcp_ip6_spec->ip6dst))
4435                         new_mask &= ~I40E_L3_V6_DST_MASK;
4436                 else
4437                         return -EOPNOTSUPP;
4438
4439                 /* L4 source port */
4440                 if (tcp_ip6_spec->psrc == htons(0xFFFF))
4441                         new_mask |= I40E_L4_SRC_MASK;
4442                 else if (!tcp_ip6_spec->psrc)
4443                         new_mask &= ~I40E_L4_SRC_MASK;
4444                 else
4445                         return -EOPNOTSUPP;
4446
4447                 /* L4 destination port */
4448                 if (tcp_ip6_spec->pdst == htons(0xFFFF))
4449                         new_mask |= I40E_L4_DST_MASK;
4450                 else if (!tcp_ip6_spec->pdst)
4451                         new_mask &= ~I40E_L4_DST_MASK;
4452                 else
4453                         return -EOPNOTSUPP;
4454
4455                 /* Filtering on Traffic Classes is not supported. */
4456                 if (tcp_ip6_spec->tclass)
4457                         return -EOPNOTSUPP;
4458                 break;
4459         case IP_USER_FLOW:
4460                 usr_ip4_spec = &fsp->m_u.usr_ip4_spec;
4461
4462                 /* IPv4 source address */
4463                 if (usr_ip4_spec->ip4src == htonl(0xFFFFFFFF))
4464                         new_mask |= I40E_L3_SRC_MASK;
4465                 else if (!usr_ip4_spec->ip4src)
4466                         new_mask &= ~I40E_L3_SRC_MASK;
4467                 else
4468                         return -EOPNOTSUPP;
4469
4470                 /* IPv4 destination address */
4471                 if (usr_ip4_spec->ip4dst == htonl(0xFFFFFFFF))
4472                         new_mask |= I40E_L3_DST_MASK;
4473                 else if (!usr_ip4_spec->ip4dst)
4474                         new_mask &= ~I40E_L3_DST_MASK;
4475                 else
4476                         return -EOPNOTSUPP;
4477
4478                 /* First 4 bytes of L4 header */
4479                 if (usr_ip4_spec->l4_4_bytes)
4480                         return -EOPNOTSUPP;
4481
4482                 /* Filtering on Type of Service is not supported. */
4483                 if (usr_ip4_spec->tos)
4484                         return -EOPNOTSUPP;
4485
4486                 /* Filtering on IP version is not supported */
4487                 if (usr_ip4_spec->ip_ver)
4488                         return -EINVAL;
4489
4490                 /* Filtering on L4 protocol is not supported */
4491                 if (usr_ip4_spec->proto)
4492                         return -EINVAL;
4493
4494                 break;
4495         case IPV6_USER_FLOW:
4496                 usr_ip6_spec = &fsp->m_u.usr_ip6_spec;
4497
4498                 /* Check if user provided IPv6 source address. */
4499                 if (ipv6_addr_equal((struct in6_addr *)&usr_ip6_spec->ip6src,
4500                                     (struct in6_addr *)&ipv6_full_mask))
4501                         new_mask |= I40E_L3_V6_SRC_MASK;
4502                 else if (ipv6_addr_any((struct in6_addr *)
4503                                        &usr_ip6_spec->ip6src))
4504                         new_mask &= ~I40E_L3_V6_SRC_MASK;
4505                 else
4506                         return -EOPNOTSUPP;
4507
4508                 /* Check if user provided destination address. */
4509                 if (ipv6_addr_equal((struct in6_addr *)&usr_ip6_spec->ip6dst,
4510                                     (struct in6_addr *)&ipv6_full_mask))
4511                         new_mask |= I40E_L3_V6_DST_MASK;
4512                 else if (ipv6_addr_any((struct in6_addr *)
4513                                        &usr_ip6_spec->ip6dst))
4514                         new_mask &= ~I40E_L3_V6_DST_MASK;
4515                 else
4516                         return -EOPNOTSUPP;
4517
4518                 if (usr_ip6_spec->l4_4_bytes)
4519                         return -EOPNOTSUPP;
4520
4521                 /* Filtering on Traffic class is not supported. */
4522                 if (usr_ip6_spec->tclass)
4523                         return -EOPNOTSUPP;
4524
4525                 /* Filtering on L4 protocol is not supported */
4526                 if (usr_ip6_spec->l4_proto)
4527                         return -EINVAL;
4528
4529                 break;
4530         default:
4531                 return -EOPNOTSUPP;
4532         }
4533
4534         if (fsp->flow_type & FLOW_EXT) {
4535                 /* Allow only 802.1Q and no etype defined, as
4536                  * later it's modified to 0x8100
4537                  */
4538                 if (fsp->h_ext.vlan_etype != htons(ETH_P_8021Q) &&
4539                     fsp->h_ext.vlan_etype != 0)
4540                         return -EOPNOTSUPP;
4541                 if (fsp->m_ext.vlan_tci == htons(0xFFFF))
4542                         new_mask |= I40E_VLAN_SRC_MASK;
4543                 else
4544                         new_mask &= ~I40E_VLAN_SRC_MASK;
4545         }
4546
4547         /* First, clear all flexible filter entries */
4548         new_mask &= ~I40E_FLEX_INPUT_MASK;
4549
4550         /* If we have a flexible filter, try to add this offset to the correct
4551          * flexible filter PIT list. Once finished, we can update the mask.
4552          * If the src_offset changed, we will get a new mask value which will
4553          * trigger an input set change.
4554          */
4555         if (userdef->flex_filter) {
4556                 struct i40e_flex_pit *l3_flex_pit = NULL, *flex_pit = NULL;
4557
4558                 /* Flexible offset must be even, since the flexible payload
4559                  * must be aligned on 2-byte boundary.
4560                  */
4561                 if (userdef->flex_offset & 0x1) {
4562                         dev_warn(&pf->pdev->dev,
4563                                  "Flexible data offset must be 2-byte aligned\n");
4564                         return -EINVAL;
4565                 }
4566
4567                 src_offset = userdef->flex_offset >> 1;
4568
4569                 /* FLX_PIT source offset value is only so large */
4570                 if (src_offset > I40E_MAX_FLEX_SRC_OFFSET) {
4571                         dev_warn(&pf->pdev->dev,
4572                                  "Flexible data must reside within first 64 bytes of the packet payload\n");
4573                         return -EINVAL;
4574                 }
4575
4576                 /* See if this offset has already been programmed. If we get
4577                  * an ERR_PTR, then the filter is not safe to add. Otherwise,
4578                  * if we get a NULL pointer, this means we will need to add
4579                  * the offset.
4580                  */
4581                 flex_pit = i40e_find_flex_offset(&pf->l4_flex_pit_list,
4582                                                  src_offset);
4583                 if (IS_ERR(flex_pit))
4584                         return PTR_ERR(flex_pit);
4585
4586                 /* IP_USER_FLOW filters match both L4 (ICMP) and L3 (unknown)
4587                  * packet types, and thus we need to program both L3 and L4
4588                  * flexible values. These must have identical flexible index,
4589                  * as otherwise we can't correctly program the input set. So
4590                  * we'll find both an L3 and L4 index and make sure they are
4591                  * the same.
4592                  */
4593                 if (flex_l3) {
4594                         l3_flex_pit =
4595                                 i40e_find_flex_offset(&pf->l3_flex_pit_list,
4596                                                       src_offset);
4597                         if (IS_ERR(l3_flex_pit))
4598                                 return PTR_ERR(l3_flex_pit);
4599
4600                         if (flex_pit) {
4601                                 /* If we already had a matching L4 entry, we
4602                                  * need to make sure that the L3 entry we
4603                                  * obtained uses the same index.
4604                                  */
4605                                 if (l3_flex_pit) {
4606                                         if (l3_flex_pit->pit_index !=
4607                                             flex_pit->pit_index) {
4608                                                 return -EINVAL;
4609                                         }
4610                                 } else {
4611                                         new_flex_offset = true;
4612                                 }
4613                         } else {
4614                                 flex_pit = l3_flex_pit;
4615                         }
4616                 }
4617
4618                 /* If we didn't find an existing flex offset, we need to
4619                  * program a new one. However, we don't immediately program it
4620                  * here because we will wait to program until after we check
4621                  * that it is safe to change the input set.
4622                  */
4623                 if (!flex_pit) {
4624                         new_flex_offset = true;
4625                         pit_index = i40e_unused_pit_index(pf);
4626                 } else {
4627                         pit_index = flex_pit->pit_index;
4628                 }
4629
4630                 /* Update the mask with the new offset */
4631                 new_mask |= i40e_pit_index_to_mask(pit_index);
4632         }
4633
4634         /* If the mask and flexible filter offsets for this filter match the
4635          * currently programmed values we don't need any input set change, so
4636          * this filter is safe to install.
4637          */
4638         if (new_mask == current_mask && !new_flex_offset)
4639                 return 0;
4640
4641         netif_info(pf, drv, vsi->netdev, "Input set change requested for %s flows:\n",
4642                    i40e_flow_str(fsp));
4643         i40e_print_input_set(vsi, current_mask, new_mask);
4644         if (new_flex_offset) {
4645                 netif_info(pf, drv, vsi->netdev, "FLEX index %d: Offset -> %d",
4646                            pit_index, src_offset);
4647         }
4648
4649         /* Hardware input sets are global across multiple ports, so even the
4650          * main port cannot change them when in MFP mode as this would impact
4651          * any filters on the other ports.
4652          */
4653         if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
4654                 netif_err(pf, drv, vsi->netdev, "Cannot change Flow Director input sets while MFP is enabled\n");
4655                 return -EOPNOTSUPP;
4656         }
4657
4658         /* This filter requires us to update the input set. However, hardware
4659          * only supports one input set per flow type, and does not support
4660          * separate masks for each filter. This means that we can only support
4661          * a single mask for all filters of a specific type.
4662          *
4663          * If we have preexisting filters, they obviously depend on the
4664          * current programmed input set. Display a diagnostic message in this
4665          * case explaining why the filter could not be accepted.
4666          */
4667         if (*fdir_filter_count) {
4668                 netif_err(pf, drv, vsi->netdev, "Cannot change input set for %s flows until %d preexisting filters are removed\n",
4669                           i40e_flow_str(fsp),
4670                           *fdir_filter_count);
4671                 return -EOPNOTSUPP;
4672         }
4673
4674         i40e_write_fd_input_set(pf, index, new_mask);
4675
4676         /* IP_USER_FLOW filters match both IPv4/Other and IPv4/Fragmented
4677          * frames. If we're programming the input set for IPv4/Other, we also
4678          * need to program the IPv4/Fragmented input set. Since we don't have
4679          * separate support, we'll always assume and enforce that the two flow
4680          * types must have matching input sets.
4681          */
4682         if (index == I40E_FILTER_PCTYPE_NONF_IPV4_OTHER)
4683                 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
4684                                         new_mask);
4685
4686         /* Add the new offset and update table, if necessary */
4687         if (new_flex_offset) {
4688                 err = i40e_add_flex_offset(&pf->l4_flex_pit_list, src_offset,
4689                                            pit_index);
4690                 if (err)
4691                         return err;
4692
4693                 if (flex_l3) {
4694                         err = i40e_add_flex_offset(&pf->l3_flex_pit_list,
4695                                                    src_offset,
4696                                                    pit_index);
4697                         if (err)
4698                                 return err;
4699                 }
4700
4701                 i40e_reprogram_flex_pit(pf);
4702         }
4703
4704         return 0;
4705 }
4706
4707 /**
4708  * i40e_match_fdir_filter - Return true of two filters match
4709  * @a: pointer to filter struct
4710  * @b: pointer to filter struct
4711  *
4712  * Returns true if the two filters match exactly the same criteria. I.e. they
4713  * match the same flow type and have the same parameters. We don't need to
4714  * check any input-set since all filters of the same flow type must use the
4715  * same input set.
4716  **/
4717 static bool i40e_match_fdir_filter(struct i40e_fdir_filter *a,
4718                                    struct i40e_fdir_filter *b)
4719 {
4720         /* The filters do not much if any of these criteria differ. */
4721         if (a->dst_ip != b->dst_ip ||
4722             a->src_ip != b->src_ip ||
4723             a->dst_port != b->dst_port ||
4724             a->src_port != b->src_port ||
4725             a->flow_type != b->flow_type ||
4726             a->ipl4_proto != b->ipl4_proto ||
4727             a->vlan_tag != b->vlan_tag ||
4728             a->vlan_etype != b->vlan_etype)
4729                 return false;
4730
4731         return true;
4732 }
4733
4734 /**
4735  * i40e_disallow_matching_filters - Check that new filters differ
4736  * @vsi: pointer to the targeted VSI
4737  * @input: new filter to check
4738  *
4739  * Due to hardware limitations, it is not possible for two filters that match
4740  * similar criteria to be programmed at the same time. This is true for a few
4741  * reasons:
4742  *
4743  * (a) all filters matching a particular flow type must use the same input
4744  * set, that is they must match the same criteria.
4745  * (b) different flow types will never match the same packet, as the flow type
4746  * is decided by hardware before checking which rules apply.
4747  * (c) hardware has no way to distinguish which order filters apply in.
4748  *
4749  * Due to this, we can't really support using the location data to order
4750  * filters in the hardware parsing. It is technically possible for the user to
4751  * request two filters matching the same criteria but which select different
4752  * queues. In this case, rather than keep both filters in the list, we reject
4753  * the 2nd filter when the user requests adding it.
4754  *
4755  * This avoids needing to track location for programming the filter to
4756  * hardware, and ensures that we avoid some strange scenarios involving
4757  * deleting filters which match the same criteria.
4758  **/
4759 static int i40e_disallow_matching_filters(struct i40e_vsi *vsi,
4760                                           struct i40e_fdir_filter *input)
4761 {
4762         struct i40e_pf *pf = vsi->back;
4763         struct i40e_fdir_filter *rule;
4764         struct hlist_node *node2;
4765
4766         /* Loop through every filter, and check that it doesn't match */
4767         hlist_for_each_entry_safe(rule, node2,
4768                                   &pf->fdir_filter_list, fdir_node) {
4769                 /* Don't check the filters match if they share the same fd_id,
4770                  * since the new filter is actually just updating the target
4771                  * of the old filter.
4772                  */
4773                 if (rule->fd_id == input->fd_id)
4774                         continue;
4775
4776                 /* If any filters match, then print a warning message to the
4777                  * kernel message buffer and bail out.
4778                  */
4779                 if (i40e_match_fdir_filter(rule, input)) {
4780                         dev_warn(&pf->pdev->dev,
4781                                  "Existing user defined filter %d already matches this flow.\n",
4782                                  rule->fd_id);
4783                         return -EINVAL;
4784                 }
4785         }
4786
4787         return 0;
4788 }
4789
4790 /**
4791  * i40e_add_fdir_ethtool - Add/Remove Flow Director filters
4792  * @vsi: pointer to the targeted VSI
4793  * @cmd: command to get or set RX flow classification rules
4794  *
4795  * Add Flow Director filters for a specific flow spec based on their
4796  * protocol.  Returns 0 if the filters were successfully added.
4797  **/
4798 static int i40e_add_fdir_ethtool(struct i40e_vsi *vsi,
4799                                  struct ethtool_rxnfc *cmd)
4800 {
4801         struct i40e_rx_flow_userdef userdef;
4802         struct ethtool_rx_flow_spec *fsp;
4803         struct i40e_fdir_filter *input;
4804         u16 dest_vsi = 0, q_index = 0;
4805         struct i40e_pf *pf;
4806         int ret = -EINVAL;
4807         u8 dest_ctl;
4808
4809         if (!vsi)
4810                 return -EINVAL;
4811         pf = vsi->back;
4812
4813         if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
4814                 return -EOPNOTSUPP;
4815
4816         if (test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
4817                 return -ENOSPC;
4818
4819         if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
4820             test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
4821                 return -EBUSY;
4822
4823         if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
4824                 return -EBUSY;
4825
4826         fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
4827
4828         /* Parse the user-defined field */
4829         if (i40e_parse_rx_flow_user_data(fsp, &userdef))
4830                 return -EINVAL;
4831
4832         /* Extended MAC field is not supported */
4833         if (fsp->flow_type & FLOW_MAC_EXT)
4834                 return -EINVAL;
4835
4836         ret = i40e_check_fdir_input_set(vsi, fsp, &userdef);
4837         if (ret)
4838                 return ret;
4839
4840         if (fsp->location >= (pf->hw.func_caps.fd_filters_best_effort +
4841                               pf->hw.func_caps.fd_filters_guaranteed)) {
4842                 return -EINVAL;
4843         }
4844
4845         /* ring_cookie is either the drop index, or is a mask of the queue
4846          * index and VF id we wish to target.
4847          */
4848         if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
4849                 dest_ctl = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
4850         } else {
4851                 u32 ring = ethtool_get_flow_spec_ring(fsp->ring_cookie);
4852                 u8 vf = ethtool_get_flow_spec_ring_vf(fsp->ring_cookie);
4853
4854                 if (!vf) {
4855                         if (ring >= vsi->num_queue_pairs)
4856                                 return -EINVAL;
4857                         dest_vsi = vsi->id;
4858                 } else {
4859                         /* VFs are zero-indexed, so we subtract one here */
4860                         vf--;
4861
4862                         if (vf >= pf->num_alloc_vfs)
4863                                 return -EINVAL;
4864                         if (ring >= pf->vf[vf].num_queue_pairs)
4865                                 return -EINVAL;
4866                         dest_vsi = pf->vf[vf].lan_vsi_id;
4867                 }
4868                 dest_ctl = I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX;
4869                 q_index = ring;
4870         }
4871
4872         input = kzalloc(sizeof(*input), GFP_KERNEL);
4873
4874         if (!input)
4875                 return -ENOMEM;
4876
4877         input->fd_id = fsp->location;
4878         input->q_index = q_index;
4879         input->dest_vsi = dest_vsi;
4880         input->dest_ctl = dest_ctl;
4881         input->fd_status = I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID;
4882         input->cnt_index  = I40E_FD_SB_STAT_IDX(pf->hw.pf_id);
4883         input->dst_ip = fsp->h_u.tcp_ip4_spec.ip4src;
4884         input->src_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
4885         input->flow_type = fsp->flow_type & ~FLOW_EXT;
4886
4887         input->vlan_etype = fsp->h_ext.vlan_etype;
4888         if (!fsp->m_ext.vlan_etype && fsp->h_ext.vlan_tci)
4889                 input->vlan_etype = cpu_to_be16(ETH_P_8021Q);
4890         if (fsp->m_ext.vlan_tci && input->vlan_etype)
4891                 input->vlan_tag = fsp->h_ext.vlan_tci;
4892         if (input->flow_type == IPV6_USER_FLOW ||
4893             input->flow_type == UDP_V6_FLOW ||
4894             input->flow_type == TCP_V6_FLOW ||
4895             input->flow_type == SCTP_V6_FLOW) {
4896                 /* Reverse the src and dest notion, since the HW expects them
4897                  * to be from Tx perspective where as the input from user is
4898                  * from Rx filter view.
4899                  */
4900                 input->ipl4_proto = fsp->h_u.usr_ip6_spec.l4_proto;
4901                 input->dst_port = fsp->h_u.tcp_ip6_spec.psrc;
4902                 input->src_port = fsp->h_u.tcp_ip6_spec.pdst;
4903                 memcpy(input->dst_ip6, fsp->h_u.ah_ip6_spec.ip6src,
4904                        sizeof(__be32) * 4);
4905                 memcpy(input->src_ip6, fsp->h_u.ah_ip6_spec.ip6dst,
4906                        sizeof(__be32) * 4);
4907         } else {
4908                 /* Reverse the src and dest notion, since the HW expects them
4909                  * to be from Tx perspective where as the input from user is
4910                  * from Rx filter view.
4911                  */
4912                 input->ipl4_proto = fsp->h_u.usr_ip4_spec.proto;
4913                 input->dst_port = fsp->h_u.tcp_ip4_spec.psrc;
4914                 input->src_port = fsp->h_u.tcp_ip4_spec.pdst;
4915                 input->dst_ip = fsp->h_u.tcp_ip4_spec.ip4src;
4916                 input->src_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
4917         }
4918
4919         if (userdef.flex_filter) {
4920                 input->flex_filter = true;
4921                 input->flex_word = cpu_to_be16(userdef.flex_word);
4922                 input->flex_offset = userdef.flex_offset;
4923         }
4924
4925         /* Avoid programming two filters with identical match criteria. */
4926         ret = i40e_disallow_matching_filters(vsi, input);
4927         if (ret)
4928                 goto free_filter_memory;
4929
4930         /* Add the input filter to the fdir_input_list, possibly replacing
4931          * a previous filter. Do not free the input structure after adding it
4932          * to the list as this would cause a use-after-free bug.
4933          */
4934         i40e_update_ethtool_fdir_entry(vsi, input, fsp->location, NULL);
4935         ret = i40e_add_del_fdir(vsi, input, true);
4936         if (ret)
4937                 goto remove_sw_rule;
4938         return 0;
4939
4940 remove_sw_rule:
4941         hlist_del(&input->fdir_node);
4942         pf->fdir_pf_active_filters--;
4943 free_filter_memory:
4944         kfree(input);
4945         return ret;
4946 }
4947
4948 /**
4949  * i40e_set_rxnfc - command to set RX flow classification rules
4950  * @netdev: network interface device structure
4951  * @cmd: ethtool rxnfc command
4952  *
4953  * Returns Success if the command is supported.
4954  **/
4955 static int i40e_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
4956 {
4957         struct i40e_netdev_priv *np = netdev_priv(netdev);
4958         struct i40e_vsi *vsi = np->vsi;
4959         struct i40e_pf *pf = vsi->back;
4960         int ret = -EOPNOTSUPP;
4961
4962         switch (cmd->cmd) {
4963         case ETHTOOL_SRXFH:
4964                 ret = i40e_set_rss_hash_opt(pf, cmd);
4965                 break;
4966         case ETHTOOL_SRXCLSRLINS:
4967                 ret = i40e_add_fdir_ethtool(vsi, cmd);
4968                 break;
4969         case ETHTOOL_SRXCLSRLDEL:
4970                 ret = i40e_del_fdir_entry(vsi, cmd);
4971                 break;
4972         default:
4973                 break;
4974         }
4975
4976         return ret;
4977 }
4978
4979 /**
4980  * i40e_max_channels - get Max number of combined channels supported
4981  * @vsi: vsi pointer
4982  **/
4983 static unsigned int i40e_max_channels(struct i40e_vsi *vsi)
4984 {
4985         /* TODO: This code assumes DCB and FD is disabled for now. */
4986         return vsi->alloc_queue_pairs;
4987 }
4988
4989 /**
4990  * i40e_get_channels - Get the current channels enabled and max supported etc.
4991  * @dev: network interface device structure
4992  * @ch: ethtool channels structure
4993  *
4994  * We don't support separate tx and rx queues as channels. The other count
4995  * represents how many queues are being used for control. max_combined counts
4996  * how many queue pairs we can support. They may not be mapped 1 to 1 with
4997  * q_vectors since we support a lot more queue pairs than q_vectors.
4998  **/
4999 static void i40e_get_channels(struct net_device *dev,
5000                               struct ethtool_channels *ch)
5001 {
5002         struct i40e_netdev_priv *np = netdev_priv(dev);
5003         struct i40e_vsi *vsi = np->vsi;
5004         struct i40e_pf *pf = vsi->back;
5005
5006         /* report maximum channels */
5007         ch->max_combined = i40e_max_channels(vsi);
5008
5009         /* report info for other vector */
5010         ch->other_count = test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ? 1 : 0;
5011         ch->max_other = ch->other_count;
5012
5013         /* Note: This code assumes DCB is disabled for now. */
5014         ch->combined_count = vsi->num_queue_pairs;
5015 }
5016
5017 /**
5018  * i40e_set_channels - Set the new channels count.
5019  * @dev: network interface device structure
5020  * @ch: ethtool channels structure
5021  *
5022  * The new channels count may not be the same as requested by the user
5023  * since it gets rounded down to a power of 2 value.
5024  **/
5025 static int i40e_set_channels(struct net_device *dev,
5026                              struct ethtool_channels *ch)
5027 {
5028         const u8 drop = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
5029         struct i40e_netdev_priv *np = netdev_priv(dev);
5030         unsigned int count = ch->combined_count;
5031         struct i40e_vsi *vsi = np->vsi;
5032         struct i40e_pf *pf = vsi->back;
5033         struct i40e_fdir_filter *rule;
5034         struct hlist_node *node2;
5035         int new_count;
5036         int err = 0;
5037
5038         /* We do not support setting channels for any other VSI at present */
5039         if (vsi->type != I40E_VSI_MAIN)
5040                 return -EINVAL;
5041
5042         /* We do not support setting channels via ethtool when TCs are
5043          * configured through mqprio
5044          */
5045         if (i40e_is_tc_mqprio_enabled(pf))
5046                 return -EINVAL;
5047
5048         /* verify they are not requesting separate vectors */
5049         if (!count || ch->rx_count || ch->tx_count)
5050                 return -EINVAL;
5051
5052         /* verify other_count has not changed */
5053         if (ch->other_count != (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ? 1 : 0))
5054                 return -EINVAL;
5055
5056         /* verify the number of channels does not exceed hardware limits */
5057         if (count > i40e_max_channels(vsi))
5058                 return -EINVAL;
5059
5060         /* verify that the number of channels does not invalidate any current
5061          * flow director rules
5062          */
5063         hlist_for_each_entry_safe(rule, node2,
5064                                   &pf->fdir_filter_list, fdir_node) {
5065                 if (rule->dest_ctl != drop && count <= rule->q_index) {
5066                         dev_warn(&pf->pdev->dev,
5067                                  "Existing user defined filter %d assigns flow to queue %d\n",
5068                                  rule->fd_id, rule->q_index);
5069                         err = -EINVAL;
5070                 }
5071         }
5072
5073         if (err) {
5074                 dev_err(&pf->pdev->dev,
5075                         "Existing filter rules must be deleted to reduce combined channel count to %d\n",
5076                         count);
5077                 return err;
5078         }
5079
5080         /* update feature limits from largest to smallest supported values */
5081         /* TODO: Flow director limit, DCB etc */
5082
5083         /* use rss_reconfig to rebuild with new queue count and update traffic
5084          * class queue mapping
5085          */
5086         new_count = i40e_reconfig_rss_queues(pf, count);
5087         if (new_count > 0)
5088                 return 0;
5089         else
5090                 return -EINVAL;
5091 }
5092
5093 /**
5094  * i40e_get_rxfh_key_size - get the RSS hash key size
5095  * @netdev: network interface device structure
5096  *
5097  * Returns the table size.
5098  **/
5099 static u32 i40e_get_rxfh_key_size(struct net_device *netdev)
5100 {
5101         return I40E_HKEY_ARRAY_SIZE;
5102 }
5103
5104 /**
5105  * i40e_get_rxfh_indir_size - get the rx flow hash indirection table size
5106  * @netdev: network interface device structure
5107  *
5108  * Returns the table size.
5109  **/
5110 static u32 i40e_get_rxfh_indir_size(struct net_device *netdev)
5111 {
5112         return I40E_HLUT_ARRAY_SIZE;
5113 }
5114
5115 /**
5116  * i40e_get_rxfh - get the rx flow hash indirection table
5117  * @netdev: network interface device structure
5118  * @rxfh: pointer to param struct (indir, key, hfunc)
5119  *
5120  * Reads the indirection table directly from the hardware. Returns 0 on
5121  * success.
5122  **/
5123 static int i40e_get_rxfh(struct net_device *netdev,
5124                          struct ethtool_rxfh_param *rxfh)
5125 {
5126         struct i40e_netdev_priv *np = netdev_priv(netdev);
5127         struct i40e_vsi *vsi = np->vsi;
5128         u8 *lut, *seed = NULL;
5129         int ret;
5130         u16 i;
5131
5132         rxfh->hfunc = ETH_RSS_HASH_TOP;
5133
5134         if (!rxfh->indir)
5135                 return 0;
5136
5137         seed = rxfh->key;
5138         lut = kzalloc(I40E_HLUT_ARRAY_SIZE, GFP_KERNEL);
5139         if (!lut)
5140                 return -ENOMEM;
5141         ret = i40e_get_rss(vsi, seed, lut, I40E_HLUT_ARRAY_SIZE);
5142         if (ret)
5143                 goto out;
5144         for (i = 0; i < I40E_HLUT_ARRAY_SIZE; i++)
5145                 rxfh->indir[i] = (u32)(lut[i]);
5146
5147 out:
5148         kfree(lut);
5149
5150         return ret;
5151 }
5152
5153 /**
5154  * i40e_set_rxfh - set the rx flow hash indirection table
5155  * @netdev: network interface device structure
5156  * @rxfh: pointer to param struct (indir, key, hfunc)
5157  * @extack: extended ACK from the Netlink message
5158  *
5159  * Returns -EINVAL if the table specifies an invalid queue id, otherwise
5160  * returns 0 after programming the table.
5161  **/
5162 static int i40e_set_rxfh(struct net_device *netdev,
5163                          struct ethtool_rxfh_param *rxfh,
5164                          struct netlink_ext_ack *extack)
5165 {
5166         struct i40e_netdev_priv *np = netdev_priv(netdev);
5167         struct i40e_vsi *vsi = np->vsi;
5168         struct i40e_pf *pf = vsi->back;
5169         u8 *seed = NULL;
5170         u16 i;
5171
5172         if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
5173             rxfh->hfunc != ETH_RSS_HASH_TOP)
5174                 return -EOPNOTSUPP;
5175
5176         if (rxfh->key) {
5177                 if (!vsi->rss_hkey_user) {
5178                         vsi->rss_hkey_user = kzalloc(I40E_HKEY_ARRAY_SIZE,
5179                                                      GFP_KERNEL);
5180                         if (!vsi->rss_hkey_user)
5181                                 return -ENOMEM;
5182                 }
5183                 memcpy(vsi->rss_hkey_user, rxfh->key, I40E_HKEY_ARRAY_SIZE);
5184                 seed = vsi->rss_hkey_user;
5185         }
5186         if (!vsi->rss_lut_user) {
5187                 vsi->rss_lut_user = kzalloc(I40E_HLUT_ARRAY_SIZE, GFP_KERNEL);
5188                 if (!vsi->rss_lut_user)
5189                         return -ENOMEM;
5190         }
5191
5192         /* Each 32 bits pointed by 'indir' is stored with a lut entry */
5193         if (rxfh->indir)
5194                 for (i = 0; i < I40E_HLUT_ARRAY_SIZE; i++)
5195                         vsi->rss_lut_user[i] = (u8)(rxfh->indir[i]);
5196         else
5197                 i40e_fill_rss_lut(pf, vsi->rss_lut_user, I40E_HLUT_ARRAY_SIZE,
5198                                   vsi->rss_size);
5199
5200         return i40e_config_rss(vsi, seed, vsi->rss_lut_user,
5201                                I40E_HLUT_ARRAY_SIZE);
5202 }
5203
5204 /**
5205  * i40e_get_priv_flags - report device private flags
5206  * @dev: network interface device structure
5207  *
5208  * The get string set count and the string set should be matched for each
5209  * flag returned.  Add new strings for each flag to the i40e_gstrings_priv_flags
5210  * array.
5211  *
5212  * Returns a u32 bitmap of flags.
5213  **/
5214 static u32 i40e_get_priv_flags(struct net_device *dev)
5215 {
5216         struct i40e_netdev_priv *np = netdev_priv(dev);
5217         struct i40e_vsi *vsi = np->vsi;
5218         struct i40e_pf *pf = vsi->back;
5219         u32 i, j, ret_flags = 0;
5220
5221         for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
5222                 const struct i40e_priv_flags *priv_flag;
5223
5224                 priv_flag = &i40e_gstrings_priv_flags[i];
5225
5226                 if (test_bit(priv_flag->bitno, pf->flags))
5227                         ret_flags |= BIT(i);
5228         }
5229
5230         if (pf->hw.pf_id != 0)
5231                 return ret_flags;
5232
5233         for (j = 0; j < I40E_GL_PRIV_FLAGS_STR_LEN; j++) {
5234                 const struct i40e_priv_flags *priv_flag;
5235
5236                 priv_flag = &i40e_gl_gstrings_priv_flags[j];
5237
5238                 if (test_bit(priv_flag->bitno, pf->flags))
5239                         ret_flags |= BIT(i + j);
5240         }
5241
5242         return ret_flags;
5243 }
5244
5245 /**
5246  * i40e_set_priv_flags - set private flags
5247  * @dev: network interface device structure
5248  * @flags: bit flags to be set
5249  **/
5250 static int i40e_set_priv_flags(struct net_device *dev, u32 flags)
5251 {
5252         DECLARE_BITMAP(changed_flags, I40E_PF_FLAGS_NBITS);
5253         DECLARE_BITMAP(orig_flags, I40E_PF_FLAGS_NBITS);
5254         DECLARE_BITMAP(new_flags, I40E_PF_FLAGS_NBITS);
5255         struct i40e_netdev_priv *np = netdev_priv(dev);
5256         enum i40e_admin_queue_err adq_err;
5257         struct i40e_vsi *vsi = np->vsi;
5258         struct i40e_pf *pf = vsi->back;
5259         u32 reset_needed = 0;
5260         int status;
5261         u32 i, j;
5262
5263         bitmap_copy(orig_flags, pf->flags, I40E_PF_FLAGS_NBITS);
5264         bitmap_copy(new_flags, pf->flags, I40E_PF_FLAGS_NBITS);
5265
5266         for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
5267                 const struct i40e_priv_flags *priv_flag;
5268                 bool new_val;
5269
5270                 priv_flag = &i40e_gstrings_priv_flags[i];
5271                 new_val = (flags & BIT(i)) ? true : false;
5272
5273                 /* If this is a read-only flag, it can't be changed */
5274                 if (priv_flag->read_only &&
5275                     test_bit(priv_flag->bitno, orig_flags) != new_val)
5276                         return -EOPNOTSUPP;
5277
5278                 if (new_val)
5279                         set_bit(priv_flag->bitno, new_flags);
5280                 else
5281                         clear_bit(priv_flag->bitno, new_flags);
5282         }
5283
5284         if (pf->hw.pf_id != 0)
5285                 goto flags_complete;
5286
5287         for (j = 0; j < I40E_GL_PRIV_FLAGS_STR_LEN; j++) {
5288                 const struct i40e_priv_flags *priv_flag;
5289                 bool new_val;
5290
5291                 priv_flag = &i40e_gl_gstrings_priv_flags[j];
5292                 new_val = (flags & BIT(i + j)) ? true : false;
5293
5294                 /* If this is a read-only flag, it can't be changed */
5295                 if (priv_flag->read_only &&
5296                     test_bit(priv_flag->bitno, orig_flags) != new_val)
5297                         return -EOPNOTSUPP;
5298
5299                 if (new_val)
5300                         set_bit(priv_flag->bitno, new_flags);
5301                 else
5302                         clear_bit(priv_flag->bitno, new_flags);
5303         }
5304
5305 flags_complete:
5306         bitmap_xor(changed_flags, pf->flags, orig_flags, I40E_PF_FLAGS_NBITS);
5307
5308         if (test_bit(I40E_FLAG_FW_LLDP_DIS, changed_flags))
5309                 reset_needed = I40E_PF_RESET_AND_REBUILD_FLAG;
5310
5311         if (test_bit(I40E_FLAG_VEB_STATS_ENA, changed_flags) ||
5312             test_bit(I40E_FLAG_LEGACY_RX_ENA, changed_flags) ||
5313             test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, changed_flags))
5314                 reset_needed = BIT(__I40E_PF_RESET_REQUESTED);
5315
5316         /* Before we finalize any flag changes, we need to perform some
5317          * checks to ensure that the changes are supported and safe.
5318          */
5319
5320         /* ATR eviction is not supported on all devices */
5321         if (test_bit(I40E_FLAG_HW_ATR_EVICT_ENA, new_flags) &&
5322             !test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
5323                 return -EOPNOTSUPP;
5324
5325         /* If the driver detected FW LLDP was disabled on init, this flag could
5326          * be set, however we do not support _changing_ the flag:
5327          * - on XL710 if NPAR is enabled or FW API version < 1.7
5328          * - on X722 with FW API version < 1.6
5329          * There are situations where older FW versions/NPAR enabled PFs could
5330          * disable LLDP, however we _must_ not allow the user to enable/disable
5331          * LLDP with this flag on unsupported FW versions.
5332          */
5333         if (test_bit(I40E_FLAG_FW_LLDP_DIS, changed_flags) &&
5334             !test_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps)) {
5335                 dev_warn(&pf->pdev->dev,
5336                          "Device does not support changing FW LLDP\n");
5337                 return -EOPNOTSUPP;
5338         }
5339
5340         if (test_bit(I40E_FLAG_RS_FEC, changed_flags) &&
5341             pf->hw.device_id != I40E_DEV_ID_25G_SFP28 &&
5342             pf->hw.device_id != I40E_DEV_ID_25G_B) {
5343                 dev_warn(&pf->pdev->dev,
5344                          "Device does not support changing FEC configuration\n");
5345                 return -EOPNOTSUPP;
5346         }
5347
5348         if (test_bit(I40E_FLAG_BASE_R_FEC, changed_flags) &&
5349             pf->hw.device_id != I40E_DEV_ID_25G_SFP28 &&
5350             pf->hw.device_id != I40E_DEV_ID_25G_B &&
5351             pf->hw.device_id != I40E_DEV_ID_KX_X722) {
5352                 dev_warn(&pf->pdev->dev,
5353                          "Device does not support changing FEC configuration\n");
5354                 return -EOPNOTSUPP;
5355         }
5356
5357         /* Process any additional changes needed as a result of flag changes.
5358          * The changed_flags value reflects the list of bits that were
5359          * changed in the code above.
5360          */
5361
5362         /* Flush current ATR settings if ATR was disabled */
5363         if (test_bit(I40E_FLAG_FD_ATR_ENA, changed_flags) &&
5364             !test_bit(I40E_FLAG_FD_ATR_ENA, new_flags)) {
5365                 set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
5366                 set_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
5367         }
5368
5369         if (test_bit(I40E_FLAG_TRUE_PROMISC_ENA, changed_flags)) {
5370                 u16 sw_flags = 0, valid_flags = 0;
5371                 int ret;
5372
5373                 if (!test_bit(I40E_FLAG_TRUE_PROMISC_ENA, new_flags))
5374                         sw_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
5375                 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
5376                 ret = i40e_aq_set_switch_config(&pf->hw, sw_flags, valid_flags,
5377                                                 0, NULL);
5378                 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
5379                         dev_info(&pf->pdev->dev,
5380                                  "couldn't set switch config bits, err %pe aq_err %s\n",
5381                                  ERR_PTR(ret),
5382                                  i40e_aq_str(&pf->hw,
5383                                              pf->hw.aq.asq_last_status));
5384                         /* not a fatal problem, just keep going */
5385                 }
5386         }
5387
5388         if (test_bit(I40E_FLAG_RS_FEC, changed_flags) ||
5389             test_bit(I40E_FLAG_BASE_R_FEC, changed_flags)) {
5390                 u8 fec_cfg = 0;
5391
5392                 if (test_bit(I40E_FLAG_RS_FEC, new_flags) &&
5393                     test_bit(I40E_FLAG_BASE_R_FEC, new_flags)) {
5394                         fec_cfg = I40E_AQ_SET_FEC_AUTO;
5395                 } else if (test_bit(I40E_FLAG_RS_FEC, new_flags)) {
5396                         fec_cfg = (I40E_AQ_SET_FEC_REQUEST_RS |
5397                                    I40E_AQ_SET_FEC_ABILITY_RS);
5398                 } else if (test_bit(I40E_FLAG_BASE_R_FEC, new_flags)) {
5399                         fec_cfg = (I40E_AQ_SET_FEC_REQUEST_KR |
5400                                    I40E_AQ_SET_FEC_ABILITY_KR);
5401                 }
5402                 if (i40e_set_fec_cfg(dev, fec_cfg))
5403                         dev_warn(&pf->pdev->dev, "Cannot change FEC config\n");
5404         }
5405
5406         if (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, changed_flags) &&
5407             test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, orig_flags)) {
5408                 dev_err(&pf->pdev->dev,
5409                         "Setting link-down-on-close not supported on this port (because total-port-shutdown is enabled)\n");
5410                 return -EOPNOTSUPP;
5411         }
5412
5413         if (test_bit(I40E_FLAG_VF_VLAN_PRUNING_ENA, changed_flags) &&
5414             pf->num_alloc_vfs) {
5415                 dev_warn(&pf->pdev->dev,
5416                          "Changing vf-vlan-pruning flag while VF(s) are active is not supported\n");
5417                 return -EOPNOTSUPP;
5418         }
5419
5420         if (test_bit(I40E_FLAG_LEGACY_RX_ENA, changed_flags) &&
5421             I40E_2K_TOO_SMALL_WITH_PADDING) {
5422                 dev_warn(&pf->pdev->dev,
5423                          "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
5424                 return -EOPNOTSUPP;
5425         }
5426
5427         if (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, changed_flags) &&
5428             test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, new_flags) &&
5429             test_bit(I40E_FLAG_MFP_ENA, new_flags))
5430                 dev_warn(&pf->pdev->dev,
5431                          "Turning on link-down-on-close flag may affect other partitions\n");
5432
5433         if (test_bit(I40E_FLAG_FW_LLDP_DIS, changed_flags)) {
5434                 if (test_bit(I40E_FLAG_FW_LLDP_DIS, new_flags)) {
5435 #ifdef CONFIG_I40E_DCB
5436                         i40e_dcb_sw_default_config(pf);
5437 #endif /* CONFIG_I40E_DCB */
5438                         i40e_aq_cfg_lldp_mib_change_event(&pf->hw, false, NULL);
5439                         i40e_aq_stop_lldp(&pf->hw, true, false, NULL);
5440                 } else {
5441                         status = i40e_aq_start_lldp(&pf->hw, false, NULL);
5442                         if (status) {
5443                                 adq_err = pf->hw.aq.asq_last_status;
5444                                 switch (adq_err) {
5445                                 case I40E_AQ_RC_EEXIST:
5446                                         dev_warn(&pf->pdev->dev,
5447                                                  "FW LLDP agent is already running\n");
5448                                         reset_needed = 0;
5449                                         break;
5450                                 case I40E_AQ_RC_EPERM:
5451                                         dev_warn(&pf->pdev->dev,
5452                                                  "Device configuration forbids SW from starting the LLDP agent.\n");
5453                                         return -EINVAL;
5454                                 case I40E_AQ_RC_EAGAIN:
5455                                         dev_warn(&pf->pdev->dev,
5456                                                  "Stop FW LLDP agent command is still being processed, please try again in a second.\n");
5457                                         return -EBUSY;
5458                                 default:
5459                                         dev_warn(&pf->pdev->dev,
5460                                                  "Starting FW LLDP agent failed: error: %pe, %s\n",
5461                                                  ERR_PTR(status),
5462                                                  i40e_aq_str(&pf->hw,
5463                                                              adq_err));
5464                                         return -EINVAL;
5465                                 }
5466                         }
5467                 }
5468         }
5469
5470         /* Now that we've checked to ensure that the new flags are valid, load
5471          * them into place. Since we only modify flags either (a) during
5472          * initialization or (b) while holding the RTNL lock, we don't need
5473          * anything fancy here.
5474          */
5475         bitmap_copy(pf->flags, new_flags, I40E_PF_FLAGS_NBITS);
5476
5477         /* Issue reset to cause things to take effect, as additional bits
5478          * are added we will need to create a mask of bits requiring reset
5479          */
5480         if (reset_needed)
5481                 i40e_do_reset(pf, reset_needed, true);
5482
5483         return 0;
5484 }
5485
5486 /**
5487  * i40e_get_module_info - get (Q)SFP+ module type info
5488  * @netdev: network interface device structure
5489  * @modinfo: module EEPROM size and layout information structure
5490  **/
5491 static int i40e_get_module_info(struct net_device *netdev,
5492                                 struct ethtool_modinfo *modinfo)
5493 {
5494         struct i40e_netdev_priv *np = netdev_priv(netdev);
5495         struct i40e_vsi *vsi = np->vsi;
5496         struct i40e_pf *pf = vsi->back;
5497         struct i40e_hw *hw = &pf->hw;
5498         u32 sff8472_comp = 0;
5499         u32 sff8472_swap = 0;
5500         u32 sff8636_rev = 0;
5501         u32 type = 0;
5502         int status;
5503
5504         /* Check if firmware supports reading module EEPROM. */
5505         if (!test_bit(I40E_HW_CAP_AQ_PHY_ACCESS, hw->caps)) {
5506                 netdev_err(vsi->netdev, "Module EEPROM memory read not supported. Please update the NVM image.\n");
5507                 return -EINVAL;
5508         }
5509
5510         status = i40e_update_link_info(hw);
5511         if (status)
5512                 return -EIO;
5513
5514         if (hw->phy.link_info.phy_type == I40E_PHY_TYPE_EMPTY) {
5515                 netdev_err(vsi->netdev, "Cannot read module EEPROM memory. No module connected.\n");
5516                 return -EINVAL;
5517         }
5518
5519         type = hw->phy.link_info.module_type[0];
5520
5521         switch (type) {
5522         case I40E_MODULE_TYPE_SFP:
5523                 status = i40e_aq_get_phy_register(hw,
5524                                 I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
5525                                 I40E_I2C_EEPROM_DEV_ADDR, true,
5526                                 I40E_MODULE_SFF_8472_COMP,
5527                                 &sff8472_comp, NULL);
5528                 if (status)
5529                         return -EIO;
5530
5531                 status = i40e_aq_get_phy_register(hw,
5532                                 I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
5533                                 I40E_I2C_EEPROM_DEV_ADDR, true,
5534                                 I40E_MODULE_SFF_8472_SWAP,
5535                                 &sff8472_swap, NULL);
5536                 if (status)
5537                         return -EIO;
5538
5539                 /* Check if the module requires address swap to access
5540                  * the other EEPROM memory page.
5541                  */
5542                 if (sff8472_swap & I40E_MODULE_SFF_ADDR_MODE) {
5543                         netdev_warn(vsi->netdev, "Module address swap to access page 0xA2 is not supported.\n");
5544                         modinfo->type = ETH_MODULE_SFF_8079;
5545                         modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
5546                 } else if (sff8472_comp == 0x00) {
5547                         /* Module is not SFF-8472 compliant */
5548                         modinfo->type = ETH_MODULE_SFF_8079;
5549                         modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
5550                 } else if (!(sff8472_swap & I40E_MODULE_SFF_DDM_IMPLEMENTED)) {
5551                         /* Module is SFF-8472 compliant but doesn't implement
5552                          * Digital Diagnostic Monitoring (DDM).
5553                          */
5554                         modinfo->type = ETH_MODULE_SFF_8079;
5555                         modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
5556                 } else {
5557                         modinfo->type = ETH_MODULE_SFF_8472;
5558                         modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
5559                 }
5560                 break;
5561         case I40E_MODULE_TYPE_QSFP_PLUS:
5562                 /* Read from memory page 0. */
5563                 status = i40e_aq_get_phy_register(hw,
5564                                 I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
5565                                 0, true,
5566                                 I40E_MODULE_REVISION_ADDR,
5567                                 &sff8636_rev, NULL);
5568                 if (status)
5569                         return -EIO;
5570                 /* Determine revision compliance byte */
5571                 if (sff8636_rev > 0x02) {
5572                         /* Module is SFF-8636 compliant */
5573                         modinfo->type = ETH_MODULE_SFF_8636;
5574                         modinfo->eeprom_len = I40E_MODULE_QSFP_MAX_LEN;
5575                 } else {
5576                         modinfo->type = ETH_MODULE_SFF_8436;
5577                         modinfo->eeprom_len = I40E_MODULE_QSFP_MAX_LEN;
5578                 }
5579                 break;
5580         case I40E_MODULE_TYPE_QSFP28:
5581                 modinfo->type = ETH_MODULE_SFF_8636;
5582                 modinfo->eeprom_len = I40E_MODULE_QSFP_MAX_LEN;
5583                 break;
5584         default:
5585                 netdev_dbg(vsi->netdev, "SFP module type unrecognized or no SFP connector used.\n");
5586                 return -EOPNOTSUPP;
5587         }
5588         return 0;
5589 }
5590
5591 /**
5592  * i40e_get_module_eeprom - fills buffer with (Q)SFP+ module memory contents
5593  * @netdev: network interface device structure
5594  * @ee: EEPROM dump request structure
5595  * @data: buffer to be filled with EEPROM contents
5596  **/
5597 static int i40e_get_module_eeprom(struct net_device *netdev,
5598                                   struct ethtool_eeprom *ee,
5599                                   u8 *data)
5600 {
5601         struct i40e_netdev_priv *np = netdev_priv(netdev);
5602         struct i40e_vsi *vsi = np->vsi;
5603         struct i40e_pf *pf = vsi->back;
5604         struct i40e_hw *hw = &pf->hw;
5605         bool is_sfp = false;
5606         u32 value = 0;
5607         int status;
5608         int i;
5609
5610         if (!ee || !ee->len || !data)
5611                 return -EINVAL;
5612
5613         if (hw->phy.link_info.module_type[0] == I40E_MODULE_TYPE_SFP)
5614                 is_sfp = true;
5615
5616         for (i = 0; i < ee->len; i++) {
5617                 u32 offset = i + ee->offset;
5618                 u32 addr = is_sfp ? I40E_I2C_EEPROM_DEV_ADDR : 0;
5619
5620                 /* Check if we need to access the other memory page */
5621                 if (is_sfp) {
5622                         if (offset >= ETH_MODULE_SFF_8079_LEN) {
5623                                 offset -= ETH_MODULE_SFF_8079_LEN;
5624                                 addr = I40E_I2C_EEPROM_DEV_ADDR2;
5625                         }
5626                 } else {
5627                         while (offset >= ETH_MODULE_SFF_8436_LEN) {
5628                                 /* Compute memory page number and offset. */
5629                                 offset -= ETH_MODULE_SFF_8436_LEN / 2;
5630                                 addr++;
5631                         }
5632                 }
5633
5634                 status = i40e_aq_get_phy_register(hw,
5635                                 I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
5636                                 addr, true, offset, &value, NULL);
5637                 if (status)
5638                         return -EIO;
5639                 data[i] = value;
5640         }
5641         return 0;
5642 }
5643
5644 static int i40e_get_eee(struct net_device *netdev, struct ethtool_keee *edata)
5645 {
5646         struct i40e_netdev_priv *np = netdev_priv(netdev);
5647         struct i40e_aq_get_phy_abilities_resp phy_cfg;
5648         struct i40e_vsi *vsi = np->vsi;
5649         struct i40e_pf *pf = vsi->back;
5650         struct i40e_hw *hw = &pf->hw;
5651         int status = 0;
5652
5653         /* Get initial PHY capabilities */
5654         status = i40e_aq_get_phy_capabilities(hw, false, true, &phy_cfg, NULL);
5655         if (status)
5656                 return -EAGAIN;
5657
5658         /* Check whether NIC configuration is compatible with Energy Efficient
5659          * Ethernet (EEE) mode.
5660          */
5661         if (phy_cfg.eee_capability == 0)
5662                 return -EOPNOTSUPP;
5663
5664         /* Get current configuration */
5665         status = i40e_aq_get_phy_capabilities(hw, false, false, &phy_cfg, NULL);
5666         if (status)
5667                 return -EAGAIN;
5668
5669         edata->eee_enabled = !!phy_cfg.eee_capability;
5670         edata->tx_lpi_enabled = pf->stats.tx_lpi_status;
5671
5672         edata->eee_active = pf->stats.tx_lpi_status && pf->stats.rx_lpi_status;
5673
5674         return 0;
5675 }
5676
5677 static int i40e_is_eee_param_supported(struct net_device *netdev,
5678                                        struct ethtool_keee *edata)
5679 {
5680         struct i40e_netdev_priv *np = netdev_priv(netdev);
5681         struct i40e_vsi *vsi = np->vsi;
5682         struct i40e_pf *pf = vsi->back;
5683         struct i40e_ethtool_not_used {
5684                 u32 value;
5685                 const char *name;
5686         } param[] = {
5687                 {edata->tx_lpi_timer, "tx-timer"},
5688                 {edata->tx_lpi_enabled != pf->stats.tx_lpi_status, "tx-lpi"}
5689         };
5690         int i;
5691
5692         for (i = 0; i < ARRAY_SIZE(param); i++) {
5693                 if (param[i].value) {
5694                         netdev_info(netdev,
5695                                     "EEE setting %s not supported\n",
5696                                     param[i].name);
5697                         return -EOPNOTSUPP;
5698                 }
5699         }
5700
5701         return 0;
5702 }
5703
5704 static int i40e_set_eee(struct net_device *netdev, struct ethtool_keee *edata)
5705 {
5706         struct i40e_netdev_priv *np = netdev_priv(netdev);
5707         struct i40e_aq_get_phy_abilities_resp abilities;
5708         struct i40e_aq_set_phy_config config;
5709         struct i40e_vsi *vsi = np->vsi;
5710         struct i40e_pf *pf = vsi->back;
5711         struct i40e_hw *hw = &pf->hw;
5712         __le16 eee_capability;
5713         int status = 0;
5714
5715         /* Deny parameters we don't support */
5716         if (i40e_is_eee_param_supported(netdev, edata))
5717                 return -EOPNOTSUPP;
5718
5719         /* Get initial PHY capabilities */
5720         status = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
5721                                               NULL);
5722         if (status)
5723                 return -EAGAIN;
5724
5725         /* Check whether NIC configuration is compatible with Energy Efficient
5726          * Ethernet (EEE) mode.
5727          */
5728         if (abilities.eee_capability == 0)
5729                 return -EOPNOTSUPP;
5730
5731         /* Cache initial EEE capability */
5732         eee_capability = abilities.eee_capability;
5733
5734         /* Get current PHY configuration */
5735         status = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
5736                                               NULL);
5737         if (status)
5738                 return -EAGAIN;
5739
5740         /* Cache current PHY configuration */
5741         config.phy_type = abilities.phy_type;
5742         config.phy_type_ext = abilities.phy_type_ext;
5743         config.link_speed = abilities.link_speed;
5744         config.abilities = abilities.abilities |
5745                            I40E_AQ_PHY_ENABLE_ATOMIC_LINK;
5746         config.eeer = abilities.eeer_val;
5747         config.low_power_ctrl = abilities.d3_lpan;
5748         config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
5749                             I40E_AQ_PHY_FEC_CONFIG_MASK;
5750
5751         /* Set desired EEE state */
5752         if (edata->eee_enabled) {
5753                 config.eee_capability = eee_capability;
5754                 config.eeer |= cpu_to_le32(I40E_PRTPM_EEER_TX_LPI_EN_MASK);
5755         } else {
5756                 config.eee_capability = 0;
5757                 config.eeer &= cpu_to_le32(~I40E_PRTPM_EEER_TX_LPI_EN_MASK);
5758         }
5759
5760         /* Apply modified PHY configuration */
5761         status = i40e_aq_set_phy_config(hw, &config, NULL);
5762         if (status)
5763                 return -EAGAIN;
5764
5765         return 0;
5766 }
5767
5768 static const struct ethtool_ops i40e_ethtool_recovery_mode_ops = {
5769         .get_drvinfo            = i40e_get_drvinfo,
5770         .set_eeprom             = i40e_set_eeprom,
5771         .get_eeprom_len         = i40e_get_eeprom_len,
5772         .get_eeprom             = i40e_get_eeprom,
5773 };
5774
5775 static const struct ethtool_ops i40e_ethtool_ops = {
5776         .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
5777                                      ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ |
5778                                      ETHTOOL_COALESCE_USE_ADAPTIVE |
5779                                      ETHTOOL_COALESCE_RX_USECS_HIGH |
5780                                      ETHTOOL_COALESCE_TX_USECS_HIGH,
5781         .get_drvinfo            = i40e_get_drvinfo,
5782         .get_regs_len           = i40e_get_regs_len,
5783         .get_regs               = i40e_get_regs,
5784         .nway_reset             = i40e_nway_reset,
5785         .get_link               = ethtool_op_get_link,
5786         .get_wol                = i40e_get_wol,
5787         .set_wol                = i40e_set_wol,
5788         .set_eeprom             = i40e_set_eeprom,
5789         .get_eeprom_len         = i40e_get_eeprom_len,
5790         .get_eeprom             = i40e_get_eeprom,
5791         .get_ringparam          = i40e_get_ringparam,
5792         .set_ringparam          = i40e_set_ringparam,
5793         .get_pauseparam         = i40e_get_pauseparam,
5794         .set_pauseparam         = i40e_set_pauseparam,
5795         .get_msglevel           = i40e_get_msglevel,
5796         .set_msglevel           = i40e_set_msglevel,
5797         .get_rxnfc              = i40e_get_rxnfc,
5798         .set_rxnfc              = i40e_set_rxnfc,
5799         .self_test              = i40e_diag_test,
5800         .get_strings            = i40e_get_strings,
5801         .get_eee                = i40e_get_eee,
5802         .set_eee                = i40e_set_eee,
5803         .set_phys_id            = i40e_set_phys_id,
5804         .get_sset_count         = i40e_get_sset_count,
5805         .get_ethtool_stats      = i40e_get_ethtool_stats,
5806         .get_coalesce           = i40e_get_coalesce,
5807         .set_coalesce           = i40e_set_coalesce,
5808         .get_rxfh_key_size      = i40e_get_rxfh_key_size,
5809         .get_rxfh_indir_size    = i40e_get_rxfh_indir_size,
5810         .get_rxfh               = i40e_get_rxfh,
5811         .set_rxfh               = i40e_set_rxfh,
5812         .get_channels           = i40e_get_channels,
5813         .set_channels           = i40e_set_channels,
5814         .get_module_info        = i40e_get_module_info,
5815         .get_module_eeprom      = i40e_get_module_eeprom,
5816         .get_ts_info            = i40e_get_ts_info,
5817         .get_priv_flags         = i40e_get_priv_flags,
5818         .set_priv_flags         = i40e_set_priv_flags,
5819         .get_per_queue_coalesce = i40e_get_per_queue_coalesce,
5820         .set_per_queue_coalesce = i40e_set_per_queue_coalesce,
5821         .get_link_ksettings     = i40e_get_link_ksettings,
5822         .set_link_ksettings     = i40e_set_link_ksettings,
5823         .get_fecparam = i40e_get_fec_param,
5824         .set_fecparam = i40e_set_fec_param,
5825         .flash_device = i40e_ddp_flash,
5826 };
5827
5828 void i40e_set_ethtool_ops(struct net_device *netdev)
5829 {
5830         struct i40e_netdev_priv *np = netdev_priv(netdev);
5831         struct i40e_pf          *pf = np->vsi->back;
5832
5833         if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
5834                 netdev->ethtool_ops = &i40e_ethtool_ops;
5835         else
5836                 netdev->ethtool_ops = &i40e_ethtool_recovery_mode_ops;
5837 }