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 | |
| 21 | #include "libmctp.h" |
| 22 | #include "libmctp-alloc.h" |
| 23 | #include "libmctp-log.h" |
| 24 | #include "libmctp-astlpc.h" |
Przemyslaw Czarnowski | ff25d7e | 2020-03-26 11:39:37 +0100 | [diff] [blame] | 25 | #include "container_of.h" |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 26 | |
Jeremy Kerr | b214c64 | 2019-11-27 11:34:00 +0800 | [diff] [blame] | 27 | #ifdef MCTP_HAVE_FILEIO |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 28 | |
Jeremy Kerr | c6f676d | 2019-12-19 09:24:06 +0800 | [diff] [blame] | 29 | #include <unistd.h> |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 30 | #include <fcntl.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <sys/mman.h> |
| 33 | #include <linux/aspeed-lpc-ctrl.h> |
| 34 | |
| 35 | /* kernel interface */ |
| 36 | static const char *kcs_path = "/dev/mctp0"; |
| 37 | static const char *lpc_path = "/dev/aspeed-lpc-ctrl"; |
| 38 | |
| 39 | #endif |
| 40 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 41 | struct mctp_astlpc_buffer { |
| 42 | uint32_t offset; |
| 43 | uint32_t size; |
| 44 | }; |
| 45 | |
| 46 | struct mctp_astlpc_layout { |
| 47 | struct mctp_astlpc_buffer rx; |
| 48 | struct mctp_astlpc_buffer tx; |
| 49 | }; |
| 50 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 51 | struct mctp_binding_astlpc { |
| 52 | struct mctp_binding binding; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 53 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 54 | void *lpc_map; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 55 | struct mctp_astlpc_layout layout; |
| 56 | |
| 57 | uint8_t mode; |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 58 | uint16_t version; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 59 | |
| 60 | /* direct ops data */ |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 61 | struct mctp_binding_astlpc_ops ops; |
| 62 | void *ops_data; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 63 | |
| 64 | /* fileio ops data */ |
Andrew Jeffery | 979c6a1 | 2020-05-23 20:04:49 +0930 | [diff] [blame] | 65 | int kcs_fd; |
| 66 | uint8_t kcs_status; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 67 | |
| 68 | bool running; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 69 | }; |
| 70 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 71 | #define binding_to_astlpc(b) \ |
| 72 | container_of(b, struct mctp_binding_astlpc, binding) |
| 73 | |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 74 | #define astlpc_prlog(ctx, lvl, fmt, ...) \ |
| 75 | do { \ |
| 76 | bool __bmc = ((ctx)->mode == MCTP_BINDING_ASTLPC_MODE_BMC); \ |
| 77 | mctp_prlog(lvl, pr_fmt("%s: " fmt), __bmc ? "bmc" : "host", \ |
| 78 | ##__VA_ARGS__); \ |
| 79 | } while (0) |
| 80 | |
| 81 | #define astlpc_prerr(ctx, fmt, ...) \ |
| 82 | astlpc_prlog(ctx, MCTP_LOG_ERR, fmt, ##__VA_ARGS__) |
| 83 | #define astlpc_prwarn(ctx, fmt, ...) \ |
| 84 | astlpc_prlog(ctx, MCTP_LOG_WARNING, fmt, ##__VA_ARGS__) |
| 85 | #define astlpc_prinfo(ctx, fmt, ...) \ |
| 86 | astlpc_prlog(ctx, MCTP_LOG_INFO, fmt, ##__VA_ARGS__) |
| 87 | #define astlpc_prdebug(ctx, fmt, ...) \ |
| 88 | astlpc_prlog(ctx, MCTP_LOG_DEBUG, fmt, ##__VA_ARGS__) |
| 89 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 90 | /* clang-format off */ |
| 91 | #define ASTLPC_MCTP_MAGIC 0x4d435450 |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 92 | #define ASTLPC_VER_BAD 0 |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 93 | #define ASTLPC_VER_MIN 1 |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 94 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 95 | /* Support testing of new binding protocols */ |
| 96 | #ifndef ASTLPC_VER_CUR |
| 97 | #define ASTLPC_VER_CUR 1 |
| 98 | #endif |
| 99 | |
| 100 | #define ASTLPC_PACKET_SIZE(sz) (4 + (sz)) |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 101 | #define ASTLPC_BODY_SIZE(sz) ((sz) - 4) |
| 102 | /* clang-format on */ |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 103 | |
| 104 | struct mctp_lpcmap_hdr { |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 105 | uint32_t magic; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 106 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 107 | uint16_t bmc_ver_min; |
| 108 | uint16_t bmc_ver_cur; |
| 109 | uint16_t host_ver_min; |
| 110 | uint16_t host_ver_cur; |
| 111 | uint16_t negotiated_ver; |
| 112 | uint16_t pad0; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 113 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 114 | struct { |
| 115 | uint32_t rx_offset; |
| 116 | uint32_t rx_size; |
| 117 | uint32_t tx_offset; |
| 118 | uint32_t tx_size; |
| 119 | } layout; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 120 | } __attribute__((packed)); |
| 121 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 122 | static const uint32_t control_size = 0x100; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 123 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 124 | #define LPC_WIN_SIZE (1 * 1024 * 1024) |
| 125 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 126 | #define KCS_STATUS_BMC_READY 0x80 |
| 127 | #define KCS_STATUS_CHANNEL_ACTIVE 0x40 |
| 128 | #define KCS_STATUS_IBF 0x02 |
| 129 | #define KCS_STATUS_OBF 0x01 |
| 130 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 131 | #define MIN(a, b) \ |
| 132 | ({ \ |
| 133 | typeof(a) _a = a; \ |
| 134 | typeof(b) _b = b; \ |
| 135 | _a < _b ? _a : _b; \ |
| 136 | }) |
| 137 | |
| 138 | #define MAX(a, b) \ |
| 139 | ({ \ |
| 140 | typeof(a) _a = a; \ |
| 141 | typeof(b) _b = b; \ |
| 142 | _a > _b ? _a : _b; \ |
| 143 | }) |
| 144 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 145 | static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc, |
| 146 | enum mctp_binding_astlpc_kcs_reg reg, |
| 147 | uint8_t val) |
| 148 | { |
| 149 | return astlpc->ops.kcs_write(astlpc->ops_data, reg, val); |
| 150 | } |
| 151 | |
| 152 | static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc, |
| 153 | enum mctp_binding_astlpc_kcs_reg reg, |
| 154 | uint8_t *val) |
| 155 | { |
| 156 | return astlpc->ops.kcs_read(astlpc->ops_data, reg, val); |
| 157 | } |
| 158 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 159 | static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc, |
| 160 | const void *buf, long offset, |
| 161 | size_t len) |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 162 | { |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 163 | astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset); |
| 164 | |
| 165 | assert(offset >= 0); |
| 166 | |
| 167 | /* Indirect access */ |
| 168 | if (astlpc->ops.lpc_write) { |
| 169 | void *data = astlpc->ops_data; |
| 170 | |
| 171 | return astlpc->ops.lpc_write(data, buf, offset, len); |
| 172 | } |
| 173 | |
| 174 | /* Direct mapping */ |
| 175 | assert(astlpc->lpc_map); |
| 176 | memcpy(&((char *)astlpc->lpc_map)[offset], buf, len); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc, |
| 182 | void *buf, long offset, size_t len) |
| 183 | { |
| 184 | astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len, |
| 185 | offset); |
| 186 | |
| 187 | assert(offset >= 0); |
| 188 | |
| 189 | /* Indirect access */ |
| 190 | if (astlpc->ops.lpc_read) { |
| 191 | void *data = astlpc->ops_data; |
| 192 | |
| 193 | return astlpc->ops.lpc_read(data, buf, offset, len); |
| 194 | } |
| 195 | |
| 196 | /* Direct mapping */ |
| 197 | assert(astlpc->lpc_map); |
| 198 | memcpy(buf, &((char *)astlpc->lpc_map)[offset], len); |
| 199 | |
| 200 | return 0; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 201 | } |
| 202 | |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 203 | static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc, |
| 204 | uint8_t status) |
| 205 | { |
| 206 | uint8_t data; |
| 207 | int rc; |
| 208 | |
| 209 | /* Since we're setting the status register, we want the other endpoint |
| 210 | * to be interrupted. However, some hardware may only raise a host-side |
| 211 | * interrupt on an ODR event. |
| 212 | * So, write a dummy value of 0xff to ODR, which will ensure that an |
| 213 | * interrupt is triggered, and can be ignored by the host. |
| 214 | */ |
| 215 | data = 0xff; |
| 216 | status |= KCS_STATUS_OBF; |
| 217 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 218 | rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status); |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 219 | if (rc) { |
| 220 | astlpc_prwarn(astlpc, "KCS status write failed"); |
| 221 | return -1; |
| 222 | } |
| 223 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 224 | rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data); |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 225 | if (rc) { |
| 226 | astlpc_prwarn(astlpc, "KCS dummy data write failed"); |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 233 | static int mctp_astlpc_layout_read(struct mctp_binding_astlpc *astlpc, |
| 234 | struct mctp_astlpc_layout *layout) |
| 235 | { |
| 236 | struct mctp_lpcmap_hdr hdr; |
| 237 | int rc; |
| 238 | |
| 239 | rc = mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); |
| 240 | if (rc < 0) |
| 241 | return rc; |
| 242 | |
| 243 | /* Flip the buffers as the names are defined in terms of the host */ |
| 244 | if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) { |
| 245 | layout->rx.offset = be32toh(hdr.layout.tx_offset); |
| 246 | layout->rx.size = be32toh(hdr.layout.tx_size); |
| 247 | layout->tx.offset = be32toh(hdr.layout.rx_offset); |
| 248 | layout->tx.size = be32toh(hdr.layout.rx_size); |
| 249 | } else { |
| 250 | assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); |
| 251 | |
| 252 | layout->rx.offset = be32toh(hdr.layout.rx_offset); |
| 253 | layout->rx.size = be32toh(hdr.layout.rx_size); |
| 254 | layout->tx.offset = be32toh(hdr.layout.tx_offset); |
| 255 | layout->tx.size = be32toh(hdr.layout.tx_size); |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static int mctp_astlpc_layout_write(struct mctp_binding_astlpc *astlpc, |
| 262 | struct mctp_astlpc_layout *layout) |
| 263 | { |
| 264 | uint32_t rx_size_be; |
| 265 | |
| 266 | if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) { |
| 267 | struct mctp_lpcmap_hdr hdr; |
| 268 | |
| 269 | /* |
| 270 | * Flip the buffers as the names are defined in terms of the |
| 271 | * host |
| 272 | */ |
| 273 | hdr.layout.rx_offset = htobe32(layout->tx.offset); |
| 274 | hdr.layout.rx_size = htobe32(layout->tx.size); |
| 275 | hdr.layout.tx_offset = htobe32(layout->rx.offset); |
| 276 | hdr.layout.tx_size = htobe32(layout->rx.size); |
| 277 | |
| 278 | return mctp_astlpc_lpc_write(astlpc, &hdr.layout, |
| 279 | offsetof(struct mctp_lpcmap_hdr, layout), |
| 280 | sizeof(hdr.layout)); |
| 281 | } |
| 282 | |
| 283 | assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); |
| 284 | |
| 285 | /* |
| 286 | * As of v2 we only need to write rx_size - the offsets are controlled |
| 287 | * by the BMC, as is the BMC's rx_size (host tx_size). |
| 288 | */ |
| 289 | rx_size_be = htobe32(layout->rx.size); |
| 290 | return mctp_astlpc_lpc_write(astlpc, &rx_size_be, |
| 291 | offsetof(struct mctp_lpcmap_hdr, layout.rx_size), |
| 292 | sizeof(rx_size_be)); |
| 293 | } |
| 294 | |
| 295 | static bool mctp_astlpc_buffer_validate(struct mctp_astlpc_buffer *buf, |
| 296 | const char *name) |
| 297 | { |
| 298 | /* Check for overflow */ |
| 299 | if (buf->offset + buf->size < buf->offset) { |
| 300 | mctp_prerr( |
| 301 | "%s packet buffer parameters overflow: offset: 0x%" PRIx32 |
| 302 | ", size: %" PRIu32, |
| 303 | name, buf->offset, buf->size); |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | /* Check that the buffers are contained within the allocated space */ |
| 308 | if (buf->offset + buf->size > LPC_WIN_SIZE) { |
| 309 | mctp_prerr( |
| 310 | "%s packet buffer parameters exceed %uM window size: offset: 0x%" PRIx32 |
| 311 | ", size: %" PRIu32, |
| 312 | name, (LPC_WIN_SIZE / (1024 * 1024)), buf->offset, |
| 313 | buf->size); |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | /* Check that the baseline transmission unit is supported */ |
| 318 | if (buf->size < ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU))) { |
| 319 | mctp_prerr( |
| 320 | "%s packet buffer too small: Require %zu bytes to support the %u byte baseline transmission unit, found %" PRIu32, |
| 321 | name, ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU)), |
| 322 | MCTP_BTU, buf->size); |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | /* Check for overlap with the control space */ |
| 327 | if (buf->offset < control_size) { |
| 328 | mctp_prerr( |
| 329 | "%s packet buffer overlaps control region {0x%" PRIx32 |
| 330 | ", %" PRIu32 "}: Rx {0x%" PRIx32 ", %" PRIu32 "}", |
| 331 | name, 0U, control_size, buf->offset, buf->size); |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | static bool mctp_astlpc_layout_validate(struct mctp_astlpc_layout *layout) |
| 339 | { |
| 340 | struct mctp_astlpc_buffer *rx = &layout->rx; |
| 341 | struct mctp_astlpc_buffer *tx = &layout->tx; |
| 342 | bool rx_valid, tx_valid; |
| 343 | |
| 344 | rx_valid = mctp_astlpc_buffer_validate(rx, "Rx"); |
| 345 | tx_valid = mctp_astlpc_buffer_validate(tx, "Tx"); |
| 346 | |
| 347 | if (!(rx_valid && tx_valid)) |
| 348 | return false; |
| 349 | |
| 350 | /* Check that the buffers are disjoint */ |
| 351 | if ((rx->offset <= tx->offset && rx->offset + rx->size > tx->offset) || |
| 352 | (tx->offset <= rx->offset && tx->offset + tx->size > rx->offset)) { |
| 353 | mctp_prerr("Rx and Tx packet buffers overlap: Rx {0x%" PRIx32 |
| 354 | ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}", |
| 355 | rx->offset, rx->size, tx->offset, tx->size); |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 362 | static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc) |
| 363 | { |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 364 | struct mctp_lpcmap_hdr hdr = { 0 }; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 365 | uint8_t status; |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 366 | size_t sz; |
| 367 | |
| 368 | /* |
| 369 | * The largest buffer size is half of the allocated MCTP space |
| 370 | * excluding the control space. |
| 371 | */ |
| 372 | sz = ((LPC_WIN_SIZE - control_size) / 2); |
| 373 | |
| 374 | /* |
| 375 | * Trim the MTU to a multiple of 16 to meet the requirements of 12.17 |
| 376 | * Query Hop in DSP0236 v1.3.0. |
| 377 | */ |
| 378 | sz = MCTP_BODY_SIZE(ASTLPC_BODY_SIZE(sz)); |
| 379 | sz &= ~0xfUL; |
| 380 | sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(sz)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 381 | |
| 382 | /* 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^] | 383 | astlpc->layout.tx.offset = control_size; |
| 384 | astlpc->layout.tx.size = sz; |
| 385 | astlpc->layout.rx.offset = |
| 386 | astlpc->layout.tx.offset + astlpc->layout.tx.size; |
| 387 | astlpc->layout.rx.size = sz; |
| 388 | |
| 389 | /* Sanity check that can be eliminated if asserts are off */ |
| 390 | assert(mctp_astlpc_layout_validate(&astlpc->layout)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 391 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 392 | hdr = (struct mctp_lpcmap_hdr){ |
| 393 | .magic = htobe32(ASTLPC_MCTP_MAGIC), |
| 394 | .bmc_ver_min = htobe16(ASTLPC_VER_MIN), |
| 395 | .bmc_ver_cur = htobe16(ASTLPC_VER_CUR), |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 396 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 397 | /* Flip the buffers back as we're now describing the host's |
| 398 | * configuration to the host */ |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 399 | .layout.rx_offset = htobe32(astlpc->layout.tx.offset), |
| 400 | .layout.rx_size = htobe32(astlpc->layout.tx.size), |
| 401 | .layout.tx_offset = htobe32(astlpc->layout.rx.offset), |
| 402 | .layout.tx_size = htobe32(astlpc->layout.rx.size), |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 403 | }; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 404 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 405 | mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 406 | |
| 407 | /* set status indicating that the BMC is now active */ |
| 408 | status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF; |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 409 | return mctp_astlpc_kcs_set_status(astlpc, status); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b) |
| 413 | { |
| 414 | struct mctp_binding_astlpc *astlpc = |
| 415 | container_of(b, struct mctp_binding_astlpc, binding); |
| 416 | |
| 417 | return mctp_astlpc_init_bmc(astlpc); |
| 418 | } |
| 419 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 420 | static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min, |
| 421 | uint16_t bmc_ver_cur, |
| 422 | uint16_t host_ver_min, |
| 423 | uint16_t host_ver_cur) |
| 424 | { |
| 425 | if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) { |
| 426 | mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16 |
| 427 | "], [%" PRIu16 ", %" PRIu16 "]", |
| 428 | bmc_ver_min, bmc_ver_cur, host_ver_min, |
| 429 | host_ver_cur); |
| 430 | return false; |
| 431 | } else if (bmc_ver_min > bmc_ver_cur) { |
| 432 | mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16 |
| 433 | "]", |
| 434 | bmc_ver_min, bmc_ver_cur); |
| 435 | return false; |
| 436 | } else if (host_ver_min > host_ver_cur) { |
| 437 | mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16 |
| 438 | "]", |
| 439 | host_ver_min, host_ver_cur); |
| 440 | return false; |
| 441 | } else if ((host_ver_cur < bmc_ver_min) || |
| 442 | (host_ver_min > bmc_ver_cur)) { |
| 443 | mctp_prerr( |
| 444 | "Unable to satisfy version negotiation with ranges [%" PRIu16 |
| 445 | ", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]", |
| 446 | bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur); |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | return true; |
| 451 | } |
| 452 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 453 | static int mctp_astlpc_negotiate_layout_host(struct mctp_binding_astlpc *astlpc) |
| 454 | { |
| 455 | struct mctp_astlpc_layout layout; |
| 456 | uint32_t sz; |
| 457 | int rc; |
| 458 | |
| 459 | rc = mctp_astlpc_layout_read(astlpc, &layout); |
| 460 | if (rc < 0) |
| 461 | return rc; |
| 462 | |
| 463 | if (!mctp_astlpc_layout_validate(&layout)) { |
| 464 | astlpc_prerr( |
| 465 | astlpc, |
| 466 | "BMC provided invalid buffer layout: Rx {0x%" PRIx32 |
| 467 | ", %" PRIu32 "}, Tx {0x%" PRIx32 ", %" PRIu32 "}", |
| 468 | layout.rx.offset, layout.rx.size, layout.tx.offset, |
| 469 | layout.tx.size); |
| 470 | return -EINVAL; |
| 471 | } |
| 472 | |
| 473 | sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(MCTP_BTU)); |
| 474 | layout.rx.size = sz; |
| 475 | |
| 476 | if (!mctp_astlpc_layout_validate(&layout)) { |
| 477 | astlpc_prerr( |
| 478 | astlpc, |
| 479 | "Generated invalid buffer layout with size %" PRIu32 |
| 480 | ": Rx {0x%" PRIx32 ", %" PRIu32 "}, Tx {0x%" PRIx32 |
| 481 | ", %" PRIu32 "}", |
| 482 | sz, layout.rx.offset, layout.rx.size, layout.tx.offset, |
| 483 | layout.tx.size); |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | |
| 487 | astlpc_prinfo(astlpc, "Requesting MTU of %" PRIu32 " bytes", MCTP_BTU); |
| 488 | |
| 489 | return mctp_astlpc_layout_write(astlpc, &layout); |
| 490 | } |
| 491 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 492 | static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc) |
| 493 | { |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 494 | const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN); |
| 495 | const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 496 | uint16_t bmc_ver_min, bmc_ver_cur; |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 497 | struct mctp_lpcmap_hdr hdr; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 498 | uint8_t status; |
| 499 | int rc; |
| 500 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 501 | rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 502 | if (rc) { |
| 503 | mctp_prwarn("KCS status read failed"); |
| 504 | return rc; |
| 505 | } |
| 506 | |
| 507 | astlpc->kcs_status = status; |
| 508 | |
| 509 | if (!(status & KCS_STATUS_BMC_READY)) |
| 510 | return -EHOSTDOWN; |
| 511 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 512 | mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 513 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 514 | bmc_ver_min = be16toh(hdr.bmc_ver_min); |
| 515 | bmc_ver_cur = be16toh(hdr.bmc_ver_cur); |
| 516 | |
| 517 | if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur, |
| 518 | ASTLPC_VER_MIN, ASTLPC_VER_CUR)) { |
| 519 | astlpc_prerr(astlpc, "Cannot negotiate with invalid versions"); |
| 520 | return -EINVAL; |
| 521 | } |
| 522 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 523 | /* |
| 524 | * Negotation always chooses the highest protocol version that |
| 525 | * satisfies the version constraints. So check whether the BMC supports |
| 526 | * v2, and if so, negotiate in v2 style. |
| 527 | */ |
| 528 | if (ASTLPC_VER_CUR >= 2 && bmc_ver_cur >= 2) { |
| 529 | rc = mctp_astlpc_negotiate_layout_host(astlpc); |
| 530 | if (rc < 0) |
| 531 | return rc; |
| 532 | } |
| 533 | |
| 534 | /* Version negotiation */ |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 535 | mctp_astlpc_lpc_write(astlpc, &ver_min_be, |
| 536 | offsetof(struct mctp_lpcmap_hdr, host_ver_min), |
| 537 | sizeof(ver_min_be)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 538 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 539 | mctp_astlpc_lpc_write(astlpc, &ver_cur_be, |
| 540 | offsetof(struct mctp_lpcmap_hdr, host_ver_cur), |
| 541 | sizeof(ver_cur_be)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 542 | |
| 543 | /* Send channel init command */ |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 544 | rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 545 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 546 | astlpc_prwarn(astlpc, "KCS write failed"); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | return rc; |
| 550 | } |
| 551 | |
| 552 | static int mctp_binding_astlpc_start_host(struct mctp_binding *b) |
| 553 | { |
| 554 | struct mctp_binding_astlpc *astlpc = |
| 555 | container_of(b, struct mctp_binding_astlpc, binding); |
| 556 | |
| 557 | return mctp_astlpc_init_host(astlpc); |
| 558 | } |
| 559 | |
| 560 | static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc, |
| 561 | uint8_t status, bool is_write) |
| 562 | { |
| 563 | bool is_bmc; |
| 564 | bool ready_state; |
| 565 | uint8_t flag; |
| 566 | |
| 567 | is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC); |
| 568 | flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF; |
| 569 | ready_state = is_write ? 0 : 1; |
| 570 | |
| 571 | return !!(status & flag) == ready_state; |
| 572 | } |
| 573 | |
| 574 | static inline bool |
| 575 | mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status) |
| 576 | { |
| 577 | return __mctp_astlpc_kcs_ready(astlpc, status, false); |
| 578 | } |
| 579 | |
| 580 | static inline bool |
| 581 | mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status) |
| 582 | { |
| 583 | return __mctp_astlpc_kcs_ready(astlpc, status, true); |
| 584 | } |
| 585 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 586 | static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc, |
| 587 | uint8_t data) |
| 588 | { |
| 589 | uint8_t status; |
| 590 | int rc; |
| 591 | |
| 592 | for (;;) { |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 593 | rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, |
| 594 | &status); |
Andrew Jeffery | 1b27fe8 | 2020-01-24 16:05:11 +1030 | [diff] [blame] | 595 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 596 | astlpc_prwarn(astlpc, "KCS status read failed"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 597 | return -1; |
| 598 | } |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 599 | if (mctp_astlpc_kcs_write_ready(astlpc, status)) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 600 | break; |
| 601 | /* todo: timeout */ |
| 602 | } |
| 603 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 604 | rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 605 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 606 | astlpc_prwarn(astlpc, "KCS data write failed"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 607 | return -1; |
| 608 | } |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | static int mctp_binding_astlpc_tx(struct mctp_binding *b, |
| 614 | struct mctp_pktbuf *pkt) |
| 615 | { |
| 616 | struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b); |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 617 | uint32_t len, len_be; |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 618 | struct mctp_hdr *hdr; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 619 | |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 620 | hdr = mctp_pktbuf_hdr(pkt); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 621 | len = mctp_pktbuf_size(pkt); |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 622 | |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 623 | astlpc_prdebug(astlpc, |
| 624 | "%s: Transmitting %" PRIu32 |
| 625 | "-byte packet (%hhu, %hhu, 0x%hhx)", |
| 626 | __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag); |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 627 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 628 | if (len > ASTLPC_BODY_SIZE(astlpc->layout.tx.size)) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 629 | astlpc_prwarn(astlpc, "invalid TX len 0x%x", len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 630 | return -1; |
| 631 | } |
| 632 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 633 | len_be = htobe32(len); |
| 634 | mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset, |
| 635 | sizeof(len_be)); |
| 636 | mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 637 | |
| 638 | mctp_binding_set_tx_enabled(b, false); |
| 639 | |
| 640 | mctp_astlpc_kcs_send(astlpc, 0x1); |
| 641 | return 0; |
| 642 | } |
| 643 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 644 | static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min, |
| 645 | uint16_t bmc_ver_cur, |
| 646 | uint16_t host_ver_min, |
| 647 | uint16_t host_ver_cur) |
| 648 | { |
| 649 | if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur, |
| 650 | host_ver_min, host_ver_cur)) |
| 651 | return ASTLPC_VER_BAD; |
| 652 | |
| 653 | if (bmc_ver_cur < host_ver_cur) |
| 654 | return bmc_ver_cur; |
| 655 | |
| 656 | return host_ver_cur; |
| 657 | } |
| 658 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 659 | static uint32_t mctp_astlpc_calculate_mtu(struct mctp_binding_astlpc *astlpc, |
| 660 | struct mctp_astlpc_layout *layout) |
| 661 | { |
| 662 | uint32_t low, high, limit; |
| 663 | |
| 664 | /* Derive the largest MTU the BMC _can_ support */ |
| 665 | low = MIN(astlpc->layout.rx.offset, astlpc->layout.tx.offset); |
| 666 | high = MAX(astlpc->layout.rx.offset, astlpc->layout.tx.offset); |
| 667 | limit = high - low; |
| 668 | |
| 669 | |
| 670 | /* Determine the accepted MTU, applied both directions by convention */ |
| 671 | return MCTP_BODY_SIZE(ASTLPC_BODY_SIZE(MIN(limit, layout->tx.size))); |
| 672 | } |
| 673 | |
| 674 | static int mctp_astlpc_negotiate_layout_bmc(struct mctp_binding_astlpc *astlpc) |
| 675 | { |
| 676 | struct mctp_astlpc_layout proposed, pending; |
| 677 | uint32_t sz, mtu; |
| 678 | int rc; |
| 679 | |
| 680 | /* Extract the host's proposed layout */ |
| 681 | rc = mctp_astlpc_layout_read(astlpc, &proposed); |
| 682 | if (rc < 0) |
| 683 | return rc; |
| 684 | |
| 685 | if (!mctp_astlpc_layout_validate(&proposed)) |
| 686 | return -EINVAL; |
| 687 | |
| 688 | /* Negotiate the MTU */ |
| 689 | mtu = mctp_astlpc_calculate_mtu(astlpc, &proposed); |
| 690 | sz = ASTLPC_PACKET_SIZE(MCTP_PACKET_SIZE(mtu)); |
| 691 | |
| 692 | /* |
| 693 | * Use symmetric MTUs by convention and to pass constraints in rx/tx |
| 694 | * functions |
| 695 | */ |
| 696 | pending = astlpc->layout; |
| 697 | pending.tx.size = sz; |
| 698 | pending.rx.size = sz; |
| 699 | |
| 700 | if (mctp_astlpc_layout_validate(&pending)) { |
| 701 | /* We found a sensible Rx MTU, so honour it */ |
| 702 | astlpc->layout = pending; |
| 703 | |
| 704 | /* Enforce the negotiated MTU */ |
| 705 | rc = mctp_astlpc_layout_write(astlpc, &astlpc->layout); |
| 706 | if (rc < 0) |
| 707 | return rc; |
| 708 | |
| 709 | astlpc_prinfo(astlpc, "Negotiated an MTU of %" PRIu32 " bytes", |
| 710 | mtu); |
| 711 | } else { |
| 712 | astlpc_prwarn(astlpc, "MTU negotiation failed"); |
| 713 | return -EINVAL; |
| 714 | } |
| 715 | |
| 716 | if (astlpc->version >= 2) |
| 717 | astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu); |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 722 | static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc) |
| 723 | { |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 724 | uint16_t negotiated, negotiated_be; |
| 725 | struct mctp_lpcmap_hdr hdr; |
| 726 | uint8_t status; |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 727 | int rc; |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 728 | |
| 729 | mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); |
| 730 | |
| 731 | /* Version negotiation */ |
| 732 | negotiated = |
| 733 | mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR, |
| 734 | be16toh(hdr.host_ver_min), |
| 735 | be16toh(hdr.host_ver_cur)); |
| 736 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 737 | /* Host Rx MTU negotiation: Failure terminates channel init */ |
| 738 | rc = mctp_astlpc_negotiate_layout_bmc(astlpc); |
| 739 | if (rc < 0) |
| 740 | negotiated = ASTLPC_VER_BAD; |
| 741 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 742 | /* Populate the negotiated version */ |
| 743 | astlpc->version = negotiated; |
| 744 | negotiated_be = htobe16(negotiated); |
| 745 | mctp_astlpc_lpc_write(astlpc, &negotiated_be, |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 746 | offsetof(struct mctp_lpcmap_hdr, negotiated_ver), |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 747 | sizeof(negotiated_be)); |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 748 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 749 | /* Finalise the configuration */ |
| 750 | status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF; |
| 751 | if (negotiated > 0) { |
| 752 | astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16, |
| 753 | negotiated); |
| 754 | status |= KCS_STATUS_CHANNEL_ACTIVE; |
| 755 | } else { |
| 756 | astlpc_prerr(astlpc, "Failed to initialise channel\n"); |
| 757 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 758 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 759 | mctp_astlpc_kcs_set_status(astlpc, status); |
| 760 | |
| 761 | mctp_binding_set_tx_enabled(&astlpc->binding, |
| 762 | status & KCS_STATUS_CHANNEL_ACTIVE); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc) |
| 766 | { |
| 767 | struct mctp_pktbuf *pkt; |
| 768 | uint32_t len; |
| 769 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 770 | mctp_astlpc_lpc_read(astlpc, &len, astlpc->layout.rx.offset, |
| 771 | sizeof(len)); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 772 | len = be32toh(len); |
| 773 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 774 | if (len > ASTLPC_BODY_SIZE(astlpc->layout.rx.size)) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 775 | astlpc_prwarn(astlpc, "invalid RX len 0x%x", len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 776 | return; |
| 777 | } |
| 778 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 779 | assert(astlpc->binding.pkt_size >= 0); |
| 780 | if (len > (uint32_t)astlpc->binding.pkt_size) { |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 781 | mctp_prwarn("invalid RX len 0x%x", len); |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 782 | astlpc_prwarn(astlpc, "invalid RX len 0x%x", len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 783 | return; |
| 784 | } |
| 785 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 786 | pkt = mctp_pktbuf_alloc(&astlpc->binding, len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 787 | if (!pkt) |
| 788 | goto out_complete; |
| 789 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 790 | mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt), |
| 791 | astlpc->layout.rx.offset + 4, len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 792 | |
| 793 | mctp_bus_rx(&astlpc->binding, pkt); |
| 794 | |
| 795 | out_complete: |
| 796 | mctp_astlpc_kcs_send(astlpc, 0x2); |
| 797 | } |
| 798 | |
| 799 | static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc) |
| 800 | { |
| 801 | mctp_binding_set_tx_enabled(&astlpc->binding, true); |
| 802 | } |
| 803 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 804 | static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc) |
| 805 | { |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 806 | struct mctp_astlpc_layout layout; |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 807 | uint16_t negotiated; |
| 808 | int rc; |
| 809 | |
| 810 | rc = mctp_astlpc_lpc_read(astlpc, &negotiated, |
| 811 | offsetof(struct mctp_lpcmap_hdr, |
| 812 | negotiated_ver), |
| 813 | sizeof(negotiated)); |
| 814 | if (rc < 0) |
| 815 | return rc; |
| 816 | |
| 817 | negotiated = be16toh(negotiated); |
| 818 | |
| 819 | if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN || |
| 820 | negotiated > ASTLPC_VER_CUR) { |
| 821 | astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n", |
| 822 | negotiated); |
| 823 | return -EINVAL; |
| 824 | } |
| 825 | |
| 826 | astlpc->version = negotiated; |
| 827 | |
Andrew Jeffery | 3a54066 | 2020-05-26 19:55:30 +0930 | [diff] [blame^] | 828 | rc = mctp_astlpc_layout_read(astlpc, &layout); |
| 829 | if (rc < 0) |
| 830 | return rc; |
| 831 | |
| 832 | if (!mctp_astlpc_layout_validate(&layout)) { |
| 833 | mctp_prerr("BMC proposed invalid buffer parameters"); |
| 834 | return -EINVAL; |
| 835 | } |
| 836 | |
| 837 | astlpc->layout = layout; |
| 838 | |
| 839 | if (negotiated >= 2) |
| 840 | astlpc->binding.pkt_size = |
| 841 | ASTLPC_BODY_SIZE(astlpc->layout.tx.size); |
| 842 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 843 | return 0; |
| 844 | } |
| 845 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 846 | static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc, |
| 847 | uint8_t status) |
| 848 | { |
| 849 | uint8_t updated; |
| 850 | int rc = 0; |
| 851 | |
| 852 | assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); |
| 853 | |
| 854 | updated = astlpc->kcs_status ^ status; |
| 855 | |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 856 | astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__, |
| 857 | status, updated); |
| 858 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 859 | if (updated & KCS_STATUS_BMC_READY) { |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 860 | if (status & KCS_STATUS_BMC_READY) { |
| 861 | astlpc->kcs_status = status; |
| 862 | return astlpc->binding.start(&astlpc->binding); |
| 863 | } else { |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 864 | mctp_binding_set_tx_enabled(&astlpc->binding, false); |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 865 | } |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 866 | } |
| 867 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame] | 868 | if (astlpc->version == 0 || updated & KCS_STATUS_CHANNEL_ACTIVE) { |
| 869 | bool enable; |
| 870 | |
| 871 | rc = mctp_astlpc_finalise_channel(astlpc); |
| 872 | enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0; |
| 873 | |
| 874 | mctp_binding_set_tx_enabled(&astlpc->binding, enable); |
| 875 | } |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 876 | |
| 877 | astlpc->kcs_status = status; |
| 878 | |
| 879 | return rc; |
| 880 | } |
| 881 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 882 | int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc) |
| 883 | { |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 884 | uint8_t status, data; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 885 | int rc; |
| 886 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 887 | rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 888 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 889 | astlpc_prwarn(astlpc, "KCS read error"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 890 | return -1; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 891 | } |
| 892 | |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 893 | astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status); |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 894 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 895 | if (!mctp_astlpc_kcs_read_ready(astlpc, status)) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 896 | return 0; |
| 897 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 898 | rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 899 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 900 | astlpc_prwarn(astlpc, "KCS data read error"); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 901 | return -1; |
| 902 | } |
| 903 | |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 904 | astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data); |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 905 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 906 | switch (data) { |
| 907 | case 0x0: |
| 908 | mctp_astlpc_init_channel(astlpc); |
| 909 | break; |
| 910 | case 0x1: |
| 911 | mctp_astlpc_rx_start(astlpc); |
| 912 | break; |
| 913 | case 0x2: |
| 914 | mctp_astlpc_tx_complete(astlpc); |
| 915 | break; |
Jeremy Kerr | 1a4b55a | 2019-06-24 14:22:39 +0800 | [diff] [blame] | 916 | case 0xff: |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 917 | /* No responsibilities for the BMC on 0xff */ |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 918 | if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) { |
| 919 | rc = mctp_astlpc_update_channel(astlpc, status); |
| 920 | if (rc < 0) |
| 921 | return rc; |
| 922 | } |
| 923 | break; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 924 | default: |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 925 | astlpc_prwarn(astlpc, "unknown message 0x%x", data); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 926 | } |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 927 | |
| 928 | /* Handle silent loss of bmc-ready */ |
| 929 | if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) { |
| 930 | if (!(status & KCS_STATUS_BMC_READY && data == 0xff)) |
| 931 | return mctp_astlpc_update_channel(astlpc, status); |
| 932 | } |
| 933 | |
| 934 | return rc; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 935 | } |
| 936 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 937 | /* allocate and basic initialisation */ |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 938 | static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode, |
| 939 | uint32_t mtu) |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 940 | { |
| 941 | struct mctp_binding_astlpc *astlpc; |
| 942 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 943 | assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) || |
| 944 | (mode == MCTP_BINDING_ASTLPC_MODE_HOST)); |
| 945 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 946 | astlpc = __mctp_alloc(sizeof(*astlpc)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 947 | if (!astlpc) |
| 948 | return NULL; |
| 949 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 950 | memset(astlpc, 0, sizeof(*astlpc)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 951 | astlpc->mode = mode; |
| 952 | astlpc->lpc_map = NULL; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 953 | astlpc->binding.name = "astlpc"; |
| 954 | astlpc->binding.version = 1; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 955 | astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 956 | astlpc->binding.pkt_pad = 0; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 957 | astlpc->binding.tx = mctp_binding_astlpc_tx; |
| 958 | if (mode == MCTP_BINDING_ASTLPC_MODE_BMC) |
| 959 | astlpc->binding.start = mctp_binding_astlpc_start_bmc; |
| 960 | else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST) |
| 961 | astlpc->binding.start = mctp_binding_astlpc_start_host; |
| 962 | else { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 963 | astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 964 | __mctp_free(astlpc); |
| 965 | return NULL; |
| 966 | } |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 967 | |
| 968 | return astlpc; |
| 969 | } |
| 970 | |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 971 | struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b) |
| 972 | { |
| 973 | return &b->binding; |
| 974 | } |
| 975 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 976 | struct mctp_binding_astlpc * |
| 977 | mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map, |
| 978 | const struct mctp_binding_astlpc_ops *ops, void *ops_data) |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 979 | { |
| 980 | struct mctp_binding_astlpc *astlpc; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 981 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 982 | if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC || |
| 983 | mode == MCTP_BINDING_ASTLPC_MODE_HOST)) { |
| 984 | mctp_prerr("Unknown binding mode: %u", mode); |
| 985 | return NULL; |
| 986 | } |
| 987 | |
| 988 | if (mtu != MCTP_BTU) { |
| 989 | mctp_prwarn("Unable to negotiate the MTU, using %u instead", |
| 990 | MCTP_BTU); |
| 991 | mtu = MCTP_BTU; |
| 992 | } |
| 993 | |
| 994 | astlpc = __mctp_astlpc_init(mode, mtu); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 995 | if (!astlpc) |
| 996 | return NULL; |
| 997 | |
| 998 | memcpy(&astlpc->ops, ops, sizeof(astlpc->ops)); |
| 999 | astlpc->ops_data = ops_data; |
| 1000 | astlpc->lpc_map = lpc_map; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1001 | astlpc->mode = mode; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1002 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1003 | return astlpc; |
| 1004 | } |
| 1005 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1006 | struct mctp_binding_astlpc * |
| 1007 | mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data, |
| 1008 | void *lpc_map) |
| 1009 | { |
| 1010 | return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map, |
| 1011 | ops, ops_data); |
| 1012 | } |
| 1013 | |
Andrew Jeffery | 4663f67 | 2020-03-10 23:36:24 +1030 | [diff] [blame] | 1014 | void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc) |
| 1015 | { |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 1016 | /* Clear channel-active and bmc-ready */ |
| 1017 | if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) |
| 1018 | mctp_astlpc_kcs_set_status(astlpc, KCS_STATUS_OBF); |
Andrew Jeffery | 4663f67 | 2020-03-10 23:36:24 +1030 | [diff] [blame] | 1019 | __mctp_free(astlpc); |
| 1020 | } |
| 1021 | |
Jeremy Kerr | b214c64 | 2019-11-27 11:34:00 +0800 | [diff] [blame] | 1022 | #ifdef MCTP_HAVE_FILEIO |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 1023 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1024 | static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1025 | { |
| 1026 | struct aspeed_lpc_ctrl_mapping map = { |
| 1027 | .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY, |
| 1028 | .window_id = 0, /* There's only one */ |
| 1029 | .flags = 0, |
| 1030 | .addr = 0, |
| 1031 | .offset = 0, |
| 1032 | .size = 0 |
| 1033 | }; |
Andrew Jeffery | 979c6a1 | 2020-05-23 20:04:49 +0930 | [diff] [blame] | 1034 | void *lpc_map_base; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1035 | int fd, rc; |
| 1036 | |
| 1037 | fd = open(lpc_path, O_RDWR | O_SYNC); |
| 1038 | if (fd < 0) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1039 | astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1040 | return -1; |
| 1041 | } |
| 1042 | |
| 1043 | rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map); |
| 1044 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1045 | astlpc_prwarn(astlpc, "LPC GET_SIZE failed"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1046 | close(fd); |
| 1047 | return -1; |
| 1048 | } |
| 1049 | |
Andrew Jeffery | 979c6a1 | 2020-05-23 20:04:49 +0930 | [diff] [blame] | 1050 | lpc_map_base = |
| 1051 | mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 1052 | if (lpc_map_base == MAP_FAILED) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1053 | astlpc_prwarn(astlpc, "LPC mmap failed"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1054 | rc = -1; |
| 1055 | } else { |
Andrew Jeffery | 979c6a1 | 2020-05-23 20:04:49 +0930 | [diff] [blame] | 1056 | astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | close(fd); |
| 1060 | |
| 1061 | return rc; |
| 1062 | } |
| 1063 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1064 | static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1065 | { |
| 1066 | astlpc->kcs_fd = open(kcs_path, O_RDWR); |
| 1067 | if (astlpc->kcs_fd < 0) |
| 1068 | return -1; |
| 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1073 | static int __mctp_astlpc_fileio_kcs_read(void *arg, |
| 1074 | enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val) |
| 1075 | { |
| 1076 | struct mctp_binding_astlpc *astlpc = arg; |
| 1077 | off_t offset = reg; |
| 1078 | int rc; |
| 1079 | |
| 1080 | rc = pread(astlpc->kcs_fd, val, 1, offset); |
| 1081 | |
| 1082 | return rc == 1 ? 0 : -1; |
| 1083 | } |
| 1084 | |
| 1085 | static int __mctp_astlpc_fileio_kcs_write(void *arg, |
| 1086 | enum mctp_binding_astlpc_kcs_reg reg, uint8_t val) |
| 1087 | { |
| 1088 | struct mctp_binding_astlpc *astlpc = arg; |
| 1089 | off_t offset = reg; |
| 1090 | int rc; |
| 1091 | |
| 1092 | rc = pwrite(astlpc->kcs_fd, &val, 1, offset); |
| 1093 | |
| 1094 | return rc == 1 ? 0 : -1; |
| 1095 | } |
| 1096 | |
| 1097 | int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc) |
| 1098 | { |
| 1099 | return astlpc->kcs_fd; |
| 1100 | } |
| 1101 | |
| 1102 | struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1103 | { |
| 1104 | struct mctp_binding_astlpc *astlpc; |
| 1105 | int rc; |
| 1106 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 1107 | /* |
| 1108 | * If we're doing file IO then we're very likely not running |
| 1109 | * freestanding, so lets assume that we're on the BMC side |
| 1110 | */ |
| 1111 | astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1112 | if (!astlpc) |
| 1113 | return NULL; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1114 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1115 | /* Set internal operations for kcs. We use direct accesses to the lpc |
| 1116 | * map area */ |
| 1117 | astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read; |
| 1118 | astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write; |
| 1119 | astlpc->ops_data = astlpc; |
| 1120 | |
| 1121 | rc = mctp_astlpc_init_fileio_lpc(astlpc); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1122 | if (rc) { |
| 1123 | free(astlpc); |
| 1124 | return NULL; |
| 1125 | } |
| 1126 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 1127 | rc = mctp_astlpc_init_fileio_kcs(astlpc); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1128 | if (rc) { |
| 1129 | free(astlpc); |
| 1130 | return NULL; |
| 1131 | } |
| 1132 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1133 | return astlpc; |
| 1134 | } |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 1135 | #else |
| 1136 | struct mctp_binding_astlpc * __attribute__((const)) |
| 1137 | mctp_astlpc_init_fileio(void) |
| 1138 | { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1139 | astlpc_prerr(astlpc, "Missing support for file IO"); |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 1140 | return NULL; |
| 1141 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 1142 | |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 1143 | int __attribute__((const)) mctp_astlpc_get_fd( |
| 1144 | struct mctp_binding_astlpc *astlpc __attribute__((unused))) |
| 1145 | { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 1146 | astlpc_prerr(astlpc, "Missing support for file IO"); |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 1147 | return -1; |
| 1148 | } |
| 1149 | #endif |