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
)
317 spin_lock_irqsave(&ssif_info
->lock
, *flags
);
321 static void ipmi_ssif_unlock_cond(struct ssif_info
*ssif_info
,
322 unsigned long *flags
)
324 spin_unlock_irqrestore(&ssif_info
->lock
, *flags
);
327 static void deliver_recv_msg(struct ssif_info
*ssif_info
,
328 struct ipmi_smi_msg
*msg
)
330 if (msg
->rsp_size
< 0) {
331 return_hosed_msg(ssif_info
, msg
);
332 dev_err(&ssif_info
->client
->dev
,
333 "%s: Malformed message: rsp_size = %d\n",
334 __func__
, msg
->rsp_size
);
336 ipmi_smi_msg_received(ssif_info
->intf
, msg
);
340 static void return_hosed_msg(struct ssif_info
*ssif_info
,
341 struct ipmi_smi_msg
*msg
)
343 ssif_inc_stat(ssif_info
, hosed
);
345 /* Make it a response */
346 msg
->rsp
[0] = msg
->data
[0] | 4;
347 msg
->rsp
[1] = msg
->data
[1];
348 msg
->rsp
[2] = 0xFF; /* Unknown error. */
351 deliver_recv_msg(ssif_info
, msg
);
355 * Must be called with the message lock held. This will release the
356 * message lock. Note that the caller will check SSIF_IDLE and start a
357 * new operation, so there is no need to check for new messages to
360 static void start_clear_flags(struct ssif_info
*ssif_info
, unsigned long *flags
)
362 unsigned char msg
[3];
364 ssif_info
->msg_flags
&= ~WDT_PRE_TIMEOUT_INT
;
365 ssif_info
->ssif_state
= SSIF_CLEARING_FLAGS
;
366 ipmi_ssif_unlock_cond(ssif_info
, flags
);
368 /* Make sure the watchdog pre-timeout flag is not set at startup. */
369 msg
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
370 msg
[1] = IPMI_CLEAR_MSG_FLAGS_CMD
;
371 msg
[2] = WDT_PRE_TIMEOUT_INT
;
373 if (start_send(ssif_info
, msg
, 3) != 0) {
374 /* Error, just go to normal state. */
375 ssif_info
->ssif_state
= SSIF_NORMAL
;
379 static void start_flag_fetch(struct ssif_info
*ssif_info
, unsigned long *flags
)
383 ssif_info
->req_flags
= false;
384 ssif_info
->ssif_state
= SSIF_GETTING_FLAGS
;
385 ipmi_ssif_unlock_cond(ssif_info
, flags
);
387 mb
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
388 mb
[1] = IPMI_GET_MSG_FLAGS_CMD
;
389 if (start_send(ssif_info
, mb
, 2) != 0)
390 ssif_info
->ssif_state
= SSIF_NORMAL
;
393 static void check_start_send(struct ssif_info
*ssif_info
, unsigned long *flags
,
394 struct ipmi_smi_msg
*msg
)
396 if (start_send(ssif_info
, msg
->data
, msg
->data_size
) != 0) {
397 unsigned long oflags
;
399 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
400 ssif_info
->curr_msg
= NULL
;
401 ssif_info
->ssif_state
= SSIF_NORMAL
;
402 ipmi_ssif_unlock_cond(ssif_info
, flags
);
403 ipmi_free_smi_msg(msg
);
407 static void start_event_fetch(struct ssif_info
*ssif_info
, unsigned long *flags
)
409 struct ipmi_smi_msg
*msg
;
411 ssif_info
->req_events
= false;
413 msg
= ipmi_alloc_smi_msg();
415 ssif_info
->ssif_state
= SSIF_NORMAL
;
416 ipmi_ssif_unlock_cond(ssif_info
, flags
);
420 ssif_info
->curr_msg
= msg
;
421 ssif_info
->ssif_state
= SSIF_GETTING_EVENTS
;
422 ipmi_ssif_unlock_cond(ssif_info
, flags
);
424 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
425 msg
->data
[1] = IPMI_READ_EVENT_MSG_BUFFER_CMD
;
428 check_start_send(ssif_info
, flags
, msg
);
431 static void start_recv_msg_fetch(struct ssif_info
*ssif_info
,
432 unsigned long *flags
)
434 struct ipmi_smi_msg
*msg
;
436 msg
= ipmi_alloc_smi_msg();
438 ssif_info
->ssif_state
= SSIF_NORMAL
;
439 ipmi_ssif_unlock_cond(ssif_info
, flags
);
443 ssif_info
->curr_msg
= msg
;
444 ssif_info
->ssif_state
= SSIF_GETTING_MESSAGES
;
445 ipmi_ssif_unlock_cond(ssif_info
, flags
);
447 msg
->data
[0] = (IPMI_NETFN_APP_REQUEST
<< 2);
448 msg
->data
[1] = IPMI_GET_MSG_CMD
;
451 check_start_send(ssif_info
, flags
, msg
);
455 * Must be called with the message lock held. This will release the
456 * message lock. Note that the caller will check SSIF_IDLE and start a
457 * new operation, so there is no need to check for new messages to
460 static void handle_flags(struct ssif_info
*ssif_info
, unsigned long *flags
)
462 if (ssif_info
->msg_flags
& WDT_PRE_TIMEOUT_INT
) {
463 /* Watchdog pre-timeout */
464 ssif_inc_stat(ssif_info
, watchdog_pretimeouts
);
465 start_clear_flags(ssif_info
, flags
);
466 ipmi_smi_watchdog_pretimeout(ssif_info
->intf
);
467 } else if (ssif_info
->msg_flags
& RECEIVE_MSG_AVAIL
)
468 /* Messages available. */
469 start_recv_msg_fetch(ssif_info
, flags
);
470 else if (ssif_info
->msg_flags
& EVENT_MSG_BUFFER_FULL
)
471 /* Events available. */
472 start_event_fetch(ssif_info
, flags
);
474 ssif_info
->ssif_state
= SSIF_NORMAL
;
475 ipmi_ssif_unlock_cond(ssif_info
, flags
);
479 static int ipmi_ssif_thread(void *data
)
481 struct ssif_info
*ssif_info
= data
;
483 while (!kthread_should_stop()) {
486 /* Wait for something to do */
487 result
= wait_for_completion_interruptible(
488 &ssif_info
->wake_thread
);
489 if (ssif_info
->stopping
)
491 if (result
== -ERESTARTSYS
)
493 init_completion(&ssif_info
->wake_thread
);
495 if (ssif_info
->i2c_read_write
== I2C_SMBUS_WRITE
) {
496 result
= i2c_smbus_write_block_data(
497 ssif_info
->client
, ssif_info
->i2c_command
,
498 ssif_info
->i2c_data
[0],
499 ssif_info
->i2c_data
+ 1);
500 ssif_info
->done_handler(ssif_info
, result
, NULL
, 0);
502 result
= i2c_smbus_read_block_data(
503 ssif_info
->client
, ssif_info
->i2c_command
,
504 ssif_info
->i2c_data
);
506 ssif_info
->done_handler(ssif_info
, result
,
509 ssif_info
->done_handler(ssif_info
, 0,
518 static int ssif_i2c_send(struct ssif_info
*ssif_info
,
519 ssif_i2c_done handler
,
520 int read_write
, int command
,
521 unsigned char *data
, unsigned int size
)
523 ssif_info
->done_handler
= handler
;
525 ssif_info
->i2c_read_write
= read_write
;
526 ssif_info
->i2c_command
= command
;
527 ssif_info
->i2c_data
= data
;
528 ssif_info
->i2c_size
= size
;
529 complete(&ssif_info
->wake_thread
);
534 static void msg_done_handler(struct ssif_info
*ssif_info
, int result
,
535 unsigned char *data
, unsigned int len
);
537 static void start_get(struct ssif_info
*ssif_info
)
541 ssif_info
->rtc_us_timer
= 0;
542 ssif_info
->multi_pos
= 0;
544 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
, I2C_SMBUS_READ
,
546 ssif_info
->recv
, I2C_SMBUS_BLOCK_DATA
);
548 /* request failed, just return the error. */
549 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
550 dev_dbg(&ssif_info
->client
->dev
,
551 "Error from i2c_non_blocking_op(5)\n");
553 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
557 static void retry_timeout(struct timer_list
*t
)
559 struct ssif_info
*ssif_info
= from_timer(ssif_info
, t
, retry_timer
);
560 unsigned long oflags
, *flags
;
563 if (ssif_info
->stopping
)
566 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
567 waiting
= ssif_info
->waiting_alert
;
568 ssif_info
->waiting_alert
= false;
569 ipmi_ssif_unlock_cond(ssif_info
, flags
);
572 start_get(ssif_info
);
575 static void watch_timeout(struct timer_list
*t
)
577 struct ssif_info
*ssif_info
= from_timer(ssif_info
, t
, watch_timer
);
578 unsigned long oflags
, *flags
;
580 if (ssif_info
->stopping
)
583 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
584 if (ssif_info
->watch_timeout
) {
585 mod_timer(&ssif_info
->watch_timer
,
586 jiffies
+ ssif_info
->watch_timeout
);
587 if (SSIF_IDLE(ssif_info
)) {
588 start_flag_fetch(ssif_info
, flags
); /* Releases lock */
591 ssif_info
->req_flags
= true;
593 ipmi_ssif_unlock_cond(ssif_info
, flags
);
596 static void ssif_alert(struct i2c_client
*client
, enum i2c_alert_protocol type
,
599 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
600 unsigned long oflags
, *flags
;
603 if (type
!= I2C_PROTOCOL_SMBUS_ALERT
)
606 ssif_inc_stat(ssif_info
, alerts
);
608 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
609 if (ssif_info
->waiting_alert
) {
610 ssif_info
->waiting_alert
= false;
611 del_timer(&ssif_info
->retry_timer
);
613 } else if (ssif_info
->curr_msg
) {
614 ssif_info
->got_alert
= true;
616 ipmi_ssif_unlock_cond(ssif_info
, flags
);
618 start_get(ssif_info
);
621 static int start_resend(struct ssif_info
*ssif_info
);
623 static void msg_done_handler(struct ssif_info
*ssif_info
, int result
,
624 unsigned char *data
, unsigned int len
)
626 struct ipmi_smi_msg
*msg
;
627 unsigned long oflags
, *flags
;
631 * We are single-threaded here, so no need for a lock until we
632 * start messing with driver states or the queues.
636 ssif_info
->retries_left
--;
637 if (ssif_info
->retries_left
> 0) {
638 ssif_inc_stat(ssif_info
, receive_retries
);
640 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
641 ssif_info
->waiting_alert
= true;
642 ssif_info
->rtc_us_timer
= SSIF_MSG_USEC
;
643 if (!ssif_info
->stopping
)
644 mod_timer(&ssif_info
->retry_timer
,
645 jiffies
+ SSIF_MSG_JIFFIES
);
646 ipmi_ssif_unlock_cond(ssif_info
, flags
);
650 ssif_inc_stat(ssif_info
, receive_errors
);
652 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
653 dev_dbg(&ssif_info
->client
->dev
,
654 "%s: Error %d\n", __func__
, result
);
659 if ((len
> 1) && (ssif_info
->multi_pos
== 0)
660 && (data
[0] == 0x00) && (data
[1] == 0x01)) {
661 /* Start of multi-part read. Start the next transaction. */
664 ssif_inc_stat(ssif_info
, received_message_parts
);
666 /* Remove the multi-part read marker. */
669 for (i
= 0; i
< len
; i
++)
670 ssif_info
->data
[i
] = data
[i
];
671 ssif_info
->multi_len
= len
;
672 ssif_info
->multi_pos
= 1;
674 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
, I2C_SMBUS_READ
,
675 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE
,
676 ssif_info
->recv
, I2C_SMBUS_BLOCK_DATA
);
678 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
679 dev_dbg(&ssif_info
->client
->dev
,
680 "Error from i2c_non_blocking_op(1)\n");
685 } else if (ssif_info
->multi_pos
) {
686 /* Middle of multi-part read. Start the next transaction. */
688 unsigned char blocknum
;
692 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
693 dev_dbg(&ssif_info
->client
->dev
,
694 "Middle message with no data\n");
703 if (blocknum
!= 0xff && len
!= 31) {
704 /* All blocks but the last must have 31 data bytes. */
706 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
707 dev_dbg(&ssif_info
->client
->dev
,
708 "Received middle message <31\n");
713 if (ssif_info
->multi_len
+ len
> IPMI_MAX_MSG_LENGTH
) {
714 /* Received message too big, abort the operation. */
716 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
717 dev_dbg(&ssif_info
->client
->dev
,
718 "Received message too big\n");
723 for (i
= 0; i
< len
; i
++)
724 ssif_info
->data
[i
+ ssif_info
->multi_len
] = data
[i
];
725 ssif_info
->multi_len
+= len
;
726 if (blocknum
== 0xff) {
728 len
= ssif_info
->multi_len
;
729 data
= ssif_info
->data
;
730 } else if (blocknum
+ 1 != ssif_info
->multi_pos
) {
732 * Out of sequence block, just abort. Block
733 * numbers start at zero for the second block,
734 * but multi_pos starts at one, so the +1.
736 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
737 dev_dbg(&ssif_info
->client
->dev
,
738 "Received message out of sequence, expected %u, got %u\n",
739 ssif_info
->multi_pos
- 1, blocknum
);
742 ssif_inc_stat(ssif_info
, received_message_parts
);
744 ssif_info
->multi_pos
++;
746 rv
= ssif_i2c_send(ssif_info
, msg_done_handler
,
748 SSIF_IPMI_MULTI_PART_RESPONSE_MIDDLE
,
750 I2C_SMBUS_BLOCK_DATA
);
752 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
753 dev_dbg(&ssif_info
->client
->dev
,
754 "Error from ssif_i2c_send\n");
764 ssif_inc_stat(ssif_info
, receive_errors
);
766 ssif_inc_stat(ssif_info
, received_messages
);
767 ssif_inc_stat(ssif_info
, received_message_parts
);
770 if (ssif_info
->ssif_debug
& SSIF_DEBUG_STATE
)
771 dev_dbg(&ssif_info
->client
->dev
,
772 "DONE 1: state = %d, result=%d\n",
773 ssif_info
->ssif_state
, result
);
775 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
776 msg
= ssif_info
->curr_msg
;
779 if (msg
->rsp_size
> IPMI_MAX_MSG_LENGTH
)
780 msg
->rsp_size
= IPMI_MAX_MSG_LENGTH
;
781 memcpy(msg
->rsp
, data
, msg
->rsp_size
);
782 ssif_info
->curr_msg
= NULL
;
785 switch (ssif_info
->ssif_state
) {
787 ipmi_ssif_unlock_cond(ssif_info
, flags
);
792 return_hosed_msg(ssif_info
, msg
);
794 deliver_recv_msg(ssif_info
, msg
);
797 case SSIF_GETTING_FLAGS
:
798 /* We got the flags from the SSIF, now handle them. */
799 if ((result
< 0) || (len
< 4) || (data
[2] != 0)) {
801 * Error fetching flags, or invalid length,
802 * just give up for now.
804 ssif_info
->ssif_state
= SSIF_NORMAL
;
805 ipmi_ssif_unlock_cond(ssif_info
, flags
);
806 dev_warn(&ssif_info
->client
->dev
,
807 "Error getting flags: %d %d, %x\n",
808 result
, len
, (len
>= 3) ? data
[2] : 0);
809 } else if (data
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
810 || data
[1] != IPMI_GET_MSG_FLAGS_CMD
) {
812 * Don't abort here, maybe it was a queued
813 * response to a previous command.
815 ipmi_ssif_unlock_cond(ssif_info
, flags
);
816 dev_warn(&ssif_info
->client
->dev
,
817 "Invalid response getting flags: %x %x\n",
820 ssif_inc_stat(ssif_info
, flag_fetches
);
821 ssif_info
->msg_flags
= data
[3];
822 handle_flags(ssif_info
, flags
);
826 case SSIF_CLEARING_FLAGS
:
827 /* We cleared the flags. */
828 if ((result
< 0) || (len
< 3) || (data
[2] != 0)) {
829 /* Error clearing flags */
830 dev_warn(&ssif_info
->client
->dev
,
831 "Error clearing flags: %d %d, %x\n",
832 result
, len
, (len
>= 3) ? data
[2] : 0);
833 } else if (data
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
834 || data
[1] != IPMI_CLEAR_MSG_FLAGS_CMD
) {
835 dev_warn(&ssif_info
->client
->dev
,
836 "Invalid response clearing flags: %x %x\n",
839 ssif_info
->ssif_state
= SSIF_NORMAL
;
840 ipmi_ssif_unlock_cond(ssif_info
, flags
);
843 case SSIF_GETTING_EVENTS
:
844 if ((result
< 0) || (len
< 3) || (msg
->rsp
[2] != 0)) {
845 /* Error getting event, probably done. */
848 /* Take off the event flag. */
849 ssif_info
->msg_flags
&= ~EVENT_MSG_BUFFER_FULL
;
850 handle_flags(ssif_info
, flags
);
851 } else if (msg
->rsp
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
852 || msg
->rsp
[1] != IPMI_READ_EVENT_MSG_BUFFER_CMD
) {
853 dev_warn(&ssif_info
->client
->dev
,
854 "Invalid response getting events: %x %x\n",
855 msg
->rsp
[0], msg
->rsp
[1]);
857 /* Take off the event flag. */
858 ssif_info
->msg_flags
&= ~EVENT_MSG_BUFFER_FULL
;
859 handle_flags(ssif_info
, flags
);
861 handle_flags(ssif_info
, flags
);
862 ssif_inc_stat(ssif_info
, events
);
863 deliver_recv_msg(ssif_info
, msg
);
867 case SSIF_GETTING_MESSAGES
:
868 if ((result
< 0) || (len
< 3) || (msg
->rsp
[2] != 0)) {
869 /* Error getting event, probably done. */
872 /* Take off the msg flag. */
873 ssif_info
->msg_flags
&= ~RECEIVE_MSG_AVAIL
;
874 handle_flags(ssif_info
, flags
);
875 } else if (msg
->rsp
[0] != (IPMI_NETFN_APP_REQUEST
| 1) << 2
876 || msg
->rsp
[1] != IPMI_GET_MSG_CMD
) {
877 dev_warn(&ssif_info
->client
->dev
,
878 "Invalid response clearing flags: %x %x\n",
879 msg
->rsp
[0], msg
->rsp
[1]);
882 /* Take off the msg flag. */
883 ssif_info
->msg_flags
&= ~RECEIVE_MSG_AVAIL
;
884 handle_flags(ssif_info
, flags
);
886 ssif_inc_stat(ssif_info
, incoming_messages
);
887 handle_flags(ssif_info
, flags
);
888 deliver_recv_msg(ssif_info
, msg
);
893 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
894 if (SSIF_IDLE(ssif_info
) && !ssif_info
->stopping
) {
895 if (ssif_info
->req_events
)
896 start_event_fetch(ssif_info
, flags
);
897 else if (ssif_info
->req_flags
)
898 start_flag_fetch(ssif_info
, flags
);
900 start_next_msg(ssif_info
, flags
);
902 ipmi_ssif_unlock_cond(ssif_info
, flags
);
904 if (ssif_info
->ssif_debug
& SSIF_DEBUG_STATE
)
905 dev_dbg(&ssif_info
->client
->dev
,
906 "DONE 2: state = %d.\n", ssif_info
->ssif_state
);
909 static void msg_written_handler(struct ssif_info
*ssif_info
, int result
,
910 unsigned char *data
, unsigned int len
)
914 /* We are single-threaded here, so no need for a lock. */
916 ssif_info
->retries_left
--;
917 if (ssif_info
->retries_left
> 0) {
918 if (!start_resend(ssif_info
)) {
919 ssif_inc_stat(ssif_info
, send_retries
);
922 /* request failed, just return the error. */
923 ssif_inc_stat(ssif_info
, send_errors
);
925 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
926 dev_dbg(&ssif_info
->client
->dev
,
927 "%s: Out of retries\n", __func__
);
928 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
932 ssif_inc_stat(ssif_info
, send_errors
);
935 * Got an error on transmit, let the done routine
938 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
939 dev_dbg(&ssif_info
->client
->dev
,
940 "%s: Error %d\n", __func__
, result
);
942 msg_done_handler(ssif_info
, result
, NULL
, 0);
946 if (ssif_info
->multi_data
) {
948 * In the middle of a multi-data write. See the comment
949 * in the SSIF_MULTI_n_PART case in the probe function
950 * for details on the intricacies of this.
953 unsigned char *data_to_send
;
956 ssif_inc_stat(ssif_info
, sent_messages_parts
);
958 left
= ssif_info
->multi_len
- ssif_info
->multi_pos
;
963 ssif_info
->multi_data
[ssif_info
->multi_pos
] = to_write
;
964 data_to_send
= ssif_info
->multi_data
+ ssif_info
->multi_pos
;
965 ssif_info
->multi_pos
+= to_write
;
966 cmd
= SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
;
967 if (ssif_info
->cmd8_works
) {
968 if (left
== to_write
) {
969 cmd
= SSIF_IPMI_MULTI_PART_REQUEST_END
;
970 ssif_info
->multi_data
= NULL
;
972 } else if (to_write
< 32) {
973 ssif_info
->multi_data
= NULL
;
976 rv
= ssif_i2c_send(ssif_info
, msg_written_handler
,
977 I2C_SMBUS_WRITE
, cmd
,
978 data_to_send
, I2C_SMBUS_BLOCK_DATA
);
980 /* request failed, just return the error. */
981 ssif_inc_stat(ssif_info
, send_errors
);
983 if (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
)
984 dev_dbg(&ssif_info
->client
->dev
,
985 "Error from i2c_non_blocking_op(3)\n");
986 msg_done_handler(ssif_info
, -EIO
, NULL
, 0);
989 /* Ready to request the result. */
990 unsigned long oflags
, *flags
;
992 ssif_inc_stat(ssif_info
, sent_messages
);
993 ssif_inc_stat(ssif_info
, sent_messages_parts
);
995 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
996 if (ssif_info
->got_alert
) {
997 /* The result is already ready, just start it. */
998 ssif_info
->got_alert
= false;
999 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1000 start_get(ssif_info
);
1002 /* Wait a jiffie then request the next message */
1003 ssif_info
->waiting_alert
= true;
1004 ssif_info
->retries_left
= SSIF_RECV_RETRIES
;
1005 ssif_info
->rtc_us_timer
= SSIF_MSG_PART_USEC
;
1006 if (!ssif_info
->stopping
)
1007 mod_timer(&ssif_info
->retry_timer
,
1008 jiffies
+ SSIF_MSG_PART_JIFFIES
);
1009 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1014 static int start_resend(struct ssif_info
*ssif_info
)
1019 ssif_info
->got_alert
= false;
1021 if (ssif_info
->data_len
> 32) {
1022 command
= SSIF_IPMI_MULTI_PART_REQUEST_START
;
1023 ssif_info
->multi_data
= ssif_info
->data
;
1024 ssif_info
->multi_len
= ssif_info
->data_len
;
1026 * Subtle thing, this is 32, not 33, because we will
1027 * overwrite the thing at position 32 (which was just
1028 * transmitted) with the new length.
1030 ssif_info
->multi_pos
= 32;
1031 ssif_info
->data
[0] = 32;
1033 ssif_info
->multi_data
= NULL
;
1034 command
= SSIF_IPMI_REQUEST
;
1035 ssif_info
->data
[0] = ssif_info
->data_len
;
1038 rv
= ssif_i2c_send(ssif_info
, msg_written_handler
, I2C_SMBUS_WRITE
,
1039 command
, ssif_info
->data
, I2C_SMBUS_BLOCK_DATA
);
1040 if (rv
&& (ssif_info
->ssif_debug
& SSIF_DEBUG_MSG
))
1041 dev_dbg(&ssif_info
->client
->dev
,
1042 "Error from i2c_non_blocking_op(4)\n");
1046 static int start_send(struct ssif_info
*ssif_info
,
1047 unsigned char *data
,
1050 if (len
> IPMI_MAX_MSG_LENGTH
)
1052 if (len
> ssif_info
->max_xmit_msg_size
)
1055 ssif_info
->retries_left
= SSIF_SEND_RETRIES
;
1056 memcpy(ssif_info
->data
+ 1, data
, len
);
1057 ssif_info
->data_len
= len
;
1058 return start_resend(ssif_info
);
1061 /* Must be called with the message lock held. */
1062 static void start_next_msg(struct ssif_info
*ssif_info
, unsigned long *flags
)
1064 struct ipmi_smi_msg
*msg
;
1065 unsigned long oflags
;
1068 if (!SSIF_IDLE(ssif_info
)) {
1069 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1073 if (!ssif_info
->waiting_msg
) {
1074 ssif_info
->curr_msg
= NULL
;
1075 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1079 ssif_info
->curr_msg
= ssif_info
->waiting_msg
;
1080 ssif_info
->waiting_msg
= NULL
;
1081 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1082 rv
= start_send(ssif_info
,
1083 ssif_info
->curr_msg
->data
,
1084 ssif_info
->curr_msg
->data_size
);
1086 msg
= ssif_info
->curr_msg
;
1087 ssif_info
->curr_msg
= NULL
;
1088 return_hosed_msg(ssif_info
, msg
);
1089 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1095 static void sender(void *send_info
,
1096 struct ipmi_smi_msg
*msg
)
1098 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1099 unsigned long oflags
, *flags
;
1101 BUG_ON(ssif_info
->waiting_msg
);
1102 ssif_info
->waiting_msg
= msg
;
1104 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1105 start_next_msg(ssif_info
, flags
);
1107 if (ssif_info
->ssif_debug
& SSIF_DEBUG_TIMING
) {
1108 struct timespec64 t
;
1110 ktime_get_real_ts64(&t
);
1111 dev_dbg(&ssif_info
->client
->dev
,
1112 "**Enqueue %02x %02x: %lld.%6.6ld\n",
1113 msg
->data
[0], msg
->data
[1],
1114 (long long)t
.tv_sec
, (long)t
.tv_nsec
/ NSEC_PER_USEC
);
1118 static int get_smi_info(void *send_info
, struct ipmi_smi_info
*data
)
1120 struct ssif_info
*ssif_info
= send_info
;
1122 data
->addr_src
= ssif_info
->addr_source
;
1123 data
->dev
= &ssif_info
->client
->dev
;
1124 data
->addr_info
= ssif_info
->addr_info
;
1125 get_device(data
->dev
);
1131 * Upper layer wants us to request events.
1133 static void request_events(void *send_info
)
1135 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1136 unsigned long oflags
, *flags
;
1138 if (!ssif_info
->has_event_buffer
)
1141 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1142 ssif_info
->req_events
= true;
1143 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1147 * Upper layer is changing the flag saying whether we need to request
1148 * flags periodically or not.
1150 static void ssif_set_need_watch(void *send_info
, unsigned int watch_mask
)
1152 struct ssif_info
*ssif_info
= (struct ssif_info
*) send_info
;
1153 unsigned long oflags
, *flags
;
1156 if (watch_mask
& IPMI_WATCH_MASK_CHECK_MESSAGES
)
1157 timeout
= SSIF_WATCH_MSG_TIMEOUT
;
1158 else if (watch_mask
)
1159 timeout
= SSIF_WATCH_WATCHDOG_TIMEOUT
;
1161 flags
= ipmi_ssif_lock_cond(ssif_info
, &oflags
);
1162 if (timeout
!= ssif_info
->watch_timeout
) {
1163 ssif_info
->watch_timeout
= timeout
;
1164 if (ssif_info
->watch_timeout
)
1165 mod_timer(&ssif_info
->watch_timer
,
1166 jiffies
+ ssif_info
->watch_timeout
);
1168 ipmi_ssif_unlock_cond(ssif_info
, flags
);
1171 static int ssif_start_processing(void *send_info
,
1172 struct ipmi_smi
*intf
)
1174 struct ssif_info
*ssif_info
= send_info
;
1176 ssif_info
->intf
= intf
;
1181 #define MAX_SSIF_BMCS 4
1183 static unsigned short addr
[MAX_SSIF_BMCS
];
1184 static int num_addrs
;
1185 module_param_array(addr
, ushort
, &num_addrs
, 0);
1186 MODULE_PARM_DESC(addr
, "The addresses to scan for IPMI BMCs on the SSIFs.");
1188 static char *adapter_name
[MAX_SSIF_BMCS
];
1189 static int num_adapter_names
;
1190 module_param_array(adapter_name
, charp
, &num_adapter_names
, 0);
1191 MODULE_PARM_DESC(adapter_name
, "The string name of the I2C device that has the BMC. By default all devices are scanned.");
1193 static int slave_addrs
[MAX_SSIF_BMCS
];
1194 static int num_slave_addrs
;
1195 module_param_array(slave_addrs
, int, &num_slave_addrs
, 0);
1196 MODULE_PARM_DESC(slave_addrs
,
1197 "The default IPMB slave address for the controller.");
1199 static bool alerts_broken
;
1200 module_param(alerts_broken
, bool, 0);
1201 MODULE_PARM_DESC(alerts_broken
, "Don't enable alerts for the controller.");
1204 * Bit 0 enables message debugging, bit 1 enables state debugging, and
1205 * bit 2 enables timing debugging. This is an array indexed by
1208 static int dbg
[MAX_SSIF_BMCS
];
1210 module_param_array(dbg
, int, &num_dbg
, 0);
1211 MODULE_PARM_DESC(dbg
, "Turn on debugging.");
1213 static bool ssif_dbg_probe
;
1214 module_param_named(dbg_probe
, ssif_dbg_probe
, bool, 0);
1215 MODULE_PARM_DESC(dbg_probe
, "Enable debugging of probing of adapters.");
1217 static bool ssif_tryacpi
= true;
1218 module_param_named(tryacpi
, ssif_tryacpi
, bool, 0);
1219 MODULE_PARM_DESC(tryacpi
, "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
1221 static bool ssif_trydmi
= true;
1222 module_param_named(trydmi
, ssif_trydmi
, bool, 0);
1223 MODULE_PARM_DESC(trydmi
, "Setting this to zero will disable the default scan of the interfaces identified via DMI (SMBIOS)");
1225 static DEFINE_MUTEX(ssif_infos_mutex
);
1226 static LIST_HEAD(ssif_infos
);
1228 #define IPMI_SSIF_ATTR(name) \
1229 static ssize_t ipmi_##name##_show(struct device *dev, \
1230 struct device_attribute *attr, \
1233 struct ssif_info *ssif_info = dev_get_drvdata(dev); \
1235 return snprintf(buf, 10, "%u\n", ssif_get_stat(ssif_info, name));\
1237 static DEVICE_ATTR(name, S_IRUGO, ipmi_##name##_show, NULL)
1239 static ssize_t
ipmi_type_show(struct device
*dev
,
1240 struct device_attribute
*attr
,
1243 return snprintf(buf
, 10, "ssif\n");
1245 static DEVICE_ATTR(type
, S_IRUGO
, ipmi_type_show
, NULL
);
1247 IPMI_SSIF_ATTR(sent_messages
);
1248 IPMI_SSIF_ATTR(sent_messages_parts
);
1249 IPMI_SSIF_ATTR(send_retries
);
1250 IPMI_SSIF_ATTR(send_errors
);
1251 IPMI_SSIF_ATTR(received_messages
);
1252 IPMI_SSIF_ATTR(received_message_parts
);
1253 IPMI_SSIF_ATTR(receive_retries
);
1254 IPMI_SSIF_ATTR(receive_errors
);
1255 IPMI_SSIF_ATTR(flag_fetches
);
1256 IPMI_SSIF_ATTR(hosed
);
1257 IPMI_SSIF_ATTR(events
);
1258 IPMI_SSIF_ATTR(watchdog_pretimeouts
);
1259 IPMI_SSIF_ATTR(alerts
);
1261 static struct attribute
*ipmi_ssif_dev_attrs
[] = {
1262 &dev_attr_type
.attr
,
1263 &dev_attr_sent_messages
.attr
,
1264 &dev_attr_sent_messages_parts
.attr
,
1265 &dev_attr_send_retries
.attr
,
1266 &dev_attr_send_errors
.attr
,
1267 &dev_attr_received_messages
.attr
,
1268 &dev_attr_received_message_parts
.attr
,
1269 &dev_attr_receive_retries
.attr
,
1270 &dev_attr_receive_errors
.attr
,
1271 &dev_attr_flag_fetches
.attr
,
1272 &dev_attr_hosed
.attr
,
1273 &dev_attr_events
.attr
,
1274 &dev_attr_watchdog_pretimeouts
.attr
,
1275 &dev_attr_alerts
.attr
,
1279 static const struct attribute_group ipmi_ssif_dev_attr_group
= {
1280 .attrs
= ipmi_ssif_dev_attrs
,
1283 static void shutdown_ssif(void *send_info
)
1285 struct ssif_info
*ssif_info
= send_info
;
1287 device_remove_group(&ssif_info
->client
->dev
, &ipmi_ssif_dev_attr_group
);
1288 dev_set_drvdata(&ssif_info
->client
->dev
, NULL
);
1290 /* make sure the driver is not looking for flags any more. */
1291 while (ssif_info
->ssif_state
!= SSIF_NORMAL
)
1292 schedule_timeout(1);
1294 ssif_info
->stopping
= true;
1295 del_timer_sync(&ssif_info
->watch_timer
);
1296 del_timer_sync(&ssif_info
->retry_timer
);
1297 if (ssif_info
->thread
) {
1298 complete(&ssif_info
->wake_thread
);
1299 kthread_stop(ssif_info
->thread
);
1303 static int ssif_remove(struct i2c_client
*client
)
1305 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
1306 struct ssif_addr_info
*addr_info
;
1312 * After this point, we won't deliver anything asychronously
1313 * to the message handler. We can unregister ourself.
1315 ipmi_unregister_smi(ssif_info
->intf
);
1317 list_for_each_entry(addr_info
, &ssif_infos
, link
) {
1318 if (addr_info
->client
== client
) {
1319 addr_info
->client
= NULL
;
1329 static int read_response(struct i2c_client
*client
, unsigned char *resp
)
1331 int ret
= -ENODEV
, retry_cnt
= SSIF_RECV_RETRIES
;
1333 while (retry_cnt
> 0) {
1334 ret
= i2c_smbus_read_block_data(client
, SSIF_IPMI_RESPONSE
,
1338 msleep(SSIF_MSG_MSEC
);
1347 static int do_cmd(struct i2c_client
*client
, int len
, unsigned char *msg
,
1348 int *resp_len
, unsigned char *resp
)
1353 retry_cnt
= SSIF_SEND_RETRIES
;
1355 ret
= i2c_smbus_write_block_data(client
, SSIF_IPMI_REQUEST
, len
, msg
);
1363 ret
= read_response(client
, resp
);
1365 /* Validate that the response is correct. */
1367 (resp
[0] != (msg
[0] | (1 << 2))) ||
1368 (resp
[1] != msg
[1]))
1370 else if (ret
> IPMI_MAX_MSG_LENGTH
) {
1381 static int ssif_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
1383 unsigned char *resp
;
1384 unsigned char msg
[3];
1388 resp
= kmalloc(IPMI_MAX_MSG_LENGTH
, GFP_KERNEL
);
1392 /* Do a Get Device ID command, since it is required. */
1393 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1394 msg
[1] = IPMI_GET_DEVICE_ID_CMD
;
1395 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1399 strlcpy(info
->type
, DEVICE_NAME
, I2C_NAME_SIZE
);
1404 static int strcmp_nospace(char *s1
, char *s2
)
1406 while (*s1
&& *s2
) {
1407 while (isspace(*s1
))
1409 while (isspace(*s2
))
1421 static struct ssif_addr_info
*ssif_info_find(unsigned short addr
,
1423 bool match_null_name
)
1425 struct ssif_addr_info
*info
, *found
= NULL
;
1428 list_for_each_entry(info
, &ssif_infos
, link
) {
1429 if (info
->binfo
.addr
== addr
) {
1430 if (info
->addr_src
== SI_SMBIOS
)
1431 info
->adapter_name
= kstrdup(adapter_name
,
1434 if (info
->adapter_name
|| adapter_name
) {
1435 if (!info
->adapter_name
!= !adapter_name
) {
1436 /* One is NULL and one is not */
1440 strcmp_nospace(info
->adapter_name
,
1442 /* Names do not match */
1450 if (!found
&& match_null_name
) {
1451 /* Try to get an exact match first, then try with a NULL name */
1452 adapter_name
= NULL
;
1453 match_null_name
= false;
1460 static bool check_acpi(struct ssif_info
*ssif_info
, struct device
*dev
)
1463 acpi_handle acpi_handle
;
1465 acpi_handle
= ACPI_HANDLE(dev
);
1467 ssif_info
->addr_source
= SI_ACPI
;
1468 ssif_info
->addr_info
.acpi_info
.acpi_handle
= acpi_handle
;
1475 static int find_slave_address(struct i2c_client
*client
, int slave_addr
)
1477 #ifdef CONFIG_IPMI_DMI_DECODE
1479 slave_addr
= ipmi_dmi_get_slave_addr(
1481 i2c_adapter_id(client
->adapter
),
1488 static int start_multipart_test(struct i2c_client
*client
,
1489 unsigned char *msg
, bool do_middle
)
1491 int retry_cnt
= SSIF_SEND_RETRIES
, ret
;
1494 ret
= i2c_smbus_write_block_data(client
,
1495 SSIF_IPMI_MULTI_PART_REQUEST_START
,
1501 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");
1508 ret
= i2c_smbus_write_block_data(client
,
1509 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
,
1512 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");
1519 static void test_multipart_messages(struct i2c_client
*client
,
1520 struct ssif_info
*ssif_info
,
1521 unsigned char *resp
)
1523 unsigned char msg
[65];
1527 if (ssif_info
->max_xmit_msg_size
<= 32)
1530 do_middle
= ssif_info
->max_xmit_msg_size
> 63;
1532 memset(msg
, 0, sizeof(msg
));
1533 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1534 msg
[1] = IPMI_GET_DEVICE_ID_CMD
;
1537 * The specification is all messed up dealing with sending
1538 * multi-part messages. Per what the specification says, it
1539 * is impossible to send a message that is a multiple of 32
1540 * bytes, except for 32 itself. It talks about a "start"
1541 * transaction (cmd=6) that must be 32 bytes, "middle"
1542 * transaction (cmd=7) that must be 32 bytes, and an "end"
1543 * transaction. The "end" transaction is shown as cmd=7 in
1544 * the text, but if that's the case there is no way to
1545 * differentiate between a middle and end part except the
1546 * length being less than 32. But there is a table at the far
1547 * end of the section (that I had never noticed until someone
1548 * pointed it out to me) that mentions it as cmd=8.
1550 * After some thought, I think the example is wrong and the
1551 * end transaction should be cmd=8. But some systems don't
1552 * implement cmd=8, they use a zero-length end transaction,
1553 * even though that violates the SMBus specification.
1555 * So, to work around this, this code tests if cmd=8 works.
1556 * If it does, then we use that. If not, it tests zero-
1557 * byte end transactions. If that works, good. If not,
1558 * we only allow 63-byte transactions max.
1561 ret
= start_multipart_test(client
, msg
, do_middle
);
1563 goto out_no_multi_part
;
1565 ret
= i2c_smbus_write_block_data(client
,
1566 SSIF_IPMI_MULTI_PART_REQUEST_END
,
1570 ret
= read_response(client
, resp
);
1573 /* End transactions work, we are good. */
1574 ssif_info
->cmd8_works
= true;
1578 ret
= start_multipart_test(client
, msg
, do_middle
);
1580 dev_err(&client
->dev
, "Second multipart test failed.\n");
1581 goto out_no_multi_part
;
1584 ret
= i2c_smbus_write_block_data(client
,
1585 SSIF_IPMI_MULTI_PART_REQUEST_MIDDLE
,
1588 ret
= read_response(client
, resp
);
1590 /* Zero-size end parts work, use those. */
1593 /* Limit to 63 bytes and use a short middle command to mark the end. */
1594 if (ssif_info
->max_xmit_msg_size
> 63)
1595 ssif_info
->max_xmit_msg_size
= 63;
1599 ssif_info
->max_xmit_msg_size
= 32;
1604 * Global enables we care about.
1606 #define GLOBAL_ENABLES_MASK (IPMI_BMC_EVT_MSG_BUFF | IPMI_BMC_RCV_MSG_INTR | \
1607 IPMI_BMC_EVT_MSG_INTR)
1609 static void ssif_remove_dup(struct i2c_client
*client
)
1611 struct ssif_info
*ssif_info
= i2c_get_clientdata(client
);
1613 ipmi_unregister_smi(ssif_info
->intf
);
1617 static int ssif_add_infos(struct i2c_client
*client
)
1619 struct ssif_addr_info
*info
;
1621 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
1624 info
->addr_src
= SI_ACPI
;
1625 info
->client
= client
;
1626 info
->adapter_name
= kstrdup(client
->adapter
->name
, GFP_KERNEL
);
1627 info
->binfo
.addr
= client
->addr
;
1628 list_add_tail(&info
->link
, &ssif_infos
);
1633 * Prefer ACPI over SMBIOS, if both are available.
1634 * So if we get an ACPI interface and have already registered a SMBIOS
1635 * interface at the same address, remove the SMBIOS and add the ACPI one.
1637 static int ssif_check_and_remove(struct i2c_client
*client
,
1638 struct ssif_info
*ssif_info
)
1640 struct ssif_addr_info
*info
;
1642 list_for_each_entry(info
, &ssif_infos
, link
) {
1645 if (!strcmp(info
->adapter_name
, client
->adapter
->name
) &&
1646 info
->binfo
.addr
== client
->addr
) {
1647 if (info
->addr_src
== SI_ACPI
)
1650 if (ssif_info
->addr_source
== SI_ACPI
&&
1651 info
->addr_src
== SI_SMBIOS
) {
1652 dev_info(&client
->dev
,
1653 "Removing %s-specified SSIF interface in favor of ACPI\n",
1654 ipmi_addr_src_to_str(info
->addr_src
));
1655 ssif_remove_dup(info
->client
);
1663 static int ssif_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1665 unsigned char msg
[3];
1666 unsigned char *resp
;
1667 struct ssif_info
*ssif_info
;
1672 struct ssif_addr_info
*addr_info
= NULL
;
1674 mutex_lock(&ssif_infos_mutex
);
1675 resp
= kmalloc(IPMI_MAX_MSG_LENGTH
, GFP_KERNEL
);
1677 mutex_unlock(&ssif_infos_mutex
);
1681 ssif_info
= kzalloc(sizeof(*ssif_info
), GFP_KERNEL
);
1684 mutex_unlock(&ssif_infos_mutex
);
1688 if (!check_acpi(ssif_info
, &client
->dev
)) {
1689 addr_info
= ssif_info_find(client
->addr
, client
->adapter
->name
,
1692 /* Must have come in through sysfs. */
1693 ssif_info
->addr_source
= SI_HOTMOD
;
1695 ssif_info
->addr_source
= addr_info
->addr_src
;
1696 ssif_info
->ssif_debug
= addr_info
->debug
;
1697 ssif_info
->addr_info
= addr_info
->addr_info
;
1698 addr_info
->client
= client
;
1699 slave_addr
= addr_info
->slave_addr
;
1703 rv
= ssif_check_and_remove(client
, ssif_info
);
1704 /* If rv is 0 and addr source is not SI_ACPI, continue probing */
1705 if (!rv
&& ssif_info
->addr_source
== SI_ACPI
) {
1706 rv
= ssif_add_infos(client
);
1708 dev_err(&client
->dev
, "Out of memory!, exiting ..\n");
1712 dev_err(&client
->dev
, "Not probing, Interface already present\n");
1716 slave_addr
= find_slave_address(client
, slave_addr
);
1718 dev_info(&client
->dev
,
1719 "Trying %s-specified SSIF interface at i2c address 0x%x, adapter %s, slave address 0x%x\n",
1720 ipmi_addr_src_to_str(ssif_info
->addr_source
),
1721 client
->addr
, client
->adapter
->name
, slave_addr
);
1723 ssif_info
->client
= client
;
1724 i2c_set_clientdata(client
, ssif_info
);
1726 /* Now check for system interface capabilities */
1727 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1728 msg
[1] = IPMI_GET_SYSTEM_INTERFACE_CAPABILITIES_CMD
;
1729 msg
[2] = 0; /* SSIF */
1730 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1731 if (!rv
&& (len
>= 3) && (resp
[2] == 0)) {
1734 dev_dbg(&ssif_info
->client
->dev
,
1735 "SSIF info too short: %d\n", len
);
1739 /* Got a good SSIF response, handle it. */
1740 ssif_info
->max_xmit_msg_size
= resp
[5];
1741 ssif_info
->max_recv_msg_size
= resp
[6];
1742 ssif_info
->multi_support
= (resp
[4] >> 6) & 0x3;
1743 ssif_info
->supports_pec
= (resp
[4] >> 3) & 0x1;
1745 /* Sanitize the data */
1746 switch (ssif_info
->multi_support
) {
1748 if (ssif_info
->max_xmit_msg_size
> 32)
1749 ssif_info
->max_xmit_msg_size
= 32;
1750 if (ssif_info
->max_recv_msg_size
> 32)
1751 ssif_info
->max_recv_msg_size
= 32;
1754 case SSIF_MULTI_2_PART
:
1755 if (ssif_info
->max_xmit_msg_size
> 63)
1756 ssif_info
->max_xmit_msg_size
= 63;
1757 if (ssif_info
->max_recv_msg_size
> 62)
1758 ssif_info
->max_recv_msg_size
= 62;
1761 case SSIF_MULTI_n_PART
:
1762 /* We take whatever size given, but do some testing. */
1766 /* Data is not sane, just give up. */
1771 /* Assume no multi-part or PEC support */
1772 dev_info(&ssif_info
->client
->dev
,
1773 "Error fetching SSIF: %d %d %2.2x, your system probably doesn't support this command so using defaults\n",
1776 ssif_info
->max_xmit_msg_size
= 32;
1777 ssif_info
->max_recv_msg_size
= 32;
1778 ssif_info
->multi_support
= SSIF_NO_MULTI
;
1779 ssif_info
->supports_pec
= 0;
1782 test_multipart_messages(client
, ssif_info
, resp
);
1784 /* Make sure the NMI timeout is cleared. */
1785 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1786 msg
[1] = IPMI_CLEAR_MSG_FLAGS_CMD
;
1787 msg
[2] = WDT_PRE_TIMEOUT_INT
;
1788 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1789 if (rv
|| (len
< 3) || (resp
[2] != 0))
1790 dev_warn(&ssif_info
->client
->dev
,
1791 "Unable to clear message flags: %d %d %2.2x\n",
1794 /* Attempt to enable the event buffer. */
1795 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1796 msg
[1] = IPMI_GET_BMC_GLOBAL_ENABLES_CMD
;
1797 rv
= do_cmd(client
, 2, msg
, &len
, resp
);
1798 if (rv
|| (len
< 4) || (resp
[2] != 0)) {
1799 dev_warn(&ssif_info
->client
->dev
,
1800 "Error getting global enables: %d %d %2.2x\n",
1802 rv
= 0; /* Not fatal */
1806 ssif_info
->global_enables
= resp
[3];
1808 if (resp
[3] & IPMI_BMC_EVT_MSG_BUFF
) {
1809 ssif_info
->has_event_buffer
= true;
1810 /* buffer is already enabled, nothing to do. */
1814 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1815 msg
[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD
;
1816 msg
[2] = ssif_info
->global_enables
| IPMI_BMC_EVT_MSG_BUFF
;
1817 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1818 if (rv
|| (len
< 2)) {
1819 dev_warn(&ssif_info
->client
->dev
,
1820 "Error setting global enables: %d %d %2.2x\n",
1822 rv
= 0; /* Not fatal */
1827 /* A successful return means the event buffer is supported. */
1828 ssif_info
->has_event_buffer
= true;
1829 ssif_info
->global_enables
|= IPMI_BMC_EVT_MSG_BUFF
;
1832 /* Some systems don't behave well if you enable alerts. */
1836 msg
[0] = IPMI_NETFN_APP_REQUEST
<< 2;
1837 msg
[1] = IPMI_SET_BMC_GLOBAL_ENABLES_CMD
;
1838 msg
[2] = ssif_info
->global_enables
| IPMI_BMC_RCV_MSG_INTR
;
1839 rv
= do_cmd(client
, 3, msg
, &len
, resp
);
1840 if (rv
|| (len
< 2)) {
1841 dev_warn(&ssif_info
->client
->dev
,
1842 "Error setting global enables: %d %d %2.2x\n",
1844 rv
= 0; /* Not fatal */
1849 /* A successful return means the alert is supported. */
1850 ssif_info
->supports_alert
= true;
1851 ssif_info
->global_enables
|= IPMI_BMC_RCV_MSG_INTR
;
1855 if (ssif_dbg_probe
) {
1856 dev_dbg(&ssif_info
->client
->dev
,
1857 "%s: i2c_probe found device at i2c address %x\n",
1858 __func__
, client
->addr
);
1861 spin_lock_init(&ssif_info
->lock
);
1862 ssif_info
->ssif_state
= SSIF_NORMAL
;
1863 timer_setup(&ssif_info
->retry_timer
, retry_timeout
, 0);
1864 timer_setup(&ssif_info
->watch_timer
, watch_timeout
, 0);
1866 for (i
= 0; i
< SSIF_NUM_STATS
; i
++)
1867 atomic_set(&ssif_info
->stats
[i
], 0);
1869 if (ssif_info
->supports_pec
)
1870 ssif_info
->client
->flags
|= I2C_CLIENT_PEC
;
1872 ssif_info
->handlers
.owner
= THIS_MODULE
;
1873 ssif_info
->handlers
.start_processing
= ssif_start_processing
;
1874 ssif_info
->handlers
.shutdown
= shutdown_ssif
;
1875 ssif_info
->handlers
.get_smi_info
= get_smi_info
;
1876 ssif_info
->handlers
.sender
= sender
;
1877 ssif_info
->handlers
.request_events
= request_events
;
1878 ssif_info
->handlers
.set_need_watch
= ssif_set_need_watch
;
1881 unsigned int thread_num
;
1883 thread_num
= ((i2c_adapter_id(ssif_info
->client
->adapter
)
1885 ssif_info
->client
->addr
);
1886 init_completion(&ssif_info
->wake_thread
);
1887 ssif_info
->thread
= kthread_run(ipmi_ssif_thread
, ssif_info
,
1888 "kssif%4.4x", thread_num
);
1889 if (IS_ERR(ssif_info
->thread
)) {
1890 rv
= PTR_ERR(ssif_info
->thread
);
1891 dev_notice(&ssif_info
->client
->dev
,
1892 "Could not start kernel thread: error %d\n",
1898 dev_set_drvdata(&ssif_info
->client
->dev
, ssif_info
);
1899 rv
= device_add_group(&ssif_info
->client
->dev
,
1900 &ipmi_ssif_dev_attr_group
);
1902 dev_err(&ssif_info
->client
->dev
,
1903 "Unable to add device attributes: error %d\n",
1908 rv
= ipmi_register_smi(&ssif_info
->handlers
,
1910 &ssif_info
->client
->dev
,
1913 dev_err(&ssif_info
->client
->dev
,
1914 "Unable to register device: error %d\n", rv
);
1915 goto out_remove_attr
;
1921 addr_info
->client
= NULL
;
1923 dev_err(&ssif_info
->client
->dev
,
1924 "Unable to start IPMI SSIF: %d\n", rv
);
1928 mutex_unlock(&ssif_infos_mutex
);
1932 device_remove_group(&ssif_info
->client
->dev
, &ipmi_ssif_dev_attr_group
);
1933 dev_set_drvdata(&ssif_info
->client
->dev
, NULL
);
1937 static int ssif_adapter_handler(struct device
*adev
, void *opaque
)
1939 struct ssif_addr_info
*addr_info
= opaque
;
1941 if (adev
->type
!= &i2c_adapter_type
)
1944 addr_info
->added_client
= i2c_new_device(to_i2c_adapter(adev
),
1947 if (!addr_info
->adapter_name
)
1948 return 1; /* Only try the first I2C adapter by default. */
1952 static int new_ssif_client(int addr
, char *adapter_name
,
1953 int debug
, int slave_addr
,
1954 enum ipmi_addr_src addr_src
,
1957 struct ssif_addr_info
*addr_info
;
1960 mutex_lock(&ssif_infos_mutex
);
1961 if (ssif_info_find(addr
, adapter_name
, false)) {
1966 addr_info
= kzalloc(sizeof(*addr_info
), GFP_KERNEL
);
1973 addr_info
->adapter_name
= kstrdup(adapter_name
, GFP_KERNEL
);
1974 if (!addr_info
->adapter_name
) {
1981 strncpy(addr_info
->binfo
.type
, DEVICE_NAME
,
1982 sizeof(addr_info
->binfo
.type
));
1983 addr_info
->binfo
.addr
= addr
;
1984 addr_info
->binfo
.platform_data
= addr_info
;
1985 addr_info
->debug
= debug
;
1986 addr_info
->slave_addr
= slave_addr
;
1987 addr_info
->addr_src
= addr_src
;
1988 addr_info
->dev
= dev
;
1991 dev_set_drvdata(dev
, addr_info
);
1993 list_add_tail(&addr_info
->link
, &ssif_infos
);
1996 i2c_for_each_dev(addr_info
, ssif_adapter_handler
);
1997 /* Otherwise address list will get it */
2000 mutex_unlock(&ssif_infos_mutex
);
2004 static void free_ssif_clients(void)
2006 struct ssif_addr_info
*info
, *tmp
;
2008 mutex_lock(&ssif_infos_mutex
);
2009 list_for_each_entry_safe(info
, tmp
, &ssif_infos
, link
) {
2010 list_del(&info
->link
);
2011 kfree(info
->adapter_name
);
2014 mutex_unlock(&ssif_infos_mutex
);
2017 static unsigned short *ssif_address_list(void)
2019 struct ssif_addr_info
*info
;
2020 unsigned int count
= 0, i
= 0;
2021 unsigned short *address_list
;
2023 list_for_each_entry(info
, &ssif_infos
, link
)
2026 address_list
= kcalloc(count
+ 1, sizeof(*address_list
),
2031 list_for_each_entry(info
, &ssif_infos
, link
) {
2032 unsigned short addr
= info
->binfo
.addr
;
2035 for (j
= 0; j
< i
; j
++) {
2036 if (address_list
[j
] == addr
)
2040 if (j
== i
) /* Didn't find it in the list. */
2041 address_list
[i
++] = addr
;
2043 address_list
[i
] = I2C_CLIENT_END
;
2045 return address_list
;
2049 static const struct acpi_device_id ssif_acpi_match
[] = {
2053 MODULE_DEVICE_TABLE(acpi
, ssif_acpi_match
);
2057 static int dmi_ipmi_probe(struct platform_device
*pdev
)
2066 rv
= device_property_read_u16(&pdev
->dev
, "i2c-addr", &i2c_addr
);
2068 dev_warn(&pdev
->dev
, "No i2c-addr property\n");
2072 rv
= device_property_read_u8(&pdev
->dev
, "slave-addr", &slave_addr
);
2076 return new_ssif_client(i2c_addr
, NULL
, 0,
2077 slave_addr
, SI_SMBIOS
, &pdev
->dev
);
2080 static int dmi_ipmi_probe(struct platform_device
*pdev
)
2086 static const struct i2c_device_id ssif_id
[] = {
2090 MODULE_DEVICE_TABLE(i2c
, ssif_id
);
2092 static struct i2c_driver ssif_i2c_driver
= {
2093 .class = I2C_CLASS_HWMON
,
2097 .probe
= ssif_probe
,
2098 .remove
= ssif_remove
,
2099 .alert
= ssif_alert
,
2100 .id_table
= ssif_id
,
2101 .detect
= ssif_detect
2104 static int ssif_platform_probe(struct platform_device
*dev
)
2106 return dmi_ipmi_probe(dev
);
2109 static int ssif_platform_remove(struct platform_device
*dev
)
2111 struct ssif_addr_info
*addr_info
= dev_get_drvdata(&dev
->dev
);
2116 mutex_lock(&ssif_infos_mutex
);
2117 i2c_unregister_device(addr_info
->added_client
);
2119 list_del(&addr_info
->link
);
2121 mutex_unlock(&ssif_infos_mutex
);
2125 static const struct platform_device_id ssif_plat_ids
[] = {
2126 { "dmi-ipmi-ssif", 0 },
2130 static struct platform_driver ipmi_driver
= {
2132 .name
= DEVICE_NAME
,
2134 .probe
= ssif_platform_probe
,
2135 .remove
= ssif_platform_remove
,
2136 .id_table
= ssif_plat_ids
2139 static int init_ipmi_ssif(void)
2147 pr_info("IPMI SSIF Interface driver\n");
2149 /* build list for i2c from addr list */
2150 for (i
= 0; i
< num_addrs
; i
++) {
2151 rv
= new_ssif_client(addr
[i
], adapter_name
[i
],
2152 dbg
[i
], slave_addrs
[i
],
2153 SI_HARDCODED
, NULL
);
2155 pr_err("Couldn't add hardcoded device at addr 0x%x\n",
2160 ssif_i2c_driver
.driver
.acpi_match_table
=
2161 ACPI_PTR(ssif_acpi_match
);
2164 rv
= platform_driver_register(&ipmi_driver
);
2166 pr_err("Unable to register driver: %d\n", rv
);
2168 platform_registered
= true;
2171 ssif_i2c_driver
.address_list
= ssif_address_list();
2173 rv
= i2c_add_driver(&ssif_i2c_driver
);
2179 module_init(init_ipmi_ssif
);
2181 static void cleanup_ipmi_ssif(void)
2186 initialized
= false;
2188 i2c_del_driver(&ssif_i2c_driver
);
2190 kfree(ssif_i2c_driver
.address_list
);
2192 if (ssif_trydmi
&& platform_registered
)
2193 platform_driver_unregister(&ipmi_driver
);
2195 free_ssif_clients();
2197 module_exit(cleanup_ipmi_ssif
);
2199 MODULE_ALIAS("platform:dmi-ipmi-ssif");
2200 MODULE_AUTHOR("Todd C Davis <todd.c.davis@intel.com>, Corey Minyard <minyard@acm.org>");
2201 MODULE_DESCRIPTION("IPMI driver for management controllers on a SMBus");
2202 MODULE_LICENSE("GPL");