1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2014-2016 Freescale Semiconductor Inc.
6 #include <linux/net_tstamp.h>
8 #include "dpni.h" /* DPNI_LINK_OPT_* */
11 /* To be kept in sync with DPNI statistics */
12 static char dpaa2_ethtool_stats[][ETH_GSTRING_LEN] = {
15 "[hw] rx mcast frames",
16 "[hw] rx mcast bytes",
17 "[hw] rx bcast frames",
18 "[hw] rx bcast bytes",
21 "[hw] tx mcast frames",
22 "[hw] tx mcast bytes",
23 "[hw] tx bcast frames",
24 "[hw] tx bcast bytes",
25 "[hw] rx filtered frames",
26 "[hw] rx discarded frames",
27 "[hw] rx nobuffer discards",
28 "[hw] tx discarded frames",
29 "[hw] tx confirmed frames",
32 #define DPAA2_ETH_NUM_STATS ARRAY_SIZE(dpaa2_ethtool_stats)
34 static char dpaa2_ethtool_extras[][ETH_GSTRING_LEN] = {
36 "[drv] tx conf frames",
37 "[drv] tx conf bytes",
40 "[drv] tx realloc frames",
43 "[drv] enqueue portal busy",
45 "[drv] dequeue portal busy",
46 "[drv] channel pull errors",
50 #define DPAA2_ETH_NUM_EXTRA_STATS ARRAY_SIZE(dpaa2_ethtool_extras)
52 static void dpaa2_eth_get_drvinfo(struct net_device *net_dev,
53 struct ethtool_drvinfo *drvinfo)
55 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
57 strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
59 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
60 "%u.%u", priv->dpni_ver_major, priv->dpni_ver_minor);
62 strlcpy(drvinfo->bus_info, dev_name(net_dev->dev.parent->parent),
63 sizeof(drvinfo->bus_info));
67 dpaa2_eth_get_link_ksettings(struct net_device *net_dev,
68 struct ethtool_link_ksettings *link_settings)
70 struct dpni_link_state state = {0};
72 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
74 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
76 netdev_err(net_dev, "ERROR %d getting link state\n", err);
80 /* At the moment, we have no way of interrogating the DPMAC
81 * from the DPNI side - and for that matter there may exist
82 * no DPMAC at all. So for now we just don't report anything
83 * beyond the DPNI attributes.
85 if (state.options & DPNI_LINK_OPT_AUTONEG)
86 link_settings->base.autoneg = AUTONEG_ENABLE;
87 if (!(state.options & DPNI_LINK_OPT_HALF_DUPLEX))
88 link_settings->base.duplex = DUPLEX_FULL;
89 link_settings->base.speed = state.rate;
95 #define DPNI_DYNAMIC_LINK_SET_VER_MAJOR 7
96 #define DPNI_DYNAMIC_LINK_SET_VER_MINOR 1
98 dpaa2_eth_set_link_ksettings(struct net_device *net_dev,
99 const struct ethtool_link_ksettings *link_settings)
101 struct dpni_link_cfg cfg = {0};
102 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
105 /* If using an older MC version, the DPNI must be down
106 * in order to be able to change link settings. Taking steps to let
107 * the user know that.
109 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_DYNAMIC_LINK_SET_VER_MAJOR,
110 DPNI_DYNAMIC_LINK_SET_VER_MINOR) < 0) {
111 if (netif_running(net_dev)) {
112 netdev_info(net_dev, "Interface must be brought down first.\n");
117 cfg.rate = link_settings->base.speed;
118 if (link_settings->base.autoneg == AUTONEG_ENABLE)
119 cfg.options |= DPNI_LINK_OPT_AUTONEG;
121 cfg.options &= ~DPNI_LINK_OPT_AUTONEG;
122 if (link_settings->base.duplex == DUPLEX_HALF)
123 cfg.options |= DPNI_LINK_OPT_HALF_DUPLEX;
125 cfg.options &= ~DPNI_LINK_OPT_HALF_DUPLEX;
127 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &cfg);
129 /* ethtool will be loud enough if we return an error; no point
130 * in putting our own error message on the console by default
132 netdev_dbg(net_dev, "ERROR %d setting link cfg\n", err);
137 static void dpaa2_eth_get_strings(struct net_device *netdev, u32 stringset,
145 for (i = 0; i < DPAA2_ETH_NUM_STATS; i++) {
146 strlcpy(p, dpaa2_ethtool_stats[i], ETH_GSTRING_LEN);
147 p += ETH_GSTRING_LEN;
149 for (i = 0; i < DPAA2_ETH_NUM_EXTRA_STATS; i++) {
150 strlcpy(p, dpaa2_ethtool_extras[i], ETH_GSTRING_LEN);
151 p += ETH_GSTRING_LEN;
157 static int dpaa2_eth_get_sset_count(struct net_device *net_dev, int sset)
160 case ETH_SS_STATS: /* ethtool_get_stats(), ethtool_get_drvinfo() */
161 return DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS;
167 /** Fill in hardware counters, as returned by MC.
169 static void dpaa2_eth_get_ethtool_stats(struct net_device *net_dev,
170 struct ethtool_stats *stats,
176 union dpni_statistics dpni_stats;
178 u64 portal_busy = 0, pull_err = 0;
179 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
180 struct dpaa2_eth_drv_stats *extras;
181 struct dpaa2_eth_ch_stats *ch_stats;
184 sizeof(u64) * (DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS));
186 /* Print standard counters, from DPNI statistics */
187 for (j = 0; j <= 2; j++) {
188 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token,
191 netdev_warn(net_dev, "dpni_get_stats(%d) failed\n", j);
194 num_cnt = sizeof(dpni_stats.page_0) / sizeof(u64);
197 num_cnt = sizeof(dpni_stats.page_1) / sizeof(u64);
200 num_cnt = sizeof(dpni_stats.page_2) / sizeof(u64);
203 for (k = 0; k < num_cnt; k++)
204 *(data + i++) = dpni_stats.raw.counter[k];
207 /* Print per-cpu extra stats */
208 for_each_online_cpu(k) {
209 extras = per_cpu_ptr(priv->percpu_extras, k);
210 for (j = 0; j < sizeof(*extras) / sizeof(__u64); j++)
211 *((__u64 *)data + i + j) += *((__u64 *)extras + j);
215 for (j = 0; j < priv->num_channels; j++) {
216 ch_stats = &priv->channel[j]->stats;
217 cdan += ch_stats->cdan;
218 portal_busy += ch_stats->dequeue_portal_busy;
219 pull_err += ch_stats->pull_err;
222 *(data + i++) = portal_busy;
223 *(data + i++) = pull_err;
224 *(data + i++) = cdan;
227 static int prep_eth_rule(struct ethhdr *eth_value, struct ethhdr *eth_mask,
228 void *key, void *mask)
232 if (eth_mask->h_proto) {
233 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
234 *(__be16 *)(key + off) = eth_value->h_proto;
235 *(__be16 *)(mask + off) = eth_mask->h_proto;
238 if (!is_zero_ether_addr(eth_mask->h_source)) {
239 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_SA);
240 ether_addr_copy(key + off, eth_value->h_source);
241 ether_addr_copy(mask + off, eth_mask->h_source);
244 if (!is_zero_ether_addr(eth_mask->h_dest)) {
245 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
246 ether_addr_copy(key + off, eth_value->h_dest);
247 ether_addr_copy(mask + off, eth_mask->h_dest);
253 static int prep_uip_rule(struct ethtool_usrip4_spec *uip_value,
254 struct ethtool_usrip4_spec *uip_mask,
255 void *key, void *mask)
258 u32 tmp_value, tmp_mask;
260 if (uip_mask->tos || uip_mask->ip_ver)
263 if (uip_mask->ip4src) {
264 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
265 *(__be32 *)(key + off) = uip_value->ip4src;
266 *(__be32 *)(mask + off) = uip_mask->ip4src;
269 if (uip_mask->ip4dst) {
270 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
271 *(__be32 *)(key + off) = uip_value->ip4dst;
272 *(__be32 *)(mask + off) = uip_mask->ip4dst;
275 if (uip_mask->proto) {
276 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
277 *(u8 *)(key + off) = uip_value->proto;
278 *(u8 *)(mask + off) = uip_mask->proto;
281 if (uip_mask->l4_4_bytes) {
282 tmp_value = be32_to_cpu(uip_value->l4_4_bytes);
283 tmp_mask = be32_to_cpu(uip_mask->l4_4_bytes);
285 off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
286 *(__be16 *)(key + off) = htons(tmp_value >> 16);
287 *(__be16 *)(mask + off) = htons(tmp_mask >> 16);
289 off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
290 *(__be16 *)(key + off) = htons(tmp_value & 0xFFFF);
291 *(__be16 *)(mask + off) = htons(tmp_mask & 0xFFFF);
294 /* Only apply the rule for IPv4 frames */
295 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
296 *(__be16 *)(key + off) = htons(ETH_P_IP);
297 *(__be16 *)(mask + off) = htons(0xFFFF);
302 static int prep_l4_rule(struct ethtool_tcpip4_spec *l4_value,
303 struct ethtool_tcpip4_spec *l4_mask,
304 void *key, void *mask, u8 l4_proto)
311 if (l4_mask->ip4src) {
312 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
313 *(__be32 *)(key + off) = l4_value->ip4src;
314 *(__be32 *)(mask + off) = l4_mask->ip4src;
317 if (l4_mask->ip4dst) {
318 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
319 *(__be32 *)(key + off) = l4_value->ip4dst;
320 *(__be32 *)(mask + off) = l4_mask->ip4dst;
324 off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
325 *(__be16 *)(key + off) = l4_value->psrc;
326 *(__be16 *)(mask + off) = l4_mask->psrc;
330 off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
331 *(__be16 *)(key + off) = l4_value->pdst;
332 *(__be16 *)(mask + off) = l4_mask->pdst;
335 /* Only apply the rule for IPv4 frames with the specified L4 proto */
336 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
337 *(__be16 *)(key + off) = htons(ETH_P_IP);
338 *(__be16 *)(mask + off) = htons(0xFFFF);
340 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
341 *(u8 *)(key + off) = l4_proto;
342 *(u8 *)(mask + off) = 0xFF;
347 static int prep_ext_rule(struct ethtool_flow_ext *ext_value,
348 struct ethtool_flow_ext *ext_mask,
349 void *key, void *mask)
353 if (ext_mask->vlan_etype)
356 if (ext_mask->vlan_tci) {
357 off = dpaa2_eth_cls_fld_off(NET_PROT_VLAN, NH_FLD_VLAN_TCI);
358 *(__be16 *)(key + off) = ext_value->vlan_tci;
359 *(__be16 *)(mask + off) = ext_mask->vlan_tci;
365 static int prep_mac_ext_rule(struct ethtool_flow_ext *ext_value,
366 struct ethtool_flow_ext *ext_mask,
367 void *key, void *mask)
371 if (!is_zero_ether_addr(ext_mask->h_dest)) {
372 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
373 ether_addr_copy(key + off, ext_value->h_dest);
374 ether_addr_copy(mask + off, ext_mask->h_dest);
380 static int prep_cls_rule(struct ethtool_rx_flow_spec *fs, void *key, void *mask)
384 switch (fs->flow_type & 0xFF) {
386 err = prep_eth_rule(&fs->h_u.ether_spec, &fs->m_u.ether_spec,
390 err = prep_uip_rule(&fs->h_u.usr_ip4_spec,
391 &fs->m_u.usr_ip4_spec, key, mask);
394 err = prep_l4_rule(&fs->h_u.tcp_ip4_spec, &fs->m_u.tcp_ip4_spec,
395 key, mask, IPPROTO_TCP);
398 err = prep_l4_rule(&fs->h_u.udp_ip4_spec, &fs->m_u.udp_ip4_spec,
399 key, mask, IPPROTO_UDP);
402 err = prep_l4_rule(&fs->h_u.sctp_ip4_spec,
403 &fs->m_u.sctp_ip4_spec, key, mask,
413 if (fs->flow_type & FLOW_EXT) {
414 err = prep_ext_rule(&fs->h_ext, &fs->m_ext, key, mask);
419 if (fs->flow_type & FLOW_MAC_EXT) {
420 err = prep_mac_ext_rule(&fs->h_ext, &fs->m_ext, key, mask);
428 static int do_cls_rule(struct net_device *net_dev,
429 struct ethtool_rx_flow_spec *fs,
432 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
433 struct device *dev = net_dev->dev.parent;
434 struct dpni_rule_cfg rule_cfg = { 0 };
435 struct dpni_fs_action_cfg fs_act = { 0 };
440 if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
441 fs->ring_cookie >= dpaa2_eth_queue_count(priv))
444 rule_cfg.key_size = dpaa2_eth_cls_key_size();
446 /* allocate twice the key size, for the actual key and for mask */
447 key_buf = kzalloc(rule_cfg.key_size * 2, GFP_KERNEL);
451 /* Fill the key and mask memory areas */
452 err = prep_cls_rule(fs, key_buf, key_buf + rule_cfg.key_size);
456 key_iova = dma_map_single(dev, key_buf, rule_cfg.key_size * 2,
458 if (dma_mapping_error(dev, key_iova)) {
463 rule_cfg.key_iova = key_iova;
464 rule_cfg.mask_iova = key_iova + rule_cfg.key_size;
467 if (fs->ring_cookie == RX_CLS_FLOW_DISC)
468 fs_act.options |= DPNI_FS_OPT_DISCARD;
470 fs_act.flow_id = fs->ring_cookie;
471 err = dpni_add_fs_entry(priv->mc_io, 0, priv->mc_token, 0,
472 fs->location, &rule_cfg, &fs_act);
474 err = dpni_remove_fs_entry(priv->mc_io, 0, priv->mc_token, 0,
478 dma_unmap_single(dev, key_iova, rule_cfg.key_size * 2, DMA_TO_DEVICE);
486 static int update_cls_rule(struct net_device *net_dev,
487 struct ethtool_rx_flow_spec *new_fs,
490 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
491 struct dpaa2_eth_cls_rule *rule;
494 if (!priv->rx_cls_enabled)
497 if (location >= dpaa2_eth_fs_count(priv))
500 rule = &priv->cls_rules[location];
502 /* If a rule is present at the specified location, delete it. */
504 err = do_cls_rule(net_dev, &rule->fs, false);
511 /* If no new entry to add, return here */
515 err = do_cls_rule(net_dev, new_fs, true);
525 static int dpaa2_eth_get_rxnfc(struct net_device *net_dev,
526 struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
528 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
529 int max_rules = dpaa2_eth_fs_count(priv);
532 switch (rxnfc->cmd) {
534 /* we purposely ignore cmd->flow_type for now, because the
535 * classifier only supports a single set of fields for all
538 rxnfc->data = priv->rx_hash_fields;
540 case ETHTOOL_GRXRINGS:
541 rxnfc->data = dpaa2_eth_queue_count(priv);
543 case ETHTOOL_GRXCLSRLCNT:
545 for (i = 0; i < max_rules; i++)
546 if (priv->cls_rules[i].in_use)
548 rxnfc->data = max_rules;
550 case ETHTOOL_GRXCLSRULE:
551 if (rxnfc->fs.location >= max_rules)
553 if (!priv->cls_rules[rxnfc->fs.location].in_use)
555 rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
557 case ETHTOOL_GRXCLSRLALL:
558 for (i = 0; i < max_rules; i++) {
559 if (!priv->cls_rules[i].in_use)
561 if (j == rxnfc->rule_cnt)
566 rxnfc->data = max_rules;
575 static int dpaa2_eth_set_rxnfc(struct net_device *net_dev,
576 struct ethtool_rxnfc *rxnfc)
580 switch (rxnfc->cmd) {
582 if ((rxnfc->data & DPAA2_RXH_SUPPORTED) != rxnfc->data)
584 err = dpaa2_eth_set_hash(net_dev, rxnfc->data);
586 case ETHTOOL_SRXCLSRLINS:
587 err = update_cls_rule(net_dev, &rxnfc->fs, rxnfc->fs.location);
589 case ETHTOOL_SRXCLSRLDEL:
590 err = update_cls_rule(net_dev, NULL, rxnfc->fs.location);
599 int dpaa2_phc_index = -1;
600 EXPORT_SYMBOL(dpaa2_phc_index);
602 static int dpaa2_eth_get_ts_info(struct net_device *dev,
603 struct ethtool_ts_info *info)
605 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
606 SOF_TIMESTAMPING_RX_HARDWARE |
607 SOF_TIMESTAMPING_RAW_HARDWARE;
609 info->phc_index = dpaa2_phc_index;
611 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
612 (1 << HWTSTAMP_TX_ON);
614 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
615 (1 << HWTSTAMP_FILTER_ALL);
619 const struct ethtool_ops dpaa2_ethtool_ops = {
620 .get_drvinfo = dpaa2_eth_get_drvinfo,
621 .get_link = ethtool_op_get_link,
622 .get_link_ksettings = dpaa2_eth_get_link_ksettings,
623 .set_link_ksettings = dpaa2_eth_set_link_ksettings,
624 .get_sset_count = dpaa2_eth_get_sset_count,
625 .get_ethtool_stats = dpaa2_eth_get_ethtool_stats,
626 .get_strings = dpaa2_eth_get_strings,
627 .get_rxnfc = dpaa2_eth_get_rxnfc,
628 .set_rxnfc = dpaa2_eth_set_rxnfc,
629 .get_ts_info = dpaa2_eth_get_ts_info,