1 // SPDX-License-Identifier: GPL-2.0+
5 * The interface to the IPMI driver for SMBus access to a SMBus
6 * compliant device. Called SSIF by the IPMI spec.
8 * Author: Intel Corporation
9 * Todd Davis <todd.c.davis@intel.com>
11 * Rewritten by Corey Minyard <minyard@acm.org> to support the
12 * non-blocking I2C interface, add support for multi-part
13 * transactions, add PEC support, and general clenaup.
15 * Copyright 2003 Intel Corporation
16 * Copyright 2005 MontaVista Software
20 * This file holds the "policy" for the interface to the SSIF state
21 * machine. It does the configuration, handles timers and interrupts,
22 * and drives the real SSIF state machine.
26 * TODO: Figure out how to use SMB alerts. This will require a new
27 * interface into the I2C driver, I believe.
30 #define pr_fmt(fmt) "ipmi_ssif: " fmt
31 #define dev_fmt(fmt) "ipmi_ssif: " fmt
33 #if defined(MODVERSIONS)
34 #include <linux/modversions.h>
37 #include <linux/module.h>
38 #include <linux/moduleparam.h>
39 #include <linux/sched.h>
40 #include <linux/seq_file.h>
41 #include <linux/timer.h>
42 #include <linux/delay.h>
43 #include <linux/errno.h>
44 #include <linux/spinlock.h>
45 #include <linux/slab.h>
46 #include <linux/list.h>
47 #include <linux/i2c.h>
48 #include <linux/ipmi_smi.h>
49 #include <linux/init.h>
50 #include <linux/dmi.h>
51 #include <linux/kthread.h>
52 #include <linux/acpi.h>
53 #include <linux/ctype.h>
54 #include <linux/time64.h>
55 #include "ipmi_si_sm.h"
58 #define DEVICE_NAME "ipmi_ssif"
60 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
62 #define SSIF_IPMI_REQUEST 2
63 #define SSIF_IPMI_MULTI_PART_REQUEST_START 6
64 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
65 #define SSIF_IPMI_MULTI_PART_REQUEST_END 8
66 #define SSIF_IPMI_RESPONSE 3
67 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
69 /* ssif_debug is a bit-field
70 * SSIF_DEBUG_MSG - commands and their responses
71 * SSIF_DEBUG_STATES - message states
72 * SSIF_DEBUG_TIMING - Measure times between events in the driver
74 #define SSIF_DEBUG_TIMING 4
75 #define SSIF_DEBUG_STATE 2
76 #define SSIF_DEBUG_MSG 1
77 #define SSIF_NODEBUG 0
78 #define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
83 #define SSIF_MSG_USEC 20000 /* 20ms between message tries. */
84 #define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
86 /* How many times to we retry sending/receiving the message. */
87 #define SSIF_SEND_RETRIES 5
88 #define SSIF_RECV_RETRIES 250
90 #define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
91 #define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
92 #define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
95 * Timeout for the watch, only used for get flag timer.
97 #define SSIF_WATCH_MSG_TIMEOUT msecs_to_jiffies(10)
98 #define SSIF_WATCH_WATCHDOG_TIMEOUT msecs_to_jiffies(250)
100 enum ssif_intf_state
{
105 SSIF_GETTING_MESSAGES
,
106 /* FIXME - add watchdog stuff. */
109 #define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
110 && (ssif)->curr_msg == NULL)
113 * Indexes into stats[] in ssif_info below.
115 enum ssif_stat_indexes
{
116 /* Number of total messages sent. */
117 SSIF_STAT_sent_messages
= 0,
120 * Number of message parts sent. Messages may be broken into
121 * parts if they are long.
123 SSIF_STAT_sent_messages_parts
,
126 * Number of time a message was retried.
128 SSIF_STAT_send_retries
,
131 * Number of times the send of a message failed.
133 SSIF_STAT_send_errors
,
136 * Number of message responses received.
138 SSIF_STAT_received_messages
,
141 * Number of message fragments received.
143 SSIF_STAT_received_message_parts
,
146 * Number of times the receive of a message was retried.
148 SSIF_STAT_receive_retries
,
151 * Number of errors receiving messages.
153 SSIF_STAT_receive_errors
,
156 * Number of times a flag fetch was requested.
158 SSIF_STAT_flag_fetches
,
161 * Number of times the hardware didn't follow the state machine.
166 * Number of received events.
170 /* Number of asyncronous messages received. */
171 SSIF_STAT_incoming_messages
,
173 /* Number of watchdog pretimeouts. */
174 SSIF_STAT_watchdog_pretimeouts
,
176 /* Number of alers received. */
179 /* Always add statistics before this value, it must be last. */
183 struct ssif_addr_info
{
184 struct i2c_board_info binfo
;
188 enum ipmi_addr_src addr_src
;
189 union ipmi_smi_info_union addr_info
;
191 struct i2c_client
*client
;
193 struct i2c_client
*added_client
;
195 struct mutex clients_mutex
;
196 struct list_head clients
;
198 struct list_head link
;
203 typedef void (*ssif_i2c_done
)(struct ssif_info
*ssif_info
, int result
,
204 unsigned char *data
, unsigned int len
);
207 struct ipmi_smi
*intf
;
209 struct ipmi_smi_msg
*waiting_msg
;
210 struct ipmi_smi_msg
*curr_msg
;
211 enum ssif_intf_state ssif_state
;
212 unsigned long ssif_debug
;
214 struct ipmi_smi_handlers handlers
;
216 enum ipmi_addr_src addr_source
; /* ACPI, PCI, SMBIOS, hardcode, etc. */
217 union ipmi_smi_info_union addr_info
;
220 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
221 * is set to hold the flags until we are done handling everything
224 #define RECEIVE_MSG_AVAIL 0x01
225 #define EVENT_MSG_BUFFER_FULL 0x02
226 #define WDT_PRE_TIMEOUT_INT 0x08
227 unsigned char msg_flags
;
230 bool has_event_buffer
;
234 * Used to tell what we should do with alerts. If we are
235 * waiting on a response, read the data immediately.
241 * If set to true, this will request events the next time the
242 * state machine is idle.
247 * If set to true, this will request flags the next time the
248 * state machine is idle.
253 * Used to perform timer operations when run-to-completion
254 * mode is on. This is a countdown timer.
258 /* Used for sending/receiving data. +1 for the length. */
259 unsigned char data
[IPMI_MAX_MSG_LENGTH
+ 1];
260 unsigned int data_len
;
262 /* Temp receive buffer, gets copied into data. */
263 unsigned char recv
[I2C_SMBUS_BLOCK_MAX
];
265 struct i2c_client
*client
;
266 ssif_i2c_done done_handler
;
268 /* Thread interface handling */
269 struct task_struct
*thread
;
270 struct completion wake_thread
;
274 unsigned char *i2c_data
;
275 unsigned int i2c_size
;
277 struct timer_list retry_timer
;
280 long watch_timeout
; /* Timeout for flags check, 0 if off. */
281 struct timer_list watch_timer
; /* Flag fetch timer. */
283 /* Info from SSIF cmd */
284 unsigned char max_xmit_msg_size
;
285 unsigned char max_recv_msg_size
;
286 bool cmd8_works
; /* See test_multipart_messages() for details. */
287 unsigned int multi_support
;
290 #define SSIF_NO_MULTI 0
291 #define SSIF_MULTI_2_PART 1
292 #define SSIF_MULTI_n_PART 2
293 unsigned char *multi_data
;
294 unsigned int multi_len
;
295 unsigned int multi_pos
;
297 atomic_t stats
[SSIF_NUM_STATS
];
300 #define ssif_inc_stat(ssif, stat) \
301 atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
302 #define ssif_get_stat(ssif, stat) \
303 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
305 static bool initialized
;
306 static bool platform_registered
;
308 static void return_hosed_msg(struct ssif_info
*ssif_info
,
309 struct ipmi_smi_msg
*msg
);
310 static void start_next_msg(struct ssif_info
*ssif_info
, unsigned long *flags
);
311 static int start_send(struct ssif_info
*ssif_info
,
315 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info
*ssif_info
,
316 unsigned long *flags
)
318 spin_lock_irqsave(&ssif_info
->lock
, *flags
);
322 static void ipmi_ssif_unlock_cond(struct ssif_info
*ssif_info
,
323 unsigned long *flags
)
325 spin_unlock_irqrestore(&ssif_info
->lock
, *flags
);
328 static void deliver_recv_msg(struct ssif_info
*ssif_info
,
329 struct ipmi_smi_msg
*msg
)
331 if (msg
->rsp_size
< 0) {
332 return_hosed_msg(ssif_info
, msg
);
333 dev_err(&ssif_info
->client
->dev
,
334 "%s: Malformed message: rsp_size = %d\n",
335 __func__
, msg
->rsp_size
);
337 ipmi_smi_msg_received(ssif_info
->intf
, msg
);
341 static void return_hosed_msg(struct ssif_info
*ssif_info
,
342 struct ipmi_smi_msg
*msg
)
344 ssif_inc_stat(ssif_info
, hosed
);
346 /* Make it a response */
347 msg
->rsp
[0] = msg
->data
[0] | 4;
348 msg
->rsp
[1] = msg
->data
[1];
349 msg
->rsp
[2] = 0xFF; /* Unknown error. */
352 deliver_recv_msg(ssif_info
, msg
);
356 * Must be called with the message lock held. This will release the
357 * message lock. Note that the caller will check SSIF_IDLE and start a
358 * new operation, so there is no need to check for new messages to
361 static void start_clear_flags(struct ssif_info
*ssif_info
, unsigned long *flags
)
363 unsigned char msg
[3];
365 ssif_info
->msg_flags
&= ~WDT_PRE_TIMEOUT_INT
;
366 ssif_info
->ssif_state
= SSIF_CLEARING_FLAGS
;
367 ipmi_ssif_unlock_cond(ssif_info
, flags
);
369 /* Make sure the watchdog pre-timeout flag is not set at startup. */
370 msg
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
371 msg
[1] = IPMI_CLEAR_MSG_FLAGS_CMD
;
372 msg
[2] = WDT_PRE_TIMEOUT_INT
;
374 if (start_send(ssif_info
, msg
, 3) != 0) {
375 /* Error, just go to normal state. */
376 ssif_info
->ssif_state
= SSIF_NORMAL
;
380 static void start_flag_fetch(struct ssif_info
*ssif_info
, unsigned long *flags
)
384 ssif_info
->req_flags
= false;
385 ssif_info
->ssif_state
= SSIF_GETTING_FLAGS
;
386 ipmi_ssif_unlock_cond(ssif_info
, flags
);
388 mb
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
389 mb
[1] = IPMI_GET_MSG_FLAGS_CMD
;
390 if (start_send(ssif_info
, mb
, 2) != 0)
391 ssif_info
->ssif_state
= SSIF_NORMAL
;
394 static void check_start_send(struct ssif_info
*ssif_info
, unsigned long *flags
,
395 struct ipmi_smi_msg
*msg
)
397 if (start_send(ssif_info
, msg
->data
, msg
->data_size
) != 0) {
398 unsigned long oflags
;
400 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
401 ssif_info
->curr_msg
= NULL
;
402 ssif_info
->ssif_state
= SSIF_NORMAL
;
403 ipmi_ssif_unlock_cond(ssif_info
, flags
);
404 ipmi_free_smi_msg(msg
);
408 static void start_event_fetch(struct ssif_info
*ssif_info
, unsigned long *flags
)
410 struct ipmi_smi_msg
*msg
;
412 ssif_info
->req_events
= false;
414 msg
= ipmi_alloc_smi_msg();
416 ssif_info
->ssif_state
= SSIF_NORMAL
;
417 ipmi_ssif_unlock_cond(ssif_info
, flags
);
421 ssif_info
->curr_msg
= msg
;
422 ssif_info
->ssif_state
= SSIF_GETTING_EVENTS
;
423 ipmi_ssif_unlock_cond(ssif_info
, flags
);
425 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
426 msg
->data
[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD
;
429 check_start_send(ssif_info
, flags
, msg
);
432 static void start_recv_msg_fetch(struct ssif_info
*ssif_info
,
433 unsigned long *flags
)
435 struct ipmi_smi_msg
*msg
;
437 msg
= ipmi_alloc_smi_msg();
439 ssif_info
->ssif_state
= SSIF_NORMAL
;
440 ipmi_ssif_unlock_cond(ssif_info
, flags
);
444 ssif_info
->curr_msg
= msg
;
445 ssif_info
->ssif_state
= SSIF_GETTING_MESSAGES
;
446 ipmi_ssif_unlock_cond(ssif_info
, flags
);
448 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
449 msg
->data
[1] = IPMI_GET_MSG_CMD
;
452 check_start_send(ssif_info
, flags
, msg
);
456 * Must be called with the message lock held. This will release the
457 * message lock. Note that the caller will check SSIF_IDLE and start a
458 * new operation, so there is no need to check for new messages to
461 static void handle_flags(struct ssif_info
*ssif_info
, unsigned long *flags
)
463 if (ssif_info
->msg_flags
& WDT_PRE_TIMEOUT_INT
) {
464 /* Watchdog pre-timeout */
465 ssif_inc_stat(ssif_info
, watchdog_pretimeouts
);
466 start_clear_flags(ssif_info
, flags
);
467 ipmi_smi_watchdog_pretimeout(ssif_info
->intf
);
468 } else if (ssif_info
->msg_flags
& RECEIVE_MSG_AVAIL
)
469 /* Messages available. */
470 start_recv_msg_fetch(ssif_info
, flags
);
471 else if (ssif_info
->msg_flags
& EVENT_MSG_BUFFER_FULL
)
472 /* Events available. */
473 start_event_fetch(ssif_info
, flags
);
475 ssif_info
->ssif_state
= SSIF_NORMAL
;
476 ipmi_ssif_unlock_cond(ssif_info
, flags
);
480 static int ipmi_ssif_thread(void *data
)
482 struct ssif_info
*ssif_info
= data
;
484 while (!kthread_should_stop()) {
487 /* Wait for something to do */
488 result
= wait_for_completion_interruptible(
489 &ssif_info
->wake_thread
);
490 if (ssif_info
->stopping
)
492 if (result
== -ERESTARTSYS
)
494 init_completion(&ssif_info
->wake_thread
);
496 if (ssif_info
->i2c_read_write
== I2C_SMBUS_WRITE
) {
497 result
= i2c_smbus_write_block_data(
498 ssif_info
->client
, ssif_info
->i2c_command
,
499 ssif_info
->i2c_data
[0],
500 ssif_info
->i2c_data
+ 1);
501 ssif_info
->done_handler(ssif_info
, result
, NULL
, 0);
503 result
= i2c_smbus_read_block_data(
504 ssif_info
->client
, ssif_info
->i2c_command
,
505 ssif_info
->i2c_data
);
507 ssif_info
->done_handler(ssif_info
, result
,
510 ssif_info
->done_handler(ssif_info
, 0,
519 static int ssif_i2c_send(struct ssif_info
*ssif_info
,
520 ssif_i2c_done handler
,
521 int read_write
, int command
,
522 unsigned char *data
, unsigned int size
)
524 ssif_info
->done_handler
= handler
;
526 ssif_info
->i2c_read_write
= read_write
;
527 ssif_info
->i2c_command
= command
;
528 ssif_info
->i2c_data
= data
;
529 ssif_info
->i2c_size
= size
;
530 complete(&ssif_info
->wake_thread
);
535 static void msg_done_handler(struct ssif_info
*ssif_info
, int result
,
536 unsigned char *data
, unsigned int len
);
538 static void start_get(struct ssif_info
*ssif_info
)
542 ssif_info
->rtc_us_timer
= 0;
543 ssif_info
->multi_pos
= 0;
545 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
, I2C_SMBUS_READ
,
547 ssif_info
->recv
, I2C_SMBUS_BLOCK_DATA
);
549 /* request failed, just return the error. */
550 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
551 dev_dbg(&ssif_info
->client
->dev
,
552 "Error from i2c_non_blocking_op(5)\n");
554 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
558 static void retry_timeout(struct timer_list
*t
)
560 struct ssif_info
*ssif_info
= from_timer(ssif_info
, t
, retry_timer
);
561 unsigned long oflags
, *flags
;
564 if (ssif_info
->stopping
)
567 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
568 waiting
= ssif_info
->waiting_alert
;
569 ssif_info
->waiting_alert
= false;
570 ipmi_ssif_unlock_cond(ssif_info
, flags
);
573 start_get(ssif_info
);
576 static void watch_timeout(struct timer_list
*t
)
578 struct ssif_info
*ssif_info
= from_timer(ssif_info
, t
, watch_timer
);
579 unsigned long oflags
, *flags
;
581 if (ssif_info
->stopping
)
584 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
585 if (ssif_info
->watch_timeout
) {
586 mod_timer(&ssif_info
->watch_timer
,
587 jiffies
+ ssif_info
->watch_timeout
);
588 if (SSIF_IDLE(ssif_info
)) {
589 start_flag_fetch(ssif_info
, flags
); /* Releases lock */
592 ssif_info
->req_flags
= true;
594 ipmi_ssif_unlock_cond(ssif_info
, flags
);
597 static void ssif_alert(struct i2c_client
*client
, enum i2c_alert_protocol type
,
600 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
601 unsigned long oflags
, *flags
;
604 if (type
!= I2C_PROTOCOL_SMBUS_ALERT
)
607 ssif_inc_stat(ssif_info
, alerts
);
609 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
610 if (ssif_info
->waiting_alert
) {
611 ssif_info
->waiting_alert
= false;
612 del_timer(&ssif_info
->retry_timer
);
614 } else if (ssif_info
->curr_msg
) {
615 ssif_info
->got_alert
= true;
617 ipmi_ssif_unlock_cond(ssif_info
, flags
);
619 start_get(ssif_info
);
622 static int start_resend(struct ssif_info
*ssif_info
);
624 static void msg_done_handler(struct ssif_info
*ssif_info
, int result
,
625 unsigned char *data
, unsigned int len
)
627 struct ipmi_smi_msg
*msg
;
628 unsigned long oflags
, *flags
;
632 * We are single-threaded here, so no need for a lock until we
633 * start messing with driver states or the queues.
637 ssif_info
->retries_left
--;
638 if (ssif_info
->retries_left
> 0) {
639 ssif_inc_stat(ssif_info
, receive_retries
);
641 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
642 ssif_info
->waiting_alert
= true;
643 ssif_info
->rtc_us_timer
= SSIF_MSG_USEC
;
644 if (!ssif_info
->stopping
)
645 mod_timer(&ssif_info
->retry_timer
,
646 jiffies
+ SSIF_MSG_JIFFIES
);
647 ipmi_ssif_unlock_cond(ssif_info
, flags
);
651 ssif_inc_stat(ssif_info
, receive_errors
);
653 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
654 dev_dbg(&ssif_info
->client
->dev
,
655 "%s: Error %d\n", __func__
, result
);
660 if ((len
> 1) && (ssif_info
->multi_pos
== 0)
661 && (data
[0] == 0x00) && (data
[1] == 0x01)) {
662 /* Start of multi-part read. Start the next transaction. */
665 ssif_inc_stat(ssif_info
, received_message_parts
);
667 /* Remove the multi-part read marker. */
670 for (i
= 0; i
< len
; i
++)
671 ssif_info
->data
[i
] = data
[i
];
672 ssif_info
->multi_len
= len
;
673 ssif_info
->multi_pos
= 1;
675 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
, I2C_SMBUS_READ
,
676 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE
,
677 ssif_info
->recv
, I2C_SMBUS_BLOCK_DATA
);
679 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
680 dev_dbg(&ssif_info
->client
->dev
,
681 "Error from i2c_non_blocking_op(1)\n");
686 } else if (ssif_info
->multi_pos
) {
687 /* Middle of multi-part read. Start the next transaction. */
689 unsigned char blocknum
;
693 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
694 dev_dbg(&ssif_info
->client
->dev
,
695 "Middle message with no data\n");
704 if (blocknum
!= 0xff && len
!= 31) {
705 /* All blocks but the last must have 31 data bytes. */
707 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
708 dev_dbg(&ssif_info
->client
->dev
,
709 "Received middle message <31\n");
714 if (ssif_info
->multi_len
+ len
> IPMI_MAX_MSG_LENGTH
) {
715 /* Received message too big, abort the operation. */
717 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
718 dev_dbg(&ssif_info
->client
->dev
,
719 "Received message too big\n");
724 for (i
= 0; i
< len
; i
++)
725 ssif_info
->data
[i
+ ssif_info
->multi_len
] = data
[i
];
726 ssif_info
->multi_len
+= len
;
727 if (blocknum
== 0xff) {
729 len
= ssif_info
->multi_len
;
730 data
= ssif_info
->data
;
731 } else if (blocknum
+ 1 != ssif_info
->multi_pos
) {
733 * Out of sequence block, just abort. Block
734 * numbers start at zero for the second block,
735 * but multi_pos starts at one, so the +1.
737 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
738 dev_dbg(&ssif_info
->client
->dev
,
739 "Received message out of sequence, expected %u, got %u\n",
740 ssif_info
->multi_pos
- 1, blocknum
);
743 ssif_inc_stat(ssif_info
, received_message_parts
);
745 ssif_info
->multi_pos
++;
747 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
,
749 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE
,
751 I2C_SMBUS_BLOCK_DATA
);
753 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
754 dev_dbg(&ssif_info
->client
->dev
,
755 "Error from ssif_i2c_send\n");
765 ssif_inc_stat(ssif_info
, receive_errors
);
767 ssif_inc_stat(ssif_info
, received_messages
);
768 ssif_inc_stat(ssif_info
, received_message_parts
);
771 if (ssif_info
->ssif_debug
& SSIF_DEBUG_STATE
)
772 dev_dbg(&ssif_info
->client
->dev
,
773 "DONE 1: state = %d, result=%d\n",
774 ssif_info
->ssif_state
, result
);
776 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
777 msg
= ssif_info
->curr_msg
;
780 if (msg
->rsp_size
> IPMI_MAX_MSG_LENGTH
)
781 msg
->rsp_size
= IPMI_MAX_MSG_LENGTH
;
782 memcpy(msg
->rsp
, data
, msg
->rsp_size
);
783 ssif_info
->curr_msg
= NULL
;
786 switch (ssif_info
->ssif_state
) {
788 ipmi_ssif_unlock_cond(ssif_info
, flags
);
793 return_hosed_msg(ssif_info
, msg
);
795 deliver_recv_msg(ssif_info
, msg
);
798 case SSIF_GETTING_FLAGS
:
799 /* We got the flags from the SSIF, now handle them. */
800 if ((result
< 0) || (len
< 4) || (data
[2] != 0)) {
802 * Error fetching flags, or invalid length,
803 * just give up for now.
805 ssif_info
->ssif_state
= SSIF_NORMAL
;
806 ipmi_ssif_unlock_cond(ssif_info
, flags
);
807 dev_warn(&ssif_info
->client
->dev
,
808 "Error getting flags: %d %d, %x\n",
809 result
, len
, (len
>= 3) ? data
[2] : 0);
810 } else if (data
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
811 || data
[1] != IPMI_GET_MSG_FLAGS_CMD
) {
813 * Don't abort here, maybe it was a queued
814 * response to a previous command.
816 ipmi_ssif_unlock_cond(ssif_info
, flags
);
817 dev_warn(&ssif_info
->client
->dev
,
818 "Invalid response getting flags: %x %x\n",
821 ssif_inc_stat(ssif_info
, flag_fetches
);
822 ssif_info
->msg_flags
= data
[3];
823 handle_flags(ssif_info
, flags
);
827 case SSIF_CLEARING_FLAGS
:
828 /* We cleared the flags. */
829 if ((result
< 0) || (len
< 3) || (data
[2] != 0)) {
830 /* Error clearing flags */
831 dev_warn(&ssif_info
->client
->dev
,
832 "Error clearing flags: %d %d, %x\n",
833 result
, len
, (len
>= 3) ? data
[2] : 0);
834 } else if (data
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
835 || data
[1] != IPMI_CLEAR_MSG_FLAGS_CMD
) {
836 dev_warn(&ssif_info
->client
->dev
,
837 "Invalid response clearing flags: %x %x\n",
840 ssif_info
->ssif_state
= SSIF_NORMAL
;
841 ipmi_ssif_unlock_cond(ssif_info
, flags
);
844 case SSIF_GETTING_EVENTS
:
845 if ((result
< 0) || (len
< 3) || (msg
->rsp
[2] != 0)) {
846 /* Error getting event, probably done. */
849 /* Take off the event flag. */
850 ssif_info
->msg_flags
&= ~EVENT_MSG_BUFFER_FULL
;
851 handle_flags(ssif_info
, flags
);
852 } else if (msg
->rsp
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
853 || msg
->rsp
[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD
) {
854 dev_warn(&ssif_info
->client
->dev
,
855 "Invalid response getting events: %x %x\n",
856 msg
->rsp
[0], msg
->rsp
[1]);
858 /* Take off the event flag. */
859 ssif_info
->msg_flags
&= ~EVENT_MSG_BUFFER_FULL
;
860 handle_flags(ssif_info
, flags
);
862 handle_flags(ssif_info
, flags
);
863 ssif_inc_stat(ssif_info
, events
);
864 deliver_recv_msg(ssif_info
, msg
);
868 case SSIF_GETTING_MESSAGES
:
869 if ((result
< 0) || (len
< 3) || (msg
->rsp
[2] != 0)) {
870 /* Error getting event, probably done. */
873 /* Take off the msg flag. */
874 ssif_info
->msg_flags
&= ~RECEIVE_MSG_AVAIL
;
875 handle_flags(ssif_info
, flags
);
876 } else if (msg
->rsp
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
877 || msg
->rsp
[1] != IPMI_GET_MSG_CMD
) {
878 dev_warn(&ssif_info
->client
->dev
,
879 "Invalid response clearing flags: %x %x\n",
880 msg
->rsp
[0], msg
->rsp
[1]);
883 /* Take off the msg flag. */
884 ssif_info
->msg_flags
&= ~RECEIVE_MSG_AVAIL
;
885 handle_flags(ssif_info
, flags
);
887 ssif_inc_stat(ssif_info
, incoming_messages
);
888 handle_flags(ssif_info
, flags
);
889 deliver_recv_msg(ssif_info
, msg
);
894 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
895 if (SSIF_IDLE(ssif_info
) && !ssif_info
->stopping
) {
896 if (ssif_info
->req_events
)
897 start_event_fetch(ssif_info
, flags
);
898 else if (ssif_info
->req_flags
)
899 start_flag_fetch(ssif_info
, flags
);
901 start_next_msg(ssif_info
, flags
);
903 ipmi_ssif_unlock_cond(ssif_info
, flags
);
905 if (ssif_info
->ssif_debug
& SSIF_DEBUG_STATE
)
906 dev_dbg(&ssif_info
->client
->dev
,
907 "DONE 2: state = %d.\n", ssif_info
->ssif_state
);
910 static void msg_written_handler(struct ssif_info
*ssif_info
, int result
,
911 unsigned char *data
, unsigned int len
)
915 /* We are single-threaded here, so no need for a lock. */
917 ssif_info
->retries_left
--;
918 if (ssif_info
->retries_left
> 0) {
919 if (!start_resend(ssif_info
)) {
920 ssif_inc_stat(ssif_info
, send_retries
);
923 /* request failed, just return the error. */
924 ssif_inc_stat(ssif_info
, send_errors
);
926 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
927 dev_dbg(&ssif_info
->client
->dev
,
928 "%s: Out of retries\n", __func__
);
929 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
933 ssif_inc_stat(ssif_info
, send_errors
);
936 * Got an error on transmit, let the done routine
939 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
940 dev_dbg(&ssif_info
->client
->dev
,
941 "%s: Error %d\n", __func__
, result
);
943 msg_done_handler(ssif_info
, result
, NULL
, 0);
947 if (ssif_info
->multi_data
) {
949 * In the middle of a multi-data write. See the comment
950 * in the SSIF_MULTI_n_PART case in the probe function
951 * for details on the intricacies of this.
954 unsigned char *data_to_send
;
957 ssif_inc_stat(ssif_info
, sent_messages_parts
);
959 left
= ssif_info
->multi_len
- ssif_info
->multi_pos
;
964 ssif_info
->multi_data
[ssif_info
->multi_pos
] = to_write
;
965 data_to_send
= ssif_info
->multi_data
+ ssif_info
->multi_pos
;
966 ssif_info
->multi_pos
+= to_write
;
967 cmd
= SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
;
968 if (ssif_info
->cmd8_works
) {
969 if (left
== to_write
) {
970 cmd
= SSIF_IPMI_MULTI_PART_REQUEST_END
;
971 ssif_info
->multi_data
= NULL
;
973 } else if (to_write
< 32) {
974 ssif_info
->multi_data
= NULL
;
977 rv
= ssif_i2c_send(ssif_info
, msg_written_handler
,
978 I2C_SMBUS_WRITE
, cmd
,
979 data_to_send
, I2C_SMBUS_BLOCK_DATA
);
981 /* request failed, just return the error. */
982 ssif_inc_stat(ssif_info
, send_errors
);
984 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
985 dev_dbg(&ssif_info
->client
->dev
,
986 "Error from i2c_non_blocking_op(3)\n");
987 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
990 /* Ready to request the result. */
991 unsigned long oflags
, *flags
;
993 ssif_inc_stat(ssif_info
, sent_messages
);
994 ssif_inc_stat(ssif_info
, sent_messages_parts
);
996 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
997 if (ssif_info
->got_alert
) {
998 /* The result is already ready, just start it. */
999 ssif_info
->got_alert
= false;
1000 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1001 start_get(ssif_info
);
1003 /* Wait a jiffie then request the next message */
1004 ssif_info
->waiting_alert
= true;
1005 ssif_info
->retries_left
= SSIF_RECV_RETRIES
;
1006 ssif_info
->rtc_us_timer
= SSIF_MSG_PART_USEC
;
1007 if (!ssif_info
->stopping
)
1008 mod_timer(&ssif_info
->retry_timer
,
1009 jiffies
+ SSIF_MSG_PART_JIFFIES
);
1010 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1015 static int start_resend(struct ssif_info
*ssif_info
)
1020 ssif_info
->got_alert
= false;
1022 if (ssif_info
->data_len
> 32) {
1023 command
= SSIF_IPMI_MULTI_PART_REQUEST_START
;
1024 ssif_info
->multi_data
= ssif_info
->data
;
1025 ssif_info
->multi_len
= ssif_info
->data_len
;
1027 * Subtle thing, this is 32, not 33, because we will
1028 * overwrite the thing at position 32 (which was just
1029 * transmitted) with the new length.
1031 ssif_info
->multi_pos
= 32;
1032 ssif_info
->data
[0] = 32;
1034 ssif_info
->multi_data
= NULL
;
1035 command
= SSIF_IPMI_REQUEST
;
1036 ssif_info
->data
[0] = ssif_info
->data_len
;
1039 rv
= ssif_i2c_send(ssif_info
, msg_written_handler
, I2C_SMBUS_WRITE
,
1040 command
, ssif_info
->data
, I2C_SMBUS_BLOCK_DATA
);
1041 if (rv
&& (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
))
1042 dev_dbg(&ssif_info
->client
->dev
,
1043 "Error from i2c_non_blocking_op(4)\n");
1047 static int start_send(struct ssif_info
*ssif_info
,
1048 unsigned char *data
,
1051 if (len
> IPMI_MAX_MSG_LENGTH
)
1053 if (len
> ssif_info
->max_xmit_msg_size
)
1056 ssif_info
->retries_left
= SSIF_SEND_RETRIES
;
1057 memcpy(ssif_info
->data
+ 1, data
, len
);
1058 ssif_info
->data_len
= len
;
1059 return start_resend(ssif_info
);
1062 /* Must be called with the message lock held. */
1063 static void start_next_msg(struct ssif_info
*ssif_info
, unsigned long *flags
)
1065 struct ipmi_smi_msg
*msg
;
1066 unsigned long oflags
;
1069 if (!SSIF_IDLE(ssif_info
)) {
1070 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1074 if (!ssif_info
->waiting_msg
) {
1075 ssif_info
->curr_msg
= NULL
;
1076 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1080 ssif_info
->curr_msg
= ssif_info
->waiting_msg
;
1081 ssif_info
->waiting_msg
= NULL
;
1082 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1083 rv
= start_send(ssif_info
,
1084 ssif_info
->curr_msg
->data
,
1085 ssif_info
->curr_msg
->data_size
);
1087 msg
= ssif_info
->curr_msg
;
1088 ssif_info
->curr_msg
= NULL
;
1089 return_hosed_msg(ssif_info
, msg
);
1090 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1096 static void sender(void *send_info
,
1097 struct ipmi_smi_msg
*msg
)
1099 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1100 unsigned long oflags
, *flags
;
1102 BUG_ON(ssif_info
->waiting_msg
);
1103 ssif_info
->waiting_msg
= msg
;
1105 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1106 start_next_msg(ssif_info
, flags
);
1108 if (ssif_info
->ssif_debug
& SSIF_DEBUG_TIMING
) {
1109 struct timespec64 t
;
1111 ktime_get_real_ts64(&t
);
1112 dev_dbg(&ssif_info
->client
->dev
,
1113 "**Enqueue %02x %02x: %lld.%6.6ld\n",
1114 msg
->data
[0], msg
->data
[1],
1115 (long long)t
.tv_sec
, (long)t
.tv_nsec
/ NSEC_PER_USEC
);
1119 static int get_smi_info(void *send_info
, struct ipmi_smi_info
*data
)
1121 struct ssif_info
*ssif_info
= send_info
;
1123 data
->addr_src
= ssif_info
->addr_source
;
1124 data
->dev
= &ssif_info
->client
->dev
;
1125 data
->addr_info
= ssif_info
->addr_info
;
1126 get_device(data
->dev
);
1132 * Upper layer wants us to request events.
1134 static void request_events(void *send_info
)
1136 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1137 unsigned long oflags
, *flags
;
1139 if (!ssif_info
->has_event_buffer
)
1142 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1143 ssif_info
->req_events
= true;
1144 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1148 * Upper layer is changing the flag saying whether we need to request
1149 * flags periodically or not.
1151 static void ssif_set_need_watch(void *send_info
, unsigned int watch_mask
)
1153 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1154 unsigned long oflags
, *flags
;
1157 if (watch_mask
& IPMI_WATCH_MASK_CHECK_MESSAGES
)
1158 timeout
= SSIF_WATCH_MSG_TIMEOUT
;
1159 else if (watch_mask
)
1160 timeout
= SSIF_WATCH_WATCHDOG_TIMEOUT
;
1162 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1163 if (timeout
!= ssif_info
->watch_timeout
) {
1164 ssif_info
->watch_timeout
= timeout
;
1165 if (ssif_info
->watch_timeout
)
1166 mod_timer(&ssif_info
->watch_timer
,
1167 jiffies
+ ssif_info
->watch_timeout
);
1169 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1172 static int ssif_start_processing(void *send_info
,
1173 struct ipmi_smi
*intf
)
1175 struct ssif_info
*ssif_info
= send_info
;
1177 ssif_info
->intf
= intf
;
1182 #define MAX_SSIF_BMCS 4
1184 static unsigned short addr
[MAX_SSIF_BMCS
];
1185 static int num_addrs
;
1186 module_param_array(addr
, ushort
, &num_addrs
, 0);
1187 MODULE_PARM_DESC(addr
, "The addresses to scan for IPMI BMCs on the SSIFs.");
1189 static char *adapter_name
[MAX_SSIF_BMCS
];
1190 static int num_adapter_names
;
1191 module_param_array(adapter_name
, charp
, &num_adapter_names
, 0);
1192 MODULE_PARM_DESC(adapter_name
, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1194 static int slave_addrs
[MAX_SSIF_BMCS
];
1195 static int num_slave_addrs
;
1196 module_param_array(slave_addrs
, int, &num_slave_addrs
, 0);
1197 MODULE_PARM_DESC(slave_addrs
,
1198 "The default IPMB slave address for the controller.");
1200 static bool alerts_broken
;
1201 module_param(alerts_broken
, bool, 0);
1202 MODULE_PARM_DESC(alerts_broken
, "Don't enable alerts for the controller.");
1205 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1206 * bit 2 enables timing debugging. This is an array indexed by
1209 static int dbg
[MAX_SSIF_BMCS
];
1211 module_param_array(dbg
, int, &num_dbg
, 0);
1212 MODULE_PARM_DESC(dbg
, "Turn on debugging.");
1214 static bool ssif_dbg_probe
;
1215 module_param_named(dbg_probe
, ssif_dbg_probe
, bool, 0);
1216 MODULE_PARM_DESC(dbg_probe
, "Enable debugging of probing of adapters.");
1218 static bool ssif_tryacpi
= true;
1219 module_param_named(tryacpi
, ssif_tryacpi
, bool, 0);
1220 MODULE_PARM_DESC(tryacpi
, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1222 static bool ssif_trydmi
= true;
1223 module_param_named(trydmi
, ssif_trydmi
, bool, 0);
1224 MODULE_PARM_DESC(trydmi
, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1226 static DEFINE_MUTEX(ssif_infos_mutex
);
1227 static LIST_HEAD(ssif_infos
);
1229 #define IPMI_SSIF_ATTR(name) \
1230 static ssize_t ipmi_##name##_show(struct device *dev, \
1231 struct device_attribute *attr, \
1234 struct ssif_info *ssif_info = dev_get_drvdata(dev); \
1236 return snprintf(buf, 10, "%u\n", ssif_get_stat(ssif_info, name));\
1238 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1240 static ssize_t
ipmi_type_show(struct device
*dev
,
1241 struct device_attribute
*attr
,
1244 return snprintf(buf
, 10, "ssif\n");
1246 static DEVICE_ATTR(type
, S_IRUGO
, ipmi_type_show
, NULL
);
1248 IPMI_SSIF_ATTR(sent_messages
);
1249 IPMI_SSIF_ATTR(sent_messages_parts
);
1250 IPMI_SSIF_ATTR(send_retries
);
1251 IPMI_SSIF_ATTR(send_errors
);
1252 IPMI_SSIF_ATTR(received_messages
);
1253 IPMI_SSIF_ATTR(received_message_parts
);
1254 IPMI_SSIF_ATTR(receive_retries
);
1255 IPMI_SSIF_ATTR(receive_errors
);
1256 IPMI_SSIF_ATTR(flag_fetches
);
1257 IPMI_SSIF_ATTR(hosed
);
1258 IPMI_SSIF_ATTR(events
);
1259 IPMI_SSIF_ATTR(watchdog_pretimeouts
);
1260 IPMI_SSIF_ATTR(alerts
);
1262 static struct attribute
*ipmi_ssif_dev_attrs
[] = {
1263 &dev_attr_type
.attr
,
1264 &dev_attr_sent_messages
.attr
,
1265 &dev_attr_sent_messages_parts
.attr
,
1266 &dev_attr_send_retries
.attr
,
1267 &dev_attr_send_errors
.attr
,
1268 &dev_attr_received_messages
.attr
,
1269 &dev_attr_received_message_parts
.attr
,
1270 &dev_attr_receive_retries
.attr
,
1271 &dev_attr_receive_errors
.attr
,
1272 &dev_attr_flag_fetches
.attr
,
1273 &dev_attr_hosed
.attr
,
1274 &dev_attr_events
.attr
,
1275 &dev_attr_watchdog_pretimeouts
.attr
,
1276 &dev_attr_alerts
.attr
,
1280 static const struct attribute_group ipmi_ssif_dev_attr_group
= {
1281 .attrs
= ipmi_ssif_dev_attrs
,
1284 static void shutdown_ssif(void *send_info
)
1286 struct ssif_info
*ssif_info
= send_info
;
1288 device_remove_group(&ssif_info
->client
->dev
, &ipmi_ssif_dev_attr_group
);
1289 dev_set_drvdata(&ssif_info
->client
->dev
, NULL
);
1291 /* make sure the driver is not looking for flags any more. */
1292 while (ssif_info
->ssif_state
!= SSIF_NORMAL
)
1293 schedule_timeout(1);
1295 ssif_info
->stopping
= true;
1296 del_timer_sync(&ssif_info
->watch_timer
);
1297 del_timer_sync(&ssif_info
->retry_timer
);
1298 if (ssif_info
->thread
) {
1299 complete(&ssif_info
->wake_thread
);
1300 kthread_stop(ssif_info
->thread
);
1304 static int ssif_remove(struct i2c_client
*client
)
1306 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
1307 struct ssif_addr_info
*addr_info
;
1313 * After this point, we won't deliver anything asychronously
1314 * to the message handler. We can unregister ourself.
1316 ipmi_unregister_smi(ssif_info
->intf
);
1318 list_for_each_entry(addr_info
, &ssif_infos
, link
) {
1319 if (addr_info
->client
== client
) {
1320 addr_info
->client
= NULL
;
1330 static int read_response(struct i2c_client
*client
, unsigned char *resp
)
1332 int ret
= -ENODEV
, retry_cnt
= SSIF_RECV_RETRIES
;
1334 while (retry_cnt
> 0) {
1335 ret
= i2c_smbus_read_block_data(client
, SSIF_IPMI_RESPONSE
,
1339 msleep(SSIF_MSG_MSEC
);
1348 static int do_cmd(struct i2c_client
*client
, int len
, unsigned char *msg
,
1349 int *resp_len
, unsigned char *resp
)
1354 retry_cnt
= SSIF_SEND_RETRIES
;
1356 ret
= i2c_smbus_write_block_data(client
, SSIF_IPMI_REQUEST
, len
, msg
);
1364 ret
= read_response(client
, resp
);
1366 /* Validate that the response is correct. */
1368 (resp
[0] != (msg
[0] | (1 << 2))) ||
1369 (resp
[1] != msg
[1]))
1371 else if (ret
> IPMI_MAX_MSG_LENGTH
) {
1382 static int ssif_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
1384 unsigned char *resp
;
1385 unsigned char msg
[3];
1389 resp
= kmalloc(IPMI_MAX_MSG_LENGTH
, GFP_KERNEL
);
1393 /* Do a Get Device ID command, since it is required. */
1394 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1395 msg
[1] = IPMI_GET_DEVICE_ID_CMD
;
1396 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1400 strlcpy(info
->type
, DEVICE_NAME
, I2C_NAME_SIZE
);
1405 static int strcmp_nospace(char *s1
, char *s2
)
1407 while (*s1
&& *s2
) {
1408 while (isspace(*s1
))
1410 while (isspace(*s2
))
1422 static struct ssif_addr_info
*ssif_info_find(unsigned short addr
,
1424 bool match_null_name
)
1426 struct ssif_addr_info
*info
, *found
= NULL
;
1429 list_for_each_entry(info
, &ssif_infos
, link
) {
1430 if (info
->binfo
.addr
== addr
) {
1431 if (info
->adapter_name
|| adapter_name
) {
1432 if (!info
->adapter_name
!= !adapter_name
) {
1433 /* One is NULL and one is not */
1437 strcmp_nospace(info
->adapter_name
,
1439 /* Names do not match */
1447 if (!found
&& match_null_name
) {
1448 /* Try to get an exact match first, then try with a NULL name */
1449 adapter_name
= NULL
;
1450 match_null_name
= false;
1457 static bool check_acpi(struct ssif_info
*ssif_info
, struct device
*dev
)
1460 acpi_handle acpi_handle
;
1462 acpi_handle
= ACPI_HANDLE(dev
);
1464 ssif_info
->addr_source
= SI_ACPI
;
1465 ssif_info
->addr_info
.acpi_info
.acpi_handle
= acpi_handle
;
1472 static int find_slave_address(struct i2c_client
*client
, int slave_addr
)
1474 #ifdef CONFIG_IPMI_DMI_DECODE
1476 slave_addr
= ipmi_dmi_get_slave_addr(
1478 i2c_adapter_id(client
->adapter
),
1485 static int start_multipart_test(struct i2c_client
*client
,
1486 unsigned char *msg
, bool do_middle
)
1488 int retry_cnt
= SSIF_SEND_RETRIES
, ret
;
1491 ret
= i2c_smbus_write_block_data(client
,
1492 SSIF_IPMI_MULTI_PART_REQUEST_START
,
1498 dev_err(&client
->dev
, "Could not write multi-part start, though the BMC said it could handle it. Just limit sends to one part.\n");
1505 ret
= i2c_smbus_write_block_data(client
,
1506 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
,
1509 dev_err(&client
->dev
, "Could not write multi-part middle, though the BMC said it could handle it. Just limit sends to one part.\n");
1516 static void test_multipart_messages(struct i2c_client
*client
,
1517 struct ssif_info
*ssif_info
,
1518 unsigned char *resp
)
1520 unsigned char msg
[65];
1524 if (ssif_info
->max_xmit_msg_size
<= 32)
1527 do_middle
= ssif_info
->max_xmit_msg_size
> 63;
1529 memset(msg
, 0, sizeof(msg
));
1530 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1531 msg
[1] = IPMI_GET_DEVICE_ID_CMD
;
1534 * The specification is all messed up dealing with sending
1535 * multi-part messages. Per what the specification says, it
1536 * is impossible to send a message that is a multiple of 32
1537 * bytes, except for 32 itself. It talks about a "start"
1538 * transaction (cmd=6) that must be 32 bytes, "middle"
1539 * transaction (cmd=7) that must be 32 bytes, and an "end"
1540 * transaction. The "end" transaction is shown as cmd=7 in
1541 * the text, but if that's the case there is no way to
1542 * differentiate between a middle and end part except the
1543 * length being less than 32. But there is a table at the far
1544 * end of the section (that I had never noticed until someone
1545 * pointed it out to me) that mentions it as cmd=8.
1547 * After some thought, I think the example is wrong and the
1548 * end transaction should be cmd=8. But some systems don't
1549 * implement cmd=8, they use a zero-length end transaction,
1550 * even though that violates the SMBus specification.
1552 * So, to work around this, this code tests if cmd=8 works.
1553 * If it does, then we use that. If not, it tests zero-
1554 * byte end transactions. If that works, good. If not,
1555 * we only allow 63-byte transactions max.
1558 ret
= start_multipart_test(client
, msg
, do_middle
);
1560 goto out_no_multi_part
;
1562 ret
= i2c_smbus_write_block_data(client
,
1563 SSIF_IPMI_MULTI_PART_REQUEST_END
,
1567 ret
= read_response(client
, resp
);
1570 /* End transactions work, we are good. */
1571 ssif_info
->cmd8_works
= true;
1575 ret
= start_multipart_test(client
, msg
, do_middle
);
1577 dev_err(&client
->dev
, "Second multipart test failed.\n");
1578 goto out_no_multi_part
;
1581 ret
= i2c_smbus_write_block_data(client
,
1582 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
,
1585 ret
= read_response(client
, resp
);
1587 /* Zero-size end parts work, use those. */
1590 /* Limit to 63 bytes and use a short middle command to mark the end. */
1591 if (ssif_info
->max_xmit_msg_size
> 63)
1592 ssif_info
->max_xmit_msg_size
= 63;
1596 ssif_info
->max_xmit_msg_size
= 32;
1601 * Global enables we care about.
1603 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1604 IPMI_BMC_EVT_MSG_INTR)
1606 static int ssif_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1608 unsigned char msg
[3];
1609 unsigned char *resp
;
1610 struct ssif_info
*ssif_info
;
1615 struct ssif_addr_info
*addr_info
= NULL
;
1617 resp
= kmalloc(IPMI_MAX_MSG_LENGTH
, GFP_KERNEL
);
1621 ssif_info
= kzalloc(sizeof(*ssif_info
), GFP_KERNEL
);
1627 if (!check_acpi(ssif_info
, &client
->dev
)) {
1628 addr_info
= ssif_info_find(client
->addr
, client
->adapter
->name
,
1631 /* Must have come in through sysfs. */
1632 ssif_info
->addr_source
= SI_HOTMOD
;
1634 ssif_info
->addr_source
= addr_info
->addr_src
;
1635 ssif_info
->ssif_debug
= addr_info
->debug
;
1636 ssif_info
->addr_info
= addr_info
->addr_info
;
1637 addr_info
->client
= client
;
1638 slave_addr
= addr_info
->slave_addr
;
1642 slave_addr
= find_slave_address(client
, slave_addr
);
1644 dev_info(&client
->dev
,
1645 "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1646 ipmi_addr_src_to_str(ssif_info
->addr_source
),
1647 client
->addr
, client
->adapter
->name
, slave_addr
);
1649 ssif_info
->client
= client
;
1650 i2c_set_clientdata(client
, ssif_info
);
1652 /* Now check for system interface capabilities */
1653 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1654 msg
[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD
;
1655 msg
[2] = 0; /* SSIF */
1656 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1657 if (!rv
&& (len
>= 3) && (resp
[2] == 0)) {
1660 dev_dbg(&ssif_info
->client
->dev
,
1661 "SSIF info too short: %d\n", len
);
1665 /* Got a good SSIF response, handle it. */
1666 ssif_info
->max_xmit_msg_size
= resp
[5];
1667 ssif_info
->max_recv_msg_size
= resp
[6];
1668 ssif_info
->multi_support
= (resp
[4] >> 6) & 0x3;
1669 ssif_info
->supports_pec
= (resp
[4] >> 3) & 0x1;
1671 /* Sanitize the data */
1672 switch (ssif_info
->multi_support
) {
1674 if (ssif_info
->max_xmit_msg_size
> 32)
1675 ssif_info
->max_xmit_msg_size
= 32;
1676 if (ssif_info
->max_recv_msg_size
> 32)
1677 ssif_info
->max_recv_msg_size
= 32;
1680 case SSIF_MULTI_2_PART
:
1681 if (ssif_info
->max_xmit_msg_size
> 63)
1682 ssif_info
->max_xmit_msg_size
= 63;
1683 if (ssif_info
->max_recv_msg_size
> 62)
1684 ssif_info
->max_recv_msg_size
= 62;
1687 case SSIF_MULTI_n_PART
:
1688 /* We take whatever size given, but do some testing. */
1692 /* Data is not sane, just give up. */
1697 /* Assume no multi-part or PEC support */
1698 dev_info(&ssif_info
->client
->dev
,
1699 "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1702 ssif_info
->max_xmit_msg_size
= 32;
1703 ssif_info
->max_recv_msg_size
= 32;
1704 ssif_info
->multi_support
= SSIF_NO_MULTI
;
1705 ssif_info
->supports_pec
= 0;
1708 test_multipart_messages(client
, ssif_info
, resp
);
1710 /* Make sure the NMI timeout is cleared. */
1711 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1712 msg
[1] = IPMI_CLEAR_MSG_FLAGS_CMD
;
1713 msg
[2] = WDT_PRE_TIMEOUT_INT
;
1714 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1715 if (rv
|| (len
< 3) || (resp
[2] != 0))
1716 dev_warn(&ssif_info
->client
->dev
,
1717 "Unable to clear message flags: %d %d %2.2x\n",
1720 /* Attempt to enable the event buffer. */
1721 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1722 msg
[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD
;
1723 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1724 if (rv
|| (len
< 4) || (resp
[2] != 0)) {
1725 dev_warn(&ssif_info
->client
->dev
,
1726 "Error getting global enables: %d %d %2.2x\n",
1728 rv
= 0; /* Not fatal */
1732 ssif_info
->global_enables
= resp
[3];
1734 if (resp
[3] & IPMI_BMC_EVT_MSG_BUFF
) {
1735 ssif_info
->has_event_buffer
= true;
1736 /* buffer is already enabled, nothing to do. */
1740 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1741 msg
[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD
;
1742 msg
[2] = ssif_info
->global_enables
| IPMI_BMC_EVT_MSG_BUFF
;
1743 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1744 if (rv
|| (len
< 2)) {
1745 dev_warn(&ssif_info
->client
->dev
,
1746 "Error setting global enables: %d %d %2.2x\n",
1748 rv
= 0; /* Not fatal */
1753 /* A successful return means the event buffer is supported. */
1754 ssif_info
->has_event_buffer
= true;
1755 ssif_info
->global_enables
|= IPMI_BMC_EVT_MSG_BUFF
;
1758 /* Some systems don't behave well if you enable alerts. */
1762 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1763 msg
[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD
;
1764 msg
[2] = ssif_info
->global_enables
| IPMI_BMC_RCV_MSG_INTR
;
1765 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1766 if (rv
|| (len
< 2)) {
1767 dev_warn(&ssif_info
->client
->dev
,
1768 "Error setting global enables: %d %d %2.2x\n",
1770 rv
= 0; /* Not fatal */
1775 /* A successful return means the alert is supported. */
1776 ssif_info
->supports_alert
= true;
1777 ssif_info
->global_enables
|= IPMI_BMC_RCV_MSG_INTR
;
1781 if (ssif_dbg_probe
) {
1782 dev_dbg(&ssif_info
->client
->dev
,
1783 "%s: i2c_probe found device at i2c address %x\n",
1784 __func__
, client
->addr
);
1787 spin_lock_init(&ssif_info
->lock
);
1788 ssif_info
->ssif_state
= SSIF_NORMAL
;
1789 timer_setup(&ssif_info
->retry_timer
, retry_timeout
, 0);
1790 timer_setup(&ssif_info
->watch_timer
, watch_timeout
, 0);
1792 for (i
= 0; i
< SSIF_NUM_STATS
; i
++)
1793 atomic_set(&ssif_info
->stats
[i
], 0);
1795 if (ssif_info
->supports_pec
)
1796 ssif_info
->client
->flags
|= I2C_CLIENT_PEC
;
1798 ssif_info
->handlers
.owner
= THIS_MODULE
;
1799 ssif_info
->handlers
.start_processing
= ssif_start_processing
;
1800 ssif_info
->handlers
.shutdown
= shutdown_ssif
;
1801 ssif_info
->handlers
.get_smi_info
= get_smi_info
;
1802 ssif_info
->handlers
.sender
= sender
;
1803 ssif_info
->handlers
.request_events
= request_events
;
1804 ssif_info
->handlers
.set_need_watch
= ssif_set_need_watch
;
1807 unsigned int thread_num
;
1809 thread_num
= ((i2c_adapter_id(ssif_info
->client
->adapter
)
1811 ssif_info
->client
->addr
);
1812 init_completion(&ssif_info
->wake_thread
);
1813 ssif_info
->thread
= kthread_run(ipmi_ssif_thread
, ssif_info
,
1814 "kssif%4.4x", thread_num
);
1815 if (IS_ERR(ssif_info
->thread
)) {
1816 rv
= PTR_ERR(ssif_info
->thread
);
1817 dev_notice(&ssif_info
->client
->dev
,
1818 "Could not start kernel thread: error %d\n",
1824 dev_set_drvdata(&ssif_info
->client
->dev
, ssif_info
);
1825 rv
= device_add_group(&ssif_info
->client
->dev
,
1826 &ipmi_ssif_dev_attr_group
);
1828 dev_err(&ssif_info
->client
->dev
,
1829 "Unable to add device attributes: error %d\n",
1834 rv
= ipmi_register_smi(&ssif_info
->handlers
,
1836 &ssif_info
->client
->dev
,
1839 dev_err(&ssif_info
->client
->dev
,
1840 "Unable to register device: error %d\n", rv
);
1841 goto out_remove_attr
;
1847 addr_info
->client
= NULL
;
1849 dev_err(&ssif_info
->client
->dev
,
1850 "Unable to start IPMI SSIF: %d\n", rv
);
1857 device_remove_group(&ssif_info
->client
->dev
, &ipmi_ssif_dev_attr_group
);
1858 dev_set_drvdata(&ssif_info
->client
->dev
, NULL
);
1862 static int ssif_adapter_handler(struct device
*adev
, void *opaque
)
1864 struct ssif_addr_info
*addr_info
= opaque
;
1866 if (adev
->type
!= &i2c_adapter_type
)
1869 addr_info
->added_client
= i2c_new_device(to_i2c_adapter(adev
),
1872 if (!addr_info
->adapter_name
)
1873 return 1; /* Only try the first I2C adapter by default. */
1877 static int new_ssif_client(int addr
, char *adapter_name
,
1878 int debug
, int slave_addr
,
1879 enum ipmi_addr_src addr_src
,
1882 struct ssif_addr_info
*addr_info
;
1885 mutex_lock(&ssif_infos_mutex
);
1886 if (ssif_info_find(addr
, adapter_name
, false)) {
1891 addr_info
= kzalloc(sizeof(*addr_info
), GFP_KERNEL
);
1898 addr_info
->adapter_name
= kstrdup(adapter_name
, GFP_KERNEL
);
1899 if (!addr_info
->adapter_name
) {
1906 strncpy(addr_info
->binfo
.type
, DEVICE_NAME
,
1907 sizeof(addr_info
->binfo
.type
));
1908 addr_info
->binfo
.addr
= addr
;
1909 addr_info
->binfo
.platform_data
= addr_info
;
1910 addr_info
->debug
= debug
;
1911 addr_info
->slave_addr
= slave_addr
;
1912 addr_info
->addr_src
= addr_src
;
1913 addr_info
->dev
= dev
;
1916 dev_set_drvdata(dev
, addr_info
);
1918 list_add_tail(&addr_info
->link
, &ssif_infos
);
1921 i2c_for_each_dev(addr_info
, ssif_adapter_handler
);
1922 /* Otherwise address list will get it */
1925 mutex_unlock(&ssif_infos_mutex
);
1929 static void free_ssif_clients(void)
1931 struct ssif_addr_info
*info
, *tmp
;
1933 mutex_lock(&ssif_infos_mutex
);
1934 list_for_each_entry_safe(info
, tmp
, &ssif_infos
, link
) {
1935 list_del(&info
->link
);
1936 kfree(info
->adapter_name
);
1939 mutex_unlock(&ssif_infos_mutex
);
1942 static unsigned short *ssif_address_list(void)
1944 struct ssif_addr_info
*info
;
1945 unsigned int count
= 0, i
= 0;
1946 unsigned short *address_list
;
1948 list_for_each_entry(info
, &ssif_infos
, link
)
1951 address_list
= kcalloc(count
+ 1, sizeof(*address_list
),
1956 list_for_each_entry(info
, &ssif_infos
, link
) {
1957 unsigned short addr
= info
->binfo
.addr
;
1960 for (j
= 0; j
< i
; j
++) {
1961 if (address_list
[j
] == addr
)
1965 if (j
== i
) /* Didn't find it in the list. */
1966 address_list
[i
++] = addr
;
1968 address_list
[i
] = I2C_CLIENT_END
;
1970 return address_list
;
1974 static const struct acpi_device_id ssif_acpi_match
[] = {
1978 MODULE_DEVICE_TABLE(acpi
, ssif_acpi_match
);
1982 static int dmi_ipmi_probe(struct platform_device
*pdev
)
1991 rv
= device_property_read_u16(&pdev
->dev
, "i2c-addr", &i2c_addr
);
1993 dev_warn(&pdev
->dev
, "No i2c-addr property\n");
1997 rv
= device_property_read_u8(&pdev
->dev
, "slave-addr", &slave_addr
);
2001 return new_ssif_client(i2c_addr
, NULL
, 0,
2002 slave_addr
, SI_SMBIOS
, &pdev
->dev
);
2005 static int dmi_ipmi_probe(struct platform_device
*pdev
)
2011 static const struct i2c_device_id ssif_id
[] = {
2015 MODULE_DEVICE_TABLE(i2c
, ssif_id
);
2017 static struct i2c_driver ssif_i2c_driver
= {
2018 .class = I2C_CLASS_HWMON
,
2022 .probe
= ssif_probe
,
2023 .remove
= ssif_remove
,
2024 .alert
= ssif_alert
,
2025 .id_table
= ssif_id
,
2026 .detect
= ssif_detect
2029 static int ssif_platform_probe(struct platform_device
*dev
)
2031 return dmi_ipmi_probe(dev
);
2034 static int ssif_platform_remove(struct platform_device
*dev
)
2036 struct ssif_addr_info
*addr_info
= dev_get_drvdata(&dev
->dev
);
2041 mutex_lock(&ssif_infos_mutex
);
2042 i2c_unregister_device(addr_info
->added_client
);
2044 list_del(&addr_info
->link
);
2046 mutex_unlock(&ssif_infos_mutex
);
2050 static const struct platform_device_id ssif_plat_ids
[] = {
2051 { "dmi-ipmi-ssif", 0 },
2055 static struct platform_driver ipmi_driver
= {
2057 .name
= DEVICE_NAME
,
2059 .probe
= ssif_platform_probe
,
2060 .remove
= ssif_platform_remove
,
2061 .id_table
= ssif_plat_ids
2064 static int init_ipmi_ssif(void)
2072 pr_info("IPMI SSIF Interface driver\n");
2074 /* build list for i2c from addr list */
2075 for (i
= 0; i
< num_addrs
; i
++) {
2076 rv
= new_ssif_client(addr
[i
], adapter_name
[i
],
2077 dbg
[i
], slave_addrs
[i
],
2078 SI_HARDCODED
, NULL
);
2080 pr_err("Couldn't add hardcoded device at addr 0x%x\n",
2085 ssif_i2c_driver
.driver
.acpi_match_table
=
2086 ACPI_PTR(ssif_acpi_match
);
2089 rv
= platform_driver_register(&ipmi_driver
);
2091 pr_err("Unable to register driver: %d\n", rv
);
2093 platform_registered
= true;
2096 ssif_i2c_driver
.address_list
= ssif_address_list();
2098 rv
= i2c_add_driver(&ssif_i2c_driver
);
2104 module_init(init_ipmi_ssif
);
2106 static void cleanup_ipmi_ssif(void)
2111 initialized
= false;
2113 i2c_del_driver(&ssif_i2c_driver
);
2115 kfree(ssif_i2c_driver
.address_list
);
2117 if (ssif_trydmi
&& platform_registered
)
2118 platform_driver_unregister(&ipmi_driver
);
2120 free_ssif_clients();
2122 module_exit(cleanup_ipmi_ssif
);
2124 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2125 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2126 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2127 MODULE_LICENSE("GPL");