92ba958e204e1385ea50c5502d3b4df4803cdf97
[muen/linux.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 #include <linux/net_tstamp.h>
13
14 #include "nic_reg.h"
15 #include "nic.h"
16 #include "nicvf_queues.h"
17 #include "q_struct.h"
18 #include "thunder_bgx.h"
19 #include "../common/cavium_ptp.h"
20
21 #define DRV_NAME        "nicvf"
22 #define DRV_VERSION     "1.0"
23
24 struct nicvf_stat {
25         char name[ETH_GSTRING_LEN];
26         unsigned int index;
27 };
28
29 #define NICVF_HW_STAT(stat) { \
30         .name = #stat, \
31         .index = offsetof(struct nicvf_hw_stats, stat) / sizeof(u64), \
32 }
33
34 #define NICVF_DRV_STAT(stat) { \
35         .name = #stat, \
36         .index = offsetof(struct nicvf_drv_stats, stat) / sizeof(u64), \
37 }
38
39 static const struct nicvf_stat nicvf_hw_stats[] = {
40         NICVF_HW_STAT(rx_bytes),
41         NICVF_HW_STAT(rx_frames),
42         NICVF_HW_STAT(rx_ucast_frames),
43         NICVF_HW_STAT(rx_bcast_frames),
44         NICVF_HW_STAT(rx_mcast_frames),
45         NICVF_HW_STAT(rx_drops),
46         NICVF_HW_STAT(rx_drop_red),
47         NICVF_HW_STAT(rx_drop_red_bytes),
48         NICVF_HW_STAT(rx_drop_overrun),
49         NICVF_HW_STAT(rx_drop_overrun_bytes),
50         NICVF_HW_STAT(rx_drop_bcast),
51         NICVF_HW_STAT(rx_drop_mcast),
52         NICVF_HW_STAT(rx_drop_l3_bcast),
53         NICVF_HW_STAT(rx_drop_l3_mcast),
54         NICVF_HW_STAT(rx_fcs_errors),
55         NICVF_HW_STAT(rx_l2_errors),
56         NICVF_HW_STAT(tx_bytes),
57         NICVF_HW_STAT(tx_frames),
58         NICVF_HW_STAT(tx_ucast_frames),
59         NICVF_HW_STAT(tx_bcast_frames),
60         NICVF_HW_STAT(tx_mcast_frames),
61         NICVF_HW_STAT(tx_drops),
62 };
63
64 static const struct nicvf_stat nicvf_drv_stats[] = {
65         NICVF_DRV_STAT(rx_bgx_truncated_pkts),
66         NICVF_DRV_STAT(rx_jabber_errs),
67         NICVF_DRV_STAT(rx_fcs_errs),
68         NICVF_DRV_STAT(rx_bgx_errs),
69         NICVF_DRV_STAT(rx_prel2_errs),
70         NICVF_DRV_STAT(rx_l2_hdr_malformed),
71         NICVF_DRV_STAT(rx_oversize),
72         NICVF_DRV_STAT(rx_undersize),
73         NICVF_DRV_STAT(rx_l2_len_mismatch),
74         NICVF_DRV_STAT(rx_l2_pclp),
75         NICVF_DRV_STAT(rx_ip_ver_errs),
76         NICVF_DRV_STAT(rx_ip_csum_errs),
77         NICVF_DRV_STAT(rx_ip_hdr_malformed),
78         NICVF_DRV_STAT(rx_ip_payload_malformed),
79         NICVF_DRV_STAT(rx_ip_ttl_errs),
80         NICVF_DRV_STAT(rx_l3_pclp),
81         NICVF_DRV_STAT(rx_l4_malformed),
82         NICVF_DRV_STAT(rx_l4_csum_errs),
83         NICVF_DRV_STAT(rx_udp_len_errs),
84         NICVF_DRV_STAT(rx_l4_port_errs),
85         NICVF_DRV_STAT(rx_tcp_flag_errs),
86         NICVF_DRV_STAT(rx_tcp_offset_errs),
87         NICVF_DRV_STAT(rx_l4_pclp),
88         NICVF_DRV_STAT(rx_truncated_pkts),
89
90         NICVF_DRV_STAT(tx_desc_fault),
91         NICVF_DRV_STAT(tx_hdr_cons_err),
92         NICVF_DRV_STAT(tx_subdesc_err),
93         NICVF_DRV_STAT(tx_max_size_exceeded),
94         NICVF_DRV_STAT(tx_imm_size_oflow),
95         NICVF_DRV_STAT(tx_data_seq_err),
96         NICVF_DRV_STAT(tx_mem_seq_err),
97         NICVF_DRV_STAT(tx_lock_viol),
98         NICVF_DRV_STAT(tx_data_fault),
99         NICVF_DRV_STAT(tx_tstmp_conflict),
100         NICVF_DRV_STAT(tx_tstmp_timeout),
101         NICVF_DRV_STAT(tx_mem_fault),
102         NICVF_DRV_STAT(tx_csum_overlap),
103         NICVF_DRV_STAT(tx_csum_overflow),
104
105         NICVF_DRV_STAT(tx_tso),
106         NICVF_DRV_STAT(tx_timeout),
107         NICVF_DRV_STAT(txq_stop),
108         NICVF_DRV_STAT(txq_wake),
109         NICVF_DRV_STAT(rcv_buffer_alloc_failures),
110         NICVF_DRV_STAT(page_alloc),
111 };
112
113 static const struct nicvf_stat nicvf_queue_stats[] = {
114         { "bytes", 0 },
115         { "frames", 1 },
116 };
117
118 static const unsigned int nicvf_n_hw_stats = ARRAY_SIZE(nicvf_hw_stats);
119 static const unsigned int nicvf_n_drv_stats = ARRAY_SIZE(nicvf_drv_stats);
120 static const unsigned int nicvf_n_queue_stats = ARRAY_SIZE(nicvf_queue_stats);
121
122 static int nicvf_get_link_ksettings(struct net_device *netdev,
123                                     struct ethtool_link_ksettings *cmd)
124 {
125         struct nicvf *nic = netdev_priv(netdev);
126         u32 supported, advertising;
127
128         supported = 0;
129         advertising = 0;
130
131         if (!nic->link_up) {
132                 cmd->base.duplex = DUPLEX_UNKNOWN;
133                 cmd->base.speed = SPEED_UNKNOWN;
134                 return 0;
135         }
136
137         switch (nic->speed) {
138         case SPEED_1000:
139                 cmd->base.port = PORT_MII | PORT_TP;
140                 cmd->base.autoneg = AUTONEG_ENABLE;
141                 supported |= SUPPORTED_MII | SUPPORTED_TP;
142                 supported |= SUPPORTED_1000baseT_Full |
143                                   SUPPORTED_1000baseT_Half |
144                                   SUPPORTED_100baseT_Full  |
145                                   SUPPORTED_100baseT_Half  |
146                                   SUPPORTED_10baseT_Full   |
147                                   SUPPORTED_10baseT_Half;
148                 supported |= SUPPORTED_Autoneg;
149                 advertising |= ADVERTISED_1000baseT_Full |
150                                     ADVERTISED_1000baseT_Half |
151                                     ADVERTISED_100baseT_Full  |
152                                     ADVERTISED_100baseT_Half  |
153                                     ADVERTISED_10baseT_Full   |
154                                     ADVERTISED_10baseT_Half;
155                 break;
156         case SPEED_10000:
157                 if (nic->mac_type == BGX_MODE_RXAUI) {
158                         cmd->base.port = PORT_TP;
159                         supported |= SUPPORTED_TP;
160                 } else {
161                         cmd->base.port = PORT_FIBRE;
162                         supported |= SUPPORTED_FIBRE;
163                 }
164                 cmd->base.autoneg = AUTONEG_DISABLE;
165                 supported |= SUPPORTED_10000baseT_Full;
166                 break;
167         case SPEED_40000:
168                 cmd->base.port = PORT_FIBRE;
169                 cmd->base.autoneg = AUTONEG_DISABLE;
170                 supported |= SUPPORTED_FIBRE;
171                 supported |= SUPPORTED_40000baseCR4_Full;
172                 break;
173         }
174         cmd->base.duplex = nic->duplex;
175         cmd->base.speed = nic->speed;
176
177         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
178                                                 supported);
179         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
180                                                 advertising);
181
182         return 0;
183 }
184
185 static u32 nicvf_get_link(struct net_device *netdev)
186 {
187         struct nicvf *nic = netdev_priv(netdev);
188
189         return nic->link_up;
190 }
191
192 static void nicvf_get_drvinfo(struct net_device *netdev,
193                               struct ethtool_drvinfo *info)
194 {
195         struct nicvf *nic = netdev_priv(netdev);
196
197         strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
198         strlcpy(info->version, DRV_VERSION, sizeof(info->version));
199         strlcpy(info->bus_info, pci_name(nic->pdev), sizeof(info->bus_info));
200 }
201
202 static u32 nicvf_get_msglevel(struct net_device *netdev)
203 {
204         struct nicvf *nic = netdev_priv(netdev);
205
206         return nic->msg_enable;
207 }
208
209 static void nicvf_set_msglevel(struct net_device *netdev, u32 lvl)
210 {
211         struct nicvf *nic = netdev_priv(netdev);
212
213         nic->msg_enable = lvl;
214 }
215
216 static void nicvf_get_qset_strings(struct nicvf *nic, u8 **data, int qset)
217 {
218         int stats, qidx;
219         int start_qidx = qset * MAX_RCV_QUEUES_PER_QS;
220
221         for (qidx = 0; qidx < nic->qs->rq_cnt; qidx++) {
222                 for (stats = 0; stats < nicvf_n_queue_stats; stats++) {
223                         sprintf(*data, "rxq%d: %s", qidx + start_qidx,
224                                 nicvf_queue_stats[stats].name);
225                         *data += ETH_GSTRING_LEN;
226                 }
227         }
228
229         for (qidx = 0; qidx < nic->qs->sq_cnt; qidx++) {
230                 for (stats = 0; stats < nicvf_n_queue_stats; stats++) {
231                         sprintf(*data, "txq%d: %s", qidx + start_qidx,
232                                 nicvf_queue_stats[stats].name);
233                         *data += ETH_GSTRING_LEN;
234                 }
235         }
236 }
237
238 static void nicvf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
239 {
240         struct nicvf *nic = netdev_priv(netdev);
241         int stats;
242         int sqs;
243
244         if (sset != ETH_SS_STATS)
245                 return;
246
247         for (stats = 0; stats < nicvf_n_hw_stats; stats++) {
248                 memcpy(data, nicvf_hw_stats[stats].name, ETH_GSTRING_LEN);
249                 data += ETH_GSTRING_LEN;
250         }
251
252         for (stats = 0; stats < nicvf_n_drv_stats; stats++) {
253                 memcpy(data, nicvf_drv_stats[stats].name, ETH_GSTRING_LEN);
254                 data += ETH_GSTRING_LEN;
255         }
256
257         nicvf_get_qset_strings(nic, &data, 0);
258
259         for (sqs = 0; sqs < nic->sqs_count; sqs++) {
260                 if (!nic->snicvf[sqs])
261                         continue;
262                 nicvf_get_qset_strings(nic->snicvf[sqs], &data, sqs + 1);
263         }
264
265         for (stats = 0; stats < BGX_RX_STATS_COUNT; stats++) {
266                 sprintf(data, "bgx_rxstat%d: ", stats);
267                 data += ETH_GSTRING_LEN;
268         }
269
270         for (stats = 0; stats < BGX_TX_STATS_COUNT; stats++) {
271                 sprintf(data, "bgx_txstat%d: ", stats);
272                 data += ETH_GSTRING_LEN;
273         }
274 }
275
276 static int nicvf_get_sset_count(struct net_device *netdev, int sset)
277 {
278         struct nicvf *nic = netdev_priv(netdev);
279         int qstats_count;
280         int sqs;
281
282         if (sset != ETH_SS_STATS)
283                 return -EINVAL;
284
285         qstats_count = nicvf_n_queue_stats *
286                        (nic->qs->rq_cnt + nic->qs->sq_cnt);
287         for (sqs = 0; sqs < nic->sqs_count; sqs++) {
288                 struct nicvf *snic;
289
290                 snic = nic->snicvf[sqs];
291                 if (!snic)
292                         continue;
293                 qstats_count += nicvf_n_queue_stats *
294                                 (snic->qs->rq_cnt + snic->qs->sq_cnt);
295         }
296
297         return nicvf_n_hw_stats + nicvf_n_drv_stats +
298                 qstats_count +
299                 BGX_RX_STATS_COUNT + BGX_TX_STATS_COUNT;
300 }
301
302 static void nicvf_get_qset_stats(struct nicvf *nic,
303                                  struct ethtool_stats *stats, u64 **data)
304 {
305         int stat, qidx;
306
307         if (!nic)
308                 return;
309
310         for (qidx = 0; qidx < nic->qs->rq_cnt; qidx++) {
311                 nicvf_update_rq_stats(nic, qidx);
312                 for (stat = 0; stat < nicvf_n_queue_stats; stat++)
313                         *((*data)++) = ((u64 *)&nic->qs->rq[qidx].stats)
314                                         [nicvf_queue_stats[stat].index];
315         }
316
317         for (qidx = 0; qidx < nic->qs->sq_cnt; qidx++) {
318                 nicvf_update_sq_stats(nic, qidx);
319                 for (stat = 0; stat < nicvf_n_queue_stats; stat++)
320                         *((*data)++) = ((u64 *)&nic->qs->sq[qidx].stats)
321                                         [nicvf_queue_stats[stat].index];
322         }
323 }
324
325 static void nicvf_get_ethtool_stats(struct net_device *netdev,
326                                     struct ethtool_stats *stats, u64 *data)
327 {
328         struct nicvf *nic = netdev_priv(netdev);
329         int stat, tmp_stats;
330         int sqs, cpu;
331
332         nicvf_update_stats(nic);
333
334         /* Update LMAC stats */
335         nicvf_update_lmac_stats(nic);
336
337         for (stat = 0; stat < nicvf_n_hw_stats; stat++)
338                 *(data++) = ((u64 *)&nic->hw_stats)
339                                 [nicvf_hw_stats[stat].index];
340         for (stat = 0; stat < nicvf_n_drv_stats; stat++) {
341                 tmp_stats = 0;
342                 for_each_possible_cpu(cpu)
343                         tmp_stats += ((u64 *)per_cpu_ptr(nic->drv_stats, cpu))
344                                      [nicvf_drv_stats[stat].index];
345                 *(data++) = tmp_stats;
346         }
347
348         nicvf_get_qset_stats(nic, stats, &data);
349
350         for (sqs = 0; sqs < nic->sqs_count; sqs++) {
351                 if (!nic->snicvf[sqs])
352                         continue;
353                 nicvf_get_qset_stats(nic->snicvf[sqs], stats, &data);
354         }
355
356         for (stat = 0; stat < BGX_RX_STATS_COUNT; stat++)
357                 *(data++) = nic->bgx_stats.rx_stats[stat];
358         for (stat = 0; stat < BGX_TX_STATS_COUNT; stat++)
359                 *(data++) = nic->bgx_stats.tx_stats[stat];
360 }
361
362 static int nicvf_get_regs_len(struct net_device *dev)
363 {
364         return sizeof(u64) * NIC_VF_REG_COUNT;
365 }
366
367 static void nicvf_get_regs(struct net_device *dev,
368                            struct ethtool_regs *regs, void *reg)
369 {
370         struct nicvf *nic = netdev_priv(dev);
371         u64 *p = (u64 *)reg;
372         u64 reg_offset;
373         int mbox, key, stat, q;
374         int i = 0;
375
376         regs->version = 0;
377         memset(p, 0, NIC_VF_REG_COUNT);
378
379         p[i++] = nicvf_reg_read(nic, NIC_VNIC_CFG);
380         /* Mailbox registers */
381         for (mbox = 0; mbox < NIC_PF_VF_MAILBOX_SIZE; mbox++)
382                 p[i++] = nicvf_reg_read(nic,
383                                         NIC_VF_PF_MAILBOX_0_1 | (mbox << 3));
384
385         p[i++] = nicvf_reg_read(nic, NIC_VF_INT);
386         p[i++] = nicvf_reg_read(nic, NIC_VF_INT_W1S);
387         p[i++] = nicvf_reg_read(nic, NIC_VF_ENA_W1C);
388         p[i++] = nicvf_reg_read(nic, NIC_VF_ENA_W1S);
389         p[i++] = nicvf_reg_read(nic, NIC_VNIC_RSS_CFG);
390
391         for (key = 0; key < RSS_HASH_KEY_SIZE; key++)
392                 p[i++] = nicvf_reg_read(nic, NIC_VNIC_RSS_KEY_0_4 | (key << 3));
393
394         /* Tx/Rx statistics */
395         for (stat = 0; stat < TX_STATS_ENUM_LAST; stat++)
396                 p[i++] = nicvf_reg_read(nic,
397                                         NIC_VNIC_TX_STAT_0_4 | (stat << 3));
398
399         for (i = 0; i < RX_STATS_ENUM_LAST; i++)
400                 p[i++] = nicvf_reg_read(nic,
401                                         NIC_VNIC_RX_STAT_0_13 | (stat << 3));
402
403         p[i++] = nicvf_reg_read(nic, NIC_QSET_RQ_GEN_CFG);
404
405         /* All completion queue's registers */
406         for (q = 0; q < MAX_CMP_QUEUES_PER_QS; q++) {
407                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_CFG, q);
408                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_CFG2, q);
409                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_THRESH, q);
410                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_BASE, q);
411                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD, q);
412                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_TAIL, q);
413                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_DOOR, q);
414                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS, q);
415                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS2, q);
416                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_DEBUG, q);
417         }
418
419         /* All receive queue's registers */
420         for (q = 0; q < MAX_RCV_QUEUES_PER_QS; q++) {
421                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RQ_0_7_CFG, q);
422                 p[i++] = nicvf_queue_reg_read(nic,
423                                                   NIC_QSET_RQ_0_7_STAT_0_1, q);
424                 reg_offset = NIC_QSET_RQ_0_7_STAT_0_1 | (1 << 3);
425                 p[i++] = nicvf_queue_reg_read(nic, reg_offset, q);
426         }
427
428         for (q = 0; q < MAX_SND_QUEUES_PER_QS; q++) {
429                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, q);
430                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_THRESH, q);
431                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_BASE, q);
432                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_HEAD, q);
433                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_TAIL, q);
434                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_DOOR, q);
435                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_STATUS, q);
436                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_DEBUG, q);
437                 /* Padding, was NIC_QSET_SQ_0_7_CNM_CHG, which
438                  * produces bus errors when read
439                  */
440                 p[i++] = 0;
441                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_STAT_0_1, q);
442                 reg_offset = NIC_QSET_SQ_0_7_STAT_0_1 | (1 << 3);
443                 p[i++] = nicvf_queue_reg_read(nic, reg_offset, q);
444         }
445
446         for (q = 0; q < MAX_RCV_BUF_DESC_RINGS_PER_QS; q++) {
447                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_CFG, q);
448                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_THRESH, q);
449                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_BASE, q);
450                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_HEAD, q);
451                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_TAIL, q);
452                 p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_DOOR, q);
453                 p[i++] = nicvf_queue_reg_read(nic,
454                                               NIC_QSET_RBDR_0_1_STATUS0, q);
455                 p[i++] = nicvf_queue_reg_read(nic,
456                                               NIC_QSET_RBDR_0_1_STATUS1, q);
457                 reg_offset = NIC_QSET_RBDR_0_1_PREFETCH_STATUS;
458                 p[i++] = nicvf_queue_reg_read(nic, reg_offset, q);
459         }
460 }
461
462 static int nicvf_get_coalesce(struct net_device *netdev,
463                               struct ethtool_coalesce *cmd)
464 {
465         struct nicvf *nic = netdev_priv(netdev);
466
467         cmd->rx_coalesce_usecs = nic->cq_coalesce_usecs;
468         return 0;
469 }
470
471 static void nicvf_get_ringparam(struct net_device *netdev,
472                                 struct ethtool_ringparam *ring)
473 {
474         struct nicvf *nic = netdev_priv(netdev);
475         struct queue_set *qs = nic->qs;
476
477         ring->rx_max_pending = MAX_CMP_QUEUE_LEN;
478         ring->rx_pending = qs->cq_len;
479         ring->tx_max_pending = MAX_SND_QUEUE_LEN;
480         ring->tx_pending = qs->sq_len;
481 }
482
483 static int nicvf_set_ringparam(struct net_device *netdev,
484                                struct ethtool_ringparam *ring)
485 {
486         struct nicvf *nic = netdev_priv(netdev);
487         struct queue_set *qs = nic->qs;
488         u32 rx_count, tx_count;
489
490         /* Due to HW errata this is not supported on T88 pass 1.x silicon */
491         if (pass1_silicon(nic->pdev))
492                 return -EINVAL;
493
494         if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
495                 return -EINVAL;
496
497         tx_count = clamp_t(u32, ring->tx_pending,
498                            MIN_SND_QUEUE_LEN, MAX_SND_QUEUE_LEN);
499         rx_count = clamp_t(u32, ring->rx_pending,
500                            MIN_CMP_QUEUE_LEN, MAX_CMP_QUEUE_LEN);
501
502         if ((tx_count == qs->sq_len) && (rx_count == qs->cq_len))
503                 return 0;
504
505         /* Permitted lengths are 1K, 2K, 4K, 8K, 16K, 32K, 64K */
506         qs->sq_len = rounddown_pow_of_two(tx_count);
507         qs->cq_len = rounddown_pow_of_two(rx_count);
508
509         if (netif_running(netdev)) {
510                 nicvf_stop(netdev);
511                 nicvf_open(netdev);
512         }
513
514         return 0;
515 }
516
517 static int nicvf_get_rss_hash_opts(struct nicvf *nic,
518                                    struct ethtool_rxnfc *info)
519 {
520         info->data = 0;
521
522         switch (info->flow_type) {
523         case TCP_V4_FLOW:
524         case TCP_V6_FLOW:
525         case UDP_V4_FLOW:
526         case UDP_V6_FLOW:
527         case SCTP_V4_FLOW:
528         case SCTP_V6_FLOW:
529                 info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
530                 /* Fall through */
531         case IPV4_FLOW:
532         case IPV6_FLOW:
533                 info->data |= RXH_IP_SRC | RXH_IP_DST;
534                 break;
535         default:
536                 return -EINVAL;
537         }
538
539         return 0;
540 }
541
542 static int nicvf_get_rxnfc(struct net_device *dev,
543                            struct ethtool_rxnfc *info, u32 *rules)
544 {
545         struct nicvf *nic = netdev_priv(dev);
546         int ret = -EOPNOTSUPP;
547
548         switch (info->cmd) {
549         case ETHTOOL_GRXRINGS:
550                 info->data = nic->rx_queues;
551                 ret = 0;
552                 break;
553         case ETHTOOL_GRXFH:
554                 return nicvf_get_rss_hash_opts(nic, info);
555         default:
556                 break;
557         }
558         return ret;
559 }
560
561 static int nicvf_set_rss_hash_opts(struct nicvf *nic,
562                                    struct ethtool_rxnfc *info)
563 {
564         struct nicvf_rss_info *rss = &nic->rss_info;
565         u64 rss_cfg = nicvf_reg_read(nic, NIC_VNIC_RSS_CFG);
566
567         if (!rss->enable)
568                 netdev_err(nic->netdev,
569                            "RSS is disabled, hash cannot be set\n");
570
571         netdev_info(nic->netdev, "Set RSS flow type = %d, data = %lld\n",
572                     info->flow_type, info->data);
573
574         if (!(info->data & RXH_IP_SRC) || !(info->data & RXH_IP_DST))
575                 return -EINVAL;
576
577         switch (info->flow_type) {
578         case TCP_V4_FLOW:
579         case TCP_V6_FLOW:
580                 switch (info->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
581                 case 0:
582                         rss_cfg &= ~(1ULL << RSS_HASH_TCP);
583                         break;
584                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
585                         rss_cfg |= (1ULL << RSS_HASH_TCP);
586                         break;
587                 default:
588                         return -EINVAL;
589                 }
590                 break;
591         case UDP_V4_FLOW:
592         case UDP_V6_FLOW:
593                 switch (info->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
594                 case 0:
595                         rss_cfg &= ~(1ULL << RSS_HASH_UDP);
596                         break;
597                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
598                         rss_cfg |= (1ULL << RSS_HASH_UDP);
599                         break;
600                 default:
601                         return -EINVAL;
602                 }
603                 break;
604         case SCTP_V4_FLOW:
605         case SCTP_V6_FLOW:
606                 switch (info->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
607                 case 0:
608                         rss_cfg &= ~(1ULL << RSS_HASH_L4ETC);
609                         break;
610                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
611                         rss_cfg |= (1ULL << RSS_HASH_L4ETC);
612                         break;
613                 default:
614                         return -EINVAL;
615                 }
616                 break;
617         case IPV4_FLOW:
618         case IPV6_FLOW:
619                 rss_cfg = RSS_HASH_IP;
620                 break;
621         default:
622                 return -EINVAL;
623         }
624
625         nicvf_reg_write(nic, NIC_VNIC_RSS_CFG, rss_cfg);
626         return 0;
627 }
628
629 static int nicvf_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
630 {
631         struct nicvf *nic = netdev_priv(dev);
632
633         switch (info->cmd) {
634         case ETHTOOL_SRXFH:
635                 return nicvf_set_rss_hash_opts(nic, info);
636         default:
637                 break;
638         }
639         return -EOPNOTSUPP;
640 }
641
642 static u32 nicvf_get_rxfh_key_size(struct net_device *netdev)
643 {
644         return RSS_HASH_KEY_SIZE * sizeof(u64);
645 }
646
647 static u32 nicvf_get_rxfh_indir_size(struct net_device *dev)
648 {
649         struct nicvf *nic = netdev_priv(dev);
650
651         return nic->rss_info.rss_size;
652 }
653
654 static int nicvf_get_rxfh(struct net_device *dev, u32 *indir, u8 *hkey,
655                           u8 *hfunc)
656 {
657         struct nicvf *nic = netdev_priv(dev);
658         struct nicvf_rss_info *rss = &nic->rss_info;
659         int idx;
660
661         if (indir) {
662                 for (idx = 0; idx < rss->rss_size; idx++)
663                         indir[idx] = rss->ind_tbl[idx];
664         }
665
666         if (hkey)
667                 memcpy(hkey, rss->key, RSS_HASH_KEY_SIZE * sizeof(u64));
668
669         if (hfunc)
670                 *hfunc = ETH_RSS_HASH_TOP;
671
672         return 0;
673 }
674
675 static int nicvf_set_rxfh(struct net_device *dev, const u32 *indir,
676                           const u8 *hkey, const u8 hfunc)
677 {
678         struct nicvf *nic = netdev_priv(dev);
679         struct nicvf_rss_info *rss = &nic->rss_info;
680         int idx;
681
682         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
683                 return -EOPNOTSUPP;
684
685         if (!rss->enable) {
686                 netdev_err(nic->netdev,
687                            "RSS is disabled, cannot change settings\n");
688                 return -EIO;
689         }
690
691         if (indir) {
692                 for (idx = 0; idx < rss->rss_size; idx++)
693                         rss->ind_tbl[idx] = indir[idx];
694         }
695
696         if (hkey) {
697                 memcpy(rss->key, hkey, RSS_HASH_KEY_SIZE * sizeof(u64));
698                 nicvf_set_rss_key(nic);
699         }
700
701         nicvf_config_rss(nic);
702         return 0;
703 }
704
705 /* Get no of queues device supports and current queue count */
706 static void nicvf_get_channels(struct net_device *dev,
707                                struct ethtool_channels *channel)
708 {
709         struct nicvf *nic = netdev_priv(dev);
710
711         memset(channel, 0, sizeof(*channel));
712
713         channel->max_rx = nic->max_queues;
714         channel->max_tx = nic->max_queues;
715
716         channel->rx_count = nic->rx_queues;
717         channel->tx_count = nic->tx_queues;
718 }
719
720 /* Set no of Tx, Rx queues to be used */
721 static int nicvf_set_channels(struct net_device *dev,
722                               struct ethtool_channels *channel)
723 {
724         struct nicvf *nic = netdev_priv(dev);
725         int err = 0;
726         bool if_up = netif_running(dev);
727         u8 cqcount, txq_count;
728
729         if (!channel->rx_count || !channel->tx_count)
730                 return -EINVAL;
731         if (channel->rx_count > nic->max_queues)
732                 return -EINVAL;
733         if (channel->tx_count > nic->max_queues)
734                 return -EINVAL;
735
736         if (nic->xdp_prog &&
737             ((channel->tx_count + channel->rx_count) > nic->max_queues)) {
738                 netdev_err(nic->netdev,
739                            "XDP mode, RXQs + TXQs > Max %d\n",
740                            nic->max_queues);
741                 return -EINVAL;
742         }
743
744         if (if_up)
745                 nicvf_stop(dev);
746
747         nic->rx_queues = channel->rx_count;
748         nic->tx_queues = channel->tx_count;
749         if (!nic->xdp_prog)
750                 nic->xdp_tx_queues = 0;
751         else
752                 nic->xdp_tx_queues = channel->rx_count;
753
754         txq_count = nic->xdp_tx_queues + nic->tx_queues;
755         cqcount = max(nic->rx_queues, txq_count);
756
757         if (cqcount > MAX_CMP_QUEUES_PER_QS) {
758                 nic->sqs_count = roundup(cqcount, MAX_CMP_QUEUES_PER_QS);
759                 nic->sqs_count = (nic->sqs_count / MAX_CMP_QUEUES_PER_QS) - 1;
760         } else {
761                 nic->sqs_count = 0;
762         }
763
764         nic->qs->rq_cnt = min_t(u8, nic->rx_queues, MAX_RCV_QUEUES_PER_QS);
765         nic->qs->sq_cnt = min_t(u8, txq_count, MAX_SND_QUEUES_PER_QS);
766         nic->qs->cq_cnt = max(nic->qs->rq_cnt, nic->qs->sq_cnt);
767
768         err = nicvf_set_real_num_queues(dev, nic->tx_queues, nic->rx_queues);
769         if (err)
770                 return err;
771
772         if (if_up)
773                 nicvf_open(dev);
774
775         netdev_info(dev, "Setting num Tx rings to %d, Rx rings to %d success\n",
776                     nic->tx_queues, nic->rx_queues);
777
778         return err;
779 }
780
781 static void nicvf_get_pauseparam(struct net_device *dev,
782                                  struct ethtool_pauseparam *pause)
783 {
784         struct nicvf *nic = netdev_priv(dev);
785         union nic_mbx mbx = {};
786
787         /* Supported only for 10G/40G interfaces */
788         if ((nic->mac_type == BGX_MODE_SGMII) ||
789             (nic->mac_type == BGX_MODE_QSGMII) ||
790             (nic->mac_type == BGX_MODE_RGMII))
791                 return;
792
793         mbx.pfc.msg = NIC_MBOX_MSG_PFC;
794         mbx.pfc.get = 1;
795         if (!nicvf_send_msg_to_pf(nic, &mbx)) {
796                 pause->autoneg = nic->pfc.autoneg;
797                 pause->rx_pause = nic->pfc.fc_rx;
798                 pause->tx_pause = nic->pfc.fc_tx;
799         }
800 }
801
802 static int nicvf_set_pauseparam(struct net_device *dev,
803                                 struct ethtool_pauseparam *pause)
804 {
805         struct nicvf *nic = netdev_priv(dev);
806         union nic_mbx mbx = {};
807
808         /* Supported only for 10G/40G interfaces */
809         if ((nic->mac_type == BGX_MODE_SGMII) ||
810             (nic->mac_type == BGX_MODE_QSGMII) ||
811             (nic->mac_type == BGX_MODE_RGMII))
812                 return -EOPNOTSUPP;
813
814         if (pause->autoneg)
815                 return -EOPNOTSUPP;
816
817         mbx.pfc.msg = NIC_MBOX_MSG_PFC;
818         mbx.pfc.get = 0;
819         mbx.pfc.fc_rx = pause->rx_pause;
820         mbx.pfc.fc_tx = pause->tx_pause;
821         if (nicvf_send_msg_to_pf(nic, &mbx))
822                 return -EAGAIN;
823
824         nic->pfc.fc_rx = pause->rx_pause;
825         nic->pfc.fc_tx = pause->tx_pause;
826
827         return 0;
828 }
829
830 static int nicvf_get_ts_info(struct net_device *netdev,
831                              struct ethtool_ts_info *info)
832 {
833         struct nicvf *nic = netdev_priv(netdev);
834
835         if (!nic->ptp_clock)
836                 return ethtool_op_get_ts_info(netdev, info);
837
838         info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
839                                 SOF_TIMESTAMPING_RX_SOFTWARE |
840                                 SOF_TIMESTAMPING_SOFTWARE |
841                                 SOF_TIMESTAMPING_TX_HARDWARE |
842                                 SOF_TIMESTAMPING_RX_HARDWARE |
843                                 SOF_TIMESTAMPING_RAW_HARDWARE;
844
845         info->phc_index = cavium_ptp_clock_index(nic->ptp_clock);
846
847         info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
848
849         info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
850                            (1 << HWTSTAMP_FILTER_ALL);
851
852         return 0;
853 }
854
855 static const struct ethtool_ops nicvf_ethtool_ops = {
856         .get_link               = nicvf_get_link,
857         .get_drvinfo            = nicvf_get_drvinfo,
858         .get_msglevel           = nicvf_get_msglevel,
859         .set_msglevel           = nicvf_set_msglevel,
860         .get_strings            = nicvf_get_strings,
861         .get_sset_count         = nicvf_get_sset_count,
862         .get_ethtool_stats      = nicvf_get_ethtool_stats,
863         .get_regs_len           = nicvf_get_regs_len,
864         .get_regs               = nicvf_get_regs,
865         .get_coalesce           = nicvf_get_coalesce,
866         .get_ringparam          = nicvf_get_ringparam,
867         .set_ringparam          = nicvf_set_ringparam,
868         .get_rxnfc              = nicvf_get_rxnfc,
869         .set_rxnfc              = nicvf_set_rxnfc,
870         .get_rxfh_key_size      = nicvf_get_rxfh_key_size,
871         .get_rxfh_indir_size    = nicvf_get_rxfh_indir_size,
872         .get_rxfh               = nicvf_get_rxfh,
873         .set_rxfh               = nicvf_set_rxfh,
874         .get_channels           = nicvf_get_channels,
875         .set_channels           = nicvf_set_channels,
876         .get_pauseparam         = nicvf_get_pauseparam,
877         .set_pauseparam         = nicvf_set_pauseparam,
878         .get_ts_info            = nicvf_get_ts_info,
879         .get_link_ksettings     = nicvf_get_link_ksettings,
880 };
881
882 void nicvf_set_ethtool_ops(struct net_device *netdev)
883 {
884         netdev->ethtool_ops = &nicvf_ethtool_ops;
885 }