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 "bnxt_hsi.h" 18 #include "bnxt.h" 19 #include "bnxt_hwrm.h" 20 #include "bnxt_ptp.h" 21 22 static int bnxt_ptp_cfg_settime(struct bnxt *bp, u64 time) 23 { 24 struct hwrm_func_ptp_cfg_input *req; 25 int rc; 26 27 rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_CFG); 28 if (rc) 29 return rc; 30 31 req->enables = cpu_to_le16(FUNC_PTP_CFG_REQ_ENABLES_PTP_SET_TIME); 32 req->ptp_set_time = cpu_to_le64(time); 33 return hwrm_req_send(bp, req); 34 } 35 36 int bnxt_ptp_parse(struct sk_buff *skb, u16 *seq_id, u16 *hdr_off) 37 { 38 unsigned int ptp_class; 39 struct ptp_header *hdr; 40 41 ptp_class = ptp_classify_raw(skb); 42 43 switch (ptp_class & PTP_CLASS_VMASK) { 44 case PTP_CLASS_V1: 45 case PTP_CLASS_V2: 46 hdr = ptp_parse_header(skb, ptp_class); 47 if (!hdr) 48 return -EINVAL; 49 50 *hdr_off = (u8 *)hdr - skb->data; 51 *seq_id = ntohs(hdr->sequence_id); 52 return 0; 53 default: 54 return -ERANGE; 55 } 56 } 57 58 static int bnxt_ptp_settime(struct ptp_clock_info *ptp_info, 59 const struct timespec64 *ts) 60 { 61 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg, 62 ptp_info); 63 u64 ns = timespec64_to_ns(ts); 64 65 if (ptp->bp->fw_cap & BNXT_FW_CAP_PTP_RTC) 66 return bnxt_ptp_cfg_settime(ptp->bp, ns); 67 68 spin_lock_bh(&ptp->ptp_lock); 69 timecounter_init(&ptp->tc, &ptp->cc, ns); 70 spin_unlock_bh(&ptp->ptp_lock); 71 return 0; 72 } 73 74 /* Caller holds ptp_lock */ 75 static int bnxt_refclk_read(struct bnxt *bp, struct ptp_system_timestamp *sts, 76 u64 *ns) 77 { 78 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 79 80 if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) 81 return -EIO; 82 83 ptp_read_system_prets(sts); 84 *ns = readl(bp->bar0 + ptp->refclk_mapped_regs[0]); 85 ptp_read_system_postts(sts); 86 *ns |= (u64)readl(bp->bar0 + ptp->refclk_mapped_regs[1]) << 32; 87 return 0; 88 } 89 90 static void bnxt_ptp_get_current_time(struct bnxt *bp) 91 { 92 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 93 94 if (!ptp) 95 return; 96 spin_lock_bh(&ptp->ptp_lock); 97 WRITE_ONCE(ptp->old_time, ptp->current_time); 98 bnxt_refclk_read(bp, NULL, &ptp->current_time); 99 spin_unlock_bh(&ptp->ptp_lock); 100 } 101 102 static int bnxt_hwrm_port_ts_query(struct bnxt *bp, u32 flags, u64 *ts) 103 { 104 struct hwrm_port_ts_query_output *resp; 105 struct hwrm_port_ts_query_input *req; 106 int rc; 107 108 rc = hwrm_req_init(bp, req, HWRM_PORT_TS_QUERY); 109 if (rc) 110 return rc; 111 112 req->flags = cpu_to_le32(flags); 113 if ((flags & PORT_TS_QUERY_REQ_FLAGS_PATH) == 114 PORT_TS_QUERY_REQ_FLAGS_PATH_TX) { 115 req->enables = cpu_to_le16(BNXT_PTP_QTS_TX_ENABLES); 116 req->ptp_seq_id = cpu_to_le32(bp->ptp_cfg->tx_seqid); 117 req->ptp_hdr_offset = cpu_to_le16(bp->ptp_cfg->tx_hdr_off); 118 req->ts_req_timeout = cpu_to_le16(BNXT_PTP_QTS_TIMEOUT); 119 } 120 resp = hwrm_req_hold(bp, req); 121 122 rc = hwrm_req_send(bp, req); 123 if (!rc) 124 *ts = le64_to_cpu(resp->ptp_msg_ts); 125 hwrm_req_drop(bp, req); 126 return rc; 127 } 128 129 static int bnxt_ptp_gettimex(struct ptp_clock_info *ptp_info, 130 struct timespec64 *ts, 131 struct ptp_system_timestamp *sts) 132 { 133 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg, 134 ptp_info); 135 u64 ns, cycles; 136 int rc; 137 138 spin_lock_bh(&ptp->ptp_lock); 139 rc = bnxt_refclk_read(ptp->bp, sts, &cycles); 140 if (rc) { 141 spin_unlock_bh(&ptp->ptp_lock); 142 return rc; 143 } 144 ns = timecounter_cyc2time(&ptp->tc, cycles); 145 spin_unlock_bh(&ptp->ptp_lock); 146 *ts = ns_to_timespec64(ns); 147 148 return 0; 149 } 150 151 /* Caller holds ptp_lock */ 152 void bnxt_ptp_update_current_time(struct bnxt *bp) 153 { 154 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 155 156 bnxt_refclk_read(ptp->bp, NULL, &ptp->current_time); 157 WRITE_ONCE(ptp->old_time, ptp->current_time); 158 } 159 160 static int bnxt_ptp_adjphc(struct bnxt_ptp_cfg *ptp, s64 delta) 161 { 162 struct hwrm_port_mac_cfg_input *req; 163 int rc; 164 165 rc = hwrm_req_init(ptp->bp, req, HWRM_PORT_MAC_CFG); 166 if (rc) 167 return rc; 168 169 req->enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_PTP_ADJ_PHASE); 170 req->ptp_adj_phase = cpu_to_le64(delta); 171 172 rc = hwrm_req_send(ptp->bp, req); 173 if (rc) { 174 netdev_err(ptp->bp->dev, "ptp adjphc failed. rc = %x\n", rc); 175 } else { 176 spin_lock_bh(&ptp->ptp_lock); 177 bnxt_ptp_update_current_time(ptp->bp); 178 spin_unlock_bh(&ptp->ptp_lock); 179 } 180 181 return rc; 182 } 183 184 static int bnxt_ptp_adjtime(struct ptp_clock_info *ptp_info, s64 delta) 185 { 186 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg, 187 ptp_info); 188 189 if (ptp->bp->fw_cap & BNXT_FW_CAP_PTP_RTC) 190 return bnxt_ptp_adjphc(ptp, delta); 191 192 spin_lock_bh(&ptp->ptp_lock); 193 timecounter_adjtime(&ptp->tc, delta); 194 spin_unlock_bh(&ptp->ptp_lock); 195 return 0; 196 } 197 198 static int bnxt_ptp_adjfreq(struct ptp_clock_info *ptp_info, s32 ppb) 199 { 200 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg, 201 ptp_info); 202 struct hwrm_port_mac_cfg_input *req; 203 struct bnxt *bp = ptp->bp; 204 int rc; 205 206 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_CFG); 207 if (rc) 208 return rc; 209 210 req->ptp_freq_adj_ppb = cpu_to_le32(ppb); 211 req->enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_PTP_FREQ_ADJ_PPB); 212 rc = hwrm_req_send(ptp->bp, req); 213 if (rc) 214 netdev_err(ptp->bp->dev, 215 "ptp adjfreq failed. rc = %d\n", rc); 216 return rc; 217 } 218 219 void bnxt_ptp_pps_event(struct bnxt *bp, u32 data1, u32 data2) 220 { 221 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 222 struct ptp_clock_event event; 223 u64 ns, pps_ts; 224 225 pps_ts = EVENT_PPS_TS(data2, data1); 226 spin_lock_bh(&ptp->ptp_lock); 227 ns = timecounter_cyc2time(&ptp->tc, pps_ts); 228 spin_unlock_bh(&ptp->ptp_lock); 229 230 switch (EVENT_DATA2_PPS_EVENT_TYPE(data2)) { 231 case ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA2_EVENT_TYPE_INTERNAL: 232 event.pps_times.ts_real = ns_to_timespec64(ns); 233 event.type = PTP_CLOCK_PPSUSR; 234 event.index = EVENT_DATA2_PPS_PIN_NUM(data2); 235 break; 236 case ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA2_EVENT_TYPE_EXTERNAL: 237 event.timestamp = ns; 238 event.type = PTP_CLOCK_EXTTS; 239 event.index = EVENT_DATA2_PPS_PIN_NUM(data2); 240 break; 241 } 242 243 ptp_clock_event(bp->ptp_cfg->ptp_clock, &event); 244 } 245 246 static int bnxt_ptp_cfg_pin(struct bnxt *bp, u8 pin, u8 usage) 247 { 248 struct hwrm_func_ptp_pin_cfg_input *req; 249 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 250 u8 state = usage != BNXT_PPS_PIN_NONE; 251 u8 *pin_state, *pin_usg; 252 u32 enables; 253 int rc; 254 255 if (!TSIO_PIN_VALID(pin)) { 256 netdev_err(ptp->bp->dev, "1PPS: Invalid pin. Check pin-function configuration\n"); 257 return -EOPNOTSUPP; 258 } 259 260 rc = hwrm_req_init(ptp->bp, req, HWRM_FUNC_PTP_PIN_CFG); 261 if (rc) 262 return rc; 263 264 enables = (FUNC_PTP_PIN_CFG_REQ_ENABLES_PIN0_STATE | 265 FUNC_PTP_PIN_CFG_REQ_ENABLES_PIN0_USAGE) << (pin * 2); 266 req->enables = cpu_to_le32(enables); 267 268 pin_state = &req->pin0_state; 269 pin_usg = &req->pin0_usage; 270 271 *(pin_state + (pin * 2)) = state; 272 *(pin_usg + (pin * 2)) = usage; 273 274 rc = hwrm_req_send(ptp->bp, req); 275 if (rc) 276 return rc; 277 278 ptp->pps_info.pins[pin].usage = usage; 279 ptp->pps_info.pins[pin].state = state; 280 281 return 0; 282 } 283 284 static int bnxt_ptp_cfg_event(struct bnxt *bp, u8 event) 285 { 286 struct hwrm_func_ptp_cfg_input *req; 287 int rc; 288 289 rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_CFG); 290 if (rc) 291 return rc; 292 293 req->enables = cpu_to_le16(FUNC_PTP_CFG_REQ_ENABLES_PTP_PPS_EVENT); 294 req->ptp_pps_event = event; 295 return hwrm_req_send(bp, req); 296 } 297 298 void bnxt_ptp_reapply_pps(struct bnxt *bp) 299 { 300 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 301 struct bnxt_pps *pps; 302 u32 pin = 0; 303 int rc; 304 305 if (!ptp || !(bp->fw_cap & BNXT_FW_CAP_PTP_PPS) || 306 !(ptp->ptp_info.pin_config)) 307 return; 308 pps = &ptp->pps_info; 309 for (pin = 0; pin < BNXT_MAX_TSIO_PINS; pin++) { 310 if (pps->pins[pin].state) { 311 rc = bnxt_ptp_cfg_pin(bp, pin, pps->pins[pin].usage); 312 if (!rc && pps->pins[pin].event) 313 rc = bnxt_ptp_cfg_event(bp, 314 pps->pins[pin].event); 315 if (rc) 316 netdev_err(bp->dev, "1PPS: Failed to configure pin%d\n", 317 pin); 318 } 319 } 320 } 321 322 static int bnxt_get_target_cycles(struct bnxt_ptp_cfg *ptp, u64 target_ns, 323 u64 *cycles_delta) 324 { 325 u64 cycles_now; 326 u64 nsec_now, nsec_delta; 327 int rc; 328 329 spin_lock_bh(&ptp->ptp_lock); 330 rc = bnxt_refclk_read(ptp->bp, NULL, &cycles_now); 331 if (rc) { 332 spin_unlock_bh(&ptp->ptp_lock); 333 return rc; 334 } 335 nsec_now = timecounter_cyc2time(&ptp->tc, cycles_now); 336 spin_unlock_bh(&ptp->ptp_lock); 337 338 nsec_delta = target_ns - nsec_now; 339 *cycles_delta = div64_u64(nsec_delta << ptp->cc.shift, ptp->cc.mult); 340 return 0; 341 } 342 343 static int bnxt_ptp_perout_cfg(struct bnxt_ptp_cfg *ptp, 344 struct ptp_clock_request *rq) 345 { 346 struct hwrm_func_ptp_cfg_input *req; 347 struct bnxt *bp = ptp->bp; 348 struct timespec64 ts; 349 u64 target_ns, delta; 350 u16 enables; 351 int rc; 352 353 ts.tv_sec = rq->perout.start.sec; 354 ts.tv_nsec = rq->perout.start.nsec; 355 target_ns = timespec64_to_ns(&ts); 356 357 rc = bnxt_get_target_cycles(ptp, target_ns, &delta); 358 if (rc) 359 return rc; 360 361 rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_CFG); 362 if (rc) 363 return rc; 364 365 enables = FUNC_PTP_CFG_REQ_ENABLES_PTP_FREQ_ADJ_EXT_PERIOD | 366 FUNC_PTP_CFG_REQ_ENABLES_PTP_FREQ_ADJ_EXT_UP | 367 FUNC_PTP_CFG_REQ_ENABLES_PTP_FREQ_ADJ_EXT_PHASE; 368 req->enables = cpu_to_le16(enables); 369 req->ptp_pps_event = 0; 370 req->ptp_freq_adj_dll_source = 0; 371 req->ptp_freq_adj_dll_phase = 0; 372 req->ptp_freq_adj_ext_period = cpu_to_le32(NSEC_PER_SEC); 373 req->ptp_freq_adj_ext_up = 0; 374 req->ptp_freq_adj_ext_phase_lower = cpu_to_le32(delta); 375 376 return hwrm_req_send(bp, req); 377 } 378 379 static int bnxt_ptp_enable(struct ptp_clock_info *ptp_info, 380 struct ptp_clock_request *rq, int on) 381 { 382 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg, 383 ptp_info); 384 struct bnxt *bp = ptp->bp; 385 u8 pin_id; 386 int rc; 387 388 switch (rq->type) { 389 case PTP_CLK_REQ_EXTTS: 390 /* Configure an External PPS IN */ 391 pin_id = ptp_find_pin(ptp->ptp_clock, PTP_PF_EXTTS, 392 rq->extts.index); 393 if (!on) 394 break; 395 rc = bnxt_ptp_cfg_pin(bp, pin_id, BNXT_PPS_PIN_PPS_IN); 396 if (rc) 397 return rc; 398 rc = bnxt_ptp_cfg_event(bp, BNXT_PPS_EVENT_EXTERNAL); 399 if (!rc) 400 ptp->pps_info.pins[pin_id].event = BNXT_PPS_EVENT_EXTERNAL; 401 return rc; 402 case PTP_CLK_REQ_PEROUT: 403 /* Configure a Periodic PPS OUT */ 404 pin_id = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT, 405 rq->perout.index); 406 if (!on) 407 break; 408 409 rc = bnxt_ptp_cfg_pin(bp, pin_id, BNXT_PPS_PIN_PPS_OUT); 410 if (!rc) 411 rc = bnxt_ptp_perout_cfg(ptp, rq); 412 413 return rc; 414 case PTP_CLK_REQ_PPS: 415 /* Configure PHC PPS IN */ 416 rc = bnxt_ptp_cfg_pin(bp, 0, BNXT_PPS_PIN_PPS_IN); 417 if (rc) 418 return rc; 419 rc = bnxt_ptp_cfg_event(bp, BNXT_PPS_EVENT_INTERNAL); 420 if (!rc) 421 ptp->pps_info.pins[0].event = BNXT_PPS_EVENT_INTERNAL; 422 return rc; 423 default: 424 netdev_err(ptp->bp->dev, "Unrecognized PIN function\n"); 425 return -EOPNOTSUPP; 426 } 427 428 return bnxt_ptp_cfg_pin(bp, pin_id, BNXT_PPS_PIN_NONE); 429 } 430 431 static int bnxt_hwrm_ptp_cfg(struct bnxt *bp) 432 { 433 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 434 struct hwrm_port_mac_cfg_input *req; 435 u32 flags = 0; 436 int rc; 437 438 rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_CFG); 439 if (rc) 440 return rc; 441 442 if (ptp->rx_filter) 443 flags |= PORT_MAC_CFG_REQ_FLAGS_PTP_RX_TS_CAPTURE_ENABLE; 444 else 445 flags |= PORT_MAC_CFG_REQ_FLAGS_PTP_RX_TS_CAPTURE_DISABLE; 446 if (ptp->tx_tstamp_en) 447 flags |= PORT_MAC_CFG_REQ_FLAGS_PTP_TX_TS_CAPTURE_ENABLE; 448 else 449 flags |= PORT_MAC_CFG_REQ_FLAGS_PTP_TX_TS_CAPTURE_DISABLE; 450 req->flags = cpu_to_le32(flags); 451 req->enables = cpu_to_le32(PORT_MAC_CFG_REQ_ENABLES_RX_TS_CAPTURE_PTP_MSG_TYPE); 452 req->rx_ts_capture_ptp_msg_type = cpu_to_le16(ptp->rxctl); 453 454 return hwrm_req_send(bp, req); 455 } 456 457 int bnxt_hwtstamp_set(struct net_device *dev, struct ifreq *ifr) 458 { 459 struct bnxt *bp = netdev_priv(dev); 460 struct hwtstamp_config stmpconf; 461 struct bnxt_ptp_cfg *ptp; 462 u16 old_rxctl; 463 int old_rx_filter, rc; 464 u8 old_tx_tstamp_en; 465 466 ptp = bp->ptp_cfg; 467 if (!ptp) 468 return -EOPNOTSUPP; 469 470 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf))) 471 return -EFAULT; 472 473 if (stmpconf.tx_type != HWTSTAMP_TX_ON && 474 stmpconf.tx_type != HWTSTAMP_TX_OFF) 475 return -ERANGE; 476 477 old_rx_filter = ptp->rx_filter; 478 old_rxctl = ptp->rxctl; 479 old_tx_tstamp_en = ptp->tx_tstamp_en; 480 switch (stmpconf.rx_filter) { 481 case HWTSTAMP_FILTER_NONE: 482 ptp->rxctl = 0; 483 ptp->rx_filter = HWTSTAMP_FILTER_NONE; 484 break; 485 case HWTSTAMP_FILTER_PTP_V2_EVENT: 486 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 487 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 488 ptp->rxctl = BNXT_PTP_MSG_EVENTS; 489 ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 490 break; 491 case HWTSTAMP_FILTER_PTP_V2_SYNC: 492 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 493 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 494 ptp->rxctl = BNXT_PTP_MSG_SYNC; 495 ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC; 496 break; 497 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 498 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 499 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 500 ptp->rxctl = BNXT_PTP_MSG_DELAY_REQ; 501 ptp->rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ; 502 break; 503 default: 504 return -ERANGE; 505 } 506 507 if (stmpconf.tx_type == HWTSTAMP_TX_ON) 508 ptp->tx_tstamp_en = 1; 509 else 510 ptp->tx_tstamp_en = 0; 511 512 rc = bnxt_hwrm_ptp_cfg(bp); 513 if (rc) 514 goto ts_set_err; 515 516 stmpconf.rx_filter = ptp->rx_filter; 517 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ? 518 -EFAULT : 0; 519 520 ts_set_err: 521 ptp->rx_filter = old_rx_filter; 522 ptp->rxctl = old_rxctl; 523 ptp->tx_tstamp_en = old_tx_tstamp_en; 524 return rc; 525 } 526 527 int bnxt_hwtstamp_get(struct net_device *dev, struct ifreq *ifr) 528 { 529 struct bnxt *bp = netdev_priv(dev); 530 struct hwtstamp_config stmpconf; 531 struct bnxt_ptp_cfg *ptp; 532 533 ptp = bp->ptp_cfg; 534 if (!ptp) 535 return -EOPNOTSUPP; 536 537 stmpconf.flags = 0; 538 stmpconf.tx_type = ptp->tx_tstamp_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 539 540 stmpconf.rx_filter = ptp->rx_filter; 541 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ? 542 -EFAULT : 0; 543 } 544 545 static int bnxt_map_regs(struct bnxt *bp, u32 *reg_arr, int count, int reg_win) 546 { 547 u32 reg_base = *reg_arr & BNXT_GRC_BASE_MASK; 548 u32 win_off; 549 int i; 550 551 for (i = 0; i < count; i++) { 552 if ((reg_arr[i] & BNXT_GRC_BASE_MASK) != reg_base) 553 return -ERANGE; 554 } 555 win_off = BNXT_GRCPF_REG_WINDOW_BASE_OUT + (reg_win - 1) * 4; 556 writel(reg_base, bp->bar0 + win_off); 557 return 0; 558 } 559 560 static int bnxt_map_ptp_regs(struct bnxt *bp) 561 { 562 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 563 u32 *reg_arr; 564 int rc, i; 565 566 reg_arr = ptp->refclk_regs; 567 if (bp->flags & BNXT_FLAG_CHIP_P5) { 568 rc = bnxt_map_regs(bp, reg_arr, 2, BNXT_PTP_GRC_WIN); 569 if (rc) 570 return rc; 571 for (i = 0; i < 2; i++) 572 ptp->refclk_mapped_regs[i] = BNXT_PTP_GRC_WIN_BASE + 573 (ptp->refclk_regs[i] & BNXT_GRC_OFFSET_MASK); 574 return 0; 575 } 576 return -ENODEV; 577 } 578 579 static void bnxt_unmap_ptp_regs(struct bnxt *bp) 580 { 581 writel(0, bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 582 (BNXT_PTP_GRC_WIN - 1) * 4); 583 } 584 585 static u64 bnxt_cc_read(const struct cyclecounter *cc) 586 { 587 struct bnxt_ptp_cfg *ptp = container_of(cc, struct bnxt_ptp_cfg, cc); 588 u64 ns = 0; 589 590 bnxt_refclk_read(ptp->bp, NULL, &ns); 591 return ns; 592 } 593 594 static void bnxt_stamp_tx_skb(struct bnxt *bp, struct sk_buff *skb) 595 { 596 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 597 struct skb_shared_hwtstamps timestamp; 598 u64 ts = 0, ns = 0; 599 int rc; 600 601 rc = bnxt_hwrm_port_ts_query(bp, PORT_TS_QUERY_REQ_FLAGS_PATH_TX, &ts); 602 if (!rc) { 603 memset(×tamp, 0, sizeof(timestamp)); 604 spin_lock_bh(&ptp->ptp_lock); 605 ns = timecounter_cyc2time(&ptp->tc, ts); 606 spin_unlock_bh(&ptp->ptp_lock); 607 timestamp.hwtstamp = ns_to_ktime(ns); 608 skb_tstamp_tx(ptp->tx_skb, ×tamp); 609 } else { 610 netdev_err(bp->dev, "TS query for TX timer failed rc = %x\n", 611 rc); 612 } 613 614 dev_kfree_skb_any(ptp->tx_skb); 615 ptp->tx_skb = NULL; 616 atomic_inc(&ptp->tx_avail); 617 } 618 619 static long bnxt_ptp_ts_aux_work(struct ptp_clock_info *ptp_info) 620 { 621 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg, 622 ptp_info); 623 unsigned long now = jiffies; 624 struct bnxt *bp = ptp->bp; 625 626 if (ptp->tx_skb) 627 bnxt_stamp_tx_skb(bp, ptp->tx_skb); 628 629 if (!time_after_eq(now, ptp->next_period)) 630 return ptp->next_period - now; 631 632 bnxt_ptp_get_current_time(bp); 633 ptp->next_period = now + HZ; 634 if (time_after_eq(now, ptp->next_overflow_check)) { 635 spin_lock_bh(&ptp->ptp_lock); 636 timecounter_read(&ptp->tc); 637 spin_unlock_bh(&ptp->ptp_lock); 638 ptp->next_overflow_check = now + BNXT_PHC_OVERFLOW_PERIOD; 639 } 640 return HZ; 641 } 642 643 int bnxt_get_tx_ts_p5(struct bnxt *bp, struct sk_buff *skb) 644 { 645 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 646 647 if (ptp->tx_skb) { 648 netdev_err(bp->dev, "deferring skb:one SKB is still outstanding\n"); 649 return -EBUSY; 650 } 651 ptp->tx_skb = skb; 652 ptp_schedule_worker(ptp->ptp_clock, 0); 653 return 0; 654 } 655 656 int bnxt_get_rx_ts_p5(struct bnxt *bp, u64 *ts, u32 pkt_ts) 657 { 658 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 659 u64 time; 660 661 if (!ptp) 662 return -ENODEV; 663 664 BNXT_READ_TIME64(ptp, time, ptp->old_time); 665 *ts = (time & BNXT_HI_TIMER_MASK) | pkt_ts; 666 if (pkt_ts < (time & BNXT_LO_TIMER_MASK)) 667 *ts += BNXT_LO_TIMER_MASK + 1; 668 669 return 0; 670 } 671 672 static const struct ptp_clock_info bnxt_ptp_caps = { 673 .owner = THIS_MODULE, 674 .name = "bnxt clock", 675 .max_adj = BNXT_MAX_PHC_DRIFT, 676 .n_alarm = 0, 677 .n_ext_ts = 0, 678 .n_per_out = 0, 679 .n_pins = 0, 680 .pps = 0, 681 .adjfreq = bnxt_ptp_adjfreq, 682 .adjtime = bnxt_ptp_adjtime, 683 .do_aux_work = bnxt_ptp_ts_aux_work, 684 .gettimex64 = bnxt_ptp_gettimex, 685 .settime64 = bnxt_ptp_settime, 686 .enable = bnxt_ptp_enable, 687 }; 688 689 static int bnxt_ptp_verify(struct ptp_clock_info *ptp_info, unsigned int pin, 690 enum ptp_pin_function func, unsigned int chan) 691 { 692 struct bnxt_ptp_cfg *ptp = container_of(ptp_info, struct bnxt_ptp_cfg, 693 ptp_info); 694 /* Allow only PPS pin function configuration */ 695 if (ptp->pps_info.pins[pin].usage <= BNXT_PPS_PIN_PPS_OUT && 696 func != PTP_PF_PHYSYNC) 697 return 0; 698 else 699 return -EOPNOTSUPP; 700 } 701 702 static int bnxt_ptp_pps_init(struct bnxt *bp) 703 { 704 struct hwrm_func_ptp_pin_qcfg_output *resp; 705 struct hwrm_func_ptp_pin_qcfg_input *req; 706 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 707 struct ptp_clock_info *ptp_info; 708 struct bnxt_pps *pps_info; 709 u8 *pin_usg; 710 u32 i, rc; 711 712 /* Query current/default PIN CFG */ 713 rc = hwrm_req_init(bp, req, HWRM_FUNC_PTP_PIN_QCFG); 714 if (rc) 715 return rc; 716 717 resp = hwrm_req_hold(bp, req); 718 rc = hwrm_req_send(bp, req); 719 if (rc || !resp->num_pins) { 720 hwrm_req_drop(bp, req); 721 return -EOPNOTSUPP; 722 } 723 724 ptp_info = &ptp->ptp_info; 725 pps_info = &ptp->pps_info; 726 pps_info->num_pins = resp->num_pins; 727 ptp_info->n_pins = pps_info->num_pins; 728 ptp_info->pin_config = kcalloc(ptp_info->n_pins, 729 sizeof(*ptp_info->pin_config), 730 GFP_KERNEL); 731 if (!ptp_info->pin_config) { 732 hwrm_req_drop(bp, req); 733 return -ENOMEM; 734 } 735 736 /* Report the TSIO capability to kernel */ 737 pin_usg = &resp->pin0_usage; 738 for (i = 0; i < pps_info->num_pins; i++, pin_usg++) { 739 snprintf(ptp_info->pin_config[i].name, 740 sizeof(ptp_info->pin_config[i].name), "bnxt_pps%d", i); 741 ptp_info->pin_config[i].index = i; 742 ptp_info->pin_config[i].chan = i; 743 if (*pin_usg == BNXT_PPS_PIN_PPS_IN) 744 ptp_info->pin_config[i].func = PTP_PF_EXTTS; 745 else if (*pin_usg == BNXT_PPS_PIN_PPS_OUT) 746 ptp_info->pin_config[i].func = PTP_PF_PEROUT; 747 else 748 ptp_info->pin_config[i].func = PTP_PF_NONE; 749 750 pps_info->pins[i].usage = *pin_usg; 751 } 752 hwrm_req_drop(bp, req); 753 754 /* Only 1 each of ext_ts and per_out pins is available in HW */ 755 ptp_info->n_ext_ts = 1; 756 ptp_info->n_per_out = 1; 757 ptp_info->pps = 1; 758 ptp_info->verify = bnxt_ptp_verify; 759 760 return 0; 761 } 762 763 static bool bnxt_pps_config_ok(struct bnxt *bp) 764 { 765 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 766 767 return !(bp->fw_cap & BNXT_FW_CAP_PTP_PPS) == !ptp->ptp_info.pin_config; 768 } 769 770 static void bnxt_ptp_timecounter_init(struct bnxt *bp, bool init_tc) 771 { 772 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 773 774 if (!ptp->ptp_clock) { 775 memset(&ptp->cc, 0, sizeof(ptp->cc)); 776 ptp->cc.read = bnxt_cc_read; 777 ptp->cc.mask = CYCLECOUNTER_MASK(48); 778 ptp->cc.shift = 0; 779 ptp->cc.mult = 1; 780 ptp->next_overflow_check = jiffies + BNXT_PHC_OVERFLOW_PERIOD; 781 } 782 if (init_tc) 783 timecounter_init(&ptp->tc, &ptp->cc, ktime_to_ns(ktime_get_real())); 784 } 785 786 /* Caller holds ptp_lock */ 787 void bnxt_ptp_rtc_timecounter_init(struct bnxt_ptp_cfg *ptp, u64 ns) 788 { 789 timecounter_init(&ptp->tc, &ptp->cc, ns); 790 /* For RTC, cycle_last must be in sync with the timecounter value. */ 791 ptp->tc.cycle_last = ns & ptp->cc.mask; 792 } 793 794 int bnxt_ptp_init_rtc(struct bnxt *bp, bool phc_cfg) 795 { 796 struct timespec64 tsp; 797 u64 ns; 798 int rc; 799 800 if (!bp->ptp_cfg || !(bp->fw_cap & BNXT_FW_CAP_PTP_RTC)) 801 return -ENODEV; 802 803 if (!phc_cfg) { 804 ktime_get_real_ts64(&tsp); 805 ns = timespec64_to_ns(&tsp); 806 rc = bnxt_ptp_cfg_settime(bp, ns); 807 if (rc) 808 return rc; 809 } else { 810 rc = bnxt_hwrm_port_ts_query(bp, PORT_TS_QUERY_REQ_FLAGS_CURRENT_TIME, &ns); 811 if (rc) 812 return rc; 813 } 814 spin_lock_bh(&bp->ptp_cfg->ptp_lock); 815 bnxt_ptp_rtc_timecounter_init(bp->ptp_cfg, ns); 816 spin_unlock_bh(&bp->ptp_cfg->ptp_lock); 817 818 return 0; 819 } 820 821 static void bnxt_ptp_free(struct bnxt *bp) 822 { 823 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 824 825 if (ptp->ptp_clock) { 826 ptp_clock_unregister(ptp->ptp_clock); 827 ptp->ptp_clock = NULL; 828 kfree(ptp->ptp_info.pin_config); 829 ptp->ptp_info.pin_config = NULL; 830 } 831 } 832 833 int bnxt_ptp_init(struct bnxt *bp, bool phc_cfg) 834 { 835 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 836 int rc; 837 838 if (!ptp) 839 return 0; 840 841 rc = bnxt_map_ptp_regs(bp); 842 if (rc) 843 return rc; 844 845 if (bp->fw_cap & BNXT_FW_CAP_PTP_RTC) { 846 bnxt_ptp_timecounter_init(bp, false); 847 rc = bnxt_ptp_init_rtc(bp, phc_cfg); 848 if (rc) 849 goto out; 850 } 851 852 if (ptp->ptp_clock && bnxt_pps_config_ok(bp)) 853 return 0; 854 855 bnxt_ptp_free(bp); 856 857 atomic_set(&ptp->tx_avail, BNXT_MAX_TX_TS); 858 spin_lock_init(&ptp->ptp_lock); 859 860 if (!(bp->fw_cap & BNXT_FW_CAP_PTP_RTC)) 861 bnxt_ptp_timecounter_init(bp, true); 862 863 ptp->ptp_info = bnxt_ptp_caps; 864 if ((bp->fw_cap & BNXT_FW_CAP_PTP_PPS)) { 865 if (bnxt_ptp_pps_init(bp)) 866 netdev_err(bp->dev, "1pps not initialized, continuing without 1pps support\n"); 867 } 868 ptp->ptp_clock = ptp_clock_register(&ptp->ptp_info, &bp->pdev->dev); 869 if (IS_ERR(ptp->ptp_clock)) { 870 int err = PTR_ERR(ptp->ptp_clock); 871 872 ptp->ptp_clock = NULL; 873 rc = err; 874 goto out; 875 } 876 if (bp->flags & BNXT_FLAG_CHIP_P5) { 877 spin_lock_bh(&ptp->ptp_lock); 878 bnxt_refclk_read(bp, NULL, &ptp->current_time); 879 WRITE_ONCE(ptp->old_time, ptp->current_time); 880 spin_unlock_bh(&ptp->ptp_lock); 881 ptp_schedule_worker(ptp->ptp_clock, 0); 882 } 883 return 0; 884 885 out: 886 bnxt_ptp_free(bp); 887 bnxt_unmap_ptp_regs(bp); 888 return rc; 889 } 890 891 void bnxt_ptp_clear(struct bnxt *bp) 892 { 893 struct bnxt_ptp_cfg *ptp = bp->ptp_cfg; 894 895 if (!ptp) 896 return; 897 898 if (ptp->ptp_clock) 899 ptp_clock_unregister(ptp->ptp_clock); 900 901 ptp->ptp_clock = NULL; 902 kfree(ptp->ptp_info.pin_config); 903 ptp->ptp_info.pin_config = NULL; 904 905 if (ptp->tx_skb) { 906 dev_kfree_skb_any(ptp->tx_skb); 907 ptp->tx_skb = NULL; 908 } 909 bnxt_unmap_ptp_regs(bp); 910 } 911