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 "container_of.h" 22 #include "crc32.h" 23 #include "libmctp.h" 24 #include "libmctp-alloc.h" 25 #include "libmctp-log.h" 26 #include "libmctp-astlpc.h" 27 #include "range.h" 28 29 #ifdef MCTP_HAVE_FILEIO 30 31 #include <unistd.h> 32 #include <fcntl.h> 33 #include <poll.h> 34 #include <sys/ioctl.h> 35 #include <sys/mman.h> 36 #include <linux/aspeed-lpc-ctrl.h> 37 38 /* kernel interface */ 39 static const char *kcs_path = "/dev/mctp0"; 40 static const char *lpc_path = "/dev/aspeed-lpc-ctrl"; 41 42 #endif 43 44 enum mctp_astlpc_buffer_state { 45 /* 46 * Prior to "Channel Ready" we mark the buffers as "idle" to catch illegal accesses. In this 47 * state neither side is considered the owner of the buffer. 48 * 49 * Upon "Channel Ready", each side transitions the buffers from the initial "idle" state 50 * to the following target states: 51 * 52 * Tx buffer: "acquired" 53 * Rx buffer: "released" 54 */ 55 buffer_state_idle, 56 57 /* 58 * Beyond initialisation by "Channel Ready", buffers are in the "acquired" state once: 59 * 60 * 1. We dequeue a control command transferring the buffer to our ownership out of the KCS 61 * interface, and 62 * 2. We are yet to complete all of our required accesses to the buffer 63 * 64 * * The Tx buffer enters the "acquired" state when we dequeue the "Rx Complete" command 65 * * The Rx buffer enters the "acquired" state when we dequeue the "Tx Begin" command 66 * 67 * It is a failure of implementation if it's possible for both sides to simultaneously 68 * consider a buffer as "acquired". 69 */ 70 buffer_state_acquired, 71 72 /* 73 * Buffers are in the "prepared" state when: 74 * 75 * 1. We have completed all of our required accesses (read or write) for the buffer, and 76 * 2. We have not yet successfully enqueued the control command to hand off ownership 77 */ 78 buffer_state_prepared, 79 80 /* 81 * Beyond initialisation by "Channel Ready", buffers are in the "released" state once: 82 * 83 * 1. We successfully enqueue the control command transferring ownership to the remote 84 * side in to the KCS interface 85 * 86 * * The Tx buffer enters the "released" state when we enqueue the "Tx Begin" command 87 * * The Rx buffer enters the "released" state when we enqueue the "Rx Complete" command 88 * 89 * It may be the case that both sides simultaneously consider a buffer to be in the 90 * "released" state. However, if this is true, it must also be true that a buffer ownership 91 * transfer command has been enqueued in the KCS interface and is yet to be dequeued. 92 */ 93 buffer_state_released, 94 }; 95 96 struct mctp_astlpc_buffer { 97 uint32_t offset; 98 uint32_t size; 99 enum mctp_astlpc_buffer_state state; 100 }; 101 102 struct mctp_astlpc_layout { 103 struct mctp_astlpc_buffer rx; 104 struct mctp_astlpc_buffer tx; 105 }; 106 107 struct mctp_astlpc_protocol { 108 uint16_t version; 109 uint32_t (*packet_size)(uint32_t body); 110 uint32_t (*body_size)(uint32_t packet); 111 void (*pktbuf_protect)(struct mctp_pktbuf *pkt); 112 bool (*pktbuf_validate)(struct mctp_pktbuf *pkt); 113 }; 114 115 struct mctp_binding_astlpc { 116 struct mctp_binding binding; 117 118 void *lpc_map; 119 struct mctp_astlpc_layout layout; 120 121 uint8_t mode; 122 uint32_t requested_mtu; 123 124 const struct mctp_astlpc_protocol *proto; 125 126 /* direct ops data */ 127 struct mctp_binding_astlpc_ops ops; 128 void *ops_data; 129 130 /* fileio ops data */ 131 int kcs_fd; 132 uint8_t kcs_status; 133 }; 134 135 #define binding_to_astlpc(b) \ 136 container_of(b, struct mctp_binding_astlpc, binding) 137 138 #define astlpc_prlog(ctx, lvl, fmt, ...) \ 139 do { \ 140 bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \ 141 mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \ 142 ##__VA_ARGS__); \ 143 } while (0) 144 145 #define astlpc_prerr(ctx, fmt, ...) \ 146 astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__) 147 #define astlpc_prwarn(ctx, fmt, ...) \ 148 astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__) 149 #define astlpc_prinfo(ctx, fmt, ...) \ 150 astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__) 151 #define astlpc_prdebug(ctx, fmt, ...) \ 152 astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__) 153 154 /* clang-format off */ 155 #define ASTLPC_MCTP_MAGIC 0x4d435450 156 #define ASTLPC_VER_BAD 0 157 #define ASTLPC_VER_MIN 1 158 159 /* Support testing of new binding protocols */ 160 #ifndef ASTLPC_VER_CUR 161 #define ASTLPC_VER_CUR 3 162 #endif 163 /* clang-format on */ 164 165 #ifndef ARRAY_SIZE 166 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 167 #endif 168 169 static uint32_t astlpc_packet_size_v1(uint32_t body) 170 { 171 assert((body + 4) > body); 172 173 return body + 4; 174 } 175 176 static uint32_t astlpc_body_size_v1(uint32_t packet) 177 { 178 assert((packet - 4) < packet); 179 180 return packet - 4; 181 } 182 183 void astlpc_pktbuf_protect_v1(struct mctp_pktbuf *pkt) 184 { 185 (void)pkt; 186 } 187 188 bool astlpc_pktbuf_validate_v1(struct mctp_pktbuf *pkt) 189 { 190 (void)pkt; 191 return true; 192 } 193 194 static uint32_t astlpc_packet_size_v3(uint32_t body) 195 { 196 assert((body + 4 + 4) > body); 197 198 return body + 4 + 4; 199 } 200 201 static uint32_t astlpc_body_size_v3(uint32_t packet) 202 { 203 assert((packet - 4 - 4) < packet); 204 205 return packet - 4 - 4; 206 } 207 208 void astlpc_pktbuf_protect_v3(struct mctp_pktbuf *pkt) 209 { 210 uint32_t code; 211 212 code = htobe32(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt))); 213 mctp_prdebug("%s: 0x%" PRIx32, __func__, code); 214 mctp_pktbuf_push(pkt, &code, 4); 215 } 216 217 bool astlpc_pktbuf_validate_v3(struct mctp_pktbuf *pkt) 218 { 219 uint32_t code; 220 void *check; 221 222 code = be32toh(crc32(mctp_pktbuf_hdr(pkt), mctp_pktbuf_size(pkt) - 4)); 223 mctp_prdebug("%s: 0x%" PRIx32, __func__, code); 224 check = mctp_pktbuf_pop(pkt, 4); 225 return check && !memcmp(&code, check, 4); 226 } 227 228 static const struct mctp_astlpc_protocol astlpc_protocol_version[] = { 229 [0] = { 230 .version = 0, 231 .packet_size = NULL, 232 .body_size = NULL, 233 .pktbuf_protect = NULL, 234 .pktbuf_validate = NULL, 235 }, 236 [1] = { 237 .version = 1, 238 .packet_size = astlpc_packet_size_v1, 239 .body_size = astlpc_body_size_v1, 240 .pktbuf_protect = astlpc_pktbuf_protect_v1, 241 .pktbuf_validate = astlpc_pktbuf_validate_v1, 242 }, 243 [2] = { 244 .version = 2, 245 .packet_size = astlpc_packet_size_v1, 246 .body_size = astlpc_body_size_v1, 247 .pktbuf_protect = astlpc_pktbuf_protect_v1, 248 .pktbuf_validate = astlpc_pktbuf_validate_v1, 249 }, 250 [3] = { 251 .version = 3, 252 .packet_size = astlpc_packet_size_v3, 253 .body_size = astlpc_body_size_v3, 254 .pktbuf_protect = astlpc_pktbuf_protect_v3, 255 .pktbuf_validate = astlpc_pktbuf_validate_v3, 256 }, 257 }; 258 259 struct mctp_lpcmap_hdr { 260 uint32_t magic; 261 262 uint16_t bmc_ver_min; 263 uint16_t bmc_ver_cur; 264 uint16_t host_ver_min; 265 uint16_t host_ver_cur; 266 uint16_t negotiated_ver; 267 uint16_t pad0; 268 269 struct { 270 uint32_t rx_offset; 271 uint32_t rx_size; 272 uint32_t tx_offset; 273 uint32_t tx_size; 274 } layout; 275 } __attribute__((packed)); 276 277 static const uint32_t control_size = 0x100; 278 279 #define LPC_WIN_SIZE (1 * 1024 * 1024) 280 281 #define KCS_STATUS_BMC_READY 0x80 282 #define KCS_STATUS_CHANNEL_ACTIVE 0x40 283 #define KCS_STATUS_IBF 0x02 284 #define KCS_STATUS_OBF 0x01 285 286 static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc, 287 enum mctp_binding_astlpc_kcs_reg reg, 288 uint8_t val) 289 { 290 return astlpc->ops.kcs_write(astlpc->ops_data, reg, val); 291 } 292 293 static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc, 294 enum mctp_binding_astlpc_kcs_reg reg, 295 uint8_t *val) 296 { 297 return astlpc->ops.kcs_read(astlpc->ops_data, reg, val); 298 } 299 300 static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc, 301 const void *buf, long offset, 302 size_t len) 303 { 304 astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset); 305 306 assert(offset >= 0); 307 308 /* Indirect access */ 309 if (astlpc->ops.lpc_write) { 310 void *data = astlpc->ops_data; 311 312 return astlpc->ops.lpc_write(data, buf, offset, len); 313 } 314 315 /* Direct mapping */ 316 assert(astlpc->lpc_map); 317 memcpy(&((char *)astlpc->lpc_map)[offset], buf, len); 318 319 return 0; 320 } 321 322 static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc, 323 void *buf, long offset, size_t len) 324 { 325 astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len, 326 offset); 327 328 assert(offset >= 0); 329 330 /* Indirect access */ 331 if (astlpc->ops.lpc_read) { 332 void *data = astlpc->ops_data; 333 334 return astlpc->ops.lpc_read(data, buf, offset, len); 335 } 336 337 /* Direct mapping */ 338 assert(astlpc->lpc_map); 339 memcpy(buf, &((char *)astlpc->lpc_map)[offset], len); 340 341 return 0; 342 } 343 344 static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc, 345 uint8_t status) 346 { 347 uint8_t data; 348 int rc; 349 350 /* Since we're setting the status register, we want the other endpoint 351 * to be interrupted. However, some hardware may only raise a host-side 352 * interrupt on an ODR event. 353 * So, write a dummy value of 0xff to ODR, which will ensure that an 354 * interrupt is triggered, and can be ignored by the host. 355 */ 356 data = 0xff; 357 358 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status); 359 if (rc) { 360 astlpc_prwarn(astlpc, "KCS status write failed"); 361 return -1; 362 } 363 364 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data); 365 if (rc) { 366 astlpc_prwarn(astlpc, "KCS dummy data write failed"); 367 return -1; 368 } 369 370 return 0; 371 } 372 373 static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc, 374 struct mctp_astlpc_layout *layout) 375 { 376 struct mctp_lpcmap_hdr hdr; 377 int rc; 378 379 rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); 380 if (rc < 0) 381 return rc; 382 383 /* Flip the buffers as the names are defined in terms of the host */ 384 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) { 385 layout->rx.offset = be32toh(hdr.layout.tx_offset); 386 layout->rx.size = be32toh(hdr.layout.tx_size); 387 layout->tx.offset = be32toh(hdr.layout.rx_offset); 388 layout->tx.size = be32toh(hdr.layout.rx_size); 389 } else { 390 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); 391 392 layout->rx.offset = be32toh(hdr.layout.rx_offset); 393 layout->rx.size = be32toh(hdr.layout.rx_size); 394 layout->tx.offset = be32toh(hdr.layout.tx_offset); 395 layout->tx.size = be32toh(hdr.layout.tx_size); 396 } 397 398 return 0; 399 } 400 401 static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc, 402 struct mctp_astlpc_layout *layout) 403 { 404 uint32_t rx_size_be; 405 406 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) { 407 struct mctp_lpcmap_hdr hdr; 408 409 /* 410 * Flip the buffers as the names are defined in terms of the 411 * host 412 */ 413 hdr.layout.rx_offset = htobe32(layout->tx.offset); 414 hdr.layout.rx_size = htobe32(layout->tx.size); 415 hdr.layout.tx_offset = htobe32(layout->rx.offset); 416 hdr.layout.tx_size = htobe32(layout->rx.size); 417 418 return mctp_astlpc_lpc_write(astlpc, &hdr.layout, 419 offsetof(struct mctp_lpcmap_hdr, layout), 420 sizeof(hdr.layout)); 421 } 422 423 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); 424 425 /* 426 * As of v2 we only need to write rx_size - the offsets are controlled 427 * by the BMC, as is the BMC's rx_size (host tx_size). 428 */ 429 rx_size_be = htobe32(layout->rx.size); 430 return mctp_astlpc_lpc_write(astlpc, &rx_size_be, 431 offsetof(struct mctp_lpcmap_hdr, layout.rx_size), 432 sizeof(rx_size_be)); 433 } 434 435 static bool 436 mctp_astlpc_buffer_validate(const struct mctp_binding_astlpc *astlpc, 437 const struct mctp_astlpc_buffer *buf, 438 const char *name) 439 { 440 /* Check for overflow */ 441 if (buf->offset + buf->size < buf->offset) { 442 mctp_prerr( 443 "%s packet buffer parameters overflow: offset: 0x%" PRIx32 444 ", size: %" PRIu32, 445 name, buf->offset, buf->size); 446 return false; 447 } 448 449 /* Check that the buffers are contained within the allocated space */ 450 if (buf->offset + buf->size > LPC_WIN_SIZE) { 451 mctp_prerr( 452 "%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32 453 ", size: %" PRIu32, 454 name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset, 455 buf->size); 456 return false; 457 } 458 459 /* Check that the baseline transmission unit is supported */ 460 if (buf->size < astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU))) { 461 mctp_prerr( 462 "%s packet buffer too small: Require %" PRIu32 " bytes to support the %u byte baseline transmission unit, found %" PRIu32, 463 name, 464 astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU)), 465 MCTP_BTU, buf->size); 466 return false; 467 } 468 469 /* Check for overlap with the control space */ 470 if (buf->offset < control_size) { 471 mctp_prerr( 472 "%s packet buffer overlaps control region {0x%" PRIx32 473 ", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}", 474 name, 0U, control_size, buf->offset, buf->size); 475 return false; 476 } 477 478 return true; 479 } 480 481 static bool 482 mctp_astlpc_layout_validate(const struct mctp_binding_astlpc *astlpc, 483 const struct mctp_astlpc_layout *layout) 484 { 485 const struct mctp_astlpc_buffer *rx = &layout->rx; 486 const struct mctp_astlpc_buffer *tx = &layout->tx; 487 bool rx_valid, tx_valid; 488 489 rx_valid = mctp_astlpc_buffer_validate(astlpc, rx, "Rx"); 490 tx_valid = mctp_astlpc_buffer_validate(astlpc, tx, "Tx"); 491 492 if (!(rx_valid && tx_valid)) 493 return false; 494 495 /* Check that the buffers are disjoint */ 496 if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) || 497 (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) { 498 mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32 499 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}", 500 rx->offset, rx->size, tx->offset, tx->size); 501 return false; 502 } 503 504 return true; 505 } 506 507 static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc) 508 { 509 struct mctp_lpcmap_hdr hdr = { 0 }; 510 uint8_t status; 511 uint32_t sz; 512 513 /* 514 * The largest buffer size is half of the allocated MCTP space 515 * excluding the control space. 516 */ 517 sz = ((LPC_WIN_SIZE - control_size) / 2); 518 519 /* 520 * Trim the MTU to a multiple of 16 to meet the requirements of 12.17 521 * Query Hop in DSP0236 v1.3.0. 522 */ 523 sz = MCTP_BODY_SIZE(astlpc->proto->body_size(sz)); 524 sz &= ~0xfUL; 525 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(sz)); 526 527 if (astlpc->requested_mtu) { 528 uint32_t rpkt, rmtu; 529 530 rmtu = astlpc->requested_mtu; 531 rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu)); 532 sz = MIN(sz, rpkt); 533 } 534 535 /* Flip the buffers as the names are defined in terms of the host */ 536 astlpc->layout.tx.offset = control_size; 537 astlpc->layout.tx.size = sz; 538 astlpc->layout.rx.offset = 539 astlpc->layout.tx.offset + astlpc->layout.tx.size; 540 astlpc->layout.rx.size = sz; 541 542 if (!mctp_astlpc_layout_validate(astlpc, &astlpc->layout)) { 543 astlpc_prerr(astlpc, "Cannot support an MTU of %" PRIu32, sz); 544 return -EINVAL; 545 } 546 547 hdr = (struct mctp_lpcmap_hdr){ 548 .magic = htobe32(ASTLPC_MCTP_MAGIC), 549 .bmc_ver_min = htobe16(ASTLPC_VER_MIN), 550 .bmc_ver_cur = htobe16(ASTLPC_VER_CUR), 551 552 /* Flip the buffers back as we're now describing the host's 553 * configuration to the host */ 554 .layout.rx_offset = htobe32(astlpc->layout.tx.offset), 555 .layout.rx_size = htobe32(astlpc->layout.tx.size), 556 .layout.tx_offset = htobe32(astlpc->layout.rx.offset), 557 .layout.tx_size = htobe32(astlpc->layout.rx.size), 558 }; 559 560 mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr)); 561 562 /* 563 * Set status indicating that the BMC is now active. Be explicit about 564 * clearing OBF; we're reinitialising the binding and so any previous 565 * buffer state is irrelevant. 566 */ 567 status = KCS_STATUS_BMC_READY & ~KCS_STATUS_OBF; 568 return mctp_astlpc_kcs_set_status(astlpc, status); 569 } 570 571 static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b) 572 { 573 struct mctp_binding_astlpc *astlpc = 574 container_of(b, struct mctp_binding_astlpc, binding); 575 576 astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_CUR]; 577 578 return mctp_astlpc_init_bmc(astlpc); 579 } 580 581 static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min, 582 uint16_t bmc_ver_cur, 583 uint16_t host_ver_min, 584 uint16_t host_ver_cur) 585 { 586 if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) { 587 mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16 588 "], [%" PRIu16 ", %" PRIu16 "]", 589 bmc_ver_min, bmc_ver_cur, host_ver_min, 590 host_ver_cur); 591 return false; 592 } else if (bmc_ver_min > bmc_ver_cur) { 593 mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16 594 "]", 595 bmc_ver_min, bmc_ver_cur); 596 return false; 597 } else if (host_ver_min > host_ver_cur) { 598 mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16 599 "]", 600 host_ver_min, host_ver_cur); 601 return false; 602 } else if ((host_ver_cur < bmc_ver_min) || 603 (host_ver_min > bmc_ver_cur)) { 604 mctp_prerr( 605 "Unable to satisfy version negotiation with ranges [%" PRIu16 606 ", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]", 607 bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur); 608 return false; 609 } 610 611 return true; 612 } 613 614 static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc) 615 { 616 struct mctp_astlpc_layout layout; 617 uint32_t rmtu; 618 uint32_t sz; 619 int rc; 620 621 rc = mctp_astlpc_layout_read(astlpc, &layout); 622 if (rc < 0) 623 return rc; 624 625 if (!mctp_astlpc_layout_validate(astlpc, &layout)) { 626 astlpc_prerr( 627 astlpc, 628 "BMC provided invalid buffer layout: Rx {0x%" PRIx32 629 ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}", 630 layout.rx.offset, layout.rx.size, layout.tx.offset, 631 layout.tx.size); 632 return -EINVAL; 633 } 634 635 astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes", 636 astlpc->requested_mtu); 637 638 rmtu = astlpc->requested_mtu; 639 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu)); 640 layout.rx.size = sz; 641 642 if (!mctp_astlpc_layout_validate(astlpc, &layout)) { 643 astlpc_prerr( 644 astlpc, 645 "Generated invalid buffer layout with size %" PRIu32 646 ": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32 647 ", %" PRIu32 "}", 648 sz, layout.rx.offset, layout.rx.size, layout.tx.offset, 649 layout.tx.size); 650 return -EINVAL; 651 } 652 653 astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes", 654 astlpc->requested_mtu); 655 656 return mctp_astlpc_layout_write(astlpc, &layout); 657 } 658 659 static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min, 660 uint16_t bmc_ver_cur, 661 uint16_t host_ver_min, 662 uint16_t host_ver_cur) 663 { 664 if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur, 665 host_ver_min, host_ver_cur)) 666 return ASTLPC_VER_BAD; 667 668 if (bmc_ver_cur < host_ver_cur) 669 return bmc_ver_cur; 670 671 return host_ver_cur; 672 } 673 674 static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc) 675 { 676 const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN); 677 const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR); 678 uint16_t bmc_ver_min, bmc_ver_cur, negotiated; 679 struct mctp_lpcmap_hdr hdr; 680 uint8_t status; 681 int rc; 682 683 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status); 684 if (rc) { 685 mctp_prwarn("KCS status read failed"); 686 return rc; 687 } 688 689 astlpc->kcs_status = status; 690 691 if (!(status & KCS_STATUS_BMC_READY)) 692 return -EHOSTDOWN; 693 694 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); 695 696 bmc_ver_min = be16toh(hdr.bmc_ver_min); 697 bmc_ver_cur = be16toh(hdr.bmc_ver_cur); 698 699 /* Calculate the expected value of negotiated_ver */ 700 negotiated = mctp_astlpc_negotiate_version(bmc_ver_min, bmc_ver_cur, 701 ASTLPC_VER_MIN, 702 ASTLPC_VER_CUR); 703 if (!negotiated) { 704 astlpc_prerr(astlpc, "Cannot negotiate with invalid versions"); 705 return -EINVAL; 706 } 707 708 /* Assign protocol ops so we can calculate the packet buffer sizes */ 709 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version)); 710 astlpc->proto = &astlpc_protocol_version[negotiated]; 711 712 /* Negotiate packet buffers in v2 style if the BMC supports it */ 713 if (negotiated >= 2) { 714 rc = mctp_astlpc_negotiate_layout_host(astlpc); 715 if (rc < 0) 716 return rc; 717 } 718 719 /* Advertise the host's supported protocol versions */ 720 mctp_astlpc_lpc_write(astlpc, &ver_min_be, 721 offsetof(struct mctp_lpcmap_hdr, host_ver_min), 722 sizeof(ver_min_be)); 723 724 mctp_astlpc_lpc_write(astlpc, &ver_cur_be, 725 offsetof(struct mctp_lpcmap_hdr, host_ver_cur), 726 sizeof(ver_cur_be)); 727 728 /* Send channel init command */ 729 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0); 730 if (rc) { 731 astlpc_prwarn(astlpc, "KCS write failed"); 732 } 733 734 /* 735 * Configure the host so `astlpc->proto->version == 0` holds until we 736 * receive a subsequent status update from the BMC. Until then, 737 * `astlpc->proto->version == 0` indicates that we're yet to complete 738 * the channel initialisation handshake. 739 * 740 * When the BMC provides a status update with KCS_STATUS_CHANNEL_ACTIVE 741 * set we will assign the appropriate protocol ops struct in accordance 742 * with `negotiated_ver`. 743 */ 744 astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_BAD]; 745 746 return rc; 747 } 748 749 static int mctp_binding_astlpc_start_host(struct mctp_binding *b) 750 { 751 struct mctp_binding_astlpc *astlpc = 752 container_of(b, struct mctp_binding_astlpc, binding); 753 754 return mctp_astlpc_init_host(astlpc); 755 } 756 757 static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc, 758 uint8_t status, bool is_write) 759 { 760 bool is_bmc; 761 bool ready_state; 762 uint8_t flag; 763 764 is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC); 765 flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF; 766 ready_state = is_write ? 0 : 1; 767 768 return !!(status & flag) == ready_state; 769 } 770 771 static inline bool 772 mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status) 773 { 774 return __mctp_astlpc_kcs_ready(astlpc, status, false); 775 } 776 777 static inline bool 778 mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status) 779 { 780 return __mctp_astlpc_kcs_ready(astlpc, status, true); 781 } 782 783 static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc, 784 uint8_t data) 785 { 786 uint8_t status; 787 int rc; 788 789 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, 790 &status); 791 if (rc) { 792 astlpc_prwarn(astlpc, "KCS status read failed"); 793 return -EIO; 794 } 795 if (!mctp_astlpc_kcs_write_ready(astlpc, status)) 796 return -EBUSY; 797 798 rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data); 799 if (rc) { 800 astlpc_prwarn(astlpc, "KCS data write failed"); 801 return -EIO; 802 } 803 804 return 0; 805 } 806 807 static int mctp_binding_astlpc_tx(struct mctp_binding *b, 808 struct mctp_pktbuf *pkt) 809 { 810 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b); 811 uint32_t len, len_be; 812 struct mctp_hdr *hdr; 813 int rc; 814 815 hdr = mctp_pktbuf_hdr(pkt); 816 len = mctp_pktbuf_size(pkt); 817 818 astlpc_prdebug(astlpc, 819 "%s: Transmitting %" PRIu32 820 "-byte packet (%hhu, %hhu, 0x%hhx)", 821 __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag); 822 823 if (len > astlpc->proto->body_size(astlpc->layout.tx.size)) { 824 astlpc_prwarn(astlpc, "invalid TX len %" PRIu32 ": %" PRIu32, len, 825 astlpc->proto->body_size(astlpc->layout.tx.size)); 826 return -1; 827 } 828 829 mctp_binding_set_tx_enabled(b, false); 830 831 len_be = htobe32(len); 832 mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset, 833 sizeof(len_be)); 834 835 astlpc->proto->pktbuf_protect(pkt); 836 len = mctp_pktbuf_size(pkt); 837 838 mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len); 839 840 astlpc->layout.tx.state = buffer_state_prepared; 841 842 rc = mctp_astlpc_kcs_send(astlpc, 0x1); 843 if (!rc) 844 astlpc->layout.tx.state = buffer_state_released; 845 846 return rc == -EBUSY ? 0 : rc; 847 } 848 849 static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc, 850 struct mctp_astlpc_layout *layout) 851 { 852 uint32_t low, high, limit, rpkt; 853 854 /* Derive the largest MTU the BMC _can_ support */ 855 low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset); 856 high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset); 857 limit = high - low; 858 859 /* Determine the largest MTU the BMC _wants_ to support */ 860 if (astlpc->requested_mtu) { 861 uint32_t rmtu = astlpc->requested_mtu; 862 863 rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu)); 864 limit = MIN(limit, rpkt); 865 } 866 867 /* Determine the accepted MTU, applied both directions by convention */ 868 rpkt = MIN(limit, layout->tx.size); 869 return MCTP_BODY_SIZE(astlpc->proto->body_size(rpkt)); 870 } 871 872 static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc) 873 { 874 struct mctp_astlpc_layout proposed, pending; 875 uint32_t sz, mtu; 876 int rc; 877 878 /* Do we have a valid protocol version? */ 879 if (!astlpc->proto->version) 880 return -EINVAL; 881 882 /* Extract the host's proposed layout */ 883 rc = mctp_astlpc_layout_read(astlpc, &proposed); 884 if (rc < 0) 885 return rc; 886 887 /* Do we have a reasonable layout? */ 888 if (!mctp_astlpc_layout_validate(astlpc, &proposed)) 889 return -EINVAL; 890 891 /* Negotiate the MTU */ 892 mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed); 893 sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(mtu)); 894 895 /* 896 * Use symmetric MTUs by convention and to pass constraints in rx/tx 897 * functions 898 */ 899 pending = astlpc->layout; 900 pending.tx.size = sz; 901 pending.rx.size = sz; 902 903 if (mctp_astlpc_layout_validate(astlpc, &pending)) { 904 /* We found a sensible Rx MTU, so honour it */ 905 astlpc->layout = pending; 906 907 /* Enforce the negotiated MTU */ 908 rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout); 909 if (rc < 0) 910 return rc; 911 912 astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes", 913 mtu); 914 } else { 915 astlpc_prwarn(astlpc, "MTU negotiation failed"); 916 return -EINVAL; 917 } 918 919 if (astlpc->proto->version >= 2) 920 astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu); 921 922 return 0; 923 } 924 925 static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc) 926 { 927 uint16_t negotiated, negotiated_be; 928 struct mctp_lpcmap_hdr hdr; 929 uint8_t status; 930 int rc; 931 932 mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); 933 934 /* Version negotiation */ 935 negotiated = 936 mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR, 937 be16toh(hdr.host_ver_min), 938 be16toh(hdr.host_ver_cur)); 939 940 /* MTU negotiation requires knowing which protocol we'll use */ 941 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version)); 942 astlpc->proto = &astlpc_protocol_version[negotiated]; 943 944 /* Host Rx MTU negotiation: Failure terminates channel init */ 945 rc = mctp_astlpc_negotiate_layout_bmc(astlpc); 946 if (rc < 0) 947 negotiated = ASTLPC_VER_BAD; 948 949 /* Populate the negotiated version */ 950 negotiated_be = htobe16(negotiated); 951 mctp_astlpc_lpc_write(astlpc, &negotiated_be, 952 offsetof(struct mctp_lpcmap_hdr, negotiated_ver), 953 sizeof(negotiated_be)); 954 955 /* Track buffer ownership */ 956 astlpc->layout.tx.state = buffer_state_acquired; 957 astlpc->layout.rx.state = buffer_state_released; 958 959 /* Finalise the configuration */ 960 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF; 961 if (negotiated > 0) { 962 astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16, 963 negotiated); 964 status |= KCS_STATUS_CHANNEL_ACTIVE; 965 } else { 966 astlpc_prerr(astlpc, "Failed to initialise channel"); 967 } 968 969 mctp_astlpc_kcs_set_status(astlpc, status); 970 971 mctp_binding_set_tx_enabled(&astlpc->binding, 972 status & KCS_STATUS_CHANNEL_ACTIVE); 973 } 974 975 static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc) 976 { 977 struct mctp_pktbuf *pkt; 978 uint32_t body, packet; 979 980 mctp_astlpc_lpc_read(astlpc, &body, astlpc->layout.rx.offset, 981 sizeof(body)); 982 body = be32toh(body); 983 984 if (body > astlpc->proto->body_size(astlpc->layout.rx.size)) { 985 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body); 986 return; 987 } 988 989 assert(astlpc->binding.pkt_size >= 0); 990 if (body > (uint32_t)astlpc->binding.pkt_size) { 991 astlpc_prwarn(astlpc, "invalid RX len 0x%x", body); 992 return; 993 } 994 995 /* Eliminate the medium-specific header that we just read */ 996 packet = astlpc->proto->packet_size(body) - 4; 997 pkt = mctp_pktbuf_alloc(&astlpc->binding, packet); 998 if (!pkt) { 999 astlpc_prwarn(astlpc, "unable to allocate pktbuf len 0x%x", packet); 1000 return; 1001 } 1002 1003 /* 1004 * Read payload and medium-specific trailer from immediately after the 1005 * medium-specific header. 1006 */ 1007 mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt), 1008 astlpc->layout.rx.offset + 4, packet); 1009 1010 astlpc->layout.rx.state = buffer_state_prepared; 1011 1012 /* Inform the other side of the MCTP interface that we have read 1013 * the packet off the bus before handling the contents of the packet. 1014 */ 1015 if (!mctp_astlpc_kcs_send(astlpc, 0x2)) 1016 astlpc->layout.rx.state = buffer_state_released; 1017 1018 /* 1019 * v3 will validate the CRC32 in the medium-specific trailer and adjust 1020 * the packet size accordingly. On older protocols validation is a no-op 1021 * that always returns true. 1022 */ 1023 if (astlpc->proto->pktbuf_validate(pkt)) { 1024 mctp_bus_rx(&astlpc->binding, pkt); 1025 } else { 1026 /* TODO: Drop any associated assembly */ 1027 mctp_pktbuf_free(pkt); 1028 astlpc_prdebug(astlpc, "Dropped corrupt packet"); 1029 } 1030 } 1031 1032 static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc) 1033 { 1034 astlpc->layout.tx.state = buffer_state_acquired; 1035 mctp_binding_set_tx_enabled(&astlpc->binding, true); 1036 } 1037 1038 static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc) 1039 { 1040 struct mctp_astlpc_layout layout; 1041 uint16_t negotiated; 1042 int rc; 1043 1044 rc = mctp_astlpc_lpc_read(astlpc, &negotiated, 1045 offsetof(struct mctp_lpcmap_hdr, 1046 negotiated_ver), 1047 sizeof(negotiated)); 1048 if (rc < 0) 1049 return rc; 1050 1051 negotiated = be16toh(negotiated); 1052 astlpc_prerr(astlpc, "Version negotiation got: %u", negotiated); 1053 1054 if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN || 1055 negotiated > ASTLPC_VER_CUR) { 1056 astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n", 1057 negotiated); 1058 return -EINVAL; 1059 } 1060 1061 assert(negotiated < ARRAY_SIZE(astlpc_protocol_version)); 1062 astlpc->proto = &astlpc_protocol_version[negotiated]; 1063 1064 rc = mctp_astlpc_layout_read(astlpc, &layout); 1065 if (rc < 0) 1066 return rc; 1067 1068 if (!mctp_astlpc_layout_validate(astlpc, &layout)) { 1069 mctp_prerr("BMC proposed invalid buffer parameters"); 1070 return -EINVAL; 1071 } 1072 1073 astlpc->layout = layout; 1074 1075 if (negotiated >= 2) 1076 astlpc->binding.pkt_size = 1077 astlpc->proto->body_size(astlpc->layout.tx.size); 1078 1079 /* Track buffer ownership */ 1080 astlpc->layout.tx.state = buffer_state_acquired; 1081 astlpc->layout.rx.state = buffer_state_released; 1082 1083 return 0; 1084 } 1085 1086 static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc, 1087 uint8_t status) 1088 { 1089 uint8_t updated; 1090 int rc = 0; 1091 1092 assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); 1093 1094 updated = astlpc->kcs_status ^ status; 1095 1096 astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__, 1097 status, updated); 1098 1099 if (updated & KCS_STATUS_BMC_READY) { 1100 if (status & KCS_STATUS_BMC_READY) { 1101 astlpc->kcs_status = status; 1102 return astlpc->binding.start(&astlpc->binding); 1103 } else { 1104 /* Shut down the channel */ 1105 astlpc->layout.rx.state = buffer_state_idle; 1106 astlpc->layout.tx.state = buffer_state_idle; 1107 mctp_binding_set_tx_enabled(&astlpc->binding, false); 1108 } 1109 } 1110 1111 if (astlpc->proto->version == 0 || 1112 updated & KCS_STATUS_CHANNEL_ACTIVE) { 1113 bool enable; 1114 1115 astlpc->layout.rx.state = buffer_state_idle; 1116 astlpc->layout.tx.state = buffer_state_idle; 1117 rc = mctp_astlpc_finalise_channel(astlpc); 1118 enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0; 1119 mctp_binding_set_tx_enabled(&astlpc->binding, enable); 1120 } 1121 1122 astlpc->kcs_status = status; 1123 1124 return rc; 1125 } 1126 1127 int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc) 1128 { 1129 uint8_t status, data; 1130 int rc; 1131 1132 if (astlpc->layout.rx.state == buffer_state_prepared) 1133 if (!mctp_astlpc_kcs_send(astlpc, 0x2)) 1134 astlpc->layout.rx.state = buffer_state_released; 1135 1136 if (astlpc->layout.tx.state == buffer_state_prepared) 1137 if (!mctp_astlpc_kcs_send(astlpc, 0x1)) 1138 astlpc->layout.tx.state = buffer_state_released; 1139 1140 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status); 1141 if (rc) { 1142 astlpc_prwarn(astlpc, "KCS read error"); 1143 return -1; 1144 } 1145 1146 astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status); 1147 1148 if (!mctp_astlpc_kcs_read_ready(astlpc, status)) 1149 return 0; 1150 1151 rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data); 1152 if (rc) { 1153 astlpc_prwarn(astlpc, "KCS data read error"); 1154 return -1; 1155 } 1156 1157 astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data); 1158 1159 if (!astlpc->proto->version && !(data == 0x0 || data == 0xff)) { 1160 astlpc_prwarn(astlpc, "Invalid message for binding state: 0x%x", 1161 data); 1162 return 0; 1163 } 1164 1165 switch (data) { 1166 case 0x0: 1167 mctp_astlpc_init_channel(astlpc); 1168 break; 1169 case 0x1: 1170 if (astlpc->layout.rx.state != buffer_state_released) { 1171 astlpc_prerr(astlpc, 1172 "Protocol error: Invalid Rx buffer state for event %d: %d\n", 1173 data, astlpc->layout.rx.state); 1174 return 0; 1175 } 1176 mctp_astlpc_rx_start(astlpc); 1177 break; 1178 case 0x2: 1179 if (astlpc->layout.tx.state != buffer_state_released) { 1180 astlpc_prerr(astlpc, 1181 "Protocol error: Invalid Tx buffer state for event %d: %d\n", 1182 data, astlpc->layout.tx.state); 1183 return 0; 1184 } 1185 mctp_astlpc_tx_complete(astlpc); 1186 break; 1187 case 0xff: 1188 /* No responsibilities for the BMC on 0xff */ 1189 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) { 1190 rc = mctp_astlpc_update_channel(astlpc, status); 1191 if (rc < 0) 1192 return rc; 1193 } 1194 break; 1195 default: 1196 astlpc_prwarn(astlpc, "unknown message 0x%x", data); 1197 } 1198 1199 /* Handle silent loss of bmc-ready */ 1200 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) { 1201 if (!(status & KCS_STATUS_BMC_READY && data == 0xff)) 1202 return mctp_astlpc_update_channel(astlpc, status); 1203 } 1204 1205 return rc; 1206 } 1207 1208 /* allocate and basic initialisation */ 1209 static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode, 1210 uint32_t mtu) 1211 { 1212 struct mctp_binding_astlpc *astlpc; 1213 1214 assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) || 1215 (mode == MCTP_BINDING_ASTLPC_MODE_HOST)); 1216 1217 astlpc = __mctp_alloc(sizeof(*astlpc)); 1218 if (!astlpc) 1219 return NULL; 1220 1221 memset(astlpc, 0, sizeof(*astlpc)); 1222 astlpc->mode = mode; 1223 astlpc->lpc_map = NULL; 1224 astlpc->layout.rx.state = buffer_state_idle; 1225 astlpc->layout.tx.state = buffer_state_idle; 1226 astlpc->requested_mtu = mtu; 1227 astlpc->binding.name = "astlpc"; 1228 astlpc->binding.version = 1; 1229 astlpc->binding.pkt_size = 1230 MCTP_PACKET_SIZE(mtu > MCTP_BTU ? mtu : MCTP_BTU); 1231 astlpc->binding.pkt_header = 4; 1232 astlpc->binding.pkt_trailer = 4; 1233 astlpc->binding.tx = mctp_binding_astlpc_tx; 1234 if (mode == MCTP_BINDING_ASTLPC_MODE_BMC) 1235 astlpc->binding.start = mctp_binding_astlpc_start_bmc; 1236 else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST) 1237 astlpc->binding.start = mctp_binding_astlpc_start_host; 1238 else { 1239 astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode); 1240 __mctp_free(astlpc); 1241 return NULL; 1242 } 1243 1244 return astlpc; 1245 } 1246 1247 struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b) 1248 { 1249 return &b->binding; 1250 } 1251 1252 struct mctp_binding_astlpc * 1253 mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map, 1254 const struct mctp_binding_astlpc_ops *ops, void *ops_data) 1255 { 1256 struct mctp_binding_astlpc *astlpc; 1257 1258 if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC || 1259 mode == MCTP_BINDING_ASTLPC_MODE_HOST)) { 1260 mctp_prerr("Unknown binding mode: %u", mode); 1261 return NULL; 1262 } 1263 1264 astlpc = __mctp_astlpc_init(mode, mtu); 1265 if (!astlpc) 1266 return NULL; 1267 1268 memcpy(&astlpc->ops, ops, sizeof(astlpc->ops)); 1269 astlpc->ops_data = ops_data; 1270 astlpc->lpc_map = lpc_map; 1271 astlpc->mode = mode; 1272 1273 return astlpc; 1274 } 1275 1276 struct mctp_binding_astlpc * 1277 mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data, 1278 void *lpc_map) 1279 { 1280 return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map, 1281 ops, ops_data); 1282 } 1283 1284 void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc) 1285 { 1286 /* Clear channel-active and bmc-ready */ 1287 if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) 1288 mctp_astlpc_kcs_set_status(astlpc, 0); 1289 __mctp_free(astlpc); 1290 } 1291 1292 #ifdef MCTP_HAVE_FILEIO 1293 1294 static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc) 1295 { 1296 struct aspeed_lpc_ctrl_mapping map = { 1297 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY, 1298 .window_id = 0, /* There's only one */ 1299 .flags = 0, 1300 .addr = 0, 1301 .offset = 0, 1302 .size = 0 1303 }; 1304 void *lpc_map_base; 1305 int fd, rc; 1306 1307 fd = open(lpc_path, O_RDWR | O_SYNC); 1308 if (fd < 0) { 1309 astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path); 1310 return -1; 1311 } 1312 1313 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map); 1314 if (rc) { 1315 astlpc_prwarn(astlpc, "LPC GET_SIZE failed"); 1316 close(fd); 1317 return -1; 1318 } 1319 1320 /* 1321 * 1322 * 1323 * Decouple ourselves from hiomapd[1] (another user of the FW2AHB) by 1324 * mapping the FW2AHB to the reserved memory here as well. 1325 * 1326 * It's not possible to use the MCTP ASTLPC binding on machines that 1327 * need the FW2AHB bridge mapped anywhere except to the reserved memory 1328 * (e.g. the host SPI NOR). 1329 * 1330 * [1] https://github.com/openbmc/hiomapd/ 1331 * 1332 * 1333 * 1334 * The following calculation must align with what's going on in 1335 * hiomapd's lpc.c so as not to disrupt its behaviour: 1336 * 1337 * https://github.com/openbmc/hiomapd/blob/5ff50e3cbd7702aefc185264e4adfb9952040575/lpc.c#L68 1338 * 1339 * 1340 */ 1341 1342 /* Map the reserved memory at the top of the 28-bit LPC firmware address space */ 1343 map.addr = 0x0FFFFFFF & -map.size; 1344 astlpc_prinfo(astlpc, 1345 "Configuring FW2AHB to map reserved memory at 0x%08x for 0x%x in the LPC FW cycle address-space", 1346 map.addr, map.size); 1347 1348 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map); 1349 if (rc) { 1350 astlpc_prwarn(astlpc, "Failed to map FW2AHB to reserved memory"); 1351 close(fd); 1352 return -1; 1353 } 1354 1355 /* Map the reserved memory into our address space */ 1356 lpc_map_base = 1357 mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 1358 if (lpc_map_base == MAP_FAILED) { 1359 astlpc_prwarn(astlpc, "LPC mmap failed"); 1360 rc = -1; 1361 } else { 1362 astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE; 1363 } 1364 1365 close(fd); 1366 1367 return rc; 1368 } 1369 1370 static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc) 1371 { 1372 astlpc->kcs_fd = open(kcs_path, O_RDWR); 1373 if (astlpc->kcs_fd < 0) 1374 return -1; 1375 1376 return 0; 1377 } 1378 1379 static int __mctp_astlpc_fileio_kcs_read(void *arg, 1380 enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val) 1381 { 1382 struct mctp_binding_astlpc *astlpc = arg; 1383 off_t offset = reg; 1384 int rc; 1385 1386 rc = pread(astlpc->kcs_fd, val, 1, offset); 1387 1388 return rc == 1 ? 0 : -1; 1389 } 1390 1391 static int __mctp_astlpc_fileio_kcs_write(void *arg, 1392 enum mctp_binding_astlpc_kcs_reg reg, uint8_t val) 1393 { 1394 struct mctp_binding_astlpc *astlpc = arg; 1395 off_t offset = reg; 1396 int rc; 1397 1398 rc = pwrite(astlpc->kcs_fd, &val, 1, offset); 1399 1400 return rc == 1 ? 0 : -1; 1401 } 1402 1403 int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc, 1404 struct pollfd *pollfd) 1405 { 1406 bool release; 1407 1408 pollfd->fd = astlpc->kcs_fd; 1409 pollfd->events = 0; 1410 1411 release = astlpc->layout.rx.state == buffer_state_prepared || 1412 astlpc->layout.tx.state == buffer_state_prepared; 1413 1414 pollfd->events = release ? POLLOUT : POLLIN; 1415 1416 return 0; 1417 } 1418 1419 struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void) 1420 { 1421 struct mctp_binding_astlpc *astlpc; 1422 int rc; 1423 1424 /* 1425 * If we're doing file IO then we're very likely not running 1426 * freestanding, so lets assume that we're on the BMC side. 1427 * 1428 * Requesting an MTU of 0 requests the largest possible MTU, whatever 1429 * value that might take. 1430 */ 1431 astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0); 1432 if (!astlpc) 1433 return NULL; 1434 1435 /* Set internal operations for kcs. We use direct accesses to the lpc 1436 * map area */ 1437 astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read; 1438 astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write; 1439 astlpc->ops_data = astlpc; 1440 1441 rc = mctp_astlpc_init_fileio_lpc(astlpc); 1442 if (rc) { 1443 free(astlpc); 1444 return NULL; 1445 } 1446 1447 rc = mctp_astlpc_init_fileio_kcs(astlpc); 1448 if (rc) { 1449 free(astlpc); 1450 return NULL; 1451 } 1452 1453 return astlpc; 1454 } 1455 #else 1456 struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void) 1457 { 1458 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__); 1459 return NULL; 1460 } 1461 1462 int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc __unused, 1463 struct pollfd *pollfd __unused) 1464 { 1465 mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__); 1466 return -1; 1467 } 1468 #endif 1469