1 /* Copyright 2008-2016 Freescale Semiconductor, Inc.
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5 * * Redistributions of source code must retain the above copyright
6 * notice, this list of conditions and the following disclaimer.
7 * * Redistributions in binary form must reproduce the above copyright
8 * notice, this list of conditions and the following disclaimer in the
9 * documentation and/or other materials provided with the distribution.
10 * * Neither the name of Freescale Semiconductor nor the
11 * names of its contributors may be used to endorse or promote products
12 * derived from this software without specific prior written permission.
15 * ALTERNATIVELY, this software may be distributed under the terms of the
16 * GNU General Public License ("GPL") as published by the Free Software
17 * Foundation, either version 2 of that License or (at your option) any
20 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34 #include <linux/string.h>
35 #include <linux/of_platform.h>
36 #include <linux/net_tstamp.h>
37 #include <linux/fsl/ptp_qoriq.h>
42 static const char dpaa_stats_percpu[][ETH_GSTRING_LEN] = {
52 static char dpaa_stats_global[][ETH_GSTRING_LEN] = {
55 "rx frame physical error",
56 "rx frame size error",
59 /* demultiplexing errors */
69 /* congestion related stats */
70 "congestion time (ms)",
75 #define DPAA_STATS_PERCPU_LEN ARRAY_SIZE(dpaa_stats_percpu)
76 #define DPAA_STATS_GLOBAL_LEN ARRAY_SIZE(dpaa_stats_global)
78 static int dpaa_get_link_ksettings(struct net_device *net_dev,
79 struct ethtool_link_ksettings *cmd)
81 if (!net_dev->phydev) {
82 netdev_dbg(net_dev, "phy device not initialized\n");
86 phy_ethtool_ksettings_get(net_dev->phydev, cmd);
91 static int dpaa_set_link_ksettings(struct net_device *net_dev,
92 const struct ethtool_link_ksettings *cmd)
96 if (!net_dev->phydev) {
97 netdev_err(net_dev, "phy device not initialized\n");
101 err = phy_ethtool_ksettings_set(net_dev->phydev, cmd);
103 netdev_err(net_dev, "phy_ethtool_ksettings_set() = %d\n", err);
108 static void dpaa_get_drvinfo(struct net_device *net_dev,
109 struct ethtool_drvinfo *drvinfo)
113 strlcpy(drvinfo->driver, KBUILD_MODNAME,
114 sizeof(drvinfo->driver));
115 len = snprintf(drvinfo->version, sizeof(drvinfo->version),
117 len = snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
120 if (len >= sizeof(drvinfo->fw_version)) {
121 /* Truncated output */
122 netdev_notice(net_dev, "snprintf() = %d\n", len);
124 strlcpy(drvinfo->bus_info, dev_name(net_dev->dev.parent->parent),
125 sizeof(drvinfo->bus_info));
128 static u32 dpaa_get_msglevel(struct net_device *net_dev)
130 return ((struct dpaa_priv *)netdev_priv(net_dev))->msg_enable;
133 static void dpaa_set_msglevel(struct net_device *net_dev,
136 ((struct dpaa_priv *)netdev_priv(net_dev))->msg_enable = msg_enable;
139 static int dpaa_nway_reset(struct net_device *net_dev)
143 if (!net_dev->phydev) {
144 netdev_err(net_dev, "phy device not initialized\n");
149 if (net_dev->phydev->autoneg) {
150 err = phy_start_aneg(net_dev->phydev);
152 netdev_err(net_dev, "phy_start_aneg() = %d\n",
159 static void dpaa_get_pauseparam(struct net_device *net_dev,
160 struct ethtool_pauseparam *epause)
162 struct mac_device *mac_dev;
163 struct dpaa_priv *priv;
165 priv = netdev_priv(net_dev);
166 mac_dev = priv->mac_dev;
168 if (!net_dev->phydev) {
169 netdev_err(net_dev, "phy device not initialized\n");
173 epause->autoneg = mac_dev->autoneg_pause;
174 epause->rx_pause = mac_dev->rx_pause_active;
175 epause->tx_pause = mac_dev->tx_pause_active;
178 static int dpaa_set_pauseparam(struct net_device *net_dev,
179 struct ethtool_pauseparam *epause)
181 struct mac_device *mac_dev;
182 struct phy_device *phydev;
183 bool rx_pause, tx_pause;
184 struct dpaa_priv *priv;
187 priv = netdev_priv(net_dev);
188 mac_dev = priv->mac_dev;
190 phydev = net_dev->phydev;
192 netdev_err(net_dev, "phy device not initialized\n");
196 if (!phy_validate_pause(phydev, epause))
199 /* The MAC should know how to handle PAUSE frame autonegotiation before
200 * adjust_link is triggered by a forced renegotiation of sym/asym PAUSE
203 mac_dev->autoneg_pause = !!epause->autoneg;
204 mac_dev->rx_pause_req = !!epause->rx_pause;
205 mac_dev->tx_pause_req = !!epause->tx_pause;
207 /* Determine the sym/asym advertised PAUSE capabilities from the desired
208 * rx/tx pause settings.
211 phy_set_asym_pause(phydev, epause->rx_pause, epause->tx_pause);
213 fman_get_pause_cfg(mac_dev, &rx_pause, &tx_pause);
214 err = fman_set_mac_active_pause(mac_dev, rx_pause, tx_pause);
216 netdev_err(net_dev, "set_mac_active_pause() = %d\n", err);
221 static int dpaa_get_sset_count(struct net_device *net_dev, int type)
223 unsigned int total_stats, num_stats;
225 num_stats = num_online_cpus() + 1;
226 total_stats = num_stats * (DPAA_STATS_PERCPU_LEN + DPAA_BPS_NUM) +
227 DPAA_STATS_GLOBAL_LEN;
237 static void copy_stats(struct dpaa_percpu_priv *percpu_priv, int num_cpus,
238 int crr_cpu, u64 *bp_count, u64 *data)
240 int num_values = num_cpus + 1;
243 /* update current CPU's stats and also add them to the total values */
244 data[crr * num_values + crr_cpu] = percpu_priv->in_interrupt;
245 data[crr++ * num_values + num_cpus] += percpu_priv->in_interrupt;
247 data[crr * num_values + crr_cpu] = percpu_priv->stats.rx_packets;
248 data[crr++ * num_values + num_cpus] += percpu_priv->stats.rx_packets;
250 data[crr * num_values + crr_cpu] = percpu_priv->stats.tx_packets;
251 data[crr++ * num_values + num_cpus] += percpu_priv->stats.tx_packets;
253 data[crr * num_values + crr_cpu] = percpu_priv->tx_confirm;
254 data[crr++ * num_values + num_cpus] += percpu_priv->tx_confirm;
256 data[crr * num_values + crr_cpu] = percpu_priv->tx_frag_skbuffs;
257 data[crr++ * num_values + num_cpus] += percpu_priv->tx_frag_skbuffs;
259 data[crr * num_values + crr_cpu] = percpu_priv->stats.tx_errors;
260 data[crr++ * num_values + num_cpus] += percpu_priv->stats.tx_errors;
262 data[crr * num_values + crr_cpu] = percpu_priv->stats.rx_errors;
263 data[crr++ * num_values + num_cpus] += percpu_priv->stats.rx_errors;
265 for (j = 0; j < DPAA_BPS_NUM; j++) {
266 data[crr * num_values + crr_cpu] = bp_count[j];
267 data[crr++ * num_values + num_cpus] += bp_count[j];
271 static void dpaa_get_ethtool_stats(struct net_device *net_dev,
272 struct ethtool_stats *stats, u64 *data)
274 u64 bp_count[DPAA_BPS_NUM], cg_time, cg_num;
275 struct dpaa_percpu_priv *percpu_priv;
276 struct dpaa_rx_errors rx_errors;
277 unsigned int num_cpus, offset;
278 struct dpaa_ern_cnt ern_cnt;
279 struct dpaa_bp *dpaa_bp;
280 struct dpaa_priv *priv;
281 int total_stats, i, j;
284 total_stats = dpaa_get_sset_count(net_dev, ETH_SS_STATS);
285 priv = netdev_priv(net_dev);
286 num_cpus = num_online_cpus();
288 memset(&bp_count, 0, sizeof(bp_count));
289 memset(&rx_errors, 0, sizeof(struct dpaa_rx_errors));
290 memset(&ern_cnt, 0, sizeof(struct dpaa_ern_cnt));
291 memset(data, 0, total_stats * sizeof(u64));
293 for_each_online_cpu(i) {
294 percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
295 for (j = 0; j < DPAA_BPS_NUM; j++) {
296 dpaa_bp = priv->dpaa_bps[j];
297 if (!dpaa_bp->percpu_count)
299 bp_count[j] = *(per_cpu_ptr(dpaa_bp->percpu_count, i));
301 rx_errors.dme += percpu_priv->rx_errors.dme;
302 rx_errors.fpe += percpu_priv->rx_errors.fpe;
303 rx_errors.fse += percpu_priv->rx_errors.fse;
304 rx_errors.phe += percpu_priv->rx_errors.phe;
306 ern_cnt.cg_tdrop += percpu_priv->ern_cnt.cg_tdrop;
307 ern_cnt.wred += percpu_priv->ern_cnt.wred;
308 ern_cnt.err_cond += percpu_priv->ern_cnt.err_cond;
309 ern_cnt.early_window += percpu_priv->ern_cnt.early_window;
310 ern_cnt.late_window += percpu_priv->ern_cnt.late_window;
311 ern_cnt.fq_tdrop += percpu_priv->ern_cnt.fq_tdrop;
312 ern_cnt.fq_retired += percpu_priv->ern_cnt.fq_retired;
313 ern_cnt.orp_zero += percpu_priv->ern_cnt.orp_zero;
315 copy_stats(percpu_priv, num_cpus, i, bp_count, data);
318 offset = (num_cpus + 1) * (DPAA_STATS_PERCPU_LEN + DPAA_BPS_NUM);
319 memcpy(data + offset, &rx_errors, sizeof(struct dpaa_rx_errors));
321 offset += sizeof(struct dpaa_rx_errors) / sizeof(u64);
322 memcpy(data + offset, &ern_cnt, sizeof(struct dpaa_ern_cnt));
324 /* gather congestion related counters */
327 cg_time = jiffies_to_msecs(priv->cgr_data.congested_jiffies);
328 if (qman_query_cgr_congested(&priv->cgr_data.cgr, &cg_status) == 0) {
329 cg_num = priv->cgr_data.cgr_congested_count;
331 /* reset congestion stats (like QMan API does */
332 priv->cgr_data.congested_jiffies = 0;
333 priv->cgr_data.cgr_congested_count = 0;
336 offset += sizeof(struct dpaa_ern_cnt) / sizeof(u64);
337 data[offset++] = cg_time;
338 data[offset++] = cg_num;
339 data[offset++] = cg_status;
342 static void dpaa_get_strings(struct net_device *net_dev, u32 stringset,
345 unsigned int i, j, num_cpus, size;
346 char string_cpu[ETH_GSTRING_LEN];
349 memset(string_cpu, 0, sizeof(string_cpu));
351 num_cpus = num_online_cpus();
352 size = DPAA_STATS_GLOBAL_LEN * ETH_GSTRING_LEN;
354 for (i = 0; i < DPAA_STATS_PERCPU_LEN; i++) {
355 for (j = 0; j < num_cpus; j++) {
356 snprintf(string_cpu, ETH_GSTRING_LEN, "%s [CPU %d]",
357 dpaa_stats_percpu[i], j);
358 memcpy(strings, string_cpu, ETH_GSTRING_LEN);
359 strings += ETH_GSTRING_LEN;
361 snprintf(string_cpu, ETH_GSTRING_LEN, "%s [TOTAL]",
362 dpaa_stats_percpu[i]);
363 memcpy(strings, string_cpu, ETH_GSTRING_LEN);
364 strings += ETH_GSTRING_LEN;
366 for (i = 0; i < DPAA_BPS_NUM; i++) {
367 for (j = 0; j < num_cpus; j++) {
368 snprintf(string_cpu, ETH_GSTRING_LEN,
369 "bpool %c [CPU %d]", 'a' + i, j);
370 memcpy(strings, string_cpu, ETH_GSTRING_LEN);
371 strings += ETH_GSTRING_LEN;
373 snprintf(string_cpu, ETH_GSTRING_LEN, "bpool %c [TOTAL]",
375 memcpy(strings, string_cpu, ETH_GSTRING_LEN);
376 strings += ETH_GSTRING_LEN;
378 memcpy(strings, dpaa_stats_global, size);
381 static int dpaa_get_hash_opts(struct net_device *dev,
382 struct ethtool_rxnfc *cmd)
384 struct dpaa_priv *priv = netdev_priv(dev);
388 switch (cmd->flow_type) {
393 if (priv->keygen_in_use)
394 cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
406 if (priv->keygen_in_use)
407 cmd->data |= RXH_IP_SRC | RXH_IP_DST;
417 static int dpaa_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
420 int ret = -EOPNOTSUPP;
424 ret = dpaa_get_hash_opts(dev, cmd);
433 static void dpaa_set_hash(struct net_device *net_dev, bool enable)
435 struct mac_device *mac_dev;
436 struct fman_port *rxport;
437 struct dpaa_priv *priv;
439 priv = netdev_priv(net_dev);
440 mac_dev = priv->mac_dev;
441 rxport = mac_dev->port[0];
443 fman_port_use_kg_hash(rxport, enable);
444 priv->keygen_in_use = enable;
447 static int dpaa_set_hash_opts(struct net_device *dev,
448 struct ethtool_rxnfc *nfc)
452 /* we support hashing on IPv4/v6 src/dest IP and L4 src/dest port */
454 ~(RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3))
457 switch (nfc->flow_type) {
472 dpaa_set_hash(dev, !!nfc->data);
482 static int dpaa_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
484 int ret = -EOPNOTSUPP;
488 ret = dpaa_set_hash_opts(dev, cmd);
497 static int dpaa_get_ts_info(struct net_device *net_dev,
498 struct ethtool_ts_info *info)
500 struct device *dev = net_dev->dev.parent;
501 struct device_node *mac_node = dev->of_node;
502 struct device_node *fman_node = NULL, *ptp_node = NULL;
503 struct platform_device *ptp_dev = NULL;
504 struct qoriq_ptp *ptp = NULL;
506 info->phc_index = -1;
508 fman_node = of_get_parent(mac_node);
510 ptp_node = of_parse_phandle(fman_node, "ptimer-handle", 0);
513 ptp_dev = of_find_device_by_node(ptp_node);
516 ptp = platform_get_drvdata(ptp_dev);
519 info->phc_index = ptp->phc_index;
521 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
522 SOF_TIMESTAMPING_RX_HARDWARE |
523 SOF_TIMESTAMPING_RAW_HARDWARE;
524 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
525 (1 << HWTSTAMP_TX_ON);
526 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
527 (1 << HWTSTAMP_FILTER_ALL);
532 const struct ethtool_ops dpaa_ethtool_ops = {
533 .get_drvinfo = dpaa_get_drvinfo,
534 .get_msglevel = dpaa_get_msglevel,
535 .set_msglevel = dpaa_set_msglevel,
536 .nway_reset = dpaa_nway_reset,
537 .get_pauseparam = dpaa_get_pauseparam,
538 .set_pauseparam = dpaa_set_pauseparam,
539 .get_link = ethtool_op_get_link,
540 .get_sset_count = dpaa_get_sset_count,
541 .get_ethtool_stats = dpaa_get_ethtool_stats,
542 .get_strings = dpaa_get_strings,
543 .get_link_ksettings = dpaa_get_link_ksettings,
544 .set_link_ksettings = dpaa_set_link_ksettings,
545 .get_rxnfc = dpaa_get_rxnfc,
546 .set_rxnfc = dpaa_set_rxnfc,
547 .get_ts_info = dpaa_get_ts_info,