1 /* Broadcom NetXtreme-C/E network driver.
2 *
3 * Copyright (c) 2021 Broadcom Inc.
4 *
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.
8 */
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/net_tstamp.h>
15 #include <linux/timekeeping.h>
16 #include <linux/ptp_classify.h>
17 #include <linux/clocksource.h>
18 #include <linux/bnxt/hsi.h>
19 #include "bnxt.h"
20 #include "bnxt_hwrm.h"
21 #include "bnxt_ptp.h"
22
bnxt_ptp_cfg_settime(struct bnxt * bp,u64 time)23 static int bnxt_ptp_cfg_settime(struct bnxt *bp, u64 time)
24 {
25 struct hwrm_func_ptp_cfg_input *req;
26 int rc;
27
28 rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_CFG);
29 if (rc)
30 return rc;
31
32 req->enables = cpu_to_le16(FUNC_PTP_CFG_REQ_ENABLES_PTP_SET_TIME);
33 req->ptp_set_time = cpu_to_le64(time);
34 return hwrm_req_send(bp, req);
35 }
36
bnxt_ptp_parse(struct sk_buff * skb,u16 * seq_id,u16 * hdr_off)37 int bnxt_ptp_parse(struct sk_buff *skb, u16 *seq_id, u16 *hdr_off)
38 {
39 unsigned int ptp_class;
40 struct ptp_header *hdr;
41
42 ptp_class = ptp_classify_raw(skb);
43
44 switch (ptp_class & PTP_CLASS_VMASK) {
45 case PTP_CLASS_V1:
46 case PTP_CLASS_V2:
47 hdr = ptp_parse_header(skb, ptp_class);
48 if (!hdr)
49 return -EINVAL;
50
51 *hdr_off = (u8 *)hdr - skb->data;
52 *seq_id = ntohs(hdr->sequence_id);
53 return 0;
54 default:
55 return -ERANGE;
56 }
57 }
58
bnxt_ptp_settime(struct ptp_clock_info * ptp_info,const struct timespec64 * ts)59 static int bnxt_ptp_settime(struct ptp_clock_info *ptp_info,
60 const struct timespec64 *ts)
61 {
62 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
63 ptp_info);
64 u64 ns = timespec64_to_ns(ts);
65 unsigned long flags;
66
67 if (BNXT_PTP_USE_RTC(ptp->bp))
68 return bnxt_ptp_cfg_settime(ptp->bp, ns);
69
70 write_seqlock_irqsave(&ptp->ptp_lock, flags);
71 timecounter_init(&ptp->tc, &ptp->cc, ns);
72 write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
73 return 0;
74 }
75
76 /* Caller holds ptp_lock */
__bnxt_refclk_read(struct bnxt * bp,struct ptp_system_timestamp * sts,u64 * ns)77 static int __bnxt_refclk_read(struct bnxt *bp, struct ptp_system_timestamp *sts,
78 u64 *ns)
79 {
80 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
81 u32 high_before, high_now, low;
82
83 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
84 return -EIO;
85
86 high_before = readl(bp->bar0 + ptp->refclk_mapped_regs[1]);
87 ptp_read_system_prets(sts);
88 low = readl(bp->bar0 + ptp->refclk_mapped_regs[0]);
89 ptp_read_system_postts(sts);
90 high_now = readl(bp->bar0 + ptp->refclk_mapped_regs[1]);
91 if (high_now != high_before) {
92 ptp_read_system_prets(sts);
93 low = readl(bp->bar0 + ptp->refclk_mapped_regs[0]);
94 ptp_read_system_postts(sts);
95 }
96 *ns = ((u64)high_now << 32) | low;
97
98 return 0;
99 }
100
bnxt_refclk_read(struct bnxt * bp,struct ptp_system_timestamp * sts,u64 * ns)101 static int bnxt_refclk_read(struct bnxt *bp, struct ptp_system_timestamp *sts,
102 u64 *ns)
103 {
104 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
105 unsigned long flags;
106 int rc;
107
108 /* We have to serialize reg access and FW reset */
109 read_seqlock_excl_irqsave(&ptp->ptp_lock, flags);
110 rc = __bnxt_refclk_read(bp, sts, ns);
111 read_sequnlock_excl_irqrestore(&ptp->ptp_lock, flags);
112 return rc;
113 }
114
bnxt_refclk_read_low(struct bnxt * bp,struct ptp_system_timestamp * sts,u32 * low)115 static int bnxt_refclk_read_low(struct bnxt *bp, struct ptp_system_timestamp *sts,
116 u32 *low)
117 {
118 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
119 unsigned long flags;
120
121 /* We have to serialize reg access and FW reset */
122 read_seqlock_excl_irqsave(&ptp->ptp_lock, flags);
123
124 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
125 read_sequnlock_excl_irqrestore(&ptp->ptp_lock, flags);
126 return -EIO;
127 }
128
129 ptp_read_system_prets(sts);
130 *low = readl(bp->bar0 + ptp->refclk_mapped_regs[0]);
131 ptp_read_system_postts(sts);
132
133 read_sequnlock_excl_irqrestore(&ptp->ptp_lock, flags);
134 return 0;
135 }
136
bnxt_ptp_get_current_time(struct bnxt * bp)137 static void bnxt_ptp_get_current_time(struct bnxt *bp)
138 {
139 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
140
141 if (!ptp)
142 return;
143 WRITE_ONCE(ptp->old_time, ptp->current_time >> BNXT_HI_TIMER_SHIFT);
144 bnxt_refclk_read(bp, NULL, &ptp->current_time);
145 }
146
bnxt_hwrm_port_ts_query(struct bnxt * bp,u32 flags,u64 * ts,u32 txts_tmo,int slot)147 static int bnxt_hwrm_port_ts_query(struct bnxt *bp, u32 flags, u64 *ts,
148 u32 txts_tmo, int slot)
149 {
150 struct hwrm_port_ts_query_output *resp;
151 struct hwrm_port_ts_query_input *req;
152 int rc;
153
154 rc = hwrm_req_init(bp, req, HWRM_PORT_TS_QUERY);
155 if (rc)
156 return rc;
157
158 req->flags = cpu_to_le32(flags);
159 if ((flags & PORT_TS_QUERY_REQ_FLAGS_PATH) ==
160 PORT_TS_QUERY_REQ_FLAGS_PATH_TX) {
161 struct bnxt_ptp_tx_req *txts_req = &bp->ptp_cfg->txts_req[slot];
162 u32 tmo_us = txts_tmo * 1000;
163
164 req->enables = cpu_to_le16(BNXT_PTP_QTS_TX_ENABLES);
165 req->ptp_seq_id = cpu_to_le32(txts_req->tx_seqid);
166 req->ptp_hdr_offset = cpu_to_le16(txts_req->tx_hdr_off);
167 if (!tmo_us)
168 tmo_us = BNXT_PTP_QTS_TIMEOUT;
169 tmo_us = min(tmo_us, BNXT_PTP_QTS_MAX_TMO_US);
170 req->ts_req_timeout = cpu_to_le16(tmo_us);
171 }
172 resp = hwrm_req_hold(bp, req);
173
174 rc = hwrm_req_send_silent(bp, req);
175 if (!rc)
176 *ts = le64_to_cpu(resp->ptp_msg_ts);
177 hwrm_req_drop(bp, req);
178 return rc;
179 }
180
bnxt_ptp_gettimex(struct ptp_clock_info * ptp_info,struct timespec64 * ts,struct ptp_system_timestamp * sts)181 static int bnxt_ptp_gettimex(struct ptp_clock_info *ptp_info,
182 struct timespec64 *ts,
183 struct ptp_system_timestamp *sts)
184 {
185 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
186 ptp_info);
187 u64 ns, cycles;
188 u32 low;
189 int rc;
190
191 rc = bnxt_refclk_read_low(ptp->bp, sts, &low);
192 if (rc)
193 return rc;
194
195 cycles = bnxt_extend_cycles_32b_to_48b(ptp, low);
196 ns = bnxt_timecounter_cyc2time(ptp, cycles);
197 *ts = ns_to_timespec64(ns);
198
199 return 0;
200 }
201
bnxt_ptp_update_current_time(struct bnxt * bp)202 void bnxt_ptp_update_current_time(struct bnxt *bp)
203 {
204 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
205
206 bnxt_refclk_read(ptp->bp, NULL, &ptp->current_time);
207 WRITE_ONCE(ptp->old_time, ptp->current_time >> BNXT_HI_TIMER_SHIFT);
208 }
209
bnxt_ptp_adjphc(struct bnxt_ptp_cfg * ptp,s64 delta)210 static int bnxt_ptp_adjphc(struct bnxt_ptp_cfg *ptp, s64 delta)
211 {
212 struct hwrm_port_mac_cfg_input *req;
213 int rc;
214
215 rc = hwrm_req_init(ptp->bp, req, HWRM_PORT_MAC_CFG);
216 if (rc)
217 return rc;
218
219 req->enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_PTP_ADJ_PHASE);
220 req->ptp_adj_phase = cpu_to_le64(delta);
221
222 rc = hwrm_req_send(ptp->bp, req);
223 if (rc) {
224 netdev_err(ptp->bp->dev, "ptp adjphc failed. rc = %x\n", rc);
225 } else {
226 bnxt_ptp_update_current_time(ptp->bp);
227 }
228
229 return rc;
230 }
231
bnxt_ptp_adjtime(struct ptp_clock_info * ptp_info,s64 delta)232 static int bnxt_ptp_adjtime(struct ptp_clock_info *ptp_info, s64 delta)
233 {
234 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
235 ptp_info);
236 unsigned long flags;
237
238 if (BNXT_PTP_USE_RTC(ptp->bp))
239 return bnxt_ptp_adjphc(ptp, delta);
240
241 write_seqlock_irqsave(&ptp->ptp_lock, flags);
242 timecounter_adjtime(&ptp->tc, delta);
243 write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
244 return 0;
245 }
246
bnxt_ptp_adjfine_rtc(struct bnxt * bp,long scaled_ppm)247 static int bnxt_ptp_adjfine_rtc(struct bnxt *bp, long scaled_ppm)
248 {
249 s32 ppb = scaled_ppm_to_ppb(scaled_ppm);
250 struct hwrm_port_mac_cfg_input *req;
251 int rc;
252
253 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_CFG);
254 if (rc)
255 return rc;
256
257 req->ptp_freq_adj_ppb = cpu_to_le32(ppb);
258 req->enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_PTP_FREQ_ADJ_PPB);
259 rc = hwrm_req_send(bp, req);
260 if (rc)
261 netdev_err(bp->dev,
262 "ptp adjfine failed. rc = %d\n", rc);
263 return rc;
264 }
265
bnxt_ptp_adjfine(struct ptp_clock_info * ptp_info,long scaled_ppm)266 static int bnxt_ptp_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
267 {
268 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
269 ptp_info);
270 struct bnxt *bp = ptp->bp;
271 unsigned long flags;
272
273 if (!BNXT_MH(bp))
274 return bnxt_ptp_adjfine_rtc(bp, scaled_ppm);
275
276 write_seqlock_irqsave(&ptp->ptp_lock, flags);
277 timecounter_read(&ptp->tc);
278 ptp->cc.mult = adjust_by_scaled_ppm(ptp->cmult, scaled_ppm);
279 write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
280 return 0;
281 }
282
bnxt_ptp_pps_event(struct bnxt * bp,u32 data1,u32 data2)283 void bnxt_ptp_pps_event(struct bnxt *bp, u32 data1, u32 data2)
284 {
285 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
286 struct ptp_clock_event event;
287 u64 ns, pps_ts;
288
289 pps_ts = EVENT_PPS_TS(data2, data1);
290 ns = bnxt_timecounter_cyc2time(ptp, pps_ts);
291
292 switch (EVENT_DATA2_PPS_EVENT_TYPE(data2)) {
293 case ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA2_EVENT_TYPE_INTERNAL:
294 event.pps_times.ts_real = ns_to_timespec64(ns);
295 event.type = PTP_CLOCK_PPSUSR;
296 event.index = EVENT_DATA2_PPS_PIN_NUM(data2);
297 break;
298 case ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA2_EVENT_TYPE_EXTERNAL:
299 event.timestamp = ns;
300 event.type = PTP_CLOCK_EXTTS;
301 event.index = EVENT_DATA2_PPS_PIN_NUM(data2);
302 break;
303 }
304
305 ptp_clock_event(bp->ptp_cfg->ptp_clock, &event);
306 }
307
bnxt_ptp_cfg_pin(struct bnxt * bp,u8 pin,u8 usage)308 static int bnxt_ptp_cfg_pin(struct bnxt *bp, u8 pin, u8 usage)
309 {
310 struct hwrm_func_ptp_pin_cfg_input *req;
311 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
312 u8 state = usage != BNXT_PPS_PIN_NONE;
313 u8 *pin_state, *pin_usg;
314 u32 enables;
315 int rc;
316
317 if (!TSIO_PIN_VALID(pin)) {
318 netdev_err(ptp->bp->dev, "1PPS: Invalid pin. Check pin-function configuration\n");
319 return -EOPNOTSUPP;
320 }
321
322 rc = hwrm_req_init(ptp->bp, req, HWRM_FUNC_PTP_PIN_CFG);
323 if (rc)
324 return rc;
325
326 enables = (FUNC_PTP_PIN_CFG_REQ_ENABLES_PIN0_STATE |
327 FUNC_PTP_PIN_CFG_REQ_ENABLES_PIN0_USAGE) << (pin * 2);
328 req->enables = cpu_to_le32(enables);
329
330 pin_state = &req->pin0_state;
331 pin_usg = &req->pin0_usage;
332
333 *(pin_state + (pin * 2)) = state;
334 *(pin_usg + (pin * 2)) = usage;
335
336 rc = hwrm_req_send(ptp->bp, req);
337 if (rc)
338 return rc;
339
340 ptp->pps_info.pins[pin].usage = usage;
341 ptp->pps_info.pins[pin].state = state;
342
343 return 0;
344 }
345
bnxt_ptp_cfg_event(struct bnxt * bp,u8 event)346 static int bnxt_ptp_cfg_event(struct bnxt *bp, u8 event)
347 {
348 struct hwrm_func_ptp_cfg_input *req;
349 int rc;
350
351 rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_CFG);
352 if (rc)
353 return rc;
354
355 req->enables = cpu_to_le16(FUNC_PTP_CFG_REQ_ENABLES_PTP_PPS_EVENT);
356 req->ptp_pps_event = event;
357 return hwrm_req_send(bp, req);
358 }
359
bnxt_ptp_cfg_tstamp_filters(struct bnxt * bp)360 int bnxt_ptp_cfg_tstamp_filters(struct bnxt *bp)
361 {
362 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
363 struct hwrm_port_mac_cfg_input *req;
364 int rc;
365
366 if (!ptp || !ptp->tstamp_filters)
367 return -EIO;
368
369 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_CFG);
370 if (rc)
371 goto out;
372
373 if (!(bp->fw_cap & BNXT_FW_CAP_RX_ALL_PKT_TS) && (ptp->tstamp_filters &
374 (PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_ENABLE |
375 PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_DISABLE))) {
376 ptp->tstamp_filters &= ~(PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_ENABLE |
377 PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_DISABLE);
378 netdev_warn(bp->dev, "Unsupported FW for all RX pkts timestamp filter\n");
379 }
380
381 req->flags = cpu_to_le32(ptp->tstamp_filters);
382 req->enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_RX_TS_CAPTURE_PTP_MSG_TYPE);
383 req->rx_ts_capture_ptp_msg_type = cpu_to_le16(ptp->rxctl);
384
385 rc = hwrm_req_send(bp, req);
386 if (!rc) {
387 bp->ptp_all_rx_tstamp = !!(ptp->tstamp_filters &
388 PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_ENABLE);
389 return 0;
390 }
391 ptp->tstamp_filters = 0;
392 out:
393 bp->ptp_all_rx_tstamp = 0;
394 netdev_warn(bp->dev, "Failed to configure HW packet timestamp filters\n");
395 return rc;
396 }
397
bnxt_ptp_reapply_pps(struct bnxt * bp)398 void bnxt_ptp_reapply_pps(struct bnxt *bp)
399 {
400 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
401 struct bnxt_pps *pps;
402 u32 pin = 0;
403 int rc;
404
405 if (!ptp || !(bp->fw_cap & BNXT_FW_CAP_PTP_PPS) ||
406 !(ptp->ptp_info.pin_config))
407 return;
408 pps = &ptp->pps_info;
409 for (pin = 0; pin < BNXT_MAX_TSIO_PINS; pin++) {
410 if (pps->pins[pin].state) {
411 rc = bnxt_ptp_cfg_pin(bp, pin, pps->pins[pin].usage);
412 if (!rc && pps->pins[pin].event)
413 rc = bnxt_ptp_cfg_event(bp,
414 pps->pins[pin].event);
415 if (rc)
416 netdev_err(bp->dev, "1PPS: Failed to configure pin%d\n",
417 pin);
418 }
419 }
420 }
421
bnxt_ptp_perout_cfg(struct bnxt_ptp_cfg * ptp,struct ptp_clock_request * rq)422 static int bnxt_ptp_perout_cfg(struct bnxt_ptp_cfg *ptp,
423 struct ptp_clock_request *rq)
424 {
425 struct hwrm_func_ptp_cfg_input *req;
426 struct bnxt *bp = ptp->bp;
427 struct timespec64 ts;
428 u64 target_ns;
429 u16 enables;
430 int rc;
431
432 ts.tv_sec = rq->perout.start.sec;
433 ts.tv_nsec = rq->perout.start.nsec;
434 target_ns = timespec64_to_ns(&ts);
435
436 rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_CFG);
437 if (rc)
438 return rc;
439
440 enables = FUNC_PTP_CFG_REQ_ENABLES_PTP_FREQ_ADJ_EXT_PERIOD |
441 FUNC_PTP_CFG_REQ_ENABLES_PTP_FREQ_ADJ_EXT_UP |
442 FUNC_PTP_CFG_REQ_ENABLES_PTP_FREQ_ADJ_EXT_PHASE;
443 req->enables = cpu_to_le16(enables);
444 req->ptp_pps_event = 0;
445 req->ptp_freq_adj_dll_source = 0;
446 req->ptp_freq_adj_dll_phase = 0;
447 req->ptp_freq_adj_ext_period = cpu_to_le32(NSEC_PER_SEC);
448 req->ptp_freq_adj_ext_up = 0;
449 req->ptp_freq_adj_ext_phase_lower =
450 cpu_to_le32(lower_32_bits(target_ns));
451 req->ptp_freq_adj_ext_phase_upper =
452 cpu_to_le32(upper_32_bits(target_ns));
453
454 return hwrm_req_send(bp, req);
455 }
456
bnxt_ptp_enable(struct ptp_clock_info * ptp_info,struct ptp_clock_request * rq,int on)457 static int bnxt_ptp_enable(struct ptp_clock_info *ptp_info,
458 struct ptp_clock_request *rq, int on)
459 {
460 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
461 ptp_info);
462 struct bnxt *bp = ptp->bp;
463 int pin_id;
464 int rc;
465
466 switch (rq->type) {
467 case PTP_CLK_REQ_EXTTS:
468 /* Configure an External PPS IN */
469 pin_id = ptp_find_pin(ptp->ptp_clock, PTP_PF_EXTTS,
470 rq->extts.index);
471 if (!TSIO_PIN_VALID(pin_id))
472 return -EOPNOTSUPP;
473 if (!on)
474 break;
475 rc = bnxt_ptp_cfg_pin(bp, pin_id, BNXT_PPS_PIN_PPS_IN);
476 if (rc)
477 return rc;
478 rc = bnxt_ptp_cfg_event(bp, BNXT_PPS_EVENT_EXTERNAL);
479 if (!rc)
480 ptp->pps_info.pins[pin_id].event = BNXT_PPS_EVENT_EXTERNAL;
481 return rc;
482 case PTP_CLK_REQ_PEROUT:
483 /* Configure a Periodic PPS OUT */
484 pin_id = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT,
485 rq->perout.index);
486 if (!TSIO_PIN_VALID(pin_id))
487 return -EOPNOTSUPP;
488 if (!on)
489 break;
490
491 rc = bnxt_ptp_cfg_pin(bp, pin_id, BNXT_PPS_PIN_PPS_OUT);
492 if (!rc)
493 rc = bnxt_ptp_perout_cfg(ptp, rq);
494
495 return rc;
496 case PTP_CLK_REQ_PPS:
497 /* Configure PHC PPS IN */
498 rc = bnxt_ptp_cfg_pin(bp, 0, BNXT_PPS_PIN_PPS_IN);
499 if (rc)
500 return rc;
501 rc = bnxt_ptp_cfg_event(bp, BNXT_PPS_EVENT_INTERNAL);
502 if (!rc)
503 ptp->pps_info.pins[0].event = BNXT_PPS_EVENT_INTERNAL;
504 return rc;
505 default:
506 netdev_err(ptp->bp->dev, "Unrecognized PIN function\n");
507 return -EOPNOTSUPP;
508 }
509
510 return bnxt_ptp_cfg_pin(bp, pin_id, BNXT_PPS_PIN_NONE);
511 }
512
bnxt_hwrm_ptp_cfg(struct bnxt * bp)513 static int bnxt_hwrm_ptp_cfg(struct bnxt *bp)
514 {
515 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
516 u32 flags = 0;
517
518 switch (ptp->rx_filter) {
519 case HWTSTAMP_FILTER_ALL:
520 flags = PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_ENABLE;
521 break;
522 case HWTSTAMP_FILTER_NONE:
523 flags = PORT_MAC_CFG_REQ_FLAGS_PTP_RX_TS_CAPTURE_DISABLE;
524 if (bp->fw_cap & BNXT_FW_CAP_RX_ALL_PKT_TS)
525 flags |= PORT_MAC_CFG_REQ_FLAGS_ALL_RX_TS_CAPTURE_DISABLE;
526 break;
527 case HWTSTAMP_FILTER_PTP_V2_EVENT:
528 case HWTSTAMP_FILTER_PTP_V2_SYNC:
529 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
530 flags = PORT_MAC_CFG_REQ_FLAGS_PTP_RX_TS_CAPTURE_ENABLE;
531 break;
532 }
533
534 if (ptp->tx_tstamp_en)
535 flags |= PORT_MAC_CFG_REQ_FLAGS_PTP_TX_TS_CAPTURE_ENABLE;
536 else
537 flags |= PORT_MAC_CFG_REQ_FLAGS_PTP_TX_TS_CAPTURE_DISABLE;
538
539 ptp->tstamp_filters = flags;
540
541 return bnxt_ptp_cfg_tstamp_filters(bp);
542 }
543
bnxt_hwtstamp_set(struct net_device * dev,struct kernel_hwtstamp_config * stmpconf,struct netlink_ext_ack * extack)544 int bnxt_hwtstamp_set(struct net_device *dev,
545 struct kernel_hwtstamp_config *stmpconf,
546 struct netlink_ext_ack *extack)
547 {
548 struct bnxt *bp = netdev_priv(dev);
549 struct bnxt_ptp_cfg *ptp;
550 u16 old_rxctl;
551 int old_rx_filter, rc;
552 u8 old_tx_tstamp_en;
553
554 ptp = bp->ptp_cfg;
555 if (!ptp)
556 return -EOPNOTSUPP;
557
558 if (stmpconf->tx_type != HWTSTAMP_TX_ON &&
559 stmpconf->tx_type != HWTSTAMP_TX_OFF)
560 return -ERANGE;
561
562 old_rx_filter = ptp->rx_filter;
563 old_rxctl = ptp->rxctl;
564 old_tx_tstamp_en = ptp->tx_tstamp_en;
565 switch (stmpconf->rx_filter) {
566 case HWTSTAMP_FILTER_NONE:
567 ptp->rxctl = 0;
568 ptp->rx_filter = HWTSTAMP_FILTER_NONE;
569 break;
570 case HWTSTAMP_FILTER_ALL:
571 if (bp->fw_cap & BNXT_FW_CAP_RX_ALL_PKT_TS) {
572 ptp->rx_filter = HWTSTAMP_FILTER_ALL;
573 break;
574 }
575 return -EOPNOTSUPP;
576 case HWTSTAMP_FILTER_PTP_V2_EVENT:
577 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
578 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
579 ptp->rxctl = BNXT_PTP_MSG_EVENTS;
580 ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
581 break;
582 case HWTSTAMP_FILTER_PTP_V2_SYNC:
583 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
584 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
585 ptp->rxctl = BNXT_PTP_MSG_SYNC;
586 ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
587 break;
588 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
589 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
590 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
591 ptp->rxctl = BNXT_PTP_MSG_DELAY_REQ;
592 ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
593 break;
594 default:
595 return -ERANGE;
596 }
597
598 if (stmpconf->tx_type == HWTSTAMP_TX_ON)
599 ptp->tx_tstamp_en = 1;
600 else
601 ptp->tx_tstamp_en = 0;
602
603 rc = bnxt_hwrm_ptp_cfg(bp);
604 if (rc)
605 goto ts_set_err;
606
607 stmpconf->rx_filter = ptp->rx_filter;
608 return 0;
609
610 ts_set_err:
611 ptp->rx_filter = old_rx_filter;
612 ptp->rxctl = old_rxctl;
613 ptp->tx_tstamp_en = old_tx_tstamp_en;
614 return rc;
615 }
616
bnxt_hwtstamp_get(struct net_device * dev,struct kernel_hwtstamp_config * stmpconf)617 int bnxt_hwtstamp_get(struct net_device *dev,
618 struct kernel_hwtstamp_config *stmpconf)
619 {
620 struct bnxt *bp = netdev_priv(dev);
621 struct bnxt_ptp_cfg *ptp;
622
623 ptp = bp->ptp_cfg;
624 if (!ptp)
625 return -EOPNOTSUPP;
626
627 stmpconf->flags = 0;
628 stmpconf->tx_type = ptp->tx_tstamp_en ? HWTSTAMP_TX_ON
629 : HWTSTAMP_TX_OFF;
630
631 stmpconf->rx_filter = ptp->rx_filter;
632 return 0;
633 }
634
bnxt_map_regs(struct bnxt * bp,u32 * reg_arr,int count,int reg_win)635 static int bnxt_map_regs(struct bnxt *bp, u32 *reg_arr, int count, int reg_win)
636 {
637 u32 reg_base = *reg_arr & BNXT_GRC_BASE_MASK;
638 u32 win_off;
639 int i;
640
641 for (i = 0; i < count; i++) {
642 if ((reg_arr[i] & BNXT_GRC_BASE_MASK) != reg_base)
643 return -ERANGE;
644 }
645 win_off = BNXT_GRCPF_REG_WINDOW_BASE_OUT + (reg_win - 1) * 4;
646 writel(reg_base, bp->bar0 + win_off);
647 return 0;
648 }
649
bnxt_map_ptp_regs(struct bnxt * bp)650 static int bnxt_map_ptp_regs(struct bnxt *bp)
651 {
652 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
653 u32 *reg_arr;
654 int rc, i;
655
656 reg_arr = ptp->refclk_regs;
657 if (BNXT_CHIP_P5(bp)) {
658 rc = bnxt_map_regs(bp, reg_arr, 2, BNXT_PTP_GRC_WIN);
659 if (rc)
660 return rc;
661 for (i = 0; i < 2; i++)
662 ptp->refclk_mapped_regs[i] = BNXT_PTP_GRC_WIN_BASE +
663 (ptp->refclk_regs[i] & BNXT_GRC_OFFSET_MASK);
664 return 0;
665 }
666 if (bp->flags & BNXT_FLAG_CHIP_P7) {
667 for (i = 0; i < 2; i++) {
668 if (reg_arr[i] & BNXT_GRC_BASE_MASK)
669 return -EINVAL;
670 ptp->refclk_mapped_regs[i] = reg_arr[i];
671 }
672 return 0;
673 }
674 return -ENODEV;
675 }
676
bnxt_unmap_ptp_regs(struct bnxt * bp)677 static void bnxt_unmap_ptp_regs(struct bnxt *bp)
678 {
679 writel(0, bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT +
680 (BNXT_PTP_GRC_WIN - 1) * 4);
681 }
682
bnxt_cc_read(struct cyclecounter * cc)683 static u64 bnxt_cc_read(struct cyclecounter *cc)
684 {
685 struct bnxt_ptp_cfg *ptp = container_of(cc, struct bnxt_ptp_cfg, cc);
686 u64 ns = 0;
687
688 __bnxt_refclk_read(ptp->bp, NULL, &ns);
689 return ns;
690 }
691
bnxt_stamp_tx_skb(struct bnxt * bp,int slot)692 static int bnxt_stamp_tx_skb(struct bnxt *bp, int slot)
693 {
694 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
695 struct skb_shared_hwtstamps timestamp;
696 struct bnxt_ptp_tx_req *txts_req;
697 unsigned long now = jiffies;
698 u64 ts = 0, ns = 0;
699 u32 tmo = 0;
700 int rc;
701
702 txts_req = &ptp->txts_req[slot];
703 /* make sure bnxt_get_tx_ts_p5() has updated abs_txts_tmo */
704 smp_rmb();
705 if (!time_after_eq(now, txts_req->abs_txts_tmo))
706 tmo = jiffies_to_msecs(txts_req->abs_txts_tmo - now);
707 rc = bnxt_hwrm_port_ts_query(bp, PORT_TS_QUERY_REQ_FLAGS_PATH_TX, &ts,
708 tmo, slot);
709 if (!rc) {
710 memset(×tamp, 0, sizeof(timestamp));
711 ns = bnxt_timecounter_cyc2time(ptp, ts);
712 timestamp.hwtstamp = ns_to_ktime(ns);
713 skb_tstamp_tx(txts_req->tx_skb, ×tamp);
714 ptp->stats.ts_pkts++;
715 } else {
716 if (!time_after_eq(jiffies, txts_req->abs_txts_tmo))
717 return -EAGAIN;
718
719 ptp->stats.ts_lost++;
720 netdev_warn_once(bp->dev,
721 "TS query for TX timer failed rc = %x\n", rc);
722 }
723
724 dev_kfree_skb_any(txts_req->tx_skb);
725 txts_req->tx_skb = NULL;
726
727 return 0;
728 }
729
bnxt_ptp_ts_aux_work(struct ptp_clock_info * ptp_info)730 static long bnxt_ptp_ts_aux_work(struct ptp_clock_info *ptp_info)
731 {
732 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
733 ptp_info);
734 unsigned long now = jiffies;
735 struct bnxt *bp = ptp->bp;
736 u16 cons = ptp->txts_cons;
737 unsigned long flags;
738 u32 num_requests;
739 int rc = 0;
740
741 num_requests = BNXT_MAX_TX_TS - READ_ONCE(ptp->tx_avail);
742 while (num_requests--) {
743 if (IS_ERR(ptp->txts_req[cons].tx_skb))
744 goto next_slot;
745 if (!ptp->txts_req[cons].tx_skb)
746 break;
747 rc = bnxt_stamp_tx_skb(bp, cons);
748 if (rc == -EAGAIN)
749 break;
750 next_slot:
751 BNXT_PTP_INC_TX_AVAIL(ptp);
752 cons = NEXT_TXTS(cons);
753 }
754 ptp->txts_cons = cons;
755
756 if (!time_after_eq(now, ptp->next_period)) {
757 if (rc == -EAGAIN)
758 return 0;
759 return ptp->next_period - now;
760 }
761
762 bnxt_ptp_get_current_time(bp);
763 ptp->next_period = now + HZ;
764 if (time_after_eq(now, ptp->next_overflow_check)) {
765 write_seqlock_irqsave(&ptp->ptp_lock, flags);
766 timecounter_read(&ptp->tc);
767 write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
768 ptp->next_overflow_check = now + BNXT_PHC_OVERFLOW_PERIOD;
769 }
770 if (rc == -EAGAIN)
771 return 0;
772 return HZ;
773 }
774
bnxt_ptp_free_txts_skbs(struct bnxt_ptp_cfg * ptp)775 void bnxt_ptp_free_txts_skbs(struct bnxt_ptp_cfg *ptp)
776 {
777 struct bnxt_ptp_tx_req *txts_req;
778 u16 cons = ptp->txts_cons;
779
780 /* make sure ptp aux worker finished with
781 * possible BNXT_STATE_OPEN set
782 */
783 ptp_cancel_worker_sync(ptp->ptp_clock);
784
785 ptp->tx_avail = BNXT_MAX_TX_TS;
786 while (cons != ptp->txts_prod) {
787 txts_req = &ptp->txts_req[cons];
788 if (!IS_ERR_OR_NULL(txts_req->tx_skb))
789 dev_kfree_skb_any(txts_req->tx_skb);
790 cons = NEXT_TXTS(cons);
791 }
792 ptp->txts_cons = cons;
793 ptp_schedule_worker(ptp->ptp_clock, 0);
794 }
795
bnxt_ptp_get_txts_prod(struct bnxt_ptp_cfg * ptp,u16 * prod)796 int bnxt_ptp_get_txts_prod(struct bnxt_ptp_cfg *ptp, u16 *prod)
797 {
798 spin_lock_bh(&ptp->ptp_tx_lock);
799 if (ptp->tx_avail) {
800 *prod = ptp->txts_prod;
801 ptp->txts_prod = NEXT_TXTS(*prod);
802 ptp->tx_avail--;
803 spin_unlock_bh(&ptp->ptp_tx_lock);
804 return 0;
805 }
806 spin_unlock_bh(&ptp->ptp_tx_lock);
807 atomic64_inc(&ptp->stats.ts_err);
808 return -ENOSPC;
809 }
810
bnxt_get_tx_ts_p5(struct bnxt * bp,struct sk_buff * skb,u16 prod)811 void bnxt_get_tx_ts_p5(struct bnxt *bp, struct sk_buff *skb, u16 prod)
812 {
813 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
814 struct bnxt_ptp_tx_req *txts_req;
815
816 txts_req = &ptp->txts_req[prod];
817 txts_req->abs_txts_tmo = jiffies + msecs_to_jiffies(ptp->txts_tmo);
818 /* make sure abs_txts_tmo is written first */
819 smp_wmb();
820 txts_req->tx_skb = skb;
821 ptp_schedule_worker(ptp->ptp_clock, 0);
822 }
823
bnxt_get_rx_ts_p5(struct bnxt * bp,u64 * ts,u32 pkt_ts)824 int bnxt_get_rx_ts_p5(struct bnxt *bp, u64 *ts, u32 pkt_ts)
825 {
826 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
827
828 if (!ptp)
829 return -ENODEV;
830
831 *ts = bnxt_extend_cycles_32b_to_48b(ptp, pkt_ts);
832
833 return 0;
834 }
835
bnxt_tx_ts_cmp(struct bnxt * bp,struct bnxt_napi * bnapi,struct tx_ts_cmp * tscmp)836 void bnxt_tx_ts_cmp(struct bnxt *bp, struct bnxt_napi *bnapi,
837 struct tx_ts_cmp *tscmp)
838 {
839 struct skb_shared_hwtstamps timestamp = {};
840 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
841 u32 opaque = tscmp->tx_ts_cmp_opaque;
842 struct bnxt_tx_ring_info *txr;
843 struct bnxt_sw_tx_bd *tx_buf;
844 u64 ts, ns;
845 u16 cons;
846
847 txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
848 ts = BNXT_GET_TX_TS_48B_NS(tscmp);
849 cons = TX_OPAQUE_IDX(opaque);
850 tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
851 if (tx_buf->is_ts_pkt) {
852 if (BNXT_TX_TS_ERR(tscmp)) {
853 netdev_err(bp->dev,
854 "timestamp completion error 0x%x 0x%x\n",
855 le32_to_cpu(tscmp->tx_ts_cmp_flags_type),
856 le32_to_cpu(tscmp->tx_ts_cmp_errors_v));
857 } else {
858 ns = bnxt_timecounter_cyc2time(ptp, ts);
859 timestamp.hwtstamp = ns_to_ktime(ns);
860 skb_tstamp_tx(tx_buf->skb, ×tamp);
861 }
862 tx_buf->is_ts_pkt = 0;
863 }
864 }
865
866 #ifdef CONFIG_X86
bnxt_phc_get_syncdevicetime(ktime_t * device,struct system_counterval_t * system,void * ctx)867 static int bnxt_phc_get_syncdevicetime(ktime_t *device,
868 struct system_counterval_t *system,
869 void *ctx)
870 {
871 struct bnxt_ptp_cfg *ptp = (struct bnxt_ptp_cfg *)ctx;
872 struct hwrm_func_ptp_ts_query_output *resp;
873 struct hwrm_func_ptp_ts_query_input *req;
874 struct bnxt *bp = ptp->bp;
875 u64 ptm_local_ts;
876 int rc;
877
878 rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_TS_QUERY);
879 if (rc)
880 return rc;
881 req->flags = cpu_to_le32(FUNC_PTP_TS_QUERY_REQ_FLAGS_PTM_TIME);
882 resp = hwrm_req_hold(bp, req);
883 rc = hwrm_req_send(bp, req);
884 if (rc) {
885 hwrm_req_drop(bp, req);
886 return rc;
887 }
888 ptm_local_ts = le64_to_cpu(resp->ptm_local_ts);
889 *device = ns_to_ktime(bnxt_timecounter_cyc2time(ptp, ptm_local_ts));
890 /* ptm_system_ts is 64-bit */
891 system->cycles = le64_to_cpu(resp->ptm_system_ts);
892 system->cs_id = CSID_X86_ART;
893 system->use_nsecs = true;
894
895 hwrm_req_drop(bp, req);
896
897 return 0;
898 }
899
bnxt_ptp_getcrosststamp(struct ptp_clock_info * ptp_info,struct system_device_crosststamp * xtstamp)900 static int bnxt_ptp_getcrosststamp(struct ptp_clock_info *ptp_info,
901 struct system_device_crosststamp *xtstamp)
902 {
903 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
904 ptp_info);
905
906 return get_device_system_crosststamp(bnxt_phc_get_syncdevicetime,
907 ptp, NULL, xtstamp);
908 }
909 #endif /* CONFIG_X86 */
910
911 static const struct ptp_clock_info bnxt_ptp_caps = {
912 .owner = THIS_MODULE,
913 .name = "bnxt clock",
914 .max_adj = BNXT_MAX_PHC_DRIFT,
915 .n_alarm = 0,
916 .n_ext_ts = 0,
917 .n_per_out = 0,
918 .n_pins = 0,
919 .pps = 0,
920 .adjfine = bnxt_ptp_adjfine,
921 .adjtime = bnxt_ptp_adjtime,
922 .do_aux_work = bnxt_ptp_ts_aux_work,
923 .gettimex64 = bnxt_ptp_gettimex,
924 .settime64 = bnxt_ptp_settime,
925 .enable = bnxt_ptp_enable,
926 };
927
bnxt_ptp_verify(struct ptp_clock_info * ptp_info,unsigned int pin,enum ptp_pin_function func,unsigned int chan)928 static int bnxt_ptp_verify(struct ptp_clock_info *ptp_info, unsigned int pin,
929 enum ptp_pin_function func, unsigned int chan)
930 {
931 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg,
932 ptp_info);
933 /* Allow only PPS pin function configuration */
934 if (ptp->pps_info.pins[pin].usage <= BNXT_PPS_PIN_PPS_OUT &&
935 func != PTP_PF_PHYSYNC)
936 return 0;
937 else
938 return -EOPNOTSUPP;
939 }
940
bnxt_ptp_pps_init(struct bnxt * bp)941 static int bnxt_ptp_pps_init(struct bnxt *bp)
942 {
943 struct hwrm_func_ptp_pin_qcfg_output *resp;
944 struct hwrm_func_ptp_pin_qcfg_input *req;
945 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
946 struct ptp_clock_info *ptp_info;
947 struct bnxt_pps *pps_info;
948 u8 *pin_usg;
949 u32 i, rc;
950
951 /* Query current/default PIN CFG */
952 rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_PIN_QCFG);
953 if (rc)
954 return rc;
955
956 resp = hwrm_req_hold(bp, req);
957 rc = hwrm_req_send(bp, req);
958 if (rc || !resp->num_pins) {
959 hwrm_req_drop(bp, req);
960 return -EOPNOTSUPP;
961 }
962
963 ptp_info = &ptp->ptp_info;
964 pps_info = &ptp->pps_info;
965 pps_info->num_pins = resp->num_pins;
966 ptp_info->n_pins = pps_info->num_pins;
967 ptp_info->pin_config = kzalloc_objs(*ptp_info->pin_config,
968 ptp_info->n_pins);
969 if (!ptp_info->pin_config) {
970 hwrm_req_drop(bp, req);
971 return -ENOMEM;
972 }
973
974 /* Report the TSIO capability to kernel */
975 pin_usg = &resp->pin0_usage;
976 for (i = 0; i < pps_info->num_pins; i++, pin_usg++) {
977 snprintf(ptp_info->pin_config[i].name,
978 sizeof(ptp_info->pin_config[i].name), "bnxt_pps%d", i);
979 ptp_info->pin_config[i].index = i;
980 if (*pin_usg == BNXT_PPS_PIN_PPS_IN)
981 ptp_info->pin_config[i].func = PTP_PF_EXTTS;
982 else if (*pin_usg == BNXT_PPS_PIN_PPS_OUT)
983 ptp_info->pin_config[i].func = PTP_PF_PEROUT;
984 else
985 ptp_info->pin_config[i].func = PTP_PF_NONE;
986
987 pps_info->pins[i].usage = *pin_usg;
988 }
989 hwrm_req_drop(bp, req);
990
991 /* Only 1 each of ext_ts and per_out pins is available in HW */
992 ptp_info->n_ext_ts = 1;
993 ptp_info->n_per_out = 1;
994 ptp_info->pps = 1;
995 ptp_info->verify = bnxt_ptp_verify;
996 ptp_info->supported_extts_flags = PTP_RISING_EDGE | PTP_STRICT_FLAGS;
997 ptp_info->supported_perout_flags = PTP_PEROUT_DUTY_CYCLE;
998
999 return 0;
1000 }
1001
bnxt_pps_config_ok(struct bnxt * bp)1002 static bool bnxt_pps_config_ok(struct bnxt *bp)
1003 {
1004 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
1005
1006 return !(bp->fw_cap & BNXT_FW_CAP_PTP_PPS) == !ptp->ptp_info.pin_config;
1007 }
1008
bnxt_ptp_timecounter_init(struct bnxt * bp,bool init_tc)1009 static void bnxt_ptp_timecounter_init(struct bnxt *bp, bool init_tc)
1010 {
1011 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
1012 unsigned long flags;
1013
1014 if (!ptp->ptp_clock) {
1015 memset(&ptp->cc, 0, sizeof(ptp->cc));
1016 ptp->cc.read = bnxt_cc_read;
1017 ptp->cc.mask = CYCLECOUNTER_MASK(48);
1018 if (BNXT_MH(bp)) {
1019 /* Use timecounter based non-real time mode */
1020 ptp->cc.shift = BNXT_CYCLES_SHIFT;
1021 ptp->cc.mult = clocksource_khz2mult(BNXT_DEVCLK_FREQ, ptp->cc.shift);
1022 ptp->cmult = ptp->cc.mult;
1023 } else {
1024 ptp->cc.shift = 0;
1025 ptp->cc.mult = 1;
1026 }
1027 ptp->next_overflow_check = jiffies + BNXT_PHC_OVERFLOW_PERIOD;
1028 }
1029 if (init_tc) {
1030 write_seqlock_irqsave(&ptp->ptp_lock, flags);
1031 timecounter_init(&ptp->tc, &ptp->cc, ktime_to_ns(ktime_get_real()));
1032 write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
1033 }
1034 }
1035
1036 /* Caller holds ptp_lock */
bnxt_ptp_rtc_timecounter_init(struct bnxt_ptp_cfg * ptp,u64 ns)1037 void bnxt_ptp_rtc_timecounter_init(struct bnxt_ptp_cfg *ptp, u64 ns)
1038 {
1039 timecounter_init(&ptp->tc, &ptp->cc, ns);
1040 /* For RTC, cycle_last must be in sync with the timecounter value. */
1041 ptp->tc.cycle_last = ns & ptp->cc.mask;
1042 }
1043
bnxt_ptp_init_rtc(struct bnxt * bp,bool phc_cfg)1044 int bnxt_ptp_init_rtc(struct bnxt *bp, bool phc_cfg)
1045 {
1046 struct timespec64 tsp;
1047 unsigned long flags;
1048 u64 ns;
1049 int rc;
1050
1051 if (!bp->ptp_cfg || !BNXT_PTP_USE_RTC(bp))
1052 return -ENODEV;
1053
1054 if (!phc_cfg) {
1055 ktime_get_real_ts64(&tsp);
1056 ns = timespec64_to_ns(&tsp);
1057 rc = bnxt_ptp_cfg_settime(bp, ns);
1058 if (rc)
1059 return rc;
1060 } else {
1061 rc = bnxt_hwrm_port_ts_query(bp, PORT_TS_QUERY_REQ_FLAGS_CURRENT_TIME,
1062 &ns, 0, 0);
1063 if (rc)
1064 return rc;
1065 }
1066 write_seqlock_irqsave(&bp->ptp_cfg->ptp_lock, flags);
1067 bnxt_ptp_rtc_timecounter_init(bp->ptp_cfg, ns);
1068 write_sequnlock_irqrestore(&bp->ptp_cfg->ptp_lock, flags);
1069
1070 return 0;
1071 }
1072
bnxt_ptp_free(struct bnxt * bp)1073 static void bnxt_ptp_free(struct bnxt *bp)
1074 {
1075 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
1076
1077 if (ptp->ptp_clock) {
1078 ptp_clock_unregister(ptp->ptp_clock);
1079 ptp->ptp_clock = NULL;
1080 }
1081 kfree(ptp->ptp_info.pin_config);
1082 ptp->ptp_info.pin_config = NULL;
1083 }
1084
bnxt_ptp_init(struct bnxt * bp)1085 int bnxt_ptp_init(struct bnxt *bp)
1086 {
1087 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
1088 int rc;
1089
1090 if (!ptp)
1091 return 0;
1092
1093 rc = bnxt_map_ptp_regs(bp);
1094 if (rc)
1095 return rc;
1096
1097 if (ptp->ptp_clock && bnxt_pps_config_ok(bp))
1098 return 0;
1099
1100 bnxt_ptp_free(bp);
1101
1102 WRITE_ONCE(ptp->tx_avail, BNXT_MAX_TX_TS);
1103 seqlock_init(&ptp->ptp_lock);
1104 spin_lock_init(&ptp->ptp_tx_lock);
1105
1106 if (BNXT_PTP_USE_RTC(bp)) {
1107 bnxt_ptp_timecounter_init(bp, false);
1108 rc = bnxt_ptp_init_rtc(bp, ptp->rtc_configured);
1109 if (rc)
1110 goto out;
1111 } else {
1112 bnxt_ptp_timecounter_init(bp, true);
1113 bnxt_ptp_adjfine_rtc(bp, 0);
1114 }
1115 bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, true);
1116
1117 ptp->ptp_info = bnxt_ptp_caps;
1118 if ((bp->fw_cap & BNXT_FW_CAP_PTP_PPS)) {
1119 if (bnxt_ptp_pps_init(bp))
1120 netdev_err(bp->dev, "1pps not initialized, continuing without 1pps support\n");
1121 }
1122 #ifdef CONFIG_X86
1123 if ((bp->fw_cap & BNXT_FW_CAP_PTP_PTM) && pcie_ptm_enabled(bp->pdev) &&
1124 boot_cpu_has(X86_FEATURE_ART))
1125 ptp->ptp_info.getcrosststamp = bnxt_ptp_getcrosststamp;
1126 #endif /* CONFIG_X86 */
1127
1128 ptp->ptp_clock = ptp_clock_register(&ptp->ptp_info, &bp->pdev->dev);
1129 if (IS_ERR(ptp->ptp_clock)) {
1130 int err = PTR_ERR(ptp->ptp_clock);
1131
1132 ptp->ptp_clock = NULL;
1133 rc = err;
1134 goto out;
1135 }
1136
1137 ptp->stats.ts_pkts = 0;
1138 ptp->stats.ts_lost = 0;
1139 atomic64_set(&ptp->stats.ts_err, 0);
1140
1141 if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1142 bnxt_refclk_read(bp, NULL, &ptp->current_time);
1143 WRITE_ONCE(ptp->old_time, ptp->current_time >> BNXT_HI_TIMER_SHIFT);
1144 ptp_schedule_worker(ptp->ptp_clock, 0);
1145 }
1146 ptp->txts_tmo = BNXT_PTP_DFLT_TX_TMO;
1147 return 0;
1148
1149 out:
1150 bnxt_ptp_free(bp);
1151 bnxt_unmap_ptp_regs(bp);
1152 return rc;
1153 }
1154
bnxt_ptp_clear(struct bnxt * bp)1155 void bnxt_ptp_clear(struct bnxt *bp)
1156 {
1157 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
1158
1159 if (!ptp)
1160 return;
1161
1162 if (ptp->ptp_clock)
1163 ptp_clock_unregister(ptp->ptp_clock);
1164
1165 ptp->ptp_clock = NULL;
1166 kfree(ptp->ptp_info.pin_config);
1167 ptp->ptp_info.pin_config = NULL;
1168
1169 bnxt_unmap_ptp_regs(bp);
1170 }
1171