1219820Sjeff/*
2219820Sjeff * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *        copyright notice, this list of conditions and the following
16219820Sjeff *        disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer in the documentation and/or other materials
21219820Sjeff *        provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31219820Sjeff */
32219820Sjeff
33219820Sjeff#include <linux/kernel.h>
34219820Sjeff#include <linux/netdevice.h>
35219820Sjeff
36219820Sjeff#include "ipoib.h"
37219820Sjeff
38219820Sjeffstatic void ipoib_get_drvinfo(struct ifnet *netdev,
39219820Sjeff			      struct ethtool_drvinfo *drvinfo)
40219820Sjeff{
41219820Sjeff	strncpy(drvinfo->driver, "ipoib", sizeof(drvinfo->driver) - 1);
42219820Sjeff}
43219820Sjeff
44219820Sjeffstatic u32 ipoib_get_rx_csum(struct ifnet *dev)
45219820Sjeff{
46219820Sjeff	struct ipoib_dev_priv *priv = dev->if_softc;
47219820Sjeff	return test_bit(IPOIB_FLAG_CSUM, &priv->flags) &&
48219820Sjeff		!test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
49219820Sjeff}
50219820Sjeff
51219820Sjeffstatic int ipoib_get_coalesce(struct ifnet *dev,
52219820Sjeff			      struct ethtool_coalesce *coal)
53219820Sjeff{
54219820Sjeff	struct ipoib_dev_priv *priv = dev->if_softc;
55219820Sjeff
56219820Sjeff	coal->rx_coalesce_usecs = priv->ethtool.coalesce_usecs;
57219820Sjeff	coal->tx_coalesce_usecs = priv->ethtool.coalesce_usecs;
58219820Sjeff	coal->rx_max_coalesced_frames = priv->ethtool.max_coalesced_frames;
59219820Sjeff	coal->tx_max_coalesced_frames = priv->ethtool.max_coalesced_frames;
60219820Sjeff
61219820Sjeff	return 0;
62219820Sjeff}
63219820Sjeff
64219820Sjeffstatic int ipoib_set_coalesce(struct ifnet *dev,
65219820Sjeff			      struct ethtool_coalesce *coal)
66219820Sjeff{
67219820Sjeff	struct ipoib_dev_priv *priv = dev->if_softc;
68219820Sjeff	int ret;
69219820Sjeff
70219820Sjeff	/*
71219820Sjeff	 * Since IPoIB uses a single CQ for both rx and tx, we assume
72219820Sjeff	 * that rx params dictate the configuration.  These values are
73219820Sjeff	 * saved in the private data and returned when ipoib_get_coalesce()
74219820Sjeff	 * is called.
75219820Sjeff	 */
76219820Sjeff	if (coal->rx_coalesce_usecs       > 0xffff ||
77219820Sjeff	    coal->rx_max_coalesced_frames > 0xffff)
78219820Sjeff		return -EINVAL;
79219820Sjeff
80219820Sjeff	if (coal->rx_max_coalesced_frames | coal->rx_coalesce_usecs) {
81219820Sjeff		if (!coal->rx_max_coalesced_frames)
82219820Sjeff			coal->rx_max_coalesced_frames = 0xffff;
83219820Sjeff		else if (!coal->rx_coalesce_usecs)
84219820Sjeff			coal->rx_coalesce_usecs = 0xffff;
85219820Sjeff	}
86219820Sjeff
87219820Sjeff	ret = ib_modify_cq(priv->recv_cq, coal->rx_max_coalesced_frames,
88219820Sjeff			   coal->rx_coalesce_usecs);
89219820Sjeff	if (ret && ret != -ENOSYS) {
90219820Sjeff		ipoib_warn(priv, "failed modifying CQ (%d)\n", ret);
91219820Sjeff		return ret;
92219820Sjeff	}
93219820Sjeff
94219820Sjeff	coal->tx_coalesce_usecs       = coal->rx_coalesce_usecs;
95219820Sjeff	coal->tx_max_coalesced_frames = coal->rx_max_coalesced_frames;
96219820Sjeff	priv->ethtool.coalesce_usecs       = coal->rx_coalesce_usecs;
97219820Sjeff	priv->ethtool.max_coalesced_frames = coal->rx_max_coalesced_frames;
98219820Sjeff
99219820Sjeff	return 0;
100219820Sjeff}
101219820Sjeff
102219820Sjeffstatic const char ipoib_stats_keys[][ETH_GSTRING_LEN] = {
103219820Sjeff	"LRO aggregated", "LRO flushed",
104219820Sjeff	"LRO avg aggr", "LRO no desc"
105219820Sjeff};
106219820Sjeff
107219820Sjeffstatic void ipoib_get_strings(struct ifnet *netdev, u32 stringset, u8 *data)
108219820Sjeff{
109219820Sjeff	switch (stringset) {
110219820Sjeff	case ETH_SS_STATS:
111219820Sjeff		memcpy(data, *ipoib_stats_keys,	sizeof(ipoib_stats_keys));
112219820Sjeff		break;
113219820Sjeff	}
114219820Sjeff}
115219820Sjeff
116219820Sjeffstatic int ipoib_get_sset_count(struct ifnet *dev, int sset)
117219820Sjeff{
118219820Sjeff	switch (sset) {
119219820Sjeff	case ETH_SS_STATS:
120219820Sjeff		return ARRAY_SIZE(ipoib_stats_keys);
121219820Sjeff	default:
122219820Sjeff		return -EOPNOTSUPP;
123219820Sjeff	}
124219820Sjeff}
125219820Sjeff
126219820Sjeffstatic void ipoib_get_ethtool_stats(struct ifnet *dev,
127219820Sjeff				struct ethtool_stats *stats, uint64_t *data)
128219820Sjeff{
129219820Sjeff	struct ipoib_dev_priv *priv = dev->if_softc;
130219820Sjeff	int index = 0;
131219820Sjeff
132219820Sjeff	/* Get LRO statistics */
133219820Sjeff	data[index++] = priv->lro.lro_mgr.stats.aggregated;
134219820Sjeff	data[index++] = priv->lro.lro_mgr.stats.flushed;
135219820Sjeff	if (priv->lro.lro_mgr.stats.flushed)
136219820Sjeff		data[index++] = priv->lro.lro_mgr.stats.aggregated /
137219820Sjeff				priv->lro.lro_mgr.stats.flushed;
138219820Sjeff	else
139219820Sjeff		data[index++] = 0;
140219820Sjeff	data[index++] = priv->lro.lro_mgr.stats.no_desc;
141219820Sjeff}
142219820Sjeff
143219820Sjeffstatic const struct ethtool_ops ipoib_ethtool_ops = {
144219820Sjeff	.get_drvinfo		= ipoib_get_drvinfo,
145219820Sjeff	.get_rx_csum		= ipoib_get_rx_csum,
146219820Sjeff	.get_coalesce		= ipoib_get_coalesce,
147219820Sjeff	.set_coalesce		= ipoib_set_coalesce,
148219820Sjeff	.get_flags		= ethtool_op_get_flags,
149219820Sjeff	.set_flags		= ethtool_op_set_flags,
150219820Sjeff	.get_strings		= ipoib_get_strings,
151219820Sjeff	.get_sset_count		= ipoib_get_sset_count,
152219820Sjeff	.get_ethtool_stats	= ipoib_get_ethtool_stats,
153219820Sjeff};
154219820Sjeff
155219820Sjeffvoid ipoib_set_ethtool_ops(struct ifnet *dev)
156219820Sjeff{
157219820Sjeff	SET_ETHTOOL_OPS(dev, &ipoib_ethtool_ops);
158219820Sjeff}
159