xref: /freebsd/sys/ofed/drivers/infiniband/ulp/ipoib/ipoib_ethtool.c (revision 6a75471dbcf0fb187cf7918ee1a918fd7c9d002d)
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 <sys/cdefs.h>
36 #include <linux/kernel.h>
37 #include <linux/netdevice.h>
38 
39 #include "ipoib.h"
40 
ipoib_get_drvinfo(if_t netdev,struct ethtool_drvinfo * drvinfo)41 static void ipoib_get_drvinfo(if_t netdev,
42 			      struct ethtool_drvinfo *drvinfo)
43 {
44 	strncpy(drvinfo->driver, "ipoib", sizeof(drvinfo->driver) - 1);
45 }
46 
ipoib_get_rx_csum(if_t dev)47 static u32 ipoib_get_rx_csum(if_t dev)
48 {
49 	struct ipoib_dev_priv *priv = dev->if_softc;
50 	return test_bit(IPOIB_FLAG_CSUM, &priv->flags) &&
51 		!test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
52 }
53 
ipoib_get_coalesce(if_t dev,struct ethtool_coalesce * coal)54 static int ipoib_get_coalesce(if_t dev,
55 			      struct ethtool_coalesce *coal)
56 {
57 	struct ipoib_dev_priv *priv = dev->if_softc;
58 
59 	coal->rx_coalesce_usecs = priv->ethtool.coalesce_usecs;
60 	coal->tx_coalesce_usecs = priv->ethtool.coalesce_usecs;
61 	coal->rx_max_coalesced_frames = priv->ethtool.max_coalesced_frames;
62 	coal->tx_max_coalesced_frames = priv->ethtool.max_coalesced_frames;
63 
64 	return 0;
65 }
66 
ipoib_set_coalesce(if_t dev,struct ethtool_coalesce * coal)67 static int ipoib_set_coalesce(if_t dev,
68 			      struct ethtool_coalesce *coal)
69 {
70 	struct ipoib_dev_priv *priv = dev->if_softc;
71 	int ret;
72 
73 	/*
74 	 * Since IPoIB uses a single CQ for both rx and tx, we assume
75 	 * that rx params dictate the configuration.  These values are
76 	 * saved in the private data and returned when ipoib_get_coalesce()
77 	 * is called.
78 	 */
79 	if (coal->rx_coalesce_usecs       > 0xffff ||
80 	    coal->rx_max_coalesced_frames > 0xffff)
81 		return -EINVAL;
82 
83 	if (coal->rx_max_coalesced_frames | coal->rx_coalesce_usecs) {
84 		if (!coal->rx_max_coalesced_frames)
85 			coal->rx_max_coalesced_frames = 0xffff;
86 		else if (!coal->rx_coalesce_usecs)
87 			coal->rx_coalesce_usecs = 0xffff;
88 	}
89 
90 	ret = rdma_set_cq_moderation(priv->recv_cq,
91 				     coal->rx_max_coalesced_frames,
92 				     coal->rx_coalesce_usecs);
93 	if (ret && ret != -EOPNOTSUPP) {
94 		ipoib_warn(priv, "failed modifying CQ (%d)\n", ret);
95 		return ret;
96 	}
97 
98 	coal->tx_coalesce_usecs       = coal->rx_coalesce_usecs;
99 	coal->tx_max_coalesced_frames = coal->rx_max_coalesced_frames;
100 	priv->ethtool.coalesce_usecs       = coal->rx_coalesce_usecs;
101 	priv->ethtool.max_coalesced_frames = coal->rx_max_coalesced_frames;
102 
103 	return 0;
104 }
105 
106 static const char ipoib_stats_keys[][ETH_GSTRING_LEN] = {
107 	"LRO aggregated", "LRO flushed",
108 	"LRO avg aggr", "LRO no desc"
109 };
110 
ipoib_get_strings(if_t netdev,u32 stringset,u8 * data)111 static void ipoib_get_strings(if_t netdev, u32 stringset, u8 *data)
112 {
113 	switch (stringset) {
114 	case ETH_SS_STATS:
115 		memcpy(data, *ipoib_stats_keys,	sizeof(ipoib_stats_keys));
116 		break;
117 	}
118 }
119 
ipoib_get_sset_count(if_t dev,int sset)120 static int ipoib_get_sset_count(if_t dev, int sset)
121 {
122 	switch (sset) {
123 	case ETH_SS_STATS:
124 		return ARRAY_SIZE(ipoib_stats_keys);
125 	default:
126 		return -EOPNOTSUPP;
127 	}
128 }
129 
ipoib_get_ethtool_stats(if_t dev,struct ethtool_stats * stats,uint64_t * data)130 static void ipoib_get_ethtool_stats(if_t dev,
131 				struct ethtool_stats *stats, uint64_t *data)
132 {
133 	struct ipoib_dev_priv *priv = dev->if_softc;
134 	int index = 0;
135 
136 	/* Get LRO statistics */
137 	data[index++] = priv->lro.lro_mgr.stats.aggregated;
138 	data[index++] = priv->lro.lro_mgr.stats.flushed;
139 	if (priv->lro.lro_mgr.stats.flushed)
140 		data[index++] = priv->lro.lro_mgr.stats.aggregated /
141 				priv->lro.lro_mgr.stats.flushed;
142 	else
143 		data[index++] = 0;
144 	data[index++] = priv->lro.lro_mgr.stats.no_desc;
145 }
146 
147 static const struct ethtool_ops ipoib_ethtool_ops = {
148 	.get_drvinfo		= ipoib_get_drvinfo,
149 	.get_rx_csum		= ipoib_get_rx_csum,
150 	.get_coalesce		= ipoib_get_coalesce,
151 	.set_coalesce		= ipoib_set_coalesce,
152 	.get_flags		= ethtool_op_get_flags,
153 	.set_flags		= ethtool_op_set_flags,
154 	.get_strings		= ipoib_get_strings,
155 	.get_sset_count		= ipoib_get_sset_count,
156 	.get_ethtool_stats	= ipoib_get_ethtool_stats,
157 };
158 
ipoib_set_ethtool_ops(if_t dev)159 void ipoib_set_ethtool_ops(if_t dev)
160 {
161 	SET_ETHTOOL_OPS(dev, &ipoib_ethtool_ops);
162 }
163