cxgb4: Add support in ethtool to dump channel stats
[firefly-linux-kernel-4.4.55.git] / drivers / net / ethernet / chelsio / cxgb4 / cxgb4_ethtool.c
1 /*
2  *  Copyright (C) 2013-2015 Chelsio Communications.  All rights reserved.
3  *
4  *  This program is free software; you can redistribute it and/or modify it
5  *  under the terms and conditions of the GNU General Public License,
6  *  version 2, as published by the Free Software Foundation.
7  *
8  *  This program is distributed in the hope it will be useful, but WITHOUT
9  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  *  more details.
12  *
13  *  The full GNU General Public License is included in this distribution in
14  *  the file called "COPYING".
15  *
16  */
17
18 #include <linux/firmware.h>
19 #include <linux/mdio.h>
20
21 #include "cxgb4.h"
22 #include "t4_regs.h"
23 #include "t4fw_api.h"
24
25 #define EEPROM_MAGIC 0x38E2F10C
26
27 static u32 get_msglevel(struct net_device *dev)
28 {
29         return netdev2adap(dev)->msg_enable;
30 }
31
32 static void set_msglevel(struct net_device *dev, u32 val)
33 {
34         netdev2adap(dev)->msg_enable = val;
35 }
36
37 static const char stats_strings[][ETH_GSTRING_LEN] = {
38         "TxOctetsOK         ",
39         "TxFramesOK         ",
40         "TxBroadcastFrames  ",
41         "TxMulticastFrames  ",
42         "TxUnicastFrames    ",
43         "TxErrorFrames      ",
44
45         "TxFrames64         ",
46         "TxFrames65To127    ",
47         "TxFrames128To255   ",
48         "TxFrames256To511   ",
49         "TxFrames512To1023  ",
50         "TxFrames1024To1518 ",
51         "TxFrames1519ToMax  ",
52
53         "TxFramesDropped    ",
54         "TxPauseFrames      ",
55         "TxPPP0Frames       ",
56         "TxPPP1Frames       ",
57         "TxPPP2Frames       ",
58         "TxPPP3Frames       ",
59         "TxPPP4Frames       ",
60         "TxPPP5Frames       ",
61         "TxPPP6Frames       ",
62         "TxPPP7Frames       ",
63
64         "RxOctetsOK         ",
65         "RxFramesOK         ",
66         "RxBroadcastFrames  ",
67         "RxMulticastFrames  ",
68         "RxUnicastFrames    ",
69
70         "RxFramesTooLong    ",
71         "RxJabberErrors     ",
72         "RxFCSErrors        ",
73         "RxLengthErrors     ",
74         "RxSymbolErrors     ",
75         "RxRuntFrames       ",
76
77         "RxFrames64         ",
78         "RxFrames65To127    ",
79         "RxFrames128To255   ",
80         "RxFrames256To511   ",
81         "RxFrames512To1023  ",
82         "RxFrames1024To1518 ",
83         "RxFrames1519ToMax  ",
84
85         "RxPauseFrames      ",
86         "RxPPP0Frames       ",
87         "RxPPP1Frames       ",
88         "RxPPP2Frames       ",
89         "RxPPP3Frames       ",
90         "RxPPP4Frames       ",
91         "RxPPP5Frames       ",
92         "RxPPP6Frames       ",
93         "RxPPP7Frames       ",
94
95         "RxBG0FramesDropped ",
96         "RxBG1FramesDropped ",
97         "RxBG2FramesDropped ",
98         "RxBG3FramesDropped ",
99         "RxBG0FramesTrunc   ",
100         "RxBG1FramesTrunc   ",
101         "RxBG2FramesTrunc   ",
102         "RxBG3FramesTrunc   ",
103
104         "TSO                ",
105         "TxCsumOffload      ",
106         "RxCsumGood         ",
107         "VLANextractions    ",
108         "VLANinsertions     ",
109         "GROpackets         ",
110         "GROmerged          ",
111 };
112
113 static char adapter_stats_strings[][ETH_GSTRING_LEN] = {
114         "db_drop                ",
115         "db_full                ",
116         "db_empty               ",
117         "tcp_ipv4_out_rsts      ",
118         "tcp_ipv4_in_segs       ",
119         "tcp_ipv4_out_segs      ",
120         "tcp_ipv4_retrans_segs  ",
121         "tcp_ipv6_out_rsts      ",
122         "tcp_ipv6_in_segs       ",
123         "tcp_ipv6_out_segs      ",
124         "tcp_ipv6_retrans_segs  ",
125         "usm_ddp_frames         ",
126         "usm_ddp_octets         ",
127         "usm_ddp_drops          ",
128         "rdma_no_rqe_mod_defer  ",
129         "rdma_no_rqe_pkt_defer  ",
130         "tp_err_ofld_no_neigh   ",
131         "tp_err_ofld_cong_defer ",
132         "write_coal_success     ",
133         "write_coal_fail        ",
134 };
135
136 static char channel_stats_strings[][ETH_GSTRING_LEN] = {
137         "--------Channel--------- ",
138         "tp_cpl_requests        ",
139         "tp_cpl_responses       ",
140         "tp_mac_in_errs         ",
141         "tp_hdr_in_errs         ",
142         "tp_tcp_in_errs         ",
143         "tp_tcp6_in_errs        ",
144         "tp_tnl_cong_drops      ",
145         "tp_tnl_tx_drops        ",
146         "tp_ofld_vlan_drops     ",
147         "tp_ofld_chan_drops     ",
148         "fcoe_octets_ddp        ",
149         "fcoe_frames_ddp        ",
150         "fcoe_frames_drop       ",
151 };
152
153 static int get_sset_count(struct net_device *dev, int sset)
154 {
155         switch (sset) {
156         case ETH_SS_STATS:
157                 return ARRAY_SIZE(stats_strings) +
158                        ARRAY_SIZE(adapter_stats_strings) +
159                        ARRAY_SIZE(channel_stats_strings);
160         default:
161                 return -EOPNOTSUPP;
162         }
163 }
164
165 static int get_regs_len(struct net_device *dev)
166 {
167         struct adapter *adap = netdev2adap(dev);
168
169         return t4_get_regs_len(adap);
170 }
171
172 static int get_eeprom_len(struct net_device *dev)
173 {
174         return EEPROMSIZE;
175 }
176
177 static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
178 {
179         struct adapter *adapter = netdev2adap(dev);
180         u32 exprom_vers;
181
182         strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
183         strlcpy(info->version, cxgb4_driver_version,
184                 sizeof(info->version));
185         strlcpy(info->bus_info, pci_name(adapter->pdev),
186                 sizeof(info->bus_info));
187
188         if (adapter->params.fw_vers)
189                 snprintf(info->fw_version, sizeof(info->fw_version),
190                          "%u.%u.%u.%u, TP %u.%u.%u.%u",
191                          FW_HDR_FW_VER_MAJOR_G(adapter->params.fw_vers),
192                          FW_HDR_FW_VER_MINOR_G(adapter->params.fw_vers),
193                          FW_HDR_FW_VER_MICRO_G(adapter->params.fw_vers),
194                          FW_HDR_FW_VER_BUILD_G(adapter->params.fw_vers),
195                          FW_HDR_FW_VER_MAJOR_G(adapter->params.tp_vers),
196                          FW_HDR_FW_VER_MINOR_G(adapter->params.tp_vers),
197                          FW_HDR_FW_VER_MICRO_G(adapter->params.tp_vers),
198                          FW_HDR_FW_VER_BUILD_G(adapter->params.tp_vers));
199
200         if (!t4_get_exprom_version(adapter, &exprom_vers))
201                 snprintf(info->erom_version, sizeof(info->erom_version),
202                          "%u.%u.%u.%u",
203                          FW_HDR_FW_VER_MAJOR_G(exprom_vers),
204                          FW_HDR_FW_VER_MINOR_G(exprom_vers),
205                          FW_HDR_FW_VER_MICRO_G(exprom_vers),
206                          FW_HDR_FW_VER_BUILD_G(exprom_vers));
207 }
208
209 static void get_strings(struct net_device *dev, u32 stringset, u8 *data)
210 {
211         if (stringset == ETH_SS_STATS) {
212                 memcpy(data, stats_strings, sizeof(stats_strings));
213                 data += sizeof(stats_strings);
214                 memcpy(data, adapter_stats_strings,
215                        sizeof(adapter_stats_strings));
216                 data += sizeof(adapter_stats_strings);
217                 memcpy(data, channel_stats_strings,
218                        sizeof(channel_stats_strings));
219         }
220 }
221
222 /* port stats maintained per queue of the port. They should be in the same
223  * order as in stats_strings above.
224  */
225 struct queue_port_stats {
226         u64 tso;
227         u64 tx_csum;
228         u64 rx_csum;
229         u64 vlan_ex;
230         u64 vlan_ins;
231         u64 gro_pkts;
232         u64 gro_merged;
233 };
234
235 struct adapter_stats {
236         u64 db_drop;
237         u64 db_full;
238         u64 db_empty;
239         u64 tcp_v4_out_rsts;
240         u64 tcp_v4_in_segs;
241         u64 tcp_v4_out_segs;
242         u64 tcp_v4_retrans_segs;
243         u64 tcp_v6_out_rsts;
244         u64 tcp_v6_in_segs;
245         u64 tcp_v6_out_segs;
246         u64 tcp_v6_retrans_segs;
247         u64 frames;
248         u64 octets;
249         u64 drops;
250         u64 rqe_dfr_mod;
251         u64 rqe_dfr_pkt;
252         u64 ofld_no_neigh;
253         u64 ofld_cong_defer;
254         u64 wc_success;
255         u64 wc_fail;
256 };
257
258 struct channel_stats {
259         u64 cpl_req;
260         u64 cpl_rsp;
261         u64 mac_in_errs;
262         u64 hdr_in_errs;
263         u64 tcp_in_errs;
264         u64 tcp6_in_errs;
265         u64 tnl_cong_drops;
266         u64 tnl_tx_drops;
267         u64 ofld_vlan_drops;
268         u64 ofld_chan_drops;
269         u64 octets_ddp;
270         u64 frames_ddp;
271         u64 frames_drop;
272 };
273
274 static void collect_sge_port_stats(const struct adapter *adap,
275                                    const struct port_info *p,
276                                    struct queue_port_stats *s)
277 {
278         int i;
279         const struct sge_eth_txq *tx = &adap->sge.ethtxq[p->first_qset];
280         const struct sge_eth_rxq *rx = &adap->sge.ethrxq[p->first_qset];
281
282         memset(s, 0, sizeof(*s));
283         for (i = 0; i < p->nqsets; i++, rx++, tx++) {
284                 s->tso += tx->tso;
285                 s->tx_csum += tx->tx_cso;
286                 s->rx_csum += rx->stats.rx_cso;
287                 s->vlan_ex += rx->stats.vlan_ex;
288                 s->vlan_ins += tx->vlan_ins;
289                 s->gro_pkts += rx->stats.lro_pkts;
290                 s->gro_merged += rx->stats.lro_merged;
291         }
292 }
293
294 static void collect_adapter_stats(struct adapter *adap, struct adapter_stats *s)
295 {
296         struct tp_tcp_stats v4, v6;
297         struct tp_rdma_stats rdma_stats;
298         struct tp_err_stats err_stats;
299         struct tp_usm_stats usm_stats;
300         u64 val1, val2;
301
302         memset(s, 0, sizeof(*s));
303
304         spin_lock(&adap->stats_lock);
305         t4_tp_get_tcp_stats(adap, &v4, &v6);
306         t4_tp_get_rdma_stats(adap, &rdma_stats);
307         t4_get_usm_stats(adap, &usm_stats);
308         t4_tp_get_err_stats(adap, &err_stats);
309         spin_unlock(&adap->stats_lock);
310
311         s->db_drop = adap->db_stats.db_drop;
312         s->db_full = adap->db_stats.db_full;
313         s->db_empty = adap->db_stats.db_empty;
314
315         s->tcp_v4_out_rsts = v4.tcp_out_rsts;
316         s->tcp_v4_in_segs = v4.tcp_in_segs;
317         s->tcp_v4_out_segs = v4.tcp_out_segs;
318         s->tcp_v4_retrans_segs = v4.tcp_retrans_segs;
319         s->tcp_v6_out_rsts = v6.tcp_out_rsts;
320         s->tcp_v6_in_segs = v6.tcp_in_segs;
321         s->tcp_v6_out_segs = v6.tcp_out_segs;
322         s->tcp_v6_retrans_segs = v6.tcp_retrans_segs;
323
324         if (is_offload(adap)) {
325                 s->frames = usm_stats.frames;
326                 s->octets = usm_stats.octets;
327                 s->drops = usm_stats.drops;
328                 s->rqe_dfr_mod = rdma_stats.rqe_dfr_mod;
329                 s->rqe_dfr_pkt = rdma_stats.rqe_dfr_pkt;
330         }
331
332         s->ofld_no_neigh = err_stats.ofld_no_neigh;
333         s->ofld_cong_defer = err_stats.ofld_cong_defer;
334
335         if (!is_t4(adap->params.chip)) {
336                 int v;
337
338                 v = t4_read_reg(adap, SGE_STAT_CFG_A);
339                 if (STATSOURCE_T5_G(v) == 7) {
340                         val2 = t4_read_reg(adap, SGE_STAT_MATCH_A);
341                         val1 = t4_read_reg(adap, SGE_STAT_TOTAL_A);
342                         s->wc_success = val1 - val2;
343                         s->wc_fail = val2;
344                 }
345         }
346 }
347
348 static void collect_channel_stats(struct adapter *adap, struct channel_stats *s,
349                                   u8 i)
350 {
351         struct tp_cpl_stats cpl_stats;
352         struct tp_err_stats err_stats;
353         struct tp_fcoe_stats fcoe_stats;
354
355         memset(s, 0, sizeof(*s));
356
357         spin_lock(&adap->stats_lock);
358         t4_tp_get_cpl_stats(adap, &cpl_stats);
359         t4_tp_get_err_stats(adap, &err_stats);
360         t4_get_fcoe_stats(adap, i, &fcoe_stats);
361         spin_unlock(&adap->stats_lock);
362
363         s->cpl_req = cpl_stats.req[i];
364         s->cpl_rsp = cpl_stats.rsp[i];
365         s->mac_in_errs = err_stats.mac_in_errs[i];
366         s->hdr_in_errs = err_stats.hdr_in_errs[i];
367         s->tcp_in_errs = err_stats.tcp_in_errs[i];
368         s->tcp6_in_errs = err_stats.tcp6_in_errs[i];
369         s->tnl_cong_drops = err_stats.tnl_cong_drops[i];
370         s->tnl_tx_drops = err_stats.tnl_tx_drops[i];
371         s->ofld_vlan_drops = err_stats.ofld_vlan_drops[i];
372         s->ofld_chan_drops = err_stats.ofld_chan_drops[i];
373         s->octets_ddp = fcoe_stats.octets_ddp;
374         s->frames_ddp = fcoe_stats.frames_ddp;
375         s->frames_drop = fcoe_stats.frames_drop;
376 }
377
378 static void get_stats(struct net_device *dev, struct ethtool_stats *stats,
379                       u64 *data)
380 {
381         struct port_info *pi = netdev_priv(dev);
382         struct adapter *adapter = pi->adapter;
383
384         t4_get_port_stats_offset(adapter, pi->tx_chan,
385                                  (struct port_stats *)data,
386                                  &pi->stats_base);
387
388         data += sizeof(struct port_stats) / sizeof(u64);
389         collect_sge_port_stats(adapter, pi, (struct queue_port_stats *)data);
390         data += sizeof(struct queue_port_stats) / sizeof(u64);
391         collect_adapter_stats(adapter, (struct adapter_stats *)data);
392         data += sizeof(struct adapter_stats) / sizeof(u64);
393
394         *data++ = (u64)pi->port_id;
395         collect_channel_stats(adapter, (struct channel_stats *)data,
396                               pi->port_id);
397
398 }
399
400 static void get_regs(struct net_device *dev, struct ethtool_regs *regs,
401                      void *buf)
402 {
403         struct adapter *adap = netdev2adap(dev);
404         size_t buf_size;
405
406         buf_size = t4_get_regs_len(adap);
407         regs->version = mk_adap_vers(adap);
408         t4_get_regs(adap, buf, buf_size);
409 }
410
411 static int restart_autoneg(struct net_device *dev)
412 {
413         struct port_info *p = netdev_priv(dev);
414
415         if (!netif_running(dev))
416                 return -EAGAIN;
417         if (p->link_cfg.autoneg != AUTONEG_ENABLE)
418                 return -EINVAL;
419         t4_restart_aneg(p->adapter, p->adapter->pf, p->tx_chan);
420         return 0;
421 }
422
423 static int identify_port(struct net_device *dev,
424                          enum ethtool_phys_id_state state)
425 {
426         unsigned int val;
427         struct adapter *adap = netdev2adap(dev);
428
429         if (state == ETHTOOL_ID_ACTIVE)
430                 val = 0xffff;
431         else if (state == ETHTOOL_ID_INACTIVE)
432                 val = 0;
433         else
434                 return -EINVAL;
435
436         return t4_identify_port(adap, adap->pf, netdev2pinfo(dev)->viid, val);
437 }
438
439 static unsigned int from_fw_linkcaps(enum fw_port_type type, unsigned int caps)
440 {
441         unsigned int v = 0;
442
443         if (type == FW_PORT_TYPE_BT_SGMII || type == FW_PORT_TYPE_BT_XFI ||
444             type == FW_PORT_TYPE_BT_XAUI) {
445                 v |= SUPPORTED_TP;
446                 if (caps & FW_PORT_CAP_SPEED_100M)
447                         v |= SUPPORTED_100baseT_Full;
448                 if (caps & FW_PORT_CAP_SPEED_1G)
449                         v |= SUPPORTED_1000baseT_Full;
450                 if (caps & FW_PORT_CAP_SPEED_10G)
451                         v |= SUPPORTED_10000baseT_Full;
452         } else if (type == FW_PORT_TYPE_KX4 || type == FW_PORT_TYPE_KX) {
453                 v |= SUPPORTED_Backplane;
454                 if (caps & FW_PORT_CAP_SPEED_1G)
455                         v |= SUPPORTED_1000baseKX_Full;
456                 if (caps & FW_PORT_CAP_SPEED_10G)
457                         v |= SUPPORTED_10000baseKX4_Full;
458         } else if (type == FW_PORT_TYPE_KR) {
459                 v |= SUPPORTED_Backplane | SUPPORTED_10000baseKR_Full;
460         } else if (type == FW_PORT_TYPE_BP_AP) {
461                 v |= SUPPORTED_Backplane | SUPPORTED_10000baseR_FEC |
462                      SUPPORTED_10000baseKR_Full | SUPPORTED_1000baseKX_Full;
463         } else if (type == FW_PORT_TYPE_BP4_AP) {
464                 v |= SUPPORTED_Backplane | SUPPORTED_10000baseR_FEC |
465                      SUPPORTED_10000baseKR_Full | SUPPORTED_1000baseKX_Full |
466                      SUPPORTED_10000baseKX4_Full;
467         } else if (type == FW_PORT_TYPE_FIBER_XFI ||
468                    type == FW_PORT_TYPE_FIBER_XAUI ||
469                    type == FW_PORT_TYPE_SFP ||
470                    type == FW_PORT_TYPE_QSFP_10G ||
471                    type == FW_PORT_TYPE_QSA) {
472                 v |= SUPPORTED_FIBRE;
473                 if (caps & FW_PORT_CAP_SPEED_1G)
474                         v |= SUPPORTED_1000baseT_Full;
475                 if (caps & FW_PORT_CAP_SPEED_10G)
476                         v |= SUPPORTED_10000baseT_Full;
477         } else if (type == FW_PORT_TYPE_BP40_BA ||
478                    type == FW_PORT_TYPE_QSFP) {
479                 v |= SUPPORTED_40000baseSR4_Full;
480                 v |= SUPPORTED_FIBRE;
481         }
482
483         if (caps & FW_PORT_CAP_ANEG)
484                 v |= SUPPORTED_Autoneg;
485         return v;
486 }
487
488 static unsigned int to_fw_linkcaps(unsigned int caps)
489 {
490         unsigned int v = 0;
491
492         if (caps & ADVERTISED_100baseT_Full)
493                 v |= FW_PORT_CAP_SPEED_100M;
494         if (caps & ADVERTISED_1000baseT_Full)
495                 v |= FW_PORT_CAP_SPEED_1G;
496         if (caps & ADVERTISED_10000baseT_Full)
497                 v |= FW_PORT_CAP_SPEED_10G;
498         if (caps & ADVERTISED_40000baseSR4_Full)
499                 v |= FW_PORT_CAP_SPEED_40G;
500         return v;
501 }
502
503 static int get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
504 {
505         const struct port_info *p = netdev_priv(dev);
506
507         if (p->port_type == FW_PORT_TYPE_BT_SGMII ||
508             p->port_type == FW_PORT_TYPE_BT_XFI ||
509             p->port_type == FW_PORT_TYPE_BT_XAUI) {
510                 cmd->port = PORT_TP;
511         } else if (p->port_type == FW_PORT_TYPE_FIBER_XFI ||
512                    p->port_type == FW_PORT_TYPE_FIBER_XAUI) {
513                 cmd->port = PORT_FIBRE;
514         } else if (p->port_type == FW_PORT_TYPE_SFP ||
515                    p->port_type == FW_PORT_TYPE_QSFP_10G ||
516                    p->port_type == FW_PORT_TYPE_QSA ||
517                    p->port_type == FW_PORT_TYPE_QSFP) {
518                 if (p->mod_type == FW_PORT_MOD_TYPE_LR ||
519                     p->mod_type == FW_PORT_MOD_TYPE_SR ||
520                     p->mod_type == FW_PORT_MOD_TYPE_ER ||
521                     p->mod_type == FW_PORT_MOD_TYPE_LRM)
522                         cmd->port = PORT_FIBRE;
523                 else if (p->mod_type == FW_PORT_MOD_TYPE_TWINAX_PASSIVE ||
524                          p->mod_type == FW_PORT_MOD_TYPE_TWINAX_ACTIVE)
525                         cmd->port = PORT_DA;
526                 else
527                         cmd->port = PORT_OTHER;
528         } else {
529                 cmd->port = PORT_OTHER;
530         }
531
532         if (p->mdio_addr >= 0) {
533                 cmd->phy_address = p->mdio_addr;
534                 cmd->transceiver = XCVR_EXTERNAL;
535                 cmd->mdio_support = p->port_type == FW_PORT_TYPE_BT_SGMII ?
536                         MDIO_SUPPORTS_C22 : MDIO_SUPPORTS_C45;
537         } else {
538                 cmd->phy_address = 0;  /* not really, but no better option */
539                 cmd->transceiver = XCVR_INTERNAL;
540                 cmd->mdio_support = 0;
541         }
542
543         cmd->supported = from_fw_linkcaps(p->port_type, p->link_cfg.supported);
544         cmd->advertising = from_fw_linkcaps(p->port_type,
545                                             p->link_cfg.advertising);
546         ethtool_cmd_speed_set(cmd,
547                               netif_carrier_ok(dev) ? p->link_cfg.speed : 0);
548         cmd->duplex = DUPLEX_FULL;
549         cmd->autoneg = p->link_cfg.autoneg;
550         cmd->maxtxpkt = 0;
551         cmd->maxrxpkt = 0;
552         return 0;
553 }
554
555 static unsigned int speed_to_caps(int speed)
556 {
557         if (speed == 100)
558                 return FW_PORT_CAP_SPEED_100M;
559         if (speed == 1000)
560                 return FW_PORT_CAP_SPEED_1G;
561         if (speed == 10000)
562                 return FW_PORT_CAP_SPEED_10G;
563         if (speed == 40000)
564                 return FW_PORT_CAP_SPEED_40G;
565         return 0;
566 }
567
568 static int set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
569 {
570         unsigned int cap;
571         struct port_info *p = netdev_priv(dev);
572         struct link_config *lc = &p->link_cfg;
573         u32 speed = ethtool_cmd_speed(cmd);
574
575         if (cmd->duplex != DUPLEX_FULL)     /* only full-duplex supported */
576                 return -EINVAL;
577
578         if (!(lc->supported & FW_PORT_CAP_ANEG)) {
579                 /* PHY offers a single speed.  See if that's what's
580                  * being requested.
581                  */
582                 if (cmd->autoneg == AUTONEG_DISABLE &&
583                     (lc->supported & speed_to_caps(speed)))
584                         return 0;
585                 return -EINVAL;
586         }
587
588         if (cmd->autoneg == AUTONEG_DISABLE) {
589                 cap = speed_to_caps(speed);
590
591                 if (!(lc->supported & cap) ||
592                     (speed == 1000) ||
593                     (speed == 10000) ||
594                     (speed == 40000))
595                         return -EINVAL;
596                 lc->requested_speed = cap;
597                 lc->advertising = 0;
598         } else {
599                 cap = to_fw_linkcaps(cmd->advertising);
600                 if (!(lc->supported & cap))
601                         return -EINVAL;
602                 lc->requested_speed = 0;
603                 lc->advertising = cap | FW_PORT_CAP_ANEG;
604         }
605         lc->autoneg = cmd->autoneg;
606
607         if (netif_running(dev))
608                 return t4_link_start(p->adapter, p->adapter->pf, p->tx_chan,
609                                      lc);
610         return 0;
611 }
612
613 static void get_pauseparam(struct net_device *dev,
614                            struct ethtool_pauseparam *epause)
615 {
616         struct port_info *p = netdev_priv(dev);
617
618         epause->autoneg = (p->link_cfg.requested_fc & PAUSE_AUTONEG) != 0;
619         epause->rx_pause = (p->link_cfg.fc & PAUSE_RX) != 0;
620         epause->tx_pause = (p->link_cfg.fc & PAUSE_TX) != 0;
621 }
622
623 static int set_pauseparam(struct net_device *dev,
624                           struct ethtool_pauseparam *epause)
625 {
626         struct port_info *p = netdev_priv(dev);
627         struct link_config *lc = &p->link_cfg;
628
629         if (epause->autoneg == AUTONEG_DISABLE)
630                 lc->requested_fc = 0;
631         else if (lc->supported & FW_PORT_CAP_ANEG)
632                 lc->requested_fc = PAUSE_AUTONEG;
633         else
634                 return -EINVAL;
635
636         if (epause->rx_pause)
637                 lc->requested_fc |= PAUSE_RX;
638         if (epause->tx_pause)
639                 lc->requested_fc |= PAUSE_TX;
640         if (netif_running(dev))
641                 return t4_link_start(p->adapter, p->adapter->pf, p->tx_chan,
642                                      lc);
643         return 0;
644 }
645
646 static void get_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
647 {
648         const struct port_info *pi = netdev_priv(dev);
649         const struct sge *s = &pi->adapter->sge;
650
651         e->rx_max_pending = MAX_RX_BUFFERS;
652         e->rx_mini_max_pending = MAX_RSPQ_ENTRIES;
653         e->rx_jumbo_max_pending = 0;
654         e->tx_max_pending = MAX_TXQ_ENTRIES;
655
656         e->rx_pending = s->ethrxq[pi->first_qset].fl.size - 8;
657         e->rx_mini_pending = s->ethrxq[pi->first_qset].rspq.size;
658         e->rx_jumbo_pending = 0;
659         e->tx_pending = s->ethtxq[pi->first_qset].q.size;
660 }
661
662 static int set_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
663 {
664         int i;
665         const struct port_info *pi = netdev_priv(dev);
666         struct adapter *adapter = pi->adapter;
667         struct sge *s = &adapter->sge;
668
669         if (e->rx_pending > MAX_RX_BUFFERS || e->rx_jumbo_pending ||
670             e->tx_pending > MAX_TXQ_ENTRIES ||
671             e->rx_mini_pending > MAX_RSPQ_ENTRIES ||
672             e->rx_mini_pending < MIN_RSPQ_ENTRIES ||
673             e->rx_pending < MIN_FL_ENTRIES || e->tx_pending < MIN_TXQ_ENTRIES)
674                 return -EINVAL;
675
676         if (adapter->flags & FULL_INIT_DONE)
677                 return -EBUSY;
678
679         for (i = 0; i < pi->nqsets; ++i) {
680                 s->ethtxq[pi->first_qset + i].q.size = e->tx_pending;
681                 s->ethrxq[pi->first_qset + i].fl.size = e->rx_pending + 8;
682                 s->ethrxq[pi->first_qset + i].rspq.size = e->rx_mini_pending;
683         }
684         return 0;
685 }
686
687 /**
688  * set_rx_intr_params - set a net devices's RX interrupt holdoff paramete!
689  * @dev: the network device
690  * @us: the hold-off time in us, or 0 to disable timer
691  * @cnt: the hold-off packet count, or 0 to disable counter
692  *
693  * Set the RX interrupt hold-off parameters for a network device.
694  */
695 static int set_rx_intr_params(struct net_device *dev,
696                               unsigned int us, unsigned int cnt)
697 {
698         int i, err;
699         struct port_info *pi = netdev_priv(dev);
700         struct adapter *adap = pi->adapter;
701         struct sge_eth_rxq *q = &adap->sge.ethrxq[pi->first_qset];
702
703         for (i = 0; i < pi->nqsets; i++, q++) {
704                 err = cxgb4_set_rspq_intr_params(&q->rspq, us, cnt);
705                 if (err)
706                         return err;
707         }
708         return 0;
709 }
710
711 static int set_adaptive_rx_setting(struct net_device *dev, int adaptive_rx)
712 {
713         int i;
714         struct port_info *pi = netdev_priv(dev);
715         struct adapter *adap = pi->adapter;
716         struct sge_eth_rxq *q = &adap->sge.ethrxq[pi->first_qset];
717
718         for (i = 0; i < pi->nqsets; i++, q++)
719                 q->rspq.adaptive_rx = adaptive_rx;
720
721         return 0;
722 }
723
724 static int get_adaptive_rx_setting(struct net_device *dev)
725 {
726         struct port_info *pi = netdev_priv(dev);
727         struct adapter *adap = pi->adapter;
728         struct sge_eth_rxq *q = &adap->sge.ethrxq[pi->first_qset];
729
730         return q->rspq.adaptive_rx;
731 }
732
733 static int set_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
734 {
735         set_adaptive_rx_setting(dev, c->use_adaptive_rx_coalesce);
736         return set_rx_intr_params(dev, c->rx_coalesce_usecs,
737                                   c->rx_max_coalesced_frames);
738 }
739
740 static int get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
741 {
742         const struct port_info *pi = netdev_priv(dev);
743         const struct adapter *adap = pi->adapter;
744         const struct sge_rspq *rq = &adap->sge.ethrxq[pi->first_qset].rspq;
745
746         c->rx_coalesce_usecs = qtimer_val(adap, rq);
747         c->rx_max_coalesced_frames = (rq->intr_params & QINTR_CNT_EN_F) ?
748                 adap->sge.counter_val[rq->pktcnt_idx] : 0;
749         c->use_adaptive_rx_coalesce = get_adaptive_rx_setting(dev);
750         return 0;
751 }
752
753 /**
754  *      eeprom_ptov - translate a physical EEPROM address to virtual
755  *      @phys_addr: the physical EEPROM address
756  *      @fn: the PCI function number
757  *      @sz: size of function-specific area
758  *
759  *      Translate a physical EEPROM address to virtual.  The first 1K is
760  *      accessed through virtual addresses starting at 31K, the rest is
761  *      accessed through virtual addresses starting at 0.
762  *
763  *      The mapping is as follows:
764  *      [0..1K) -> [31K..32K)
765  *      [1K..1K+A) -> [31K-A..31K)
766  *      [1K+A..ES) -> [0..ES-A-1K)
767  *
768  *      where A = @fn * @sz, and ES = EEPROM size.
769  */
770 static int eeprom_ptov(unsigned int phys_addr, unsigned int fn, unsigned int sz)
771 {
772         fn *= sz;
773         if (phys_addr < 1024)
774                 return phys_addr + (31 << 10);
775         if (phys_addr < 1024 + fn)
776                 return 31744 - fn + phys_addr - 1024;
777         if (phys_addr < EEPROMSIZE)
778                 return phys_addr - 1024 - fn;
779         return -EINVAL;
780 }
781
782 /* The next two routines implement eeprom read/write from physical addresses.
783  */
784 static int eeprom_rd_phys(struct adapter *adap, unsigned int phys_addr, u32 *v)
785 {
786         int vaddr = eeprom_ptov(phys_addr, adap->pf, EEPROMPFSIZE);
787
788         if (vaddr >= 0)
789                 vaddr = pci_read_vpd(adap->pdev, vaddr, sizeof(u32), v);
790         return vaddr < 0 ? vaddr : 0;
791 }
792
793 static int eeprom_wr_phys(struct adapter *adap, unsigned int phys_addr, u32 v)
794 {
795         int vaddr = eeprom_ptov(phys_addr, adap->pf, EEPROMPFSIZE);
796
797         if (vaddr >= 0)
798                 vaddr = pci_write_vpd(adap->pdev, vaddr, sizeof(u32), &v);
799         return vaddr < 0 ? vaddr : 0;
800 }
801
802 #define EEPROM_MAGIC 0x38E2F10C
803
804 static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
805                       u8 *data)
806 {
807         int i, err = 0;
808         struct adapter *adapter = netdev2adap(dev);
809         u8 *buf = kmalloc(EEPROMSIZE, GFP_KERNEL);
810
811         if (!buf)
812                 return -ENOMEM;
813
814         e->magic = EEPROM_MAGIC;
815         for (i = e->offset & ~3; !err && i < e->offset + e->len; i += 4)
816                 err = eeprom_rd_phys(adapter, i, (u32 *)&buf[i]);
817
818         if (!err)
819                 memcpy(data, buf + e->offset, e->len);
820         kfree(buf);
821         return err;
822 }
823
824 static int set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
825                       u8 *data)
826 {
827         u8 *buf;
828         int err = 0;
829         u32 aligned_offset, aligned_len, *p;
830         struct adapter *adapter = netdev2adap(dev);
831
832         if (eeprom->magic != EEPROM_MAGIC)
833                 return -EINVAL;
834
835         aligned_offset = eeprom->offset & ~3;
836         aligned_len = (eeprom->len + (eeprom->offset & 3) + 3) & ~3;
837
838         if (adapter->pf > 0) {
839                 u32 start = 1024 + adapter->pf * EEPROMPFSIZE;
840
841                 if (aligned_offset < start ||
842                     aligned_offset + aligned_len > start + EEPROMPFSIZE)
843                         return -EPERM;
844         }
845
846         if (aligned_offset != eeprom->offset || aligned_len != eeprom->len) {
847                 /* RMW possibly needed for first or last words.
848                  */
849                 buf = kmalloc(aligned_len, GFP_KERNEL);
850                 if (!buf)
851                         return -ENOMEM;
852                 err = eeprom_rd_phys(adapter, aligned_offset, (u32 *)buf);
853                 if (!err && aligned_len > 4)
854                         err = eeprom_rd_phys(adapter,
855                                              aligned_offset + aligned_len - 4,
856                                              (u32 *)&buf[aligned_len - 4]);
857                 if (err)
858                         goto out;
859                 memcpy(buf + (eeprom->offset & 3), data, eeprom->len);
860         } else {
861                 buf = data;
862         }
863
864         err = t4_seeprom_wp(adapter, false);
865         if (err)
866                 goto out;
867
868         for (p = (u32 *)buf; !err && aligned_len; aligned_len -= 4, p++) {
869                 err = eeprom_wr_phys(adapter, aligned_offset, *p);
870                 aligned_offset += 4;
871         }
872
873         if (!err)
874                 err = t4_seeprom_wp(adapter, true);
875 out:
876         if (buf != data)
877                 kfree(buf);
878         return err;
879 }
880
881 static int set_flash(struct net_device *netdev, struct ethtool_flash *ef)
882 {
883         int ret;
884         const struct firmware *fw;
885         struct adapter *adap = netdev2adap(netdev);
886         unsigned int mbox = PCIE_FW_MASTER_M + 1;
887
888         ef->data[sizeof(ef->data) - 1] = '\0';
889         ret = request_firmware(&fw, ef->data, adap->pdev_dev);
890         if (ret < 0)
891                 return ret;
892
893         /* If the adapter has been fully initialized then we'll go ahead and
894          * try to get the firmware's cooperation in upgrading to the new
895          * firmware image otherwise we'll try to do the entire job from the
896          * host ... and we always "force" the operation in this path.
897          */
898         if (adap->flags & FULL_INIT_DONE)
899                 mbox = adap->mbox;
900
901         ret = t4_fw_upgrade(adap, mbox, fw->data, fw->size, 1);
902         release_firmware(fw);
903         if (!ret)
904                 dev_info(adap->pdev_dev,
905                          "loaded firmware %s, reload cxgb4 driver\n", ef->data);
906         return ret;
907 }
908
909 #define WOL_SUPPORTED (WAKE_BCAST | WAKE_MAGIC)
910 #define BCAST_CRC 0xa0ccc1a6
911
912 static void get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
913 {
914         wol->supported = WAKE_BCAST | WAKE_MAGIC;
915         wol->wolopts = netdev2adap(dev)->wol;
916         memset(&wol->sopass, 0, sizeof(wol->sopass));
917 }
918
919 static int set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
920 {
921         int err = 0;
922         struct port_info *pi = netdev_priv(dev);
923
924         if (wol->wolopts & ~WOL_SUPPORTED)
925                 return -EINVAL;
926         t4_wol_magic_enable(pi->adapter, pi->tx_chan,
927                             (wol->wolopts & WAKE_MAGIC) ? dev->dev_addr : NULL);
928         if (wol->wolopts & WAKE_BCAST) {
929                 err = t4_wol_pat_enable(pi->adapter, pi->tx_chan, 0xfe, ~0ULL,
930                                         ~0ULL, 0, false);
931                 if (!err)
932                         err = t4_wol_pat_enable(pi->adapter, pi->tx_chan, 1,
933                                                 ~6ULL, ~0ULL, BCAST_CRC, true);
934         } else {
935                 t4_wol_pat_enable(pi->adapter, pi->tx_chan, 0, 0, 0, 0, false);
936         }
937         return err;
938 }
939
940 static u32 get_rss_table_size(struct net_device *dev)
941 {
942         const struct port_info *pi = netdev_priv(dev);
943
944         return pi->rss_size;
945 }
946
947 static int get_rss_table(struct net_device *dev, u32 *p, u8 *key, u8 *hfunc)
948 {
949         const struct port_info *pi = netdev_priv(dev);
950         unsigned int n = pi->rss_size;
951
952         if (hfunc)
953                 *hfunc = ETH_RSS_HASH_TOP;
954         if (!p)
955                 return 0;
956         while (n--)
957                 p[n] = pi->rss[n];
958         return 0;
959 }
960
961 static int set_rss_table(struct net_device *dev, const u32 *p, const u8 *key,
962                          const u8 hfunc)
963 {
964         unsigned int i;
965         struct port_info *pi = netdev_priv(dev);
966
967         /* We require at least one supported parameter to be changed and no
968          * change in any of the unsupported parameters
969          */
970         if (key ||
971             (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
972                 return -EOPNOTSUPP;
973         if (!p)
974                 return 0;
975
976         for (i = 0; i < pi->rss_size; i++)
977                 pi->rss[i] = p[i];
978         if (pi->adapter->flags & FULL_INIT_DONE)
979                 return cxgb4_write_rss(pi, pi->rss);
980         return 0;
981 }
982
983 static int get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
984                      u32 *rules)
985 {
986         const struct port_info *pi = netdev_priv(dev);
987
988         switch (info->cmd) {
989         case ETHTOOL_GRXFH: {
990                 unsigned int v = pi->rss_mode;
991
992                 info->data = 0;
993                 switch (info->flow_type) {
994                 case TCP_V4_FLOW:
995                         if (v & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F)
996                                 info->data = RXH_IP_SRC | RXH_IP_DST |
997                                              RXH_L4_B_0_1 | RXH_L4_B_2_3;
998                         else if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F)
999                                 info->data = RXH_IP_SRC | RXH_IP_DST;
1000                         break;
1001                 case UDP_V4_FLOW:
1002                         if ((v & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F) &&
1003                             (v & FW_RSS_VI_CONFIG_CMD_UDPEN_F))
1004                                 info->data = RXH_IP_SRC | RXH_IP_DST |
1005                                              RXH_L4_B_0_1 | RXH_L4_B_2_3;
1006                         else if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F)
1007                                 info->data = RXH_IP_SRC | RXH_IP_DST;
1008                         break;
1009                 case SCTP_V4_FLOW:
1010                 case AH_ESP_V4_FLOW:
1011                 case IPV4_FLOW:
1012                         if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F)
1013                                 info->data = RXH_IP_SRC | RXH_IP_DST;
1014                         break;
1015                 case TCP_V6_FLOW:
1016                         if (v & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F)
1017                                 info->data = RXH_IP_SRC | RXH_IP_DST |
1018                                              RXH_L4_B_0_1 | RXH_L4_B_2_3;
1019                         else if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F)
1020                                 info->data = RXH_IP_SRC | RXH_IP_DST;
1021                         break;
1022                 case UDP_V6_FLOW:
1023                         if ((v & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F) &&
1024                             (v & FW_RSS_VI_CONFIG_CMD_UDPEN_F))
1025                                 info->data = RXH_IP_SRC | RXH_IP_DST |
1026                                              RXH_L4_B_0_1 | RXH_L4_B_2_3;
1027                         else if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F)
1028                                 info->data = RXH_IP_SRC | RXH_IP_DST;
1029                         break;
1030                 case SCTP_V6_FLOW:
1031                 case AH_ESP_V6_FLOW:
1032                 case IPV6_FLOW:
1033                         if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F)
1034                                 info->data = RXH_IP_SRC | RXH_IP_DST;
1035                         break;
1036                 }
1037                 return 0;
1038         }
1039         case ETHTOOL_GRXRINGS:
1040                 info->data = pi->nqsets;
1041                 return 0;
1042         }
1043         return -EOPNOTSUPP;
1044 }
1045
1046 static const struct ethtool_ops cxgb_ethtool_ops = {
1047         .get_settings      = get_settings,
1048         .set_settings      = set_settings,
1049         .get_drvinfo       = get_drvinfo,
1050         .get_msglevel      = get_msglevel,
1051         .set_msglevel      = set_msglevel,
1052         .get_ringparam     = get_sge_param,
1053         .set_ringparam     = set_sge_param,
1054         .get_coalesce      = get_coalesce,
1055         .set_coalesce      = set_coalesce,
1056         .get_eeprom_len    = get_eeprom_len,
1057         .get_eeprom        = get_eeprom,
1058         .set_eeprom        = set_eeprom,
1059         .get_pauseparam    = get_pauseparam,
1060         .set_pauseparam    = set_pauseparam,
1061         .get_link          = ethtool_op_get_link,
1062         .get_strings       = get_strings,
1063         .set_phys_id       = identify_port,
1064         .nway_reset        = restart_autoneg,
1065         .get_sset_count    = get_sset_count,
1066         .get_ethtool_stats = get_stats,
1067         .get_regs_len      = get_regs_len,
1068         .get_regs          = get_regs,
1069         .get_wol           = get_wol,
1070         .set_wol           = set_wol,
1071         .get_rxnfc         = get_rxnfc,
1072         .get_rxfh_indir_size = get_rss_table_size,
1073         .get_rxfh          = get_rss_table,
1074         .set_rxfh          = set_rss_table,
1075         .flash_device      = set_flash,
1076 };
1077
1078 void cxgb4_set_ethtool_ops(struct net_device *netdev)
1079 {
1080         netdev->ethtool_ops = &cxgb_ethtool_ops;
1081 }