Skip to content

Commit cc772ab

Browse files
Manfred Rudigierdavem330
authored andcommitted
gianfar: Add hardware RX timestamping support
The device is configured to insert hardware timestamps into all received packets. The RX timestamps are extracted from the padding alingment bytes during the clean_rx_ring operation and copied into the skb_shared_hwtstamps struct of the skb. This extraction only happens if the rx_filter was set to something else than HWTSTAMP_FILTER_NONE with the SIOCSHWTSTAMP ioctl command. Hardware timestamping is only supported for eTSEC devices. To indicate device support the new FSL_GIANFAR_DEV_HAS_TIMER flag was introduced. Signed-off-by: Manfred Rudigier <manfred.rudigier@omicron.at> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent e44171f commit cc772ab

2 files changed

Lines changed: 66 additions & 5 deletions

File tree

‎drivers/net/gianfar.c‎

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#include <linux/tcp.h>
8383
#include <linux/udp.h>
8484
#include <linux/in.h>
85+
#include <linux/net_tstamp.h>
8586

8687
#include <asm/io.h>
8788
#include <asm/irq.h>
@@ -377,6 +378,13 @@ static void gfar_init_mac(struct net_device *ndev)
377378
rctrl |= RCTRL_PADDING(priv->padding);
378379
}
379380

381+
/* Insert receive time stamps into padding alignment bytes */
382+
if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER) {
383+
rctrl &= ~RCTRL_PAL_MASK;
384+
rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE | RCTRL_PADDING(8);
385+
priv->padding = 8;
386+
}
387+
380388
/* keep vlan related bits if it's enabled */
381389
if (priv->vlgrp) {
382390
rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
@@ -501,7 +509,8 @@ void unlock_tx_qs(struct gfar_private *priv)
501509
/* Returns 1 if incoming frames use an FCB */
502510
static inline int gfar_uses_fcb(struct gfar_private *priv)
503511
{
504-
return priv->vlgrp || priv->rx_csum_enable;
512+
return priv->vlgrp || priv->rx_csum_enable ||
513+
(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER);
505514
}
506515

507516
static void free_tx_pointers(struct gfar_private *priv)
@@ -742,7 +751,8 @@ static int gfar_of_init(struct of_device *ofdev, struct net_device **pdev)
742751
FSL_GIANFAR_DEV_HAS_CSUM |
743752
FSL_GIANFAR_DEV_HAS_VLAN |
744753
FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
745-
FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
754+
FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
755+
FSL_GIANFAR_DEV_HAS_TIMER;
746756

747757
ctype = of_get_property(np, "phy-connection-type", NULL);
748758

@@ -772,6 +782,38 @@ static int gfar_of_init(struct of_device *ofdev, struct net_device **pdev)
772782
return err;
773783
}
774784

785+
static int gfar_hwtstamp_ioctl(struct net_device *netdev,
786+
struct ifreq *ifr, int cmd)
787+
{
788+
struct hwtstamp_config config;
789+
struct gfar_private *priv = netdev_priv(netdev);
790+
791+
if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
792+
return -EFAULT;
793+
794+
/* reserved for future extensions */
795+
if (config.flags)
796+
return -EINVAL;
797+
798+
if (config.tx_type)
799+
return -ERANGE;
800+
801+
switch (config.rx_filter) {
802+
case HWTSTAMP_FILTER_NONE:
803+
priv->hwts_rx_en = 0;
804+
break;
805+
default:
806+
if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
807+
return -ERANGE;
808+
priv->hwts_rx_en = 1;
809+
config.rx_filter = HWTSTAMP_FILTER_ALL;
810+
break;
811+
}
812+
813+
return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
814+
-EFAULT : 0;
815+
}
816+
775817
/* Ioctl MII Interface */
776818
static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
777819
{
@@ -780,6 +822,9 @@ static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
780822
if (!netif_running(dev))
781823
return -EINVAL;
782824

825+
if (cmd == SIOCSHWTSTAMP)
826+
return gfar_hwtstamp_ioctl(dev, rq, cmd);
827+
783828
if (!priv->phydev)
784829
return -ENODEV;
785830

@@ -982,7 +1027,8 @@ static int gfar_probe(struct of_device *ofdev,
9821027
else
9831028
priv->padding = 0;
9841029

985-
if (dev->features & NETIF_F_IP_CSUM)
1030+
if (dev->features & NETIF_F_IP_CSUM ||
1031+
priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
9861032
dev->hard_header_len += GMAC_FCB_LEN;
9871033

9881034
/* Program the isrg regs only if number of grps > 1 */
@@ -2474,6 +2520,17 @@ static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
24742520
skb_pull(skb, amount_pull);
24752521
}
24762522

2523+
/* Get receive timestamp from the skb */
2524+
if (priv->hwts_rx_en) {
2525+
struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2526+
u64 *ns = (u64 *) skb->data;
2527+
memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2528+
shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2529+
}
2530+
2531+
if (priv->padding)
2532+
skb_pull(skb, priv->padding);
2533+
24772534
if (priv->rx_csum_enable)
24782535
gfar_rx_checksum(skb, fcb);
24792536

@@ -2510,8 +2567,7 @@ int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
25102567
bdp = rx_queue->cur_rx;
25112568
base = rx_queue->rx_bd_base;
25122569

2513-
amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
2514-
priv->padding;
2570+
amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0);
25152571

25162572
while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
25172573
struct sk_buff *newskb;

‎drivers/net/gianfar.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ extern const char gfar_driver_version[];
262262

263263
#define next_bd(bdp, base, ring_size) skip_bd(bdp, 1, base, ring_size)
264264

265+
#define RCTRL_TS_ENABLE 0x01000000
265266
#define RCTRL_PAL_MASK 0x001f0000
266267
#define RCTRL_VLEX 0x00002000
267268
#define RCTRL_FILREN 0x00001000
@@ -885,6 +886,7 @@ struct gfar {
885886
#define FSL_GIANFAR_DEV_HAS_MAGIC_PACKET 0x00000100
886887
#define FSL_GIANFAR_DEV_HAS_BD_STASHING 0x00000200
887888
#define FSL_GIANFAR_DEV_HAS_BUF_STASHING 0x00000400
889+
#define FSL_GIANFAR_DEV_HAS_TIMER 0x00000800
888890

889891
#if (MAXGROUPS == 2)
890892
#define DEFAULT_MAPPING 0xAA
@@ -1100,6 +1102,9 @@ struct gfar_private {
11001102

11011103
/* Network Statistics */
11021104
struct gfar_extra_stats extra_stats;
1105+
1106+
/* HW time stamping enabled flag */
1107+
int hwts_rx_en;
11031108
};
11041109

11051110
extern unsigned int ftp_rqfpr[MAX_FILER_IDX + 1];

0 commit comments

Comments
 (0)