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 |
| 94 | #define ASTLPC_VER_CUR 1 |
| 95 | |
| 96 | #define ASTLPC_BODY_SIZE(sz) ((sz) - 4) |
| 97 | /* clang-format on */ |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 98 | |
| 99 | struct mctp_lpcmap_hdr { |
| 100 | uint32_t magic; |
| 101 | |
| 102 | uint16_t bmc_ver_min; |
| 103 | uint16_t bmc_ver_cur; |
| 104 | uint16_t host_ver_min; |
| 105 | uint16_t host_ver_cur; |
| 106 | uint16_t negotiated_ver; |
| 107 | uint16_t pad0; |
| 108 | |
| 109 | uint32_t rx_offset; |
| 110 | uint32_t rx_size; |
| 111 | uint32_t tx_offset; |
| 112 | uint32_t tx_size; |
| 113 | } __attribute__((packed)); |
| 114 | |
| 115 | /* layout of TX/RX areas */ |
| 116 | static const uint32_t rx_offset = 0x100; |
| 117 | static const uint32_t rx_size = 0x100; |
| 118 | static const uint32_t tx_offset = 0x200; |
| 119 | static const uint32_t tx_size = 0x100; |
| 120 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 121 | #define LPC_WIN_SIZE (1 * 1024 * 1024) |
| 122 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 123 | #define KCS_STATUS_BMC_READY 0x80 |
| 124 | #define KCS_STATUS_CHANNEL_ACTIVE 0x40 |
| 125 | #define KCS_STATUS_IBF 0x02 |
| 126 | #define KCS_STATUS_OBF 0x01 |
| 127 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 128 | static inline int mctp_astlpc_kcs_write(struct mctp_binding_astlpc *astlpc, |
| 129 | enum mctp_binding_astlpc_kcs_reg reg, |
| 130 | uint8_t val) |
| 131 | { |
| 132 | return astlpc->ops.kcs_write(astlpc->ops_data, reg, val); |
| 133 | } |
| 134 | |
| 135 | static inline int mctp_astlpc_kcs_read(struct mctp_binding_astlpc *astlpc, |
| 136 | enum mctp_binding_astlpc_kcs_reg reg, |
| 137 | uint8_t *val) |
| 138 | { |
| 139 | return astlpc->ops.kcs_read(astlpc->ops_data, reg, val); |
| 140 | } |
| 141 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 142 | static inline int mctp_astlpc_lpc_write(struct mctp_binding_astlpc *astlpc, |
| 143 | const void *buf, long offset, |
| 144 | size_t len) |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 145 | { |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 146 | astlpc_prdebug(astlpc, "%s: %zu bytes to 0x%lx", __func__, len, offset); |
| 147 | |
| 148 | assert(offset >= 0); |
| 149 | |
| 150 | /* Indirect access */ |
| 151 | if (astlpc->ops.lpc_write) { |
| 152 | void *data = astlpc->ops_data; |
| 153 | |
| 154 | return astlpc->ops.lpc_write(data, buf, offset, len); |
| 155 | } |
| 156 | |
| 157 | /* Direct mapping */ |
| 158 | assert(astlpc->lpc_map); |
| 159 | memcpy(&((char *)astlpc->lpc_map)[offset], buf, len); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static inline int mctp_astlpc_lpc_read(struct mctp_binding_astlpc *astlpc, |
| 165 | void *buf, long offset, size_t len) |
| 166 | { |
| 167 | astlpc_prdebug(astlpc, "%s: %zu bytes from 0x%lx", __func__, len, |
| 168 | offset); |
| 169 | |
| 170 | assert(offset >= 0); |
| 171 | |
| 172 | /* Indirect access */ |
| 173 | if (astlpc->ops.lpc_read) { |
| 174 | void *data = astlpc->ops_data; |
| 175 | |
| 176 | return astlpc->ops.lpc_read(data, buf, offset, len); |
| 177 | } |
| 178 | |
| 179 | /* Direct mapping */ |
| 180 | assert(astlpc->lpc_map); |
| 181 | memcpy(buf, &((char *)astlpc->lpc_map)[offset], len); |
| 182 | |
| 183 | return 0; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 184 | } |
| 185 | |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 186 | static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc, |
| 187 | uint8_t status) |
| 188 | { |
| 189 | uint8_t data; |
| 190 | int rc; |
| 191 | |
| 192 | /* Since we're setting the status register, we want the other endpoint |
| 193 | * to be interrupted. However, some hardware may only raise a host-side |
| 194 | * interrupt on an ODR event. |
| 195 | * So, write a dummy value of 0xff to ODR, which will ensure that an |
| 196 | * interrupt is triggered, and can be ignored by the host. |
| 197 | */ |
| 198 | data = 0xff; |
| 199 | status |= KCS_STATUS_OBF; |
| 200 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 201 | rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, status); |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 202 | if (rc) { |
| 203 | astlpc_prwarn(astlpc, "KCS status write failed"); |
| 204 | return -1; |
| 205 | } |
| 206 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 207 | rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data); |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 208 | if (rc) { |
| 209 | astlpc_prwarn(astlpc, "KCS dummy data write failed"); |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 216 | static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc) |
| 217 | { |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 218 | struct mctp_lpcmap_hdr hdr = { 0 }; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 219 | uint8_t status; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 220 | |
| 221 | /* Flip the buffers as the names are defined in terms of the host */ |
| 222 | astlpc->layout.rx.offset = tx_offset; |
| 223 | astlpc->layout.rx.size = tx_size; |
| 224 | astlpc->layout.tx.offset = rx_offset; |
| 225 | astlpc->layout.tx.size = rx_size; |
| 226 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 227 | hdr = (struct mctp_lpcmap_hdr){ |
| 228 | .magic = htobe32(ASTLPC_MCTP_MAGIC), |
| 229 | .bmc_ver_min = htobe16(ASTLPC_VER_MIN), |
| 230 | .bmc_ver_cur = htobe16(ASTLPC_VER_CUR), |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 231 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 232 | /* Flip the buffers back as we're now describing the host's |
| 233 | * configuration to the host */ |
| 234 | .rx_offset = htobe32(astlpc->layout.tx.offset), |
| 235 | .rx_size = htobe32(astlpc->layout.tx.size), |
| 236 | .tx_offset = htobe32(astlpc->layout.rx.offset), |
| 237 | .tx_size = htobe32(astlpc->layout.rx.size), |
| 238 | }; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 239 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 240 | mctp_astlpc_lpc_write(astlpc, &hdr, 0, sizeof(hdr)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 241 | |
| 242 | /* set status indicating that the BMC is now active */ |
| 243 | status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF; |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 244 | return mctp_astlpc_kcs_set_status(astlpc, status); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static int mctp_binding_astlpc_start_bmc(struct mctp_binding *b) |
| 248 | { |
| 249 | struct mctp_binding_astlpc *astlpc = |
| 250 | container_of(b, struct mctp_binding_astlpc, binding); |
| 251 | |
| 252 | return mctp_astlpc_init_bmc(astlpc); |
| 253 | } |
| 254 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame^] | 255 | static bool mctp_astlpc_validate_version(uint16_t bmc_ver_min, |
| 256 | uint16_t bmc_ver_cur, |
| 257 | uint16_t host_ver_min, |
| 258 | uint16_t host_ver_cur) |
| 259 | { |
| 260 | if (!(bmc_ver_min && bmc_ver_cur && host_ver_min && host_ver_cur)) { |
| 261 | mctp_prerr("Invalid version present in [%" PRIu16 ", %" PRIu16 |
| 262 | "], [%" PRIu16 ", %" PRIu16 "]", |
| 263 | bmc_ver_min, bmc_ver_cur, host_ver_min, |
| 264 | host_ver_cur); |
| 265 | return false; |
| 266 | } else if (bmc_ver_min > bmc_ver_cur) { |
| 267 | mctp_prerr("Invalid bmc version range [%" PRIu16 ", %" PRIu16 |
| 268 | "]", |
| 269 | bmc_ver_min, bmc_ver_cur); |
| 270 | return false; |
| 271 | } else if (host_ver_min > host_ver_cur) { |
| 272 | mctp_prerr("Invalid host version range [%" PRIu16 ", %" PRIu16 |
| 273 | "]", |
| 274 | host_ver_min, host_ver_cur); |
| 275 | return false; |
| 276 | } else if ((host_ver_cur < bmc_ver_min) || |
| 277 | (host_ver_min > bmc_ver_cur)) { |
| 278 | mctp_prerr( |
| 279 | "Unable to satisfy version negotiation with ranges [%" PRIu16 |
| 280 | ", %" PRIu16 "] and [%" PRIu16 ", %" PRIu16 "]", |
| 281 | bmc_ver_min, bmc_ver_cur, host_ver_min, host_ver_cur); |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | return true; |
| 286 | } |
| 287 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 288 | static int mctp_astlpc_init_host(struct mctp_binding_astlpc *astlpc) |
| 289 | { |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 290 | const uint16_t ver_min_be = htobe16(ASTLPC_VER_MIN); |
| 291 | const uint16_t ver_cur_be = htobe16(ASTLPC_VER_CUR); |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame^] | 292 | uint16_t bmc_ver_min, bmc_ver_cur; |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 293 | struct mctp_lpcmap_hdr hdr; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 294 | uint8_t status; |
| 295 | int rc; |
| 296 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 297 | rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 298 | if (rc) { |
| 299 | mctp_prwarn("KCS status read failed"); |
| 300 | return rc; |
| 301 | } |
| 302 | |
| 303 | astlpc->kcs_status = status; |
| 304 | |
| 305 | if (!(status & KCS_STATUS_BMC_READY)) |
| 306 | return -EHOSTDOWN; |
| 307 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 308 | mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 309 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 310 | astlpc->layout.rx.offset = be32toh(hdr.rx_offset); |
| 311 | astlpc->layout.rx.size = be32toh(hdr.rx_size); |
| 312 | astlpc->layout.tx.offset = be32toh(hdr.tx_offset); |
| 313 | astlpc->layout.tx.size = be32toh(hdr.tx_size); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 314 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame^] | 315 | bmc_ver_min = be16toh(hdr.bmc_ver_min); |
| 316 | bmc_ver_cur = be16toh(hdr.bmc_ver_cur); |
| 317 | |
| 318 | if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur, |
| 319 | ASTLPC_VER_MIN, ASTLPC_VER_CUR)) { |
| 320 | astlpc_prerr(astlpc, "Cannot negotiate with invalid versions"); |
| 321 | return -EINVAL; |
| 322 | } |
| 323 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 324 | mctp_astlpc_lpc_write(astlpc, &ver_min_be, |
| 325 | offsetof(struct mctp_lpcmap_hdr, host_ver_min), |
| 326 | sizeof(ver_min_be)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 327 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 328 | mctp_astlpc_lpc_write(astlpc, &ver_cur_be, |
| 329 | offsetof(struct mctp_lpcmap_hdr, host_ver_cur), |
| 330 | sizeof(ver_cur_be)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 331 | |
| 332 | /* Send channel init command */ |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 333 | rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, 0x0); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 334 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 335 | astlpc_prwarn(astlpc, "KCS write failed"); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return rc; |
| 339 | } |
| 340 | |
| 341 | static int mctp_binding_astlpc_start_host(struct mctp_binding *b) |
| 342 | { |
| 343 | struct mctp_binding_astlpc *astlpc = |
| 344 | container_of(b, struct mctp_binding_astlpc, binding); |
| 345 | |
| 346 | return mctp_astlpc_init_host(astlpc); |
| 347 | } |
| 348 | |
| 349 | static bool __mctp_astlpc_kcs_ready(struct mctp_binding_astlpc *astlpc, |
| 350 | uint8_t status, bool is_write) |
| 351 | { |
| 352 | bool is_bmc; |
| 353 | bool ready_state; |
| 354 | uint8_t flag; |
| 355 | |
| 356 | is_bmc = (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC); |
| 357 | flag = (is_bmc ^ is_write) ? KCS_STATUS_IBF : KCS_STATUS_OBF; |
| 358 | ready_state = is_write ? 0 : 1; |
| 359 | |
| 360 | return !!(status & flag) == ready_state; |
| 361 | } |
| 362 | |
| 363 | static inline bool |
| 364 | mctp_astlpc_kcs_read_ready(struct mctp_binding_astlpc *astlpc, uint8_t status) |
| 365 | { |
| 366 | return __mctp_astlpc_kcs_ready(astlpc, status, false); |
| 367 | } |
| 368 | |
| 369 | static inline bool |
| 370 | mctp_astlpc_kcs_write_ready(struct mctp_binding_astlpc *astlpc, uint8_t status) |
| 371 | { |
| 372 | return __mctp_astlpc_kcs_ready(astlpc, status, true); |
| 373 | } |
| 374 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 375 | static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc, |
| 376 | uint8_t data) |
| 377 | { |
| 378 | uint8_t status; |
| 379 | int rc; |
| 380 | |
| 381 | for (;;) { |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 382 | rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, |
| 383 | &status); |
Andrew Jeffery | 1b27fe8 | 2020-01-24 16:05:11 +1030 | [diff] [blame] | 384 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 385 | astlpc_prwarn(astlpc, "KCS status read failed"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 386 | return -1; |
| 387 | } |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 388 | if (mctp_astlpc_kcs_write_ready(astlpc, status)) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 389 | break; |
| 390 | /* todo: timeout */ |
| 391 | } |
| 392 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 393 | rc = mctp_astlpc_kcs_write(astlpc, MCTP_ASTLPC_KCS_REG_DATA, data); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 394 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 395 | astlpc_prwarn(astlpc, "KCS data write failed"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static int mctp_binding_astlpc_tx(struct mctp_binding *b, |
| 403 | struct mctp_pktbuf *pkt) |
| 404 | { |
| 405 | struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b); |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 406 | uint32_t len, len_be; |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 407 | struct mctp_hdr *hdr; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 408 | |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 409 | hdr = mctp_pktbuf_hdr(pkt); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 410 | len = mctp_pktbuf_size(pkt); |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 411 | |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 412 | astlpc_prdebug(astlpc, |
| 413 | "%s: Transmitting %" PRIu32 |
| 414 | "-byte packet (%hhu, %hhu, 0x%hhx)", |
| 415 | __func__, len, hdr->src, hdr->dest, hdr->flags_seq_tag); |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 416 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 417 | if (len > ASTLPC_BODY_SIZE(astlpc->layout.tx.size)) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 418 | astlpc_prwarn(astlpc, "invalid TX len 0x%x", len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 419 | return -1; |
| 420 | } |
| 421 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 422 | len_be = htobe32(len); |
| 423 | mctp_astlpc_lpc_write(astlpc, &len_be, astlpc->layout.tx.offset, |
| 424 | sizeof(len_be)); |
| 425 | mctp_astlpc_lpc_write(astlpc, hdr, astlpc->layout.tx.offset + 4, len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 426 | |
| 427 | mctp_binding_set_tx_enabled(b, false); |
| 428 | |
| 429 | mctp_astlpc_kcs_send(astlpc, 0x1); |
| 430 | return 0; |
| 431 | } |
| 432 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame^] | 433 | static uint16_t mctp_astlpc_negotiate_version(uint16_t bmc_ver_min, |
| 434 | uint16_t bmc_ver_cur, |
| 435 | uint16_t host_ver_min, |
| 436 | uint16_t host_ver_cur) |
| 437 | { |
| 438 | if (!mctp_astlpc_validate_version(bmc_ver_min, bmc_ver_cur, |
| 439 | host_ver_min, host_ver_cur)) |
| 440 | return ASTLPC_VER_BAD; |
| 441 | |
| 442 | if (bmc_ver_cur < host_ver_cur) |
| 443 | return bmc_ver_cur; |
| 444 | |
| 445 | return host_ver_cur; |
| 446 | } |
| 447 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 448 | static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc) |
| 449 | { |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame^] | 450 | uint16_t negotiated, negotiated_be; |
| 451 | struct mctp_lpcmap_hdr hdr; |
| 452 | uint8_t status; |
| 453 | |
| 454 | mctp_astlpc_lpc_read(astlpc, &hdr, 0, sizeof(hdr)); |
| 455 | |
| 456 | /* Version negotiation */ |
| 457 | negotiated = |
| 458 | mctp_astlpc_negotiate_version(ASTLPC_VER_MIN, ASTLPC_VER_CUR, |
| 459 | be16toh(hdr.host_ver_min), |
| 460 | be16toh(hdr.host_ver_cur)); |
| 461 | |
| 462 | /* Populate the negotiated version */ |
| 463 | astlpc->version = negotiated; |
| 464 | negotiated_be = htobe16(negotiated); |
| 465 | mctp_astlpc_lpc_write(astlpc, &negotiated_be, |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 466 | offsetof(struct mctp_lpcmap_hdr, negotiated_ver), |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame^] | 467 | sizeof(negotiated_be)); |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 468 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame^] | 469 | /* Finalise the configuration */ |
| 470 | status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF; |
| 471 | if (negotiated > 0) { |
| 472 | astlpc_prinfo(astlpc, "Negotiated binding version %" PRIu16, |
| 473 | negotiated); |
| 474 | status |= KCS_STATUS_CHANNEL_ACTIVE; |
| 475 | } else { |
| 476 | astlpc_prerr(astlpc, "Failed to initialise channel\n"); |
| 477 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 478 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame^] | 479 | mctp_astlpc_kcs_set_status(astlpc, status); |
| 480 | |
| 481 | mctp_binding_set_tx_enabled(&astlpc->binding, |
| 482 | status & KCS_STATUS_CHANNEL_ACTIVE); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc) |
| 486 | { |
| 487 | struct mctp_pktbuf *pkt; |
| 488 | uint32_t len; |
| 489 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 490 | mctp_astlpc_lpc_read(astlpc, &len, astlpc->layout.rx.offset, |
| 491 | sizeof(len)); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 492 | len = be32toh(len); |
| 493 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 494 | if (len > ASTLPC_BODY_SIZE(astlpc->layout.rx.size)) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 495 | astlpc_prwarn(astlpc, "invalid RX len 0x%x", len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 496 | return; |
| 497 | } |
| 498 | |
Andrew Jeffery | b93b611 | 2020-06-05 14:13:44 +0930 | [diff] [blame] | 499 | assert(astlpc->binding.pkt_size >= 0); |
| 500 | if (len > (uint32_t)astlpc->binding.pkt_size) { |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 501 | mctp_prwarn("invalid RX len 0x%x", len); |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 502 | astlpc_prwarn(astlpc, "invalid RX len 0x%x", len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 503 | return; |
| 504 | } |
| 505 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 506 | pkt = mctp_pktbuf_alloc(&astlpc->binding, len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 507 | if (!pkt) |
| 508 | goto out_complete; |
| 509 | |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 510 | mctp_astlpc_lpc_read(astlpc, mctp_pktbuf_hdr(pkt), |
| 511 | astlpc->layout.rx.offset + 4, len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 512 | |
| 513 | mctp_bus_rx(&astlpc->binding, pkt); |
| 514 | |
| 515 | out_complete: |
| 516 | mctp_astlpc_kcs_send(astlpc, 0x2); |
| 517 | } |
| 518 | |
| 519 | static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc) |
| 520 | { |
| 521 | mctp_binding_set_tx_enabled(&astlpc->binding, true); |
| 522 | } |
| 523 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame^] | 524 | static int mctp_astlpc_finalise_channel(struct mctp_binding_astlpc *astlpc) |
| 525 | { |
| 526 | uint16_t negotiated; |
| 527 | int rc; |
| 528 | |
| 529 | rc = mctp_astlpc_lpc_read(astlpc, &negotiated, |
| 530 | offsetof(struct mctp_lpcmap_hdr, |
| 531 | negotiated_ver), |
| 532 | sizeof(negotiated)); |
| 533 | if (rc < 0) |
| 534 | return rc; |
| 535 | |
| 536 | negotiated = be16toh(negotiated); |
| 537 | |
| 538 | if (negotiated == ASTLPC_VER_BAD || negotiated < ASTLPC_VER_MIN || |
| 539 | negotiated > ASTLPC_VER_CUR) { |
| 540 | astlpc_prerr(astlpc, "Failed to negotiate version, got: %u\n", |
| 541 | negotiated); |
| 542 | return -EINVAL; |
| 543 | } |
| 544 | |
| 545 | astlpc->version = negotiated; |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 550 | static int mctp_astlpc_update_channel(struct mctp_binding_astlpc *astlpc, |
| 551 | uint8_t status) |
| 552 | { |
| 553 | uint8_t updated; |
| 554 | int rc = 0; |
| 555 | |
| 556 | assert(astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST); |
| 557 | |
| 558 | updated = astlpc->kcs_status ^ status; |
| 559 | |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 560 | astlpc_prdebug(astlpc, "%s: status: 0x%x, update: 0x%x", __func__, |
| 561 | status, updated); |
| 562 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 563 | if (updated & KCS_STATUS_BMC_READY) { |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 564 | if (status & KCS_STATUS_BMC_READY) { |
| 565 | astlpc->kcs_status = status; |
| 566 | return astlpc->binding.start(&astlpc->binding); |
| 567 | } else { |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 568 | mctp_binding_set_tx_enabled(&astlpc->binding, false); |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 569 | } |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 570 | } |
| 571 | |
Andrew Jeffery | 4e8264b | 2020-05-23 20:34:33 +0930 | [diff] [blame^] | 572 | if (astlpc->version == 0 || updated & KCS_STATUS_CHANNEL_ACTIVE) { |
| 573 | bool enable; |
| 574 | |
| 575 | rc = mctp_astlpc_finalise_channel(astlpc); |
| 576 | enable = (status & KCS_STATUS_CHANNEL_ACTIVE) && rc == 0; |
| 577 | |
| 578 | mctp_binding_set_tx_enabled(&astlpc->binding, enable); |
| 579 | } |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 580 | |
| 581 | astlpc->kcs_status = status; |
| 582 | |
| 583 | return rc; |
| 584 | } |
| 585 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 586 | int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc) |
| 587 | { |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 588 | uint8_t status, data; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 589 | int rc; |
| 590 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 591 | rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_STATUS, &status); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 592 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 593 | astlpc_prwarn(astlpc, "KCS read error"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 594 | return -1; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 595 | } |
| 596 | |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 597 | astlpc_prdebug(astlpc, "%s: status: 0x%hhx", __func__, status); |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 598 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 599 | if (!mctp_astlpc_kcs_read_ready(astlpc, status)) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 600 | return 0; |
| 601 | |
Andrew Jeffery | f13cb97 | 2020-05-28 09:30:09 +0930 | [diff] [blame] | 602 | rc = mctp_astlpc_kcs_read(astlpc, MCTP_ASTLPC_KCS_REG_DATA, &data); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 603 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 604 | astlpc_prwarn(astlpc, "KCS data read error"); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 605 | return -1; |
| 606 | } |
| 607 | |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 608 | astlpc_prdebug(astlpc, "%s: data: 0x%hhx", __func__, data); |
Andrew Jeffery | edfe383 | 2020-02-06 11:52:11 +1030 | [diff] [blame] | 609 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 610 | switch (data) { |
| 611 | case 0x0: |
| 612 | mctp_astlpc_init_channel(astlpc); |
| 613 | break; |
| 614 | case 0x1: |
| 615 | mctp_astlpc_rx_start(astlpc); |
| 616 | break; |
| 617 | case 0x2: |
| 618 | mctp_astlpc_tx_complete(astlpc); |
| 619 | break; |
Jeremy Kerr | 1a4b55a | 2019-06-24 14:22:39 +0800 | [diff] [blame] | 620 | case 0xff: |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 621 | /* No responsibilities for the BMC on 0xff */ |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 622 | if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) { |
| 623 | rc = mctp_astlpc_update_channel(astlpc, status); |
| 624 | if (rc < 0) |
| 625 | return rc; |
| 626 | } |
| 627 | break; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 628 | default: |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 629 | astlpc_prwarn(astlpc, "unknown message 0x%x", data); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 630 | } |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 631 | |
| 632 | /* Handle silent loss of bmc-ready */ |
| 633 | if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_HOST) { |
| 634 | if (!(status & KCS_STATUS_BMC_READY && data == 0xff)) |
| 635 | return mctp_astlpc_update_channel(astlpc, status); |
| 636 | } |
| 637 | |
| 638 | return rc; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 639 | } |
| 640 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 641 | /* allocate and basic initialisation */ |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 642 | static struct mctp_binding_astlpc *__mctp_astlpc_init(uint8_t mode, |
| 643 | uint32_t mtu) |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 644 | { |
| 645 | struct mctp_binding_astlpc *astlpc; |
| 646 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 647 | assert((mode == MCTP_BINDING_ASTLPC_MODE_BMC) || |
| 648 | (mode == MCTP_BINDING_ASTLPC_MODE_HOST)); |
| 649 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 650 | astlpc = __mctp_alloc(sizeof(*astlpc)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 651 | if (!astlpc) |
| 652 | return NULL; |
| 653 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 654 | memset(astlpc, 0, sizeof(*astlpc)); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 655 | astlpc->mode = mode; |
| 656 | astlpc->lpc_map = NULL; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 657 | astlpc->binding.name = "astlpc"; |
| 658 | astlpc->binding.version = 1; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 659 | astlpc->binding.pkt_size = MCTP_PACKET_SIZE(mtu); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 660 | astlpc->binding.pkt_pad = 0; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 661 | astlpc->binding.tx = mctp_binding_astlpc_tx; |
| 662 | if (mode == MCTP_BINDING_ASTLPC_MODE_BMC) |
| 663 | astlpc->binding.start = mctp_binding_astlpc_start_bmc; |
| 664 | else if (mode == MCTP_BINDING_ASTLPC_MODE_HOST) |
| 665 | astlpc->binding.start = mctp_binding_astlpc_start_host; |
| 666 | else { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 667 | astlpc_prerr(astlpc, "%s: Invalid mode: %d\n", __func__, mode); |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 668 | __mctp_free(astlpc); |
| 669 | return NULL; |
| 670 | } |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 671 | |
| 672 | return astlpc; |
| 673 | } |
| 674 | |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 675 | struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b) |
| 676 | { |
| 677 | return &b->binding; |
| 678 | } |
| 679 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 680 | struct mctp_binding_astlpc * |
| 681 | mctp_astlpc_init(uint8_t mode, uint32_t mtu, void *lpc_map, |
| 682 | const struct mctp_binding_astlpc_ops *ops, void *ops_data) |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 683 | { |
| 684 | struct mctp_binding_astlpc *astlpc; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 685 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 686 | if (!(mode == MCTP_BINDING_ASTLPC_MODE_BMC || |
| 687 | mode == MCTP_BINDING_ASTLPC_MODE_HOST)) { |
| 688 | mctp_prerr("Unknown binding mode: %u", mode); |
| 689 | return NULL; |
| 690 | } |
| 691 | |
| 692 | if (mtu != MCTP_BTU) { |
| 693 | mctp_prwarn("Unable to negotiate the MTU, using %u instead", |
| 694 | MCTP_BTU); |
| 695 | mtu = MCTP_BTU; |
| 696 | } |
| 697 | |
| 698 | astlpc = __mctp_astlpc_init(mode, mtu); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 699 | if (!astlpc) |
| 700 | return NULL; |
| 701 | |
| 702 | memcpy(&astlpc->ops, ops, sizeof(astlpc->ops)); |
| 703 | astlpc->ops_data = ops_data; |
| 704 | astlpc->lpc_map = lpc_map; |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 705 | astlpc->mode = mode; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 706 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 707 | return astlpc; |
| 708 | } |
| 709 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 710 | struct mctp_binding_astlpc * |
| 711 | mctp_astlpc_init_ops(const struct mctp_binding_astlpc_ops *ops, void *ops_data, |
| 712 | void *lpc_map) |
| 713 | { |
| 714 | return mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU, lpc_map, |
| 715 | ops, ops_data); |
| 716 | } |
| 717 | |
Andrew Jeffery | 4663f67 | 2020-03-10 23:36:24 +1030 | [diff] [blame] | 718 | void mctp_astlpc_destroy(struct mctp_binding_astlpc *astlpc) |
| 719 | { |
Andrew Jeffery | d0f5da0 | 2020-05-28 09:12:55 +0930 | [diff] [blame] | 720 | /* Clear channel-active and bmc-ready */ |
| 721 | if (astlpc->mode == MCTP_BINDING_ASTLPC_MODE_BMC) |
| 722 | mctp_astlpc_kcs_set_status(astlpc, KCS_STATUS_OBF); |
Andrew Jeffery | 4663f67 | 2020-03-10 23:36:24 +1030 | [diff] [blame] | 723 | __mctp_free(astlpc); |
| 724 | } |
| 725 | |
Jeremy Kerr | b214c64 | 2019-11-27 11:34:00 +0800 | [diff] [blame] | 726 | #ifdef MCTP_HAVE_FILEIO |
Andrew Jeffery | 55fb90b | 2020-05-12 13:54:37 +0930 | [diff] [blame] | 727 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 728 | static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 729 | { |
| 730 | struct aspeed_lpc_ctrl_mapping map = { |
| 731 | .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY, |
| 732 | .window_id = 0, /* There's only one */ |
| 733 | .flags = 0, |
| 734 | .addr = 0, |
| 735 | .offset = 0, |
| 736 | .size = 0 |
| 737 | }; |
Andrew Jeffery | 979c6a1 | 2020-05-23 20:04:49 +0930 | [diff] [blame] | 738 | void *lpc_map_base; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 739 | int fd, rc; |
| 740 | |
| 741 | fd = open(lpc_path, O_RDWR | O_SYNC); |
| 742 | if (fd < 0) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 743 | astlpc_prwarn(astlpc, "LPC open (%s) failed", lpc_path); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 744 | return -1; |
| 745 | } |
| 746 | |
| 747 | rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map); |
| 748 | if (rc) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 749 | astlpc_prwarn(astlpc, "LPC GET_SIZE failed"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 750 | close(fd); |
| 751 | return -1; |
| 752 | } |
| 753 | |
Andrew Jeffery | 979c6a1 | 2020-05-23 20:04:49 +0930 | [diff] [blame] | 754 | lpc_map_base = |
| 755 | mmap(NULL, map.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 756 | if (lpc_map_base == MAP_FAILED) { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 757 | astlpc_prwarn(astlpc, "LPC mmap failed"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 758 | rc = -1; |
| 759 | } else { |
Andrew Jeffery | 979c6a1 | 2020-05-23 20:04:49 +0930 | [diff] [blame] | 760 | astlpc->lpc_map = lpc_map_base + map.size - LPC_WIN_SIZE; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | close(fd); |
| 764 | |
| 765 | return rc; |
| 766 | } |
| 767 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 768 | static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 769 | { |
| 770 | astlpc->kcs_fd = open(kcs_path, O_RDWR); |
| 771 | if (astlpc->kcs_fd < 0) |
| 772 | return -1; |
| 773 | |
| 774 | return 0; |
| 775 | } |
| 776 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 777 | static int __mctp_astlpc_fileio_kcs_read(void *arg, |
| 778 | enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val) |
| 779 | { |
| 780 | struct mctp_binding_astlpc *astlpc = arg; |
| 781 | off_t offset = reg; |
| 782 | int rc; |
| 783 | |
| 784 | rc = pread(astlpc->kcs_fd, val, 1, offset); |
| 785 | |
| 786 | return rc == 1 ? 0 : -1; |
| 787 | } |
| 788 | |
| 789 | static int __mctp_astlpc_fileio_kcs_write(void *arg, |
| 790 | enum mctp_binding_astlpc_kcs_reg reg, uint8_t val) |
| 791 | { |
| 792 | struct mctp_binding_astlpc *astlpc = arg; |
| 793 | off_t offset = reg; |
| 794 | int rc; |
| 795 | |
| 796 | rc = pwrite(astlpc->kcs_fd, &val, 1, offset); |
| 797 | |
| 798 | return rc == 1 ? 0 : -1; |
| 799 | } |
| 800 | |
| 801 | int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc) |
| 802 | { |
| 803 | return astlpc->kcs_fd; |
| 804 | } |
| 805 | |
| 806 | struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 807 | { |
| 808 | struct mctp_binding_astlpc *astlpc; |
| 809 | int rc; |
| 810 | |
Andrew Jeffery | 7cd72f1 | 2020-05-12 20:27:59 +0930 | [diff] [blame] | 811 | /* |
| 812 | * If we're doing file IO then we're very likely not running |
| 813 | * freestanding, so lets assume that we're on the BMC side |
| 814 | */ |
| 815 | astlpc = __mctp_astlpc_init(MCTP_BINDING_ASTLPC_MODE_BMC, MCTP_BTU); |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 816 | if (!astlpc) |
| 817 | return NULL; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 818 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 819 | /* Set internal operations for kcs. We use direct accesses to the lpc |
| 820 | * map area */ |
| 821 | astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read; |
| 822 | astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write; |
| 823 | astlpc->ops_data = astlpc; |
| 824 | |
| 825 | rc = mctp_astlpc_init_fileio_lpc(astlpc); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 826 | if (rc) { |
| 827 | free(astlpc); |
| 828 | return NULL; |
| 829 | } |
| 830 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 831 | rc = mctp_astlpc_init_fileio_kcs(astlpc); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 832 | if (rc) { |
| 833 | free(astlpc); |
| 834 | return NULL; |
| 835 | } |
| 836 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 837 | return astlpc; |
| 838 | } |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 839 | #else |
| 840 | struct mctp_binding_astlpc * __attribute__((const)) |
| 841 | mctp_astlpc_init_fileio(void) |
| 842 | { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 843 | astlpc_prerr(astlpc, "Missing support for file IO"); |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 844 | return NULL; |
| 845 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 846 | |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 847 | int __attribute__((const)) mctp_astlpc_get_fd( |
| 848 | struct mctp_binding_astlpc *astlpc __attribute__((unused))) |
| 849 | { |
Andrew Jeffery | 9101a2a | 2020-05-22 16:08:03 +0930 | [diff] [blame] | 850 | astlpc_prerr(astlpc, "Missing support for file IO"); |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 851 | return -1; |
| 852 | } |
| 853 | #endif |