162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/* Copyright(c) 2013 - 2018 Intel Corporation. */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include "i40e.h"
562306a36Sopenharmony_ci#include <linux/ptp_classify.h>
662306a36Sopenharmony_ci#include <linux/posix-clock.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci/* The XL710 timesync is very much like Intel's 82599 design when it comes to
962306a36Sopenharmony_ci * the fundamental clock design. However, the clock operations are much simpler
1062306a36Sopenharmony_ci * in the XL710 because the device supports a full 64 bits of nanoseconds.
1162306a36Sopenharmony_ci * Because the field is so wide, we can forgo the cycle counter and just
1262306a36Sopenharmony_ci * operate with the nanosecond field directly without fear of overflow.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * Much like the 82599, the update period is dependent upon the link speed:
1562306a36Sopenharmony_ci * At 40Gb, 25Gb, or no link, the period is 1.6ns.
1662306a36Sopenharmony_ci * At 10Gb or 5Gb link, the period is multiplied by 2. (3.2ns)
1762306a36Sopenharmony_ci * At 1Gb link, the period is multiplied by 20. (32ns)
1862306a36Sopenharmony_ci * 1588 functionality is not supported at 100Mbps.
1962306a36Sopenharmony_ci */
2062306a36Sopenharmony_ci#define I40E_PTP_40GB_INCVAL		0x0199999999ULL
2162306a36Sopenharmony_ci#define I40E_PTP_10GB_INCVAL_MULT	2
2262306a36Sopenharmony_ci#define I40E_PTP_5GB_INCVAL_MULT	2
2362306a36Sopenharmony_ci#define I40E_PTP_1GB_INCVAL_MULT	20
2462306a36Sopenharmony_ci#define I40E_ISGN			0x80000000
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define I40E_PRTTSYN_CTL1_TSYNTYPE_V1  BIT(I40E_PRTTSYN_CTL1_TSYNTYPE_SHIFT)
2762306a36Sopenharmony_ci#define I40E_PRTTSYN_CTL1_TSYNTYPE_V2  (2 << \
2862306a36Sopenharmony_ci					I40E_PRTTSYN_CTL1_TSYNTYPE_SHIFT)
2962306a36Sopenharmony_ci#define I40E_SUBDEV_ID_25G_PTP_PIN	0xB
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cienum i40e_ptp_pin {
3262306a36Sopenharmony_ci	SDP3_2 = 0,
3362306a36Sopenharmony_ci	SDP3_3,
3462306a36Sopenharmony_ci	GPIO_4
3562306a36Sopenharmony_ci};
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cienum i40e_can_set_pins_t {
3862306a36Sopenharmony_ci	CANT_DO_PINS = -1,
3962306a36Sopenharmony_ci	CAN_SET_PINS,
4062306a36Sopenharmony_ci	CAN_DO_PINS
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic struct ptp_pin_desc sdp_desc[] = {
4462306a36Sopenharmony_ci	/* name     idx      func      chan */
4562306a36Sopenharmony_ci	{"SDP3_2", SDP3_2, PTP_PF_NONE, 0},
4662306a36Sopenharmony_ci	{"SDP3_3", SDP3_3, PTP_PF_NONE, 1},
4762306a36Sopenharmony_ci	{"GPIO_4", GPIO_4, PTP_PF_NONE, 1},
4862306a36Sopenharmony_ci};
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cienum i40e_ptp_gpio_pin_state {
5162306a36Sopenharmony_ci	end = -2,
5262306a36Sopenharmony_ci	invalid,
5362306a36Sopenharmony_ci	off,
5462306a36Sopenharmony_ci	in_A,
5562306a36Sopenharmony_ci	in_B,
5662306a36Sopenharmony_ci	out_A,
5762306a36Sopenharmony_ci	out_B,
5862306a36Sopenharmony_ci};
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic const char * const i40e_ptp_gpio_pin_state2str[] = {
6162306a36Sopenharmony_ci	"off", "in_A", "in_B", "out_A", "out_B"
6262306a36Sopenharmony_ci};
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_cienum i40e_ptp_led_pin_state {
6562306a36Sopenharmony_ci	led_end = -2,
6662306a36Sopenharmony_ci	low = 0,
6762306a36Sopenharmony_ci	high,
6862306a36Sopenharmony_ci};
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_cistruct i40e_ptp_pins_settings {
7162306a36Sopenharmony_ci	enum i40e_ptp_gpio_pin_state sdp3_2;
7262306a36Sopenharmony_ci	enum i40e_ptp_gpio_pin_state sdp3_3;
7362306a36Sopenharmony_ci	enum i40e_ptp_gpio_pin_state gpio_4;
7462306a36Sopenharmony_ci	enum i40e_ptp_led_pin_state led2_0;
7562306a36Sopenharmony_ci	enum i40e_ptp_led_pin_state led2_1;
7662306a36Sopenharmony_ci	enum i40e_ptp_led_pin_state led3_0;
7762306a36Sopenharmony_ci	enum i40e_ptp_led_pin_state led3_1;
7862306a36Sopenharmony_ci};
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistatic const struct i40e_ptp_pins_settings
8162306a36Sopenharmony_ci	i40e_ptp_pin_led_allowed_states[] = {
8262306a36Sopenharmony_ci	{off,	off,	off,		high,	high,	high,	high},
8362306a36Sopenharmony_ci	{off,	in_A,	off,		high,	high,	high,	low},
8462306a36Sopenharmony_ci	{off,	out_A,	off,		high,	low,	high,	high},
8562306a36Sopenharmony_ci	{off,	in_B,	off,		high,	high,	high,	low},
8662306a36Sopenharmony_ci	{off,	out_B,	off,		high,	low,	high,	high},
8762306a36Sopenharmony_ci	{in_A,	off,	off,		high,	high,	high,	low},
8862306a36Sopenharmony_ci	{in_A,	in_B,	off,		high,	high,	high,	low},
8962306a36Sopenharmony_ci	{in_A,	out_B,	off,		high,	low,	high,	high},
9062306a36Sopenharmony_ci	{out_A,	off,	off,		high,	low,	high,	high},
9162306a36Sopenharmony_ci	{out_A,	in_B,	off,		high,	low,	high,	high},
9262306a36Sopenharmony_ci	{in_B,	off,	off,		high,	high,	high,	low},
9362306a36Sopenharmony_ci	{in_B,	in_A,	off,		high,	high,	high,	low},
9462306a36Sopenharmony_ci	{in_B,	out_A,	off,		high,	low,	high,	high},
9562306a36Sopenharmony_ci	{out_B,	off,	off,		high,	low,	high,	high},
9662306a36Sopenharmony_ci	{out_B,	in_A,	off,		high,	low,	high,	high},
9762306a36Sopenharmony_ci	{off,	off,	in_A,		high,	high,	low,	high},
9862306a36Sopenharmony_ci	{off,	out_A,	in_A,		high,	low,	low,	high},
9962306a36Sopenharmony_ci	{off,	in_B,	in_A,		high,	high,	low,	low},
10062306a36Sopenharmony_ci	{off,	out_B,	in_A,		high,	low,	low,	high},
10162306a36Sopenharmony_ci	{out_A,	off,	in_A,		high,	low,	low,	high},
10262306a36Sopenharmony_ci	{out_A,	in_B,	in_A,		high,	low,	low,	high},
10362306a36Sopenharmony_ci	{in_B,	off,	in_A,		high,	high,	low,	low},
10462306a36Sopenharmony_ci	{in_B,	out_A,	in_A,		high,	low,	low,	high},
10562306a36Sopenharmony_ci	{out_B,	off,	in_A,		high,	low,	low,	high},
10662306a36Sopenharmony_ci	{off,	off,	out_A,		low,	high,	high,	high},
10762306a36Sopenharmony_ci	{off,	in_A,	out_A,		low,	high,	high,	low},
10862306a36Sopenharmony_ci	{off,	in_B,	out_A,		low,	high,	high,	low},
10962306a36Sopenharmony_ci	{off,	out_B,	out_A,		low,	low,	high,	high},
11062306a36Sopenharmony_ci	{in_A,	off,	out_A,		low,	high,	high,	low},
11162306a36Sopenharmony_ci	{in_A,	in_B,	out_A,		low,	high,	high,	low},
11262306a36Sopenharmony_ci	{in_A,	out_B,	out_A,		low,	low,	high,	high},
11362306a36Sopenharmony_ci	{in_B,	off,	out_A,		low,	high,	high,	low},
11462306a36Sopenharmony_ci	{in_B,	in_A,	out_A,		low,	high,	high,	low},
11562306a36Sopenharmony_ci	{out_B,	off,	out_A,		low,	low,	high,	high},
11662306a36Sopenharmony_ci	{out_B,	in_A,	out_A,		low,	low,	high,	high},
11762306a36Sopenharmony_ci	{off,	off,	in_B,		high,	high,	low,	high},
11862306a36Sopenharmony_ci	{off,	in_A,	in_B,		high,	high,	low,	low},
11962306a36Sopenharmony_ci	{off,	out_A,	in_B,		high,	low,	low,	high},
12062306a36Sopenharmony_ci	{off,	out_B,	in_B,		high,	low,	low,	high},
12162306a36Sopenharmony_ci	{in_A,	off,	in_B,		high,	high,	low,	low},
12262306a36Sopenharmony_ci	{in_A,	out_B,	in_B,		high,	low,	low,	high},
12362306a36Sopenharmony_ci	{out_A,	off,	in_B,		high,	low,	low,	high},
12462306a36Sopenharmony_ci	{out_B,	off,	in_B,		high,	low,	low,	high},
12562306a36Sopenharmony_ci	{out_B,	in_A,	in_B,		high,	low,	low,	high},
12662306a36Sopenharmony_ci	{off,	off,	out_B,		low,	high,	high,	high},
12762306a36Sopenharmony_ci	{off,	in_A,	out_B,		low,	high,	high,	low},
12862306a36Sopenharmony_ci	{off,	out_A,	out_B,		low,	low,	high,	high},
12962306a36Sopenharmony_ci	{off,	in_B,	out_B,		low,	high,	high,	low},
13062306a36Sopenharmony_ci	{in_A,	off,	out_B,		low,	high,	high,	low},
13162306a36Sopenharmony_ci	{in_A,	in_B,	out_B,		low,	high,	high,	low},
13262306a36Sopenharmony_ci	{out_A,	off,	out_B,		low,	low,	high,	high},
13362306a36Sopenharmony_ci	{out_A,	in_B,	out_B,		low,	low,	high,	high},
13462306a36Sopenharmony_ci	{in_B,	off,	out_B,		low,	high,	high,	low},
13562306a36Sopenharmony_ci	{in_B,	in_A,	out_B,		low,	high,	high,	low},
13662306a36Sopenharmony_ci	{in_B,	out_A,	out_B,		low,	low,	high,	high},
13762306a36Sopenharmony_ci	{end,	end,	end,	led_end, led_end, led_end, led_end}
13862306a36Sopenharmony_ci};
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_cistatic int i40e_ptp_set_pins(struct i40e_pf *pf,
14162306a36Sopenharmony_ci			     struct i40e_ptp_pins_settings *pins);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci/**
14462306a36Sopenharmony_ci * i40e_ptp_extts0_work - workqueue task function
14562306a36Sopenharmony_ci * @work: workqueue task structure
14662306a36Sopenharmony_ci *
14762306a36Sopenharmony_ci * Service for PTP external clock event
14862306a36Sopenharmony_ci **/
14962306a36Sopenharmony_cistatic void i40e_ptp_extts0_work(struct work_struct *work)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	struct i40e_pf *pf = container_of(work, struct i40e_pf,
15262306a36Sopenharmony_ci					  ptp_extts0_work);
15362306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
15462306a36Sopenharmony_ci	struct ptp_clock_event event;
15562306a36Sopenharmony_ci	u32 hi, lo;
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	/* Event time is captured by one of the two matched registers
15862306a36Sopenharmony_ci	 *      PRTTSYN_EVNT_L: 32 LSB of sampled time event
15962306a36Sopenharmony_ci	 *      PRTTSYN_EVNT_H: 32 MSB of sampled time event
16062306a36Sopenharmony_ci	 * Event is defined in PRTTSYN_EVNT_0 register
16162306a36Sopenharmony_ci	 */
16262306a36Sopenharmony_ci	lo = rd32(hw, I40E_PRTTSYN_EVNT_L(0));
16362306a36Sopenharmony_ci	hi = rd32(hw, I40E_PRTTSYN_EVNT_H(0));
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	event.timestamp = (((u64)hi) << 32) | lo;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	event.type = PTP_CLOCK_EXTTS;
16862306a36Sopenharmony_ci	event.index = hw->pf_id;
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	/* fire event */
17162306a36Sopenharmony_ci	ptp_clock_event(pf->ptp_clock, &event);
17262306a36Sopenharmony_ci}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci/**
17562306a36Sopenharmony_ci * i40e_is_ptp_pin_dev - check if device supports PTP pins
17662306a36Sopenharmony_ci * @hw: pointer to the hardware structure
17762306a36Sopenharmony_ci *
17862306a36Sopenharmony_ci * Return true if device supports PTP pins, false otherwise.
17962306a36Sopenharmony_ci **/
18062306a36Sopenharmony_cistatic bool i40e_is_ptp_pin_dev(struct i40e_hw *hw)
18162306a36Sopenharmony_ci{
18262306a36Sopenharmony_ci	return hw->device_id == I40E_DEV_ID_25G_SFP28 &&
18362306a36Sopenharmony_ci	       hw->subsystem_device_id == I40E_SUBDEV_ID_25G_PTP_PIN;
18462306a36Sopenharmony_ci}
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci/**
18762306a36Sopenharmony_ci * i40e_can_set_pins - check possibility of manipulating the pins
18862306a36Sopenharmony_ci * @pf: board private structure
18962306a36Sopenharmony_ci *
19062306a36Sopenharmony_ci * Check if all conditions are satisfied to manipulate PTP pins.
19162306a36Sopenharmony_ci * Return CAN_SET_PINS if pins can be set on a specific PF or
19262306a36Sopenharmony_ci * return CAN_DO_PINS if pins can be manipulated within a NIC or
19362306a36Sopenharmony_ci * return CANT_DO_PINS otherwise.
19462306a36Sopenharmony_ci **/
19562306a36Sopenharmony_cistatic enum i40e_can_set_pins_t i40e_can_set_pins(struct i40e_pf *pf)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	if (!i40e_is_ptp_pin_dev(&pf->hw)) {
19862306a36Sopenharmony_ci		dev_warn(&pf->pdev->dev,
19962306a36Sopenharmony_ci			 "PTP external clock not supported.\n");
20062306a36Sopenharmony_ci		return CANT_DO_PINS;
20162306a36Sopenharmony_ci	}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	if (!pf->ptp_pins) {
20462306a36Sopenharmony_ci		dev_warn(&pf->pdev->dev,
20562306a36Sopenharmony_ci			 "PTP PIN manipulation not allowed.\n");
20662306a36Sopenharmony_ci		return CANT_DO_PINS;
20762306a36Sopenharmony_ci	}
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	if (pf->hw.pf_id) {
21062306a36Sopenharmony_ci		dev_warn(&pf->pdev->dev,
21162306a36Sopenharmony_ci			 "PTP PINs should be accessed via PF0.\n");
21262306a36Sopenharmony_ci		return CAN_DO_PINS;
21362306a36Sopenharmony_ci	}
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	return CAN_SET_PINS;
21662306a36Sopenharmony_ci}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci/**
21962306a36Sopenharmony_ci * i40_ptp_reset_timing_events - Reset PTP timing events
22062306a36Sopenharmony_ci * @pf: Board private structure
22162306a36Sopenharmony_ci *
22262306a36Sopenharmony_ci * This function resets timing events for pf.
22362306a36Sopenharmony_ci **/
22462306a36Sopenharmony_cistatic void i40_ptp_reset_timing_events(struct i40e_pf *pf)
22562306a36Sopenharmony_ci{
22662306a36Sopenharmony_ci	u32 i;
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	spin_lock_bh(&pf->ptp_rx_lock);
22962306a36Sopenharmony_ci	for (i = 0; i <= I40E_PRTTSYN_RXTIME_L_MAX_INDEX; i++) {
23062306a36Sopenharmony_ci		/* reading and automatically clearing timing events registers */
23162306a36Sopenharmony_ci		rd32(&pf->hw, I40E_PRTTSYN_RXTIME_L(i));
23262306a36Sopenharmony_ci		rd32(&pf->hw, I40E_PRTTSYN_RXTIME_H(i));
23362306a36Sopenharmony_ci		pf->latch_events[i] = 0;
23462306a36Sopenharmony_ci	}
23562306a36Sopenharmony_ci	/* reading and automatically clearing timing events registers */
23662306a36Sopenharmony_ci	rd32(&pf->hw, I40E_PRTTSYN_TXTIME_L);
23762306a36Sopenharmony_ci	rd32(&pf->hw, I40E_PRTTSYN_TXTIME_H);
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	pf->tx_hwtstamp_timeouts = 0;
24062306a36Sopenharmony_ci	pf->tx_hwtstamp_skipped = 0;
24162306a36Sopenharmony_ci	pf->rx_hwtstamp_cleared = 0;
24262306a36Sopenharmony_ci	pf->latch_event_flags = 0;
24362306a36Sopenharmony_ci	spin_unlock_bh(&pf->ptp_rx_lock);
24462306a36Sopenharmony_ci}
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci/**
24762306a36Sopenharmony_ci * i40e_ptp_verify - check pins
24862306a36Sopenharmony_ci * @ptp: ptp clock
24962306a36Sopenharmony_ci * @pin: pin index
25062306a36Sopenharmony_ci * @func: assigned function
25162306a36Sopenharmony_ci * @chan: channel
25262306a36Sopenharmony_ci *
25362306a36Sopenharmony_ci * Check pins consistency.
25462306a36Sopenharmony_ci * Return 0 on success or error on failure.
25562306a36Sopenharmony_ci **/
25662306a36Sopenharmony_cistatic int i40e_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
25762306a36Sopenharmony_ci			   enum ptp_pin_function func, unsigned int chan)
25862306a36Sopenharmony_ci{
25962306a36Sopenharmony_ci	switch (func) {
26062306a36Sopenharmony_ci	case PTP_PF_NONE:
26162306a36Sopenharmony_ci	case PTP_PF_EXTTS:
26262306a36Sopenharmony_ci	case PTP_PF_PEROUT:
26362306a36Sopenharmony_ci		break;
26462306a36Sopenharmony_ci	case PTP_PF_PHYSYNC:
26562306a36Sopenharmony_ci		return -EOPNOTSUPP;
26662306a36Sopenharmony_ci	}
26762306a36Sopenharmony_ci	return 0;
26862306a36Sopenharmony_ci}
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci/**
27162306a36Sopenharmony_ci * i40e_ptp_read - Read the PHC time from the device
27262306a36Sopenharmony_ci * @pf: Board private structure
27362306a36Sopenharmony_ci * @ts: timespec structure to hold the current time value
27462306a36Sopenharmony_ci * @sts: structure to hold the system time before and after reading the PHC
27562306a36Sopenharmony_ci *
27662306a36Sopenharmony_ci * This function reads the PRTTSYN_TIME registers and stores them in a
27762306a36Sopenharmony_ci * timespec. However, since the registers are 64 bits of nanoseconds, we must
27862306a36Sopenharmony_ci * convert the result to a timespec before we can return.
27962306a36Sopenharmony_ci **/
28062306a36Sopenharmony_cistatic void i40e_ptp_read(struct i40e_pf *pf, struct timespec64 *ts,
28162306a36Sopenharmony_ci			  struct ptp_system_timestamp *sts)
28262306a36Sopenharmony_ci{
28362306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
28462306a36Sopenharmony_ci	u32 hi, lo;
28562306a36Sopenharmony_ci	u64 ns;
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	/* The timer latches on the lowest register read. */
28862306a36Sopenharmony_ci	ptp_read_system_prets(sts);
28962306a36Sopenharmony_ci	lo = rd32(hw, I40E_PRTTSYN_TIME_L);
29062306a36Sopenharmony_ci	ptp_read_system_postts(sts);
29162306a36Sopenharmony_ci	hi = rd32(hw, I40E_PRTTSYN_TIME_H);
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci	ns = (((u64)hi) << 32) | lo;
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci	*ts = ns_to_timespec64(ns);
29662306a36Sopenharmony_ci}
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci/**
29962306a36Sopenharmony_ci * i40e_ptp_write - Write the PHC time to the device
30062306a36Sopenharmony_ci * @pf: Board private structure
30162306a36Sopenharmony_ci * @ts: timespec structure that holds the new time value
30262306a36Sopenharmony_ci *
30362306a36Sopenharmony_ci * This function writes the PRTTSYN_TIME registers with the user value. Since
30462306a36Sopenharmony_ci * we receive a timespec from the stack, we must convert that timespec into
30562306a36Sopenharmony_ci * nanoseconds before programming the registers.
30662306a36Sopenharmony_ci **/
30762306a36Sopenharmony_cistatic void i40e_ptp_write(struct i40e_pf *pf, const struct timespec64 *ts)
30862306a36Sopenharmony_ci{
30962306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
31062306a36Sopenharmony_ci	u64 ns = timespec64_to_ns(ts);
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	/* The timer will not update until the high register is written, so
31362306a36Sopenharmony_ci	 * write the low register first.
31462306a36Sopenharmony_ci	 */
31562306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_TIME_L, ns & 0xFFFFFFFF);
31662306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_TIME_H, ns >> 32);
31762306a36Sopenharmony_ci}
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci/**
32062306a36Sopenharmony_ci * i40e_ptp_convert_to_hwtstamp - Convert device clock to system time
32162306a36Sopenharmony_ci * @hwtstamps: Timestamp structure to update
32262306a36Sopenharmony_ci * @timestamp: Timestamp from the hardware
32362306a36Sopenharmony_ci *
32462306a36Sopenharmony_ci * We need to convert the NIC clock value into a hwtstamp which can be used by
32562306a36Sopenharmony_ci * the upper level timestamping functions. Since the timestamp is simply a 64-
32662306a36Sopenharmony_ci * bit nanosecond value, we can call ns_to_ktime directly to handle this.
32762306a36Sopenharmony_ci **/
32862306a36Sopenharmony_cistatic void i40e_ptp_convert_to_hwtstamp(struct skb_shared_hwtstamps *hwtstamps,
32962306a36Sopenharmony_ci					 u64 timestamp)
33062306a36Sopenharmony_ci{
33162306a36Sopenharmony_ci	memset(hwtstamps, 0, sizeof(*hwtstamps));
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci	hwtstamps->hwtstamp = ns_to_ktime(timestamp);
33462306a36Sopenharmony_ci}
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci/**
33762306a36Sopenharmony_ci * i40e_ptp_adjfine - Adjust the PHC frequency
33862306a36Sopenharmony_ci * @ptp: The PTP clock structure
33962306a36Sopenharmony_ci * @scaled_ppm: Scaled parts per million adjustment from base
34062306a36Sopenharmony_ci *
34162306a36Sopenharmony_ci * Adjust the frequency of the PHC by the indicated delta from the base
34262306a36Sopenharmony_ci * frequency.
34362306a36Sopenharmony_ci *
34462306a36Sopenharmony_ci * Scaled parts per million is ppm with a 16 bit binary fractional field.
34562306a36Sopenharmony_ci **/
34662306a36Sopenharmony_cistatic int i40e_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
34762306a36Sopenharmony_ci{
34862306a36Sopenharmony_ci	struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
34962306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
35062306a36Sopenharmony_ci	u64 adj, base_adj;
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	smp_mb(); /* Force any pending update before accessing. */
35362306a36Sopenharmony_ci	base_adj = I40E_PTP_40GB_INCVAL * READ_ONCE(pf->ptp_adj_mult);
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	adj = adjust_by_scaled_ppm(base_adj, scaled_ppm);
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_INC_L, adj & 0xFFFFFFFF);
35862306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_INC_H, adj >> 32);
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci	return 0;
36162306a36Sopenharmony_ci}
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci/**
36462306a36Sopenharmony_ci * i40e_ptp_set_1pps_signal_hw - configure 1PPS PTP signal for pins
36562306a36Sopenharmony_ci * @pf: the PF private data structure
36662306a36Sopenharmony_ci *
36762306a36Sopenharmony_ci * Configure 1PPS signal used for PTP pins
36862306a36Sopenharmony_ci **/
36962306a36Sopenharmony_cistatic void i40e_ptp_set_1pps_signal_hw(struct i40e_pf *pf)
37062306a36Sopenharmony_ci{
37162306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
37262306a36Sopenharmony_ci	struct timespec64 now;
37362306a36Sopenharmony_ci	u64 ns;
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_AUX_0(1), 0);
37662306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_AUX_1(1), I40E_PRTTSYN_AUX_1_INSTNT);
37762306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_AUX_0(1), I40E_PRTTSYN_AUX_0_OUT_ENABLE);
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	i40e_ptp_read(pf, &now, NULL);
38062306a36Sopenharmony_ci	now.tv_sec += I40E_PTP_2_SEC_DELAY;
38162306a36Sopenharmony_ci	now.tv_nsec = 0;
38262306a36Sopenharmony_ci	ns = timespec64_to_ns(&now);
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	/* I40E_PRTTSYN_TGT_L(1) */
38562306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_TGT_L(1), ns & 0xFFFFFFFF);
38662306a36Sopenharmony_ci	/* I40E_PRTTSYN_TGT_H(1) */
38762306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_TGT_H(1), ns >> 32);
38862306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_CLKO(1), I40E_PTP_HALF_SECOND);
38962306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_AUX_1(1), I40E_PRTTSYN_AUX_1_INSTNT);
39062306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_AUX_0(1),
39162306a36Sopenharmony_ci	     I40E_PRTTSYN_AUX_0_OUT_ENABLE_CLK_MOD);
39262306a36Sopenharmony_ci}
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci/**
39562306a36Sopenharmony_ci * i40e_ptp_adjtime - Adjust the PHC time
39662306a36Sopenharmony_ci * @ptp: The PTP clock structure
39762306a36Sopenharmony_ci * @delta: Offset in nanoseconds to adjust the PHC time by
39862306a36Sopenharmony_ci *
39962306a36Sopenharmony_ci * Adjust the current clock time by a delta specified in nanoseconds.
40062306a36Sopenharmony_ci **/
40162306a36Sopenharmony_cistatic int i40e_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
40262306a36Sopenharmony_ci{
40362306a36Sopenharmony_ci	struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
40462306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci	mutex_lock(&pf->tmreg_lock);
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci	if (delta > -999999900LL && delta < 999999900LL) {
40962306a36Sopenharmony_ci		int neg_adj = 0;
41062306a36Sopenharmony_ci		u32 timadj;
41162306a36Sopenharmony_ci		u64 tohw;
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ci		if (delta < 0) {
41462306a36Sopenharmony_ci			neg_adj = 1;
41562306a36Sopenharmony_ci			tohw = -delta;
41662306a36Sopenharmony_ci		} else {
41762306a36Sopenharmony_ci			tohw = delta;
41862306a36Sopenharmony_ci		}
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci		timadj = tohw & 0x3FFFFFFF;
42162306a36Sopenharmony_ci		if (neg_adj)
42262306a36Sopenharmony_ci			timadj |= I40E_ISGN;
42362306a36Sopenharmony_ci		wr32(hw, I40E_PRTTSYN_ADJ, timadj);
42462306a36Sopenharmony_ci	} else {
42562306a36Sopenharmony_ci		struct timespec64 then, now;
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci		then = ns_to_timespec64(delta);
42862306a36Sopenharmony_ci		i40e_ptp_read(pf, &now, NULL);
42962306a36Sopenharmony_ci		now = timespec64_add(now, then);
43062306a36Sopenharmony_ci		i40e_ptp_write(pf, (const struct timespec64 *)&now);
43162306a36Sopenharmony_ci		i40e_ptp_set_1pps_signal_hw(pf);
43262306a36Sopenharmony_ci	}
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci	mutex_unlock(&pf->tmreg_lock);
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci	return 0;
43762306a36Sopenharmony_ci}
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ci/**
44062306a36Sopenharmony_ci * i40e_ptp_gettimex - Get the time of the PHC
44162306a36Sopenharmony_ci * @ptp: The PTP clock structure
44262306a36Sopenharmony_ci * @ts: timespec structure to hold the current time value
44362306a36Sopenharmony_ci * @sts: structure to hold the system time before and after reading the PHC
44462306a36Sopenharmony_ci *
44562306a36Sopenharmony_ci * Read the device clock and return the correct value on ns, after converting it
44662306a36Sopenharmony_ci * into a timespec struct.
44762306a36Sopenharmony_ci **/
44862306a36Sopenharmony_cistatic int i40e_ptp_gettimex(struct ptp_clock_info *ptp, struct timespec64 *ts,
44962306a36Sopenharmony_ci			     struct ptp_system_timestamp *sts)
45062306a36Sopenharmony_ci{
45162306a36Sopenharmony_ci	struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci	mutex_lock(&pf->tmreg_lock);
45462306a36Sopenharmony_ci	i40e_ptp_read(pf, ts, sts);
45562306a36Sopenharmony_ci	mutex_unlock(&pf->tmreg_lock);
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci	return 0;
45862306a36Sopenharmony_ci}
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci/**
46162306a36Sopenharmony_ci * i40e_ptp_settime - Set the time of the PHC
46262306a36Sopenharmony_ci * @ptp: The PTP clock structure
46362306a36Sopenharmony_ci * @ts: timespec64 structure that holds the new time value
46462306a36Sopenharmony_ci *
46562306a36Sopenharmony_ci * Set the device clock to the user input value. The conversion from timespec
46662306a36Sopenharmony_ci * to ns happens in the write function.
46762306a36Sopenharmony_ci **/
46862306a36Sopenharmony_cistatic int i40e_ptp_settime(struct ptp_clock_info *ptp,
46962306a36Sopenharmony_ci			    const struct timespec64 *ts)
47062306a36Sopenharmony_ci{
47162306a36Sopenharmony_ci	struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci	mutex_lock(&pf->tmreg_lock);
47462306a36Sopenharmony_ci	i40e_ptp_write(pf, ts);
47562306a36Sopenharmony_ci	mutex_unlock(&pf->tmreg_lock);
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci	return 0;
47862306a36Sopenharmony_ci}
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci/**
48162306a36Sopenharmony_ci * i40e_pps_configure - configure PPS events
48262306a36Sopenharmony_ci * @ptp: ptp clock
48362306a36Sopenharmony_ci * @rq: clock request
48462306a36Sopenharmony_ci * @on: status
48562306a36Sopenharmony_ci *
48662306a36Sopenharmony_ci * Configure PPS events for external clock source.
48762306a36Sopenharmony_ci * Return 0 on success or error on failure.
48862306a36Sopenharmony_ci **/
48962306a36Sopenharmony_cistatic int i40e_pps_configure(struct ptp_clock_info *ptp,
49062306a36Sopenharmony_ci			      struct ptp_clock_request *rq,
49162306a36Sopenharmony_ci			      int on)
49262306a36Sopenharmony_ci{
49362306a36Sopenharmony_ci	struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	if (!!on)
49662306a36Sopenharmony_ci		i40e_ptp_set_1pps_signal_hw(pf);
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci	return 0;
49962306a36Sopenharmony_ci}
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ci/**
50262306a36Sopenharmony_ci * i40e_pin_state - determine PIN state
50362306a36Sopenharmony_ci * @index: PIN index
50462306a36Sopenharmony_ci * @func: function assigned to PIN
50562306a36Sopenharmony_ci *
50662306a36Sopenharmony_ci * Determine PIN state based on PIN index and function assigned.
50762306a36Sopenharmony_ci * Return PIN state.
50862306a36Sopenharmony_ci **/
50962306a36Sopenharmony_cistatic enum i40e_ptp_gpio_pin_state i40e_pin_state(int index, int func)
51062306a36Sopenharmony_ci{
51162306a36Sopenharmony_ci	enum i40e_ptp_gpio_pin_state state = off;
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ci	if (index == 0 && func == PTP_PF_EXTTS)
51462306a36Sopenharmony_ci		state = in_A;
51562306a36Sopenharmony_ci	if (index == 1 && func == PTP_PF_EXTTS)
51662306a36Sopenharmony_ci		state = in_B;
51762306a36Sopenharmony_ci	if (index == 0 && func == PTP_PF_PEROUT)
51862306a36Sopenharmony_ci		state = out_A;
51962306a36Sopenharmony_ci	if (index == 1 && func == PTP_PF_PEROUT)
52062306a36Sopenharmony_ci		state = out_B;
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci	return state;
52362306a36Sopenharmony_ci}
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci/**
52662306a36Sopenharmony_ci * i40e_ptp_enable_pin - enable PINs.
52762306a36Sopenharmony_ci * @pf: private board structure
52862306a36Sopenharmony_ci * @chan: channel
52962306a36Sopenharmony_ci * @func: PIN function
53062306a36Sopenharmony_ci * @on: state
53162306a36Sopenharmony_ci *
53262306a36Sopenharmony_ci * Enable PTP pins for external clock source.
53362306a36Sopenharmony_ci * Return 0 on success or error code on failure.
53462306a36Sopenharmony_ci **/
53562306a36Sopenharmony_cistatic int i40e_ptp_enable_pin(struct i40e_pf *pf, unsigned int chan,
53662306a36Sopenharmony_ci			       enum ptp_pin_function func, int on)
53762306a36Sopenharmony_ci{
53862306a36Sopenharmony_ci	enum i40e_ptp_gpio_pin_state *pin = NULL;
53962306a36Sopenharmony_ci	struct i40e_ptp_pins_settings pins;
54062306a36Sopenharmony_ci	int pin_index;
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ci	/* Use PF0 to set pins. Return success for user space tools */
54362306a36Sopenharmony_ci	if (pf->hw.pf_id)
54462306a36Sopenharmony_ci		return 0;
54562306a36Sopenharmony_ci
54662306a36Sopenharmony_ci	/* Preserve previous state of pins that we don't touch */
54762306a36Sopenharmony_ci	pins.sdp3_2 = pf->ptp_pins->sdp3_2;
54862306a36Sopenharmony_ci	pins.sdp3_3 = pf->ptp_pins->sdp3_3;
54962306a36Sopenharmony_ci	pins.gpio_4 = pf->ptp_pins->gpio_4;
55062306a36Sopenharmony_ci
55162306a36Sopenharmony_ci	/* To turn on the pin - find the corresponding one based on
55262306a36Sopenharmony_ci	 * the given index. To to turn the function off - find
55362306a36Sopenharmony_ci	 * which pin had it assigned. Don't use ptp_find_pin here
55462306a36Sopenharmony_ci	 * because it tries to lock the pincfg_mux which is locked by
55562306a36Sopenharmony_ci	 * ptp_pin_store() that calls here.
55662306a36Sopenharmony_ci	 */
55762306a36Sopenharmony_ci	if (on) {
55862306a36Sopenharmony_ci		pin_index = ptp_find_pin(pf->ptp_clock, func, chan);
55962306a36Sopenharmony_ci		if (pin_index < 0)
56062306a36Sopenharmony_ci			return -EBUSY;
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_ci		switch (pin_index) {
56362306a36Sopenharmony_ci		case SDP3_2:
56462306a36Sopenharmony_ci			pin = &pins.sdp3_2;
56562306a36Sopenharmony_ci			break;
56662306a36Sopenharmony_ci		case SDP3_3:
56762306a36Sopenharmony_ci			pin = &pins.sdp3_3;
56862306a36Sopenharmony_ci			break;
56962306a36Sopenharmony_ci		case GPIO_4:
57062306a36Sopenharmony_ci			pin = &pins.gpio_4;
57162306a36Sopenharmony_ci			break;
57262306a36Sopenharmony_ci		default:
57362306a36Sopenharmony_ci			return -EINVAL;
57462306a36Sopenharmony_ci		}
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci		*pin = i40e_pin_state(chan, func);
57762306a36Sopenharmony_ci	} else {
57862306a36Sopenharmony_ci		pins.sdp3_2 = off;
57962306a36Sopenharmony_ci		pins.sdp3_3 = off;
58062306a36Sopenharmony_ci		pins.gpio_4 = off;
58162306a36Sopenharmony_ci	}
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_ci	return i40e_ptp_set_pins(pf, &pins) ? -EINVAL : 0;
58462306a36Sopenharmony_ci}
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ci/**
58762306a36Sopenharmony_ci * i40e_ptp_feature_enable - Enable external clock pins
58862306a36Sopenharmony_ci * @ptp: The PTP clock structure
58962306a36Sopenharmony_ci * @rq: The PTP clock request structure
59062306a36Sopenharmony_ci * @on: To turn feature on/off
59162306a36Sopenharmony_ci *
59262306a36Sopenharmony_ci * Setting on/off PTP PPS feature for pin.
59362306a36Sopenharmony_ci **/
59462306a36Sopenharmony_cistatic int i40e_ptp_feature_enable(struct ptp_clock_info *ptp,
59562306a36Sopenharmony_ci				   struct ptp_clock_request *rq,
59662306a36Sopenharmony_ci				   int on)
59762306a36Sopenharmony_ci{
59862306a36Sopenharmony_ci	struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps);
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_ci	enum ptp_pin_function func;
60162306a36Sopenharmony_ci	unsigned int chan;
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ci	/* TODO: Implement flags handling for EXTTS and PEROUT */
60462306a36Sopenharmony_ci	switch (rq->type) {
60562306a36Sopenharmony_ci	case PTP_CLK_REQ_EXTTS:
60662306a36Sopenharmony_ci		func = PTP_PF_EXTTS;
60762306a36Sopenharmony_ci		chan = rq->extts.index;
60862306a36Sopenharmony_ci		break;
60962306a36Sopenharmony_ci	case PTP_CLK_REQ_PEROUT:
61062306a36Sopenharmony_ci		func = PTP_PF_PEROUT;
61162306a36Sopenharmony_ci		chan = rq->perout.index;
61262306a36Sopenharmony_ci		break;
61362306a36Sopenharmony_ci	case PTP_CLK_REQ_PPS:
61462306a36Sopenharmony_ci		return i40e_pps_configure(ptp, rq, on);
61562306a36Sopenharmony_ci	default:
61662306a36Sopenharmony_ci		return -EOPNOTSUPP;
61762306a36Sopenharmony_ci	}
61862306a36Sopenharmony_ci
61962306a36Sopenharmony_ci	return i40e_ptp_enable_pin(pf, chan, func, on);
62062306a36Sopenharmony_ci}
62162306a36Sopenharmony_ci
62262306a36Sopenharmony_ci/**
62362306a36Sopenharmony_ci * i40e_ptp_get_rx_events - Read I40E_PRTTSYN_STAT_1 and latch events
62462306a36Sopenharmony_ci * @pf: the PF data structure
62562306a36Sopenharmony_ci *
62662306a36Sopenharmony_ci * This function reads I40E_PRTTSYN_STAT_1 and updates the corresponding timers
62762306a36Sopenharmony_ci * for noticed latch events. This allows the driver to keep track of the first
62862306a36Sopenharmony_ci * time a latch event was noticed which will be used to help clear out Rx
62962306a36Sopenharmony_ci * timestamps for packets that got dropped or lost.
63062306a36Sopenharmony_ci *
63162306a36Sopenharmony_ci * This function will return the current value of I40E_PRTTSYN_STAT_1 and is
63262306a36Sopenharmony_ci * expected to be called only while under the ptp_rx_lock.
63362306a36Sopenharmony_ci **/
63462306a36Sopenharmony_cistatic u32 i40e_ptp_get_rx_events(struct i40e_pf *pf)
63562306a36Sopenharmony_ci{
63662306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
63762306a36Sopenharmony_ci	u32 prttsyn_stat, new_latch_events;
63862306a36Sopenharmony_ci	int  i;
63962306a36Sopenharmony_ci
64062306a36Sopenharmony_ci	prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_1);
64162306a36Sopenharmony_ci	new_latch_events = prttsyn_stat & ~pf->latch_event_flags;
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ci	/* Update the jiffies time for any newly latched timestamp. This
64462306a36Sopenharmony_ci	 * ensures that we store the time that we first discovered a timestamp
64562306a36Sopenharmony_ci	 * was latched by the hardware. The service task will later determine
64662306a36Sopenharmony_ci	 * if we should free the latch and drop that timestamp should too much
64762306a36Sopenharmony_ci	 * time pass. This flow ensures that we only update jiffies for new
64862306a36Sopenharmony_ci	 * events latched since the last time we checked, and not all events
64962306a36Sopenharmony_ci	 * currently latched, so that the service task accounting remains
65062306a36Sopenharmony_ci	 * accurate.
65162306a36Sopenharmony_ci	 */
65262306a36Sopenharmony_ci	for (i = 0; i < 4; i++) {
65362306a36Sopenharmony_ci		if (new_latch_events & BIT(i))
65462306a36Sopenharmony_ci			pf->latch_events[i] = jiffies;
65562306a36Sopenharmony_ci	}
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci	/* Finally, we store the current status of the Rx timestamp latches */
65862306a36Sopenharmony_ci	pf->latch_event_flags = prttsyn_stat;
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_ci	return prttsyn_stat;
66162306a36Sopenharmony_ci}
66262306a36Sopenharmony_ci
66362306a36Sopenharmony_ci/**
66462306a36Sopenharmony_ci * i40e_ptp_rx_hang - Detect error case when Rx timestamp registers are hung
66562306a36Sopenharmony_ci * @pf: The PF private data structure
66662306a36Sopenharmony_ci *
66762306a36Sopenharmony_ci * This watchdog task is scheduled to detect error case where hardware has
66862306a36Sopenharmony_ci * dropped an Rx packet that was timestamped when the ring is full. The
66962306a36Sopenharmony_ci * particular error is rare but leaves the device in a state unable to timestamp
67062306a36Sopenharmony_ci * any future packets.
67162306a36Sopenharmony_ci **/
67262306a36Sopenharmony_civoid i40e_ptp_rx_hang(struct i40e_pf *pf)
67362306a36Sopenharmony_ci{
67462306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
67562306a36Sopenharmony_ci	unsigned int i, cleared = 0;
67662306a36Sopenharmony_ci
67762306a36Sopenharmony_ci	/* Since we cannot turn off the Rx timestamp logic if the device is
67862306a36Sopenharmony_ci	 * configured for Tx timestamping, we check if Rx timestamping is
67962306a36Sopenharmony_ci	 * configured. We don't want to spuriously warn about Rx timestamp
68062306a36Sopenharmony_ci	 * hangs if we don't care about the timestamps.
68162306a36Sopenharmony_ci	 */
68262306a36Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_rx)
68362306a36Sopenharmony_ci		return;
68462306a36Sopenharmony_ci
68562306a36Sopenharmony_ci	spin_lock_bh(&pf->ptp_rx_lock);
68662306a36Sopenharmony_ci
68762306a36Sopenharmony_ci	/* Update current latch times for Rx events */
68862306a36Sopenharmony_ci	i40e_ptp_get_rx_events(pf);
68962306a36Sopenharmony_ci
69062306a36Sopenharmony_ci	/* Check all the currently latched Rx events and see whether they have
69162306a36Sopenharmony_ci	 * been latched for over a second. It is assumed that any timestamp
69262306a36Sopenharmony_ci	 * should have been cleared within this time, or else it was captured
69362306a36Sopenharmony_ci	 * for a dropped frame that the driver never received. Thus, we will
69462306a36Sopenharmony_ci	 * clear any timestamp that has been latched for over 1 second.
69562306a36Sopenharmony_ci	 */
69662306a36Sopenharmony_ci	for (i = 0; i < 4; i++) {
69762306a36Sopenharmony_ci		if ((pf->latch_event_flags & BIT(i)) &&
69862306a36Sopenharmony_ci		    time_is_before_jiffies(pf->latch_events[i] + HZ)) {
69962306a36Sopenharmony_ci			rd32(hw, I40E_PRTTSYN_RXTIME_H(i));
70062306a36Sopenharmony_ci			pf->latch_event_flags &= ~BIT(i);
70162306a36Sopenharmony_ci			cleared++;
70262306a36Sopenharmony_ci		}
70362306a36Sopenharmony_ci	}
70462306a36Sopenharmony_ci
70562306a36Sopenharmony_ci	spin_unlock_bh(&pf->ptp_rx_lock);
70662306a36Sopenharmony_ci
70762306a36Sopenharmony_ci	/* Log a warning if more than 2 timestamps got dropped in the same
70862306a36Sopenharmony_ci	 * check. We don't want to warn about all drops because it can occur
70962306a36Sopenharmony_ci	 * in normal scenarios such as PTP frames on multicast addresses we
71062306a36Sopenharmony_ci	 * aren't listening to. However, administrator should know if this is
71162306a36Sopenharmony_ci	 * the reason packets aren't receiving timestamps.
71262306a36Sopenharmony_ci	 */
71362306a36Sopenharmony_ci	if (cleared > 2)
71462306a36Sopenharmony_ci		dev_dbg(&pf->pdev->dev,
71562306a36Sopenharmony_ci			"Dropped %d missed RXTIME timestamp events\n",
71662306a36Sopenharmony_ci			cleared);
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci	/* Finally, update the rx_hwtstamp_cleared counter */
71962306a36Sopenharmony_ci	pf->rx_hwtstamp_cleared += cleared;
72062306a36Sopenharmony_ci}
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci/**
72362306a36Sopenharmony_ci * i40e_ptp_tx_hang - Detect error case when Tx timestamp register is hung
72462306a36Sopenharmony_ci * @pf: The PF private data structure
72562306a36Sopenharmony_ci *
72662306a36Sopenharmony_ci * This watchdog task is run periodically to make sure that we clear the Tx
72762306a36Sopenharmony_ci * timestamp logic if we don't obtain a timestamp in a reasonable amount of
72862306a36Sopenharmony_ci * time. It is unexpected in the normal case but if it occurs it results in
72962306a36Sopenharmony_ci * permanently preventing timestamps of future packets.
73062306a36Sopenharmony_ci **/
73162306a36Sopenharmony_civoid i40e_ptp_tx_hang(struct i40e_pf *pf)
73262306a36Sopenharmony_ci{
73362306a36Sopenharmony_ci	struct sk_buff *skb;
73462306a36Sopenharmony_ci
73562306a36Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_tx)
73662306a36Sopenharmony_ci		return;
73762306a36Sopenharmony_ci
73862306a36Sopenharmony_ci	/* Nothing to do if we're not already waiting for a timestamp */
73962306a36Sopenharmony_ci	if (!test_bit(__I40E_PTP_TX_IN_PROGRESS, pf->state))
74062306a36Sopenharmony_ci		return;
74162306a36Sopenharmony_ci
74262306a36Sopenharmony_ci	/* We already have a handler routine which is run when we are notified
74362306a36Sopenharmony_ci	 * of a Tx timestamp in the hardware. If we don't get an interrupt
74462306a36Sopenharmony_ci	 * within a second it is reasonable to assume that we never will.
74562306a36Sopenharmony_ci	 */
74662306a36Sopenharmony_ci	if (time_is_before_jiffies(pf->ptp_tx_start + HZ)) {
74762306a36Sopenharmony_ci		skb = pf->ptp_tx_skb;
74862306a36Sopenharmony_ci		pf->ptp_tx_skb = NULL;
74962306a36Sopenharmony_ci		clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
75062306a36Sopenharmony_ci
75162306a36Sopenharmony_ci		/* Free the skb after we clear the bitlock */
75262306a36Sopenharmony_ci		dev_kfree_skb_any(skb);
75362306a36Sopenharmony_ci		pf->tx_hwtstamp_timeouts++;
75462306a36Sopenharmony_ci	}
75562306a36Sopenharmony_ci}
75662306a36Sopenharmony_ci
75762306a36Sopenharmony_ci/**
75862306a36Sopenharmony_ci * i40e_ptp_tx_hwtstamp - Utility function which returns the Tx timestamp
75962306a36Sopenharmony_ci * @pf: Board private structure
76062306a36Sopenharmony_ci *
76162306a36Sopenharmony_ci * Read the value of the Tx timestamp from the registers, convert it into a
76262306a36Sopenharmony_ci * value consumable by the stack, and store that result into the shhwtstamps
76362306a36Sopenharmony_ci * struct before returning it up the stack.
76462306a36Sopenharmony_ci **/
76562306a36Sopenharmony_civoid i40e_ptp_tx_hwtstamp(struct i40e_pf *pf)
76662306a36Sopenharmony_ci{
76762306a36Sopenharmony_ci	struct skb_shared_hwtstamps shhwtstamps;
76862306a36Sopenharmony_ci	struct sk_buff *skb = pf->ptp_tx_skb;
76962306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
77062306a36Sopenharmony_ci	u32 hi, lo;
77162306a36Sopenharmony_ci	u64 ns;
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_tx)
77462306a36Sopenharmony_ci		return;
77562306a36Sopenharmony_ci
77662306a36Sopenharmony_ci	/* don't attempt to timestamp if we don't have an skb */
77762306a36Sopenharmony_ci	if (!pf->ptp_tx_skb)
77862306a36Sopenharmony_ci		return;
77962306a36Sopenharmony_ci
78062306a36Sopenharmony_ci	lo = rd32(hw, I40E_PRTTSYN_TXTIME_L);
78162306a36Sopenharmony_ci	hi = rd32(hw, I40E_PRTTSYN_TXTIME_H);
78262306a36Sopenharmony_ci
78362306a36Sopenharmony_ci	ns = (((u64)hi) << 32) | lo;
78462306a36Sopenharmony_ci	i40e_ptp_convert_to_hwtstamp(&shhwtstamps, ns);
78562306a36Sopenharmony_ci
78662306a36Sopenharmony_ci	/* Clear the bit lock as soon as possible after reading the register,
78762306a36Sopenharmony_ci	 * and prior to notifying the stack via skb_tstamp_tx(). Otherwise
78862306a36Sopenharmony_ci	 * applications might wake up and attempt to request another transmit
78962306a36Sopenharmony_ci	 * timestamp prior to the bit lock being cleared.
79062306a36Sopenharmony_ci	 */
79162306a36Sopenharmony_ci	pf->ptp_tx_skb = NULL;
79262306a36Sopenharmony_ci	clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
79362306a36Sopenharmony_ci
79462306a36Sopenharmony_ci	/* Notify the stack and free the skb after we've unlocked */
79562306a36Sopenharmony_ci	skb_tstamp_tx(skb, &shhwtstamps);
79662306a36Sopenharmony_ci	dev_kfree_skb_any(skb);
79762306a36Sopenharmony_ci}
79862306a36Sopenharmony_ci
79962306a36Sopenharmony_ci/**
80062306a36Sopenharmony_ci * i40e_ptp_rx_hwtstamp - Utility function which checks for an Rx timestamp
80162306a36Sopenharmony_ci * @pf: Board private structure
80262306a36Sopenharmony_ci * @skb: Particular skb to send timestamp with
80362306a36Sopenharmony_ci * @index: Index into the receive timestamp registers for the timestamp
80462306a36Sopenharmony_ci *
80562306a36Sopenharmony_ci * The XL710 receives a notification in the receive descriptor with an offset
80662306a36Sopenharmony_ci * into the set of RXTIME registers where the timestamp is for that skb. This
80762306a36Sopenharmony_ci * function goes and fetches the receive timestamp from that offset, if a valid
80862306a36Sopenharmony_ci * one exists. The RXTIME registers are in ns, so we must convert the result
80962306a36Sopenharmony_ci * first.
81062306a36Sopenharmony_ci **/
81162306a36Sopenharmony_civoid i40e_ptp_rx_hwtstamp(struct i40e_pf *pf, struct sk_buff *skb, u8 index)
81262306a36Sopenharmony_ci{
81362306a36Sopenharmony_ci	u32 prttsyn_stat, hi, lo;
81462306a36Sopenharmony_ci	struct i40e_hw *hw;
81562306a36Sopenharmony_ci	u64 ns;
81662306a36Sopenharmony_ci
81762306a36Sopenharmony_ci	/* Since we cannot turn off the Rx timestamp logic if the device is
81862306a36Sopenharmony_ci	 * doing Tx timestamping, check if Rx timestamping is configured.
81962306a36Sopenharmony_ci	 */
82062306a36Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP) || !pf->ptp_rx)
82162306a36Sopenharmony_ci		return;
82262306a36Sopenharmony_ci
82362306a36Sopenharmony_ci	hw = &pf->hw;
82462306a36Sopenharmony_ci
82562306a36Sopenharmony_ci	spin_lock_bh(&pf->ptp_rx_lock);
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci	/* Get current Rx events and update latch times */
82862306a36Sopenharmony_ci	prttsyn_stat = i40e_ptp_get_rx_events(pf);
82962306a36Sopenharmony_ci
83062306a36Sopenharmony_ci	/* TODO: Should we warn about missing Rx timestamp event? */
83162306a36Sopenharmony_ci	if (!(prttsyn_stat & BIT(index))) {
83262306a36Sopenharmony_ci		spin_unlock_bh(&pf->ptp_rx_lock);
83362306a36Sopenharmony_ci		return;
83462306a36Sopenharmony_ci	}
83562306a36Sopenharmony_ci
83662306a36Sopenharmony_ci	/* Clear the latched event since we're about to read its register */
83762306a36Sopenharmony_ci	pf->latch_event_flags &= ~BIT(index);
83862306a36Sopenharmony_ci
83962306a36Sopenharmony_ci	lo = rd32(hw, I40E_PRTTSYN_RXTIME_L(index));
84062306a36Sopenharmony_ci	hi = rd32(hw, I40E_PRTTSYN_RXTIME_H(index));
84162306a36Sopenharmony_ci
84262306a36Sopenharmony_ci	spin_unlock_bh(&pf->ptp_rx_lock);
84362306a36Sopenharmony_ci
84462306a36Sopenharmony_ci	ns = (((u64)hi) << 32) | lo;
84562306a36Sopenharmony_ci
84662306a36Sopenharmony_ci	i40e_ptp_convert_to_hwtstamp(skb_hwtstamps(skb), ns);
84762306a36Sopenharmony_ci}
84862306a36Sopenharmony_ci
84962306a36Sopenharmony_ci/**
85062306a36Sopenharmony_ci * i40e_ptp_set_increment - Utility function to update clock increment rate
85162306a36Sopenharmony_ci * @pf: Board private structure
85262306a36Sopenharmony_ci *
85362306a36Sopenharmony_ci * During a link change, the DMA frequency that drives the 1588 logic will
85462306a36Sopenharmony_ci * change. In order to keep the PRTTSYN_TIME registers in units of nanoseconds,
85562306a36Sopenharmony_ci * we must update the increment value per clock tick.
85662306a36Sopenharmony_ci **/
85762306a36Sopenharmony_civoid i40e_ptp_set_increment(struct i40e_pf *pf)
85862306a36Sopenharmony_ci{
85962306a36Sopenharmony_ci	struct i40e_link_status *hw_link_info;
86062306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
86162306a36Sopenharmony_ci	u64 incval;
86262306a36Sopenharmony_ci	u32 mult;
86362306a36Sopenharmony_ci
86462306a36Sopenharmony_ci	hw_link_info = &hw->phy.link_info;
86562306a36Sopenharmony_ci
86662306a36Sopenharmony_ci	i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
86762306a36Sopenharmony_ci
86862306a36Sopenharmony_ci	switch (hw_link_info->link_speed) {
86962306a36Sopenharmony_ci	case I40E_LINK_SPEED_10GB:
87062306a36Sopenharmony_ci		mult = I40E_PTP_10GB_INCVAL_MULT;
87162306a36Sopenharmony_ci		break;
87262306a36Sopenharmony_ci	case I40E_LINK_SPEED_5GB:
87362306a36Sopenharmony_ci		mult = I40E_PTP_5GB_INCVAL_MULT;
87462306a36Sopenharmony_ci		break;
87562306a36Sopenharmony_ci	case I40E_LINK_SPEED_1GB:
87662306a36Sopenharmony_ci		mult = I40E_PTP_1GB_INCVAL_MULT;
87762306a36Sopenharmony_ci		break;
87862306a36Sopenharmony_ci	case I40E_LINK_SPEED_100MB:
87962306a36Sopenharmony_ci	{
88062306a36Sopenharmony_ci		static int warn_once;
88162306a36Sopenharmony_ci
88262306a36Sopenharmony_ci		if (!warn_once) {
88362306a36Sopenharmony_ci			dev_warn(&pf->pdev->dev,
88462306a36Sopenharmony_ci				 "1588 functionality is not supported at 100 Mbps. Stopping the PHC.\n");
88562306a36Sopenharmony_ci			warn_once++;
88662306a36Sopenharmony_ci		}
88762306a36Sopenharmony_ci		mult = 0;
88862306a36Sopenharmony_ci		break;
88962306a36Sopenharmony_ci	}
89062306a36Sopenharmony_ci	case I40E_LINK_SPEED_40GB:
89162306a36Sopenharmony_ci	default:
89262306a36Sopenharmony_ci		mult = 1;
89362306a36Sopenharmony_ci		break;
89462306a36Sopenharmony_ci	}
89562306a36Sopenharmony_ci
89662306a36Sopenharmony_ci	/* The increment value is calculated by taking the base 40GbE incvalue
89762306a36Sopenharmony_ci	 * and multiplying it by a factor based on the link speed.
89862306a36Sopenharmony_ci	 */
89962306a36Sopenharmony_ci	incval = I40E_PTP_40GB_INCVAL * mult;
90062306a36Sopenharmony_ci
90162306a36Sopenharmony_ci	/* Write the new increment value into the increment register. The
90262306a36Sopenharmony_ci	 * hardware will not update the clock until both registers have been
90362306a36Sopenharmony_ci	 * written.
90462306a36Sopenharmony_ci	 */
90562306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_INC_L, incval & 0xFFFFFFFF);
90662306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_INC_H, incval >> 32);
90762306a36Sopenharmony_ci
90862306a36Sopenharmony_ci	/* Update the base adjustement value. */
90962306a36Sopenharmony_ci	WRITE_ONCE(pf->ptp_adj_mult, mult);
91062306a36Sopenharmony_ci	smp_mb(); /* Force the above update. */
91162306a36Sopenharmony_ci}
91262306a36Sopenharmony_ci
91362306a36Sopenharmony_ci/**
91462306a36Sopenharmony_ci * i40e_ptp_get_ts_config - ioctl interface to read the HW timestamping
91562306a36Sopenharmony_ci * @pf: Board private structure
91662306a36Sopenharmony_ci * @ifr: ioctl data
91762306a36Sopenharmony_ci *
91862306a36Sopenharmony_ci * Obtain the current hardware timestamping settigs as requested. To do this,
91962306a36Sopenharmony_ci * keep a shadow copy of the timestamp settings rather than attempting to
92062306a36Sopenharmony_ci * deconstruct it from the registers.
92162306a36Sopenharmony_ci **/
92262306a36Sopenharmony_ciint i40e_ptp_get_ts_config(struct i40e_pf *pf, struct ifreq *ifr)
92362306a36Sopenharmony_ci{
92462306a36Sopenharmony_ci	struct hwtstamp_config *config = &pf->tstamp_config;
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP))
92762306a36Sopenharmony_ci		return -EOPNOTSUPP;
92862306a36Sopenharmony_ci
92962306a36Sopenharmony_ci	return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
93062306a36Sopenharmony_ci		-EFAULT : 0;
93162306a36Sopenharmony_ci}
93262306a36Sopenharmony_ci
93362306a36Sopenharmony_ci/**
93462306a36Sopenharmony_ci * i40e_ptp_free_pins - free memory used by PTP pins
93562306a36Sopenharmony_ci * @pf: Board private structure
93662306a36Sopenharmony_ci *
93762306a36Sopenharmony_ci * Release memory allocated for PTP pins.
93862306a36Sopenharmony_ci **/
93962306a36Sopenharmony_cistatic void i40e_ptp_free_pins(struct i40e_pf *pf)
94062306a36Sopenharmony_ci{
94162306a36Sopenharmony_ci	if (i40e_is_ptp_pin_dev(&pf->hw)) {
94262306a36Sopenharmony_ci		kfree(pf->ptp_pins);
94362306a36Sopenharmony_ci		kfree(pf->ptp_caps.pin_config);
94462306a36Sopenharmony_ci		pf->ptp_pins = NULL;
94562306a36Sopenharmony_ci	}
94662306a36Sopenharmony_ci}
94762306a36Sopenharmony_ci
94862306a36Sopenharmony_ci/**
94962306a36Sopenharmony_ci * i40e_ptp_set_pin_hw - Set HW GPIO pin
95062306a36Sopenharmony_ci * @hw: pointer to the hardware structure
95162306a36Sopenharmony_ci * @pin: pin index
95262306a36Sopenharmony_ci * @state: pin state
95362306a36Sopenharmony_ci *
95462306a36Sopenharmony_ci * Set status of GPIO pin for external clock handling.
95562306a36Sopenharmony_ci **/
95662306a36Sopenharmony_cistatic void i40e_ptp_set_pin_hw(struct i40e_hw *hw,
95762306a36Sopenharmony_ci				unsigned int pin,
95862306a36Sopenharmony_ci				enum i40e_ptp_gpio_pin_state state)
95962306a36Sopenharmony_ci{
96062306a36Sopenharmony_ci	switch (state) {
96162306a36Sopenharmony_ci	case off:
96262306a36Sopenharmony_ci		wr32(hw, I40E_GLGEN_GPIO_CTL(pin), 0);
96362306a36Sopenharmony_ci		break;
96462306a36Sopenharmony_ci	case in_A:
96562306a36Sopenharmony_ci		wr32(hw, I40E_GLGEN_GPIO_CTL(pin),
96662306a36Sopenharmony_ci		     I40E_GLGEN_GPIO_CTL_PORT_0_IN_TIMESYNC_0);
96762306a36Sopenharmony_ci		break;
96862306a36Sopenharmony_ci	case in_B:
96962306a36Sopenharmony_ci		wr32(hw, I40E_GLGEN_GPIO_CTL(pin),
97062306a36Sopenharmony_ci		     I40E_GLGEN_GPIO_CTL_PORT_1_IN_TIMESYNC_0);
97162306a36Sopenharmony_ci		break;
97262306a36Sopenharmony_ci	case out_A:
97362306a36Sopenharmony_ci		wr32(hw, I40E_GLGEN_GPIO_CTL(pin),
97462306a36Sopenharmony_ci		     I40E_GLGEN_GPIO_CTL_PORT_0_OUT_TIMESYNC_1);
97562306a36Sopenharmony_ci		break;
97662306a36Sopenharmony_ci	case out_B:
97762306a36Sopenharmony_ci		wr32(hw, I40E_GLGEN_GPIO_CTL(pin),
97862306a36Sopenharmony_ci		     I40E_GLGEN_GPIO_CTL_PORT_1_OUT_TIMESYNC_1);
97962306a36Sopenharmony_ci		break;
98062306a36Sopenharmony_ci	default:
98162306a36Sopenharmony_ci		break;
98262306a36Sopenharmony_ci	}
98362306a36Sopenharmony_ci}
98462306a36Sopenharmony_ci
98562306a36Sopenharmony_ci/**
98662306a36Sopenharmony_ci * i40e_ptp_set_led_hw - Set HW GPIO led
98762306a36Sopenharmony_ci * @hw: pointer to the hardware structure
98862306a36Sopenharmony_ci * @led: led index
98962306a36Sopenharmony_ci * @state: led state
99062306a36Sopenharmony_ci *
99162306a36Sopenharmony_ci * Set status of GPIO led for external clock handling.
99262306a36Sopenharmony_ci **/
99362306a36Sopenharmony_cistatic void i40e_ptp_set_led_hw(struct i40e_hw *hw,
99462306a36Sopenharmony_ci				unsigned int led,
99562306a36Sopenharmony_ci				enum i40e_ptp_led_pin_state state)
99662306a36Sopenharmony_ci{
99762306a36Sopenharmony_ci	switch (state) {
99862306a36Sopenharmony_ci	case low:
99962306a36Sopenharmony_ci		wr32(hw, I40E_GLGEN_GPIO_SET,
100062306a36Sopenharmony_ci		     I40E_GLGEN_GPIO_SET_DRV_SDP_DATA | led);
100162306a36Sopenharmony_ci		break;
100262306a36Sopenharmony_ci	case high:
100362306a36Sopenharmony_ci		wr32(hw, I40E_GLGEN_GPIO_SET,
100462306a36Sopenharmony_ci		     I40E_GLGEN_GPIO_SET_DRV_SDP_DATA |
100562306a36Sopenharmony_ci		     I40E_GLGEN_GPIO_SET_SDP_DATA_HI | led);
100662306a36Sopenharmony_ci		break;
100762306a36Sopenharmony_ci	default:
100862306a36Sopenharmony_ci		break;
100962306a36Sopenharmony_ci	}
101062306a36Sopenharmony_ci}
101162306a36Sopenharmony_ci
101262306a36Sopenharmony_ci/**
101362306a36Sopenharmony_ci * i40e_ptp_init_leds_hw - init LEDs
101462306a36Sopenharmony_ci * @hw: pointer to a hardware structure
101562306a36Sopenharmony_ci *
101662306a36Sopenharmony_ci * Set initial state of LEDs
101762306a36Sopenharmony_ci **/
101862306a36Sopenharmony_cistatic void i40e_ptp_init_leds_hw(struct i40e_hw *hw)
101962306a36Sopenharmony_ci{
102062306a36Sopenharmony_ci	wr32(hw, I40E_GLGEN_GPIO_CTL(I40E_LED2_0),
102162306a36Sopenharmony_ci	     I40E_GLGEN_GPIO_CTL_LED_INIT);
102262306a36Sopenharmony_ci	wr32(hw, I40E_GLGEN_GPIO_CTL(I40E_LED2_1),
102362306a36Sopenharmony_ci	     I40E_GLGEN_GPIO_CTL_LED_INIT);
102462306a36Sopenharmony_ci	wr32(hw, I40E_GLGEN_GPIO_CTL(I40E_LED3_0),
102562306a36Sopenharmony_ci	     I40E_GLGEN_GPIO_CTL_LED_INIT);
102662306a36Sopenharmony_ci	wr32(hw, I40E_GLGEN_GPIO_CTL(I40E_LED3_1),
102762306a36Sopenharmony_ci	     I40E_GLGEN_GPIO_CTL_LED_INIT);
102862306a36Sopenharmony_ci}
102962306a36Sopenharmony_ci
103062306a36Sopenharmony_ci/**
103162306a36Sopenharmony_ci * i40e_ptp_set_pins_hw - Set HW GPIO pins
103262306a36Sopenharmony_ci * @pf: Board private structure
103362306a36Sopenharmony_ci *
103462306a36Sopenharmony_ci * This function sets GPIO pins for PTP
103562306a36Sopenharmony_ci **/
103662306a36Sopenharmony_cistatic void i40e_ptp_set_pins_hw(struct i40e_pf *pf)
103762306a36Sopenharmony_ci{
103862306a36Sopenharmony_ci	const struct i40e_ptp_pins_settings *pins = pf->ptp_pins;
103962306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci	/* pin must be disabled before it may be used */
104262306a36Sopenharmony_ci	i40e_ptp_set_pin_hw(hw, I40E_SDP3_2, off);
104362306a36Sopenharmony_ci	i40e_ptp_set_pin_hw(hw, I40E_SDP3_3, off);
104462306a36Sopenharmony_ci	i40e_ptp_set_pin_hw(hw, I40E_GPIO_4, off);
104562306a36Sopenharmony_ci
104662306a36Sopenharmony_ci	i40e_ptp_set_pin_hw(hw, I40E_SDP3_2, pins->sdp3_2);
104762306a36Sopenharmony_ci	i40e_ptp_set_pin_hw(hw, I40E_SDP3_3, pins->sdp3_3);
104862306a36Sopenharmony_ci	i40e_ptp_set_pin_hw(hw, I40E_GPIO_4, pins->gpio_4);
104962306a36Sopenharmony_ci
105062306a36Sopenharmony_ci	i40e_ptp_set_led_hw(hw, I40E_LED2_0, pins->led2_0);
105162306a36Sopenharmony_ci	i40e_ptp_set_led_hw(hw, I40E_LED2_1, pins->led2_1);
105262306a36Sopenharmony_ci	i40e_ptp_set_led_hw(hw, I40E_LED3_0, pins->led3_0);
105362306a36Sopenharmony_ci	i40e_ptp_set_led_hw(hw, I40E_LED3_1, pins->led3_1);
105462306a36Sopenharmony_ci
105562306a36Sopenharmony_ci	dev_info(&pf->pdev->dev,
105662306a36Sopenharmony_ci		 "PTP configuration set to: SDP3_2: %s,  SDP3_3: %s,  GPIO_4: %s.\n",
105762306a36Sopenharmony_ci		 i40e_ptp_gpio_pin_state2str[pins->sdp3_2],
105862306a36Sopenharmony_ci		 i40e_ptp_gpio_pin_state2str[pins->sdp3_3],
105962306a36Sopenharmony_ci		 i40e_ptp_gpio_pin_state2str[pins->gpio_4]);
106062306a36Sopenharmony_ci}
106162306a36Sopenharmony_ci
106262306a36Sopenharmony_ci/**
106362306a36Sopenharmony_ci * i40e_ptp_set_pins - set PTP pins in HW
106462306a36Sopenharmony_ci * @pf: Board private structure
106562306a36Sopenharmony_ci * @pins: PTP pins to be applied
106662306a36Sopenharmony_ci *
106762306a36Sopenharmony_ci * Validate and set PTP pins in HW for specific PF.
106862306a36Sopenharmony_ci * Return 0 on success or negative value on error.
106962306a36Sopenharmony_ci **/
107062306a36Sopenharmony_cistatic int i40e_ptp_set_pins(struct i40e_pf *pf,
107162306a36Sopenharmony_ci			     struct i40e_ptp_pins_settings *pins)
107262306a36Sopenharmony_ci{
107362306a36Sopenharmony_ci	enum i40e_can_set_pins_t pin_caps = i40e_can_set_pins(pf);
107462306a36Sopenharmony_ci	int i = 0;
107562306a36Sopenharmony_ci
107662306a36Sopenharmony_ci	if (pin_caps == CANT_DO_PINS)
107762306a36Sopenharmony_ci		return -EOPNOTSUPP;
107862306a36Sopenharmony_ci	else if (pin_caps == CAN_DO_PINS)
107962306a36Sopenharmony_ci		return 0;
108062306a36Sopenharmony_ci
108162306a36Sopenharmony_ci	if (pins->sdp3_2 == invalid)
108262306a36Sopenharmony_ci		pins->sdp3_2 = pf->ptp_pins->sdp3_2;
108362306a36Sopenharmony_ci	if (pins->sdp3_3 == invalid)
108462306a36Sopenharmony_ci		pins->sdp3_3 = pf->ptp_pins->sdp3_3;
108562306a36Sopenharmony_ci	if (pins->gpio_4 == invalid)
108662306a36Sopenharmony_ci		pins->gpio_4 = pf->ptp_pins->gpio_4;
108762306a36Sopenharmony_ci	while (i40e_ptp_pin_led_allowed_states[i].sdp3_2 != end) {
108862306a36Sopenharmony_ci		if (pins->sdp3_2 == i40e_ptp_pin_led_allowed_states[i].sdp3_2 &&
108962306a36Sopenharmony_ci		    pins->sdp3_3 == i40e_ptp_pin_led_allowed_states[i].sdp3_3 &&
109062306a36Sopenharmony_ci		    pins->gpio_4 == i40e_ptp_pin_led_allowed_states[i].gpio_4) {
109162306a36Sopenharmony_ci			pins->led2_0 =
109262306a36Sopenharmony_ci				i40e_ptp_pin_led_allowed_states[i].led2_0;
109362306a36Sopenharmony_ci			pins->led2_1 =
109462306a36Sopenharmony_ci				i40e_ptp_pin_led_allowed_states[i].led2_1;
109562306a36Sopenharmony_ci			pins->led3_0 =
109662306a36Sopenharmony_ci				i40e_ptp_pin_led_allowed_states[i].led3_0;
109762306a36Sopenharmony_ci			pins->led3_1 =
109862306a36Sopenharmony_ci				i40e_ptp_pin_led_allowed_states[i].led3_1;
109962306a36Sopenharmony_ci			break;
110062306a36Sopenharmony_ci		}
110162306a36Sopenharmony_ci		i++;
110262306a36Sopenharmony_ci	}
110362306a36Sopenharmony_ci	if (i40e_ptp_pin_led_allowed_states[i].sdp3_2 == end) {
110462306a36Sopenharmony_ci		dev_warn(&pf->pdev->dev,
110562306a36Sopenharmony_ci			 "Unsupported PTP pin configuration: SDP3_2: %s,  SDP3_3: %s,  GPIO_4: %s.\n",
110662306a36Sopenharmony_ci			 i40e_ptp_gpio_pin_state2str[pins->sdp3_2],
110762306a36Sopenharmony_ci			 i40e_ptp_gpio_pin_state2str[pins->sdp3_3],
110862306a36Sopenharmony_ci			 i40e_ptp_gpio_pin_state2str[pins->gpio_4]);
110962306a36Sopenharmony_ci
111062306a36Sopenharmony_ci		return -EPERM;
111162306a36Sopenharmony_ci	}
111262306a36Sopenharmony_ci	memcpy(pf->ptp_pins, pins, sizeof(*pins));
111362306a36Sopenharmony_ci	i40e_ptp_set_pins_hw(pf);
111462306a36Sopenharmony_ci	i40_ptp_reset_timing_events(pf);
111562306a36Sopenharmony_ci
111662306a36Sopenharmony_ci	return 0;
111762306a36Sopenharmony_ci}
111862306a36Sopenharmony_ci
111962306a36Sopenharmony_ci/**
112062306a36Sopenharmony_ci * i40e_ptp_alloc_pins - allocate PTP pins structure
112162306a36Sopenharmony_ci * @pf: Board private structure
112262306a36Sopenharmony_ci *
112362306a36Sopenharmony_ci * allocate PTP pins structure
112462306a36Sopenharmony_ci **/
112562306a36Sopenharmony_ciint i40e_ptp_alloc_pins(struct i40e_pf *pf)
112662306a36Sopenharmony_ci{
112762306a36Sopenharmony_ci	if (!i40e_is_ptp_pin_dev(&pf->hw))
112862306a36Sopenharmony_ci		return 0;
112962306a36Sopenharmony_ci
113062306a36Sopenharmony_ci	pf->ptp_pins =
113162306a36Sopenharmony_ci		kzalloc(sizeof(struct i40e_ptp_pins_settings), GFP_KERNEL);
113262306a36Sopenharmony_ci
113362306a36Sopenharmony_ci	if (!pf->ptp_pins) {
113462306a36Sopenharmony_ci		dev_warn(&pf->pdev->dev, "Cannot allocate memory for PTP pins structure.\n");
113562306a36Sopenharmony_ci		return -ENOMEM;
113662306a36Sopenharmony_ci	}
113762306a36Sopenharmony_ci
113862306a36Sopenharmony_ci	pf->ptp_pins->sdp3_2 = off;
113962306a36Sopenharmony_ci	pf->ptp_pins->sdp3_3 = off;
114062306a36Sopenharmony_ci	pf->ptp_pins->gpio_4 = off;
114162306a36Sopenharmony_ci	pf->ptp_pins->led2_0 = high;
114262306a36Sopenharmony_ci	pf->ptp_pins->led2_1 = high;
114362306a36Sopenharmony_ci	pf->ptp_pins->led3_0 = high;
114462306a36Sopenharmony_ci	pf->ptp_pins->led3_1 = high;
114562306a36Sopenharmony_ci
114662306a36Sopenharmony_ci	/* Use PF0 to set pins in HW. Return success for user space tools */
114762306a36Sopenharmony_ci	if (pf->hw.pf_id)
114862306a36Sopenharmony_ci		return 0;
114962306a36Sopenharmony_ci
115062306a36Sopenharmony_ci	i40e_ptp_init_leds_hw(&pf->hw);
115162306a36Sopenharmony_ci	i40e_ptp_set_pins_hw(pf);
115262306a36Sopenharmony_ci
115362306a36Sopenharmony_ci	return 0;
115462306a36Sopenharmony_ci}
115562306a36Sopenharmony_ci
115662306a36Sopenharmony_ci/**
115762306a36Sopenharmony_ci * i40e_ptp_set_timestamp_mode - setup hardware for requested timestamp mode
115862306a36Sopenharmony_ci * @pf: Board private structure
115962306a36Sopenharmony_ci * @config: hwtstamp settings requested or saved
116062306a36Sopenharmony_ci *
116162306a36Sopenharmony_ci * Control hardware registers to enter the specific mode requested by the
116262306a36Sopenharmony_ci * user. Also used during reset path to ensure that timestamp settings are
116362306a36Sopenharmony_ci * maintained.
116462306a36Sopenharmony_ci *
116562306a36Sopenharmony_ci * Note: modifies config in place, and may update the requested mode to be
116662306a36Sopenharmony_ci * more broad if the specific filter is not directly supported.
116762306a36Sopenharmony_ci **/
116862306a36Sopenharmony_cistatic int i40e_ptp_set_timestamp_mode(struct i40e_pf *pf,
116962306a36Sopenharmony_ci				       struct hwtstamp_config *config)
117062306a36Sopenharmony_ci{
117162306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
117262306a36Sopenharmony_ci	u32 tsyntype, regval;
117362306a36Sopenharmony_ci
117462306a36Sopenharmony_ci	/* Selects external trigger to cause event */
117562306a36Sopenharmony_ci	regval = rd32(hw, I40E_PRTTSYN_AUX_0(0));
117662306a36Sopenharmony_ci	/* Bit 17:16 is EVNTLVL, 01B rising edge */
117762306a36Sopenharmony_ci	regval &= 0;
117862306a36Sopenharmony_ci	regval |= (1 << I40E_PRTTSYN_AUX_0_EVNTLVL_SHIFT);
117962306a36Sopenharmony_ci	/* regval: 0001 0000 0000 0000 0000 */
118062306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_AUX_0(0), regval);
118162306a36Sopenharmony_ci
118262306a36Sopenharmony_ci	/* Enabel interrupts */
118362306a36Sopenharmony_ci	regval = rd32(hw, I40E_PRTTSYN_CTL0);
118462306a36Sopenharmony_ci	regval |= 1 << I40E_PRTTSYN_CTL0_EVENT_INT_ENA_SHIFT;
118562306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_CTL0, regval);
118662306a36Sopenharmony_ci
118762306a36Sopenharmony_ci	INIT_WORK(&pf->ptp_extts0_work, i40e_ptp_extts0_work);
118862306a36Sopenharmony_ci
118962306a36Sopenharmony_ci	switch (config->tx_type) {
119062306a36Sopenharmony_ci	case HWTSTAMP_TX_OFF:
119162306a36Sopenharmony_ci		pf->ptp_tx = false;
119262306a36Sopenharmony_ci		break;
119362306a36Sopenharmony_ci	case HWTSTAMP_TX_ON:
119462306a36Sopenharmony_ci		pf->ptp_tx = true;
119562306a36Sopenharmony_ci		break;
119662306a36Sopenharmony_ci	default:
119762306a36Sopenharmony_ci		return -ERANGE;
119862306a36Sopenharmony_ci	}
119962306a36Sopenharmony_ci
120062306a36Sopenharmony_ci	switch (config->rx_filter) {
120162306a36Sopenharmony_ci	case HWTSTAMP_FILTER_NONE:
120262306a36Sopenharmony_ci		pf->ptp_rx = false;
120362306a36Sopenharmony_ci		/* We set the type to V1, but do not enable UDP packet
120462306a36Sopenharmony_ci		 * recognition. In this way, we should be as close to
120562306a36Sopenharmony_ci		 * disabling PTP Rx timestamps as possible since V1 packets
120662306a36Sopenharmony_ci		 * are always UDP, since L2 packets are a V2 feature.
120762306a36Sopenharmony_ci		 */
120862306a36Sopenharmony_ci		tsyntype = I40E_PRTTSYN_CTL1_TSYNTYPE_V1;
120962306a36Sopenharmony_ci		break;
121062306a36Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
121162306a36Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
121262306a36Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
121362306a36Sopenharmony_ci		if (!(pf->hw_features & I40E_HW_PTP_L4_CAPABLE))
121462306a36Sopenharmony_ci			return -ERANGE;
121562306a36Sopenharmony_ci		pf->ptp_rx = true;
121662306a36Sopenharmony_ci		tsyntype = I40E_PRTTSYN_CTL1_V1MESSTYPE0_MASK |
121762306a36Sopenharmony_ci			   I40E_PRTTSYN_CTL1_TSYNTYPE_V1 |
121862306a36Sopenharmony_ci			   I40E_PRTTSYN_CTL1_UDP_ENA_MASK;
121962306a36Sopenharmony_ci		config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
122062306a36Sopenharmony_ci		break;
122162306a36Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_EVENT:
122262306a36Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
122362306a36Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_SYNC:
122462306a36Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
122562306a36Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
122662306a36Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
122762306a36Sopenharmony_ci		if (!(pf->hw_features & I40E_HW_PTP_L4_CAPABLE))
122862306a36Sopenharmony_ci			return -ERANGE;
122962306a36Sopenharmony_ci		fallthrough;
123062306a36Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
123162306a36Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
123262306a36Sopenharmony_ci	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
123362306a36Sopenharmony_ci		pf->ptp_rx = true;
123462306a36Sopenharmony_ci		tsyntype = I40E_PRTTSYN_CTL1_V2MESSTYPE0_MASK |
123562306a36Sopenharmony_ci			   I40E_PRTTSYN_CTL1_TSYNTYPE_V2;
123662306a36Sopenharmony_ci		if (pf->hw_features & I40E_HW_PTP_L4_CAPABLE) {
123762306a36Sopenharmony_ci			tsyntype |= I40E_PRTTSYN_CTL1_UDP_ENA_MASK;
123862306a36Sopenharmony_ci			config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
123962306a36Sopenharmony_ci		} else {
124062306a36Sopenharmony_ci			config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
124162306a36Sopenharmony_ci		}
124262306a36Sopenharmony_ci		break;
124362306a36Sopenharmony_ci	case HWTSTAMP_FILTER_NTP_ALL:
124462306a36Sopenharmony_ci	case HWTSTAMP_FILTER_ALL:
124562306a36Sopenharmony_ci	default:
124662306a36Sopenharmony_ci		return -ERANGE;
124762306a36Sopenharmony_ci	}
124862306a36Sopenharmony_ci
124962306a36Sopenharmony_ci	/* Clear out all 1588-related registers to clear and unlatch them. */
125062306a36Sopenharmony_ci	spin_lock_bh(&pf->ptp_rx_lock);
125162306a36Sopenharmony_ci	rd32(hw, I40E_PRTTSYN_STAT_0);
125262306a36Sopenharmony_ci	rd32(hw, I40E_PRTTSYN_TXTIME_H);
125362306a36Sopenharmony_ci	rd32(hw, I40E_PRTTSYN_RXTIME_H(0));
125462306a36Sopenharmony_ci	rd32(hw, I40E_PRTTSYN_RXTIME_H(1));
125562306a36Sopenharmony_ci	rd32(hw, I40E_PRTTSYN_RXTIME_H(2));
125662306a36Sopenharmony_ci	rd32(hw, I40E_PRTTSYN_RXTIME_H(3));
125762306a36Sopenharmony_ci	pf->latch_event_flags = 0;
125862306a36Sopenharmony_ci	spin_unlock_bh(&pf->ptp_rx_lock);
125962306a36Sopenharmony_ci
126062306a36Sopenharmony_ci	/* Enable/disable the Tx timestamp interrupt based on user input. */
126162306a36Sopenharmony_ci	regval = rd32(hw, I40E_PRTTSYN_CTL0);
126262306a36Sopenharmony_ci	if (pf->ptp_tx)
126362306a36Sopenharmony_ci		regval |= I40E_PRTTSYN_CTL0_TXTIME_INT_ENA_MASK;
126462306a36Sopenharmony_ci	else
126562306a36Sopenharmony_ci		regval &= ~I40E_PRTTSYN_CTL0_TXTIME_INT_ENA_MASK;
126662306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_CTL0, regval);
126762306a36Sopenharmony_ci
126862306a36Sopenharmony_ci	regval = rd32(hw, I40E_PFINT_ICR0_ENA);
126962306a36Sopenharmony_ci	if (pf->ptp_tx)
127062306a36Sopenharmony_ci		regval |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
127162306a36Sopenharmony_ci	else
127262306a36Sopenharmony_ci		regval &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
127362306a36Sopenharmony_ci	wr32(hw, I40E_PFINT_ICR0_ENA, regval);
127462306a36Sopenharmony_ci
127562306a36Sopenharmony_ci	/* Although there is no simple on/off switch for Rx, we "disable" Rx
127662306a36Sopenharmony_ci	 * timestamps by setting to V1 only mode and clear the UDP
127762306a36Sopenharmony_ci	 * recognition. This ought to disable all PTP Rx timestamps as V1
127862306a36Sopenharmony_ci	 * packets are always over UDP. Note that software is configured to
127962306a36Sopenharmony_ci	 * ignore Rx timestamps via the pf->ptp_rx flag.
128062306a36Sopenharmony_ci	 */
128162306a36Sopenharmony_ci	regval = rd32(hw, I40E_PRTTSYN_CTL1);
128262306a36Sopenharmony_ci	/* clear everything but the enable bit */
128362306a36Sopenharmony_ci	regval &= I40E_PRTTSYN_CTL1_TSYNENA_MASK;
128462306a36Sopenharmony_ci	/* now enable bits for desired Rx timestamps */
128562306a36Sopenharmony_ci	regval |= tsyntype;
128662306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_CTL1, regval);
128762306a36Sopenharmony_ci
128862306a36Sopenharmony_ci	return 0;
128962306a36Sopenharmony_ci}
129062306a36Sopenharmony_ci
129162306a36Sopenharmony_ci/**
129262306a36Sopenharmony_ci * i40e_ptp_set_ts_config - ioctl interface to control the HW timestamping
129362306a36Sopenharmony_ci * @pf: Board private structure
129462306a36Sopenharmony_ci * @ifr: ioctl data
129562306a36Sopenharmony_ci *
129662306a36Sopenharmony_ci * Respond to the user filter requests and make the appropriate hardware
129762306a36Sopenharmony_ci * changes here. The XL710 cannot support splitting of the Tx/Rx timestamping
129862306a36Sopenharmony_ci * logic, so keep track in software of whether to indicate these timestamps
129962306a36Sopenharmony_ci * or not.
130062306a36Sopenharmony_ci *
130162306a36Sopenharmony_ci * It is permissible to "upgrade" the user request to a broader filter, as long
130262306a36Sopenharmony_ci * as the user receives the timestamps they care about and the user is notified
130362306a36Sopenharmony_ci * the filter has been broadened.
130462306a36Sopenharmony_ci **/
130562306a36Sopenharmony_ciint i40e_ptp_set_ts_config(struct i40e_pf *pf, struct ifreq *ifr)
130662306a36Sopenharmony_ci{
130762306a36Sopenharmony_ci	struct hwtstamp_config config;
130862306a36Sopenharmony_ci	int err;
130962306a36Sopenharmony_ci
131062306a36Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP))
131162306a36Sopenharmony_ci		return -EOPNOTSUPP;
131262306a36Sopenharmony_ci
131362306a36Sopenharmony_ci	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
131462306a36Sopenharmony_ci		return -EFAULT;
131562306a36Sopenharmony_ci
131662306a36Sopenharmony_ci	err = i40e_ptp_set_timestamp_mode(pf, &config);
131762306a36Sopenharmony_ci	if (err)
131862306a36Sopenharmony_ci		return err;
131962306a36Sopenharmony_ci
132062306a36Sopenharmony_ci	/* save these settings for future reference */
132162306a36Sopenharmony_ci	pf->tstamp_config = config;
132262306a36Sopenharmony_ci
132362306a36Sopenharmony_ci	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
132462306a36Sopenharmony_ci		-EFAULT : 0;
132562306a36Sopenharmony_ci}
132662306a36Sopenharmony_ci
132762306a36Sopenharmony_ci/**
132862306a36Sopenharmony_ci * i40e_init_pin_config - initialize pins.
132962306a36Sopenharmony_ci * @pf: private board structure
133062306a36Sopenharmony_ci *
133162306a36Sopenharmony_ci * Initialize pins for external clock source.
133262306a36Sopenharmony_ci * Return 0 on success or error code on failure.
133362306a36Sopenharmony_ci **/
133462306a36Sopenharmony_cistatic int i40e_init_pin_config(struct i40e_pf *pf)
133562306a36Sopenharmony_ci{
133662306a36Sopenharmony_ci	int i;
133762306a36Sopenharmony_ci
133862306a36Sopenharmony_ci	pf->ptp_caps.n_pins = 3;
133962306a36Sopenharmony_ci	pf->ptp_caps.n_ext_ts = 2;
134062306a36Sopenharmony_ci	pf->ptp_caps.pps = 1;
134162306a36Sopenharmony_ci	pf->ptp_caps.n_per_out = 2;
134262306a36Sopenharmony_ci
134362306a36Sopenharmony_ci	pf->ptp_caps.pin_config = kcalloc(pf->ptp_caps.n_pins,
134462306a36Sopenharmony_ci					  sizeof(*pf->ptp_caps.pin_config),
134562306a36Sopenharmony_ci					  GFP_KERNEL);
134662306a36Sopenharmony_ci	if (!pf->ptp_caps.pin_config)
134762306a36Sopenharmony_ci		return -ENOMEM;
134862306a36Sopenharmony_ci
134962306a36Sopenharmony_ci	for (i = 0; i < pf->ptp_caps.n_pins; i++) {
135062306a36Sopenharmony_ci		snprintf(pf->ptp_caps.pin_config[i].name,
135162306a36Sopenharmony_ci			 sizeof(pf->ptp_caps.pin_config[i].name),
135262306a36Sopenharmony_ci			 "%s", sdp_desc[i].name);
135362306a36Sopenharmony_ci		pf->ptp_caps.pin_config[i].index = sdp_desc[i].index;
135462306a36Sopenharmony_ci		pf->ptp_caps.pin_config[i].func = PTP_PF_NONE;
135562306a36Sopenharmony_ci		pf->ptp_caps.pin_config[i].chan = sdp_desc[i].chan;
135662306a36Sopenharmony_ci	}
135762306a36Sopenharmony_ci
135862306a36Sopenharmony_ci	pf->ptp_caps.verify = i40e_ptp_verify;
135962306a36Sopenharmony_ci	pf->ptp_caps.enable = i40e_ptp_feature_enable;
136062306a36Sopenharmony_ci
136162306a36Sopenharmony_ci	pf->ptp_caps.pps = 1;
136262306a36Sopenharmony_ci
136362306a36Sopenharmony_ci	return 0;
136462306a36Sopenharmony_ci}
136562306a36Sopenharmony_ci
136662306a36Sopenharmony_ci/**
136762306a36Sopenharmony_ci * i40e_ptp_create_clock - Create PTP clock device for userspace
136862306a36Sopenharmony_ci * @pf: Board private structure
136962306a36Sopenharmony_ci *
137062306a36Sopenharmony_ci * This function creates a new PTP clock device. It only creates one if we
137162306a36Sopenharmony_ci * don't already have one, so it is safe to call. Will return error if it
137262306a36Sopenharmony_ci * can't create one, but success if we already have a device. Should be used
137362306a36Sopenharmony_ci * by i40e_ptp_init to create clock initially, and prevent global resets from
137462306a36Sopenharmony_ci * creating new clock devices.
137562306a36Sopenharmony_ci **/
137662306a36Sopenharmony_cistatic long i40e_ptp_create_clock(struct i40e_pf *pf)
137762306a36Sopenharmony_ci{
137862306a36Sopenharmony_ci	/* no need to create a clock device if we already have one */
137962306a36Sopenharmony_ci	if (!IS_ERR_OR_NULL(pf->ptp_clock))
138062306a36Sopenharmony_ci		return 0;
138162306a36Sopenharmony_ci
138262306a36Sopenharmony_ci	strscpy(pf->ptp_caps.name, i40e_driver_name,
138362306a36Sopenharmony_ci		sizeof(pf->ptp_caps.name) - 1);
138462306a36Sopenharmony_ci	pf->ptp_caps.owner = THIS_MODULE;
138562306a36Sopenharmony_ci	pf->ptp_caps.max_adj = 999999999;
138662306a36Sopenharmony_ci	pf->ptp_caps.adjfine = i40e_ptp_adjfine;
138762306a36Sopenharmony_ci	pf->ptp_caps.adjtime = i40e_ptp_adjtime;
138862306a36Sopenharmony_ci	pf->ptp_caps.gettimex64 = i40e_ptp_gettimex;
138962306a36Sopenharmony_ci	pf->ptp_caps.settime64 = i40e_ptp_settime;
139062306a36Sopenharmony_ci	if (i40e_is_ptp_pin_dev(&pf->hw)) {
139162306a36Sopenharmony_ci		int err = i40e_init_pin_config(pf);
139262306a36Sopenharmony_ci
139362306a36Sopenharmony_ci		if (err)
139462306a36Sopenharmony_ci			return err;
139562306a36Sopenharmony_ci	}
139662306a36Sopenharmony_ci
139762306a36Sopenharmony_ci	/* Attempt to register the clock before enabling the hardware. */
139862306a36Sopenharmony_ci	pf->ptp_clock = ptp_clock_register(&pf->ptp_caps, &pf->pdev->dev);
139962306a36Sopenharmony_ci	if (IS_ERR(pf->ptp_clock))
140062306a36Sopenharmony_ci		return PTR_ERR(pf->ptp_clock);
140162306a36Sopenharmony_ci
140262306a36Sopenharmony_ci	/* clear the hwtstamp settings here during clock create, instead of
140362306a36Sopenharmony_ci	 * during regular init, so that we can maintain settings across a
140462306a36Sopenharmony_ci	 * reset or suspend.
140562306a36Sopenharmony_ci	 */
140662306a36Sopenharmony_ci	pf->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
140762306a36Sopenharmony_ci	pf->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
140862306a36Sopenharmony_ci
140962306a36Sopenharmony_ci	/* Set the previous "reset" time to the current Kernel clock time */
141062306a36Sopenharmony_ci	ktime_get_real_ts64(&pf->ptp_prev_hw_time);
141162306a36Sopenharmony_ci	pf->ptp_reset_start = ktime_get();
141262306a36Sopenharmony_ci
141362306a36Sopenharmony_ci	return 0;
141462306a36Sopenharmony_ci}
141562306a36Sopenharmony_ci
141662306a36Sopenharmony_ci/**
141762306a36Sopenharmony_ci * i40e_ptp_save_hw_time - Save the current PTP time as ptp_prev_hw_time
141862306a36Sopenharmony_ci * @pf: Board private structure
141962306a36Sopenharmony_ci *
142062306a36Sopenharmony_ci * Read the current PTP time and save it into pf->ptp_prev_hw_time. This should
142162306a36Sopenharmony_ci * be called at the end of preparing to reset, just before hardware reset
142262306a36Sopenharmony_ci * occurs, in order to preserve the PTP time as close as possible across
142362306a36Sopenharmony_ci * resets.
142462306a36Sopenharmony_ci */
142562306a36Sopenharmony_civoid i40e_ptp_save_hw_time(struct i40e_pf *pf)
142662306a36Sopenharmony_ci{
142762306a36Sopenharmony_ci	/* don't try to access the PTP clock if it's not enabled */
142862306a36Sopenharmony_ci	if (!(pf->flags & I40E_FLAG_PTP))
142962306a36Sopenharmony_ci		return;
143062306a36Sopenharmony_ci
143162306a36Sopenharmony_ci	i40e_ptp_gettimex(&pf->ptp_caps, &pf->ptp_prev_hw_time, NULL);
143262306a36Sopenharmony_ci	/* Get a monotonic starting time for this reset */
143362306a36Sopenharmony_ci	pf->ptp_reset_start = ktime_get();
143462306a36Sopenharmony_ci}
143562306a36Sopenharmony_ci
143662306a36Sopenharmony_ci/**
143762306a36Sopenharmony_ci * i40e_ptp_restore_hw_time - Restore the ptp_prev_hw_time + delta to PTP regs
143862306a36Sopenharmony_ci * @pf: Board private structure
143962306a36Sopenharmony_ci *
144062306a36Sopenharmony_ci * Restore the PTP hardware clock registers. We previously cached the PTP
144162306a36Sopenharmony_ci * hardware time as pf->ptp_prev_hw_time. To be as accurate as possible,
144262306a36Sopenharmony_ci * update this value based on the time delta since the time was saved, using
144362306a36Sopenharmony_ci * CLOCK_MONOTONIC (via ktime_get()) to calculate the time difference.
144462306a36Sopenharmony_ci *
144562306a36Sopenharmony_ci * This ensures that the hardware clock is restored to nearly what it should
144662306a36Sopenharmony_ci * have been if a reset had not occurred.
144762306a36Sopenharmony_ci */
144862306a36Sopenharmony_civoid i40e_ptp_restore_hw_time(struct i40e_pf *pf)
144962306a36Sopenharmony_ci{
145062306a36Sopenharmony_ci	ktime_t delta = ktime_sub(ktime_get(), pf->ptp_reset_start);
145162306a36Sopenharmony_ci
145262306a36Sopenharmony_ci	/* Update the previous HW time with the ktime delta */
145362306a36Sopenharmony_ci	timespec64_add_ns(&pf->ptp_prev_hw_time, ktime_to_ns(delta));
145462306a36Sopenharmony_ci
145562306a36Sopenharmony_ci	/* Restore the hardware clock registers */
145662306a36Sopenharmony_ci	i40e_ptp_settime(&pf->ptp_caps, &pf->ptp_prev_hw_time);
145762306a36Sopenharmony_ci}
145862306a36Sopenharmony_ci
145962306a36Sopenharmony_ci/**
146062306a36Sopenharmony_ci * i40e_ptp_init - Initialize the 1588 support after device probe or reset
146162306a36Sopenharmony_ci * @pf: Board private structure
146262306a36Sopenharmony_ci *
146362306a36Sopenharmony_ci * This function sets device up for 1588 support. The first time it is run, it
146462306a36Sopenharmony_ci * will create a PHC clock device. It does not create a clock device if one
146562306a36Sopenharmony_ci * already exists. It also reconfigures the device after a reset.
146662306a36Sopenharmony_ci *
146762306a36Sopenharmony_ci * The first time a clock is created, i40e_ptp_create_clock will set
146862306a36Sopenharmony_ci * pf->ptp_prev_hw_time to the current system time. During resets, it is
146962306a36Sopenharmony_ci * expected that this timespec will be set to the last known PTP clock time,
147062306a36Sopenharmony_ci * in order to preserve the clock time as close as possible across a reset.
147162306a36Sopenharmony_ci **/
147262306a36Sopenharmony_civoid i40e_ptp_init(struct i40e_pf *pf)
147362306a36Sopenharmony_ci{
147462306a36Sopenharmony_ci	struct net_device *netdev = pf->vsi[pf->lan_vsi]->netdev;
147562306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
147662306a36Sopenharmony_ci	u32 pf_id;
147762306a36Sopenharmony_ci	long err;
147862306a36Sopenharmony_ci
147962306a36Sopenharmony_ci	/* Only one PF is assigned to control 1588 logic per port. Do not
148062306a36Sopenharmony_ci	 * enable any support for PFs not assigned via PRTTSYN_CTL0.PF_ID
148162306a36Sopenharmony_ci	 */
148262306a36Sopenharmony_ci	pf_id = (rd32(hw, I40E_PRTTSYN_CTL0) & I40E_PRTTSYN_CTL0_PF_ID_MASK) >>
148362306a36Sopenharmony_ci		I40E_PRTTSYN_CTL0_PF_ID_SHIFT;
148462306a36Sopenharmony_ci	if (hw->pf_id != pf_id) {
148562306a36Sopenharmony_ci		pf->flags &= ~I40E_FLAG_PTP;
148662306a36Sopenharmony_ci		dev_info(&pf->pdev->dev, "%s: PTP not supported on %s\n",
148762306a36Sopenharmony_ci			 __func__,
148862306a36Sopenharmony_ci			 netdev->name);
148962306a36Sopenharmony_ci		return;
149062306a36Sopenharmony_ci	}
149162306a36Sopenharmony_ci
149262306a36Sopenharmony_ci	mutex_init(&pf->tmreg_lock);
149362306a36Sopenharmony_ci	spin_lock_init(&pf->ptp_rx_lock);
149462306a36Sopenharmony_ci
149562306a36Sopenharmony_ci	/* ensure we have a clock device */
149662306a36Sopenharmony_ci	err = i40e_ptp_create_clock(pf);
149762306a36Sopenharmony_ci	if (err) {
149862306a36Sopenharmony_ci		pf->ptp_clock = NULL;
149962306a36Sopenharmony_ci		dev_err(&pf->pdev->dev, "%s: ptp_clock_register failed\n",
150062306a36Sopenharmony_ci			__func__);
150162306a36Sopenharmony_ci	} else if (pf->ptp_clock) {
150262306a36Sopenharmony_ci		u32 regval;
150362306a36Sopenharmony_ci
150462306a36Sopenharmony_ci		if (pf->hw.debug_mask & I40E_DEBUG_LAN)
150562306a36Sopenharmony_ci			dev_info(&pf->pdev->dev, "PHC enabled\n");
150662306a36Sopenharmony_ci		pf->flags |= I40E_FLAG_PTP;
150762306a36Sopenharmony_ci
150862306a36Sopenharmony_ci		/* Ensure the clocks are running. */
150962306a36Sopenharmony_ci		regval = rd32(hw, I40E_PRTTSYN_CTL0);
151062306a36Sopenharmony_ci		regval |= I40E_PRTTSYN_CTL0_TSYNENA_MASK;
151162306a36Sopenharmony_ci		wr32(hw, I40E_PRTTSYN_CTL0, regval);
151262306a36Sopenharmony_ci		regval = rd32(hw, I40E_PRTTSYN_CTL1);
151362306a36Sopenharmony_ci		regval |= I40E_PRTTSYN_CTL1_TSYNENA_MASK;
151462306a36Sopenharmony_ci		wr32(hw, I40E_PRTTSYN_CTL1, regval);
151562306a36Sopenharmony_ci
151662306a36Sopenharmony_ci		/* Set the increment value per clock tick. */
151762306a36Sopenharmony_ci		i40e_ptp_set_increment(pf);
151862306a36Sopenharmony_ci
151962306a36Sopenharmony_ci		/* reset timestamping mode */
152062306a36Sopenharmony_ci		i40e_ptp_set_timestamp_mode(pf, &pf->tstamp_config);
152162306a36Sopenharmony_ci
152262306a36Sopenharmony_ci		/* Restore the clock time based on last known value */
152362306a36Sopenharmony_ci		i40e_ptp_restore_hw_time(pf);
152462306a36Sopenharmony_ci	}
152562306a36Sopenharmony_ci
152662306a36Sopenharmony_ci	i40e_ptp_set_1pps_signal_hw(pf);
152762306a36Sopenharmony_ci}
152862306a36Sopenharmony_ci
152962306a36Sopenharmony_ci/**
153062306a36Sopenharmony_ci * i40e_ptp_stop - Disable the driver/hardware support and unregister the PHC
153162306a36Sopenharmony_ci * @pf: Board private structure
153262306a36Sopenharmony_ci *
153362306a36Sopenharmony_ci * This function handles the cleanup work required from the initialization by
153462306a36Sopenharmony_ci * clearing out the important information and unregistering the PHC.
153562306a36Sopenharmony_ci **/
153662306a36Sopenharmony_civoid i40e_ptp_stop(struct i40e_pf *pf)
153762306a36Sopenharmony_ci{
153862306a36Sopenharmony_ci	struct i40e_hw *hw = &pf->hw;
153962306a36Sopenharmony_ci	u32 regval;
154062306a36Sopenharmony_ci
154162306a36Sopenharmony_ci	pf->flags &= ~I40E_FLAG_PTP;
154262306a36Sopenharmony_ci	pf->ptp_tx = false;
154362306a36Sopenharmony_ci	pf->ptp_rx = false;
154462306a36Sopenharmony_ci
154562306a36Sopenharmony_ci	if (pf->ptp_tx_skb) {
154662306a36Sopenharmony_ci		struct sk_buff *skb = pf->ptp_tx_skb;
154762306a36Sopenharmony_ci
154862306a36Sopenharmony_ci		pf->ptp_tx_skb = NULL;
154962306a36Sopenharmony_ci		clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
155062306a36Sopenharmony_ci		dev_kfree_skb_any(skb);
155162306a36Sopenharmony_ci	}
155262306a36Sopenharmony_ci
155362306a36Sopenharmony_ci	if (pf->ptp_clock) {
155462306a36Sopenharmony_ci		ptp_clock_unregister(pf->ptp_clock);
155562306a36Sopenharmony_ci		pf->ptp_clock = NULL;
155662306a36Sopenharmony_ci		dev_info(&pf->pdev->dev, "%s: removed PHC on %s\n", __func__,
155762306a36Sopenharmony_ci			 pf->vsi[pf->lan_vsi]->netdev->name);
155862306a36Sopenharmony_ci	}
155962306a36Sopenharmony_ci
156062306a36Sopenharmony_ci	if (i40e_is_ptp_pin_dev(&pf->hw)) {
156162306a36Sopenharmony_ci		i40e_ptp_set_pin_hw(hw, I40E_SDP3_2, off);
156262306a36Sopenharmony_ci		i40e_ptp_set_pin_hw(hw, I40E_SDP3_3, off);
156362306a36Sopenharmony_ci		i40e_ptp_set_pin_hw(hw, I40E_GPIO_4, off);
156462306a36Sopenharmony_ci	}
156562306a36Sopenharmony_ci
156662306a36Sopenharmony_ci	regval = rd32(hw, I40E_PRTTSYN_AUX_0(0));
156762306a36Sopenharmony_ci	regval &= ~I40E_PRTTSYN_AUX_0_PTPFLAG_MASK;
156862306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_AUX_0(0), regval);
156962306a36Sopenharmony_ci
157062306a36Sopenharmony_ci	/* Disable interrupts */
157162306a36Sopenharmony_ci	regval = rd32(hw, I40E_PRTTSYN_CTL0);
157262306a36Sopenharmony_ci	regval &= ~I40E_PRTTSYN_CTL0_EVENT_INT_ENA_MASK;
157362306a36Sopenharmony_ci	wr32(hw, I40E_PRTTSYN_CTL0, regval);
157462306a36Sopenharmony_ci
157562306a36Sopenharmony_ci	i40e_ptp_free_pins(pf);
157662306a36Sopenharmony_ci}
1577