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