1 // SPDX-License-Identifier: GPL-2.0
4 * Driver to talk to a remote management controller on IPMB.
7 #include <linux/acpi.h>
8 #include <linux/errno.h>
10 #include <linux/miscdevice.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/poll.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/semaphore.h>
17 #include <linux/kthread.h>
18 #include <linux/wait.h>
19 #include <linux/ipmi_msgdefs.h>
20 #include <linux/ipmi_smi.h>
22 #define DEVICE_NAME "ipmi-ipmb"
24 static int bmcaddr
= 0x20;
25 module_param(bmcaddr
, int, 0644);
26 MODULE_PARM_DESC(bmcaddr
, "Address to use for BMC.");
28 static unsigned int retry_time_ms
= 250;
29 module_param(retry_time_ms
, uint
, 0644);
30 MODULE_PARM_DESC(retry_time_ms
, "Timeout time between retries, in milliseconds.");
32 static unsigned int max_retries
= 1;
33 module_param(max_retries
, uint
, 0644);
34 MODULE_PARM_DESC(max_retries
, "Max resends of a command before timing out.");
36 /* Add room for the two slave addresses, two checksums, and rqSeq. */
37 #define IPMB_MAX_MSG_LEN (IPMI_MAX_MSG_LENGTH + 5)
39 struct ipmi_ipmb_dev
{
40 struct ipmi_smi
*intf
;
41 struct i2c_client
*client
;
42 struct i2c_client
*slave
;
44 struct ipmi_smi_handlers handlers
;
54 struct ipmi_smi_msg
*next_msg
;
55 struct ipmi_smi_msg
*working_msg
;
57 /* Transmit thread. */
58 struct task_struct
*thread
;
59 struct semaphore wake_thread
;
60 struct semaphore got_rsp
;
64 u8 xmitmsg
[IPMB_MAX_MSG_LEN
];
67 u8 rcvmsg
[IPMB_MAX_MSG_LEN
];
72 static bool valid_ipmb(struct ipmi_ipmb_dev
*iidev
)
74 u8
*msg
= iidev
->rcvmsg
;
80 /* Minimum message size. */
81 if (iidev
->rcvlen
< 7)
84 /* Is it a response? */
87 /* Response messages have an added completion code. */
88 if (iidev
->rcvlen
< 8)
92 if (ipmb_checksum(msg
, 3) != 0)
94 if (ipmb_checksum(msg
+ 3, iidev
->rcvlen
- 3) != 0)
100 static void ipmi_ipmb_check_msg_done(struct ipmi_ipmb_dev
*iidev
)
102 struct ipmi_smi_msg
*imsg
= NULL
;
103 u8
*msg
= iidev
->rcvmsg
;
107 if (iidev
->rcvlen
== 0)
109 if (!valid_ipmb(iidev
))
112 is_cmd
= ((msg
[1] >> 2) & 1) == 0;
115 /* Ignore commands until we are up. */
119 /* It's a command, allocate a message for it. */
120 imsg
= ipmi_alloc_smi_msg();
123 imsg
->type
= IPMI_SMI_MSG_TYPE_IPMB_DIRECT
;
126 spin_lock_irqsave(&iidev
->lock
, flags
);
127 if (iidev
->working_msg
) {
128 u8 seq
= msg
[4] >> 2;
129 bool xmit_rsp
= (iidev
->working_msg
->data
[0] >> 2) & 1;
132 * Responses should carry the sequence we sent
133 * them with. If it's a transmitted response,
134 * ignore it. And if the message hasn't been
135 * transmitted, ignore it.
137 if (!xmit_rsp
&& seq
== iidev
->curr_seq
) {
138 iidev
->curr_seq
= (iidev
->curr_seq
+ 1) & 0x3f;
140 imsg
= iidev
->working_msg
;
141 iidev
->working_msg
= NULL
;
144 spin_unlock_irqrestore(&iidev
->lock
, flags
);
150 if (imsg
->type
== IPMI_SMI_MSG_TYPE_IPMB_DIRECT
) {
151 imsg
->rsp
[0] = msg
[1]; /* NetFn/LUN */
153 * Keep the source address, rqSeq. Drop the trailing
156 memcpy(imsg
->rsp
+ 1, msg
+ 3, iidev
->rcvlen
- 4);
157 imsg
->rsp_size
= iidev
->rcvlen
- 3;
159 imsg
->rsp
[0] = msg
[1]; /* NetFn/LUN */
161 * Skip the source address, rqSeq. Drop the trailing
164 memcpy(imsg
->rsp
+ 1, msg
+ 5, iidev
->rcvlen
- 6);
165 imsg
->rsp_size
= iidev
->rcvlen
- 5;
167 ipmi_smi_msg_received(iidev
->intf
, imsg
);
172 iidev
->overrun
= false;
177 * The IPMB protocol only supports i2c writes so there is no need to
178 * support I2C_SLAVE_READ* events, except to know if the other end has
179 * issued a read without going to stop mode.
181 static int ipmi_ipmb_slave_cb(struct i2c_client
*client
,
182 enum i2c_slave_event event
, u8
*val
)
184 struct ipmi_ipmb_dev
*iidev
= i2c_get_clientdata(client
);
187 case I2C_SLAVE_WRITE_REQUESTED
:
188 ipmi_ipmb_check_msg_done(iidev
);
190 * First byte is the slave address, to ease the checksum
193 iidev
->rcvmsg
[0] = client
->addr
<< 1;
197 case I2C_SLAVE_WRITE_RECEIVED
:
198 if (iidev
->rcvlen
>= sizeof(iidev
->rcvmsg
))
199 iidev
->overrun
= true;
201 iidev
->rcvmsg
[iidev
->rcvlen
++] = *val
;
204 case I2C_SLAVE_READ_REQUESTED
:
206 ipmi_ipmb_check_msg_done(iidev
);
209 case I2C_SLAVE_READ_PROCESSED
:
216 static void ipmi_ipmb_send_response(struct ipmi_ipmb_dev
*iidev
,
217 struct ipmi_smi_msg
*msg
, u8 cc
)
219 if ((msg
->data
[0] >> 2) & 1) {
221 * It's a response being sent, we need to return a
222 * response to the response. Fake a send msg command
223 * response with channel 0. This will always be ipmb
226 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
| 1) << 2;
227 msg
->data
[3] = IPMI_SEND_MSG_CMD
;
231 msg
->rsp
[0] = msg
->data
[0] | (1 << 2);
232 if (msg
->type
== IPMI_SMI_MSG_TYPE_IPMB_DIRECT
) {
233 msg
->rsp
[1] = msg
->data
[1];
234 msg
->rsp
[2] = msg
->data
[2];
235 msg
->rsp
[3] = msg
->data
[3];
239 msg
->rsp
[1] = msg
->data
[1];
243 ipmi_smi_msg_received(iidev
->intf
, msg
);
246 static void ipmi_ipmb_format_for_xmit(struct ipmi_ipmb_dev
*iidev
,
247 struct ipmi_smi_msg
*msg
)
249 if (msg
->type
== IPMI_SMI_MSG_TYPE_IPMB_DIRECT
) {
250 iidev
->xmitmsg
[0] = msg
->data
[1];
251 iidev
->xmitmsg
[1] = msg
->data
[0];
252 memcpy(iidev
->xmitmsg
+ 4, msg
->data
+ 2, msg
->data_size
- 2);
253 iidev
->xmitlen
= msg
->data_size
+ 2;
255 iidev
->xmitmsg
[0] = iidev
->bmcaddr
;
256 iidev
->xmitmsg
[1] = msg
->data
[0];
257 iidev
->xmitmsg
[4] = 0;
258 memcpy(iidev
->xmitmsg
+ 5, msg
->data
+ 1, msg
->data_size
- 1);
259 iidev
->xmitlen
= msg
->data_size
+ 4;
261 iidev
->xmitmsg
[3] = iidev
->slave
->addr
<< 1;
262 if (((msg
->data
[0] >> 2) & 1) == 0)
263 /* If it's a command, put in our own sequence number. */
264 iidev
->xmitmsg
[4] = ((iidev
->xmitmsg
[4] & 0x03) |
265 (iidev
->curr_seq
<< 2));
267 /* Now add on the final checksums. */
268 iidev
->xmitmsg
[2] = ipmb_checksum(iidev
->xmitmsg
, 2);
269 iidev
->xmitmsg
[iidev
->xmitlen
] =
270 ipmb_checksum(iidev
->xmitmsg
+ 3, iidev
->xmitlen
- 3);
274 static int ipmi_ipmb_thread(void *data
)
276 struct ipmi_ipmb_dev
*iidev
= data
;
278 while (!kthread_should_stop()) {
280 struct i2c_msg i2c_msg
;
281 struct ipmi_smi_msg
*msg
= NULL
;
283 unsigned int retries
= 0;
285 /* Wait for a message to send */
286 ret
= down_interruptible(&iidev
->wake_thread
);
292 spin_lock_irqsave(&iidev
->lock
, flags
);
293 if (iidev
->next_msg
) {
294 msg
= iidev
->next_msg
;
295 iidev
->next_msg
= NULL
;
297 spin_unlock_irqrestore(&iidev
->lock
, flags
);
301 ipmi_ipmb_format_for_xmit(iidev
, msg
);
304 i2c_msg
.len
= iidev
->xmitlen
- 1;
305 if (i2c_msg
.len
> 32) {
306 ipmi_ipmb_send_response(iidev
, msg
,
307 IPMI_REQ_LEN_EXCEEDED_ERR
);
311 i2c_msg
.addr
= iidev
->xmitmsg
[0] >> 1;
313 i2c_msg
.buf
= iidev
->xmitmsg
+ 1;
315 /* Rely on i2c_transfer for a barrier. */
316 iidev
->working_msg
= msg
;
318 ret
= i2c_transfer(iidev
->client
->adapter
, &i2c_msg
, 1);
320 if ((msg
->data
[0] >> 2) & 1) {
322 * It's a response, nothing will be returned
326 iidev
->working_msg
= NULL
;
327 ipmi_ipmb_send_response(iidev
, msg
,
328 ret
< 0 ? IPMI_BUS_ERR
: 0);
332 iidev
->working_msg
= NULL
;
333 ipmi_ipmb_send_response(iidev
, msg
, IPMI_BUS_ERR
);
337 /* A command was sent, wait for its response. */
338 ret
= down_timeout(&iidev
->got_rsp
,
339 msecs_to_jiffies(iidev
->retry_time_ms
));
342 * Grab the message if we can. If the handler hasn't
343 * already handled it, the message will still be there.
345 spin_lock_irqsave(&iidev
->lock
, flags
);
346 msg
= iidev
->working_msg
;
347 iidev
->working_msg
= NULL
;
348 spin_unlock_irqrestore(&iidev
->lock
, flags
);
352 * If working_msg is not set and we timed out,
353 * that means the message grabbed by
354 * check_msg_done before we could grab it
355 * here. Wait again for check_msg_done to up
358 down(&iidev
->got_rsp
);
359 } else if (msg
&& ++retries
<= iidev
->max_retries
) {
360 spin_lock_irqsave(&iidev
->lock
, flags
);
361 iidev
->working_msg
= msg
;
362 spin_unlock_irqrestore(&iidev
->lock
, flags
);
367 ipmi_ipmb_send_response(iidev
, msg
, IPMI_TIMEOUT_ERR
);
371 /* Return an unspecified error. */
372 ipmi_ipmb_send_response(iidev
, iidev
->next_msg
, 0xff);
377 static int ipmi_ipmb_start_processing(void *send_info
,
378 struct ipmi_smi
*new_intf
)
380 struct ipmi_ipmb_dev
*iidev
= send_info
;
382 iidev
->intf
= new_intf
;
387 static void ipmi_ipmb_stop_thread(struct ipmi_ipmb_dev
*iidev
)
390 struct task_struct
*t
= iidev
->thread
;
392 iidev
->thread
= NULL
;
393 iidev
->stopping
= true;
394 up(&iidev
->wake_thread
);
400 static void ipmi_ipmb_shutdown(void *send_info
)
402 struct ipmi_ipmb_dev
*iidev
= send_info
;
404 ipmi_ipmb_stop_thread(iidev
);
407 static void ipmi_ipmb_sender(void *send_info
,
408 struct ipmi_smi_msg
*msg
)
410 struct ipmi_ipmb_dev
*iidev
= send_info
;
413 spin_lock_irqsave(&iidev
->lock
, flags
);
414 BUG_ON(iidev
->next_msg
);
416 iidev
->next_msg
= msg
;
417 spin_unlock_irqrestore(&iidev
->lock
, flags
);
419 up(&iidev
->wake_thread
);
422 static void ipmi_ipmb_request_events(void *send_info
)
424 /* We don't fetch events here. */
427 static void ipmi_ipmb_cleanup(struct ipmi_ipmb_dev
*iidev
)
430 i2c_slave_unregister(iidev
->slave
);
431 if (iidev
->slave
!= iidev
->client
)
432 i2c_unregister_device(iidev
->slave
);
435 iidev
->client
= NULL
;
436 ipmi_ipmb_stop_thread(iidev
);
439 static void ipmi_ipmb_remove(struct i2c_client
*client
)
441 struct ipmi_ipmb_dev
*iidev
= i2c_get_clientdata(client
);
443 ipmi_ipmb_cleanup(iidev
);
444 ipmi_unregister_smi(iidev
->intf
);
447 static int ipmi_ipmb_probe(struct i2c_client
*client
)
449 struct device
*dev
= &client
->dev
;
450 struct ipmi_ipmb_dev
*iidev
;
451 struct device_node
*slave_np
;
452 struct i2c_adapter
*slave_adap
= NULL
;
453 struct i2c_client
*slave
= NULL
;
456 iidev
= devm_kzalloc(&client
->dev
, sizeof(*iidev
), GFP_KERNEL
);
460 if (of_property_read_u8(dev
->of_node
, "bmcaddr", &iidev
->bmcaddr
) != 0)
461 iidev
->bmcaddr
= bmcaddr
;
462 if (iidev
->bmcaddr
== 0 || iidev
->bmcaddr
& 1) {
463 /* Can't have the write bit set. */
464 dev_notice(&client
->dev
,
465 "Invalid bmc address value %2.2x\n", iidev
->bmcaddr
);
469 if (of_property_read_u32(dev
->of_node
, "retry-time",
470 &iidev
->retry_time_ms
) != 0)
471 iidev
->retry_time_ms
= retry_time_ms
;
473 if (of_property_read_u32(dev
->of_node
, "max-retries",
474 &iidev
->max_retries
) != 0)
475 iidev
->max_retries
= max_retries
;
477 slave_np
= of_parse_phandle(dev
->of_node
, "slave-dev", 0);
479 slave_adap
= of_get_i2c_adapter_by_node(slave_np
);
480 of_node_put(slave_np
);
482 dev_notice(&client
->dev
,
483 "Could not find slave adapter\n");
488 iidev
->client
= client
;
491 struct i2c_board_info binfo
;
493 memset(&binfo
, 0, sizeof(binfo
));
494 strscpy(binfo
.type
, "ipmb-slave", I2C_NAME_SIZE
);
495 binfo
.addr
= client
->addr
;
496 binfo
.flags
= I2C_CLIENT_SLAVE
;
497 slave
= i2c_new_client_device(slave_adap
, &binfo
);
498 i2c_put_adapter(slave_adap
);
501 dev_notice(&client
->dev
,
502 "Could not allocate slave device: %d\n", rv
);
505 i2c_set_clientdata(slave
, iidev
);
509 i2c_set_clientdata(client
, iidev
);
510 slave
->flags
|= I2C_CLIENT_SLAVE
;
512 rv
= i2c_slave_register(slave
, ipmi_ipmb_slave_cb
);
515 iidev
->slave
= slave
;
518 iidev
->handlers
.flags
= IPMI_SMI_CAN_HANDLE_IPMB_DIRECT
;
519 iidev
->handlers
.start_processing
= ipmi_ipmb_start_processing
;
520 iidev
->handlers
.shutdown
= ipmi_ipmb_shutdown
;
521 iidev
->handlers
.sender
= ipmi_ipmb_sender
;
522 iidev
->handlers
.request_events
= ipmi_ipmb_request_events
;
524 spin_lock_init(&iidev
->lock
);
525 sema_init(&iidev
->wake_thread
, 0);
526 sema_init(&iidev
->got_rsp
, 0);
528 iidev
->thread
= kthread_run(ipmi_ipmb_thread
, iidev
,
529 "kipmb%4.4x", client
->addr
);
530 if (IS_ERR(iidev
->thread
)) {
531 rv
= PTR_ERR(iidev
->thread
);
532 dev_notice(&client
->dev
,
533 "Could not start kernel thread: error %d\n", rv
);
537 rv
= ipmi_register_smi(&iidev
->handlers
,
547 if (slave
&& slave
!= client
)
548 i2c_unregister_device(slave
);
549 ipmi_ipmb_cleanup(iidev
);
554 static const struct of_device_id of_ipmi_ipmb_match
[] = {
555 { .type
= "ipmi", .compatible
= DEVICE_NAME
},
558 MODULE_DEVICE_TABLE(of
, of_ipmi_ipmb_match
);
560 #define of_ipmi_ipmb_match NULL
563 static const struct i2c_device_id ipmi_ipmb_id
[] = {
567 MODULE_DEVICE_TABLE(i2c
, ipmi_ipmb_id
);
569 static struct i2c_driver ipmi_ipmb_driver
= {
570 .class = I2C_CLASS_HWMON
,
573 .of_match_table
= of_ipmi_ipmb_match
,
575 .probe
= ipmi_ipmb_probe
,
576 .remove
= ipmi_ipmb_remove
,
577 .id_table
= ipmi_ipmb_id
,
579 module_i2c_driver(ipmi_ipmb_driver
);
581 MODULE_AUTHOR("Corey Minyard");
582 MODULE_DESCRIPTION("IPMI IPMB driver");
583 MODULE_LICENSE("GPL v2");