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