ef8c3276065e91339bd669e74bd5e97afbe02040
[linux-2.6-block.git] / drivers / net / ethernet / qlogic / qede / qede_ethtool.c
1 /* QLogic qede NIC Driver
2 * Copyright (c) 2015 QLogic Corporation
3 *
4 * This software is available under the terms of the GNU General Public License
5 * (GPL) Version 2, available from the file COPYING in the main directory of
6 * this source tree.
7 */
8
9 #include <linux/version.h>
10 #include <linux/types.h>
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/ethtool.h>
14 #include <linux/string.h>
15 #include <linux/pci.h>
16 #include <linux/capability.h>
17 #include "qede.h"
18
19 #define QEDE_RQSTAT_OFFSET(stat_name) \
20          (offsetof(struct qede_rx_queue, stat_name))
21 #define QEDE_RQSTAT_STRING(stat_name) (#stat_name)
22 #define QEDE_RQSTAT(stat_name) \
23          {QEDE_RQSTAT_OFFSET(stat_name), QEDE_RQSTAT_STRING(stat_name)}
24
25 #define QEDE_SELFTEST_POLL_COUNT 100
26
27 static const struct {
28         u64 offset;
29         char string[ETH_GSTRING_LEN];
30 } qede_rqstats_arr[] = {
31         QEDE_RQSTAT(rcv_pkts),
32         QEDE_RQSTAT(rx_hw_errors),
33         QEDE_RQSTAT(rx_alloc_errors),
34         QEDE_RQSTAT(rx_ip_frags),
35 };
36
37 #define QEDE_NUM_RQSTATS ARRAY_SIZE(qede_rqstats_arr)
38 #define QEDE_TQSTAT_OFFSET(stat_name) \
39         (offsetof(struct qede_tx_queue, stat_name))
40 #define QEDE_TQSTAT_STRING(stat_name) (#stat_name)
41 #define QEDE_TQSTAT(stat_name) \
42         {QEDE_TQSTAT_OFFSET(stat_name), QEDE_TQSTAT_STRING(stat_name)}
43 #define QEDE_NUM_TQSTATS ARRAY_SIZE(qede_tqstats_arr)
44 static const struct {
45         u64 offset;
46         char string[ETH_GSTRING_LEN];
47 } qede_tqstats_arr[] = {
48         QEDE_TQSTAT(xmit_pkts),
49         QEDE_TQSTAT(stopped_cnt),
50 };
51
52 #define QEDE_STAT_OFFSET(stat_name) (offsetof(struct qede_stats, stat_name))
53 #define QEDE_STAT_STRING(stat_name) (#stat_name)
54 #define _QEDE_STAT(stat_name, pf_only) \
55          {QEDE_STAT_OFFSET(stat_name), QEDE_STAT_STRING(stat_name), pf_only}
56 #define QEDE_PF_STAT(stat_name) _QEDE_STAT(stat_name, true)
57 #define QEDE_STAT(stat_name)    _QEDE_STAT(stat_name, false)
58 static const struct {
59         u64 offset;
60         char string[ETH_GSTRING_LEN];
61         bool pf_only;
62 } qede_stats_arr[] = {
63         QEDE_STAT(rx_ucast_bytes),
64         QEDE_STAT(rx_mcast_bytes),
65         QEDE_STAT(rx_bcast_bytes),
66         QEDE_STAT(rx_ucast_pkts),
67         QEDE_STAT(rx_mcast_pkts),
68         QEDE_STAT(rx_bcast_pkts),
69
70         QEDE_STAT(tx_ucast_bytes),
71         QEDE_STAT(tx_mcast_bytes),
72         QEDE_STAT(tx_bcast_bytes),
73         QEDE_STAT(tx_ucast_pkts),
74         QEDE_STAT(tx_mcast_pkts),
75         QEDE_STAT(tx_bcast_pkts),
76
77         QEDE_PF_STAT(rx_64_byte_packets),
78         QEDE_PF_STAT(rx_65_to_127_byte_packets),
79         QEDE_PF_STAT(rx_128_to_255_byte_packets),
80         QEDE_PF_STAT(rx_256_to_511_byte_packets),
81         QEDE_PF_STAT(rx_512_to_1023_byte_packets),
82         QEDE_PF_STAT(rx_1024_to_1518_byte_packets),
83         QEDE_PF_STAT(rx_1519_to_1522_byte_packets),
84         QEDE_PF_STAT(rx_1519_to_2047_byte_packets),
85         QEDE_PF_STAT(rx_2048_to_4095_byte_packets),
86         QEDE_PF_STAT(rx_4096_to_9216_byte_packets),
87         QEDE_PF_STAT(rx_9217_to_16383_byte_packets),
88         QEDE_PF_STAT(tx_64_byte_packets),
89         QEDE_PF_STAT(tx_65_to_127_byte_packets),
90         QEDE_PF_STAT(tx_128_to_255_byte_packets),
91         QEDE_PF_STAT(tx_256_to_511_byte_packets),
92         QEDE_PF_STAT(tx_512_to_1023_byte_packets),
93         QEDE_PF_STAT(tx_1024_to_1518_byte_packets),
94         QEDE_PF_STAT(tx_1519_to_2047_byte_packets),
95         QEDE_PF_STAT(tx_2048_to_4095_byte_packets),
96         QEDE_PF_STAT(tx_4096_to_9216_byte_packets),
97         QEDE_PF_STAT(tx_9217_to_16383_byte_packets),
98
99         QEDE_PF_STAT(rx_mac_crtl_frames),
100         QEDE_PF_STAT(tx_mac_ctrl_frames),
101         QEDE_PF_STAT(rx_pause_frames),
102         QEDE_PF_STAT(tx_pause_frames),
103         QEDE_PF_STAT(rx_pfc_frames),
104         QEDE_PF_STAT(tx_pfc_frames),
105
106         QEDE_PF_STAT(rx_crc_errors),
107         QEDE_PF_STAT(rx_align_errors),
108         QEDE_PF_STAT(rx_carrier_errors),
109         QEDE_PF_STAT(rx_oversize_packets),
110         QEDE_PF_STAT(rx_jabbers),
111         QEDE_PF_STAT(rx_undersize_packets),
112         QEDE_PF_STAT(rx_fragments),
113         QEDE_PF_STAT(tx_lpi_entry_count),
114         QEDE_PF_STAT(tx_total_collisions),
115         QEDE_PF_STAT(brb_truncates),
116         QEDE_PF_STAT(brb_discards),
117         QEDE_STAT(no_buff_discards),
118         QEDE_PF_STAT(mftag_filter_discards),
119         QEDE_PF_STAT(mac_filter_discards),
120         QEDE_STAT(tx_err_drop_pkts),
121         QEDE_STAT(ttl0_discard),
122         QEDE_STAT(packet_too_big_discard),
123
124         QEDE_STAT(coalesced_pkts),
125         QEDE_STAT(coalesced_events),
126         QEDE_STAT(coalesced_aborts_num),
127         QEDE_STAT(non_coalesced_pkts),
128         QEDE_STAT(coalesced_bytes),
129 };
130
131 #define QEDE_NUM_STATS  ARRAY_SIZE(qede_stats_arr)
132
133 enum {
134         QEDE_PRI_FLAG_CMT,
135         QEDE_PRI_FLAG_LEN,
136 };
137
138 static const char qede_private_arr[QEDE_PRI_FLAG_LEN][ETH_GSTRING_LEN] = {
139         "Coupled-Function",
140 };
141
142 enum qede_ethtool_tests {
143         QEDE_ETHTOOL_INT_LOOPBACK,
144         QEDE_ETHTOOL_INTERRUPT_TEST,
145         QEDE_ETHTOOL_MEMORY_TEST,
146         QEDE_ETHTOOL_REGISTER_TEST,
147         QEDE_ETHTOOL_CLOCK_TEST,
148         QEDE_ETHTOOL_NVRAM_TEST,
149         QEDE_ETHTOOL_TEST_MAX
150 };
151
152 static const char qede_tests_str_arr[QEDE_ETHTOOL_TEST_MAX][ETH_GSTRING_LEN] = {
153         "Internal loopback (offline)",
154         "Interrupt (online)\t",
155         "Memory (online)\t\t",
156         "Register (online)\t",
157         "Clock (online)\t\t",
158         "Nvram (online)\t\t",
159 };
160
161 static void qede_get_strings_stats_txq(struct qede_dev *edev,
162                                        struct qede_tx_queue *txq, u8 **buf)
163 {
164         int i;
165
166         for (i = 0; i < QEDE_NUM_TQSTATS; i++) {
167                 sprintf(*buf, "%d: %s", txq->index,
168                         qede_tqstats_arr[i].string);
169                 *buf += ETH_GSTRING_LEN;
170         }
171 }
172
173 static void qede_get_strings_stats_rxq(struct qede_dev *edev,
174                                        struct qede_rx_queue *rxq, u8 **buf)
175 {
176         int i;
177
178         for (i = 0; i < QEDE_NUM_RQSTATS; i++) {
179                 sprintf(*buf, "%d: %s", rxq->rxq_id,
180                         qede_rqstats_arr[i].string);
181                 *buf += ETH_GSTRING_LEN;
182         }
183 }
184
185 static void qede_get_strings_stats(struct qede_dev *edev, u8 *buf)
186 {
187         struct qede_fastpath *fp;
188         int i;
189
190         /* Account for queue statistics */
191         for (i = 0; i < QEDE_QUEUE_CNT(edev); i++) {
192                 fp = &edev->fp_array[i];
193
194                 if (fp->type & QEDE_FASTPATH_RX)
195                         qede_get_strings_stats_rxq(edev, fp->rxq, &buf);
196
197                 if (fp->type & QEDE_FASTPATH_TX)
198                         qede_get_strings_stats_txq(edev, fp->txq, &buf);
199         }
200
201         /* Account for non-queue statistics */
202         for (i = 0; i < QEDE_NUM_STATS; i++) {
203                 if (IS_VF(edev) && qede_stats_arr[i].pf_only)
204                         continue;
205                 strcpy(buf, qede_stats_arr[i].string);
206                 buf += ETH_GSTRING_LEN;
207         }
208 }
209
210 static void qede_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
211 {
212         struct qede_dev *edev = netdev_priv(dev);
213
214         switch (stringset) {
215         case ETH_SS_STATS:
216                 qede_get_strings_stats(edev, buf);
217                 break;
218         case ETH_SS_PRIV_FLAGS:
219                 memcpy(buf, qede_private_arr,
220                        ETH_GSTRING_LEN * QEDE_PRI_FLAG_LEN);
221                 break;
222         case ETH_SS_TEST:
223                 memcpy(buf, qede_tests_str_arr,
224                        ETH_GSTRING_LEN * QEDE_ETHTOOL_TEST_MAX);
225                 break;
226         default:
227                 DP_VERBOSE(edev, QED_MSG_DEBUG,
228                            "Unsupported stringset 0x%08x\n", stringset);
229         }
230 }
231
232 static void qede_get_ethtool_stats_txq(struct qede_tx_queue *txq, u64 **buf)
233 {
234         int i;
235
236         for (i = 0; i < QEDE_NUM_TQSTATS; i++) {
237                 **buf = *((u64 *)(((void *)txq) + qede_tqstats_arr[i].offset));
238                 (*buf)++;
239         }
240 }
241
242 static void qede_get_ethtool_stats_rxq(struct qede_rx_queue *rxq, u64 **buf)
243 {
244         int i;
245
246         for (i = 0; i < QEDE_NUM_RQSTATS; i++) {
247                 **buf = *((u64 *)(((void *)rxq) + qede_rqstats_arr[i].offset));
248                 (*buf)++;
249         }
250 }
251
252 static void qede_get_ethtool_stats(struct net_device *dev,
253                                    struct ethtool_stats *stats, u64 *buf)
254 {
255         struct qede_dev *edev = netdev_priv(dev);
256         struct qede_fastpath *fp;
257         int i;
258
259         qede_fill_by_demand_stats(edev);
260
261         /* Need to protect the access to the fastpath array */
262         __qede_lock(edev);
263
264         for (i = 0; i < QEDE_QUEUE_CNT(edev); i++) {
265                 fp = &edev->fp_array[i];
266
267                 if (fp->type & QEDE_FASTPATH_RX)
268                         qede_get_ethtool_stats_rxq(fp->rxq, &buf);
269
270                 if (fp->type & QEDE_FASTPATH_TX)
271                         qede_get_ethtool_stats_txq(fp->txq, &buf);
272         }
273
274         for (i = 0; i < QEDE_NUM_STATS; i++) {
275                 if (IS_VF(edev) && qede_stats_arr[i].pf_only)
276                         continue;
277                 *buf = *((u64 *)(((void *)&edev->stats) +
278                                  qede_stats_arr[i].offset));
279
280                 buf++;
281         }
282
283         __qede_unlock(edev);
284 }
285
286 static int qede_get_sset_count(struct net_device *dev, int stringset)
287 {
288         struct qede_dev *edev = netdev_priv(dev);
289         int num_stats = QEDE_NUM_STATS;
290
291         switch (stringset) {
292         case ETH_SS_STATS:
293                 if (IS_VF(edev)) {
294                         int i;
295
296                         for (i = 0; i < QEDE_NUM_STATS; i++)
297                                 if (qede_stats_arr[i].pf_only)
298                                         num_stats--;
299                 }
300
301                 /* Account for the Regular Tx statistics */
302                 num_stats += QEDE_TSS_COUNT(edev) * QEDE_NUM_TQSTATS;
303
304                 /* Account for the Regular Rx statistics */
305                 num_stats += QEDE_RSS_COUNT(edev) * QEDE_NUM_RQSTATS;
306
307                 return num_stats;
308
309         case ETH_SS_PRIV_FLAGS:
310                 return QEDE_PRI_FLAG_LEN;
311         case ETH_SS_TEST:
312                 if (!IS_VF(edev))
313                         return QEDE_ETHTOOL_TEST_MAX;
314                 else
315                         return 0;
316         default:
317                 DP_VERBOSE(edev, QED_MSG_DEBUG,
318                            "Unsupported stringset 0x%08x\n", stringset);
319                 return -EINVAL;
320         }
321 }
322
323 static u32 qede_get_priv_flags(struct net_device *dev)
324 {
325         struct qede_dev *edev = netdev_priv(dev);
326
327         return (!!(edev->dev_info.common.num_hwfns > 1)) << QEDE_PRI_FLAG_CMT;
328 }
329
330 struct qede_link_mode_mapping {
331         u32 qed_link_mode;
332         u32 ethtool_link_mode;
333 };
334
335 static const struct qede_link_mode_mapping qed_lm_map[] = {
336         {QED_LM_FIBRE_BIT, ETHTOOL_LINK_MODE_FIBRE_BIT},
337         {QED_LM_Autoneg_BIT, ETHTOOL_LINK_MODE_Autoneg_BIT},
338         {QED_LM_Asym_Pause_BIT, ETHTOOL_LINK_MODE_Asym_Pause_BIT},
339         {QED_LM_Pause_BIT, ETHTOOL_LINK_MODE_Pause_BIT},
340         {QED_LM_1000baseT_Half_BIT, ETHTOOL_LINK_MODE_1000baseT_Half_BIT},
341         {QED_LM_1000baseT_Full_BIT, ETHTOOL_LINK_MODE_1000baseT_Full_BIT},
342         {QED_LM_10000baseKR_Full_BIT, ETHTOOL_LINK_MODE_10000baseKR_Full_BIT},
343         {QED_LM_25000baseKR_Full_BIT, ETHTOOL_LINK_MODE_25000baseKR_Full_BIT},
344         {QED_LM_40000baseLR4_Full_BIT, ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT},
345         {QED_LM_50000baseKR2_Full_BIT, ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT},
346         {QED_LM_100000baseKR4_Full_BIT,
347          ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT},
348 };
349
350 #define QEDE_DRV_TO_ETHTOOL_CAPS(caps, lk_ksettings, name)      \
351 {                                                               \
352         int i;                                                  \
353                                                                 \
354         for (i = 0; i < ARRAY_SIZE(qed_lm_map); i++) {          \
355                 if ((caps) & (qed_lm_map[i].qed_link_mode))     \
356                         __set_bit(qed_lm_map[i].ethtool_link_mode,\
357                                   lk_ksettings->link_modes.name); \
358         }                                                       \
359 }
360
361 #define QEDE_ETHTOOL_TO_DRV_CAPS(caps, lk_ksettings, name)      \
362 {                                                               \
363         int i;                                                  \
364                                                                 \
365         for (i = 0; i < ARRAY_SIZE(qed_lm_map); i++) {          \
366                 if (test_bit(qed_lm_map[i].ethtool_link_mode,   \
367                              lk_ksettings->link_modes.name))    \
368                         caps |= qed_lm_map[i].qed_link_mode;    \
369         }                                                       \
370 }
371
372 static int qede_get_link_ksettings(struct net_device *dev,
373                                    struct ethtool_link_ksettings *cmd)
374 {
375         struct ethtool_link_settings *base = &cmd->base;
376         struct qede_dev *edev = netdev_priv(dev);
377         struct qed_link_output current_link;
378
379         __qede_lock(edev);
380
381         memset(&current_link, 0, sizeof(current_link));
382         edev->ops->common->get_link(edev->cdev, &current_link);
383
384         ethtool_link_ksettings_zero_link_mode(cmd, supported);
385         QEDE_DRV_TO_ETHTOOL_CAPS(current_link.supported_caps, cmd, supported)
386
387         ethtool_link_ksettings_zero_link_mode(cmd, advertising);
388         QEDE_DRV_TO_ETHTOOL_CAPS(current_link.advertised_caps, cmd, advertising)
389
390         ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising);
391         QEDE_DRV_TO_ETHTOOL_CAPS(current_link.lp_caps, cmd, lp_advertising)
392
393         if ((edev->state == QEDE_STATE_OPEN) && (current_link.link_up)) {
394                 base->speed = current_link.speed;
395                 base->duplex = current_link.duplex;
396         } else {
397                 base->speed = SPEED_UNKNOWN;
398                 base->duplex = DUPLEX_UNKNOWN;
399         }
400
401         __qede_unlock(edev);
402
403         base->port = current_link.port;
404         base->autoneg = (current_link.autoneg) ? AUTONEG_ENABLE :
405                         AUTONEG_DISABLE;
406
407         return 0;
408 }
409
410 static int qede_set_link_ksettings(struct net_device *dev,
411                                    const struct ethtool_link_ksettings *cmd)
412 {
413         const struct ethtool_link_settings *base = &cmd->base;
414         struct qede_dev *edev = netdev_priv(dev);
415         struct qed_link_output current_link;
416         struct qed_link_params params;
417
418         if (!edev->ops || !edev->ops->common->can_link_change(edev->cdev)) {
419                 DP_INFO(edev, "Link settings are not allowed to be changed\n");
420                 return -EOPNOTSUPP;
421         }
422         memset(&current_link, 0, sizeof(current_link));
423         memset(&params, 0, sizeof(params));
424         edev->ops->common->get_link(edev->cdev, &current_link);
425
426         params.override_flags |= QED_LINK_OVERRIDE_SPEED_ADV_SPEEDS;
427         params.override_flags |= QED_LINK_OVERRIDE_SPEED_AUTONEG;
428         if (base->autoneg == AUTONEG_ENABLE) {
429                 params.autoneg = true;
430                 params.forced_speed = 0;
431                 QEDE_ETHTOOL_TO_DRV_CAPS(params.adv_speeds, cmd, advertising)
432         } else {                /* forced speed */
433                 params.override_flags |= QED_LINK_OVERRIDE_SPEED_FORCED_SPEED;
434                 params.autoneg = false;
435                 params.forced_speed = base->speed;
436                 switch (base->speed) {
437                 case SPEED_10000:
438                         if (!(current_link.supported_caps &
439                               QED_LM_10000baseKR_Full_BIT)) {
440                                 DP_INFO(edev, "10G speed not supported\n");
441                                 return -EINVAL;
442                         }
443                         params.adv_speeds = QED_LM_10000baseKR_Full_BIT;
444                         break;
445                 case SPEED_25000:
446                         if (!(current_link.supported_caps &
447                               QED_LM_25000baseKR_Full_BIT)) {
448                                 DP_INFO(edev, "25G speed not supported\n");
449                                 return -EINVAL;
450                         }
451                         params.adv_speeds = QED_LM_25000baseKR_Full_BIT;
452                         break;
453                 case SPEED_40000:
454                         if (!(current_link.supported_caps &
455                               QED_LM_40000baseLR4_Full_BIT)) {
456                                 DP_INFO(edev, "40G speed not supported\n");
457                                 return -EINVAL;
458                         }
459                         params.adv_speeds = QED_LM_40000baseLR4_Full_BIT;
460                         break;
461                 case SPEED_50000:
462                         if (!(current_link.supported_caps &
463                               QED_LM_50000baseKR2_Full_BIT)) {
464                                 DP_INFO(edev, "50G speed not supported\n");
465                                 return -EINVAL;
466                         }
467                         params.adv_speeds = QED_LM_50000baseKR2_Full_BIT;
468                         break;
469                 case SPEED_100000:
470                         if (!(current_link.supported_caps &
471                               QED_LM_100000baseKR4_Full_BIT)) {
472                                 DP_INFO(edev, "100G speed not supported\n");
473                                 return -EINVAL;
474                         }
475                         params.adv_speeds = QED_LM_100000baseKR4_Full_BIT;
476                         break;
477                 default:
478                         DP_INFO(edev, "Unsupported speed %u\n", base->speed);
479                         return -EINVAL;
480                 }
481         }
482
483         params.link_up = true;
484         edev->ops->common->set_link(edev->cdev, &params);
485
486         return 0;
487 }
488
489 static void qede_get_drvinfo(struct net_device *ndev,
490                              struct ethtool_drvinfo *info)
491 {
492         char mfw[ETHTOOL_FWVERS_LEN], storm[ETHTOOL_FWVERS_LEN];
493         struct qede_dev *edev = netdev_priv(ndev);
494
495         strlcpy(info->driver, "qede", sizeof(info->driver));
496         strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
497
498         snprintf(storm, ETHTOOL_FWVERS_LEN, "%d.%d.%d.%d",
499                  edev->dev_info.common.fw_major,
500                  edev->dev_info.common.fw_minor,
501                  edev->dev_info.common.fw_rev,
502                  edev->dev_info.common.fw_eng);
503
504         snprintf(mfw, ETHTOOL_FWVERS_LEN, "%d.%d.%d.%d",
505                  (edev->dev_info.common.mfw_rev >> 24) & 0xFF,
506                  (edev->dev_info.common.mfw_rev >> 16) & 0xFF,
507                  (edev->dev_info.common.mfw_rev >> 8) & 0xFF,
508                  edev->dev_info.common.mfw_rev & 0xFF);
509
510         if ((strlen(storm) + strlen(mfw) + strlen("mfw storm  ")) <
511             sizeof(info->fw_version)) {
512                 snprintf(info->fw_version, sizeof(info->fw_version),
513                          "mfw %s storm %s", mfw, storm);
514         } else {
515                 snprintf(info->fw_version, sizeof(info->fw_version),
516                          "%s %s", mfw, storm);
517         }
518
519         strlcpy(info->bus_info, pci_name(edev->pdev), sizeof(info->bus_info));
520 }
521
522 static void qede_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
523 {
524         struct qede_dev *edev = netdev_priv(ndev);
525
526         if (edev->dev_info.common.wol_support) {
527                 wol->supported = WAKE_MAGIC;
528                 wol->wolopts = edev->wol_enabled ? WAKE_MAGIC : 0;
529         }
530 }
531
532 static int qede_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
533 {
534         struct qede_dev *edev = netdev_priv(ndev);
535         bool wol_requested;
536         int rc;
537
538         if (wol->wolopts & ~WAKE_MAGIC) {
539                 DP_INFO(edev,
540                         "Can't support WoL options other than magic-packet\n");
541                 return -EINVAL;
542         }
543
544         wol_requested = !!(wol->wolopts & WAKE_MAGIC);
545         if (wol_requested == edev->wol_enabled)
546                 return 0;
547
548         /* Need to actually change configuration */
549         if (!edev->dev_info.common.wol_support) {
550                 DP_INFO(edev, "Device doesn't support WoL\n");
551                 return -EINVAL;
552         }
553
554         rc = edev->ops->common->update_wol(edev->cdev, wol_requested);
555         if (!rc)
556                 edev->wol_enabled = wol_requested;
557
558         return rc;
559 }
560
561 static u32 qede_get_msglevel(struct net_device *ndev)
562 {
563         struct qede_dev *edev = netdev_priv(ndev);
564
565         return ((u32)edev->dp_level << QED_LOG_LEVEL_SHIFT) | edev->dp_module;
566 }
567
568 static void qede_set_msglevel(struct net_device *ndev, u32 level)
569 {
570         struct qede_dev *edev = netdev_priv(ndev);
571         u32 dp_module = 0;
572         u8 dp_level = 0;
573
574         qede_config_debug(level, &dp_module, &dp_level);
575
576         edev->dp_level = dp_level;
577         edev->dp_module = dp_module;
578         edev->ops->common->update_msglvl(edev->cdev,
579                                          dp_module, dp_level);
580 }
581
582 static int qede_nway_reset(struct net_device *dev)
583 {
584         struct qede_dev *edev = netdev_priv(dev);
585         struct qed_link_output current_link;
586         struct qed_link_params link_params;
587
588         if (!edev->ops || !edev->ops->common->can_link_change(edev->cdev)) {
589                 DP_INFO(edev, "Link settings are not allowed to be changed\n");
590                 return -EOPNOTSUPP;
591         }
592
593         if (!netif_running(dev))
594                 return 0;
595
596         memset(&current_link, 0, sizeof(current_link));
597         edev->ops->common->get_link(edev->cdev, &current_link);
598         if (!current_link.link_up)
599                 return 0;
600
601         /* Toggle the link */
602         memset(&link_params, 0, sizeof(link_params));
603         link_params.link_up = false;
604         edev->ops->common->set_link(edev->cdev, &link_params);
605         link_params.link_up = true;
606         edev->ops->common->set_link(edev->cdev, &link_params);
607
608         return 0;
609 }
610
611 static u32 qede_get_link(struct net_device *dev)
612 {
613         struct qede_dev *edev = netdev_priv(dev);
614         struct qed_link_output current_link;
615
616         memset(&current_link, 0, sizeof(current_link));
617         edev->ops->common->get_link(edev->cdev, &current_link);
618
619         return current_link.link_up;
620 }
621
622 static int qede_get_coalesce(struct net_device *dev,
623                              struct ethtool_coalesce *coal)
624 {
625         struct qede_dev *edev = netdev_priv(dev);
626         u16 rxc, txc;
627
628         memset(coal, 0, sizeof(struct ethtool_coalesce));
629         edev->ops->common->get_coalesce(edev->cdev, &rxc, &txc);
630
631         coal->rx_coalesce_usecs = rxc;
632         coal->tx_coalesce_usecs = txc;
633
634         return 0;
635 }
636
637 static int qede_set_coalesce(struct net_device *dev,
638                              struct ethtool_coalesce *coal)
639 {
640         struct qede_dev *edev = netdev_priv(dev);
641         int i, rc = 0;
642         u16 rxc, txc;
643         u8 sb_id;
644
645         if (!netif_running(dev)) {
646                 DP_INFO(edev, "Interface is down\n");
647                 return -EINVAL;
648         }
649
650         if (coal->rx_coalesce_usecs > QED_COALESCE_MAX ||
651             coal->tx_coalesce_usecs > QED_COALESCE_MAX) {
652                 DP_INFO(edev,
653                         "Can't support requested %s coalesce value [max supported value %d]\n",
654                         coal->rx_coalesce_usecs > QED_COALESCE_MAX ? "rx"
655                                                                    : "tx",
656                         QED_COALESCE_MAX);
657                 return -EINVAL;
658         }
659
660         rxc = (u16)coal->rx_coalesce_usecs;
661         txc = (u16)coal->tx_coalesce_usecs;
662         for_each_queue(i) {
663                 sb_id = edev->fp_array[i].sb_info->igu_sb_id;
664                 rc = edev->ops->common->set_coalesce(edev->cdev, rxc, txc,
665                                                      (u8)i, sb_id);
666                 if (rc) {
667                         DP_INFO(edev, "Set coalesce error, rc = %d\n", rc);
668                         return rc;
669                 }
670         }
671
672         return rc;
673 }
674
675 static void qede_get_ringparam(struct net_device *dev,
676                                struct ethtool_ringparam *ering)
677 {
678         struct qede_dev *edev = netdev_priv(dev);
679
680         ering->rx_max_pending = NUM_RX_BDS_MAX;
681         ering->rx_pending = edev->q_num_rx_buffers;
682         ering->tx_max_pending = NUM_TX_BDS_MAX;
683         ering->tx_pending = edev->q_num_tx_buffers;
684 }
685
686 static int qede_set_ringparam(struct net_device *dev,
687                               struct ethtool_ringparam *ering)
688 {
689         struct qede_dev *edev = netdev_priv(dev);
690
691         DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
692                    "Set ring params command parameters: rx_pending = %d, tx_pending = %d\n",
693                    ering->rx_pending, ering->tx_pending);
694
695         /* Validate legality of configuration */
696         if (ering->rx_pending > NUM_RX_BDS_MAX ||
697             ering->rx_pending < NUM_RX_BDS_MIN ||
698             ering->tx_pending > NUM_TX_BDS_MAX ||
699             ering->tx_pending < NUM_TX_BDS_MIN) {
700                 DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
701                            "Can only support Rx Buffer size [0%08x,...,0x%08x] and Tx Buffer size [0x%08x,...,0x%08x]\n",
702                            NUM_RX_BDS_MIN, NUM_RX_BDS_MAX,
703                            NUM_TX_BDS_MIN, NUM_TX_BDS_MAX);
704                 return -EINVAL;
705         }
706
707         /* Change ring size and re-load */
708         edev->q_num_rx_buffers = ering->rx_pending;
709         edev->q_num_tx_buffers = ering->tx_pending;
710
711         qede_reload(edev, NULL, false);
712
713         return 0;
714 }
715
716 static void qede_get_pauseparam(struct net_device *dev,
717                                 struct ethtool_pauseparam *epause)
718 {
719         struct qede_dev *edev = netdev_priv(dev);
720         struct qed_link_output current_link;
721
722         memset(&current_link, 0, sizeof(current_link));
723         edev->ops->common->get_link(edev->cdev, &current_link);
724
725         if (current_link.pause_config & QED_LINK_PAUSE_AUTONEG_ENABLE)
726                 epause->autoneg = true;
727         if (current_link.pause_config & QED_LINK_PAUSE_RX_ENABLE)
728                 epause->rx_pause = true;
729         if (current_link.pause_config & QED_LINK_PAUSE_TX_ENABLE)
730                 epause->tx_pause = true;
731
732         DP_VERBOSE(edev, QED_MSG_DEBUG,
733                    "ethtool_pauseparam: cmd %d  autoneg %d  rx_pause %d  tx_pause %d\n",
734                    epause->cmd, epause->autoneg, epause->rx_pause,
735                    epause->tx_pause);
736 }
737
738 static int qede_set_pauseparam(struct net_device *dev,
739                                struct ethtool_pauseparam *epause)
740 {
741         struct qede_dev *edev = netdev_priv(dev);
742         struct qed_link_params params;
743         struct qed_link_output current_link;
744
745         if (!edev->ops || !edev->ops->common->can_link_change(edev->cdev)) {
746                 DP_INFO(edev,
747                         "Pause settings are not allowed to be changed\n");
748                 return -EOPNOTSUPP;
749         }
750
751         memset(&current_link, 0, sizeof(current_link));
752         edev->ops->common->get_link(edev->cdev, &current_link);
753
754         memset(&params, 0, sizeof(params));
755         params.override_flags |= QED_LINK_OVERRIDE_PAUSE_CONFIG;
756         if (epause->autoneg) {
757                 if (!(current_link.supported_caps & QED_LM_Autoneg_BIT)) {
758                         DP_INFO(edev, "autoneg not supported\n");
759                         return -EINVAL;
760                 }
761                 params.pause_config |= QED_LINK_PAUSE_AUTONEG_ENABLE;
762         }
763         if (epause->rx_pause)
764                 params.pause_config |= QED_LINK_PAUSE_RX_ENABLE;
765         if (epause->tx_pause)
766                 params.pause_config |= QED_LINK_PAUSE_TX_ENABLE;
767
768         params.link_up = true;
769         edev->ops->common->set_link(edev->cdev, &params);
770
771         return 0;
772 }
773
774 static void qede_get_regs(struct net_device *ndev,
775                           struct ethtool_regs *regs, void *buffer)
776 {
777         struct qede_dev *edev = netdev_priv(ndev);
778
779         regs->version = 0;
780         memset(buffer, 0, regs->len);
781
782         if (edev->ops && edev->ops->common)
783                 edev->ops->common->dbg_all_data(edev->cdev, buffer);
784 }
785
786 static int qede_get_regs_len(struct net_device *ndev)
787 {
788         struct qede_dev *edev = netdev_priv(ndev);
789
790         if (edev->ops && edev->ops->common)
791                 return edev->ops->common->dbg_all_data_size(edev->cdev);
792         else
793                 return -EINVAL;
794 }
795
796 static void qede_update_mtu(struct qede_dev *edev,
797                             struct qede_reload_args *args)
798 {
799         edev->ndev->mtu = args->u.mtu;
800 }
801
802 /* Netdevice NDOs */
803 int qede_change_mtu(struct net_device *ndev, int new_mtu)
804 {
805         struct qede_dev *edev = netdev_priv(ndev);
806         struct qede_reload_args args;
807
808         DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
809                    "Configuring MTU size of %d\n", new_mtu);
810
811         /* Set the mtu field and re-start the interface if needed */
812         args.u.mtu = new_mtu;
813         args.func = &qede_update_mtu;
814         qede_reload(edev, &args, false);
815
816         edev->ops->common->update_mtu(edev->cdev, new_mtu);
817
818         return 0;
819 }
820
821 static void qede_get_channels(struct net_device *dev,
822                               struct ethtool_channels *channels)
823 {
824         struct qede_dev *edev = netdev_priv(dev);
825
826         channels->max_combined = QEDE_MAX_RSS_CNT(edev);
827         channels->max_rx = QEDE_MAX_RSS_CNT(edev);
828         channels->max_tx = QEDE_MAX_RSS_CNT(edev);
829         channels->combined_count = QEDE_QUEUE_CNT(edev) - edev->fp_num_tx -
830                                         edev->fp_num_rx;
831         channels->tx_count = edev->fp_num_tx;
832         channels->rx_count = edev->fp_num_rx;
833 }
834
835 static int qede_set_channels(struct net_device *dev,
836                              struct ethtool_channels *channels)
837 {
838         struct qede_dev *edev = netdev_priv(dev);
839         u32 count;
840
841         DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
842                    "set-channels command parameters: rx = %d, tx = %d, other = %d, combined = %d\n",
843                    channels->rx_count, channels->tx_count,
844                    channels->other_count, channels->combined_count);
845
846         count = channels->rx_count + channels->tx_count +
847                         channels->combined_count;
848
849         /* We don't support `other' channels */
850         if (channels->other_count) {
851                 DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
852                            "command parameters not supported\n");
853                 return -EINVAL;
854         }
855
856         if (!(channels->combined_count || (channels->rx_count &&
857                                            channels->tx_count))) {
858                 DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
859                            "need to request at least one transmit and one receive channel\n");
860                 return -EINVAL;
861         }
862
863         if (count > QEDE_MAX_RSS_CNT(edev)) {
864                 DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
865                            "requested channels = %d max supported channels = %d\n",
866                            count, QEDE_MAX_RSS_CNT(edev));
867                 return -EINVAL;
868         }
869
870         /* Check if there was a change in the active parameters */
871         if ((count == QEDE_QUEUE_CNT(edev)) &&
872             (channels->tx_count == edev->fp_num_tx) &&
873             (channels->rx_count == edev->fp_num_rx)) {
874                 DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
875                            "No change in active parameters\n");
876                 return 0;
877         }
878
879         /* We need the number of queues to be divisible between the hwfns */
880         if ((count % edev->dev_info.common.num_hwfns) ||
881             (channels->tx_count % edev->dev_info.common.num_hwfns) ||
882             (channels->rx_count % edev->dev_info.common.num_hwfns)) {
883                 DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
884                            "Number of channels must be divisible by %04x\n",
885                            edev->dev_info.common.num_hwfns);
886                 return -EINVAL;
887         }
888
889         /* Set number of queues and reload if necessary */
890         edev->req_queues = count;
891         edev->req_num_tx = channels->tx_count;
892         edev->req_num_rx = channels->rx_count;
893         /* Reset the indirection table if rx queue count is updated */
894         if ((edev->req_queues - edev->req_num_tx) != QEDE_RSS_COUNT(edev)) {
895                 edev->rss_params_inited &= ~QEDE_RSS_INDIR_INITED;
896                 memset(&edev->rss_params.rss_ind_table, 0,
897                        sizeof(edev->rss_params.rss_ind_table));
898         }
899
900         qede_reload(edev, NULL, false);
901
902         return 0;
903 }
904
905 static int qede_set_phys_id(struct net_device *dev,
906                             enum ethtool_phys_id_state state)
907 {
908         struct qede_dev *edev = netdev_priv(dev);
909         u8 led_state = 0;
910
911         switch (state) {
912         case ETHTOOL_ID_ACTIVE:
913                 return 1;       /* cycle on/off once per second */
914
915         case ETHTOOL_ID_ON:
916                 led_state = QED_LED_MODE_ON;
917                 break;
918
919         case ETHTOOL_ID_OFF:
920                 led_state = QED_LED_MODE_OFF;
921                 break;
922
923         case ETHTOOL_ID_INACTIVE:
924                 led_state = QED_LED_MODE_RESTORE;
925                 break;
926         }
927
928         edev->ops->common->set_led(edev->cdev, led_state);
929
930         return 0;
931 }
932
933 static int qede_get_rss_flags(struct qede_dev *edev, struct ethtool_rxnfc *info)
934 {
935         info->data = RXH_IP_SRC | RXH_IP_DST;
936
937         switch (info->flow_type) {
938         case TCP_V4_FLOW:
939         case TCP_V6_FLOW:
940                 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
941                 break;
942         case UDP_V4_FLOW:
943                 if (edev->rss_params.rss_caps & QED_RSS_IPV4_UDP)
944                         info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
945                 break;
946         case UDP_V6_FLOW:
947                 if (edev->rss_params.rss_caps & QED_RSS_IPV6_UDP)
948                         info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
949                 break;
950         case IPV4_FLOW:
951         case IPV6_FLOW:
952                 break;
953         default:
954                 info->data = 0;
955                 break;
956         }
957
958         return 0;
959 }
960
961 static int qede_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
962                           u32 *rules __always_unused)
963 {
964         struct qede_dev *edev = netdev_priv(dev);
965
966         switch (info->cmd) {
967         case ETHTOOL_GRXRINGS:
968                 info->data = QEDE_RSS_COUNT(edev);
969                 return 0;
970         case ETHTOOL_GRXFH:
971                 return qede_get_rss_flags(edev, info);
972         default:
973                 DP_ERR(edev, "Command parameters not supported\n");
974                 return -EOPNOTSUPP;
975         }
976 }
977
978 static int qede_set_rss_flags(struct qede_dev *edev, struct ethtool_rxnfc *info)
979 {
980         struct qed_update_vport_params vport_update_params;
981         u8 set_caps = 0, clr_caps = 0;
982
983         DP_VERBOSE(edev, QED_MSG_DEBUG,
984                    "Set rss flags command parameters: flow type = %d, data = %llu\n",
985                    info->flow_type, info->data);
986
987         switch (info->flow_type) {
988         case TCP_V4_FLOW:
989         case TCP_V6_FLOW:
990                 /* For TCP only 4-tuple hash is supported */
991                 if (info->data ^ (RXH_IP_SRC | RXH_IP_DST |
992                                   RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
993                         DP_INFO(edev, "Command parameters not supported\n");
994                         return -EINVAL;
995                 }
996                 return 0;
997         case UDP_V4_FLOW:
998                 /* For UDP either 2-tuple hash or 4-tuple hash is supported */
999                 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1000                                    RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1001                         set_caps = QED_RSS_IPV4_UDP;
1002                         DP_VERBOSE(edev, QED_MSG_DEBUG,
1003                                    "UDP 4-tuple enabled\n");
1004                 } else if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1005                         clr_caps = QED_RSS_IPV4_UDP;
1006                         DP_VERBOSE(edev, QED_MSG_DEBUG,
1007                                    "UDP 4-tuple disabled\n");
1008                 } else {
1009                         return -EINVAL;
1010                 }
1011                 break;
1012         case UDP_V6_FLOW:
1013                 /* For UDP either 2-tuple hash or 4-tuple hash is supported */
1014                 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1015                                    RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1016                         set_caps = QED_RSS_IPV6_UDP;
1017                         DP_VERBOSE(edev, QED_MSG_DEBUG,
1018                                    "UDP 4-tuple enabled\n");
1019                 } else if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1020                         clr_caps = QED_RSS_IPV6_UDP;
1021                         DP_VERBOSE(edev, QED_MSG_DEBUG,
1022                                    "UDP 4-tuple disabled\n");
1023                 } else {
1024                         return -EINVAL;
1025                 }
1026                 break;
1027         case IPV4_FLOW:
1028         case IPV6_FLOW:
1029                 /* For IP only 2-tuple hash is supported */
1030                 if (info->data ^ (RXH_IP_SRC | RXH_IP_DST)) {
1031                         DP_INFO(edev, "Command parameters not supported\n");
1032                         return -EINVAL;
1033                 }
1034                 return 0;
1035         case SCTP_V4_FLOW:
1036         case AH_ESP_V4_FLOW:
1037         case AH_V4_FLOW:
1038         case ESP_V4_FLOW:
1039         case SCTP_V6_FLOW:
1040         case AH_ESP_V6_FLOW:
1041         case AH_V6_FLOW:
1042         case ESP_V6_FLOW:
1043         case IP_USER_FLOW:
1044         case ETHER_FLOW:
1045                 /* RSS is not supported for these protocols */
1046                 if (info->data) {
1047                         DP_INFO(edev, "Command parameters not supported\n");
1048                         return -EINVAL;
1049                 }
1050                 return 0;
1051         default:
1052                 return -EINVAL;
1053         }
1054
1055         /* No action is needed if there is no change in the rss capability */
1056         if (edev->rss_params.rss_caps == ((edev->rss_params.rss_caps &
1057                                            ~clr_caps) | set_caps))
1058                 return 0;
1059
1060         /* Update internal configuration */
1061         edev->rss_params.rss_caps = (edev->rss_params.rss_caps & ~clr_caps) |
1062                                     set_caps;
1063         edev->rss_params_inited |= QEDE_RSS_CAPS_INITED;
1064
1065         /* Re-configure if possible */
1066         if (netif_running(edev->ndev)) {
1067                 memset(&vport_update_params, 0, sizeof(vport_update_params));
1068                 vport_update_params.update_rss_flg = 1;
1069                 vport_update_params.vport_id = 0;
1070                 memcpy(&vport_update_params.rss_params, &edev->rss_params,
1071                        sizeof(vport_update_params.rss_params));
1072                 return edev->ops->vport_update(edev->cdev,
1073                                                &vport_update_params);
1074         }
1075
1076         return 0;
1077 }
1078
1079 static int qede_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
1080 {
1081         struct qede_dev *edev = netdev_priv(dev);
1082
1083         switch (info->cmd) {
1084         case ETHTOOL_SRXFH:
1085                 return qede_set_rss_flags(edev, info);
1086         default:
1087                 DP_INFO(edev, "Command parameters not supported\n");
1088                 return -EOPNOTSUPP;
1089         }
1090 }
1091
1092 static u32 qede_get_rxfh_indir_size(struct net_device *dev)
1093 {
1094         return QED_RSS_IND_TABLE_SIZE;
1095 }
1096
1097 static u32 qede_get_rxfh_key_size(struct net_device *dev)
1098 {
1099         struct qede_dev *edev = netdev_priv(dev);
1100
1101         return sizeof(edev->rss_params.rss_key);
1102 }
1103
1104 static int qede_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
1105 {
1106         struct qede_dev *edev = netdev_priv(dev);
1107         int i;
1108
1109         if (hfunc)
1110                 *hfunc = ETH_RSS_HASH_TOP;
1111
1112         if (!indir)
1113                 return 0;
1114
1115         for (i = 0; i < QED_RSS_IND_TABLE_SIZE; i++)
1116                 indir[i] = edev->rss_params.rss_ind_table[i];
1117
1118         if (key)
1119                 memcpy(key, edev->rss_params.rss_key,
1120                        qede_get_rxfh_key_size(dev));
1121
1122         return 0;
1123 }
1124
1125 static int qede_set_rxfh(struct net_device *dev, const u32 *indir,
1126                          const u8 *key, const u8 hfunc)
1127 {
1128         struct qed_update_vport_params vport_update_params;
1129         struct qede_dev *edev = netdev_priv(dev);
1130         int i;
1131
1132         if (edev->dev_info.common.num_hwfns > 1) {
1133                 DP_INFO(edev,
1134                         "RSS configuration is not supported for 100G devices\n");
1135                 return -EOPNOTSUPP;
1136         }
1137
1138         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1139                 return -EOPNOTSUPP;
1140
1141         if (!indir && !key)
1142                 return 0;
1143
1144         if (indir) {
1145                 for (i = 0; i < QED_RSS_IND_TABLE_SIZE; i++)
1146                         edev->rss_params.rss_ind_table[i] = indir[i];
1147                 edev->rss_params_inited |= QEDE_RSS_INDIR_INITED;
1148         }
1149
1150         if (key) {
1151                 memcpy(&edev->rss_params.rss_key, key,
1152                        qede_get_rxfh_key_size(dev));
1153                 edev->rss_params_inited |= QEDE_RSS_KEY_INITED;
1154         }
1155
1156         if (netif_running(edev->ndev)) {
1157                 memset(&vport_update_params, 0, sizeof(vport_update_params));
1158                 vport_update_params.update_rss_flg = 1;
1159                 vport_update_params.vport_id = 0;
1160                 memcpy(&vport_update_params.rss_params, &edev->rss_params,
1161                        sizeof(vport_update_params.rss_params));
1162                 return edev->ops->vport_update(edev->cdev,
1163                                                &vport_update_params);
1164         }
1165
1166         return 0;
1167 }
1168
1169 /* This function enables the interrupt generation and the NAPI on the device */
1170 static void qede_netif_start(struct qede_dev *edev)
1171 {
1172         int i;
1173
1174         if (!netif_running(edev->ndev))
1175                 return;
1176
1177         for_each_queue(i) {
1178                 /* Update and reenable interrupts */
1179                 qed_sb_ack(edev->fp_array[i].sb_info, IGU_INT_ENABLE, 1);
1180                 napi_enable(&edev->fp_array[i].napi);
1181         }
1182 }
1183
1184 /* This function disables the NAPI and the interrupt generation on the device */
1185 static void qede_netif_stop(struct qede_dev *edev)
1186 {
1187         int i;
1188
1189         for_each_queue(i) {
1190                 napi_disable(&edev->fp_array[i].napi);
1191                 /* Disable interrupts */
1192                 qed_sb_ack(edev->fp_array[i].sb_info, IGU_INT_DISABLE, 0);
1193         }
1194 }
1195
1196 static int qede_selftest_transmit_traffic(struct qede_dev *edev,
1197                                           struct sk_buff *skb)
1198 {
1199         struct qede_tx_queue *txq = NULL;
1200         struct eth_tx_1st_bd *first_bd;
1201         dma_addr_t mapping;
1202         int i, idx, val;
1203
1204         for_each_queue(i) {
1205                 if (edev->fp_array[i].type & QEDE_FASTPATH_TX) {
1206                         txq = edev->fp_array[i].txq;
1207                         break;
1208                 }
1209         }
1210
1211         if (!txq) {
1212                 DP_NOTICE(edev, "Tx path is not available\n");
1213                 return -1;
1214         }
1215
1216         /* Fill the entry in the SW ring and the BDs in the FW ring */
1217         idx = txq->sw_tx_prod & NUM_TX_BDS_MAX;
1218         txq->sw_tx_ring[idx].skb = skb;
1219         first_bd = qed_chain_produce(&txq->tx_pbl);
1220         memset(first_bd, 0, sizeof(*first_bd));
1221         val = 1 << ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT;
1222         first_bd->data.bd_flags.bitfields = val;
1223         val = skb->len & ETH_TX_DATA_1ST_BD_PKT_LEN_MASK;
1224         first_bd->data.bitfields |= (val << ETH_TX_DATA_1ST_BD_PKT_LEN_SHIFT);
1225
1226         /* Map skb linear data for DMA and set in the first BD */
1227         mapping = dma_map_single(&edev->pdev->dev, skb->data,
1228                                  skb_headlen(skb), DMA_TO_DEVICE);
1229         if (unlikely(dma_mapping_error(&edev->pdev->dev, mapping))) {
1230                 DP_NOTICE(edev, "SKB mapping failed\n");
1231                 return -ENOMEM;
1232         }
1233         BD_SET_UNMAP_ADDR_LEN(first_bd, mapping, skb_headlen(skb));
1234
1235         /* update the first BD with the actual num BDs */
1236         first_bd->data.nbds = 1;
1237         txq->sw_tx_prod++;
1238         /* 'next page' entries are counted in the producer value */
1239         val = cpu_to_le16(qed_chain_get_prod_idx(&txq->tx_pbl));
1240         txq->tx_db.data.bd_prod = val;
1241
1242         /* wmb makes sure that the BDs data is updated before updating the
1243          * producer, otherwise FW may read old data from the BDs.
1244          */
1245         wmb();
1246         barrier();
1247         writel(txq->tx_db.raw, txq->doorbell_addr);
1248
1249         /* mmiowb is needed to synchronize doorbell writes from more than one
1250          * processor. It guarantees that the write arrives to the device before
1251          * the queue lock is released and another start_xmit is called (possibly
1252          * on another CPU). Without this barrier, the next doorbell can bypass
1253          * this doorbell. This is applicable to IA64/Altix systems.
1254          */
1255         mmiowb();
1256
1257         for (i = 0; i < QEDE_SELFTEST_POLL_COUNT; i++) {
1258                 if (qede_txq_has_work(txq))
1259                         break;
1260                 usleep_range(100, 200);
1261         }
1262
1263         if (!qede_txq_has_work(txq)) {
1264                 DP_NOTICE(edev, "Tx completion didn't happen\n");
1265                 return -1;
1266         }
1267
1268         first_bd = (struct eth_tx_1st_bd *)qed_chain_consume(&txq->tx_pbl);
1269         dma_unmap_single(&edev->pdev->dev, BD_UNMAP_ADDR(first_bd),
1270                          BD_UNMAP_LEN(first_bd), DMA_TO_DEVICE);
1271         txq->sw_tx_cons++;
1272         txq->sw_tx_ring[idx].skb = NULL;
1273
1274         return 0;
1275 }
1276
1277 static int qede_selftest_receive_traffic(struct qede_dev *edev)
1278 {
1279         u16 hw_comp_cons, sw_comp_cons, sw_rx_index, len;
1280         struct eth_fast_path_rx_reg_cqe *fp_cqe;
1281         struct qede_rx_queue *rxq = NULL;
1282         struct sw_rx_data *sw_rx_data;
1283         union eth_rx_cqe *cqe;
1284         int i, rc = 0;
1285         u8 *data_ptr;
1286
1287         for_each_queue(i) {
1288                 if (edev->fp_array[i].type & QEDE_FASTPATH_RX) {
1289                         rxq = edev->fp_array[i].rxq;
1290                         break;
1291                 }
1292         }
1293
1294         if (!rxq) {
1295                 DP_NOTICE(edev, "Rx path is not available\n");
1296                 return -1;
1297         }
1298
1299         /* The packet is expected to receive on rx-queue 0 even though RSS is
1300          * enabled. This is because the queue 0 is configured as the default
1301          * queue and that the loopback traffic is not IP.
1302          */
1303         for (i = 0; i < QEDE_SELFTEST_POLL_COUNT; i++) {
1304                 if (!qede_has_rx_work(rxq)) {
1305                         usleep_range(100, 200);
1306                         continue;
1307                 }
1308
1309                 hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
1310                 sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
1311
1312                 /* Memory barrier to prevent the CPU from doing speculative
1313                  * reads of CQE/BD before reading hw_comp_cons. If the CQE is
1314                  * read before it is written by FW, then FW writes CQE and SB,
1315                  * and then the CPU reads the hw_comp_cons, it will use an old
1316                  * CQE.
1317                  */
1318                 rmb();
1319
1320                 /* Get the CQE from the completion ring */
1321                 cqe = (union eth_rx_cqe *)qed_chain_consume(&rxq->rx_comp_ring);
1322
1323                 /* Get the data from the SW ring */
1324                 sw_rx_index = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
1325                 sw_rx_data = &rxq->sw_rx_ring[sw_rx_index];
1326                 fp_cqe = &cqe->fast_path_regular;
1327                 len =  le16_to_cpu(fp_cqe->len_on_first_bd);
1328                 data_ptr = (u8 *)(page_address(sw_rx_data->data) +
1329                                   fp_cqe->placement_offset +
1330                                   sw_rx_data->page_offset);
1331                 if (ether_addr_equal(data_ptr,  edev->ndev->dev_addr) &&
1332                     ether_addr_equal(data_ptr + ETH_ALEN,
1333                                      edev->ndev->dev_addr)) {
1334                         for (i = ETH_HLEN; i < len; i++)
1335                                 if (data_ptr[i] != (unsigned char)(i & 0xff)) {
1336                                         rc = -1;
1337                                         break;
1338                                 }
1339
1340                         qede_recycle_rx_bd_ring(rxq, edev, 1);
1341                         qed_chain_recycle_consumed(&rxq->rx_comp_ring);
1342                         break;
1343                 }
1344
1345                 DP_INFO(edev, "Not the transmitted packet\n");
1346                 qede_recycle_rx_bd_ring(rxq, edev, 1);
1347                 qed_chain_recycle_consumed(&rxq->rx_comp_ring);
1348         }
1349
1350         if (i == QEDE_SELFTEST_POLL_COUNT) {
1351                 DP_NOTICE(edev, "Failed to receive the traffic\n");
1352                 return -1;
1353         }
1354
1355         qede_update_rx_prod(edev, rxq);
1356
1357         return rc;
1358 }
1359
1360 static int qede_selftest_run_loopback(struct qede_dev *edev, u32 loopback_mode)
1361 {
1362         struct qed_link_params link_params;
1363         struct sk_buff *skb = NULL;
1364         int rc = 0, i;
1365         u32 pkt_size;
1366         u8 *packet;
1367
1368         if (!netif_running(edev->ndev)) {
1369                 DP_NOTICE(edev, "Interface is down\n");
1370                 return -EINVAL;
1371         }
1372
1373         qede_netif_stop(edev);
1374
1375         /* Bring up the link in Loopback mode */
1376         memset(&link_params, 0, sizeof(link_params));
1377         link_params.link_up = true;
1378         link_params.override_flags = QED_LINK_OVERRIDE_LOOPBACK_MODE;
1379         link_params.loopback_mode = loopback_mode;
1380         edev->ops->common->set_link(edev->cdev, &link_params);
1381
1382         /* Wait for loopback configuration to apply */
1383         msleep_interruptible(500);
1384
1385         /* prepare the loopback packet */
1386         pkt_size = edev->ndev->mtu + ETH_HLEN;
1387
1388         skb = netdev_alloc_skb(edev->ndev, pkt_size);
1389         if (!skb) {
1390                 DP_INFO(edev, "Can't allocate skb\n");
1391                 rc = -ENOMEM;
1392                 goto test_loopback_exit;
1393         }
1394         packet = skb_put(skb, pkt_size);
1395         ether_addr_copy(packet, edev->ndev->dev_addr);
1396         ether_addr_copy(packet + ETH_ALEN, edev->ndev->dev_addr);
1397         memset(packet + (2 * ETH_ALEN), 0x77, (ETH_HLEN - (2 * ETH_ALEN)));
1398         for (i = ETH_HLEN; i < pkt_size; i++)
1399                 packet[i] = (unsigned char)(i & 0xff);
1400
1401         rc = qede_selftest_transmit_traffic(edev, skb);
1402         if (rc)
1403                 goto test_loopback_exit;
1404
1405         rc = qede_selftest_receive_traffic(edev);
1406         if (rc)
1407                 goto test_loopback_exit;
1408
1409         DP_VERBOSE(edev, NETIF_MSG_RX_STATUS, "Loopback test successful\n");
1410
1411 test_loopback_exit:
1412         dev_kfree_skb(skb);
1413
1414         /* Bring up the link in Normal mode */
1415         memset(&link_params, 0, sizeof(link_params));
1416         link_params.link_up = true;
1417         link_params.override_flags = QED_LINK_OVERRIDE_LOOPBACK_MODE;
1418         link_params.loopback_mode = QED_LINK_LOOPBACK_NONE;
1419         edev->ops->common->set_link(edev->cdev, &link_params);
1420
1421         /* Wait for loopback configuration to apply */
1422         msleep_interruptible(500);
1423
1424         qede_netif_start(edev);
1425
1426         return rc;
1427 }
1428
1429 static void qede_self_test(struct net_device *dev,
1430                            struct ethtool_test *etest, u64 *buf)
1431 {
1432         struct qede_dev *edev = netdev_priv(dev);
1433
1434         DP_VERBOSE(edev, QED_MSG_DEBUG,
1435                    "Self-test command parameters: offline = %d, external_lb = %d\n",
1436                    (etest->flags & ETH_TEST_FL_OFFLINE),
1437                    (etest->flags & ETH_TEST_FL_EXTERNAL_LB) >> 2);
1438
1439         memset(buf, 0, sizeof(u64) * QEDE_ETHTOOL_TEST_MAX);
1440
1441         if (etest->flags & ETH_TEST_FL_OFFLINE) {
1442                 if (qede_selftest_run_loopback(edev,
1443                                                QED_LINK_LOOPBACK_INT_PHY)) {
1444                         buf[QEDE_ETHTOOL_INT_LOOPBACK] = 1;
1445                         etest->flags |= ETH_TEST_FL_FAILED;
1446                 }
1447         }
1448
1449         if (edev->ops->common->selftest->selftest_interrupt(edev->cdev)) {
1450                 buf[QEDE_ETHTOOL_INTERRUPT_TEST] = 1;
1451                 etest->flags |= ETH_TEST_FL_FAILED;
1452         }
1453
1454         if (edev->ops->common->selftest->selftest_memory(edev->cdev)) {
1455                 buf[QEDE_ETHTOOL_MEMORY_TEST] = 1;
1456                 etest->flags |= ETH_TEST_FL_FAILED;
1457         }
1458
1459         if (edev->ops->common->selftest->selftest_register(edev->cdev)) {
1460                 buf[QEDE_ETHTOOL_REGISTER_TEST] = 1;
1461                 etest->flags |= ETH_TEST_FL_FAILED;
1462         }
1463
1464         if (edev->ops->common->selftest->selftest_clock(edev->cdev)) {
1465                 buf[QEDE_ETHTOOL_CLOCK_TEST] = 1;
1466                 etest->flags |= ETH_TEST_FL_FAILED;
1467         }
1468
1469         if (edev->ops->common->selftest->selftest_nvram(edev->cdev)) {
1470                 buf[QEDE_ETHTOOL_NVRAM_TEST] = 1;
1471                 etest->flags |= ETH_TEST_FL_FAILED;
1472         }
1473 }
1474
1475 static int qede_set_tunable(struct net_device *dev,
1476                             const struct ethtool_tunable *tuna,
1477                             const void *data)
1478 {
1479         struct qede_dev *edev = netdev_priv(dev);
1480         u32 val;
1481
1482         switch (tuna->id) {
1483         case ETHTOOL_RX_COPYBREAK:
1484                 val = *(u32 *)data;
1485                 if (val < QEDE_MIN_PKT_LEN || val > QEDE_RX_HDR_SIZE) {
1486                         DP_VERBOSE(edev, QED_MSG_DEBUG,
1487                                    "Invalid rx copy break value, range is [%u, %u]",
1488                                    QEDE_MIN_PKT_LEN, QEDE_RX_HDR_SIZE);
1489                         return -EINVAL;
1490                 }
1491
1492                 edev->rx_copybreak = *(u32 *)data;
1493                 break;
1494         default:
1495                 return -EOPNOTSUPP;
1496         }
1497
1498         return 0;
1499 }
1500
1501 static int qede_get_tunable(struct net_device *dev,
1502                             const struct ethtool_tunable *tuna, void *data)
1503 {
1504         struct qede_dev *edev = netdev_priv(dev);
1505
1506         switch (tuna->id) {
1507         case ETHTOOL_RX_COPYBREAK:
1508                 *(u32 *)data = edev->rx_copybreak;
1509                 break;
1510         default:
1511                 return -EOPNOTSUPP;
1512         }
1513
1514         return 0;
1515 }
1516
1517 static const struct ethtool_ops qede_ethtool_ops = {
1518         .get_link_ksettings = qede_get_link_ksettings,
1519         .set_link_ksettings = qede_set_link_ksettings,
1520         .get_drvinfo = qede_get_drvinfo,
1521         .get_regs_len = qede_get_regs_len,
1522         .get_regs = qede_get_regs,
1523         .get_wol = qede_get_wol,
1524         .set_wol = qede_set_wol,
1525         .get_msglevel = qede_get_msglevel,
1526         .set_msglevel = qede_set_msglevel,
1527         .nway_reset = qede_nway_reset,
1528         .get_link = qede_get_link,
1529         .get_coalesce = qede_get_coalesce,
1530         .set_coalesce = qede_set_coalesce,
1531         .get_ringparam = qede_get_ringparam,
1532         .set_ringparam = qede_set_ringparam,
1533         .get_pauseparam = qede_get_pauseparam,
1534         .set_pauseparam = qede_set_pauseparam,
1535         .get_strings = qede_get_strings,
1536         .set_phys_id = qede_set_phys_id,
1537         .get_ethtool_stats = qede_get_ethtool_stats,
1538         .get_priv_flags = qede_get_priv_flags,
1539         .get_sset_count = qede_get_sset_count,
1540         .get_rxnfc = qede_get_rxnfc,
1541         .set_rxnfc = qede_set_rxnfc,
1542         .get_rxfh_indir_size = qede_get_rxfh_indir_size,
1543         .get_rxfh_key_size = qede_get_rxfh_key_size,
1544         .get_rxfh = qede_get_rxfh,
1545         .set_rxfh = qede_set_rxfh,
1546         .get_channels = qede_get_channels,
1547         .set_channels = qede_set_channels,
1548         .self_test = qede_self_test,
1549         .get_tunable = qede_get_tunable,
1550         .set_tunable = qede_set_tunable,
1551 };
1552
1553 static const struct ethtool_ops qede_vf_ethtool_ops = {
1554         .get_link_ksettings = qede_get_link_ksettings,
1555         .get_drvinfo = qede_get_drvinfo,
1556         .get_msglevel = qede_get_msglevel,
1557         .set_msglevel = qede_set_msglevel,
1558         .get_link = qede_get_link,
1559         .get_ringparam = qede_get_ringparam,
1560         .set_ringparam = qede_set_ringparam,
1561         .get_strings = qede_get_strings,
1562         .get_ethtool_stats = qede_get_ethtool_stats,
1563         .get_priv_flags = qede_get_priv_flags,
1564         .get_sset_count = qede_get_sset_count,
1565         .get_rxnfc = qede_get_rxnfc,
1566         .set_rxnfc = qede_set_rxnfc,
1567         .get_rxfh_indir_size = qede_get_rxfh_indir_size,
1568         .get_rxfh_key_size = qede_get_rxfh_key_size,
1569         .get_rxfh = qede_get_rxfh,
1570         .set_rxfh = qede_set_rxfh,
1571         .get_channels = qede_get_channels,
1572         .set_channels = qede_set_channels,
1573         .get_tunable = qede_get_tunable,
1574         .set_tunable = qede_set_tunable,
1575 };
1576
1577 void qede_set_ethtool_ops(struct net_device *dev)
1578 {
1579         struct qede_dev *edev = netdev_priv(dev);
1580
1581         if (IS_VF(edev))
1582                 dev->ethtool_ops = &qede_vf_ethtool_ops;
1583         else
1584                 dev->ethtool_ops = &qede_ethtool_ops;
1585 }