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