Skip to content

Commit f0ee7ac

Browse files
Manfred Rudigierdavem330
authored andcommitted
gianfar: Add hardware TX timestamping support
If a packet has the skb_shared_tx->hardware flag set the device is instructed to generate a TX timestamp and write it back to memory after the frame is transmitted. During the clean_tx_ring operation the timestamp will be extracted and copied into the skb_shared_hwtstamps struct of the skb. TX timestamping is enabled by setting the tx_type to something else than HWTSTAMP_TX_OFF with the SIOCSHWTSTAMP ioctl command. It is only supported by eTSEC devices. Signed-off-by: Manfred Rudigier <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent cc772ab commit f0ee7ac

File tree

2 files changed

+101
-20
lines changed

2 files changed

+101
-20
lines changed

drivers/net/gianfar.c

Lines changed: 99 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,18 @@ static int gfar_hwtstamp_ioctl(struct net_device *netdev,
795795
if (config.flags)
796796
return -EINVAL;
797797

798-
if (config.tx_type)
798+
switch (config.tx_type) {
799+
case HWTSTAMP_TX_OFF:
800+
priv->hwts_tx_en = 0;
801+
break;
802+
case HWTSTAMP_TX_ON:
803+
if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
804+
return -ERANGE;
805+
priv->hwts_tx_en = 1;
806+
break;
807+
default:
799808
return -ERANGE;
809+
}
800810

801811
switch (config.rx_filter) {
802812
case HWTSTAMP_FILTER_NONE:
@@ -1972,23 +1982,29 @@ static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
19721982
struct netdev_queue *txq;
19731983
struct gfar __iomem *regs = NULL;
19741984
struct txfcb *fcb = NULL;
1975-
struct txbd8 *txbdp, *txbdp_start, *base;
1985+
struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
19761986
u32 lstatus;
1977-
int i, rq = 0;
1987+
int i, rq = 0, do_tstamp = 0;
19781988
u32 bufaddr;
19791989
unsigned long flags;
1980-
unsigned int nr_frags, length;
1981-
1990+
unsigned int nr_frags, nr_txbds, length;
1991+
union skb_shared_tx *shtx;
19821992

19831993
rq = skb->queue_mapping;
19841994
tx_queue = priv->tx_queue[rq];
19851995
txq = netdev_get_tx_queue(dev, rq);
19861996
base = tx_queue->tx_bd_base;
19871997
regs = tx_queue->grp->regs;
1998+
shtx = skb_tx(skb);
1999+
2000+
/* check if time stamp should be generated */
2001+
if (unlikely(shtx->hardware && priv->hwts_tx_en))
2002+
do_tstamp = 1;
19882003

19892004
/* make space for additional header when fcb is needed */
19902005
if (((skb->ip_summed == CHECKSUM_PARTIAL) ||
1991-
(priv->vlgrp && vlan_tx_tag_present(skb))) &&
2006+
(priv->vlgrp && vlan_tx_tag_present(skb)) ||
2007+
unlikely(do_tstamp)) &&
19922008
(skb_headroom(skb) < GMAC_FCB_LEN)) {
19932009
struct sk_buff *skb_new;
19942010

@@ -2005,8 +2021,14 @@ static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
20052021
/* total number of fragments in the SKB */
20062022
nr_frags = skb_shinfo(skb)->nr_frags;
20072023

2024+
/* calculate the required number of TxBDs for this skb */
2025+
if (unlikely(do_tstamp))
2026+
nr_txbds = nr_frags + 2;
2027+
else
2028+
nr_txbds = nr_frags + 1;
2029+
20082030
/* check if there is space to queue this packet */
2009-
if ((nr_frags+1) > tx_queue->num_txbdfree) {
2031+
if (nr_txbds > tx_queue->num_txbdfree) {
20102032
/* no space, stop the queue */
20112033
netif_tx_stop_queue(txq);
20122034
dev->stats.tx_fifo_errors++;
@@ -2018,9 +2040,19 @@ static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
20182040
txq->tx_packets ++;
20192041

20202042
txbdp = txbdp_start = tx_queue->cur_tx;
2043+
lstatus = txbdp->lstatus;
2044+
2045+
/* Time stamp insertion requires one additional TxBD */
2046+
if (unlikely(do_tstamp))
2047+
txbdp_tstamp = txbdp = next_txbd(txbdp, base,
2048+
tx_queue->tx_ring_size);
20212049

20222050
if (nr_frags == 0) {
2023-
lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2051+
if (unlikely(do_tstamp))
2052+
txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
2053+
TXBD_INTERRUPT);
2054+
else
2055+
lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
20242056
} else {
20252057
/* Place the fragment addresses and lengths into the TxBDs */
20262058
for (i = 0; i < nr_frags; i++) {
@@ -2066,11 +2098,32 @@ static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
20662098
gfar_tx_vlan(skb, fcb);
20672099
}
20682100

2069-
/* setup the TxBD length and buffer pointer for the first BD */
2101+
/* Setup tx hardware time stamping if requested */
2102+
if (unlikely(do_tstamp)) {
2103+
shtx->in_progress = 1;
2104+
if (fcb == NULL)
2105+
fcb = gfar_add_fcb(skb);
2106+
fcb->ptp = 1;
2107+
lstatus |= BD_LFLAG(TXBD_TOE);
2108+
}
2109+
20702110
txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
20712111
skb_headlen(skb), DMA_TO_DEVICE);
20722112

2073-
lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2113+
/*
2114+
* If time stamping is requested one additional TxBD must be set up. The
2115+
* first TxBD points to the FCB and must have a data length of
2116+
* GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2117+
* the full frame length.
2118+
*/
2119+
if (unlikely(do_tstamp)) {
2120+
txbdp_tstamp->bufPtr = txbdp_start->bufPtr + GMAC_FCB_LEN;
2121+
txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
2122+
(skb_headlen(skb) - GMAC_FCB_LEN);
2123+
lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2124+
} else {
2125+
lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2126+
}
20742127

20752128
/*
20762129
* We can work in parallel with gfar_clean_tx_ring(), except
@@ -2110,7 +2163,7 @@ static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
21102163
tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
21112164

21122165
/* reduce TxBD free count */
2113-
tx_queue->num_txbdfree -= (nr_frags + 1);
2166+
tx_queue->num_txbdfree -= (nr_txbds);
21142167

21152168
dev->trans_start = jiffies;
21162169

@@ -2301,16 +2354,18 @@ static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
23012354
struct net_device *dev = tx_queue->dev;
23022355
struct gfar_private *priv = netdev_priv(dev);
23032356
struct gfar_priv_rx_q *rx_queue = NULL;
2304-
struct txbd8 *bdp;
2357+
struct txbd8 *bdp, *next = NULL;
23052358
struct txbd8 *lbdp = NULL;
23062359
struct txbd8 *base = tx_queue->tx_bd_base;
23072360
struct sk_buff *skb;
23082361
int skb_dirtytx;
23092362
int tx_ring_size = tx_queue->tx_ring_size;
2310-
int frags = 0;
2363+
int frags = 0, nr_txbds = 0;
23112364
int i;
23122365
int howmany = 0;
23132366
u32 lstatus;
2367+
size_t buflen;
2368+
union skb_shared_tx *shtx;
23142369

23152370
rx_queue = priv->rx_queue[tx_queue->qindex];
23162371
bdp = tx_queue->dirty_tx;
@@ -2320,7 +2375,18 @@ static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
23202375
unsigned long flags;
23212376

23222377
frags = skb_shinfo(skb)->nr_frags;
2323-
lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
2378+
2379+
/*
2380+
* When time stamping, one additional TxBD must be freed.
2381+
* Also, we need to dma_unmap_single() the TxPAL.
2382+
*/
2383+
shtx = skb_tx(skb);
2384+
if (unlikely(shtx->in_progress))
2385+
nr_txbds = frags + 2;
2386+
else
2387+
nr_txbds = frags + 1;
2388+
2389+
lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
23242390

23252391
lstatus = lbdp->lstatus;
23262392

@@ -2329,10 +2395,24 @@ static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
23292395
(lstatus & BD_LENGTH_MASK))
23302396
break;
23312397

2332-
dma_unmap_single(&priv->ofdev->dev,
2333-
bdp->bufPtr,
2334-
bdp->length,
2335-
DMA_TO_DEVICE);
2398+
if (unlikely(shtx->in_progress)) {
2399+
next = next_txbd(bdp, base, tx_ring_size);
2400+
buflen = next->length + GMAC_FCB_LEN;
2401+
} else
2402+
buflen = bdp->length;
2403+
2404+
dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
2405+
buflen, DMA_TO_DEVICE);
2406+
2407+
if (unlikely(shtx->in_progress)) {
2408+
struct skb_shared_hwtstamps shhwtstamps;
2409+
u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
2410+
memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2411+
shhwtstamps.hwtstamp = ns_to_ktime(*ns);
2412+
skb_tstamp_tx(skb, &shhwtstamps);
2413+
bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2414+
bdp = next;
2415+
}
23362416

23372417
bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
23382418
bdp = next_txbd(bdp, base, tx_ring_size);
@@ -2364,7 +2444,7 @@ static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
23642444

23652445
howmany++;
23662446
spin_lock_irqsave(&tx_queue->txlock, flags);
2367-
tx_queue->num_txbdfree += frags + 1;
2447+
tx_queue->num_txbdfree += nr_txbds;
23682448
spin_unlock_irqrestore(&tx_queue->txlock, flags);
23692449
}
23702450

drivers/net/gianfar.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ struct txbd8
540540

541541
struct txfcb {
542542
u8 flags;
543-
u8 reserved;
543+
u8 ptp; /* Flag to enable tx timestamping */
544544
u8 l4os; /* Level 4 Header Offset */
545545
u8 l3os; /* Level 3 Header Offset */
546546
u16 phcs; /* Pseudo-header Checksum */
@@ -1105,6 +1105,7 @@ struct gfar_private {
11051105

11061106
/* HW time stamping enabled flag */
11071107
int hwts_rx_en;
1108+
int hwts_tx_en;
11081109
};
11091110

11101111
extern unsigned int ftp_rqfpr[MAX_FILER_IDX + 1];

0 commit comments

Comments
 (0)