Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 2 | /* |
| 3 | * PTP 1588 clock support - support for timestamping in PHY devices |
| 4 | * |
| 5 | * Copyright (C) 2010 OMICRON electronics GmbH |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/errqueue.h> |
| 8 | #include <linux/phy.h> |
| 9 | #include <linux/ptp_classify.h> |
| 10 | #include <linux/skbuff.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 12 | |
Eric Dumazet | 62ab081 | 2010-12-06 20:50:09 +0000 | [diff] [blame] | 13 | static unsigned int classify(const struct sk_buff *skb) |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 14 | { |
Daniel Borkmann | e62d2df | 2014-03-28 18:58:21 +0100 | [diff] [blame] | 15 | if (likely(skb->dev && skb->dev->phydev && |
Richard Cochran | 4715f65 | 2019-12-25 18:16:15 -0800 | [diff] [blame] | 16 | skb->dev->phydev->mii_ts)) |
Daniel Borkmann | 164d8c6 | 2014-03-28 18:58:22 +0100 | [diff] [blame] | 17 | return ptp_classify_raw(skb); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 18 | else |
| 19 | return PTP_CLASS_NONE; |
| 20 | } |
| 21 | |
| 22 | void skb_clone_tx_timestamp(struct sk_buff *skb) |
| 23 | { |
Richard Cochran | 4715f65 | 2019-12-25 18:16:15 -0800 | [diff] [blame] | 24 | struct mii_timestamper *mii_ts; |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 25 | struct sk_buff *clone; |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 26 | unsigned int type; |
| 27 | |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 28 | if (!skb->sk) |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 29 | return; |
| 30 | |
| 31 | type = classify(skb); |
Stefan Sørensen | b9c701e | 2014-06-27 11:59:09 +0200 | [diff] [blame] | 32 | if (type == PTP_CLASS_NONE) |
| 33 | return; |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 34 | |
Richard Cochran | 4715f65 | 2019-12-25 18:16:15 -0800 | [diff] [blame] | 35 | mii_ts = skb->dev->phydev->mii_ts; |
| 36 | if (likely(mii_ts->txtstamp)) { |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 37 | clone = skb_clone_sk(skb); |
| 38 | if (!clone) |
Stefan Sørensen | b9c701e | 2014-06-27 11:59:09 +0200 | [diff] [blame] | 39 | return; |
Richard Cochran | 4715f65 | 2019-12-25 18:16:15 -0800 | [diff] [blame] | 40 | mii_ts->txtstamp(mii_ts, clone, type); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
Richard Cochran | 1c17216 | 2011-06-12 02:18:58 +0000 | [diff] [blame] | 43 | EXPORT_SYMBOL_GPL(skb_clone_tx_timestamp); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 44 | |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 45 | bool skb_defer_rx_timestamp(struct sk_buff *skb) |
| 46 | { |
Richard Cochran | 4715f65 | 2019-12-25 18:16:15 -0800 | [diff] [blame] | 47 | struct mii_timestamper *mii_ts; |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 48 | unsigned int type; |
| 49 | |
Richard Cochran | 4715f65 | 2019-12-25 18:16:15 -0800 | [diff] [blame] | 50 | if (!skb->dev || !skb->dev->phydev || !skb->dev->phydev->mii_ts) |
Alexander Duyck | 1007f59 | 2015-07-09 11:02:52 -0700 | [diff] [blame] | 51 | return false; |
| 52 | |
Eric Dumazet | a19faf0 | 2010-12-05 18:50:32 +0000 | [diff] [blame] | 53 | if (skb_headroom(skb) < ETH_HLEN) |
| 54 | return false; |
Alexander Duyck | 1007f59 | 2015-07-09 11:02:52 -0700 | [diff] [blame] | 55 | |
Eric Dumazet | a19faf0 | 2010-12-05 18:50:32 +0000 | [diff] [blame] | 56 | __skb_push(skb, ETH_HLEN); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 57 | |
Alexander Duyck | 1007f59 | 2015-07-09 11:02:52 -0700 | [diff] [blame] | 58 | type = ptp_classify_raw(skb); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 59 | |
Eric Dumazet | a19faf0 | 2010-12-05 18:50:32 +0000 | [diff] [blame] | 60 | __skb_pull(skb, ETH_HLEN); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 61 | |
Stefan Sørensen | b9c701e | 2014-06-27 11:59:09 +0200 | [diff] [blame] | 62 | if (type == PTP_CLASS_NONE) |
| 63 | return false; |
| 64 | |
Richard Cochran | 4715f65 | 2019-12-25 18:16:15 -0800 | [diff] [blame] | 65 | mii_ts = skb->dev->phydev->mii_ts; |
| 66 | if (likely(mii_ts->rxtstamp)) |
| 67 | return mii_ts->rxtstamp(mii_ts, skb, type); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 68 | |
| 69 | return false; |
| 70 | } |
Richard Cochran | 238442f | 2011-06-19 21:51:23 +0000 | [diff] [blame] | 71 | EXPORT_SYMBOL_GPL(skb_defer_rx_timestamp); |