1 /* 2 * Copyright (c) 2018 Intel Corporation. 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /** 8 * @file 9 * @brief PTP data sets 10 * 11 * This is not to be included by the application. 12 */ 13 14 #ifndef __GPTP_DS_H 15 #define __GPTP_DS_H 16 17 #if defined(CONFIG_NET_GPTP) 18 19 #include <zephyr/net/gptp.h> 20 #include "gptp_state.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 /* Parameters for PTP data sets. */ 27 #define GPTP_ALLOWED_LOST_RESP 3 28 29 #if defined(CONFIG_NET_GPTP_NEIGHBOR_PROP_DELAY_THR) 30 #define GPTP_NEIGHBOR_PROP_DELAY_THR CONFIG_NET_GPTP_NEIGHBOR_PROP_DELAY_THR 31 #else 32 /* See IEEE802.1AS B.3 should be less than 800ns (cur: 100us). */ 33 #define GPTP_NEIGHBOR_PROP_DELAY_THR 100000 34 #endif 35 36 /* Max number of ClockIdentities in pathTrace. */ 37 #define GPTP_MAX_PATHTRACE_SIZE CONFIG_NET_GPTP_PATH_TRACE_ELEMENTS 38 39 /* Helpers to access gptp_domain fields. */ 40 #define GPTP_PORT_START 1 41 #define GPTP_PORT_END (gptp_domain.default_ds.nb_ports + GPTP_PORT_START) 42 43 #define GPTP_PORT_INDEX (port - GPTP_PORT_START) 44 45 #define GPTP_GLOBAL_DS() (&gptp_domain.global_ds) 46 #define GPTP_DEFAULT_DS() (&gptp_domain.default_ds) 47 #define GPTP_CURRENT_DS() (&gptp_domain.current_ds) 48 #define GPTP_PARENT_DS() (&gptp_domain.parent_ds) 49 #define GPTP_PROPERTIES_DS() (&gptp_domain.properties_ds) 50 #define GPTP_STATE() (&gptp_domain.state) 51 52 #define GPTP_PORT_DS(port) \ 53 (&gptp_domain.port_ds[port - GPTP_PORT_START]) 54 #define GPTP_PORT_STATE(port) \ 55 (&gptp_domain.port_state[port - GPTP_PORT_START]) 56 #define GPTP_PORT_BMCA_DATA(port) \ 57 (&gptp_domain.port_bmca_data[port - GPTP_PORT_START]) 58 #define GPTP_PORT_IFACE(port) \ 59 gptp_domain.iface[port - GPTP_PORT_START] 60 61 #if defined(CONFIG_NET_GPTP_STATISTICS) 62 #define GPTP_PORT_PARAM_DS(port) \ 63 (&gptp_domain.port_param_ds[port - GPTP_PORT_START]) 64 #endif 65 66 #define CLEAR_RESELECT(global_ds, port) \ 67 (global_ds->reselect_array &= (~(1 << (port - 1)))) 68 #define SET_RESELECT(global_ds, port) \ 69 (global_ds->reselect_array |= (1 << (port - 1))) 70 #define CLEAR_SELECTED(global_ds, port) \ 71 (global_ds->selected_array &= (~(1 << (port - 1)))) 72 #define SET_SELECTED(global_ds, port) \ 73 (global_ds->selected_array |= (1 << (port - 1))) 74 #define IS_SELECTED(global_ds, port) \ 75 ((global_ds->selected_array >> (port - 1)) & 0x1) 76 #define IS_RESELECT(global_ds, port) \ 77 ((global_ds->reselect_array >> (port - 1)) & 0x1) 78 79 /* 80 * Global definition of the gPTP domain. 81 * Note: Only one domain is supported for now. 82 */ 83 extern struct gptp_domain gptp_domain; 84 85 /* 86 * Type of TLV message received. 87 */ 88 enum gptp_tlv_type { 89 GPTP_TLV_MGNT = 0x0001, 90 GPTP_TLV_MGNT_ERR_STATUS = 0x0002, 91 GPTP_TLV_ORGANIZATION_EXT = 0x0003, 92 GPTP_TLV_REQ_UNICAST_TX = 0x0004, 93 GPTP_TLV_GRANT_UNICAST_TX = 0x0005, 94 GPTP_TLV_CANCEL_UNICAST_TX = 0x0006, 95 GPTP_TLV_ACK_CANCEL_UNICAST_TX = 0x0007, 96 GPTP_TLV_PATH_TRACE = 0x0008, 97 GPTP_TLV_ALT_TIME_OFFSET_INDICATOR = 0x0009, 98 GPTP_TLV_AUTH = 0x2000, 99 GPTP_TLV_AUTH_CHALLENGE = 0x2001, 100 GPTP_TLV_SECURITY_ASSOC_UPDATE = 0x2002, 101 GPTP_TLV_CUM_FREQ_SCALE_FACTOR_OFFSET = 0x2003, 102 }; 103 104 /* 105 * Class of the local clock used for a port. 106 * This is used when determining the Grand Master. 107 */ 108 enum gptp_clock_class { 109 GPTP_CLASS_PRIMARY = 6, 110 GPTP_CLASS_APP_SPECIFIC = 13, 111 GPTP_CLASS_APP_SPECIFIC_LOST = 14, 112 GPTP_CLASS_PRIMARY_DEGRADED_A = 52, 113 GPTP_CLASS_APP_SPECIFIC_DEGRADED_A = 58, 114 GPTP_CLASS_PRIMARY_DEGRADED_B = 187, 115 GPTP_CLASS_APP_SPECIFIC_DEGRADED_B = 193, 116 GPTP_CLASS_OTHER = 248, 117 GPTP_CLASS_SLAVE_ONLY = 255, 118 }; 119 120 /* 121 * For gPTP, only a subset are used. 122 * - DisabledPort 123 * - MasterPort 124 * - PassivePort 125 * - SlavePort 126 */ 127 enum gptp_port_state { 128 GPTP_PORT_INITIALIZING, 129 GPTP_PORT_FAULTY, 130 GPTP_PORT_DISABLED, 131 GPTP_PORT_LISTENING, 132 GPTP_PORT_PRE_MASTER, 133 GPTP_PORT_MASTER, 134 GPTP_PORT_PASSIVE, 135 GPTP_PORT_UNCALIBRATED, 136 GPTP_PORT_SLAVE, 137 }; 138 139 enum gptp_received_info { 140 GPTP_RCVD_INFO_SUPERIOR_MASTER_INFO, 141 GPTP_RCVD_INFO_REPEATED_MASTER_INFO, 142 GPTP_RCVD_INFO_INFERIOR_MASTER_INFO, 143 GPTP_RCVD_INFO_OTHER_INFO, 144 }; 145 146 /** 147 * @brief Announce path trace retaining structure. 148 */ 149 struct gptp_path_trace { 150 /** Length of the path trace. */ 151 uint16_t len; 152 153 /** Path trace of the announce message. */ 154 uint8_t path_sequence[GPTP_MAX_PATHTRACE_SIZE][GPTP_CLOCK_ID_LEN]; 155 }; 156 157 /** 158 * @brief Per-time-aware system global variables. 159 * 160 * Not all variables from the standard are defined yet. 161 * The structure is to be enhanced with missing fields when those are needed. 162 * 163 * selectedRole is not defined here as it is a duplicate of the port_state 164 * variable declared in gptp_port_ds. 165 */ 166 struct gptp_global_ds { 167 /** Mean time interval between messages providing time-sync info. */ 168 uint64_t clk_master_sync_itv; 169 170 /** Value if current time. */ 171 uint64_t sync_receipt_local_time; 172 173 /** Fractional frequency offset of the Clock Source entity. */ 174 double clk_src_freq_offset; 175 176 /** Last Grand Master Frequency Change. */ 177 double clk_src_last_gm_freq_change; 178 179 /** Ratio of the frequency of the ClockSource to the LocalClock. */ 180 double gm_rate_ratio; 181 182 /** Last Grand Master Frequency Change. */ 183 double last_gm_freq_change; 184 185 /** The synchronized time computed by the ClockSlave entity. */ 186 struct net_ptp_extended_time sync_receipt_time; 187 188 /** Last Grand Master Phase Change. */ 189 struct gptp_scaled_ns clk_src_last_gm_phase_change; 190 191 /** Last Grand Master Phase Change. */ 192 struct gptp_scaled_ns last_gm_phase_change; 193 194 /** Global flags. */ 195 struct gptp_flags global_flags; 196 197 /** System current flags. */ 198 struct gptp_flags sys_flags; 199 200 /** Path trace to be sent in announce message. */ 201 struct gptp_path_trace path_trace; 202 203 /** Grand Master priority vector. */ 204 struct gptp_priority_vector gm_priority; 205 206 /** Previous Grand Master priority vector. */ 207 struct gptp_priority_vector last_gm_priority; 208 209 /** Value of current_time when the last invoke call was received. */ 210 struct gptp_uscaled_ns local_time; 211 212 /** Time maintained by the ClockMaster entity. */ 213 struct net_ptp_extended_time master_time; 214 215 /** Time provided by the ClockSource entity minus the sync time. */ 216 struct gptp_scaled_ns clk_src_phase_offset; 217 218 /** Grand Master Time Base Indicator. */ 219 uint16_t gm_time_base_indicator; 220 221 /** Reselect port bit array. */ 222 uint32_t reselect_array; 223 224 /** Selected port bit array. */ 225 uint32_t selected_array; 226 227 /** Steps removed from selected master. */ 228 uint16_t master_steps_removed; 229 230 /** Current UTC offset. */ 231 int16_t current_utc_offset; 232 233 /** System current UTC offset. */ 234 int16_t sys_current_utc_offset; 235 236 /** Time Base Indicator. */ 237 uint16_t clk_src_time_base_indicator; 238 239 /** Previous Time Base Indicator. */ 240 uint16_t clk_src_time_base_indicator_prev; 241 242 /** Time source. */ 243 enum gptp_time_source time_source; 244 245 /** System time source. */ 246 enum gptp_time_source sys_time_source; 247 248 /** Selected port Roles. */ 249 enum gptp_port_state selected_role[CONFIG_NET_GPTP_NUM_PORTS + 1]; 250 251 /** A Grand Master is present in the domain. */ 252 bool gm_present; 253 }; 254 255 /** 256 * @brief Default Parameter Data Set. 257 * 258 * Data Set representing capabilities of the time-aware system. 259 */ 260 struct gptp_default_ds { 261 /** Quality of the local clock. */ 262 struct gptp_clock_quality clk_quality; 263 264 /* Source of time used by the Grand Master Clock. */ 265 enum gptp_time_source time_source; 266 267 /** Clock Identity of the local clock. */ 268 uint8_t clk_id[GPTP_CLOCK_ID_LEN]; 269 270 /** System current flags. */ 271 struct gptp_flags flags; 272 273 /** Current UTC offset. */ 274 uint16_t cur_utc_offset; 275 276 /** Defines if this system is Grand Master capable. */ 277 bool gm_capable; 278 279 /** Number of ports of the time-aware system. */ 280 uint8_t nb_ports; 281 282 /** Primary priority of the time-aware system. */ 283 uint8_t priority1; 284 285 /** Secondary priority of the time-aware system. */ 286 uint8_t priority2; 287 }; 288 289 /** 290 * @brief Current Parameter Data Set. 291 * 292 * Data Set representing information relative to the Grand Master. 293 */ 294 struct gptp_current_ds { 295 /** Last Grand Master Phase change . */ 296 struct gptp_scaled_ns last_gm_phase_change; 297 298 /** Time difference between a slave and the Grand Master. */ 299 int64_t offset_from_master; 300 301 /** Last Grand Master Frequency change. */ 302 double last_gm_freq_change; 303 304 /** Number of times a Grand Master has changed in the domain. */ 305 uint32_t gm_change_count; 306 307 /** Time when the most recent Grand Master changed. */ 308 uint32_t last_gm_chg_evt_time; 309 310 /** Time when the most recent Grand Master phase changed. */ 311 uint32_t last_gm_phase_chg_evt_time; 312 313 /** Time when the most recent Grand Master frequency changed. */ 314 uint32_t last_gm_freq_chg_evt_time; 315 316 /** Time Base Indicator of the current Grand Master. */ 317 uint16_t gm_timebase_indicator; 318 319 /** Number of steps between the local clock and the Grand Master. */ 320 uint8_t steps_removed; 321 }; 322 323 /** 324 * @brief Parent Parameter Data Set. 325 * 326 * Data Set representing the parent capabilities. 327 */ 328 struct gptp_parent_ds { 329 /** Ratio of the frequency of the GM with the local clock. */ 330 int32_t cumulative_rate_ratio; 331 332 /** Clock Identity of the Grand Master clock. */ 333 uint8_t gm_id[GPTP_CLOCK_ID_LEN]; 334 335 /** Clock Class of the Grand Master clock. */ 336 struct gptp_clock_quality gm_clk_quality; 337 338 /** Port Identity of the Master Port attached to this system. */ 339 struct gptp_port_identity port_id; 340 341 /** Primary Priority of the Grand Master clock. */ 342 uint8_t gm_priority1; 343 344 /** Secondary Priority of the Grand Master clock. */ 345 uint8_t gm_priority2; 346 }; 347 348 /** 349 * @brief Time Properties Parameter Data Set. 350 * 351 * Data Set representing Grand Master capabilities from the point of view 352 * of this system. 353 */ 354 struct gptp_time_prop_ds { 355 /** The time source of the Grand Master. */ 356 enum gptp_time_source time_source; 357 358 /** Current UTC offset for the Grand Master. */ 359 uint16_t cur_utc_offset; 360 361 /** Current UTC offset valid for the Grand Master. */ 362 bool cur_utc_offset_valid : 1; 363 364 /** The Grand Master will have 59s at the end of the current UTC day. 365 */ 366 bool leap59 : 1; 367 368 /** The Grand Master will have 61s at the end of the current UTC day. 369 */ 370 bool leap61 : 1; 371 372 /** The current UTC offset of the GM is traceable to a primary ref. */ 373 bool time_traceable : 1; 374 375 /** The frequency of the Grand Master is traceable to a primary ref. */ 376 bool freq_traceable : 1; 377 }; 378 379 /** 380 * @brief Port Parameter Data Set. 381 * 382 * Data Set representing port capabilities. 383 */ 384 struct gptp_port_ds { 385 /** Maximum interval between sync messages. */ 386 uint64_t sync_receipt_timeout_time_itv; 387 388 /** Asymmetry on the link relative to the grand master time base. */ 389 int64_t delay_asymmetry; 390 391 /** One way propagation time on the link attached to this port. */ 392 double neighbor_prop_delay; 393 394 /** Propagation time threshold for the link attached to this port. */ 395 double neighbor_prop_delay_thresh; 396 397 /** Estimate of the ratio of the frequency with the peer. */ 398 double neighbor_rate_ratio; 399 400 /** Port Identity of the port. */ 401 struct gptp_port_identity port_id; 402 403 /** Sync event transmission interval for the port. */ 404 struct gptp_uscaled_ns half_sync_itv; 405 406 /** Path Delay Request transmission interval for the port. */ 407 struct gptp_uscaled_ns pdelay_req_itv; 408 409 /** Maximum number of Path Delay Requests without a response. */ 410 uint16_t allowed_lost_responses; 411 412 /** Current Sync sequence id for this port. */ 413 uint16_t sync_seq_id; 414 415 /** Current Path Delay Request sequence id for this port. */ 416 uint16_t pdelay_req_seq_id; 417 418 /** Current Announce sequence id for this port. */ 419 uint16_t announce_seq_id; 420 421 /** Current Signaling sequence id for this port. */ 422 uint16_t signaling_seq_id; 423 424 /** Initial Announce Interval as a Logarithm to base 2. */ 425 int8_t ini_log_announce_itv; 426 427 /** Current Announce Interval as a Logarithm to base 2. */ 428 int8_t cur_log_announce_itv; 429 430 /** Time without receiving announce messages before running BMCA. */ 431 uint8_t announce_receipt_timeout; 432 433 /** Initial Sync Interval as a Logarithm to base 2. */ 434 int8_t ini_log_half_sync_itv; 435 436 /** Current Sync Interval as a Logarithm to base 2. */ 437 int8_t cur_log_half_sync_itv; 438 439 /** Time without receiving sync messages before running BMCA. */ 440 uint8_t sync_receipt_timeout; 441 442 /** Initial Path Delay Request Interval as a Logarithm to base 2. */ 443 int8_t ini_log_pdelay_req_itv; 444 445 /** Current Path Delay Request Interval as a Logarithm to base 2. */ 446 int8_t cur_log_pdelay_req_itv; 447 448 /** Version of PTP running on this port. */ 449 uint8_t version; 450 451 /** Time synchronization and Best Master Selection enabled. */ 452 bool ptt_port_enabled : 1; 453 454 /** Previous status of ptt_port_enabled. */ 455 bool prev_ptt_port_enabled : 1; 456 457 /** The port is measuring the path delay. */ 458 bool is_measuring_delay : 1; 459 460 /** The port is capable of running IEEE802.1AS. */ 461 bool as_capable : 1; 462 463 /** Whether neighborRateRatio needs to be computed for this port. */ 464 bool compute_neighbor_rate_ratio : 1; 465 466 /** Whether neighborPropDelay needs to be computed for this port. */ 467 bool compute_neighbor_prop_delay : 1; 468 469 /** Whether neighbor rate ratio can be used to update clocks. */ 470 bool neighbor_rate_ratio_valid : 1; 471 }; 472 473 /** 474 * @brief Port Parameter Statistics. 475 * 476 * Data Set containing statistics associated with various events. 477 */ 478 struct gptp_port_param_ds { 479 /** Number of Sync messages received. */ 480 uint32_t rx_sync_count; 481 482 /** Number of Follow Up messages received. */ 483 uint32_t rx_fup_count; 484 485 /** Number of Path Delay Requests messages received. */ 486 uint32_t rx_pdelay_req_count; 487 488 /** Number of Path Delay Response messages received. */ 489 uint32_t rx_pdelay_resp_count; 490 491 /** Number of Path Delay Follow Up messages received. */ 492 uint32_t rx_pdelay_resp_fup_count; 493 494 /** Number of Announce messages received. */ 495 uint32_t rx_announce_count; 496 497 /** Number of ptp messages discarded. */ 498 uint32_t rx_ptp_packet_discard_count; 499 500 /** Number of Sync reception timeout. */ 501 uint32_t sync_receipt_timeout_count; 502 503 /** Number of Announce reception timeout. */ 504 uint32_t announce_receipt_timeout_count; 505 506 /** Number Path Delay Requests without a response. */ 507 uint32_t pdelay_allowed_lost_resp_exceed_count; 508 509 /** Number of Sync messages sent. */ 510 uint32_t tx_sync_count; 511 512 /** Number of Follow Up messages sent. */ 513 uint32_t tx_fup_count; 514 515 /** Number of Path Delay Request messages sent. */ 516 uint32_t tx_pdelay_req_count; 517 518 /** Number of Path Delay Response messages sent. */ 519 uint32_t tx_pdelay_resp_count; 520 521 /** Number of Path Delay Response messages sent. */ 522 uint32_t tx_pdelay_resp_fup_count; 523 524 /** Number of Announce messages sent. */ 525 uint32_t tx_announce_count; 526 527 /** Neighbor propagation delay threshold exceeded. */ 528 uint32_t neighbor_prop_delay_exceeded; 529 }; 530 531 /** 532 * @brief gPTP domain. 533 * 534 * Data Set containing all the information necessary to represent 535 * one time-aware system domain. 536 */ 537 struct gptp_domain { 538 /** Global Data Set for this gPTP domain. */ 539 struct gptp_global_ds global_ds; 540 541 /** Default Data Set for this gPTP domain. */ 542 struct gptp_default_ds default_ds; 543 544 /** Current Data Set for this gPTP domain. */ 545 struct gptp_current_ds current_ds; 546 547 /** Parent Data Set for this gPTP domain. */ 548 struct gptp_parent_ds parent_ds; 549 550 /** Time Properties Data Set for this gPTP domain. */ 551 struct gptp_time_prop_ds properties_ds; 552 553 /** Current State of the MI State Machines for this gPTP domain. */ 554 struct gptp_states state; 555 556 /** Port Parameter Data Sets for this gPTP domain. */ 557 struct gptp_port_ds port_ds[CONFIG_NET_GPTP_NUM_PORTS]; 558 559 #if defined(CONFIG_NET_GPTP_STATISTICS) 560 /** Port Parameter Statistics Data Sets for this gPTP domain. */ 561 struct gptp_port_param_ds port_param_ds[CONFIG_NET_GPTP_NUM_PORTS]; 562 #endif /* CONFIG_NET_GPTP_STATISTICS */ 563 564 /** Current States of the MD State Machines for this gPTP domain. */ 565 struct gptp_port_states port_state[CONFIG_NET_GPTP_NUM_PORTS]; 566 567 /** Shared data between BMCA State Machines for this gPTP domain. */ 568 struct gptp_port_bmca_data port_bmca_data[CONFIG_NET_GPTP_NUM_PORTS]; 569 570 /* Network interface linked to the PTP PORT. */ 571 struct net_if *iface[CONFIG_NET_GPTP_NUM_PORTS]; 572 }; 573 574 /** 575 * @brief Get port specific data from gPTP domain. 576 * @details This contains all the configuration / status of the gPTP domain. 577 * 578 * @param domain gPTP domain 579 * @param port Port id 580 * @param port_ds Port specific parameter data set (returned to caller) 581 * @param port_param_ds Port parameter statistics data set (returned to caller) 582 * @param port_state Port specific states data set (returned to caller) 583 * @param port_bmca Port BMCA state machine specific data (returned to caller) 584 * @param iface Port specific parameter data set (returned to caller) 585 * 586 * @return 0 if ok, < 0 if error 587 */ 588 int gptp_get_port_data(struct gptp_domain *domain, int port, 589 struct gptp_port_ds **port_ds, 590 struct gptp_port_param_ds **port_param_ds, 591 struct gptp_port_states **port_state, 592 struct gptp_port_bmca_data **port_bmca_data, 593 struct net_if **iface); 594 595 #ifdef __cplusplus 596 } 597 #endif 598 599 #endif /* CONFIG_NET_GPTP */ 600 601 #endif /* __GPTP_DS_H */ 602