xref: /linux/drivers/net/ethernet/intel/idpf/idpf_ptp.c (revision 1b98f357dadd6ea613a435fbaef1a5dd7b35fd21)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2024 Intel Corporation */
3 
4 #include "idpf.h"
5 #include "idpf_ptp.h"
6 
7 /**
8  * idpf_ptp_get_access - Determine the access type of the PTP features
9  * @adapter: Driver specific private structure
10  * @direct: Capability that indicates the direct access
11  * @mailbox: Capability that indicates the mailbox access
12  *
13  * Return: the type of supported access for the PTP feature.
14  */
15 static enum idpf_ptp_access
16 idpf_ptp_get_access(const struct idpf_adapter *adapter, u32 direct, u32 mailbox)
17 {
18 	if (adapter->ptp->caps & direct)
19 		return IDPF_PTP_DIRECT;
20 	else if (adapter->ptp->caps & mailbox)
21 		return IDPF_PTP_MAILBOX;
22 	else
23 		return IDPF_PTP_NONE;
24 }
25 
26 /**
27  * idpf_ptp_get_features_access - Determine the access type of PTP features
28  * @adapter: Driver specific private structure
29  *
30  * Fulfill the adapter structure with type of the supported PTP features
31  * access.
32  */
33 void idpf_ptp_get_features_access(const struct idpf_adapter *adapter)
34 {
35 	struct idpf_ptp *ptp = adapter->ptp;
36 	u32 direct, mailbox;
37 
38 	/* Get the device clock time */
39 	direct = VIRTCHNL2_CAP_PTP_GET_DEVICE_CLK_TIME;
40 	mailbox = VIRTCHNL2_CAP_PTP_GET_DEVICE_CLK_TIME_MB;
41 	ptp->get_dev_clk_time_access = idpf_ptp_get_access(adapter,
42 							   direct,
43 							   mailbox);
44 
45 	/* Set the device clock time */
46 	direct = VIRTCHNL2_CAP_PTP_SET_DEVICE_CLK_TIME;
47 	mailbox = VIRTCHNL2_CAP_PTP_SET_DEVICE_CLK_TIME;
48 	ptp->set_dev_clk_time_access = idpf_ptp_get_access(adapter,
49 							   direct,
50 							   mailbox);
51 
52 	/* Adjust the device clock time */
53 	direct = VIRTCHNL2_CAP_PTP_ADJ_DEVICE_CLK;
54 	mailbox = VIRTCHNL2_CAP_PTP_ADJ_DEVICE_CLK_MB;
55 	ptp->adj_dev_clk_time_access = idpf_ptp_get_access(adapter,
56 							   direct,
57 							   mailbox);
58 
59 	/* Tx timestamping */
60 	direct = VIRTCHNL2_CAP_PTP_TX_TSTAMPS;
61 	mailbox = VIRTCHNL2_CAP_PTP_TX_TSTAMPS_MB;
62 	ptp->tx_tstamp_access = idpf_ptp_get_access(adapter,
63 						    direct,
64 						    mailbox);
65 }
66 
67 /**
68  * idpf_ptp_enable_shtime - Enable shadow time and execute a command
69  * @adapter: Driver specific private structure
70  */
71 static void idpf_ptp_enable_shtime(struct idpf_adapter *adapter)
72 {
73 	u32 shtime_enable, exec_cmd;
74 
75 	/* Get offsets */
76 	shtime_enable = adapter->ptp->cmd.shtime_enable_mask;
77 	exec_cmd = adapter->ptp->cmd.exec_cmd_mask;
78 
79 	/* Set the shtime en and the sync field */
80 	writel(shtime_enable, adapter->ptp->dev_clk_regs.cmd_sync);
81 	writel(exec_cmd | shtime_enable, adapter->ptp->dev_clk_regs.cmd_sync);
82 }
83 
84 /**
85  * idpf_ptp_read_src_clk_reg_direct - Read directly the main timer value
86  * @adapter: Driver specific private structure
87  * @sts: Optional parameter for holding a pair of system timestamps from
88  *	 the system clock. Will be ignored when NULL is given.
89  *
90  * Return: the device clock time.
91  */
92 static u64 idpf_ptp_read_src_clk_reg_direct(struct idpf_adapter *adapter,
93 					    struct ptp_system_timestamp *sts)
94 {
95 	struct idpf_ptp *ptp = adapter->ptp;
96 	u32 hi, lo;
97 
98 	spin_lock(&ptp->read_dev_clk_lock);
99 
100 	/* Read the system timestamp pre PHC read */
101 	ptp_read_system_prets(sts);
102 
103 	idpf_ptp_enable_shtime(adapter);
104 
105 	/* Read the system timestamp post PHC read */
106 	ptp_read_system_postts(sts);
107 
108 	lo = readl(ptp->dev_clk_regs.dev_clk_ns_l);
109 	hi = readl(ptp->dev_clk_regs.dev_clk_ns_h);
110 
111 	spin_unlock(&ptp->read_dev_clk_lock);
112 
113 	return ((u64)hi << 32) | lo;
114 }
115 
116 /**
117  * idpf_ptp_read_src_clk_reg_mailbox - Read the main timer value through mailbox
118  * @adapter: Driver specific private structure
119  * @sts: Optional parameter for holding a pair of system timestamps from
120  *	 the system clock. Will be ignored when NULL is given.
121  * @src_clk: Returned main timer value in nanoseconds unit
122  *
123  * Return: 0 on success, -errno otherwise.
124  */
125 static int idpf_ptp_read_src_clk_reg_mailbox(struct idpf_adapter *adapter,
126 					     struct ptp_system_timestamp *sts,
127 					     u64 *src_clk)
128 {
129 	struct idpf_ptp_dev_timers clk_time;
130 	int err;
131 
132 	/* Read the system timestamp pre PHC read */
133 	ptp_read_system_prets(sts);
134 
135 	err = idpf_ptp_get_dev_clk_time(adapter, &clk_time);
136 	if (err)
137 		return err;
138 
139 	/* Read the system timestamp post PHC read */
140 	ptp_read_system_postts(sts);
141 
142 	*src_clk = clk_time.dev_clk_time_ns;
143 
144 	return 0;
145 }
146 
147 /**
148  * idpf_ptp_read_src_clk_reg - Read the main timer value
149  * @adapter: Driver specific private structure
150  * @src_clk: Returned main timer value in nanoseconds unit
151  * @sts: Optional parameter for holding a pair of system timestamps from
152  *	 the system clock. Will be ignored if NULL is given.
153  *
154  * Return: the device clock time on success, -errno otherwise.
155  */
156 static int idpf_ptp_read_src_clk_reg(struct idpf_adapter *adapter, u64 *src_clk,
157 				     struct ptp_system_timestamp *sts)
158 {
159 	switch (adapter->ptp->get_dev_clk_time_access) {
160 	case IDPF_PTP_NONE:
161 		return -EOPNOTSUPP;
162 	case IDPF_PTP_MAILBOX:
163 		return idpf_ptp_read_src_clk_reg_mailbox(adapter, sts, src_clk);
164 	case IDPF_PTP_DIRECT:
165 		*src_clk = idpf_ptp_read_src_clk_reg_direct(adapter, sts);
166 		break;
167 	default:
168 		return -EOPNOTSUPP;
169 	}
170 
171 	return 0;
172 }
173 
174 /**
175  * idpf_ptp_gettimex64 - Get the time of the clock
176  * @info: the driver's PTP info structure
177  * @ts: timespec64 structure to hold the current time value
178  * @sts: Optional parameter for holding a pair of system timestamps from
179  *	 the system clock. Will be ignored if NULL is given.
180  *
181  * Return: the device clock value in ns, after converting it into a timespec
182  * struct on success, -errno otherwise.
183  */
184 static int idpf_ptp_gettimex64(struct ptp_clock_info *info,
185 			       struct timespec64 *ts,
186 			       struct ptp_system_timestamp *sts)
187 {
188 	struct idpf_adapter *adapter = idpf_ptp_info_to_adapter(info);
189 	u64 time_ns;
190 	int err;
191 
192 	err = idpf_ptp_read_src_clk_reg(adapter, &time_ns, sts);
193 	if (err)
194 		return -EACCES;
195 
196 	*ts = ns_to_timespec64(time_ns);
197 
198 	return 0;
199 }
200 
201 /**
202  * idpf_ptp_update_phctime_rxq_grp - Update the cached PHC time for a given Rx
203  *				     queue group.
204  * @grp: receive queue group in which Rx timestamp is enabled
205  * @split: Indicates whether the queue model is split or single queue
206  * @systime: Cached system time
207  */
208 static void
209 idpf_ptp_update_phctime_rxq_grp(const struct idpf_rxq_group *grp, bool split,
210 				u64 systime)
211 {
212 	struct idpf_rx_queue *rxq;
213 	u16 i;
214 
215 	if (!split) {
216 		for (i = 0; i < grp->singleq.num_rxq; i++) {
217 			rxq = grp->singleq.rxqs[i];
218 			if (rxq)
219 				WRITE_ONCE(rxq->cached_phc_time, systime);
220 		}
221 	} else {
222 		for (i = 0; i < grp->splitq.num_rxq_sets; i++) {
223 			rxq = &grp->splitq.rxq_sets[i]->rxq;
224 			if (rxq)
225 				WRITE_ONCE(rxq->cached_phc_time, systime);
226 		}
227 	}
228 }
229 
230 /**
231  * idpf_ptp_update_cached_phctime - Update the cached PHC time values
232  * @adapter: Driver specific private structure
233  *
234  * This function updates the system time values which are cached in the adapter
235  * structure and the Rx queues.
236  *
237  * This function must be called periodically to ensure that the cached value
238  * is never more than 2 seconds old.
239  *
240  * Return: 0 on success, -errno otherwise.
241  */
242 static int idpf_ptp_update_cached_phctime(struct idpf_adapter *adapter)
243 {
244 	u64 systime;
245 	int err;
246 
247 	err = idpf_ptp_read_src_clk_reg(adapter, &systime, NULL);
248 	if (err)
249 		return -EACCES;
250 
251 	/* Update the cached PHC time stored in the adapter structure.
252 	 * These values are used to extend Tx timestamp values to 64 bit
253 	 * expected by the stack.
254 	 */
255 	WRITE_ONCE(adapter->ptp->cached_phc_time, systime);
256 	WRITE_ONCE(adapter->ptp->cached_phc_jiffies, jiffies);
257 
258 	idpf_for_each_vport(adapter, vport) {
259 		bool split;
260 
261 		if (!vport || !vport->rxq_grps)
262 			continue;
263 
264 		split = idpf_is_queue_model_split(vport->rxq_model);
265 
266 		for (u16 i = 0; i < vport->num_rxq_grp; i++) {
267 			struct idpf_rxq_group *grp = &vport->rxq_grps[i];
268 
269 			idpf_ptp_update_phctime_rxq_grp(grp, split, systime);
270 		}
271 	}
272 
273 	return 0;
274 }
275 
276 /**
277  * idpf_ptp_settime64 - Set the time of the clock
278  * @info: the driver's PTP info structure
279  * @ts: timespec64 structure that holds the new time value
280  *
281  * Set the device clock to the user input value. The conversion from timespec
282  * to ns happens in the write function.
283  *
284  * Return: 0 on success, -errno otherwise.
285  */
286 static int idpf_ptp_settime64(struct ptp_clock_info *info,
287 			      const struct timespec64 *ts)
288 {
289 	struct idpf_adapter *adapter = idpf_ptp_info_to_adapter(info);
290 	enum idpf_ptp_access access;
291 	int err;
292 	u64 ns;
293 
294 	access = adapter->ptp->set_dev_clk_time_access;
295 	if (access != IDPF_PTP_MAILBOX)
296 		return -EOPNOTSUPP;
297 
298 	ns = timespec64_to_ns(ts);
299 
300 	err = idpf_ptp_set_dev_clk_time(adapter, ns);
301 	if (err) {
302 		pci_err(adapter->pdev, "Failed to set the time, err: %pe\n",
303 			ERR_PTR(err));
304 		return err;
305 	}
306 
307 	err = idpf_ptp_update_cached_phctime(adapter);
308 	if (err)
309 		pci_warn(adapter->pdev,
310 			 "Unable to immediately update cached PHC time\n");
311 
312 	return 0;
313 }
314 
315 /**
316  * idpf_ptp_adjtime_nonatomic - Do a non-atomic clock adjustment
317  * @info: the driver's PTP info structure
318  * @delta: Offset in nanoseconds to adjust the time by
319  *
320  * Return: 0 on success, -errno otherwise.
321  */
322 static int idpf_ptp_adjtime_nonatomic(struct ptp_clock_info *info, s64 delta)
323 {
324 	struct timespec64 now, then;
325 	int err;
326 
327 	err = idpf_ptp_gettimex64(info, &now, NULL);
328 	if (err)
329 		return err;
330 
331 	then = ns_to_timespec64(delta);
332 	now = timespec64_add(now, then);
333 
334 	return idpf_ptp_settime64(info, &now);
335 }
336 
337 /**
338  * idpf_ptp_adjtime - Adjust the time of the clock by the indicated delta
339  * @info: the driver's PTP info structure
340  * @delta: Offset in nanoseconds to adjust the time by
341  *
342  * Return: 0 on success, -errno otherwise.
343  */
344 static int idpf_ptp_adjtime(struct ptp_clock_info *info, s64 delta)
345 {
346 	struct idpf_adapter *adapter = idpf_ptp_info_to_adapter(info);
347 	enum idpf_ptp_access access;
348 	int err;
349 
350 	access = adapter->ptp->adj_dev_clk_time_access;
351 	if (access != IDPF_PTP_MAILBOX)
352 		return -EOPNOTSUPP;
353 
354 	/* Hardware only supports atomic adjustments using signed 32-bit
355 	 * integers. For any adjustment outside this range, perform
356 	 * a non-atomic get->adjust->set flow.
357 	 */
358 	if (delta > S32_MAX || delta < S32_MIN)
359 		return idpf_ptp_adjtime_nonatomic(info, delta);
360 
361 	err = idpf_ptp_adj_dev_clk_time(adapter, delta);
362 	if (err) {
363 		pci_err(adapter->pdev, "Failed to adjust the clock with delta %lld err: %pe\n",
364 			delta, ERR_PTR(err));
365 		return err;
366 	}
367 
368 	err = idpf_ptp_update_cached_phctime(adapter);
369 	if (err)
370 		pci_warn(adapter->pdev,
371 			 "Unable to immediately update cached PHC time\n");
372 
373 	return 0;
374 }
375 
376 /**
377  * idpf_ptp_adjfine - Adjust clock increment rate
378  * @info: the driver's PTP info structure
379  * @scaled_ppm: Parts per million with 16-bit fractional field
380  *
381  * Adjust the frequency of the clock by the indicated scaled ppm from the
382  * base frequency.
383  *
384  * Return: 0 on success, -errno otherwise.
385  */
386 static int idpf_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
387 {
388 	struct idpf_adapter *adapter = idpf_ptp_info_to_adapter(info);
389 	enum idpf_ptp_access access;
390 	u64 incval, diff;
391 	int err;
392 
393 	access = adapter->ptp->adj_dev_clk_time_access;
394 	if (access != IDPF_PTP_MAILBOX)
395 		return -EOPNOTSUPP;
396 
397 	incval = adapter->ptp->base_incval;
398 
399 	diff = adjust_by_scaled_ppm(incval, scaled_ppm);
400 	err = idpf_ptp_adj_dev_clk_fine(adapter, diff);
401 	if (err)
402 		pci_err(adapter->pdev, "Failed to adjust clock increment rate for scaled ppm %ld %pe\n",
403 			scaled_ppm, ERR_PTR(err));
404 
405 	return 0;
406 }
407 
408 /**
409  * idpf_ptp_verify_pin - Verify if pin supports requested pin function
410  * @info: the driver's PTP info structure
411  * @pin: Pin index
412  * @func: Assigned function
413  * @chan: Assigned channel
414  *
415  * Return: EOPNOTSUPP as not supported yet.
416  */
417 static int idpf_ptp_verify_pin(struct ptp_clock_info *info, unsigned int pin,
418 			       enum ptp_pin_function func, unsigned int chan)
419 {
420 	return -EOPNOTSUPP;
421 }
422 
423 /**
424  * idpf_ptp_gpio_enable - Enable/disable ancillary features of PHC
425  * @info: the driver's PTP info structure
426  * @rq: The requested feature to change
427  * @on: Enable/disable flag
428  *
429  * Return: EOPNOTSUPP as not supported yet.
430  */
431 static int idpf_ptp_gpio_enable(struct ptp_clock_info *info,
432 				struct ptp_clock_request *rq, int on)
433 {
434 	return -EOPNOTSUPP;
435 }
436 
437 /**
438  * idpf_ptp_tstamp_extend_32b_to_64b - Convert a 32b nanoseconds Tx or Rx
439  *				       timestamp value to 64b.
440  * @cached_phc_time: recently cached copy of PHC time
441  * @in_timestamp: Ingress/egress 32b nanoseconds timestamp value
442  *
443  * Hardware captures timestamps which contain only 32 bits of nominal
444  * nanoseconds, as opposed to the 64bit timestamps that the stack expects.
445  *
446  * Return: Tx timestamp value extended to 64 bits based on cached PHC time.
447  */
448 u64 idpf_ptp_tstamp_extend_32b_to_64b(u64 cached_phc_time, u32 in_timestamp)
449 {
450 	u32 delta, phc_time_lo;
451 	u64 ns;
452 
453 	/* Extract the lower 32 bits of the PHC time */
454 	phc_time_lo = (u32)cached_phc_time;
455 
456 	/* Calculate the delta between the lower 32bits of the cached PHC
457 	 * time and the in_timestamp value.
458 	 */
459 	delta = in_timestamp - phc_time_lo;
460 
461 	if (delta > U32_MAX / 2) {
462 		/* Reverse the delta calculation here */
463 		delta = phc_time_lo - in_timestamp;
464 		ns = cached_phc_time - delta;
465 	} else {
466 		ns = cached_phc_time + delta;
467 	}
468 
469 	return ns;
470 }
471 
472 /**
473  * idpf_ptp_extend_ts - Convert a 40b timestamp to 64b nanoseconds
474  * @vport: Virtual port structure
475  * @in_tstamp: Ingress/egress timestamp value
476  *
477  * It is assumed that the caller verifies the timestamp is valid prior to
478  * calling this function.
479  *
480  * Extract the 32bit nominal nanoseconds and extend them. Use the cached PHC
481  * time stored in the device private PTP structure as the basis for timestamp
482  * extension.
483  *
484  * Return: Tx timestamp value extended to 64 bits.
485  */
486 u64 idpf_ptp_extend_ts(struct idpf_vport *vport, u64 in_tstamp)
487 {
488 	struct idpf_ptp *ptp = vport->adapter->ptp;
489 	unsigned long discard_time;
490 
491 	discard_time = ptp->cached_phc_jiffies + 2 * HZ;
492 
493 	if (time_is_before_jiffies(discard_time))
494 		return 0;
495 
496 	return idpf_ptp_tstamp_extend_32b_to_64b(ptp->cached_phc_time,
497 						 lower_32_bits(in_tstamp));
498 }
499 
500 /**
501  * idpf_ptp_request_ts - Request an available Tx timestamp index
502  * @tx_q: Transmit queue on which the Tx timestamp is requested
503  * @skb: The SKB to associate with this timestamp request
504  * @idx: Index of the Tx timestamp latch
505  *
506  * Request tx timestamp index negotiated during PTP init that will be set into
507  * Tx descriptor.
508  *
509  * Return: 0 and the index that can be provided to Tx descriptor on success,
510  * -errno otherwise.
511  */
512 int idpf_ptp_request_ts(struct idpf_tx_queue *tx_q, struct sk_buff *skb,
513 			u32 *idx)
514 {
515 	struct idpf_ptp_tx_tstamp *ptp_tx_tstamp;
516 	struct list_head *head;
517 
518 	/* Get the index from the free latches list */
519 	spin_lock(&tx_q->cached_tstamp_caps->latches_lock);
520 
521 	head = &tx_q->cached_tstamp_caps->latches_free;
522 	if (list_empty(head)) {
523 		spin_unlock(&tx_q->cached_tstamp_caps->latches_lock);
524 		return -ENOBUFS;
525 	}
526 
527 	ptp_tx_tstamp = list_first_entry(head, struct idpf_ptp_tx_tstamp,
528 					 list_member);
529 	list_del(&ptp_tx_tstamp->list_member);
530 
531 	ptp_tx_tstamp->skb = skb_get(skb);
532 	skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
533 
534 	/* Move the element to the used latches list */
535 	list_add(&ptp_tx_tstamp->list_member,
536 		 &tx_q->cached_tstamp_caps->latches_in_use);
537 	spin_unlock(&tx_q->cached_tstamp_caps->latches_lock);
538 
539 	*idx = ptp_tx_tstamp->idx;
540 
541 	return 0;
542 }
543 
544 /**
545  * idpf_ptp_set_rx_tstamp - Enable or disable Rx timestamping
546  * @vport: Virtual port structure
547  * @rx_filter: Receive timestamp filter
548  */
549 static void idpf_ptp_set_rx_tstamp(struct idpf_vport *vport, int rx_filter)
550 {
551 	bool enable = true, splitq;
552 
553 	splitq = idpf_is_queue_model_split(vport->rxq_model);
554 
555 	if (rx_filter == HWTSTAMP_FILTER_NONE) {
556 		enable = false;
557 		vport->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
558 	} else {
559 		vport->tstamp_config.rx_filter = HWTSTAMP_FILTER_ALL;
560 	}
561 
562 	for (u16 i = 0; i < vport->num_rxq_grp; i++) {
563 		struct idpf_rxq_group *grp = &vport->rxq_grps[i];
564 		struct idpf_rx_queue *rx_queue;
565 		u16 j, num_rxq;
566 
567 		if (splitq)
568 			num_rxq = grp->splitq.num_rxq_sets;
569 		else
570 			num_rxq = grp->singleq.num_rxq;
571 
572 		for (j = 0; j < num_rxq; j++) {
573 			if (splitq)
574 				rx_queue = &grp->splitq.rxq_sets[j]->rxq;
575 			else
576 				rx_queue = grp->singleq.rxqs[j];
577 
578 			if (enable)
579 				idpf_queue_set(PTP, rx_queue);
580 			else
581 				idpf_queue_clear(PTP, rx_queue);
582 		}
583 	}
584 }
585 
586 /**
587  * idpf_ptp_set_timestamp_mode - Setup driver for requested timestamp mode
588  * @vport: Virtual port structure
589  * @config: Hwtstamp settings requested or saved
590  *
591  * Return: 0 on success, -errno otherwise.
592  */
593 int idpf_ptp_set_timestamp_mode(struct idpf_vport *vport,
594 				struct kernel_hwtstamp_config *config)
595 {
596 	switch (config->tx_type) {
597 	case HWTSTAMP_TX_OFF:
598 		break;
599 	case HWTSTAMP_TX_ON:
600 		if (!idpf_ptp_is_vport_tx_tstamp_ena(vport))
601 			return -EINVAL;
602 		break;
603 	default:
604 		return -EINVAL;
605 	}
606 
607 	vport->tstamp_config.tx_type = config->tx_type;
608 	idpf_ptp_set_rx_tstamp(vport, config->rx_filter);
609 	*config = vport->tstamp_config;
610 
611 	return 0;
612 }
613 
614 /**
615  * idpf_tstamp_task - Delayed task to handle Tx tstamps
616  * @work: work_struct handle
617  */
618 void idpf_tstamp_task(struct work_struct *work)
619 {
620 	struct idpf_vport *vport;
621 
622 	vport = container_of(work, struct idpf_vport, tstamp_task);
623 
624 	idpf_ptp_get_tx_tstamp(vport);
625 }
626 
627 /**
628  * idpf_ptp_do_aux_work - Do PTP periodic work
629  * @info: Driver's PTP info structure
630  *
631  * Return: Number of jiffies to periodic work.
632  */
633 static long idpf_ptp_do_aux_work(struct ptp_clock_info *info)
634 {
635 	struct idpf_adapter *adapter = idpf_ptp_info_to_adapter(info);
636 
637 	idpf_ptp_update_cached_phctime(adapter);
638 
639 	return msecs_to_jiffies(500);
640 }
641 
642 /**
643  * idpf_ptp_set_caps - Set PTP capabilities
644  * @adapter: Driver specific private structure
645  *
646  * This function sets the PTP functions.
647  */
648 static void idpf_ptp_set_caps(const struct idpf_adapter *adapter)
649 {
650 	struct ptp_clock_info *info = &adapter->ptp->info;
651 
652 	snprintf(info->name, sizeof(info->name), "%s-%s-clk",
653 		 KBUILD_MODNAME, pci_name(adapter->pdev));
654 
655 	info->owner = THIS_MODULE;
656 	info->max_adj = adapter->ptp->max_adj;
657 	info->gettimex64 = idpf_ptp_gettimex64;
658 	info->settime64 = idpf_ptp_settime64;
659 	info->adjfine = idpf_ptp_adjfine;
660 	info->adjtime = idpf_ptp_adjtime;
661 	info->verify = idpf_ptp_verify_pin;
662 	info->enable = idpf_ptp_gpio_enable;
663 	info->do_aux_work = idpf_ptp_do_aux_work;
664 }
665 
666 /**
667  * idpf_ptp_create_clock - Create PTP clock device for userspace
668  * @adapter: Driver specific private structure
669  *
670  * This function creates a new PTP clock device.
671  *
672  * Return: 0 on success, -errno otherwise.
673  */
674 static int idpf_ptp_create_clock(const struct idpf_adapter *adapter)
675 {
676 	struct ptp_clock *clock;
677 
678 	idpf_ptp_set_caps(adapter);
679 
680 	/* Attempt to register the clock before enabling the hardware. */
681 	clock = ptp_clock_register(&adapter->ptp->info,
682 				   &adapter->pdev->dev);
683 	if (IS_ERR(clock)) {
684 		pci_err(adapter->pdev, "PTP clock creation failed: %pe\n",
685 			clock);
686 		return PTR_ERR(clock);
687 	}
688 
689 	adapter->ptp->clock = clock;
690 
691 	return 0;
692 }
693 
694 /**
695  * idpf_ptp_release_vport_tstamp - Release the Tx timestamps trakcers for a
696  *				   given vport.
697  * @vport: Virtual port structure
698  *
699  * Remove the queues and delete lists that tracks Tx timestamp entries for a
700  * given vport.
701  */
702 static void idpf_ptp_release_vport_tstamp(struct idpf_vport *vport)
703 {
704 	struct idpf_ptp_tx_tstamp *ptp_tx_tstamp, *tmp;
705 	struct list_head *head;
706 
707 	cancel_work_sync(&vport->tstamp_task);
708 
709 	/* Remove list with free latches */
710 	spin_lock_bh(&vport->tx_tstamp_caps->latches_lock);
711 
712 	head = &vport->tx_tstamp_caps->latches_free;
713 	list_for_each_entry_safe(ptp_tx_tstamp, tmp, head, list_member) {
714 		list_del(&ptp_tx_tstamp->list_member);
715 		kfree(ptp_tx_tstamp);
716 	}
717 
718 	/* Remove list with latches in use */
719 	head = &vport->tx_tstamp_caps->latches_in_use;
720 	list_for_each_entry_safe(ptp_tx_tstamp, tmp, head, list_member) {
721 		list_del(&ptp_tx_tstamp->list_member);
722 		kfree(ptp_tx_tstamp);
723 	}
724 
725 	spin_unlock_bh(&vport->tx_tstamp_caps->latches_lock);
726 
727 	kfree(vport->tx_tstamp_caps);
728 	vport->tx_tstamp_caps = NULL;
729 }
730 
731 /**
732  * idpf_ptp_release_tstamp - Release the Tx timestamps trackers
733  * @adapter: Driver specific private structure
734  *
735  * Remove the queues and delete lists that tracks Tx timestamp entries.
736  */
737 static void idpf_ptp_release_tstamp(struct idpf_adapter *adapter)
738 {
739 	idpf_for_each_vport(adapter, vport) {
740 		if (!idpf_ptp_is_vport_tx_tstamp_ena(vport))
741 			continue;
742 
743 		idpf_ptp_release_vport_tstamp(vport);
744 	}
745 }
746 
747 /**
748  * idpf_ptp_get_txq_tstamp_capability - Verify the timestamping capability
749  *					for a given tx queue.
750  * @txq: Transmit queue
751  *
752  * Since performing timestamp flows requires reading the device clock value and
753  * the support in the Control Plane, the function checks both factors and
754  * summarizes the support for the timestamping.
755  *
756  * Return: true if the timestamping is supported, false otherwise.
757  */
758 bool idpf_ptp_get_txq_tstamp_capability(struct idpf_tx_queue *txq)
759 {
760 	if (!txq || !txq->cached_tstamp_caps)
761 		return false;
762 	else if (txq->cached_tstamp_caps->access)
763 		return true;
764 	else
765 		return false;
766 }
767 
768 /**
769  * idpf_ptp_init - Initialize PTP hardware clock support
770  * @adapter: Driver specific private structure
771  *
772  * Set up the device for interacting with the PTP hardware clock for all
773  * functions. Function will allocate and register a ptp_clock with the
774  * PTP_1588_CLOCK infrastructure.
775  *
776  * Return: 0 on success, -errno otherwise.
777  */
778 int idpf_ptp_init(struct idpf_adapter *adapter)
779 {
780 	struct timespec64 ts;
781 	int err;
782 
783 	if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_PTP)) {
784 		pci_dbg(adapter->pdev, "PTP capability is not detected\n");
785 		return -EOPNOTSUPP;
786 	}
787 
788 	adapter->ptp = kzalloc(sizeof(*adapter->ptp), GFP_KERNEL);
789 	if (!adapter->ptp)
790 		return -ENOMEM;
791 
792 	/* add a back pointer to adapter */
793 	adapter->ptp->adapter = adapter;
794 
795 	if (adapter->dev_ops.reg_ops.ptp_reg_init)
796 		adapter->dev_ops.reg_ops.ptp_reg_init(adapter);
797 
798 	err = idpf_ptp_get_caps(adapter);
799 	if (err) {
800 		pci_err(adapter->pdev, "Failed to get PTP caps err %d\n", err);
801 		goto free_ptp;
802 	}
803 
804 	err = idpf_ptp_create_clock(adapter);
805 	if (err)
806 		goto free_ptp;
807 
808 	if (adapter->ptp->get_dev_clk_time_access != IDPF_PTP_NONE)
809 		ptp_schedule_worker(adapter->ptp->clock, 0);
810 
811 	/* Write the default increment time value if the clock adjustments
812 	 * are enabled.
813 	 */
814 	if (adapter->ptp->adj_dev_clk_time_access != IDPF_PTP_NONE) {
815 		err = idpf_ptp_adj_dev_clk_fine(adapter,
816 						adapter->ptp->base_incval);
817 		if (err)
818 			goto remove_clock;
819 	}
820 
821 	/* Write the initial time value if the set time operation is enabled */
822 	if (adapter->ptp->set_dev_clk_time_access != IDPF_PTP_NONE) {
823 		ts = ktime_to_timespec64(ktime_get_real());
824 		err = idpf_ptp_settime64(&adapter->ptp->info, &ts);
825 		if (err)
826 			goto remove_clock;
827 	}
828 
829 	spin_lock_init(&adapter->ptp->read_dev_clk_lock);
830 
831 	pci_dbg(adapter->pdev, "PTP init successful\n");
832 
833 	return 0;
834 
835 remove_clock:
836 	if (adapter->ptp->get_dev_clk_time_access != IDPF_PTP_NONE)
837 		ptp_cancel_worker_sync(adapter->ptp->clock);
838 
839 	ptp_clock_unregister(adapter->ptp->clock);
840 	adapter->ptp->clock = NULL;
841 
842 free_ptp:
843 	kfree(adapter->ptp);
844 	adapter->ptp = NULL;
845 
846 	return err;
847 }
848 
849 /**
850  * idpf_ptp_release - Clear PTP hardware clock support
851  * @adapter: Driver specific private structure
852  */
853 void idpf_ptp_release(struct idpf_adapter *adapter)
854 {
855 	struct idpf_ptp *ptp = adapter->ptp;
856 
857 	if (!ptp)
858 		return;
859 
860 	if (ptp->tx_tstamp_access != IDPF_PTP_NONE &&
861 	    ptp->get_dev_clk_time_access != IDPF_PTP_NONE)
862 		idpf_ptp_release_tstamp(adapter);
863 
864 	if (ptp->clock) {
865 		if (adapter->ptp->get_dev_clk_time_access != IDPF_PTP_NONE)
866 			ptp_cancel_worker_sync(adapter->ptp->clock);
867 
868 		ptp_clock_unregister(ptp->clock);
869 	}
870 
871 	kfree(ptp);
872 	adapter->ptp = NULL;
873 }
874