summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-core/interfaces/libmctp/smbus.c
blob: d7c39644487f74f736263178055b4a2c15917027 (plain)
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
/* SPDX-License-Identifier: Apache-2.0 */

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef MCTP_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"

struct mctp_binding_smbus {
	struct mctp_binding binding;
	int out_fd;
	int in_fd;

	unsigned long bus_id;

	/* receive buffer */
	uint8_t rxbuf[1024];
	struct mctp_pktbuf *rx_pkt;

	/* temporary transmit buffer */
	uint8_t txbuf[256];
};

#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_ADDRESS 0x1d
#define MCTP_SOURCE_SLAVE_ADDRESS 0x21

#define SMBUS_PEC_BYTE_SIZE 1
#define SMBUS_COMMAND_CODE_SIZE 1
#define SMBUS_LENGTH_FIELD_SIZE 1

struct mctp_smbus_header_tx {
	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;
};

#define POLYCHECK (0x1070U << 3)
static uint8_t crc8_calculate(uint16_t d)
{
	int i;

	for (i = 0; i < 8; i++) {
		if (d & 0x8000) {
			d = d ^ POLYCHECK;
		}
		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,
				  uint16_t flags)
{
	uint8_t addr = (address << 1) | (flags & I2C_M_RD ? 1 : 0);
	uint8_t pec = pec_calculate(0, &addr, 1);
	pec = pec_calculate(pec, buf, len);

	return pec;
}

static int mctp_smbus_tx(struct mctp_binding_smbus *smbus, uint8_t len)
{

#ifdef I2C_M_HOLD
	/* Hold message */
	static uint16_t holdtimeout = 1000; // timeout in ms.
	struct i2c_msg msg[2] =
#else  // !I2C_M_HOLD
	struct i2c_msg msg[1] =
#endif // I2C_M_HOLD
		{{.addr = MCTP_SLAVE_ADDRESS,
		  .flags = 0,
		  .len = len,
		  .buf = (__uint8_t *)smbus->txbuf}
#ifdef I2C_M_HOLD
		 ,
		 {.addr = 0,
		  .flags = I2C_M_HOLD,
		  .len = sizeof(holdtimeout),
		  .buf = (__uint8_t *)&holdtimeout}
#endif // I2C_M_HOLD
		};

#ifdef I2C_M_HOLD
	struct i2c_rdwr_ioctl_data msgrdwr = {&msg, 2};
#else  // !I2C_M_HOLD
	struct i2c_rdwr_ioctl_data msgrdwr = {&msg, 1};
#endif // I2C_M_HOLD

	return ioctl(smbus->out_fd, I2C_RDWR, &msgrdwr);
}

#ifdef I2C_M_HOLD
static int mctp_smbus_unhold_bus(struct mctp_binding_smbus *smbus)
{
	/* Unhold message */
	static uint16_t holdtimeout = 0; // unhold
	struct i2c_msg holdmsg = {0, I2C_M_HOLD, sizeof(holdtimeout),
				  (__uint8_t *)&holdtimeout};

	struct i2c_rdwr_ioctl_data msgrdwr = {&holdmsg, 1};

	return ioctl(smbus->out_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 *hdr;
	size_t pkt_length;

	uint8_t i2c_message_buf[256];
	uint8_t *buf_ptr;
	uint8_t i2c_message_len;

	uint16_t timeout = 1000;

	/* the length field in the header excludes smbus framing
	 * and escape sequences */
	pkt_length = mctp_pktbuf_size(pkt);

	buf_ptr = (void *)smbus->txbuf;
	*buf_ptr = MCTP_COMMAND_CODE;
	buf_ptr++;
	*buf_ptr = pkt_length + sizeof(*hdr);
	buf_ptr++;

	hdr = (void *)buf_ptr;
	hdr->source_slave_address = MCTP_SOURCE_SLAVE_ADDRESS;
	buf_ptr = (buf_ptr + sizeof(*hdr));
	memcpy(buf_ptr, &pkt->data[pkt->start], pkt_length);
	buf_ptr = buf_ptr + pkt_length;

	uint8_t pec_byte = calculate_pec_byte(
		smbus->txbuf,
		SMBUS_COMMAND_CODE_SIZE + SMBUS_LENGTH_FIELD_SIZE + sizeof(*hdr)
			+ pkt_length,
		MCTP_SLAVE_ADDRESS, 0);

	*buf_ptr = pec_byte;

	i2c_message_len = SMBUS_COMMAND_CODE_SIZE + SMBUS_LENGTH_FIELD_SIZE
			  + sizeof(*hdr) + pkt_length
			  + SMBUS_PEC_BYTE_SIZE; // command code, length,
						 // header, data, pec byte

	if (mctp_smbus_tx(smbus, i2c_message_len)) {
		mctp_prerr("Can't hold mux");
		return -1;
	}

	return 0;
}

#ifdef MCTP_FILEIO
int mctp_smbus_read(struct mctp_binding_smbus *smbus)
{
	ssize_t len = 0;
	struct mctp_smbus_header_rx *hdr;
	int ret = 0;

	do {
		ret = lseek(smbus->in_fd, 0, SEEK_SET);
		if (ret < 0) {
			mctp_prerr("Failed to seek");
			ret = -1;
		}

		len = read(smbus->in_fd, smbus->rxbuf, sizeof(smbus->rxbuf));
		if (len < sizeof(*hdr)) {
			// This condition hits from 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.
			ret = 0;
			break;
		}

		hdr = (void *)smbus->rxbuf;
		if (hdr->destination_slave_address
		    != (MCTP_SOURCE_SLAVE_ADDRESS & ~1)) {
			mctp_prerr("Got bad slave address %d",
				   hdr->destination_slave_address);
			ret = 0;
			break;
		}
		if (hdr->command_code != MCTP_COMMAND_CODE) {
			mctp_prerr("Got bad command code %d",
				   hdr->command_code);
			// Not a payload intended for us
			ret = 0;
			break;
		}

		if (hdr->byte_count != (len - sizeof(*hdr))) {
			// Got an incorrectly sized payload
			mctp_prerr("Got smbus payload sized %d, expecting %d",
				   hdr->byte_count, len - sizeof(*hdr));
			ret = 0;
			break;
		}

		if (len < 0) {
			mctp_prerr("can't read from smbus device: %m");
			ret = -1;
			break;
		}

		smbus->rx_pkt = mctp_pktbuf_alloc(0);
		assert(smbus->rx_pkt);

		if (mctp_pktbuf_push(smbus->rx_pkt, &smbus->rxbuf[sizeof(*hdr)],
				     len - sizeof(*hdr) - SMBUS_PEC_BYTE_SIZE)
		    != 0) {
			mctp_prerr("Can't push tok pktbuf: %m");
			ret = -1;
			break;
		}

		mctp_bus_rx(&(smbus->binding), smbus->rx_pkt);

		mctp_pktbuf_free(smbus->rx_pkt);
		smbus->rx_pkt = NULL;

	} while (0);

#ifdef I2C_M_HOLD
	if (mctp_smbus_unhold_bus(smbus)) {
		mctp_prerr("Can't hold mux");
		ret = -1;
	}
#endif // I2C_M_HOLD

	return ret;
}

int mctp_smbus_get_in_fd(struct mctp_binding_smbus *smbus)
{
	return smbus->in_fd;
}

int mctp_smbus_get_out_fd(struct mctp_binding_smbus *smbus)
{
	return smbus->out_fd;
}

int mctp_smbus_open_bus(struct mctp_binding_smbus *smbus, int out_bus_num,
			int root_bus_num)
{
	char filename[60];
	size_t filename_size = 0;
	char slave_mqueue[20];
	size_t mqueue_size = 0;
	int fd = 0;
	size_t size = sizeof(filename);
	int address_7_bit = MCTP_SOURCE_SLAVE_ADDRESS >> 1;

	snprintf(filename, size,
		 "/sys/bus/i2c/devices/i2c-%d/%d-%04x/slave-mqueue",
		 root_bus_num, root_bus_num,
		 (address_7_bit << 8) + address_7_bit);

	smbus->in_fd = open(filename, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
	if (smbus->in_fd < 0) {
		// Device doesn't exist.  Create it.
		filename_size = sizeof(filename);
		snprintf(filename, filename_size,
			 "/sys/bus/i2c/devices/i2c-%d/new_device",
			 root_bus_num);
		filename[filename_size - 1] = '\0';

		fd = open(filename, O_WRONLY);
		if (fd < 0) {
			mctp_prerr("can't open root device %s: %m", filename);
			return -1;
		}

		mqueue_size = sizeof(slave_mqueue);
		snprintf(slave_mqueue, mqueue_size, "slave-mqueue %#04x",
			 (address_7_bit << 8) + address_7_bit);

		size = write(fd, slave_mqueue, mqueue_size);
		close(fd);
		if (size != mqueue_size) {
			mctp_prerr("can't create mqueue device on %s: %m",
				   filename);
			return -1;
		}

		size = sizeof(filename);
		snprintf(filename, size,
			 "/sys/bus/i2c/devices/i2c-%d/%d-%04x/slave-mqueue",
			 root_bus_num, root_bus_num,
			 (address_7_bit << 8) + address_7_bit);

		smbus->in_fd =
			open(filename, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
		if (smbus->in_fd < 0) {
			mctp_prerr("can't open mqueue device on %s: %m",
				   filename);
			return -2;
		}
	}

	size = sizeof(filename);
	snprintf(filename, size, "/dev/i2c-%d", out_bus_num);
	filename[size - 1] = '\0';

	smbus->out_fd = open(filename, O_RDWR | O_NONBLOCK);
	if (smbus->out_fd < 0) {
		close(smbus->in_fd);
		mctp_prerr("can't open device %s: %m", filename);
	}

	return 0;
}
#endif

void mctp_smbus_register_bus(struct mctp_binding_smbus *smbus,
			     struct mctp *mctp, mctp_eid_t eid)
{
	assert(smbus->out_fd >= 0);
	assert(smbus->in_fd >= 0);
	smbus->bus_id = mctp_register_bus(mctp, &smbus->binding, eid);
	mctp_binding_set_tx_enabled(&smbus->binding, true);
}

struct mctp_binding_smbus *mctp_smbus_init(void)
{
	struct mctp_binding_smbus *smbus;

	smbus = __mctp_alloc(sizeof(*smbus));
	smbus->in_fd = -1;
	smbus->out_fd = -1;

	smbus->rx_pkt = NULL;
	smbus->binding.name = "smbus";
	smbus->binding.version = 1;

	smbus->binding.tx = mctp_binding_smbus_tx;

	return smbus;
}

void mctp_smbus_free(struct mctp_binding_smbus *smbus)
{
	__mctp_free(smbus);
}