blob: 085cb6d83b4c29b96b19703557d6750bd8585ecc [file] [log] [blame]
Jeremy Kerr3d36ee22019-05-30 11:15:37 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Jeremy Kerr672c8852019-03-01 12:18:07 +08002
3#include <assert.h>
4#include <fcntl.h>
5#include <stdbool.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9
10#include <sys/ioctl.h>
11#include <sys/mman.h>
12
13#include <linux/aspeed-lpc-ctrl.h>
14
15#define pr_fmt(x) "astlpc: " x
16
17#include "libmctp.h"
18#include "libmctp-alloc.h"
19#include "libmctp-log.h"
20#include "libmctp-astlpc.h"
21
22struct mctp_binding_astlpc {
23 struct mctp_binding binding;
24 void *lpc_map_base;
25 union {
26 void *lpc_map;
27 struct mctp_lpcmap_hdr *lpc_hdr;
28 };
29 int kcs_fd;
30 uint8_t kcs_status;
31
32 bool running;
33
34 /* temporary transmit buffer */
35 uint8_t txbuf[256];
36};
37
38#ifndef container_of
39#define container_of(ptr, type, member) \
40 (type *)((char *)(ptr) - (char *)&((type *)0)->member)
41#endif
42
43#define binding_to_astlpc(b) \
44 container_of(b, struct mctp_binding_astlpc, binding)
45
46#define MCTP_MAGIC 0x4d435450
47#define BMC_VER_MIN 1
48#define BMC_VER_CUR 1
49
50struct mctp_lpcmap_hdr {
51 uint32_t magic;
52
53 uint16_t bmc_ver_min;
54 uint16_t bmc_ver_cur;
55 uint16_t host_ver_min;
56 uint16_t host_ver_cur;
57 uint16_t negotiated_ver;
58 uint16_t pad0;
59
60 uint32_t rx_offset;
61 uint32_t rx_size;
62 uint32_t tx_offset;
63 uint32_t tx_size;
64} __attribute__((packed));
65
66/* layout of TX/RX areas */
67static const uint32_t rx_offset = 0x100;
68static const uint32_t rx_size = 0x100;
69static const uint32_t tx_offset = 0x200;
70static const uint32_t tx_size = 0x100;
71
72/* kernel interface */
73static const char *kcs_path = "/dev/mctp0";
74static const char *lpc_path = "/dev/aspeed-lpc-ctrl";
75
76#define LPC_WIN_SIZE (1 * 1024 * 1024)
77
78enum {
79 KCS_REG_DATA = 0,
80 KCS_REG_STATUS = 1,
81};
82
83#define KCS_STATUS_BMC_READY 0x80
84#define KCS_STATUS_CHANNEL_ACTIVE 0x40
85#define KCS_STATUS_IBF 0x02
86#define KCS_STATUS_OBF 0x01
87
88static int mctp_astlpc_kcs_set_status(struct mctp_binding_astlpc *astlpc,
89 uint8_t status)
90{
91 int rc;
92
93 rc = pwrite(astlpc->kcs_fd, &status, 1, KCS_REG_STATUS);
94 if (rc != 1) {
95 mctp_prwarn("KCS status write failed");
96 return -1;
97 }
98 return 0;
99}
100
101static int mctp_astlpc_kcs_send(struct mctp_binding_astlpc *astlpc,
102 uint8_t data)
103{
104 uint8_t status;
105 int rc;
106
107 for (;;) {
108 rc = pread(astlpc->kcs_fd, &status, 1, KCS_REG_STATUS);
109 if (rc != 1) {
110 mctp_prwarn("KCE status read failed");
111 return -1;
112 }
113 if (!(status & KCS_STATUS_OBF))
114 break;
115 /* todo: timeout */
116 }
117
118 rc = pwrite(astlpc->kcs_fd, &data, 1, KCS_REG_DATA);
119 if (rc != 1) {
120 mctp_prwarn("KCS data write failed");
121 return -1;
122 }
123
124 return 0;
125}
126
127static int mctp_binding_astlpc_tx(struct mctp_binding *b,
128 struct mctp_pktbuf *pkt)
129{
130 struct mctp_binding_astlpc *astlpc = binding_to_astlpc(b);
131 uint32_t len;
132
133 len = mctp_pktbuf_size(pkt);
134 if (len > rx_size - 4) {
135 mctp_prwarn("invalid TX len 0x%x", len);
136 return -1;
137 }
138
139 *(uint32_t *)(astlpc->lpc_map + rx_offset) = htobe32(len);
140
141 memcpy(astlpc->lpc_map + rx_offset + 4, mctp_pktbuf_hdr(pkt), len);
142
143 mctp_binding_set_tx_enabled(b, false);
144
145 mctp_astlpc_kcs_send(astlpc, 0x1);
146 return 0;
147}
148
149static void mctp_astlpc_init_channel(struct mctp_binding_astlpc *astlpc)
150{
151 /* todo: actual version negotiation */
152 astlpc->lpc_hdr->negotiated_ver = htobe16(1);
153 mctp_astlpc_kcs_set_status(astlpc,
154 KCS_STATUS_BMC_READY | KCS_STATUS_CHANNEL_ACTIVE |
155 KCS_STATUS_OBF);
156
157 mctp_binding_set_tx_enabled(&astlpc->binding, true);
158}
159
160static void mctp_astlpc_rx_start(struct mctp_binding_astlpc *astlpc)
161{
162 struct mctp_pktbuf *pkt;
163 uint32_t len;
164
165 len = htobe32(*(uint32_t *)(astlpc->lpc_map + tx_offset));
166 if (len > tx_size - 4) {
167 mctp_prwarn("invalid RX len 0x%x", len);
168 return;
169 }
170
171 if (len > MCTP_MTU + sizeof(struct mctp_hdr)) {
172 mctp_prwarn("invalid RX len 0x%x", len);
173 return;
174 }
175
176 pkt = mctp_pktbuf_alloc(len);
177 if (!pkt)
178 goto out_complete;
179
180 memcpy(mctp_pktbuf_hdr(pkt), astlpc->lpc_map + tx_offset + 4, len);
181
182 mctp_bus_rx(&astlpc->binding, pkt);
183
184out_complete:
185 mctp_astlpc_kcs_send(astlpc, 0x2);
186}
187
188static void mctp_astlpc_tx_complete(struct mctp_binding_astlpc *astlpc)
189{
190 mctp_binding_set_tx_enabled(&astlpc->binding, true);
191}
192
193int mctp_astlpc_poll(struct mctp_binding_astlpc *astlpc)
194{
195 uint8_t kcs_regs[2], data;
196 int rc;
197
198 rc = pread(astlpc->kcs_fd, kcs_regs, 2, 0);
199 if (rc < 0) {
200 mctp_prwarn("KCS read error");
201 return -1;
202 } else if (rc != 2) {
203 mctp_prwarn("KCS short read (%d)", rc);
204 return -1;
205 }
206
207 if (!(kcs_regs[KCS_REG_STATUS] & KCS_STATUS_IBF))
208 return 0;
209
210 data = kcs_regs[KCS_REG_DATA];
211 switch (data) {
212 case 0x0:
213 mctp_astlpc_init_channel(astlpc);
214 break;
215 case 0x1:
216 mctp_astlpc_rx_start(astlpc);
217 break;
218 case 0x2:
219 mctp_astlpc_tx_complete(astlpc);
220 break;
221 default:
222 mctp_prwarn("unknown message 0x%x", data);
223 }
224 return 0;
225}
226
227int mctp_astlpc_get_fd(struct mctp_binding_astlpc *astlpc)
228{
229 return astlpc->kcs_fd;
230}
231
232
233void mctp_astlpc_register_bus(struct mctp_binding_astlpc *astlpc,
234 struct mctp *mctp, mctp_eid_t eid)
235{
236 mctp_register_bus(mctp, &astlpc->binding, eid);
237}
238
239static int mctp_astlpc_init_bmc(struct mctp_binding_astlpc *astlpc)
240{
241 uint8_t status;
242 int rc;
243
244 astlpc->lpc_hdr->magic = htobe32(MCTP_MAGIC);
245 astlpc->lpc_hdr->bmc_ver_min = htobe16(BMC_VER_MIN);
246 astlpc->lpc_hdr->bmc_ver_cur = htobe16(BMC_VER_CUR);
247
248 astlpc->lpc_hdr->rx_offset = htobe32(rx_offset);
249 astlpc->lpc_hdr->rx_size = htobe32(rx_size);
250 astlpc->lpc_hdr->tx_offset = htobe32(tx_offset);
251 astlpc->lpc_hdr->tx_size = htobe32(tx_size);
252
253 /* set status indicating that the BMC is now active */
254 status = KCS_STATUS_BMC_READY | KCS_STATUS_OBF;
255 rc = pwrite(astlpc->kcs_fd, &status, 1, KCS_REG_STATUS);
256 if (rc != 1) {
257 mctp_prwarn("KCS write failed");
258 rc = -1;
259 } else {
260 rc = 0;
261 }
262
263 return rc;
264}
265
266static int mctp_astlpc_init_lpc(struct mctp_binding_astlpc *astlpc)
267{
268 struct aspeed_lpc_ctrl_mapping map = {
269 .window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY,
270 .window_id = 0, /* There's only one */
271 .flags = 0,
272 .addr = 0,
273 .offset = 0,
274 .size = 0
275 };
276 int fd, rc;
277
278 fd = open(lpc_path, O_RDWR | O_SYNC);
279 if (fd < 0) {
280 mctp_prwarn("LPC open (%s) failed", lpc_path);
281 return -1;
282 }
283
284 rc = ioctl(fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, &map);
285 if (rc) {
286 mctp_prwarn("LPC GET_SIZE failed");
287 close(fd);
288 return -1;
289 }
290
291 astlpc->lpc_map_base = mmap(NULL, map.size, PROT_READ | PROT_WRITE,
292 MAP_SHARED, fd, 0);
293 if (astlpc->lpc_map_base == MAP_FAILED) {
294 mctp_prwarn("LPC mmap failed");
295 rc = -1;
296 } else {
297 astlpc->lpc_map = astlpc->lpc_map_base +
298 map.size - LPC_WIN_SIZE;
299 }
300
301 close(fd);
302
303 return rc;
304}
305
306static int mctp_astlpc_init_kcs(struct mctp_binding_astlpc *astlpc)
307{
308 astlpc->kcs_fd = open(kcs_path, O_RDWR);
309 if (astlpc->kcs_fd < 0)
310 return -1;
311
312 return 0;
313}
314
315struct mctp_binding_astlpc *mctp_astlpc_init(void)
316{
317 struct mctp_binding_astlpc *astlpc;
318 int rc;
319
320 astlpc = __mctp_alloc(sizeof(*astlpc));
321 memset(astlpc, 0, sizeof(*astlpc));
322 astlpc->binding.name = "astlpc";
323 astlpc->binding.version = 1;
324 astlpc->binding.tx = mctp_binding_astlpc_tx;
325
326 rc = mctp_astlpc_init_lpc(astlpc);
327 if (rc) {
328 free(astlpc);
329 return NULL;
330 }
331
332 rc = mctp_astlpc_init_kcs(astlpc);
333 if (rc) {
334 free(astlpc);
335 return NULL;
336 }
337
338 rc = mctp_astlpc_init_bmc(astlpc);
339 if (rc) {
340 free(astlpc);
341 return NULL;
342 }
343
344 return astlpc;
345}
346