1 /* Broadcom NetXtreme-C/E network driver.
3 * Copyright (c) 2021 Broadcom Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation.
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/pci.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/ptp_clock_kernel.h>
15 #include <linux/net_tstamp.h>
16 #include <linux/timecounter.h>
17 #include <linux/timekeeping.h>
18 #include <linux/ptp_classify.h>
23 int bnxt_ptp_parse(struct sk_buff *skb, u16 *seq_id)
25 unsigned int ptp_class;
26 struct ptp_header *hdr;
28 ptp_class = ptp_classify_raw(skb);
30 switch (ptp_class & PTP_CLASS_VMASK) {
33 hdr = ptp_parse_header(skb, ptp_class);
37 *seq_id = ntohs(hdr->sequence_id);
44 static int bnxt_ptp_settime(struct ptp_clock_info *ptp_info,
45 const struct timespec64 *ts)
47 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
49 u64 ns = timespec64_to_ns(ts);
51 spin_lock_bh(&ptp->ptp_lock);
52 timecounter_init(&ptp->tc, &ptp->cc, ns);
53 spin_unlock_bh(&ptp->ptp_lock);
57 /* Caller holds ptp_lock */
58 static u64 bnxt_refclk_read(struct bnxt *bp, struct ptp_system_timestamp *sts)
60 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
63 ptp_read_system_prets(sts);
64 ns = readl(bp->bar0 + ptp->refclk_mapped_regs[0]);
65 ptp_read_system_postts(sts);
66 ns |= (u64)readl(bp->bar0 + ptp->refclk_mapped_regs[1]) << 32;
70 static void bnxt_ptp_get_current_time(struct bnxt *bp)
72 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
76 spin_lock_bh(&ptp->ptp_lock);
77 WRITE_ONCE(ptp->old_time, ptp->current_time);
78 ptp->current_time = bnxt_refclk_read(bp, NULL);
79 spin_unlock_bh(&ptp->ptp_lock);
82 static int bnxt_hwrm_port_ts_query(struct bnxt *bp, u32 flags, u64 *ts)
84 struct hwrm_port_ts_query_output *resp = bp->hwrm_cmd_resp_addr;
85 struct hwrm_port_ts_query_input req = {0};
88 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_TS_QUERY, -1, -1);
89 req.flags = cpu_to_le32(flags);
90 if ((flags & PORT_TS_QUERY_REQ_FLAGS_PATH) ==
91 PORT_TS_QUERY_REQ_FLAGS_PATH_TX) {
92 req.enables = cpu_to_le16(BNXT_PTP_QTS_TX_ENABLES);
93 req.ptp_seq_id = cpu_to_le32(bp->ptp_cfg->tx_seqid);
94 req.ts_req_timeout = cpu_to_le16(BNXT_PTP_QTS_TIMEOUT);
96 mutex_lock(&bp->hwrm_cmd_lock);
97 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
99 *ts = le64_to_cpu(resp->ptp_msg_ts);
100 mutex_unlock(&bp->hwrm_cmd_lock);
104 static int bnxt_ptp_gettimex(struct ptp_clock_info *ptp_info,
105 struct timespec64 *ts,
106 struct ptp_system_timestamp *sts)
108 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
112 spin_lock_bh(&ptp->ptp_lock);
113 cycles = bnxt_refclk_read(ptp->bp, sts);
114 ns = timecounter_cyc2time(&ptp->tc, cycles);
115 spin_unlock_bh(&ptp->ptp_lock);
116 *ts = ns_to_timespec64(ns);
121 static int bnxt_ptp_adjtime(struct ptp_clock_info *ptp_info, s64 delta)
123 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
126 spin_lock_bh(&ptp->ptp_lock);
127 timecounter_adjtime(&ptp->tc, delta);
128 spin_unlock_bh(&ptp->ptp_lock);
132 static int bnxt_ptp_adjfreq(struct ptp_clock_info *ptp_info, s32 ppb)
134 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
136 struct hwrm_port_mac_cfg_input req = {0};
137 struct bnxt *bp = ptp->bp;
140 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_CFG, -1, -1);
141 req.ptp_freq_adj_ppb = cpu_to_le32(ppb);
142 req.enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_PTP_FREQ_ADJ_PPB);
143 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
145 netdev_err(ptp->bp->dev,
146 "ptp adjfreq failed. rc = %d\n", rc);
150 static int bnxt_ptp_enable(struct ptp_clock_info *ptp,
151 struct ptp_clock_request *rq, int on)
156 static int bnxt_hwrm_ptp_cfg(struct bnxt *bp)
158 struct hwrm_port_mac_cfg_input req = {0};
159 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
162 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_MAC_CFG, -1, -1);
164 flags |= PORT_MAC_CFG_REQ_FLAGS_PTP_RX_TS_CAPTURE_ENABLE;
166 flags |= PORT_MAC_CFG_REQ_FLAGS_PTP_RX_TS_CAPTURE_DISABLE;
167 if (ptp->tx_tstamp_en)
168 flags |= PORT_MAC_CFG_REQ_FLAGS_PTP_TX_TS_CAPTURE_ENABLE;
170 flags |= PORT_MAC_CFG_REQ_FLAGS_PTP_TX_TS_CAPTURE_DISABLE;
171 req.flags = cpu_to_le32(flags);
172 req.enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_RX_TS_CAPTURE_PTP_MSG_TYPE);
173 req.rx_ts_capture_ptp_msg_type = cpu_to_le16(ptp->rxctl);
175 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
178 int bnxt_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
180 struct bnxt *bp = netdev_priv(dev);
181 struct hwtstamp_config stmpconf;
182 struct bnxt_ptp_cfg *ptp;
184 int old_rx_filter, rc;
191 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
197 if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
198 stmpconf.tx_type != HWTSTAMP_TX_OFF)
201 old_rx_filter = ptp->rx_filter;
202 old_rxctl = ptp->rxctl;
203 old_tx_tstamp_en = ptp->tx_tstamp_en;
204 switch (stmpconf.rx_filter) {
205 case HWTSTAMP_FILTER_NONE:
207 ptp->rx_filter = HWTSTAMP_FILTER_NONE;
209 case HWTSTAMP_FILTER_PTP_V2_EVENT:
210 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
211 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
212 ptp->rxctl = BNXT_PTP_MSG_EVENTS;
213 ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
215 case HWTSTAMP_FILTER_PTP_V2_SYNC:
216 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
217 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
218 ptp->rxctl = BNXT_PTP_MSG_SYNC;
219 ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
221 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
222 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
223 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
224 ptp->rxctl = BNXT_PTP_MSG_DELAY_REQ;
225 ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
231 if (stmpconf.tx_type == HWTSTAMP_TX_ON)
232 ptp->tx_tstamp_en = 1;
234 ptp->tx_tstamp_en = 0;
236 rc = bnxt_hwrm_ptp_cfg(bp);
240 stmpconf.rx_filter = ptp->rx_filter;
241 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
245 ptp->rx_filter = old_rx_filter;
246 ptp->rxctl = old_rxctl;
247 ptp->tx_tstamp_en = old_tx_tstamp_en;
251 int bnxt_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
253 struct bnxt *bp = netdev_priv(dev);
254 struct hwtstamp_config stmpconf;
255 struct bnxt_ptp_cfg *ptp;
262 stmpconf.tx_type = ptp->tx_tstamp_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
264 stmpconf.rx_filter = ptp->rx_filter;
265 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
269 static int bnxt_map_regs(struct bnxt *bp, u32 *reg_arr, int count, int reg_win)
271 u32 reg_base = *reg_arr & BNXT_GRC_BASE_MASK;
275 for (i = 0; i < count; i++) {
276 if ((reg_arr[i] & BNXT_GRC_BASE_MASK) != reg_base)
279 win_off = BNXT_GRCPF_REG_WINDOW_BASE_OUT + (reg_win - 1) * 4;
280 writel(reg_base, bp->bar0 + win_off);
284 static int bnxt_map_ptp_regs(struct bnxt *bp)
286 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
290 reg_arr = ptp->refclk_regs;
291 if (bp->flags & BNXT_FLAG_CHIP_P5) {
292 rc = bnxt_map_regs(bp, reg_arr, 2, BNXT_PTP_GRC_WIN);
295 for (i = 0; i < 2; i++)
296 ptp->refclk_mapped_regs[i] = BNXT_PTP_GRC_WIN_BASE +
297 (ptp->refclk_regs[i] & BNXT_GRC_OFFSET_MASK);
303 static void bnxt_unmap_ptp_regs(struct bnxt *bp)
305 writel(0, bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT +
306 (BNXT_PTP_GRC_WIN - 1) * 4);
309 static u64 bnxt_cc_read(const struct cyclecounter *cc)
311 struct bnxt_ptp_cfg *ptp = container_of(cc, struct bnxt_ptp_cfg, cc);
313 return bnxt_refclk_read(ptp->bp, NULL);
316 static void bnxt_stamp_tx_skb(struct bnxt *bp, struct sk_buff *skb)
318 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
319 struct skb_shared_hwtstamps timestamp;
323 rc = bnxt_hwrm_port_ts_query(bp, PORT_TS_QUERY_REQ_FLAGS_PATH_TX, &ts);
325 memset(×tamp, 0, sizeof(timestamp));
326 spin_lock_bh(&ptp->ptp_lock);
327 ns = timecounter_cyc2time(&ptp->tc, ts);
328 spin_unlock_bh(&ptp->ptp_lock);
329 timestamp.hwtstamp = ns_to_ktime(ns);
330 skb_tstamp_tx(ptp->tx_skb, ×tamp);
332 netdev_err(bp->dev, "TS query for TX timer failed rc = %x\n",
336 dev_kfree_skb_any(ptp->tx_skb);
338 atomic_inc(&ptp->tx_avail);
341 static long bnxt_ptp_ts_aux_work(struct ptp_clock_info *ptp_info)
343 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
345 unsigned long now = jiffies;
346 struct bnxt *bp = ptp->bp;
349 bnxt_stamp_tx_skb(bp, ptp->tx_skb);
351 if (!time_after_eq(now, ptp->next_period))
352 return ptp->next_period - now;
354 bnxt_ptp_get_current_time(bp);
355 ptp->next_period = now + HZ;
359 int bnxt_get_tx_ts_p5(struct bnxt *bp, struct sk_buff *skb)
361 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
364 netdev_err(bp->dev, "deferring skb:one SKB is still outstanding\n");
368 ptp_schedule_worker(ptp->ptp_clock, 0);
372 int bnxt_get_rx_ts_p5(struct bnxt *bp, u64 *ts, u32 pkt_ts)
374 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
380 BNXT_READ_TIME64(ptp, time, ptp->old_time);
381 *ts = (time & BNXT_HI_TIMER_MASK) | pkt_ts;
382 if (pkt_ts < (time & BNXT_LO_TIMER_MASK))
383 *ts += BNXT_LO_TIMER_MASK + 1;
388 void bnxt_ptp_start(struct bnxt *bp)
390 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
395 if (bp->flags & BNXT_FLAG_CHIP_P5) {
396 spin_lock_bh(&ptp->ptp_lock);
397 ptp->current_time = bnxt_refclk_read(bp, NULL);
398 WRITE_ONCE(ptp->old_time, ptp->current_time);
399 spin_unlock_bh(&ptp->ptp_lock);
400 ptp_schedule_worker(ptp->ptp_clock, 0);
404 static const struct ptp_clock_info bnxt_ptp_caps = {
405 .owner = THIS_MODULE,
406 .name = "bnxt clock",
407 .max_adj = BNXT_MAX_PHC_DRIFT,
413 .adjfreq = bnxt_ptp_adjfreq,
414 .adjtime = bnxt_ptp_adjtime,
415 .do_aux_work = bnxt_ptp_ts_aux_work,
416 .gettimex64 = bnxt_ptp_gettimex,
417 .settime64 = bnxt_ptp_settime,
418 .enable = bnxt_ptp_enable,
421 int bnxt_ptp_init(struct bnxt *bp)
423 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
429 rc = bnxt_map_ptp_regs(bp);
433 atomic_set(&ptp->tx_avail, BNXT_MAX_TX_TS);
434 spin_lock_init(&ptp->ptp_lock);
436 memset(&ptp->cc, 0, sizeof(ptp->cc));
437 ptp->cc.read = bnxt_cc_read;
438 ptp->cc.mask = CYCLECOUNTER_MASK(48);
442 timecounter_init(&ptp->tc, &ptp->cc, ktime_to_ns(ktime_get_real()));
444 ptp->ptp_info = bnxt_ptp_caps;
445 ptp->ptp_clock = ptp_clock_register(&ptp->ptp_info, &bp->pdev->dev);
446 if (IS_ERR(ptp->ptp_clock)) {
447 int err = PTR_ERR(ptp->ptp_clock);
449 ptp->ptp_clock = NULL;
450 bnxt_unmap_ptp_regs(bp);
457 void bnxt_ptp_clear(struct bnxt *bp)
459 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
465 ptp_clock_unregister(ptp->ptp_clock);
467 ptp->ptp_clock = NULL;
469 dev_kfree_skb_any(ptp->tx_skb);
472 bnxt_unmap_ptp_regs(bp);