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