dpaa2-eth: send a scatter-gather FD instead of realloc-ing
[linux-2.6-microblaze.git] / drivers / net / ethernet / freescale / dpaa2 / dpaa2-ethtool.c
1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2014-2016 Freescale Semiconductor Inc.
3  * Copyright 2016 NXP
4  */
5
6 #include <linux/net_tstamp.h>
7 #include <linux/nospec.h>
8
9 #include "dpni.h"       /* DPNI_LINK_OPT_* */
10 #include "dpaa2-eth.h"
11
12 /* To be kept in sync with DPNI statistics */
13 static char dpaa2_ethtool_stats[][ETH_GSTRING_LEN] = {
14         "[hw] rx frames",
15         "[hw] rx bytes",
16         "[hw] rx mcast frames",
17         "[hw] rx mcast bytes",
18         "[hw] rx bcast frames",
19         "[hw] rx bcast bytes",
20         "[hw] tx frames",
21         "[hw] tx bytes",
22         "[hw] tx mcast frames",
23         "[hw] tx mcast bytes",
24         "[hw] tx bcast frames",
25         "[hw] tx bcast bytes",
26         "[hw] rx filtered frames",
27         "[hw] rx discarded frames",
28         "[hw] rx nobuffer discards",
29         "[hw] tx discarded frames",
30         "[hw] tx confirmed frames",
31         "[hw] tx dequeued bytes",
32         "[hw] tx dequeued frames",
33         "[hw] tx rejected bytes",
34         "[hw] tx rejected frames",
35         "[hw] tx pending frames",
36 };
37
38 #define DPAA2_ETH_NUM_STATS     ARRAY_SIZE(dpaa2_ethtool_stats)
39
40 static char dpaa2_ethtool_extras[][ETH_GSTRING_LEN] = {
41         /* per-cpu stats */
42         "[drv] tx conf frames",
43         "[drv] tx conf bytes",
44         "[drv] tx sg frames",
45         "[drv] tx sg bytes",
46         "[drv] rx sg frames",
47         "[drv] rx sg bytes",
48         "[drv] enqueue portal busy",
49         /* Channel stats */
50         "[drv] dequeue portal busy",
51         "[drv] channel pull errors",
52         "[drv] cdan",
53         "[drv] xdp drop",
54         "[drv] xdp tx",
55         "[drv] xdp tx errors",
56         "[drv] xdp redirect",
57         /* FQ stats */
58         "[qbman] rx pending frames",
59         "[qbman] rx pending bytes",
60         "[qbman] tx conf pending frames",
61         "[qbman] tx conf pending bytes",
62         "[qbman] buffer count",
63 };
64
65 #define DPAA2_ETH_NUM_EXTRA_STATS       ARRAY_SIZE(dpaa2_ethtool_extras)
66
67 static void dpaa2_eth_get_drvinfo(struct net_device *net_dev,
68                                   struct ethtool_drvinfo *drvinfo)
69 {
70         struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
71
72         strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
73
74         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
75                  "%u.%u", priv->dpni_ver_major, priv->dpni_ver_minor);
76
77         strlcpy(drvinfo->bus_info, dev_name(net_dev->dev.parent->parent),
78                 sizeof(drvinfo->bus_info));
79 }
80
81 static int dpaa2_eth_nway_reset(struct net_device *net_dev)
82 {
83         struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
84
85         if (priv->mac)
86                 return phylink_ethtool_nway_reset(priv->mac->phylink);
87
88         return -EOPNOTSUPP;
89 }
90
91 static int
92 dpaa2_eth_get_link_ksettings(struct net_device *net_dev,
93                              struct ethtool_link_ksettings *link_settings)
94 {
95         struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
96
97         if (priv->mac)
98                 return phylink_ethtool_ksettings_get(priv->mac->phylink,
99                                                      link_settings);
100
101         link_settings->base.autoneg = AUTONEG_DISABLE;
102         if (!(priv->link_state.options & DPNI_LINK_OPT_HALF_DUPLEX))
103                 link_settings->base.duplex = DUPLEX_FULL;
104         link_settings->base.speed = priv->link_state.rate;
105
106         return 0;
107 }
108
109 static int
110 dpaa2_eth_set_link_ksettings(struct net_device *net_dev,
111                              const struct ethtool_link_ksettings *link_settings)
112 {
113         struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
114
115         if (!priv->mac)
116                 return -ENOTSUPP;
117
118         return phylink_ethtool_ksettings_set(priv->mac->phylink, link_settings);
119 }
120
121 static void dpaa2_eth_get_pauseparam(struct net_device *net_dev,
122                                      struct ethtool_pauseparam *pause)
123 {
124         struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
125         u64 link_options = priv->link_state.options;
126
127         if (priv->mac) {
128                 phylink_ethtool_get_pauseparam(priv->mac->phylink, pause);
129                 return;
130         }
131
132         pause->rx_pause = dpaa2_eth_rx_pause_enabled(link_options);
133         pause->tx_pause = dpaa2_eth_tx_pause_enabled(link_options);
134         pause->autoneg = AUTONEG_DISABLE;
135 }
136
137 static int dpaa2_eth_set_pauseparam(struct net_device *net_dev,
138                                     struct ethtool_pauseparam *pause)
139 {
140         struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
141         struct dpni_link_cfg cfg = {0};
142         int err;
143
144         if (!dpaa2_eth_has_pause_support(priv)) {
145                 netdev_info(net_dev, "No pause frame support for DPNI version < %d.%d\n",
146                             DPNI_PAUSE_VER_MAJOR, DPNI_PAUSE_VER_MINOR);
147                 return -EOPNOTSUPP;
148         }
149
150         if (priv->mac)
151                 return phylink_ethtool_set_pauseparam(priv->mac->phylink,
152                                                       pause);
153         if (pause->autoneg)
154                 return -EOPNOTSUPP;
155
156         cfg.rate = priv->link_state.rate;
157         cfg.options = priv->link_state.options;
158         if (pause->rx_pause)
159                 cfg.options |= DPNI_LINK_OPT_PAUSE;
160         else
161                 cfg.options &= ~DPNI_LINK_OPT_PAUSE;
162         if (!!pause->rx_pause ^ !!pause->tx_pause)
163                 cfg.options |= DPNI_LINK_OPT_ASYM_PAUSE;
164         else
165                 cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
166
167         if (cfg.options == priv->link_state.options)
168                 return 0;
169
170         err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &cfg);
171         if (err) {
172                 netdev_err(net_dev, "dpni_set_link_state failed\n");
173                 return err;
174         }
175
176         priv->link_state.options = cfg.options;
177
178         return 0;
179 }
180
181 static void dpaa2_eth_get_strings(struct net_device *netdev, u32 stringset,
182                                   u8 *data)
183 {
184         struct dpaa2_eth_priv *priv = netdev_priv(netdev);
185         u8 *p = data;
186         int i;
187
188         switch (stringset) {
189         case ETH_SS_STATS:
190                 for (i = 0; i < DPAA2_ETH_NUM_STATS; i++) {
191                         strlcpy(p, dpaa2_ethtool_stats[i], ETH_GSTRING_LEN);
192                         p += ETH_GSTRING_LEN;
193                 }
194                 for (i = 0; i < DPAA2_ETH_NUM_EXTRA_STATS; i++) {
195                         strlcpy(p, dpaa2_ethtool_extras[i], ETH_GSTRING_LEN);
196                         p += ETH_GSTRING_LEN;
197                 }
198                 if (priv->mac)
199                         dpaa2_mac_get_strings(p);
200                 break;
201         }
202 }
203
204 static int dpaa2_eth_get_sset_count(struct net_device *net_dev, int sset)
205 {
206         int num_ss_stats = DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS;
207         struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
208
209         switch (sset) {
210         case ETH_SS_STATS: /* ethtool_get_stats(), ethtool_get_drvinfo() */
211                 if (priv->mac)
212                         num_ss_stats += dpaa2_mac_get_sset_count();
213                 return num_ss_stats;
214         default:
215                 return -EOPNOTSUPP;
216         }
217 }
218
219 /** Fill in hardware counters, as returned by MC.
220  */
221 static void dpaa2_eth_get_ethtool_stats(struct net_device *net_dev,
222                                         struct ethtool_stats *stats,
223                                         u64 *data)
224 {
225         int i = 0;
226         int j, k, err;
227         int num_cnt;
228         union dpni_statistics dpni_stats;
229         u32 fcnt, bcnt;
230         u32 fcnt_rx_total = 0, fcnt_tx_total = 0;
231         u32 bcnt_rx_total = 0, bcnt_tx_total = 0;
232         u32 buf_cnt;
233         struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
234         struct dpaa2_eth_drv_stats *extras;
235         struct dpaa2_eth_ch_stats *ch_stats;
236         int dpni_stats_page_size[DPNI_STATISTICS_CNT] = {
237                 sizeof(dpni_stats.page_0),
238                 sizeof(dpni_stats.page_1),
239                 sizeof(dpni_stats.page_2),
240                 sizeof(dpni_stats.page_3),
241                 sizeof(dpni_stats.page_4),
242                 sizeof(dpni_stats.page_5),
243                 sizeof(dpni_stats.page_6),
244         };
245
246         memset(data, 0,
247                sizeof(u64) * (DPAA2_ETH_NUM_STATS + DPAA2_ETH_NUM_EXTRA_STATS));
248
249         /* Print standard counters, from DPNI statistics */
250         for (j = 0; j <= 6; j++) {
251                 /* We're not interested in pages 4 & 5 for now */
252                 if (j == 4 || j == 5)
253                         continue;
254                 err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token,
255                                           j, &dpni_stats);
256                 if (err == -EINVAL)
257                         /* Older firmware versions don't support all pages */
258                         memset(&dpni_stats, 0, sizeof(dpni_stats));
259                 else if (err)
260                         netdev_warn(net_dev, "dpni_get_stats(%d) failed\n", j);
261
262                 num_cnt = dpni_stats_page_size[j] / sizeof(u64);
263                 for (k = 0; k < num_cnt; k++)
264                         *(data + i++) = dpni_stats.raw.counter[k];
265         }
266
267         /* Print per-cpu extra stats */
268         for_each_online_cpu(k) {
269                 extras = per_cpu_ptr(priv->percpu_extras, k);
270                 for (j = 0; j < sizeof(*extras) / sizeof(__u64); j++)
271                         *((__u64 *)data + i + j) += *((__u64 *)extras + j);
272         }
273         i += j;
274
275         /* Per-channel stats */
276         for (k = 0; k < priv->num_channels; k++) {
277                 ch_stats = &priv->channel[k]->stats;
278                 for (j = 0; j < sizeof(*ch_stats) / sizeof(__u64) - 1; j++)
279                         *((__u64 *)data + i + j) += *((__u64 *)ch_stats + j);
280         }
281         i += j;
282
283         for (j = 0; j < priv->num_fqs; j++) {
284                 /* Print FQ instantaneous counts */
285                 err = dpaa2_io_query_fq_count(NULL, priv->fq[j].fqid,
286                                               &fcnt, &bcnt);
287                 if (err) {
288                         netdev_warn(net_dev, "FQ query error %d", err);
289                         return;
290                 }
291
292                 if (priv->fq[j].type == DPAA2_TX_CONF_FQ) {
293                         fcnt_tx_total += fcnt;
294                         bcnt_tx_total += bcnt;
295                 } else {
296                         fcnt_rx_total += fcnt;
297                         bcnt_rx_total += bcnt;
298                 }
299         }
300
301         *(data + i++) = fcnt_rx_total;
302         *(data + i++) = bcnt_rx_total;
303         *(data + i++) = fcnt_tx_total;
304         *(data + i++) = bcnt_tx_total;
305
306         err = dpaa2_io_query_bp_count(NULL, priv->bpid, &buf_cnt);
307         if (err) {
308                 netdev_warn(net_dev, "Buffer count query error %d\n", err);
309                 return;
310         }
311         *(data + i++) = buf_cnt;
312
313         if (priv->mac)
314                 dpaa2_mac_get_ethtool_stats(priv->mac, data + i);
315 }
316
317 static int prep_eth_rule(struct ethhdr *eth_value, struct ethhdr *eth_mask,
318                          void *key, void *mask, u64 *fields)
319 {
320         int off;
321
322         if (eth_mask->h_proto) {
323                 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
324                 *(__be16 *)(key + off) = eth_value->h_proto;
325                 *(__be16 *)(mask + off) = eth_mask->h_proto;
326                 *fields |= DPAA2_ETH_DIST_ETHTYPE;
327         }
328
329         if (!is_zero_ether_addr(eth_mask->h_source)) {
330                 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_SA);
331                 ether_addr_copy(key + off, eth_value->h_source);
332                 ether_addr_copy(mask + off, eth_mask->h_source);
333                 *fields |= DPAA2_ETH_DIST_ETHSRC;
334         }
335
336         if (!is_zero_ether_addr(eth_mask->h_dest)) {
337                 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
338                 ether_addr_copy(key + off, eth_value->h_dest);
339                 ether_addr_copy(mask + off, eth_mask->h_dest);
340                 *fields |= DPAA2_ETH_DIST_ETHDST;
341         }
342
343         return 0;
344 }
345
346 static int prep_uip_rule(struct ethtool_usrip4_spec *uip_value,
347                          struct ethtool_usrip4_spec *uip_mask,
348                          void *key, void *mask, u64 *fields)
349 {
350         int off;
351         u32 tmp_value, tmp_mask;
352
353         if (uip_mask->tos || uip_mask->ip_ver)
354                 return -EOPNOTSUPP;
355
356         if (uip_mask->ip4src) {
357                 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
358                 *(__be32 *)(key + off) = uip_value->ip4src;
359                 *(__be32 *)(mask + off) = uip_mask->ip4src;
360                 *fields |= DPAA2_ETH_DIST_IPSRC;
361         }
362
363         if (uip_mask->ip4dst) {
364                 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
365                 *(__be32 *)(key + off) = uip_value->ip4dst;
366                 *(__be32 *)(mask + off) = uip_mask->ip4dst;
367                 *fields |= DPAA2_ETH_DIST_IPDST;
368         }
369
370         if (uip_mask->proto) {
371                 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
372                 *(u8 *)(key + off) = uip_value->proto;
373                 *(u8 *)(mask + off) = uip_mask->proto;
374                 *fields |= DPAA2_ETH_DIST_IPPROTO;
375         }
376
377         if (uip_mask->l4_4_bytes) {
378                 tmp_value = be32_to_cpu(uip_value->l4_4_bytes);
379                 tmp_mask = be32_to_cpu(uip_mask->l4_4_bytes);
380
381                 off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
382                 *(__be16 *)(key + off) = htons(tmp_value >> 16);
383                 *(__be16 *)(mask + off) = htons(tmp_mask >> 16);
384                 *fields |= DPAA2_ETH_DIST_L4SRC;
385
386                 off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
387                 *(__be16 *)(key + off) = htons(tmp_value & 0xFFFF);
388                 *(__be16 *)(mask + off) = htons(tmp_mask & 0xFFFF);
389                 *fields |= DPAA2_ETH_DIST_L4DST;
390         }
391
392         /* Only apply the rule for IPv4 frames */
393         off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
394         *(__be16 *)(key + off) = htons(ETH_P_IP);
395         *(__be16 *)(mask + off) = htons(0xFFFF);
396         *fields |= DPAA2_ETH_DIST_ETHTYPE;
397
398         return 0;
399 }
400
401 static int prep_l4_rule(struct ethtool_tcpip4_spec *l4_value,
402                         struct ethtool_tcpip4_spec *l4_mask,
403                         void *key, void *mask, u8 l4_proto, u64 *fields)
404 {
405         int off;
406
407         if (l4_mask->tos)
408                 return -EOPNOTSUPP;
409
410         if (l4_mask->ip4src) {
411                 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_SRC);
412                 *(__be32 *)(key + off) = l4_value->ip4src;
413                 *(__be32 *)(mask + off) = l4_mask->ip4src;
414                 *fields |= DPAA2_ETH_DIST_IPSRC;
415         }
416
417         if (l4_mask->ip4dst) {
418                 off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_DST);
419                 *(__be32 *)(key + off) = l4_value->ip4dst;
420                 *(__be32 *)(mask + off) = l4_mask->ip4dst;
421                 *fields |= DPAA2_ETH_DIST_IPDST;
422         }
423
424         if (l4_mask->psrc) {
425                 off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_SRC);
426                 *(__be16 *)(key + off) = l4_value->psrc;
427                 *(__be16 *)(mask + off) = l4_mask->psrc;
428                 *fields |= DPAA2_ETH_DIST_L4SRC;
429         }
430
431         if (l4_mask->pdst) {
432                 off = dpaa2_eth_cls_fld_off(NET_PROT_UDP, NH_FLD_UDP_PORT_DST);
433                 *(__be16 *)(key + off) = l4_value->pdst;
434                 *(__be16 *)(mask + off) = l4_mask->pdst;
435                 *fields |= DPAA2_ETH_DIST_L4DST;
436         }
437
438         /* Only apply the rule for IPv4 frames with the specified L4 proto */
439         off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_TYPE);
440         *(__be16 *)(key + off) = htons(ETH_P_IP);
441         *(__be16 *)(mask + off) = htons(0xFFFF);
442         *fields |= DPAA2_ETH_DIST_ETHTYPE;
443
444         off = dpaa2_eth_cls_fld_off(NET_PROT_IP, NH_FLD_IP_PROTO);
445         *(u8 *)(key + off) = l4_proto;
446         *(u8 *)(mask + off) = 0xFF;
447         *fields |= DPAA2_ETH_DIST_IPPROTO;
448
449         return 0;
450 }
451
452 static int prep_ext_rule(struct ethtool_flow_ext *ext_value,
453                          struct ethtool_flow_ext *ext_mask,
454                          void *key, void *mask, u64 *fields)
455 {
456         int off;
457
458         if (ext_mask->vlan_etype)
459                 return -EOPNOTSUPP;
460
461         if (ext_mask->vlan_tci) {
462                 off = dpaa2_eth_cls_fld_off(NET_PROT_VLAN, NH_FLD_VLAN_TCI);
463                 *(__be16 *)(key + off) = ext_value->vlan_tci;
464                 *(__be16 *)(mask + off) = ext_mask->vlan_tci;
465                 *fields |= DPAA2_ETH_DIST_VLAN;
466         }
467
468         return 0;
469 }
470
471 static int prep_mac_ext_rule(struct ethtool_flow_ext *ext_value,
472                              struct ethtool_flow_ext *ext_mask,
473                              void *key, void *mask, u64 *fields)
474 {
475         int off;
476
477         if (!is_zero_ether_addr(ext_mask->h_dest)) {
478                 off = dpaa2_eth_cls_fld_off(NET_PROT_ETH, NH_FLD_ETH_DA);
479                 ether_addr_copy(key + off, ext_value->h_dest);
480                 ether_addr_copy(mask + off, ext_mask->h_dest);
481                 *fields |= DPAA2_ETH_DIST_ETHDST;
482         }
483
484         return 0;
485 }
486
487 static int prep_cls_rule(struct ethtool_rx_flow_spec *fs, void *key, void *mask,
488                          u64 *fields)
489 {
490         int err;
491
492         switch (fs->flow_type & 0xFF) {
493         case ETHER_FLOW:
494                 err = prep_eth_rule(&fs->h_u.ether_spec, &fs->m_u.ether_spec,
495                                     key, mask, fields);
496                 break;
497         case IP_USER_FLOW:
498                 err = prep_uip_rule(&fs->h_u.usr_ip4_spec,
499                                     &fs->m_u.usr_ip4_spec, key, mask, fields);
500                 break;
501         case TCP_V4_FLOW:
502                 err = prep_l4_rule(&fs->h_u.tcp_ip4_spec, &fs->m_u.tcp_ip4_spec,
503                                    key, mask, IPPROTO_TCP, fields);
504                 break;
505         case UDP_V4_FLOW:
506                 err = prep_l4_rule(&fs->h_u.udp_ip4_spec, &fs->m_u.udp_ip4_spec,
507                                    key, mask, IPPROTO_UDP, fields);
508                 break;
509         case SCTP_V4_FLOW:
510                 err = prep_l4_rule(&fs->h_u.sctp_ip4_spec,
511                                    &fs->m_u.sctp_ip4_spec, key, mask,
512                                    IPPROTO_SCTP, fields);
513                 break;
514         default:
515                 return -EOPNOTSUPP;
516         }
517
518         if (err)
519                 return err;
520
521         if (fs->flow_type & FLOW_EXT) {
522                 err = prep_ext_rule(&fs->h_ext, &fs->m_ext, key, mask, fields);
523                 if (err)
524                         return err;
525         }
526
527         if (fs->flow_type & FLOW_MAC_EXT) {
528                 err = prep_mac_ext_rule(&fs->h_ext, &fs->m_ext, key, mask,
529                                         fields);
530                 if (err)
531                         return err;
532         }
533
534         return 0;
535 }
536
537 static int do_cls_rule(struct net_device *net_dev,
538                        struct ethtool_rx_flow_spec *fs,
539                        bool add)
540 {
541         struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
542         struct device *dev = net_dev->dev.parent;
543         struct dpni_rule_cfg rule_cfg = { 0 };
544         struct dpni_fs_action_cfg fs_act = { 0 };
545         dma_addr_t key_iova;
546         u64 fields = 0;
547         void *key_buf;
548         int i, err;
549
550         if (fs->ring_cookie != RX_CLS_FLOW_DISC &&
551             fs->ring_cookie >= dpaa2_eth_queue_count(priv))
552                 return -EINVAL;
553
554         rule_cfg.key_size = dpaa2_eth_cls_key_size(DPAA2_ETH_DIST_ALL);
555
556         /* allocate twice the key size, for the actual key and for mask */
557         key_buf = kzalloc(rule_cfg.key_size * 2, GFP_KERNEL);
558         if (!key_buf)
559                 return -ENOMEM;
560
561         /* Fill the key and mask memory areas */
562         err = prep_cls_rule(fs, key_buf, key_buf + rule_cfg.key_size, &fields);
563         if (err)
564                 goto free_mem;
565
566         if (!dpaa2_eth_fs_mask_enabled(priv)) {
567                 /* Masking allows us to configure a maximal key during init and
568                  * use it for all flow steering rules. Without it, we include
569                  * in the key only the fields actually used, so we need to
570                  * extract the others from the final key buffer.
571                  *
572                  * Program the FS key if needed, or return error if previously
573                  * set key can't be used for the current rule. User needs to
574                  * delete existing rules in this case to allow for the new one.
575                  */
576                 if (!priv->rx_cls_fields) {
577                         err = dpaa2_eth_set_cls(net_dev, fields);
578                         if (err)
579                                 goto free_mem;
580
581                         priv->rx_cls_fields = fields;
582                 } else if (priv->rx_cls_fields != fields) {
583                         netdev_err(net_dev, "No support for multiple FS keys, need to delete existing rules\n");
584                         err = -EOPNOTSUPP;
585                         goto free_mem;
586                 }
587
588                 dpaa2_eth_cls_trim_rule(key_buf, fields);
589                 rule_cfg.key_size = dpaa2_eth_cls_key_size(fields);
590         }
591
592         key_iova = dma_map_single(dev, key_buf, rule_cfg.key_size * 2,
593                                   DMA_TO_DEVICE);
594         if (dma_mapping_error(dev, key_iova)) {
595                 err = -ENOMEM;
596                 goto free_mem;
597         }
598
599         rule_cfg.key_iova = key_iova;
600         if (dpaa2_eth_fs_mask_enabled(priv))
601                 rule_cfg.mask_iova = key_iova + rule_cfg.key_size;
602
603         if (add) {
604                 if (fs->ring_cookie == RX_CLS_FLOW_DISC)
605                         fs_act.options |= DPNI_FS_OPT_DISCARD;
606                 else
607                         fs_act.flow_id = fs->ring_cookie;
608         }
609         for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
610                 if (add)
611                         err = dpni_add_fs_entry(priv->mc_io, 0, priv->mc_token,
612                                                 i, fs->location, &rule_cfg,
613                                                 &fs_act);
614                 else
615                         err = dpni_remove_fs_entry(priv->mc_io, 0,
616                                                    priv->mc_token, i,
617                                                    &rule_cfg);
618                 if (err)
619                         break;
620         }
621
622         dma_unmap_single(dev, key_iova, rule_cfg.key_size * 2, DMA_TO_DEVICE);
623
624 free_mem:
625         kfree(key_buf);
626
627         return err;
628 }
629
630 static int num_rules(struct dpaa2_eth_priv *priv)
631 {
632         int i, rules = 0;
633
634         for (i = 0; i < dpaa2_eth_fs_count(priv); i++)
635                 if (priv->cls_rules[i].in_use)
636                         rules++;
637
638         return rules;
639 }
640
641 static int update_cls_rule(struct net_device *net_dev,
642                            struct ethtool_rx_flow_spec *new_fs,
643                            unsigned int location)
644 {
645         struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
646         struct dpaa2_eth_cls_rule *rule;
647         int err = -EINVAL;
648
649         if (!priv->rx_cls_enabled)
650                 return -EOPNOTSUPP;
651
652         if (location >= dpaa2_eth_fs_count(priv))
653                 return -EINVAL;
654
655         rule = &priv->cls_rules[location];
656
657         /* If a rule is present at the specified location, delete it. */
658         if (rule->in_use) {
659                 err = do_cls_rule(net_dev, &rule->fs, false);
660                 if (err)
661                         return err;
662
663                 rule->in_use = 0;
664
665                 if (!dpaa2_eth_fs_mask_enabled(priv) && !num_rules(priv))
666                         priv->rx_cls_fields = 0;
667         }
668
669         /* If no new entry to add, return here */
670         if (!new_fs)
671                 return err;
672
673         err = do_cls_rule(net_dev, new_fs, true);
674         if (err)
675                 return err;
676
677         rule->in_use = 1;
678         rule->fs = *new_fs;
679
680         return 0;
681 }
682
683 static int dpaa2_eth_get_rxnfc(struct net_device *net_dev,
684                                struct ethtool_rxnfc *rxnfc, u32 *rule_locs)
685 {
686         struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
687         int max_rules = dpaa2_eth_fs_count(priv);
688         int i, j = 0;
689
690         switch (rxnfc->cmd) {
691         case ETHTOOL_GRXFH:
692                 /* we purposely ignore cmd->flow_type for now, because the
693                  * classifier only supports a single set of fields for all
694                  * protocols
695                  */
696                 rxnfc->data = priv->rx_hash_fields;
697                 break;
698         case ETHTOOL_GRXRINGS:
699                 rxnfc->data = dpaa2_eth_queue_count(priv);
700                 break;
701         case ETHTOOL_GRXCLSRLCNT:
702                 rxnfc->rule_cnt = 0;
703                 rxnfc->rule_cnt = num_rules(priv);
704                 rxnfc->data = max_rules;
705                 break;
706         case ETHTOOL_GRXCLSRULE:
707                 if (rxnfc->fs.location >= max_rules)
708                         return -EINVAL;
709                 rxnfc->fs.location = array_index_nospec(rxnfc->fs.location,
710                                                         max_rules);
711                 if (!priv->cls_rules[rxnfc->fs.location].in_use)
712                         return -EINVAL;
713                 rxnfc->fs = priv->cls_rules[rxnfc->fs.location].fs;
714                 break;
715         case ETHTOOL_GRXCLSRLALL:
716                 for (i = 0; i < max_rules; i++) {
717                         if (!priv->cls_rules[i].in_use)
718                                 continue;
719                         if (j == rxnfc->rule_cnt)
720                                 return -EMSGSIZE;
721                         rule_locs[j++] = i;
722                 }
723                 rxnfc->rule_cnt = j;
724                 rxnfc->data = max_rules;
725                 break;
726         default:
727                 return -EOPNOTSUPP;
728         }
729
730         return 0;
731 }
732
733 static int dpaa2_eth_set_rxnfc(struct net_device *net_dev,
734                                struct ethtool_rxnfc *rxnfc)
735 {
736         int err = 0;
737
738         switch (rxnfc->cmd) {
739         case ETHTOOL_SRXFH:
740                 if ((rxnfc->data & DPAA2_RXH_SUPPORTED) != rxnfc->data)
741                         return -EOPNOTSUPP;
742                 err = dpaa2_eth_set_hash(net_dev, rxnfc->data);
743                 break;
744         case ETHTOOL_SRXCLSRLINS:
745                 err = update_cls_rule(net_dev, &rxnfc->fs, rxnfc->fs.location);
746                 break;
747         case ETHTOOL_SRXCLSRLDEL:
748                 err = update_cls_rule(net_dev, NULL, rxnfc->fs.location);
749                 break;
750         default:
751                 err = -EOPNOTSUPP;
752         }
753
754         return err;
755 }
756
757 int dpaa2_phc_index = -1;
758 EXPORT_SYMBOL(dpaa2_phc_index);
759
760 static int dpaa2_eth_get_ts_info(struct net_device *dev,
761                                  struct ethtool_ts_info *info)
762 {
763         info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
764                                 SOF_TIMESTAMPING_RX_HARDWARE |
765                                 SOF_TIMESTAMPING_RAW_HARDWARE;
766
767         info->phc_index = dpaa2_phc_index;
768
769         info->tx_types = (1 << HWTSTAMP_TX_OFF) |
770                          (1 << HWTSTAMP_TX_ON);
771
772         info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
773                            (1 << HWTSTAMP_FILTER_ALL);
774         return 0;
775 }
776
777 const struct ethtool_ops dpaa2_ethtool_ops = {
778         .get_drvinfo = dpaa2_eth_get_drvinfo,
779         .nway_reset = dpaa2_eth_nway_reset,
780         .get_link = ethtool_op_get_link,
781         .get_link_ksettings = dpaa2_eth_get_link_ksettings,
782         .set_link_ksettings = dpaa2_eth_set_link_ksettings,
783         .get_pauseparam = dpaa2_eth_get_pauseparam,
784         .set_pauseparam = dpaa2_eth_set_pauseparam,
785         .get_sset_count = dpaa2_eth_get_sset_count,
786         .get_ethtool_stats = dpaa2_eth_get_ethtool_stats,
787         .get_strings = dpaa2_eth_get_strings,
788         .get_rxnfc = dpaa2_eth_get_rxnfc,
789         .set_rxnfc = dpaa2_eth_set_rxnfc,
790         .get_ts_info = dpaa2_eth_get_ts_info,
791 };