1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ 2 3 #if HAVE_CONFIG_H 4 #include "config.h" 5 #endif 6 7 #if HAVE_ENDIAN_H 8 #include <endian.h> 9 #endif 10 11 #include <assert.h> 12 #include <err.h> 13 #include <errno.h> 14 #include <inttypes.h> 15 #include <stdbool.h> 16 #include <stdlib.h> 17 #include <string.h> 18 19 #define pr_fmt(x) "astlpc: " x 20 21 #include "libmctp.h" 22 #include "libmctp-alloc.h" 23 #include "libmctp-log.h" 24 #include "libmctp-astlpc.h" 25 #include "container_of.h" 26 #include "range.h" 27 28 #ifdef MCTP_HAVE_FILEIO 29 30 #include <unistd.h> 31 #include <fcntl.h> 32 #include <sys/ioctl.h> 33 #include <sys/mman.h> 34 #include <linux/aspeed-lpc-ctrl.h> 35 36 /* kernel interface */ 37 static const char *kcs_path = "/dev/mctp0"; 38 static const char *lpc_path = "/dev/aspeed-lpc-ctrl"; 39 40 #endif 41 42 struct mctp_astlpc_buffer { 43 uint32_t offset; 44 uint32_t size; 45 }; 46 47 struct mctp_astlpc_layout { 48 struct mctp_astlpc_buffer rx; 49 struct mctp_astlpc_buffer tx; 50 }; 51 52 struct mctp_binding_astlpc { 53 struct mctp_binding binding; 54 55 void *lpc_map; 56 struct mctp_astlpc_layout layout; 57 58 uint8_t mode; 59 uint16_t version; 60 uint32_t requested_mtu; 61 62 /* direct ops data */ 63 struct mctp_binding_astlpc_ops ops; 64 void *ops_data; 65 66 /* fileio ops data */ 67 int kcs_fd; 68 uint8_t kcs_status; 69 70 bool running; 71 }; 72 73 #define binding_to_astlpc(b) \ 74 container_of(b, struct mctp_binding_astlpc, binding) 75 76 #define astlpc_prlog(ctx, lvl, fmt, ...) \ 77 do { \ 78 bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \ 79 mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \ 80 ##__VA_ARGS__); \ 81 } while (0) 82 83 #define astlpc_prerr(ctx, fmt, ...) \ 84 astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__) 85 #define astlpc_prwarn(ctx, fmt, ...) \ 86 astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__) 87 #define astlpc_prinfo(ctx, fmt, ...) \ 88 astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__) 89 #define astlpc_prdebug(ctx, fmt, ...) \ 90 astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__) 91 92 /* clang-format off */ 93 #define ASTLPC_MCTP_MAGIC 0x4d435450 94 #define ASTLPC_VER_BAD 0 95 #define ASTLPC_VER_MIN 1 96 97 /* Support testing of new binding protocols */ 98 #ifndef ASTLPC_VER_CUR 99 #define ASTLPC_VER_CUR 2 100 #endif 101 102 #define ASTLPC_PACKET_SIZE(sz) (4 + (sz)) 103 #define ASTLPC_BODY_SIZE(sz) ((sz) - 4) 104 /* clang-format on */ 105 106 struct mctp_lpcmap_hdr { 107 uint32_t magic; 108 109 uint16_t bmc_ver_min; 110 uint16_t bmc_ver_cur; 111 uint16_t host_ver_min; 112 uint16_t host_ver_cur; 113 uint16_t negotiated_ver; 114 uint16_t pad0; 115 116 struct { 117 uint32_t rx_offset; 118 uint32_t rx_size; 119 uint32_t tx_offset; 120 uint32_t tx_size; 121 } layout; 122 } __attribute__((packed)); 123 124 static const uint32_t control_size = 0x100; 125 126 #define LPC_WIN_SIZE (1 * 1024 * 1024) 127 128 #define KCS_STATUS_BMC_READY 0x80 129 #define KCS_STATUS_CHANNEL_ACTIVE 0x40 130 #define KCS_STATUS_IBF 0x02 131 #define KCS_STATUS_OBF 0x01 132 133 static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc, 134 enum mctp_binding_astlpc_kcs_reg reg, 135 uint8_t val) 136 { 137 return astlpc->ops.kcs_write(astlpc->ops_data, reg, val); 138 } 139 140 static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc, 141 enum mctp_binding_astlpc_kcs_reg reg, 142 uint8_t *val) 143 { 144 return astlpc->ops.kcs_read(astlpc->ops_data, reg, val); 145 } 146 147 static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc, 148 const void *buf, long offset, 149 size_t len) 150 { 151 astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset); 152 153 assert(offset >= 0); 154 155 /* Indirect access */ 156 if (astlpc->ops.lpc_write) { 157 void *data = astlpc->ops_data; 158 159 return astlpc->ops.lpc_write(data, buf, offset, len); 160 } 161 162 /* Direct mapping */ 163 assert(astlpc->lpc_map); 164 memcpy(&((char *)astlpc->lpc_map)[offset], buf, len); 165 166 return 0; 167 } 168 169 static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc, 170 void *buf, long offset, size_t len) 171 { 172 astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len, 173 offset); 174 175 assert(offset >= 0); 176 177 /* Indirect access */ 178 if (astlpc->ops.lpc_read) { 179 void *data = astlpc->ops_data; 180 181 return astlpc->ops.lpc_read(data, buf, offset, len); 182 } 183 184 /* Direct mapping */ 185 assert(astlpc->lpc_map); 186 memcpy(buf, &((char *)astlpc->lpc_map)[offset], len); 187 188 return 0; 189 } 190 191 static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc, 192 uint8_t status) 193 { 194 uint8_t data; 195 int rc; 196 197 /* Since we're setting the status register, we want the other endpoint 198 * to be interrupted. However, some hardware may only raise a host-side 199 * interrupt on an ODR event. 200 * So, write a dummy value of 0xff to ODR, which will ensure that an 201 * interrupt is triggered, and can be ignored by the host. 202 */ 203 data = 0xff; 204 205 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status); 206 if (rc) { 207 astlpc_prwarn(astlpc, "KCS status write failed"); 208 return -1; 209 } 210 211 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data); 212 if (rc) { 213 astlpc_prwarn(astlpc, "KCS dummy data write failed"); 214 return -1; 215 } 216 217 return 0; 218 } 219 220 static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc, 221 struct mctp_astlpc_layout *layout) 222 { 223 struct mctp_lpcmap_hdr hdr; 224 int rc; 225 226 rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); 227 if (rc < 0) 228 return rc; 229 230 /* Flip the buffers as the names are defined in terms of the host */ 231 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) { 232 layout->rx.offset = be32toh(hdr.layout.tx_offset); 233 layout->rx.size = be32toh(hdr.layout.tx_size); 234 layout->tx.offset = be32toh(hdr.layout.rx_offset); 235 layout->tx.size = be32toh(hdr.layout.rx_size); 236 } else { 237 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); 238 239 layout->rx.offset = be32toh(hdr.layout.rx_offset); 240 layout->rx.size = be32toh(hdr.layout.rx_size); 241 layout->tx.offset = be32toh(hdr.layout.tx_offset); 242 layout->tx.size = be32toh(hdr.layout.tx_size); 243 } 244 245 return 0; 246 } 247 248 static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc, 249 struct mctp_astlpc_layout *layout) 250 { 251 uint32_t rx_size_be; 252 253 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) { 254 struct mctp_lpcmap_hdr hdr; 255 256 /* 257 * Flip the buffers as the names are defined in terms of the 258 * host 259 */ 260 hdr.layout.rx_offset = htobe32(layout->tx.offset); 261 hdr.layout.rx_size = htobe32(layout->tx.size); 262 hdr.layout.tx_offset = htobe32(layout->rx.offset); 263 hdr.layout.tx_size = htobe32(layout->rx.size); 264 265 return mctp_astlpc_lpc_write(astlpc, &hdr.layout, 266 offsetof(struct mctp_lpcmap_hdr, layout), 267 sizeof(hdr.layout)); 268 } 269 270 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); 271 272 /* 273 * As of v2 we only need to write rx_size - the offsets are controlled 274 * by the BMC, as is the BMC's rx_size (host tx_size). 275 */ 276 rx_size_be = htobe32(layout->rx.size); 277 return mctp_astlpc_lpc_write(astlpc, &rx_size_be, 278 offsetof(struct mctp_lpcmap_hdr, layout.rx_size), 279 sizeof(rx_size_be)); 280 } 281 282 static bool mctp_astlpc_buffer_validate(struct mctp_astlpc_buffer *buf, 283 const char *name) 284 { 285 /* Check for overflow */ 286 if (buf->offset + buf->size < buf->offset) { 287 mctp_prerr( 288 "%s packet buffer parameters overflow: offset: 0x%" PRIx32 289 ", size: %" PRIu32, 290 name, buf->offset, buf->size); 291 return false; 292 } 293 294 /* Check that the buffers are contained within the allocated space */ 295 if (buf->offset + buf->size > LPC_WIN_SIZE) { 296 mctp_prerr( 297 "%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32 298 ", size: %" PRIu32, 299 name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset, 300 buf->size); 301 return false; 302 } 303 304 /* Check that the baseline transmission unit is supported */ 305 if (buf->size < ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU))) { 306 mctp_prerr( 307 "%s packet buffer too small: Require %zu bytes to support the %u byte baseline transmission unit, found %" PRIu32, 308 name, ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU)), 309 MCTP_BTU, buf->size); 310 return false; 311 } 312 313 /* Check for overlap with the control space */ 314 if (buf->offset < control_size) { 315 mctp_prerr( 316 "%s packet buffer overlaps control region {0x%" PRIx32 317 ", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}", 318 name, 0U, control_size, buf->offset, buf->size); 319 return false; 320 } 321 322 return true; 323 } 324 325 static bool mctp_astlpc_layout_validate(struct mctp_astlpc_layout *layout) 326 { 327 struct mctp_astlpc_buffer *rx = &layout->rx; 328 struct mctp_astlpc_buffer *tx = &layout->tx; 329 bool rx_valid, tx_valid; 330 331 rx_valid = mctp_astlpc_buffer_validate(rx, "Rx"); 332 tx_valid = mctp_astlpc_buffer_validate(tx, "Tx"); 333 334 if (!(rx_valid && tx_valid)) 335 return false; 336 337 /* Check that the buffers are disjoint */ 338 if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) || 339 (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) { 340 mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32 341 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}", 342 rx->offset, rx->size, tx->offset, tx->size); 343 return false; 344 } 345 346 return true; 347 } 348 349 static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc) 350 { 351 struct mctp_lpcmap_hdr hdr = { 0 }; 352 uint8_t status; 353 size_t sz; 354 355 /* 356 * The largest buffer size is half of the allocated MCTP space 357 * excluding the control space. 358 */ 359 sz = ((LPC_WIN_SIZE - control_size) / 2); 360 361 /* 362 * Trim the MTU to a multiple of 16 to meet the requirements of 12.17 363 * Query Hop in DSP0236 v1.3.0. 364 */ 365 sz = MCTP_BODY_SIZE(ASTLPC_BODY_SIZE(sz)); 366 sz &= ~0xfUL; 367 sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(sz)); 368 369 if (astlpc->requested_mtu) { 370 size_t r; 371 372 r = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(astlpc->requested_mtu)); 373 sz = MIN(sz, r); 374 } 375 376 /* Flip the buffers as the names are defined in terms of the host */ 377 astlpc->layout.tx.offset = control_size; 378 astlpc->layout.tx.size = sz; 379 astlpc->layout.rx.offset = 380 astlpc->layout.tx.offset + astlpc->layout.tx.size; 381 astlpc->layout.rx.size = sz; 382 383 if (!mctp_astlpc_layout_validate(&astlpc->layout)) { 384 astlpc_prerr(astlpc, "Cannot support an MTU of %zu", sz); 385 return -EINVAL; 386 } 387 388 hdr = (struct mctp_lpcmap_hdr){ 389 .magic = htobe32(ASTLPC_MCTP_MAGIC), 390 .bmc_ver_min = htobe16(ASTLPC_VER_MIN), 391 .bmc_ver_cur = htobe16(ASTLPC_VER_CUR), 392 393 /* Flip the buffers back as we're now describing the host's 394 * configuration to the host */ 395 .layout.rx_offset = htobe32(astlpc->layout.tx.offset), 396 .layout.rx_size = htobe32(astlpc->layout.tx.size), 397 .layout.tx_offset = htobe32(astlpc->layout.rx.offset), 398 .layout.tx_size = htobe32(astlpc->layout.rx.size), 399 }; 400 401 mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr)); 402 403 /* 404 * Set status indicating that the BMC is now active. Be explicit about 405 * clearing OBF; we're reinitialising the binding and so any previous 406 * buffer state is irrelevant. 407 */ 408 status = KCS_STATUS_BMC_READY & ~KCS_STATUS_OBF; 409 return mctp_astlpc_kcs_set_status(astlpc, status); 410 } 411 412 static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b) 413 { 414 struct mctp_binding_astlpc *astlpc = 415 container_of(b, struct mctp_binding_astlpc, binding); 416 417 return mctp_astlpc_init_bmc(astlpc); 418 } 419 420 static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min, 421 uint16_t bmc_ver_cur, 422 uint16_t host_ver_min, 423 uint16_t host_ver_cur) 424 { 425 if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) { 426 mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16 427 "], [%" PRIu16 ", %" PRIu16 "]", 428 bmc_ver_min, bmc_ver_cur, host_ver_min, 429 host_ver_cur); 430 return false; 431 } else if (bmc_ver_min > bmc_ver_cur) { 432 mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16 433 "]", 434 bmc_ver_min, bmc_ver_cur); 435 return false; 436 } else if (host_ver_min > host_ver_cur) { 437 mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16 438 "]", 439 host_ver_min, host_ver_cur); 440 return false; 441 } else if ((host_ver_cur < bmc_ver_min) || 442 (host_ver_min > bmc_ver_cur)) { 443 mctp_prerr( 444 "Unable to satisfy version negotiation with ranges [%" PRIu16 445 ", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]", 446 bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur); 447 return false; 448 } 449 450 return true; 451 } 452 453 static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc) 454 { 455 struct mctp_astlpc_layout layout; 456 uint32_t sz; 457 int rc; 458 459 rc = mctp_astlpc_layout_read(astlpc, &layout); 460 if (rc < 0) 461 return rc; 462 463 if (!mctp_astlpc_layout_validate(&layout)) { 464 astlpc_prerr( 465 astlpc, 466 "BMC provided invalid buffer layout: Rx {0x%" PRIx32 467 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}", 468 layout.rx.offset, layout.rx.size, layout.tx.offset, 469 layout.tx.size); 470 return -EINVAL; 471 } 472 473 astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes", 474 astlpc->requested_mtu); 475 476 sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(astlpc->requested_mtu)); 477 layout.rx.size = sz; 478 479 if (!mctp_astlpc_layout_validate(&layout)) { 480 astlpc_prerr( 481 astlpc, 482 "Generated invalid buffer layout with size %" PRIu32 483 ": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32 484 ", %" PRIu32 "}", 485 sz, layout.rx.offset, layout.rx.size, layout.tx.offset, 486 layout.tx.size); 487 return -EINVAL; 488 } 489 490 astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes", 491 astlpc->requested_mtu); 492 493 return mctp_astlpc_layout_write(astlpc, &layout); 494 } 495 496 static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc) 497 { 498 const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN); 499 const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR); 500 uint16_t bmc_ver_min, bmc_ver_cur; 501 struct mctp_lpcmap_hdr hdr; 502 uint8_t status; 503 int rc; 504 505 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status); 506 if (rc) { 507 mctp_prwarn("KCS status read failed"); 508 return rc; 509 } 510 511 astlpc->kcs_status = status; 512 513 if (!(status & KCS_STATUS_BMC_READY)) 514 return -EHOSTDOWN; 515 516 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); 517 518 bmc_ver_min = be16toh(hdr.bmc_ver_min); 519 bmc_ver_cur = be16toh(hdr.bmc_ver_cur); 520 521 if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur, 522 ASTLPC_VER_MIN, ASTLPC_VER_CUR)) { 523 astlpc_prerr(astlpc, "Cannot negotiate with invalid versions"); 524 return -EINVAL; 525 } 526 527 /* 528 * Negotation always chooses the highest protocol version that 529 * satisfies the version constraints. So check whether the BMC supports 530 * v2, and if so, negotiate in v2 style. 531 */ 532 if (ASTLPC_VER_CUR >= 2 && bmc_ver_cur >= 2) { 533 rc = mctp_astlpc_negotiate_layout_host(astlpc); 534 if (rc < 0) 535 return rc; 536 } 537 538 /* Version negotiation */ 539 mctp_astlpc_lpc_write(astlpc, &ver_min_be, 540 offsetof(struct mctp_lpcmap_hdr, host_ver_min), 541 sizeof(ver_min_be)); 542 543 mctp_astlpc_lpc_write(astlpc, &ver_cur_be, 544 offsetof(struct mctp_lpcmap_hdr, host_ver_cur), 545 sizeof(ver_cur_be)); 546 547 /* Send channel init command */ 548 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0); 549 if (rc) { 550 astlpc_prwarn(astlpc, "KCS write failed"); 551 } 552 553 return rc; 554 } 555 556 static int mctp_binding_astlpc_start_host(struct mctp_binding *b) 557 { 558 struct mctp_binding_astlpc *astlpc = 559 container_of(b, struct mctp_binding_astlpc, binding); 560 561 return mctp_astlpc_init_host(astlpc); 562 } 563 564 static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc, 565 uint8_t status, bool is_write) 566 { 567 bool is_bmc; 568 bool ready_state; 569 uint8_t flag; 570 571 is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC); 572 flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF; 573 ready_state = is_write ? 0 : 1; 574 575 return !!(status & flag) == ready_state; 576 } 577 578 static inline bool 579 mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status) 580 { 581 return __mctp_astlpc_kcs_ready(astlpc, status, false); 582 } 583 584 static inline bool 585 mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status) 586 { 587 return __mctp_astlpc_kcs_ready(astlpc, status, true); 588 } 589 590 static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc, 591 uint8_t data) 592 { 593 uint8_t status; 594 int rc; 595 596 for (;;) { 597 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, 598 &status); 599 if (rc) { 600 astlpc_prwarn(astlpc, "KCS status read failed"); 601 return -1; 602 } 603 if (mctp_astlpc_kcs_write_ready(astlpc, status)) 604 break; 605 /* todo: timeout */ 606 } 607 608 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data); 609 if (rc) { 610 astlpc_prwarn(astlpc, "KCS data write failed"); 611 return -1; 612 } 613 614 return 0; 615 } 616 617 static int mctp_binding_astlpc_tx(struct mctp_binding *b, 618 struct mctp_pktbuf *pkt) 619 { 620 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b); 621 uint32_t len, len_be; 622 struct mctp_hdr *hdr; 623 624 hdr = mctp_pktbuf_hdr(pkt); 625 len = mctp_pktbuf_size(pkt); 626 627 astlpc_prdebug(astlpc, 628 "%s: Transmitting %" PRIu32 629 "-byte packet (%hhu, %hhu, 0x%hhx)", 630 __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag); 631 632 if (len > ASTLPC_BODY_SIZE(astlpc->layout.tx.size)) { 633 astlpc_prwarn(astlpc, "invalid TX len 0x%x", len); 634 return -1; 635 } 636 637 len_be = htobe32(len); 638 mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset, 639 sizeof(len_be)); 640 mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len); 641 642 mctp_binding_set_tx_enabled(b, false); 643 644 mctp_astlpc_kcs_send(astlpc, 0x1); 645 return 0; 646 } 647 648 static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min, 649 uint16_t bmc_ver_cur, 650 uint16_t host_ver_min, 651 uint16_t host_ver_cur) 652 { 653 if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur, 654 host_ver_min, host_ver_cur)) 655 return ASTLPC_VER_BAD; 656 657 if (bmc_ver_cur < host_ver_cur) 658 return bmc_ver_cur; 659 660 return host_ver_cur; 661 } 662 663 static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc, 664 struct mctp_astlpc_layout *layout) 665 { 666 uint32_t low, high, limit; 667 668 /* Derive the largest MTU the BMC _can_ support */ 669 low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset); 670 high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset); 671 limit = high - low; 672 673 /* Determine the largest MTU the BMC _wants_ to support */ 674 if (astlpc->requested_mtu) { 675 uint32_t req = astlpc->requested_mtu; 676 677 limit = MIN(limit, ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(req))); 678 } 679 680 /* Determine the accepted MTU, applied both directions by convention */ 681 return MCTP_BODY_SIZE(ASTLPC_BODY_SIZE(MIN(limit, layout->tx.size))); 682 } 683 684 static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc) 685 { 686 struct mctp_astlpc_layout proposed, pending; 687 uint32_t sz, mtu; 688 int rc; 689 690 /* Extract the host's proposed layout */ 691 rc = mctp_astlpc_layout_read(astlpc, &proposed); 692 if (rc < 0) 693 return rc; 694 695 if (!mctp_astlpc_layout_validate(&proposed)) 696 return -EINVAL; 697 698 /* Negotiate the MTU */ 699 mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed); 700 sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(mtu)); 701 702 /* 703 * Use symmetric MTUs by convention and to pass constraints in rx/tx 704 * functions 705 */ 706 pending = astlpc->layout; 707 pending.tx.size = sz; 708 pending.rx.size = sz; 709 710 if (mctp_astlpc_layout_validate(&pending)) { 711 /* We found a sensible Rx MTU, so honour it */ 712 astlpc->layout = pending; 713 714 /* Enforce the negotiated MTU */ 715 rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout); 716 if (rc < 0) 717 return rc; 718 719 astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes", 720 mtu); 721 } else { 722 astlpc_prwarn(astlpc, "MTU negotiation failed"); 723 return -EINVAL; 724 } 725 726 if (astlpc->version >= 2) 727 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu); 728 729 return 0; 730 } 731 732 static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc) 733 { 734 uint16_t negotiated, negotiated_be; 735 struct mctp_lpcmap_hdr hdr; 736 uint8_t status; 737 int rc; 738 739 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); 740 741 /* Version negotiation */ 742 negotiated = 743 mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR, 744 be16toh(hdr.host_ver_min), 745 be16toh(hdr.host_ver_cur)); 746 747 /* Host Rx MTU negotiation: Failure terminates channel init */ 748 rc = mctp_astlpc_negotiate_layout_bmc(astlpc); 749 if (rc < 0) 750 negotiated = ASTLPC_VER_BAD; 751 752 /* Populate the negotiated version */ 753 astlpc->version = negotiated; 754 negotiated_be = htobe16(negotiated); 755 mctp_astlpc_lpc_write(astlpc, &negotiated_be, 756 offsetof(struct mctp_lpcmap_hdr, negotiated_ver), 757 sizeof(negotiated_be)); 758 759 /* Finalise the configuration */ 760 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF; 761 if (negotiated > 0) { 762 astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16, 763 negotiated); 764 status |= KCS_STATUS_CHANNEL_ACTIVE; 765 } else { 766 astlpc_prerr(astlpc, "Failed to initialise channel\n"); 767 } 768 769 mctp_astlpc_kcs_set_status(astlpc, status); 770 771 mctp_binding_set_tx_enabled(&astlpc->binding, 772 status & KCS_STATUS_CHANNEL_ACTIVE); 773 } 774 775 static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc) 776 { 777 struct mctp_pktbuf *pkt; 778 uint32_t len; 779 780 mctp_astlpc_lpc_read(astlpc, &len, astlpc->layout.rx.offset, 781 sizeof(len)); 782 len = be32toh(len); 783 784 if (len > ASTLPC_BODY_SIZE(astlpc->layout.rx.size)) { 785 astlpc_prwarn(astlpc, "invalid RX len 0x%x", len); 786 return; 787 } 788 789 assert(astlpc->binding.pkt_size >= 0); 790 if (len > (uint32_t)astlpc->binding.pkt_size) { 791 mctp_prwarn("invalid RX len 0x%x", len); 792 astlpc_prwarn(astlpc, "invalid RX len 0x%x", len); 793 return; 794 } 795 796 pkt = mctp_pktbuf_alloc(&astlpc->binding, len); 797 if (!pkt) 798 goto out_complete; 799 800 mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt), 801 astlpc->layout.rx.offset + 4, len); 802 803 mctp_bus_rx(&astlpc->binding, pkt); 804 805 out_complete: 806 mctp_astlpc_kcs_send(astlpc, 0x2); 807 } 808 809 static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc) 810 { 811 mctp_binding_set_tx_enabled(&astlpc->binding, true); 812 } 813 814 static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc) 815 { 816 struct mctp_astlpc_layout layout; 817 uint16_t negotiated; 818 int rc; 819 820 rc = mctp_astlpc_lpc_read(astlpc, &negotiated, 821 offsetof(struct mctp_lpcmap_hdr, 822 negotiated_ver), 823 sizeof(negotiated)); 824 if (rc < 0) 825 return rc; 826 827 negotiated = be16toh(negotiated); 828 829 if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN || 830 negotiated > ASTLPC_VER_CUR) { 831 astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n", 832 negotiated); 833 return -EINVAL; 834 } 835 836 astlpc->version = negotiated; 837 838 rc = mctp_astlpc_layout_read(astlpc, &layout); 839 if (rc < 0) 840 return rc; 841 842 if (!mctp_astlpc_layout_validate(&layout)) { 843 mctp_prerr("BMC proposed invalid buffer parameters"); 844 return -EINVAL; 845 } 846 847 astlpc->layout = layout; 848 849 if (negotiated >= 2) 850 astlpc->binding.pkt_size = 851 ASTLPC_BODY_SIZE(astlpc->layout.tx.size); 852 853 return 0; 854 } 855 856 static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc, 857 uint8_t status) 858 { 859 uint8_t updated; 860 int rc = 0; 861 862 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); 863 864 updated = astlpc->kcs_status ^ status; 865 866 astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__, 867 status, updated); 868 869 if (updated & KCS_STATUS_BMC_READY) { 870 if (status & KCS_STATUS_BMC_READY) { 871 astlpc->kcs_status = status; 872 return astlpc->binding.start(&astlpc->binding); 873 } else { 874 mctp_binding_set_tx_enabled(&astlpc->binding, false); 875 } 876 } 877 878 if (astlpc->version == 0 || updated & KCS_STATUS_CHANNEL_ACTIVE) { 879 bool enable; 880 881 rc = mctp_astlpc_finalise_channel(astlpc); 882 enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0; 883 884 mctp_binding_set_tx_enabled(&astlpc->binding, enable); 885 } 886 887 astlpc->kcs_status = status; 888 889 return rc; 890 } 891 892 int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc) 893 { 894 uint8_t status, data; 895 int rc; 896 897 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status); 898 if (rc) { 899 astlpc_prwarn(astlpc, "KCS read error"); 900 return -1; 901 } 902 903 astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status); 904 905 if (!mctp_astlpc_kcs_read_ready(astlpc, status)) 906 return 0; 907 908 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data); 909 if (rc) { 910 astlpc_prwarn(astlpc, "KCS data read error"); 911 return -1; 912 } 913 914 astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data); 915 916 switch (data) { 917 case 0x0: 918 mctp_astlpc_init_channel(astlpc); 919 break; 920 case 0x1: 921 mctp_astlpc_rx_start(astlpc); 922 break; 923 case 0x2: 924 mctp_astlpc_tx_complete(astlpc); 925 break; 926 case 0xff: 927 /* No responsibilities for the BMC on 0xff */ 928 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) { 929 rc = mctp_astlpc_update_channel(astlpc, status); 930 if (rc < 0) 931 return rc; 932 } 933 break; 934 default: 935 astlpc_prwarn(astlpc, "unknown message 0x%x", data); 936 } 937 938 /* Handle silent loss of bmc-ready */ 939 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) { 940 if (!(status & KCS_STATUS_BMC_READY && data == 0xff)) 941 return mctp_astlpc_update_channel(astlpc, status); 942 } 943 944 return rc; 945 } 946 947 /* allocate and basic initialisation */ 948 static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode, 949 uint32_t mtu) 950 { 951 struct mctp_binding_astlpc *astlpc; 952 953 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) || 954 (mode == MCTP_BINDING_ASTLPC_MODE_HOST)); 955 956 astlpc = __mctp_alloc(sizeof(*astlpc)); 957 if (!astlpc) 958 return NULL; 959 960 memset(astlpc, 0, sizeof(*astlpc)); 961 astlpc->mode = mode; 962 astlpc->lpc_map = NULL; 963 astlpc->requested_mtu = mtu; 964 astlpc->binding.name = "astlpc"; 965 astlpc->binding.version = 1; 966 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu); 967 astlpc->binding.pkt_pad = 0; 968 astlpc->binding.tx = mctp_binding_astlpc_tx; 969 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC) 970 astlpc->binding.start = mctp_binding_astlpc_start_bmc; 971 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST) 972 astlpc->binding.start = mctp_binding_astlpc_start_host; 973 else { 974 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode); 975 __mctp_free(astlpc); 976 return NULL; 977 } 978 979 return astlpc; 980 } 981 982 struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b) 983 { 984 return &b->binding; 985 } 986 987 struct mctp_binding_astlpc * 988 mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map, 989 const struct mctp_binding_astlpc_ops *ops, void *ops_data) 990 { 991 struct mctp_binding_astlpc *astlpc; 992 993 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC || 994 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) { 995 mctp_prerr("Unknown binding mode: %u", mode); 996 return NULL; 997 } 998 999 astlpc = __mctp_astlpc_init(mode, mtu); 1000 if (!astlpc) 1001 return NULL; 1002 1003 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops)); 1004 astlpc->ops_data = ops_data; 1005 astlpc->lpc_map = lpc_map; 1006 astlpc->mode = mode; 1007 1008 return astlpc; 1009 } 1010 1011 struct mctp_binding_astlpc * 1012 mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data, 1013 void *lpc_map) 1014 { 1015 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map, 1016 ops, ops_data); 1017 } 1018 1019 void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc) 1020 { 1021 /* Clear channel-active and bmc-ready */ 1022 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) 1023 mctp_astlpc_kcs_set_status(astlpc, KCS_STATUS_OBF); 1024 __mctp_free(astlpc); 1025 } 1026 1027 #ifdef MCTP_HAVE_FILEIO 1028 1029 static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc) 1030 { 1031 struct aspeed_lpc_ctrl_mapping map = { 1032 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY, 1033 .window_id = 0, /* There's only one */ 1034 .flags = 0, 1035 .addr = 0, 1036 .offset = 0, 1037 .size = 0 1038 }; 1039 void *lpc_map_base; 1040 int fd, rc; 1041 1042 fd = open(lpc_path, O_RDWR | O_SYNC); 1043 if (fd < 0) { 1044 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path); 1045 return -1; 1046 } 1047 1048 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map); 1049 if (rc) { 1050 astlpc_prwarn(astlpc, "LPC GET_SIZE failed"); 1051 close(fd); 1052 return -1; 1053 } 1054 1055 lpc_map_base = 1056 mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 1057 if (lpc_map_base == MAP_FAILED) { 1058 astlpc_prwarn(astlpc, "LPC mmap failed"); 1059 rc = -1; 1060 } else { 1061 astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE; 1062 } 1063 1064 close(fd); 1065 1066 return rc; 1067 } 1068 1069 static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc) 1070 { 1071 astlpc->kcs_fd = open(kcs_path, O_RDWR); 1072 if (astlpc->kcs_fd < 0) 1073 return -1; 1074 1075 return 0; 1076 } 1077 1078 static int __mctp_astlpc_fileio_kcs_read(void *arg, 1079 enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val) 1080 { 1081 struct mctp_binding_astlpc *astlpc = arg; 1082 off_t offset = reg; 1083 int rc; 1084 1085 rc = pread(astlpc->kcs_fd, val, 1, offset); 1086 1087 return rc == 1 ? 0 : -1; 1088 } 1089 1090 static int __mctp_astlpc_fileio_kcs_write(void *arg, 1091 enum mctp_binding_astlpc_kcs_reg reg, uint8_t val) 1092 { 1093 struct mctp_binding_astlpc *astlpc = arg; 1094 off_t offset = reg; 1095 int rc; 1096 1097 rc = pwrite(astlpc->kcs_fd, &val, 1, offset); 1098 1099 return rc == 1 ? 0 : -1; 1100 } 1101 1102 int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc) 1103 { 1104 return astlpc->kcs_fd; 1105 } 1106 1107 struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void) 1108 { 1109 struct mctp_binding_astlpc *astlpc; 1110 int rc; 1111 1112 /* 1113 * If we're doing file IO then we're very likely not running 1114 * freestanding, so lets assume that we're on the BMC side. 1115 * 1116 * Requesting an MTU of 0 requests the largest possible MTU, whatever 1117 * value that might take. 1118 */ 1119 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0); 1120 if (!astlpc) 1121 return NULL; 1122 1123 /* Set internal operations for kcs. We use direct accesses to the lpc 1124 * map area */ 1125 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read; 1126 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write; 1127 astlpc->ops_data = astlpc; 1128 1129 rc = mctp_astlpc_init_fileio_lpc(astlpc); 1130 if (rc) { 1131 free(astlpc); 1132 return NULL; 1133 } 1134 1135 rc = mctp_astlpc_init_fileio_kcs(astlpc); 1136 if (rc) { 1137 free(astlpc); 1138 return NULL; 1139 } 1140 1141 return astlpc; 1142 } 1143 #else 1144 struct mctp_binding_astlpc * __attribute__((const)) 1145 mctp_astlpc_init_fileio(void) 1146 { 1147 astlpc_prerr(astlpc, "Missing support for file IO"); 1148 return NULL; 1149 } 1150 1151 int __attribute__((const)) mctp_astlpc_get_fd( 1152 struct mctp_binding_astlpc *astlpc __attribute__((unused))) 1153 { 1154 astlpc_prerr(astlpc, "Missing support for file IO"); 1155 return -1; 1156 } 1157 #endif 1158