treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500
[sfrench/cifs-2.6.git] / drivers / net / ethernet / sfc / ethtool.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2005-2006 Fen Systems Ltd.
5  * Copyright 2006-2013 Solarflare Communications Inc.
6  */
7
8 #include <linux/netdevice.h>
9 #include <linux/ethtool.h>
10 #include <linux/rtnetlink.h>
11 #include <linux/in.h>
12 #include "net_driver.h"
13 #include "workarounds.h"
14 #include "selftest.h"
15 #include "efx.h"
16 #include "filter.h"
17 #include "nic.h"
18
19 struct efx_sw_stat_desc {
20         const char *name;
21         enum {
22                 EFX_ETHTOOL_STAT_SOURCE_nic,
23                 EFX_ETHTOOL_STAT_SOURCE_channel,
24                 EFX_ETHTOOL_STAT_SOURCE_tx_queue
25         } source;
26         unsigned offset;
27         u64(*get_stat) (void *field); /* Reader function */
28 };
29
30 /* Initialiser for a struct efx_sw_stat_desc with type-checking */
31 #define EFX_ETHTOOL_STAT(stat_name, source_name, field, field_type, \
32                                 get_stat_function) {                    \
33         .name = #stat_name,                                             \
34         .source = EFX_ETHTOOL_STAT_SOURCE_##source_name,                \
35         .offset = ((((field_type *) 0) ==                               \
36                       &((struct efx_##source_name *)0)->field) ?        \
37                     offsetof(struct efx_##source_name, field) :         \
38                     offsetof(struct efx_##source_name, field)),         \
39         .get_stat = get_stat_function,                                  \
40 }
41
42 static u64 efx_get_uint_stat(void *field)
43 {
44         return *(unsigned int *)field;
45 }
46
47 static u64 efx_get_atomic_stat(void *field)
48 {
49         return atomic_read((atomic_t *) field);
50 }
51
52 #define EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(field)                \
53         EFX_ETHTOOL_STAT(field, nic, field,                     \
54                          atomic_t, efx_get_atomic_stat)
55
56 #define EFX_ETHTOOL_UINT_CHANNEL_STAT(field)                    \
57         EFX_ETHTOOL_STAT(field, channel, n_##field,             \
58                          unsigned int, efx_get_uint_stat)
59
60 #define EFX_ETHTOOL_UINT_TXQ_STAT(field)                        \
61         EFX_ETHTOOL_STAT(tx_##field, tx_queue, field,           \
62                          unsigned int, efx_get_uint_stat)
63
64 static const struct efx_sw_stat_desc efx_sw_stat_desc[] = {
65         EFX_ETHTOOL_UINT_TXQ_STAT(merge_events),
66         EFX_ETHTOOL_UINT_TXQ_STAT(tso_bursts),
67         EFX_ETHTOOL_UINT_TXQ_STAT(tso_long_headers),
68         EFX_ETHTOOL_UINT_TXQ_STAT(tso_packets),
69         EFX_ETHTOOL_UINT_TXQ_STAT(tso_fallbacks),
70         EFX_ETHTOOL_UINT_TXQ_STAT(pushes),
71         EFX_ETHTOOL_UINT_TXQ_STAT(pio_packets),
72         EFX_ETHTOOL_UINT_TXQ_STAT(cb_packets),
73         EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(rx_reset),
74         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tobe_disc),
75         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_ip_hdr_chksum_err),
76         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tcp_udp_chksum_err),
77         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_inner_ip_hdr_chksum_err),
78         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_inner_tcp_udp_chksum_err),
79         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_outer_ip_hdr_chksum_err),
80         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_outer_tcp_udp_chksum_err),
81         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_eth_crc_err),
82         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_mcast_mismatch),
83         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_frm_trunc),
84         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_merge_events),
85         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_merge_packets),
86 };
87
88 #define EFX_ETHTOOL_SW_STAT_COUNT ARRAY_SIZE(efx_sw_stat_desc)
89
90 #define EFX_ETHTOOL_EEPROM_MAGIC 0xEFAB
91
92 /**************************************************************************
93  *
94  * Ethtool operations
95  *
96  **************************************************************************
97  */
98
99 /* Identify device by flashing LEDs */
100 static int efx_ethtool_phys_id(struct net_device *net_dev,
101                                enum ethtool_phys_id_state state)
102 {
103         struct efx_nic *efx = netdev_priv(net_dev);
104         enum efx_led_mode mode = EFX_LED_DEFAULT;
105
106         switch (state) {
107         case ETHTOOL_ID_ON:
108                 mode = EFX_LED_ON;
109                 break;
110         case ETHTOOL_ID_OFF:
111                 mode = EFX_LED_OFF;
112                 break;
113         case ETHTOOL_ID_INACTIVE:
114                 mode = EFX_LED_DEFAULT;
115                 break;
116         case ETHTOOL_ID_ACTIVE:
117                 return 1;       /* cycle on/off once per second */
118         }
119
120         efx->type->set_id_led(efx, mode);
121         return 0;
122 }
123
124 /* This must be called with rtnl_lock held. */
125 static int
126 efx_ethtool_get_link_ksettings(struct net_device *net_dev,
127                                struct ethtool_link_ksettings *cmd)
128 {
129         struct efx_nic *efx = netdev_priv(net_dev);
130         struct efx_link_state *link_state = &efx->link_state;
131         u32 supported;
132
133         mutex_lock(&efx->mac_lock);
134         efx->phy_op->get_link_ksettings(efx, cmd);
135         mutex_unlock(&efx->mac_lock);
136
137         /* Both MACs support pause frames (bidirectional and respond-only) */
138         ethtool_convert_link_mode_to_legacy_u32(&supported,
139                                                 cmd->link_modes.supported);
140
141         supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
142
143         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
144                                                 supported);
145
146         if (LOOPBACK_INTERNAL(efx)) {
147                 cmd->base.speed = link_state->speed;
148                 cmd->base.duplex = link_state->fd ? DUPLEX_FULL : DUPLEX_HALF;
149         }
150
151         return 0;
152 }
153
154 /* This must be called with rtnl_lock held. */
155 static int
156 efx_ethtool_set_link_ksettings(struct net_device *net_dev,
157                                const struct ethtool_link_ksettings *cmd)
158 {
159         struct efx_nic *efx = netdev_priv(net_dev);
160         int rc;
161
162         /* GMAC does not support 1000Mbps HD */
163         if ((cmd->base.speed == SPEED_1000) &&
164             (cmd->base.duplex != DUPLEX_FULL)) {
165                 netif_dbg(efx, drv, efx->net_dev,
166                           "rejecting unsupported 1000Mbps HD setting\n");
167                 return -EINVAL;
168         }
169
170         mutex_lock(&efx->mac_lock);
171         rc = efx->phy_op->set_link_ksettings(efx, cmd);
172         mutex_unlock(&efx->mac_lock);
173         return rc;
174 }
175
176 static void efx_ethtool_get_drvinfo(struct net_device *net_dev,
177                                     struct ethtool_drvinfo *info)
178 {
179         struct efx_nic *efx = netdev_priv(net_dev);
180
181         strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
182         strlcpy(info->version, EFX_DRIVER_VERSION, sizeof(info->version));
183         efx_mcdi_print_fwver(efx, info->fw_version,
184                              sizeof(info->fw_version));
185         strlcpy(info->bus_info, pci_name(efx->pci_dev), sizeof(info->bus_info));
186 }
187
188 static int efx_ethtool_get_regs_len(struct net_device *net_dev)
189 {
190         return efx_nic_get_regs_len(netdev_priv(net_dev));
191 }
192
193 static void efx_ethtool_get_regs(struct net_device *net_dev,
194                                  struct ethtool_regs *regs, void *buf)
195 {
196         struct efx_nic *efx = netdev_priv(net_dev);
197
198         regs->version = efx->type->revision;
199         efx_nic_get_regs(efx, buf);
200 }
201
202 static u32 efx_ethtool_get_msglevel(struct net_device *net_dev)
203 {
204         struct efx_nic *efx = netdev_priv(net_dev);
205         return efx->msg_enable;
206 }
207
208 static void efx_ethtool_set_msglevel(struct net_device *net_dev, u32 msg_enable)
209 {
210         struct efx_nic *efx = netdev_priv(net_dev);
211         efx->msg_enable = msg_enable;
212 }
213
214 /**
215  * efx_fill_test - fill in an individual self-test entry
216  * @test_index:         Index of the test
217  * @strings:            Ethtool strings, or %NULL
218  * @data:               Ethtool test results, or %NULL
219  * @test:               Pointer to test result (used only if data != %NULL)
220  * @unit_format:        Unit name format (e.g. "chan\%d")
221  * @unit_id:            Unit id (e.g. 0 for "chan0")
222  * @test_format:        Test name format (e.g. "loopback.\%s.tx.sent")
223  * @test_id:            Test id (e.g. "PHYXS" for "loopback.PHYXS.tx_sent")
224  *
225  * Fill in an individual self-test entry.
226  */
227 static void efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,
228                           int *test, const char *unit_format, int unit_id,
229                           const char *test_format, const char *test_id)
230 {
231         char unit_str[ETH_GSTRING_LEN], test_str[ETH_GSTRING_LEN];
232
233         /* Fill data value, if applicable */
234         if (data)
235                 data[test_index] = *test;
236
237         /* Fill string, if applicable */
238         if (strings) {
239                 if (strchr(unit_format, '%'))
240                         snprintf(unit_str, sizeof(unit_str),
241                                  unit_format, unit_id);
242                 else
243                         strcpy(unit_str, unit_format);
244                 snprintf(test_str, sizeof(test_str), test_format, test_id);
245                 snprintf(strings + test_index * ETH_GSTRING_LEN,
246                          ETH_GSTRING_LEN,
247                          "%-6s %-24s", unit_str, test_str);
248         }
249 }
250
251 #define EFX_CHANNEL_NAME(_channel) "chan%d", _channel->channel
252 #define EFX_TX_QUEUE_NAME(_tx_queue) "txq%d", _tx_queue->queue
253 #define EFX_RX_QUEUE_NAME(_rx_queue) "rxq%d", _rx_queue->queue
254 #define EFX_LOOPBACK_NAME(_mode, _counter)                      \
255         "loopback.%s." _counter, STRING_TABLE_LOOKUP(_mode, efx_loopback_mode)
256
257 /**
258  * efx_fill_loopback_test - fill in a block of loopback self-test entries
259  * @efx:                Efx NIC
260  * @lb_tests:           Efx loopback self-test results structure
261  * @mode:               Loopback test mode
262  * @test_index:         Starting index of the test
263  * @strings:            Ethtool strings, or %NULL
264  * @data:               Ethtool test results, or %NULL
265  *
266  * Fill in a block of loopback self-test entries.  Return new test
267  * index.
268  */
269 static int efx_fill_loopback_test(struct efx_nic *efx,
270                                   struct efx_loopback_self_tests *lb_tests,
271                                   enum efx_loopback_mode mode,
272                                   unsigned int test_index,
273                                   u8 *strings, u64 *data)
274 {
275         struct efx_channel *channel =
276                 efx_get_channel(efx, efx->tx_channel_offset);
277         struct efx_tx_queue *tx_queue;
278
279         efx_for_each_channel_tx_queue(tx_queue, channel) {
280                 efx_fill_test(test_index++, strings, data,
281                               &lb_tests->tx_sent[tx_queue->queue],
282                               EFX_TX_QUEUE_NAME(tx_queue),
283                               EFX_LOOPBACK_NAME(mode, "tx_sent"));
284                 efx_fill_test(test_index++, strings, data,
285                               &lb_tests->tx_done[tx_queue->queue],
286                               EFX_TX_QUEUE_NAME(tx_queue),
287                               EFX_LOOPBACK_NAME(mode, "tx_done"));
288         }
289         efx_fill_test(test_index++, strings, data,
290                       &lb_tests->rx_good,
291                       "rx", 0,
292                       EFX_LOOPBACK_NAME(mode, "rx_good"));
293         efx_fill_test(test_index++, strings, data,
294                       &lb_tests->rx_bad,
295                       "rx", 0,
296                       EFX_LOOPBACK_NAME(mode, "rx_bad"));
297
298         return test_index;
299 }
300
301 /**
302  * efx_ethtool_fill_self_tests - get self-test details
303  * @efx:                Efx NIC
304  * @tests:              Efx self-test results structure, or %NULL
305  * @strings:            Ethtool strings, or %NULL
306  * @data:               Ethtool test results, or %NULL
307  *
308  * Get self-test number of strings, strings, and/or test results.
309  * Return number of strings (== number of test results).
310  *
311  * The reason for merging these three functions is to make sure that
312  * they can never be inconsistent.
313  */
314 static int efx_ethtool_fill_self_tests(struct efx_nic *efx,
315                                        struct efx_self_tests *tests,
316                                        u8 *strings, u64 *data)
317 {
318         struct efx_channel *channel;
319         unsigned int n = 0, i;
320         enum efx_loopback_mode mode;
321
322         efx_fill_test(n++, strings, data, &tests->phy_alive,
323                       "phy", 0, "alive", NULL);
324         efx_fill_test(n++, strings, data, &tests->nvram,
325                       "core", 0, "nvram", NULL);
326         efx_fill_test(n++, strings, data, &tests->interrupt,
327                       "core", 0, "interrupt", NULL);
328
329         /* Event queues */
330         efx_for_each_channel(channel, efx) {
331                 efx_fill_test(n++, strings, data,
332                               &tests->eventq_dma[channel->channel],
333                               EFX_CHANNEL_NAME(channel),
334                               "eventq.dma", NULL);
335                 efx_fill_test(n++, strings, data,
336                               &tests->eventq_int[channel->channel],
337                               EFX_CHANNEL_NAME(channel),
338                               "eventq.int", NULL);
339         }
340
341         efx_fill_test(n++, strings, data, &tests->memory,
342                       "core", 0, "memory", NULL);
343         efx_fill_test(n++, strings, data, &tests->registers,
344                       "core", 0, "registers", NULL);
345
346         if (efx->phy_op->run_tests != NULL) {
347                 EFX_WARN_ON_PARANOID(efx->phy_op->test_name == NULL);
348
349                 for (i = 0; true; ++i) {
350                         const char *name;
351
352                         EFX_WARN_ON_PARANOID(i >= EFX_MAX_PHY_TESTS);
353                         name = efx->phy_op->test_name(efx, i);
354                         if (name == NULL)
355                                 break;
356
357                         efx_fill_test(n++, strings, data, &tests->phy_ext[i],
358                                       "phy", 0, name, NULL);
359                 }
360         }
361
362         /* Loopback tests */
363         for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
364                 if (!(efx->loopback_modes & (1 << mode)))
365                         continue;
366                 n = efx_fill_loopback_test(efx,
367                                            &tests->loopback[mode], mode, n,
368                                            strings, data);
369         }
370
371         return n;
372 }
373
374 static size_t efx_describe_per_queue_stats(struct efx_nic *efx, u8 *strings)
375 {
376         size_t n_stats = 0;
377         struct efx_channel *channel;
378
379         efx_for_each_channel(channel, efx) {
380                 if (efx_channel_has_tx_queues(channel)) {
381                         n_stats++;
382                         if (strings != NULL) {
383                                 snprintf(strings, ETH_GSTRING_LEN,
384                                          "tx-%u.tx_packets",
385                                          channel->tx_queue[0].queue /
386                                          EFX_TXQ_TYPES);
387
388                                 strings += ETH_GSTRING_LEN;
389                         }
390                 }
391         }
392         efx_for_each_channel(channel, efx) {
393                 if (efx_channel_has_rx_queue(channel)) {
394                         n_stats++;
395                         if (strings != NULL) {
396                                 snprintf(strings, ETH_GSTRING_LEN,
397                                          "rx-%d.rx_packets", channel->channel);
398                                 strings += ETH_GSTRING_LEN;
399                         }
400                 }
401         }
402         return n_stats;
403 }
404
405 static int efx_ethtool_get_sset_count(struct net_device *net_dev,
406                                       int string_set)
407 {
408         struct efx_nic *efx = netdev_priv(net_dev);
409
410         switch (string_set) {
411         case ETH_SS_STATS:
412                 return efx->type->describe_stats(efx, NULL) +
413                        EFX_ETHTOOL_SW_STAT_COUNT +
414                        efx_describe_per_queue_stats(efx, NULL) +
415                        efx_ptp_describe_stats(efx, NULL);
416         case ETH_SS_TEST:
417                 return efx_ethtool_fill_self_tests(efx, NULL, NULL, NULL);
418         default:
419                 return -EINVAL;
420         }
421 }
422
423 static void efx_ethtool_get_strings(struct net_device *net_dev,
424                                     u32 string_set, u8 *strings)
425 {
426         struct efx_nic *efx = netdev_priv(net_dev);
427         int i;
428
429         switch (string_set) {
430         case ETH_SS_STATS:
431                 strings += (efx->type->describe_stats(efx, strings) *
432                             ETH_GSTRING_LEN);
433                 for (i = 0; i < EFX_ETHTOOL_SW_STAT_COUNT; i++)
434                         strlcpy(strings + i * ETH_GSTRING_LEN,
435                                 efx_sw_stat_desc[i].name, ETH_GSTRING_LEN);
436                 strings += EFX_ETHTOOL_SW_STAT_COUNT * ETH_GSTRING_LEN;
437                 strings += (efx_describe_per_queue_stats(efx, strings) *
438                             ETH_GSTRING_LEN);
439                 efx_ptp_describe_stats(efx, strings);
440                 break;
441         case ETH_SS_TEST:
442                 efx_ethtool_fill_self_tests(efx, NULL, strings, NULL);
443                 break;
444         default:
445                 /* No other string sets */
446                 break;
447         }
448 }
449
450 static void efx_ethtool_get_stats(struct net_device *net_dev,
451                                   struct ethtool_stats *stats,
452                                   u64 *data)
453 {
454         struct efx_nic *efx = netdev_priv(net_dev);
455         const struct efx_sw_stat_desc *stat;
456         struct efx_channel *channel;
457         struct efx_tx_queue *tx_queue;
458         struct efx_rx_queue *rx_queue;
459         int i;
460
461         spin_lock_bh(&efx->stats_lock);
462
463         /* Get NIC statistics */
464         data += efx->type->update_stats(efx, data, NULL);
465
466         /* Get software statistics */
467         for (i = 0; i < EFX_ETHTOOL_SW_STAT_COUNT; i++) {
468                 stat = &efx_sw_stat_desc[i];
469                 switch (stat->source) {
470                 case EFX_ETHTOOL_STAT_SOURCE_nic:
471                         data[i] = stat->get_stat((void *)efx + stat->offset);
472                         break;
473                 case EFX_ETHTOOL_STAT_SOURCE_channel:
474                         data[i] = 0;
475                         efx_for_each_channel(channel, efx)
476                                 data[i] += stat->get_stat((void *)channel +
477                                                           stat->offset);
478                         break;
479                 case EFX_ETHTOOL_STAT_SOURCE_tx_queue:
480                         data[i] = 0;
481                         efx_for_each_channel(channel, efx) {
482                                 efx_for_each_channel_tx_queue(tx_queue, channel)
483                                         data[i] +=
484                                                 stat->get_stat((void *)tx_queue
485                                                                + stat->offset);
486                         }
487                         break;
488                 }
489         }
490         data += EFX_ETHTOOL_SW_STAT_COUNT;
491
492         spin_unlock_bh(&efx->stats_lock);
493
494         efx_for_each_channel(channel, efx) {
495                 if (efx_channel_has_tx_queues(channel)) {
496                         *data = 0;
497                         efx_for_each_channel_tx_queue(tx_queue, channel) {
498                                 *data += tx_queue->tx_packets;
499                         }
500                         data++;
501                 }
502         }
503         efx_for_each_channel(channel, efx) {
504                 if (efx_channel_has_rx_queue(channel)) {
505                         *data = 0;
506                         efx_for_each_channel_rx_queue(rx_queue, channel) {
507                                 *data += rx_queue->rx_packets;
508                         }
509                         data++;
510                 }
511         }
512
513         efx_ptp_update_stats(efx, data);
514 }
515
516 static void efx_ethtool_self_test(struct net_device *net_dev,
517                                   struct ethtool_test *test, u64 *data)
518 {
519         struct efx_nic *efx = netdev_priv(net_dev);
520         struct efx_self_tests *efx_tests;
521         bool already_up;
522         int rc = -ENOMEM;
523
524         efx_tests = kzalloc(sizeof(*efx_tests), GFP_KERNEL);
525         if (!efx_tests)
526                 goto fail;
527
528         if (efx->state != STATE_READY) {
529                 rc = -EBUSY;
530                 goto out;
531         }
532
533         netif_info(efx, drv, efx->net_dev, "starting %sline testing\n",
534                    (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on");
535
536         /* We need rx buffers and interrupts. */
537         already_up = (efx->net_dev->flags & IFF_UP);
538         if (!already_up) {
539                 rc = dev_open(efx->net_dev, NULL);
540                 if (rc) {
541                         netif_err(efx, drv, efx->net_dev,
542                                   "failed opening device.\n");
543                         goto out;
544                 }
545         }
546
547         rc = efx_selftest(efx, efx_tests, test->flags);
548
549         if (!already_up)
550                 dev_close(efx->net_dev);
551
552         netif_info(efx, drv, efx->net_dev, "%s %sline self-tests\n",
553                    rc == 0 ? "passed" : "failed",
554                    (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on");
555
556 out:
557         efx_ethtool_fill_self_tests(efx, efx_tests, NULL, data);
558         kfree(efx_tests);
559 fail:
560         if (rc)
561                 test->flags |= ETH_TEST_FL_FAILED;
562 }
563
564 /* Restart autonegotiation */
565 static int efx_ethtool_nway_reset(struct net_device *net_dev)
566 {
567         struct efx_nic *efx = netdev_priv(net_dev);
568
569         return mdio45_nway_restart(&efx->mdio);
570 }
571
572 /*
573  * Each channel has a single IRQ and moderation timer, started by any
574  * completion (or other event).  Unless the module parameter
575  * separate_tx_channels is set, IRQs and moderation are therefore
576  * shared between RX and TX completions.  In this case, when RX IRQ
577  * moderation is explicitly changed then TX IRQ moderation is
578  * automatically changed too, but otherwise we fail if the two values
579  * are requested to be different.
580  *
581  * The hardware does not support a limit on the number of completions
582  * before an IRQ, so we do not use the max_frames fields.  We should
583  * report and require that max_frames == (usecs != 0), but this would
584  * invalidate existing user documentation.
585  *
586  * The hardware does not have distinct settings for interrupt
587  * moderation while the previous IRQ is being handled, so we should
588  * not use the 'irq' fields.  However, an earlier developer
589  * misunderstood the meaning of the 'irq' fields and the driver did
590  * not support the standard fields.  To avoid invalidating existing
591  * user documentation, we report and accept changes through either the
592  * standard or 'irq' fields.  If both are changed at the same time, we
593  * prefer the standard field.
594  *
595  * We implement adaptive IRQ moderation, but use a different algorithm
596  * from that assumed in the definition of struct ethtool_coalesce.
597  * Therefore we do not use any of the adaptive moderation parameters
598  * in it.
599  */
600
601 static int efx_ethtool_get_coalesce(struct net_device *net_dev,
602                                     struct ethtool_coalesce *coalesce)
603 {
604         struct efx_nic *efx = netdev_priv(net_dev);
605         unsigned int tx_usecs, rx_usecs;
606         bool rx_adaptive;
607
608         efx_get_irq_moderation(efx, &tx_usecs, &rx_usecs, &rx_adaptive);
609
610         coalesce->tx_coalesce_usecs = tx_usecs;
611         coalesce->tx_coalesce_usecs_irq = tx_usecs;
612         coalesce->rx_coalesce_usecs = rx_usecs;
613         coalesce->rx_coalesce_usecs_irq = rx_usecs;
614         coalesce->use_adaptive_rx_coalesce = rx_adaptive;
615
616         return 0;
617 }
618
619 static int efx_ethtool_set_coalesce(struct net_device *net_dev,
620                                     struct ethtool_coalesce *coalesce)
621 {
622         struct efx_nic *efx = netdev_priv(net_dev);
623         struct efx_channel *channel;
624         unsigned int tx_usecs, rx_usecs;
625         bool adaptive, rx_may_override_tx;
626         int rc;
627
628         if (coalesce->use_adaptive_tx_coalesce)
629                 return -EINVAL;
630
631         efx_get_irq_moderation(efx, &tx_usecs, &rx_usecs, &adaptive);
632
633         if (coalesce->rx_coalesce_usecs != rx_usecs)
634                 rx_usecs = coalesce->rx_coalesce_usecs;
635         else
636                 rx_usecs = coalesce->rx_coalesce_usecs_irq;
637
638         adaptive = coalesce->use_adaptive_rx_coalesce;
639
640         /* If channels are shared, TX IRQ moderation can be quietly
641          * overridden unless it is changed from its old value.
642          */
643         rx_may_override_tx = (coalesce->tx_coalesce_usecs == tx_usecs &&
644                               coalesce->tx_coalesce_usecs_irq == tx_usecs);
645         if (coalesce->tx_coalesce_usecs != tx_usecs)
646                 tx_usecs = coalesce->tx_coalesce_usecs;
647         else
648                 tx_usecs = coalesce->tx_coalesce_usecs_irq;
649
650         rc = efx_init_irq_moderation(efx, tx_usecs, rx_usecs, adaptive,
651                                      rx_may_override_tx);
652         if (rc != 0)
653                 return rc;
654
655         efx_for_each_channel(channel, efx)
656                 efx->type->push_irq_moderation(channel);
657
658         return 0;
659 }
660
661 static void efx_ethtool_get_ringparam(struct net_device *net_dev,
662                                       struct ethtool_ringparam *ring)
663 {
664         struct efx_nic *efx = netdev_priv(net_dev);
665
666         ring->rx_max_pending = EFX_MAX_DMAQ_SIZE;
667         ring->tx_max_pending = EFX_TXQ_MAX_ENT(efx);
668         ring->rx_pending = efx->rxq_entries;
669         ring->tx_pending = efx->txq_entries;
670 }
671
672 static int efx_ethtool_set_ringparam(struct net_device *net_dev,
673                                      struct ethtool_ringparam *ring)
674 {
675         struct efx_nic *efx = netdev_priv(net_dev);
676         u32 txq_entries;
677
678         if (ring->rx_mini_pending || ring->rx_jumbo_pending ||
679             ring->rx_pending > EFX_MAX_DMAQ_SIZE ||
680             ring->tx_pending > EFX_TXQ_MAX_ENT(efx))
681                 return -EINVAL;
682
683         if (ring->rx_pending < EFX_RXQ_MIN_ENT) {
684                 netif_err(efx, drv, efx->net_dev,
685                           "RX queues cannot be smaller than %u\n",
686                           EFX_RXQ_MIN_ENT);
687                 return -EINVAL;
688         }
689
690         txq_entries = max(ring->tx_pending, EFX_TXQ_MIN_ENT(efx));
691         if (txq_entries != ring->tx_pending)
692                 netif_warn(efx, drv, efx->net_dev,
693                            "increasing TX queue size to minimum of %u\n",
694                            txq_entries);
695
696         return efx_realloc_channels(efx, ring->rx_pending, txq_entries);
697 }
698
699 static int efx_ethtool_set_pauseparam(struct net_device *net_dev,
700                                       struct ethtool_pauseparam *pause)
701 {
702         struct efx_nic *efx = netdev_priv(net_dev);
703         u8 wanted_fc, old_fc;
704         u32 old_adv;
705         int rc = 0;
706
707         mutex_lock(&efx->mac_lock);
708
709         wanted_fc = ((pause->rx_pause ? EFX_FC_RX : 0) |
710                      (pause->tx_pause ? EFX_FC_TX : 0) |
711                      (pause->autoneg ? EFX_FC_AUTO : 0));
712
713         if ((wanted_fc & EFX_FC_TX) && !(wanted_fc & EFX_FC_RX)) {
714                 netif_dbg(efx, drv, efx->net_dev,
715                           "Flow control unsupported: tx ON rx OFF\n");
716                 rc = -EINVAL;
717                 goto out;
718         }
719
720         if ((wanted_fc & EFX_FC_AUTO) && !efx->link_advertising[0]) {
721                 netif_dbg(efx, drv, efx->net_dev,
722                           "Autonegotiation is disabled\n");
723                 rc = -EINVAL;
724                 goto out;
725         }
726
727         /* Hook for Falcon bug 11482 workaround */
728         if (efx->type->prepare_enable_fc_tx &&
729             (wanted_fc & EFX_FC_TX) && !(efx->wanted_fc & EFX_FC_TX))
730                 efx->type->prepare_enable_fc_tx(efx);
731
732         old_adv = efx->link_advertising[0];
733         old_fc = efx->wanted_fc;
734         efx_link_set_wanted_fc(efx, wanted_fc);
735         if (efx->link_advertising[0] != old_adv ||
736             (efx->wanted_fc ^ old_fc) & EFX_FC_AUTO) {
737                 rc = efx->phy_op->reconfigure(efx);
738                 if (rc) {
739                         netif_err(efx, drv, efx->net_dev,
740                                   "Unable to advertise requested flow "
741                                   "control setting\n");
742                         goto out;
743                 }
744         }
745
746         /* Reconfigure the MAC. The PHY *may* generate a link state change event
747          * if the user just changed the advertised capabilities, but there's no
748          * harm doing this twice */
749         efx_mac_reconfigure(efx);
750
751 out:
752         mutex_unlock(&efx->mac_lock);
753
754         return rc;
755 }
756
757 static void efx_ethtool_get_pauseparam(struct net_device *net_dev,
758                                        struct ethtool_pauseparam *pause)
759 {
760         struct efx_nic *efx = netdev_priv(net_dev);
761
762         pause->rx_pause = !!(efx->wanted_fc & EFX_FC_RX);
763         pause->tx_pause = !!(efx->wanted_fc & EFX_FC_TX);
764         pause->autoneg = !!(efx->wanted_fc & EFX_FC_AUTO);
765 }
766
767 static void efx_ethtool_get_wol(struct net_device *net_dev,
768                                 struct ethtool_wolinfo *wol)
769 {
770         struct efx_nic *efx = netdev_priv(net_dev);
771         return efx->type->get_wol(efx, wol);
772 }
773
774
775 static int efx_ethtool_set_wol(struct net_device *net_dev,
776                                struct ethtool_wolinfo *wol)
777 {
778         struct efx_nic *efx = netdev_priv(net_dev);
779         return efx->type->set_wol(efx, wol->wolopts);
780 }
781
782 static int efx_ethtool_reset(struct net_device *net_dev, u32 *flags)
783 {
784         struct efx_nic *efx = netdev_priv(net_dev);
785         int rc;
786
787         rc = efx->type->map_reset_flags(flags);
788         if (rc < 0)
789                 return rc;
790
791         return efx_reset(efx, rc);
792 }
793
794 /* MAC address mask including only I/G bit */
795 static const u8 mac_addr_ig_mask[ETH_ALEN] __aligned(2) = {0x01, 0, 0, 0, 0, 0};
796
797 #define IP4_ADDR_FULL_MASK      ((__force __be32)~0)
798 #define IP_PROTO_FULL_MASK      0xFF
799 #define PORT_FULL_MASK          ((__force __be16)~0)
800 #define ETHER_TYPE_FULL_MASK    ((__force __be16)~0)
801
802 static inline void ip6_fill_mask(__be32 *mask)
803 {
804         mask[0] = mask[1] = mask[2] = mask[3] = ~(__be32)0;
805 }
806
807 static int efx_ethtool_get_class_rule(struct efx_nic *efx,
808                                       struct ethtool_rx_flow_spec *rule,
809                                       u32 *rss_context)
810 {
811         struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec;
812         struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec;
813         struct ethtool_usrip4_spec *uip_entry = &rule->h_u.usr_ip4_spec;
814         struct ethtool_usrip4_spec *uip_mask = &rule->m_u.usr_ip4_spec;
815         struct ethtool_tcpip6_spec *ip6_entry = &rule->h_u.tcp_ip6_spec;
816         struct ethtool_tcpip6_spec *ip6_mask = &rule->m_u.tcp_ip6_spec;
817         struct ethtool_usrip6_spec *uip6_entry = &rule->h_u.usr_ip6_spec;
818         struct ethtool_usrip6_spec *uip6_mask = &rule->m_u.usr_ip6_spec;
819         struct ethhdr *mac_entry = &rule->h_u.ether_spec;
820         struct ethhdr *mac_mask = &rule->m_u.ether_spec;
821         struct efx_filter_spec spec;
822         int rc;
823
824         rc = efx_filter_get_filter_safe(efx, EFX_FILTER_PRI_MANUAL,
825                                         rule->location, &spec);
826         if (rc)
827                 return rc;
828
829         if (spec.dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
830                 rule->ring_cookie = RX_CLS_FLOW_DISC;
831         else
832                 rule->ring_cookie = spec.dmaq_id;
833
834         if ((spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) &&
835             spec.ether_type == htons(ETH_P_IP) &&
836             (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) &&
837             (spec.ip_proto == IPPROTO_TCP || spec.ip_proto == IPPROTO_UDP) &&
838             !(spec.match_flags &
839               ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
840                 EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
841                 EFX_FILTER_MATCH_IP_PROTO |
842                 EFX_FILTER_MATCH_LOC_PORT | EFX_FILTER_MATCH_REM_PORT))) {
843                 rule->flow_type = ((spec.ip_proto == IPPROTO_TCP) ?
844                                    TCP_V4_FLOW : UDP_V4_FLOW);
845                 if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
846                         ip_entry->ip4dst = spec.loc_host[0];
847                         ip_mask->ip4dst = IP4_ADDR_FULL_MASK;
848                 }
849                 if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
850                         ip_entry->ip4src = spec.rem_host[0];
851                         ip_mask->ip4src = IP4_ADDR_FULL_MASK;
852                 }
853                 if (spec.match_flags & EFX_FILTER_MATCH_LOC_PORT) {
854                         ip_entry->pdst = spec.loc_port;
855                         ip_mask->pdst = PORT_FULL_MASK;
856                 }
857                 if (spec.match_flags & EFX_FILTER_MATCH_REM_PORT) {
858                         ip_entry->psrc = spec.rem_port;
859                         ip_mask->psrc = PORT_FULL_MASK;
860                 }
861         } else if ((spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) &&
862             spec.ether_type == htons(ETH_P_IPV6) &&
863             (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) &&
864             (spec.ip_proto == IPPROTO_TCP || spec.ip_proto == IPPROTO_UDP) &&
865             !(spec.match_flags &
866               ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
867                 EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
868                 EFX_FILTER_MATCH_IP_PROTO |
869                 EFX_FILTER_MATCH_LOC_PORT | EFX_FILTER_MATCH_REM_PORT))) {
870                 rule->flow_type = ((spec.ip_proto == IPPROTO_TCP) ?
871                                    TCP_V6_FLOW : UDP_V6_FLOW);
872                 if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
873                         memcpy(ip6_entry->ip6dst, spec.loc_host,
874                                sizeof(ip6_entry->ip6dst));
875                         ip6_fill_mask(ip6_mask->ip6dst);
876                 }
877                 if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
878                         memcpy(ip6_entry->ip6src, spec.rem_host,
879                                sizeof(ip6_entry->ip6src));
880                         ip6_fill_mask(ip6_mask->ip6src);
881                 }
882                 if (spec.match_flags & EFX_FILTER_MATCH_LOC_PORT) {
883                         ip6_entry->pdst = spec.loc_port;
884                         ip6_mask->pdst = PORT_FULL_MASK;
885                 }
886                 if (spec.match_flags & EFX_FILTER_MATCH_REM_PORT) {
887                         ip6_entry->psrc = spec.rem_port;
888                         ip6_mask->psrc = PORT_FULL_MASK;
889                 }
890         } else if (!(spec.match_flags &
891                      ~(EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG |
892                        EFX_FILTER_MATCH_REM_MAC | EFX_FILTER_MATCH_ETHER_TYPE |
893                        EFX_FILTER_MATCH_OUTER_VID))) {
894                 rule->flow_type = ETHER_FLOW;
895                 if (spec.match_flags &
896                     (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG)) {
897                         ether_addr_copy(mac_entry->h_dest, spec.loc_mac);
898                         if (spec.match_flags & EFX_FILTER_MATCH_LOC_MAC)
899                                 eth_broadcast_addr(mac_mask->h_dest);
900                         else
901                                 ether_addr_copy(mac_mask->h_dest,
902                                                 mac_addr_ig_mask);
903                 }
904                 if (spec.match_flags & EFX_FILTER_MATCH_REM_MAC) {
905                         ether_addr_copy(mac_entry->h_source, spec.rem_mac);
906                         eth_broadcast_addr(mac_mask->h_source);
907                 }
908                 if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) {
909                         mac_entry->h_proto = spec.ether_type;
910                         mac_mask->h_proto = ETHER_TYPE_FULL_MASK;
911                 }
912         } else if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE &&
913                    spec.ether_type == htons(ETH_P_IP) &&
914                    !(spec.match_flags &
915                      ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
916                        EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
917                        EFX_FILTER_MATCH_IP_PROTO))) {
918                 rule->flow_type = IPV4_USER_FLOW;
919                 uip_entry->ip_ver = ETH_RX_NFC_IP4;
920                 if (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) {
921                         uip_mask->proto = IP_PROTO_FULL_MASK;
922                         uip_entry->proto = spec.ip_proto;
923                 }
924                 if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
925                         uip_entry->ip4dst = spec.loc_host[0];
926                         uip_mask->ip4dst = IP4_ADDR_FULL_MASK;
927                 }
928                 if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
929                         uip_entry->ip4src = spec.rem_host[0];
930                         uip_mask->ip4src = IP4_ADDR_FULL_MASK;
931                 }
932         } else if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE &&
933                    spec.ether_type == htons(ETH_P_IPV6) &&
934                    !(spec.match_flags &
935                      ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
936                        EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
937                        EFX_FILTER_MATCH_IP_PROTO))) {
938                 rule->flow_type = IPV6_USER_FLOW;
939                 if (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) {
940                         uip6_mask->l4_proto = IP_PROTO_FULL_MASK;
941                         uip6_entry->l4_proto = spec.ip_proto;
942                 }
943                 if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
944                         memcpy(uip6_entry->ip6dst, spec.loc_host,
945                                sizeof(uip6_entry->ip6dst));
946                         ip6_fill_mask(uip6_mask->ip6dst);
947                 }
948                 if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
949                         memcpy(uip6_entry->ip6src, spec.rem_host,
950                                sizeof(uip6_entry->ip6src));
951                         ip6_fill_mask(uip6_mask->ip6src);
952                 }
953         } else {
954                 /* The above should handle all filters that we insert */
955                 WARN_ON(1);
956                 return -EINVAL;
957         }
958
959         if (spec.match_flags & EFX_FILTER_MATCH_OUTER_VID) {
960                 rule->flow_type |= FLOW_EXT;
961                 rule->h_ext.vlan_tci = spec.outer_vid;
962                 rule->m_ext.vlan_tci = htons(0xfff);
963         }
964
965         if (spec.flags & EFX_FILTER_FLAG_RX_RSS) {
966                 rule->flow_type |= FLOW_RSS;
967                 *rss_context = spec.rss_context;
968         }
969
970         return rc;
971 }
972
973 static int
974 efx_ethtool_get_rxnfc(struct net_device *net_dev,
975                       struct ethtool_rxnfc *info, u32 *rule_locs)
976 {
977         struct efx_nic *efx = netdev_priv(net_dev);
978         u32 rss_context = 0;
979         s32 rc = 0;
980
981         switch (info->cmd) {
982         case ETHTOOL_GRXRINGS:
983                 info->data = efx->n_rx_channels;
984                 return 0;
985
986         case ETHTOOL_GRXFH: {
987                 struct efx_rss_context *ctx = &efx->rss_context;
988
989                 mutex_lock(&efx->rss_lock);
990                 if (info->flow_type & FLOW_RSS && info->rss_context) {
991                         ctx = efx_find_rss_context_entry(efx, info->rss_context);
992                         if (!ctx) {
993                                 rc = -ENOENT;
994                                 goto out_unlock;
995                         }
996                 }
997                 info->data = 0;
998                 if (!efx_rss_active(ctx)) /* No RSS */
999                         goto out_unlock;
1000                 switch (info->flow_type & ~FLOW_RSS) {
1001                 case UDP_V4_FLOW:
1002                         if (ctx->rx_hash_udp_4tuple)
1003                                 /* fall through */
1004                 case TCP_V4_FLOW:
1005                                 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1006                         /* fall through */
1007                 case SCTP_V4_FLOW:
1008                 case AH_ESP_V4_FLOW:
1009                 case IPV4_FLOW:
1010                         info->data |= RXH_IP_SRC | RXH_IP_DST;
1011                         break;
1012                 case UDP_V6_FLOW:
1013                         if (ctx->rx_hash_udp_4tuple)
1014                                 /* fall through */
1015                 case TCP_V6_FLOW:
1016                                 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1017                         /* fall through */
1018                 case SCTP_V6_FLOW:
1019                 case AH_ESP_V6_FLOW:
1020                 case IPV6_FLOW:
1021                         info->data |= RXH_IP_SRC | RXH_IP_DST;
1022                         break;
1023                 default:
1024                         break;
1025                 }
1026 out_unlock:
1027                 mutex_unlock(&efx->rss_lock);
1028                 return rc;
1029         }
1030
1031         case ETHTOOL_GRXCLSRLCNT:
1032                 info->data = efx_filter_get_rx_id_limit(efx);
1033                 if (info->data == 0)
1034                         return -EOPNOTSUPP;
1035                 info->data |= RX_CLS_LOC_SPECIAL;
1036                 info->rule_cnt =
1037                         efx_filter_count_rx_used(efx, EFX_FILTER_PRI_MANUAL);
1038                 return 0;
1039
1040         case ETHTOOL_GRXCLSRULE:
1041                 if (efx_filter_get_rx_id_limit(efx) == 0)
1042                         return -EOPNOTSUPP;
1043                 rc = efx_ethtool_get_class_rule(efx, &info->fs, &rss_context);
1044                 if (rc < 0)
1045                         return rc;
1046                 if (info->fs.flow_type & FLOW_RSS)
1047                         info->rss_context = rss_context;
1048                 return 0;
1049
1050         case ETHTOOL_GRXCLSRLALL:
1051                 info->data = efx_filter_get_rx_id_limit(efx);
1052                 if (info->data == 0)
1053                         return -EOPNOTSUPP;
1054                 rc = efx_filter_get_rx_ids(efx, EFX_FILTER_PRI_MANUAL,
1055                                            rule_locs, info->rule_cnt);
1056                 if (rc < 0)
1057                         return rc;
1058                 info->rule_cnt = rc;
1059                 return 0;
1060
1061         default:
1062                 return -EOPNOTSUPP;
1063         }
1064 }
1065
1066 static inline bool ip6_mask_is_full(__be32 mask[4])
1067 {
1068         return !~(mask[0] & mask[1] & mask[2] & mask[3]);
1069 }
1070
1071 static inline bool ip6_mask_is_empty(__be32 mask[4])
1072 {
1073         return !(mask[0] | mask[1] | mask[2] | mask[3]);
1074 }
1075
1076 static int efx_ethtool_set_class_rule(struct efx_nic *efx,
1077                                       struct ethtool_rx_flow_spec *rule,
1078                                       u32 rss_context)
1079 {
1080         struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec;
1081         struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec;
1082         struct ethtool_usrip4_spec *uip_entry = &rule->h_u.usr_ip4_spec;
1083         struct ethtool_usrip4_spec *uip_mask = &rule->m_u.usr_ip4_spec;
1084         struct ethtool_tcpip6_spec *ip6_entry = &rule->h_u.tcp_ip6_spec;
1085         struct ethtool_tcpip6_spec *ip6_mask = &rule->m_u.tcp_ip6_spec;
1086         struct ethtool_usrip6_spec *uip6_entry = &rule->h_u.usr_ip6_spec;
1087         struct ethtool_usrip6_spec *uip6_mask = &rule->m_u.usr_ip6_spec;
1088         u32 flow_type = rule->flow_type & ~(FLOW_EXT | FLOW_RSS);
1089         struct ethhdr *mac_entry = &rule->h_u.ether_spec;
1090         struct ethhdr *mac_mask = &rule->m_u.ether_spec;
1091         enum efx_filter_flags flags = 0;
1092         struct efx_filter_spec spec;
1093         int rc;
1094
1095         /* Check that user wants us to choose the location */
1096         if (rule->location != RX_CLS_LOC_ANY)
1097                 return -EINVAL;
1098
1099         /* Range-check ring_cookie */
1100         if (rule->ring_cookie >= efx->n_rx_channels &&
1101             rule->ring_cookie != RX_CLS_FLOW_DISC)
1102                 return -EINVAL;
1103
1104         /* Check for unsupported extensions */
1105         if ((rule->flow_type & FLOW_EXT) &&
1106             (rule->m_ext.vlan_etype || rule->m_ext.data[0] ||
1107              rule->m_ext.data[1]))
1108                 return -EINVAL;
1109
1110         if (efx->rx_scatter)
1111                 flags |= EFX_FILTER_FLAG_RX_SCATTER;
1112         if (rule->flow_type & FLOW_RSS)
1113                 flags |= EFX_FILTER_FLAG_RX_RSS;
1114
1115         efx_filter_init_rx(&spec, EFX_FILTER_PRI_MANUAL, flags,
1116                            (rule->ring_cookie == RX_CLS_FLOW_DISC) ?
1117                            EFX_FILTER_RX_DMAQ_ID_DROP : rule->ring_cookie);
1118
1119         if (rule->flow_type & FLOW_RSS)
1120                 spec.rss_context = rss_context;
1121
1122         switch (flow_type) {
1123         case TCP_V4_FLOW:
1124         case UDP_V4_FLOW:
1125                 spec.match_flags = (EFX_FILTER_MATCH_ETHER_TYPE |
1126                                     EFX_FILTER_MATCH_IP_PROTO);
1127                 spec.ether_type = htons(ETH_P_IP);
1128                 spec.ip_proto = flow_type == TCP_V4_FLOW ? IPPROTO_TCP
1129                                                          : IPPROTO_UDP;
1130                 if (ip_mask->ip4dst) {
1131                         if (ip_mask->ip4dst != IP4_ADDR_FULL_MASK)
1132                                 return -EINVAL;
1133                         spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1134                         spec.loc_host[0] = ip_entry->ip4dst;
1135                 }
1136                 if (ip_mask->ip4src) {
1137                         if (ip_mask->ip4src != IP4_ADDR_FULL_MASK)
1138                                 return -EINVAL;
1139                         spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1140                         spec.rem_host[0] = ip_entry->ip4src;
1141                 }
1142                 if (ip_mask->pdst) {
1143                         if (ip_mask->pdst != PORT_FULL_MASK)
1144                                 return -EINVAL;
1145                         spec.match_flags |= EFX_FILTER_MATCH_LOC_PORT;
1146                         spec.loc_port = ip_entry->pdst;
1147                 }
1148                 if (ip_mask->psrc) {
1149                         if (ip_mask->psrc != PORT_FULL_MASK)
1150                                 return -EINVAL;
1151                         spec.match_flags |= EFX_FILTER_MATCH_REM_PORT;
1152                         spec.rem_port = ip_entry->psrc;
1153                 }
1154                 if (ip_mask->tos)
1155                         return -EINVAL;
1156                 break;
1157
1158         case TCP_V6_FLOW:
1159         case UDP_V6_FLOW:
1160                 spec.match_flags = (EFX_FILTER_MATCH_ETHER_TYPE |
1161                                     EFX_FILTER_MATCH_IP_PROTO);
1162                 spec.ether_type = htons(ETH_P_IPV6);
1163                 spec.ip_proto = flow_type == TCP_V6_FLOW ? IPPROTO_TCP
1164                                                          : IPPROTO_UDP;
1165                 if (!ip6_mask_is_empty(ip6_mask->ip6dst)) {
1166                         if (!ip6_mask_is_full(ip6_mask->ip6dst))
1167                                 return -EINVAL;
1168                         spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1169                         memcpy(spec.loc_host, ip6_entry->ip6dst, sizeof(spec.loc_host));
1170                 }
1171                 if (!ip6_mask_is_empty(ip6_mask->ip6src)) {
1172                         if (!ip6_mask_is_full(ip6_mask->ip6src))
1173                                 return -EINVAL;
1174                         spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1175                         memcpy(spec.rem_host, ip6_entry->ip6src, sizeof(spec.rem_host));
1176                 }
1177                 if (ip6_mask->pdst) {
1178                         if (ip6_mask->pdst != PORT_FULL_MASK)
1179                                 return -EINVAL;
1180                         spec.match_flags |= EFX_FILTER_MATCH_LOC_PORT;
1181                         spec.loc_port = ip6_entry->pdst;
1182                 }
1183                 if (ip6_mask->psrc) {
1184                         if (ip6_mask->psrc != PORT_FULL_MASK)
1185                                 return -EINVAL;
1186                         spec.match_flags |= EFX_FILTER_MATCH_REM_PORT;
1187                         spec.rem_port = ip6_entry->psrc;
1188                 }
1189                 if (ip6_mask->tclass)
1190                         return -EINVAL;
1191                 break;
1192
1193         case IPV4_USER_FLOW:
1194                 if (uip_mask->l4_4_bytes || uip_mask->tos || uip_mask->ip_ver ||
1195                     uip_entry->ip_ver != ETH_RX_NFC_IP4)
1196                         return -EINVAL;
1197                 spec.match_flags = EFX_FILTER_MATCH_ETHER_TYPE;
1198                 spec.ether_type = htons(ETH_P_IP);
1199                 if (uip_mask->ip4dst) {
1200                         if (uip_mask->ip4dst != IP4_ADDR_FULL_MASK)
1201                                 return -EINVAL;
1202                         spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1203                         spec.loc_host[0] = uip_entry->ip4dst;
1204                 }
1205                 if (uip_mask->ip4src) {
1206                         if (uip_mask->ip4src != IP4_ADDR_FULL_MASK)
1207                                 return -EINVAL;
1208                         spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1209                         spec.rem_host[0] = uip_entry->ip4src;
1210                 }
1211                 if (uip_mask->proto) {
1212                         if (uip_mask->proto != IP_PROTO_FULL_MASK)
1213                                 return -EINVAL;
1214                         spec.match_flags |= EFX_FILTER_MATCH_IP_PROTO;
1215                         spec.ip_proto = uip_entry->proto;
1216                 }
1217                 break;
1218
1219         case IPV6_USER_FLOW:
1220                 if (uip6_mask->l4_4_bytes || uip6_mask->tclass)
1221                         return -EINVAL;
1222                 spec.match_flags = EFX_FILTER_MATCH_ETHER_TYPE;
1223                 spec.ether_type = htons(ETH_P_IPV6);
1224                 if (!ip6_mask_is_empty(uip6_mask->ip6dst)) {
1225                         if (!ip6_mask_is_full(uip6_mask->ip6dst))
1226                                 return -EINVAL;
1227                         spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1228                         memcpy(spec.loc_host, uip6_entry->ip6dst, sizeof(spec.loc_host));
1229                 }
1230                 if (!ip6_mask_is_empty(uip6_mask->ip6src)) {
1231                         if (!ip6_mask_is_full(uip6_mask->ip6src))
1232                                 return -EINVAL;
1233                         spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1234                         memcpy(spec.rem_host, uip6_entry->ip6src, sizeof(spec.rem_host));
1235                 }
1236                 if (uip6_mask->l4_proto) {
1237                         if (uip6_mask->l4_proto != IP_PROTO_FULL_MASK)
1238                                 return -EINVAL;
1239                         spec.match_flags |= EFX_FILTER_MATCH_IP_PROTO;
1240                         spec.ip_proto = uip6_entry->l4_proto;
1241                 }
1242                 break;
1243
1244         case ETHER_FLOW:
1245                 if (!is_zero_ether_addr(mac_mask->h_dest)) {
1246                         if (ether_addr_equal(mac_mask->h_dest,
1247                                              mac_addr_ig_mask))
1248                                 spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC_IG;
1249                         else if (is_broadcast_ether_addr(mac_mask->h_dest))
1250                                 spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC;
1251                         else
1252                                 return -EINVAL;
1253                         ether_addr_copy(spec.loc_mac, mac_entry->h_dest);
1254                 }
1255                 if (!is_zero_ether_addr(mac_mask->h_source)) {
1256                         if (!is_broadcast_ether_addr(mac_mask->h_source))
1257                                 return -EINVAL;
1258                         spec.match_flags |= EFX_FILTER_MATCH_REM_MAC;
1259                         ether_addr_copy(spec.rem_mac, mac_entry->h_source);
1260                 }
1261                 if (mac_mask->h_proto) {
1262                         if (mac_mask->h_proto != ETHER_TYPE_FULL_MASK)
1263                                 return -EINVAL;
1264                         spec.match_flags |= EFX_FILTER_MATCH_ETHER_TYPE;
1265                         spec.ether_type = mac_entry->h_proto;
1266                 }
1267                 break;
1268
1269         default:
1270                 return -EINVAL;
1271         }
1272
1273         if ((rule->flow_type & FLOW_EXT) && rule->m_ext.vlan_tci) {
1274                 if (rule->m_ext.vlan_tci != htons(0xfff))
1275                         return -EINVAL;
1276                 spec.match_flags |= EFX_FILTER_MATCH_OUTER_VID;
1277                 spec.outer_vid = rule->h_ext.vlan_tci;
1278         }
1279
1280         rc = efx_filter_insert_filter(efx, &spec, true);
1281         if (rc < 0)
1282                 return rc;
1283
1284         rule->location = rc;
1285         return 0;
1286 }
1287
1288 static int efx_ethtool_set_rxnfc(struct net_device *net_dev,
1289                                  struct ethtool_rxnfc *info)
1290 {
1291         struct efx_nic *efx = netdev_priv(net_dev);
1292
1293         if (efx_filter_get_rx_id_limit(efx) == 0)
1294                 return -EOPNOTSUPP;
1295
1296         switch (info->cmd) {
1297         case ETHTOOL_SRXCLSRLINS:
1298                 return efx_ethtool_set_class_rule(efx, &info->fs,
1299                                                   info->rss_context);
1300
1301         case ETHTOOL_SRXCLSRLDEL:
1302                 return efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_MANUAL,
1303                                                  info->fs.location);
1304
1305         default:
1306                 return -EOPNOTSUPP;
1307         }
1308 }
1309
1310 static u32 efx_ethtool_get_rxfh_indir_size(struct net_device *net_dev)
1311 {
1312         struct efx_nic *efx = netdev_priv(net_dev);
1313
1314         if (efx->n_rx_channels == 1)
1315                 return 0;
1316         return ARRAY_SIZE(efx->rss_context.rx_indir_table);
1317 }
1318
1319 static u32 efx_ethtool_get_rxfh_key_size(struct net_device *net_dev)
1320 {
1321         struct efx_nic *efx = netdev_priv(net_dev);
1322
1323         return efx->type->rx_hash_key_size;
1324 }
1325
1326 static int efx_ethtool_get_rxfh(struct net_device *net_dev, u32 *indir, u8 *key,
1327                                 u8 *hfunc)
1328 {
1329         struct efx_nic *efx = netdev_priv(net_dev);
1330         int rc;
1331
1332         rc = efx->type->rx_pull_rss_config(efx);
1333         if (rc)
1334                 return rc;
1335
1336         if (hfunc)
1337                 *hfunc = ETH_RSS_HASH_TOP;
1338         if (indir)
1339                 memcpy(indir, efx->rss_context.rx_indir_table,
1340                        sizeof(efx->rss_context.rx_indir_table));
1341         if (key)
1342                 memcpy(key, efx->rss_context.rx_hash_key,
1343                        efx->type->rx_hash_key_size);
1344         return 0;
1345 }
1346
1347 static int efx_ethtool_set_rxfh(struct net_device *net_dev, const u32 *indir,
1348                                 const u8 *key, const u8 hfunc)
1349 {
1350         struct efx_nic *efx = netdev_priv(net_dev);
1351
1352         /* Hash function is Toeplitz, cannot be changed */
1353         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1354                 return -EOPNOTSUPP;
1355         if (!indir && !key)
1356                 return 0;
1357
1358         if (!key)
1359                 key = efx->rss_context.rx_hash_key;
1360         if (!indir)
1361                 indir = efx->rss_context.rx_indir_table;
1362
1363         return efx->type->rx_push_rss_config(efx, true, indir, key);
1364 }
1365
1366 static int efx_ethtool_get_rxfh_context(struct net_device *net_dev, u32 *indir,
1367                                         u8 *key, u8 *hfunc, u32 rss_context)
1368 {
1369         struct efx_nic *efx = netdev_priv(net_dev);
1370         struct efx_rss_context *ctx;
1371         int rc = 0;
1372
1373         if (!efx->type->rx_pull_rss_context_config)
1374                 return -EOPNOTSUPP;
1375
1376         mutex_lock(&efx->rss_lock);
1377         ctx = efx_find_rss_context_entry(efx, rss_context);
1378         if (!ctx) {
1379                 rc = -ENOENT;
1380                 goto out_unlock;
1381         }
1382         rc = efx->type->rx_pull_rss_context_config(efx, ctx);
1383         if (rc)
1384                 goto out_unlock;
1385
1386         if (hfunc)
1387                 *hfunc = ETH_RSS_HASH_TOP;
1388         if (indir)
1389                 memcpy(indir, ctx->rx_indir_table, sizeof(ctx->rx_indir_table));
1390         if (key)
1391                 memcpy(key, ctx->rx_hash_key, efx->type->rx_hash_key_size);
1392 out_unlock:
1393         mutex_unlock(&efx->rss_lock);
1394         return rc;
1395 }
1396
1397 static int efx_ethtool_set_rxfh_context(struct net_device *net_dev,
1398                                         const u32 *indir, const u8 *key,
1399                                         const u8 hfunc, u32 *rss_context,
1400                                         bool delete)
1401 {
1402         struct efx_nic *efx = netdev_priv(net_dev);
1403         struct efx_rss_context *ctx;
1404         bool allocated = false;
1405         int rc;
1406
1407         if (!efx->type->rx_push_rss_context_config)
1408                 return -EOPNOTSUPP;
1409         /* Hash function is Toeplitz, cannot be changed */
1410         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1411                 return -EOPNOTSUPP;
1412
1413         mutex_lock(&efx->rss_lock);
1414
1415         if (*rss_context == ETH_RXFH_CONTEXT_ALLOC) {
1416                 if (delete) {
1417                         /* alloc + delete == Nothing to do */
1418                         rc = -EINVAL;
1419                         goto out_unlock;
1420                 }
1421                 ctx = efx_alloc_rss_context_entry(efx);
1422                 if (!ctx) {
1423                         rc = -ENOMEM;
1424                         goto out_unlock;
1425                 }
1426                 ctx->context_id = EFX_EF10_RSS_CONTEXT_INVALID;
1427                 /* Initialise indir table and key to defaults */
1428                 efx_set_default_rx_indir_table(efx, ctx);
1429                 netdev_rss_key_fill(ctx->rx_hash_key, sizeof(ctx->rx_hash_key));
1430                 allocated = true;
1431         } else {
1432                 ctx = efx_find_rss_context_entry(efx, *rss_context);
1433                 if (!ctx) {
1434                         rc = -ENOENT;
1435                         goto out_unlock;
1436                 }
1437         }
1438
1439         if (delete) {
1440                 /* delete this context */
1441                 rc = efx->type->rx_push_rss_context_config(efx, ctx, NULL, NULL);
1442                 if (!rc)
1443                         efx_free_rss_context_entry(ctx);
1444                 goto out_unlock;
1445         }
1446
1447         if (!key)
1448                 key = ctx->rx_hash_key;
1449         if (!indir)
1450                 indir = ctx->rx_indir_table;
1451
1452         rc = efx->type->rx_push_rss_context_config(efx, ctx, indir, key);
1453         if (rc && allocated)
1454                 efx_free_rss_context_entry(ctx);
1455         else
1456                 *rss_context = ctx->user_id;
1457 out_unlock:
1458         mutex_unlock(&efx->rss_lock);
1459         return rc;
1460 }
1461
1462 static int efx_ethtool_get_ts_info(struct net_device *net_dev,
1463                                    struct ethtool_ts_info *ts_info)
1464 {
1465         struct efx_nic *efx = netdev_priv(net_dev);
1466
1467         /* Software capabilities */
1468         ts_info->so_timestamping = (SOF_TIMESTAMPING_RX_SOFTWARE |
1469                                     SOF_TIMESTAMPING_SOFTWARE);
1470         ts_info->phc_index = -1;
1471
1472         efx_ptp_get_ts_info(efx, ts_info);
1473         return 0;
1474 }
1475
1476 static int efx_ethtool_get_module_eeprom(struct net_device *net_dev,
1477                                          struct ethtool_eeprom *ee,
1478                                          u8 *data)
1479 {
1480         struct efx_nic *efx = netdev_priv(net_dev);
1481         int ret;
1482
1483         if (!efx->phy_op || !efx->phy_op->get_module_eeprom)
1484                 return -EOPNOTSUPP;
1485
1486         mutex_lock(&efx->mac_lock);
1487         ret = efx->phy_op->get_module_eeprom(efx, ee, data);
1488         mutex_unlock(&efx->mac_lock);
1489
1490         return ret;
1491 }
1492
1493 static int efx_ethtool_get_module_info(struct net_device *net_dev,
1494                                        struct ethtool_modinfo *modinfo)
1495 {
1496         struct efx_nic *efx = netdev_priv(net_dev);
1497         int ret;
1498
1499         if (!efx->phy_op || !efx->phy_op->get_module_info)
1500                 return -EOPNOTSUPP;
1501
1502         mutex_lock(&efx->mac_lock);
1503         ret = efx->phy_op->get_module_info(efx, modinfo);
1504         mutex_unlock(&efx->mac_lock);
1505
1506         return ret;
1507 }
1508
1509 static int efx_ethtool_get_fecparam(struct net_device *net_dev,
1510                                     struct ethtool_fecparam *fecparam)
1511 {
1512         struct efx_nic *efx = netdev_priv(net_dev);
1513         int rc;
1514
1515         if (!efx->phy_op || !efx->phy_op->get_fecparam)
1516                 return -EOPNOTSUPP;
1517         mutex_lock(&efx->mac_lock);
1518         rc = efx->phy_op->get_fecparam(efx, fecparam);
1519         mutex_unlock(&efx->mac_lock);
1520
1521         return rc;
1522 }
1523
1524 static int efx_ethtool_set_fecparam(struct net_device *net_dev,
1525                                     struct ethtool_fecparam *fecparam)
1526 {
1527         struct efx_nic *efx = netdev_priv(net_dev);
1528         int rc;
1529
1530         if (!efx->phy_op || !efx->phy_op->get_fecparam)
1531                 return -EOPNOTSUPP;
1532         mutex_lock(&efx->mac_lock);
1533         rc = efx->phy_op->set_fecparam(efx, fecparam);
1534         mutex_unlock(&efx->mac_lock);
1535
1536         return rc;
1537 }
1538
1539 const struct ethtool_ops efx_ethtool_ops = {
1540         .get_drvinfo            = efx_ethtool_get_drvinfo,
1541         .get_regs_len           = efx_ethtool_get_regs_len,
1542         .get_regs               = efx_ethtool_get_regs,
1543         .get_msglevel           = efx_ethtool_get_msglevel,
1544         .set_msglevel           = efx_ethtool_set_msglevel,
1545         .nway_reset             = efx_ethtool_nway_reset,
1546         .get_link               = ethtool_op_get_link,
1547         .get_coalesce           = efx_ethtool_get_coalesce,
1548         .set_coalesce           = efx_ethtool_set_coalesce,
1549         .get_ringparam          = efx_ethtool_get_ringparam,
1550         .set_ringparam          = efx_ethtool_set_ringparam,
1551         .get_pauseparam         = efx_ethtool_get_pauseparam,
1552         .set_pauseparam         = efx_ethtool_set_pauseparam,
1553         .get_sset_count         = efx_ethtool_get_sset_count,
1554         .self_test              = efx_ethtool_self_test,
1555         .get_strings            = efx_ethtool_get_strings,
1556         .set_phys_id            = efx_ethtool_phys_id,
1557         .get_ethtool_stats      = efx_ethtool_get_stats,
1558         .get_wol                = efx_ethtool_get_wol,
1559         .set_wol                = efx_ethtool_set_wol,
1560         .reset                  = efx_ethtool_reset,
1561         .get_rxnfc              = efx_ethtool_get_rxnfc,
1562         .set_rxnfc              = efx_ethtool_set_rxnfc,
1563         .get_rxfh_indir_size    = efx_ethtool_get_rxfh_indir_size,
1564         .get_rxfh_key_size      = efx_ethtool_get_rxfh_key_size,
1565         .get_rxfh               = efx_ethtool_get_rxfh,
1566         .set_rxfh               = efx_ethtool_set_rxfh,
1567         .get_rxfh_context       = efx_ethtool_get_rxfh_context,
1568         .set_rxfh_context       = efx_ethtool_set_rxfh_context,
1569         .get_ts_info            = efx_ethtool_get_ts_info,
1570         .get_module_info        = efx_ethtool_get_module_info,
1571         .get_module_eeprom      = efx_ethtool_get_module_eeprom,
1572         .get_link_ksettings     = efx_ethtool_get_link_ksettings,
1573         .set_link_ksettings     = efx_ethtool_set_link_ksettings,
1574         .get_fecparam           = efx_ethtool_get_fecparam,
1575         .set_fecparam           = efx_ethtool_set_fecparam,
1576 };