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