Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 1 | /* |
| 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 Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 24 | #include <linux/export.h> |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 25 | |
Eric Dumazet | 62ab081 | 2010-12-06 20:50:09 +0000 | [diff] [blame] | 26 | static unsigned int classify(const struct sk_buff *skb) |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 27 | { |
Daniel Borkmann | e62d2df | 2014-03-28 18:58:21 +0100 | [diff] [blame] | 28 | if (likely(skb->dev && skb->dev->phydev && |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 29 | skb->dev->phydev->drv)) |
Daniel Borkmann | 164d8c6 | 2014-03-28 18:58:22 +0100 | [diff] [blame] | 30 | return ptp_classify_raw(skb); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 31 | else |
| 32 | return PTP_CLASS_NONE; |
| 33 | } |
| 34 | |
| 35 | void skb_clone_tx_timestamp(struct sk_buff *skb) |
| 36 | { |
| 37 | struct phy_device *phydev; |
| 38 | struct sk_buff *clone; |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 39 | unsigned int type; |
| 40 | |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 41 | if (!skb->sk) |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 42 | return; |
| 43 | |
| 44 | type = classify(skb); |
Stefan Sørensen | b9c701e | 2014-06-27 11:59:09 +0200 | [diff] [blame] | 45 | if (type == PTP_CLASS_NONE) |
| 46 | return; |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 47 | |
Stefan Sørensen | b9c701e | 2014-06-27 11:59:09 +0200 | [diff] [blame] | 48 | phydev = skb->dev->phydev; |
| 49 | if (likely(phydev->drv->txtstamp)) { |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 50 | clone = skb_clone_sk(skb); |
| 51 | if (!clone) |
Stefan Sørensen | b9c701e | 2014-06-27 11:59:09 +0200 | [diff] [blame] | 52 | return; |
Stefan Sørensen | b9c701e | 2014-06-27 11:59:09 +0200 | [diff] [blame] | 53 | phydev->drv->txtstamp(phydev, clone, type); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
Richard Cochran | 1c17216 | 2011-06-12 02:18:58 +0000 | [diff] [blame] | 56 | EXPORT_SYMBOL_GPL(skb_clone_tx_timestamp); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 57 | |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 58 | bool skb_defer_rx_timestamp(struct sk_buff *skb) |
| 59 | { |
| 60 | struct phy_device *phydev; |
| 61 | unsigned int type; |
| 62 | |
Alexander Duyck | 1007f59 | 2015-07-09 11:02:52 -0700 | [diff] [blame] | 63 | if (!skb->dev || !skb->dev->phydev || !skb->dev->phydev->drv) |
| 64 | return false; |
| 65 | |
Eric Dumazet | a19faf0 | 2010-12-05 18:50:32 +0000 | [diff] [blame] | 66 | if (skb_headroom(skb) < ETH_HLEN) |
| 67 | return false; |
Alexander Duyck | 1007f59 | 2015-07-09 11:02:52 -0700 | [diff] [blame] | 68 | |
Eric Dumazet | a19faf0 | 2010-12-05 18:50:32 +0000 | [diff] [blame] | 69 | __skb_push(skb, ETH_HLEN); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 70 | |
Alexander Duyck | 1007f59 | 2015-07-09 11:02:52 -0700 | [diff] [blame] | 71 | type = ptp_classify_raw(skb); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 72 | |
Eric Dumazet | a19faf0 | 2010-12-05 18:50:32 +0000 | [diff] [blame] | 73 | __skb_pull(skb, ETH_HLEN); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 74 | |
Stefan Sørensen | b9c701e | 2014-06-27 11:59:09 +0200 | [diff] [blame] | 75 | 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 Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 81 | |
| 82 | return false; |
| 83 | } |
Richard Cochran | 238442f | 2011-06-19 21:51:23 +0000 | [diff] [blame] | 84 | EXPORT_SYMBOL_GPL(skb_defer_rx_timestamp); |