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
/
smbus.c
503 lines (417 loc) · 11.9 KB
/
smbus.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/* SPDX-License-Identifier: Apache-2.0 */
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/errno-base.h>
#ifdef MCTP_HAVE_FILEIO
#include <fcntl.h>
#endif
#define pr_fmt(x) "smbus: " x
#include <i2c/smbus.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <sys/ioctl.h>
#include "libmctp-alloc.h"
#include "libmctp-log.h"
#include "libmctp-smbus.h"
#include "libmctp.h"
#ifndef container_of
#define container_of(ptr, type, member) \
(type *)((char *)(ptr) - (char *)&((type *)0)->member)
#endif
#define binding_to_smbus(b) container_of(b, struct mctp_binding_smbus, binding)
#define MCTP_COMMAND_CODE 0x0F
#define MCTP_SLAVE_ADDR_INDEX 0
#define DEFAULT_SLAVE_ADDRESS 0x21
#define SMBUS_COMMAND_CODE_SIZE 1
#define SMBUS_LENGTH_FIELD_SIZE 1
#define SMBUS_ADDR_OFFSET_SLAVE 0x1000
#ifdef I2C_M_HOLD
static struct mctp_smbus_pkt_private active_mux_info = { .fd = -1,
.mux_hold_timeout = 0,
.mux_flags = 0,
.slave_addr = 0 };
static struct mctp_smbus_pkt_private reserve_mux_info = { .fd = -1,
.mux_hold_timeout = 0,
.mux_flags = 0,
.slave_addr = 0 };
#endif
struct mctp_smbus_header_tx {
uint8_t command_code;
uint8_t byte_count;
uint8_t source_slave_address;
};
struct mctp_smbus_header_rx {
uint8_t destination_slave_address;
uint8_t command_code;
uint8_t byte_count;
uint8_t source_slave_address;
};
static uint8_t crc8_calculate(uint16_t d)
{
const uint32_t poly_check = 0x1070 << 3;
int i;
for (i = 0; i < 8; i++) {
if (d & 0x8000) {
d = d ^ poly_check;
}
d = d << 1;
}
return (uint8_t)(d >> 8);
}
/* Incremental CRC8 over count bytes in the array pointed to by p */
static uint8_t pec_calculate(uint8_t crc, uint8_t *p, size_t count)
{
int i;
for (i = 0; i < count; i++) {
crc = crc8_calculate((crc ^ p[i]) << 8);
}
return crc;
}
static uint8_t calculate_pec_byte(uint8_t *buf, size_t len, uint8_t address)
{
uint8_t pec = pec_calculate(0, &address, 1);
pec = pec_calculate(pec, buf, len);
return pec;
}
#ifdef I2C_M_HOLD
static void cleanup_reserve_mux_info(void)
{
reserve_mux_info.fd = -1;
reserve_mux_info.mux_hold_timeout = 0;
reserve_mux_info.mux_flags = 0;
reserve_mux_info.slave_addr = 0;
}
static int smbus_model_mux(const uint16_t holdtimeout)
{
struct i2c_msg holdmsg = { 0, I2C_M_HOLD, sizeof(holdtimeout),
(uint8_t *)&holdtimeout };
struct i2c_rdwr_ioctl_data msgrdwr = { &holdmsg, 1 };
return ioctl(reserve_mux_info.fd, I2C_RDWR, &msgrdwr);
}
static int smbus_pull_model_hold_mux(void)
{
/*taking max hold time as 0xFFFF seconds*/
return smbus_model_mux(0xFFFF);
}
static int smbus_pull_model_unhold_mux(void)
{
return smbus_model_mux(0);
}
static bool pull_model_active;
#endif
int mctp_smbus_close_mux(const int fd, const int address)
{
#ifdef I2C_M_HOLD
uint8_t txbuf[2] = { 0 };
struct i2c_msg msg[1] = {
{ .addr = address, .flags = 0, .len = 2, .buf = txbuf }
};
struct i2c_rdwr_ioctl_data msgrdwr = { &msg[0], 1 };
return ioctl(fd, I2C_RDWR, &msgrdwr);
#else
return 0;
#endif
}
int mctp_smbus_init_pull_model(const struct mctp_smbus_pkt_private *prvt)
{
#ifdef I2C_M_HOLD
int rc = -1;
if (pull_model_active) {
mctp_prerr("%s: pull model is already active.", __func__);
return rc;
}
reserve_mux_info.fd = prvt->fd;
reserve_mux_info.mux_flags = prvt->mux_flags;
reserve_mux_info.slave_addr = prvt->slave_addr;
rc = smbus_pull_model_hold_mux();
if (rc < 0) {
cleanup_reserve_mux_info();
mctp_prerr(
"%s: Failed to hold the bus for device address: 0X%x",
__func__, prvt->slave_addr);
return rc;
}
pull_model_active = true;
return rc;
#else
return 0;
#endif
}
int mctp_smbus_exit_pull_model(const struct mctp_smbus_pkt_private *prvt)
{
#ifdef I2C_M_HOLD
int rc = -1;
if (!(pull_model_active &&
reserve_mux_info.slave_addr == prvt->slave_addr &&
reserve_mux_info.fd == prvt->fd)) {
mctp_prerr(
"%s: pull model is not active for device address: 0X%x.",
__func__, prvt->slave_addr);
return rc;
}
rc = smbus_pull_model_unhold_mux();
if (rc < 0) {
mctp_prerr(
"%s: Failed to unhold the bus for device address: 0X%x",
__func__, prvt->slave_addr);
return rc;
}
pull_model_active = false;
cleanup_reserve_mux_info();
return rc;
#else
return 0;
#endif
}
static int mctp_smbus_tx(struct mctp_binding_smbus *smbus, const uint8_t len,
struct mctp_smbus_pkt_private *pkt_pvt)
{
#ifdef I2C_M_HOLD
int rc;
if (pull_model_active) {
rc = smbus_pull_model_unhold_mux();
if (rc < 0) {
mctp_prerr("%s: Failed to unhold the bus.", __func__);
return rc;
}
}
if (pkt_pvt->mux_flags) {
uint16_t holdtimeout =
pkt_pvt->mux_hold_timeout; /*timeout in ms. */
struct i2c_msg msg[2] = { { .addr = pkt_pvt->slave_addr >>
1, /* seven bit address */
.flags = 0,
.len = len,
.buf = smbus->txbuf },
{ .addr = 0,
.flags = I2C_M_HOLD,
.len = sizeof(holdtimeout),
.buf = (uint8_t *)&holdtimeout } };
struct i2c_rdwr_ioctl_data msgrdwr = { &msg[0], 2 };
mctp_trace_tx(smbus->txbuf, len);
rc = ioctl(pkt_pvt->fd, I2C_RDWR, &msgrdwr);
/* Store active mux info */
active_mux_info.fd = pkt_pvt->fd;
active_mux_info.mux_flags = pkt_pvt->mux_flags;
active_mux_info.slave_addr = pkt_pvt->slave_addr;
return rc;
}
#endif
mctp_trace_tx(smbus->txbuf, len);
struct i2c_msg msg[1] = { { .addr = pkt_pvt->slave_addr >>
1, /* seven bit address */
.flags = 0,
.len = len,
.buf = smbus->txbuf } };
struct i2c_rdwr_ioctl_data msgrdwr = { &msg[0], 1 };
return ioctl(pkt_pvt->fd, I2C_RDWR, &msgrdwr);
}
#ifdef I2C_M_HOLD
static int mctp_smbus_unhold_bus(const uint8_t source_addr)
{
/* If we received a packet from a different slave, don't unhold mux */
if (active_mux_info.slave_addr != source_addr)
return 0;
/* Unhold message */
uint16_t holdtimeout = 0;
struct i2c_msg holdmsg = { 0, I2C_M_HOLD, sizeof(holdtimeout),
(uint8_t *)&holdtimeout };
struct i2c_rdwr_ioctl_data msgrdwr = { &holdmsg, 1 };
return ioctl(active_mux_info.fd, I2C_RDWR, &msgrdwr);
}
#endif /* I2C_M_HOLD */
static int mctp_binding_smbus_tx(struct mctp_binding *b,
struct mctp_pktbuf *pkt)
{
struct mctp_binding_smbus *smbus = binding_to_smbus(b);
struct mctp_smbus_header_tx *smbus_hdr_tx = (void *)smbus->txbuf;
struct mctp_smbus_pkt_private *pkt_pvt =
(struct mctp_smbus_pkt_private *)pkt->msg_binding_private;
#ifdef I2C_M_HOLD
struct mctp_hdr *mctp_hdr = (void *)(&pkt->data[pkt->start]);
/* Set mux_flags only for EOM packets */
if (!(mctp_hdr->flags_seq_tag & MCTP_HDR_FLAG_EOM)) {
pkt_pvt->mux_flags = 0;
}
#endif
smbus_hdr_tx->command_code = MCTP_COMMAND_CODE;
if (!pkt_pvt) {
mctp_prerr("Binding private information not available");
return -EINVAL;
}
/* the length field in the header excludes smbus framing
* and escape sequences.
*/
size_t pkt_length = mctp_pktbuf_size(pkt);
smbus_hdr_tx->byte_count = pkt_length + 1;
smbus_hdr_tx->source_slave_address = smbus->src_slave_addr;
size_t tx_buf_len = sizeof(*smbus_hdr_tx);
uint8_t i2c_message_len = tx_buf_len + pkt_length + SMBUS_PEC_BYTE_SIZE;
if (i2c_message_len > sizeof(smbus->txbuf)) {
mctp_prerr(
"tx message length exceeds max smbus message length");
return -EINVAL;
}
memcpy(smbus->txbuf + tx_buf_len, &pkt->data[pkt->start], pkt_length);
tx_buf_len += pkt_length;
smbus->txbuf[tx_buf_len] = calculate_pec_byte(smbus->txbuf, tx_buf_len,
pkt_pvt->slave_addr);
int ret = mctp_smbus_tx(smbus, i2c_message_len, pkt_pvt);
if (ret == -EPERM) {
mctp_prdebug(
"Error in tx of smbus message; Operation not permitted");
return ret;
}
if (ret < 0) {
mctp_prerr("Error in tx of smbus message");
return ret;
}
return 0;
}
#ifdef MCTP_HAVE_FILEIO
int mctp_smbus_read(struct mctp_binding_smbus *smbus)
{
ssize_t len = 0;
struct mctp_smbus_header_rx *smbus_hdr_rx;
struct mctp_smbus_pkt_private pvt_data;
uint8_t rx_pec;
#ifdef I2C_M_HOLD
struct mctp_hdr *mctp_hdr;
bool eom = false;
#endif
smbus_hdr_rx = (void *)smbus->rxbuf;
int ret = lseek(smbus->in_fd, 0, SEEK_SET);
if (ret < 0) {
mctp_prerr("Failed to seek");
return -1;
}
len = read(smbus->in_fd, smbus->rxbuf, sizeof(smbus->rxbuf));
if (len < 0) {
mctp_prerr("Failed to read");
return -1;
}
mctp_trace_rx(smbus->rxbuf, len);
if (len < sizeof(*smbus_hdr_rx)) {
/* This condition hits from time to time, even with
* a properly written poll loop, although it's not clear
* why. Return an error so that the upper layer can
* retry.
*/
mctp_prerr("Invalid packet size");
return 0;
}
else if (smbus_hdr_rx->byte_count != (len - sizeof(*smbus_hdr_rx))) {
/* Got an incorrectly sized payload */
mctp_prerr("Got smbus payload sized %zu, expecting %hhu",
len - sizeof(*smbus_hdr_rx),
smbus_hdr_rx->byte_count);
return 0;
}
if (smbus_hdr_rx->destination_slave_address !=
(smbus->src_slave_addr & ~1)) {
mctp_prerr("Got bad slave address %d",
smbus_hdr_rx->destination_slave_address);
return 0;
}
if (smbus_hdr_rx->command_code != MCTP_COMMAND_CODE) {
mctp_prerr("Got bad command code %d",
smbus_hdr_rx->command_code);
/* Not a payload intended for us */
return 0;
}
rx_pec = pec_calculate(0, smbus->rxbuf, len - 1);
if (rx_pec != smbus->rxbuf[len - 1]) {
mctp_prerr("Invalid PEC value: expected: 0x%02x, found 0x%02x",
rx_pec, smbus->rxbuf[len - 1]);
return -1;
}
smbus->rx_pkt = mctp_pktbuf_alloc(&(smbus->binding), 0);
if (!smbus->rx_pkt) {
mctp_prerr("Cannot allocate memory");
return -1;
}
if (mctp_pktbuf_push(
smbus->rx_pkt, &smbus->rxbuf[sizeof(*smbus_hdr_rx)],
len - sizeof(*smbus_hdr_rx) - SMBUS_PEC_BYTE_SIZE) != 0) {
mctp_prerr("Can't push to pktbuf: %m");
mctp_pktbuf_free(smbus->rx_pkt);
return -1;
}
#ifdef I2C_M_HOLD
mctp_hdr = mctp_pktbuf_hdr(smbus->rx_pkt);
if (mctp_hdr->flags_seq_tag & MCTP_HDR_FLAG_EOM) {
eom = true;
}
#endif
memset(&pvt_data, 0, sizeof(struct mctp_smbus_pkt_private));
pvt_data.slave_addr = (smbus_hdr_rx->source_slave_address & ~1);
pvt_data.fd = smbus->out_fd;
memcpy(smbus->rx_pkt->msg_binding_private, &pvt_data, sizeof(pvt_data));
mctp_bus_rx(&(smbus->binding), smbus->rx_pkt);
#ifdef I2C_M_HOLD
/* Unhold mux only for packets with EOM */
if (eom && mctp_smbus_unhold_bus(pvt_data.slave_addr)) {
mctp_prerr("Can't hold mux");
return -1;
}
#endif // I2C_M_HOLD
smbus->rx_pkt = NULL;
return 0;
}
void mctp_smbus_set_in_fd(struct mctp_binding_smbus *smbus, int fd)
{
smbus->in_fd = fd;
}
void mctp_smbus_set_out_fd(struct mctp_binding_smbus *smbus, int fd)
{
smbus->out_fd = fd;
}
#endif
int mctp_smbus_register_bus(struct mctp_binding_smbus *smbus, struct mctp *mctp,
mctp_eid_t eid)
{
int rc = mctp_register_bus(mctp, &smbus->binding, eid);
if (rc == 0) {
/* TODO: Can we drop bus_id from mctp_binding_smbus? */
smbus->bus_id = 0;
mctp_binding_set_tx_enabled(&smbus->binding, true);
}
return rc;
}
struct mctp_binding_smbus *mctp_smbus_init(void)
{
struct mctp_binding_smbus *smbus;
smbus = __mctp_alloc(sizeof(*smbus));
memset(&(smbus->binding), 0, sizeof(smbus->binding));
smbus->in_fd = -1;
smbus->out_fd = -1;
smbus->rx_pkt = NULL;
smbus->binding.name = "smbus";
smbus->binding.version = 1;
smbus->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU);
smbus->binding.pkt_pad = SMBUS_HEADER_SIZE;
smbus->binding.pkt_priv_size = sizeof(struct mctp_smbus_pkt_private);
smbus->binding.tx = mctp_binding_smbus_tx;
/* Setting the default slave address */
smbus->src_slave_addr = DEFAULT_SLAVE_ADDRESS;
return smbus;
}
void mctp_smbus_free(struct mctp_binding_smbus *smbus)
{
if (!(smbus->in_fd < 0)) {
close(smbus->in_fd);
}
if (!(smbus->out_fd < 0)) {
close(smbus->out_fd);
}
__mctp_free(smbus);
}
void mctp_smbus_set_src_slave_addr(struct mctp_binding_smbus *smbus,
uint8_t slave_addr)
{
smbus->src_slave_addr = slave_addr;
}