blob: 42689d5c468cb4f53baa058c74cdee58099137c7 [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;
Richard Cochranc1f19b52010-07-17 08:49:36 +000039 unsigned int type;
40
Alexander Duyck62bccb82014-09-04 13:31:35 -040041 if (!skb->sk)
Richard Cochranc1f19b52010-07-17 08:49:36 +000042 return;
43
44 type = classify(skb);
Stefan Sørensenb9c701e2014-06-27 11:59:09 +020045 if (type == PTP_CLASS_NONE)
46 return;
Richard Cochranc1f19b52010-07-17 08:49:36 +000047
Stefan Sørensenb9c701e2014-06-27 11:59:09 +020048 phydev = skb->dev->phydev;
49 if (likely(phydev->drv->txtstamp)) {
Alexander Duyck62bccb82014-09-04 13:31:35 -040050 clone = skb_clone_sk(skb);
51 if (!clone)
Stefan Sørensenb9c701e2014-06-27 11:59:09 +020052 return;
Stefan Sørensenb9c701e2014-06-27 11:59:09 +020053 phydev->drv->txtstamp(phydev, clone, type);
Richard Cochranc1f19b52010-07-17 08:49:36 +000054 }
55}
Richard Cochran1c172162011-06-12 02:18:58 +000056EXPORT_SYMBOL_GPL(skb_clone_tx_timestamp);
Richard Cochranc1f19b52010-07-17 08:49:36 +000057
Richard Cochranc1f19b52010-07-17 08:49:36 +000058bool skb_defer_rx_timestamp(struct sk_buff *skb)
59{
60 struct phy_device *phydev;
61 unsigned int type;
62
Alexander Duyck1007f592015-07-09 11:02:52 -070063 if (!skb->dev || !skb->dev->phydev || !skb->dev->phydev->drv)
64 return false;
65
Eric Dumazeta19faf02010-12-05 18:50:32 +000066 if (skb_headroom(skb) < ETH_HLEN)
67 return false;
Alexander Duyck1007f592015-07-09 11:02:52 -070068
Eric Dumazeta19faf02010-12-05 18:50:32 +000069 __skb_push(skb, ETH_HLEN);
Richard Cochranc1f19b52010-07-17 08:49:36 +000070
Alexander Duyck1007f592015-07-09 11:02:52 -070071 type = ptp_classify_raw(skb);
Richard Cochranc1f19b52010-07-17 08:49:36 +000072
Eric Dumazeta19faf02010-12-05 18:50:32 +000073 __skb_pull(skb, ETH_HLEN);
Richard Cochranc1f19b52010-07-17 08:49:36 +000074
Stefan Sørensenb9c701e2014-06-27 11:59:09 +020075 if (type == PTP_CLASS_NONE)
76 return false;
77
78 phydev = skb->dev->phydev;
79 if (likely(phydev->drv->rxtstamp))
80 return phydev->drv->rxtstamp(phydev, skb, type);
Richard Cochranc1f19b52010-07-17 08:49:36 +000081
82 return false;
83}
Richard Cochran238442f2011-06-19 21:51:23 +000084EXPORT_SYMBOL_GPL(skb_defer_rx_timestamp);