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
190196static 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+
435538static 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()". */
711915static 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
11541361fail :
@@ -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. */
18862096static 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