GNU Linux-libre 4.9.283-gnu1
[releases.git] / drivers / net / ethernet / cavium / thunder / nicvf_ethtool.c
1 /*
2  * Copyright (C) 2015 Cavium, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License
6  * as published by the Free Software Foundation.
7  */
8
9 /* ETHTOOL Support for VNIC_VF Device*/
10
11 #include <linux/pci.h>
12
13 #include "nic_reg.h"
14 #include "nic.h"
15 #include "nicvf_queues.h"
16 #include "q_struct.h"
17 #include "thunder_bgx.h"
18
19 #define DRV_NAME        "thunder-nicvf"
20 #define DRV_VERSION     "1.0"
21
22 struct nicvf_stat {
23         char name[ETH_GSTRING_LEN];
24         unsigned int index;
25 };
26
27 #define NICVF_HW_STAT(stat) { \
28         .name = #stat, \
29         .index = offsetof(struct nicvf_hw_stats, stat) / sizeof(u64), \
30 }
31
32 #define NICVF_DRV_STAT(stat) { \
33         .name = #stat, \
34         .index = offsetof(struct nicvf_drv_stats, stat) / sizeof(u64), \
35 }
36
37 static const struct nicvf_stat nicvf_hw_stats[] = {
38         NICVF_HW_STAT(rx_bytes),
39         NICVF_HW_STAT(rx_frames),
40         NICVF_HW_STAT(rx_ucast_frames),
41         NICVF_HW_STAT(rx_bcast_frames),
42         NICVF_HW_STAT(rx_mcast_frames),
43         NICVF_HW_STAT(rx_drops),
44         NICVF_HW_STAT(rx_drop_red),
45         NICVF_HW_STAT(rx_drop_red_bytes),
46         NICVF_HW_STAT(rx_drop_overrun),
47         NICVF_HW_STAT(rx_drop_overrun_bytes),
48         NICVF_HW_STAT(rx_drop_bcast),
49         NICVF_HW_STAT(rx_drop_mcast),
50         NICVF_HW_STAT(rx_drop_l3_bcast),
51         NICVF_HW_STAT(rx_drop_l3_mcast),
52         NICVF_HW_STAT(rx_fcs_errors),
53         NICVF_HW_STAT(rx_l2_errors),
54         NICVF_HW_STAT(tx_bytes),
55         NICVF_HW_STAT(tx_frames),
56         NICVF_HW_STAT(tx_ucast_frames),
57         NICVF_HW_STAT(tx_bcast_frames),
58         NICVF_HW_STAT(tx_mcast_frames),
59         NICVF_HW_STAT(tx_drops),
60 };
61
62 static const struct nicvf_stat nicvf_drv_stats[] = {
63         NICVF_DRV_STAT(rx_bgx_truncated_pkts),
64         NICVF_DRV_STAT(rx_jabber_errs),
65         NICVF_DRV_STAT(rx_fcs_errs),
66         NICVF_DRV_STAT(rx_bgx_errs),
67         NICVF_DRV_STAT(rx_prel2_errs),
68         NICVF_DRV_STAT(rx_l2_hdr_malformed),
69         NICVF_DRV_STAT(rx_oversize),
70         NICVF_DRV_STAT(rx_undersize),
71         NICVF_DRV_STAT(rx_l2_len_mismatch),
72         NICVF_DRV_STAT(rx_l2_pclp),
73         NICVF_DRV_STAT(rx_ip_ver_errs),
74         NICVF_DRV_STAT(rx_ip_csum_errs),
75         NICVF_DRV_STAT(rx_ip_hdr_malformed),
76         NICVF_DRV_STAT(rx_ip_payload_malformed),
77         NICVF_DRV_STAT(rx_ip_ttl_errs),
78         NICVF_DRV_STAT(rx_l3_pclp),
79         NICVF_DRV_STAT(rx_l4_malformed),
80         NICVF_DRV_STAT(rx_l4_csum_errs),
81         NICVF_DRV_STAT(rx_udp_len_errs),
82         NICVF_DRV_STAT(rx_l4_port_errs),
83         NICVF_DRV_STAT(rx_tcp_flag_errs),
84         NICVF_DRV_STAT(rx_tcp_offset_errs),
85         NICVF_DRV_STAT(rx_l4_pclp),
86         NICVF_DRV_STAT(rx_truncated_pkts),
87
88         NICVF_DRV_STAT(tx_desc_fault),
89         NICVF_DRV_STAT(tx_hdr_cons_err),
90         NICVF_DRV_STAT(tx_subdesc_err),
91         NICVF_DRV_STAT(tx_max_size_exceeded),
92         NICVF_DRV_STAT(tx_imm_size_oflow),
93         NICVF_DRV_STAT(tx_data_seq_err),
94         NICVF_DRV_STAT(tx_mem_seq_err),
95         NICVF_DRV_STAT(tx_lock_viol),
96         NICVF_DRV_STAT(tx_data_fault),
97         NICVF_DRV_STAT(tx_tstmp_conflict),
98         NICVF_DRV_STAT(tx_tstmp_timeout),
99         NICVF_DRV_STAT(tx_mem_fault),
100         NICVF_DRV_STAT(tx_csum_overlap),
101         NICVF_DRV_STAT(tx_csum_overflow),
102
103         NICVF_DRV_STAT(rcv_buffer_alloc_failures),
104         NICVF_DRV_STAT(tx_tso),
105         NICVF_DRV_STAT(tx_timeout),
106         NICVF_DRV_STAT(txq_stop),
107         NICVF_DRV_STAT(txq_wake),
108 };
109
110 static const struct nicvf_stat nicvf_queue_stats[] = {
111         { "bytes", 0 },
112         { "frames", 1 },
113 };
114
115 static const unsigned int nicvf_n_hw_stats = ARRAY_SIZE(nicvf_hw_stats);
116 static const unsigned int nicvf_n_drv_stats = ARRAY_SIZE(nicvf_drv_stats);
117 static const unsigned int nicvf_n_queue_stats = ARRAY_SIZE(nicvf_queue_stats);
118
119 static int nicvf_get_settings(struct net_device *netdev,
120                               struct ethtool_cmd *cmd)
121 {
122         struct nicvf *nic = netdev_priv(netdev);
123
124         cmd->supported = 0;
125         cmd->transceiver = XCVR_EXTERNAL;
126
127         if (!nic->link_up) {
128                 cmd->duplex = DUPLEX_UNKNOWN;
129                 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
130                 return 0;
131         }
132
133         if (nic->speed <= 1000) {
134                 cmd->port = PORT_MII;
135                 cmd->autoneg = AUTONEG_ENABLE;
136         } else {
137                 cmd->port = PORT_FIBRE;
138                 cmd->autoneg = AUTONEG_DISABLE;
139         }
140         cmd->duplex = nic->duplex;
141         ethtool_cmd_speed_set(cmd, nic->speed);
142
143         return 0;
144 }
145
146 static u32 nicvf_get_link(struct net_device *netdev)
147 {
148         struct nicvf *nic = netdev_priv(netdev);
149
150         return nic->link_up;
151 }
152
153 static void nicvf_get_drvinfo(struct net_device *netdev,
154                               struct ethtool_drvinfo *info)
155 {
156         struct nicvf *nic = netdev_priv(netdev);
157
158         strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
159         strlcpy(info->version, DRV_VERSION, sizeof(info->version));
160         strlcpy(info->bus_info, pci_name(nic->pdev), sizeof(info->bus_info));
161 }
162
163 static u32 nicvf_get_msglevel(struct net_device *netdev)
164 {
165         struct nicvf *nic = netdev_priv(netdev);
166
167         return nic->msg_enable;
168 }
169
170 static void nicvf_set_msglevel(struct net_device *netdev, u32 lvl)
171 {
172         struct nicvf *nic = netdev_priv(netdev);
173
174         nic->msg_enable = lvl;
175 }
176
177 static void nicvf_get_qset_strings(struct nicvf *nic, u8 **data, int qset)
178 {
179         int stats, qidx;
180         int start_qidx = qset * MAX_RCV_QUEUES_PER_QS;
181
182         for (qidx = 0; qidx < nic->qs->rq_cnt; qidx++) {
183                 for (stats = 0; stats < nicvf_n_queue_stats; stats++) {
184                         sprintf(*data, "rxq%d: %s", qidx + start_qidx,
185                                 nicvf_queue_stats[stats].name);
186                         *data += ETH_GSTRING_LEN;
187                 }
188         }
189
190         for (qidx = 0; qidx < nic->qs->sq_cnt; qidx++) {
191                 for (stats = 0; stats < nicvf_n_queue_stats; stats++) {
192                         sprintf(*data, "txq%d: %s", qidx + start_qidx,
193                                 nicvf_queue_stats[stats].name);
194                         *data += ETH_GSTRING_LEN;
195                 }
196         }
197 }
198
199 static void nicvf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
200 {
201         struct nicvf *nic = netdev_priv(netdev);
202         int stats;
203         int sqs;
204
205         if (sset != ETH_SS_STATS)
206                 return;
207
208         for (stats = 0; stats < nicvf_n_hw_stats; stats++) {
209                 memcpy(data, nicvf_hw_stats[stats].name, ETH_GSTRING_LEN);
210                 data += ETH_GSTRING_LEN;
211         }
212
213         for (stats = 0; stats < nicvf_n_drv_stats; stats++) {
214                 memcpy(data, nicvf_drv_stats[stats].name, ETH_GSTRING_LEN);
215                 data += ETH_GSTRING_LEN;
216         }
217
218         nicvf_get_qset_strings(nic, &data, 0);
219
220         for (sqs = 0; sqs < nic->sqs_count; sqs++) {
221                 if (!nic->snicvf[sqs])
222                         continue;
223                 nicvf_get_qset_strings(nic->snicvf[sqs], &data, sqs + 1);
224         }
225
226         for (stats = 0; stats < BGX_RX_STATS_COUNT; stats++) {
227                 sprintf(data, "bgx_rxstat%d: ", stats);
228                 data += ETH_GSTRING_LEN;
229         }
230
231         for (stats = 0; stats < BGX_TX_STATS_COUNT; stats++) {
232                 sprintf(data, "bgx_txstat%d: ", stats);
233                 data += ETH_GSTRING_LEN;
234         }
235 }
236
237 static int nicvf_get_sset_count(struct net_device *netdev, int sset)
238 {
239         struct nicvf *nic = netdev_priv(netdev);
240         int qstats_count;
241         int sqs;
242
243         if (sset != ETH_SS_STATS)
244                 return -EINVAL;
245
246         qstats_count = nicvf_n_queue_stats *
247                        (nic->qs->rq_cnt + nic->qs->sq_cnt);
248         for (sqs = 0; sqs < nic->sqs_count; sqs++) {
249                 struct nicvf *snic;
250
251                 snic = nic->snicvf[sqs];
252                 if (!snic)
253                         continue;
254                 qstats_count += nicvf_n_queue_stats *
255                                 (snic->qs->rq_cnt + snic->qs->sq_cnt);
256         }
257
258         return nicvf_n_hw_stats + nicvf_n_drv_stats +
259                 qstats_count +
260                 BGX_RX_STATS_COUNT + BGX_TX_STATS_COUNT;
261 }
262
263 static void nicvf_get_qset_stats(struct nicvf *nic,
264                                  struct ethtool_stats *stats, u64 **data)
265 {
266         int stat, qidx;
267
268         if (!nic)
269                 return;
270
271         for (qidx = 0; qidx < nic->qs->rq_cnt; qidx++) {
272                 nicvf_update_rq_stats(nic, qidx);
273                 for (stat = 0; stat < nicvf_n_queue_stats; stat++)
274                         *((*data)++) = ((u64 *)&nic->qs->rq[qidx].stats)
275                                         [nicvf_queue_stats[stat].index];
276         }
277
278         for (qidx = 0; qidx < nic->qs->sq_cnt; qidx++) {
279                 nicvf_update_sq_stats(nic, qidx);
280                 for (stat = 0; stat < nicvf_n_queue_stats; stat++)
281                         *((*data)++) = ((u64 *)&nic->qs->sq[qidx].stats)
282                                         [nicvf_queue_stats[stat].index];
283         }
284 }
285
286 static void nicvf_get_ethtool_stats(struct net_device *netdev,
287                                     struct ethtool_stats *stats, u64 *data)
288 {
289         struct nicvf *nic = netdev_priv(netdev);
290         int stat, tmp_stats;
291         int sqs, cpu;
292
293         nicvf_update_stats(nic);
294
295         /* Update LMAC stats */
296         nicvf_update_lmac_stats(nic);
297
298         for (stat = 0; stat < nicvf_n_hw_stats; stat++)
299                 *(data++) = ((u64 *)&nic->hw_stats)
300                                 [nicvf_hw_stats[stat].index];
301         for (stat = 0; stat < nicvf_n_drv_stats; stat++) {
302                 tmp_stats = 0;
303                 for_each_possible_cpu(cpu)
304                         tmp_stats += ((u64 *)per_cpu_ptr(nic->drv_stats, cpu))
305                                      [nicvf_drv_stats[stat].index];
306                 *(data++) = tmp_stats;
307         }
308
309         nicvf_get_qset_stats(nic, stats, &data);
310
311         for (sqs = 0; sqs < nic->sqs_count; sqs++) {
312                 if (!nic->snicvf[sqs])
313                         continue;
314                 nicvf_get_qset_stats(nic->snicvf[sqs], stats, &data);
315         }
316
317         for (stat = 0; stat < BGX_RX_STATS_COUNT; stat++)
318                 *(data++) = nic->bgx_stats.rx_stats[stat];
319         for (stat = 0; stat < BGX_TX_STATS_COUNT; stat++)
320                 *(data++) = nic->bgx_stats.tx_stats[stat];
321 }
322
323 static int nicvf_get_regs_len(struct net_device *dev)
324 {
325         return sizeof(u64) * NIC_VF_REG_COUNT;
326 }
327
328 static void nicvf_get_regs(struct net_device *dev,
329                            struct ethtool_regs *regs, void *reg)
330 {
331         struct nicvf *nic = netdev_priv(dev);
332         u64 *p = (u64 *)reg;
333         u64 reg_offset;
334         int mbox, key, stat, q;
335         int i = 0;
336
337         regs->version = 0;
338         memset(p, 0, NIC_VF_REG_COUNT);
339
340         p[i++] = nicvf_reg_read(nic, NIC_VNIC_CFG);
341         /* Mailbox registers */
342         for (mbox = 0; mbox < NIC_PF_VF_MAILBOX_SIZE; mbox++)
343                 p[i++] = nicvf_reg_read(nic,
344                                         NIC_VF_PF_MAILBOX_0_1 | (mbox << 3));
345
346         p[i++] = nicvf_reg_read(nic, NIC_VF_INT);
347         p[i++] = nicvf_reg_read(nic, NIC_VF_INT_W1S);
348         p[i++] = nicvf_reg_read(nic, NIC_VF_ENA_W1C);
349         p[i++] = nicvf_reg_read(nic, NIC_VF_ENA_W1S);
350         p[i++] = nicvf_reg_read(nic, NIC_VNIC_RSS_CFG);
351
352         for (key = 0; key < RSS_HASH_KEY_SIZE; key++)
353                 p[i++] = nicvf_reg_read(nic, NIC_VNIC_RSS_KEY_0_4 | (key << 3));
354
355         /* Tx/Rx statistics */
356         for (stat = 0; stat < TX_STATS_ENUM_LAST; stat++)
357                 p[i++] = nicvf_reg_read(nic,
358                                         NIC_VNIC_TX_STAT_0_4 | (stat << 3));
359
360         for (i = 0; i < RX_STATS_ENUM_LAST; i++)
361                 p[i++] = nicvf_reg_read(nic,
362                                         NIC_VNIC_RX_STAT_0_13 | (stat << 3));
363
364         p[i++] = nicvf_reg_read(nic, NIC_QSET_RQ_GEN_CFG);
365
366         /* All completion queue's registers */
367         for (q = 0; q < MAX_CMP_QUEUES_PER_QS; q++) {
368                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_CFG, q);
369                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_CFG2, q);
370                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_THRESH, q);
371                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_BASE, q);
372                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD, q);
373                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_TAIL, q);
374                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_DOOR, q);
375                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS, q);
376                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS2, q);
377                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_DEBUG, q);
378         }
379
380         /* All receive queue's registers */
381         for (q = 0; q < MAX_RCV_QUEUES_PER_QS; q++) {
382                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RQ_0_7_CFG, q);
383                 p[i++] = nicvf_queue_reg_read(nic,
384                                                   NIC_QSET_RQ_0_7_STAT_0_1, q);
385                 reg_offset = NIC_QSET_RQ_0_7_STAT_0_1 | (1 << 3);
386                 p[i++] = nicvf_queue_reg_read(nic, reg_offset, q);
387         }
388
389         for (q = 0; q < MAX_SND_QUEUES_PER_QS; q++) {
390                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, q);
391                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_THRESH, q);
392                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_BASE, q);
393                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_HEAD, q);
394                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_TAIL, q);
395                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_DOOR, q);
396                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_STATUS, q);
397                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_DEBUG, q);
398                 /* Padding, was NIC_QSET_SQ_0_7_CNM_CHG, which
399                  * produces bus errors when read
400                  */
401                 p[i++] = 0;
402                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_STAT_0_1, q);
403                 reg_offset = NIC_QSET_SQ_0_7_STAT_0_1 | (1 << 3);
404                 p[i++] = nicvf_queue_reg_read(nic, reg_offset, q);
405         }
406
407         for (q = 0; q < MAX_RCV_BUF_DESC_RINGS_PER_QS; q++) {
408                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_CFG, q);
409                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_THRESH, q);
410                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_BASE, q);
411                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_HEAD, q);
412                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_TAIL, q);
413                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_DOOR, q);
414                 p[i++] = nicvf_queue_reg_read(nic,
415                                               NIC_QSET_RBDR_0_1_STATUS0, q);
416                 p[i++] = nicvf_queue_reg_read(nic,
417                                               NIC_QSET_RBDR_0_1_STATUS1, q);
418                 reg_offset = NIC_QSET_RBDR_0_1_PREFETCH_STATUS;
419                 p[i++] = nicvf_queue_reg_read(nic, reg_offset, q);
420         }
421 }
422
423 static int nicvf_get_coalesce(struct net_device *netdev,
424                               struct ethtool_coalesce *cmd)
425 {
426         struct nicvf *nic = netdev_priv(netdev);
427
428         cmd->rx_coalesce_usecs = nic->cq_coalesce_usecs;
429         return 0;
430 }
431
432 static void nicvf_get_ringparam(struct net_device *netdev,
433                                 struct ethtool_ringparam *ring)
434 {
435         struct nicvf *nic = netdev_priv(netdev);
436         struct queue_set *qs = nic->qs;
437
438         ring->rx_max_pending = MAX_RCV_BUF_COUNT;
439         ring->rx_pending = qs->rbdr_len;
440         ring->tx_max_pending = MAX_SND_QUEUE_LEN;
441         ring->tx_pending = qs->sq_len;
442 }
443
444 static int nicvf_get_rss_hash_opts(struct nicvf *nic,
445                                    struct ethtool_rxnfc *info)
446 {
447         info->data = 0;
448
449         switch (info->flow_type) {
450         case TCP_V4_FLOW:
451         case TCP_V6_FLOW:
452         case UDP_V4_FLOW:
453         case UDP_V6_FLOW:
454         case SCTP_V4_FLOW:
455         case SCTP_V6_FLOW:
456                 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
457         case IPV4_FLOW:
458         case IPV6_FLOW:
459                 info->data |= RXH_IP_SRC | RXH_IP_DST;
460                 break;
461         default:
462                 return -EINVAL;
463         }
464
465         return 0;
466 }
467
468 static int nicvf_get_rxnfc(struct net_device *dev,
469                            struct ethtool_rxnfc *info, u32 *rules)
470 {
471         struct nicvf *nic = netdev_priv(dev);
472         int ret = -EOPNOTSUPP;
473
474         switch (info->cmd) {
475         case ETHTOOL_GRXRINGS:
476                 info->data = nic->rx_queues;
477                 ret = 0;
478                 break;
479         case ETHTOOL_GRXFH:
480                 return nicvf_get_rss_hash_opts(nic, info);
481         default:
482                 break;
483         }
484         return ret;
485 }
486
487 static int nicvf_set_rss_hash_opts(struct nicvf *nic,
488                                    struct ethtool_rxnfc *info)
489 {
490         struct nicvf_rss_info *rss = &nic->rss_info;
491         u64 rss_cfg = nicvf_reg_read(nic, NIC_VNIC_RSS_CFG);
492
493         if (!rss->enable)
494                 netdev_err(nic->netdev,
495                            "RSS is disabled, hash cannot be set\n");
496
497         netdev_info(nic->netdev, "Set RSS flow type = %d, data = %lld\n",
498                     info->flow_type, info->data);
499
500         if (!(info->data & RXH_IP_SRC) || !(info->data & RXH_IP_DST))
501                 return -EINVAL;
502
503         switch (info->flow_type) {
504         case TCP_V4_FLOW:
505         case TCP_V6_FLOW:
506                 switch (info->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
507                 case 0:
508                         rss_cfg &= ~(1ULL << RSS_HASH_TCP);
509                         break;
510                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
511                         rss_cfg |= (1ULL << RSS_HASH_TCP);
512                         break;
513                 default:
514                         return -EINVAL;
515                 }
516                 break;
517         case UDP_V4_FLOW:
518         case UDP_V6_FLOW:
519                 switch (info->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
520                 case 0:
521                         rss_cfg &= ~(1ULL << RSS_HASH_UDP);
522                         break;
523                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
524                         rss_cfg |= (1ULL << RSS_HASH_UDP);
525                         break;
526                 default:
527                         return -EINVAL;
528                 }
529                 break;
530         case SCTP_V4_FLOW:
531         case SCTP_V6_FLOW:
532                 switch (info->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
533                 case 0:
534                         rss_cfg &= ~(1ULL << RSS_HASH_L4ETC);
535                         break;
536                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
537                         rss_cfg |= (1ULL << RSS_HASH_L4ETC);
538                         break;
539                 default:
540                         return -EINVAL;
541                 }
542                 break;
543         case IPV4_FLOW:
544         case IPV6_FLOW:
545                 rss_cfg = RSS_HASH_IP;
546                 break;
547         default:
548                 return -EINVAL;
549         }
550
551         nicvf_reg_write(nic, NIC_VNIC_RSS_CFG, rss_cfg);
552         return 0;
553 }
554
555 static int nicvf_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
556 {
557         struct nicvf *nic = netdev_priv(dev);
558
559         switch (info->cmd) {
560         case ETHTOOL_SRXFH:
561                 return nicvf_set_rss_hash_opts(nic, info);
562         default:
563                 break;
564         }
565         return -EOPNOTSUPP;
566 }
567
568 static u32 nicvf_get_rxfh_key_size(struct net_device *netdev)
569 {
570         return RSS_HASH_KEY_SIZE * sizeof(u64);
571 }
572
573 static u32 nicvf_get_rxfh_indir_size(struct net_device *dev)
574 {
575         struct nicvf *nic = netdev_priv(dev);
576
577         return nic->rss_info.rss_size;
578 }
579
580 static int nicvf_get_rxfh(struct net_device *dev, u32 *indir, u8 *hkey,
581                           u8 *hfunc)
582 {
583         struct nicvf *nic = netdev_priv(dev);
584         struct nicvf_rss_info *rss = &nic->rss_info;
585         int idx;
586
587         if (indir) {
588                 for (idx = 0; idx < rss->rss_size; idx++)
589                         indir[idx] = rss->ind_tbl[idx];
590         }
591
592         if (hkey)
593                 memcpy(hkey, rss->key, RSS_HASH_KEY_SIZE * sizeof(u64));
594
595         if (hfunc)
596                 *hfunc = ETH_RSS_HASH_TOP;
597
598         return 0;
599 }
600
601 static int nicvf_set_rxfh(struct net_device *dev, const u32 *indir,
602                           const u8 *hkey, u8 hfunc)
603 {
604         struct nicvf *nic = netdev_priv(dev);
605         struct nicvf_rss_info *rss = &nic->rss_info;
606         int idx;
607
608         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
609                 return -EOPNOTSUPP;
610
611         if (!rss->enable) {
612                 netdev_err(nic->netdev,
613                            "RSS is disabled, cannot change settings\n");
614                 return -EIO;
615         }
616
617         if (indir) {
618                 for (idx = 0; idx < rss->rss_size; idx++)
619                         rss->ind_tbl[idx] = indir[idx];
620         }
621
622         if (hkey) {
623                 memcpy(rss->key, hkey, RSS_HASH_KEY_SIZE * sizeof(u64));
624                 nicvf_set_rss_key(nic);
625         }
626
627         nicvf_config_rss(nic);
628         return 0;
629 }
630
631 /* Get no of queues device supports and current queue count */
632 static void nicvf_get_channels(struct net_device *dev,
633                                struct ethtool_channels *channel)
634 {
635         struct nicvf *nic = netdev_priv(dev);
636
637         memset(channel, 0, sizeof(*channel));
638
639         channel->max_rx = nic->max_queues;
640         channel->max_tx = nic->max_queues;
641
642         channel->rx_count = nic->rx_queues;
643         channel->tx_count = nic->tx_queues;
644 }
645
646 /* Set no of Tx, Rx queues to be used */
647 static int nicvf_set_channels(struct net_device *dev,
648                               struct ethtool_channels *channel)
649 {
650         struct nicvf *nic = netdev_priv(dev);
651         int err = 0;
652         bool if_up = netif_running(dev);
653         int cqcount;
654
655         if (!channel->rx_count || !channel->tx_count)
656                 return -EINVAL;
657         if (channel->rx_count > nic->max_queues)
658                 return -EINVAL;
659         if (channel->tx_count > nic->max_queues)
660                 return -EINVAL;
661
662         if (if_up)
663                 nicvf_stop(dev);
664
665         cqcount = max(channel->rx_count, channel->tx_count);
666
667         if (cqcount > MAX_CMP_QUEUES_PER_QS) {
668                 nic->sqs_count = roundup(cqcount, MAX_CMP_QUEUES_PER_QS);
669                 nic->sqs_count = (nic->sqs_count / MAX_CMP_QUEUES_PER_QS) - 1;
670         } else {
671                 nic->sqs_count = 0;
672         }
673
674         nic->qs->rq_cnt = min_t(u32, channel->rx_count, MAX_RCV_QUEUES_PER_QS);
675         nic->qs->sq_cnt = min_t(u32, channel->tx_count, MAX_SND_QUEUES_PER_QS);
676         nic->qs->cq_cnt = max(nic->qs->rq_cnt, nic->qs->sq_cnt);
677
678         nic->rx_queues = channel->rx_count;
679         nic->tx_queues = channel->tx_count;
680         err = nicvf_set_real_num_queues(dev, nic->tx_queues, nic->rx_queues);
681         if (err)
682                 return err;
683
684         if (if_up)
685                 nicvf_open(dev);
686
687         netdev_info(dev, "Setting num Tx rings to %d, Rx rings to %d success\n",
688                     nic->tx_queues, nic->rx_queues);
689
690         return err;
691 }
692
693 static const struct ethtool_ops nicvf_ethtool_ops = {
694         .get_settings           = nicvf_get_settings,
695         .get_link               = nicvf_get_link,
696         .get_drvinfo            = nicvf_get_drvinfo,
697         .get_msglevel           = nicvf_get_msglevel,
698         .set_msglevel           = nicvf_set_msglevel,
699         .get_strings            = nicvf_get_strings,
700         .get_sset_count         = nicvf_get_sset_count,
701         .get_ethtool_stats      = nicvf_get_ethtool_stats,
702         .get_regs_len           = nicvf_get_regs_len,
703         .get_regs               = nicvf_get_regs,
704         .get_coalesce           = nicvf_get_coalesce,
705         .get_ringparam          = nicvf_get_ringparam,
706         .get_rxnfc              = nicvf_get_rxnfc,
707         .set_rxnfc              = nicvf_set_rxnfc,
708         .get_rxfh_key_size      = nicvf_get_rxfh_key_size,
709         .get_rxfh_indir_size    = nicvf_get_rxfh_indir_size,
710         .get_rxfh               = nicvf_get_rxfh,
711         .set_rxfh               = nicvf_set_rxfh,
712         .get_channels           = nicvf_get_channels,
713         .set_channels           = nicvf_set_channels,
714         .get_ts_info            = ethtool_op_get_ts_info,
715 };
716
717 void nicvf_set_ethtool_ops(struct net_device *netdev)
718 {
719         netdev->ethtool_ops = &nicvf_ethtool_ops;
720 }