1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Support for generic time stamping devices on MII buses.
4 * Copyright (C) 2018 Richard Cochran <richardcochran@gmail.com>
5 */
6#ifndef _LINUX_MII_TIMESTAMPER_H
7#define _LINUX_MII_TIMESTAMPER_H
8
9#include <linux/device.h>
10#include <linux/ethtool.h>
11#include <linux/skbuff.h>
12#include <linux/net_tstamp.h>
13
14struct phy_device;
15
16/**
17 * struct mii_timestamper - Callback interface to MII time stamping devices.
18 *
19 * @rxtstamp:	Requests a Rx timestamp for 'skb'.  If the skb is accepted,
20 *		the MII time stamping device promises to deliver it using
21 *		netif_rx() as soon as a timestamp becomes available. One of
22 *		the PTP_CLASS_ values is passed in 'type'.  The function
23 *		must return true if the skb is accepted for delivery.
24 *
25 * @txtstamp:	Requests a Tx timestamp for 'skb'.  The MII time stamping
26 *		device promises to deliver it using skb_complete_tx_timestamp()
27 *		as soon as a timestamp becomes available. One of the PTP_CLASS_
28 *		values is passed in 'type'.
29 *
30 * @hwtstamp:	Handles SIOCSHWTSTAMP ioctl for hardware time stamping.
31 *
32 * @link_state: Allows the device to respond to changes in the link
33 *		state.  The caller invokes this function while holding
34 *		the phy_device mutex.
35 *
36 * @ts_info:	Handles ethtool queries for hardware time stamping.
37 * @device:	Remembers the device to which the instance belongs.
38 *
39 * Drivers for PHY time stamping devices should embed their
40 * mii_timestamper within a private structure, obtaining a reference
41 * to it using container_of().
42 *
43 * Drivers for non-PHY time stamping devices should return a pointer
44 * to a mii_timestamper from the probe_channel() callback of their
45 * mii_timestamping_ctrl interface.
46 */
47struct mii_timestamper {
48	bool (*rxtstamp)(struct mii_timestamper *mii_ts,
49			 struct sk_buff *skb, int type);
50
51	void (*txtstamp)(struct mii_timestamper *mii_ts,
52			 struct sk_buff *skb, int type);
53
54	int  (*hwtstamp)(struct mii_timestamper *mii_ts,
55			 struct kernel_hwtstamp_config *kernel_config,
56			 struct netlink_ext_ack *extack);
57
58	void (*link_state)(struct mii_timestamper *mii_ts,
59			   struct phy_device *phydev);
60
61	int  (*ts_info)(struct mii_timestamper *mii_ts,
62			struct ethtool_ts_info *ts_info);
63
64	struct device *device;
65};
66
67/**
68 * struct mii_timestamping_ctrl - MII time stamping controller interface.
69 *
70 * @probe_channel:	Callback into the controller driver announcing the
71 *			presence of the 'port' channel.  The 'device' field
72 *			had been passed to register_mii_tstamp_controller().
73 *			The driver must return either a pointer to a valid
74 *			MII timestamper instance or PTR_ERR.
75 *
76 * @release_channel:	Releases an instance obtained via .probe_channel.
77 */
78struct mii_timestamping_ctrl {
79	struct mii_timestamper *(*probe_channel)(struct device *device,
80						 unsigned int port);
81	void (*release_channel)(struct device *device,
82				struct mii_timestamper *mii_ts);
83};
84
85#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
86
87int register_mii_tstamp_controller(struct device *device,
88				   struct mii_timestamping_ctrl *ctrl);
89
90void unregister_mii_tstamp_controller(struct device *device);
91
92struct mii_timestamper *register_mii_timestamper(struct device_node *node,
93						 unsigned int port);
94
95void unregister_mii_timestamper(struct mii_timestamper *mii_ts);
96
97#else
98
99static inline
100int register_mii_tstamp_controller(struct device *device,
101				   struct mii_timestamping_ctrl *ctrl)
102{
103	return -EOPNOTSUPP;
104}
105
106static inline void unregister_mii_tstamp_controller(struct device *device)
107{
108}
109
110static inline
111struct mii_timestamper *register_mii_timestamper(struct device_node *node,
112						 unsigned int port)
113{
114	return NULL;
115}
116
117static inline void unregister_mii_timestamper(struct mii_timestamper *mii_ts)
118{
119}
120
121#endif
122
123#endif
124