blob: e43d56acf803cdef5aeb38a17e071db9325b2de1 [file] [log] [blame]
Richard Cochranc1f19b52010-07-17 08:49:36 +00001/*
2 * PTP 1588 clock support - support for timestamping in PHY devices
3 *
4 * Copyright (C) 2010 OMICRON electronics GmbH
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <linux/errqueue.h>
21#include <linux/phy.h>
22#include <linux/ptp_classify.h>
23#include <linux/skbuff.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040024#include <linux/export.h>
Richard Cochranc1f19b52010-07-17 08:49:36 +000025
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010026static struct sk_filter *ptp_insns __read_mostly;
Richard Cochranc1f19b52010-07-17 08:49:36 +000027
Eric Dumazet62ab0812010-12-06 20:50:09 +000028static unsigned int classify(const struct sk_buff *skb)
Richard Cochranc1f19b52010-07-17 08:49:36 +000029{
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010030 if (likely(skb->dev && skb->dev->phydev &&
Richard Cochranc1f19b52010-07-17 08:49:36 +000031 skb->dev->phydev->drv))
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010032 return SK_RUN_FILTER(ptp_insns, skb);
Richard Cochranc1f19b52010-07-17 08:49:36 +000033 else
34 return PTP_CLASS_NONE;
35}
36
37void skb_clone_tx_timestamp(struct sk_buff *skb)
38{
39 struct phy_device *phydev;
40 struct sk_buff *clone;
41 struct sock *sk = skb->sk;
42 unsigned int type;
43
44 if (!sk)
45 return;
46
47 type = classify(skb);
48
49 switch (type) {
50 case PTP_CLASS_V1_IPV4:
51 case PTP_CLASS_V1_IPV6:
52 case PTP_CLASS_V2_IPV4:
53 case PTP_CLASS_V2_IPV6:
54 case PTP_CLASS_V2_L2:
55 case PTP_CLASS_V2_VLAN:
56 phydev = skb->dev->phydev;
57 if (likely(phydev->drv->txtstamp)) {
Richard Cochranda92b192011-10-21 00:49:15 +000058 if (!atomic_inc_not_zero(&sk->sk_refcnt))
Richard Cochranc1f19b52010-07-17 08:49:36 +000059 return;
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010060
Richard Cochranda92b192011-10-21 00:49:15 +000061 clone = skb_clone(skb, GFP_ATOMIC);
62 if (!clone) {
63 sock_put(sk);
64 return;
65 }
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010066
Richard Cochranc1f19b52010-07-17 08:49:36 +000067 clone->sk = sk;
68 phydev->drv->txtstamp(phydev, clone, type);
69 }
70 break;
71 default:
72 break;
73 }
74}
Richard Cochran1c172162011-06-12 02:18:58 +000075EXPORT_SYMBOL_GPL(skb_clone_tx_timestamp);
Richard Cochranc1f19b52010-07-17 08:49:36 +000076
77void skb_complete_tx_timestamp(struct sk_buff *skb,
78 struct skb_shared_hwtstamps *hwtstamps)
79{
80 struct sock *sk = skb->sk;
81 struct sock_exterr_skb *serr;
82 int err;
83
Richard Cochranda92b192011-10-21 00:49:15 +000084 if (!hwtstamps) {
85 sock_put(sk);
86 kfree_skb(skb);
Richard Cochranc1f19b52010-07-17 08:49:36 +000087 return;
Richard Cochranda92b192011-10-21 00:49:15 +000088 }
Richard Cochranc1f19b52010-07-17 08:49:36 +000089
90 *skb_hwtstamps(skb) = *hwtstamps;
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010091
Richard Cochranc1f19b52010-07-17 08:49:36 +000092 serr = SKB_EXT_ERR(skb);
93 memset(serr, 0, sizeof(*serr));
94 serr->ee.ee_errno = ENOMSG;
95 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
96 skb->sk = NULL;
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010097
Richard Cochranc1f19b52010-07-17 08:49:36 +000098 err = sock_queue_err_skb(sk, skb);
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010099
Richard Cochranda92b192011-10-21 00:49:15 +0000100 sock_put(sk);
Richard Cochranc1f19b52010-07-17 08:49:36 +0000101 if (err)
102 kfree_skb(skb);
103}
104EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
105
106bool skb_defer_rx_timestamp(struct sk_buff *skb)
107{
108 struct phy_device *phydev;
109 unsigned int type;
110
Eric Dumazeta19faf02010-12-05 18:50:32 +0000111 if (skb_headroom(skb) < ETH_HLEN)
112 return false;
113 __skb_push(skb, ETH_HLEN);
Richard Cochranc1f19b52010-07-17 08:49:36 +0000114
115 type = classify(skb);
116
Eric Dumazeta19faf02010-12-05 18:50:32 +0000117 __skb_pull(skb, ETH_HLEN);
Richard Cochranc1f19b52010-07-17 08:49:36 +0000118
119 switch (type) {
120 case PTP_CLASS_V1_IPV4:
121 case PTP_CLASS_V1_IPV6:
122 case PTP_CLASS_V2_IPV4:
123 case PTP_CLASS_V2_IPV6:
124 case PTP_CLASS_V2_L2:
125 case PTP_CLASS_V2_VLAN:
126 phydev = skb->dev->phydev;
127 if (likely(phydev->drv->rxtstamp))
128 return phydev->drv->rxtstamp(phydev, skb, type);
129 break;
130 default:
131 break;
132 }
133
134 return false;
135}
Richard Cochran238442f2011-06-19 21:51:23 +0000136EXPORT_SYMBOL_GPL(skb_defer_rx_timestamp);
Richard Cochranc1f19b52010-07-17 08:49:36 +0000137
138void __init skb_timestamping_init(void)
139{
Daniel Borkmanne62d2df2014-03-28 18:58:21 +0100140 static struct sock_filter ptp_filter[] = { PTP_FILTER };
141 struct sock_fprog ptp_prog = {
142 .len = ARRAY_SIZE(ptp_filter), .filter = ptp_filter,
143 };
144
145 BUG_ON(sk_unattached_filter_create(&ptp_insns, &ptp_prog));
Richard Cochranc1f19b52010-07-17 08:49:36 +0000146}