Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/jkirsher/net...
[firefly-linux-kernel-4.4.55.git] / drivers / net / ethernet / intel / i40e / i40e_ethtool.c
1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * The full GNU General Public License is included in this distribution in
20  * the file called "COPYING".
21  *
22  * Contact Information:
23  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25  *
26  ******************************************************************************/
27
28 /* ethtool support for i40e */
29
30 #include "i40e.h"
31 #include "i40e_diag.h"
32
33 struct i40e_stats {
34         char stat_string[ETH_GSTRING_LEN];
35         int sizeof_stat;
36         int stat_offset;
37 };
38
39 #define I40E_STAT(_type, _name, _stat) { \
40         .stat_string = _name, \
41         .sizeof_stat = FIELD_SIZEOF(_type, _stat), \
42         .stat_offset = offsetof(_type, _stat) \
43 }
44 #define I40E_NETDEV_STAT(_net_stat) \
45                 I40E_STAT(struct net_device_stats, #_net_stat, _net_stat)
46 #define I40E_PF_STAT(_name, _stat) \
47                 I40E_STAT(struct i40e_pf, _name, _stat)
48 #define I40E_VSI_STAT(_name, _stat) \
49                 I40E_STAT(struct i40e_vsi, _name, _stat)
50
51 static const struct i40e_stats i40e_gstrings_net_stats[] = {
52         I40E_NETDEV_STAT(rx_packets),
53         I40E_NETDEV_STAT(tx_packets),
54         I40E_NETDEV_STAT(rx_bytes),
55         I40E_NETDEV_STAT(tx_bytes),
56         I40E_NETDEV_STAT(rx_errors),
57         I40E_NETDEV_STAT(tx_errors),
58         I40E_NETDEV_STAT(rx_dropped),
59         I40E_NETDEV_STAT(tx_dropped),
60         I40E_NETDEV_STAT(multicast),
61         I40E_NETDEV_STAT(collisions),
62         I40E_NETDEV_STAT(rx_length_errors),
63         I40E_NETDEV_STAT(rx_crc_errors),
64 };
65
66 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
67  * but they are separate.  This device supports Virtualization, and
68  * as such might have several netdevs supporting VMDq and FCoE going
69  * through a single port.  The NETDEV_STATs are for individual netdevs
70  * seen at the top of the stack, and the PF_STATs are for the physical
71  * function at the bottom of the stack hosting those netdevs.
72  *
73  * The PF_STATs are appended to the netdev stats only when ethtool -S
74  * is queried on the base PF netdev, not on the VMDq or FCoE netdev.
75  */
76 static struct i40e_stats i40e_gstrings_stats[] = {
77         I40E_PF_STAT("rx_bytes", stats.eth.rx_bytes),
78         I40E_PF_STAT("tx_bytes", stats.eth.tx_bytes),
79         I40E_PF_STAT("rx_errors", stats.eth.rx_errors),
80         I40E_PF_STAT("tx_errors", stats.eth.tx_errors),
81         I40E_PF_STAT("rx_dropped", stats.eth.rx_discards),
82         I40E_PF_STAT("tx_dropped", stats.eth.tx_discards),
83         I40E_PF_STAT("tx_dropped_link_down", stats.tx_dropped_link_down),
84         I40E_PF_STAT("crc_errors", stats.crc_errors),
85         I40E_PF_STAT("illegal_bytes", stats.illegal_bytes),
86         I40E_PF_STAT("mac_local_faults", stats.mac_local_faults),
87         I40E_PF_STAT("mac_remote_faults", stats.mac_remote_faults),
88         I40E_PF_STAT("rx_length_errors", stats.rx_length_errors),
89         I40E_PF_STAT("link_xon_rx", stats.link_xon_rx),
90         I40E_PF_STAT("link_xoff_rx", stats.link_xoff_rx),
91         I40E_PF_STAT("link_xon_tx", stats.link_xon_tx),
92         I40E_PF_STAT("link_xoff_tx", stats.link_xoff_tx),
93         I40E_PF_STAT("rx_size_64", stats.rx_size_64),
94         I40E_PF_STAT("rx_size_127", stats.rx_size_127),
95         I40E_PF_STAT("rx_size_255", stats.rx_size_255),
96         I40E_PF_STAT("rx_size_511", stats.rx_size_511),
97         I40E_PF_STAT("rx_size_1023", stats.rx_size_1023),
98         I40E_PF_STAT("rx_size_1522", stats.rx_size_1522),
99         I40E_PF_STAT("rx_size_big", stats.rx_size_big),
100         I40E_PF_STAT("tx_size_64", stats.tx_size_64),
101         I40E_PF_STAT("tx_size_127", stats.tx_size_127),
102         I40E_PF_STAT("tx_size_255", stats.tx_size_255),
103         I40E_PF_STAT("tx_size_511", stats.tx_size_511),
104         I40E_PF_STAT("tx_size_1023", stats.tx_size_1023),
105         I40E_PF_STAT("tx_size_1522", stats.tx_size_1522),
106         I40E_PF_STAT("tx_size_big", stats.tx_size_big),
107         I40E_PF_STAT("rx_undersize", stats.rx_undersize),
108         I40E_PF_STAT("rx_fragments", stats.rx_fragments),
109         I40E_PF_STAT("rx_oversize", stats.rx_oversize),
110         I40E_PF_STAT("rx_jabber", stats.rx_jabber),
111         I40E_PF_STAT("VF_admin_queue_requests", vf_aq_requests),
112 };
113
114 #define I40E_QUEUE_STATS_LEN(n) \
115   ((((struct i40e_netdev_priv *)netdev_priv((n)))->vsi->num_queue_pairs + \
116     ((struct i40e_netdev_priv *)netdev_priv((n)))->vsi->num_queue_pairs) * 2)
117 #define I40E_GLOBAL_STATS_LEN   ARRAY_SIZE(i40e_gstrings_stats)
118 #define I40E_NETDEV_STATS_LEN   ARRAY_SIZE(i40e_gstrings_net_stats)
119 #define I40E_VSI_STATS_LEN(n)   (I40E_NETDEV_STATS_LEN + \
120                                  I40E_QUEUE_STATS_LEN((n)))
121 #define I40E_PFC_STATS_LEN ( \
122                 (FIELD_SIZEOF(struct i40e_pf, stats.priority_xoff_rx) + \
123                  FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_rx) + \
124                  FIELD_SIZEOF(struct i40e_pf, stats.priority_xoff_tx) + \
125                  FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_tx) + \
126                  FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_2_xoff)) \
127                  / sizeof(u64))
128 #define I40E_PF_STATS_LEN(n)    (I40E_GLOBAL_STATS_LEN + \
129                                  I40E_PFC_STATS_LEN + \
130                                  I40E_VSI_STATS_LEN((n)))
131
132 enum i40e_ethtool_test_id {
133         I40E_ETH_TEST_REG = 0,
134         I40E_ETH_TEST_EEPROM,
135         I40E_ETH_TEST_INTR,
136         I40E_ETH_TEST_LOOPBACK,
137         I40E_ETH_TEST_LINK,
138 };
139
140 static const char i40e_gstrings_test[][ETH_GSTRING_LEN] = {
141         "Register test  (offline)",
142         "Eeprom test    (offline)",
143         "Interrupt test (offline)",
144         "Loopback test  (offline)",
145         "Link test   (on/offline)"
146 };
147
148 #define I40E_TEST_LEN (sizeof(i40e_gstrings_test) / ETH_GSTRING_LEN)
149
150 /**
151  * i40e_get_settings - Get Link Speed and Duplex settings
152  * @netdev: network interface device structure
153  * @ecmd: ethtool command
154  *
155  * Reports speed/duplex settings based on media_type
156  **/
157 static int i40e_get_settings(struct net_device *netdev,
158                              struct ethtool_cmd *ecmd)
159 {
160         struct i40e_netdev_priv *np = netdev_priv(netdev);
161         struct i40e_pf *pf = np->vsi->back;
162         struct i40e_hw *hw = &pf->hw;
163         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
164         bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
165         u32 link_speed = hw_link_info->link_speed;
166
167         /* hardware is either in 40G mode or 10G mode
168          * NOTE: this section initializes supported and advertising
169          */
170         switch (hw_link_info->phy_type) {
171         case I40E_PHY_TYPE_40GBASE_CR4:
172         case I40E_PHY_TYPE_40GBASE_CR4_CU:
173                 ecmd->supported = SUPPORTED_40000baseCR4_Full;
174                 ecmd->advertising = ADVERTISED_40000baseCR4_Full;
175                 break;
176         case I40E_PHY_TYPE_40GBASE_KR4:
177                 ecmd->supported = SUPPORTED_40000baseKR4_Full;
178                 ecmd->advertising = ADVERTISED_40000baseKR4_Full;
179                 break;
180         case I40E_PHY_TYPE_40GBASE_SR4:
181                 ecmd->supported = SUPPORTED_40000baseSR4_Full;
182                 ecmd->advertising = ADVERTISED_40000baseSR4_Full;
183                 break;
184         case I40E_PHY_TYPE_40GBASE_LR4:
185                 ecmd->supported = SUPPORTED_40000baseLR4_Full;
186                 ecmd->advertising = ADVERTISED_40000baseLR4_Full;
187                 break;
188         case I40E_PHY_TYPE_10GBASE_KX4:
189                 ecmd->supported = SUPPORTED_10000baseKX4_Full;
190                 ecmd->advertising = ADVERTISED_10000baseKX4_Full;
191                 break;
192         case I40E_PHY_TYPE_10GBASE_KR:
193                 ecmd->supported = SUPPORTED_10000baseKR_Full;
194                 ecmd->advertising = ADVERTISED_10000baseKR_Full;
195                 break;
196         case I40E_PHY_TYPE_10GBASE_T:
197         default:
198                 ecmd->supported = SUPPORTED_10000baseT_Full;
199                 ecmd->advertising = ADVERTISED_10000baseT_Full;
200                 break;
201         }
202
203         /* for now just say autoneg all the time */
204         ecmd->supported |= SUPPORTED_Autoneg;
205
206         if (hw->phy.media_type == I40E_MEDIA_TYPE_BACKPLANE) {
207                 ecmd->supported |= SUPPORTED_Backplane;
208                 ecmd->advertising |= ADVERTISED_Backplane;
209                 ecmd->port = PORT_NONE;
210         } else if (hw->phy.media_type == I40E_MEDIA_TYPE_BASET) {
211                 ecmd->supported |= SUPPORTED_TP;
212                 ecmd->advertising |= ADVERTISED_TP;
213                 ecmd->port = PORT_TP;
214         } else {
215                 ecmd->supported |= SUPPORTED_FIBRE;
216                 ecmd->advertising |= ADVERTISED_FIBRE;
217                 ecmd->port = PORT_FIBRE;
218         }
219
220         ecmd->transceiver = XCVR_EXTERNAL;
221
222         if (link_up) {
223                 switch (link_speed) {
224                 case I40E_LINK_SPEED_40GB:
225                         /* need a SPEED_40000 in ethtool.h */
226                         ethtool_cmd_speed_set(ecmd, 40000);
227                         break;
228                 case I40E_LINK_SPEED_10GB:
229                         ethtool_cmd_speed_set(ecmd, SPEED_10000);
230                         break;
231                 default:
232                         break;
233                 }
234                 ecmd->duplex = DUPLEX_FULL;
235         } else {
236                 ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
237                 ecmd->duplex = DUPLEX_UNKNOWN;
238         }
239
240         return 0;
241 }
242
243 /**
244  * i40e_get_pauseparam -  Get Flow Control status
245  * Return tx/rx-pause status
246  **/
247 static void i40e_get_pauseparam(struct net_device *netdev,
248                                 struct ethtool_pauseparam *pause)
249 {
250         struct i40e_netdev_priv *np = netdev_priv(netdev);
251         struct i40e_pf *pf = np->vsi->back;
252         struct i40e_hw *hw = &pf->hw;
253         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
254
255         pause->autoneg =
256                 ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
257                   AUTONEG_ENABLE : AUTONEG_DISABLE);
258
259         pause->rx_pause = 0;
260         pause->tx_pause = 0;
261         if (hw_link_info->an_info & I40E_AQ_LINK_PAUSE_RX)
262                 pause->rx_pause = 1;
263         if (hw_link_info->an_info & I40E_AQ_LINK_PAUSE_TX)
264                 pause->tx_pause = 1;
265 }
266
267 static u32 i40e_get_msglevel(struct net_device *netdev)
268 {
269         struct i40e_netdev_priv *np = netdev_priv(netdev);
270         struct i40e_pf *pf = np->vsi->back;
271
272         return pf->msg_enable;
273 }
274
275 static void i40e_set_msglevel(struct net_device *netdev, u32 data)
276 {
277         struct i40e_netdev_priv *np = netdev_priv(netdev);
278         struct i40e_pf *pf = np->vsi->back;
279
280         if (I40E_DEBUG_USER & data)
281                 pf->hw.debug_mask = data;
282         pf->msg_enable = data;
283 }
284
285 static int i40e_get_regs_len(struct net_device *netdev)
286 {
287         int reg_count = 0;
288         int i;
289
290         for (i = 0; i40e_reg_list[i].offset != 0; i++)
291                 reg_count += i40e_reg_list[i].elements;
292
293         return reg_count * sizeof(u32);
294 }
295
296 static void i40e_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
297                           void *p)
298 {
299         struct i40e_netdev_priv *np = netdev_priv(netdev);
300         struct i40e_pf *pf = np->vsi->back;
301         struct i40e_hw *hw = &pf->hw;
302         u32 *reg_buf = p;
303         int i, j, ri;
304         u32 reg;
305
306         /* Tell ethtool which driver-version-specific regs output we have.
307          *
308          * At some point, if we have ethtool doing special formatting of
309          * this data, it will rely on this version number to know how to
310          * interpret things.  Hence, this needs to be updated if/when the
311          * diags register table is changed.
312          */
313         regs->version = 1;
314
315         /* loop through the diags reg table for what to print */
316         ri = 0;
317         for (i = 0; i40e_reg_list[i].offset != 0; i++) {
318                 for (j = 0; j < i40e_reg_list[i].elements; j++) {
319                         reg = i40e_reg_list[i].offset
320                                 + (j * i40e_reg_list[i].stride);
321                         reg_buf[ri++] = rd32(hw, reg);
322                 }
323         }
324
325 }
326
327 static int i40e_get_eeprom(struct net_device *netdev,
328                            struct ethtool_eeprom *eeprom, u8 *bytes)
329 {
330         struct i40e_netdev_priv *np = netdev_priv(netdev);
331         struct i40e_hw *hw = &np->vsi->back->hw;
332         int first_word, last_word;
333         u16 i, eeprom_len;
334         u16 *eeprom_buff;
335         int ret_val = 0;
336
337         if (eeprom->len == 0)
338                 return -EINVAL;
339
340         eeprom->magic = hw->vendor_id | (hw->device_id << 16);
341
342         first_word = eeprom->offset >> 1;
343         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
344         eeprom_len = last_word - first_word + 1;
345
346         eeprom_buff = kmalloc(sizeof(u16) * eeprom_len, GFP_KERNEL);
347         if (!eeprom_buff)
348                 return -ENOMEM;
349
350         ret_val = i40e_read_nvm_buffer(hw, first_word, &eeprom_len,
351                                            eeprom_buff);
352         if (eeprom_len == 0) {
353                 kfree(eeprom_buff);
354                 return -EACCES;
355         }
356
357         /* Device's eeprom is always little-endian, word addressable */
358         for (i = 0; i < eeprom_len; i++)
359                 le16_to_cpus(&eeprom_buff[i]);
360
361         memcpy(bytes, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
362         kfree(eeprom_buff);
363
364         return ret_val;
365 }
366
367 static int i40e_get_eeprom_len(struct net_device *netdev)
368 {
369         struct i40e_netdev_priv *np = netdev_priv(netdev);
370         struct i40e_hw *hw = &np->vsi->back->hw;
371
372         return hw->nvm.sr_size * 2;
373 }
374
375 static void i40e_get_drvinfo(struct net_device *netdev,
376                              struct ethtool_drvinfo *drvinfo)
377 {
378         struct i40e_netdev_priv *np = netdev_priv(netdev);
379         struct i40e_vsi *vsi = np->vsi;
380         struct i40e_pf *pf = vsi->back;
381
382         strlcpy(drvinfo->driver, i40e_driver_name, sizeof(drvinfo->driver));
383         strlcpy(drvinfo->version, i40e_driver_version_str,
384                 sizeof(drvinfo->version));
385         strlcpy(drvinfo->fw_version, i40e_fw_version_str(&pf->hw),
386                 sizeof(drvinfo->fw_version));
387         strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
388                 sizeof(drvinfo->bus_info));
389 }
390
391 static void i40e_get_ringparam(struct net_device *netdev,
392                                struct ethtool_ringparam *ring)
393 {
394         struct i40e_netdev_priv *np = netdev_priv(netdev);
395         struct i40e_pf *pf = np->vsi->back;
396         struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
397
398         ring->rx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
399         ring->tx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
400         ring->rx_mini_max_pending = 0;
401         ring->rx_jumbo_max_pending = 0;
402         ring->rx_pending = vsi->rx_rings[0]->count;
403         ring->tx_pending = vsi->tx_rings[0]->count;
404         ring->rx_mini_pending = 0;
405         ring->rx_jumbo_pending = 0;
406 }
407
408 static int i40e_set_ringparam(struct net_device *netdev,
409                               struct ethtool_ringparam *ring)
410 {
411         struct i40e_ring *tx_rings = NULL, *rx_rings = NULL;
412         struct i40e_netdev_priv *np = netdev_priv(netdev);
413         struct i40e_vsi *vsi = np->vsi;
414         struct i40e_pf *pf = vsi->back;
415         u32 new_rx_count, new_tx_count;
416         int i, err = 0;
417
418         if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
419                 return -EINVAL;
420
421         new_tx_count = clamp_t(u32, ring->tx_pending,
422                                I40E_MIN_NUM_DESCRIPTORS,
423                                I40E_MAX_NUM_DESCRIPTORS);
424         new_tx_count = ALIGN(new_tx_count, I40E_REQ_DESCRIPTOR_MULTIPLE);
425
426         new_rx_count = clamp_t(u32, ring->rx_pending,
427                                I40E_MIN_NUM_DESCRIPTORS,
428                                I40E_MAX_NUM_DESCRIPTORS);
429         new_rx_count = ALIGN(new_rx_count, I40E_REQ_DESCRIPTOR_MULTIPLE);
430
431         /* if nothing to do return success */
432         if ((new_tx_count == vsi->tx_rings[0]->count) &&
433             (new_rx_count == vsi->rx_rings[0]->count))
434                 return 0;
435
436         while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
437                 usleep_range(1000, 2000);
438
439         if (!netif_running(vsi->netdev)) {
440                 /* simple case - set for the next time the netdev is started */
441                 for (i = 0; i < vsi->num_queue_pairs; i++) {
442                         vsi->tx_rings[i]->count = new_tx_count;
443                         vsi->rx_rings[i]->count = new_rx_count;
444                 }
445                 goto done;
446         }
447
448         /* We can't just free everything and then setup again,
449          * because the ISRs in MSI-X mode get passed pointers
450          * to the Tx and Rx ring structs.
451          */
452
453         /* alloc updated Tx resources */
454         if (new_tx_count != vsi->tx_rings[0]->count) {
455                 netdev_info(netdev,
456                             "Changing Tx descriptor count from %d to %d.\n",
457                             vsi->tx_rings[0]->count, new_tx_count);
458                 tx_rings = kcalloc(vsi->alloc_queue_pairs,
459                                    sizeof(struct i40e_ring), GFP_KERNEL);
460                 if (!tx_rings) {
461                         err = -ENOMEM;
462                         goto done;
463                 }
464
465                 for (i = 0; i < vsi->num_queue_pairs; i++) {
466                         /* clone ring and setup updated count */
467                         tx_rings[i] = *vsi->tx_rings[i];
468                         tx_rings[i].count = new_tx_count;
469                         err = i40e_setup_tx_descriptors(&tx_rings[i]);
470                         if (err) {
471                                 while (i) {
472                                         i--;
473                                         i40e_free_tx_resources(&tx_rings[i]);
474                                 }
475                                 kfree(tx_rings);
476                                 tx_rings = NULL;
477
478                                 goto done;
479                         }
480                 }
481         }
482
483         /* alloc updated Rx resources */
484         if (new_rx_count != vsi->rx_rings[0]->count) {
485                 netdev_info(netdev,
486                             "Changing Rx descriptor count from %d to %d\n",
487                             vsi->rx_rings[0]->count, new_rx_count);
488                 rx_rings = kcalloc(vsi->alloc_queue_pairs,
489                                    sizeof(struct i40e_ring), GFP_KERNEL);
490                 if (!rx_rings) {
491                         err = -ENOMEM;
492                         goto free_tx;
493                 }
494
495                 for (i = 0; i < vsi->num_queue_pairs; i++) {
496                         /* clone ring and setup updated count */
497                         rx_rings[i] = *vsi->rx_rings[i];
498                         rx_rings[i].count = new_rx_count;
499                         err = i40e_setup_rx_descriptors(&rx_rings[i]);
500                         if (err) {
501                                 while (i) {
502                                         i--;
503                                         i40e_free_rx_resources(&rx_rings[i]);
504                                 }
505                                 kfree(rx_rings);
506                                 rx_rings = NULL;
507
508                                 goto free_tx;
509                         }
510                 }
511         }
512
513         /* Bring interface down, copy in the new ring info,
514          * then restore the interface
515          */
516         i40e_down(vsi);
517
518         if (tx_rings) {
519                 for (i = 0; i < vsi->num_queue_pairs; i++) {
520                         i40e_free_tx_resources(vsi->tx_rings[i]);
521                         *vsi->tx_rings[i] = tx_rings[i];
522                 }
523                 kfree(tx_rings);
524                 tx_rings = NULL;
525         }
526
527         if (rx_rings) {
528                 for (i = 0; i < vsi->num_queue_pairs; i++) {
529                         i40e_free_rx_resources(vsi->rx_rings[i]);
530                         *vsi->rx_rings[i] = rx_rings[i];
531                 }
532                 kfree(rx_rings);
533                 rx_rings = NULL;
534         }
535
536         i40e_up(vsi);
537
538 free_tx:
539         /* error cleanup if the Rx allocations failed after getting Tx */
540         if (tx_rings) {
541                 for (i = 0; i < vsi->num_queue_pairs; i++)
542                         i40e_free_tx_resources(&tx_rings[i]);
543                 kfree(tx_rings);
544                 tx_rings = NULL;
545         }
546
547 done:
548         clear_bit(__I40E_CONFIG_BUSY, &pf->state);
549
550         return err;
551 }
552
553 static int i40e_get_sset_count(struct net_device *netdev, int sset)
554 {
555         struct i40e_netdev_priv *np = netdev_priv(netdev);
556         struct i40e_vsi *vsi = np->vsi;
557         struct i40e_pf *pf = vsi->back;
558
559         switch (sset) {
560         case ETH_SS_TEST:
561                 return I40E_TEST_LEN;
562         case ETH_SS_STATS:
563                 if (vsi == pf->vsi[pf->lan_vsi])
564                         return I40E_PF_STATS_LEN(netdev);
565                 else
566                         return I40E_VSI_STATS_LEN(netdev);
567         default:
568                 return -EOPNOTSUPP;
569         }
570 }
571
572 static void i40e_get_ethtool_stats(struct net_device *netdev,
573                                    struct ethtool_stats *stats, u64 *data)
574 {
575         struct i40e_netdev_priv *np = netdev_priv(netdev);
576         struct i40e_vsi *vsi = np->vsi;
577         struct i40e_pf *pf = vsi->back;
578         int i = 0;
579         char *p;
580         int j;
581         struct rtnl_link_stats64 *net_stats = i40e_get_vsi_stats_struct(vsi);
582         unsigned int start;
583
584         i40e_update_stats(vsi);
585
586         for (j = 0; j < I40E_NETDEV_STATS_LEN; j++) {
587                 p = (char *)net_stats + i40e_gstrings_net_stats[j].stat_offset;
588                 data[i++] = (i40e_gstrings_net_stats[j].sizeof_stat ==
589                         sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
590         }
591         rcu_read_lock();
592         for (j = 0; j < vsi->num_queue_pairs; j++, i += 4) {
593                 struct i40e_ring *tx_ring = ACCESS_ONCE(vsi->tx_rings[j]);
594                 struct i40e_ring *rx_ring;
595
596                 if (!tx_ring)
597                         continue;
598
599                 /* process Tx ring statistics */
600                 do {
601                         start = u64_stats_fetch_begin_bh(&tx_ring->syncp);
602                         data[i] = tx_ring->stats.packets;
603                         data[i + 1] = tx_ring->stats.bytes;
604                 } while (u64_stats_fetch_retry_bh(&tx_ring->syncp, start));
605
606                 /* Rx ring is the 2nd half of the queue pair */
607                 rx_ring = &tx_ring[1];
608                 do {
609                         start = u64_stats_fetch_begin_bh(&rx_ring->syncp);
610                         data[i + 2] = rx_ring->stats.packets;
611                         data[i + 3] = rx_ring->stats.bytes;
612                 } while (u64_stats_fetch_retry_bh(&rx_ring->syncp, start));
613         }
614         rcu_read_unlock();
615         if (vsi == pf->vsi[pf->lan_vsi]) {
616                 for (j = 0; j < I40E_GLOBAL_STATS_LEN; j++) {
617                         p = (char *)pf + i40e_gstrings_stats[j].stat_offset;
618                         data[i++] = (i40e_gstrings_stats[j].sizeof_stat ==
619                                    sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
620                 }
621                 for (j = 0; j < I40E_MAX_USER_PRIORITY; j++) {
622                         data[i++] = pf->stats.priority_xon_tx[j];
623                         data[i++] = pf->stats.priority_xoff_tx[j];
624                 }
625                 for (j = 0; j < I40E_MAX_USER_PRIORITY; j++) {
626                         data[i++] = pf->stats.priority_xon_rx[j];
627                         data[i++] = pf->stats.priority_xoff_rx[j];
628                 }
629                 for (j = 0; j < I40E_MAX_USER_PRIORITY; j++)
630                         data[i++] = pf->stats.priority_xon_2_xoff[j];
631         }
632 }
633
634 static void i40e_get_strings(struct net_device *netdev, u32 stringset,
635                              u8 *data)
636 {
637         struct i40e_netdev_priv *np = netdev_priv(netdev);
638         struct i40e_vsi *vsi = np->vsi;
639         struct i40e_pf *pf = vsi->back;
640         char *p = (char *)data;
641         int i;
642
643         switch (stringset) {
644         case ETH_SS_TEST:
645                 for (i = 0; i < I40E_TEST_LEN; i++) {
646                         memcpy(data, i40e_gstrings_test[i], ETH_GSTRING_LEN);
647                         data += ETH_GSTRING_LEN;
648                 }
649                 break;
650         case ETH_SS_STATS:
651                 for (i = 0; i < I40E_NETDEV_STATS_LEN; i++) {
652                         snprintf(p, ETH_GSTRING_LEN, "%s",
653                                  i40e_gstrings_net_stats[i].stat_string);
654                         p += ETH_GSTRING_LEN;
655                 }
656                 for (i = 0; i < vsi->num_queue_pairs; i++) {
657                         snprintf(p, ETH_GSTRING_LEN, "tx-%u.tx_packets", i);
658                         p += ETH_GSTRING_LEN;
659                         snprintf(p, ETH_GSTRING_LEN, "tx-%u.tx_bytes", i);
660                         p += ETH_GSTRING_LEN;
661                         snprintf(p, ETH_GSTRING_LEN, "rx-%u.rx_packets", i);
662                         p += ETH_GSTRING_LEN;
663                         snprintf(p, ETH_GSTRING_LEN, "rx-%u.rx_bytes", i);
664                         p += ETH_GSTRING_LEN;
665                 }
666                 if (vsi == pf->vsi[pf->lan_vsi]) {
667                         for (i = 0; i < I40E_GLOBAL_STATS_LEN; i++) {
668                                 snprintf(p, ETH_GSTRING_LEN, "port.%s",
669                                          i40e_gstrings_stats[i].stat_string);
670                                 p += ETH_GSTRING_LEN;
671                         }
672                         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
673                                 snprintf(p, ETH_GSTRING_LEN,
674                                          "port.tx_priority_%u_xon", i);
675                                 p += ETH_GSTRING_LEN;
676                                 snprintf(p, ETH_GSTRING_LEN,
677                                          "port.tx_priority_%u_xoff", i);
678                                 p += ETH_GSTRING_LEN;
679                         }
680                         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
681                                 snprintf(p, ETH_GSTRING_LEN,
682                                          "port.rx_priority_%u_xon", i);
683                                 p += ETH_GSTRING_LEN;
684                                 snprintf(p, ETH_GSTRING_LEN,
685                                          "port.rx_priority_%u_xoff", i);
686                                 p += ETH_GSTRING_LEN;
687                         }
688                         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
689                                 snprintf(p, ETH_GSTRING_LEN,
690                                          "port.rx_priority_%u_xon_2_xoff", i);
691                                 p += ETH_GSTRING_LEN;
692                         }
693                 }
694                 /* BUG_ON(p - data != I40E_STATS_LEN * ETH_GSTRING_LEN); */
695                 break;
696         }
697 }
698
699 static int i40e_get_ts_info(struct net_device *dev,
700                             struct ethtool_ts_info *info)
701 {
702         return ethtool_op_get_ts_info(dev, info);
703 }
704
705 static int i40e_link_test(struct i40e_pf *pf, u64 *data)
706 {
707         if (i40e_get_link_status(&pf->hw))
708                 *data = 0;
709         else
710                 *data = 1;
711
712         return *data;
713 }
714
715 static int i40e_reg_test(struct i40e_pf *pf, u64 *data)
716 {
717         i40e_status ret;
718
719         ret = i40e_diag_reg_test(&pf->hw);
720         *data = ret;
721
722         return ret;
723 }
724
725 static int i40e_eeprom_test(struct i40e_pf *pf, u64 *data)
726 {
727         i40e_status ret;
728
729         ret = i40e_diag_eeprom_test(&pf->hw);
730         *data = ret;
731
732         return ret;
733 }
734
735 static int i40e_intr_test(struct i40e_pf *pf, u64 *data)
736 {
737         u16 swc_old = pf->sw_int_count;
738
739         wr32(&pf->hw, I40E_PFINT_DYN_CTL0,
740              (I40E_PFINT_DYN_CTL0_INTENA_MASK |
741               I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
742         usleep_range(1000, 2000);
743         *data = (swc_old == pf->sw_int_count);
744
745         return *data;
746 }
747
748 static int i40e_loopback_test(struct i40e_pf *pf, u64 *data)
749 {
750         *data = 0;
751
752         return *data;
753 }
754
755 static void i40e_diag_test(struct net_device *netdev,
756                            struct ethtool_test *eth_test, u64 *data)
757 {
758         struct i40e_netdev_priv *np = netdev_priv(netdev);
759         struct i40e_pf *pf = np->vsi->back;
760
761         set_bit(__I40E_TESTING, &pf->state);
762         if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
763                 /* Offline tests */
764
765                 netdev_info(netdev, "offline testing starting\n");
766
767                 /* Link test performed before hardware reset
768                  * so autoneg doesn't interfere with test result
769                  */
770                 netdev_info(netdev, "link test starting\n");
771                 if (i40e_link_test(pf, &data[I40E_ETH_TEST_LINK]))
772                         eth_test->flags |= ETH_TEST_FL_FAILED;
773
774                 netdev_info(netdev, "register test starting\n");
775                 if (i40e_reg_test(pf, &data[I40E_ETH_TEST_REG]))
776                         eth_test->flags |= ETH_TEST_FL_FAILED;
777
778                 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
779                 netdev_info(netdev, "eeprom test starting\n");
780                 if (i40e_eeprom_test(pf, &data[I40E_ETH_TEST_EEPROM]))
781                         eth_test->flags |= ETH_TEST_FL_FAILED;
782
783                 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
784                 netdev_info(netdev, "interrupt test starting\n");
785                 if (i40e_intr_test(pf, &data[I40E_ETH_TEST_INTR]))
786                         eth_test->flags |= ETH_TEST_FL_FAILED;
787
788                 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
789                 netdev_info(netdev, "loopback test starting\n");
790                 if (i40e_loopback_test(pf, &data[I40E_ETH_TEST_LOOPBACK]))
791                         eth_test->flags |= ETH_TEST_FL_FAILED;
792
793         } else {
794                 netdev_info(netdev, "online test starting\n");
795                 /* Online tests */
796                 if (i40e_link_test(pf, &data[I40E_ETH_TEST_LINK]))
797                         eth_test->flags |= ETH_TEST_FL_FAILED;
798
799                 /* Offline only tests, not run in online; pass by default */
800                 data[I40E_ETH_TEST_REG] = 0;
801                 data[I40E_ETH_TEST_EEPROM] = 0;
802                 data[I40E_ETH_TEST_INTR] = 0;
803                 data[I40E_ETH_TEST_LOOPBACK] = 0;
804
805                 clear_bit(__I40E_TESTING, &pf->state);
806         }
807 }
808
809 static void i40e_get_wol(struct net_device *netdev,
810                          struct ethtool_wolinfo *wol)
811 {
812         wol->supported = 0;
813         wol->wolopts = 0;
814 }
815
816 static int i40e_nway_reset(struct net_device *netdev)
817 {
818         /* restart autonegotiation */
819         struct i40e_netdev_priv *np = netdev_priv(netdev);
820         struct i40e_pf *pf = np->vsi->back;
821         struct i40e_hw *hw = &pf->hw;
822         i40e_status ret = 0;
823
824         ret = i40e_aq_set_link_restart_an(hw, NULL);
825         if (ret) {
826                 netdev_info(netdev, "link restart failed, aq_err=%d\n",
827                             pf->hw.aq.asq_last_status);
828                 return -EIO;
829         }
830
831         return 0;
832 }
833
834 static int i40e_set_phys_id(struct net_device *netdev,
835                             enum ethtool_phys_id_state state)
836 {
837         struct i40e_netdev_priv *np = netdev_priv(netdev);
838         struct i40e_pf *pf = np->vsi->back;
839         struct i40e_hw *hw = &pf->hw;
840         int blink_freq = 2;
841
842         switch (state) {
843         case ETHTOOL_ID_ACTIVE:
844                 pf->led_status = i40e_led_get(hw);
845                 return blink_freq;
846         case ETHTOOL_ID_ON:
847                 i40e_led_set(hw, 0xF);
848                 break;
849         case ETHTOOL_ID_OFF:
850                 i40e_led_set(hw, 0x0);
851                 break;
852         case ETHTOOL_ID_INACTIVE:
853                 i40e_led_set(hw, pf->led_status);
854                 break;
855         }
856
857         return 0;
858 }
859
860 /* NOTE: i40e hardware uses a conversion factor of 2 for Interrupt
861  * Throttle Rate (ITR) ie. ITR(1) = 2us ITR(10) = 20 us, and also
862  * 125us (8000 interrupts per second) == ITR(62)
863  */
864
865 static int i40e_get_coalesce(struct net_device *netdev,
866                              struct ethtool_coalesce *ec)
867 {
868         struct i40e_netdev_priv *np = netdev_priv(netdev);
869         struct i40e_vsi *vsi = np->vsi;
870
871         ec->tx_max_coalesced_frames_irq = vsi->work_limit;
872         ec->rx_max_coalesced_frames_irq = vsi->work_limit;
873
874         if (ITR_IS_DYNAMIC(vsi->rx_itr_setting))
875                 ec->rx_coalesce_usecs = 1;
876         else
877                 ec->rx_coalesce_usecs = vsi->rx_itr_setting;
878
879         if (ITR_IS_DYNAMIC(vsi->tx_itr_setting))
880                 ec->tx_coalesce_usecs = 1;
881         else
882                 ec->tx_coalesce_usecs = vsi->tx_itr_setting;
883
884         return 0;
885 }
886
887 static int i40e_set_coalesce(struct net_device *netdev,
888                              struct ethtool_coalesce *ec)
889 {
890         struct i40e_netdev_priv *np = netdev_priv(netdev);
891         struct i40e_q_vector *q_vector;
892         struct i40e_vsi *vsi = np->vsi;
893         struct i40e_pf *pf = vsi->back;
894         struct i40e_hw *hw = &pf->hw;
895         u16 vector;
896         int i;
897
898         if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
899                 vsi->work_limit = ec->tx_max_coalesced_frames_irq;
900
901         switch (ec->rx_coalesce_usecs) {
902         case 0:
903                 vsi->rx_itr_setting = 0;
904                 break;
905         case 1:
906                 vsi->rx_itr_setting = (I40E_ITR_DYNAMIC |
907                                        ITR_REG_TO_USEC(I40E_ITR_RX_DEF));
908                 break;
909         default:
910                 if ((ec->rx_coalesce_usecs < (I40E_MIN_ITR << 1)) ||
911                     (ec->rx_coalesce_usecs > (I40E_MAX_ITR << 1)))
912                         return -EINVAL;
913                 vsi->rx_itr_setting = ec->rx_coalesce_usecs;
914                 break;
915         }
916
917         switch (ec->tx_coalesce_usecs) {
918         case 0:
919                 vsi->tx_itr_setting = 0;
920                 break;
921         case 1:
922                 vsi->tx_itr_setting = (I40E_ITR_DYNAMIC |
923                                        ITR_REG_TO_USEC(I40E_ITR_TX_DEF));
924                 break;
925         default:
926                 if ((ec->tx_coalesce_usecs < (I40E_MIN_ITR << 1)) ||
927                     (ec->tx_coalesce_usecs > (I40E_MAX_ITR << 1)))
928                         return -EINVAL;
929                 vsi->tx_itr_setting = ec->tx_coalesce_usecs;
930                 break;
931         }
932
933         vector = vsi->base_vector;
934         for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
935                 q_vector = vsi->q_vectors[i];
936                 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
937                 wr32(hw, I40E_PFINT_ITRN(0, vector - 1), q_vector->rx.itr);
938                 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
939                 wr32(hw, I40E_PFINT_ITRN(1, vector - 1), q_vector->tx.itr);
940                 i40e_flush(hw);
941         }
942
943         return 0;
944 }
945
946 /**
947  * i40e_get_rss_hash_opts - Get RSS hash Input Set for each flow type
948  * @pf: pointer to the physical function struct
949  * @cmd: ethtool rxnfc command
950  *
951  * Returns Success if the flow is supported, else Invalid Input.
952  **/
953 static int i40e_get_rss_hash_opts(struct i40e_pf *pf, struct ethtool_rxnfc *cmd)
954 {
955         cmd->data = 0;
956
957         /* Report default options for RSS on i40e */
958         switch (cmd->flow_type) {
959         case TCP_V4_FLOW:
960         case UDP_V4_FLOW:
961                 cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
962         /* fall through to add IP fields */
963         case SCTP_V4_FLOW:
964         case AH_ESP_V4_FLOW:
965         case AH_V4_FLOW:
966         case ESP_V4_FLOW:
967         case IPV4_FLOW:
968                 cmd->data |= RXH_IP_SRC | RXH_IP_DST;
969                 break;
970         case TCP_V6_FLOW:
971         case UDP_V6_FLOW:
972                 cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
973         /* fall through to add IP fields */
974         case SCTP_V6_FLOW:
975         case AH_ESP_V6_FLOW:
976         case AH_V6_FLOW:
977         case ESP_V6_FLOW:
978         case IPV6_FLOW:
979                 cmd->data |= RXH_IP_SRC | RXH_IP_DST;
980                 break;
981         default:
982                 return -EINVAL;
983         }
984
985         return 0;
986 }
987
988 /**
989  * i40e_get_rxnfc - command to get RX flow classification rules
990  * @netdev: network interface device structure
991  * @cmd: ethtool rxnfc command
992  *
993  * Returns Success if the command is supported.
994  **/
995 static int i40e_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
996                           u32 *rule_locs)
997 {
998         struct i40e_netdev_priv *np = netdev_priv(netdev);
999         struct i40e_vsi *vsi = np->vsi;
1000         struct i40e_pf *pf = vsi->back;
1001         int ret = -EOPNOTSUPP;
1002
1003         switch (cmd->cmd) {
1004         case ETHTOOL_GRXRINGS:
1005                 cmd->data = vsi->alloc_queue_pairs;
1006                 ret = 0;
1007                 break;
1008         case ETHTOOL_GRXFH:
1009                 ret = i40e_get_rss_hash_opts(pf, cmd);
1010                 break;
1011         case ETHTOOL_GRXCLSRLCNT:
1012                 ret = 0;
1013                 break;
1014         case ETHTOOL_GRXCLSRULE:
1015                 ret = 0;
1016                 break;
1017         case ETHTOOL_GRXCLSRLALL:
1018                 cmd->data = 500;
1019                 ret = 0;
1020         default:
1021                 break;
1022         }
1023
1024         return ret;
1025 }
1026
1027 /**
1028  * i40e_set_rss_hash_opt - Enable/Disable flow types for RSS hash
1029  * @pf: pointer to the physical function struct
1030  * @cmd: ethtool rxnfc command
1031  *
1032  * Returns Success if the flow input set is supported.
1033  **/
1034 static int i40e_set_rss_hash_opt(struct i40e_pf *pf, struct ethtool_rxnfc *nfc)
1035 {
1036         struct i40e_hw *hw = &pf->hw;
1037         u64 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
1038                    ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
1039
1040         /* RSS does not support anything other than hashing
1041          * to queues on src and dst IPs and ports
1042          */
1043         if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
1044                           RXH_L4_B_0_1 | RXH_L4_B_2_3))
1045                 return -EINVAL;
1046
1047         /* We need at least the IP SRC and DEST fields for hashing */
1048         if (!(nfc->data & RXH_IP_SRC) ||
1049             !(nfc->data & RXH_IP_DST))
1050                 return -EINVAL;
1051
1052         switch (nfc->flow_type) {
1053         case TCP_V4_FLOW:
1054                 switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1055                 case 0:
1056                         hena &= ~((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP);
1057                         break;
1058                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1059                         hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP);
1060                         break;
1061                 default:
1062                         return -EINVAL;
1063                 }
1064                 break;
1065         case TCP_V6_FLOW:
1066                 switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1067                 case 0:
1068                         hena &= ~((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP);
1069                         break;
1070                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1071                         hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP);
1072                         break;
1073                 default:
1074                         return -EINVAL;
1075                 }
1076                 break;
1077         case UDP_V4_FLOW:
1078                 switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1079                 case 0:
1080                         hena &=
1081                         ~(((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |
1082                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) |
1083                         ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4));
1084                         break;
1085                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1086                         hena |=
1087                         (((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP)  |
1088                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) |
1089                         ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4));
1090                         break;
1091                 default:
1092                         return -EINVAL;
1093                 }
1094                 break;
1095         case UDP_V6_FLOW:
1096                 switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1097                 case 0:
1098                         hena &=
1099                         ~(((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |
1100                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) |
1101                         ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6));
1102                         break;
1103                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1104                         hena |=
1105                         (((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP)  |
1106                         ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) |
1107                         ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6));
1108                         break;
1109                 default:
1110                         return -EINVAL;
1111                 }
1112                 break;
1113         case AH_ESP_V4_FLOW:
1114         case AH_V4_FLOW:
1115         case ESP_V4_FLOW:
1116         case SCTP_V4_FLOW:
1117                 if ((nfc->data & RXH_L4_B_0_1) ||
1118                     (nfc->data & RXH_L4_B_2_3))
1119                         return -EINVAL;
1120                 hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER);
1121                 break;
1122         case AH_ESP_V6_FLOW:
1123         case AH_V6_FLOW:
1124         case ESP_V6_FLOW:
1125         case SCTP_V6_FLOW:
1126                 if ((nfc->data & RXH_L4_B_0_1) ||
1127                     (nfc->data & RXH_L4_B_2_3))
1128                         return -EINVAL;
1129                 hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER);
1130                 break;
1131         case IPV4_FLOW:
1132                 hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
1133                         ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4);
1134                 break;
1135         case IPV6_FLOW:
1136                 hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
1137                         ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6);
1138                 break;
1139         default:
1140                 return -EINVAL;
1141         }
1142
1143         wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
1144         wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
1145         i40e_flush(hw);
1146
1147         return 0;
1148 }
1149
1150 #define IP_HEADER_OFFSET 14
1151 /**
1152  * i40e_add_del_fdir_udpv4 - Add/Remove UDPv4 Flow Director filters for
1153  * a specific flow spec
1154  * @vsi: pointer to the targeted VSI
1155  * @fd_data: the flow director data required from the FDir descriptor
1156  * @ethtool_rx_flow_spec: the flow spec
1157  * @add: true adds a filter, false removes it
1158  *
1159  * Returns 0 if the filters were successfully added or removed
1160  **/
1161 static int i40e_add_del_fdir_udpv4(struct i40e_vsi *vsi,
1162                                    struct i40e_fdir_data *fd_data,
1163                                    struct ethtool_rx_flow_spec *fsp, bool add)
1164 {
1165         struct i40e_pf *pf = vsi->back;
1166         struct udphdr *udp;
1167         struct iphdr *ip;
1168         bool err = false;
1169         int ret;
1170         int i;
1171
1172         ip = (struct iphdr *)(fd_data->raw_packet + IP_HEADER_OFFSET);
1173         udp = (struct udphdr *)(fd_data->raw_packet + IP_HEADER_OFFSET
1174               + sizeof(struct iphdr));
1175
1176         ip->saddr = fsp->h_u.tcp_ip4_spec.ip4src;
1177         ip->daddr = fsp->h_u.tcp_ip4_spec.ip4dst;
1178         udp->source = fsp->h_u.tcp_ip4_spec.psrc;
1179         udp->dest = fsp->h_u.tcp_ip4_spec.pdst;
1180
1181         for (i = I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP;
1182              i <= I40E_FILTER_PCTYPE_NONF_IPV4_UDP; i++) {
1183                 fd_data->pctype = i;
1184                 ret = i40e_program_fdir_filter(fd_data, pf, add);
1185
1186                 if (ret) {
1187                         dev_info(&pf->pdev->dev,
1188                                  "Filter command send failed for PCTYPE %d (ret = %d)\n",
1189                                  fd_data->pctype, ret);
1190                         err = true;
1191                 } else {
1192                         dev_info(&pf->pdev->dev,
1193                                  "Filter OK for PCTYPE %d (ret = %d)\n",
1194                                  fd_data->pctype, ret);
1195                 }
1196         }
1197
1198         return err ? -EOPNOTSUPP : 0;
1199 }
1200
1201 /**
1202  * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 Flow Director filters for
1203  * a specific flow spec
1204  * @vsi: pointer to the targeted VSI
1205  * @fd_data: the flow director data required from the FDir descriptor
1206  * @ethtool_rx_flow_spec: the flow spec
1207  * @add: true adds a filter, false removes it
1208  *
1209  * Returns 0 if the filters were successfully added or removed
1210  **/
1211 static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
1212                                    struct i40e_fdir_data *fd_data,
1213                                    struct ethtool_rx_flow_spec *fsp, bool add)
1214 {
1215         struct i40e_pf *pf = vsi->back;
1216         struct tcphdr *tcp;
1217         struct iphdr *ip;
1218         bool err = false;
1219         int ret;
1220
1221         ip = (struct iphdr *)(fd_data->raw_packet + IP_HEADER_OFFSET);
1222         tcp = (struct tcphdr *)(fd_data->raw_packet + IP_HEADER_OFFSET
1223               + sizeof(struct iphdr));
1224
1225         ip->daddr = fsp->h_u.tcp_ip4_spec.ip4dst;
1226         tcp->dest = fsp->h_u.tcp_ip4_spec.pdst;
1227
1228         fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN;
1229         ret = i40e_program_fdir_filter(fd_data, pf, add);
1230
1231         if (ret) {
1232                 dev_info(&pf->pdev->dev,
1233                          "Filter command send failed for PCTYPE %d (ret = %d)\n",
1234                          fd_data->pctype, ret);
1235                 err = true;
1236         } else {
1237                 dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d (ret = %d)\n",
1238                          fd_data->pctype, ret);
1239         }
1240
1241         ip->saddr = fsp->h_u.tcp_ip4_spec.ip4src;
1242         tcp->source = fsp->h_u.tcp_ip4_spec.psrc;
1243
1244         fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
1245
1246         ret = i40e_program_fdir_filter(fd_data, pf, add);
1247         if (ret) {
1248                 dev_info(&pf->pdev->dev,
1249                          "Filter command send failed for PCTYPE %d (ret = %d)\n",
1250                          fd_data->pctype, ret);
1251                 err = true;
1252         } else {
1253                 dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d (ret = %d)\n",
1254                           fd_data->pctype, ret);
1255         }
1256
1257         return err ? -EOPNOTSUPP : 0;
1258 }
1259
1260 /**
1261  * i40e_add_del_fdir_sctpv4 - Add/Remove SCTPv4 Flow Director filters for
1262  * a specific flow spec
1263  * @vsi: pointer to the targeted VSI
1264  * @fd_data: the flow director data required from the FDir descriptor
1265  * @ethtool_rx_flow_spec: the flow spec
1266  * @add: true adds a filter, false removes it
1267  *
1268  * Returns 0 if the filters were successfully added or removed
1269  **/
1270 static int i40e_add_del_fdir_sctpv4(struct i40e_vsi *vsi,
1271                                     struct i40e_fdir_data *fd_data,
1272                                     struct ethtool_rx_flow_spec *fsp, bool add)
1273 {
1274         return -EOPNOTSUPP;
1275 }
1276
1277 /**
1278  * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 Flow Director filters for
1279  * a specific flow spec
1280  * @vsi: pointer to the targeted VSI
1281  * @fd_data: the flow director data required for the FDir descriptor
1282  * @fsp: the ethtool flow spec
1283  * @add: true adds a filter, false removes it
1284  *
1285  * Returns 0 if the filters were successfully added or removed
1286  **/
1287 static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
1288                                   struct i40e_fdir_data *fd_data,
1289                                   struct ethtool_rx_flow_spec *fsp, bool add)
1290 {
1291         struct i40e_pf *pf = vsi->back;
1292         struct iphdr *ip;
1293         bool err = false;
1294         int ret;
1295         int i;
1296
1297         ip = (struct iphdr *)(fd_data->raw_packet + IP_HEADER_OFFSET);
1298
1299         ip->saddr = fsp->h_u.usr_ip4_spec.ip4src;
1300         ip->daddr = fsp->h_u.usr_ip4_spec.ip4dst;
1301         ip->protocol = fsp->h_u.usr_ip4_spec.proto;
1302
1303         for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
1304              i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
1305                 fd_data->pctype = i;
1306                 ret = i40e_program_fdir_filter(fd_data, pf, add);
1307
1308                 if (ret) {
1309                         dev_info(&pf->pdev->dev,
1310                                  "Filter command send failed for PCTYPE %d (ret = %d)\n",
1311                                  fd_data->pctype, ret);
1312                         err = true;
1313                 } else {
1314                         dev_info(&pf->pdev->dev,
1315                                  "Filter OK for PCTYPE %d (ret = %d)\n",
1316                                  fd_data->pctype, ret);
1317                 }
1318         }
1319
1320         return err ? -EOPNOTSUPP : 0;
1321 }
1322
1323 /**
1324  * i40e_add_del_fdir_ethtool - Add/Remove Flow Director filters for
1325  * a specific flow spec based on their protocol
1326  * @vsi: pointer to the targeted VSI
1327  * @cmd: command to get or set RX flow classification rules
1328  * @add: true adds a filter, false removes it
1329  *
1330  * Returns 0 if the filters were successfully added or removed
1331  **/
1332 static int i40e_add_del_fdir_ethtool(struct i40e_vsi *vsi,
1333                         struct ethtool_rxnfc *cmd, bool add)
1334 {
1335         struct i40e_fdir_data fd_data;
1336         int ret = -EINVAL;
1337         struct i40e_pf *pf;
1338         struct ethtool_rx_flow_spec *fsp =
1339                 (struct ethtool_rx_flow_spec *)&cmd->fs;
1340
1341         if (!vsi)
1342                 return -EINVAL;
1343
1344         pf = vsi->back;
1345
1346         if ((fsp->ring_cookie != RX_CLS_FLOW_DISC) &&
1347             (fsp->ring_cookie >= vsi->num_queue_pairs))
1348                 return -EINVAL;
1349
1350         /* Populate the Flow Director that we have at the moment
1351          * and allocate the raw packet buffer for the calling functions
1352          */
1353         fd_data.raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_LOOKUP,
1354                                      GFP_KERNEL);
1355
1356         if (!fd_data.raw_packet) {
1357                 dev_info(&pf->pdev->dev, "Could not allocate memory\n");
1358                 return -ENOMEM;
1359         }
1360
1361         fd_data.q_index = fsp->ring_cookie;
1362         fd_data.flex_off = 0;
1363         fd_data.pctype = 0;
1364         fd_data.dest_vsi = vsi->id;
1365         fd_data.dest_ctl = 0;
1366         fd_data.fd_status = 0;
1367         fd_data.cnt_index = 0;
1368         fd_data.fd_id = 0;
1369
1370         switch (fsp->flow_type & ~FLOW_EXT) {
1371         case TCP_V4_FLOW:
1372                 ret = i40e_add_del_fdir_tcpv4(vsi, &fd_data, fsp, add);
1373                 break;
1374         case UDP_V4_FLOW:
1375                 ret = i40e_add_del_fdir_udpv4(vsi, &fd_data, fsp, add);
1376                 break;
1377         case SCTP_V4_FLOW:
1378                 ret = i40e_add_del_fdir_sctpv4(vsi, &fd_data, fsp, add);
1379                 break;
1380         case IPV4_FLOW:
1381                 ret = i40e_add_del_fdir_ipv4(vsi, &fd_data, fsp, add);
1382                 break;
1383         case IP_USER_FLOW:
1384                 switch (fsp->h_u.usr_ip4_spec.proto) {
1385                 case IPPROTO_TCP:
1386                         ret = i40e_add_del_fdir_tcpv4(vsi, &fd_data, fsp, add);
1387                         break;
1388                 case IPPROTO_UDP:
1389                         ret = i40e_add_del_fdir_udpv4(vsi, &fd_data, fsp, add);
1390                         break;
1391                 case IPPROTO_SCTP:
1392                         ret = i40e_add_del_fdir_sctpv4(vsi, &fd_data, fsp, add);
1393                         break;
1394                 default:
1395                         ret = i40e_add_del_fdir_ipv4(vsi, &fd_data, fsp, add);
1396                         break;
1397                 }
1398                 break;
1399         default:
1400                 dev_info(&pf->pdev->dev, "Could not specify spec type\n");
1401                 ret = -EINVAL;
1402         }
1403
1404         kfree(fd_data.raw_packet);
1405         fd_data.raw_packet = NULL;
1406
1407         return ret;
1408 }
1409 /**
1410  * i40e_set_rxnfc - command to set RX flow classification rules
1411  * @netdev: network interface device structure
1412  * @cmd: ethtool rxnfc command
1413  *
1414  * Returns Success if the command is supported.
1415  **/
1416 static int i40e_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1417 {
1418         struct i40e_netdev_priv *np = netdev_priv(netdev);
1419         struct i40e_vsi *vsi = np->vsi;
1420         struct i40e_pf *pf = vsi->back;
1421         int ret = -EOPNOTSUPP;
1422
1423         switch (cmd->cmd) {
1424         case ETHTOOL_SRXFH:
1425                 ret = i40e_set_rss_hash_opt(pf, cmd);
1426                 break;
1427         case ETHTOOL_SRXCLSRLINS:
1428                 ret = i40e_add_del_fdir_ethtool(vsi, cmd, true);
1429                 break;
1430         case ETHTOOL_SRXCLSRLDEL:
1431                 ret = i40e_add_del_fdir_ethtool(vsi, cmd, false);
1432                 break;
1433         default:
1434                 break;
1435         }
1436
1437         return ret;
1438 }
1439
1440 static const struct ethtool_ops i40e_ethtool_ops = {
1441         .get_settings           = i40e_get_settings,
1442         .get_drvinfo            = i40e_get_drvinfo,
1443         .get_regs_len           = i40e_get_regs_len,
1444         .get_regs               = i40e_get_regs,
1445         .nway_reset             = i40e_nway_reset,
1446         .get_link               = ethtool_op_get_link,
1447         .get_wol                = i40e_get_wol,
1448         .get_eeprom_len         = i40e_get_eeprom_len,
1449         .get_eeprom             = i40e_get_eeprom,
1450         .get_ringparam          = i40e_get_ringparam,
1451         .set_ringparam          = i40e_set_ringparam,
1452         .get_pauseparam         = i40e_get_pauseparam,
1453         .get_msglevel           = i40e_get_msglevel,
1454         .set_msglevel           = i40e_set_msglevel,
1455         .get_rxnfc              = i40e_get_rxnfc,
1456         .set_rxnfc              = i40e_set_rxnfc,
1457         .self_test              = i40e_diag_test,
1458         .get_strings            = i40e_get_strings,
1459         .set_phys_id            = i40e_set_phys_id,
1460         .get_sset_count         = i40e_get_sset_count,
1461         .get_ethtool_stats      = i40e_get_ethtool_stats,
1462         .get_coalesce           = i40e_get_coalesce,
1463         .set_coalesce           = i40e_set_coalesce,
1464         .get_ts_info            = i40e_get_ts_info,
1465 };
1466
1467 void i40e_set_ethtool_ops(struct net_device *netdev)
1468 {
1469         SET_ETHTOOL_OPS(netdev, &i40e_ethtool_ops);
1470 }