blob: a8770391ea5bfc0db85135cf913c84d09f55cedb [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
Eric Dumazet62ab0812010-12-06 20:50:09 +000026static unsigned int classify(const struct sk_buff *skb)
Richard Cochranc1f19b52010-07-17 08:49:36 +000027{
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010028 if (likely(skb->dev && skb->dev->phydev &&
Richard Cochranc1f19b52010-07-17 08:49:36 +000029 skb->dev->phydev->drv))
Daniel Borkmann164d8c62014-03-28 18:58:22 +010030 return ptp_classify_raw(skb);
Richard Cochranc1f19b52010-07-17 08:49:36 +000031 else
32 return PTP_CLASS_NONE;
33}
34
35void skb_clone_tx_timestamp(struct sk_buff *skb)
36{
37 struct phy_device *phydev;
38 struct sk_buff *clone;
39 struct sock *sk = skb->sk;
40 unsigned int type;
41
42 if (!sk)
43 return;
44
45 type = classify(skb);
Stefan Sørensenb9c701e2014-06-27 11:59:09 +020046 if (type == PTP_CLASS_NONE)
47 return;
Richard Cochranc1f19b52010-07-17 08:49:36 +000048
Stefan Sørensenb9c701e2014-06-27 11:59:09 +020049 phydev = skb->dev->phydev;
50 if (likely(phydev->drv->txtstamp)) {
51 if (!atomic_inc_not_zero(&sk->sk_refcnt))
52 return;
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010053
Stefan Sørensenb9c701e2014-06-27 11:59:09 +020054 clone = skb_clone(skb, GFP_ATOMIC);
55 if (!clone) {
56 sock_put(sk);
57 return;
Richard Cochranc1f19b52010-07-17 08:49:36 +000058 }
Stefan Sørensenb9c701e2014-06-27 11:59:09 +020059
60 clone->sk = sk;
61 phydev->drv->txtstamp(phydev, clone, type);
Richard Cochranc1f19b52010-07-17 08:49:36 +000062 }
63}
Richard Cochran1c172162011-06-12 02:18:58 +000064EXPORT_SYMBOL_GPL(skb_clone_tx_timestamp);
Richard Cochranc1f19b52010-07-17 08:49:36 +000065
66void skb_complete_tx_timestamp(struct sk_buff *skb,
67 struct skb_shared_hwtstamps *hwtstamps)
68{
69 struct sock *sk = skb->sk;
70 struct sock_exterr_skb *serr;
71 int err;
72
Richard Cochranda92b192011-10-21 00:49:15 +000073 if (!hwtstamps) {
74 sock_put(sk);
75 kfree_skb(skb);
Richard Cochranc1f19b52010-07-17 08:49:36 +000076 return;
Richard Cochranda92b192011-10-21 00:49:15 +000077 }
Richard Cochranc1f19b52010-07-17 08:49:36 +000078
79 *skb_hwtstamps(skb) = *hwtstamps;
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010080
Richard Cochranc1f19b52010-07-17 08:49:36 +000081 serr = SKB_EXT_ERR(skb);
82 memset(serr, 0, sizeof(*serr));
83 serr->ee.ee_errno = ENOMSG;
84 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
85 skb->sk = NULL;
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010086
Richard Cochranc1f19b52010-07-17 08:49:36 +000087 err = sock_queue_err_skb(sk, skb);
Daniel Borkmanne62d2df2014-03-28 18:58:21 +010088
Richard Cochranda92b192011-10-21 00:49:15 +000089 sock_put(sk);
Richard Cochranc1f19b52010-07-17 08:49:36 +000090 if (err)
91 kfree_skb(skb);
92}
93EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
94
95bool skb_defer_rx_timestamp(struct sk_buff *skb)
96{
97 struct phy_device *phydev;
98 unsigned int type;
99
Eric Dumazeta19faf02010-12-05 18:50:32 +0000100 if (skb_headroom(skb) < ETH_HLEN)
101 return false;
102 __skb_push(skb, ETH_HLEN);
Richard Cochranc1f19b52010-07-17 08:49:36 +0000103
104 type = classify(skb);
105
Eric Dumazeta19faf02010-12-05 18:50:32 +0000106 __skb_pull(skb, ETH_HLEN);
Richard Cochranc1f19b52010-07-17 08:49:36 +0000107
Stefan Sørensenb9c701e2014-06-27 11:59:09 +0200108 if (type == PTP_CLASS_NONE)
109 return false;
110
111 phydev = skb->dev->phydev;
112 if (likely(phydev->drv->rxtstamp))
113 return phydev->drv->rxtstamp(phydev, skb, type);
Richard Cochranc1f19b52010-07-17 08:49:36 +0000114
115 return false;
116}
Richard Cochran238442f2011-06-19 21:51:23 +0000117EXPORT_SYMBOL_GPL(skb_defer_rx_timestamp);