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 | |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 96 | struct mctp_astlpc_buffer { |
| 97 | uint32_t offset; |
| 98 | uint32_t size; |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 99 | enum mctp_astlpc_buffer_state state; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | struct mctp_astlpc_layout { |
| 103 | struct mctp_astlpc_buffer rx; |
| 104 | struct mctp_astlpc_buffer tx; |
| 105 | }; |
| 106 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 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); |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 111 | void (*pktbuf_protect)(struct mctp_pktbuf *pkt); |
| 112 | bool (*pktbuf_validate)(struct mctp_pktbuf *pkt); |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 113 | }; |
| 114 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 115 | struct mctp_binding_astlpc { |
| 116 | struct mctp_binding binding; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 117 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 118 | void *lpc_map; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 119 | struct mctp_astlpc_layout layout; |
| 120 | |
| 121 | uint8_t mode; |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 122 | uint32_t requested_mtu; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 123 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 124 | const struct mctp_astlpc_protocol *proto; |
| 125 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 126 | /* direct ops data */ |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 127 | struct mctp_binding_astlpc_ops ops; |
| 128 | void *ops_data; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 129 | |
| 130 | /* fileio ops data */ |
Andrew Jeffery | 979c6a1 | 2020-05-23 20:04:49 +0930 | [diff] [blame] | 131 | int kcs_fd; |
| 132 | uint8_t kcs_status; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 133 | }; |
| 134 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 135 | #define binding_to_astlpc(b) \ |
| 136 | container_of(b, struct mctp_binding_astlpc, binding) |
| 137 | |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 154 | /* clang-format off */ |
| 155 | #define ASTLPC_MCTP_MAGIC 0x4d435450 |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 156 | #define ASTLPC_VER_BAD 0 |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 157 | #define ASTLPC_VER_MIN 1 |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 158 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 159 | /* Support testing of new binding protocols */ |
| 160 | #ifndef ASTLPC_VER_CUR |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 161 | #define ASTLPC_VER_CUR 3 |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 162 | #endif |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 163 | /* clang-format on */ |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 164 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 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 | |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 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 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 228 | static const struct mctp_astlpc_protocol astlpc_protocol_version[] = { |
| 229 | [0] = { |
| 230 | .version = 0, |
| 231 | .packet_size = NULL, |
| 232 | .body_size = NULL, |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 233 | .pktbuf_protect = NULL, |
| 234 | .pktbuf_validate = NULL, |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 235 | }, |
| 236 | [1] = { |
| 237 | .version = 1, |
| 238 | .packet_size = astlpc_packet_size_v1, |
| 239 | .body_size = astlpc_body_size_v1, |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 240 | .pktbuf_protect = astlpc_pktbuf_protect_v1, |
| 241 | .pktbuf_validate = astlpc_pktbuf_validate_v1, |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 242 | }, |
| 243 | [2] = { |
| 244 | .version = 2, |
| 245 | .packet_size = astlpc_packet_size_v1, |
| 246 | .body_size = astlpc_body_size_v1, |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 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, |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 256 | }, |
| 257 | }; |
| 258 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 259 | struct mctp_lpcmap_hdr { |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 260 | uint32_t magic; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 261 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 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; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 268 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 269 | struct { |
| 270 | uint32_t rx_offset; |
| 271 | uint32_t rx_size; |
| 272 | uint32_t tx_offset; |
| 273 | uint32_t tx_size; |
| 274 | } layout; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 275 | } __attribute__((packed)); |
| 276 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 277 | static const uint32_t control_size = 0x100; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 278 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 279 | #define LPC_WIN_SIZE (1 * 1024 * 1024) |
| 280 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 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 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 300 | static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc, |
| 301 | const void *buf, long offset, |
| 302 | size_t len) |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 303 | { |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 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; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 342 | } |
| 343 | |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 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; |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 357 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 358 | rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status); |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 359 | if (rc) { |
| 360 | astlpc_prwarn(astlpc, "KCS status write failed"); |
| 361 | return -1; |
| 362 | } |
| 363 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 364 | rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data); |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 365 | if (rc) { |
| 366 | astlpc_prwarn(astlpc, "KCS dummy data write failed"); |
| 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 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) |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 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 */ |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 460 | if (buf->size < astlpc->proto->packet_size(MCTP_PACKET_SIZE(MCTP_BTU))) { |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 461 | mctp_prerr( |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 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)), |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 481 | static bool |
| 482 | mctp_astlpc_layout_validate(const struct mctp_binding_astlpc *astlpc, |
| 483 | const struct mctp_astlpc_layout *layout) |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 484 | { |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 485 | const struct mctp_astlpc_buffer *rx = &layout->rx; |
| 486 | const struct mctp_astlpc_buffer *tx = &layout->tx; |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 487 | bool rx_valid, tx_valid; |
| 488 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 489 | rx_valid = mctp_astlpc_buffer_validate(astlpc, rx, "Rx"); |
| 490 | tx_valid = mctp_astlpc_buffer_validate(astlpc, tx, "Tx"); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 507 | static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc) |
| 508 | { |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 509 | struct mctp_lpcmap_hdr hdr = { 0 }; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 510 | uint8_t status; |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 511 | uint32_t sz; |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 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 | */ |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 523 | sz = MCTP_BODY_SIZE(astlpc->proto->body_size(sz)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 524 | sz &= ~0xfUL; |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 525 | sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(sz)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 526 | |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 527 | if (astlpc->requested_mtu) { |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 528 | uint32_t rpkt, rmtu; |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 529 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 530 | rmtu = astlpc->requested_mtu; |
| 531 | rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu)); |
| 532 | sz = MIN(sz, rpkt); |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 533 | } |
| 534 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 535 | /* 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] | 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 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 542 | if (!mctp_astlpc_layout_validate(astlpc, &astlpc->layout)) { |
| 543 | astlpc_prerr(astlpc, "Cannot support an MTU of %" PRIu32, sz); |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 544 | return -EINVAL; |
| 545 | } |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 546 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 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), |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 551 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 552 | /* Flip the buffers back as we're now describing the host's |
| 553 | * configuration to the host */ |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 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), |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 558 | }; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 559 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 560 | mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 561 | |
Andrew Jeffery | b3b55a6 | 2020-07-06 13:34:18 +0930 | [diff] [blame] | 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; |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 568 | return mctp_astlpc_kcs_set_status(astlpc, status); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 576 | astlpc->proto = &astlpc_protocol_version[ASTLPC_VER_CUR]; |
| 577 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 578 | return mctp_astlpc_init_bmc(astlpc); |
| 579 | } |
| 580 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 614 | static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc) |
| 615 | { |
| 616 | struct mctp_astlpc_layout layout; |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 617 | uint32_t rmtu; |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 618 | uint32_t sz; |
| 619 | int rc; |
| 620 | |
| 621 | rc = mctp_astlpc_layout_read(astlpc, &layout); |
| 622 | if (rc < 0) |
| 623 | return rc; |
| 624 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 625 | if (!mctp_astlpc_layout_validate(astlpc, &layout)) { |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 635 | astlpc_prinfo(astlpc, "Desire an MTU of %" PRIu32 " bytes", |
| 636 | astlpc->requested_mtu); |
| 637 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 638 | rmtu = astlpc->requested_mtu; |
| 639 | sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 640 | layout.rx.size = sz; |
| 641 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 642 | if (!mctp_astlpc_layout_validate(astlpc, &layout)) { |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 653 | astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes", |
| 654 | astlpc->requested_mtu); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 655 | |
| 656 | return mctp_astlpc_layout_write(astlpc, &layout); |
| 657 | } |
| 658 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 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 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 674 | static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc) |
| 675 | { |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 676 | const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN); |
| 677 | const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR); |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 678 | uint16_t bmc_ver_min, bmc_ver_cur, negotiated; |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 679 | struct mctp_lpcmap_hdr hdr; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 680 | uint8_t status; |
| 681 | int rc; |
| 682 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 683 | rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 694 | mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 695 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 696 | bmc_ver_min = be16toh(hdr.bmc_ver_min); |
| 697 | bmc_ver_cur = be16toh(hdr.bmc_ver_cur); |
| 698 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 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) { |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 704 | astlpc_prerr(astlpc, "Cannot negotiate with invalid versions"); |
| 705 | return -EINVAL; |
| 706 | } |
| 707 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 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) { |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 714 | rc = mctp_astlpc_negotiate_layout_host(astlpc); |
| 715 | if (rc < 0) |
| 716 | return rc; |
| 717 | } |
| 718 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 719 | /* Advertise the host's supported protocol versions */ |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 720 | mctp_astlpc_lpc_write(astlpc, &ver_min_be, |
| 721 | offsetof(struct mctp_lpcmap_hdr, host_ver_min), |
| 722 | sizeof(ver_min_be)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 723 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 724 | mctp_astlpc_lpc_write(astlpc, &ver_cur_be, |
| 725 | offsetof(struct mctp_lpcmap_hdr, host_ver_cur), |
| 726 | sizeof(ver_cur_be)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 727 | |
| 728 | /* Send channel init command */ |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 729 | rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 730 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 731 | astlpc_prwarn(astlpc, "KCS write failed"); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 732 | } |
| 733 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 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 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 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 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 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 | |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 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; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 794 | } |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 795 | if (!mctp_astlpc_kcs_write_ready(astlpc, status)) |
| 796 | return -EBUSY; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 797 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 798 | rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 799 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 800 | astlpc_prwarn(astlpc, "KCS data write failed"); |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 801 | return -EIO; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 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); |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 811 | uint32_t len, len_be; |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 812 | struct mctp_hdr *hdr; |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 813 | int rc; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 814 | |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 815 | hdr = mctp_pktbuf_hdr(pkt); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 816 | len = mctp_pktbuf_size(pkt); |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 817 | |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 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); |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 822 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 823 | if (len > astlpc->proto->body_size(astlpc->layout.tx.size)) { |
Andrew Jeffery | 0721f58 | 2022-09-29 12:12:39 +0930 | [diff] [blame] | 824 | astlpc_prwarn(astlpc, "invalid TX len %" PRIu32 ": %" PRIu32, |
| 825 | len, |
| 826 | astlpc->proto->body_size(astlpc->layout.tx.size)); |
| 827 | return -EMSGSIZE; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 828 | } |
| 829 | |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 830 | mctp_binding_set_tx_enabled(b, false); |
| 831 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 832 | len_be = htobe32(len); |
| 833 | mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset, |
| 834 | sizeof(len_be)); |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 835 | |
| 836 | astlpc->proto->pktbuf_protect(pkt); |
| 837 | len = mctp_pktbuf_size(pkt); |
| 838 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 839 | mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 840 | |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 841 | astlpc->layout.tx.state = buffer_state_prepared; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 842 | |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 843 | rc = mctp_astlpc_kcs_send(astlpc, 0x1); |
| 844 | if (!rc) |
| 845 | astlpc->layout.tx.state = buffer_state_released; |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 846 | |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 847 | return rc == -EBUSY ? 0 : rc; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 848 | } |
| 849 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 850 | static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc, |
| 851 | struct mctp_astlpc_layout *layout) |
| 852 | { |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 853 | uint32_t low, high, limit, rpkt; |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 854 | |
| 855 | /* Derive the largest MTU the BMC _can_ support */ |
| 856 | low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset); |
| 857 | high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset); |
| 858 | limit = high - low; |
| 859 | |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 860 | /* Determine the largest MTU the BMC _wants_ to support */ |
| 861 | if (astlpc->requested_mtu) { |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 862 | uint32_t rmtu = astlpc->requested_mtu; |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 863 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 864 | rpkt = astlpc->proto->packet_size(MCTP_PACKET_SIZE(rmtu)); |
| 865 | limit = MIN(limit, rpkt); |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 866 | } |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 867 | |
| 868 | /* Determine the accepted MTU, applied both directions by convention */ |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 869 | rpkt = MIN(limit, layout->tx.size); |
| 870 | return MCTP_BODY_SIZE(astlpc->proto->body_size(rpkt)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 871 | } |
| 872 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 873 | static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc) |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 874 | { |
| 875 | struct mctp_astlpc_layout proposed, pending; |
| 876 | uint32_t sz, mtu; |
| 877 | int rc; |
| 878 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 879 | /* Do we have a valid protocol version? */ |
| 880 | if (!astlpc->proto->version) |
| 881 | return -EINVAL; |
| 882 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 883 | /* Extract the host's proposed layout */ |
| 884 | rc = mctp_astlpc_layout_read(astlpc, &proposed); |
| 885 | if (rc < 0) |
| 886 | return rc; |
| 887 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 888 | /* Do we have a reasonable layout? */ |
| 889 | if (!mctp_astlpc_layout_validate(astlpc, &proposed)) |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 890 | return -EINVAL; |
| 891 | |
| 892 | /* Negotiate the MTU */ |
| 893 | mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed); |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 894 | sz = astlpc->proto->packet_size(MCTP_PACKET_SIZE(mtu)); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 895 | |
| 896 | /* |
| 897 | * Use symmetric MTUs by convention and to pass constraints in rx/tx |
| 898 | * functions |
| 899 | */ |
| 900 | pending = astlpc->layout; |
| 901 | pending.tx.size = sz; |
| 902 | pending.rx.size = sz; |
| 903 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 904 | if (mctp_astlpc_layout_validate(astlpc, &pending)) { |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 905 | /* We found a sensible Rx MTU, so honour it */ |
| 906 | astlpc->layout = pending; |
| 907 | |
| 908 | /* Enforce the negotiated MTU */ |
| 909 | rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout); |
| 910 | if (rc < 0) |
| 911 | return rc; |
| 912 | |
| 913 | astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes", |
| 914 | mtu); |
| 915 | } else { |
| 916 | astlpc_prwarn(astlpc, "MTU negotiation failed"); |
| 917 | return -EINVAL; |
| 918 | } |
| 919 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 920 | if (astlpc->proto->version >= 2) |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 921 | astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu); |
| 922 | |
| 923 | return 0; |
| 924 | } |
| 925 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 926 | static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc) |
| 927 | { |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 928 | uint16_t negotiated, negotiated_be; |
| 929 | struct mctp_lpcmap_hdr hdr; |
| 930 | uint8_t status; |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 931 | int rc; |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 932 | |
| 933 | mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); |
| 934 | |
| 935 | /* Version negotiation */ |
| 936 | negotiated = |
| 937 | mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR, |
| 938 | be16toh(hdr.host_ver_min), |
| 939 | be16toh(hdr.host_ver_cur)); |
| 940 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 941 | /* MTU negotiation requires knowing which protocol we'll use */ |
| 942 | assert(negotiated < ARRAY_SIZE(astlpc_protocol_version)); |
| 943 | astlpc->proto = &astlpc_protocol_version[negotiated]; |
| 944 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 945 | /* Host Rx MTU negotiation: Failure terminates channel init */ |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 946 | rc = mctp_astlpc_negotiate_layout_bmc(astlpc); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 947 | if (rc < 0) |
| 948 | negotiated = ASTLPC_VER_BAD; |
| 949 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 950 | /* Populate the negotiated version */ |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 951 | negotiated_be = htobe16(negotiated); |
| 952 | mctp_astlpc_lpc_write(astlpc, &negotiated_be, |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 953 | offsetof(struct mctp_lpcmap_hdr, negotiated_ver), |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 954 | sizeof(negotiated_be)); |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 955 | |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 956 | /* Track buffer ownership */ |
| 957 | astlpc->layout.tx.state = buffer_state_acquired; |
| 958 | astlpc->layout.rx.state = buffer_state_released; |
| 959 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 960 | /* Finalise the configuration */ |
| 961 | status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF; |
| 962 | if (negotiated > 0) { |
| 963 | astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16, |
| 964 | negotiated); |
| 965 | status |= KCS_STATUS_CHANNEL_ACTIVE; |
| 966 | } else { |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 967 | astlpc_prerr(astlpc, "Failed to initialise channel"); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 968 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 969 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 970 | mctp_astlpc_kcs_set_status(astlpc, status); |
| 971 | |
| 972 | mctp_binding_set_tx_enabled(&astlpc->binding, |
| 973 | status & KCS_STATUS_CHANNEL_ACTIVE); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc) |
| 977 | { |
| 978 | struct mctp_pktbuf *pkt; |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 979 | uint32_t body, packet; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 980 | |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 981 | mctp_astlpc_lpc_read(astlpc, &body, astlpc->layout.rx.offset, |
| 982 | sizeof(body)); |
| 983 | body = be32toh(body); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 984 | |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 985 | if (body > astlpc->proto->body_size(astlpc->layout.rx.size)) { |
| 986 | astlpc_prwarn(astlpc, "invalid RX len 0x%x", body); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 987 | return; |
| 988 | } |
| 989 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 990 | assert(astlpc->binding.pkt_size >= 0); |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 991 | if (body > (uint32_t)astlpc->binding.pkt_size) { |
| 992 | astlpc_prwarn(astlpc, "invalid RX len 0x%x", body); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 993 | return; |
| 994 | } |
| 995 | |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 996 | /* Eliminate the medium-specific header that we just read */ |
| 997 | packet = astlpc->proto->packet_size(body) - 4; |
| 998 | pkt = mctp_pktbuf_alloc(&astlpc->binding, packet); |
Christian Geddes | ae59f4f | 2021-09-01 09:54:25 -0500 | [diff] [blame] | 999 | if (!pkt) { |
| 1000 | astlpc_prwarn(astlpc, "unable to allocate pktbuf len 0x%x", packet); |
| 1001 | return; |
| 1002 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1003 | |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 1004 | /* |
| 1005 | * Read payload and medium-specific trailer from immediately after the |
| 1006 | * medium-specific header. |
| 1007 | */ |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 1008 | mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt), |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 1009 | astlpc->layout.rx.offset + 4, packet); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1010 | |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 1011 | astlpc->layout.rx.state = buffer_state_prepared; |
| 1012 | |
Christian Geddes | ae59f4f | 2021-09-01 09:54:25 -0500 | [diff] [blame] | 1013 | /* Inform the other side of the MCTP interface that we have read |
| 1014 | * the packet off the bus before handling the contents of the packet. |
| 1015 | */ |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 1016 | if (!mctp_astlpc_kcs_send(astlpc, 0x2)) |
| 1017 | astlpc->layout.rx.state = buffer_state_released; |
Christian Geddes | ae59f4f | 2021-09-01 09:54:25 -0500 | [diff] [blame] | 1018 | |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 1019 | /* |
| 1020 | * v3 will validate the CRC32 in the medium-specific trailer and adjust |
| 1021 | * the packet size accordingly. On older protocols validation is a no-op |
| 1022 | * that always returns true. |
| 1023 | */ |
| 1024 | if (astlpc->proto->pktbuf_validate(pkt)) { |
| 1025 | mctp_bus_rx(&astlpc->binding, pkt); |
| 1026 | } else { |
| 1027 | /* TODO: Drop any associated assembly */ |
| 1028 | mctp_pktbuf_free(pkt); |
| 1029 | astlpc_prdebug(astlpc, "Dropped corrupt packet"); |
| 1030 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc) |
| 1034 | { |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 1035 | astlpc->layout.tx.state = buffer_state_acquired; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1036 | mctp_binding_set_tx_enabled(&astlpc->binding, true); |
| 1037 | } |
| 1038 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 1039 | static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc) |
| 1040 | { |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 1041 | struct mctp_astlpc_layout layout; |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 1042 | uint16_t negotiated; |
| 1043 | int rc; |
| 1044 | |
| 1045 | rc = mctp_astlpc_lpc_read(astlpc, &negotiated, |
| 1046 | offsetof(struct mctp_lpcmap_hdr, |
| 1047 | negotiated_ver), |
| 1048 | sizeof(negotiated)); |
| 1049 | if (rc < 0) |
| 1050 | return rc; |
| 1051 | |
| 1052 | negotiated = be16toh(negotiated); |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 1053 | astlpc_prerr(astlpc, "Version negotiation got: %u", negotiated); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 1054 | |
| 1055 | if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN || |
| 1056 | negotiated > ASTLPC_VER_CUR) { |
| 1057 | astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n", |
| 1058 | negotiated); |
| 1059 | return -EINVAL; |
| 1060 | } |
| 1061 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 1062 | assert(negotiated < ARRAY_SIZE(astlpc_protocol_version)); |
| 1063 | astlpc->proto = &astlpc_protocol_version[negotiated]; |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 1064 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 1065 | rc = mctp_astlpc_layout_read(astlpc, &layout); |
| 1066 | if (rc < 0) |
| 1067 | return rc; |
| 1068 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 1069 | if (!mctp_astlpc_layout_validate(astlpc, &layout)) { |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 1070 | mctp_prerr("BMC proposed invalid buffer parameters"); |
| 1071 | return -EINVAL; |
| 1072 | } |
| 1073 | |
| 1074 | astlpc->layout = layout; |
| 1075 | |
| 1076 | if (negotiated >= 2) |
| 1077 | astlpc->binding.pkt_size = |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 1078 | astlpc->proto->body_size(astlpc->layout.tx.size); |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame] | 1079 | |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 1080 | /* Track buffer ownership */ |
| 1081 | astlpc->layout.tx.state = buffer_state_acquired; |
| 1082 | astlpc->layout.rx.state = buffer_state_released; |
| 1083 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 1084 | return 0; |
| 1085 | } |
| 1086 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1087 | static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc, |
| 1088 | uint8_t status) |
| 1089 | { |
| 1090 | uint8_t updated; |
| 1091 | int rc = 0; |
| 1092 | |
| 1093 | assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); |
| 1094 | |
| 1095 | updated = astlpc->kcs_status ^ status; |
| 1096 | |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 1097 | astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__, |
| 1098 | status, updated); |
| 1099 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1100 | if (updated & KCS_STATUS_BMC_READY) { |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 1101 | if (status & KCS_STATUS_BMC_READY) { |
| 1102 | astlpc->kcs_status = status; |
| 1103 | return astlpc->binding.start(&astlpc->binding); |
| 1104 | } else { |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 1105 | /* Shut down the channel */ |
| 1106 | astlpc->layout.rx.state = buffer_state_idle; |
| 1107 | astlpc->layout.tx.state = buffer_state_idle; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1108 | mctp_binding_set_tx_enabled(&astlpc->binding, false); |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 1109 | } |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1110 | } |
| 1111 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 1112 | if (astlpc->proto->version == 0 || |
| 1113 | updated & KCS_STATUS_CHANNEL_ACTIVE) { |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 1114 | bool enable; |
| 1115 | |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 1116 | astlpc->layout.rx.state = buffer_state_idle; |
| 1117 | astlpc->layout.tx.state = buffer_state_idle; |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 1118 | rc = mctp_astlpc_finalise_channel(astlpc); |
| 1119 | enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0; |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 1120 | mctp_binding_set_tx_enabled(&astlpc->binding, enable); |
| 1121 | } |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1122 | |
| 1123 | astlpc->kcs_status = status; |
| 1124 | |
| 1125 | return rc; |
| 1126 | } |
| 1127 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1128 | int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc) |
| 1129 | { |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1130 | uint8_t status, data; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1131 | int rc; |
| 1132 | |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 1133 | if (astlpc->layout.rx.state == buffer_state_prepared) |
| 1134 | if (!mctp_astlpc_kcs_send(astlpc, 0x2)) |
| 1135 | astlpc->layout.rx.state = buffer_state_released; |
| 1136 | |
| 1137 | if (astlpc->layout.tx.state == buffer_state_prepared) |
| 1138 | if (!mctp_astlpc_kcs_send(astlpc, 0x1)) |
| 1139 | astlpc->layout.tx.state = buffer_state_released; |
| 1140 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 1141 | rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1142 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1143 | astlpc_prwarn(astlpc, "KCS read error"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1144 | return -1; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1145 | } |
| 1146 | |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1147 | astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status); |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 1148 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1149 | if (!mctp_astlpc_kcs_read_ready(astlpc, status)) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1150 | return 0; |
| 1151 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 1152 | rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1153 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1154 | astlpc_prwarn(astlpc, "KCS data read error"); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1155 | return -1; |
| 1156 | } |
| 1157 | |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1158 | astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data); |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 1159 | |
Andrew Jeffery | 88412be | 2021-03-09 22:05:22 +1030 | [diff] [blame] | 1160 | if (!astlpc->proto->version && !(data == 0x0 || data == 0xff)) { |
Andrew Jeffery | afcb701 | 2021-01-28 17:00:36 +1030 | [diff] [blame] | 1161 | astlpc_prwarn(astlpc, "Invalid message for binding state: 0x%x", |
| 1162 | data); |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1166 | switch (data) { |
| 1167 | case 0x0: |
| 1168 | mctp_astlpc_init_channel(astlpc); |
| 1169 | break; |
| 1170 | case 0x1: |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 1171 | if (astlpc->layout.rx.state != buffer_state_released) { |
| 1172 | astlpc_prerr(astlpc, |
| 1173 | "Protocol error: Invalid Rx buffer state for event %d: %d\n", |
| 1174 | data, astlpc->layout.rx.state); |
| 1175 | return 0; |
| 1176 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1177 | mctp_astlpc_rx_start(astlpc); |
| 1178 | break; |
| 1179 | case 0x2: |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 1180 | if (astlpc->layout.tx.state != buffer_state_released) { |
| 1181 | astlpc_prerr(astlpc, |
| 1182 | "Protocol error: Invalid Tx buffer state for event %d: %d\n", |
| 1183 | data, astlpc->layout.tx.state); |
| 1184 | return 0; |
| 1185 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1186 | mctp_astlpc_tx_complete(astlpc); |
| 1187 | break; |
Jeremy Kerr | 1a4b55a | 2019-06-24 14:22:39 +0800 | [diff] [blame] | 1188 | case 0xff: |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1189 | /* No responsibilities for the BMC on 0xff */ |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 1190 | if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) { |
| 1191 | rc = mctp_astlpc_update_channel(astlpc, status); |
| 1192 | if (rc < 0) |
| 1193 | return rc; |
| 1194 | } |
| 1195 | break; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1196 | default: |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1197 | astlpc_prwarn(astlpc, "unknown message 0x%x", data); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1198 | } |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 1199 | |
| 1200 | /* Handle silent loss of bmc-ready */ |
| 1201 | if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) { |
| 1202 | if (!(status & KCS_STATUS_BMC_READY && data == 0xff)) |
| 1203 | return mctp_astlpc_update_channel(astlpc, status); |
| 1204 | } |
| 1205 | |
| 1206 | return rc; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1207 | } |
| 1208 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1209 | /* allocate and basic initialisation */ |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1210 | static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode, |
| 1211 | uint32_t mtu) |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1212 | { |
| 1213 | struct mctp_binding_astlpc *astlpc; |
| 1214 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1215 | assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) || |
| 1216 | (mode == MCTP_BINDING_ASTLPC_MODE_HOST)); |
| 1217 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1218 | astlpc = __mctp_alloc(sizeof(*astlpc)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1219 | if (!astlpc) |
| 1220 | return NULL; |
| 1221 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1222 | memset(astlpc, 0, sizeof(*astlpc)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1223 | astlpc->mode = mode; |
| 1224 | astlpc->lpc_map = NULL; |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 1225 | astlpc->layout.rx.state = buffer_state_idle; |
| 1226 | astlpc->layout.tx.state = buffer_state_idle; |
Andrew Jeffery | a936898 | 2020-06-09 13:07:39 +0930 | [diff] [blame] | 1227 | astlpc->requested_mtu = mtu; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1228 | astlpc->binding.name = "astlpc"; |
| 1229 | astlpc->binding.version = 1; |
Andrew Jeffery | 1a4f441 | 2021-01-28 13:42:02 +1030 | [diff] [blame] | 1230 | astlpc->binding.pkt_size = |
| 1231 | MCTP_PACKET_SIZE(mtu > MCTP_BTU ? mtu : MCTP_BTU); |
Andrew Jeffery | eba19a3 | 2021-03-09 23:09:40 +1030 | [diff] [blame] | 1232 | astlpc->binding.pkt_header = 4; |
| 1233 | astlpc->binding.pkt_trailer = 4; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1234 | astlpc->binding.tx = mctp_binding_astlpc_tx; |
| 1235 | if (mode == MCTP_BINDING_ASTLPC_MODE_BMC) |
| 1236 | astlpc->binding.start = mctp_binding_astlpc_start_bmc; |
| 1237 | else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST) |
| 1238 | astlpc->binding.start = mctp_binding_astlpc_start_host; |
| 1239 | else { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1240 | astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1241 | __mctp_free(astlpc); |
| 1242 | return NULL; |
| 1243 | } |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1244 | |
| 1245 | return astlpc; |
| 1246 | } |
| 1247 | |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 1248 | struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b) |
| 1249 | { |
| 1250 | return &b->binding; |
| 1251 | } |
| 1252 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1253 | struct mctp_binding_astlpc * |
| 1254 | mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map, |
| 1255 | const struct mctp_binding_astlpc_ops *ops, void *ops_data) |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1256 | { |
| 1257 | struct mctp_binding_astlpc *astlpc; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1258 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1259 | if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC || |
| 1260 | mode == MCTP_BINDING_ASTLPC_MODE_HOST)) { |
| 1261 | mctp_prerr("Unknown binding mode: %u", mode); |
| 1262 | return NULL; |
| 1263 | } |
| 1264 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1265 | astlpc = __mctp_astlpc_init(mode, mtu); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1266 | if (!astlpc) |
| 1267 | return NULL; |
| 1268 | |
| 1269 | memcpy(&astlpc->ops, ops, sizeof(astlpc->ops)); |
| 1270 | astlpc->ops_data = ops_data; |
| 1271 | astlpc->lpc_map = lpc_map; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1272 | astlpc->mode = mode; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1273 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1274 | return astlpc; |
| 1275 | } |
| 1276 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1277 | struct mctp_binding_astlpc * |
| 1278 | mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data, |
| 1279 | void *lpc_map) |
| 1280 | { |
| 1281 | return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map, |
| 1282 | ops, ops_data); |
| 1283 | } |
| 1284 | |
Andrew Jeffery | 4663f67 | 2020-03-10 23:36:24 +1030 | [diff] [blame] | 1285 | void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc) |
| 1286 | { |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 1287 | /* Clear channel-active and bmc-ready */ |
| 1288 | if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) |
Andrew Jeffery | 7c4509a | 2021-08-26 14:56:16 +0930 | [diff] [blame] | 1289 | mctp_astlpc_kcs_set_status(astlpc, 0); |
Andrew Jeffery | 4663f67 | 2020-03-10 23:36:24 +1030 | [diff] [blame] | 1290 | __mctp_free(astlpc); |
| 1291 | } |
| 1292 | |
Jeremy Kerr | b214c64 | 2019-11-27 11:34:00 +0800 | [diff] [blame] | 1293 | #ifdef MCTP_HAVE_FILEIO |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 1294 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1295 | static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1296 | { |
| 1297 | struct aspeed_lpc_ctrl_mapping map = { |
| 1298 | .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY, |
| 1299 | .window_id = 0, /* There's only one */ |
| 1300 | .flags = 0, |
| 1301 | .addr = 0, |
| 1302 | .offset = 0, |
| 1303 | .size = 0 |
| 1304 | }; |
Andrew Jeffery | 979c6a1 | 2020-05-23 20:04:49 +0930 | [diff] [blame] | 1305 | void *lpc_map_base; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1306 | int fd, rc; |
| 1307 | |
| 1308 | fd = open(lpc_path, O_RDWR | O_SYNC); |
| 1309 | if (fd < 0) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1310 | astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1311 | return -1; |
| 1312 | } |
| 1313 | |
| 1314 | rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map); |
| 1315 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1316 | astlpc_prwarn(astlpc, "LPC GET_SIZE failed"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1317 | close(fd); |
| 1318 | return -1; |
| 1319 | } |
| 1320 | |
Andrew Jeffery | c1d5c54 | 2021-10-27 15:25:48 +1030 | [diff] [blame] | 1321 | /* |
| 1322 | * 🚨🚨🚨 |
| 1323 | * |
| 1324 | * Decouple ourselves from hiomapd[1] (another user of the FW2AHB) by |
| 1325 | * mapping the FW2AHB to the reserved memory here as well. |
| 1326 | * |
| 1327 | * It's not possible to use the MCTP ASTLPC binding on machines that |
| 1328 | * need the FW2AHB bridge mapped anywhere except to the reserved memory |
| 1329 | * (e.g. the host SPI NOR). |
| 1330 | * |
| 1331 | * [1] https://github.com/openbmc/hiomapd/ |
| 1332 | * |
| 1333 | * 🚨🚨🚨 |
| 1334 | * |
| 1335 | * The following calculation must align with what's going on in |
| 1336 | * hiomapd's lpc.c so as not to disrupt its behaviour: |
| 1337 | * |
| 1338 | * https://github.com/openbmc/hiomapd/blob/5ff50e3cbd7702aefc185264e4adfb9952040575/lpc.c#L68 |
| 1339 | * |
| 1340 | * 🚨🚨🚨 |
| 1341 | */ |
| 1342 | |
| 1343 | /* Map the reserved memory at the top of the 28-bit LPC firmware address space */ |
| 1344 | map.addr = 0x0FFFFFFF & -map.size; |
| 1345 | astlpc_prinfo(astlpc, |
| 1346 | "Configuring FW2AHB to map reserved memory at 0x%08x for 0x%x in the LPC FW cycle address-space", |
| 1347 | map.addr, map.size); |
| 1348 | |
| 1349 | rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map); |
| 1350 | if (rc) { |
| 1351 | astlpc_prwarn(astlpc, "Failed to map FW2AHB to reserved memory"); |
| 1352 | close(fd); |
| 1353 | return -1; |
| 1354 | } |
| 1355 | |
| 1356 | /* Map the reserved memory into our address space */ |
Andrew Jeffery | 979c6a1 | 2020-05-23 20:04:49 +0930 | [diff] [blame] | 1357 | lpc_map_base = |
| 1358 | mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 1359 | if (lpc_map_base == MAP_FAILED) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1360 | astlpc_prwarn(astlpc, "LPC mmap failed"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1361 | rc = -1; |
| 1362 | } else { |
Andrew Jeffery | 979c6a1 | 2020-05-23 20:04:49 +0930 | [diff] [blame] | 1363 | astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | close(fd); |
| 1367 | |
| 1368 | return rc; |
| 1369 | } |
| 1370 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1371 | static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1372 | { |
| 1373 | astlpc->kcs_fd = open(kcs_path, O_RDWR); |
| 1374 | if (astlpc->kcs_fd < 0) |
| 1375 | return -1; |
| 1376 | |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1380 | static int __mctp_astlpc_fileio_kcs_read(void *arg, |
| 1381 | enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val) |
| 1382 | { |
| 1383 | struct mctp_binding_astlpc *astlpc = arg; |
| 1384 | off_t offset = reg; |
| 1385 | int rc; |
| 1386 | |
| 1387 | rc = pread(astlpc->kcs_fd, val, 1, offset); |
| 1388 | |
| 1389 | return rc == 1 ? 0 : -1; |
| 1390 | } |
| 1391 | |
| 1392 | static int __mctp_astlpc_fileio_kcs_write(void *arg, |
| 1393 | enum mctp_binding_astlpc_kcs_reg reg, uint8_t val) |
| 1394 | { |
| 1395 | struct mctp_binding_astlpc *astlpc = arg; |
| 1396 | off_t offset = reg; |
| 1397 | int rc; |
| 1398 | |
| 1399 | rc = pwrite(astlpc->kcs_fd, &val, 1, offset); |
| 1400 | |
| 1401 | return rc == 1 ? 0 : -1; |
| 1402 | } |
| 1403 | |
Andrew Jeffery | 1111c6a | 2022-07-25 20:44:39 +0930 | [diff] [blame] | 1404 | int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc, |
| 1405 | struct pollfd *pollfd) |
| 1406 | { |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 1407 | bool release; |
| 1408 | |
Andrew Jeffery | 1111c6a | 2022-07-25 20:44:39 +0930 | [diff] [blame] | 1409 | pollfd->fd = astlpc->kcs_fd; |
Andrew Jeffery | fe763e9 | 2022-08-05 23:16:17 +0930 | [diff] [blame] | 1410 | pollfd->events = 0; |
| 1411 | |
| 1412 | release = astlpc->layout.rx.state == buffer_state_prepared || |
| 1413 | astlpc->layout.tx.state == buffer_state_prepared; |
| 1414 | |
| 1415 | pollfd->events = release ? POLLOUT : POLLIN; |
Andrew Jeffery | 1111c6a | 2022-07-25 20:44:39 +0930 | [diff] [blame] | 1416 | |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1420 | struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1421 | { |
| 1422 | struct mctp_binding_astlpc *astlpc; |
| 1423 | int rc; |
| 1424 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1425 | /* |
| 1426 | * 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] | 1427 | * freestanding, so lets assume that we're on the BMC side. |
| 1428 | * |
| 1429 | * Requesting an MTU of 0 requests the largest possible MTU, whatever |
| 1430 | * value that might take. |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1431 | */ |
Andrew Jeffery | 8877c46 | 2020-06-15 12:22:53 +0930 | [diff] [blame] | 1432 | astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, 0); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1433 | if (!astlpc) |
| 1434 | return NULL; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1435 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1436 | /* Set internal operations for kcs. We use direct accesses to the lpc |
| 1437 | * map area */ |
| 1438 | astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read; |
| 1439 | astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write; |
| 1440 | astlpc->ops_data = astlpc; |
| 1441 | |
| 1442 | rc = mctp_astlpc_init_fileio_lpc(astlpc); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1443 | if (rc) { |
| 1444 | free(astlpc); |
| 1445 | return NULL; |
| 1446 | } |
| 1447 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1448 | rc = mctp_astlpc_init_fileio_kcs(astlpc); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1449 | if (rc) { |
| 1450 | free(astlpc); |
| 1451 | return NULL; |
| 1452 | } |
| 1453 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1454 | return astlpc; |
| 1455 | } |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 1456 | #else |
Andrew Jeffery | 5b2702d | 2022-07-25 16:21:43 +0930 | [diff] [blame] | 1457 | struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void) |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 1458 | { |
Andrew Jeffery | 5b2702d | 2022-07-25 16:21:43 +0930 | [diff] [blame] | 1459 | mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__); |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 1460 | return NULL; |
| 1461 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1462 | |
Andrew Jeffery | 1111c6a | 2022-07-25 20:44:39 +0930 | [diff] [blame] | 1463 | int mctp_astlpc_init_pollfd(struct mctp_binding_astlpc *astlpc __unused, |
| 1464 | struct pollfd *pollfd __unused) |
| 1465 | { |
| 1466 | mctp_prlog(MCTP_LOG_ERR, "%s: Missing support for file IO", __func__); |
| 1467 | return -1; |
| 1468 | } |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 1469 | #endif |