OSDN Git Service

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