xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/chelsio/cxgb4/cxgb4_ptp.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * cxgb4_ptp.c:Chelsio PTP support for T5/T6
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (c) 2003-2017 Chelsio Communications, Inc. All rights reserved.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This software is available to you under a choice of one of two
7*4882a593Smuzhiyun  * licenses.  You may choose to be licensed under the terms of the GNU
8*4882a593Smuzhiyun  * General Public License (GPL) Version 2, available from the file
9*4882a593Smuzhiyun  * COPYING in the main directory of this source tree, or the
10*4882a593Smuzhiyun  * OpenIB.org BSD license below:
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *     Redistribution and use in source and binary forms, with or
13*4882a593Smuzhiyun  *     without modification, are permitted provided that the following
14*4882a593Smuzhiyun  *     conditions are met:
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *      - Redistributions of source code must retain the above
17*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
18*4882a593Smuzhiyun  *        disclaimer.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  *      - Redistributions in binary form must reproduce the above
21*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
22*4882a593Smuzhiyun  *        disclaimer in the documentation and/or other materials
23*4882a593Smuzhiyun  *        provided with the distribution.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28*4882a593Smuzhiyun  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32*4882a593Smuzhiyun  * SOFTWARE.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * Written by: Atul Gupta (atul.gupta@chelsio.com)
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #include <linux/module.h>
38*4882a593Smuzhiyun #include <linux/net_tstamp.h>
39*4882a593Smuzhiyun #include <linux/skbuff.h>
40*4882a593Smuzhiyun #include <linux/netdevice.h>
41*4882a593Smuzhiyun #include <linux/pps_kernel.h>
42*4882a593Smuzhiyun #include <linux/ptp_clock_kernel.h>
43*4882a593Smuzhiyun #include <linux/ptp_classify.h>
44*4882a593Smuzhiyun #include <linux/udp.h>
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #include "cxgb4.h"
47*4882a593Smuzhiyun #include "t4_hw.h"
48*4882a593Smuzhiyun #include "t4_regs.h"
49*4882a593Smuzhiyun #include "t4_msg.h"
50*4882a593Smuzhiyun #include "t4fw_api.h"
51*4882a593Smuzhiyun #include "cxgb4_ptp.h"
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun  * cxgb4_ptp_is_ptp_tx - determine whether TX packet is PTP or not
55*4882a593Smuzhiyun  * @skb: skb of outgoing ptp request
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  */
cxgb4_ptp_is_ptp_tx(struct sk_buff * skb)58*4882a593Smuzhiyun bool cxgb4_ptp_is_ptp_tx(struct sk_buff *skb)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	struct udphdr *uh;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	uh = udp_hdr(skb);
63*4882a593Smuzhiyun 	return skb->len >= PTP_MIN_LENGTH &&
64*4882a593Smuzhiyun 		skb->len <= PTP_IN_TRANSMIT_PACKET_MAXNUM &&
65*4882a593Smuzhiyun 		likely(skb->protocol == htons(ETH_P_IP)) &&
66*4882a593Smuzhiyun 		ip_hdr(skb)->protocol == IPPROTO_UDP &&
67*4882a593Smuzhiyun 		uh->dest == htons(PTP_EVENT_PORT);
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
is_ptp_enabled(struct sk_buff * skb,struct net_device * dev)70*4882a593Smuzhiyun bool is_ptp_enabled(struct sk_buff *skb, struct net_device *dev)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	struct port_info *pi;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	pi = netdev_priv(dev);
75*4882a593Smuzhiyun 	return (pi->ptp_enable && cxgb4_xmit_with_hwtstamp(skb) &&
76*4882a593Smuzhiyun 		cxgb4_ptp_is_ptp_tx(skb));
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /**
80*4882a593Smuzhiyun  * cxgb4_ptp_is_ptp_rx - determine whether RX packet is PTP or not
81*4882a593Smuzhiyun  * @skb: skb of incoming ptp request
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  */
cxgb4_ptp_is_ptp_rx(struct sk_buff * skb)84*4882a593Smuzhiyun bool cxgb4_ptp_is_ptp_rx(struct sk_buff *skb)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	struct udphdr *uh = (struct udphdr *)(skb->data + ETH_HLEN +
87*4882a593Smuzhiyun 					      IPV4_HLEN(skb->data));
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	return  uh->dest == htons(PTP_EVENT_PORT) &&
90*4882a593Smuzhiyun 		uh->source == htons(PTP_EVENT_PORT);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun /**
94*4882a593Smuzhiyun  * cxgb4_ptp_read_hwstamp - read timestamp for TX event PTP message
95*4882a593Smuzhiyun  * @adapter: board private structure
96*4882a593Smuzhiyun  * @pi: port private structure
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  */
cxgb4_ptp_read_hwstamp(struct adapter * adapter,struct port_info * pi)99*4882a593Smuzhiyun void cxgb4_ptp_read_hwstamp(struct adapter *adapter, struct port_info *pi)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	struct skb_shared_hwtstamps *skb_ts = NULL;
102*4882a593Smuzhiyun 	u64 tx_ts;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	skb_ts = skb_hwtstamps(adapter->ptp_tx_skb);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	tx_ts = t4_read_reg(adapter,
107*4882a593Smuzhiyun 			    T5_PORT_REG(pi->port_id, MAC_PORT_TX_TS_VAL_LO));
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	tx_ts |= (u64)t4_read_reg(adapter,
110*4882a593Smuzhiyun 				  T5_PORT_REG(pi->port_id,
111*4882a593Smuzhiyun 					      MAC_PORT_TX_TS_VAL_HI)) << 32;
112*4882a593Smuzhiyun 	skb_ts->hwtstamp = ns_to_ktime(tx_ts);
113*4882a593Smuzhiyun 	skb_tstamp_tx(adapter->ptp_tx_skb, skb_ts);
114*4882a593Smuzhiyun 	dev_kfree_skb_any(adapter->ptp_tx_skb);
115*4882a593Smuzhiyun 	spin_lock(&adapter->ptp_lock);
116*4882a593Smuzhiyun 	adapter->ptp_tx_skb = NULL;
117*4882a593Smuzhiyun 	spin_unlock(&adapter->ptp_lock);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /**
121*4882a593Smuzhiyun  * cxgb4_ptprx_timestamping - Enable Timestamp for RX PTP event message
122*4882a593Smuzhiyun  * @pi: port private structure
123*4882a593Smuzhiyun  * @port: pot number
124*4882a593Smuzhiyun  * @mode: RX mode
125*4882a593Smuzhiyun  *
126*4882a593Smuzhiyun  */
cxgb4_ptprx_timestamping(struct port_info * pi,u8 port,u16 mode)127*4882a593Smuzhiyun int cxgb4_ptprx_timestamping(struct port_info *pi, u8 port, u16 mode)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	struct adapter *adapter = pi->adapter;
130*4882a593Smuzhiyun 	struct fw_ptp_cmd c;
131*4882a593Smuzhiyun 	int err;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	memset(&c, 0, sizeof(c));
134*4882a593Smuzhiyun 	c.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PTP_CMD) |
135*4882a593Smuzhiyun 				     FW_CMD_REQUEST_F |
136*4882a593Smuzhiyun 				     FW_CMD_WRITE_F |
137*4882a593Smuzhiyun 				     FW_PTP_CMD_PORTID_V(port));
138*4882a593Smuzhiyun 	c.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(sizeof(c) / 16));
139*4882a593Smuzhiyun 	c.u.init.sc = FW_PTP_SC_RXTIME_STAMP;
140*4882a593Smuzhiyun 	c.u.init.mode = cpu_to_be16(mode);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	err = t4_wr_mbox(adapter, adapter->mbox, &c, sizeof(c), NULL);
143*4882a593Smuzhiyun 	if (err < 0)
144*4882a593Smuzhiyun 		dev_err(adapter->pdev_dev,
145*4882a593Smuzhiyun 			"PTP: %s error %d\n", __func__, -err);
146*4882a593Smuzhiyun 	return err;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
cxgb4_ptp_txtype(struct adapter * adapter,u8 port)149*4882a593Smuzhiyun int cxgb4_ptp_txtype(struct adapter *adapter, u8 port)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun 	struct fw_ptp_cmd c;
152*4882a593Smuzhiyun 	int err;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	memset(&c, 0, sizeof(c));
155*4882a593Smuzhiyun 	c.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PTP_CMD) |
156*4882a593Smuzhiyun 				     FW_CMD_REQUEST_F |
157*4882a593Smuzhiyun 				     FW_CMD_WRITE_F |
158*4882a593Smuzhiyun 				     FW_PTP_CMD_PORTID_V(port));
159*4882a593Smuzhiyun 	c.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(sizeof(c) / 16));
160*4882a593Smuzhiyun 	c.u.init.sc = FW_PTP_SC_TX_TYPE;
161*4882a593Smuzhiyun 	c.u.init.mode = cpu_to_be16(PTP_TS_NONE);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	err = t4_wr_mbox(adapter, adapter->mbox, &c, sizeof(c), NULL);
164*4882a593Smuzhiyun 	if (err < 0)
165*4882a593Smuzhiyun 		dev_err(adapter->pdev_dev,
166*4882a593Smuzhiyun 			"PTP: %s error %d\n", __func__, -err);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	return err;
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun 
cxgb4_ptp_redirect_rx_packet(struct adapter * adapter,struct port_info * pi)171*4882a593Smuzhiyun int cxgb4_ptp_redirect_rx_packet(struct adapter *adapter, struct port_info *pi)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun 	struct sge *s = &adapter->sge;
174*4882a593Smuzhiyun 	struct sge_eth_rxq *receive_q =  &s->ethrxq[pi->first_qset];
175*4882a593Smuzhiyun 	struct fw_ptp_cmd c;
176*4882a593Smuzhiyun 	int err;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	memset(&c, 0, sizeof(c));
179*4882a593Smuzhiyun 	c.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PTP_CMD) |
180*4882a593Smuzhiyun 				     FW_CMD_REQUEST_F |
181*4882a593Smuzhiyun 				     FW_CMD_WRITE_F |
182*4882a593Smuzhiyun 				     FW_PTP_CMD_PORTID_V(pi->port_id));
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	c.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(sizeof(c) / 16));
185*4882a593Smuzhiyun 	c.u.init.sc = FW_PTP_SC_RDRX_TYPE;
186*4882a593Smuzhiyun 	c.u.init.txchan = pi->tx_chan;
187*4882a593Smuzhiyun 	c.u.init.absid = cpu_to_be16(receive_q->rspq.abs_id);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	err = t4_wr_mbox(adapter, adapter->mbox, &c, sizeof(c), NULL);
190*4882a593Smuzhiyun 	if (err < 0)
191*4882a593Smuzhiyun 		dev_err(adapter->pdev_dev,
192*4882a593Smuzhiyun 			"PTP: %s error %d\n", __func__, -err);
193*4882a593Smuzhiyun 	return err;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun /**
197*4882a593Smuzhiyun  * cxgb4_ptp_adjfreq - Adjust frequency of PHC cycle counter
198*4882a593Smuzhiyun  * @ptp: ptp clock structure
199*4882a593Smuzhiyun  * @ppb: Desired frequency change in parts per billion
200*4882a593Smuzhiyun  *
201*4882a593Smuzhiyun  * Adjust the frequency of the PHC cycle counter by the indicated ppb from
202*4882a593Smuzhiyun  * the base frequency.
203*4882a593Smuzhiyun  */
cxgb4_ptp_adjfreq(struct ptp_clock_info * ptp,s32 ppb)204*4882a593Smuzhiyun static int cxgb4_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	struct adapter *adapter = (struct adapter *)container_of(ptp,
207*4882a593Smuzhiyun 				   struct adapter, ptp_clock_info);
208*4882a593Smuzhiyun 	struct fw_ptp_cmd c;
209*4882a593Smuzhiyun 	int err;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	memset(&c, 0, sizeof(c));
212*4882a593Smuzhiyun 	c.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PTP_CMD) |
213*4882a593Smuzhiyun 				     FW_CMD_REQUEST_F |
214*4882a593Smuzhiyun 				     FW_CMD_WRITE_F |
215*4882a593Smuzhiyun 				     FW_PTP_CMD_PORTID_V(0));
216*4882a593Smuzhiyun 	c.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(sizeof(c) / 16));
217*4882a593Smuzhiyun 	c.u.ts.sc = FW_PTP_SC_ADJ_FREQ;
218*4882a593Smuzhiyun 	c.u.ts.sign = (ppb < 0) ? 1 : 0;
219*4882a593Smuzhiyun 	if (ppb < 0)
220*4882a593Smuzhiyun 		ppb = -ppb;
221*4882a593Smuzhiyun 	c.u.ts.ppb = cpu_to_be32(ppb);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	err = t4_wr_mbox(adapter, adapter->mbox, &c, sizeof(c), NULL);
224*4882a593Smuzhiyun 	if (err < 0)
225*4882a593Smuzhiyun 		dev_err(adapter->pdev_dev,
226*4882a593Smuzhiyun 			"PTP: %s error %d\n", __func__, -err);
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	return err;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun  * cxgb4_ptp_fineadjtime - Shift the time of the hardware clock
233*4882a593Smuzhiyun  * @adapter: board private structure
234*4882a593Smuzhiyun  * @delta: Desired change in nanoseconds
235*4882a593Smuzhiyun  *
236*4882a593Smuzhiyun  * Adjust the timer by resetting the timecounter structure.
237*4882a593Smuzhiyun  */
cxgb4_ptp_fineadjtime(struct adapter * adapter,s64 delta)238*4882a593Smuzhiyun static int  cxgb4_ptp_fineadjtime(struct adapter *adapter, s64 delta)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	struct fw_ptp_cmd c;
241*4882a593Smuzhiyun 	int err;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	memset(&c, 0, sizeof(c));
244*4882a593Smuzhiyun 	c.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PTP_CMD) |
245*4882a593Smuzhiyun 			     FW_CMD_REQUEST_F |
246*4882a593Smuzhiyun 			     FW_CMD_WRITE_F |
247*4882a593Smuzhiyun 			     FW_PTP_CMD_PORTID_V(0));
248*4882a593Smuzhiyun 	c.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(sizeof(c) / 16));
249*4882a593Smuzhiyun 	c.u.ts.sc = FW_PTP_SC_ADJ_FTIME;
250*4882a593Smuzhiyun 	c.u.ts.sign = (delta < 0) ? 1 : 0;
251*4882a593Smuzhiyun 	if (delta < 0)
252*4882a593Smuzhiyun 		delta = -delta;
253*4882a593Smuzhiyun 	c.u.ts.tm = cpu_to_be64(delta);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	err = t4_wr_mbox(adapter, adapter->mbox, &c, sizeof(c), NULL);
256*4882a593Smuzhiyun 	if (err < 0)
257*4882a593Smuzhiyun 		dev_err(adapter->pdev_dev,
258*4882a593Smuzhiyun 			"PTP: %s error %d\n", __func__, -err);
259*4882a593Smuzhiyun 	return err;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun /**
263*4882a593Smuzhiyun  * cxgb4_ptp_adjtime - Shift the time of the hardware clock
264*4882a593Smuzhiyun  * @ptp: ptp clock structure
265*4882a593Smuzhiyun  * @delta: Desired change in nanoseconds
266*4882a593Smuzhiyun  *
267*4882a593Smuzhiyun  * Adjust the timer by resetting the timecounter structure.
268*4882a593Smuzhiyun  */
cxgb4_ptp_adjtime(struct ptp_clock_info * ptp,s64 delta)269*4882a593Smuzhiyun static int cxgb4_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun 	struct adapter *adapter =
272*4882a593Smuzhiyun 		(struct adapter *)container_of(ptp, struct adapter,
273*4882a593Smuzhiyun 					       ptp_clock_info);
274*4882a593Smuzhiyun 	struct fw_ptp_cmd c;
275*4882a593Smuzhiyun 	s64 sign = 1;
276*4882a593Smuzhiyun 	int err;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	if (delta < 0)
279*4882a593Smuzhiyun 		sign = -1;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	if (delta * sign > PTP_CLOCK_MAX_ADJTIME) {
282*4882a593Smuzhiyun 		memset(&c, 0, sizeof(c));
283*4882a593Smuzhiyun 		c.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PTP_CMD) |
284*4882a593Smuzhiyun 					     FW_CMD_REQUEST_F |
285*4882a593Smuzhiyun 					     FW_CMD_WRITE_F |
286*4882a593Smuzhiyun 					     FW_PTP_CMD_PORTID_V(0));
287*4882a593Smuzhiyun 		c.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(sizeof(c) / 16));
288*4882a593Smuzhiyun 		c.u.ts.sc = FW_PTP_SC_ADJ_TIME;
289*4882a593Smuzhiyun 		c.u.ts.sign = (delta < 0) ? 1 : 0;
290*4882a593Smuzhiyun 		if (delta < 0)
291*4882a593Smuzhiyun 			delta = -delta;
292*4882a593Smuzhiyun 		c.u.ts.tm = cpu_to_be64(delta);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 		err = t4_wr_mbox(adapter, adapter->mbox, &c, sizeof(c), NULL);
295*4882a593Smuzhiyun 		if (err < 0)
296*4882a593Smuzhiyun 			dev_err(adapter->pdev_dev,
297*4882a593Smuzhiyun 				"PTP: %s error %d\n", __func__, -err);
298*4882a593Smuzhiyun 	} else {
299*4882a593Smuzhiyun 		err = cxgb4_ptp_fineadjtime(adapter, delta);
300*4882a593Smuzhiyun 	}
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	return err;
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun /**
306*4882a593Smuzhiyun  * cxgb4_ptp_gettime - Reads the current time from the hardware clock
307*4882a593Smuzhiyun  * @ptp: ptp clock structure
308*4882a593Smuzhiyun  * @ts: timespec structure to hold the current time value
309*4882a593Smuzhiyun  *
310*4882a593Smuzhiyun  * Read the timecounter and return the correct value in ns after converting
311*4882a593Smuzhiyun  * it into a struct timespec.
312*4882a593Smuzhiyun  */
cxgb4_ptp_gettime(struct ptp_clock_info * ptp,struct timespec64 * ts)313*4882a593Smuzhiyun static int cxgb4_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	struct adapter *adapter = container_of(ptp, struct adapter,
316*4882a593Smuzhiyun 					       ptp_clock_info);
317*4882a593Smuzhiyun 	u64 ns;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	ns = t4_read_reg(adapter, T5_PORT_REG(0, MAC_PORT_PTP_SUM_LO_A));
320*4882a593Smuzhiyun 	ns |= (u64)t4_read_reg(adapter,
321*4882a593Smuzhiyun 			       T5_PORT_REG(0, MAC_PORT_PTP_SUM_HI_A)) << 32;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	/* convert to timespec*/
324*4882a593Smuzhiyun 	*ts = ns_to_timespec64(ns);
325*4882a593Smuzhiyun 	return 0;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun /**
329*4882a593Smuzhiyun  *  cxgb4_ptp_settime - Set the current time on the hardware clock
330*4882a593Smuzhiyun  *  @ptp: ptp clock structure
331*4882a593Smuzhiyun  *  @ts: timespec containing the new time for the cycle counter
332*4882a593Smuzhiyun  *
333*4882a593Smuzhiyun  *  Reset value to new base value instead of the kernel
334*4882a593Smuzhiyun  *  wall timer value.
335*4882a593Smuzhiyun  */
cxgb4_ptp_settime(struct ptp_clock_info * ptp,const struct timespec64 * ts)336*4882a593Smuzhiyun static int cxgb4_ptp_settime(struct ptp_clock_info *ptp,
337*4882a593Smuzhiyun 			     const struct timespec64 *ts)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun 	struct adapter *adapter = (struct adapter *)container_of(ptp,
340*4882a593Smuzhiyun 				   struct adapter, ptp_clock_info);
341*4882a593Smuzhiyun 	struct fw_ptp_cmd c;
342*4882a593Smuzhiyun 	u64 ns;
343*4882a593Smuzhiyun 	int err;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	memset(&c, 0, sizeof(c));
346*4882a593Smuzhiyun 	c.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PTP_CMD) |
347*4882a593Smuzhiyun 				     FW_CMD_REQUEST_F |
348*4882a593Smuzhiyun 				     FW_CMD_WRITE_F |
349*4882a593Smuzhiyun 				     FW_PTP_CMD_PORTID_V(0));
350*4882a593Smuzhiyun 	c.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(sizeof(c) / 16));
351*4882a593Smuzhiyun 	c.u.ts.sc = FW_PTP_SC_SET_TIME;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	ns = timespec64_to_ns(ts);
354*4882a593Smuzhiyun 	c.u.ts.tm = cpu_to_be64(ns);
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	err =  t4_wr_mbox(adapter, adapter->mbox, &c, sizeof(c), NULL);
357*4882a593Smuzhiyun 	if (err < 0)
358*4882a593Smuzhiyun 		dev_err(adapter->pdev_dev,
359*4882a593Smuzhiyun 			"PTP: %s error %d\n", __func__, -err);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	return err;
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun 
cxgb4_init_ptp_timer(struct adapter * adapter)364*4882a593Smuzhiyun static void cxgb4_init_ptp_timer(struct adapter *adapter)
365*4882a593Smuzhiyun {
366*4882a593Smuzhiyun 	struct fw_ptp_cmd c;
367*4882a593Smuzhiyun 	int err;
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	memset(&c, 0, sizeof(c));
370*4882a593Smuzhiyun 	c.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PTP_CMD) |
371*4882a593Smuzhiyun 				     FW_CMD_REQUEST_F |
372*4882a593Smuzhiyun 				     FW_CMD_WRITE_F |
373*4882a593Smuzhiyun 				     FW_PTP_CMD_PORTID_V(0));
374*4882a593Smuzhiyun 	c.retval_len16 = cpu_to_be32(FW_CMD_LEN16_V(sizeof(c) / 16));
375*4882a593Smuzhiyun 	c.u.scmd.sc = FW_PTP_SC_INIT_TIMER;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	err = t4_wr_mbox(adapter, adapter->mbox, &c, sizeof(c), NULL);
378*4882a593Smuzhiyun 	if (err < 0)
379*4882a593Smuzhiyun 		dev_err(adapter->pdev_dev,
380*4882a593Smuzhiyun 			"PTP: %s error %d\n", __func__, -err);
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun /**
384*4882a593Smuzhiyun  * cxgb4_ptp_enable - enable or disable an ancillary feature
385*4882a593Smuzhiyun  * @ptp: ptp clock structure
386*4882a593Smuzhiyun  * @request: Desired resource to enable or disable
387*4882a593Smuzhiyun  * @on: Caller passes one to enable or zero to disable
388*4882a593Smuzhiyun  *
389*4882a593Smuzhiyun  * Enable (or disable) ancillary features of the PHC subsystem.
390*4882a593Smuzhiyun  * Currently, no ancillary features are supported.
391*4882a593Smuzhiyun  */
cxgb4_ptp_enable(struct ptp_clock_info __always_unused * ptp,struct ptp_clock_request __always_unused * request,int __always_unused on)392*4882a593Smuzhiyun static int cxgb4_ptp_enable(struct ptp_clock_info __always_unused *ptp,
393*4882a593Smuzhiyun 			    struct ptp_clock_request __always_unused *request,
394*4882a593Smuzhiyun 			    int __always_unused on)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun 	return -ENOTSUPP;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun static const struct ptp_clock_info cxgb4_ptp_clock_info = {
400*4882a593Smuzhiyun 	.owner          = THIS_MODULE,
401*4882a593Smuzhiyun 	.name           = "cxgb4_clock",
402*4882a593Smuzhiyun 	.max_adj        = MAX_PTP_FREQ_ADJ,
403*4882a593Smuzhiyun 	.n_alarm        = 0,
404*4882a593Smuzhiyun 	.n_ext_ts       = 0,
405*4882a593Smuzhiyun 	.n_per_out      = 0,
406*4882a593Smuzhiyun 	.pps            = 0,
407*4882a593Smuzhiyun 	.adjfreq        = cxgb4_ptp_adjfreq,
408*4882a593Smuzhiyun 	.adjtime        = cxgb4_ptp_adjtime,
409*4882a593Smuzhiyun 	.gettime64      = cxgb4_ptp_gettime,
410*4882a593Smuzhiyun 	.settime64      = cxgb4_ptp_settime,
411*4882a593Smuzhiyun 	.enable         = cxgb4_ptp_enable,
412*4882a593Smuzhiyun };
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun /**
415*4882a593Smuzhiyun  * cxgb4_ptp_init - initialize PTP for devices which support it
416*4882a593Smuzhiyun  * @adapter: board private structure
417*4882a593Smuzhiyun  *
418*4882a593Smuzhiyun  * This function performs the required steps for enabling PTP support.
419*4882a593Smuzhiyun  */
cxgb4_ptp_init(struct adapter * adapter)420*4882a593Smuzhiyun void cxgb4_ptp_init(struct adapter *adapter)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun 	struct timespec64 now;
423*4882a593Smuzhiyun 	 /* no need to create a clock device if we already have one */
424*4882a593Smuzhiyun 	if (!IS_ERR_OR_NULL(adapter->ptp_clock))
425*4882a593Smuzhiyun 		return;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	adapter->ptp_tx_skb = NULL;
428*4882a593Smuzhiyun 	adapter->ptp_clock_info = cxgb4_ptp_clock_info;
429*4882a593Smuzhiyun 	spin_lock_init(&adapter->ptp_lock);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
432*4882a593Smuzhiyun 						&adapter->pdev->dev);
433*4882a593Smuzhiyun 	if (IS_ERR_OR_NULL(adapter->ptp_clock)) {
434*4882a593Smuzhiyun 		adapter->ptp_clock = NULL;
435*4882a593Smuzhiyun 		dev_err(adapter->pdev_dev,
436*4882a593Smuzhiyun 			"PTP %s Clock registration has failed\n", __func__);
437*4882a593Smuzhiyun 		return;
438*4882a593Smuzhiyun 	}
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	now = ktime_to_timespec64(ktime_get_real());
441*4882a593Smuzhiyun 	cxgb4_init_ptp_timer(adapter);
442*4882a593Smuzhiyun 	if (cxgb4_ptp_settime(&adapter->ptp_clock_info, &now) < 0) {
443*4882a593Smuzhiyun 		ptp_clock_unregister(adapter->ptp_clock);
444*4882a593Smuzhiyun 		adapter->ptp_clock = NULL;
445*4882a593Smuzhiyun 	}
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun /**
449*4882a593Smuzhiyun  * cxgb4_ptp_remove - disable PTP device and stop the overflow check
450*4882a593Smuzhiyun  * @adapter: board private structure
451*4882a593Smuzhiyun  *
452*4882a593Smuzhiyun  * Stop the PTP support.
453*4882a593Smuzhiyun  */
cxgb4_ptp_stop(struct adapter * adapter)454*4882a593Smuzhiyun void cxgb4_ptp_stop(struct adapter *adapter)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun 	if (adapter->ptp_tx_skb) {
457*4882a593Smuzhiyun 		dev_kfree_skb_any(adapter->ptp_tx_skb);
458*4882a593Smuzhiyun 		adapter->ptp_tx_skb = NULL;
459*4882a593Smuzhiyun 	}
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	if (adapter->ptp_clock) {
462*4882a593Smuzhiyun 		ptp_clock_unregister(adapter->ptp_clock);
463*4882a593Smuzhiyun 		adapter->ptp_clock = NULL;
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun }
466