-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipmb_device.c
437 lines (351 loc) · 11.1 KB
/
ipmb_device.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
// SPDX-License-Identifier: GPL-2.0
/******************************************************************************
* Copyright © 2023, Spectranetix Inc, All Rights Reserved.
*
* File name: ipmb_device.c
* Library: Linux IPMB Driver
*
* Author: Nick Winterer
* Created: 7/12/2021
*
* Description: IPMB driver that acts as a low-level interface to the i2c bus,
* allowing clients to send both requests and responses. This is
* based on the ipmb_dev_int driver, with modifications to support
* both sides of the IPMB transaction.
******************************************************************************/
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/i2c.h>
#include <linux/types.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/kernel.h>
#include <linux/mutex.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/version.h>
#include <linux/wait.h>
#include "ipmb_device.h"
#define REQUEST_QUEUE_MAX_LEN 256
#define HEADER_SA_OFFSET 0
#define HEADER_NETFN_OFFSET 1
#define HEADER_CHECKSUM_OFFSET 2
#define IPMB_HEADER_LENGTH 3
#define GET_7BIT_ADDR(addr_8bit) ((addr_8bit) >> 1)
#define GET_8BIT_ADDR(addr_7bit) (((addr_7bit) << 1) & 0xff)
#define GET_IPMB_MESSAGE_BODY(msg) (((const char *)(msg)) + 1)
/*
* There are several kernel API changes that we need to account for in order to
* support the full range of kernel versions since I2C slave support was added
* in kernel v4.0:
* - **v4.16.0:** __poll_t was introduced
* - **v6.1.0:** i2c_driver.remove signature changed
* - old: int (*remove)(struct i2c_client)
* - new: void (*remove)(struct i2c_client)
* - **v6.3.1:** i2c_driver.probe signature changed
* - old: int (*probe)(struct i2c_client, const struct i2c_device_id*)
* - new: int (*probe)(struct i2c_client)
*
* TODO: Instead of abusing the preprocessor for this, maintain master to be
* compatible with the latest kernel release and add branches for the
* kernel versions prior to each API change.
*/
#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0))
typedef __bitwise unsigned __poll_t;
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0))
#define REMOVE_RET_TYPE void
#define REMOVE_RET_VAL
#else
#define REMOVE_RET_TYPE int
#define REMOVE_RET_VAL 0
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0))
#define PROBE_PARAM_PACK struct i2c_client *client
#else
#define PROBE_PARAM_PACK struct i2c_client *client, const struct i2c_device_id *id
#endif
struct ipmb_msg {
u8 payload_len;
u8 sa;
u8 netfn_lun;
u8 checksum;
u8 payload[U8_MAX];
} __packed;
struct ipmb_request_elem {
struct list_head list;
struct ipmb_msg msg;
};
struct ipmb_device {
struct i2c_client *client;
struct miscdevice miscdev;
struct ipmb_msg msg;
struct list_head msg_queue;
atomic_t msg_queue_len;
size_t msg_idx;
spinlock_t lock;
wait_queue_head_t wait_queue;
struct mutex file_mutex;
bool ignore_nack;
bool verify_checksum;
};
static inline struct ipmb_device *to_ipmb_dev(struct file *file)
{
return container_of(file->private_data, struct ipmb_device, miscdev);
}
static ssize_t ipmb_read(struct file *file, char __user *buf, size_t count,
loff_t *ppos)
{
struct ipmb_device *ipmb_dev = to_ipmb_dev(file);
struct ipmb_request_elem *queue_elem;
struct ipmb_msg msg;
ssize_t ret = 0;
memset(&msg, 0, sizeof(msg));
spin_lock_irq(&ipmb_dev->lock);
while (list_empty(&ipmb_dev->msg_queue)) {
spin_unlock_irq(&ipmb_dev->lock);
if (file->f_flags & O_NONBLOCK)
return -EAGAIN;
ret = wait_event_interruptible(
ipmb_dev->wait_queue,
!list_empty(&ipmb_dev->msg_queue));
if (ret)
return ret;
spin_lock_irq(&ipmb_dev->lock);
}
queue_elem = list_first_entry(&ipmb_dev->msg_queue,
struct ipmb_request_elem, list);
memcpy(&msg, &queue_elem->msg, sizeof(msg));
list_del(&queue_elem->list);
kfree(queue_elem);
atomic_dec(&ipmb_dev->msg_queue_len);
spin_unlock_irq(&ipmb_dev->lock);
count = min_t(size_t, count, msg.payload_len + IPMB_HEADER_LENGTH);
if (copy_to_user(buf, GET_IPMB_MESSAGE_BODY(&msg), count))
ret = -EFAULT;
return ret < 0 ? ret : count;
}
static int ipmb_i2c_write(struct ipmb_device *ipmb_dev, u8 *msg, u8 len,
u8 addr)
{
struct i2c_msg i2c_msg;
/* Assign msg to buffer except first bytes (address) */
i2c_msg.buf = msg + 1;
i2c_msg.len = len - 1;
i2c_msg.addr = addr;
i2c_msg.flags = ipmb_dev->client->flags;
if (ipmb_dev->ignore_nack)
i2c_msg.flags |= I2C_M_IGNORE_NAK;
return i2c_transfer(ipmb_dev->client->adapter, &i2c_msg, 1);
}
static bool ipmb_checksum_verify(u8 sa, u8 netfn_lun, u8 checksum)
{
/* LSB sums to 0 when header checksum is valid */
return (u8)(sa + netfn_lun + checksum) == 0;
}
static ssize_t ipmb_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
struct ipmb_device *ipmb_dev = to_ipmb_dev(file);
u8 sa;
u8 msg[sizeof(struct ipmb_msg)];
ssize_t ret;
if (count > sizeof(msg) || count < IPMB_HEADER_LENGTH)
return -EINVAL;
if (copy_from_user(&msg, buf, count))
return -EFAULT;
if (!ipmb_checksum_verify(msg[HEADER_SA_OFFSET],
msg[HEADER_NETFN_OFFSET],
msg[HEADER_CHECKSUM_OFFSET]))
return -EINVAL;
sa = GET_7BIT_ADDR(msg[HEADER_SA_OFFSET]);
ret = ipmb_i2c_write(ipmb_dev, msg, count, sa);
return (ret == 1) ? count : ret;
}
static __poll_t ipmb_poll(struct file *file, poll_table *wait)
{
struct ipmb_device *ipmb_dev = to_ipmb_dev(file);
__poll_t mask = POLLOUT;
mutex_lock(&ipmb_dev->file_mutex);
poll_wait(file, &ipmb_dev->wait_queue, wait);
if (atomic_read(&ipmb_dev->msg_queue_len))
mask |= POLLIN;
mutex_unlock(&ipmb_dev->file_mutex);
return mask;
}
static long ipmb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int retval = 0;
struct ipmb_device *ipmb_dev = to_ipmb_dev(file);
switch (cmd) {
case IPMB_IOC_EN_IGNORE_NACK:
/* Make sure protocol mangling is supported */
if (i2c_check_functionality(ipmb_dev->client->adapter,
I2C_FUNC_PROTOCOL_MANGLING))
ipmb_dev->ignore_nack = true;
else
retval = EOPNOTSUPP;
break;
case IPMB_IOC_DIS_IGNORE_NACK:
ipmb_dev->ignore_nack = false;
break;
case IPMB_IOC_ENABLE_CHECKSUM:
ipmb_dev->verify_checksum = true;
break;
case IPMB_IOC_DISABLE_CHECKSUM:
ipmb_dev->verify_checksum = false;
break;
}
return retval;
}
static const struct file_operations ipmb_fops = {
.owner = THIS_MODULE,
.read = ipmb_read,
.write = ipmb_write,
.poll = ipmb_poll,
.unlocked_ioctl = ipmb_ioctl,
};
/* Called with ipmb_device->lock held. */
static void ipmb_handle_request(struct ipmb_device *ipmb_dev)
{
struct ipmb_request_elem *queue_elem;
if (atomic_read(&ipmb_dev->msg_queue_len) >= REQUEST_QUEUE_MAX_LEN)
return;
queue_elem = kmalloc(sizeof(*queue_elem), GFP_ATOMIC);
if (!queue_elem)
return;
memcpy(&queue_elem->msg, &ipmb_dev->msg, sizeof(struct ipmb_msg));
list_add(&queue_elem->list, &ipmb_dev->msg_queue);
atomic_inc(&ipmb_dev->msg_queue_len);
wake_up_all(&ipmb_dev->wait_queue);
}
/*
* The IPMB protocol only supports I2C Writes so there is no need
* to support I2C_SLAVE_READ* events.
* This i2c callback function only monitors IPMB msg messages
* and adds them in a queue, so that they can be handled by
* receive_ipmb_request.
*/
static int ipmb_device_slave_cb(struct i2c_client *client,
enum i2c_slave_event event, u8 *val)
{
struct ipmb_device *ipmb_dev = i2c_get_clientdata(client);
u8 *buf = (u8 *)&ipmb_dev->msg;
unsigned long flags;
int ret = 0;
spin_lock_irqsave(&ipmb_dev->lock, flags);
switch (event) {
case I2C_SLAVE_WRITE_REQUESTED:
memset(&ipmb_dev->msg, 0, sizeof(ipmb_dev->msg));
ipmb_dev->msg_idx = 0;
/*
* We need to leave space at index 0 to fill in the length
* after we have received the full message.
*
* The I2C bus driver doesn't include the i2c slave address in
* the data it passes to the i2c slave callback function, but the
* IPMB spec considers it to be part of the message header and it's
* included in the checksum calculation. We can add the address
* to the message here and then append bytes to the message as
* they are received.
*/
buf[++ipmb_dev->msg_idx] = GET_8BIT_ADDR(client->addr);
break;
case I2C_SLAVE_WRITE_RECEIVED:
if (ipmb_dev->msg_idx >= sizeof(struct ipmb_msg) - 1)
break;
buf[++ipmb_dev->msg_idx] = *val;
/*
* Validate checksum immediately after receipt if validation is
* enabled. Bus driver will attempt to NACK the transaction in
* order to free up the bus immediately.
*/
if (ipmb_dev->msg_idx == IPMB_HEADER_LENGTH &&
ipmb_dev->verify_checksum &&
!ipmb_checksum_verify(ipmb_dev->msg.sa,
ipmb_dev->msg.netfn_lun,
ipmb_dev->msg.checksum)) {
ret = -EINVAL;
/*
* SIZE_MAX is used to flag the current message as invalid
* so that when we receive the stop signal, we will know
* not to copy it into the read queue.
*/
ipmb_dev->msg_idx = SIZE_MAX;
}
break;
case I2C_SLAVE_STOP:
if (ipmb_dev->msg_idx <= sizeof(struct ipmb_msg) - 1) {
ipmb_dev->msg.payload_len = ipmb_dev->msg_idx - IPMB_HEADER_LENGTH;
ipmb_handle_request(ipmb_dev);
}
break;
default:
break;
}
spin_unlock_irqrestore(&ipmb_dev->lock, flags);
return ret;
}
static int ipmb_device_probe(PROBE_PARAM_PACK)
{
struct ipmb_device *ipmb_dev;
int ret;
ipmb_dev = devm_kzalloc(&client->dev, sizeof(*ipmb_dev), GFP_KERNEL);
if (!ipmb_dev)
return -ENOMEM;
spin_lock_init(&ipmb_dev->lock);
init_waitqueue_head(&ipmb_dev->wait_queue);
atomic_set(&ipmb_dev->msg_queue_len, 0);
INIT_LIST_HEAD(&ipmb_dev->msg_queue);
mutex_init(&ipmb_dev->file_mutex);
ipmb_dev->ignore_nack = false;
ipmb_dev->miscdev.minor = MISC_DYNAMIC_MINOR;
ipmb_dev->miscdev.name =
devm_kasprintf(&client->dev, GFP_KERNEL, "ipmb-%d",
client->adapter->nr);
ipmb_dev->miscdev.fops = &ipmb_fops;
ipmb_dev->miscdev.parent = &client->dev;
ret = misc_register(&ipmb_dev->miscdev);
if (ret)
return ret;
ipmb_dev->client = client;
i2c_set_clientdata(client, ipmb_dev);
ret = i2c_slave_register(client, ipmb_device_slave_cb);
if (ret) {
misc_deregister(&ipmb_dev->miscdev);
return ret;
}
dev_dbg(ipmb_dev->miscdev.this_device,
"registering at address 0x%02x\n", client->addr);
return 0;
}
static REMOVE_RET_TYPE ipmb_device_remove(struct i2c_client *client)
{
struct ipmb_device *ipmb_dev = i2c_get_clientdata(client);
i2c_slave_unregister(client);
misc_deregister(&ipmb_dev->miscdev);
return REMOVE_RET_VAL;
}
static const struct i2c_device_id ipmb_device_id[] = { { .name = "ipmb-device" },
{} };
MODULE_DEVICE_TABLE(i2c, ipmb_device_id);
static const struct of_device_id ipmb_device_match[] = {
{ .compatible = "ipmb-device" },
{}
};
MODULE_DEVICE_TABLE(of, ipmb_device_match);
static struct i2c_driver ipmb_device_driver = {
.driver = { .name = "ipmb-device",
.owner = THIS_MODULE,
.of_match_table = ipmb_device_match },
.id_table = ipmb_device_id,
.probe = ipmb_device_probe,
.remove = ipmb_device_remove,
};
module_i2c_driver(ipmb_device_driver);
MODULE_AUTHOR("Nick Winterer <[email protected]>");
MODULE_LICENSE("GPL v2");