1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Marvell 88E6xxx Switch hardware timestamping support
5 * Copyright (c) 2008 Marvell Semiconductor
7 * Copyright (c) 2017 National Instruments
8 * Erik Hons <erik.hons@ni.com>
9 * Brandon Streiff <brandon.streiff@ni.com>
10 * Dane Wagner <dane.wagner@ni.com>
17 #include <linux/ptp_classify.h>
19 #define SKB_PTP_TYPE(__skb) (*(unsigned int *)((__skb)->cb))
21 static int mv88e6xxx_port_ptp_read(struct mv88e6xxx_chip
*chip
, int port
,
22 int addr
, u16
*data
, int len
)
24 if (!chip
->info
->ops
->avb_ops
->port_ptp_read
)
27 return chip
->info
->ops
->avb_ops
->port_ptp_read(chip
, port
, addr
,
31 static int mv88e6xxx_port_ptp_write(struct mv88e6xxx_chip
*chip
, int port
,
34 if (!chip
->info
->ops
->avb_ops
->port_ptp_write
)
37 return chip
->info
->ops
->avb_ops
->port_ptp_write(chip
, port
, addr
,
41 static int mv88e6xxx_ptp_write(struct mv88e6xxx_chip
*chip
, int addr
,
44 if (!chip
->info
->ops
->avb_ops
->ptp_write
)
47 return chip
->info
->ops
->avb_ops
->ptp_write(chip
, addr
, data
);
50 static int mv88e6xxx_ptp_read(struct mv88e6xxx_chip
*chip
, int addr
,
53 if (!chip
->info
->ops
->avb_ops
->ptp_read
)
56 return chip
->info
->ops
->avb_ops
->ptp_read(chip
, addr
, data
, 1);
59 /* TX_TSTAMP_TIMEOUT: This limits the time spent polling for a TX
60 * timestamp. When working properly, hardware will produce a timestamp
61 * within 1ms. Software may enounter delays due to MDIO contention, so
62 * the timeout is set accordingly.
64 #define TX_TSTAMP_TIMEOUT msecs_to_jiffies(40)
66 int mv88e6xxx_get_ts_info(struct dsa_switch
*ds
, int port
,
67 struct ethtool_ts_info
*info
)
69 const struct mv88e6xxx_ptp_ops
*ptp_ops
;
70 struct mv88e6xxx_chip
*chip
;
73 ptp_ops
= chip
->info
->ops
->ptp_ops
;
75 if (!chip
->info
->ptp_support
)
78 info
->so_timestamping
=
79 SOF_TIMESTAMPING_TX_HARDWARE
|
80 SOF_TIMESTAMPING_RX_HARDWARE
|
81 SOF_TIMESTAMPING_RAW_HARDWARE
;
82 info
->phc_index
= ptp_clock_index(chip
->ptp_clock
);
84 (1 << HWTSTAMP_TX_OFF
) |
85 (1 << HWTSTAMP_TX_ON
);
86 info
->rx_filters
= ptp_ops
->rx_filters
;
91 static int mv88e6xxx_set_hwtstamp_config(struct mv88e6xxx_chip
*chip
, int port
,
92 struct hwtstamp_config
*config
)
94 const struct mv88e6xxx_ptp_ops
*ptp_ops
= chip
->info
->ops
->ptp_ops
;
95 struct mv88e6xxx_port_hwtstamp
*ps
= &chip
->port_hwtstamp
[port
];
96 bool tstamp_enable
= false;
98 /* Prevent the TX/RX paths from trying to interact with the
99 * timestamp hardware while we reconfigure it.
101 clear_bit_unlock(MV88E6XXX_HWTSTAMP_ENABLED
, &ps
->state
);
103 /* reserved for future extensions */
107 switch (config
->tx_type
) {
108 case HWTSTAMP_TX_OFF
:
109 tstamp_enable
= false;
112 tstamp_enable
= true;
118 /* The switch supports timestamping both L2 and L4; one cannot be
119 * disabled independently of the other.
122 if (!(BIT(config
->rx_filter
) & ptp_ops
->rx_filters
)) {
123 config
->rx_filter
= HWTSTAMP_FILTER_NONE
;
124 dev_dbg(chip
->dev
, "Unsupported rx_filter %d\n",
129 switch (config
->rx_filter
) {
130 case HWTSTAMP_FILTER_NONE
:
131 tstamp_enable
= false;
133 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
134 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
135 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
136 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
137 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
138 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
139 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
140 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
141 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
142 config
->rx_filter
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
144 case HWTSTAMP_FILTER_ALL
:
146 config
->rx_filter
= HWTSTAMP_FILTER_NONE
;
150 mv88e6xxx_reg_lock(chip
);
152 chip
->enable_count
+= 1;
153 if (chip
->enable_count
== 1 && ptp_ops
->global_enable
)
154 ptp_ops
->global_enable(chip
);
155 if (ptp_ops
->port_enable
)
156 ptp_ops
->port_enable(chip
, port
);
158 if (ptp_ops
->port_disable
)
159 ptp_ops
->port_disable(chip
, port
);
160 chip
->enable_count
-= 1;
161 if (chip
->enable_count
== 0 && ptp_ops
->global_disable
)
162 ptp_ops
->global_disable(chip
);
164 mv88e6xxx_reg_unlock(chip
);
166 /* Once hardware has been configured, enable timestamp checks
167 * in the RX/TX paths.
170 set_bit(MV88E6XXX_HWTSTAMP_ENABLED
, &ps
->state
);
175 int mv88e6xxx_port_hwtstamp_set(struct dsa_switch
*ds
, int port
,
178 struct mv88e6xxx_chip
*chip
= ds
->priv
;
179 struct mv88e6xxx_port_hwtstamp
*ps
= &chip
->port_hwtstamp
[port
];
180 struct hwtstamp_config config
;
183 if (!chip
->info
->ptp_support
)
186 if (copy_from_user(&config
, ifr
->ifr_data
, sizeof(config
)))
189 err
= mv88e6xxx_set_hwtstamp_config(chip
, port
, &config
);
193 /* Save the chosen configuration to be returned later. */
194 memcpy(&ps
->tstamp_config
, &config
, sizeof(config
));
196 return copy_to_user(ifr
->ifr_data
, &config
, sizeof(config
)) ?
200 int mv88e6xxx_port_hwtstamp_get(struct dsa_switch
*ds
, int port
,
203 struct mv88e6xxx_chip
*chip
= ds
->priv
;
204 struct mv88e6xxx_port_hwtstamp
*ps
= &chip
->port_hwtstamp
[port
];
205 struct hwtstamp_config
*config
= &ps
->tstamp_config
;
207 if (!chip
->info
->ptp_support
)
210 return copy_to_user(ifr
->ifr_data
, config
, sizeof(*config
)) ?
214 /* Get the start of the PTP header in this skb */
215 static u8
*parse_ptp_header(struct sk_buff
*skb
, unsigned int type
)
217 u8
*data
= skb_mac_header(skb
);
218 unsigned int offset
= 0;
220 if (type
& PTP_CLASS_VLAN
)
223 switch (type
& PTP_CLASS_PMASK
) {
225 offset
+= ETH_HLEN
+ IPV4_HLEN(data
+ offset
) + UDP_HLEN
;
228 offset
+= ETH_HLEN
+ IP6_HLEN
+ UDP_HLEN
;
237 /* Ensure that the entire header is present in this packet. */
238 if (skb
->len
+ ETH_HLEN
< offset
+ 34)
241 return data
+ offset
;
244 /* Returns a pointer to the PTP header if the caller should time stamp,
245 * or NULL if the caller should not.
247 static u8
*mv88e6xxx_should_tstamp(struct mv88e6xxx_chip
*chip
, int port
,
248 struct sk_buff
*skb
, unsigned int type
)
250 struct mv88e6xxx_port_hwtstamp
*ps
= &chip
->port_hwtstamp
[port
];
253 if (!chip
->info
->ptp_support
)
256 hdr
= parse_ptp_header(skb
, type
);
260 if (!test_bit(MV88E6XXX_HWTSTAMP_ENABLED
, &ps
->state
))
266 static int mv88e6xxx_ts_valid(u16 status
)
268 if (!(status
& MV88E6XXX_PTP_TS_VALID
))
270 if (status
& MV88E6XXX_PTP_TS_STATUS_MASK
)
275 static int seq_match(struct sk_buff
*skb
, u16 ts_seqid
)
277 unsigned int type
= SKB_PTP_TYPE(skb
);
278 u8
*hdr
= parse_ptp_header(skb
, type
);
281 seqid
= (__be16
*)(hdr
+ OFF_PTP_SEQUENCE_ID
);
283 return ts_seqid
== ntohs(*seqid
);
286 static void mv88e6xxx_get_rxts(struct mv88e6xxx_chip
*chip
,
287 struct mv88e6xxx_port_hwtstamp
*ps
,
288 struct sk_buff
*skb
, u16 reg
,
289 struct sk_buff_head
*rxq
)
291 u16 buf
[4] = { 0 }, status
, seq_id
;
292 struct skb_shared_hwtstamps
*shwt
;
293 struct sk_buff_head received
;
294 u64 ns
, timelo
, timehi
;
298 /* The latched timestamp belongs to one of the received frames. */
299 __skb_queue_head_init(&received
);
300 spin_lock_irqsave(&rxq
->lock
, flags
);
301 skb_queue_splice_tail_init(rxq
, &received
);
302 spin_unlock_irqrestore(&rxq
->lock
, flags
);
304 mv88e6xxx_reg_lock(chip
);
305 err
= mv88e6xxx_port_ptp_read(chip
, ps
->port_id
,
306 reg
, buf
, ARRAY_SIZE(buf
));
307 mv88e6xxx_reg_unlock(chip
);
309 pr_err("failed to get the receive time stamp\n");
316 if (status
& MV88E6XXX_PTP_TS_VALID
) {
317 mv88e6xxx_reg_lock(chip
);
318 err
= mv88e6xxx_port_ptp_write(chip
, ps
->port_id
, reg
, 0);
319 mv88e6xxx_reg_unlock(chip
);
321 pr_err("failed to clear the receive status\n");
323 /* Since the device can only handle one time stamp at a time,
324 * we purge any extra frames from the queue.
326 for ( ; skb
; skb
= __skb_dequeue(&received
)) {
327 if (mv88e6xxx_ts_valid(status
) && seq_match(skb
, seq_id
)) {
328 ns
= timehi
<< 16 | timelo
;
330 mv88e6xxx_reg_lock(chip
);
331 ns
= timecounter_cyc2time(&chip
->tstamp_tc
, ns
);
332 mv88e6xxx_reg_unlock(chip
);
333 shwt
= skb_hwtstamps(skb
);
334 memset(shwt
, 0, sizeof(*shwt
));
335 shwt
->hwtstamp
= ns_to_ktime(ns
);
336 status
&= ~MV88E6XXX_PTP_TS_VALID
;
342 static void mv88e6xxx_rxtstamp_work(struct mv88e6xxx_chip
*chip
,
343 struct mv88e6xxx_port_hwtstamp
*ps
)
345 const struct mv88e6xxx_ptp_ops
*ptp_ops
= chip
->info
->ops
->ptp_ops
;
348 skb
= skb_dequeue(&ps
->rx_queue
);
351 mv88e6xxx_get_rxts(chip
, ps
, skb
, ptp_ops
->arr0_sts_reg
,
354 skb
= skb_dequeue(&ps
->rx_queue2
);
356 mv88e6xxx_get_rxts(chip
, ps
, skb
, ptp_ops
->arr1_sts_reg
,
360 static int is_pdelay_resp(u8
*msgtype
)
362 return (*msgtype
& 0xf) == 3;
365 bool mv88e6xxx_port_rxtstamp(struct dsa_switch
*ds
, int port
,
366 struct sk_buff
*skb
, unsigned int type
)
368 struct mv88e6xxx_port_hwtstamp
*ps
;
369 struct mv88e6xxx_chip
*chip
;
373 ps
= &chip
->port_hwtstamp
[port
];
375 if (ps
->tstamp_config
.rx_filter
!= HWTSTAMP_FILTER_PTP_V2_EVENT
)
378 hdr
= mv88e6xxx_should_tstamp(chip
, port
, skb
, type
);
382 SKB_PTP_TYPE(skb
) = type
;
384 if (is_pdelay_resp(hdr
))
385 skb_queue_tail(&ps
->rx_queue2
, skb
);
387 skb_queue_tail(&ps
->rx_queue
, skb
);
389 ptp_schedule_worker(chip
->ptp_clock
, 0);
394 static int mv88e6xxx_txtstamp_work(struct mv88e6xxx_chip
*chip
,
395 struct mv88e6xxx_port_hwtstamp
*ps
)
397 const struct mv88e6xxx_ptp_ops
*ptp_ops
= chip
->info
->ops
->ptp_ops
;
398 struct skb_shared_hwtstamps shhwtstamps
;
399 u16 departure_block
[4], status
;
400 struct sk_buff
*tmp_skb
;
408 mv88e6xxx_reg_lock(chip
);
409 err
= mv88e6xxx_port_ptp_read(chip
, ps
->port_id
,
410 ptp_ops
->dep_sts_reg
,
412 ARRAY_SIZE(departure_block
));
413 mv88e6xxx_reg_unlock(chip
);
416 goto free_and_clear_skb
;
418 if (!(departure_block
[0] & MV88E6XXX_PTP_TS_VALID
)) {
419 if (time_is_before_jiffies(ps
->tx_tstamp_start
+
420 TX_TSTAMP_TIMEOUT
)) {
421 dev_warn(chip
->dev
, "p%d: clearing tx timestamp hang\n",
423 goto free_and_clear_skb
;
425 /* The timestamp should be available quickly, while getting it
426 * is high priority and time bounded to only 10ms. A poll is
427 * warranted so restart the work.
432 /* We have the timestamp; go ahead and clear valid now */
433 mv88e6xxx_reg_lock(chip
);
434 mv88e6xxx_port_ptp_write(chip
, ps
->port_id
, ptp_ops
->dep_sts_reg
, 0);
435 mv88e6xxx_reg_unlock(chip
);
437 status
= departure_block
[0] & MV88E6XXX_PTP_TS_STATUS_MASK
;
438 if (status
!= MV88E6XXX_PTP_TS_STATUS_NORMAL
) {
439 dev_warn(chip
->dev
, "p%d: tx timestamp overrun\n", ps
->port_id
);
440 goto free_and_clear_skb
;
443 if (departure_block
[3] != ps
->tx_seq_id
) {
444 dev_warn(chip
->dev
, "p%d: unexpected seq. id\n", ps
->port_id
);
445 goto free_and_clear_skb
;
448 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
449 time_raw
= ((u32
)departure_block
[2] << 16) | departure_block
[1];
450 mv88e6xxx_reg_lock(chip
);
451 ns
= timecounter_cyc2time(&chip
->tstamp_tc
, time_raw
);
452 mv88e6xxx_reg_unlock(chip
);
453 shhwtstamps
.hwtstamp
= ns_to_ktime(ns
);
456 "p%d: txtstamp %llx status 0x%04x skb ID 0x%04x hw ID 0x%04x\n",
457 ps
->port_id
, ktime_to_ns(shhwtstamps
.hwtstamp
),
458 departure_block
[0], ps
->tx_seq_id
, departure_block
[3]);
460 /* skb_complete_tx_timestamp() will free up the client to make
461 * another timestamp-able transmit. We have to be ready for it
462 * -- by clearing the ps->tx_skb "flag" -- beforehand.
465 tmp_skb
= ps
->tx_skb
;
467 clear_bit_unlock(MV88E6XXX_HWTSTAMP_TX_IN_PROGRESS
, &ps
->state
);
468 skb_complete_tx_timestamp(tmp_skb
, &shhwtstamps
);
473 dev_kfree_skb_any(ps
->tx_skb
);
475 clear_bit_unlock(MV88E6XXX_HWTSTAMP_TX_IN_PROGRESS
, &ps
->state
);
480 long mv88e6xxx_hwtstamp_work(struct ptp_clock_info
*ptp
)
482 struct mv88e6xxx_chip
*chip
= ptp_to_chip(ptp
);
483 struct dsa_switch
*ds
= chip
->ds
;
484 struct mv88e6xxx_port_hwtstamp
*ps
;
487 for (i
= 0; i
< ds
->num_ports
; i
++) {
488 if (!dsa_is_user_port(ds
, i
))
491 ps
= &chip
->port_hwtstamp
[i
];
492 if (test_bit(MV88E6XXX_HWTSTAMP_TX_IN_PROGRESS
, &ps
->state
))
493 restart
|= mv88e6xxx_txtstamp_work(chip
, ps
);
495 mv88e6xxx_rxtstamp_work(chip
, ps
);
498 return restart
? 1 : -1;
501 bool mv88e6xxx_port_txtstamp(struct dsa_switch
*ds
, int port
,
502 struct sk_buff
*clone
, unsigned int type
)
504 struct mv88e6xxx_chip
*chip
= ds
->priv
;
505 struct mv88e6xxx_port_hwtstamp
*ps
= &chip
->port_hwtstamp
[port
];
509 if (!(skb_shinfo(clone
)->tx_flags
& SKBTX_HW_TSTAMP
))
512 hdr
= mv88e6xxx_should_tstamp(chip
, port
, clone
, type
);
516 seq_ptr
= (__be16
*)(hdr
+ OFF_PTP_SEQUENCE_ID
);
518 if (test_and_set_bit_lock(MV88E6XXX_HWTSTAMP_TX_IN_PROGRESS
,
523 ps
->tx_tstamp_start
= jiffies
;
524 ps
->tx_seq_id
= be16_to_cpup(seq_ptr
);
526 ptp_schedule_worker(chip
->ptp_clock
, 0);
530 int mv88e6165_global_disable(struct mv88e6xxx_chip
*chip
)
535 err
= mv88e6xxx_ptp_read(chip
, MV88E6165_PTP_CFG
, &val
);
538 val
|= MV88E6165_PTP_CFG_DISABLE_PTP
;
540 return mv88e6xxx_ptp_write(chip
, MV88E6165_PTP_CFG
, val
);
543 int mv88e6165_global_enable(struct mv88e6xxx_chip
*chip
)
548 err
= mv88e6xxx_ptp_read(chip
, MV88E6165_PTP_CFG
, &val
);
552 val
&= ~(MV88E6165_PTP_CFG_DISABLE_PTP
| MV88E6165_PTP_CFG_TSPEC_MASK
);
554 return mv88e6xxx_ptp_write(chip
, MV88E6165_PTP_CFG
, val
);
557 int mv88e6352_hwtstamp_port_disable(struct mv88e6xxx_chip
*chip
, int port
)
559 return mv88e6xxx_port_ptp_write(chip
, port
, MV88E6XXX_PORT_PTP_CFG0
,
560 MV88E6XXX_PORT_PTP_CFG0_DISABLE_PTP
);
563 int mv88e6352_hwtstamp_port_enable(struct mv88e6xxx_chip
*chip
, int port
)
565 return mv88e6xxx_port_ptp_write(chip
, port
, MV88E6XXX_PORT_PTP_CFG0
,
566 MV88E6XXX_PORT_PTP_CFG0_DISABLE_TSPEC_MATCH
);
569 static int mv88e6xxx_hwtstamp_port_setup(struct mv88e6xxx_chip
*chip
, int port
)
571 const struct mv88e6xxx_ptp_ops
*ptp_ops
= chip
->info
->ops
->ptp_ops
;
572 struct mv88e6xxx_port_hwtstamp
*ps
= &chip
->port_hwtstamp
[port
];
576 skb_queue_head_init(&ps
->rx_queue
);
577 skb_queue_head_init(&ps
->rx_queue2
);
579 if (ptp_ops
->port_disable
)
580 return ptp_ops
->port_disable(chip
, port
);
585 int mv88e6xxx_hwtstamp_setup(struct mv88e6xxx_chip
*chip
)
587 const struct mv88e6xxx_ptp_ops
*ptp_ops
= chip
->info
->ops
->ptp_ops
;
591 /* Disable timestamping on all ports. */
592 for (i
= 0; i
< mv88e6xxx_num_ports(chip
); ++i
) {
593 err
= mv88e6xxx_hwtstamp_port_setup(chip
, i
);
598 /* Disable PTP globally */
599 if (ptp_ops
->global_disable
) {
600 err
= ptp_ops
->global_disable(chip
);
605 /* Set the ethertype of L2 PTP messages */
606 err
= mv88e6xxx_ptp_write(chip
, MV88E6XXX_PTP_GC_ETYPE
, ETH_P_1588
);
610 /* MV88E6XXX_PTP_MSG_TYPE is a mask of PTP message types to
611 * timestamp. This affects all ports that have timestamping enabled,
612 * but the timestamp config is per-port; thus we configure all events
613 * here and only support the HWTSTAMP_FILTER_*_EVENT filter types.
615 err
= mv88e6xxx_ptp_write(chip
, MV88E6XXX_PTP_MSGTYPE
,
616 MV88E6XXX_PTP_MSGTYPE_ALL_EVENT
);
620 /* Use ARRIVAL1 for peer delay response messages. */
621 err
= mv88e6xxx_ptp_write(chip
, MV88E6XXX_PTP_TS_ARRIVAL_PTR
,
622 MV88E6XXX_PTP_MSGTYPE_PDLAY_RES
);
626 /* 88E6341 devices default to timestamping at the PHY, but this has
627 * a hardware issue that results in unreliable timestamps. Force
628 * these devices to timestamp at the MAC.
630 if (chip
->info
->family
== MV88E6XXX_FAMILY_6341
) {
631 u16 val
= MV88E6341_PTP_CFG_UPDATE
|
632 MV88E6341_PTP_CFG_MODE_IDX
|
633 MV88E6341_PTP_CFG_MODE_TS_AT_MAC
;
634 err
= mv88e6xxx_ptp_write(chip
, MV88E6341_PTP_CFG
, val
);
642 void mv88e6xxx_hwtstamp_free(struct mv88e6xxx_chip
*chip
)