Skip to content

Commit 9ab5ec5

Browse files
cmetcalf-tileradavem330
authored andcommitted
tile: support PTP using the tilegx mPIPE (IEEE 1588)
Signed-off-by: Chris Metcalf <cmetcalf@tilera.com> Acked-by: Richard Cochran <richardcochran@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 84e181b commit 9ab5ec5

5 files changed

Lines changed: 266 additions & 4 deletions

File tree

‎arch/tile/gxio/iorpc_mpipe.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,25 @@ int gxio_mpipe_adjust_timestamp_aux(gxio_mpipe_context_t * context,
475475

476476
EXPORT_SYMBOL(gxio_mpipe_adjust_timestamp_aux);
477477

478+
struct adjust_timestamp_freq_param {
479+
int32_t ppb;
480+
};
481+
482+
int gxio_mpipe_adjust_timestamp_freq(gxio_mpipe_context_t * context,
483+
int32_t ppb)
484+
{
485+
struct adjust_timestamp_freq_param temp;
486+
struct adjust_timestamp_freq_param *params = &temp;
487+
488+
params->ppb = ppb;
489+
490+
return hv_dev_pwrite(context->fd, 0, (HV_VirtAddr) params,
491+
sizeof(*params),
492+
GXIO_MPIPE_OP_ADJUST_TIMESTAMP_FREQ);
493+
}
494+
495+
EXPORT_SYMBOL(gxio_mpipe_adjust_timestamp_freq);
496+
478497
struct config_edma_ring_blks_param {
479498
unsigned int ering;
480499
unsigned int max_blks;

‎arch/tile/include/gxio/iorpc_mpipe.h‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@
4646
#define GXIO_MPIPE_OP_LINK_CLOSE_AUX IORPC_OPCODE(IORPC_FORMAT_NONE, 0x1212)
4747
#define GXIO_MPIPE_OP_LINK_SET_ATTR_AUX IORPC_OPCODE(IORPC_FORMAT_NONE, 0x1213)
4848

49-
#define GXIO_MPIPE_OP_GET_TIMESTAMP_AUX IORPC_OPCODE(IORPC_FORMAT_NONE_NOUSER, 0x121e)
50-
#define GXIO_MPIPE_OP_SET_TIMESTAMP_AUX IORPC_OPCODE(IORPC_FORMAT_NONE_NOUSER, 0x121f)
51-
#define GXIO_MPIPE_OP_ADJUST_TIMESTAMP_AUX IORPC_OPCODE(IORPC_FORMAT_NONE_NOUSER, 0x1220)
49+
#define GXIO_MPIPE_OP_GET_TIMESTAMP_AUX IORPC_OPCODE(IORPC_FORMAT_NONE, 0x121e)
50+
#define GXIO_MPIPE_OP_SET_TIMESTAMP_AUX IORPC_OPCODE(IORPC_FORMAT_NONE, 0x121f)
51+
#define GXIO_MPIPE_OP_ADJUST_TIMESTAMP_AUX IORPC_OPCODE(IORPC_FORMAT_NONE, 0x1220)
5252
#define GXIO_MPIPE_OP_CONFIG_EDMA_RING_BLKS IORPC_OPCODE(IORPC_FORMAT_NONE, 0x1221)
53+
#define GXIO_MPIPE_OP_ADJUST_TIMESTAMP_FREQ IORPC_OPCODE(IORPC_FORMAT_NONE, 0x1222)
5354
#define GXIO_MPIPE_OP_ARM_POLLFD IORPC_OPCODE(IORPC_FORMAT_KERNEL_POLLFD, 0x9000)
5455
#define GXIO_MPIPE_OP_CLOSE_POLLFD IORPC_OPCODE(IORPC_FORMAT_KERNEL_POLLFD, 0x9001)
5556
#define GXIO_MPIPE_OP_GET_MMIO_BASE IORPC_OPCODE(IORPC_FORMAT_NONE_NOUSER, 0x8000)
@@ -128,6 +129,9 @@ int gxio_mpipe_set_timestamp_aux(gxio_mpipe_context_t * context, uint64_t sec,
128129
int gxio_mpipe_adjust_timestamp_aux(gxio_mpipe_context_t * context,
129130
int64_t nsec);
130131

132+
int gxio_mpipe_adjust_timestamp_freq(gxio_mpipe_context_t * context,
133+
int32_t ppb);
134+
131135
int gxio_mpipe_arm_pollfd(gxio_mpipe_context_t * context, int pollfd_cookie);
132136

133137
int gxio_mpipe_close_pollfd(gxio_mpipe_context_t * context, int pollfd_cookie);

‎arch/tile/include/gxio/mpipe.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,4 +1854,18 @@ extern int gxio_mpipe_set_timestamp(gxio_mpipe_context_t *context,
18541854
extern int gxio_mpipe_adjust_timestamp(gxio_mpipe_context_t *context,
18551855
int64_t delta);
18561856

1857+
/** Adjust the mPIPE timestamp clock frequency.
1858+
*
1859+
* @param context An initialized mPIPE context.
1860+
* @param ppb A 32-bit signed PPB (Parts Per Billion) value to adjust.
1861+
* The absolute value of ppb must be less than or equal to 1000000000.
1862+
* Values less than about 30000 will generally cause a GXIO_ERR_INVAL
1863+
* return due to the granularity of the hardware that converts reference
1864+
* clock cycles into seconds and nanoseconds.
1865+
* @return If the call was successful, zero; otherwise, a negative error
1866+
* code.
1867+
*/
1868+
extern int gxio_mpipe_adjust_timestamp_freq(gxio_mpipe_context_t* context,
1869+
int32_t ppb);
1870+
18571871
#endif /* !_GXIO_MPIPE_H_ */

‎drivers/net/ethernet/tile/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@ config TILE_NET
1515

1616
To compile this driver as a module, choose M here: the module
1717
will be called tile_net.
18+
19+
config PTP_1588_CLOCK_TILEGX
20+
tristate "Tilera TILE-Gx mPIPE as PTP clock"
21+
select PTP_1588_CLOCK
22+
depends on TILE_NET
23+
depends on TILEGX
24+
---help---
25+
This driver adds support for using the mPIPE as a PTP
26+
clock. This clock is only useful if your PTP programs are
27+
getting hardware time stamps on the PTP Ethernet packets
28+
using the SO_TIMESTAMPING API.

‎drivers/net/ethernet/tile/tilegx.c‎

Lines changed: 215 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include <linux/ip.h>
3939
#include <linux/ipv6.h>
4040
#include <linux/tcp.h>
41+
#include <linux/net_tstamp.h>
42+
#include <linux/ptp_clock_kernel.h>
4143

4244
#include <asm/checksum.h>
4345
#include <asm/homecache.h>
@@ -185,6 +187,10 @@ struct tile_net_priv {
185187
int echannel;
186188
/* mPIPE instance, 0 or 1. */
187189
int instance;
190+
#ifdef CONFIG_PTP_1588_CLOCK_TILEGX
191+
/* The timestamp config. */
192+
struct hwtstamp_config stamp_cfg;
193+
#endif
188194
};
189195

190196
static struct mpipe_data {
@@ -223,6 +229,15 @@ static struct mpipe_data {
223229
int first_bucket;
224230
int num_buckets;
225231

232+
#ifdef CONFIG_PTP_1588_CLOCK_TILEGX
233+
/* PTP-specific data. */
234+
struct ptp_clock *ptp_clock;
235+
struct ptp_clock_info caps;
236+
237+
/* Lock for ptp accessors. */
238+
struct mutex ptp_lock;
239+
#endif
240+
226241
} mpipe_data[NR_MPIPE_MAX] = {
227242
[0 ... (NR_MPIPE_MAX - 1)] {
228243
.ingress_irq = -1,
@@ -432,6 +447,94 @@ static void tile_net_provide_needed_buffers(void)
432447
}
433448
}
434449

450+
/* Get RX timestamp, and store it in the skb. */
451+
static void tile_rx_timestamp(struct tile_net_priv *priv, struct sk_buff *skb,
452+
gxio_mpipe_idesc_t *idesc)
453+
{
454+
#ifdef CONFIG_PTP_1588_CLOCK_TILEGX
455+
if (unlikely(priv->stamp_cfg.rx_filter != HWTSTAMP_FILTER_NONE)) {
456+
struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
457+
memset(shhwtstamps, 0, sizeof(*shhwtstamps));
458+
shhwtstamps->hwtstamp = ktime_set(idesc->time_stamp_sec,
459+
idesc->time_stamp_ns);
460+
}
461+
#endif
462+
}
463+
464+
/* Get TX timestamp, and store it in the skb. */
465+
static void tile_tx_timestamp(struct sk_buff *skb, int instance)
466+
{
467+
#ifdef CONFIG_PTP_1588_CLOCK_TILEGX
468+
struct skb_shared_info *shtx = skb_shinfo(skb);
469+
if (unlikely((shtx->tx_flags & SKBTX_HW_TSTAMP) != 0)) {
470+
struct mpipe_data *md = &mpipe_data[instance];
471+
struct skb_shared_hwtstamps shhwtstamps;
472+
struct timespec ts;
473+
474+
shtx->tx_flags |= SKBTX_IN_PROGRESS;
475+
gxio_mpipe_get_timestamp(&md->context, &ts);
476+
memset(&shhwtstamps, 0, sizeof(shhwtstamps));
477+
shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
478+
skb_tstamp_tx(skb, &shhwtstamps);
479+
}
480+
#endif
481+
}
482+
483+
/* Use ioctl() to enable or disable TX or RX timestamping. */
484+
static int tile_hwtstamp_ioctl(struct net_device *dev, struct ifreq *rq,
485+
int cmd)
486+
{
487+
#ifdef CONFIG_PTP_1588_CLOCK_TILEGX
488+
struct hwtstamp_config config;
489+
struct tile_net_priv *priv = netdev_priv(dev);
490+
491+
if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
492+
return -EFAULT;
493+
494+
if (config.flags) /* reserved for future extensions */
495+
return -EINVAL;
496+
497+
switch (config.tx_type) {
498+
case HWTSTAMP_TX_OFF:
499+
case HWTSTAMP_TX_ON:
500+
break;
501+
default:
502+
return -ERANGE;
503+
}
504+
505+
switch (config.rx_filter) {
506+
case HWTSTAMP_FILTER_NONE:
507+
break;
508+
case HWTSTAMP_FILTER_ALL:
509+
case HWTSTAMP_FILTER_SOME:
510+
case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
511+
case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
512+
case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
513+
case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
514+
case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
515+
case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
516+
case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
517+
case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
518+
case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
519+
case HWTSTAMP_FILTER_PTP_V2_EVENT:
520+
case HWTSTAMP_FILTER_PTP_V2_SYNC:
521+
case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
522+
config.rx_filter = HWTSTAMP_FILTER_ALL;
523+
break;
524+
default:
525+
return -ERANGE;
526+
}
527+
528+
if (copy_to_user(rq->ifr_data, &config, sizeof(config)))
529+
return -EFAULT;
530+
531+
priv->stamp_cfg = config;
532+
return 0;
533+
#else
534+
return -EOPNOTSUPP;
535+
#endif
536+
}
537+
435538
static inline bool filter_packet(struct net_device *dev, void *buf)
436539
{
437540
/* Filter packets received before we're up. */
@@ -451,7 +554,8 @@ static void tile_net_receive_skb(struct net_device *dev, struct sk_buff *skb,
451554
gxio_mpipe_idesc_t *idesc, unsigned long len)
452555
{
453556
struct tile_net_info *info = &__get_cpu_var(per_cpu_info);
454-
int instance = mpipe_instance(dev);
557+
struct tile_net_priv *priv = netdev_priv(dev);
558+
int instance = priv->instance;
455559

456560
/* Encode the actual packet length. */
457561
skb_put(skb, len);
@@ -462,6 +566,9 @@ static void tile_net_receive_skb(struct net_device *dev, struct sk_buff *skb,
462566
if (idesc->cs && idesc->csum_seed_val == 0xFFFF)
463567
skb->ip_summed = CHECKSUM_UNNECESSARY;
464568

569+
/* Get RX timestamp from idesc. */
570+
tile_rx_timestamp(priv, skb, idesc);
571+
465572
napi_gro_receive(&info->mpipe[instance].napi, skb);
466573

467574
/* Update stats. */
@@ -707,6 +814,103 @@ static enum hrtimer_restart tile_net_handle_egress_timer(struct hrtimer *t)
707814
return HRTIMER_NORESTART;
708815
}
709816

817+
#ifdef CONFIG_PTP_1588_CLOCK_TILEGX
818+
819+
/* PTP clock operations. */
820+
821+
static int ptp_mpipe_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
822+
{
823+
int ret = 0;
824+
struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
825+
mutex_lock(&md->ptp_lock);
826+
if (gxio_mpipe_adjust_timestamp_freq(&md->context, ppb))
827+
ret = -EINVAL;
828+
mutex_unlock(&md->ptp_lock);
829+
return ret;
830+
}
831+
832+
static int ptp_mpipe_adjtime(struct ptp_clock_info *ptp, s64 delta)
833+
{
834+
int ret = 0;
835+
struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
836+
mutex_lock(&md->ptp_lock);
837+
if (gxio_mpipe_adjust_timestamp(&md->context, delta))
838+
ret = -EBUSY;
839+
mutex_unlock(&md->ptp_lock);
840+
return ret;
841+
}
842+
843+
static int ptp_mpipe_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
844+
{
845+
int ret = 0;
846+
struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
847+
mutex_lock(&md->ptp_lock);
848+
if (gxio_mpipe_get_timestamp(&md->context, ts))
849+
ret = -EBUSY;
850+
mutex_unlock(&md->ptp_lock);
851+
return ret;
852+
}
853+
854+
static int ptp_mpipe_settime(struct ptp_clock_info *ptp,
855+
const struct timespec *ts)
856+
{
857+
int ret = 0;
858+
struct mpipe_data *md = container_of(ptp, struct mpipe_data, caps);
859+
mutex_lock(&md->ptp_lock);
860+
if (gxio_mpipe_set_timestamp(&md->context, ts))
861+
ret = -EBUSY;
862+
mutex_unlock(&md->ptp_lock);
863+
return ret;
864+
}
865+
866+
static int ptp_mpipe_enable(struct ptp_clock_info *ptp,
867+
struct ptp_clock_request *request, int on)
868+
{
869+
return -EOPNOTSUPP;
870+
}
871+
872+
static struct ptp_clock_info ptp_mpipe_caps = {
873+
.owner = THIS_MODULE,
874+
.name = "mPIPE clock",
875+
.max_adj = 999999999,
876+
.n_ext_ts = 0,
877+
.pps = 0,
878+
.adjfreq = ptp_mpipe_adjfreq,
879+
.adjtime = ptp_mpipe_adjtime,
880+
.gettime = ptp_mpipe_gettime,
881+
.settime = ptp_mpipe_settime,
882+
.enable = ptp_mpipe_enable,
883+
};
884+
885+
#endif /* CONFIG_PTP_1588_CLOCK_TILEGX */
886+
887+
/* Sync mPIPE's timestamp up with Linux system time and register PTP clock. */
888+
static void register_ptp_clock(struct net_device *dev, struct mpipe_data *md)
889+
{
890+
#ifdef CONFIG_PTP_1588_CLOCK_TILEGX
891+
struct timespec ts;
892+
893+
getnstimeofday(&ts);
894+
gxio_mpipe_set_timestamp(&md->context, &ts);
895+
896+
mutex_init(&md->ptp_lock);
897+
md->caps = ptp_mpipe_caps;
898+
md->ptp_clock = ptp_clock_register(&md->caps, NULL);
899+
if (IS_ERR(md->ptp_clock))
900+
netdev_err(dev, "ptp_clock_register failed %ld\n",
901+
PTR_ERR(md->ptp_clock));
902+
#endif
903+
}
904+
905+
/* Initialize PTP fields in a new device. */
906+
static void init_ptp_dev(struct tile_net_priv *priv)
907+
{
908+
#ifdef CONFIG_PTP_1588_CLOCK_TILEGX
909+
priv->stamp_cfg.rx_filter = HWTSTAMP_FILTER_NONE;
910+
priv->stamp_cfg.tx_type = HWTSTAMP_TX_OFF;
911+
#endif
912+
}
913+
710914
/* Helper functions for "tile_net_update()". */
711915
static void enable_ingress_irq(void *irq)
712916
{
@@ -1149,6 +1353,9 @@ static int tile_net_init_mpipe(struct net_device *dev)
11491353
if (rc != 0)
11501354
goto fail;
11511355

1356+
/* Register PTP clock and set mPIPE timestamp, if configured. */
1357+
register_ptp_clock(dev, md);
1358+
11521359
return 0;
11531360

11541361
fail:
@@ -1851,6 +2058,9 @@ static int tile_net_tx(struct sk_buff *skb, struct net_device *dev)
18512058
for (i = 0; i < num_edescs; i++)
18522059
gxio_mpipe_equeue_put_at(equeue, edescs[i], slot++);
18532060

2061+
/* Store TX timestamp if needed. */
2062+
tile_tx_timestamp(skb, instance);
2063+
18542064
/* Add a completion record. */
18552065
add_comp(equeue, comps, slot - 1, skb);
18562066

@@ -1885,6 +2095,9 @@ static void tile_net_tx_timeout(struct net_device *dev)
18852095
/* Ioctl commands. */
18862096
static int tile_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
18872097
{
2098+
if (cmd == SIOCSHWTSTAMP)
2099+
return tile_hwtstamp_ioctl(dev, rq, cmd);
2100+
18882101
return -EOPNOTSUPP;
18892102
}
18902103

@@ -2005,6 +2218,7 @@ static void tile_net_dev_init(const char *name, const uint8_t *mac)
20052218
priv->channel = -1;
20062219
priv->loopify_channel = -1;
20072220
priv->echannel = -1;
2221+
init_ptp_dev(priv);
20082222

20092223
/* Get the MAC address and set it in the device struct; this must
20102224
* be done before the device is opened. If the MAC is all zeroes,

0 commit comments

Comments
 (0)