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 "[drv] xdp tx errors",
53 "[qbman] rx pending frames",
54 "[qbman] rx pending bytes",
55 "[qbman] tx conf pending frames",
56 "[qbman] tx conf pending bytes",
57 "[qbman] buffer count",
60 #define DPAA2_ETH_NUM_EXTRA_STATS ARRAY_SIZE(dpaa2_ethtool_extras)
62 static void dpaa2_eth_get_drvinfo(struct net_device *net_dev,
63 struct ethtool_drvinfo *drvinfo)
65 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
67 strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
69 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
70 "%u.%u", priv->dpni_ver_major, priv->dpni_ver_minor);
72 strlcpy(drvinfo->bus_info, dev_name(net_dev->dev.parent->parent),
73 sizeof(drvinfo->bus_info));
77 dpaa2_eth_get_link_ksettings(struct net_device *net_dev,
78 struct ethtool_link_ksettings *link_settings)
80 struct dpni_link_state state = {0};
82 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
84 err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
86 netdev_err(net_dev, "ERROR %d getting link state\n", err);
90 /* At the moment, we have no way of interrogating the DPMAC
91 * from the DPNI side - and for that matter there may exist
92 * no DPMAC at all. So for now we just don't report anything
93 * beyond the DPNI attributes.
95 if (state.options & DPNI_LINK_OPT_AUTONEG)
96 link_settings->base.autoneg = AUTONEG_ENABLE;
97 if (!(state.options & DPNI_LINK_OPT_HALF_DUPLEX))
98 link_settings->base.duplex = DUPLEX_FULL;
99 link_settings->base.speed = state.rate;
105 #define DPNI_DYNAMIC_LINK_SET_VER_MAJOR 7
106 #define DPNI_DYNAMIC_LINK_SET_VER_MINOR 1
108 dpaa2_eth_set_link_ksettings(struct net_device *net_dev,
109 const struct ethtool_link_ksettings *link_settings)
111 struct dpni_link_cfg cfg = {0};
112 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
115 /* If using an older MC version, the DPNI must be down
116 * in order to be able to change link settings. Taking steps to let
117 * the user know that.
119 if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_DYNAMIC_LINK_SET_VER_MAJOR,
120 DPNI_DYNAMIC_LINK_SET_VER_MINOR) < 0) {
121 if (netif_running(net_dev)) {
122 netdev_info(net_dev, "Interface must be brought down first.\n");
127 cfg.rate = link_settings->base.speed;
128 if (link_settings->base.autoneg == AUTONEG_ENABLE)
129 cfg.options |= DPNI_LINK_OPT_AUTONEG;
131 cfg.options &= ~DPNI_LINK_OPT_AUTONEG;
132 if (link_settings->base.duplex == DUPLEX_HALF)
133 cfg.options |= DPNI_LINK_OPT_HALF_DUPLEX;
135 cfg.options &= ~DPNI_LINK_OPT_HALF_DUPLEX;
137 err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &cfg);
139 /* ethtool will be loud enough if we return an error; no point
140 * in putting our own error message on the console by default
142 netdev_dbg(net_dev, "ERROR %d setting link cfg\n", err);
147 static void dpaa2_eth_get_strings(struct net_device *netdev, u32 stringset,
155 for (i = 0; i < DPAA2_ETH_NUM_STATS; i++) {
156 strlcpy(p, dpaa2_ethtool_stats[i], ETH_GSTRING_LEN);
157 p += ETH_GSTRING_LEN;
159 for (i = 0; i < DPAA2_ETH_NUM_EXTRA_STATS; i++) {
160 strlcpy(p, dpaa2_ethtool_extras[i], ETH_GSTRING_LEN);
161 p += ETH_GSTRING_LEN;
167 static int dpaa2_eth_get_sset_count(struct net_device *net_dev, int sset)
170 case ETH_SS_STATS: /* ethtool_get_stats(), ethtool_get_drvinfo() */
171 return DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS;
177 /** Fill in hardware counters, as returned by MC.
179 static void dpaa2_eth_get_ethtool_stats(struct net_device *net_dev,
180 struct ethtool_stats *stats,
186 union dpni_statistics dpni_stats;
188 u32 fcnt_rx_total = 0, fcnt_tx_total = 0;
189 u32 bcnt_rx_total = 0, bcnt_tx_total = 0;
191 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
192 struct dpaa2_eth_drv_stats *extras;
193 struct dpaa2_eth_ch_stats *ch_stats;
196 sizeof(u64) * (DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS));
198 /* Print standard counters, from DPNI statistics */
199 for (j = 0; j <= 2; j++) {
200 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token,
203 netdev_warn(net_dev, "dpni_get_stats(%d) failed\n", j);
206 num_cnt = sizeof(dpni_stats.page_0) / sizeof(u64);
209 num_cnt = sizeof(dpni_stats.page_1) / sizeof(u64);
212 num_cnt = sizeof(dpni_stats.page_2) / sizeof(u64);
215 for (k = 0; k < num_cnt; k++)
216 *(data + i++) = dpni_stats.raw.counter[k];
219 /* Print per-cpu extra stats */
220 for_each_online_cpu(k) {
221 extras = per_cpu_ptr(priv->percpu_extras, k);
222 for (j = 0; j < sizeof(*extras) / sizeof(__u64); j++)
223 *((__u64 *)data + i + j) += *((__u64 *)extras + j);
227 /* Per-channel stats */
228 for (k = 0; k < priv->num_channels; k++) {
229 ch_stats = &priv->channel[k]->stats;
230 for (j = 0; j < sizeof(*ch_stats) / sizeof(__u64); j++)
231 *((__u64 *)data + i + j) += *((__u64 *)ch_stats + j);
235 for (j = 0; j < priv->num_fqs; j++) {
236 /* Print FQ instantaneous counts */
237 err = dpaa2_io_query_fq_count(NULL, priv->fq[j].fqid,
240 netdev_warn(net_dev, "FQ query error %d", err);
244 if (priv->fq[j].type == DPAA2_TX_CONF_FQ) {
245 fcnt_tx_total += fcnt;
246 bcnt_tx_total += bcnt;
248 fcnt_rx_total += fcnt;
249 bcnt_rx_total += bcnt;
253 *(data + i++) = fcnt_rx_total;
254 *(data + i++) = bcnt_rx_total;
255 *(data + i++) = fcnt_tx_total;
256 *(data + i++) = bcnt_tx_total;
258 err = dpaa2_io_query_bp_count(NULL, priv->bpid, &buf_cnt);
260 netdev_warn(net_dev, "Buffer count query error %d\n", err);
263 *(data + i++) = buf_cnt;
266 static int prep_eth_rule(struct ethhdr *eth_value, struct ethhdr *eth_mask,
267 void *key, void *mask, u64 *fields)
271 if (eth_mask->h_proto) {
272 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
273 *(__be16 *)(key + off) = eth_value->h_proto;
274 *(__be16 *)(mask + off) = eth_mask->h_proto;
275 *fields |= DPAA2_ETH_DIST_ETHTYPE;
278 if (!is_zero_ether_addr(eth_mask->h_source)) {
279 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_SA);
280 ether_addr_copy(key + off, eth_value->h_source);
281 ether_addr_copy(mask + off, eth_mask->h_source);
282 *fields |= DPAA2_ETH_DIST_ETHSRC;
285 if (!is_zero_ether_addr(eth_mask->h_dest)) {
286 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
287 ether_addr_copy(key + off, eth_value->h_dest);
288 ether_addr_copy(mask + off, eth_mask->h_dest);
289 *fields |= DPAA2_ETH_DIST_ETHDST;
295 static int prep_uip_rule(struct ethtool_usrip4_spec *uip_value,
296 struct ethtool_usrip4_spec *uip_mask,
297 void *key, void *mask, u64 *fields)
300 u32 tmp_value, tmp_mask;
302 if (uip_mask->tos || uip_mask->ip_ver)
305 if (uip_mask->ip4src) {
306 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
307 *(__be32 *)(key + off) = uip_value->ip4src;
308 *(__be32 *)(mask + off) = uip_mask->ip4src;
309 *fields |= DPAA2_ETH_DIST_IPSRC;
312 if (uip_mask->ip4dst) {
313 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
314 *(__be32 *)(key + off) = uip_value->ip4dst;
315 *(__be32 *)(mask + off) = uip_mask->ip4dst;
316 *fields |= DPAA2_ETH_DIST_IPDST;
319 if (uip_mask->proto) {
320 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
321 *(u8 *)(key + off) = uip_value->proto;
322 *(u8 *)(mask + off) = uip_mask->proto;
323 *fields |= DPAA2_ETH_DIST_IPPROTO;
326 if (uip_mask->l4_4_bytes) {
327 tmp_value = be32_to_cpu(uip_value->l4_4_bytes);
328 tmp_mask = be32_to_cpu(uip_mask->l4_4_bytes);
330 off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
331 *(__be16 *)(key + off) = htons(tmp_value >> 16);
332 *(__be16 *)(mask + off) = htons(tmp_mask >> 16);
333 *fields |= DPAA2_ETH_DIST_L4SRC;
335 off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
336 *(__be16 *)(key + off) = htons(tmp_value & 0xFFFF);
337 *(__be16 *)(mask + off) = htons(tmp_mask & 0xFFFF);
338 *fields |= DPAA2_ETH_DIST_L4DST;
341 /* Only apply the rule for IPv4 frames */
342 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
343 *(__be16 *)(key + off) = htons(ETH_P_IP);
344 *(__be16 *)(mask + off) = htons(0xFFFF);
345 *fields |= DPAA2_ETH_DIST_ETHTYPE;
350 static int prep_l4_rule(struct ethtool_tcpip4_spec *l4_value,
351 struct ethtool_tcpip4_spec *l4_mask,
352 void *key, void *mask, u8 l4_proto, u64 *fields)
359 if (l4_mask->ip4src) {
360 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
361 *(__be32 *)(key + off) = l4_value->ip4src;
362 *(__be32 *)(mask + off) = l4_mask->ip4src;
363 *fields |= DPAA2_ETH_DIST_IPSRC;
366 if (l4_mask->ip4dst) {
367 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
368 *(__be32 *)(key + off) = l4_value->ip4dst;
369 *(__be32 *)(mask + off) = l4_mask->ip4dst;
370 *fields |= DPAA2_ETH_DIST_IPDST;
374 off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
375 *(__be16 *)(key + off) = l4_value->psrc;
376 *(__be16 *)(mask + off) = l4_mask->psrc;
377 *fields |= DPAA2_ETH_DIST_L4SRC;
381 off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
382 *(__be16 *)(key + off) = l4_value->pdst;
383 *(__be16 *)(mask + off) = l4_mask->pdst;
384 *fields |= DPAA2_ETH_DIST_L4DST;
387 /* Only apply the rule for IPv4 frames with the specified L4 proto */
388 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
389 *(__be16 *)(key + off) = htons(ETH_P_IP);
390 *(__be16 *)(mask + off) = htons(0xFFFF);
391 *fields |= DPAA2_ETH_DIST_ETHTYPE;
393 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
394 *(u8 *)(key + off) = l4_proto;
395 *(u8 *)(mask + off) = 0xFF;
396 *fields |= DPAA2_ETH_DIST_IPPROTO;
401 static int prep_ext_rule(struct ethtool_flow_ext *ext_value,
402 struct ethtool_flow_ext *ext_mask,
403 void *key, void *mask, u64 *fields)
407 if (ext_mask->vlan_etype)
410 if (ext_mask->vlan_tci) {
411 off = dpaa2_eth_cls_fld_off(NET_PROT_VLAN, NH_FLD_VLAN_TCI);
412 *(__be16 *)(key + off) = ext_value->vlan_tci;
413 *(__be16 *)(mask + off) = ext_mask->vlan_tci;
414 *fields |= DPAA2_ETH_DIST_VLAN;
420 static int prep_mac_ext_rule(struct ethtool_flow_ext *ext_value,
421 struct ethtool_flow_ext *ext_mask,
422 void *key, void *mask, u64 *fields)
426 if (!is_zero_ether_addr(ext_mask->h_dest)) {
427 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
428 ether_addr_copy(key + off, ext_value->h_dest);
429 ether_addr_copy(mask + off, ext_mask->h_dest);
430 *fields |= DPAA2_ETH_DIST_ETHDST;
436 static int prep_cls_rule(struct ethtool_rx_flow_spec *fs, void *key, void *mask,
441 switch (fs->flow_type & 0xFF) {
443 err = prep_eth_rule(&fs->h_u.ether_spec, &fs->m_u.ether_spec,
447 err = prep_uip_rule(&fs->h_u.usr_ip4_spec,
448 &fs->m_u.usr_ip4_spec, key, mask, fields);
451 err = prep_l4_rule(&fs->h_u.tcp_ip4_spec, &fs->m_u.tcp_ip4_spec,
452 key, mask, IPPROTO_TCP, fields);
455 err = prep_l4_rule(&fs->h_u.udp_ip4_spec, &fs->m_u.udp_ip4_spec,
456 key, mask, IPPROTO_UDP, fields);
459 err = prep_l4_rule(&fs->h_u.sctp_ip4_spec,
460 &fs->m_u.sctp_ip4_spec, key, mask,
461 IPPROTO_SCTP, fields);
470 if (fs->flow_type & FLOW_EXT) {
471 err = prep_ext_rule(&fs->h_ext, &fs->m_ext, key, mask, fields);
476 if (fs->flow_type & FLOW_MAC_EXT) {
477 err = prep_mac_ext_rule(&fs->h_ext, &fs->m_ext, key, mask,
486 static int do_cls_rule(struct net_device *net_dev,
487 struct ethtool_rx_flow_spec *fs,
490 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
491 struct device *dev = net_dev->dev.parent;
492 struct dpni_rule_cfg rule_cfg = { 0 };
493 struct dpni_fs_action_cfg fs_act = { 0 };
499 if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
500 fs->ring_cookie >= dpaa2_eth_queue_count(priv))
503 rule_cfg.key_size = dpaa2_eth_cls_key_size(DPAA2_ETH_DIST_ALL);
505 /* allocate twice the key size, for the actual key and for mask */
506 key_buf = kzalloc(rule_cfg.key_size * 2, GFP_KERNEL);
510 /* Fill the key and mask memory areas */
511 err = prep_cls_rule(fs, key_buf, key_buf + rule_cfg.key_size, &fields);
515 if (!dpaa2_eth_fs_mask_enabled(priv)) {
516 /* Masking allows us to configure a maximal key during init and
517 * use it for all flow steering rules. Without it, we include
518 * in the key only the fields actually used, so we need to
519 * extract the others from the final key buffer.
521 * Program the FS key if needed, or return error if previously
522 * set key can't be used for the current rule. User needs to
523 * delete existing rules in this case to allow for the new one.
525 if (!priv->rx_cls_fields) {
526 err = dpaa2_eth_set_cls(net_dev, fields);
530 priv->rx_cls_fields = fields;
531 } else if (priv->rx_cls_fields != fields) {
532 netdev_err(net_dev, "No support for multiple FS keys, need to delete existing rules\n");
537 dpaa2_eth_cls_trim_rule(key_buf, fields);
538 rule_cfg.key_size = dpaa2_eth_cls_key_size(fields);
541 key_iova = dma_map_single(dev, key_buf, rule_cfg.key_size * 2,
543 if (dma_mapping_error(dev, key_iova)) {
548 rule_cfg.key_iova = key_iova;
549 if (dpaa2_eth_fs_mask_enabled(priv))
550 rule_cfg.mask_iova = key_iova + rule_cfg.key_size;
553 if (fs->ring_cookie == RX_CLS_FLOW_DISC)
554 fs_act.options |= DPNI_FS_OPT_DISCARD;
556 fs_act.flow_id = fs->ring_cookie;
557 err = dpni_add_fs_entry(priv->mc_io, 0, priv->mc_token, 0,
558 fs->location, &rule_cfg, &fs_act);
560 err = dpni_remove_fs_entry(priv->mc_io, 0, priv->mc_token, 0,
564 dma_unmap_single(dev, key_iova, rule_cfg.key_size * 2, DMA_TO_DEVICE);
572 static int num_rules(struct dpaa2_eth_priv *priv)
576 for (i = 0; i < dpaa2_eth_fs_count(priv); i++)
577 if (priv->cls_rules[i].in_use)
583 static int update_cls_rule(struct net_device *net_dev,
584 struct ethtool_rx_flow_spec *new_fs,
587 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
588 struct dpaa2_eth_cls_rule *rule;
591 if (!priv->rx_cls_enabled)
594 if (location >= dpaa2_eth_fs_count(priv))
597 rule = &priv->cls_rules[location];
599 /* If a rule is present at the specified location, delete it. */
601 err = do_cls_rule(net_dev, &rule->fs, false);
607 if (!dpaa2_eth_fs_mask_enabled(priv) && !num_rules(priv))
608 priv->rx_cls_fields = 0;
611 /* If no new entry to add, return here */
615 err = do_cls_rule(net_dev, new_fs, true);
625 static int dpaa2_eth_get_rxnfc(struct net_device *net_dev,
626 struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
628 struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
629 int max_rules = dpaa2_eth_fs_count(priv);
632 switch (rxnfc->cmd) {
634 /* we purposely ignore cmd->flow_type for now, because the
635 * classifier only supports a single set of fields for all
638 rxnfc->data = priv->rx_hash_fields;
640 case ETHTOOL_GRXRINGS:
641 rxnfc->data = dpaa2_eth_queue_count(priv);
643 case ETHTOOL_GRXCLSRLCNT:
645 rxnfc->rule_cnt = num_rules(priv);
646 rxnfc->data = max_rules;
648 case ETHTOOL_GRXCLSRULE:
649 if (rxnfc->fs.location >= max_rules)
651 if (!priv->cls_rules[rxnfc->fs.location].in_use)
653 rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
655 case ETHTOOL_GRXCLSRLALL:
656 for (i = 0; i < max_rules; i++) {
657 if (!priv->cls_rules[i].in_use)
659 if (j == rxnfc->rule_cnt)
664 rxnfc->data = max_rules;
673 static int dpaa2_eth_set_rxnfc(struct net_device *net_dev,
674 struct ethtool_rxnfc *rxnfc)
678 switch (rxnfc->cmd) {
680 if ((rxnfc->data & DPAA2_RXH_SUPPORTED) != rxnfc->data)
682 err = dpaa2_eth_set_hash(net_dev, rxnfc->data);
684 case ETHTOOL_SRXCLSRLINS:
685 err = update_cls_rule(net_dev, &rxnfc->fs, rxnfc->fs.location);
687 case ETHTOOL_SRXCLSRLDEL:
688 err = update_cls_rule(net_dev, NULL, rxnfc->fs.location);
697 int dpaa2_phc_index = -1;
698 EXPORT_SYMBOL(dpaa2_phc_index);
700 static int dpaa2_eth_get_ts_info(struct net_device *dev,
701 struct ethtool_ts_info *info)
703 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
704 SOF_TIMESTAMPING_RX_HARDWARE |
705 SOF_TIMESTAMPING_RAW_HARDWARE;
707 info->phc_index = dpaa2_phc_index;
709 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
710 (1 << HWTSTAMP_TX_ON);
712 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
713 (1 << HWTSTAMP_FILTER_ALL);
717 const struct ethtool_ops dpaa2_ethtool_ops = {
718 .get_drvinfo = dpaa2_eth_get_drvinfo,
719 .get_link = ethtool_op_get_link,
720 .get_link_ksettings = dpaa2_eth_get_link_ksettings,
721 .set_link_ksettings = dpaa2_eth_set_link_ksettings,
722 .get_sset_count = dpaa2_eth_get_sset_count,
723 .get_ethtool_stats = dpaa2_eth_get_ethtool_stats,
724 .get_strings = dpaa2_eth_get_strings,
725 .get_rxnfc = dpaa2_eth_get_rxnfc,
726 .set_rxnfc = dpaa2_eth_set_rxnfc,
727 .get_ts_info = dpaa2_eth_get_ts_info,