1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #include <linux/kernel.h>
36 #include <linux/netdevice.h>
37 
38 #include "ipoib.h"
39 
40 static void ipoib_get_drvinfo(struct ifnet *netdev,
41 			      struct ethtool_drvinfo *drvinfo)
42 {
43 	strncpy(drvinfo->driver, "ipoib", sizeof(drvinfo->driver) - 1);
44 }
45 
46 static u32 ipoib_get_rx_csum(struct ifnet *dev)
47 {
48 	struct ipoib_dev_priv *priv = dev->if_softc;
49 	return test_bit(IPOIB_FLAG_CSUM, &priv->flags) &&
50 		!test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
51 }
52 
53 static int ipoib_get_coalesce(struct ifnet *dev,
54 			      struct ethtool_coalesce *coal)
55 {
56 	struct ipoib_dev_priv *priv = dev->if_softc;
57 
58 	coal->rx_coalesce_usecs = priv->ethtool.coalesce_usecs;
59 	coal->tx_coalesce_usecs = priv->ethtool.coalesce_usecs;
60 	coal->rx_max_coalesced_frames = priv->ethtool.max_coalesced_frames;
61 	coal->tx_max_coalesced_frames = priv->ethtool.max_coalesced_frames;
62 
63 	return 0;
64 }
65 
66 static int ipoib_set_coalesce(struct ifnet *dev,
67 			      struct ethtool_coalesce *coal)
68 {
69 	struct ipoib_dev_priv *priv = dev->if_softc;
70 	int ret;
71 
72 	/*
73 	 * Since IPoIB uses a single CQ for both rx and tx, we assume
74 	 * that rx params dictate the configuration.  These values are
75 	 * saved in the private data and returned when ipoib_get_coalesce()
76 	 * is called.
77 	 */
78 	if (coal->rx_coalesce_usecs       > 0xffff ||
79 	    coal->rx_max_coalesced_frames > 0xffff)
80 		return -EINVAL;
81 
82 	if (coal->rx_max_coalesced_frames | coal->rx_coalesce_usecs) {
83 		if (!coal->rx_max_coalesced_frames)
84 			coal->rx_max_coalesced_frames = 0xffff;
85 		else if (!coal->rx_coalesce_usecs)
86 			coal->rx_coalesce_usecs = 0xffff;
87 	}
88 
89 	ret = ib_modify_cq(priv->recv_cq, coal->rx_max_coalesced_frames,
90 			   coal->rx_coalesce_usecs);
91 	if (ret && ret != -ENOSYS) {
92 		ipoib_warn(priv, "failed modifying CQ (%d)\n", ret);
93 		return ret;
94 	}
95 
96 	coal->tx_coalesce_usecs       = coal->rx_coalesce_usecs;
97 	coal->tx_max_coalesced_frames = coal->rx_max_coalesced_frames;
98 	priv->ethtool.coalesce_usecs       = coal->rx_coalesce_usecs;
99 	priv->ethtool.max_coalesced_frames = coal->rx_max_coalesced_frames;
100 
101 	return 0;
102 }
103 
104 static const char ipoib_stats_keys[][ETH_GSTRING_LEN] = {
105 	"LRO aggregated", "LRO flushed",
106 	"LRO avg aggr", "LRO no desc"
107 };
108 
109 static void ipoib_get_strings(struct ifnet *netdev, u32 stringset, u8 *data)
110 {
111 	switch (stringset) {
112 	case ETH_SS_STATS:
113 		memcpy(data, *ipoib_stats_keys,	sizeof(ipoib_stats_keys));
114 		break;
115 	}
116 }
117 
118 static int ipoib_get_sset_count(struct ifnet *dev, int sset)
119 {
120 	switch (sset) {
121 	case ETH_SS_STATS:
122 		return ARRAY_SIZE(ipoib_stats_keys);
123 	default:
124 		return -EOPNOTSUPP;
125 	}
126 }
127 
128 static void ipoib_get_ethtool_stats(struct ifnet *dev,
129 				struct ethtool_stats *stats, uint64_t *data)
130 {
131 	struct ipoib_dev_priv *priv = dev->if_softc;
132 	int index = 0;
133 
134 	/* Get LRO statistics */
135 	data[index++] = priv->lro.lro_mgr.stats.aggregated;
136 	data[index++] = priv->lro.lro_mgr.stats.flushed;
137 	if (priv->lro.lro_mgr.stats.flushed)
138 		data[index++] = priv->lro.lro_mgr.stats.aggregated /
139 				priv->lro.lro_mgr.stats.flushed;
140 	else
141 		data[index++] = 0;
142 	data[index++] = priv->lro.lro_mgr.stats.no_desc;
143 }
144 
145 static const struct ethtool_ops ipoib_ethtool_ops = {
146 	.get_drvinfo		= ipoib_get_drvinfo,
147 	.get_rx_csum		= ipoib_get_rx_csum,
148 	.get_coalesce		= ipoib_get_coalesce,
149 	.set_coalesce		= ipoib_set_coalesce,
150 	.get_flags		= ethtool_op_get_flags,
151 	.set_flags		= ethtool_op_set_flags,
152 	.get_strings		= ipoib_get_strings,
153 	.get_sset_count		= ipoib_get_sset_count,
154 	.get_ethtool_stats	= ipoib_get_ethtool_stats,
155 };
156 
157 void ipoib_set_ethtool_ops(struct ifnet *dev)
158 {
159 	SET_ETHTOOL_OPS(dev, &ipoib_ethtool_ops);
160 }
161