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> |
| 24 | |
| 25 | static struct sock_filter ptp_filter[] = { |
| 26 | PTP_FILTER |
| 27 | }; |
| 28 | |
Eric Dumazet | 62ab081 | 2010-12-06 20:50:09 +0000 | [diff] [blame] | 29 | static unsigned int classify(const struct sk_buff *skb) |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 30 | { |
| 31 | if (likely(skb->dev && |
| 32 | skb->dev->phydev && |
| 33 | skb->dev->phydev->drv)) |
Eric Dumazet | 93aaae2 | 2010-11-19 09:49:59 -0800 | [diff] [blame] | 34 | return sk_run_filter(skb, ptp_filter); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 35 | else |
| 36 | return PTP_CLASS_NONE; |
| 37 | } |
| 38 | |
| 39 | void skb_clone_tx_timestamp(struct sk_buff *skb) |
| 40 | { |
| 41 | struct phy_device *phydev; |
| 42 | struct sk_buff *clone; |
| 43 | struct sock *sk = skb->sk; |
| 44 | unsigned int type; |
| 45 | |
| 46 | if (!sk) |
| 47 | return; |
| 48 | |
| 49 | type = classify(skb); |
| 50 | |
| 51 | switch (type) { |
| 52 | case PTP_CLASS_V1_IPV4: |
| 53 | case PTP_CLASS_V1_IPV6: |
| 54 | case PTP_CLASS_V2_IPV4: |
| 55 | case PTP_CLASS_V2_IPV6: |
| 56 | case PTP_CLASS_V2_L2: |
| 57 | case PTP_CLASS_V2_VLAN: |
| 58 | phydev = skb->dev->phydev; |
| 59 | if (likely(phydev->drv->txtstamp)) { |
Richard Cochran | da92b19 | 2011-10-21 00:49:15 +0000 | [diff] [blame] | 60 | if (!atomic_inc_not_zero(&sk->sk_refcnt)) |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 61 | return; |
Richard Cochran | da92b19 | 2011-10-21 00:49:15 +0000 | [diff] [blame] | 62 | clone = skb_clone(skb, GFP_ATOMIC); |
| 63 | if (!clone) { |
| 64 | sock_put(sk); |
| 65 | return; |
| 66 | } |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 67 | clone->sk = sk; |
| 68 | phydev->drv->txtstamp(phydev, clone, type); |
| 69 | } |
| 70 | break; |
| 71 | default: |
| 72 | break; |
| 73 | } |
| 74 | } |
Richard Cochran | 1c17216 | 2011-06-12 02:18:58 +0000 | [diff] [blame] | 75 | EXPORT_SYMBOL_GPL(skb_clone_tx_timestamp); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 76 | |
| 77 | void 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 Cochran | da92b19 | 2011-10-21 00:49:15 +0000 | [diff] [blame] | 84 | if (!hwtstamps) { |
| 85 | sock_put(sk); |
| 86 | kfree_skb(skb); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 87 | return; |
Richard Cochran | da92b19 | 2011-10-21 00:49:15 +0000 | [diff] [blame] | 88 | } |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 89 | |
| 90 | *skb_hwtstamps(skb) = *hwtstamps; |
| 91 | serr = SKB_EXT_ERR(skb); |
| 92 | memset(serr, 0, sizeof(*serr)); |
| 93 | serr->ee.ee_errno = ENOMSG; |
| 94 | serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; |
| 95 | skb->sk = NULL; |
| 96 | err = sock_queue_err_skb(sk, skb); |
Richard Cochran | da92b19 | 2011-10-21 00:49:15 +0000 | [diff] [blame] | 97 | sock_put(sk); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 98 | if (err) |
| 99 | kfree_skb(skb); |
| 100 | } |
| 101 | EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); |
| 102 | |
| 103 | bool skb_defer_rx_timestamp(struct sk_buff *skb) |
| 104 | { |
| 105 | struct phy_device *phydev; |
| 106 | unsigned int type; |
| 107 | |
Eric Dumazet | a19faf0 | 2010-12-05 18:50:32 +0000 | [diff] [blame] | 108 | if (skb_headroom(skb) < ETH_HLEN) |
| 109 | return false; |
| 110 | __skb_push(skb, ETH_HLEN); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 111 | |
| 112 | type = classify(skb); |
| 113 | |
Eric Dumazet | a19faf0 | 2010-12-05 18:50:32 +0000 | [diff] [blame] | 114 | __skb_pull(skb, ETH_HLEN); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 115 | |
| 116 | switch (type) { |
| 117 | case PTP_CLASS_V1_IPV4: |
| 118 | case PTP_CLASS_V1_IPV6: |
| 119 | case PTP_CLASS_V2_IPV4: |
| 120 | case PTP_CLASS_V2_IPV6: |
| 121 | case PTP_CLASS_V2_L2: |
| 122 | case PTP_CLASS_V2_VLAN: |
| 123 | phydev = skb->dev->phydev; |
| 124 | if (likely(phydev->drv->rxtstamp)) |
| 125 | return phydev->drv->rxtstamp(phydev, skb, type); |
| 126 | break; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | return false; |
| 132 | } |
Richard Cochran | 238442f | 2011-06-19 21:51:23 +0000 | [diff] [blame] | 133 | EXPORT_SYMBOL_GPL(skb_defer_rx_timestamp); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 134 | |
| 135 | void __init skb_timestamping_init(void) |
| 136 | { |
| 137 | BUG_ON(sk_chk_filter(ptp_filter, ARRAY_SIZE(ptp_filter))); |
| 138 | } |