Linux-libre 3.14.42-gnu
[librecmc/linux-libre.git] / drivers / net / ethernet / sfc / ethtool.c
1 /****************************************************************************
2  * Driver for Solarflare network controllers and boards
3  * Copyright 2005-2006 Fen Systems Ltd.
4  * Copyright 2006-2013 Solarflare Communications Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published
8  * by the Free Software Foundation, incorporated herein by reference.
9  */
10
11 #include <linux/netdevice.h>
12 #include <linux/ethtool.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/in.h>
15 #include "net_driver.h"
16 #include "workarounds.h"
17 #include "selftest.h"
18 #include "efx.h"
19 #include "filter.h"
20 #include "nic.h"
21
22 struct efx_sw_stat_desc {
23         const char *name;
24         enum {
25                 EFX_ETHTOOL_STAT_SOURCE_nic,
26                 EFX_ETHTOOL_STAT_SOURCE_channel,
27                 EFX_ETHTOOL_STAT_SOURCE_tx_queue
28         } source;
29         unsigned offset;
30         u64(*get_stat) (void *field); /* Reader function */
31 };
32
33 /* Initialiser for a struct efx_sw_stat_desc with type-checking */
34 #define EFX_ETHTOOL_STAT(stat_name, source_name, field, field_type, \
35                                 get_stat_function) {                    \
36         .name = #stat_name,                                             \
37         .source = EFX_ETHTOOL_STAT_SOURCE_##source_name,                \
38         .offset = ((((field_type *) 0) ==                               \
39                       &((struct efx_##source_name *)0)->field) ?        \
40                     offsetof(struct efx_##source_name, field) :         \
41                     offsetof(struct efx_##source_name, field)),         \
42         .get_stat = get_stat_function,                                  \
43 }
44
45 static u64 efx_get_uint_stat(void *field)
46 {
47         return *(unsigned int *)field;
48 }
49
50 static u64 efx_get_atomic_stat(void *field)
51 {
52         return atomic_read((atomic_t *) field);
53 }
54
55 #define EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(field)                \
56         EFX_ETHTOOL_STAT(field, nic, field,                     \
57                          atomic_t, efx_get_atomic_stat)
58
59 #define EFX_ETHTOOL_UINT_CHANNEL_STAT(field)                    \
60         EFX_ETHTOOL_STAT(field, channel, n_##field,             \
61                          unsigned int, efx_get_uint_stat)
62
63 #define EFX_ETHTOOL_UINT_TXQ_STAT(field)                        \
64         EFX_ETHTOOL_STAT(tx_##field, tx_queue, field,           \
65                          unsigned int, efx_get_uint_stat)
66
67 static const struct efx_sw_stat_desc efx_sw_stat_desc[] = {
68         EFX_ETHTOOL_UINT_TXQ_STAT(merge_events),
69         EFX_ETHTOOL_UINT_TXQ_STAT(tso_bursts),
70         EFX_ETHTOOL_UINT_TXQ_STAT(tso_long_headers),
71         EFX_ETHTOOL_UINT_TXQ_STAT(tso_packets),
72         EFX_ETHTOOL_UINT_TXQ_STAT(pushes),
73         EFX_ETHTOOL_UINT_TXQ_STAT(pio_packets),
74         EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(rx_reset),
75         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tobe_disc),
76         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_ip_hdr_chksum_err),
77         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tcp_udp_chksum_err),
78         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_mcast_mismatch),
79         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_frm_trunc),
80         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_nodesc_trunc),
81         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_merge_events),
82         EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_merge_packets),
83 };
84
85 #define EFX_ETHTOOL_SW_STAT_COUNT ARRAY_SIZE(efx_sw_stat_desc)
86
87 #define EFX_ETHTOOL_EEPROM_MAGIC 0xEFAB
88
89 /**************************************************************************
90  *
91  * Ethtool operations
92  *
93  **************************************************************************
94  */
95
96 /* Identify device by flashing LEDs */
97 static int efx_ethtool_phys_id(struct net_device *net_dev,
98                                enum ethtool_phys_id_state state)
99 {
100         struct efx_nic *efx = netdev_priv(net_dev);
101         enum efx_led_mode mode = EFX_LED_DEFAULT;
102
103         switch (state) {
104         case ETHTOOL_ID_ON:
105                 mode = EFX_LED_ON;
106                 break;
107         case ETHTOOL_ID_OFF:
108                 mode = EFX_LED_OFF;
109                 break;
110         case ETHTOOL_ID_INACTIVE:
111                 mode = EFX_LED_DEFAULT;
112                 break;
113         case ETHTOOL_ID_ACTIVE:
114                 return 1;       /* cycle on/off once per second */
115         }
116
117         efx->type->set_id_led(efx, mode);
118         return 0;
119 }
120
121 /* This must be called with rtnl_lock held. */
122 static int efx_ethtool_get_settings(struct net_device *net_dev,
123                                     struct ethtool_cmd *ecmd)
124 {
125         struct efx_nic *efx = netdev_priv(net_dev);
126         struct efx_link_state *link_state = &efx->link_state;
127
128         mutex_lock(&efx->mac_lock);
129         efx->phy_op->get_settings(efx, ecmd);
130         mutex_unlock(&efx->mac_lock);
131
132         /* Both MACs support pause frames (bidirectional and respond-only) */
133         ecmd->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
134
135         if (LOOPBACK_INTERNAL(efx)) {
136                 ethtool_cmd_speed_set(ecmd, link_state->speed);
137                 ecmd->duplex = link_state->fd ? DUPLEX_FULL : DUPLEX_HALF;
138         }
139
140         return 0;
141 }
142
143 /* This must be called with rtnl_lock held. */
144 static int efx_ethtool_set_settings(struct net_device *net_dev,
145                                     struct ethtool_cmd *ecmd)
146 {
147         struct efx_nic *efx = netdev_priv(net_dev);
148         int rc;
149
150         /* GMAC does not support 1000Mbps HD */
151         if ((ethtool_cmd_speed(ecmd) == SPEED_1000) &&
152             (ecmd->duplex != DUPLEX_FULL)) {
153                 netif_dbg(efx, drv, efx->net_dev,
154                           "rejecting unsupported 1000Mbps HD setting\n");
155                 return -EINVAL;
156         }
157
158         mutex_lock(&efx->mac_lock);
159         rc = efx->phy_op->set_settings(efx, ecmd);
160         mutex_unlock(&efx->mac_lock);
161         return rc;
162 }
163
164 static void efx_ethtool_get_drvinfo(struct net_device *net_dev,
165                                     struct ethtool_drvinfo *info)
166 {
167         struct efx_nic *efx = netdev_priv(net_dev);
168
169         strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
170         strlcpy(info->version, EFX_DRIVER_VERSION, sizeof(info->version));
171         if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
172                 efx_mcdi_print_fwver(efx, info->fw_version,
173                                      sizeof(info->fw_version));
174         strlcpy(info->bus_info, pci_name(efx->pci_dev), sizeof(info->bus_info));
175 }
176
177 static int efx_ethtool_get_regs_len(struct net_device *net_dev)
178 {
179         return efx_nic_get_regs_len(netdev_priv(net_dev));
180 }
181
182 static void efx_ethtool_get_regs(struct net_device *net_dev,
183                                  struct ethtool_regs *regs, void *buf)
184 {
185         struct efx_nic *efx = netdev_priv(net_dev);
186
187         regs->version = efx->type->revision;
188         efx_nic_get_regs(efx, buf);
189 }
190
191 static u32 efx_ethtool_get_msglevel(struct net_device *net_dev)
192 {
193         struct efx_nic *efx = netdev_priv(net_dev);
194         return efx->msg_enable;
195 }
196
197 static void efx_ethtool_set_msglevel(struct net_device *net_dev, u32 msg_enable)
198 {
199         struct efx_nic *efx = netdev_priv(net_dev);
200         efx->msg_enable = msg_enable;
201 }
202
203 /**
204  * efx_fill_test - fill in an individual self-test entry
205  * @test_index:         Index of the test
206  * @strings:            Ethtool strings, or %NULL
207  * @data:               Ethtool test results, or %NULL
208  * @test:               Pointer to test result (used only if data != %NULL)
209  * @unit_format:        Unit name format (e.g. "chan\%d")
210  * @unit_id:            Unit id (e.g. 0 for "chan0")
211  * @test_format:        Test name format (e.g. "loopback.\%s.tx.sent")
212  * @test_id:            Test id (e.g. "PHYXS" for "loopback.PHYXS.tx_sent")
213  *
214  * Fill in an individual self-test entry.
215  */
216 static void efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,
217                           int *test, const char *unit_format, int unit_id,
218                           const char *test_format, const char *test_id)
219 {
220         char unit_str[ETH_GSTRING_LEN], test_str[ETH_GSTRING_LEN];
221
222         /* Fill data value, if applicable */
223         if (data)
224                 data[test_index] = *test;
225
226         /* Fill string, if applicable */
227         if (strings) {
228                 if (strchr(unit_format, '%'))
229                         snprintf(unit_str, sizeof(unit_str),
230                                  unit_format, unit_id);
231                 else
232                         strcpy(unit_str, unit_format);
233                 snprintf(test_str, sizeof(test_str), test_format, test_id);
234                 snprintf(strings + test_index * ETH_GSTRING_LEN,
235                          ETH_GSTRING_LEN,
236                          "%-6s %-24s", unit_str, test_str);
237         }
238 }
239
240 #define EFX_CHANNEL_NAME(_channel) "chan%d", _channel->channel
241 #define EFX_TX_QUEUE_NAME(_tx_queue) "txq%d", _tx_queue->queue
242 #define EFX_RX_QUEUE_NAME(_rx_queue) "rxq%d", _rx_queue->queue
243 #define EFX_LOOPBACK_NAME(_mode, _counter)                      \
244         "loopback.%s." _counter, STRING_TABLE_LOOKUP(_mode, efx_loopback_mode)
245
246 /**
247  * efx_fill_loopback_test - fill in a block of loopback self-test entries
248  * @efx:                Efx NIC
249  * @lb_tests:           Efx loopback self-test results structure
250  * @mode:               Loopback test mode
251  * @test_index:         Starting index of the test
252  * @strings:            Ethtool strings, or %NULL
253  * @data:               Ethtool test results, or %NULL
254  */
255 static int efx_fill_loopback_test(struct efx_nic *efx,
256                                   struct efx_loopback_self_tests *lb_tests,
257                                   enum efx_loopback_mode mode,
258                                   unsigned int test_index,
259                                   u8 *strings, u64 *data)
260 {
261         struct efx_channel *channel =
262                 efx_get_channel(efx, efx->tx_channel_offset);
263         struct efx_tx_queue *tx_queue;
264
265         efx_for_each_channel_tx_queue(tx_queue, channel) {
266                 efx_fill_test(test_index++, strings, data,
267                               &lb_tests->tx_sent[tx_queue->queue],
268                               EFX_TX_QUEUE_NAME(tx_queue),
269                               EFX_LOOPBACK_NAME(mode, "tx_sent"));
270                 efx_fill_test(test_index++, strings, data,
271                               &lb_tests->tx_done[tx_queue->queue],
272                               EFX_TX_QUEUE_NAME(tx_queue),
273                               EFX_LOOPBACK_NAME(mode, "tx_done"));
274         }
275         efx_fill_test(test_index++, strings, data,
276                       &lb_tests->rx_good,
277                       "rx", 0,
278                       EFX_LOOPBACK_NAME(mode, "rx_good"));
279         efx_fill_test(test_index++, strings, data,
280                       &lb_tests->rx_bad,
281                       "rx", 0,
282                       EFX_LOOPBACK_NAME(mode, "rx_bad"));
283
284         return test_index;
285 }
286
287 /**
288  * efx_ethtool_fill_self_tests - get self-test details
289  * @efx:                Efx NIC
290  * @tests:              Efx self-test results structure, or %NULL
291  * @strings:            Ethtool strings, or %NULL
292  * @data:               Ethtool test results, or %NULL
293  */
294 static int efx_ethtool_fill_self_tests(struct efx_nic *efx,
295                                        struct efx_self_tests *tests,
296                                        u8 *strings, u64 *data)
297 {
298         struct efx_channel *channel;
299         unsigned int n = 0, i;
300         enum efx_loopback_mode mode;
301
302         efx_fill_test(n++, strings, data, &tests->phy_alive,
303                       "phy", 0, "alive", NULL);
304         efx_fill_test(n++, strings, data, &tests->nvram,
305                       "core", 0, "nvram", NULL);
306         efx_fill_test(n++, strings, data, &tests->interrupt,
307                       "core", 0, "interrupt", NULL);
308
309         /* Event queues */
310         efx_for_each_channel(channel, efx) {
311                 efx_fill_test(n++, strings, data,
312                               &tests->eventq_dma[channel->channel],
313                               EFX_CHANNEL_NAME(channel),
314                               "eventq.dma", NULL);
315                 efx_fill_test(n++, strings, data,
316                               &tests->eventq_int[channel->channel],
317                               EFX_CHANNEL_NAME(channel),
318                               "eventq.int", NULL);
319         }
320
321         efx_fill_test(n++, strings, data, &tests->memory,
322                       "core", 0, "memory", NULL);
323         efx_fill_test(n++, strings, data, &tests->registers,
324                       "core", 0, "registers", NULL);
325
326         if (efx->phy_op->run_tests != NULL) {
327                 EFX_BUG_ON_PARANOID(efx->phy_op->test_name == NULL);
328
329                 for (i = 0; true; ++i) {
330                         const char *name;
331
332                         EFX_BUG_ON_PARANOID(i >= EFX_MAX_PHY_TESTS);
333                         name = efx->phy_op->test_name(efx, i);
334                         if (name == NULL)
335                                 break;
336
337                         efx_fill_test(n++, strings, data, &tests->phy_ext[i],
338                                       "phy", 0, name, NULL);
339                 }
340         }
341
342         /* Loopback tests */
343         for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
344                 if (!(efx->loopback_modes & (1 << mode)))
345                         continue;
346                 n = efx_fill_loopback_test(efx,
347                                            &tests->loopback[mode], mode, n,
348                                            strings, data);
349         }
350
351         return n;
352 }
353
354 static int efx_ethtool_get_sset_count(struct net_device *net_dev,
355                                       int string_set)
356 {
357         struct efx_nic *efx = netdev_priv(net_dev);
358
359         switch (string_set) {
360         case ETH_SS_STATS:
361                 return efx->type->describe_stats(efx, NULL) +
362                         EFX_ETHTOOL_SW_STAT_COUNT +
363                         efx_ptp_describe_stats(efx, NULL);
364         case ETH_SS_TEST:
365                 return efx_ethtool_fill_self_tests(efx, NULL, NULL, NULL);
366         default:
367                 return -EINVAL;
368         }
369 }
370
371 static void efx_ethtool_get_strings(struct net_device *net_dev,
372                                     u32 string_set, u8 *strings)
373 {
374         struct efx_nic *efx = netdev_priv(net_dev);
375         int i;
376
377         switch (string_set) {
378         case ETH_SS_STATS:
379                 strings += (efx->type->describe_stats(efx, strings) *
380                             ETH_GSTRING_LEN);
381                 for (i = 0; i < EFX_ETHTOOL_SW_STAT_COUNT; i++)
382                         strlcpy(strings + i * ETH_GSTRING_LEN,
383                                 efx_sw_stat_desc[i].name, ETH_GSTRING_LEN);
384                 strings += EFX_ETHTOOL_SW_STAT_COUNT * ETH_GSTRING_LEN;
385                 efx_ptp_describe_stats(efx, strings);
386                 break;
387         case ETH_SS_TEST:
388                 efx_ethtool_fill_self_tests(efx, NULL, strings, NULL);
389                 break;
390         default:
391                 /* No other string sets */
392                 break;
393         }
394 }
395
396 static void efx_ethtool_get_stats(struct net_device *net_dev,
397                                   struct ethtool_stats *stats,
398                                   u64 *data)
399 {
400         struct efx_nic *efx = netdev_priv(net_dev);
401         const struct efx_sw_stat_desc *stat;
402         struct efx_channel *channel;
403         struct efx_tx_queue *tx_queue;
404         int i;
405
406         spin_lock_bh(&efx->stats_lock);
407
408         /* Get NIC statistics */
409         data += efx->type->update_stats(efx, data, NULL);
410
411         /* Get software statistics */
412         for (i = 0; i < EFX_ETHTOOL_SW_STAT_COUNT; i++) {
413                 stat = &efx_sw_stat_desc[i];
414                 switch (stat->source) {
415                 case EFX_ETHTOOL_STAT_SOURCE_nic:
416                         data[i] = stat->get_stat((void *)efx + stat->offset);
417                         break;
418                 case EFX_ETHTOOL_STAT_SOURCE_channel:
419                         data[i] = 0;
420                         efx_for_each_channel(channel, efx)
421                                 data[i] += stat->get_stat((void *)channel +
422                                                           stat->offset);
423                         break;
424                 case EFX_ETHTOOL_STAT_SOURCE_tx_queue:
425                         data[i] = 0;
426                         efx_for_each_channel(channel, efx) {
427                                 efx_for_each_channel_tx_queue(tx_queue, channel)
428                                         data[i] +=
429                                                 stat->get_stat((void *)tx_queue
430                                                                + stat->offset);
431                         }
432                         break;
433                 }
434         }
435         data += EFX_ETHTOOL_SW_STAT_COUNT;
436
437         spin_unlock_bh(&efx->stats_lock);
438
439         efx_ptp_update_stats(efx, data);
440 }
441
442 static void efx_ethtool_self_test(struct net_device *net_dev,
443                                   struct ethtool_test *test, u64 *data)
444 {
445         struct efx_nic *efx = netdev_priv(net_dev);
446         struct efx_self_tests *efx_tests;
447         int already_up;
448         int rc = -ENOMEM;
449
450         efx_tests = kzalloc(sizeof(*efx_tests), GFP_KERNEL);
451         if (!efx_tests)
452                 goto fail;
453
454         if (efx->state != STATE_READY) {
455                 rc = -EIO;
456                 goto fail1;
457         }
458
459         netif_info(efx, drv, efx->net_dev, "starting %sline testing\n",
460                    (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on");
461
462         /* We need rx buffers and interrupts. */
463         already_up = (efx->net_dev->flags & IFF_UP);
464         if (!already_up) {
465                 rc = dev_open(efx->net_dev);
466                 if (rc) {
467                         netif_err(efx, drv, efx->net_dev,
468                                   "failed opening device.\n");
469                         goto fail1;
470                 }
471         }
472
473         rc = efx_selftest(efx, efx_tests, test->flags);
474
475         if (!already_up)
476                 dev_close(efx->net_dev);
477
478         netif_info(efx, drv, efx->net_dev, "%s %sline self-tests\n",
479                    rc == 0 ? "passed" : "failed",
480                    (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on");
481
482 fail1:
483         /* Fill ethtool results structures */
484         efx_ethtool_fill_self_tests(efx, efx_tests, NULL, data);
485         kfree(efx_tests);
486 fail:
487         if (rc)
488                 test->flags |= ETH_TEST_FL_FAILED;
489 }
490
491 /* Restart autonegotiation */
492 static int efx_ethtool_nway_reset(struct net_device *net_dev)
493 {
494         struct efx_nic *efx = netdev_priv(net_dev);
495
496         return mdio45_nway_restart(&efx->mdio);
497 }
498
499 /*
500  * Each channel has a single IRQ and moderation timer, started by any
501  * completion (or other event).  Unless the module parameter
502  * separate_tx_channels is set, IRQs and moderation are therefore
503  * shared between RX and TX completions.  In this case, when RX IRQ
504  * moderation is explicitly changed then TX IRQ moderation is
505  * automatically changed too, but otherwise we fail if the two values
506  * are requested to be different.
507  *
508  * The hardware does not support a limit on the number of completions
509  * before an IRQ, so we do not use the max_frames fields.  We should
510  * report and require that max_frames == (usecs != 0), but this would
511  * invalidate existing user documentation.
512  *
513  * The hardware does not have distinct settings for interrupt
514  * moderation while the previous IRQ is being handled, so we should
515  * not use the 'irq' fields.  However, an earlier developer
516  * misunderstood the meaning of the 'irq' fields and the driver did
517  * not support the standard fields.  To avoid invalidating existing
518  * user documentation, we report and accept changes through either the
519  * standard or 'irq' fields.  If both are changed at the same time, we
520  * prefer the standard field.
521  *
522  * We implement adaptive IRQ moderation, but use a different algorithm
523  * from that assumed in the definition of struct ethtool_coalesce.
524  * Therefore we do not use any of the adaptive moderation parameters
525  * in it.
526  */
527
528 static int efx_ethtool_get_coalesce(struct net_device *net_dev,
529                                     struct ethtool_coalesce *coalesce)
530 {
531         struct efx_nic *efx = netdev_priv(net_dev);
532         unsigned int tx_usecs, rx_usecs;
533         bool rx_adaptive;
534
535         efx_get_irq_moderation(efx, &tx_usecs, &rx_usecs, &rx_adaptive);
536
537         coalesce->tx_coalesce_usecs = tx_usecs;
538         coalesce->tx_coalesce_usecs_irq = tx_usecs;
539         coalesce->rx_coalesce_usecs = rx_usecs;
540         coalesce->rx_coalesce_usecs_irq = rx_usecs;
541         coalesce->use_adaptive_rx_coalesce = rx_adaptive;
542
543         return 0;
544 }
545
546 static int efx_ethtool_set_coalesce(struct net_device *net_dev,
547                                     struct ethtool_coalesce *coalesce)
548 {
549         struct efx_nic *efx = netdev_priv(net_dev);
550         struct efx_channel *channel;
551         unsigned int tx_usecs, rx_usecs;
552         bool adaptive, rx_may_override_tx;
553         int rc;
554
555         if (coalesce->use_adaptive_tx_coalesce)
556                 return -EINVAL;
557
558         efx_get_irq_moderation(efx, &tx_usecs, &rx_usecs, &adaptive);
559
560         if (coalesce->rx_coalesce_usecs != rx_usecs)
561                 rx_usecs = coalesce->rx_coalesce_usecs;
562         else
563                 rx_usecs = coalesce->rx_coalesce_usecs_irq;
564
565         adaptive = coalesce->use_adaptive_rx_coalesce;
566
567         /* If channels are shared, TX IRQ moderation can be quietly
568          * overridden unless it is changed from its old value.
569          */
570         rx_may_override_tx = (coalesce->tx_coalesce_usecs == tx_usecs &&
571                               coalesce->tx_coalesce_usecs_irq == tx_usecs);
572         if (coalesce->tx_coalesce_usecs != tx_usecs)
573                 tx_usecs = coalesce->tx_coalesce_usecs;
574         else
575                 tx_usecs = coalesce->tx_coalesce_usecs_irq;
576
577         rc = efx_init_irq_moderation(efx, tx_usecs, rx_usecs, adaptive,
578                                      rx_may_override_tx);
579         if (rc != 0)
580                 return rc;
581
582         efx_for_each_channel(channel, efx)
583                 efx->type->push_irq_moderation(channel);
584
585         return 0;
586 }
587
588 static void efx_ethtool_get_ringparam(struct net_device *net_dev,
589                                       struct ethtool_ringparam *ring)
590 {
591         struct efx_nic *efx = netdev_priv(net_dev);
592
593         ring->rx_max_pending = EFX_MAX_DMAQ_SIZE;
594         ring->tx_max_pending = EFX_TXQ_MAX_ENT(efx);
595         ring->rx_pending = efx->rxq_entries;
596         ring->tx_pending = efx->txq_entries;
597 }
598
599 static int efx_ethtool_set_ringparam(struct net_device *net_dev,
600                                      struct ethtool_ringparam *ring)
601 {
602         struct efx_nic *efx = netdev_priv(net_dev);
603         u32 txq_entries;
604
605         if (ring->rx_mini_pending || ring->rx_jumbo_pending ||
606             ring->rx_pending > EFX_MAX_DMAQ_SIZE ||
607             ring->tx_pending > EFX_TXQ_MAX_ENT(efx))
608                 return -EINVAL;
609
610         if (ring->rx_pending < EFX_RXQ_MIN_ENT) {
611                 netif_err(efx, drv, efx->net_dev,
612                           "RX queues cannot be smaller than %u\n",
613                           EFX_RXQ_MIN_ENT);
614                 return -EINVAL;
615         }
616
617         txq_entries = max(ring->tx_pending, EFX_TXQ_MIN_ENT(efx));
618         if (txq_entries != ring->tx_pending)
619                 netif_warn(efx, drv, efx->net_dev,
620                            "increasing TX queue size to minimum of %u\n",
621                            txq_entries);
622
623         return efx_realloc_channels(efx, ring->rx_pending, txq_entries);
624 }
625
626 static int efx_ethtool_set_pauseparam(struct net_device *net_dev,
627                                       struct ethtool_pauseparam *pause)
628 {
629         struct efx_nic *efx = netdev_priv(net_dev);
630         u8 wanted_fc, old_fc;
631         u32 old_adv;
632         int rc = 0;
633
634         mutex_lock(&efx->mac_lock);
635
636         wanted_fc = ((pause->rx_pause ? EFX_FC_RX : 0) |
637                      (pause->tx_pause ? EFX_FC_TX : 0) |
638                      (pause->autoneg ? EFX_FC_AUTO : 0));
639
640         if ((wanted_fc & EFX_FC_TX) && !(wanted_fc & EFX_FC_RX)) {
641                 netif_dbg(efx, drv, efx->net_dev,
642                           "Flow control unsupported: tx ON rx OFF\n");
643                 rc = -EINVAL;
644                 goto out;
645         }
646
647         if ((wanted_fc & EFX_FC_AUTO) && !efx->link_advertising) {
648                 netif_dbg(efx, drv, efx->net_dev,
649                           "Autonegotiation is disabled\n");
650                 rc = -EINVAL;
651                 goto out;
652         }
653
654         /* Hook for Falcon bug 11482 workaround */
655         if (efx->type->prepare_enable_fc_tx &&
656             (wanted_fc & EFX_FC_TX) && !(efx->wanted_fc & EFX_FC_TX))
657                 efx->type->prepare_enable_fc_tx(efx);
658
659         old_adv = efx->link_advertising;
660         old_fc = efx->wanted_fc;
661         efx_link_set_wanted_fc(efx, wanted_fc);
662         if (efx->link_advertising != old_adv ||
663             (efx->wanted_fc ^ old_fc) & EFX_FC_AUTO) {
664                 rc = efx->phy_op->reconfigure(efx);
665                 if (rc) {
666                         netif_err(efx, drv, efx->net_dev,
667                                   "Unable to advertise requested flow "
668                                   "control setting\n");
669                         goto out;
670                 }
671         }
672
673         /* Reconfigure the MAC. The PHY *may* generate a link state change event
674          * if the user just changed the advertised capabilities, but there's no
675          * harm doing this twice */
676         efx->type->reconfigure_mac(efx);
677
678 out:
679         mutex_unlock(&efx->mac_lock);
680
681         return rc;
682 }
683
684 static void efx_ethtool_get_pauseparam(struct net_device *net_dev,
685                                        struct ethtool_pauseparam *pause)
686 {
687         struct efx_nic *efx = netdev_priv(net_dev);
688
689         pause->rx_pause = !!(efx->wanted_fc & EFX_FC_RX);
690         pause->tx_pause = !!(efx->wanted_fc & EFX_FC_TX);
691         pause->autoneg = !!(efx->wanted_fc & EFX_FC_AUTO);
692 }
693
694
695 static void efx_ethtool_get_wol(struct net_device *net_dev,
696                                 struct ethtool_wolinfo *wol)
697 {
698         struct efx_nic *efx = netdev_priv(net_dev);
699         return efx->type->get_wol(efx, wol);
700 }
701
702
703 static int efx_ethtool_set_wol(struct net_device *net_dev,
704                                struct ethtool_wolinfo *wol)
705 {
706         struct efx_nic *efx = netdev_priv(net_dev);
707         return efx->type->set_wol(efx, wol->wolopts);
708 }
709
710 static int efx_ethtool_reset(struct net_device *net_dev, u32 *flags)
711 {
712         struct efx_nic *efx = netdev_priv(net_dev);
713         int rc;
714
715         rc = efx->type->map_reset_flags(flags);
716         if (rc < 0)
717                 return rc;
718
719         return efx_reset(efx, rc);
720 }
721
722 /* MAC address mask including only I/G bit */
723 static const u8 mac_addr_ig_mask[ETH_ALEN] = { 0x01, 0, 0, 0, 0, 0 };
724
725 #define IP4_ADDR_FULL_MASK      ((__force __be32)~0)
726 #define PORT_FULL_MASK          ((__force __be16)~0)
727 #define ETHER_TYPE_FULL_MASK    ((__force __be16)~0)
728
729 static int efx_ethtool_get_class_rule(struct efx_nic *efx,
730                                       struct ethtool_rx_flow_spec *rule)
731 {
732         struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec;
733         struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec;
734         struct ethhdr *mac_entry = &rule->h_u.ether_spec;
735         struct ethhdr *mac_mask = &rule->m_u.ether_spec;
736         struct efx_filter_spec spec;
737         int rc;
738
739         rc = efx_filter_get_filter_safe(efx, EFX_FILTER_PRI_MANUAL,
740                                         rule->location, &spec);
741         if (rc)
742                 return rc;
743
744         if (spec.dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
745                 rule->ring_cookie = RX_CLS_FLOW_DISC;
746         else
747                 rule->ring_cookie = spec.dmaq_id;
748
749         if ((spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) &&
750             spec.ether_type == htons(ETH_P_IP) &&
751             (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) &&
752             (spec.ip_proto == IPPROTO_TCP || spec.ip_proto == IPPROTO_UDP) &&
753             !(spec.match_flags &
754               ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
755                 EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
756                 EFX_FILTER_MATCH_IP_PROTO |
757                 EFX_FILTER_MATCH_LOC_PORT | EFX_FILTER_MATCH_REM_PORT))) {
758                 rule->flow_type = ((spec.ip_proto == IPPROTO_TCP) ?
759                                    TCP_V4_FLOW : UDP_V4_FLOW);
760                 if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
761                         ip_entry->ip4dst = spec.loc_host[0];
762                         ip_mask->ip4dst = IP4_ADDR_FULL_MASK;
763                 }
764                 if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
765                         ip_entry->ip4src = spec.rem_host[0];
766                         ip_mask->ip4src = IP4_ADDR_FULL_MASK;
767                 }
768                 if (spec.match_flags & EFX_FILTER_MATCH_LOC_PORT) {
769                         ip_entry->pdst = spec.loc_port;
770                         ip_mask->pdst = PORT_FULL_MASK;
771                 }
772                 if (spec.match_flags & EFX_FILTER_MATCH_REM_PORT) {
773                         ip_entry->psrc = spec.rem_port;
774                         ip_mask->psrc = PORT_FULL_MASK;
775                 }
776         } else if (!(spec.match_flags &
777                      ~(EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG |
778                        EFX_FILTER_MATCH_REM_MAC | EFX_FILTER_MATCH_ETHER_TYPE |
779                        EFX_FILTER_MATCH_OUTER_VID))) {
780                 rule->flow_type = ETHER_FLOW;
781                 if (spec.match_flags &
782                     (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG)) {
783                         memcpy(mac_entry->h_dest, spec.loc_mac, ETH_ALEN);
784                         if (spec.match_flags & EFX_FILTER_MATCH_LOC_MAC)
785                                 memset(mac_mask->h_dest, ~0, ETH_ALEN);
786                         else
787                                 memcpy(mac_mask->h_dest, mac_addr_ig_mask,
788                                        ETH_ALEN);
789                 }
790                 if (spec.match_flags & EFX_FILTER_MATCH_REM_MAC) {
791                         memcpy(mac_entry->h_source, spec.rem_mac, ETH_ALEN);
792                         memset(mac_mask->h_source, ~0, ETH_ALEN);
793                 }
794                 if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) {
795                         mac_entry->h_proto = spec.ether_type;
796                         mac_mask->h_proto = ETHER_TYPE_FULL_MASK;
797                 }
798         } else {
799                 /* The above should handle all filters that we insert */
800                 WARN_ON(1);
801                 return -EINVAL;
802         }
803
804         if (spec.match_flags & EFX_FILTER_MATCH_OUTER_VID) {
805                 rule->flow_type |= FLOW_EXT;
806                 rule->h_ext.vlan_tci = spec.outer_vid;
807                 rule->m_ext.vlan_tci = htons(0xfff);
808         }
809
810         return rc;
811 }
812
813 static int
814 efx_ethtool_get_rxnfc(struct net_device *net_dev,
815                       struct ethtool_rxnfc *info, u32 *rule_locs)
816 {
817         struct efx_nic *efx = netdev_priv(net_dev);
818
819         switch (info->cmd) {
820         case ETHTOOL_GRXRINGS:
821                 info->data = efx->n_rx_channels;
822                 return 0;
823
824         case ETHTOOL_GRXFH: {
825                 unsigned min_revision = 0;
826
827                 info->data = 0;
828                 switch (info->flow_type) {
829                 case TCP_V4_FLOW:
830                         info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
831                         /* fall through */
832                 case UDP_V4_FLOW:
833                 case SCTP_V4_FLOW:
834                 case AH_ESP_V4_FLOW:
835                 case IPV4_FLOW:
836                         info->data |= RXH_IP_SRC | RXH_IP_DST;
837                         min_revision = EFX_REV_FALCON_B0;
838                         break;
839                 case TCP_V6_FLOW:
840                         info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
841                         /* fall through */
842                 case UDP_V6_FLOW:
843                 case SCTP_V6_FLOW:
844                 case AH_ESP_V6_FLOW:
845                 case IPV6_FLOW:
846                         info->data |= RXH_IP_SRC | RXH_IP_DST;
847                         min_revision = EFX_REV_SIENA_A0;
848                         break;
849                 default:
850                         break;
851                 }
852                 if (efx_nic_rev(efx) < min_revision)
853                         info->data = 0;
854                 return 0;
855         }
856
857         case ETHTOOL_GRXCLSRLCNT:
858                 info->data = efx_filter_get_rx_id_limit(efx);
859                 if (info->data == 0)
860                         return -EOPNOTSUPP;
861                 info->data |= RX_CLS_LOC_SPECIAL;
862                 info->rule_cnt =
863                         efx_filter_count_rx_used(efx, EFX_FILTER_PRI_MANUAL);
864                 return 0;
865
866         case ETHTOOL_GRXCLSRULE:
867                 if (efx_filter_get_rx_id_limit(efx) == 0)
868                         return -EOPNOTSUPP;
869                 return efx_ethtool_get_class_rule(efx, &info->fs);
870
871         case ETHTOOL_GRXCLSRLALL: {
872                 s32 rc;
873                 info->data = efx_filter_get_rx_id_limit(efx);
874                 if (info->data == 0)
875                         return -EOPNOTSUPP;
876                 rc = efx_filter_get_rx_ids(efx, EFX_FILTER_PRI_MANUAL,
877                                            rule_locs, info->rule_cnt);
878                 if (rc < 0)
879                         return rc;
880                 info->rule_cnt = rc;
881                 return 0;
882         }
883
884         default:
885                 return -EOPNOTSUPP;
886         }
887 }
888
889 static int efx_ethtool_set_class_rule(struct efx_nic *efx,
890                                       struct ethtool_rx_flow_spec *rule)
891 {
892         struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec;
893         struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec;
894         struct ethhdr *mac_entry = &rule->h_u.ether_spec;
895         struct ethhdr *mac_mask = &rule->m_u.ether_spec;
896         struct efx_filter_spec spec;
897         int rc;
898
899         /* Check that user wants us to choose the location */
900         if (rule->location != RX_CLS_LOC_ANY)
901                 return -EINVAL;
902
903         /* Range-check ring_cookie */
904         if (rule->ring_cookie >= efx->n_rx_channels &&
905             rule->ring_cookie != RX_CLS_FLOW_DISC)
906                 return -EINVAL;
907
908         /* Check for unsupported extensions */
909         if ((rule->flow_type & FLOW_EXT) &&
910             (rule->m_ext.vlan_etype || rule->m_ext.data[0] ||
911              rule->m_ext.data[1]))
912                 return -EINVAL;
913
914         efx_filter_init_rx(&spec, EFX_FILTER_PRI_MANUAL,
915                            efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0,
916                            (rule->ring_cookie == RX_CLS_FLOW_DISC) ?
917                            EFX_FILTER_RX_DMAQ_ID_DROP : rule->ring_cookie);
918
919         switch (rule->flow_type & ~FLOW_EXT) {
920         case TCP_V4_FLOW:
921         case UDP_V4_FLOW:
922                 spec.match_flags = (EFX_FILTER_MATCH_ETHER_TYPE |
923                                     EFX_FILTER_MATCH_IP_PROTO);
924                 spec.ether_type = htons(ETH_P_IP);
925                 spec.ip_proto = ((rule->flow_type & ~FLOW_EXT) == TCP_V4_FLOW ?
926                                  IPPROTO_TCP : IPPROTO_UDP);
927                 if (ip_mask->ip4dst) {
928                         if (ip_mask->ip4dst != IP4_ADDR_FULL_MASK)
929                                 return -EINVAL;
930                         spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
931                         spec.loc_host[0] = ip_entry->ip4dst;
932                 }
933                 if (ip_mask->ip4src) {
934                         if (ip_mask->ip4src != IP4_ADDR_FULL_MASK)
935                                 return -EINVAL;
936                         spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
937                         spec.rem_host[0] = ip_entry->ip4src;
938                 }
939                 if (ip_mask->pdst) {
940                         if (ip_mask->pdst != PORT_FULL_MASK)
941                                 return -EINVAL;
942                         spec.match_flags |= EFX_FILTER_MATCH_LOC_PORT;
943                         spec.loc_port = ip_entry->pdst;
944                 }
945                 if (ip_mask->psrc) {
946                         if (ip_mask->psrc != PORT_FULL_MASK)
947                                 return -EINVAL;
948                         spec.match_flags |= EFX_FILTER_MATCH_REM_PORT;
949                         spec.rem_port = ip_entry->psrc;
950                 }
951                 if (ip_mask->tos)
952                         return -EINVAL;
953                 break;
954
955         case ETHER_FLOW:
956                 if (!is_zero_ether_addr(mac_mask->h_dest)) {
957                         if (ether_addr_equal(mac_mask->h_dest,
958                                              mac_addr_ig_mask))
959                                 spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC_IG;
960                         else if (is_broadcast_ether_addr(mac_mask->h_dest))
961                                 spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC;
962                         else
963                                 return -EINVAL;
964                         memcpy(spec.loc_mac, mac_entry->h_dest, ETH_ALEN);
965                 }
966                 if (!is_zero_ether_addr(mac_mask->h_source)) {
967                         if (!is_broadcast_ether_addr(mac_mask->h_source))
968                                 return -EINVAL;
969                         spec.match_flags |= EFX_FILTER_MATCH_REM_MAC;
970                         memcpy(spec.rem_mac, mac_entry->h_source, ETH_ALEN);
971                 }
972                 if (mac_mask->h_proto) {
973                         if (mac_mask->h_proto != ETHER_TYPE_FULL_MASK)
974                                 return -EINVAL;
975                         spec.match_flags |= EFX_FILTER_MATCH_ETHER_TYPE;
976                         spec.ether_type = mac_entry->h_proto;
977                 }
978                 break;
979
980         default:
981                 return -EINVAL;
982         }
983
984         if ((rule->flow_type & FLOW_EXT) && rule->m_ext.vlan_tci) {
985                 if (rule->m_ext.vlan_tci != htons(0xfff))
986                         return -EINVAL;
987                 spec.match_flags |= EFX_FILTER_MATCH_OUTER_VID;
988                 spec.outer_vid = rule->h_ext.vlan_tci;
989         }
990
991         rc = efx_filter_insert_filter(efx, &spec, true);
992         if (rc < 0)
993                 return rc;
994
995         rule->location = rc;
996         return 0;
997 }
998
999 static int efx_ethtool_set_rxnfc(struct net_device *net_dev,
1000                                  struct ethtool_rxnfc *info)
1001 {
1002         struct efx_nic *efx = netdev_priv(net_dev);
1003
1004         if (efx_filter_get_rx_id_limit(efx) == 0)
1005                 return -EOPNOTSUPP;
1006
1007         switch (info->cmd) {
1008         case ETHTOOL_SRXCLSRLINS:
1009                 return efx_ethtool_set_class_rule(efx, &info->fs);
1010
1011         case ETHTOOL_SRXCLSRLDEL:
1012                 return efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_MANUAL,
1013                                                  info->fs.location);
1014
1015         default:
1016                 return -EOPNOTSUPP;
1017         }
1018 }
1019
1020 static u32 efx_ethtool_get_rxfh_indir_size(struct net_device *net_dev)
1021 {
1022         struct efx_nic *efx = netdev_priv(net_dev);
1023
1024         return ((efx_nic_rev(efx) < EFX_REV_FALCON_B0 ||
1025                  efx->n_rx_channels == 1) ?
1026                 0 : ARRAY_SIZE(efx->rx_indir_table));
1027 }
1028
1029 static int efx_ethtool_get_rxfh_indir(struct net_device *net_dev, u32 *indir)
1030 {
1031         struct efx_nic *efx = netdev_priv(net_dev);
1032
1033         memcpy(indir, efx->rx_indir_table, sizeof(efx->rx_indir_table));
1034         return 0;
1035 }
1036
1037 static int efx_ethtool_set_rxfh_indir(struct net_device *net_dev,
1038                                       const u32 *indir)
1039 {
1040         struct efx_nic *efx = netdev_priv(net_dev);
1041
1042         memcpy(efx->rx_indir_table, indir, sizeof(efx->rx_indir_table));
1043         efx->type->rx_push_rss_config(efx);
1044         return 0;
1045 }
1046
1047 static int efx_ethtool_get_ts_info(struct net_device *net_dev,
1048                                    struct ethtool_ts_info *ts_info)
1049 {
1050         struct efx_nic *efx = netdev_priv(net_dev);
1051
1052         /* Software capabilities */
1053         ts_info->so_timestamping = (SOF_TIMESTAMPING_RX_SOFTWARE |
1054                                     SOF_TIMESTAMPING_SOFTWARE);
1055         ts_info->phc_index = -1;
1056
1057         efx_ptp_get_ts_info(efx, ts_info);
1058         return 0;
1059 }
1060
1061 static int efx_ethtool_get_module_eeprom(struct net_device *net_dev,
1062                                          struct ethtool_eeprom *ee,
1063                                          u8 *data)
1064 {
1065         struct efx_nic *efx = netdev_priv(net_dev);
1066         int ret;
1067
1068         if (!efx->phy_op || !efx->phy_op->get_module_eeprom)
1069                 return -EOPNOTSUPP;
1070
1071         mutex_lock(&efx->mac_lock);
1072         ret = efx->phy_op->get_module_eeprom(efx, ee, data);
1073         mutex_unlock(&efx->mac_lock);
1074
1075         return ret;
1076 }
1077
1078 static int efx_ethtool_get_module_info(struct net_device *net_dev,
1079                                        struct ethtool_modinfo *modinfo)
1080 {
1081         struct efx_nic *efx = netdev_priv(net_dev);
1082         int ret;
1083
1084         if (!efx->phy_op || !efx->phy_op->get_module_info)
1085                 return -EOPNOTSUPP;
1086
1087         mutex_lock(&efx->mac_lock);
1088         ret = efx->phy_op->get_module_info(efx, modinfo);
1089         mutex_unlock(&efx->mac_lock);
1090
1091         return ret;
1092 }
1093
1094 const struct ethtool_ops efx_ethtool_ops = {
1095         .get_settings           = efx_ethtool_get_settings,
1096         .set_settings           = efx_ethtool_set_settings,
1097         .get_drvinfo            = efx_ethtool_get_drvinfo,
1098         .get_regs_len           = efx_ethtool_get_regs_len,
1099         .get_regs               = efx_ethtool_get_regs,
1100         .get_msglevel           = efx_ethtool_get_msglevel,
1101         .set_msglevel           = efx_ethtool_set_msglevel,
1102         .nway_reset             = efx_ethtool_nway_reset,
1103         .get_link               = ethtool_op_get_link,
1104         .get_coalesce           = efx_ethtool_get_coalesce,
1105         .set_coalesce           = efx_ethtool_set_coalesce,
1106         .get_ringparam          = efx_ethtool_get_ringparam,
1107         .set_ringparam          = efx_ethtool_set_ringparam,
1108         .get_pauseparam         = efx_ethtool_get_pauseparam,
1109         .set_pauseparam         = efx_ethtool_set_pauseparam,
1110         .get_sset_count         = efx_ethtool_get_sset_count,
1111         .self_test              = efx_ethtool_self_test,
1112         .get_strings            = efx_ethtool_get_strings,
1113         .set_phys_id            = efx_ethtool_phys_id,
1114         .get_ethtool_stats      = efx_ethtool_get_stats,
1115         .get_wol                = efx_ethtool_get_wol,
1116         .set_wol                = efx_ethtool_set_wol,
1117         .reset                  = efx_ethtool_reset,
1118         .get_rxnfc              = efx_ethtool_get_rxnfc,
1119         .set_rxnfc              = efx_ethtool_set_rxnfc,
1120         .get_rxfh_indir_size    = efx_ethtool_get_rxfh_indir_size,
1121         .get_rxfh_indir         = efx_ethtool_get_rxfh_indir,
1122         .set_rxfh_indir         = efx_ethtool_set_rxfh_indir,
1123         .get_ts_info            = efx_ethtool_get_ts_info,
1124         .get_module_info        = efx_ethtool_get_module_info,
1125         .get_module_eeprom      = efx_ethtool_get_module_eeprom,
1126 };