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 | |
| 3 | #include <assert.h> |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 4 | #include <endian.h> |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 5 | #include <stdbool.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 8 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 9 | #define pr_fmt(x) "astlpc: " x |
| 10 | |
| 11 | #include "libmctp.h" |
| 12 | #include "libmctp-alloc.h" |
| 13 | #include "libmctp-log.h" |
| 14 | #include "libmctp-astlpc.h" |
| 15 | |
Jeremy Kerr | b214c64 | 2019-11-27 11:34:00 +0800 | [diff] [blame] | 16 | #ifdef MCTP_HAVE_FILEIO |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 17 | |
Jeremy Kerr | c6f676d | 2019-12-19 09:24:06 +0800 | [diff] [blame^] | 18 | #include <unistd.h> |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <sys/ioctl.h> |
| 21 | #include <sys/mman.h> |
| 22 | #include <linux/aspeed-lpc-ctrl.h> |
| 23 | |
| 24 | /* kernel interface */ |
| 25 | static const char *kcs_path = "/dev/mctp0"; |
| 26 | static const char *lpc_path = "/dev/aspeed-lpc-ctrl"; |
| 27 | |
| 28 | #endif |
| 29 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 30 | struct mctp_binding_astlpc { |
| 31 | struct mctp_binding binding; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 32 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 33 | union { |
| 34 | void *lpc_map; |
| 35 | struct mctp_lpcmap_hdr *lpc_hdr; |
| 36 | }; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 37 | |
| 38 | /* direct ops data */ |
| 39 | struct mctp_binding_astlpc_ops ops; |
| 40 | void *ops_data; |
| 41 | struct mctp_lpcmap_hdr *priv_hdr; |
| 42 | |
| 43 | /* fileio ops data */ |
| 44 | void *lpc_map_base; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 45 | int kcs_fd; |
| 46 | uint8_t kcs_status; |
| 47 | |
| 48 | bool running; |
| 49 | |
| 50 | /* temporary transmit buffer */ |
| 51 | uint8_t txbuf[256]; |
| 52 | }; |
| 53 | |
| 54 | #ifndef container_of |
| 55 | #define container_of(ptr, type, member) \ |
| 56 | (type *)((char *)(ptr) - (char *)&((type *)0)->member) |
| 57 | #endif |
| 58 | |
| 59 | #define binding_to_astlpc(b) \ |
| 60 | container_of(b, struct mctp_binding_astlpc, binding) |
| 61 | |
| 62 | #define MCTP_MAGIC 0x4d435450 |
| 63 | #define BMC_VER_MIN 1 |
| 64 | #define BMC_VER_CUR 1 |
| 65 | |
| 66 | struct mctp_lpcmap_hdr { |
| 67 | uint32_t magic; |
| 68 | |
| 69 | uint16_t bmc_ver_min; |
| 70 | uint16_t bmc_ver_cur; |
| 71 | uint16_t host_ver_min; |
| 72 | uint16_t host_ver_cur; |
| 73 | uint16_t negotiated_ver; |
| 74 | uint16_t pad0; |
| 75 | |
| 76 | uint32_t rx_offset; |
| 77 | uint32_t rx_size; |
| 78 | uint32_t tx_offset; |
| 79 | uint32_t tx_size; |
| 80 | } __attribute__((packed)); |
| 81 | |
| 82 | /* layout of TX/RX areas */ |
| 83 | static const uint32_t rx_offset = 0x100; |
| 84 | static const uint32_t rx_size = 0x100; |
| 85 | static const uint32_t tx_offset = 0x200; |
| 86 | static const uint32_t tx_size = 0x100; |
| 87 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 88 | #define LPC_WIN_SIZE (1 * 1024 * 1024) |
| 89 | |
| 90 | enum { |
| 91 | KCS_REG_DATA = 0, |
| 92 | KCS_REG_STATUS = 1, |
| 93 | }; |
| 94 | |
| 95 | #define KCS_STATUS_BMC_READY 0x80 |
| 96 | #define KCS_STATUS_CHANNEL_ACTIVE 0x40 |
| 97 | #define KCS_STATUS_IBF 0x02 |
| 98 | #define KCS_STATUS_OBF 0x01 |
| 99 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 100 | static bool lpc_direct(struct mctp_binding_astlpc *astlpc) |
| 101 | { |
| 102 | return astlpc->lpc_map != NULL; |
| 103 | } |
| 104 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 105 | static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc, |
| 106 | uint8_t status) |
| 107 | { |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 108 | uint8_t data; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 109 | int rc; |
| 110 | |
Jeremy Kerr | 1a4b55a | 2019-06-24 14:22:39 +0800 | [diff] [blame] | 111 | /* Since we're setting the status register, we want the other endpoint |
| 112 | * to be interrupted. However, some hardware may only raise a host-side |
| 113 | * interrupt on an ODR event. |
| 114 | * So, write a dummy value of 0xff to ODR, which will ensure that an |
| 115 | * interrupt is triggered, and can be ignored by the host. |
| 116 | */ |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 117 | data = 0xff; |
| 118 | status |= KCS_STATUS_OBF; |
Jeremy Kerr | 1a4b55a | 2019-06-24 14:22:39 +0800 | [diff] [blame] | 119 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 120 | rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA, |
| 121 | data); |
| 122 | if (rc) { |
| 123 | mctp_prwarn("KCS dummy data write failed"); |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_STATUS, |
| 129 | status); |
| 130 | if (rc) { |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 131 | mctp_prwarn("KCS status write failed"); |
| 132 | return -1; |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc, |
| 138 | uint8_t data) |
| 139 | { |
| 140 | uint8_t status; |
| 141 | int rc; |
| 142 | |
| 143 | for (;;) { |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 144 | rc = astlpc->ops.kcs_read(astlpc->ops_data, |
| 145 | MCTP_ASTLPC_KCS_REG_STATUS, &status); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 146 | if (rc != 1) { |
| 147 | mctp_prwarn("KCE status read failed"); |
| 148 | return -1; |
| 149 | } |
| 150 | if (!(status & KCS_STATUS_OBF)) |
| 151 | break; |
| 152 | /* todo: timeout */ |
| 153 | } |
| 154 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 155 | rc = astlpc->ops.kcs_write(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA, |
| 156 | data); |
| 157 | if (rc) { |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 158 | mctp_prwarn("KCS data write failed"); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int mctp_binding_astlpc_tx(struct mctp_binding *b, |
| 166 | struct mctp_pktbuf *pkt) |
| 167 | { |
| 168 | struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b); |
| 169 | uint32_t len; |
| 170 | |
| 171 | len = mctp_pktbuf_size(pkt); |
| 172 | if (len > rx_size - 4) { |
| 173 | mctp_prwarn("invalid TX len 0x%x", len); |
| 174 | return -1; |
| 175 | } |
| 176 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 177 | if (lpc_direct(astlpc)) { |
| 178 | *(uint32_t *)(astlpc->lpc_map + rx_offset) = htobe32(len); |
| 179 | memcpy(astlpc->lpc_map + rx_offset + 4, mctp_pktbuf_hdr(pkt), |
| 180 | len); |
| 181 | } else { |
| 182 | uint32_t tmp = htobe32(len); |
| 183 | astlpc->ops.lpc_write(astlpc->ops_data, &tmp, rx_offset, |
| 184 | sizeof(tmp)); |
| 185 | astlpc->ops.lpc_write(astlpc->ops_data, mctp_pktbuf_hdr(pkt), |
| 186 | rx_offset + 4, len); |
| 187 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 188 | |
| 189 | mctp_binding_set_tx_enabled(b, false); |
| 190 | |
| 191 | mctp_astlpc_kcs_send(astlpc, 0x1); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc) |
| 196 | { |
| 197 | /* todo: actual version negotiation */ |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 198 | if (lpc_direct(astlpc)) { |
| 199 | astlpc->lpc_hdr->negotiated_ver = htobe16(1); |
| 200 | } else { |
| 201 | uint16_t ver = htobe16(1); |
| 202 | astlpc->ops.lpc_write(astlpc->ops_data, &ver, |
| 203 | offsetof(struct mctp_lpcmap_hdr, |
| 204 | negotiated_ver), |
| 205 | sizeof(ver)); |
| 206 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 207 | mctp_astlpc_kcs_set_status(astlpc, |
| 208 | KCS_STATUS_BMC_READY | KCS_STATUS_CHANNEL_ACTIVE | |
| 209 | KCS_STATUS_OBF); |
| 210 | |
| 211 | mctp_binding_set_tx_enabled(&astlpc->binding, true); |
| 212 | } |
| 213 | |
| 214 | static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc) |
| 215 | { |
| 216 | struct mctp_pktbuf *pkt; |
| 217 | uint32_t len; |
| 218 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 219 | if (lpc_direct(astlpc)) { |
| 220 | len = *(uint32_t *)(astlpc->lpc_map + tx_offset); |
| 221 | } else { |
| 222 | astlpc->ops.lpc_read(astlpc->ops_data, &len, |
| 223 | tx_offset, sizeof(len)); |
| 224 | } |
| 225 | len = be32toh(len); |
| 226 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 227 | if (len > tx_size - 4) { |
| 228 | mctp_prwarn("invalid RX len 0x%x", len); |
| 229 | return; |
| 230 | } |
| 231 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 232 | if (len > astlpc->binding.pkt_size) { |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 233 | mctp_prwarn("invalid RX len 0x%x", len); |
| 234 | return; |
| 235 | } |
| 236 | |
Jeremy Kerr | df15f7e | 2019-08-05 15:41:19 +0800 | [diff] [blame] | 237 | pkt = mctp_pktbuf_alloc(&astlpc->binding, len); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 238 | if (!pkt) |
| 239 | goto out_complete; |
| 240 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 241 | if (lpc_direct(astlpc)) { |
| 242 | memcpy(mctp_pktbuf_hdr(pkt), |
| 243 | astlpc->lpc_map + tx_offset + 4, len); |
| 244 | } else { |
| 245 | astlpc->ops.lpc_read(astlpc->ops_data, mctp_pktbuf_hdr(pkt), |
| 246 | tx_offset + 4, len); |
| 247 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 248 | |
| 249 | mctp_bus_rx(&astlpc->binding, pkt); |
| 250 | |
| 251 | out_complete: |
| 252 | mctp_astlpc_kcs_send(astlpc, 0x2); |
| 253 | } |
| 254 | |
| 255 | static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc) |
| 256 | { |
| 257 | mctp_binding_set_tx_enabled(&astlpc->binding, true); |
| 258 | } |
| 259 | |
| 260 | int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc) |
| 261 | { |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 262 | uint8_t status, data; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 263 | int rc; |
| 264 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 265 | rc = astlpc->ops.kcs_read(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_STATUS, |
| 266 | &status); |
| 267 | if (rc) { |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 268 | mctp_prwarn("KCS read error"); |
| 269 | return -1; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 270 | } |
| 271 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 272 | if (!(status & KCS_STATUS_IBF)) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 273 | return 0; |
| 274 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 275 | rc = astlpc->ops.kcs_read(astlpc->ops_data, MCTP_ASTLPC_KCS_REG_DATA, |
| 276 | &data); |
| 277 | if (rc) { |
| 278 | mctp_prwarn("KCS data read error"); |
| 279 | return -1; |
| 280 | } |
| 281 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 282 | switch (data) { |
| 283 | case 0x0: |
| 284 | mctp_astlpc_init_channel(astlpc); |
| 285 | break; |
| 286 | case 0x1: |
| 287 | mctp_astlpc_rx_start(astlpc); |
| 288 | break; |
| 289 | case 0x2: |
| 290 | mctp_astlpc_tx_complete(astlpc); |
| 291 | break; |
Jeremy Kerr | 1a4b55a | 2019-06-24 14:22:39 +0800 | [diff] [blame] | 292 | case 0xff: |
| 293 | /* reserved value for dummy data writes; do nothing */ |
| 294 | break; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 295 | default: |
| 296 | mctp_prwarn("unknown message 0x%x", data); |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 301 | static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc) |
| 302 | { |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 303 | struct mctp_lpcmap_hdr *hdr; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 304 | uint8_t status; |
| 305 | int rc; |
| 306 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 307 | if (lpc_direct(astlpc)) |
| 308 | hdr = astlpc->lpc_hdr; |
| 309 | else |
| 310 | hdr = astlpc->priv_hdr; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 311 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 312 | hdr->magic = htobe32(MCTP_MAGIC); |
| 313 | hdr->bmc_ver_min = htobe16(BMC_VER_MIN); |
| 314 | hdr->bmc_ver_cur = htobe16(BMC_VER_CUR); |
| 315 | |
| 316 | hdr->rx_offset = htobe32(rx_offset); |
| 317 | hdr->rx_size = htobe32(rx_size); |
| 318 | hdr->tx_offset = htobe32(tx_offset); |
| 319 | hdr->tx_size = htobe32(tx_size); |
| 320 | |
| 321 | if (!lpc_direct(astlpc)) |
| 322 | astlpc->ops.lpc_write(astlpc->ops_data, hdr, 0, sizeof(*hdr)); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 323 | |
| 324 | /* set status indicating that the BMC is now active */ |
| 325 | status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 326 | rc = astlpc->ops.kcs_write(astlpc->ops_data, |
| 327 | MCTP_ASTLPC_KCS_REG_STATUS, status); |
| 328 | if (rc) { |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 329 | mctp_prwarn("KCS write failed"); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | return rc; |
| 333 | } |
| 334 | |
Jeremy Kerr | 8081beb | 2019-09-04 12:33:00 +0800 | [diff] [blame] | 335 | static int mctp_binding_astlpc_start(struct mctp_binding *b) |
| 336 | { |
| 337 | struct mctp_binding_astlpc *astlpc = container_of(b, |
| 338 | struct mctp_binding_astlpc, binding); |
| 339 | |
| 340 | return mctp_astlpc_init_bmc(astlpc); |
| 341 | } |
| 342 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 343 | /* allocate and basic initialisation */ |
| 344 | static struct mctp_binding_astlpc *__mctp_astlpc_init(void) |
| 345 | { |
| 346 | struct mctp_binding_astlpc *astlpc; |
| 347 | |
| 348 | astlpc = __mctp_alloc(sizeof(*astlpc)); |
| 349 | memset(astlpc, 0, sizeof(*astlpc)); |
| 350 | astlpc->binding.name = "astlpc"; |
| 351 | astlpc->binding.version = 1; |
| 352 | astlpc->binding.tx = mctp_binding_astlpc_tx; |
Jeremy Kerr | 8081beb | 2019-09-04 12:33:00 +0800 | [diff] [blame] | 353 | astlpc->binding.start = mctp_binding_astlpc_start; |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 354 | astlpc->binding.pkt_size = MCTP_BMTU; |
| 355 | astlpc->binding.pkt_pad = 0; |
| 356 | astlpc->lpc_map = NULL; |
| 357 | |
| 358 | return astlpc; |
| 359 | } |
| 360 | |
Jeremy Kerr | 3b36d17 | 2019-09-04 11:56:09 +0800 | [diff] [blame] | 361 | struct mctp_binding *mctp_binding_astlpc_core(struct mctp_binding_astlpc *b) |
| 362 | { |
| 363 | return &b->binding; |
| 364 | } |
| 365 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 366 | struct mctp_binding_astlpc *mctp_astlpc_init_ops( |
| 367 | struct mctp_binding_astlpc_ops *ops, |
| 368 | void *ops_data, void *lpc_map) |
| 369 | { |
| 370 | struct mctp_binding_astlpc *astlpc; |
| 371 | int rc; |
| 372 | |
| 373 | astlpc = __mctp_astlpc_init(); |
| 374 | if (!astlpc) |
| 375 | return NULL; |
| 376 | |
| 377 | memcpy(&astlpc->ops, ops, sizeof(astlpc->ops)); |
| 378 | astlpc->ops_data = ops_data; |
| 379 | astlpc->lpc_map = lpc_map; |
| 380 | |
| 381 | /* In indirect mode, we keep a separate buffer of header data. |
| 382 | * We need to sync this through the lpc_read/lpc_write ops. |
| 383 | */ |
| 384 | if (!astlpc->lpc_map) |
| 385 | astlpc->priv_hdr = __mctp_alloc(sizeof(*astlpc->priv_hdr)); |
| 386 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 387 | return astlpc; |
| 388 | } |
| 389 | |
Jeremy Kerr | b214c64 | 2019-11-27 11:34:00 +0800 | [diff] [blame] | 390 | #ifdef MCTP_HAVE_FILEIO |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 391 | static int mctp_astlpc_init_fileio_lpc(struct mctp_binding_astlpc *astlpc) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 392 | { |
| 393 | struct aspeed_lpc_ctrl_mapping map = { |
| 394 | .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY, |
| 395 | .window_id = 0, /* There's only one */ |
| 396 | .flags = 0, |
| 397 | .addr = 0, |
| 398 | .offset = 0, |
| 399 | .size = 0 |
| 400 | }; |
| 401 | int fd, rc; |
| 402 | |
| 403 | fd = open(lpc_path, O_RDWR | O_SYNC); |
| 404 | if (fd < 0) { |
| 405 | mctp_prwarn("LPC open (%s) failed", lpc_path); |
| 406 | return -1; |
| 407 | } |
| 408 | |
| 409 | rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map); |
| 410 | if (rc) { |
| 411 | mctp_prwarn("LPC GET_SIZE failed"); |
| 412 | close(fd); |
| 413 | return -1; |
| 414 | } |
| 415 | |
| 416 | astlpc->lpc_map_base = mmap(NULL, map.size, PROT_READ | PROT_WRITE, |
| 417 | MAP_SHARED, fd, 0); |
| 418 | if (astlpc->lpc_map_base == MAP_FAILED) { |
| 419 | mctp_prwarn("LPC mmap failed"); |
| 420 | rc = -1; |
| 421 | } else { |
| 422 | astlpc->lpc_map = astlpc->lpc_map_base + |
| 423 | map.size - LPC_WIN_SIZE; |
| 424 | } |
| 425 | |
| 426 | close(fd); |
| 427 | |
| 428 | return rc; |
| 429 | } |
| 430 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 431 | static int mctp_astlpc_init_fileio_kcs(struct mctp_binding_astlpc *astlpc) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 432 | { |
| 433 | astlpc->kcs_fd = open(kcs_path, O_RDWR); |
| 434 | if (astlpc->kcs_fd < 0) |
| 435 | return -1; |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 440 | static int __mctp_astlpc_fileio_kcs_read(void *arg, |
| 441 | enum mctp_binding_astlpc_kcs_reg reg, uint8_t *val) |
| 442 | { |
| 443 | struct mctp_binding_astlpc *astlpc = arg; |
| 444 | off_t offset = reg; |
| 445 | int rc; |
| 446 | |
| 447 | rc = pread(astlpc->kcs_fd, val, 1, offset); |
| 448 | |
| 449 | return rc == 1 ? 0 : -1; |
| 450 | } |
| 451 | |
| 452 | static int __mctp_astlpc_fileio_kcs_write(void *arg, |
| 453 | enum mctp_binding_astlpc_kcs_reg reg, uint8_t val) |
| 454 | { |
| 455 | struct mctp_binding_astlpc *astlpc = arg; |
| 456 | off_t offset = reg; |
| 457 | int rc; |
| 458 | |
| 459 | rc = pwrite(astlpc->kcs_fd, &val, 1, offset); |
| 460 | |
| 461 | return rc == 1 ? 0 : -1; |
| 462 | } |
| 463 | |
| 464 | int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc) |
| 465 | { |
| 466 | return astlpc->kcs_fd; |
| 467 | } |
| 468 | |
| 469 | struct mctp_binding_astlpc *mctp_astlpc_init_fileio(void) |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 470 | { |
| 471 | struct mctp_binding_astlpc *astlpc; |
| 472 | int rc; |
| 473 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 474 | astlpc = __mctp_astlpc_init(); |
| 475 | if (!astlpc) |
| 476 | return NULL; |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 477 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 478 | /* Set internal operations for kcs. We use direct accesses to the lpc |
| 479 | * map area */ |
| 480 | astlpc->ops.kcs_read = __mctp_astlpc_fileio_kcs_read; |
| 481 | astlpc->ops.kcs_write = __mctp_astlpc_fileio_kcs_write; |
| 482 | astlpc->ops_data = astlpc; |
| 483 | |
| 484 | rc = mctp_astlpc_init_fileio_lpc(astlpc); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 485 | if (rc) { |
| 486 | free(astlpc); |
| 487 | return NULL; |
| 488 | } |
| 489 | |
Jeremy Kerr | bc53d35 | 2019-08-28 14:26:14 +0530 | [diff] [blame] | 490 | rc = mctp_astlpc_init_fileio_kcs(astlpc); |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 491 | if (rc) { |
| 492 | free(astlpc); |
| 493 | return NULL; |
| 494 | } |
| 495 | |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 496 | return astlpc; |
| 497 | } |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 498 | #else |
| 499 | struct mctp_binding_astlpc * __attribute__((const)) |
| 500 | mctp_astlpc_init_fileio(void) |
| 501 | { |
| 502 | return NULL; |
| 503 | } |
Jeremy Kerr | 672c885 | 2019-03-01 12:18:07 +0800 | [diff] [blame] | 504 | |
Jeremy Kerr | 92a10a6 | 2019-08-28 16:55:54 +0530 | [diff] [blame] | 505 | int __attribute__((const)) mctp_astlpc_get_fd( |
| 506 | struct mctp_binding_astlpc *astlpc __attribute__((unused))) |
| 507 | { |
| 508 | return -1; |
| 509 | } |
| 510 | #endif |