1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021 Gerhard Engleder <gerhard@engleder-embedded.com> */
6 static const char tsnep_stats_strings[][ETH_GSTRING_LEN] = {
13 "rx_forwarded_phy_errors",
14 "rx_invalid_frame_errors",
27 u64 rx_forwarded_phy_errors;
28 u64 rx_invalid_frame_errors;
34 #define TSNEP_STATS_COUNT (sizeof(struct tsnep_stats) / sizeof(u64))
36 static const char tsnep_rx_queue_stats_strings[][ETH_GSTRING_LEN] = {
42 "rx_%d_no_descriptor_errors",
43 "rx_%d_buffer_too_small_errors",
44 "rx_%d_fifo_overflow_errors",
45 "rx_%d_invalid_frame_errors",
48 struct tsnep_rx_queue_stats {
54 u64 rx_no_descriptor_errors;
55 u64 rx_buffer_too_small_errors;
56 u64 rx_fifo_overflow_errors;
57 u64 rx_invalid_frame_errors;
60 #define TSNEP_RX_QUEUE_STATS_COUNT (sizeof(struct tsnep_rx_queue_stats) / \
63 static const char tsnep_tx_queue_stats_strings[][ETH_GSTRING_LEN] = {
69 struct tsnep_tx_queue_stats {
75 #define TSNEP_TX_QUEUE_STATS_COUNT (sizeof(struct tsnep_tx_queue_stats) / \
78 static void tsnep_ethtool_get_drvinfo(struct net_device *netdev,
79 struct ethtool_drvinfo *drvinfo)
81 struct tsnep_adapter *adapter = netdev_priv(netdev);
83 strscpy(drvinfo->driver, TSNEP, sizeof(drvinfo->driver));
84 strscpy(drvinfo->bus_info, dev_name(&adapter->pdev->dev),
85 sizeof(drvinfo->bus_info));
88 static int tsnep_ethtool_get_regs_len(struct net_device *netdev)
90 struct tsnep_adapter *adapter = netdev_priv(netdev);
92 int num_additional_queues;
96 /* first queue pair is within TSNEP_MAC_SIZE, only queues additional to
97 * the first queue pair extend the register length by TSNEP_QUEUE_SIZE
99 num_additional_queues =
100 max(adapter->num_tx_queues, adapter->num_rx_queues) - 1;
101 len += TSNEP_QUEUE_SIZE * num_additional_queues;
106 static void tsnep_ethtool_get_regs(struct net_device *netdev,
107 struct ethtool_regs *regs,
110 struct tsnep_adapter *adapter = netdev_priv(netdev);
114 memcpy_fromio(p, adapter->addr, regs->len);
117 static u32 tsnep_ethtool_get_msglevel(struct net_device *netdev)
119 struct tsnep_adapter *adapter = netdev_priv(netdev);
121 return adapter->msg_enable;
124 static void tsnep_ethtool_set_msglevel(struct net_device *netdev, u32 data)
126 struct tsnep_adapter *adapter = netdev_priv(netdev);
128 adapter->msg_enable = data;
131 static void tsnep_ethtool_get_strings(struct net_device *netdev, u32 stringset,
134 struct tsnep_adapter *adapter = netdev_priv(netdev);
135 int rx_count = adapter->num_rx_queues;
136 int tx_count = adapter->num_tx_queues;
141 memcpy(data, tsnep_stats_strings, sizeof(tsnep_stats_strings));
142 data += sizeof(tsnep_stats_strings);
144 for (i = 0; i < rx_count; i++) {
145 for (j = 0; j < TSNEP_RX_QUEUE_STATS_COUNT; j++) {
146 snprintf(data, ETH_GSTRING_LEN,
147 tsnep_rx_queue_stats_strings[j], i);
148 data += ETH_GSTRING_LEN;
152 for (i = 0; i < tx_count; i++) {
153 for (j = 0; j < TSNEP_TX_QUEUE_STATS_COUNT; j++) {
154 snprintf(data, ETH_GSTRING_LEN,
155 tsnep_tx_queue_stats_strings[j], i);
156 data += ETH_GSTRING_LEN;
161 tsnep_ethtool_get_test_strings(data);
166 static void tsnep_ethtool_get_ethtool_stats(struct net_device *netdev,
167 struct ethtool_stats *stats,
170 struct tsnep_adapter *adapter = netdev_priv(netdev);
171 int rx_count = adapter->num_rx_queues;
172 int tx_count = adapter->num_tx_queues;
173 struct tsnep_stats tsnep_stats;
174 struct tsnep_rx_queue_stats tsnep_rx_queue_stats;
175 struct tsnep_tx_queue_stats tsnep_tx_queue_stats;
179 memset(&tsnep_stats, 0, sizeof(tsnep_stats));
180 for (i = 0; i < adapter->num_rx_queues; i++) {
181 tsnep_stats.rx_packets += adapter->rx[i].packets;
182 tsnep_stats.rx_bytes += adapter->rx[i].bytes;
183 tsnep_stats.rx_dropped += adapter->rx[i].dropped;
184 tsnep_stats.rx_multicast += adapter->rx[i].multicast;
185 tsnep_stats.rx_alloc_failed += adapter->rx[i].alloc_failed;
187 reg = ioread32(adapter->addr + ECM_STAT);
188 tsnep_stats.rx_phy_errors =
189 (reg & ECM_STAT_RX_ERR_MASK) >> ECM_STAT_RX_ERR_SHIFT;
190 tsnep_stats.rx_forwarded_phy_errors =
191 (reg & ECM_STAT_FWD_RX_ERR_MASK) >> ECM_STAT_FWD_RX_ERR_SHIFT;
192 tsnep_stats.rx_invalid_frame_errors =
193 (reg & ECM_STAT_INV_FRM_MASK) >> ECM_STAT_INV_FRM_SHIFT;
194 for (i = 0; i < adapter->num_tx_queues; i++) {
195 tsnep_stats.tx_packets += adapter->tx[i].packets;
196 tsnep_stats.tx_bytes += adapter->tx[i].bytes;
197 tsnep_stats.tx_dropped += adapter->tx[i].dropped;
199 memcpy(data, &tsnep_stats, sizeof(tsnep_stats));
200 data += TSNEP_STATS_COUNT;
202 for (i = 0; i < rx_count; i++) {
203 memset(&tsnep_rx_queue_stats, 0, sizeof(tsnep_rx_queue_stats));
204 tsnep_rx_queue_stats.rx_packets = adapter->rx[i].packets;
205 tsnep_rx_queue_stats.rx_bytes = adapter->rx[i].bytes;
206 tsnep_rx_queue_stats.rx_dropped = adapter->rx[i].dropped;
207 tsnep_rx_queue_stats.rx_multicast = adapter->rx[i].multicast;
208 tsnep_rx_queue_stats.rx_alloc_failed =
209 adapter->rx[i].alloc_failed;
210 reg = ioread32(adapter->addr + TSNEP_QUEUE(i) +
212 tsnep_rx_queue_stats.rx_no_descriptor_errors =
213 (reg & TSNEP_RX_STATISTIC_NO_DESC_MASK) >>
214 TSNEP_RX_STATISTIC_NO_DESC_SHIFT;
215 tsnep_rx_queue_stats.rx_buffer_too_small_errors =
216 (reg & TSNEP_RX_STATISTIC_BUFFER_TOO_SMALL_MASK) >>
217 TSNEP_RX_STATISTIC_BUFFER_TOO_SMALL_SHIFT;
218 tsnep_rx_queue_stats.rx_fifo_overflow_errors =
219 (reg & TSNEP_RX_STATISTIC_FIFO_OVERFLOW_MASK) >>
220 TSNEP_RX_STATISTIC_FIFO_OVERFLOW_SHIFT;
221 tsnep_rx_queue_stats.rx_invalid_frame_errors =
222 (reg & TSNEP_RX_STATISTIC_INVALID_FRAME_MASK) >>
223 TSNEP_RX_STATISTIC_INVALID_FRAME_SHIFT;
224 memcpy(data, &tsnep_rx_queue_stats,
225 sizeof(tsnep_rx_queue_stats));
226 data += TSNEP_RX_QUEUE_STATS_COUNT;
229 for (i = 0; i < tx_count; i++) {
230 memset(&tsnep_tx_queue_stats, 0, sizeof(tsnep_tx_queue_stats));
231 tsnep_tx_queue_stats.tx_packets += adapter->tx[i].packets;
232 tsnep_tx_queue_stats.tx_bytes += adapter->tx[i].bytes;
233 tsnep_tx_queue_stats.tx_dropped += adapter->tx[i].dropped;
234 memcpy(data, &tsnep_tx_queue_stats,
235 sizeof(tsnep_tx_queue_stats));
236 data += TSNEP_TX_QUEUE_STATS_COUNT;
240 static int tsnep_ethtool_get_sset_count(struct net_device *netdev, int sset)
242 struct tsnep_adapter *adapter = netdev_priv(netdev);
248 rx_count = adapter->num_rx_queues;
249 tx_count = adapter->num_tx_queues;
250 return TSNEP_STATS_COUNT +
251 TSNEP_RX_QUEUE_STATS_COUNT * rx_count +
252 TSNEP_TX_QUEUE_STATS_COUNT * tx_count;
254 return tsnep_ethtool_get_test_count();
260 static int tsnep_ethtool_get_rxnfc(struct net_device *netdev,
261 struct ethtool_rxnfc *cmd, u32 *rule_locs)
263 struct tsnep_adapter *adapter = netdev_priv(netdev);
266 case ETHTOOL_GRXRINGS:
267 cmd->data = adapter->num_rx_queues;
269 case ETHTOOL_GRXCLSRLCNT:
270 cmd->rule_cnt = adapter->rxnfc_count;
271 cmd->data = adapter->rxnfc_max;
272 cmd->data |= RX_CLS_LOC_SPECIAL;
274 case ETHTOOL_GRXCLSRULE:
275 return tsnep_rxnfc_get_rule(adapter, cmd);
276 case ETHTOOL_GRXCLSRLALL:
277 return tsnep_rxnfc_get_all(adapter, cmd, rule_locs);
283 static int tsnep_ethtool_set_rxnfc(struct net_device *netdev,
284 struct ethtool_rxnfc *cmd)
286 struct tsnep_adapter *adapter = netdev_priv(netdev);
289 case ETHTOOL_SRXCLSRLINS:
290 return tsnep_rxnfc_add_rule(adapter, cmd);
291 case ETHTOOL_SRXCLSRLDEL:
292 return tsnep_rxnfc_del_rule(adapter, cmd);
298 static void tsnep_ethtool_get_channels(struct net_device *netdev,
299 struct ethtool_channels *ch)
301 struct tsnep_adapter *adapter = netdev_priv(netdev);
303 ch->max_rx = adapter->num_rx_queues;
304 ch->max_tx = adapter->num_tx_queues;
305 ch->rx_count = adapter->num_rx_queues;
306 ch->tx_count = adapter->num_tx_queues;
309 static int tsnep_ethtool_get_ts_info(struct net_device *netdev,
310 struct ethtool_ts_info *info)
312 struct tsnep_adapter *adapter = netdev_priv(netdev);
314 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
315 SOF_TIMESTAMPING_RX_SOFTWARE |
316 SOF_TIMESTAMPING_SOFTWARE |
317 SOF_TIMESTAMPING_TX_HARDWARE |
318 SOF_TIMESTAMPING_RX_HARDWARE |
319 SOF_TIMESTAMPING_RAW_HARDWARE;
321 if (adapter->ptp_clock)
322 info->phc_index = ptp_clock_index(adapter->ptp_clock);
324 info->phc_index = -1;
326 info->tx_types = BIT(HWTSTAMP_TX_OFF) |
328 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
329 BIT(HWTSTAMP_FILTER_ALL);
334 static struct tsnep_queue *tsnep_get_queue_with_tx(struct tsnep_adapter *adapter,
339 for (i = 0; i < adapter->num_queues; i++) {
340 if (adapter->queue[i].tx) {
342 return &adapter->queue[i];
351 static struct tsnep_queue *tsnep_get_queue_with_rx(struct tsnep_adapter *adapter,
356 for (i = 0; i < adapter->num_queues; i++) {
357 if (adapter->queue[i].rx) {
359 return &adapter->queue[i];
368 static int tsnep_ethtool_get_coalesce(struct net_device *netdev,
369 struct ethtool_coalesce *ec,
370 struct kernel_ethtool_coalesce *kernel_coal,
371 struct netlink_ext_ack *extack)
373 struct tsnep_adapter *adapter = netdev_priv(netdev);
374 struct tsnep_queue *queue;
376 queue = tsnep_get_queue_with_rx(adapter, 0);
378 ec->rx_coalesce_usecs = tsnep_get_irq_coalesce(queue);
380 queue = tsnep_get_queue_with_tx(adapter, 0);
382 ec->tx_coalesce_usecs = tsnep_get_irq_coalesce(queue);
387 static int tsnep_ethtool_set_coalesce(struct net_device *netdev,
388 struct ethtool_coalesce *ec,
389 struct kernel_ethtool_coalesce *kernel_coal,
390 struct netlink_ext_ack *extack)
392 struct tsnep_adapter *adapter = netdev_priv(netdev);
396 for (i = 0; i < adapter->num_queues; i++) {
397 /* RX coalesce has priority for queues with TX and RX */
398 if (adapter->queue[i].rx)
399 retval = tsnep_set_irq_coalesce(&adapter->queue[i],
400 ec->rx_coalesce_usecs);
402 retval = tsnep_set_irq_coalesce(&adapter->queue[i],
403 ec->tx_coalesce_usecs);
411 static int tsnep_ethtool_get_per_queue_coalesce(struct net_device *netdev,
413 struct ethtool_coalesce *ec)
415 struct tsnep_adapter *adapter = netdev_priv(netdev);
416 struct tsnep_queue *queue_with_rx;
417 struct tsnep_queue *queue_with_tx;
419 if (queue >= max(adapter->num_tx_queues, adapter->num_rx_queues))
422 queue_with_rx = tsnep_get_queue_with_rx(adapter, queue);
424 ec->rx_coalesce_usecs = tsnep_get_irq_coalesce(queue_with_rx);
426 queue_with_tx = tsnep_get_queue_with_tx(adapter, queue);
428 ec->tx_coalesce_usecs = tsnep_get_irq_coalesce(queue_with_tx);
433 static int tsnep_ethtool_set_per_queue_coalesce(struct net_device *netdev,
435 struct ethtool_coalesce *ec)
437 struct tsnep_adapter *adapter = netdev_priv(netdev);
438 struct tsnep_queue *queue_with_rx;
439 struct tsnep_queue *queue_with_tx;
442 if (queue >= max(adapter->num_tx_queues, adapter->num_rx_queues))
445 queue_with_rx = tsnep_get_queue_with_rx(adapter, queue);
447 retval = tsnep_set_irq_coalesce(queue_with_rx, ec->rx_coalesce_usecs);
452 /* RX coalesce has priority for queues with TX and RX */
453 queue_with_tx = tsnep_get_queue_with_tx(adapter, queue);
454 if (queue_with_tx && !queue_with_tx->rx) {
455 retval = tsnep_set_irq_coalesce(queue_with_tx, ec->tx_coalesce_usecs);
463 const struct ethtool_ops tsnep_ethtool_ops = {
464 .supported_coalesce_params = ETHTOOL_COALESCE_USECS,
465 .get_drvinfo = tsnep_ethtool_get_drvinfo,
466 .get_regs_len = tsnep_ethtool_get_regs_len,
467 .get_regs = tsnep_ethtool_get_regs,
468 .get_msglevel = tsnep_ethtool_get_msglevel,
469 .set_msglevel = tsnep_ethtool_set_msglevel,
470 .nway_reset = phy_ethtool_nway_reset,
471 .get_link = ethtool_op_get_link,
472 .self_test = tsnep_ethtool_self_test,
473 .get_strings = tsnep_ethtool_get_strings,
474 .get_ethtool_stats = tsnep_ethtool_get_ethtool_stats,
475 .get_sset_count = tsnep_ethtool_get_sset_count,
476 .get_rxnfc = tsnep_ethtool_get_rxnfc,
477 .set_rxnfc = tsnep_ethtool_set_rxnfc,
478 .get_channels = tsnep_ethtool_get_channels,
479 .get_ts_info = tsnep_ethtool_get_ts_info,
480 .get_coalesce = tsnep_ethtool_get_coalesce,
481 .set_coalesce = tsnep_ethtool_set_coalesce,
482 .get_per_queue_coalesce = tsnep_ethtool_get_per_queue_coalesce,
483 .set_per_queue_coalesce = tsnep_ethtool_set_per_queue_coalesce,
484 .get_link_ksettings = phy_ethtool_get_link_ksettings,
485 .set_link_ksettings = phy_ethtool_set_link_ksettings,