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