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>
57 #define DEVICE_NAME "ipmi_ssif"
59 #define IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD 0x57
61 #define SSIF_IPMI_REQUEST 2
62 #define SSIF_IPMI_MULTI_PART_REQUEST_START 6
63 #define SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE 7
64 #define SSIF_IPMI_MULTI_PART_REQUEST_END 8
65 #define SSIF_IPMI_RESPONSE 3
66 #define SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE 9
68 /* ssif_debug is a bit-field
69 * SSIF_DEBUG_MSG - commands and their responses
70 * SSIF_DEBUG_STATES - message states
71 * SSIF_DEBUG_TIMING - Measure times between events in the driver
73 #define SSIF_DEBUG_TIMING 4
74 #define SSIF_DEBUG_STATE 2
75 #define SSIF_DEBUG_MSG 1
76 #define SSIF_NODEBUG 0
77 #define SSIF_DEFAULT_DEBUG (SSIF_NODEBUG)
82 #define SSIF_MSG_USEC 20000 /* 20ms between message tries. */
83 #define SSIF_MSG_PART_USEC 5000 /* 5ms for a message part */
85 /* How many times to we retry sending/receiving the message. */
86 #define SSIF_SEND_RETRIES 5
87 #define SSIF_RECV_RETRIES 250
89 #define SSIF_MSG_MSEC (SSIF_MSG_USEC / 1000)
90 #define SSIF_MSG_JIFFIES ((SSIF_MSG_USEC * 1000) / TICK_NSEC)
91 #define SSIF_MSG_PART_JIFFIES ((SSIF_MSG_PART_USEC * 1000) / TICK_NSEC)
94 * Timeout for the watch, only used for get flag timer.
96 #define SSIF_WATCH_MSG_TIMEOUT msecs_to_jiffies(10)
97 #define SSIF_WATCH_WATCHDOG_TIMEOUT msecs_to_jiffies(250)
99 enum ssif_intf_state
{
104 SSIF_GETTING_MESSAGES
,
105 /* FIXME - add watchdog stuff. */
108 #define SSIF_IDLE(ssif) ((ssif)->ssif_state == SSIF_NORMAL \
109 && (ssif)->curr_msg == NULL)
112 * Indexes into stats[] in ssif_info below.
114 enum ssif_stat_indexes
{
115 /* Number of total messages sent. */
116 SSIF_STAT_sent_messages
= 0,
119 * Number of message parts sent. Messages may be broken into
120 * parts if they are long.
122 SSIF_STAT_sent_messages_parts
,
125 * Number of time a message was retried.
127 SSIF_STAT_send_retries
,
130 * Number of times the send of a message failed.
132 SSIF_STAT_send_errors
,
135 * Number of message responses received.
137 SSIF_STAT_received_messages
,
140 * Number of message fragments received.
142 SSIF_STAT_received_message_parts
,
145 * Number of times the receive of a message was retried.
147 SSIF_STAT_receive_retries
,
150 * Number of errors receiving messages.
152 SSIF_STAT_receive_errors
,
155 * Number of times a flag fetch was requested.
157 SSIF_STAT_flag_fetches
,
160 * Number of times the hardware didn't follow the state machine.
165 * Number of received events.
169 /* Number of asyncronous messages received. */
170 SSIF_STAT_incoming_messages
,
172 /* Number of watchdog pretimeouts. */
173 SSIF_STAT_watchdog_pretimeouts
,
175 /* Number of alers received. */
178 /* Always add statistics before this value, it must be last. */
182 struct ssif_addr_info
{
183 struct i2c_board_info binfo
;
187 enum ipmi_addr_src addr_src
;
188 union ipmi_smi_info_union addr_info
;
190 struct i2c_client
*client
;
192 struct i2c_client
*added_client
;
194 struct mutex clients_mutex
;
195 struct list_head clients
;
197 struct list_head link
;
202 typedef void (*ssif_i2c_done
)(struct ssif_info
*ssif_info
, int result
,
203 unsigned char *data
, unsigned int len
);
206 struct ipmi_smi
*intf
;
208 struct ipmi_smi_msg
*waiting_msg
;
209 struct ipmi_smi_msg
*curr_msg
;
210 enum ssif_intf_state ssif_state
;
211 unsigned long ssif_debug
;
213 struct ipmi_smi_handlers handlers
;
215 enum ipmi_addr_src addr_source
; /* ACPI, PCI, SMBIOS, hardcode, etc. */
216 union ipmi_smi_info_union addr_info
;
219 * Flags from the last GET_MSG_FLAGS command, used when an ATTN
220 * is set to hold the flags until we are done handling everything
223 #define RECEIVE_MSG_AVAIL 0x01
224 #define EVENT_MSG_BUFFER_FULL 0x02
225 #define WDT_PRE_TIMEOUT_INT 0x08
226 unsigned char msg_flags
;
229 bool has_event_buffer
;
233 * Used to tell what we should do with alerts. If we are
234 * waiting on a response, read the data immediately.
240 * If set to true, this will request events the next time the
241 * state machine is idle.
246 * If set to true, this will request flags the next time the
247 * state machine is idle.
252 * Used to perform timer operations when run-to-completion
253 * mode is on. This is a countdown timer.
257 /* Used for sending/receiving data. +1 for the length. */
258 unsigned char data
[IPMI_MAX_MSG_LENGTH
+ 1];
259 unsigned int data_len
;
261 /* Temp receive buffer, gets copied into data. */
262 unsigned char recv
[I2C_SMBUS_BLOCK_MAX
];
264 struct i2c_client
*client
;
265 ssif_i2c_done done_handler
;
267 /* Thread interface handling */
268 struct task_struct
*thread
;
269 struct completion wake_thread
;
273 unsigned char *i2c_data
;
274 unsigned int i2c_size
;
276 struct timer_list retry_timer
;
279 long watch_timeout
; /* Timeout for flags check, 0 if off. */
280 struct timer_list watch_timer
; /* Flag fetch timer. */
282 /* Info from SSIF cmd */
283 unsigned char max_xmit_msg_size
;
284 unsigned char max_recv_msg_size
;
285 bool cmd8_works
; /* See test_multipart_messages() for details. */
286 unsigned int multi_support
;
289 #define SSIF_NO_MULTI 0
290 #define SSIF_MULTI_2_PART 1
291 #define SSIF_MULTI_n_PART 2
292 unsigned char *multi_data
;
293 unsigned int multi_len
;
294 unsigned int multi_pos
;
296 atomic_t stats
[SSIF_NUM_STATS
];
299 #define ssif_inc_stat(ssif, stat) \
300 atomic_inc(&(ssif)->stats[SSIF_STAT_ ## stat])
301 #define ssif_get_stat(ssif, stat) \
302 ((unsigned int) atomic_read(&(ssif)->stats[SSIF_STAT_ ## stat]))
304 static bool initialized
;
305 static bool platform_registered
;
307 static void return_hosed_msg(struct ssif_info
*ssif_info
,
308 struct ipmi_smi_msg
*msg
);
309 static void start_next_msg(struct ssif_info
*ssif_info
, unsigned long *flags
);
310 static int start_send(struct ssif_info
*ssif_info
,
314 static unsigned long *ipmi_ssif_lock_cond(struct ssif_info
*ssif_info
,
315 unsigned long *flags
)
316 __acquires(&ssif_info
->lock
)
318 spin_lock_irqsave(&ssif_info
->lock
, *flags
);
322 static void ipmi_ssif_unlock_cond(struct ssif_info
*ssif_info
,
323 unsigned long *flags
)
324 __releases(&ssif_info
->lock
)
326 spin_unlock_irqrestore(&ssif_info
->lock
, *flags
);
329 static void deliver_recv_msg(struct ssif_info
*ssif_info
,
330 struct ipmi_smi_msg
*msg
)
332 if (msg
->rsp_size
< 0) {
333 return_hosed_msg(ssif_info
, msg
);
334 dev_err(&ssif_info
->client
->dev
,
335 "%s: Malformed message: rsp_size = %d\n",
336 __func__
, msg
->rsp_size
);
338 ipmi_smi_msg_received(ssif_info
->intf
, msg
);
342 static void return_hosed_msg(struct ssif_info
*ssif_info
,
343 struct ipmi_smi_msg
*msg
)
345 ssif_inc_stat(ssif_info
, hosed
);
347 /* Make it a response */
348 msg
->rsp
[0] = msg
->data
[0] | 4;
349 msg
->rsp
[1] = msg
->data
[1];
350 msg
->rsp
[2] = 0xFF; /* Unknown error. */
353 deliver_recv_msg(ssif_info
, msg
);
357 * Must be called with the message lock held. This will release the
358 * message lock. Note that the caller will check SSIF_IDLE and start a
359 * new operation, so there is no need to check for new messages to
362 static void start_clear_flags(struct ssif_info
*ssif_info
, unsigned long *flags
)
364 unsigned char msg
[3];
366 ssif_info
->msg_flags
&= ~WDT_PRE_TIMEOUT_INT
;
367 ssif_info
->ssif_state
= SSIF_CLEARING_FLAGS
;
368 ipmi_ssif_unlock_cond(ssif_info
, flags
);
370 /* Make sure the watchdog pre-timeout flag is not set at startup. */
371 msg
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
372 msg
[1] = IPMI_CLEAR_MSG_FLAGS_CMD
;
373 msg
[2] = WDT_PRE_TIMEOUT_INT
;
375 if (start_send(ssif_info
, msg
, 3) != 0) {
376 /* Error, just go to normal state. */
377 ssif_info
->ssif_state
= SSIF_NORMAL
;
381 static void start_flag_fetch(struct ssif_info
*ssif_info
, unsigned long *flags
)
385 ssif_info
->req_flags
= false;
386 ssif_info
->ssif_state
= SSIF_GETTING_FLAGS
;
387 ipmi_ssif_unlock_cond(ssif_info
, flags
);
389 mb
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
390 mb
[1] = IPMI_GET_MSG_FLAGS_CMD
;
391 if (start_send(ssif_info
, mb
, 2) != 0)
392 ssif_info
->ssif_state
= SSIF_NORMAL
;
395 static void check_start_send(struct ssif_info
*ssif_info
, unsigned long *flags
,
396 struct ipmi_smi_msg
*msg
)
398 if (start_send(ssif_info
, msg
->data
, msg
->data_size
) != 0) {
399 unsigned long oflags
;
401 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
402 ssif_info
->curr_msg
= NULL
;
403 ssif_info
->ssif_state
= SSIF_NORMAL
;
404 ipmi_ssif_unlock_cond(ssif_info
, flags
);
405 ipmi_free_smi_msg(msg
);
409 static void start_event_fetch(struct ssif_info
*ssif_info
, unsigned long *flags
)
411 struct ipmi_smi_msg
*msg
;
413 ssif_info
->req_events
= false;
415 msg
= ipmi_alloc_smi_msg();
417 ssif_info
->ssif_state
= SSIF_NORMAL
;
418 ipmi_ssif_unlock_cond(ssif_info
, flags
);
422 ssif_info
->curr_msg
= msg
;
423 ssif_info
->ssif_state
= SSIF_GETTING_EVENTS
;
424 ipmi_ssif_unlock_cond(ssif_info
, flags
);
426 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
427 msg
->data
[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD
;
430 check_start_send(ssif_info
, flags
, msg
);
433 static void start_recv_msg_fetch(struct ssif_info
*ssif_info
,
434 unsigned long *flags
)
436 struct ipmi_smi_msg
*msg
;
438 msg
= ipmi_alloc_smi_msg();
440 ssif_info
->ssif_state
= SSIF_NORMAL
;
441 ipmi_ssif_unlock_cond(ssif_info
, flags
);
445 ssif_info
->curr_msg
= msg
;
446 ssif_info
->ssif_state
= SSIF_GETTING_MESSAGES
;
447 ipmi_ssif_unlock_cond(ssif_info
, flags
);
449 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
450 msg
->data
[1] = IPMI_GET_MSG_CMD
;
453 check_start_send(ssif_info
, flags
, msg
);
457 * Must be called with the message lock held. This will release the
458 * message lock. Note that the caller will check SSIF_IDLE and start a
459 * new operation, so there is no need to check for new messages to
462 static void handle_flags(struct ssif_info
*ssif_info
, unsigned long *flags
)
464 if (ssif_info
->msg_flags
& WDT_PRE_TIMEOUT_INT
) {
465 /* Watchdog pre-timeout */
466 ssif_inc_stat(ssif_info
, watchdog_pretimeouts
);
467 start_clear_flags(ssif_info
, flags
);
468 ipmi_smi_watchdog_pretimeout(ssif_info
->intf
);
469 } else if (ssif_info
->msg_flags
& RECEIVE_MSG_AVAIL
)
470 /* Messages available. */
471 start_recv_msg_fetch(ssif_info
, flags
);
472 else if (ssif_info
->msg_flags
& EVENT_MSG_BUFFER_FULL
)
473 /* Events available. */
474 start_event_fetch(ssif_info
, flags
);
476 ssif_info
->ssif_state
= SSIF_NORMAL
;
477 ipmi_ssif_unlock_cond(ssif_info
, flags
);
481 static int ipmi_ssif_thread(void *data
)
483 struct ssif_info
*ssif_info
= data
;
485 while (!kthread_should_stop()) {
488 /* Wait for something to do */
489 result
= wait_for_completion_interruptible(
490 &ssif_info
->wake_thread
);
491 if (ssif_info
->stopping
)
493 if (result
== -ERESTARTSYS
)
495 init_completion(&ssif_info
->wake_thread
);
497 if (ssif_info
->i2c_read_write
== I2C_SMBUS_WRITE
) {
498 result
= i2c_smbus_write_block_data(
499 ssif_info
->client
, ssif_info
->i2c_command
,
500 ssif_info
->i2c_data
[0],
501 ssif_info
->i2c_data
+ 1);
502 ssif_info
->done_handler(ssif_info
, result
, NULL
, 0);
504 result
= i2c_smbus_read_block_data(
505 ssif_info
->client
, ssif_info
->i2c_command
,
506 ssif_info
->i2c_data
);
508 ssif_info
->done_handler(ssif_info
, result
,
511 ssif_info
->done_handler(ssif_info
, 0,
520 static int ssif_i2c_send(struct ssif_info
*ssif_info
,
521 ssif_i2c_done handler
,
522 int read_write
, int command
,
523 unsigned char *data
, unsigned int size
)
525 ssif_info
->done_handler
= handler
;
527 ssif_info
->i2c_read_write
= read_write
;
528 ssif_info
->i2c_command
= command
;
529 ssif_info
->i2c_data
= data
;
530 ssif_info
->i2c_size
= size
;
531 complete(&ssif_info
->wake_thread
);
536 static void msg_done_handler(struct ssif_info
*ssif_info
, int result
,
537 unsigned char *data
, unsigned int len
);
539 static void start_get(struct ssif_info
*ssif_info
)
543 ssif_info
->rtc_us_timer
= 0;
544 ssif_info
->multi_pos
= 0;
546 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
, I2C_SMBUS_READ
,
548 ssif_info
->recv
, I2C_SMBUS_BLOCK_DATA
);
550 /* request failed, just return the error. */
551 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
552 dev_dbg(&ssif_info
->client
->dev
,
553 "Error from i2c_non_blocking_op(5)\n");
555 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
559 static void retry_timeout(struct timer_list
*t
)
561 struct ssif_info
*ssif_info
= from_timer(ssif_info
, t
, retry_timer
);
562 unsigned long oflags
, *flags
;
565 if (ssif_info
->stopping
)
568 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
569 waiting
= ssif_info
->waiting_alert
;
570 ssif_info
->waiting_alert
= false;
571 ipmi_ssif_unlock_cond(ssif_info
, flags
);
574 start_get(ssif_info
);
577 static void watch_timeout(struct timer_list
*t
)
579 struct ssif_info
*ssif_info
= from_timer(ssif_info
, t
, watch_timer
);
580 unsigned long oflags
, *flags
;
582 if (ssif_info
->stopping
)
585 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
586 if (ssif_info
->watch_timeout
) {
587 mod_timer(&ssif_info
->watch_timer
,
588 jiffies
+ ssif_info
->watch_timeout
);
589 if (SSIF_IDLE(ssif_info
)) {
590 start_flag_fetch(ssif_info
, flags
); /* Releases lock */
593 ssif_info
->req_flags
= true;
595 ipmi_ssif_unlock_cond(ssif_info
, flags
);
598 static void ssif_alert(struct i2c_client
*client
, enum i2c_alert_protocol type
,
601 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
602 unsigned long oflags
, *flags
;
605 if (type
!= I2C_PROTOCOL_SMBUS_ALERT
)
608 ssif_inc_stat(ssif_info
, alerts
);
610 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
611 if (ssif_info
->waiting_alert
) {
612 ssif_info
->waiting_alert
= false;
613 del_timer(&ssif_info
->retry_timer
);
615 } else if (ssif_info
->curr_msg
) {
616 ssif_info
->got_alert
= true;
618 ipmi_ssif_unlock_cond(ssif_info
, flags
);
620 start_get(ssif_info
);
623 static int start_resend(struct ssif_info
*ssif_info
);
625 static void msg_done_handler(struct ssif_info
*ssif_info
, int result
,
626 unsigned char *data
, unsigned int len
)
628 struct ipmi_smi_msg
*msg
;
629 unsigned long oflags
, *flags
;
633 * We are single-threaded here, so no need for a lock until we
634 * start messing with driver states or the queues.
638 ssif_info
->retries_left
--;
639 if (ssif_info
->retries_left
> 0) {
640 ssif_inc_stat(ssif_info
, receive_retries
);
642 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
643 ssif_info
->waiting_alert
= true;
644 ssif_info
->rtc_us_timer
= SSIF_MSG_USEC
;
645 if (!ssif_info
->stopping
)
646 mod_timer(&ssif_info
->retry_timer
,
647 jiffies
+ SSIF_MSG_JIFFIES
);
648 ipmi_ssif_unlock_cond(ssif_info
, flags
);
652 ssif_inc_stat(ssif_info
, receive_errors
);
654 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
655 dev_dbg(&ssif_info
->client
->dev
,
656 "%s: Error %d\n", __func__
, result
);
661 if ((len
> 1) && (ssif_info
->multi_pos
== 0)
662 && (data
[0] == 0x00) && (data
[1] == 0x01)) {
663 /* Start of multi-part read. Start the next transaction. */
666 ssif_inc_stat(ssif_info
, received_message_parts
);
668 /* Remove the multi-part read marker. */
671 for (i
= 0; i
< len
; i
++)
672 ssif_info
->data
[i
] = data
[i
];
673 ssif_info
->multi_len
= len
;
674 ssif_info
->multi_pos
= 1;
676 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
, I2C_SMBUS_READ
,
677 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE
,
678 ssif_info
->recv
, I2C_SMBUS_BLOCK_DATA
);
680 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
681 dev_dbg(&ssif_info
->client
->dev
,
682 "Error from i2c_non_blocking_op(1)\n");
687 } else if (ssif_info
->multi_pos
) {
688 /* Middle of multi-part read. Start the next transaction. */
690 unsigned char blocknum
;
694 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
695 dev_dbg(&ssif_info
->client
->dev
,
696 "Middle message with no data\n");
705 if (blocknum
!= 0xff && len
!= 31) {
706 /* All blocks but the last must have 31 data bytes. */
708 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
709 dev_dbg(&ssif_info
->client
->dev
,
710 "Received middle message <31\n");
715 if (ssif_info
->multi_len
+ len
> IPMI_MAX_MSG_LENGTH
) {
716 /* Received message too big, abort the operation. */
718 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
719 dev_dbg(&ssif_info
->client
->dev
,
720 "Received message too big\n");
725 for (i
= 0; i
< len
; i
++)
726 ssif_info
->data
[i
+ ssif_info
->multi_len
] = data
[i
];
727 ssif_info
->multi_len
+= len
;
728 if (blocknum
== 0xff) {
730 len
= ssif_info
->multi_len
;
731 data
= ssif_info
->data
;
732 } else if (blocknum
+ 1 != ssif_info
->multi_pos
) {
734 * Out of sequence block, just abort. Block
735 * numbers start at zero for the second block,
736 * but multi_pos starts at one, so the +1.
738 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
739 dev_dbg(&ssif_info
->client
->dev
,
740 "Received message out of sequence, expected %u, got %u\n",
741 ssif_info
->multi_pos
- 1, blocknum
);
744 ssif_inc_stat(ssif_info
, received_message_parts
);
746 ssif_info
->multi_pos
++;
748 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
,
750 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE
,
752 I2C_SMBUS_BLOCK_DATA
);
754 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
755 dev_dbg(&ssif_info
->client
->dev
,
756 "Error from ssif_i2c_send\n");
766 ssif_inc_stat(ssif_info
, receive_errors
);
768 ssif_inc_stat(ssif_info
, received_messages
);
769 ssif_inc_stat(ssif_info
, received_message_parts
);
772 if (ssif_info
->ssif_debug
& SSIF_DEBUG_STATE
)
773 dev_dbg(&ssif_info
->client
->dev
,
774 "DONE 1: state = %d, result=%d\n",
775 ssif_info
->ssif_state
, result
);
777 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
778 msg
= ssif_info
->curr_msg
;
781 if (len
> IPMI_MAX_MSG_LENGTH
)
782 len
= IPMI_MAX_MSG_LENGTH
;
783 memcpy(msg
->rsp
, data
, len
);
788 ssif_info
->curr_msg
= NULL
;
791 switch (ssif_info
->ssif_state
) {
793 ipmi_ssif_unlock_cond(ssif_info
, flags
);
798 return_hosed_msg(ssif_info
, msg
);
800 deliver_recv_msg(ssif_info
, msg
);
803 case SSIF_GETTING_FLAGS
:
804 /* We got the flags from the SSIF, now handle them. */
805 if ((result
< 0) || (len
< 4) || (data
[2] != 0)) {
807 * Error fetching flags, or invalid length,
808 * just give up for now.
810 ssif_info
->ssif_state
= SSIF_NORMAL
;
811 ipmi_ssif_unlock_cond(ssif_info
, flags
);
812 dev_warn(&ssif_info
->client
->dev
,
813 "Error getting flags: %d %d, %x\n",
814 result
, len
, (len
>= 3) ? data
[2] : 0);
815 } else if (data
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
816 || data
[1] != IPMI_GET_MSG_FLAGS_CMD
) {
818 * Don't abort here, maybe it was a queued
819 * response to a previous command.
821 ipmi_ssif_unlock_cond(ssif_info
, flags
);
822 dev_warn(&ssif_info
->client
->dev
,
823 "Invalid response getting flags: %x %x\n",
826 ssif_inc_stat(ssif_info
, flag_fetches
);
827 ssif_info
->msg_flags
= data
[3];
828 handle_flags(ssif_info
, flags
);
832 case SSIF_CLEARING_FLAGS
:
833 /* We cleared the flags. */
834 if ((result
< 0) || (len
< 3) || (data
[2] != 0)) {
835 /* Error clearing flags */
836 dev_warn(&ssif_info
->client
->dev
,
837 "Error clearing flags: %d %d, %x\n",
838 result
, len
, (len
>= 3) ? data
[2] : 0);
839 } else if (data
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
840 || data
[1] != IPMI_CLEAR_MSG_FLAGS_CMD
) {
841 dev_warn(&ssif_info
->client
->dev
,
842 "Invalid response clearing flags: %x %x\n",
845 ssif_info
->ssif_state
= SSIF_NORMAL
;
846 ipmi_ssif_unlock_cond(ssif_info
, flags
);
849 case SSIF_GETTING_EVENTS
:
850 if ((result
< 0) || (len
< 3) || (msg
->rsp
[2] != 0)) {
851 /* Error getting event, probably done. */
854 /* Take off the event flag. */
855 ssif_info
->msg_flags
&= ~EVENT_MSG_BUFFER_FULL
;
856 handle_flags(ssif_info
, flags
);
857 } else if (msg
->rsp
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
858 || msg
->rsp
[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD
) {
859 dev_warn(&ssif_info
->client
->dev
,
860 "Invalid response getting events: %x %x\n",
861 msg
->rsp
[0], msg
->rsp
[1]);
863 /* Take off the event flag. */
864 ssif_info
->msg_flags
&= ~EVENT_MSG_BUFFER_FULL
;
865 handle_flags(ssif_info
, flags
);
867 handle_flags(ssif_info
, flags
);
868 ssif_inc_stat(ssif_info
, events
);
869 deliver_recv_msg(ssif_info
, msg
);
873 case SSIF_GETTING_MESSAGES
:
874 if ((result
< 0) || (len
< 3) || (msg
->rsp
[2] != 0)) {
875 /* Error getting event, probably done. */
878 /* Take off the msg flag. */
879 ssif_info
->msg_flags
&= ~RECEIVE_MSG_AVAIL
;
880 handle_flags(ssif_info
, flags
);
881 } else if (msg
->rsp
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
882 || msg
->rsp
[1] != IPMI_GET_MSG_CMD
) {
883 dev_warn(&ssif_info
->client
->dev
,
884 "Invalid response clearing flags: %x %x\n",
885 msg
->rsp
[0], msg
->rsp
[1]);
888 /* Take off the msg flag. */
889 ssif_info
->msg_flags
&= ~RECEIVE_MSG_AVAIL
;
890 handle_flags(ssif_info
, flags
);
892 ssif_inc_stat(ssif_info
, incoming_messages
);
893 handle_flags(ssif_info
, flags
);
894 deliver_recv_msg(ssif_info
, msg
);
899 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
900 if (SSIF_IDLE(ssif_info
) && !ssif_info
->stopping
) {
901 if (ssif_info
->req_events
)
902 start_event_fetch(ssif_info
, flags
);
903 else if (ssif_info
->req_flags
)
904 start_flag_fetch(ssif_info
, flags
);
906 start_next_msg(ssif_info
, flags
);
908 ipmi_ssif_unlock_cond(ssif_info
, flags
);
910 if (ssif_info
->ssif_debug
& SSIF_DEBUG_STATE
)
911 dev_dbg(&ssif_info
->client
->dev
,
912 "DONE 2: state = %d.\n", ssif_info
->ssif_state
);
915 static void msg_written_handler(struct ssif_info
*ssif_info
, int result
,
916 unsigned char *data
, unsigned int len
)
920 /* We are single-threaded here, so no need for a lock. */
922 ssif_info
->retries_left
--;
923 if (ssif_info
->retries_left
> 0) {
924 if (!start_resend(ssif_info
)) {
925 ssif_inc_stat(ssif_info
, send_retries
);
928 /* request failed, just return the error. */
929 ssif_inc_stat(ssif_info
, send_errors
);
931 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
932 dev_dbg(&ssif_info
->client
->dev
,
933 "%s: Out of retries\n", __func__
);
934 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
938 ssif_inc_stat(ssif_info
, send_errors
);
941 * Got an error on transmit, let the done routine
944 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
945 dev_dbg(&ssif_info
->client
->dev
,
946 "%s: Error %d\n", __func__
, result
);
948 msg_done_handler(ssif_info
, result
, NULL
, 0);
952 if (ssif_info
->multi_data
) {
954 * In the middle of a multi-data write. See the comment
955 * in the SSIF_MULTI_n_PART case in the probe function
956 * for details on the intricacies of this.
959 unsigned char *data_to_send
;
962 ssif_inc_stat(ssif_info
, sent_messages_parts
);
964 left
= ssif_info
->multi_len
- ssif_info
->multi_pos
;
969 ssif_info
->multi_data
[ssif_info
->multi_pos
] = to_write
;
970 data_to_send
= ssif_info
->multi_data
+ ssif_info
->multi_pos
;
971 ssif_info
->multi_pos
+= to_write
;
972 cmd
= SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
;
973 if (ssif_info
->cmd8_works
) {
974 if (left
== to_write
) {
975 cmd
= SSIF_IPMI_MULTI_PART_REQUEST_END
;
976 ssif_info
->multi_data
= NULL
;
978 } else if (to_write
< 32) {
979 ssif_info
->multi_data
= NULL
;
982 rv
= ssif_i2c_send(ssif_info
, msg_written_handler
,
983 I2C_SMBUS_WRITE
, cmd
,
984 data_to_send
, I2C_SMBUS_BLOCK_DATA
);
986 /* request failed, just return the error. */
987 ssif_inc_stat(ssif_info
, send_errors
);
989 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
990 dev_dbg(&ssif_info
->client
->dev
,
991 "Error from i2c_non_blocking_op(3)\n");
992 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
995 /* Ready to request the result. */
996 unsigned long oflags
, *flags
;
998 ssif_inc_stat(ssif_info
, sent_messages
);
999 ssif_inc_stat(ssif_info
, sent_messages_parts
);
1001 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1002 if (ssif_info
->got_alert
) {
1003 /* The result is already ready, just start it. */
1004 ssif_info
->got_alert
= false;
1005 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1006 start_get(ssif_info
);
1008 /* Wait a jiffie then request the next message */
1009 ssif_info
->waiting_alert
= true;
1010 ssif_info
->retries_left
= SSIF_RECV_RETRIES
;
1011 ssif_info
->rtc_us_timer
= SSIF_MSG_PART_USEC
;
1012 if (!ssif_info
->stopping
)
1013 mod_timer(&ssif_info
->retry_timer
,
1014 jiffies
+ SSIF_MSG_PART_JIFFIES
);
1015 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1020 static int start_resend(struct ssif_info
*ssif_info
)
1025 ssif_info
->got_alert
= false;
1027 if (ssif_info
->data_len
> 32) {
1028 command
= SSIF_IPMI_MULTI_PART_REQUEST_START
;
1029 ssif_info
->multi_data
= ssif_info
->data
;
1030 ssif_info
->multi_len
= ssif_info
->data_len
;
1032 * Subtle thing, this is 32, not 33, because we will
1033 * overwrite the thing at position 32 (which was just
1034 * transmitted) with the new length.
1036 ssif_info
->multi_pos
= 32;
1037 ssif_info
->data
[0] = 32;
1039 ssif_info
->multi_data
= NULL
;
1040 command
= SSIF_IPMI_REQUEST
;
1041 ssif_info
->data
[0] = ssif_info
->data_len
;
1044 rv
= ssif_i2c_send(ssif_info
, msg_written_handler
, I2C_SMBUS_WRITE
,
1045 command
, ssif_info
->data
, I2C_SMBUS_BLOCK_DATA
);
1046 if (rv
&& (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
))
1047 dev_dbg(&ssif_info
->client
->dev
,
1048 "Error from i2c_non_blocking_op(4)\n");
1052 static int start_send(struct ssif_info
*ssif_info
,
1053 unsigned char *data
,
1056 if (len
> IPMI_MAX_MSG_LENGTH
)
1058 if (len
> ssif_info
->max_xmit_msg_size
)
1061 ssif_info
->retries_left
= SSIF_SEND_RETRIES
;
1062 memcpy(ssif_info
->data
+ 1, data
, len
);
1063 ssif_info
->data_len
= len
;
1064 return start_resend(ssif_info
);
1067 /* Must be called with the message lock held. */
1068 static void start_next_msg(struct ssif_info
*ssif_info
, unsigned long *flags
)
1070 struct ipmi_smi_msg
*msg
;
1071 unsigned long oflags
;
1074 if (!SSIF_IDLE(ssif_info
)) {
1075 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1079 if (!ssif_info
->waiting_msg
) {
1080 ssif_info
->curr_msg
= NULL
;
1081 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1085 ssif_info
->curr_msg
= ssif_info
->waiting_msg
;
1086 ssif_info
->waiting_msg
= NULL
;
1087 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1088 rv
= start_send(ssif_info
,
1089 ssif_info
->curr_msg
->data
,
1090 ssif_info
->curr_msg
->data_size
);
1092 msg
= ssif_info
->curr_msg
;
1093 ssif_info
->curr_msg
= NULL
;
1094 return_hosed_msg(ssif_info
, msg
);
1095 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1101 static void sender(void *send_info
,
1102 struct ipmi_smi_msg
*msg
)
1104 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1105 unsigned long oflags
, *flags
;
1107 BUG_ON(ssif_info
->waiting_msg
);
1108 ssif_info
->waiting_msg
= msg
;
1110 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1111 start_next_msg(ssif_info
, flags
);
1113 if (ssif_info
->ssif_debug
& SSIF_DEBUG_TIMING
) {
1114 struct timespec64 t
;
1116 ktime_get_real_ts64(&t
);
1117 dev_dbg(&ssif_info
->client
->dev
,
1118 "**Enqueue %02x %02x: %lld.%6.6ld\n",
1119 msg
->data
[0], msg
->data
[1],
1120 (long long)t
.tv_sec
, (long)t
.tv_nsec
/ NSEC_PER_USEC
);
1124 static int get_smi_info(void *send_info
, struct ipmi_smi_info
*data
)
1126 struct ssif_info
*ssif_info
= send_info
;
1128 data
->addr_src
= ssif_info
->addr_source
;
1129 data
->dev
= &ssif_info
->client
->dev
;
1130 data
->addr_info
= ssif_info
->addr_info
;
1131 get_device(data
->dev
);
1137 * Upper layer wants us to request events.
1139 static void request_events(void *send_info
)
1141 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1142 unsigned long oflags
, *flags
;
1144 if (!ssif_info
->has_event_buffer
)
1147 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1148 ssif_info
->req_events
= true;
1149 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1153 * Upper layer is changing the flag saying whether we need to request
1154 * flags periodically or not.
1156 static void ssif_set_need_watch(void *send_info
, unsigned int watch_mask
)
1158 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1159 unsigned long oflags
, *flags
;
1162 if (watch_mask
& IPMI_WATCH_MASK_CHECK_MESSAGES
)
1163 timeout
= SSIF_WATCH_MSG_TIMEOUT
;
1164 else if (watch_mask
)
1165 timeout
= SSIF_WATCH_WATCHDOG_TIMEOUT
;
1167 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1168 if (timeout
!= ssif_info
->watch_timeout
) {
1169 ssif_info
->watch_timeout
= timeout
;
1170 if (ssif_info
->watch_timeout
)
1171 mod_timer(&ssif_info
->watch_timer
,
1172 jiffies
+ ssif_info
->watch_timeout
);
1174 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1177 static int ssif_start_processing(void *send_info
,
1178 struct ipmi_smi
*intf
)
1180 struct ssif_info
*ssif_info
= send_info
;
1182 ssif_info
->intf
= intf
;
1187 #define MAX_SSIF_BMCS 4
1189 static unsigned short addr
[MAX_SSIF_BMCS
];
1190 static int num_addrs
;
1191 module_param_array(addr
, ushort
, &num_addrs
, 0);
1192 MODULE_PARM_DESC(addr
, "The addresses to scan for IPMI BMCs on the SSIFs.");
1194 static char *adapter_name
[MAX_SSIF_BMCS
];
1195 static int num_adapter_names
;
1196 module_param_array(adapter_name
, charp
, &num_adapter_names
, 0);
1197 MODULE_PARM_DESC(adapter_name
, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1199 static int slave_addrs
[MAX_SSIF_BMCS
];
1200 static int num_slave_addrs
;
1201 module_param_array(slave_addrs
, int, &num_slave_addrs
, 0);
1202 MODULE_PARM_DESC(slave_addrs
,
1203 "The default IPMB slave address for the controller.");
1205 static bool alerts_broken
;
1206 module_param(alerts_broken
, bool, 0);
1207 MODULE_PARM_DESC(alerts_broken
, "Don't enable alerts for the controller.");
1210 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1211 * bit 2 enables timing debugging. This is an array indexed by
1214 static int dbg
[MAX_SSIF_BMCS
];
1216 module_param_array(dbg
, int, &num_dbg
, 0);
1217 MODULE_PARM_DESC(dbg
, "Turn on debugging.");
1219 static bool ssif_dbg_probe
;
1220 module_param_named(dbg_probe
, ssif_dbg_probe
, bool, 0);
1221 MODULE_PARM_DESC(dbg_probe
, "Enable debugging of probing of adapters.");
1223 static bool ssif_tryacpi
= true;
1224 module_param_named(tryacpi
, ssif_tryacpi
, bool, 0);
1225 MODULE_PARM_DESC(tryacpi
, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1227 static bool ssif_trydmi
= true;
1228 module_param_named(trydmi
, ssif_trydmi
, bool, 0);
1229 MODULE_PARM_DESC(trydmi
, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1231 static DEFINE_MUTEX(ssif_infos_mutex
);
1232 static LIST_HEAD(ssif_infos
);
1234 #define IPMI_SSIF_ATTR(name) \
1235 static ssize_t ipmi_##name##_show(struct device *dev, \
1236 struct device_attribute *attr, \
1239 struct ssif_info *ssif_info = dev_get_drvdata(dev); \
1241 return snprintf(buf, 10, "%u\n", ssif_get_stat(ssif_info, name));\
1243 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1245 static ssize_t
ipmi_type_show(struct device
*dev
,
1246 struct device_attribute
*attr
,
1249 return snprintf(buf
, 10, "ssif\n");
1251 static DEVICE_ATTR(type
, S_IRUGO
, ipmi_type_show
, NULL
);
1253 IPMI_SSIF_ATTR(sent_messages
);
1254 IPMI_SSIF_ATTR(sent_messages_parts
);
1255 IPMI_SSIF_ATTR(send_retries
);
1256 IPMI_SSIF_ATTR(send_errors
);
1257 IPMI_SSIF_ATTR(received_messages
);
1258 IPMI_SSIF_ATTR(received_message_parts
);
1259 IPMI_SSIF_ATTR(receive_retries
);
1260 IPMI_SSIF_ATTR(receive_errors
);
1261 IPMI_SSIF_ATTR(flag_fetches
);
1262 IPMI_SSIF_ATTR(hosed
);
1263 IPMI_SSIF_ATTR(events
);
1264 IPMI_SSIF_ATTR(watchdog_pretimeouts
);
1265 IPMI_SSIF_ATTR(alerts
);
1267 static struct attribute
*ipmi_ssif_dev_attrs
[] = {
1268 &dev_attr_type
.attr
,
1269 &dev_attr_sent_messages
.attr
,
1270 &dev_attr_sent_messages_parts
.attr
,
1271 &dev_attr_send_retries
.attr
,
1272 &dev_attr_send_errors
.attr
,
1273 &dev_attr_received_messages
.attr
,
1274 &dev_attr_received_message_parts
.attr
,
1275 &dev_attr_receive_retries
.attr
,
1276 &dev_attr_receive_errors
.attr
,
1277 &dev_attr_flag_fetches
.attr
,
1278 &dev_attr_hosed
.attr
,
1279 &dev_attr_events
.attr
,
1280 &dev_attr_watchdog_pretimeouts
.attr
,
1281 &dev_attr_alerts
.attr
,
1285 static const struct attribute_group ipmi_ssif_dev_attr_group
= {
1286 .attrs
= ipmi_ssif_dev_attrs
,
1289 static void shutdown_ssif(void *send_info
)
1291 struct ssif_info
*ssif_info
= send_info
;
1293 device_remove_group(&ssif_info
->client
->dev
, &ipmi_ssif_dev_attr_group
);
1294 dev_set_drvdata(&ssif_info
->client
->dev
, NULL
);
1296 /* make sure the driver is not looking for flags any more. */
1297 while (ssif_info
->ssif_state
!= SSIF_NORMAL
)
1298 schedule_timeout(1);
1300 ssif_info
->stopping
= true;
1301 del_timer_sync(&ssif_info
->watch_timer
);
1302 del_timer_sync(&ssif_info
->retry_timer
);
1303 if (ssif_info
->thread
) {
1304 complete(&ssif_info
->wake_thread
);
1305 kthread_stop(ssif_info
->thread
);
1309 static int ssif_remove(struct i2c_client
*client
)
1311 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
1312 struct ssif_addr_info
*addr_info
;
1318 * After this point, we won't deliver anything asychronously
1319 * to the message handler. We can unregister ourself.
1321 ipmi_unregister_smi(ssif_info
->intf
);
1323 list_for_each_entry(addr_info
, &ssif_infos
, link
) {
1324 if (addr_info
->client
== client
) {
1325 addr_info
->client
= NULL
;
1335 static int read_response(struct i2c_client
*client
, unsigned char *resp
)
1337 int ret
= -ENODEV
, retry_cnt
= SSIF_RECV_RETRIES
;
1339 while (retry_cnt
> 0) {
1340 ret
= i2c_smbus_read_block_data(client
, SSIF_IPMI_RESPONSE
,
1344 msleep(SSIF_MSG_MSEC
);
1353 static int do_cmd(struct i2c_client
*client
, int len
, unsigned char *msg
,
1354 int *resp_len
, unsigned char *resp
)
1359 retry_cnt
= SSIF_SEND_RETRIES
;
1361 ret
= i2c_smbus_write_block_data(client
, SSIF_IPMI_REQUEST
, len
, msg
);
1369 ret
= read_response(client
, resp
);
1371 /* Validate that the response is correct. */
1373 (resp
[0] != (msg
[0] | (1 << 2))) ||
1374 (resp
[1] != msg
[1]))
1376 else if (ret
> IPMI_MAX_MSG_LENGTH
) {
1387 static int ssif_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
1389 unsigned char *resp
;
1390 unsigned char msg
[3];
1394 resp
= kmalloc(IPMI_MAX_MSG_LENGTH
, GFP_KERNEL
);
1398 /* Do a Get Device ID command, since it is required. */
1399 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1400 msg
[1] = IPMI_GET_DEVICE_ID_CMD
;
1401 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1405 strlcpy(info
->type
, DEVICE_NAME
, I2C_NAME_SIZE
);
1410 static int strcmp_nospace(char *s1
, char *s2
)
1412 while (*s1
&& *s2
) {
1413 while (isspace(*s1
))
1415 while (isspace(*s2
))
1427 static struct ssif_addr_info
*ssif_info_find(unsigned short addr
,
1429 bool match_null_name
)
1431 struct ssif_addr_info
*info
, *found
= NULL
;
1434 list_for_each_entry(info
, &ssif_infos
, link
) {
1435 if (info
->binfo
.addr
== addr
) {
1436 if (info
->addr_src
== SI_SMBIOS
)
1437 info
->adapter_name
= kstrdup(adapter_name
,
1440 if (info
->adapter_name
|| adapter_name
) {
1441 if (!info
->adapter_name
!= !adapter_name
) {
1442 /* One is NULL and one is not */
1446 strcmp_nospace(info
->adapter_name
,
1448 /* Names do not match */
1456 if (!found
&& match_null_name
) {
1457 /* Try to get an exact match first, then try with a NULL name */
1458 adapter_name
= NULL
;
1459 match_null_name
= false;
1466 static bool check_acpi(struct ssif_info
*ssif_info
, struct device
*dev
)
1469 acpi_handle acpi_handle
;
1471 acpi_handle
= ACPI_HANDLE(dev
);
1473 ssif_info
->addr_source
= SI_ACPI
;
1474 ssif_info
->addr_info
.acpi_info
.acpi_handle
= acpi_handle
;
1481 static int find_slave_address(struct i2c_client
*client
, int slave_addr
)
1483 #ifdef CONFIG_IPMI_DMI_DECODE
1485 slave_addr
= ipmi_dmi_get_slave_addr(
1487 i2c_adapter_id(client
->adapter
),
1494 static int start_multipart_test(struct i2c_client
*client
,
1495 unsigned char *msg
, bool do_middle
)
1497 int retry_cnt
= SSIF_SEND_RETRIES
, ret
;
1500 ret
= i2c_smbus_write_block_data(client
,
1501 SSIF_IPMI_MULTI_PART_REQUEST_START
,
1507 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");
1514 ret
= i2c_smbus_write_block_data(client
,
1515 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
,
1518 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");
1525 static void test_multipart_messages(struct i2c_client
*client
,
1526 struct ssif_info
*ssif_info
,
1527 unsigned char *resp
)
1529 unsigned char msg
[65];
1533 if (ssif_info
->max_xmit_msg_size
<= 32)
1536 do_middle
= ssif_info
->max_xmit_msg_size
> 63;
1538 memset(msg
, 0, sizeof(msg
));
1539 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1540 msg
[1] = IPMI_GET_DEVICE_ID_CMD
;
1543 * The specification is all messed up dealing with sending
1544 * multi-part messages. Per what the specification says, it
1545 * is impossible to send a message that is a multiple of 32
1546 * bytes, except for 32 itself. It talks about a "start"
1547 * transaction (cmd=6) that must be 32 bytes, "middle"
1548 * transaction (cmd=7) that must be 32 bytes, and an "end"
1549 * transaction. The "end" transaction is shown as cmd=7 in
1550 * the text, but if that's the case there is no way to
1551 * differentiate between a middle and end part except the
1552 * length being less than 32. But there is a table at the far
1553 * end of the section (that I had never noticed until someone
1554 * pointed it out to me) that mentions it as cmd=8.
1556 * After some thought, I think the example is wrong and the
1557 * end transaction should be cmd=8. But some systems don't
1558 * implement cmd=8, they use a zero-length end transaction,
1559 * even though that violates the SMBus specification.
1561 * So, to work around this, this code tests if cmd=8 works.
1562 * If it does, then we use that. If not, it tests zero-
1563 * byte end transactions. If that works, good. If not,
1564 * we only allow 63-byte transactions max.
1567 ret
= start_multipart_test(client
, msg
, do_middle
);
1569 goto out_no_multi_part
;
1571 ret
= i2c_smbus_write_block_data(client
,
1572 SSIF_IPMI_MULTI_PART_REQUEST_END
,
1576 ret
= read_response(client
, resp
);
1579 /* End transactions work, we are good. */
1580 ssif_info
->cmd8_works
= true;
1584 ret
= start_multipart_test(client
, msg
, do_middle
);
1586 dev_err(&client
->dev
, "Second multipart test failed.\n");
1587 goto out_no_multi_part
;
1590 ret
= i2c_smbus_write_block_data(client
,
1591 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
,
1594 ret
= read_response(client
, resp
);
1596 /* Zero-size end parts work, use those. */
1599 /* Limit to 63 bytes and use a short middle command to mark the end. */
1600 if (ssif_info
->max_xmit_msg_size
> 63)
1601 ssif_info
->max_xmit_msg_size
= 63;
1605 ssif_info
->max_xmit_msg_size
= 32;
1610 * Global enables we care about.
1612 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1613 IPMI_BMC_EVT_MSG_INTR)
1615 static void ssif_remove_dup(struct i2c_client
*client
)
1617 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
1619 ipmi_unregister_smi(ssif_info
->intf
);
1623 static int ssif_add_infos(struct i2c_client
*client
)
1625 struct ssif_addr_info
*info
;
1627 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1630 info
->addr_src
= SI_ACPI
;
1631 info
->client
= client
;
1632 info
->adapter_name
= kstrdup(client
->adapter
->name
, GFP_KERNEL
);
1633 info
->binfo
.addr
= client
->addr
;
1634 list_add_tail(&info
->link
, &ssif_infos
);
1639 * Prefer ACPI over SMBIOS, if both are available.
1640 * So if we get an ACPI interface and have already registered a SMBIOS
1641 * interface at the same address, remove the SMBIOS and add the ACPI one.
1643 static int ssif_check_and_remove(struct i2c_client
*client
,
1644 struct ssif_info
*ssif_info
)
1646 struct ssif_addr_info
*info
;
1648 list_for_each_entry(info
, &ssif_infos
, link
) {
1651 if (!strcmp(info
->adapter_name
, client
->adapter
->name
) &&
1652 info
->binfo
.addr
== client
->addr
) {
1653 if (info
->addr_src
== SI_ACPI
)
1656 if (ssif_info
->addr_source
== SI_ACPI
&&
1657 info
->addr_src
== SI_SMBIOS
) {
1658 dev_info(&client
->dev
,
1659 "Removing %s-specified SSIF interface in favor of ACPI\n",
1660 ipmi_addr_src_to_str(info
->addr_src
));
1661 ssif_remove_dup(info
->client
);
1669 static int ssif_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1671 unsigned char msg
[3];
1672 unsigned char *resp
;
1673 struct ssif_info
*ssif_info
;
1678 struct ssif_addr_info
*addr_info
= NULL
;
1680 mutex_lock(&ssif_infos_mutex
);
1681 resp
= kmalloc(IPMI_MAX_MSG_LENGTH
, GFP_KERNEL
);
1683 mutex_unlock(&ssif_infos_mutex
);
1687 ssif_info
= kzalloc(sizeof(*ssif_info
), GFP_KERNEL
);
1690 mutex_unlock(&ssif_infos_mutex
);
1694 if (!check_acpi(ssif_info
, &client
->dev
)) {
1695 addr_info
= ssif_info_find(client
->addr
, client
->adapter
->name
,
1698 /* Must have come in through sysfs. */
1699 ssif_info
->addr_source
= SI_HOTMOD
;
1701 ssif_info
->addr_source
= addr_info
->addr_src
;
1702 ssif_info
->ssif_debug
= addr_info
->debug
;
1703 ssif_info
->addr_info
= addr_info
->addr_info
;
1704 addr_info
->client
= client
;
1705 slave_addr
= addr_info
->slave_addr
;
1709 rv
= ssif_check_and_remove(client
, ssif_info
);
1710 /* If rv is 0 and addr source is not SI_ACPI, continue probing */
1711 if (!rv
&& ssif_info
->addr_source
== SI_ACPI
) {
1712 rv
= ssif_add_infos(client
);
1714 dev_err(&client
->dev
, "Out of memory!, exiting ..\n");
1718 dev_err(&client
->dev
, "Not probing, Interface already present\n");
1722 slave_addr
= find_slave_address(client
, slave_addr
);
1724 dev_info(&client
->dev
,
1725 "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1726 ipmi_addr_src_to_str(ssif_info
->addr_source
),
1727 client
->addr
, client
->adapter
->name
, slave_addr
);
1729 ssif_info
->client
= client
;
1730 i2c_set_clientdata(client
, ssif_info
);
1732 /* Now check for system interface capabilities */
1733 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1734 msg
[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD
;
1735 msg
[2] = 0; /* SSIF */
1736 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1737 if (!rv
&& (len
>= 3) && (resp
[2] == 0)) {
1740 dev_dbg(&ssif_info
->client
->dev
,
1741 "SSIF info too short: %d\n", len
);
1745 /* Got a good SSIF response, handle it. */
1746 ssif_info
->max_xmit_msg_size
= resp
[5];
1747 ssif_info
->max_recv_msg_size
= resp
[6];
1748 ssif_info
->multi_support
= (resp
[4] >> 6) & 0x3;
1749 ssif_info
->supports_pec
= (resp
[4] >> 3) & 0x1;
1751 /* Sanitize the data */
1752 switch (ssif_info
->multi_support
) {
1754 if (ssif_info
->max_xmit_msg_size
> 32)
1755 ssif_info
->max_xmit_msg_size
= 32;
1756 if (ssif_info
->max_recv_msg_size
> 32)
1757 ssif_info
->max_recv_msg_size
= 32;
1760 case SSIF_MULTI_2_PART
:
1761 if (ssif_info
->max_xmit_msg_size
> 63)
1762 ssif_info
->max_xmit_msg_size
= 63;
1763 if (ssif_info
->max_recv_msg_size
> 62)
1764 ssif_info
->max_recv_msg_size
= 62;
1767 case SSIF_MULTI_n_PART
:
1768 /* We take whatever size given, but do some testing. */
1772 /* Data is not sane, just give up. */
1777 /* Assume no multi-part or PEC support */
1778 dev_info(&ssif_info
->client
->dev
,
1779 "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1782 ssif_info
->max_xmit_msg_size
= 32;
1783 ssif_info
->max_recv_msg_size
= 32;
1784 ssif_info
->multi_support
= SSIF_NO_MULTI
;
1785 ssif_info
->supports_pec
= 0;
1788 test_multipart_messages(client
, ssif_info
, resp
);
1790 /* Make sure the NMI timeout is cleared. */
1791 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1792 msg
[1] = IPMI_CLEAR_MSG_FLAGS_CMD
;
1793 msg
[2] = WDT_PRE_TIMEOUT_INT
;
1794 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1795 if (rv
|| (len
< 3) || (resp
[2] != 0))
1796 dev_warn(&ssif_info
->client
->dev
,
1797 "Unable to clear message flags: %d %d %2.2x\n",
1800 /* Attempt to enable the event buffer. */
1801 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1802 msg
[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD
;
1803 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1804 if (rv
|| (len
< 4) || (resp
[2] != 0)) {
1805 dev_warn(&ssif_info
->client
->dev
,
1806 "Error getting global enables: %d %d %2.2x\n",
1808 rv
= 0; /* Not fatal */
1812 ssif_info
->global_enables
= resp
[3];
1814 if (resp
[3] & IPMI_BMC_EVT_MSG_BUFF
) {
1815 ssif_info
->has_event_buffer
= true;
1816 /* buffer is already enabled, nothing to do. */
1820 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1821 msg
[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD
;
1822 msg
[2] = ssif_info
->global_enables
| IPMI_BMC_EVT_MSG_BUFF
;
1823 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1824 if (rv
|| (len
< 2)) {
1825 dev_warn(&ssif_info
->client
->dev
,
1826 "Error setting global enables: %d %d %2.2x\n",
1828 rv
= 0; /* Not fatal */
1833 /* A successful return means the event buffer is supported. */
1834 ssif_info
->has_event_buffer
= true;
1835 ssif_info
->global_enables
|= IPMI_BMC_EVT_MSG_BUFF
;
1838 /* Some systems don't behave well if you enable alerts. */
1842 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1843 msg
[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD
;
1844 msg
[2] = ssif_info
->global_enables
| IPMI_BMC_RCV_MSG_INTR
;
1845 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1846 if (rv
|| (len
< 2)) {
1847 dev_warn(&ssif_info
->client
->dev
,
1848 "Error setting global enables: %d %d %2.2x\n",
1850 rv
= 0; /* Not fatal */
1855 /* A successful return means the alert is supported. */
1856 ssif_info
->supports_alert
= true;
1857 ssif_info
->global_enables
|= IPMI_BMC_RCV_MSG_INTR
;
1861 if (ssif_dbg_probe
) {
1862 dev_dbg(&ssif_info
->client
->dev
,
1863 "%s: i2c_probe found device at i2c address %x\n",
1864 __func__
, client
->addr
);
1867 spin_lock_init(&ssif_info
->lock
);
1868 ssif_info
->ssif_state
= SSIF_NORMAL
;
1869 timer_setup(&ssif_info
->retry_timer
, retry_timeout
, 0);
1870 timer_setup(&ssif_info
->watch_timer
, watch_timeout
, 0);
1872 for (i
= 0; i
< SSIF_NUM_STATS
; i
++)
1873 atomic_set(&ssif_info
->stats
[i
], 0);
1875 if (ssif_info
->supports_pec
)
1876 ssif_info
->client
->flags
|= I2C_CLIENT_PEC
;
1878 ssif_info
->handlers
.owner
= THIS_MODULE
;
1879 ssif_info
->handlers
.start_processing
= ssif_start_processing
;
1880 ssif_info
->handlers
.shutdown
= shutdown_ssif
;
1881 ssif_info
->handlers
.get_smi_info
= get_smi_info
;
1882 ssif_info
->handlers
.sender
= sender
;
1883 ssif_info
->handlers
.request_events
= request_events
;
1884 ssif_info
->handlers
.set_need_watch
= ssif_set_need_watch
;
1887 unsigned int thread_num
;
1889 thread_num
= ((i2c_adapter_id(ssif_info
->client
->adapter
)
1891 ssif_info
->client
->addr
);
1892 init_completion(&ssif_info
->wake_thread
);
1893 ssif_info
->thread
= kthread_run(ipmi_ssif_thread
, ssif_info
,
1894 "kssif%4.4x", thread_num
);
1895 if (IS_ERR(ssif_info
->thread
)) {
1896 rv
= PTR_ERR(ssif_info
->thread
);
1897 dev_notice(&ssif_info
->client
->dev
,
1898 "Could not start kernel thread: error %d\n",
1904 dev_set_drvdata(&ssif_info
->client
->dev
, ssif_info
);
1905 rv
= device_add_group(&ssif_info
->client
->dev
,
1906 &ipmi_ssif_dev_attr_group
);
1908 dev_err(&ssif_info
->client
->dev
,
1909 "Unable to add device attributes: error %d\n",
1914 rv
= ipmi_register_smi(&ssif_info
->handlers
,
1916 &ssif_info
->client
->dev
,
1919 dev_err(&ssif_info
->client
->dev
,
1920 "Unable to register device: error %d\n", rv
);
1921 goto out_remove_attr
;
1927 addr_info
->client
= NULL
;
1929 dev_err(&ssif_info
->client
->dev
,
1930 "Unable to start IPMI SSIF: %d\n", rv
);
1934 mutex_unlock(&ssif_infos_mutex
);
1938 device_remove_group(&ssif_info
->client
->dev
, &ipmi_ssif_dev_attr_group
);
1939 dev_set_drvdata(&ssif_info
->client
->dev
, NULL
);
1943 static int ssif_adapter_handler(struct device
*adev
, void *opaque
)
1945 struct ssif_addr_info
*addr_info
= opaque
;
1947 if (adev
->type
!= &i2c_adapter_type
)
1950 addr_info
->added_client
= i2c_new_client_device(to_i2c_adapter(adev
),
1953 if (!addr_info
->adapter_name
)
1954 return 1; /* Only try the first I2C adapter by default. */
1958 static int new_ssif_client(int addr
, char *adapter_name
,
1959 int debug
, int slave_addr
,
1960 enum ipmi_addr_src addr_src
,
1963 struct ssif_addr_info
*addr_info
;
1966 mutex_lock(&ssif_infos_mutex
);
1967 if (ssif_info_find(addr
, adapter_name
, false)) {
1972 addr_info
= kzalloc(sizeof(*addr_info
), GFP_KERNEL
);
1979 addr_info
->adapter_name
= kstrdup(adapter_name
, GFP_KERNEL
);
1980 if (!addr_info
->adapter_name
) {
1987 strncpy(addr_info
->binfo
.type
, DEVICE_NAME
,
1988 sizeof(addr_info
->binfo
.type
));
1989 addr_info
->binfo
.addr
= addr
;
1990 addr_info
->binfo
.platform_data
= addr_info
;
1991 addr_info
->debug
= debug
;
1992 addr_info
->slave_addr
= slave_addr
;
1993 addr_info
->addr_src
= addr_src
;
1994 addr_info
->dev
= dev
;
1997 dev_set_drvdata(dev
, addr_info
);
1999 list_add_tail(&addr_info
->link
, &ssif_infos
);
2002 i2c_for_each_dev(addr_info
, ssif_adapter_handler
);
2003 /* Otherwise address list will get it */
2006 mutex_unlock(&ssif_infos_mutex
);
2010 static void free_ssif_clients(void)
2012 struct ssif_addr_info
*info
, *tmp
;
2014 mutex_lock(&ssif_infos_mutex
);
2015 list_for_each_entry_safe(info
, tmp
, &ssif_infos
, link
) {
2016 list_del(&info
->link
);
2017 kfree(info
->adapter_name
);
2020 mutex_unlock(&ssif_infos_mutex
);
2023 static unsigned short *ssif_address_list(void)
2025 struct ssif_addr_info
*info
;
2026 unsigned int count
= 0, i
= 0;
2027 unsigned short *address_list
;
2029 list_for_each_entry(info
, &ssif_infos
, link
)
2032 address_list
= kcalloc(count
+ 1, sizeof(*address_list
),
2037 list_for_each_entry(info
, &ssif_infos
, link
) {
2038 unsigned short addr
= info
->binfo
.addr
;
2041 for (j
= 0; j
< i
; j
++) {
2042 if (address_list
[j
] == addr
)
2046 if (j
== i
) /* Didn't find it in the list. */
2047 address_list
[i
++] = addr
;
2049 address_list
[i
] = I2C_CLIENT_END
;
2051 return address_list
;
2055 static const struct acpi_device_id ssif_acpi_match
[] = {
2059 MODULE_DEVICE_TABLE(acpi
, ssif_acpi_match
);
2063 static int dmi_ipmi_probe(struct platform_device
*pdev
)
2072 rv
= device_property_read_u16(&pdev
->dev
, "i2c-addr", &i2c_addr
);
2074 dev_warn(&pdev
->dev
, "No i2c-addr property\n");
2078 rv
= device_property_read_u8(&pdev
->dev
, "slave-addr", &slave_addr
);
2082 return new_ssif_client(i2c_addr
, NULL
, 0,
2083 slave_addr
, SI_SMBIOS
, &pdev
->dev
);
2086 static int dmi_ipmi_probe(struct platform_device
*pdev
)
2092 static const struct i2c_device_id ssif_id
[] = {
2096 MODULE_DEVICE_TABLE(i2c
, ssif_id
);
2098 static struct i2c_driver ssif_i2c_driver
= {
2099 .class = I2C_CLASS_HWMON
,
2103 .probe
= ssif_probe
,
2104 .remove
= ssif_remove
,
2105 .alert
= ssif_alert
,
2106 .id_table
= ssif_id
,
2107 .detect
= ssif_detect
2110 static int ssif_platform_probe(struct platform_device
*dev
)
2112 return dmi_ipmi_probe(dev
);
2115 static int ssif_platform_remove(struct platform_device
*dev
)
2117 struct ssif_addr_info
*addr_info
= dev_get_drvdata(&dev
->dev
);
2122 mutex_lock(&ssif_infos_mutex
);
2123 i2c_unregister_device(addr_info
->added_client
);
2125 list_del(&addr_info
->link
);
2127 mutex_unlock(&ssif_infos_mutex
);
2131 static const struct platform_device_id ssif_plat_ids
[] = {
2132 { "dmi-ipmi-ssif", 0 },
2136 static struct platform_driver ipmi_driver
= {
2138 .name
= DEVICE_NAME
,
2140 .probe
= ssif_platform_probe
,
2141 .remove
= ssif_platform_remove
,
2142 .id_table
= ssif_plat_ids
2145 static int init_ipmi_ssif(void)
2153 pr_info("IPMI SSIF Interface driver\n");
2155 /* build list for i2c from addr list */
2156 for (i
= 0; i
< num_addrs
; i
++) {
2157 rv
= new_ssif_client(addr
[i
], adapter_name
[i
],
2158 dbg
[i
], slave_addrs
[i
],
2159 SI_HARDCODED
, NULL
);
2161 pr_err("Couldn't add hardcoded device at addr 0x%x\n",
2166 ssif_i2c_driver
.driver
.acpi_match_table
=
2167 ACPI_PTR(ssif_acpi_match
);
2170 rv
= platform_driver_register(&ipmi_driver
);
2172 pr_err("Unable to register driver: %d\n", rv
);
2174 platform_registered
= true;
2177 ssif_i2c_driver
.address_list
= ssif_address_list();
2179 rv
= i2c_add_driver(&ssif_i2c_driver
);
2185 module_init(init_ipmi_ssif
);
2187 static void cleanup_ipmi_ssif(void)
2192 initialized
= false;
2194 i2c_del_driver(&ssif_i2c_driver
);
2196 kfree(ssif_i2c_driver
.address_list
);
2198 if (ssif_trydmi
&& platform_registered
)
2199 platform_driver_unregister(&ipmi_driver
);
2201 free_ssif_clients();
2203 module_exit(cleanup_ipmi_ssif
);
2205 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2206 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2207 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2208 MODULE_LICENSE("GPL");