This repository has been archived by the owner on Sep 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
astpcie.c
414 lines (325 loc) · 9.41 KB
/
astpcie.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/aspeed-mctp.h>
#include "container_of.h"
#include "libmctp-alloc.h"
#include "libmctp-astpcie.h"
#include "libmctp-log.h"
#include "astpcie.h"
#undef pr_fmt
#define pr_fmt(fmt) "astpcie: " fmt
/*
* PCIe header template in "network format" - Big Endian
*/
static const struct mctp_pcie_hdr mctp_pcie_hdr_template_be = {
.fmt_type = MSG_4DW_HDR,
.mbz_attr_length = MCTP_PCIE_VDM_ATTR,
.code = MSG_CODE_VDM_TYPE_1,
.vendor = VENDOR_ID_DMTF_VDM
};
int mctp_astpcie_get_eid_info_ioctl(struct mctp_binding_astpcie *astpcie,
void *eid_info, uint16_t count,
uint8_t start_eid)
{
struct aspeed_mctp_get_eid_info get_eid_info;
int rc;
get_eid_info.count = count;
get_eid_info.start_eid = start_eid;
get_eid_info.ptr = (uintptr_t)eid_info;
rc = ioctl(astpcie->fd, ASPEED_MCTP_IOCTL_GET_EID_INFO, &get_eid_info);
if (!rc) {
uintptr_t ptr = (uintptr_t)get_eid_info.ptr;
memcpy(eid_info, (void *)ptr, get_eid_info.count);
}
return rc;
}
int mctp_astpcie_set_eid_info_ioctl(struct mctp_binding_astpcie *astpcie,
void *eid_info, uint16_t count)
{
struct aspeed_mctp_set_eid_info set_eid_info;
set_eid_info.count = count;
set_eid_info.ptr = (uintptr_t)eid_info;
return ioctl(astpcie->fd, ASPEED_MCTP_IOCTL_SET_EID_INFO,
&set_eid_info);
}
static int mctp_astpcie_get_bdf_ioctl(struct mctp_binding_astpcie *astpcie)
{
struct aspeed_mctp_get_bdf bdf;
int rc;
rc = ioctl(astpcie->fd, ASPEED_MCTP_IOCTL_GET_BDF, &bdf);
if (!rc)
astpcie->bdf = bdf.bdf;
return rc;
}
int mctp_astpcie_get_bdf(struct mctp_binding_astpcie *astpcie, uint16_t *bdf)
{
int rc;
rc = mctp_astpcie_get_bdf_ioctl(astpcie);
if (!rc)
*bdf = astpcie->bdf;
return rc;
}
static int
mctp_astpcie_get_medium_id_ioctl(struct mctp_binding_astpcie *astpcie)
{
struct aspeed_mctp_get_medium_id get_medium_id;
int rc;
rc = ioctl(astpcie->fd, ASPEED_MCTP_IOCTL_GET_MEDIUM_ID,
&get_medium_id);
if (!rc)
astpcie->medium_id = get_medium_id.medium_id;
return rc;
}
int mctp_astpcie_register_default_handler(struct mctp_binding_astpcie *astpcie)
{
return ioctl(astpcie->fd, ASPEED_MCTP_IOCTL_REGISTER_DEFAULT_HANDLER);
}
int mctp_astpcie_register_type_handler(struct mctp_binding_astpcie *astpcie,
uint8_t mctp_type,
uint16_t pci_vendor_id,
uint16_t vendor_type,
uint16_t vendor_type_mask)
{
struct aspeed_mctp_type_handler_ioctl type_handler;
type_handler.mctp_type = mctp_type;
type_handler.pci_vendor_id = pci_vendor_id;
type_handler.vendor_type = vendor_type;
type_handler.vendor_type_mask = vendor_type_mask;
return ioctl(astpcie->fd, ASPEED_MCTP_IOCTL_REGISTER_TYPE_HANDLER,
&type_handler);
}
int mctp_astpcie_unregister_type_handler(struct mctp_binding_astpcie *astpcie,
uint8_t mctp_type,
uint16_t pci_vendor_id,
uint16_t vendor_type,
uint16_t vendor_type_mask)
{
struct aspeed_mctp_type_handler_ioctl type_handler;
type_handler.mctp_type = mctp_type;
type_handler.pci_vendor_id = pci_vendor_id;
type_handler.vendor_type = vendor_type;
type_handler.vendor_type_mask = vendor_type_mask;
return ioctl(astpcie->fd, ASPEED_MCTP_IOCTL_UNREGISTER_TYPE_HANDLER,
&type_handler);
}
uint8_t mctp_astpcie_get_medium_id(struct mctp_binding_astpcie *astpcie)
{
return astpcie->medium_id;
}
static int mctp_astpcie_open(struct mctp_binding_astpcie *astpcie)
{
int fd = open(AST_DRV_FILE, O_RDWR);
if (fd < 0) {
mctp_prerr("Cannot open: %s, errno = %d", AST_DRV_FILE, errno);
return fd;
}
astpcie->fd = fd;
return 0;
}
static void mctp_astpcie_close(struct mctp_binding_astpcie *astpcie)
{
close(astpcie->fd);
astpcie->fd = -1;
}
/*
* Start function. Opens driver, read bdf and medium_id
*/
static int mctp_astpcie_start(struct mctp_binding *b)
{
struct mctp_binding_astpcie *astpcie = binding_to_astpcie(b);
int rc;
assert(astpcie);
rc = mctp_astpcie_open(astpcie);
if (rc)
return -errno;
rc = mctp_astpcie_get_bdf_ioctl(astpcie);
if (rc)
goto out_close;
rc = mctp_astpcie_get_medium_id_ioctl(astpcie);
if (rc)
goto out_close;
return 0;
out_close:
mctp_astpcie_close(astpcie);
return -errno;
}
static uint8_t mctp_astpcie_tx_get_pad_len(struct mctp_pktbuf *pkt)
{
size_t sz = mctp_pktbuf_size(pkt);
return PCIE_PKT_ALIGN(sz) - sz;
}
static uint16_t mctp_astpcie_tx_get_payload_size_dw(struct mctp_pktbuf *pkt)
{
size_t sz = mctp_pktbuf_size(pkt);
return PCIE_PKT_ALIGN(sz) / sizeof(uint32_t) - MCTP_HDR_SIZE_DW;
}
/*
* Tx function which writes single packet to device driver
*/
static int mctp_astpcie_tx(struct mctp_binding *b, struct mctp_pktbuf *pkt)
{
struct mctp_astpcie_pkt_private *pkt_prv =
(struct mctp_astpcie_pkt_private *)pkt->msg_binding_private;
struct mctp_binding_astpcie *astpcie = binding_to_astpcie(b);
struct mctp_pcie_hdr *hdr = (struct mctp_pcie_hdr *)pkt->data;
uint16_t payload_len_dw = mctp_astpcie_tx_get_payload_size_dw(pkt);
uint8_t pad = mctp_astpcie_tx_get_pad_len(pkt);
ssize_t write_len, len;
if (pkt_prv->remote_id == astpcie->bdf) {
mctp_prerr("Invalid Target ID (matches own BDF)");
return -1;
}
memcpy(hdr, &mctp_pcie_hdr_template_be, sizeof(*hdr));
mctp_prdebug("TX, len: %d, pad: %d", payload_len_dw, pad);
PCIE_SET_ROUTING(hdr, pkt_prv->routing);
PCIE_SET_DATA_LEN(hdr, payload_len_dw);
PCIE_SET_REQ_ID(hdr, astpcie->bdf);
PCIE_SET_TARGET_ID(hdr, pkt_prv->remote_id);
PCIE_SET_PAD_LEN(hdr, pad);
len = (payload_len_dw * sizeof(uint32_t)) +
ASPEED_MCTP_PCIE_VDM_HDR_SIZE;
mctp_trace_tx(pkt->data, len);
write_len = write(astpcie->fd, pkt->data, len);
if (write_len < 0) {
mctp_prerr("TX error");
return -1;
}
return 0;
}
static size_t mctp_astpcie_rx_get_payload_size(struct mctp_pcie_hdr *hdr)
{
size_t len_dw = PCIE_GET_DATA_LEN(hdr);
uint8_t pad = PCIE_GET_PAD_LEN(hdr);
/* According to PCIe Spec, 0 means 1024 DW */
if (len_dw == 0)
len_dw = PCIE_MAX_DATA_LEN_DW;
return len_dw * sizeof(uint32_t) - pad;
}
/*
* Simple poll implementation for use
*/
int mctp_astpcie_poll(struct mctp_binding_astpcie *astpcie, int timeout)
{
struct pollfd fds[1];
int rc;
fds[0].fd = astpcie->fd;
fds[0].events = POLLIN | POLLOUT;
rc = poll(fds, 1, timeout);
if (rc > 0)
return fds[0].revents;
if (rc < 0) {
mctp_prwarn("Poll returned error status (errno=%d)", errno);
return -1;
}
return 0;
}
static bool mctp_astpcie_is_routing_supported(int routing)
{
switch (routing) {
case PCIE_ROUTE_TO_RC:
case PCIE_ROUTE_BY_ID:
case PCIE_BROADCAST_FROM_RC:
return true;
default:
return false;
}
}
int mctp_astpcie_rx(struct mctp_binding_astpcie *astpcie)
{
uint32_t data[MCTP_ASTPCIE_BINDING_DEFAULT_BUFFER];
struct mctp_astpcie_pkt_private pkt_prv;
struct mctp_pktbuf *pkt;
struct mctp_pcie_hdr *hdr;
ssize_t read_len;
size_t payload_len;
int rc;
read_len = read(astpcie->fd, &data, sizeof(data));
if (read_len < 0) {
mctp_prerr("Reading RX data failed (errno = %d)", errno);
return -1;
}
mctp_trace_rx(&data, read_len);
if (read_len != ASTPCIE_PACKET_SIZE(MCTP_BTU)) {
mctp_prerr("Incorrect packet size: %zd", read_len);
return -1;
}
hdr = (struct mctp_pcie_hdr *)data;
payload_len = mctp_astpcie_rx_get_payload_size(hdr);
pkt_prv.routing = PCIE_GET_ROUTING(hdr);
if (!mctp_astpcie_is_routing_supported(pkt_prv.routing)) {
mctp_prerr("unsupported routing value: %d", pkt_prv.routing);
return -1;
}
pkt_prv.remote_id = PCIE_GET_REQ_ID(hdr);
pkt = mctp_pktbuf_alloc(&astpcie->binding, 0);
if (!pkt) {
mctp_prerr("pktbuf allocation failed");
return -1;
}
rc = mctp_pktbuf_push(pkt, data + PCIE_HDR_SIZE_DW,
payload_len + sizeof(struct mctp_hdr));
if (rc) {
mctp_prerr("Cannot push to pktbuf");
mctp_pktbuf_free(pkt);
return -1;
}
memcpy(pkt->msg_binding_private, &pkt_prv, sizeof(pkt_prv));
mctp_bus_rx(&astpcie->binding, pkt);
return 0;
}
/*
* Initializes PCIe binding structure
*/
struct mctp_binding_astpcie *mctp_astpcie_init(void)
{
struct mctp_binding_astpcie *astpcie;
astpcie = __mctp_alloc(sizeof(*astpcie));
if (!astpcie)
return NULL;
memset(astpcie, 0, sizeof(*astpcie));
astpcie->binding.name = "astpcie";
astpcie->binding.version = 1;
astpcie->binding.tx = mctp_astpcie_tx;
astpcie->binding.start = mctp_astpcie_start;
astpcie->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU);
assert(astpcie->binding.pkt_size - sizeof(struct mctp_hdr) <=
PCIE_MAX_DATA_LEN);
/* where mctp_hdr starts in in/out comming data
* note: there are two approaches: first (used here) that core
* allocates pktbuf to contain all binding metadata or this is handled
* other way by only by binding.
* This might change as smbus binding implements support for medium
* specific layer */
astpcie->binding.pkt_pad = sizeof(struct mctp_pcie_hdr);
astpcie->binding.pkt_priv_size =
sizeof(struct mctp_astpcie_pkt_private);
return astpcie;
}
/*
* Closes file descriptor and releases binding memory
*/
void mctp_astpcie_free(struct mctp_binding_astpcie *astpcie)
{
mctp_astpcie_close(astpcie);
__mctp_free(astpcie);
}
/*
* Returns generic binder handler from PCIe binding handler
*/
struct mctp_binding *mctp_astpcie_core(struct mctp_binding_astpcie *astpcie)
{
return &astpcie->binding;
}
int mctp_astpcie_get_fd(struct mctp_binding_astpcie *astpcie)
{
return astpcie->fd;
}