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