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