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